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 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
195 "standard::is-hidden,standard::is-backup,standard::size," \
196 "standard::content-type,time::modified"
198 /* the first 3 must be these due to settings caching sort column */
203 MODEL_COL_NAME_COLLATED,
207 MODEL_COL_MTIME_TEXT,
209 MODEL_COL_NUM_COLUMNS
212 /* Column numbers for the search model.
213 * Keep this in sync with search_setup_model()
216 SEARCH_MODEL_COL_CANCELLABLE = MODEL_COL_NUM_COLUMNS,
217 SEARCH_MODEL_COL_MIME_TYPE,
218 SEARCH_MODEL_COL_NUM_COLUMNS
222 RECENT_MODEL_COL_CANCELLABLE = MODEL_COL_NUM_COLUMNS,
223 RECENT_MODEL_COL_INFO,
224 RECENT_MODEL_COL_NUM_COLUMNS
227 /* Identifiers for target types */
233 search_is_possible (GtkFileChooserDefault *impl)
235 if (impl->search_engine == NULL)
236 impl->search_engine = _gtk_search_engine_new ();
238 return impl->search_engine != NULL;
241 /* Interesting places in the shortcuts bar */
245 SHORTCUTS_RECENT_SEPARATOR,
250 SHORTCUTS_BOOKMARKS_SEPARATOR,
252 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
253 SHORTCUTS_CURRENT_FOLDER
256 /* Icon size for if we can't get it from the theme */
257 #define FALLBACK_ICON_SIZE 16
259 #define PREVIEW_HBOX_SPACING 12
263 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
264 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
266 static GObject* gtk_file_chooser_default_constructor (GType type,
267 guint n_construct_properties,
268 GObjectConstructParam *construct_params);
269 static void gtk_file_chooser_default_finalize (GObject *object);
270 static void gtk_file_chooser_default_set_property (GObject *object,
274 static void gtk_file_chooser_default_get_property (GObject *object,
278 static void gtk_file_chooser_default_dispose (GObject *object);
279 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
280 static void gtk_file_chooser_default_realize (GtkWidget *widget);
281 static void gtk_file_chooser_default_map (GtkWidget *widget);
282 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
283 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
284 GtkWidget *previous_toplevel);
285 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
286 GtkStyle *previous_style);
287 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
288 GdkScreen *previous_screen);
289 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
290 GtkAllocation *allocation);
292 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
295 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
298 gboolean clear_entry,
300 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
301 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
303 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
306 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
308 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
309 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
310 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
311 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
312 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
313 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
314 GtkFileFilter *filter);
315 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
316 GtkFileFilter *filter);
317 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
318 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
321 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
324 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
326 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
328 gint *default_height);
329 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
330 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
332 static void location_popup_handler (GtkFileChooserDefault *impl,
334 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
335 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
336 static void up_folder_handler (GtkFileChooserDefault *impl);
337 static void down_folder_handler (GtkFileChooserDefault *impl);
338 static void home_folder_handler (GtkFileChooserDefault *impl);
339 static void desktop_folder_handler (GtkFileChooserDefault *impl);
340 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
341 gint bookmark_index);
342 static void show_hidden_handler (GtkFileChooserDefault *impl);
343 static void search_shortcut_handler (GtkFileChooserDefault *impl);
344 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
345 static void update_appearance (GtkFileChooserDefault *impl);
347 static void set_current_filter (GtkFileChooserDefault *impl,
348 GtkFileFilter *filter);
349 static void check_preview_change (GtkFileChooserDefault *impl);
351 static void filter_combo_changed (GtkComboBox *combo_box,
352 GtkFileChooserDefault *impl);
354 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
356 GtkFileChooserDefault *impl);
358 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
361 gboolean path_currently_selected,
363 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
365 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
367 static int shortcuts_get_index (GtkFileChooserDefault *impl,
368 ShortcutsIndex where);
369 static int shortcut_find_position (GtkFileChooserDefault *impl,
372 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
374 static gboolean list_select_func (GtkTreeSelection *selection,
377 gboolean path_currently_selected,
380 static void list_selection_changed (GtkTreeSelection *tree_selection,
381 GtkFileChooserDefault *impl);
382 static void list_row_activated (GtkTreeView *tree_view,
384 GtkTreeViewColumn *column,
385 GtkFileChooserDefault *impl);
387 static void select_func (GtkFileSystemModel *model,
392 static void path_bar_clicked (GtkPathBar *path_bar,
395 gboolean child_is_hidden,
396 GtkFileChooserDefault *impl);
398 static void add_bookmark_button_clicked_cb (GtkButton *button,
399 GtkFileChooserDefault *impl);
400 static void remove_bookmark_button_clicked_cb (GtkButton *button,
401 GtkFileChooserDefault *impl);
402 static void save_folder_combo_changed_cb (GtkComboBox *combo,
403 GtkFileChooserDefault *impl);
405 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
407 static void load_remove_timer (GtkFileChooserDefault *impl);
408 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
410 static void location_button_toggled_cb (GtkToggleButton *toggle,
411 GtkFileChooserDefault *impl);
412 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
414 static void search_stop_searching (GtkFileChooserDefault *impl,
415 gboolean remove_query);
416 static void search_clear_model_row (GtkTreeModel *model,
418 static void search_clear_model (GtkFileChooserDefault *impl,
419 gboolean remove_from_treeview);
420 static gboolean search_should_respond (GtkFileChooserDefault *impl);
421 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
422 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
423 static void search_entry_activate_cb (GtkEntry *entry,
425 static void settings_load (GtkFileChooserDefault *impl);
426 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
427 GtkTreeIter *child_iter,
430 static void recent_stop_loading (GtkFileChooserDefault *impl);
431 static void recent_clear_model (GtkFileChooserDefault *impl,
432 gboolean remove_from_treeview);
433 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
434 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
435 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
436 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
437 GtkTreeIter *child_iter,
439 static void set_file_system_backend (GtkFileChooserDefault *impl);
440 static void unset_file_system_backend (GtkFileChooserDefault *impl);
446 /* Drag and drop interface declarations */
449 GtkTreeModelFilter parent;
451 GtkFileChooserDefault *impl;
452 } ShortcutsPaneModelFilter;
455 GtkTreeModelFilterClass parent_class;
456 } ShortcutsPaneModelFilterClass;
458 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
459 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
461 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
463 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
464 _shortcuts_pane_model_filter,
465 GTK_TYPE_TREE_MODEL_FILTER,
466 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
467 shortcuts_pane_model_filter_drag_source_iface_init))
469 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
470 GtkTreeModel *child_model,
475 GtkTreeModelSort parent;
477 GtkFileChooserDefault *impl;
481 GtkTreeModelSortClass parent_class;
482 } RecentModelSortClass;
484 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
485 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
487 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
489 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
491 GTK_TYPE_TREE_MODEL_SORT,
492 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
493 recent_model_sort_drag_source_iface_init));
495 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
496 GtkTreeModel *child_model);
500 GtkTreeModelSort parent;
502 GtkFileChooserDefault *impl;
506 GtkTreeModelSortClass parent_class;
507 } SearchModelSortClass;
509 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
510 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
512 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
514 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
516 GTK_TYPE_TREE_MODEL_SORT,
517 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
518 search_model_sort_drag_source_iface_init));
520 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
521 GtkTreeModel *child_model);
525 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
526 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
527 gtk_file_chooser_default_iface_init)
528 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
529 gtk_file_chooser_embed_default_iface_init));
532 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
534 static const guint quick_bookmark_keyvals[10] = {
535 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
537 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
538 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
539 GtkBindingSet *binding_set;
542 gobject_class->finalize = gtk_file_chooser_default_finalize;
543 gobject_class->constructor = gtk_file_chooser_default_constructor;
544 gobject_class->set_property = gtk_file_chooser_default_set_property;
545 gobject_class->get_property = gtk_file_chooser_default_get_property;
546 gobject_class->dispose = gtk_file_chooser_default_dispose;
548 widget_class->show_all = gtk_file_chooser_default_show_all;
549 widget_class->realize = gtk_file_chooser_default_realize;
550 widget_class->map = gtk_file_chooser_default_map;
551 widget_class->unmap = gtk_file_chooser_default_unmap;
552 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
553 widget_class->style_set = gtk_file_chooser_default_style_set;
554 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
555 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
557 signals[LOCATION_POPUP] =
558 g_signal_new_class_handler (I_("location-popup"),
559 G_OBJECT_CLASS_TYPE (class),
560 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
561 G_CALLBACK (location_popup_handler),
563 _gtk_marshal_VOID__STRING,
564 G_TYPE_NONE, 1, G_TYPE_STRING);
566 signals[LOCATION_POPUP_ON_PASTE] =
567 g_signal_new_class_handler (I_("location-popup-on-paste"),
568 G_OBJECT_CLASS_TYPE (class),
569 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
570 G_CALLBACK (location_popup_on_paste_handler),
572 _gtk_marshal_VOID__VOID,
575 signals[LOCATION_TOGGLE_POPUP] =
576 g_signal_new_class_handler (I_("location-toggle-popup"),
577 G_OBJECT_CLASS_TYPE (class),
578 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
579 G_CALLBACK (location_toggle_popup_handler),
581 _gtk_marshal_VOID__VOID,
585 g_signal_new_class_handler (I_("up-folder"),
586 G_OBJECT_CLASS_TYPE (class),
587 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
588 G_CALLBACK (up_folder_handler),
590 _gtk_marshal_VOID__VOID,
593 signals[DOWN_FOLDER] =
594 g_signal_new_class_handler (I_("down-folder"),
595 G_OBJECT_CLASS_TYPE (class),
596 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
597 G_CALLBACK (down_folder_handler),
599 _gtk_marshal_VOID__VOID,
602 signals[HOME_FOLDER] =
603 g_signal_new_class_handler (I_("home-folder"),
604 G_OBJECT_CLASS_TYPE (class),
605 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
606 G_CALLBACK (home_folder_handler),
608 _gtk_marshal_VOID__VOID,
611 signals[DESKTOP_FOLDER] =
612 g_signal_new_class_handler (I_("desktop-folder"),
613 G_OBJECT_CLASS_TYPE (class),
614 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
615 G_CALLBACK (desktop_folder_handler),
617 _gtk_marshal_VOID__VOID,
620 signals[QUICK_BOOKMARK] =
621 g_signal_new_class_handler (I_("quick-bookmark"),
622 G_OBJECT_CLASS_TYPE (class),
623 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
624 G_CALLBACK (quick_bookmark_handler),
626 _gtk_marshal_VOID__INT,
627 G_TYPE_NONE, 1, G_TYPE_INT);
629 signals[SHOW_HIDDEN] =
630 g_signal_new_class_handler (I_("show-hidden"),
631 G_OBJECT_CLASS_TYPE (class),
632 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
633 G_CALLBACK (show_hidden_handler),
635 _gtk_marshal_VOID__VOID,
638 signals[SEARCH_SHORTCUT] =
639 g_signal_new_class_handler (I_("search-shortcut"),
640 G_OBJECT_CLASS_TYPE (class),
641 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
642 G_CALLBACK (search_shortcut_handler),
644 _gtk_marshal_VOID__VOID,
647 signals[RECENT_SHORTCUT] =
648 g_signal_new_class_handler (I_("recent-shortcut"),
649 G_OBJECT_CLASS_TYPE (class),
650 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
651 G_CALLBACK (recent_shortcut_handler),
653 _gtk_marshal_VOID__VOID,
656 binding_set = gtk_binding_set_by_class (class);
658 gtk_binding_entry_add_signal (binding_set,
659 GDK_l, GDK_CONTROL_MASK,
660 "location-toggle-popup",
663 gtk_binding_entry_add_signal (binding_set,
666 1, G_TYPE_STRING, "/");
667 gtk_binding_entry_add_signal (binding_set,
670 1, G_TYPE_STRING, "/");
673 gtk_binding_entry_add_signal (binding_set,
676 1, G_TYPE_STRING, "~");
679 gtk_binding_entry_add_signal (binding_set,
680 GDK_v, GDK_CONTROL_MASK,
681 "location-popup-on-paste",
683 gtk_binding_entry_add_signal (binding_set,
684 GDK_Up, GDK_MOD1_MASK,
687 gtk_binding_entry_add_signal (binding_set,
691 gtk_binding_entry_add_signal (binding_set,
692 GDK_KP_Up, GDK_MOD1_MASK,
696 gtk_binding_entry_add_signal (binding_set,
697 GDK_Down, GDK_MOD1_MASK,
700 gtk_binding_entry_add_signal (binding_set,
701 GDK_KP_Down, GDK_MOD1_MASK,
705 gtk_binding_entry_add_signal (binding_set,
706 GDK_Home, GDK_MOD1_MASK,
709 gtk_binding_entry_add_signal (binding_set,
710 GDK_KP_Home, GDK_MOD1_MASK,
713 gtk_binding_entry_add_signal (binding_set,
714 GDK_d, GDK_MOD1_MASK,
717 gtk_binding_entry_add_signal (binding_set,
718 GDK_h, GDK_CONTROL_MASK,
721 gtk_binding_entry_add_signal (binding_set,
722 GDK_s, GDK_MOD1_MASK,
725 gtk_binding_entry_add_signal (binding_set,
726 GDK_r, GDK_MOD1_MASK,
730 for (i = 0; i < 10; i++)
731 gtk_binding_entry_add_signal (binding_set,
732 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
736 _gtk_file_chooser_install_properties (gobject_class);
740 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
742 iface->select_file = gtk_file_chooser_default_select_file;
743 iface->unselect_file = gtk_file_chooser_default_unselect_file;
744 iface->select_all = gtk_file_chooser_default_select_all;
745 iface->unselect_all = gtk_file_chooser_default_unselect_all;
746 iface->get_files = gtk_file_chooser_default_get_files;
747 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
748 iface->get_file_system = gtk_file_chooser_default_get_file_system;
749 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
750 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
751 iface->set_current_name = gtk_file_chooser_default_set_current_name;
752 iface->add_filter = gtk_file_chooser_default_add_filter;
753 iface->remove_filter = gtk_file_chooser_default_remove_filter;
754 iface->list_filters = gtk_file_chooser_default_list_filters;
755 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
756 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
757 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
761 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
763 iface->get_default_size = gtk_file_chooser_default_get_default_size;
764 iface->should_respond = gtk_file_chooser_default_should_respond;
765 iface->initial_focus = gtk_file_chooser_default_initial_focus;
769 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
771 profile_start ("start", NULL);
772 #ifdef PROFILE_FILE_CHOOSER
773 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
775 impl->local_only = TRUE;
776 impl->preview_widget_active = TRUE;
777 impl->use_preview_label = TRUE;
778 impl->select_multiple = FALSE;
779 impl->show_hidden = FALSE;
780 impl->show_size_column = TRUE;
781 impl->icon_size = FALLBACK_ICON_SIZE;
782 impl->load_state = LOAD_EMPTY;
783 impl->reload_state = RELOAD_EMPTY;
784 impl->pending_select_files = NULL;
785 impl->location_mode = LOCATION_MODE_PATH_BAR;
786 impl->operation_mode = OPERATION_MODE_BROWSE;
787 impl->sort_column = MODEL_COL_NAME;
788 impl->sort_order = GTK_SORT_ASCENDING;
789 impl->recent_manager = gtk_recent_manager_get_default ();
790 impl->create_folders = TRUE;
792 gtk_box_set_spacing (GTK_BOX (impl), 12);
794 set_file_system_backend (impl);
796 profile_end ("end", NULL);
799 /* Frees the data columns for the specified iter in the shortcuts model*/
801 shortcuts_free_row_data (GtkFileChooserDefault *impl,
805 ShortcutType shortcut_type;
806 GCancellable *cancellable;
808 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
809 SHORTCUTS_COL_DATA, &col_data,
810 SHORTCUTS_COL_TYPE, &shortcut_type,
811 SHORTCUTS_COL_CANCELLABLE, &cancellable,
815 g_cancellable_cancel (cancellable);
817 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
818 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
822 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
824 GtkFileSystemVolume *volume;
827 _gtk_file_system_volume_free (volume);
833 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
836 g_object_unref (file);
840 /* Frees all the data columns in the shortcuts model */
842 shortcuts_free (GtkFileChooserDefault *impl)
846 if (!impl->shortcuts_model)
849 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
852 shortcuts_free_row_data (impl, &iter);
854 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
856 g_object_unref (impl->shortcuts_model);
857 impl->shortcuts_model = NULL;
861 pending_select_files_free (GtkFileChooserDefault *impl)
863 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
864 g_slist_free (impl->pending_select_files);
865 impl->pending_select_files = NULL;
869 pending_select_files_add (GtkFileChooserDefault *impl,
872 impl->pending_select_files =
873 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
876 /* Used from gtk_tree_selection_selected_foreach() */
878 store_selection_foreach (GtkTreeModel *model,
883 GtkFileChooserDefault *impl;
886 impl = GTK_FILE_CHOOSER_DEFAULT (data);
888 file = _gtk_file_system_model_get_file (GTK_FILE_SYSTEM_MODEL (model), iter);
889 pending_select_files_add (impl, file);
892 /* Stores the current selection in the list of paths to select; this is used to
893 * preserve the selection when reloading the current folder.
896 pending_select_files_store_selection (GtkFileChooserDefault *impl)
898 GtkTreeSelection *selection;
900 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
901 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
905 gtk_file_chooser_default_finalize (GObject *object)
907 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
910 unset_file_system_backend (impl);
912 if (impl->shortcuts_pane_filter_model)
913 g_object_unref (impl->shortcuts_pane_filter_model);
915 if (impl->shortcuts_combo_filter_model)
916 g_object_unref (impl->shortcuts_combo_filter_model);
918 shortcuts_free (impl);
920 g_free (impl->browse_files_last_selected_name);
922 for (l = impl->filters; l; l = l->next)
924 GtkFileFilter *filter;
926 filter = GTK_FILE_FILTER (l->data);
927 g_object_unref (filter);
929 g_slist_free (impl->filters);
931 if (impl->current_filter)
932 g_object_unref (impl->current_filter);
934 if (impl->current_volume_file)
935 g_object_unref (impl->current_volume_file);
937 if (impl->current_folder)
938 g_object_unref (impl->current_folder);
940 if (impl->preview_file)
941 g_object_unref (impl->preview_file);
943 if (impl->browse_path_bar_size_group)
944 g_object_unref (impl->browse_path_bar_size_group);
946 load_remove_timer (impl);
948 /* Free all the Models we have */
949 if (impl->browse_files_model)
950 g_object_unref (impl->browse_files_model);
952 search_clear_model (impl, FALSE);
953 recent_clear_model (impl, FALSE);
955 g_free (impl->preview_display_name);
957 g_free (impl->edited_new_text);
959 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
962 /* Shows an error dialog set as transient for the specified window */
964 error_message_with_parent (GtkWindow *parent,
970 dialog = gtk_message_dialog_new (parent,
971 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
976 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
979 if (parent && parent->group)
980 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
982 gtk_dialog_run (GTK_DIALOG (dialog));
983 gtk_widget_destroy (dialog);
986 /* Returns a toplevel GtkWindow, or NULL if none */
988 get_toplevel (GtkWidget *widget)
992 toplevel = gtk_widget_get_toplevel (widget);
993 if (!GTK_WIDGET_TOPLEVEL (toplevel))
996 return GTK_WINDOW (toplevel);
999 /* Shows an error dialog for the file chooser */
1001 error_message (GtkFileChooserDefault *impl,
1005 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1008 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1010 error_dialog (GtkFileChooserDefault *impl,
1021 uri = g_file_get_uri (file);
1022 text = g_strdup_printf (msg, uri);
1023 error_message (impl, text, error->message);
1026 g_error_free (error);
1030 /* Displays an error message about not being able to get information for a file.
1031 * Frees the GError as well.
1034 error_getting_info_dialog (GtkFileChooserDefault *impl,
1039 _("Could not retrieve information about the file"),
1043 /* Shows an error dialog about not being able to add a bookmark */
1045 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1050 _("Could not add a bookmark"),
1054 /* Shows an error dialog about not being able to remove a bookmark */
1056 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1061 _("Could not remove bookmark"),
1065 /* Shows an error dialog about not being able to create a folder */
1067 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1072 _("The folder could not be created"),
1076 /* Shows an error about not being able to create a folder because a file with
1077 * the same name is already there.
1080 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1085 _("The folder could not be created, as a file with the same "
1086 "name already exists. Try using a different name for the "
1087 "folder, or rename the file first."),
1091 /* Shows an error dialog about not being able to create a filename */
1093 error_building_filename_dialog (GtkFileChooserDefault *impl,
1096 error_dialog (impl, _("Invalid file name"),
1100 /* Shows an error dialog when we cannot switch to a folder */
1102 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1106 error_dialog (impl, _("The folder contents could not be displayed"),
1110 /* Changes folders, displaying an error dialog if this fails */
1112 change_folder_and_display_error (GtkFileChooserDefault *impl,
1114 gboolean clear_entry)
1119 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1121 /* We copy the path because of this case:
1123 * list_row_activated()
1124 * fetches path from model; path belongs to the model (*)
1125 * calls change_folder_and_display_error()
1126 * calls gtk_file_chooser_set_current_folder_file()
1127 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1131 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1134 error_changing_folder_dialog (impl, file, error);
1140 emit_default_size_changed (GtkFileChooserDefault *impl)
1142 profile_msg (" emit default-size-changed start", NULL);
1143 g_signal_emit_by_name (impl, "default-size-changed");
1144 profile_msg (" emit default-size-changed end", NULL);
1148 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1150 if (impl->use_preview_label)
1152 if (!impl->preview_label)
1154 impl->preview_label = gtk_label_new (impl->preview_display_name);
1155 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1156 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1157 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1158 gtk_widget_show (impl->preview_label);
1163 if (impl->preview_label)
1165 gtk_widget_destroy (impl->preview_label);
1166 impl->preview_label = NULL;
1170 if (impl->preview_widget_active && impl->preview_widget)
1171 gtk_widget_show (impl->preview_box);
1173 gtk_widget_hide (impl->preview_box);
1175 if (!GTK_WIDGET_MAPPED (impl))
1176 emit_default_size_changed (impl);
1180 set_preview_widget (GtkFileChooserDefault *impl,
1181 GtkWidget *preview_widget)
1183 if (preview_widget == impl->preview_widget)
1186 if (impl->preview_widget)
1187 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1188 impl->preview_widget);
1190 impl->preview_widget = preview_widget;
1191 if (impl->preview_widget)
1193 gtk_widget_show (impl->preview_widget);
1194 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1195 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1196 impl->preview_widget,
1197 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1200 update_preview_widget_visibility (impl);
1203 /* Renders a "Search" icon at an appropriate size for a tree view */
1205 render_search_icon (GtkFileChooserDefault *impl)
1207 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1211 render_recent_icon (GtkFileChooserDefault *impl)
1213 GtkIconTheme *theme;
1216 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1217 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1219 theme = gtk_icon_theme_get_default ();
1221 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1227 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1233 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1234 struct ReloadIconsData
1236 GtkFileChooserDefault *impl;
1237 GtkTreeRowReference *row_ref;
1241 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1243 const GError *error,
1249 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1250 struct ReloadIconsData *data = user_data;
1252 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1255 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1257 if (cancelled || error)
1260 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1262 path = gtk_tree_row_reference_get_path (data->row_ref);
1263 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1264 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1265 SHORTCUTS_COL_PIXBUF, pixbuf,
1267 gtk_tree_path_free (path);
1270 g_object_unref (pixbuf);
1273 gtk_tree_row_reference_free (data->row_ref);
1274 g_object_unref (data->impl);
1277 g_object_unref (cancellable);
1281 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1286 profile_start ("start", NULL);
1288 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1291 for (l = impl->reload_icon_cancellables; l; l = l->next)
1293 GCancellable *cancellable = G_CANCELLABLE (l->data);
1294 g_cancellable_cancel (cancellable);
1296 g_slist_free (impl->reload_icon_cancellables);
1297 impl->reload_icon_cancellables = NULL;
1302 ShortcutType shortcut_type;
1303 gboolean pixbuf_visible;
1306 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1307 SHORTCUTS_COL_DATA, &data,
1308 SHORTCUTS_COL_TYPE, &shortcut_type,
1309 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1315 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1317 GtkFileSystemVolume *volume;
1320 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1321 impl->icon_size, NULL);
1323 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1325 if (g_file_is_native (G_FILE (data)))
1328 struct ReloadIconsData *info;
1329 GtkTreePath *tree_path;
1330 GCancellable *cancellable;
1334 info = g_new0 (struct ReloadIconsData, 1);
1335 info->impl = g_object_ref (impl);
1336 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1337 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1338 gtk_tree_path_free (tree_path);
1340 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1342 shortcuts_reload_icons_get_info_cb,
1344 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1348 GtkIconTheme *icon_theme;
1350 /* Don't call get_info for remote paths to avoid latency and
1352 * If we switch to a better bookmarks file format (XBEL), we
1353 * should use mime info to get a better icon.
1355 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1356 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1357 impl->icon_size, 0, NULL);
1360 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1362 pixbuf = render_search_icon (impl);
1364 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1366 pixbuf = render_recent_icon (impl);
1369 gtk_list_store_set (impl->shortcuts_model, &iter,
1370 SHORTCUTS_COL_PIXBUF, pixbuf,
1374 g_object_unref (pixbuf);
1378 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1382 profile_end ("end", NULL);
1386 shortcuts_find_folder (GtkFileChooserDefault *impl,
1389 GtkTreeSelection *selection;
1393 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1395 g_assert (folder != NULL);
1396 pos = shortcut_find_position (impl, folder);
1399 gtk_tree_selection_unselect_all (selection);
1403 path = gtk_tree_path_new_from_indices (pos, -1);
1404 gtk_tree_selection_select_path (selection, path);
1405 gtk_tree_path_free (path);
1408 /* If a shortcut corresponds to the current folder, selects it */
1410 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1412 shortcuts_find_folder (impl, impl->current_folder);
1415 /* Removes the specified number of rows from the shortcuts list */
1417 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1423 path = gtk_tree_path_new_from_indices (start_row, -1);
1425 for (; n_rows; n_rows--)
1429 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1430 g_assert_not_reached ();
1432 shortcuts_free_row_data (impl, &iter);
1433 gtk_list_store_remove (impl->shortcuts_model, &iter);
1436 gtk_tree_path_free (path);
1440 shortcuts_update_count (GtkFileChooserDefault *impl,
1441 ShortcutsIndex type,
1446 case SHORTCUTS_HOME:
1448 impl->has_home = FALSE;
1450 impl->has_home = TRUE;
1453 case SHORTCUTS_DESKTOP:
1455 impl->has_desktop = FALSE;
1457 impl->has_desktop = TRUE;
1460 case SHORTCUTS_VOLUMES:
1461 impl->num_volumes += value;
1464 case SHORTCUTS_SHORTCUTS:
1465 impl->num_shortcuts += value;
1468 case SHORTCUTS_BOOKMARKS:
1469 impl->num_bookmarks += value;
1472 case SHORTCUTS_CURRENT_FOLDER:
1474 impl->shortcuts_current_folder_active = FALSE;
1476 impl->shortcuts_current_folder_active = TRUE;
1485 struct ShortcutsInsertRequest
1487 GtkFileChooserDefault *impl;
1491 GtkTreeRowReference *row_ref;
1492 ShortcutsIndex type;
1498 get_file_info_finished (GCancellable *cancellable,
1500 const GError *error,
1504 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1508 GCancellable *model_cancellable;
1509 struct ShortcutsInsertRequest *request = data;
1511 path = gtk_tree_row_reference_get_path (request->row_ref);
1513 /* Handle doesn't exist anymore in the model */
1516 pos = gtk_tree_path_get_indices (path)[0];
1517 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1519 gtk_tree_path_free (path);
1521 /* validate cancellable, else goto out */
1522 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1523 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1525 if (cancellable != model_cancellable)
1528 /* set the cancellable to NULL in the model (we unref later on) */
1529 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1530 SHORTCUTS_COL_CANCELLABLE, NULL,
1538 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1539 shortcuts_update_count (request->impl, request->type, -1);
1541 if (request->type == SHORTCUTS_HOME)
1545 home = g_file_new_for_path (g_get_home_dir ());
1546 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1547 g_object_unref (home);
1549 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1551 /* Remove the current folder separator */
1552 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1553 shortcuts_remove_rows (request->impl, separator_pos, 1);
1559 if (!request->label_copy)
1560 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1561 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1562 request->impl->icon_size);
1564 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1565 SHORTCUTS_COL_PIXBUF, pixbuf,
1566 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1567 SHORTCUTS_COL_NAME, request->label_copy,
1568 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1569 SHORTCUTS_COL_REMOVABLE, request->removable,
1572 if (request->impl->shortcuts_pane_filter_model)
1573 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1575 if (request->impl->shortcuts_combo_filter_model)
1576 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1578 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1579 request->impl->save_folder_combo != NULL)
1581 /* The current folder is updated via _activate_iter(), don't
1582 * have save_folder_combo_changed_cb() call _activate_iter()
1585 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1586 G_CALLBACK (save_folder_combo_changed_cb),
1589 if (request->impl->has_search)
1592 if (request->impl->has_recent)
1595 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1596 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1597 G_CALLBACK (save_folder_combo_changed_cb),
1602 g_object_unref (pixbuf);
1605 g_object_unref (request->impl);
1606 g_object_unref (request->file);
1607 gtk_tree_row_reference_free (request->row_ref);
1608 g_free (request->label_copy);
1611 g_object_unref (cancellable);
1614 /* FIXME: GtkFileSystem needs a function to split a remote path
1615 * into hostname and path components, or maybe just have a
1616 * gtk_file_system_path_get_display_name().
1618 * This function is also used in gtkfilechooserbutton.c
1621 _gtk_file_chooser_label_for_file (GFile *file)
1623 const gchar *path, *start, *end, *p;
1624 gchar *uri, *host, *label;
1626 uri = g_file_get_uri (file);
1628 start = strstr (uri, "://");
1632 path = strchr (start, '/');
1637 end = uri + strlen (uri);
1641 /* strip username */
1642 p = strchr (start, '@');
1646 p = strchr (start, ':');
1650 host = g_strndup (start, end - start);
1652 /* Translators: the first string is a path and the second string
1653 * is a hostname. Nautilus and the panel contain the same string
1656 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1662 label = g_strdup (uri);
1670 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1671 * inserts a volume. A position of -1 indicates the end of the tree.
1674 shortcuts_insert_file (GtkFileChooserDefault *impl,
1676 ShortcutType shortcut_type,
1677 GtkFileSystemVolume *volume,
1681 ShortcutsIndex type)
1684 GdkPixbuf *pixbuf = NULL;
1685 gpointer data = NULL;
1687 GtkIconTheme *icon_theme;
1689 profile_start ("start shortcut", NULL);
1691 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1694 label_copy = _gtk_file_system_volume_get_display_name (volume);
1695 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1696 impl->icon_size, NULL);
1698 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1700 if (g_file_is_native (file))
1702 struct ShortcutsInsertRequest *request;
1703 GCancellable *cancellable;
1706 request = g_new0 (struct ShortcutsInsertRequest, 1);
1707 request->impl = g_object_ref (impl);
1708 request->file = g_object_ref (file);
1709 request->name_only = TRUE;
1710 request->removable = removable;
1712 request->type = type;
1714 request->label_copy = g_strdup (label);
1717 gtk_list_store_append (impl->shortcuts_model, &iter);
1719 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1721 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1722 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1723 gtk_tree_path_free (p);
1725 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1726 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1727 get_file_info_finished, request);
1729 gtk_list_store_set (impl->shortcuts_model, &iter,
1730 SHORTCUTS_COL_DATA, g_object_ref (file),
1731 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1732 SHORTCUTS_COL_CANCELLABLE, cancellable,
1735 shortcuts_update_count (impl, type, 1);
1741 /* Don't call get_info for remote paths to avoid latency and
1744 data = g_object_ref (file);
1746 label_copy = g_strdup (label);
1748 label_copy = _gtk_file_chooser_label_for_file (file);
1750 /* If we switch to a better bookmarks file format (XBEL), we
1751 * should use mime info to get a better icon.
1753 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1754 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1755 impl->icon_size, 0, NULL);
1760 g_assert_not_reached ();
1766 gtk_list_store_append (impl->shortcuts_model, &iter);
1768 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1770 shortcuts_update_count (impl, type, 1);
1772 gtk_list_store_set (impl->shortcuts_model, &iter,
1773 SHORTCUTS_COL_PIXBUF, pixbuf,
1774 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1775 SHORTCUTS_COL_NAME, label_copy,
1776 SHORTCUTS_COL_DATA, data,
1777 SHORTCUTS_COL_TYPE, shortcut_type,
1778 SHORTCUTS_COL_REMOVABLE, removable,
1779 SHORTCUTS_COL_CANCELLABLE, NULL,
1782 if (impl->shortcuts_pane_filter_model)
1783 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1785 if (impl->shortcuts_combo_filter_model)
1786 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1788 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1790 /* The current folder is updated via _activate_iter(), don't
1791 * have save_folder_combo_changed_cb() call _activate_iter()
1794 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1796 if (impl->has_search)
1799 if (impl->has_recent)
1802 g_signal_handlers_block_by_func (impl->save_folder_combo,
1803 G_CALLBACK (save_folder_combo_changed_cb),
1806 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1807 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1808 G_CALLBACK (save_folder_combo_changed_cb),
1812 g_free (label_copy);
1815 g_object_unref (pixbuf);
1817 profile_end ("end", NULL);
1821 shortcuts_append_search (GtkFileChooserDefault *impl)
1826 pixbuf = render_search_icon (impl);
1828 gtk_list_store_append (impl->shortcuts_model, &iter);
1829 gtk_list_store_set (impl->shortcuts_model, &iter,
1830 SHORTCUTS_COL_PIXBUF, pixbuf,
1831 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1832 SHORTCUTS_COL_NAME, _("Search"),
1833 SHORTCUTS_COL_DATA, NULL,
1834 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1835 SHORTCUTS_COL_REMOVABLE, FALSE,
1839 g_object_unref (pixbuf);
1841 impl->has_search = TRUE;
1845 shortcuts_append_recent (GtkFileChooserDefault *impl)
1850 pixbuf = render_recent_icon (impl);
1852 gtk_list_store_append (impl->shortcuts_model, &iter);
1853 gtk_list_store_set (impl->shortcuts_model, &iter,
1854 SHORTCUTS_COL_PIXBUF, pixbuf,
1855 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1856 SHORTCUTS_COL_NAME, _("Recently Used"),
1857 SHORTCUTS_COL_DATA, NULL,
1858 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1859 SHORTCUTS_COL_REMOVABLE, FALSE,
1863 g_object_unref (pixbuf);
1865 impl->has_recent = TRUE;
1868 /* Appends an item for the user's home directory to the shortcuts model */
1870 shortcuts_append_home (GtkFileChooserDefault *impl)
1872 const char *home_path;
1875 profile_start ("start", NULL);
1877 home_path = g_get_home_dir ();
1878 if (home_path == NULL)
1880 profile_end ("end - no home directory!?", NULL);
1884 home = g_file_new_for_path (home_path);
1885 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1886 impl->has_home = TRUE;
1888 g_object_unref (home);
1890 profile_end ("end", NULL);
1893 /* Appends the ~/Desktop directory to the shortcuts model */
1895 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1900 profile_start ("start", NULL);
1902 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1903 /* "To disable a directory, point it to the homedir."
1904 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1906 if (!g_strcmp0 (name, g_get_home_dir ()))
1908 profile_end ("end", NULL);
1912 file = g_file_new_for_path (name);
1913 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1914 impl->has_desktop = TRUE;
1916 /* We do not actually pop up an error dialog if there is no desktop directory
1917 * because some people may really not want to have one.
1920 g_object_unref (file);
1922 profile_end ("end", NULL);
1925 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1927 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1934 profile_start ("start", NULL);
1936 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1939 for (; bookmarks; bookmarks = bookmarks->next)
1943 file = bookmarks->data;
1945 if (impl->local_only && !g_file_is_native (file))
1948 if (shortcut_find_position (impl, file) != -1)
1951 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1953 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1959 profile_end ("end", NULL);
1961 return num_inserted;
1964 /* Returns the index for the corresponding item in the shortcuts bar */
1966 shortcuts_get_index (GtkFileChooserDefault *impl,
1967 ShortcutsIndex where)
1973 if (where == SHORTCUTS_SEARCH)
1976 n += impl->has_search ? 1 : 0;
1978 if (where == SHORTCUTS_RECENT)
1981 n += impl->has_recent ? 1 : 0;
1983 if (where == SHORTCUTS_RECENT_SEPARATOR)
1986 n += impl->has_recent ? 1 : 0;
1988 if (where == SHORTCUTS_HOME)
1991 n += impl->has_home ? 1 : 0;
1993 if (where == SHORTCUTS_DESKTOP)
1996 n += impl->has_desktop ? 1 : 0;
1998 if (where == SHORTCUTS_VOLUMES)
2001 n += impl->num_volumes;
2003 if (where == SHORTCUTS_SHORTCUTS)
2006 n += impl->num_shortcuts;
2008 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2011 /* If there are no bookmarks there won't be a separator */
2012 n += (impl->num_bookmarks > 0) ? 1 : 0;
2014 if (where == SHORTCUTS_BOOKMARKS)
2017 n += impl->num_bookmarks;
2019 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2024 if (where == SHORTCUTS_CURRENT_FOLDER)
2027 g_assert_not_reached ();
2034 /* Adds all the file system volumes to the shortcuts model */
2036 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2041 gboolean old_changing_folders;
2043 profile_start ("start", NULL);
2045 old_changing_folders = impl->changing_folder;
2046 impl->changing_folder = TRUE;
2048 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2049 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2050 impl->num_volumes = 0;
2052 list = _gtk_file_system_list_volumes (impl->file_system);
2056 for (l = list; l; l = l->next)
2058 GtkFileSystemVolume *volume;
2062 if (impl->local_only)
2064 if (_gtk_file_system_volume_is_mounted (volume))
2067 gboolean base_is_native = TRUE;
2069 base_file = _gtk_file_system_volume_get_root (volume);
2070 if (base_file != NULL)
2072 base_is_native = g_file_is_native (base_file);
2073 g_object_unref (base_file);
2076 if (!base_is_native)
2081 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2085 impl->num_volumes = n;
2086 g_slist_free (list);
2088 if (impl->shortcuts_pane_filter_model)
2089 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2091 if (impl->shortcuts_combo_filter_model)
2092 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2094 impl->changing_folder = old_changing_folders;
2096 profile_end ("end", NULL);
2099 /* Inserts a separator node in the shortcuts list */
2101 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2102 ShortcutsIndex where)
2106 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2107 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2108 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2110 gtk_list_store_insert (impl->shortcuts_model, &iter,
2111 shortcuts_get_index (impl, where));
2112 gtk_list_store_set (impl->shortcuts_model, &iter,
2113 SHORTCUTS_COL_PIXBUF, NULL,
2114 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2115 SHORTCUTS_COL_NAME, NULL,
2116 SHORTCUTS_COL_DATA, NULL,
2117 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2121 /* Updates the list of bookmarks */
2123 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2126 gboolean old_changing_folders;
2128 GFile *list_selected = NULL;
2129 GFile *combo_selected = NULL;
2130 ShortcutType shortcut_type;
2133 profile_start ("start", NULL);
2135 old_changing_folders = impl->changing_folder;
2136 impl->changing_folder = TRUE;
2138 if (shortcuts_get_selected (impl, &iter))
2140 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2142 SHORTCUTS_COL_DATA, &col_data,
2143 SHORTCUTS_COL_TYPE, &shortcut_type,
2146 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2147 list_selected = g_object_ref (col_data);
2150 if (impl->save_folder_combo &&
2151 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2154 GtkTreeIter child_iter;
2156 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2159 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2161 SHORTCUTS_COL_DATA, &col_data,
2162 SHORTCUTS_COL_TYPE, &shortcut_type,
2165 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2166 combo_selected = g_object_ref (col_data);
2169 if (impl->num_bookmarks > 0)
2170 shortcuts_remove_rows (impl,
2171 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2172 impl->num_bookmarks + 1);
2174 impl->num_bookmarks = 0;
2175 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2177 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2178 shortcuts_append_bookmarks (impl, bookmarks);
2179 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2180 g_slist_free (bookmarks);
2182 if (impl->num_bookmarks == 0)
2183 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2185 if (impl->shortcuts_pane_filter_model)
2186 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2188 if (impl->shortcuts_combo_filter_model)
2189 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2193 shortcuts_find_folder (impl, list_selected);
2194 g_object_unref (list_selected);
2201 pos = shortcut_find_position (impl, combo_selected);
2204 if (impl->has_search)
2207 if (impl->has_recent)
2210 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2213 g_object_unref (combo_selected);
2216 impl->changing_folder = old_changing_folders;
2218 profile_end ("end", NULL);
2221 /* Appends a separator and a row to the shortcuts list for the current folder */
2223 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2228 g_assert (!impl->shortcuts_current_folder_active);
2232 g_assert (impl->current_folder != NULL);
2234 pos = shortcut_find_position (impl, impl->current_folder);
2237 GtkFileSystemVolume *volume;
2242 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2246 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2248 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2250 base_file = _gtk_file_system_volume_get_root (volume);
2254 if (base_file && g_file_equal (base_file, impl->current_folder))
2255 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2257 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2260 g_object_unref (base_file);
2262 else if (impl->save_folder_combo != NULL)
2264 if (impl->has_search)
2267 if (impl->has_recent)
2268 pos -= 2; /* + separator */
2270 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2274 /* Updates the current folder row in the shortcuts model */
2276 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2280 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2282 if (impl->shortcuts_current_folder_active)
2284 shortcuts_remove_rows (impl, pos, 2);
2285 impl->shortcuts_current_folder_active = FALSE;
2288 shortcuts_add_current_folder (impl);
2291 /* Filter function used for the shortcuts filter model */
2293 shortcuts_pane_filter_cb (GtkTreeModel *model,
2297 GtkFileChooserDefault *impl;
2301 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2303 path = gtk_tree_model_get_path (model, iter);
2307 pos = *gtk_tree_path_get_indices (path);
2308 gtk_tree_path_free (path);
2310 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2313 /* Creates the list model for shortcuts */
2315 shortcuts_model_create (GtkFileChooserDefault *impl)
2317 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2318 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2319 GDK_TYPE_PIXBUF, /* pixbuf */
2320 G_TYPE_STRING, /* name */
2321 G_TYPE_POINTER, /* path or volume */
2322 G_TYPE_INT, /* ShortcutType */
2323 G_TYPE_BOOLEAN, /* removable */
2324 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2325 G_TYPE_POINTER); /* GCancellable */
2327 if (search_is_possible (impl))
2329 shortcuts_append_search (impl);
2332 if (impl->recent_manager)
2334 shortcuts_append_recent (impl);
2335 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2338 if (impl->file_system)
2340 shortcuts_append_home (impl);
2341 shortcuts_append_desktop (impl);
2342 shortcuts_add_volumes (impl);
2345 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2346 GTK_TREE_MODEL (impl->shortcuts_model),
2349 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2350 shortcuts_pane_filter_cb,
2355 /* Callback used when the "New Folder" button is clicked */
2357 new_folder_button_clicked (GtkButton *button,
2358 GtkFileChooserDefault *impl)
2363 if (!impl->browse_files_model)
2364 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2366 /* Prevent button from being clicked twice */
2367 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2369 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2371 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2372 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2373 path, impl->list_name_column,
2376 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2377 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2379 impl->list_name_column,
2382 gtk_tree_path_free (path);
2385 /* Idle handler for creating a new folder after editing its name cell, or for
2386 * canceling the editing.
2389 edited_idle_cb (GtkFileChooserDefault *impl)
2391 GDK_THREADS_ENTER ();
2393 g_source_destroy (impl->edited_idle);
2394 impl->edited_idle = NULL;
2396 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2397 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2399 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2401 if (impl->edited_new_text /* not cancelled? */
2402 && (strlen (impl->edited_new_text) != 0)
2403 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2405 GError *error = NULL;
2408 file = g_file_get_child_for_display_name (impl->current_folder,
2409 impl->edited_new_text,
2413 GError *error = NULL;
2415 if (g_file_make_directory (file, NULL, &error))
2416 change_folder_and_display_error (impl, file, FALSE);
2418 error_creating_folder_dialog (impl, file, error);
2420 g_object_unref (file);
2423 error_creating_folder_dialog (impl, file, error);
2425 g_free (impl->edited_new_text);
2426 impl->edited_new_text = NULL;
2429 GDK_THREADS_LEAVE ();
2435 queue_edited_idle (GtkFileChooserDefault *impl,
2436 const gchar *new_text)
2438 /* We create the folder in an idle handler so that we don't modify the tree
2442 if (!impl->edited_idle)
2444 impl->edited_idle = g_idle_source_new ();
2445 g_source_set_closure (impl->edited_idle,
2446 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2448 g_source_attach (impl->edited_idle, NULL);
2451 g_free (impl->edited_new_text);
2452 impl->edited_new_text = g_strdup (new_text);
2455 /* Callback used from the text cell renderer when the new folder is named */
2457 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2459 const gchar *new_text,
2460 GtkFileChooserDefault *impl)
2462 /* work around bug #154921 */
2463 g_object_set (cell_renderer_text,
2464 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2465 queue_edited_idle (impl, new_text);
2468 /* Callback used from the text cell renderer when the new folder edition gets
2472 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2473 GtkFileChooserDefault *impl)
2475 /* work around bug #154921 */
2476 g_object_set (cell_renderer_text,
2477 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2478 queue_edited_idle (impl, NULL);
2481 /* Creates the widgets for the filter combo box */
2483 filter_create (GtkFileChooserDefault *impl)
2485 impl->filter_combo = gtk_combo_box_new_text ();
2486 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2488 g_signal_connect (impl->filter_combo, "changed",
2489 G_CALLBACK (filter_combo_changed), impl);
2491 gtk_widget_set_tooltip_text (impl->filter_combo,
2492 _("Select which types of files are shown"));
2494 return impl->filter_combo;
2498 button_new (GtkFileChooserDefault *impl,
2500 const char *stock_id,
2508 button = gtk_button_new_with_mnemonic (text);
2509 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2510 gtk_button_set_image (GTK_BUTTON (button), image);
2512 gtk_widget_set_sensitive (button, sensitive);
2513 g_signal_connect (button, "clicked", callback, impl);
2516 gtk_widget_show (button);
2521 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2523 shortcut_find_position (GtkFileChooserDefault *impl,
2528 int current_folder_separator_idx;
2530 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2533 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2536 /* FIXME: is this still needed? */
2537 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2541 for (i = 0; i < current_folder_separator_idx; i++)
2544 ShortcutType shortcut_type;
2546 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2547 SHORTCUTS_COL_DATA, &col_data,
2548 SHORTCUTS_COL_TYPE, &shortcut_type,
2553 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2555 GtkFileSystemVolume *volume;
2560 base_file = _gtk_file_system_volume_get_root (volume);
2562 exists = base_file && g_file_equal (file, base_file);
2565 g_object_unref (base_file);
2570 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2574 model_file = col_data;
2576 if (model_file && g_file_equal (model_file, file))
2581 if (i < current_folder_separator_idx - 1)
2583 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2584 g_assert_not_reached ();
2591 /* Tries to add a bookmark from a path name */
2593 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2599 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2601 if (shortcut_find_position (impl, file) != -1)
2605 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2607 error_adding_bookmark_dialog (impl, file, error);
2615 add_bookmark_foreach_cb (GtkTreeModel *model,
2620 GtkFileChooserDefault *impl;
2623 impl = (GtkFileChooserDefault *) data;
2625 gtk_tree_model_get (model, iter,
2626 MODEL_COL_FILE, &file,
2629 shortcuts_add_bookmark_from_file (impl, file, -1);
2631 g_object_unref (file);
2634 /* Adds a bookmark from the currently selected item in the file list */
2636 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2638 GtkTreeSelection *selection;
2640 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2642 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2643 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2645 gtk_tree_selection_selected_foreach (selection,
2646 add_bookmark_foreach_cb,
2650 /* Callback used when the "Add bookmark" button is clicked */
2652 add_bookmark_button_clicked_cb (GtkButton *button,
2653 GtkFileChooserDefault *impl)
2655 bookmarks_add_selected_folder (impl);
2658 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2659 * returns FALSE if no shortcut is selected.
2662 shortcuts_get_selected (GtkFileChooserDefault *impl,
2665 GtkTreeSelection *selection;
2666 GtkTreeIter parent_iter;
2668 if (!impl->browse_shortcuts_tree_view)
2671 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2673 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2676 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2682 /* Removes the selected bookmarks */
2684 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2692 if (!shortcuts_get_selected (impl, &iter))
2695 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2696 SHORTCUTS_COL_DATA, &col_data,
2697 SHORTCUTS_COL_REMOVABLE, &removable,
2703 g_assert (col_data != NULL);
2708 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2709 error_removing_bookmark_dialog (impl, file, error);
2712 /* Callback used when the "Remove bookmark" button is clicked */
2714 remove_bookmark_button_clicked_cb (GtkButton *button,
2715 GtkFileChooserDefault *impl)
2717 remove_selected_bookmarks (impl);
2720 struct selection_check_closure {
2721 GtkFileChooserDefault *impl;
2724 gboolean all_folders;
2727 /* Used from gtk_tree_selection_selected_foreach() */
2729 selection_check_foreach_cb (GtkTreeModel *model,
2734 struct selection_check_closure *closure;
2738 closure->num_selected++;
2740 gtk_tree_model_get (model, iter,
2741 MODEL_COL_IS_FOLDER, &is_folder,
2744 closure->all_folders = closure->all_folders && is_folder;
2745 closure->all_files = closure->all_files && !is_folder;
2748 /* Checks whether the selected items in the file list are all files or all folders */
2750 selection_check (GtkFileChooserDefault *impl,
2752 gboolean *all_files,
2753 gboolean *all_folders)
2755 struct selection_check_closure closure;
2756 GtkTreeSelection *selection;
2758 closure.impl = impl;
2759 closure.num_selected = 0;
2760 closure.all_files = TRUE;
2761 closure.all_folders = TRUE;
2763 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2764 gtk_tree_selection_selected_foreach (selection,
2765 selection_check_foreach_cb,
2768 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2771 *num_selected = closure.num_selected;
2774 *all_files = closure.all_files;
2777 *all_folders = closure.all_folders;
2780 struct get_selected_file_closure {
2781 GtkFileChooserDefault *impl;
2786 get_selected_file_foreach_cb (GtkTreeModel *model,
2791 struct get_selected_file_closure *closure = data;
2793 gtk_tree_model_get (model, iter,
2794 MODEL_COL_FILE, &closure->file,
2797 g_object_unref (closure->file);
2800 /* Returns a selected path from the file list */
2802 get_selected_file (GtkFileChooserDefault *impl)
2804 struct get_selected_file_closure closure;
2805 GtkTreeSelection *selection;
2807 closure.impl = impl;
2808 closure.file = NULL;
2810 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2811 gtk_tree_selection_selected_foreach (selection,
2812 get_selected_file_foreach_cb,
2815 return closure.file;
2819 GtkFileChooserDefault *impl;
2821 } UpdateTooltipData;
2824 update_tooltip (GtkTreeModel *model,
2829 UpdateTooltipData *udata = data;
2831 if (udata->tip == NULL)
2833 gchar *display_name;
2835 gtk_tree_model_get (model, iter,
2836 MODEL_COL_NAME, &display_name,
2839 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2841 g_free (display_name);
2846 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2847 * if there are no selected items *and* the current folder is not in the
2848 * bookmarks list. De-sensitize the button otherwise.
2851 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2854 gboolean all_folders;
2858 selection_check (impl, &num_selected, NULL, &all_folders);
2860 if (num_selected == 0)
2861 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2862 else if (num_selected == 1)
2866 file = get_selected_file (impl);
2867 active = all_folders && (shortcut_find_position (impl, file) == -1);
2870 active = all_folders;
2872 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2874 if (impl->browse_files_popup_menu_add_shortcut_item)
2875 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2876 (num_selected == 0) ? FALSE : active);
2880 if (num_selected == 0)
2881 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2882 else if (num_selected > 1)
2883 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2886 GtkTreeSelection *selection;
2887 UpdateTooltipData data;
2889 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2892 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2896 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2901 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2902 * bookmark row is selected in the shortcuts tree.
2905 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2908 gboolean removable = FALSE;
2912 if (shortcuts_get_selected (impl, &iter))
2914 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2915 SHORTCUTS_COL_REMOVABLE, &removable,
2916 SHORTCUTS_COL_NAME, &name,
2918 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2921 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2923 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2925 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2929 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2930 _("Remove the selected bookmark"));
2935 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2938 gboolean removable = FALSE;
2940 if (impl->browse_shortcuts_popup_menu == NULL)
2943 if (shortcuts_get_selected (impl, &iter))
2944 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2945 SHORTCUTS_COL_REMOVABLE, &removable,
2948 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2949 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2952 /* GtkWidget::drag-begin handler for the shortcuts list. */
2954 shortcuts_drag_begin_cb (GtkWidget *widget,
2955 GdkDragContext *context,
2956 GtkFileChooserDefault *impl)
2959 impl->shortcuts_drag_context = g_object_ref (context);
2964 /* Removes the idle handler for outside drags */
2966 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2968 if (!impl->shortcuts_drag_outside_idle)
2971 g_source_destroy (impl->shortcuts_drag_outside_idle);
2972 impl->shortcuts_drag_outside_idle = NULL;
2976 /* GtkWidget::drag-end handler for the shortcuts list. */
2978 shortcuts_drag_end_cb (GtkWidget *widget,
2979 GdkDragContext *context,
2980 GtkFileChooserDefault *impl)
2983 g_object_unref (impl->shortcuts_drag_context);
2985 shortcuts_cancel_drag_outside_idle (impl);
2987 if (!impl->shortcuts_drag_outside)
2990 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2992 impl->shortcuts_drag_outside = FALSE;
2996 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2998 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2999 GdkDragContext *context,
3000 GtkFileChooserDefault *impl)
3002 g_signal_stop_emission_by_name (widget, "drag-data-delete");
3006 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3010 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3013 GtkTreeView *tree_view;
3016 GdkPixmap *row_pixmap;
3021 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3023 /* Find the selected path and get its drag pixmap */
3025 if (!shortcuts_get_selected (impl, &iter))
3026 g_assert_not_reached ();
3028 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3030 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3031 gtk_tree_path_free (path);
3040 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3046 GdkPixmap *composite;
3047 int row_pixmap_width, row_pixmap_height;
3048 int pixbuf_width, pixbuf_height;
3049 int composite_width, composite_height;
3050 int pixbuf_x, pixbuf_y;
3051 GdkGC *gc, *mask_gc;
3053 GdkBitmap *pixbuf_mask;
3055 /* Create pixmap and mask for composite image */
3057 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3058 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3059 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3061 composite_width = MAX (row_pixmap_width, pixbuf_width);
3062 composite_height = MAX (row_pixmap_height, pixbuf_height);
3064 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3066 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3069 pixbuf_x = composite_width - pixbuf_width;
3071 pixbuf_y = (composite_height - pixbuf_height) / 2;
3073 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3074 gc = gdk_gc_new (composite);
3076 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3077 mask_gc = gdk_gc_new (mask);
3079 gdk_gc_set_foreground (mask_gc, &color);
3080 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3083 color.green = 0xffff;
3084 color.blue = 0xffff;
3085 gdk_gc_set_rgb_fg_color (gc, &color);
3086 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3088 /* Composite the row pixmap and the pixbuf */
3090 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3092 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3093 NULL, &pixbuf_mask, 128);
3094 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3097 pixbuf_width, pixbuf_height);
3098 g_object_unref (pixbuf_mask);
3100 gdk_draw_drawable (composite, gc, row_pixmap,
3103 row_pixmap_width, row_pixmap_height);
3105 gdk_gc_set_foreground (mask_gc, &color);
3106 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3108 gdk_draw_pixbuf (composite, gc, pixbuf,
3111 pixbuf_width, pixbuf_height,
3115 g_object_unref (pixbuf);
3116 g_object_unref (row_pixmap);
3118 row_pixmap = composite;
3122 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3124 gtk_tree_view_get_path_at_pos (tree_view,
3125 tree_view->priv->press_start_x,
3126 tree_view->priv->press_start_y,
3132 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3133 gdk_drawable_get_colormap (row_pixmap),
3136 tree_view->priv->press_start_x + 1,
3137 row_pixmap_y + cell_y + 1);
3139 g_object_unref (row_pixmap);
3141 g_object_unref (mask);
3144 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3145 * handler so that we can tell apart the drag_leave event that comes right
3146 * before a drag_drop, from a normal drag_leave. We don't want to set the
3147 * cursor nor the flag in the latter case.
3150 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3152 GDK_THREADS_ENTER ();
3154 shortcuts_drag_set_delete_cursor (impl, TRUE);
3155 impl->shortcuts_drag_outside = TRUE;
3157 shortcuts_cancel_drag_outside_idle (impl);
3159 GDK_THREADS_LEAVE ();
3165 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3169 shortcuts_drag_leave_cb (GtkWidget *widget,
3170 GdkDragContext *context,
3172 GtkFileChooserDefault *impl)
3175 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3177 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3178 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3179 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3181 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3185 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3187 GTK_TREE_VIEW_DROP_BEFORE);
3189 g_signal_stop_emission_by_name (widget, "drag-leave");
3192 /* Computes the appropriate row and position for dropping */
3194 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3198 GtkTreeViewDropPosition *pos)
3200 GtkTreeView *tree_view;
3201 GtkTreeViewColumn *column;
3205 int bookmarks_index;
3207 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3209 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3211 if (!gtk_tree_view_get_path_at_pos (tree_view,
3213 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3219 row = bookmarks_index + impl->num_bookmarks - 1;
3220 *path = gtk_tree_path_new_from_indices (row, -1);
3221 *pos = GTK_TREE_VIEW_DROP_AFTER;
3225 row = *gtk_tree_path_get_indices (*path);
3226 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3227 gtk_tree_path_free (*path);
3229 if (row < bookmarks_index)
3231 row = bookmarks_index;
3232 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3234 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3236 row = bookmarks_index + impl->num_bookmarks - 1;
3237 *pos = GTK_TREE_VIEW_DROP_AFTER;
3241 if (cell_y < cell.height / 2)
3242 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3244 *pos = GTK_TREE_VIEW_DROP_AFTER;
3247 *path = gtk_tree_path_new_from_indices (row, -1);
3250 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3251 * implement the destination side of DnD by hand, due to limitations in
3252 * GtkTreeView's DnD API.
3255 shortcuts_drag_motion_cb (GtkWidget *widget,
3256 GdkDragContext *context,
3260 GtkFileChooserDefault *impl)
3263 GtkTreeViewDropPosition pos;
3264 GdkDragAction action;
3267 if (gtk_drag_get_source_widget (context) == widget)
3269 shortcuts_cancel_drag_outside_idle (impl);
3271 if (impl->shortcuts_drag_outside)
3273 shortcuts_drag_set_delete_cursor (impl, FALSE);
3274 impl->shortcuts_drag_outside = FALSE;
3279 if (context->suggested_action == GDK_ACTION_COPY ||
3280 (context->actions & GDK_ACTION_COPY) != 0)
3281 action = GDK_ACTION_COPY;
3282 else if (context->suggested_action == GDK_ACTION_MOVE ||
3283 (context->actions & GDK_ACTION_MOVE) != 0)
3284 action = GDK_ACTION_MOVE;
3291 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3292 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3293 gtk_tree_path_free (path);
3297 g_signal_stop_emission_by_name (widget, "drag-motion");
3301 gdk_drag_status (context, action, time_);
3308 /* GtkWidget::drag-drop handler for the shortcuts list. */
3310 shortcuts_drag_drop_cb (GtkWidget *widget,
3311 GdkDragContext *context,
3315 GtkFileChooserDefault *impl)
3318 shortcuts_cancel_drag_outside_idle (impl);
3321 g_signal_stop_emission_by_name (widget, "drag-drop");
3325 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3327 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3328 GtkSelectionData *selection_data,
3334 uris = gtk_selection_data_get_uris (selection_data);
3338 for (i = 0; uris[i]; i++)
3344 file = g_file_new_for_uri (uri);
3346 if (shortcuts_add_bookmark_from_file (impl, file, position))
3349 g_object_unref (file);
3355 /* Reorders the selected bookmark to the specified position */
3357 shortcuts_reorder (GtkFileChooserDefault *impl,
3362 ShortcutType shortcut_type;
3365 int bookmarks_index;
3370 /* Get the selected path */
3372 if (!shortcuts_get_selected (impl, &iter))
3373 g_assert_not_reached ();
3375 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3376 old_position = *gtk_tree_path_get_indices (path);
3377 gtk_tree_path_free (path);
3379 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3380 old_position -= bookmarks_index;
3381 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3383 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3384 SHORTCUTS_COL_NAME, &name,
3385 SHORTCUTS_COL_DATA, &col_data,
3386 SHORTCUTS_COL_TYPE, &shortcut_type,
3388 g_assert (col_data != NULL);
3389 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3392 g_object_ref (file); /* removal below will free file, so we need a new ref */
3394 /* Remove the path from the old position and insert it in the new one */
3396 if (new_position > old_position)
3399 if (old_position == new_position)
3403 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3405 shortcuts_add_bookmark_from_file (impl, file, new_position);
3406 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3409 error_adding_bookmark_dialog (impl, file, error);
3413 g_object_unref (file);
3416 /* Callback used when we get the drag data for the bookmarks list. We add the
3417 * received URIs as bookmarks if they are folders.
3420 shortcuts_drag_data_received_cb (GtkWidget *widget,
3421 GdkDragContext *context,
3424 GtkSelectionData *selection_data,
3429 GtkFileChooserDefault *impl;
3430 GtkTreePath *tree_path;
3431 GtkTreeViewDropPosition tree_pos;
3433 int bookmarks_index;
3435 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3437 /* Compute position */
3439 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3441 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3442 position = *gtk_tree_path_get_indices (tree_path);
3443 gtk_tree_path_free (tree_path);
3445 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3448 g_assert (position >= bookmarks_index);
3449 position -= bookmarks_index;
3451 if (gtk_targets_include_uri (&selection_data->target, 1))
3452 shortcuts_drop_uris (impl, selection_data, position);
3453 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3454 shortcuts_reorder (impl, position);
3456 g_signal_stop_emission_by_name (widget, "drag-data-received");
3459 /* Callback used to display a tooltip in the shortcuts tree */
3461 shortcuts_query_tooltip_cb (GtkWidget *widget,
3464 gboolean keyboard_mode,
3465 GtkTooltip *tooltip,
3466 GtkFileChooserDefault *impl)
3468 GtkTreeModel *model;
3471 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3479 ShortcutType shortcut_type;
3481 gtk_tree_model_get (model, &iter,
3482 SHORTCUTS_COL_DATA, &col_data,
3483 SHORTCUTS_COL_TYPE, &shortcut_type,
3486 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3488 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3492 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3497 file = G_FILE (col_data);
3498 parse_name = g_file_get_parse_name (file);
3500 gtk_tooltip_set_text (tooltip, parse_name);
3502 g_free (parse_name);
3506 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3510 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3520 /* Callback used when the selection in the shortcuts tree changes */
3522 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3523 GtkFileChooserDefault *impl)
3526 GtkTreeIter child_iter;
3528 bookmarks_check_remove_sensitivity (impl);
3529 shortcuts_check_popup_sensitivity (impl);
3531 if (impl->changing_folder)
3534 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3536 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3539 shortcuts_activate_iter (impl, &child_iter);
3544 shortcuts_row_separator_func (GtkTreeModel *model,
3548 ShortcutType shortcut_type;
3550 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3552 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3556 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3558 GtkFileChooserDefault *impl)
3562 /* don't screw up focus switching with Tab */
3563 if (event->keyval == GDK_Tab
3564 || event->keyval == GDK_KP_Tab
3565 || event->keyval == GDK_ISO_Left_Tab
3566 || event->length < 1)
3569 if (impl->location_entry)
3570 entry = impl->location_entry;
3571 else if (impl->search_entry)
3572 entry = impl->search_entry;
3578 gtk_widget_grab_focus (entry);
3579 return gtk_widget_event (entry, (GdkEvent *) event);
3585 /* Callback used when the file list's popup menu is detached */
3587 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3590 GtkFileChooserDefault *impl;
3592 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3593 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3595 impl->browse_shortcuts_popup_menu = NULL;
3596 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3597 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3601 remove_shortcut_cb (GtkMenuItem *item,
3602 GtkFileChooserDefault *impl)
3604 remove_selected_bookmarks (impl);
3607 /* Rename the selected bookmark */
3609 rename_selected_bookmark (GtkFileChooserDefault *impl)
3613 GtkTreeViewColumn *column;
3614 GtkCellRenderer *cell;
3617 if (shortcuts_get_selected (impl, &iter))
3619 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3620 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3621 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3622 cell = g_list_nth_data (renderers, 1);
3623 g_list_free (renderers);
3624 g_object_set (cell, "editable", TRUE, NULL);
3625 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3626 path, column, cell, TRUE);
3627 gtk_tree_path_free (path);
3632 rename_shortcut_cb (GtkMenuItem *item,
3633 GtkFileChooserDefault *impl)
3635 rename_selected_bookmark (impl);
3638 /* Constructs the popup menu for the file list if needed */
3640 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3644 if (impl->browse_shortcuts_popup_menu)
3647 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3648 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3649 impl->browse_shortcuts_tree_view,
3650 shortcuts_popup_menu_detach_cb);
3652 item = gtk_image_menu_item_new_with_label (_("Remove"));
3653 impl->browse_shortcuts_popup_menu_remove_item = item;
3654 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3655 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3656 g_signal_connect (item, "activate",
3657 G_CALLBACK (remove_shortcut_cb), impl);
3658 gtk_widget_show (item);
3659 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3661 item = gtk_menu_item_new_with_label (_("Rename..."));
3662 impl->browse_shortcuts_popup_menu_rename_item = item;
3663 g_signal_connect (item, "activate",
3664 G_CALLBACK (rename_shortcut_cb), impl);
3665 gtk_widget_show (item);
3666 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3670 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3672 shortcuts_build_popup_menu (impl);
3673 shortcuts_check_popup_sensitivity (impl);
3677 popup_position_func (GtkMenu *menu,
3681 gpointer user_data);
3684 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3685 GdkEventButton *event)
3687 shortcuts_update_popup_menu (impl);
3689 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3690 NULL, NULL, NULL, NULL,
3691 event->button, event->time);
3694 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3696 popup_position_func, impl->browse_shortcuts_tree_view,
3697 0, GDK_CURRENT_TIME);
3698 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3703 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3705 shortcuts_popup_menu_cb (GtkWidget *widget,
3706 GtkFileChooserDefault *impl)
3708 shortcuts_popup_menu (impl, NULL);
3712 /* Callback used when a button is pressed on the shortcuts list.
3713 * We trap button 3 to bring up a popup menu.
3716 shortcuts_button_press_event_cb (GtkWidget *widget,
3717 GdkEventButton *event,
3718 GtkFileChooserDefault *impl)
3720 static gboolean in_press = FALSE;
3726 if (event->button != 3)
3730 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3736 shortcuts_popup_menu (impl, event);
3741 shortcuts_edited (GtkCellRenderer *cell,
3744 GtkFileChooserDefault *impl)
3750 g_object_set (cell, "editable", FALSE, NULL);
3752 path = gtk_tree_path_new_from_string (path_string);
3753 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3754 g_assert_not_reached ();
3756 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3757 SHORTCUTS_COL_DATA, &shortcut,
3759 gtk_tree_path_free (path);
3761 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3765 shortcuts_editing_canceled (GtkCellRenderer *cell,
3766 GtkFileChooserDefault *impl)
3768 g_object_set (cell, "editable", FALSE, NULL);
3771 /* Creates the widgets for the shortcuts and bookmarks tree */
3773 shortcuts_list_create (GtkFileChooserDefault *impl)
3776 GtkTreeSelection *selection;
3777 GtkTreeViewColumn *column;
3778 GtkCellRenderer *renderer;
3780 /* Target types for dragging a row to/from the shortcuts list */
3781 const GtkTargetEntry tree_model_row_targets[] = {
3782 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3785 /* Scrolled window */
3787 swin = gtk_scrolled_window_new (NULL, NULL);
3788 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3789 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3790 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3792 gtk_widget_show (swin);
3796 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3797 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3798 #ifdef PROFILE_FILE_CHOOSER
3799 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3802 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3804 * 1. user brings up a SAVE dialog
3805 * 2. user clicks on a shortcut in the shortcuts pane
3806 * 3. user starts typing a filename
3808 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3809 * support interactive search. However, we'd rather focus the location entry
3810 * so that the user can type *there*.
3812 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3813 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3814 * if the user starts typing while the focus is in the shortcuts pane.
3816 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3817 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3819 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3820 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3821 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3822 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3823 /* Accessible object name for the file chooser's shortcuts pane */
3824 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3826 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3828 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3830 tree_model_row_targets,
3831 G_N_ELEMENTS (tree_model_row_targets),
3834 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3835 GTK_DEST_DEFAULT_ALL,
3836 tree_model_row_targets,
3837 G_N_ELEMENTS (tree_model_row_targets),
3838 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3839 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3841 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3842 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3843 gtk_tree_selection_set_select_function (selection,
3844 shortcuts_select_func,
3847 g_signal_connect (selection, "changed",
3848 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3850 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3851 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3853 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3854 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3855 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3856 G_CALLBACK (shortcuts_drag_end_cb), impl);
3857 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3858 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3860 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3861 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3862 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3863 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3864 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3865 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3866 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3867 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3869 /* Support tooltips */
3870 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3871 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3872 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3874 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3875 gtk_widget_show (impl->browse_shortcuts_tree_view);
3879 column = gtk_tree_view_column_new ();
3880 /* Column header for the file chooser's shortcuts pane */
3881 gtk_tree_view_column_set_title (column, _("_Places"));
3883 renderer = gtk_cell_renderer_pixbuf_new ();
3884 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3885 gtk_tree_view_column_set_attributes (column, renderer,
3886 "pixbuf", SHORTCUTS_COL_PIXBUF,
3887 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3890 renderer = gtk_cell_renderer_text_new ();
3891 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
3892 g_signal_connect (renderer, "edited",
3893 G_CALLBACK (shortcuts_edited), impl);
3894 g_signal_connect (renderer, "editing-canceled",
3895 G_CALLBACK (shortcuts_editing_canceled), impl);
3896 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3897 gtk_tree_view_column_set_attributes (column, renderer,
3898 "text", SHORTCUTS_COL_NAME,
3901 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3902 shortcuts_row_separator_func,
3905 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3910 /* Creates the widgets for the shortcuts/bookmarks pane */
3912 shortcuts_pane_create (GtkFileChooserDefault *impl,
3913 GtkSizeGroup *size_group)
3919 vbox = gtk_vbox_new (FALSE, 6);
3920 gtk_widget_show (vbox);
3922 /* Shortcuts tree */
3924 widget = shortcuts_list_create (impl);
3925 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3927 /* Box for buttons */
3929 hbox = gtk_hbox_new (TRUE, 6);
3930 gtk_size_group_add_widget (size_group, hbox);
3931 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3932 gtk_widget_show (hbox);
3934 /* Add bookmark button */
3936 impl->browse_shortcuts_add_button = button_new (impl,
3941 G_CALLBACK (add_bookmark_button_clicked_cb));
3942 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3943 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3944 _("Add the selected folder to the Bookmarks"));
3946 /* Remove bookmark button */
3948 impl->browse_shortcuts_remove_button = button_new (impl,
3953 G_CALLBACK (remove_bookmark_button_clicked_cb));
3954 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3955 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3956 _("Remove the selected bookmark"));
3962 key_is_left_or_right (GdkEventKey *event)
3966 modifiers = gtk_accelerator_get_default_mod_mask ();
3968 return ((event->keyval == GDK_Right
3969 || event->keyval == GDK_KP_Right
3970 || event->keyval == GDK_Left
3971 || event->keyval == GDK_KP_Left)
3972 && (event->state & modifiers) == 0);
3975 /* Handles key press events on the file list, so that we can trap Enter to
3976 * activate the default button on our own. Also, checks to see if '/' has been
3977 * pressed. See comment by tree_view_keybinding_cb() for more details.
3980 browse_files_key_press_event_cb (GtkWidget *widget,
3984 GtkFileChooserDefault *impl;
3987 impl = (GtkFileChooserDefault *) data;
3989 modifiers = gtk_accelerator_get_default_mod_mask ();
3991 if ((event->keyval == GDK_slash
3992 || event->keyval == GDK_KP_Divide
3994 || event->keyval == GDK_asciitilde
3996 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3998 location_popup_handler (impl, event->string);
4002 if (key_is_left_or_right (event))
4004 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
4008 if ((event->keyval == GDK_Return
4009 || event->keyval == GDK_ISO_Enter
4010 || event->keyval == GDK_KP_Enter
4011 || event->keyval == GDK_space
4012 || event->keyval == GDK_KP_Space)
4013 && ((event->state & modifiers) == 0)
4014 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4015 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4019 window = get_toplevel (widget);
4021 && widget != window->default_widget
4022 && !(widget == window->focus_widget &&
4023 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4025 gtk_window_activate_default (window);
4033 /* Callback used when the file list's popup menu is detached */
4035 popup_menu_detach_cb (GtkWidget *attach_widget,
4038 GtkFileChooserDefault *impl;
4040 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4041 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4043 impl->browse_files_popup_menu = NULL;
4044 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4045 impl->browse_files_popup_menu_hidden_files_item = NULL;
4048 /* Callback used when the "Add to Bookmarks" menu item is activated */
4050 add_to_shortcuts_cb (GtkMenuItem *item,
4051 GtkFileChooserDefault *impl)
4053 bookmarks_add_selected_folder (impl);
4056 /* Callback used when the "Show Hidden Files" menu item is toggled */
4058 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4059 GtkFileChooserDefault *impl)
4062 "show-hidden", gtk_check_menu_item_get_active (item),
4066 /* Callback used when the "Show Size Column" menu item is toggled */
4068 show_size_column_toggled_cb (GtkCheckMenuItem *item,
4069 GtkFileChooserDefault *impl)
4071 impl->show_size_column = gtk_check_menu_item_get_active (item);
4073 if (impl->list_size_column)
4074 gtk_tree_view_column_set_visible (impl->list_size_column,
4075 impl->show_size_column);
4078 /* Shows an error dialog about not being able to select a dragged file */
4080 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4085 _("Could not select file"),
4090 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4095 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4097 for (i = 1; uris[i]; i++)
4100 GError *error = NULL;
4103 file = g_file_new_for_uri (uri);
4105 gtk_file_chooser_default_select_file (chooser, file, &error);
4107 error_selecting_dragged_file_dialog (impl, file, error);
4109 g_object_unref (file);
4113 struct FileListDragData
4115 GtkFileChooserDefault *impl;
4121 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4123 const GError *error,
4126 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4127 struct FileListDragData *data = user_data;
4128 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4130 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4133 data->impl->file_list_drag_data_received_cancellable = NULL;
4135 if (cancelled || error)
4138 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4139 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4140 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4141 change_folder_and_display_error (data->impl, data->file, FALSE);
4144 GError *error = NULL;
4146 gtk_file_chooser_default_unselect_all (chooser);
4147 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4149 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4151 browse_files_center_selected_row (data->impl);
4154 if (data->impl->select_multiple)
4155 file_list_drag_data_select_uris (data->impl, data->uris);
4158 g_object_unref (data->impl);
4159 g_strfreev (data->uris);
4160 g_object_unref (data->file);
4163 g_object_unref (cancellable);
4167 file_list_drag_data_received_cb (GtkWidget *widget,
4168 GdkDragContext *context,
4171 GtkSelectionData *selection_data,
4176 GtkFileChooserDefault *impl;
4177 GtkFileChooser *chooser;
4182 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4183 chooser = GTK_FILE_CHOOSER (data);
4185 /* Allow only drags from other widgets; see bug #533891. */
4186 if (gtk_drag_get_source_widget (context) == widget)
4188 g_signal_stop_emission_by_name (widget, "drag-data-received");
4192 /* Parse the text/uri-list string, navigate to the first one */
4193 uris = gtk_selection_data_get_uris (selection_data);
4194 if (uris && uris[0])
4196 struct FileListDragData *data;
4199 file = g_file_new_for_uri (uri);
4201 data = g_new0 (struct FileListDragData, 1);
4202 data->impl = g_object_ref (impl);
4206 if (impl->file_list_drag_data_received_cancellable)
4207 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4209 impl->file_list_drag_data_received_cancellable =
4210 _gtk_file_system_get_info (impl->file_system, file,
4212 file_list_drag_data_received_get_info_cb,
4216 g_signal_stop_emission_by_name (widget, "drag-data-received");
4219 /* Don't do anything with the drag_drop signal */
4221 file_list_drag_drop_cb (GtkWidget *widget,
4222 GdkDragContext *context,
4226 GtkFileChooserDefault *impl)
4228 g_signal_stop_emission_by_name (widget, "drag-drop");
4232 /* Disable the normal tree drag motion handler, it makes it look like you're
4233 dropping the dragged item onto a tree item */
4235 file_list_drag_motion_cb (GtkWidget *widget,
4236 GdkDragContext *context,
4240 GtkFileChooserDefault *impl)
4242 g_signal_stop_emission_by_name (widget, "drag-motion");
4246 /* Constructs the popup menu for the file list if needed */
4248 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4252 if (impl->browse_files_popup_menu)
4255 impl->browse_files_popup_menu = gtk_menu_new ();
4256 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4257 impl->browse_files_tree_view,
4258 popup_menu_detach_cb);
4260 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4261 impl->browse_files_popup_menu_add_shortcut_item = item;
4262 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4263 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4264 g_signal_connect (item, "activate",
4265 G_CALLBACK (add_to_shortcuts_cb), impl);
4266 gtk_widget_show (item);
4267 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4269 item = gtk_separator_menu_item_new ();
4270 gtk_widget_show (item);
4271 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4273 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4274 impl->browse_files_popup_menu_hidden_files_item = item;
4275 g_signal_connect (item, "toggled",
4276 G_CALLBACK (show_hidden_toggled_cb), impl);
4277 gtk_widget_show (item);
4278 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4280 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4281 impl->browse_files_popup_menu_size_column_item = item;
4282 g_signal_connect (item, "toggled",
4283 G_CALLBACK (show_size_column_toggled_cb), impl);
4284 gtk_widget_show (item);
4285 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4287 bookmarks_check_add_sensitivity (impl);
4290 /* Updates the popup menu for the file list, creating it if necessary */
4292 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4294 file_list_build_popup_menu (impl);
4296 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4298 /* The sensitivity of the Add to Bookmarks item is set in
4299 * bookmarks_check_add_sensitivity()
4302 /* 'Show Hidden Files' */
4303 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4304 G_CALLBACK (show_hidden_toggled_cb), impl);
4305 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4307 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4308 G_CALLBACK (show_hidden_toggled_cb), impl);
4310 /* 'Show Size Column' */
4311 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4312 G_CALLBACK (show_size_column_toggled_cb), impl);
4313 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4314 impl->show_size_column);
4315 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4316 G_CALLBACK (show_size_column_toggled_cb), impl);
4320 popup_position_func (GtkMenu *menu,
4326 GtkWidget *widget = GTK_WIDGET (user_data);
4327 GdkScreen *screen = gtk_widget_get_screen (widget);
4330 GdkRectangle monitor;
4332 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4334 gdk_window_get_origin (widget->window, x, y);
4336 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4338 *x += (widget->allocation.width - req.width) / 2;
4339 *y += (widget->allocation.height - req.height) / 2;
4341 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4342 gtk_menu_set_monitor (menu, monitor_num);
4343 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4345 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4346 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4352 file_list_popup_menu (GtkFileChooserDefault *impl,
4353 GdkEventButton *event)
4355 file_list_update_popup_menu (impl);
4357 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4358 NULL, NULL, NULL, NULL,
4359 event->button, event->time);
4362 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4364 popup_position_func, impl->browse_files_tree_view,
4365 0, GDK_CURRENT_TIME);
4366 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4372 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4374 list_popup_menu_cb (GtkWidget *widget,
4375 GtkFileChooserDefault *impl)
4377 file_list_popup_menu (impl, NULL);
4381 /* Callback used when a button is pressed on the file list. We trap button 3 to
4382 * bring up a popup menu.
4385 list_button_press_event_cb (GtkWidget *widget,
4386 GdkEventButton *event,
4387 GtkFileChooserDefault *impl)
4389 static gboolean in_press = FALSE;
4395 if (event->button != 3)
4399 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4402 file_list_popup_menu (impl, event);
4407 OperationMode operation_mode;
4408 gint general_column;
4412 /* Sets the sort column IDs for the file list based on the operation mode */
4414 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4416 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4417 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4418 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4422 file_list_query_tooltip_cb (GtkWidget *widget,
4425 gboolean keyboard_tip,
4426 GtkTooltip *tooltip,
4429 GtkFileChooserDefault *impl = user_data;
4430 GtkTreeModel *model;
4436 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4440 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4443 &model, &path, &iter))
4446 gtk_tree_model_get (model, &iter,
4447 MODEL_COL_FILE, &file,
4452 gtk_tree_path_free (path);
4456 filename = g_file_get_path (file);
4457 gtk_tooltip_set_text (tooltip, filename);
4458 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4463 g_object_unref (file);
4464 gtk_tree_path_free (path);
4469 /* Creates the widgets for the file list */
4471 create_file_list (GtkFileChooserDefault *impl)
4474 GtkTreeSelection *selection;
4475 GtkTreeViewColumn *column;
4476 GtkCellRenderer *renderer;
4478 /* Scrolled window */
4479 swin = gtk_scrolled_window_new (NULL, NULL);
4480 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4481 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4482 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4485 /* Tree/list view */
4487 impl->browse_files_tree_view = gtk_tree_view_new ();
4488 #ifdef PROFILE_FILE_CHOOSER
4489 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4491 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4492 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4494 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4495 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4497 gtk_drag_dest_set (impl->browse_files_tree_view,
4498 GTK_DEST_DEFAULT_ALL,
4500 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4501 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4503 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4504 G_CALLBACK (list_row_activated), impl);
4505 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4506 G_CALLBACK (browse_files_key_press_event_cb), impl);
4507 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4508 G_CALLBACK (list_popup_menu_cb), impl);
4509 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4510 G_CALLBACK (list_button_press_event_cb), impl);
4512 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4513 G_CALLBACK (file_list_drag_data_received_cb), impl);
4514 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4515 G_CALLBACK (file_list_drag_drop_cb), impl);
4516 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4517 G_CALLBACK (file_list_drag_motion_cb), impl);
4519 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4520 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4521 G_CALLBACK (file_list_query_tooltip_cb), impl);
4523 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4524 gtk_tree_selection_set_select_function (selection,
4527 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4530 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4531 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4533 g_signal_connect (selection, "changed",
4534 G_CALLBACK (list_selection_changed), impl);
4536 /* Filename column */
4538 impl->list_name_column = gtk_tree_view_column_new ();
4539 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4540 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4541 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4543 renderer = gtk_cell_renderer_pixbuf_new ();
4544 /* We set a fixed size so that we get an emoty row even if no icons are loaded yet */
4545 gtk_cell_renderer_set_fixed_size (renderer,
4546 renderer->xpad * 2 + impl->icon_size,
4547 renderer->ypad * 2 + impl->icon_size);
4548 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4550 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4551 g_object_set (impl->list_name_renderer,
4552 "ellipsize", PANGO_ELLIPSIZE_END,
4554 g_signal_connect (impl->list_name_renderer, "edited",
4555 G_CALLBACK (renderer_edited_cb), impl);
4556 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4557 G_CALLBACK (renderer_editing_canceled_cb), impl);
4558 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4560 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4564 column = gtk_tree_view_column_new ();
4565 gtk_tree_view_column_set_resizable (column, TRUE);
4566 gtk_tree_view_column_set_title (column, _("Size"));
4568 renderer = gtk_cell_renderer_text_new ();
4569 g_object_set (renderer,
4570 "alignment", PANGO_ALIGN_RIGHT,
4572 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4573 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4574 impl->list_size_column = column;
4576 /* Modification time column */
4578 column = gtk_tree_view_column_new ();
4579 gtk_tree_view_column_set_resizable (column, TRUE);
4580 gtk_tree_view_column_set_title (column, _("Modified"));
4582 renderer = gtk_cell_renderer_text_new ();
4583 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4584 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4585 impl->list_mtime_column = column;
4587 file_list_set_sort_column_ids (impl);
4588 update_cell_renderer_attributes (impl);
4590 gtk_widget_show_all (swin);
4596 create_path_bar (GtkFileChooserDefault *impl)
4598 GtkWidget *path_bar;
4600 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4601 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4606 /* Creates the widgets for the files/folders pane */
4608 file_pane_create (GtkFileChooserDefault *impl,
4609 GtkSizeGroup *size_group)
4615 vbox = gtk_vbox_new (FALSE, 6);
4616 gtk_widget_show (vbox);
4618 /* Box for lists and preview */
4620 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4621 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4622 gtk_widget_show (hbox);
4626 widget = create_file_list (impl);
4627 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4631 impl->preview_box = gtk_vbox_new (FALSE, 12);
4632 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4633 /* Don't show preview box initially */
4637 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4639 widget = filter_create (impl);
4641 gtk_widget_show (widget);
4642 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4644 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4645 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4650 /* Callback used when the "Browse for more folders" expander is toggled */
4652 expander_changed_cb (GtkExpander *expander,
4654 GtkFileChooserDefault *impl)
4656 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4657 update_appearance (impl);
4660 /* Callback used when the selection changes in the save folder combo box */
4662 save_folder_combo_changed_cb (GtkComboBox *combo,
4663 GtkFileChooserDefault *impl)
4667 if (impl->changing_folder)
4670 if (gtk_combo_box_get_active_iter (combo, &iter))
4672 GtkTreeIter child_iter;
4674 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4677 shortcuts_activate_iter (impl, &child_iter);
4682 save_folder_update_tooltip (GtkComboBox *combo,
4683 GtkFileChooserDefault *impl)
4690 if (gtk_combo_box_get_active_iter (combo, &iter))
4692 GtkTreeIter child_iter;
4694 ShortcutType shortcut_type;
4696 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4699 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4700 SHORTCUTS_COL_DATA, &col_data,
4701 SHORTCUTS_COL_TYPE, &shortcut_type,
4704 if (shortcut_type == SHORTCUT_TYPE_FILE)
4705 tooltip = g_file_get_parse_name (G_FILE (col_data));
4708 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4709 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4710 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4714 /* Filter function used to filter out the Search item and its separator.
4715 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4718 shortcuts_combo_filter_func (GtkTreeModel *model,
4722 GtkFileChooserDefault *impl;
4723 GtkTreePath *tree_path;
4728 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4730 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4732 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4733 g_assert (tree_path != NULL);
4735 indices = gtk_tree_path_get_indices (tree_path);
4739 if (impl->has_search)
4741 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4742 if (idx == indices[0])
4746 if (impl->has_recent)
4748 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4749 if (idx == indices[0])
4753 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4754 if (idx == indices[0])
4759 gtk_tree_path_free (tree_path);
4764 /* Creates the combo box with the save folders */
4766 save_folder_combo_create (GtkFileChooserDefault *impl)
4769 GtkCellRenderer *cell;
4771 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4772 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4773 shortcuts_combo_filter_func,
4777 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4778 "model", impl->shortcuts_combo_filter_model,
4779 "focus-on-click", FALSE,
4781 gtk_widget_show (combo);
4783 cell = gtk_cell_renderer_pixbuf_new ();
4784 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4785 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4786 "pixbuf", SHORTCUTS_COL_PIXBUF,
4787 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4788 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4791 cell = gtk_cell_renderer_text_new ();
4792 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4793 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4794 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4795 "text", SHORTCUTS_COL_NAME,
4796 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4799 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4800 shortcuts_row_separator_func,
4803 g_signal_connect (combo, "changed",
4804 G_CALLBACK (save_folder_combo_changed_cb), impl);
4805 g_signal_connect (combo, "changed",
4806 G_CALLBACK (save_folder_update_tooltip), impl);
4811 /* Creates the widgets specific to Save mode */
4813 save_widgets_create (GtkFileChooserDefault *impl)
4818 GtkWidget *alignment;
4820 if (impl->save_widgets != NULL)
4823 location_switch_to_path_bar (impl);
4825 vbox = gtk_vbox_new (FALSE, 12);
4827 table = gtk_table_new (2, 2, FALSE);
4828 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4829 gtk_widget_show (table);
4830 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4831 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4835 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4836 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4837 gtk_table_attach (GTK_TABLE (table), widget,
4841 gtk_widget_show (widget);
4843 /* Location entry */
4845 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4846 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4848 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4849 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4850 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4851 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4853 GTK_EXPAND | GTK_FILL, 0,
4855 gtk_widget_show (impl->location_entry);
4856 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4859 impl->save_folder_label = gtk_label_new (NULL);
4860 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4861 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4865 gtk_widget_show (impl->save_folder_label);
4867 impl->save_folder_combo = save_folder_combo_create (impl);
4868 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4870 GTK_EXPAND | GTK_FILL, GTK_FILL,
4872 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4875 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4876 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4878 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4879 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4880 g_signal_connect (impl->save_expander, "notify::expanded",
4881 G_CALLBACK (expander_changed_cb),
4883 gtk_widget_show_all (alignment);
4885 impl->save_widgets = vbox;
4886 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4887 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4888 gtk_widget_show (impl->save_widgets);
4891 /* Destroys the widgets specific to Save mode */
4893 save_widgets_destroy (GtkFileChooserDefault *impl)
4895 if (impl->save_widgets == NULL)
4898 gtk_widget_destroy (impl->save_widgets);
4899 impl->save_widgets = NULL;
4900 impl->location_entry = NULL;
4901 impl->save_folder_label = NULL;
4902 impl->save_folder_combo = NULL;
4903 impl->save_expander = NULL;
4906 /* Turns on the path bar widget. Can be called even if we are already in that
4910 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4912 if (impl->location_entry)
4914 gtk_widget_destroy (impl->location_entry);
4915 impl->location_entry = NULL;
4918 gtk_widget_hide (impl->location_entry_box);
4921 /* Sets the full path of the current folder as the text in the location entry. */
4923 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4925 gchar *text, *filename;
4927 if (!impl->current_folder)
4930 filename = g_file_get_path (impl->current_folder);
4934 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4938 text = g_file_get_uri (impl->current_folder);
4942 gboolean need_slash;
4945 len = strlen (text);
4946 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4952 slash_text = g_new (char, len + 2);
4953 strcpy (slash_text, text);
4954 slash_text[len] = G_DIR_SEPARATOR;
4955 slash_text[len + 1] = 0;
4961 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4968 /* Turns on the location entry. Can be called even if we are already in that
4972 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4974 /* when in search or recent files mode, we are not showing the
4975 * location_entry_box container, so there's no point in switching
4978 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4979 impl->operation_mode == OPERATION_MODE_RECENT)
4982 if (impl->location_entry)
4983 gtk_widget_destroy (impl->location_entry);
4987 gtk_widget_show (impl->location_entry_box);
4991 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4992 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4994 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4995 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4997 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4998 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5000 /* Configure the entry */
5002 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5006 gtk_widget_show (impl->location_entry);
5007 gtk_widget_grab_focus (impl->location_entry);
5010 /* Sets a new location mode. set_buttons determines whether the toggle button
5011 * for the mode will also be changed.
5014 location_mode_set (GtkFileChooserDefault *impl,
5015 LocationMode new_mode,
5016 gboolean set_button)
5018 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5019 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5021 GtkWindow *toplevel;
5022 GtkWidget *current_focus;
5023 gboolean button_active;
5024 gboolean switch_to_file_list;
5028 case LOCATION_MODE_PATH_BAR:
5029 button_active = FALSE;
5031 /* The location_entry will disappear when we switch to path bar mode. So,
5032 * we'll focus the file list in that case, to avoid having a window with
5033 * no focused widget.
5035 toplevel = get_toplevel (GTK_WIDGET (impl));
5036 switch_to_file_list = FALSE;
5039 current_focus = gtk_window_get_focus (toplevel);
5040 if (!current_focus || current_focus == impl->location_entry)
5041 switch_to_file_list = TRUE;
5044 location_switch_to_path_bar (impl);
5046 if (switch_to_file_list)
5047 gtk_widget_grab_focus (impl->browse_files_tree_view);
5051 case LOCATION_MODE_FILENAME_ENTRY:
5052 button_active = TRUE;
5053 location_switch_to_filename_entry (impl);
5057 g_assert_not_reached ();
5063 g_signal_handlers_block_by_func (impl->location_button,
5064 G_CALLBACK (location_button_toggled_cb), impl);
5066 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5068 g_signal_handlers_unblock_by_func (impl->location_button,
5069 G_CALLBACK (location_button_toggled_cb), impl);
5073 impl->location_mode = new_mode;
5077 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5079 /* when in search or recent files mode, we are not showing the
5080 * location_entry_box container, so there's no point in switching
5083 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5084 impl->operation_mode == OPERATION_MODE_RECENT)
5087 /* If the file entry is not visible, show it.
5088 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5090 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5092 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5094 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5096 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5098 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5102 gtk_widget_grab_focus (impl->location_entry);
5107 /* Callback used when one of the location mode buttons is toggled */
5109 location_button_toggled_cb (GtkToggleButton *toggle,
5110 GtkFileChooserDefault *impl)
5113 LocationMode new_mode;
5115 is_active = gtk_toggle_button_get_active (toggle);
5119 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5120 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5124 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5125 new_mode = LOCATION_MODE_PATH_BAR;
5128 location_mode_set (impl, new_mode, FALSE);
5131 /* Creates a toggle button for the location entry. */
5133 location_button_create (GtkFileChooserDefault *impl)
5138 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5139 gtk_widget_show (image);
5141 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5145 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5147 g_signal_connect (impl->location_button, "toggled",
5148 G_CALLBACK (location_button_toggled_cb), impl);
5150 str = _("Type a file name");
5152 gtk_widget_set_tooltip_text (impl->location_button, str);
5153 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5156 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5158 browse_widgets_create (GtkFileChooserDefault *impl)
5163 GtkSizeGroup *size_group;
5165 /* size group is used by the [+][-] buttons and the filter combo */
5166 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5167 vbox = gtk_vbox_new (FALSE, 12);
5169 /* Location widgets */
5170 impl->browse_path_bar_hbox = gtk_hbox_new (FALSE, 12);
5171 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5172 gtk_widget_show (impl->browse_path_bar_hbox);
5174 /* Size group that allows the path bar to be the same size between modes */
5175 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5176 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5178 /* Location button */
5180 location_button_create (impl);
5181 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5185 impl->browse_path_bar = create_path_bar (impl);
5186 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5187 gtk_widget_show_all (impl->browse_path_bar);
5188 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5191 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5192 g_signal_connect (impl->browse_new_folder_button, "clicked",
5193 G_CALLBACK (new_folder_button_clicked), impl);
5194 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5196 /* Box for the location label and entry */
5198 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5199 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5201 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5202 gtk_widget_show (impl->location_label);
5203 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5206 hpaned = gtk_hpaned_new ();
5207 gtk_widget_show (hpaned);
5208 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5210 widget = shortcuts_pane_create (impl, size_group);
5211 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5212 widget = file_pane_create (impl, size_group);
5213 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5215 g_object_unref (size_group);
5221 gtk_file_chooser_default_constructor (GType type,
5222 guint n_construct_properties,
5223 GObjectConstructParam *construct_params)
5225 GtkFileChooserDefault *impl;
5228 profile_start ("start", NULL);
5230 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5231 n_construct_properties,
5233 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5235 g_assert (impl->file_system);
5237 gtk_widget_push_composite_child ();
5239 /* Shortcuts model */
5240 shortcuts_model_create (impl);
5242 /* The browse widgets */
5243 impl->browse_widgets = browse_widgets_create (impl);
5244 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5246 /* Alignment to hold extra widget */
5247 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5248 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5250 gtk_widget_pop_composite_child ();
5251 update_appearance (impl);
5253 profile_end ("end", NULL);
5258 /* Sets the extra_widget by packing it in the appropriate place */
5260 set_extra_widget (GtkFileChooserDefault *impl,
5261 GtkWidget *extra_widget)
5265 g_object_ref (extra_widget);
5266 /* FIXME: is this right ? */
5267 gtk_widget_show (extra_widget);
5270 if (impl->extra_widget)
5272 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5273 g_object_unref (impl->extra_widget);
5276 impl->extra_widget = extra_widget;
5277 if (impl->extra_widget)
5279 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5280 gtk_widget_show (impl->extra_align);
5283 gtk_widget_hide (impl->extra_align);
5287 set_local_only (GtkFileChooserDefault *impl,
5288 gboolean local_only)
5290 if (local_only != impl->local_only)
5292 impl->local_only = local_only;
5294 if (impl->location_entry)
5295 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5297 if (impl->shortcuts_model && impl->file_system)
5299 shortcuts_add_volumes (impl);
5300 shortcuts_add_bookmarks (impl);
5303 if (local_only && impl->current_folder &&
5304 !g_file_is_native (impl->current_folder))
5306 /* If we are pointing to a non-local folder, make an effort to change
5307 * back to a local folder, but it's really up to the app to not cause
5308 * such a situation, so we ignore errors.
5310 const gchar *home = g_get_home_dir ();
5316 home_file = g_file_new_for_path (home);
5318 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5320 g_object_unref (home_file);
5326 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5327 GtkFileChooserDefault *impl)
5329 shortcuts_add_volumes (impl);
5330 shortcuts_add_bookmarks (impl);
5332 bookmarks_check_add_sensitivity (impl);
5333 bookmarks_check_remove_sensitivity (impl);
5334 shortcuts_check_popup_sensitivity (impl);
5337 /* Sets the file chooser to multiple selection mode */
5339 set_select_multiple (GtkFileChooserDefault *impl,
5340 gboolean select_multiple,
5341 gboolean property_notify)
5343 GtkTreeSelection *selection;
5344 GtkSelectionMode mode;
5346 if (select_multiple == impl->select_multiple)
5349 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5351 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5352 gtk_tree_selection_set_mode (selection, mode);
5354 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5356 impl->select_multiple = select_multiple;
5357 g_object_notify (G_OBJECT (impl), "select-multiple");
5359 check_preview_change (impl);
5363 set_file_system_backend (GtkFileChooserDefault *impl)
5365 profile_start ("start for backend", "default");
5367 impl->file_system = _gtk_file_system_new ();
5369 g_signal_connect (impl->file_system, "volumes-changed",
5370 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5371 g_signal_connect (impl->file_system, "bookmarks-changed",
5372 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5374 profile_end ("end", NULL);
5378 unset_file_system_backend (GtkFileChooserDefault *impl)
5380 g_signal_handlers_disconnect_by_func (impl->file_system,
5381 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5383 g_object_unref (impl->file_system);
5385 impl->file_system = NULL;
5388 /* This function is basically a do_all function.
5390 * It sets the visibility on all the widgets based on the current state, and
5391 * moves the custom_widget if needed.
5394 update_appearance (GtkFileChooserDefault *impl)
5396 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5397 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5401 gtk_widget_hide (impl->location_button);
5402 save_widgets_create (impl);
5404 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5405 text = _("Save in _folder:");
5407 text = _("Create in _folder:");
5409 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5411 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5413 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5414 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5415 gtk_widget_set_has_tooltip (impl->save_folder_combo, FALSE);
5416 gtk_widget_show (impl->browse_widgets);
5420 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5421 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5422 gtk_widget_set_has_tooltip (impl->save_folder_combo, TRUE);
5423 gtk_widget_hide (impl->browse_widgets);
5426 if (impl->select_multiple)
5428 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5429 "Re-setting to single selection mode.");
5430 set_select_multiple (impl, FALSE, TRUE);
5433 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5434 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5436 gtk_widget_show (impl->location_button);
5437 save_widgets_destroy (impl);
5438 gtk_widget_show (impl->browse_widgets);
5439 location_mode_set (impl, impl->location_mode, TRUE);
5442 if (impl->location_entry)
5443 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5445 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5446 gtk_widget_hide (impl->browse_new_folder_button);
5448 gtk_widget_show (impl->browse_new_folder_button);
5450 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5451 * of files may change depending whether we are in a file or folder-only mode.
5453 gtk_widget_queue_draw (impl->browse_files_tree_view);
5455 emit_default_size_changed (impl);
5459 gtk_file_chooser_default_set_property (GObject *object,
5461 const GValue *value,
5465 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5469 case GTK_FILE_CHOOSER_PROP_ACTION:
5471 GtkFileChooserAction action = g_value_get_enum (value);
5473 if (action != impl->action)
5475 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5477 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5478 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5479 && impl->select_multiple)
5481 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5482 "this is not allowed in multiple selection mode. Resetting the file chooser "
5483 "to single selection mode.");
5484 set_select_multiple (impl, FALSE, TRUE);
5486 impl->action = action;
5487 update_cell_renderer_attributes (impl);
5488 update_appearance (impl);
5489 settings_load (impl);
5494 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5495 /* Ignore property */
5498 case GTK_FILE_CHOOSER_PROP_FILTER:
5499 set_current_filter (impl, g_value_get_object (value));
5502 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5503 set_local_only (impl, g_value_get_boolean (value));
5506 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5507 set_preview_widget (impl, g_value_get_object (value));
5510 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5511 impl->preview_widget_active = g_value_get_boolean (value);
5512 update_preview_widget_visibility (impl);
5515 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5516 impl->use_preview_label = g_value_get_boolean (value);
5517 update_preview_widget_visibility (impl);
5520 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5521 set_extra_widget (impl, g_value_get_object (value));
5524 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5526 gboolean select_multiple = g_value_get_boolean (value);
5527 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5528 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5531 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5532 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5533 "leaving the file chooser in single selection mode.");
5537 set_select_multiple (impl, select_multiple, FALSE);
5541 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5543 gboolean show_hidden = g_value_get_boolean (value);
5544 if (show_hidden != impl->show_hidden)
5546 impl->show_hidden = show_hidden;
5548 if (impl->browse_files_model)
5549 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5554 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5556 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5557 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5561 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5563 gboolean create_folders = g_value_get_boolean (value);
5564 impl->create_folders = create_folders;
5565 update_appearance (impl);
5570 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5576 gtk_file_chooser_default_get_property (GObject *object,
5581 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5585 case GTK_FILE_CHOOSER_PROP_ACTION:
5586 g_value_set_enum (value, impl->action);
5589 case GTK_FILE_CHOOSER_PROP_FILTER:
5590 g_value_set_object (value, impl->current_filter);
5593 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5594 g_value_set_boolean (value, impl->local_only);
5597 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5598 g_value_set_object (value, impl->preview_widget);
5601 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5602 g_value_set_boolean (value, impl->preview_widget_active);
5605 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5606 g_value_set_boolean (value, impl->use_preview_label);
5609 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5610 g_value_set_object (value, impl->extra_widget);
5613 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5614 g_value_set_boolean (value, impl->select_multiple);
5617 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5618 g_value_set_boolean (value, impl->show_hidden);
5621 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5622 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5625 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5626 g_value_set_boolean (value, impl->create_folders);
5630 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5635 /* Removes the settings signal handler. It's safe to call multiple times */
5637 remove_settings_signal (GtkFileChooserDefault *impl,
5640 if (impl->settings_signal_id)
5642 GtkSettings *settings;
5644 settings = gtk_settings_get_for_screen (screen);
5645 g_signal_handler_disconnect (settings,
5646 impl->settings_signal_id);
5647 impl->settings_signal_id = 0;
5652 gtk_file_chooser_default_dispose (GObject *object)
5655 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5657 if (impl->extra_widget)
5659 g_object_unref (impl->extra_widget);
5660 impl->extra_widget = NULL;
5663 pending_select_files_free (impl);
5665 /* cancel all pending operations */
5666 if (impl->pending_cancellables)
5668 for (l = impl->pending_cancellables; l; l = l->next)
5670 GCancellable *cancellable = G_CANCELLABLE (l->data);
5671 g_cancellable_cancel (cancellable);
5673 g_slist_free (impl->pending_cancellables);
5674 impl->pending_cancellables = NULL;
5677 if (impl->reload_icon_cancellables)
5679 for (l = impl->reload_icon_cancellables; l; l = l->next)
5681 GCancellable *cancellable = G_CANCELLABLE (l->data);
5682 g_cancellable_cancel (cancellable);
5684 g_slist_free (impl->reload_icon_cancellables);
5685 impl->reload_icon_cancellables = NULL;
5688 if (impl->loading_shortcuts)
5690 for (l = impl->loading_shortcuts; l; l = l->next)
5692 GCancellable *cancellable = G_CANCELLABLE (l->data);
5693 g_cancellable_cancel (cancellable);
5695 g_slist_free (impl->loading_shortcuts);
5696 impl->loading_shortcuts = NULL;
5699 if (impl->file_list_drag_data_received_cancellable)
5701 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5702 impl->file_list_drag_data_received_cancellable = NULL;
5705 if (impl->update_current_folder_cancellable)
5707 g_cancellable_cancel (impl->update_current_folder_cancellable);
5708 impl->update_current_folder_cancellable = NULL;
5711 if (impl->show_and_select_files_cancellable)
5713 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5714 impl->show_and_select_files_cancellable = NULL;
5717 if (impl->should_respond_get_info_cancellable)
5719 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5720 impl->should_respond_get_info_cancellable = NULL;
5723 if (impl->update_from_entry_cancellable)
5725 g_cancellable_cancel (impl->update_from_entry_cancellable);
5726 impl->update_from_entry_cancellable = NULL;
5729 if (impl->shortcuts_activate_iter_cancellable)
5731 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5732 impl->shortcuts_activate_iter_cancellable = NULL;
5735 search_stop_searching (impl, TRUE);
5736 recent_stop_loading (impl);
5738 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5740 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5743 /* We override show-all since we have internal widgets that
5744 * shouldn't be shown when you call show_all(), like the filter
5748 gtk_file_chooser_default_show_all (GtkWidget *widget)
5750 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5752 gtk_widget_show (widget);
5754 if (impl->extra_widget)
5755 gtk_widget_show_all (impl->extra_widget);
5758 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5759 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5762 toplevel_set_focus_cb (GtkWindow *window,
5764 GtkFileChooserDefault *impl)
5766 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5769 /* We monitor the focus widget on our toplevel to be able to know which widget
5770 * was last focused at the time our "should_respond" method gets called.
5773 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5774 GtkWidget *previous_toplevel)
5776 GtkFileChooserDefault *impl;
5777 GtkWidget *toplevel;
5779 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5781 if (previous_toplevel)
5783 g_assert (impl->toplevel_set_focus_id != 0);
5784 g_signal_handler_disconnect (previous_toplevel,
5785 impl->toplevel_set_focus_id);
5786 impl->toplevel_set_focus_id = 0;
5787 impl->toplevel_last_focus_widget = NULL;
5790 g_assert (impl->toplevel_set_focus_id == 0);
5792 toplevel = gtk_widget_get_toplevel (widget);
5793 if (GTK_IS_WINDOW (toplevel))
5795 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5796 G_CALLBACK (toplevel_set_focus_cb), impl);
5797 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5801 /* Changes the icons wherever it is needed */
5803 change_icon_theme (GtkFileChooserDefault *impl)
5805 GtkSettings *settings;
5807 GtkCellRenderer *renderer;
5810 profile_start ("start", NULL);
5812 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5814 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5815 impl->icon_size = MAX (width, height);
5817 impl->icon_size = FALLBACK_ICON_SIZE;
5819 shortcuts_reload_icons (impl);
5820 /* the first cell in the first column is the icon column, and we have a fixed size there */
5821 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5822 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5823 renderer = GTK_CELL_RENDERER (cells->data);
5824 gtk_cell_renderer_set_fixed_size (renderer,
5825 renderer->xpad * 2 + impl->icon_size,
5826 renderer->ypad * 2 + impl->icon_size);
5827 g_list_free (cells);
5828 if (impl->browse_files_model)
5829 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5830 gtk_widget_queue_resize (impl->browse_files_tree_view);
5832 profile_end ("end", NULL);
5835 /* Callback used when a GtkSettings value changes */
5837 settings_notify_cb (GObject *object,
5839 GtkFileChooserDefault *impl)
5843 profile_start ("start", NULL);
5845 name = g_param_spec_get_name (pspec);
5847 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5848 strcmp (name, "gtk-icon-sizes") == 0)
5849 change_icon_theme (impl);
5851 profile_end ("end", NULL);
5854 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5858 check_icon_theme (GtkFileChooserDefault *impl)
5860 GtkSettings *settings;
5862 profile_start ("start", NULL);
5864 if (impl->settings_signal_id)
5866 profile_end ("end", NULL);
5870 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5872 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5873 impl->settings_signal_id = g_signal_connect (settings, "notify",
5874 G_CALLBACK (settings_notify_cb), impl);
5876 change_icon_theme (impl);
5879 profile_end ("end", NULL);
5883 gtk_file_chooser_default_style_set (GtkWidget *widget,
5884 GtkStyle *previous_style)
5886 GtkFileChooserDefault *impl;
5888 profile_start ("start", NULL);
5890 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5892 profile_msg (" parent class style_set start", NULL);
5893 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5894 profile_msg (" parent class style_set end", NULL);
5896 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5897 change_icon_theme (impl);
5899 emit_default_size_changed (impl);
5901 profile_end ("end", NULL);
5905 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5906 GdkScreen *previous_screen)
5908 GtkFileChooserDefault *impl;
5910 profile_start ("start", NULL);
5912 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5914 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5915 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5917 remove_settings_signal (impl, previous_screen);
5918 check_icon_theme (impl);
5920 emit_default_size_changed (impl);
5922 profile_end ("end", NULL);
5926 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5927 GtkAllocation *allocation)
5929 GtkFileChooserDefault *impl;
5931 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5933 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5937 get_is_file_filtered (GtkFileChooserDefault *impl,
5939 GFileInfo *file_info)
5941 GtkFileFilterInfo filter_info;
5942 GtkFileFilterFlags needed;
5945 if (!impl->current_filter)
5948 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5950 needed = gtk_file_filter_get_needed (impl->current_filter);
5952 filter_info.display_name = g_file_info_get_display_name (file_info);
5953 filter_info.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
5955 if (needed & GTK_FILE_FILTER_FILENAME)
5957 filter_info.filename = g_file_get_path (file);
5958 if (filter_info.filename)
5959 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5962 filter_info.filename = NULL;
5964 if (needed & GTK_FILE_FILTER_URI)
5966 filter_info.uri = g_file_get_uri (file);
5967 if (filter_info.uri)
5968 filter_info.contains |= GTK_FILE_FILTER_URI;
5971 filter_info.uri = NULL;
5973 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5975 g_free ((gchar *)filter_info.filename);
5976 g_free ((gchar *)filter_info.uri);
5977 g_free ((gchar *)filter_info.mime_type);
5983 set_sort_column (GtkFileChooserDefault *impl)
5985 GtkTreeSortable *sortable;
5987 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5988 /* can happen when we're still populating the model */
5989 if (sortable == NULL)
5992 gtk_tree_sortable_set_sort_column_id (sortable,
5998 settings_load (GtkFileChooserDefault *impl)
6000 GtkFileChooserSettings *settings;
6001 LocationMode location_mode;
6002 gboolean show_hidden;
6003 gboolean expand_folders;
6004 gboolean show_size_column;
6006 GtkSortType sort_order;
6008 settings = _gtk_file_chooser_settings_new ();
6010 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
6011 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
6012 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
6013 show_size_column = _gtk_file_chooser_settings_get_show_size_column (settings);
6014 sort_column = _gtk_file_chooser_settings_get_sort_column (settings);
6015 sort_order = _gtk_file_chooser_settings_get_sort_order (settings);
6017 g_object_unref (settings);
6019 location_mode_set (impl, location_mode, TRUE);
6021 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6023 impl->expand_folders = expand_folders;
6024 if (impl->save_expander)
6025 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
6027 impl->show_size_column = show_size_column;
6028 if (impl->list_size_column)
6029 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6031 impl->sort_column = sort_column;
6032 impl->sort_order = sort_order;
6033 /* We don't call set_sort_column() here as the models may not have been
6034 * created yet. The individual functions that create and set the models will
6035 * call set_sort_column() themselves.
6040 save_dialog_geometry (GtkFileChooserDefault *impl, GtkFileChooserSettings *settings)
6042 GtkWindow *toplevel;
6043 int x, y, width, height;
6045 /* We don't save the geometry in non-expanded "save" mode, so that the "little
6046 * dialog" won't make future Open dialogs too small.
6048 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6049 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6050 || impl->expand_folders))
6053 toplevel = get_toplevel (GTK_WIDGET (impl));
6055 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
6058 gtk_window_get_position (toplevel, &x, &y);
6059 gtk_window_get_size (toplevel, &width, &height);
6061 _gtk_file_chooser_settings_set_geometry (settings, x, y, width, height);
6065 settings_save (GtkFileChooserDefault *impl)
6067 GtkFileChooserSettings *settings;
6069 settings = _gtk_file_chooser_settings_new ();
6071 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6072 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6073 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6074 _gtk_file_chooser_settings_set_show_size_column (settings, impl->show_size_column);
6075 _gtk_file_chooser_settings_set_sort_column (settings, impl->sort_column);
6076 _gtk_file_chooser_settings_set_sort_order (settings, impl->sort_order);
6078 save_dialog_geometry (impl, settings);
6081 _gtk_file_chooser_settings_save (settings, NULL);
6083 g_object_unref (settings);
6086 /* GtkWidget::realize method */
6088 gtk_file_chooser_default_realize (GtkWidget *widget)
6090 GtkFileChooserDefault *impl;
6092 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6094 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6096 emit_default_size_changed (impl);
6099 /* GtkWidget::map method */
6101 gtk_file_chooser_default_map (GtkWidget *widget)
6103 GtkFileChooserDefault *impl;
6104 char *current_working_dir;
6106 profile_start ("start", NULL);
6108 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6110 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6112 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6114 switch (impl->reload_state)
6117 /* The user didn't explicitly give us a folder to
6118 * display, so we'll use the cwd
6120 current_working_dir = g_get_current_dir ();
6121 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6122 current_working_dir);
6123 g_free (current_working_dir);
6126 case RELOAD_HAS_FOLDER:
6127 /* Nothing; we are already loading or loaded, so we
6128 * don't need to reload
6132 case RELOAD_WAS_UNMAPPED:
6133 /* Just reload the current folder; else continue
6136 if (impl->current_folder)
6138 pending_select_files_store_selection (impl);
6139 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6144 g_assert_not_reached ();
6148 volumes_bookmarks_changed_cb (impl->file_system, impl);
6150 settings_load (impl);
6152 profile_end ("end", NULL);
6155 /* GtkWidget::unmap method */
6157 gtk_file_chooser_default_unmap (GtkWidget *widget)
6159 GtkFileChooserDefault *impl;
6161 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6163 settings_save (impl);
6165 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6167 impl->reload_state = RELOAD_WAS_UNMAPPED;
6171 list_model_filter_func (GtkFileSystemModel *model,
6173 GFileInfo *file_info,
6176 GtkFileChooserDefault *impl = user_data;
6178 if (!impl->current_filter)
6181 if (_gtk_file_info_consider_as_directory (file_info))
6184 return !get_is_file_filtered (impl, file, file_info);
6188 install_list_model_filter (GtkFileChooserDefault *impl)
6190 GtkFileSystemModelFilter filter;
6193 g_assert (impl->browse_files_model != NULL);
6195 if (impl->current_filter)
6197 filter = list_model_filter_func;
6206 _gtk_file_system_model_set_filter (impl->browse_files_model,
6211 #define COMPARE_DIRECTORIES \
6212 GtkFileChooserDefault *impl = user_data; \
6213 gboolean dir_a, dir_b; \
6215 gtk_tree_model_get (model, a, MODEL_COL_IS_FOLDER, &dir_a, -1); \
6216 gtk_tree_model_get (model, b, MODEL_COL_IS_FOLDER, &dir_b, -1); \
6218 if (dir_a != dir_b) \
6219 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6221 /* Sort callback for the filename column */
6223 name_sort_func (GtkTreeModel *model,
6228 COMPARE_DIRECTORIES;
6231 gchar *key_a, *key_b;
6234 gtk_tree_model_get (model, a, MODEL_COL_NAME_COLLATED, &key_a, -1);
6235 gtk_tree_model_get (model, b, MODEL_COL_NAME_COLLATED, &key_b, -1);
6238 result = strcmp (key_a, key_b);
6253 /* Sort callback for the size column */
6255 size_sort_func (GtkTreeModel *model,
6260 COMPARE_DIRECTORIES;
6263 gint64 size_a, size_b;
6265 gtk_tree_model_get (model, a, MODEL_COL_SIZE, &size_a, -1);
6266 gtk_tree_model_get (model, b, MODEL_COL_SIZE, &size_b, -1);
6268 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6272 /* Sort callback for the mtime column */
6274 mtime_sort_func (GtkTreeModel *model,
6279 COMPARE_DIRECTORIES;
6284 gtk_tree_model_get (model, a, MODEL_COL_MTIME, &ta, -1);
6285 gtk_tree_model_get (model, b, MODEL_COL_MTIME, &tb, -1);
6287 return ta > tb ? -1 : (ta == tb ? 0 : 1);
6291 /* Callback used when the sort column changes. We cache the sort order for use
6292 * in name_sort_func().
6295 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6296 GtkFileChooserDefault *impl)
6298 gint sort_column_id;
6299 GtkSortType sort_type;
6301 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6303 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6304 impl->sort_column = sort_column_id;
6305 impl->sort_order = sort_type;
6310 set_busy_cursor (GtkFileChooserDefault *impl,
6313 GtkWindow *toplevel;
6314 GdkDisplay *display;
6317 toplevel = get_toplevel (GTK_WIDGET (impl));
6318 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6321 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6324 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6328 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6329 gdk_display_flush (display);
6332 gdk_cursor_unref (cursor);
6335 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6337 load_set_model (GtkFileChooserDefault *impl)
6339 profile_start ("start", NULL);
6341 g_assert (impl->browse_files_model != NULL);
6343 profile_msg (" gtk_tree_view_set_model start", NULL);
6344 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6345 GTK_TREE_MODEL (impl->browse_files_model));
6346 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6347 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6349 set_sort_column (impl);
6350 profile_msg (" gtk_tree_view_set_model end", NULL);
6351 impl->list_sort_ascending = TRUE;
6353 profile_end ("end", NULL);
6356 /* Timeout callback used when the loading timer expires */
6358 load_timeout_cb (gpointer data)
6360 GtkFileChooserDefault *impl;
6362 profile_start ("start", NULL);
6364 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6365 g_assert (impl->load_state == LOAD_PRELOAD);
6366 g_assert (impl->load_timeout_id != 0);
6367 g_assert (impl->browse_files_model != NULL);
6369 impl->load_timeout_id = 0;
6370 impl->load_state = LOAD_LOADING;
6372 load_set_model (impl);
6374 profile_end ("end", NULL);
6379 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6381 load_setup_timer (GtkFileChooserDefault *impl)
6383 g_assert (impl->load_timeout_id == 0);
6384 g_assert (impl->load_state != LOAD_PRELOAD);
6386 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6387 impl->load_state = LOAD_PRELOAD;
6390 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6392 load_remove_timer (GtkFileChooserDefault *impl)
6394 if (impl->load_timeout_id != 0)
6396 g_assert (impl->load_state == LOAD_PRELOAD);
6398 g_source_remove (impl->load_timeout_id);
6399 impl->load_timeout_id = 0;
6400 impl->load_state = LOAD_EMPTY;
6403 g_assert (impl->load_state == LOAD_EMPTY ||
6404 impl->load_state == LOAD_LOADING ||
6405 impl->load_state == LOAD_FINISHED);
6408 /* Selects the first row in the file list */
6410 browse_files_select_first_row (GtkFileChooserDefault *impl)
6413 GtkTreeIter dummy_iter;
6414 GtkTreeModel *tree_model;
6416 path = gtk_tree_path_new_from_indices (0, -1);
6417 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6422 /* If the list is empty, do nothing. */
6423 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6424 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6426 gtk_tree_path_free (path);
6429 struct center_selected_row_closure {
6430 GtkFileChooserDefault *impl;
6431 gboolean already_centered;
6434 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6435 * selected row in the tree view.
6438 center_selected_row_foreach_cb (GtkTreeModel *model,
6443 struct center_selected_row_closure *closure;
6446 if (closure->already_centered)
6449 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6450 closure->already_centered = TRUE;
6453 /* Centers the selected row in the tree view */
6455 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6457 struct center_selected_row_closure closure;
6458 GtkTreeSelection *selection;
6460 closure.impl = impl;
6461 closure.already_centered = FALSE;
6463 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6464 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6467 struct ShowAndSelectPathsData
6469 GtkFileChooserDefault *impl;
6474 show_and_select_files_finished_loading (GtkFolder *folder,
6477 gboolean have_hidden;
6478 gboolean have_filtered;
6480 struct ShowAndSelectPathsData *data = user_data;
6482 have_hidden = FALSE;
6483 have_filtered = FALSE;
6485 for (l = data->files; l; l = l->next)
6492 info = _gtk_folder_get_info (folder, file);
6496 have_hidden = g_file_info_get_is_hidden (info)
6497 || g_file_info_get_is_backup (info);
6500 have_filtered = (! _gtk_file_info_consider_as_directory (info)) &&
6501 get_is_file_filtered (data->impl, file, info);
6503 g_object_unref (info);
6505 if (have_hidden && have_filtered)
6506 break; /* we now have all the information we need */
6510 g_signal_handlers_disconnect_by_func (folder,
6511 show_and_select_files_finished_loading,
6515 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6518 set_current_filter (data->impl, NULL);
6520 for (l = data->files; l; l = l->next)
6527 if (!_gtk_file_system_model_get_iter_for_file (data->impl->browse_files_model,
6532 path = gtk_tree_model_get_path (GTK_TREE_MODEL (data->impl->browse_files_model), &iter);
6533 select_func (data->impl->browse_files_model, path, &iter, data->impl);
6534 gtk_tree_path_free (path);
6537 browse_files_center_selected_row (data->impl);
6539 g_object_unref (data->impl);
6540 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6541 g_slist_free (data->files);
6546 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6548 const GError *error,
6551 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6552 struct ShowAndSelectPathsData *data = user_data;
6554 if (data->impl->show_and_select_files_cancellable != cancellable)
6557 data->impl->show_and_select_files_cancellable = NULL;
6559 if (cancelled || error)
6562 g_object_unref (cancellable);
6564 if (_gtk_folder_is_finished_loading (folder))
6565 show_and_select_files_finished_loading (folder, user_data);
6567 g_signal_connect (folder, "finished-loading",
6568 G_CALLBACK (show_and_select_files_finished_loading),
6574 g_object_unref (data->impl);
6575 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6576 g_slist_free (data->files);
6579 g_object_unref (cancellable);
6583 show_and_select_files (GtkFileChooserDefault *impl,
6588 struct ShowAndSelectPathsData *info;
6590 profile_start ("start", NULL);
6594 profile_end ("end", NULL);
6598 info = g_new (struct ShowAndSelectPathsData, 1);
6599 info->impl = g_object_ref (impl);
6600 info->files = g_slist_copy (files);
6601 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6603 if (impl->show_and_select_files_cancellable)
6604 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6606 impl->show_and_select_files_cancellable =
6607 _gtk_file_system_get_folder (impl->file_system, parent_file,
6608 "standard::is-hidden,standard::is-backup,standard::type,standard::name,standard::content-type",
6609 show_and_select_files_get_folder_cb, info);
6611 profile_end ("end", NULL);
6615 /* Processes the pending operation when a folder is finished loading */
6617 pending_select_files_process (GtkFileChooserDefault *impl)
6619 g_assert (impl->load_state == LOAD_FINISHED);
6620 g_assert (impl->browse_files_model != NULL);
6622 if (impl->pending_select_files)
6625 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6626 pending_select_files_free (impl);
6627 browse_files_center_selected_row (impl);
6631 /* We only select the first row if the chooser is actually mapped ---
6632 * selecting the first row is to help the user when he is interacting with
6633 * the chooser, but sometimes a chooser works not on behalf of the user,
6634 * but rather on behalf of something else like GtkFileChooserButton. In
6635 * that case, the chooser's selection should be what the caller expects,
6636 * as the user can't see that something else got selected. See bug #165264.
6638 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6639 browse_files_select_first_row (impl);
6642 g_assert (impl->pending_select_files == NULL);
6645 /* Callback used when the file system model finishes loading */
6647 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6649 GtkFileChooserDefault *impl)
6651 profile_start ("start", NULL);
6653 if (impl->load_state == LOAD_PRELOAD)
6655 load_remove_timer (impl);
6656 load_set_model (impl);
6658 else if (impl->load_state == LOAD_LOADING)
6664 /* We can't g_assert_not_reached(), as something other than us may have
6665 * initiated a folder reload. See #165556.
6667 profile_end ("end", NULL);
6671 g_assert (impl->load_timeout_id == 0);
6673 impl->load_state = LOAD_FINISHED;
6675 pending_select_files_process (impl);
6676 set_busy_cursor (impl, FALSE);
6677 #ifdef PROFILE_FILE_CHOOSER
6678 access ("MARK: *** FINISHED LOADING", F_OK);
6681 profile_end ("end", NULL);
6685 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6687 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6689 if (impl->browse_files_model)
6691 g_object_unref (impl->browse_files_model);
6692 impl->browse_files_model = NULL;
6695 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6699 my_g_format_time_for_display (glong secs)
6704 time_t time_mtime, time_now;
6705 const gchar *format;
6706 gchar *locale_format = NULL;
6708 char *date_str = NULL;
6712 #ifdef HAVE_LOCALTIME_R
6713 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6716 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
6720 g_warning ("ptm != NULL failed");
6722 return g_strdup (_("Unknown"));
6725 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6727 #endif /* HAVE_LOCALTIME_R */
6729 g_date_set_time_t (&mtime, time_mtime);
6730 time_now = time (NULL);
6731 g_date_set_time_t (&now, time_now);
6733 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6735 /* Translators: %H means "hours" and %M means "minutes" */
6737 format = _("%H:%M");
6738 else if (days_diff == 1)
6739 format = _("Yesterday at %H:%M");
6742 if (days_diff > 1 && days_diff < 7)
6743 format = "%A"; /* Days from last week */
6745 format = "%x"; /* Any other date */
6749 /* g_locale_from_utf8() returns a string in the system
6750 * code-page, which is not always the same as that used by the C
6751 * library. For instance when running a GTK+ program with
6752 * LANG=ko on an English version of Windows, the system
6753 * code-page is 1252, but the code-page used by the C library is
6754 * 949. (It's GTK+ itself that sets the C library locale when it
6755 * notices the LANG environment variable. See gtkmain.c The
6756 * Microsoft C library doesn't look at any locale environment
6757 * variables.) We need to pass strftime() a string in the C
6758 * library's code-page. See bug #509885.
6760 locale = setlocale (LC_ALL, NULL);
6762 dot = strchr (locale, '.');
6765 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6767 /* All codepages should fit in 16 bits AFAIK */
6768 if (codepage > 0 && codepage < 65536)
6770 sprintf (charset, "CP%u", (guint) codepage);
6771 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6775 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6777 if (locale_format != NULL &&
6778 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6781 /* As above but in opposite direction... */
6782 if (codepage > 0 && codepage < 65536)
6783 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6785 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6789 if (date_str == NULL)
6790 date_str = g_strdup (_("Unknown"));
6792 g_free (locale_format);
6796 #define copy_attribute(to, from, attribute) G_STMT_START { \
6797 GFileAttributeType type; \
6800 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL)) \
6801 g_file_info_set_attribute (to, attribute, type, value); \
6805 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6807 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6808 GFile *file = G_FILE (object);
6809 GFileInfo *queried, *info;
6812 queried = g_file_query_info_finish (file, res, NULL);
6813 if (queried == NULL)
6816 /* now we know model is valid */
6818 /* file was deleted */
6819 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6822 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6824 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6825 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6826 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6828 _gtk_file_system_model_update_file (model, file, info, FALSE);
6830 g_object_unref (info);
6834 file_system_model_set (GtkFileSystemModel *model,
6841 GtkFileChooserDefault *impl = data;
6845 case MODEL_COL_FILE:
6846 g_value_set_object (value, file);
6848 case MODEL_COL_NAME:
6850 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6852 g_value_set_string (value, g_file_info_get_display_name (info));
6854 case MODEL_COL_NAME_COLLATED:
6856 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6858 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6860 case MODEL_COL_IS_FOLDER:
6861 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6863 case MODEL_COL_PIXBUF:
6866 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6868 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6872 GtkTreeModel *tree_model;
6873 GtkTreePath *path, *start, *end;
6876 if (impl->browse_files_tree_view == NULL ||
6877 g_file_info_has_attribute (info, "filechooser::queried"))
6880 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6881 if (tree_model != GTK_TREE_MODEL (model))
6884 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
6887 g_assert_not_reached ();
6888 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6890 path = gtk_tree_model_get_path (tree_model, &iter);
6891 if (gtk_tree_path_compare (start, path) != 1 &&
6892 gtk_tree_path_compare (path, end) != 1)
6894 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6895 g_file_query_info_async (file,
6896 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6897 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6898 G_FILE_ATTRIBUTE_STANDARD_ICON,
6901 _gtk_file_system_model_get_cancellable (model),
6902 file_system_model_got_thumbnail,
6905 gtk_tree_path_free (path);
6906 gtk_tree_path_free (start);
6907 gtk_tree_path_free (end);
6912 g_value_set_object (value, NULL);
6914 case MODEL_COL_SIZE:
6915 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6917 case MODEL_COL_SIZE_TEXT:
6918 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6919 g_value_set_string (value, NULL);
6921 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6923 case MODEL_COL_MTIME:
6924 case MODEL_COL_MTIME_TEXT:
6929 g_file_info_get_modification_time (info, &tv);
6930 if (column == MODEL_COL_MTIME)
6931 g_value_set_long (value, tv.tv_sec);
6932 else if (tv.tv_sec == 0)
6933 g_value_set_static_string (value, _("Unknown"));
6935 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6938 case MODEL_COL_ELLIPSIZE:
6939 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6942 g_assert_not_reached ();
6949 /* Gets rid of the old list model and creates a new one for the current folder */
6951 set_list_model (GtkFileChooserDefault *impl,
6954 g_assert (impl->current_folder != NULL);
6956 profile_start ("start", NULL);
6958 stop_loading_and_clear_list_model (impl);
6960 set_busy_cursor (impl, TRUE);
6961 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6963 impl->browse_files_model =
6964 _gtk_file_system_model_new (impl->current_folder,
6966 file_system_model_set,
6968 MODEL_COL_NUM_COLUMNS,
6978 PANGO_TYPE_ELLIPSIZE_MODE);
6980 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6982 profile_msg (" set sort function", NULL);
6983 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6984 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6985 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6986 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6987 set_sort_column (impl);
6988 impl->list_sort_ascending = TRUE;
6989 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6990 G_CALLBACK (list_sort_column_changed_cb), impl);
6992 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6994 g_signal_connect (impl->browse_files_model, "finished-loading",
6995 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6997 install_list_model_filter (impl);
6999 profile_end ("end", NULL);
7004 struct update_chooser_entry_selected_foreach_closure {
7006 GtkTreeIter first_selected_iter;
7010 compare_utf8_filenames (const gchar *a,
7013 gchar *a_folded, *b_folded;
7016 a_folded = g_utf8_strdown (a, -1);
7017 b_folded = g_utf8_strdown (b, -1);
7019 retval = strcmp (a_folded, b_folded);
7028 update_chooser_entry_selected_foreach (GtkTreeModel *model,
7033 struct update_chooser_entry_selected_foreach_closure *closure;
7036 closure->num_selected++;
7038 if (closure->num_selected == 1)
7039 closure->first_selected_iter = *iter;
7043 update_chooser_entry (GtkFileChooserDefault *impl)
7045 GtkTreeSelection *selection;
7046 struct update_chooser_entry_selected_foreach_closure closure;
7047 const char *file_part;
7049 /* no need to update the file chooser's entry if there's no entry */
7050 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7051 impl->operation_mode == OPERATION_MODE_RECENT ||
7052 !impl->location_entry)
7055 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7056 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7057 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7058 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7059 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
7062 g_assert (impl->location_entry != NULL);
7064 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7065 closure.num_selected = 0;
7066 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
7070 if (closure.num_selected == 0)
7072 goto maybe_clear_entry;
7074 else if (closure.num_selected == 1)
7076 if (impl->operation_mode == OPERATION_MODE_BROWSE)
7079 gboolean change_entry;
7081 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
7083 /* If the cursor moved to the row of the newly created folder,
7084 * retrieving info will return NULL.
7089 g_free (impl->browse_files_last_selected_name);
7090 impl->browse_files_last_selected_name =
7091 g_strdup (g_file_info_get_display_name (info));
7093 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7094 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7095 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7097 /* We don't want the name to change when clicking on a folder... */
7098 change_entry = (! _gtk_file_info_consider_as_directory (info));
7101 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7105 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7107 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7108 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7116 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7117 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7119 /* Multiple selection, so just clear the entry. */
7121 g_free (impl->browse_files_last_selected_name);
7122 impl->browse_files_last_selected_name = NULL;
7124 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7130 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7131 && impl->browse_files_last_selected_name)
7133 const char *entry_text;
7135 gboolean clear_entry;
7137 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7138 len = strlen (entry_text);
7141 /* The file chooser entry may have appended a "/" to its text. So
7142 * take it out, and compare the result to the old selection.
7144 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7148 tmp = g_strndup (entry_text, len - 1);
7149 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7153 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7156 clear_entry = FALSE;
7159 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7164 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7168 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7172 struct UpdateCurrentFolderData
7174 GtkFileChooserDefault *impl;
7176 gboolean keep_trail;
7177 gboolean clear_entry;
7178 GFile *original_file;
7179 GError *original_error;
7183 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7184 GtkFileSystemVolume *volume,
7185 const GError *error,
7188 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7189 struct UpdateCurrentFolderData *data = user_data;
7190 GtkFileChooserDefault *impl = data->impl;
7192 if (cancellable != impl->update_current_folder_cancellable)
7195 impl->update_current_folder_cancellable = NULL;
7196 set_busy_cursor (impl, FALSE);
7203 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7204 impl->reload_state = RELOAD_EMPTY;
7208 change_folder_and_display_error (impl, data->file, data->clear_entry);
7211 g_object_unref (data->file);
7214 g_object_unref (cancellable);
7218 update_current_folder_get_info_cb (GCancellable *cancellable,
7220 const GError *error,
7223 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7224 struct UpdateCurrentFolderData *data = user_data;
7225 GtkFileChooserDefault *impl = data->impl;
7227 if (cancellable != impl->update_current_folder_cancellable)
7230 impl->update_current_folder_cancellable = NULL;
7231 impl->reload_state = RELOAD_EMPTY;
7233 set_busy_cursor (impl, FALSE);
7242 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7244 GMountOperation *mount_operation;
7245 GtkWidget *toplevel;
7247 g_object_unref (cancellable);
7248 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7250 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7252 set_busy_cursor (impl, TRUE);
7254 impl->update_current_folder_cancellable =
7255 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7257 update_current_folder_mount_enclosing_volume_cb,
7263 if (!data->original_file)
7265 data->original_file = g_object_ref (data->file);
7266 data->original_error = g_error_copy (error);
7269 parent_file = g_file_get_parent (data->file);
7271 /* get parent path and try to change the folder to that */
7274 g_object_unref (data->file);
7275 data->file = parent_file;
7277 g_object_unref (cancellable);
7279 /* restart the update current folder operation */
7280 impl->reload_state = RELOAD_HAS_FOLDER;
7282 impl->update_current_folder_cancellable =
7283 _gtk_file_system_get_info (impl->file_system, data->file,
7285 update_current_folder_get_info_cb,
7288 set_busy_cursor (impl, TRUE);
7294 /* Error and bail out, ignoring "not found" errors since they're useless:
7295 * they only happen when a program defaults to a folder that has been (re)moved.
7297 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7298 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7300 g_error_free (data->original_error);
7302 g_object_unref (data->original_file);
7308 if (data->original_file)
7310 /* Error and bail out, ignoring "not found" errors since they're useless:
7311 * they only happen when a program defaults to a folder that has been (re)moved.
7313 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7314 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7316 g_error_free (data->original_error);
7318 g_object_unref (data->original_file);
7321 if (! _gtk_file_info_consider_as_directory (info))
7324 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7327 if (impl->current_folder != data->file)
7329 if (impl->current_folder)
7330 g_object_unref (impl->current_folder);
7332 impl->current_folder = g_object_ref (data->file);
7335 impl->reload_state = RELOAD_HAS_FOLDER;
7337 /* Update the widgets that may trigger a folder change themselves. */
7339 if (!impl->changing_folder)
7341 impl->changing_folder = TRUE;
7343 shortcuts_update_current_folder (impl);
7345 impl->changing_folder = FALSE;
7348 /* Set the folder on the save entry */
7350 if (impl->location_entry)
7352 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7353 impl->current_folder);
7355 if (data->clear_entry)
7356 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7359 /* Create a new list model. This is slightly evil; we store the result value
7360 * but perform more actions rather than returning immediately even if it
7361 * generates an error.
7363 set_list_model (impl, NULL);
7365 /* Refresh controls */
7367 shortcuts_find_current_folder (impl);
7369 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7371 check_preview_change (impl);
7372 bookmarks_check_add_sensitivity (impl);
7374 g_signal_emit_by_name (impl, "selection-changed", 0);
7377 g_object_unref (data->file);
7380 g_object_unref (cancellable);
7384 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7386 gboolean keep_trail,
7387 gboolean clear_entry,
7390 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7391 struct UpdateCurrentFolderData *data;
7393 profile_start ("start", NULL);
7395 g_object_ref (file);
7397 switch (impl->operation_mode)
7399 case OPERATION_MODE_SEARCH:
7400 search_switch_to_browse_mode (impl);
7402 case OPERATION_MODE_RECENT:
7403 recent_switch_to_browse_mode (impl);
7405 case OPERATION_MODE_BROWSE:
7409 if (impl->local_only && !g_file_is_native (file))
7411 g_set_error_literal (error,
7412 GTK_FILE_CHOOSER_ERROR,
7413 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7414 _("Cannot change to folder because it is not local"));
7416 g_object_unref (file);
7417 profile_end ("end - not local", NULL);
7421 if (impl->update_current_folder_cancellable)
7422 g_cancellable_cancel (impl->update_current_folder_cancellable);
7424 /* Test validity of path here. */
7425 data = g_new0 (struct UpdateCurrentFolderData, 1);
7427 data->file = g_object_ref (file);
7428 data->keep_trail = keep_trail;
7429 data->clear_entry = clear_entry;
7431 impl->reload_state = RELOAD_HAS_FOLDER;
7433 impl->update_current_folder_cancellable =
7434 _gtk_file_system_get_info (impl->file_system, file,
7436 update_current_folder_get_info_cb,
7439 set_busy_cursor (impl, TRUE);
7440 g_object_unref (file);
7442 profile_end ("end", NULL);
7447 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7449 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7451 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7452 impl->operation_mode == OPERATION_MODE_RECENT)
7455 if (impl->reload_state == RELOAD_EMPTY)
7457 char *current_working_dir;
7460 /* We are unmapped, or we had an error while loading the last folder. We'll return
7461 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7462 * explicitly calls set_current_folder() on us.
7464 current_working_dir = g_get_current_dir ();
7465 file = g_file_new_for_path (current_working_dir);
7466 g_free (current_working_dir);
7470 if (impl->current_folder)
7471 return g_object_ref (impl->current_folder);
7477 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7480 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7482 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7483 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7485 pending_select_files_free (impl);
7486 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7490 select_func (GtkFileSystemModel *model,
7495 GtkFileChooserDefault *impl = user_data;
7496 GtkTreeSelection *selection;
7498 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7500 gtk_tree_selection_select_iter (selection, iter);
7504 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7508 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7512 parent_file = g_file_get_parent (file);
7515 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7517 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7518 impl->operation_mode == OPERATION_MODE_RECENT ||
7519 impl->load_state == LOAD_EMPTY)
7525 g_assert (impl->current_folder != NULL);
7527 same_path = g_file_equal (parent_file, impl->current_folder);
7530 if (same_path && impl->load_state == LOAD_FINISHED)
7535 files.data = (gpointer) file;
7538 result = show_and_select_files (impl, parent_file, &files, error);
7539 g_object_unref (parent_file);
7543 pending_select_files_add (impl, file);
7549 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7550 g_object_unref (parent_file);
7554 g_object_unref (parent_file);
7559 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7562 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7563 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7566 if (!impl->browse_files_model)
7569 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7574 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7579 maybe_select (GtkTreeModel *model,
7584 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7585 GtkTreeSelection *selection;
7588 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7590 gtk_tree_model_get (model, iter,
7591 MODEL_COL_IS_FOLDER, &is_folder,
7594 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7595 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7596 gtk_tree_selection_select_iter (selection, iter);
7598 gtk_tree_selection_unselect_iter (selection, iter);
7604 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7606 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7608 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7609 impl->operation_mode == OPERATION_MODE_RECENT)
7611 GtkTreeSelection *selection;
7613 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7614 gtk_tree_selection_select_all (selection);
7618 if (impl->select_multiple)
7619 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7620 maybe_select, impl);
7624 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7626 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7627 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7629 gtk_tree_selection_unselect_all (selection);
7630 pending_select_files_free (impl);
7633 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7635 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7637 * is_empty_ret - whether the file entry is totally empty
7639 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7640 * the path will be "$cwd/foobar")
7643 check_save_entry (GtkFileChooserDefault *impl,
7645 gboolean *is_well_formed_ret,
7646 gboolean *is_empty_ret,
7647 gboolean *is_file_part_empty_ret,
7648 gboolean *is_folder)
7650 GtkFileChooserEntry *chooser_entry;
7651 GFile *current_folder;
7652 const char *file_part;
7656 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7657 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7658 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7659 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7660 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7662 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7664 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7667 *is_well_formed_ret = TRUE;
7668 *is_empty_ret = TRUE;
7669 *is_file_part_empty_ret = TRUE;
7675 *is_empty_ret = FALSE;
7677 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7678 if (!current_folder)
7681 *is_well_formed_ret = FALSE;
7682 *is_file_part_empty_ret = FALSE;
7688 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7690 if (!file_part || file_part[0] == '\0')
7692 *file_ret = g_object_ref (current_folder);
7693 *is_well_formed_ret = TRUE;
7694 *is_file_part_empty_ret = TRUE;
7700 *is_file_part_empty_ret = FALSE;
7703 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7707 error_building_filename_dialog (impl, error);
7709 *is_well_formed_ret = FALSE;
7716 *is_well_formed_ret = TRUE;
7717 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7720 struct get_files_closure {
7721 GtkFileChooserDefault *impl;
7723 GFile *file_from_entry;
7727 get_files_foreach (GtkTreeModel *model,
7732 struct get_files_closure *info;
7734 GtkFileSystemModel *fs_model;
7737 fs_model = info->impl->browse_files_model;
7739 file = _gtk_file_system_model_get_file (fs_model, iter);
7741 return; /* We are on the editable row */
7743 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7744 info->result = g_slist_prepend (info->result, g_object_ref (file));
7748 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7750 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7751 struct get_files_closure info;
7752 GtkWindow *toplevel;
7753 GtkWidget *current_focus;
7754 gboolean file_list_seen;
7756 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7757 return search_get_selected_files (impl);
7759 if (impl->operation_mode == OPERATION_MODE_RECENT)
7760 return recent_get_selected_files (impl);
7764 info.file_from_entry = NULL;
7766 toplevel = get_toplevel (GTK_WIDGET (impl));
7768 current_focus = gtk_window_get_focus (toplevel);
7770 current_focus = NULL;
7772 file_list_seen = FALSE;
7773 if (current_focus == impl->browse_files_tree_view)
7775 GtkTreeSelection *selection;
7779 file_list_seen = TRUE;
7780 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7781 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7783 /* If there is no selection in the file list, we probably have this situation:
7785 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7786 * 2. He then double-clicked on a folder ("bar") in the file list
7788 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7789 * filename entry to see if that is the case.
7791 if (info.result == NULL && impl->location_entry)
7794 else if (impl->location_entry && current_focus == impl->location_entry)
7796 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7800 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7805 if (!is_well_formed)
7808 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7810 g_object_unref (info.file_from_entry);
7814 if (info.file_from_entry)
7815 info.result = g_slist_prepend (info.result, info.file_from_entry);
7816 else if (!file_list_seen)
7821 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7823 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7827 /* The focus is on a dialog's action area button or something else */
7828 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7829 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7837 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7838 * fall back to the current directory */
7839 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7840 info.result == NULL)
7842 GFile *current_folder;
7844 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7847 info.result = g_slist_prepend (info.result, current_folder);
7850 return g_slist_reverse (info.result);
7854 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7856 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7858 if (impl->preview_file)
7859 return g_object_ref (impl->preview_file);
7864 static GtkFileSystem *
7865 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7867 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7869 return impl->file_system;
7872 /* Shows or hides the filter widgets */
7874 show_filters (GtkFileChooserDefault *impl,
7878 gtk_widget_show (impl->filter_combo_hbox);
7880 gtk_widget_hide (impl->filter_combo_hbox);
7884 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7885 GtkFileFilter *filter)
7887 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7890 if (g_slist_find (impl->filters, filter))
7892 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7896 g_object_ref_sink (filter);
7897 impl->filters = g_slist_append (impl->filters, filter);
7899 name = gtk_file_filter_get_name (filter);
7901 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7903 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7905 if (!g_slist_find (impl->filters, impl->current_filter))
7906 set_current_filter (impl, filter);
7908 show_filters (impl, TRUE);
7912 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7913 GtkFileFilter *filter)
7915 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7916 GtkTreeModel *model;
7920 filter_index = g_slist_index (impl->filters, filter);
7922 if (filter_index < 0)
7924 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7928 impl->filters = g_slist_remove (impl->filters, filter);
7930 if (filter == impl->current_filter)
7933 set_current_filter (impl, impl->filters->data);
7935 set_current_filter (impl, NULL);
7938 /* Remove row from the combo box */
7939 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7940 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7941 g_assert_not_reached ();
7943 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7945 g_object_unref (filter);
7948 show_filters (impl, FALSE);
7952 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7954 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7956 return g_slist_copy (impl->filters);
7959 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7961 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7964 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7967 struct AddShortcutData
7969 GtkFileChooserDefault *impl;
7974 add_shortcut_get_info_cb (GCancellable *cancellable,
7976 const GError *error,
7980 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7981 struct AddShortcutData *data = user_data;
7983 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7986 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7988 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7991 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7993 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7996 g_object_unref (data->impl);
7997 g_object_unref (data->file);
8000 g_object_unref (cancellable);
8004 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
8008 GCancellable *cancellable;
8009 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8010 struct AddShortcutData *data;
8014 /* Avoid adding duplicates */
8015 pos = shortcut_find_position (impl, file);
8016 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
8020 uri = g_file_get_uri (file);
8021 /* translators, "Shortcut" means "Bookmark" here */
8023 GTK_FILE_CHOOSER_ERROR,
8024 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8025 _("Shortcut %s already exists"),
8032 for (l = impl->loading_shortcuts; l; l = l->next)
8034 GCancellable *c = l->data;
8037 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8038 if (f && g_file_equal (file, f))
8042 uri = g_file_get_uri (file);
8044 GTK_FILE_CHOOSER_ERROR,
8045 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8046 _("Shortcut %s already exists"),
8054 data = g_new0 (struct AddShortcutData, 1);
8055 data->impl = g_object_ref (impl);
8056 data->file = g_object_ref (file);
8058 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8060 add_shortcut_get_info_cb, data);
8065 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
8066 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
8072 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
8076 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8083 for (l = impl->loading_shortcuts; l; l = l->next)
8085 GCancellable *c = l->data;
8088 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8089 if (f && g_file_equal (file, f))
8091 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8092 g_cancellable_cancel (c);
8097 if (impl->num_shortcuts == 0)
8100 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8101 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8102 g_assert_not_reached ();
8104 for (i = 0; i < impl->num_shortcuts; i++)
8107 ShortcutType shortcut_type;
8110 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8111 SHORTCUTS_COL_DATA, &col_data,
8112 SHORTCUTS_COL_TYPE, &shortcut_type,
8114 g_assert (col_data != NULL);
8115 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8117 shortcut = col_data;
8118 if (g_file_equal (shortcut, file))
8120 shortcuts_remove_rows (impl, pos + i, 1);
8121 impl->num_shortcuts--;
8125 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8126 g_assert_not_reached ();
8131 uri = g_file_get_uri (file);
8132 /* translators, "Shortcut" means "Bookmark" here */
8134 GTK_FILE_CHOOSER_ERROR,
8135 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8136 _("Shortcut %s does not exist"),
8144 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8146 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8152 if (impl->num_shortcuts == 0)
8155 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8156 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8157 g_assert_not_reached ();
8161 for (i = 0; i < impl->num_shortcuts; i++)
8164 ShortcutType shortcut_type;
8167 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8168 SHORTCUTS_COL_DATA, &col_data,
8169 SHORTCUTS_COL_TYPE, &shortcut_type,
8171 g_assert (col_data != NULL);
8172 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8174 shortcut = col_data;
8175 list = g_slist_prepend (list, g_object_ref (shortcut));
8177 if (i != impl->num_shortcuts - 1)
8179 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8180 g_assert_not_reached ();
8184 return g_slist_reverse (list);
8187 /* Guesses a size based upon font sizes */
8189 find_good_size_from_style (GtkWidget *widget,
8193 GtkFileChooserDefault *impl;
8198 g_assert (widget->style != NULL);
8199 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
8201 screen = gtk_widget_get_screen (widget);
8204 resolution = gdk_screen_get_resolution (screen);
8205 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8209 resolution = 96.0; /* wheeee */
8211 font_size = pango_font_description_get_size (widget->style->font_desc);
8212 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
8214 *width = font_size * NUM_CHARS;
8215 *height = font_size * NUM_LINES;
8219 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8220 gint *default_width,
8221 gint *default_height)
8223 GtkFileChooserDefault *impl;
8226 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8228 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8229 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8230 || impl->expand_folders)
8232 GtkFileChooserSettings *settings;
8233 int x, y, width, height;
8235 settings = _gtk_file_chooser_settings_new ();
8236 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
8237 g_object_unref (settings);
8239 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8241 *default_width = width;
8242 *default_height = height;
8246 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8248 if (impl->preview_widget_active &&
8249 impl->preview_widget &&
8250 GTK_WIDGET_VISIBLE (impl->preview_widget))
8252 gtk_widget_size_request (impl->preview_box, &req);
8253 *default_width += PREVIEW_HBOX_SPACING + req.width;
8256 if (impl->extra_widget &&
8257 GTK_WIDGET_VISIBLE (impl->extra_widget))
8259 gtk_widget_size_request (impl->extra_align, &req);
8260 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
8265 gtk_widget_size_request (GTK_WIDGET (impl), &req);
8266 *default_width = req.width;
8267 *default_height = req.height;
8271 struct switch_folder_closure {
8272 GtkFileChooserDefault *impl;
8277 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8279 switch_folder_foreach_cb (GtkTreeModel *model,
8284 struct switch_folder_closure *closure;
8288 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8289 closure->num_selected++;
8292 /* Changes to the selected folder in the list view */
8294 switch_to_selected_folder (GtkFileChooserDefault *impl)
8296 GtkTreeSelection *selection;
8297 struct switch_folder_closure closure;
8299 /* We do this with foreach() rather than get_selected() as we may be in
8300 * multiple selection mode
8303 closure.impl = impl;
8304 closure.file = NULL;
8305 closure.num_selected = 0;
8307 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8308 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8310 g_assert (closure.file && closure.num_selected == 1);
8312 change_folder_and_display_error (impl, closure.file, FALSE);
8315 /* Gets the GFileInfo for the selected row in the file list; assumes single
8319 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8320 gboolean *had_selection)
8322 GtkTreeSelection *selection;
8326 g_assert (!impl->select_multiple);
8327 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8328 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8330 *had_selection = FALSE;
8334 *had_selection = TRUE;
8336 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8340 /* Gets the display name of the selected file in the file list; assumes single
8341 * selection mode and that something is selected.
8343 static const gchar *
8344 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8347 gboolean had_selection;
8349 info = get_selected_file_info_from_file_list (impl, &had_selection);
8350 g_assert (had_selection);
8351 g_assert (info != NULL);
8353 return g_file_info_get_display_name (info);
8357 add_custom_button_to_dialog (GtkDialog *dialog,
8358 const gchar *mnemonic_label,
8359 const gchar *stock_id,
8364 button = gtk_button_new_with_mnemonic (mnemonic_label);
8365 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8366 gtk_button_set_image (GTK_BUTTON (button),
8367 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8368 gtk_widget_show (button);
8370 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8373 /* Presents an overwrite confirmation dialog; returns whether we should accept
8377 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8378 const gchar *file_part,
8379 const gchar *folder_display_name)
8381 GtkWindow *toplevel;
8385 toplevel = get_toplevel (GTK_WIDGET (impl));
8387 dialog = gtk_message_dialog_new (toplevel,
8388 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8389 GTK_MESSAGE_QUESTION,
8391 _("A file named \"%s\" already exists. Do you want to replace it?"),
8393 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8394 _("The file already exists in \"%s\". Replacing it will "
8395 "overwrite its contents."),
8396 folder_display_name);
8398 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8399 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8400 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8401 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8402 GTK_RESPONSE_ACCEPT,
8403 GTK_RESPONSE_CANCEL,
8405 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8407 if (toplevel->group)
8408 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8410 response = gtk_dialog_run (GTK_DIALOG (dialog));
8412 gtk_widget_destroy (dialog);
8414 return (response == GTK_RESPONSE_ACCEPT);
8417 struct GetDisplayNameData
8419 GtkFileChooserDefault *impl;
8424 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8426 const GError *error,
8429 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8430 gboolean should_respond = FALSE;
8431 struct GetDisplayNameData *data = user_data;
8433 if (cancellable != data->impl->should_respond_get_info_cancellable)
8436 data->impl->should_respond_get_info_cancellable = NULL;
8442 /* Huh? Did the folder disappear? Let the caller deal with it */
8443 should_respond = TRUE;
8445 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8447 set_busy_cursor (data->impl, FALSE);
8449 g_signal_emit_by_name (data->impl, "response-requested");
8452 g_object_unref (data->impl);
8453 g_free (data->file_part);
8456 g_object_unref (cancellable);
8459 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8460 * should respond. Can get the file part from the file list or the save entry.
8463 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8464 const gchar *file_part,
8467 GtkFileChooserConfirmation conf;
8469 if (!impl->do_overwrite_confirmation)
8472 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8474 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8478 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8480 struct GetDisplayNameData *data;
8482 g_assert (file_part != NULL);
8484 data = g_new0 (struct GetDisplayNameData, 1);
8485 data->impl = g_object_ref (impl);
8486 data->file_part = g_strdup (file_part);
8488 if (impl->should_respond_get_info_cancellable)
8489 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8491 impl->should_respond_get_info_cancellable =
8492 _gtk_file_system_get_info (impl->file_system, parent_file,
8493 "standard::display-name",
8494 confirmation_confirm_get_info_cb,
8496 set_busy_cursor (data->impl, TRUE);
8500 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8503 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8507 g_assert_not_reached ();
8512 struct FileExistsData
8514 GtkFileChooserDefault *impl;
8515 gboolean file_exists_and_is_not_folder;
8521 save_entry_get_info_cb (GCancellable *cancellable,
8523 const GError *error,
8526 gboolean parent_is_folder;
8527 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8528 struct FileExistsData *data = user_data;
8530 if (cancellable != data->impl->should_respond_get_info_cancellable)
8533 data->impl->should_respond_get_info_cancellable = NULL;
8535 set_busy_cursor (data->impl, FALSE);
8541 parent_is_folder = FALSE;
8543 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8545 if (parent_is_folder)
8547 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8549 if (data->file_exists_and_is_not_folder)
8554 /* Dup the string because the string may be modified
8555 * depending on what clients do in the confirm-overwrite
8556 * signal and this corrupts the pointer
8558 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8559 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8563 g_signal_emit_by_name (data->impl, "response-requested");
8566 g_signal_emit_by_name (data->impl, "response-requested");
8568 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8570 GError *error = NULL;
8572 set_busy_cursor (data->impl, TRUE);
8573 g_file_make_directory (data->file, NULL, &error);
8574 set_busy_cursor (data->impl, FALSE);
8577 g_signal_emit_by_name (data->impl, "response-requested");
8579 error_creating_folder_dialog (data->impl, data->file, error);
8584 /* This will display an error, which is what we want */
8585 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8589 g_object_unref (data->impl);
8590 g_object_unref (data->file);
8591 g_object_unref (data->parent_file);
8594 g_object_unref (cancellable);
8598 file_exists_get_info_cb (GCancellable *cancellable,
8600 const GError *error,
8603 gboolean data_ownership_taken = FALSE;
8604 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8605 gboolean file_exists_and_is_not_folder;
8606 struct FileExistsData *data = user_data;
8608 if (cancellable != data->impl->file_exists_get_info_cancellable)
8611 data->impl->file_exists_get_info_cancellable = NULL;
8613 set_busy_cursor (data->impl, FALSE);
8618 file_exists_and_is_not_folder = info && (! _gtk_file_info_consider_as_directory (info));
8620 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8621 /* user typed a filename; we are done */
8622 g_signal_emit_by_name (data->impl, "response-requested");
8623 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8624 && file_exists_and_is_not_folder)
8626 /* Oops, the user typed the name of an existing path which is not
8629 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8630 g_error_copy (error));
8634 /* check that everything up to the last component exists */
8636 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8637 data_ownership_taken = TRUE;
8639 if (data->impl->should_respond_get_info_cancellable)
8640 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8642 data->impl->should_respond_get_info_cancellable =
8643 _gtk_file_system_get_info (data->impl->file_system,
8646 save_entry_get_info_cb,
8648 set_busy_cursor (data->impl, TRUE);
8652 if (!data_ownership_taken)
8654 g_object_unref (data->impl);
8655 g_object_unref (data->file);
8656 g_object_unref (data->parent_file);
8660 g_object_unref (cancellable);
8664 paste_text_received (GtkClipboard *clipboard,
8666 GtkFileChooserDefault *impl)
8673 file = g_file_new_for_uri (text);
8675 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8676 location_popup_handler (impl, text);
8678 g_object_unref (file);
8681 /* Handler for the "location-popup-on-paste" keybinding signal */
8683 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8685 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8686 GDK_SELECTION_CLIPBOARD);
8687 gtk_clipboard_request_text (clipboard,
8688 (GtkClipboardTextReceivedFunc) paste_text_received,
8693 /* Implementation for GtkFileChooserEmbed::should_respond() */
8695 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8697 GtkFileChooserDefault *impl;
8698 GtkWidget *toplevel;
8699 GtkWidget *current_focus;
8701 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8703 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8704 g_assert (GTK_IS_WINDOW (toplevel));
8706 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8708 if (current_focus == impl->browse_files_tree_view)
8710 /* The following array encodes what we do based on the impl->action and the
8711 * number of files selected.
8714 NOOP, /* Do nothing (don't respond) */
8715 RESPOND, /* Respond immediately */
8716 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8717 ALL_FILES, /* Respond only if everything selected is a file */
8718 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8719 SAVE_ENTRY, /* Go to the code for handling the save entry */
8720 NOT_REACHED /* Sanity check */
8722 static const ActionToTake what_to_do[4][3] = {
8723 /* 0 selected 1 selected many selected */
8724 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8725 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8726 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8727 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8731 gboolean all_files, all_folders;
8733 ActionToTake action;
8737 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8739 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8740 return search_should_respond (impl);
8742 if (impl->operation_mode == OPERATION_MODE_RECENT)
8743 return recent_should_respond (impl);
8745 selection_check (impl, &num_selected, &all_files, &all_folders);
8747 if (num_selected > 2)
8752 action = what_to_do [impl->action] [k];
8762 case RESPOND_OR_SWITCH:
8763 g_assert (num_selected == 1);
8767 switch_to_selected_folder (impl);
8770 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8771 return should_respond_after_confirm_overwrite (impl,
8772 get_display_name_from_file_list (impl),
8773 impl->current_folder);
8787 g_assert_not_reached ();
8790 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8793 gboolean is_well_formed, is_empty, is_file_part_empty;
8796 GtkFileChooserEntry *entry;
8801 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8802 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8803 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8804 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8805 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8807 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8808 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8810 if (is_empty || !is_well_formed)
8813 g_assert (file != NULL);
8818 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8819 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8821 change_folder_and_display_error (impl, file, TRUE);
8824 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8825 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8827 /* The folder already exists, so we do not need to create it.
8828 * Just respond to terminate the dialog.
8834 g_assert_not_reached ();
8840 struct FileExistsData *data;
8842 /* We need to check whether file exists and is not a folder */
8844 data = g_new0 (struct FileExistsData, 1);
8845 data->impl = g_object_ref (impl);
8846 data->file = g_object_ref (file);
8847 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8849 if (impl->file_exists_get_info_cancellable)
8850 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8852 impl->file_exists_get_info_cancellable =
8853 _gtk_file_system_get_info (impl->file_system, file,
8855 file_exists_get_info_cb,
8858 set_busy_cursor (impl, TRUE);
8862 g_error_free (error);
8865 g_object_unref (file);
8868 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8870 /* The focus is on a dialog's action area button, *and* the widget that
8871 * was focused immediately before it is the file list.
8875 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8877 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8880 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8882 /* The focus is on a dialog's action area button, *and* the widget that
8883 * was focused immediately before it is the location entry.
8888 /* The focus is on a dialog's action area button or something else */
8889 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8890 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8895 g_assert_not_reached ();
8899 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8901 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8903 GtkFileChooserDefault *impl;
8906 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8908 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8909 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8911 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8912 widget = impl->browse_files_tree_view;
8914 widget = impl->location_entry;
8916 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8917 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8918 widget = impl->location_entry;
8921 g_assert_not_reached ();
8925 g_assert (widget != NULL);
8926 gtk_widget_grab_focus (widget);
8929 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8931 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8941 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8942 *list = g_slist_prepend (*list, g_object_ref (file));
8945 /* Constructs a list of the selected paths in search mode */
8947 search_get_selected_files (GtkFileChooserDefault *impl)
8950 GtkTreeSelection *selection;
8954 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8955 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8956 result = g_slist_reverse (result);
8961 /* Called from ::should_respond(). We return whether there are selected files
8962 * in the search list.
8965 search_should_respond (GtkFileChooserDefault *impl)
8967 GtkTreeSelection *selection;
8969 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8971 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8972 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8975 struct SearchHitInsertRequest
8977 GtkFileChooserDefault *impl;
8979 GtkTreeRowReference *row_ref;
8983 search_hit_get_info_cb (GCancellable *cancellable,
8985 const GError *error,
8988 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8989 GdkPixbuf *pixbuf = NULL;
8992 GCancellable *model_cancellable;
8993 gboolean is_folder = FALSE;
8996 char *mime_type, *size_text, *mtime_text;
8997 char *display_name, *collated_name;
8998 struct SearchHitInsertRequest *request = data;
9000 if (!request->impl->search_model)
9003 path = gtk_tree_row_reference_get_path (request->row_ref);
9007 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
9009 gtk_tree_path_free (path);
9011 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
9012 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
9014 if (cancellable != model_cancellable)
9017 /* set the cancellable to NULL in the model */
9018 gtk_list_store_set (request->impl->search_model, &iter,
9019 SEARCH_MODEL_COL_CANCELLABLE, NULL,
9027 search_clear_model_row (GTK_TREE_MODEL (request->impl->search_model), &iter);
9028 gtk_list_store_remove (request->impl->search_model, &iter);
9032 display_name = g_strdup (g_file_info_get_display_name (info));
9034 collated_name = g_utf8_collate_key_for_filename (display_name, -1);
9036 collated_name = NULL;
9037 mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (info));
9038 is_folder = _gtk_file_info_consider_as_directory (info);
9039 g_file_info_get_modification_time (info, &mtime);
9040 size = g_file_info_get_size (info);
9041 size_text = is_folder ? NULL : g_format_size_for_display (size);
9042 mtime_text = mtime.tv_sec ? my_g_format_time_for_display (mtime.tv_sec) : g_strdup (_("Unknown"));
9043 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
9044 request->impl->icon_size);
9046 gtk_list_store_set (request->impl->search_model, &iter,
9047 MODEL_COL_NAME, display_name,
9048 MODEL_COL_NAME_COLLATED, collated_name,
9049 MODEL_COL_IS_FOLDER, is_folder,
9050 MODEL_COL_PIXBUF, pixbuf,
9051 MODEL_COL_SIZE, size,
9052 MODEL_COL_SIZE_TEXT, size_text,
9053 MODEL_COL_MTIME, mtime.tv_sec,
9054 MODEL_COL_MTIME_TEXT, mtime_text,
9055 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
9059 g_object_unref (pixbuf);
9060 g_free (display_name);
9061 g_free (collated_name);
9064 g_free (mtime_text);
9067 g_object_unref (request->impl);
9068 g_object_unref (request->file);
9069 gtk_tree_row_reference_free (request->row_ref);
9072 g_object_unref (cancellable);
9075 /* Adds one hit from the search engine to the search_model */
9077 search_add_hit (GtkFileChooserDefault *impl,
9083 GCancellable *cancellable;
9084 struct SearchHitInsertRequest *request;
9086 file = g_file_new_for_uri (uri);
9090 if (!g_file_is_native (file))
9092 g_object_unref (file);
9096 request = g_new0 (struct SearchHitInsertRequest, 1);
9097 request->impl = g_object_ref (impl);
9098 request->file = g_object_ref (file);
9100 gtk_list_store_append (impl->search_model, &iter);
9101 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
9103 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
9104 gtk_tree_path_free (p);
9106 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9109 "standard::content-type,"
9110 "standard::display-name,"
9113 search_hit_get_info_cb,
9116 gtk_list_store_set (impl->search_model, &iter,
9117 MODEL_COL_FILE, file,
9118 MODEL_COL_ELLIPSIZE, PANGO_ELLIPSIZE_END,
9119 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
9122 g_object_unref (file);
9123 g_object_unref (cancellable);
9126 /* Callback used from GtkSearchEngine when we get new hits */
9128 search_engine_hits_added_cb (GtkSearchEngine *engine,
9132 GtkFileChooserDefault *impl;
9135 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9137 for (l = hits; l; l = l->next)
9138 search_add_hit (impl, (gchar*)l->data);
9141 /* Callback used from GtkSearchEngine when the query is done running */
9143 search_engine_finished_cb (GtkSearchEngine *engine,
9146 GtkFileChooserDefault *impl;
9148 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9151 /* EB: setting the model here will avoid loads of row events,
9152 * but it'll make the search look like blocked.
9154 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9155 GTK_TREE_MODEL (impl->search_model_filter));
9158 /* FMQ: if search was empty, say that we got no hits */
9159 set_busy_cursor (impl, FALSE);
9162 /* Displays a generic error when we cannot create a GtkSearchEngine.
9163 * It would be better if _gtk_search_engine_new() gave us a GError
9164 * with a better message, but it doesn't do that right now.
9167 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9169 error_message (impl,
9170 _("Could not start the search process"),
9171 _("The program was not able to create a connection to the indexer "
9172 "daemon. Please make sure it is running."));
9176 search_engine_error_cb (GtkSearchEngine *engine,
9177 const gchar *message,
9180 GtkFileChooserDefault *impl;
9182 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9184 search_stop_searching (impl, TRUE);
9185 error_message (impl, _("Could not send the search request"), message);
9187 set_busy_cursor (impl, FALSE);
9191 search_clear_model_row (GtkTreeModel *model,
9194 GCancellable *cancellable;
9196 gtk_tree_model_get (model, iter,
9197 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
9202 g_cancellable_cancel (cancellable);
9203 g_object_unref (cancellable);
9207 /* Frees the data in the search_model */
9209 search_clear_model (GtkFileChooserDefault *impl,
9210 gboolean remove_from_treeview)
9212 GtkTreeModel *model;
9215 if (!impl->search_model)
9218 model = GTK_TREE_MODEL (impl->search_model);
9220 if (gtk_tree_model_get_iter_first (model, &iter))
9223 search_clear_model_row (model, &iter);
9225 while (gtk_tree_model_iter_next (model, &iter));
9227 g_object_unref (impl->search_model);
9228 impl->search_model = NULL;
9230 g_object_unref (impl->search_model_filter);
9231 impl->search_model_filter = NULL;
9233 g_object_unref (impl->search_model_sort);
9234 impl->search_model_sort = NULL;
9236 if (remove_from_treeview)
9237 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9240 /* Stops any ongoing searches; does not touch the search_model */
9242 search_stop_searching (GtkFileChooserDefault *impl,
9243 gboolean remove_query)
9245 if (remove_query && impl->search_query)
9247 g_object_unref (impl->search_query);
9248 impl->search_query = NULL;
9251 if (impl->search_engine)
9253 _gtk_search_engine_stop (impl->search_engine);
9255 g_object_unref (impl->search_engine);
9256 impl->search_engine = NULL;
9260 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
9262 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
9264 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9266 search_stop_searching (impl, FALSE);
9267 search_clear_model (impl, TRUE);
9269 gtk_widget_destroy (impl->search_hbox);
9270 impl->search_hbox = NULL;
9271 impl->search_entry = NULL;
9273 gtk_widget_show (impl->browse_path_bar);
9274 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9275 gtk_widget_hide (impl->browse_new_folder_button);
9277 gtk_widget_show (impl->browse_new_folder_button);
9280 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9281 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9283 gtk_widget_show (impl->location_button);
9285 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9286 gtk_widget_show (impl->location_entry_box);
9289 impl->operation_mode = OPERATION_MODE_BROWSE;
9291 file_list_set_sort_column_ids (impl);
9295 search_get_is_filtered (GtkFileChooserDefault *impl,
9297 const gchar *display_name,
9298 const gchar *mime_type)
9300 GtkFileFilterInfo filter_info;
9301 GtkFileFilterFlags needed;
9304 if (!impl->current_filter)
9307 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9308 needed = gtk_file_filter_get_needed (impl->current_filter);
9310 filter_info.display_name = display_name;
9311 filter_info.mime_type = mime_type;
9313 if (needed & GTK_FILE_FILTER_FILENAME)
9315 filter_info.filename = g_file_get_path (file);
9316 if (filter_info.filename)
9317 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9320 filter_info.filename = NULL;
9322 if (needed & GTK_FILE_FILTER_URI)
9324 filter_info.uri = g_file_get_uri (file);
9325 if (filter_info.uri)
9326 filter_info.contains |= GTK_FILE_FILTER_URI;
9329 filter_info.uri = NULL;
9331 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9333 if (filter_info.filename)
9334 g_free ((gchar *) filter_info.filename);
9335 if (filter_info.uri)
9336 g_free ((gchar *) filter_info.uri);
9342 /* Visibility function for the recent filter model */
9344 search_model_visible_func (GtkTreeModel *model,
9348 GtkFileChooserDefault *impl = user_data;
9350 gchar *display_name, *mime_type;
9351 gboolean is_folder, result;
9353 if (!impl->current_filter)
9356 gtk_tree_model_get (model, iter,
9357 MODEL_COL_FILE, &file,
9358 MODEL_COL_IS_FOLDER, &is_folder,
9359 MODEL_COL_NAME, &display_name,
9360 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9368 result = !search_get_is_filtered (impl, file, display_name, mime_type);
9370 g_free (display_name);
9376 /* Creates the search_model and puts it in the tree view */
9378 search_setup_model (GtkFileChooserDefault *impl)
9380 g_assert (impl->search_model == NULL);
9381 g_assert (impl->search_model_filter == NULL);
9382 g_assert (impl->search_model_sort == NULL);
9384 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9385 G_TYPE_STRING, /* MODEL_COL_NAME */
9386 G_TYPE_INT64, /* MODEL_COL_SIZE */
9387 G_TYPE_LONG, /* MODEL_COL_MTIME */
9388 G_TYPE_FILE, /* MODEL_COL_FILE */
9389 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */
9390 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */
9391 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */
9392 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */
9393 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */
9394 PANGO_TYPE_ELLIPSIZE_MODE, /* MODEL_COL_ELLIPSIZE */
9395 G_TYPE_CANCELLABLE, /* SEARCH_MODEL_COL_CANCELLABLE */
9396 G_TYPE_STRING /* SEARCH_MODEL_COL_MIME_TYPE */
9399 impl->search_model_filter =
9400 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9401 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9402 search_model_visible_func,
9405 impl->search_model_sort =
9406 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9407 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9411 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9415 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9419 set_sort_column (impl);
9421 /* EB: setting the model here will make the hits list update feel
9422 * more "alive" than setting the model at the end of the search
9425 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9426 GTK_TREE_MODEL (impl->search_model_sort));
9430 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9431 GtkTreeIter *child_iter,
9436 if (!impl->search_model)
9439 if (!impl->search_model_filter || !impl->search_model_sort)
9442 /* pass 1: get the iterator in the filter model */
9443 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9446 /* pass 2: get the iterator in the real model */
9447 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9448 child_iter, &middle);
9451 /* Creates a new query with the specified text and launches it */
9453 search_start_query (GtkFileChooserDefault *impl,
9454 const gchar *query_text)
9456 search_stop_searching (impl, FALSE);
9457 search_clear_model (impl, TRUE);
9458 search_setup_model (impl);
9459 set_busy_cursor (impl, TRUE);
9461 if (impl->search_engine == NULL)
9462 impl->search_engine = _gtk_search_engine_new ();
9464 if (!impl->search_engine)
9466 set_busy_cursor (impl, FALSE);
9467 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9471 if (!impl->search_query)
9473 impl->search_query = _gtk_query_new ();
9474 _gtk_query_set_text (impl->search_query, query_text);
9477 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9479 g_signal_connect (impl->search_engine, "hits-added",
9480 G_CALLBACK (search_engine_hits_added_cb), impl);
9481 g_signal_connect (impl->search_engine, "finished",
9482 G_CALLBACK (search_engine_finished_cb), impl);
9483 g_signal_connect (impl->search_engine, "error",
9484 G_CALLBACK (search_engine_error_cb), impl);
9486 _gtk_search_engine_start (impl->search_engine);
9489 /* Callback used when the user presses Enter while typing on the search
9490 * entry; starts the query
9493 search_entry_activate_cb (GtkEntry *entry,
9496 GtkFileChooserDefault *impl;
9499 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9501 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9502 if (strlen (text) == 0)
9505 /* reset any existing query object */
9506 if (impl->search_query)
9508 g_object_unref (impl->search_query);
9509 impl->search_query = NULL;
9512 search_start_query (impl, text);
9515 /* Hides the path bar and creates the search entry */
9517 search_setup_widgets (GtkFileChooserDefault *impl)
9523 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9527 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9528 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), image);
9529 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9533 label = gtk_label_new (NULL);
9534 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9535 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9536 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9541 impl->search_entry = gtk_entry_new ();
9542 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9543 g_signal_connect (impl->search_entry, "activate",
9544 G_CALLBACK (search_entry_activate_cb),
9546 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9548 /* if there already is a query, restart it */
9549 if (impl->search_query)
9551 gchar *query = _gtk_query_get_text (impl->search_query);
9555 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9556 search_start_query (impl, query);
9562 g_object_unref (impl->search_query);
9563 impl->search_query = NULL;
9567 gtk_widget_hide (impl->browse_path_bar);
9568 gtk_widget_hide (impl->browse_new_folder_button);
9570 /* Box for search widgets */
9571 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9572 gtk_widget_show_all (impl->search_hbox);
9574 /* Hide the location widgets temporarily */
9576 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9577 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9579 gtk_widget_hide (impl->location_button);
9580 gtk_widget_hide (impl->location_entry_box);
9583 gtk_widget_grab_focus (impl->search_entry);
9585 /* FMQ: hide the filter combo? */
9588 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9590 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9594 case OPERATION_MODE_BROWSE:
9595 stop_loading_and_clear_list_model (impl);
9598 case OPERATION_MODE_SEARCH:
9599 search_stop_searching (impl, FALSE);
9600 search_clear_model (impl, TRUE);
9602 gtk_widget_destroy (impl->search_hbox);
9603 impl->search_hbox = NULL;
9604 impl->search_entry = NULL;
9607 case OPERATION_MODE_RECENT:
9608 recent_stop_loading (impl);
9609 recent_clear_model (impl, TRUE);
9611 gtk_widget_destroy (impl->recent_hbox);
9612 impl->recent_hbox = NULL;
9617 /* Main entry point to the searching functions; this gets called when the user
9618 * activates the Search shortcut.
9621 search_activate (GtkFileChooserDefault *impl)
9623 OperationMode previous_mode;
9625 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9627 gtk_widget_grab_focus (impl->search_entry);
9631 previous_mode = impl->operation_mode;
9632 impl->operation_mode = OPERATION_MODE_SEARCH;
9634 stop_operation (impl, previous_mode);
9636 g_assert (impl->search_hbox == NULL);
9637 g_assert (impl->search_entry == NULL);
9638 g_assert (impl->search_model == NULL);
9639 g_assert (impl->search_model_filter == NULL);
9641 search_setup_widgets (impl);
9642 file_list_set_sort_column_ids (impl);
9646 * Recent files support
9649 /* Frees the data in the recent_model */
9651 recent_clear_model (GtkFileChooserDefault *impl,
9652 gboolean remove_from_treeview)
9654 GtkTreeModel *model;
9657 if (!impl->recent_model)
9660 model = GTK_TREE_MODEL (impl->recent_model);
9662 if (remove_from_treeview)
9663 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9665 if (gtk_tree_model_get_iter_first (model, &iter))
9670 GCancellable *cancellable;
9671 GtkRecentInfo *recent_info;
9672 gchar *display_name;
9674 gtk_tree_model_get (model, &iter,
9675 MODEL_COL_NAME, &display_name,
9676 MODEL_COL_FILE, &file,
9677 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9678 RECENT_MODEL_COL_INFO, &recent_info,
9682 g_cancellable_cancel (cancellable);
9684 g_object_unref (file);
9685 gtk_recent_info_unref (recent_info);
9686 g_free (display_name);
9688 while (gtk_tree_model_iter_next (model, &iter));
9691 g_object_unref (impl->recent_model);
9692 impl->recent_model = NULL;
9694 g_object_unref (impl->recent_model_filter);
9695 impl->recent_model_filter = NULL;
9697 g_object_unref (impl->recent_model_sort);
9698 impl->recent_model_sort = NULL;
9701 /* Stops any ongoing loading of the recent files list; does
9702 * not touch the recent_model
9705 recent_stop_loading (GtkFileChooserDefault *impl)
9707 if (impl->load_recent_id)
9709 g_source_remove (impl->load_recent_id);
9710 impl->load_recent_id = 0;
9714 /* Stops any pending load, clears the file list, and switches
9715 * back to OPERATION_MODE_BROWSE
9718 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9720 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9722 recent_stop_loading (impl);
9723 recent_clear_model (impl, TRUE);
9725 gtk_widget_destroy (impl->recent_hbox);
9726 impl->recent_hbox = NULL;
9728 gtk_widget_show (impl->browse_path_bar);
9729 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9730 gtk_widget_hide (impl->browse_new_folder_button);
9732 gtk_widget_show (impl->browse_new_folder_button);
9734 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9735 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9737 gtk_widget_show (impl->location_button);
9739 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9740 gtk_widget_show (impl->location_entry_box);
9743 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9745 impl->operation_mode = OPERATION_MODE_BROWSE;
9747 file_list_set_sort_column_ids (impl);
9751 recent_get_is_filtered (GtkFileChooserDefault *impl,
9753 GtkRecentInfo *recent_info)
9755 GtkFileFilterInfo filter_info;
9756 GtkFileFilterFlags needed;
9759 if (!impl->current_filter)
9762 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9763 needed = gtk_file_filter_get_needed (impl->current_filter);
9765 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9766 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9768 if (needed & GTK_FILE_FILTER_FILENAME)
9770 filter_info.filename = g_file_get_path (file);
9771 if (filter_info.filename)
9772 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9775 filter_info.filename = NULL;
9777 if (needed & GTK_FILE_FILTER_URI)
9779 filter_info.uri = g_file_get_uri (file);
9780 if (filter_info.uri)
9781 filter_info.contains |= GTK_FILE_FILTER_URI;
9784 filter_info.uri = NULL;
9786 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9788 if (filter_info.filename)
9789 g_free ((gchar *) filter_info.filename);
9790 if (filter_info.uri)
9791 g_free ((gchar *) filter_info.uri);
9796 /* Visibility function for the recent filter model */
9798 recent_model_visible_func (GtkTreeModel *model,
9802 GtkFileChooserDefault *impl = user_data;
9804 GtkRecentInfo *recent_info;
9807 if (!impl->current_filter)
9810 gtk_tree_model_get (model, iter,
9811 RECENT_MODEL_COL_INFO, &recent_info,
9812 MODEL_COL_FILE, &file,
9813 MODEL_COL_IS_FOLDER, &is_folder,
9822 return !recent_get_is_filtered (impl, file, recent_info);
9826 recent_setup_model (GtkFileChooserDefault *impl)
9828 g_assert (impl->recent_model == NULL);
9829 g_assert (impl->recent_model_filter == NULL);
9830 g_assert (impl->recent_model_sort == NULL);
9832 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9833 G_TYPE_STRING, /* MODEL_COL_NAME */
9834 G_TYPE_INT64, /* MODEL_COL_SIZE */
9835 G_TYPE_LONG, /* MODEL_COL_MTIME */
9836 G_TYPE_FILE, /* MODEL_COL_FILE */
9837 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */
9838 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */
9839 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */
9840 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */
9841 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */
9842 PANGO_TYPE_ELLIPSIZE_MODE, /* MODEL_COL_ELLIPSIZE */
9843 G_TYPE_CANCELLABLE, /* RECENT_MODEL_COL_CANCELLABLE */
9844 G_TYPE_POINTER /* RECENT_MODEL_COL_INFO */
9847 impl->recent_model_filter =
9848 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9849 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9850 recent_model_visible_func,
9854 /* this is the model that will actually be added to
9855 * the browse_files_tree_view widget; remember: we are
9856 * stuffing the real model into a filter model and then
9857 * into a sort model; this means we'll have to translate
9858 * the child iterator *twice* to get from a path or an
9859 * iterator coming from the tree view widget to the
9860 * real data inside the model.
9862 impl->recent_model_sort =
9863 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9864 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9868 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9872 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9876 set_sort_column (impl);
9881 GtkFileChooserDefault *impl;
9884 gint n_loaded_items;
9885 guint needs_sorting : 1;
9889 recent_idle_cleanup (gpointer data)
9891 RecentLoadData *load_data = data;
9892 GtkFileChooserDefault *impl = load_data->impl;
9894 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9895 GTK_TREE_MODEL (impl->recent_model_sort));
9897 set_busy_cursor (impl, FALSE);
9899 impl->load_recent_id = 0;
9901 if (load_data->items)
9903 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9904 g_list_free (load_data->items);
9910 struct RecentItemInsertRequest
9912 GtkFileChooserDefault *impl;
9914 GtkTreeRowReference *row_ref;
9918 recent_item_get_info_cb (GCancellable *cancellable,
9920 const GError *error,
9923 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9926 GCancellable *model_cancellable;
9927 gboolean is_folder = FALSE;
9928 struct RecentItemInsertRequest *request = data;
9932 if (!request->impl->recent_model)
9935 path = gtk_tree_row_reference_get_path (request->row_ref);
9939 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9941 gtk_tree_path_free (path);
9943 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9944 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9946 g_object_unref (model_cancellable);
9947 if (cancellable != model_cancellable)
9950 gtk_list_store_set (request->impl->recent_model, &iter,
9951 RECENT_MODEL_COL_CANCELLABLE, NULL,
9959 gtk_list_store_remove (request->impl->recent_model, &iter);
9963 is_folder = _gtk_file_info_consider_as_directory (info);
9964 size = g_file_info_get_size (info);
9965 size_text = is_folder ? g_format_size_for_display (size) : g_strdup ("");
9967 gtk_list_store_set (request->impl->recent_model, &iter,
9968 MODEL_COL_IS_FOLDER, is_folder,
9969 MODEL_COL_SIZE, size,
9970 MODEL_COL_SIZE_TEXT, size_text,
9976 g_object_unref (request->impl);
9977 g_object_unref (request->file);
9978 gtk_tree_row_reference_free (request->row_ref);
9981 g_object_unref (cancellable);
9985 recent_sort_mru (gconstpointer a,
9988 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9989 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9991 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9995 get_recent_files_limit (GtkWidget *widget)
9997 GtkSettings *settings;
10000 if (gtk_widget_has_screen (widget))
10001 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
10003 settings = gtk_settings_get_default ();
10005 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
10011 recent_idle_load (gpointer data)
10013 RecentLoadData *load_data = data;
10014 GtkFileChooserDefault *impl = load_data->impl;
10017 GtkRecentInfo *info;
10018 const gchar *uri, *display_name, *mtime_text, *collated_name;
10021 GCancellable *cancellable;
10022 struct RecentItemInsertRequest *request;
10024 if (!impl->recent_manager)
10027 /* first iteration: load all the items */
10028 if (!load_data->items)
10030 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
10031 if (!load_data->items)
10034 load_data->needs_sorting = TRUE;
10039 /* second iteration: preliminary MRU sorting and clamping */
10040 if (load_data->needs_sorting)
10044 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
10045 load_data->n_items = g_list_length (load_data->items);
10047 limit = get_recent_files_limit (GTK_WIDGET (impl));
10049 if (limit != -1 && (load_data->n_items > limit))
10053 clamp = g_list_nth (load_data->items, limit - 1);
10054 if (G_LIKELY (clamp))
10057 clamp->next = NULL;
10059 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
10062 load_data->n_items = limit;
10066 load_data->n_loaded_items = 0;
10067 load_data->needs_sorting = FALSE;
10072 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
10073 g_assert (info != NULL);
10075 uri = gtk_recent_info_get_uri (info);
10076 display_name = gtk_recent_info_get_display_name (info);
10077 file = g_file_new_for_uri (uri);
10079 gtk_list_store_append (impl->recent_model, &iter);
10080 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
10082 request = g_new0 (struct RecentItemInsertRequest, 1);
10083 request->impl = g_object_ref (impl);
10084 request->file = g_object_ref (file);
10085 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
10086 gtk_tree_path_free (p);
10088 cancellable = _gtk_file_system_get_info (impl->file_system, file,
10089 "standard::type,standard::size",
10090 recent_item_get_info_cb,
10093 collated_name = g_utf8_collate_key_for_filename (gtk_recent_info_get_display_name (info), -1);
10094 mtime = gtk_recent_info_get_modified (info);
10095 mtime_text = mtime ? my_g_format_time_for_display (mtime) : g_strdup (_("Unknown"));
10097 gtk_list_store_set (impl->recent_model, &iter,
10098 MODEL_COL_FILE, file,
10099 MODEL_COL_NAME, gtk_recent_info_get_display_name (info),
10100 MODEL_COL_NAME_COLLATED, collated_name,
10101 MODEL_COL_PIXBUF, gtk_recent_info_get_icon (info, impl->icon_size),
10102 MODEL_COL_MTIME, mtime,
10103 MODEL_COL_MTIME_TEXT, mtime_text,
10104 MODEL_COL_ELLIPSIZE, PANGO_ELLIPSIZE_END,
10105 RECENT_MODEL_COL_CANCELLABLE, cancellable,
10106 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
10109 load_data->n_loaded_items += 1;
10111 /* finished loading items */
10112 if (load_data->n_loaded_items == load_data->n_items)
10114 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
10115 g_list_free (load_data->items);
10116 load_data->items = NULL;
10125 recent_start_loading (GtkFileChooserDefault *impl)
10127 RecentLoadData *load_data;
10129 recent_stop_loading (impl);
10130 recent_clear_model (impl, TRUE);
10131 recent_setup_model (impl);
10132 set_busy_cursor (impl, TRUE);
10134 g_assert (impl->load_recent_id == 0);
10136 load_data = g_new (RecentLoadData, 1);
10137 load_data->impl = impl;
10138 load_data->items = NULL;
10139 load_data->n_items = 0;
10140 load_data->n_loaded_items = 0;
10141 load_data->needs_sorting = TRUE;
10143 /* begin lazy loading the recent files into the model */
10144 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
10147 recent_idle_cleanup);
10151 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
10161 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
10162 *list = g_slist_prepend (*list, file);
10165 /* Constructs a list of the selected paths in recent files mode */
10167 recent_get_selected_files (GtkFileChooserDefault *impl)
10170 GtkTreeSelection *selection;
10174 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10175 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
10176 result = g_slist_reverse (result);
10181 /* Called from ::should_respond(). We return whether there are selected
10182 * files in the recent files list.
10185 recent_should_respond (GtkFileChooserDefault *impl)
10187 GtkTreeSelection *selection;
10189 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
10191 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10192 return (gtk_tree_selection_count_selected_rows (selection) != 0);
10195 /* Hide the location widgets temporarily */
10197 recent_hide_entry (GtkFileChooserDefault *impl)
10203 impl->recent_hbox = gtk_hbox_new (FALSE, 12);
10206 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
10207 gtk_size_group_add_widget (impl->browse_path_bar_size_group, image);
10208 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
10211 label = gtk_label_new (NULL);
10212 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
10213 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
10214 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
10217 gtk_widget_hide (impl->browse_path_bar);
10218 gtk_widget_hide (impl->browse_new_folder_button);
10220 /* Box for recent widgets */
10221 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
10222 gtk_widget_show_all (impl->recent_hbox);
10224 /* Hide the location widgets temporarily */
10225 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10226 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10228 gtk_widget_hide (impl->location_button);
10229 gtk_widget_hide (impl->location_entry_box);
10233 /* Main entry point to the recent files functions; this gets called when
10234 * the user activates the Recently Used shortcut.
10237 recent_activate (GtkFileChooserDefault *impl)
10239 OperationMode previous_mode;
10241 if (impl->operation_mode == OPERATION_MODE_RECENT)
10244 previous_mode = impl->operation_mode;
10245 impl->operation_mode = OPERATION_MODE_RECENT;
10247 stop_operation (impl, previous_mode);
10249 recent_hide_entry (impl);
10251 /* hide the file size column if it's visible */
10252 gtk_tree_view_column_set_visible (impl->list_size_column, FALSE);
10254 file_list_set_sort_column_ids (impl);
10255 recent_start_loading (impl);
10258 /* convert an iterator coming from the model bound to
10259 * browse_files_tree_view to an interator inside the
10260 * real recent_model
10263 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
10264 GtkTreeIter *child_iter,
10267 GtkTreeIter middle;
10269 if (!impl->recent_model)
10272 if (!impl->recent_model_filter || !impl->recent_model_sort)
10275 /* pass 1: get the iterator in the filter model */
10276 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
10279 /* pass 2: get the iterator in the real model */
10280 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
10287 set_current_filter (GtkFileChooserDefault *impl,
10288 GtkFileFilter *filter)
10290 if (impl->current_filter != filter)
10294 /* NULL filters are allowed to reset to non-filtered status
10296 filter_index = g_slist_index (impl->filters, filter);
10297 if (impl->filters && filter && filter_index < 0)
10300 if (impl->current_filter)
10301 g_object_unref (impl->current_filter);
10302 impl->current_filter = filter;
10303 if (impl->current_filter)
10305 g_object_ref_sink (impl->current_filter);
10309 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
10312 if (impl->browse_files_model)
10313 install_list_model_filter (impl);
10315 if (impl->operation_mode == OPERATION_MODE_SEARCH &&
10316 impl->search_model_filter != NULL)
10317 gtk_tree_model_filter_refilter (impl->search_model_filter);
10319 /* we want to have all the matching results, and not just a
10320 * filter of the previous model
10322 if (impl->operation_mode == OPERATION_MODE_RECENT)
10323 recent_start_loading (impl);
10325 g_object_notify (G_OBJECT (impl), "filter");
10330 filter_combo_changed (GtkComboBox *combo_box,
10331 GtkFileChooserDefault *impl)
10333 gint new_index = gtk_combo_box_get_active (combo_box);
10334 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10336 set_current_filter (impl, new_filter);
10340 check_preview_change (GtkFileChooserDefault *impl)
10342 GtkTreePath *cursor_path;
10344 char *new_display_name;
10345 GtkTreeModel *model;
10347 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10348 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
10353 gtk_tree_model_get_iter (model, &iter, cursor_path);
10354 gtk_tree_model_get (model, &iter,
10355 MODEL_COL_FILE, &new_file,
10356 MODEL_COL_NAME, &new_display_name,
10359 gtk_tree_path_free (cursor_path);
10364 new_display_name = NULL;
10367 if (new_file != impl->preview_file &&
10368 !(new_file && impl->preview_file &&
10369 g_file_equal (new_file, impl->preview_file)))
10371 if (impl->preview_file)
10373 g_object_unref (impl->preview_file);
10374 g_free (impl->preview_display_name);
10379 impl->preview_file = new_file;
10380 impl->preview_display_name = new_display_name;
10384 impl->preview_file = NULL;
10385 impl->preview_display_name = NULL;
10386 g_free (new_display_name);
10389 if (impl->use_preview_label && impl->preview_label)
10390 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10392 g_signal_emit_by_name (impl, "update-preview");
10397 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10398 GtkFileSystemVolume *volume,
10399 const GError *error,
10403 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10404 GtkFileChooserDefault *impl = data;
10406 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10409 impl->shortcuts_activate_iter_cancellable = NULL;
10411 set_busy_cursor (impl, FALSE);
10418 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10422 name = _gtk_file_system_volume_get_display_name (volume);
10423 msg = g_strdup_printf (_("Could not mount %s"), name);
10425 error_message (impl, msg, error->message);
10434 file = _gtk_file_system_volume_get_root (volume);
10437 change_folder_and_display_error (impl, file, FALSE);
10438 g_object_unref (file);
10442 g_object_unref (impl);
10443 g_object_unref (cancellable);
10447 /* Activates a volume by mounting it if necessary and then switching to its
10451 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10452 GtkFileSystemVolume *volume)
10456 switch (impl->operation_mode)
10458 case OPERATION_MODE_BROWSE:
10460 case OPERATION_MODE_SEARCH:
10461 search_switch_to_browse_mode (impl);
10463 case OPERATION_MODE_RECENT:
10464 recent_switch_to_browse_mode (impl);
10468 /* We ref the file chooser since volume_mount() may run a main loop, and the
10469 * user could close the file chooser window in the meantime.
10471 g_object_ref (impl);
10473 if (!_gtk_file_system_volume_is_mounted (volume))
10475 GMountOperation *mount_op;
10477 set_busy_cursor (impl, TRUE);
10479 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
10480 impl->shortcuts_activate_iter_cancellable =
10481 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
10482 shortcuts_activate_volume_mount_cb,
10483 g_object_ref (impl));
10484 g_object_unref (mount_op);
10488 file = _gtk_file_system_volume_get_root (volume);
10491 change_folder_and_display_error (impl, file, FALSE);
10492 g_object_unref (file);
10496 g_object_unref (impl);
10499 /* Opens the folder or volume at the specified iter in the shortcuts model */
10500 struct ShortcutsActivateData
10502 GtkFileChooserDefault *impl;
10507 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10509 const GError *error,
10510 gpointer user_data)
10512 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10513 struct ShortcutsActivateData *data = user_data;
10515 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10518 data->impl->shortcuts_activate_iter_cancellable = NULL;
10523 if (!error && _gtk_file_info_consider_as_directory (info))
10524 change_folder_and_display_error (data->impl, data->file, FALSE);
10526 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10531 g_object_unref (data->impl);
10532 g_object_unref (data->file);
10535 g_object_unref (cancellable);
10539 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10540 GtkFileSystemVolume *volume,
10541 const GError *error,
10542 gpointer user_data)
10544 struct ShortcutsActivateData *data = user_data;
10548 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10550 g_object_unref (data->impl);
10551 g_object_unref (data->file);
10557 data->impl->shortcuts_activate_iter_cancellable =
10558 _gtk_file_system_get_info (data->impl->file_system, data->file,
10560 shortcuts_activate_get_info_cb, data);
10564 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10568 ShortcutType shortcut_type;
10570 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10571 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10572 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10573 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10575 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10576 SHORTCUTS_COL_DATA, &col_data,
10577 SHORTCUTS_COL_TYPE, &shortcut_type,
10580 if (impl->shortcuts_activate_iter_cancellable)
10582 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10583 impl->shortcuts_activate_iter_cancellable = NULL;
10586 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10588 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10590 GtkFileSystemVolume *volume;
10594 shortcuts_activate_volume (impl, volume);
10596 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10598 struct ShortcutsActivateData *data;
10599 GtkFileSystemVolume *volume;
10601 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10603 data = g_new0 (struct ShortcutsActivateData, 1);
10604 data->impl = g_object_ref (impl);
10605 data->file = g_object_ref (col_data);
10607 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10609 GMountOperation *mount_operation;
10610 GtkWidget *toplevel;
10612 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10614 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10616 impl->shortcuts_activate_iter_cancellable =
10617 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10619 shortcuts_activate_mount_enclosing_volume,
10624 impl->shortcuts_activate_iter_cancellable =
10625 _gtk_file_system_get_info (impl->file_system, data->file,
10627 shortcuts_activate_get_info_cb, data);
10630 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10632 search_activate (impl);
10634 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10636 recent_activate (impl);
10640 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10642 shortcuts_key_press_event_cb (GtkWidget *widget,
10643 GdkEventKey *event,
10644 GtkFileChooserDefault *impl)
10648 modifiers = gtk_accelerator_get_default_mod_mask ();
10650 if (key_is_left_or_right (event))
10652 gtk_widget_grab_focus (impl->browse_files_tree_view);
10656 if ((event->keyval == GDK_BackSpace
10657 || event->keyval == GDK_Delete
10658 || event->keyval == GDK_KP_Delete)
10659 && (event->state & modifiers) == 0)
10661 remove_selected_bookmarks (impl);
10665 if ((event->keyval == GDK_F2)
10666 && (event->state & modifiers) == 0)
10668 rename_selected_bookmark (impl);
10676 shortcuts_select_func (GtkTreeSelection *selection,
10677 GtkTreeModel *model,
10679 gboolean path_currently_selected,
10682 GtkFileChooserDefault *impl = data;
10683 GtkTreeIter filter_iter;
10684 ShortcutType shortcut_type;
10686 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10687 g_assert_not_reached ();
10689 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10691 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10695 list_select_func (GtkTreeSelection *selection,
10696 GtkTreeModel *model,
10698 gboolean path_currently_selected,
10701 GtkFileChooserDefault *impl = data;
10703 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10704 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10707 gboolean is_folder;
10709 if (!gtk_tree_model_get_iter (model, &iter, path))
10711 gtk_tree_model_get (model, &iter,
10712 MODEL_COL_IS_FOLDER, &is_folder,
10722 list_selection_changed (GtkTreeSelection *selection,
10723 GtkFileChooserDefault *impl)
10725 /* See if we are in the new folder editable row for Save mode */
10726 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10727 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10730 gboolean had_selection;
10732 info = get_selected_file_info_from_file_list (impl, &had_selection);
10733 if (!had_selection)
10734 goto out; /* normal processing */
10737 return; /* We are on the editable row for New Folder */
10742 if (impl->location_entry)
10743 update_chooser_entry (impl);
10745 check_preview_change (impl);
10746 bookmarks_check_add_sensitivity (impl);
10748 g_signal_emit_by_name (impl, "selection-changed", 0);
10751 /* Callback used when a row in the file list is activated */
10753 list_row_activated (GtkTreeView *tree_view,
10755 GtkTreeViewColumn *column,
10756 GtkFileChooserDefault *impl)
10760 GtkTreeModel *model;
10761 gboolean is_folder;
10763 model = gtk_tree_view_get_model (tree_view);
10765 if (!gtk_tree_model_get_iter (model, &iter, path))
10768 gtk_tree_model_get (model, &iter,
10769 MODEL_COL_FILE, &file,
10770 MODEL_COL_IS_FOLDER, &is_folder,
10773 if (is_folder && file)
10775 change_folder_and_display_error (impl, file, FALSE);
10779 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10780 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10781 g_signal_emit_by_name (impl, "file-activated");
10784 g_object_unref (file);
10788 path_bar_clicked (GtkPathBar *path_bar,
10791 gboolean child_is_hidden,
10792 GtkFileChooserDefault *impl)
10795 pending_select_files_add (impl, child_file);
10797 if (!change_folder_and_display_error (impl, file, FALSE))
10800 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10801 * show hidden files so that ".baz" appears in the file list, as it will still
10802 * be shown in the path bar: "/foo/[bar]/.baz"
10804 if (child_is_hidden)
10805 g_object_set (impl, "show-hidden", TRUE, NULL);
10809 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10811 GtkTreeViewColumn *column;
10812 GtkCellRenderer *renderer;
10813 GList *walk, *list;
10814 gboolean always_sensitive;
10816 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10817 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10820 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10821 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10822 for (walk = list; walk; walk = walk->next)
10824 renderer = walk->data;
10825 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10827 gtk_tree_view_column_set_attributes (column, renderer,
10828 "pixbuf", MODEL_COL_PIXBUF,
10833 gtk_tree_view_column_set_attributes (column, renderer,
10834 "text", MODEL_COL_NAME,
10835 "ellipsize", MODEL_COL_ELLIPSIZE,
10838 if (always_sensitive)
10839 g_object_set (renderer, "sensitive", TRUE, NULL);
10841 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10843 g_list_free (list);
10846 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10847 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10848 renderer = list->data;
10849 gtk_tree_view_column_set_attributes (column, renderer,
10850 "text", MODEL_COL_SIZE_TEXT,
10852 if (always_sensitive)
10853 g_object_set (renderer, "sensitive", TRUE, NULL);
10855 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10856 g_list_free (list);
10859 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10860 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10861 renderer = list->data;
10862 gtk_tree_view_column_set_attributes (column, renderer,
10863 "text", MODEL_COL_MTIME_TEXT,
10865 if (always_sensitive)
10866 g_object_set (renderer, "sensitive", TRUE, NULL);
10868 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10869 g_list_free (list);
10873 _gtk_file_chooser_default_new (void)
10875 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10879 location_set_user_text (GtkFileChooserDefault *impl,
10882 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10883 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10887 location_popup_handler (GtkFileChooserDefault *impl,
10890 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10892 GtkWidget *widget_to_focus;
10894 /* This will give us the location widgets back */
10895 switch (impl->operation_mode)
10897 case OPERATION_MODE_SEARCH:
10898 search_switch_to_browse_mode (impl);
10900 case OPERATION_MODE_RECENT:
10901 recent_switch_to_browse_mode (impl);
10903 case OPERATION_MODE_BROWSE:
10904 g_assert_not_reached ();
10908 if (impl->current_folder)
10909 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10911 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10912 widget_to_focus = impl->browse_files_tree_view;
10914 widget_to_focus = impl->location_entry;
10916 gtk_widget_grab_focus (widget_to_focus);
10920 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10921 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10923 LocationMode new_mode;
10927 /* since the user typed something, we unconditionally want to turn on the entry */
10928 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10930 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10931 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10932 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10933 new_mode = LOCATION_MODE_PATH_BAR;
10936 g_assert_not_reached ();
10940 location_mode_set (impl, new_mode, TRUE);
10941 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10944 location_set_user_text (impl, path);
10947 location_entry_set_initial_text (impl);
10948 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10952 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10953 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10955 gtk_widget_grab_focus (impl->location_entry);
10957 location_set_user_text (impl, path);
10960 g_assert_not_reached ();
10963 /* Handler for the "up-folder" keybinding signal */
10965 up_folder_handler (GtkFileChooserDefault *impl)
10967 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10970 /* Handler for the "down-folder" keybinding signal */
10972 down_folder_handler (GtkFileChooserDefault *impl)
10974 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10977 /* Switches to the shortcut in the specified index */
10979 switch_to_shortcut (GtkFileChooserDefault *impl,
10984 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10985 g_assert_not_reached ();
10987 shortcuts_activate_iter (impl, &iter);
10990 /* Handler for the "home-folder" keybinding signal */
10992 home_folder_handler (GtkFileChooserDefault *impl)
10994 if (impl->has_home)
10995 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10998 /* Handler for the "desktop-folder" keybinding signal */
11000 desktop_folder_handler (GtkFileChooserDefault *impl)
11002 if (impl->has_desktop)
11003 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11006 /* Handler for the "search-shortcut" keybinding signal */
11008 search_shortcut_handler (GtkFileChooserDefault *impl)
11010 if (impl->has_search)
11012 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11014 /* we want the entry widget to grab the focus the first
11015 * time, not the browse_files_tree_view widget.
11017 if (impl->search_entry)
11018 gtk_widget_grab_focus (impl->search_entry);
11022 /* Handler for the "recent-shortcut" keybinding signal */
11024 recent_shortcut_handler (GtkFileChooserDefault *impl)
11026 if (impl->has_recent)
11027 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11031 quick_bookmark_handler (GtkFileChooserDefault *impl,
11032 gint bookmark_index)
11037 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11040 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11042 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11043 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11046 gtk_tree_path_free (path);
11048 switch_to_shortcut (impl, bookmark_pos);
11052 show_hidden_handler (GtkFileChooserDefault *impl)
11054 g_object_set (impl,
11055 "show-hidden", !impl->show_hidden,
11060 /* Drag and drop interfaces */
11063 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11068 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11070 model->impl = NULL;
11073 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11075 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11078 ShortcutsPaneModelFilter *model;
11082 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11084 pos = *gtk_tree_path_get_indices (path);
11085 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11087 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11090 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11092 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11094 GtkSelectionData *selection_data)
11096 ShortcutsPaneModelFilter *model;
11098 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11105 /* Fill the GtkTreeDragSourceIface vtable */
11107 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11109 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11110 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11114 /* Fill the GtkTreeDragDestIface vtable */
11116 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11118 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11119 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11123 static GtkTreeModel *
11124 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11125 GtkTreeModel *child_model,
11128 ShortcutsPaneModelFilter *model;
11130 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11131 "child-model", child_model,
11132 "virtual-root", root,
11135 model->impl = impl;
11137 return GTK_TREE_MODEL (model);
11143 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11146 RecentModelSort *model;
11147 GtkTreeIter iter, child_iter;
11148 gboolean is_folder;
11150 model = RECENT_MODEL_SORT (drag_source);
11151 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11154 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11155 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11156 MODEL_COL_IS_FOLDER, &is_folder,
11163 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11165 GtkSelectionData *selection_data)
11167 RecentModelSort *model;
11168 GtkTreeIter iter, child_iter;
11172 model = RECENT_MODEL_SORT (drag_source);
11173 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11176 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11177 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11178 MODEL_COL_FILE, &file,
11180 g_assert (file != NULL);
11182 uris[0] = g_file_get_uri (file);
11185 gtk_selection_data_set_uris (selection_data, uris);
11188 g_object_unref (file);
11194 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11196 iface->row_draggable = recent_model_sort_row_draggable;
11197 iface->drag_data_get = recent_model_sort_drag_data_get;
11201 _recent_model_sort_class_init (RecentModelSortClass *klass)
11207 _recent_model_sort_init (RecentModelSort *model)
11209 model->impl = NULL;
11212 static GtkTreeModel *
11213 recent_model_sort_new (GtkFileChooserDefault *impl,
11214 GtkTreeModel *child_model)
11216 RecentModelSort *model;
11218 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11219 "model", child_model,
11221 model->impl = impl;
11223 return GTK_TREE_MODEL (model);
11229 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11232 SearchModelSort *model;
11233 GtkTreeIter iter, child_iter;
11234 gboolean is_folder;
11236 model = SEARCH_MODEL_SORT (drag_source);
11237 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11240 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11241 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11242 MODEL_COL_IS_FOLDER, &is_folder,
11249 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11251 GtkSelectionData *selection_data)
11253 SearchModelSort *model;
11254 GtkTreeIter iter, child_iter;
11258 model = SEARCH_MODEL_SORT (drag_source);
11259 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11262 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11263 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11264 MODEL_COL_FILE, &file,
11266 g_assert (file != NULL);
11268 uris[0] = g_file_get_uri (file);
11271 gtk_selection_data_set_uris (selection_data, uris);
11274 g_object_unref (file);
11280 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11282 iface->row_draggable = search_model_sort_row_draggable;
11283 iface->drag_data_get = search_model_sort_drag_data_get;
11287 _search_model_sort_class_init (SearchModelSortClass *klass)
11293 _search_model_sort_init (SearchModelSort *model)
11295 model->impl = NULL;
11298 static GtkTreeModel *
11299 search_model_sort_new (GtkFileChooserDefault *impl,
11300 GtkTreeModel *child_model)
11302 SearchModelSort *model;
11304 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11305 "model", child_model,
11307 model->impl = impl;
11309 return GTK_TREE_MODEL (model);