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.
23 #include "gdk/gdkkeysyms.h"
24 #include "gtkalignment.h"
25 #include "gtkbindings.h"
26 #include "gtkbutton.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 "gtkeventbox.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserprivate.h"
37 #include "gtkfilechooserdefault.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"
54 #include "gtkmarshalers.h"
55 #include "gtkmenuitem.h"
56 #include "gtkmessagedialog.h"
57 #include "gtkmountoperation.h"
58 #include "gtkpathbar.h"
59 #include "gtkprivate.h"
60 #include "gtkradiobutton.h"
61 #include "gtkrecentfilter.h"
62 #include "gtkrecentmanager.h"
63 #include "gtkscrolledwindow.h"
64 #include "gtkseparatormenuitem.h"
65 #include "gtksizegroup.h"
68 #include "gtktooltip.h"
69 #include "gtktreednd.h"
70 #include "gtktreeprivate.h"
71 #include "gtktreeselection.h"
72 #include "gtktypebuiltins.h"
81 #include <sys/types.h>
92 #undef PROFILE_FILE_CHOOSER
93 #ifdef PROFILE_FILE_CHOOSER
100 #define PROFILE_INDENT 4
101 static int profile_indent;
104 profile_add_indent (int indent)
106 profile_indent += indent;
107 if (profile_indent < 0)
108 g_error ("You screwed up your indentation");
112 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
117 profile_add_indent (indent);
119 if (profile_indent == 0)
120 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
122 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
128 profile_add_indent (indent);
131 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
132 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
133 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
135 #define profile_start(x, y)
136 #define profile_end(x, y)
137 #define profile_msg(x, y)
142 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
144 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
146 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
148 #define MAX_LOADING_TIME 500
150 struct _GtkFileChooserDefaultClass
152 GtkVBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
172 static guint signals[LAST_SIGNAL] = { 0 };
174 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
176 SHORTCUTS_COL_PIXBUF,
180 SHORTCUTS_COL_REMOVABLE,
181 SHORTCUTS_COL_PIXBUF_VISIBLE,
182 SHORTCUTS_COL_CANCELLABLE,
183 SHORTCUTS_COL_NUM_COLUMNS
188 SHORTCUT_TYPE_VOLUME,
189 SHORTCUT_TYPE_SEPARATOR,
190 SHORTCUT_TYPE_SEARCH,
194 /* Column numbers for the file list */
199 FILE_LIST_COL_NUM_COLUMNS
202 /* Column numbers for the search model.
203 * Keep this in sync with search_setup_model()
206 SEARCH_MODEL_COL_FILE,
207 SEARCH_MODEL_COL_DISPLAY_NAME,
208 SEARCH_MODEL_COL_COLLATION_KEY,
209 SEARCH_MODEL_COL_STAT,
210 SEARCH_MODEL_COL_CANCELLABLE,
211 SEARCH_MODEL_COL_PIXBUF,
212 SEARCH_MODEL_COL_MIME_TYPE,
213 SEARCH_MODEL_COL_IS_FOLDER,
214 SEARCH_MODEL_COL_NUM_COLUMNS
218 RECENT_MODEL_COL_FILE,
219 RECENT_MODEL_COL_DISPLAY_NAME,
220 RECENT_MODEL_COL_INFO,
221 RECENT_MODEL_COL_IS_FOLDER,
222 RECENT_MODEL_COL_CANCELLABLE,
223 RECENT_MODEL_COL_NUM_COLUMNS
226 /* Identifiers for target types */
232 search_is_possible (GtkFileChooserDefault *impl)
234 if (impl->search_engine == NULL)
235 impl->search_engine = _gtk_search_engine_new ();
237 return impl->search_engine != NULL;
240 /* Interesting places in the shortcuts bar */
244 SHORTCUTS_RECENT_SEPARATOR,
249 SHORTCUTS_BOOKMARKS_SEPARATOR,
251 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
252 SHORTCUTS_CURRENT_FOLDER
255 /* Icon size for if we can't get it from the theme */
256 #define FALLBACK_ICON_SIZE 16
258 #define PREVIEW_HBOX_SPACING 12
262 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
263 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
265 static GObject* gtk_file_chooser_default_constructor (GType type,
266 guint n_construct_properties,
267 GObjectConstructParam *construct_params);
268 static void gtk_file_chooser_default_finalize (GObject *object);
269 static void gtk_file_chooser_default_set_property (GObject *object,
273 static void gtk_file_chooser_default_get_property (GObject *object,
277 static void gtk_file_chooser_default_dispose (GObject *object);
278 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
279 static void gtk_file_chooser_default_map (GtkWidget *widget);
280 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
281 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
282 GtkWidget *previous_toplevel);
283 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
284 GtkStyle *previous_style);
285 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
286 GdkScreen *previous_screen);
287 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
288 GtkAllocation *allocation);
290 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
293 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
296 gboolean clear_entry,
298 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
299 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
301 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
304 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
306 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
307 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
308 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
309 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
310 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
311 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
312 GtkFileFilter *filter);
313 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
314 GtkFileFilter *filter);
315 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
316 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
319 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
322 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
324 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
326 gint *default_height);
327 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
328 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
330 static void location_popup_handler (GtkFileChooserDefault *impl,
332 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
333 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
334 static void up_folder_handler (GtkFileChooserDefault *impl);
335 static void down_folder_handler (GtkFileChooserDefault *impl);
336 static void home_folder_handler (GtkFileChooserDefault *impl);
337 static void desktop_folder_handler (GtkFileChooserDefault *impl);
338 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
339 gint bookmark_index);
340 static void show_hidden_handler (GtkFileChooserDefault *impl);
341 static void search_shortcut_handler (GtkFileChooserDefault *impl);
342 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
343 static void update_appearance (GtkFileChooserDefault *impl);
345 static void set_current_filter (GtkFileChooserDefault *impl,
346 GtkFileFilter *filter);
347 static void check_preview_change (GtkFileChooserDefault *impl);
349 static void filter_combo_changed (GtkComboBox *combo_box,
350 GtkFileChooserDefault *impl);
352 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
354 GtkFileChooserDefault *impl);
356 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
359 gboolean path_currently_selected,
361 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
363 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
365 static int shortcuts_get_index (GtkFileChooserDefault *impl,
366 ShortcutsIndex where);
367 static int shortcut_find_position (GtkFileChooserDefault *impl,
370 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
372 static gboolean list_select_func (GtkTreeSelection *selection,
375 gboolean path_currently_selected,
378 static void list_selection_changed (GtkTreeSelection *tree_selection,
379 GtkFileChooserDefault *impl);
380 static void list_row_activated (GtkTreeView *tree_view,
382 GtkTreeViewColumn *column,
383 GtkFileChooserDefault *impl);
385 static void select_func (GtkFileSystemModel *model,
390 static void path_bar_clicked (GtkPathBar *path_bar,
393 gboolean child_is_hidden,
394 GtkFileChooserDefault *impl);
396 static void add_bookmark_button_clicked_cb (GtkButton *button,
397 GtkFileChooserDefault *impl);
398 static void remove_bookmark_button_clicked_cb (GtkButton *button,
399 GtkFileChooserDefault *impl);
400 static void save_folder_combo_changed_cb (GtkComboBox *combo,
401 GtkFileChooserDefault *impl);
403 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
404 GtkCellRenderer *cell,
405 GtkTreeModel *tree_model,
408 static void list_name_data_func (GtkTreeViewColumn *tree_column,
409 GtkCellRenderer *cell,
410 GtkTreeModel *tree_model,
414 static void list_size_data_func (GtkTreeViewColumn *tree_column,
415 GtkCellRenderer *cell,
416 GtkTreeModel *tree_model,
420 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
421 GtkCellRenderer *cell,
422 GtkTreeModel *tree_model,
426 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
429 static void load_remove_timer (GtkFileChooserDefault *impl);
430 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
432 static void location_button_toggled_cb (GtkToggleButton *toggle,
433 GtkFileChooserDefault *impl);
434 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
436 static void search_stop_searching (GtkFileChooserDefault *impl,
437 gboolean remove_query);
438 static void search_clear_model (GtkFileChooserDefault *impl,
439 gboolean remove_from_treeview);
440 static gboolean search_should_respond (GtkFileChooserDefault *impl);
441 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
442 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
443 static void search_entry_activate_cb (GtkEntry *entry,
445 static void settings_load (GtkFileChooserDefault *impl);
446 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
447 GtkTreeIter *child_iter,
450 static void recent_stop_loading (GtkFileChooserDefault *impl);
451 static void recent_clear_model (GtkFileChooserDefault *impl,
452 gboolean remove_from_treeview);
453 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
454 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
455 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
456 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
457 GtkTreeIter *child_iter,
459 static void set_file_system_backend (GtkFileChooserDefault *impl);
465 /* Drag and drop interface declarations */
468 GtkTreeModelFilter parent;
470 GtkFileChooserDefault *impl;
471 } ShortcutsPaneModelFilter;
474 GtkTreeModelFilterClass parent_class;
475 } ShortcutsPaneModelFilterClass;
477 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
478 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
480 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
482 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
483 _shortcuts_pane_model_filter,
484 GTK_TYPE_TREE_MODEL_FILTER,
485 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
486 shortcuts_pane_model_filter_drag_source_iface_init))
488 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
489 GtkTreeModel *child_model,
494 GtkTreeModelSort parent;
496 GtkFileChooserDefault *impl;
500 GtkTreeModelSortClass parent_class;
501 } RecentModelSortClass;
503 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
504 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
506 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
508 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
510 GTK_TYPE_TREE_MODEL_SORT,
511 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
512 recent_model_sort_drag_source_iface_init));
514 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
515 GtkTreeModel *child_model);
519 GtkTreeModelSort parent;
521 GtkFileChooserDefault *impl;
525 GtkTreeModelSortClass parent_class;
526 } SearchModelSortClass;
528 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
529 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
531 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
533 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
535 GTK_TYPE_TREE_MODEL_SORT,
536 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
537 search_model_sort_drag_source_iface_init));
539 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
540 GtkTreeModel *child_model);
544 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
545 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
546 gtk_file_chooser_default_iface_init)
547 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
548 gtk_file_chooser_embed_default_iface_init));
551 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
553 static const guint quick_bookmark_keyvals[10] = {
554 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
556 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
557 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
558 GtkBindingSet *binding_set;
561 gobject_class->finalize = gtk_file_chooser_default_finalize;
562 gobject_class->constructor = gtk_file_chooser_default_constructor;
563 gobject_class->set_property = gtk_file_chooser_default_set_property;
564 gobject_class->get_property = gtk_file_chooser_default_get_property;
565 gobject_class->dispose = gtk_file_chooser_default_dispose;
567 widget_class->show_all = gtk_file_chooser_default_show_all;
568 widget_class->map = gtk_file_chooser_default_map;
569 widget_class->unmap = gtk_file_chooser_default_unmap;
570 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
571 widget_class->style_set = gtk_file_chooser_default_style_set;
572 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
573 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
575 signals[LOCATION_POPUP] =
576 _gtk_binding_signal_new (I_("location-popup"),
577 G_OBJECT_CLASS_TYPE (class),
578 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
579 G_CALLBACK (location_popup_handler),
581 _gtk_marshal_VOID__STRING,
582 G_TYPE_NONE, 1, G_TYPE_STRING);
583 signals[LOCATION_POPUP_ON_PASTE] =
584 _gtk_binding_signal_new ("location-popup-on-paste",
585 G_OBJECT_CLASS_TYPE (class),
586 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
587 G_CALLBACK (location_popup_on_paste_handler),
589 _gtk_marshal_VOID__VOID,
591 signals[LOCATION_TOGGLE_POPUP] =
592 _gtk_binding_signal_new (I_("location-toggle-popup"),
593 G_OBJECT_CLASS_TYPE (class),
594 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
595 G_CALLBACK (location_toggle_popup_handler),
597 _gtk_marshal_VOID__VOID,
600 _gtk_binding_signal_new (I_("up-folder"),
601 G_OBJECT_CLASS_TYPE (class),
602 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
603 G_CALLBACK (up_folder_handler),
605 _gtk_marshal_VOID__VOID,
607 signals[DOWN_FOLDER] =
608 _gtk_binding_signal_new (I_("down-folder"),
609 G_OBJECT_CLASS_TYPE (class),
610 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
611 G_CALLBACK (down_folder_handler),
613 _gtk_marshal_VOID__VOID,
615 signals[HOME_FOLDER] =
616 _gtk_binding_signal_new (I_("home-folder"),
617 G_OBJECT_CLASS_TYPE (class),
618 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
619 G_CALLBACK (home_folder_handler),
621 _gtk_marshal_VOID__VOID,
623 signals[DESKTOP_FOLDER] =
624 _gtk_binding_signal_new (I_("desktop-folder"),
625 G_OBJECT_CLASS_TYPE (class),
626 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
627 G_CALLBACK (desktop_folder_handler),
629 _gtk_marshal_VOID__VOID,
631 signals[QUICK_BOOKMARK] =
632 _gtk_binding_signal_new (I_("quick-bookmark"),
633 G_OBJECT_CLASS_TYPE (class),
634 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
635 G_CALLBACK (quick_bookmark_handler),
637 _gtk_marshal_VOID__INT,
638 G_TYPE_NONE, 1, G_TYPE_INT);
639 signals[SHOW_HIDDEN] =
640 _gtk_binding_signal_new ("show-hidden",
641 G_OBJECT_CLASS_TYPE (class),
642 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
643 G_CALLBACK (show_hidden_handler),
645 _gtk_marshal_VOID__VOID,
647 signals[SEARCH_SHORTCUT] =
648 _gtk_binding_signal_new ("search-shortcut",
649 G_OBJECT_CLASS_TYPE (class),
650 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
651 G_CALLBACK (search_shortcut_handler),
653 _gtk_marshal_VOID__VOID,
655 signals[RECENT_SHORTCUT] =
656 _gtk_binding_signal_new ("recent-shortcut",
657 G_OBJECT_CLASS_TYPE (class),
658 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
659 G_CALLBACK (recent_shortcut_handler),
661 _gtk_marshal_VOID__VOID,
664 binding_set = gtk_binding_set_by_class (class);
666 gtk_binding_entry_add_signal (binding_set,
667 GDK_l, GDK_CONTROL_MASK,
668 "location-toggle-popup",
671 gtk_binding_entry_add_signal (binding_set,
674 1, G_TYPE_STRING, "/");
675 gtk_binding_entry_add_signal (binding_set,
678 1, G_TYPE_STRING, "/");
681 gtk_binding_entry_add_signal (binding_set,
684 1, G_TYPE_STRING, "~");
687 gtk_binding_entry_add_signal (binding_set,
688 GDK_v, GDK_CONTROL_MASK,
689 "location-popup-on-paste",
691 gtk_binding_entry_add_signal (binding_set,
692 GDK_Up, GDK_MOD1_MASK,
695 gtk_binding_entry_add_signal (binding_set,
699 gtk_binding_entry_add_signal (binding_set,
700 GDK_KP_Up, GDK_MOD1_MASK,
704 gtk_binding_entry_add_signal (binding_set,
705 GDK_Down, GDK_MOD1_MASK,
708 gtk_binding_entry_add_signal (binding_set,
709 GDK_KP_Down, GDK_MOD1_MASK,
713 gtk_binding_entry_add_signal (binding_set,
714 GDK_Home, GDK_MOD1_MASK,
717 gtk_binding_entry_add_signal (binding_set,
718 GDK_KP_Home, GDK_MOD1_MASK,
721 gtk_binding_entry_add_signal (binding_set,
722 GDK_d, GDK_MOD1_MASK,
725 gtk_binding_entry_add_signal (binding_set,
726 GDK_h, GDK_CONTROL_MASK,
729 gtk_binding_entry_add_signal (binding_set,
730 GDK_s, GDK_MOD1_MASK,
733 gtk_binding_entry_add_signal (binding_set,
734 GDK_r, GDK_MOD1_MASK,
738 for (i = 0; i < 10; i++)
739 gtk_binding_entry_add_signal (binding_set,
740 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
744 _gtk_file_chooser_install_properties (gobject_class);
748 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
750 iface->select_file = gtk_file_chooser_default_select_file;
751 iface->unselect_file = gtk_file_chooser_default_unselect_file;
752 iface->select_all = gtk_file_chooser_default_select_all;
753 iface->unselect_all = gtk_file_chooser_default_unselect_all;
754 iface->get_files = gtk_file_chooser_default_get_files;
755 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
756 iface->get_file_system = gtk_file_chooser_default_get_file_system;
757 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
758 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
759 iface->set_current_name = gtk_file_chooser_default_set_current_name;
760 iface->add_filter = gtk_file_chooser_default_add_filter;
761 iface->remove_filter = gtk_file_chooser_default_remove_filter;
762 iface->list_filters = gtk_file_chooser_default_list_filters;
763 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
764 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
765 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
769 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
771 iface->get_default_size = gtk_file_chooser_default_get_default_size;
772 iface->should_respond = gtk_file_chooser_default_should_respond;
773 iface->initial_focus = gtk_file_chooser_default_initial_focus;
777 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
779 profile_start ("start", NULL);
780 #ifdef PROFILE_FILE_CHOOSER
781 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
783 impl->local_only = TRUE;
784 impl->preview_widget_active = TRUE;
785 impl->use_preview_label = TRUE;
786 impl->select_multiple = FALSE;
787 impl->show_hidden = FALSE;
788 impl->icon_size = FALLBACK_ICON_SIZE;
789 impl->load_state = LOAD_EMPTY;
790 impl->reload_state = RELOAD_EMPTY;
791 impl->pending_select_files = NULL;
792 impl->location_mode = LOCATION_MODE_PATH_BAR;
793 impl->operation_mode = OPERATION_MODE_BROWSE;
794 impl->recent_manager = gtk_recent_manager_get_default ();
796 gtk_box_set_spacing (GTK_BOX (impl), 12);
798 set_file_system_backend (impl);
800 profile_end ("end", NULL);
803 /* Frees the data columns for the specified iter in the shortcuts model*/
805 shortcuts_free_row_data (GtkFileChooserDefault *impl,
809 ShortcutType shortcut_type;
810 GCancellable *cancellable;
812 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
813 SHORTCUTS_COL_DATA, &col_data,
814 SHORTCUTS_COL_TYPE, &shortcut_type,
815 SHORTCUTS_COL_CANCELLABLE, &cancellable,
819 g_cancellable_cancel (cancellable);
821 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
822 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
826 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
828 GtkFileSystemVolume *volume;
831 _gtk_file_system_volume_free (volume);
837 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
840 g_object_unref (file);
844 /* Frees all the data columns in the shortcuts model */
846 shortcuts_free (GtkFileChooserDefault *impl)
850 if (!impl->shortcuts_model)
853 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
856 shortcuts_free_row_data (impl, &iter);
858 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
860 g_object_unref (impl->shortcuts_model);
861 impl->shortcuts_model = NULL;
865 pending_select_files_free (GtkFileChooserDefault *impl)
867 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
868 g_slist_free (impl->pending_select_files);
869 impl->pending_select_files = NULL;
873 pending_select_files_add (GtkFileChooserDefault *impl,
876 impl->pending_select_files =
877 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
880 /* Used from gtk_tree_selection_selected_foreach() */
882 store_selection_foreach (GtkTreeModel *model,
887 GtkFileChooserDefault *impl;
888 GtkTreeIter child_iter;
891 impl = GTK_FILE_CHOOSER_DEFAULT (data);
893 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
895 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
896 pending_select_files_add (impl, file);
899 /* Stores the current selection in the list of paths to select; this is used to
900 * preserve the selection when reloading the current folder.
903 pending_select_files_store_selection (GtkFileChooserDefault *impl)
905 GtkTreeSelection *selection;
907 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
908 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
912 gtk_file_chooser_default_finalize (GObject *object)
914 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
917 if (impl->shortcuts_pane_filter_model)
918 g_object_unref (impl->shortcuts_pane_filter_model);
920 if (impl->shortcuts_combo_filter_model)
921 g_object_unref (impl->shortcuts_combo_filter_model);
923 shortcuts_free (impl);
925 g_object_unref (impl->file_system);
927 g_free (impl->browse_files_last_selected_name);
929 for (l = impl->filters; l; l = l->next)
931 GtkFileFilter *filter;
933 filter = GTK_FILE_FILTER (l->data);
934 g_object_unref (filter);
936 g_slist_free (impl->filters);
938 if (impl->current_filter)
939 g_object_unref (impl->current_filter);
941 if (impl->current_volume_file)
942 g_object_unref (impl->current_volume_file);
944 if (impl->current_folder)
945 g_object_unref (impl->current_folder);
947 if (impl->preview_file)
948 g_object_unref (impl->preview_file);
950 load_remove_timer (impl);
952 /* Free all the Models we have */
953 if (impl->browse_files_model)
954 g_object_unref (impl->browse_files_model);
956 if (impl->sort_model)
957 g_object_unref (impl->sort_model);
959 search_clear_model (impl, FALSE);
960 recent_clear_model (impl, FALSE);
962 g_free (impl->preview_display_name);
964 g_free (impl->edited_new_text);
966 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
969 /* Shows an error dialog set as transient for the specified window */
971 error_message_with_parent (GtkWindow *parent,
977 dialog = gtk_message_dialog_new (parent,
978 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
983 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
987 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
989 gtk_dialog_run (GTK_DIALOG (dialog));
990 gtk_widget_destroy (dialog);
993 /* Returns a toplevel GtkWindow, or NULL if none */
995 get_toplevel (GtkWidget *widget)
999 toplevel = gtk_widget_get_toplevel (widget);
1000 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1003 return GTK_WINDOW (toplevel);
1006 /* Shows an error dialog for the file chooser */
1008 error_message (GtkFileChooserDefault *impl,
1012 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1015 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1017 error_dialog (GtkFileChooserDefault *impl,
1028 uri = g_file_get_uri (file);
1029 text = g_strdup_printf (msg, uri);
1030 error_message (impl, text, error->message);
1033 g_error_free (error);
1037 /* Displays an error message about not being able to get information for a file.
1038 * Frees the GError as well.
1041 error_getting_info_dialog (GtkFileChooserDefault *impl,
1046 _("Could not retrieve information about the file"),
1050 /* Shows an error dialog about not being able to add a bookmark */
1052 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1057 _("Could not add a bookmark"),
1061 /* Shows an error dialog about not being able to remove a bookmark */
1063 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1068 _("Could not remove bookmark"),
1072 /* Shows an error dialog about not being able to create a folder */
1074 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1079 _("The folder could not be created"),
1083 /* Shows an error about not being able to create a folder because a file with
1084 * the same name is already there.
1087 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1092 _("The folder could not be created, as a file with the same "
1093 "name already exists. Try using a different name for the "
1094 "folder, or rename the file first."),
1098 /* Shows an error dialog about not being able to create a filename */
1100 error_building_filename_dialog (GtkFileChooserDefault *impl,
1103 error_dialog (impl, _("Invalid file name"),
1107 /* Shows an error dialog when we cannot switch to a folder */
1109 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1113 error_dialog (impl, _("The folder contents could not be displayed"),
1117 /* Changes folders, displaying an error dialog if this fails */
1119 change_folder_and_display_error (GtkFileChooserDefault *impl,
1121 gboolean clear_entry)
1126 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1128 /* We copy the path because of this case:
1130 * list_row_activated()
1131 * fetches path from model; path belongs to the model (*)
1132 * calls change_folder_and_display_error()
1133 * calls _gtk_file_chooser_set_current_folder_file()
1134 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1138 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1141 error_changing_folder_dialog (impl, file, error);
1147 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1149 if (impl->use_preview_label)
1151 if (!impl->preview_label)
1153 impl->preview_label = gtk_label_new (impl->preview_display_name);
1154 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1155 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1156 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1157 gtk_widget_show (impl->preview_label);
1162 if (impl->preview_label)
1164 gtk_widget_destroy (impl->preview_label);
1165 impl->preview_label = NULL;
1169 if (impl->preview_widget_active && impl->preview_widget)
1170 gtk_widget_show (impl->preview_box);
1172 gtk_widget_hide (impl->preview_box);
1174 g_signal_emit_by_name (impl, "default-size-changed");
1178 set_preview_widget (GtkFileChooserDefault *impl,
1179 GtkWidget *preview_widget)
1181 if (preview_widget == impl->preview_widget)
1184 if (impl->preview_widget)
1185 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1186 impl->preview_widget);
1188 impl->preview_widget = preview_widget;
1189 if (impl->preview_widget)
1191 gtk_widget_show (impl->preview_widget);
1192 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1193 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1194 impl->preview_widget,
1195 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1198 update_preview_widget_visibility (impl);
1201 /* Renders a "Search" icon at an appropriate size for a tree view */
1203 render_search_icon (GtkFileChooserDefault *impl)
1205 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1209 render_recent_icon (GtkFileChooserDefault *impl)
1211 GtkIconTheme *theme;
1214 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1215 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1217 theme = gtk_icon_theme_get_default ();
1219 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1225 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1231 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1232 struct ReloadIconsData
1234 GtkFileChooserDefault *impl;
1235 GtkTreeRowReference *row_ref;
1239 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1241 const GError *error,
1247 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1248 struct ReloadIconsData *data = user_data;
1250 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1253 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1255 if (cancelled || error)
1258 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1260 path = gtk_tree_row_reference_get_path (data->row_ref);
1261 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1262 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1263 SHORTCUTS_COL_PIXBUF, pixbuf,
1265 gtk_tree_path_free (path);
1268 g_object_unref (pixbuf);
1271 gtk_tree_row_reference_free (data->row_ref);
1272 g_object_unref (data->impl);
1275 g_object_unref (cancellable);
1279 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1284 profile_start ("start", NULL);
1286 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1289 for (l = impl->reload_icon_cancellables; l; l = l->next)
1291 GCancellable *cancellable = G_CANCELLABLE (l->data);
1292 g_cancellable_cancel (cancellable);
1294 g_slist_free (impl->reload_icon_cancellables);
1295 impl->reload_icon_cancellables = NULL;
1300 ShortcutType shortcut_type;
1301 gboolean pixbuf_visible;
1304 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1305 SHORTCUTS_COL_DATA, &data,
1306 SHORTCUTS_COL_TYPE, &shortcut_type,
1307 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1313 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1315 GtkFileSystemVolume *volume;
1318 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1319 impl->icon_size, NULL);
1321 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1323 if (g_file_is_native (G_FILE (data)))
1326 struct ReloadIconsData *info;
1327 GtkTreePath *tree_path;
1328 GCancellable *cancellable;
1332 info = g_new0 (struct ReloadIconsData, 1);
1333 info->impl = g_object_ref (impl);
1334 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1335 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1336 gtk_tree_path_free (tree_path);
1338 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1340 shortcuts_reload_icons_get_info_cb,
1342 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1346 GtkIconTheme *icon_theme;
1348 /* Don't call get_info for remote paths to avoid latency and
1350 * If we switch to a better bookmarks file format (XBEL), we
1351 * should use mime info to get a better icon.
1353 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1354 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1355 impl->icon_size, 0, NULL);
1358 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1360 pixbuf = render_search_icon (impl);
1362 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1364 pixbuf = render_recent_icon (impl);
1367 gtk_list_store_set (impl->shortcuts_model, &iter,
1368 SHORTCUTS_COL_PIXBUF, pixbuf,
1372 g_object_unref (pixbuf);
1376 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1380 profile_end ("end", NULL);
1384 shortcuts_find_folder (GtkFileChooserDefault *impl,
1387 GtkTreeSelection *selection;
1391 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1393 g_assert (folder != NULL);
1394 pos = shortcut_find_position (impl, folder);
1397 gtk_tree_selection_unselect_all (selection);
1401 path = gtk_tree_path_new_from_indices (pos, -1);
1402 gtk_tree_selection_select_path (selection, path);
1403 gtk_tree_path_free (path);
1406 /* If a shortcut corresponds to the current folder, selects it */
1408 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1410 shortcuts_find_folder (impl, impl->current_folder);
1413 /* Removes the specified number of rows from the shortcuts list */
1415 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1421 path = gtk_tree_path_new_from_indices (start_row, -1);
1423 for (; n_rows; n_rows--)
1427 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1428 g_assert_not_reached ();
1430 shortcuts_free_row_data (impl, &iter);
1431 gtk_list_store_remove (impl->shortcuts_model, &iter);
1434 gtk_tree_path_free (path);
1438 shortcuts_update_count (GtkFileChooserDefault *impl,
1439 ShortcutsIndex type,
1444 case SHORTCUTS_HOME:
1446 impl->has_home = FALSE;
1448 impl->has_home = TRUE;
1451 case SHORTCUTS_DESKTOP:
1453 impl->has_desktop = FALSE;
1455 impl->has_desktop = TRUE;
1458 case SHORTCUTS_VOLUMES:
1459 impl->num_volumes += value;
1462 case SHORTCUTS_SHORTCUTS:
1463 impl->num_shortcuts += value;
1466 case SHORTCUTS_BOOKMARKS:
1467 impl->num_bookmarks += value;
1470 case SHORTCUTS_CURRENT_FOLDER:
1472 impl->shortcuts_current_folder_active = FALSE;
1474 impl->shortcuts_current_folder_active = TRUE;
1483 struct ShortcutsInsertRequest
1485 GtkFileChooserDefault *impl;
1489 GtkTreeRowReference *row_ref;
1490 ShortcutsIndex type;
1496 get_file_info_finished (GCancellable *cancellable,
1498 const GError *error,
1502 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1506 GCancellable *model_cancellable;
1507 struct ShortcutsInsertRequest *request = data;
1509 path = gtk_tree_row_reference_get_path (request->row_ref);
1511 /* Handle doesn't exist anymore in the model */
1514 pos = gtk_tree_path_get_indices (path)[0];
1515 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1517 gtk_tree_path_free (path);
1519 /* validate cancellable, else goto out */
1520 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1521 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1523 if (cancellable != model_cancellable)
1526 /* set the cancellable to NULL in the model (we unref later on) */
1527 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1528 SHORTCUTS_COL_CANCELLABLE, NULL,
1536 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1537 shortcuts_update_count (request->impl, request->type, -1);
1539 if (request->type == SHORTCUTS_HOME)
1543 home = g_file_new_for_path (g_get_home_dir ());
1544 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1545 g_object_unref (home);
1547 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1549 /* Remove the current folder separator */
1550 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1551 shortcuts_remove_rows (request->impl, separator_pos, 1);
1557 if (!request->label_copy)
1558 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1559 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1560 request->impl->icon_size);
1562 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1563 SHORTCUTS_COL_PIXBUF, pixbuf,
1564 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1565 SHORTCUTS_COL_NAME, request->label_copy,
1566 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1567 SHORTCUTS_COL_REMOVABLE, request->removable,
1570 if (request->impl->shortcuts_pane_filter_model)
1571 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1573 if (request->impl->shortcuts_combo_filter_model)
1574 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1576 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1577 request->impl->save_folder_combo != NULL)
1579 /* The current folder is updated via _activate_iter(), don't
1580 * have save_folder_combo_changed_cb() call _activate_iter()
1583 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1584 G_CALLBACK (save_folder_combo_changed_cb),
1587 if (request->impl->has_search)
1590 if (request->impl->has_recent)
1593 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1594 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1595 G_CALLBACK (save_folder_combo_changed_cb),
1600 g_object_unref (pixbuf);
1603 g_object_unref (request->impl);
1604 g_object_unref (request->file);
1605 gtk_tree_row_reference_free (request->row_ref);
1606 g_free (request->label_copy);
1609 g_object_unref (cancellable);
1612 /* FIXME: GtkFileSystem needs a function to split a remote path
1613 * into hostname and path components, or maybe just have a
1614 * gtk_file_system_path_get_display_name().
1616 * This function is also used in gtkfilechooserbutton.c
1619 _gtk_file_chooser_label_for_file (GFile *file)
1621 const gchar *path, *start, *end, *p;
1622 gchar *uri, *host, *label;
1624 uri = g_file_get_uri (file);
1626 start = strstr (uri, "://");
1628 path = strchr (start, '/');
1634 end = uri + strlen (uri);
1638 /* strip username */
1639 p = strchr (start, '@');
1645 p = strchr (start, ':');
1649 host = g_strndup (start, end - start);
1651 /* Translators: the first string is a path and the second string
1652 * is a hostname. Nautilus and the panel contain the same string
1655 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1663 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1664 * inserts a volume. A position of -1 indicates the end of the tree.
1667 shortcuts_insert_file (GtkFileChooserDefault *impl,
1669 ShortcutType shortcut_type,
1670 GtkFileSystemVolume *volume,
1674 ShortcutsIndex type)
1677 GdkPixbuf *pixbuf = NULL;
1678 gpointer data = NULL;
1680 GtkIconTheme *icon_theme;
1682 profile_start ("start shortcut", NULL);
1684 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1687 label_copy = _gtk_file_system_volume_get_display_name (volume);
1688 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1689 impl->icon_size, NULL);
1691 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1693 if (g_file_is_native (file))
1695 struct ShortcutsInsertRequest *request;
1696 GCancellable *cancellable;
1699 request = g_new0 (struct ShortcutsInsertRequest, 1);
1700 request->impl = g_object_ref (impl);
1701 request->file = g_object_ref (file);
1702 request->name_only = TRUE;
1703 request->removable = removable;
1705 request->type = type;
1707 request->label_copy = g_strdup (label);
1710 gtk_list_store_append (impl->shortcuts_model, &iter);
1712 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1714 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1715 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1716 gtk_tree_path_free (p);
1718 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1719 "standard::is-hidden,standard::display-name,standard::icon",
1720 get_file_info_finished, request);
1722 gtk_list_store_set (impl->shortcuts_model, &iter,
1723 SHORTCUTS_COL_DATA, g_object_ref (file),
1724 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1725 SHORTCUTS_COL_CANCELLABLE, cancellable,
1728 shortcuts_update_count (impl, type, 1);
1734 /* Don't call get_info for remote paths to avoid latency and
1737 data = g_object_ref (file);
1739 label_copy = g_strdup (label);
1741 label_copy = _gtk_file_chooser_label_for_file (file);
1743 /* If we switch to a better bookmarks file format (XBEL), we
1744 * should use mime info to get a better icon.
1746 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1747 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1748 impl->icon_size, 0, NULL);
1753 g_assert_not_reached ();
1759 gtk_list_store_append (impl->shortcuts_model, &iter);
1761 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1763 shortcuts_update_count (impl, type, 1);
1765 gtk_list_store_set (impl->shortcuts_model, &iter,
1766 SHORTCUTS_COL_PIXBUF, pixbuf,
1767 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1768 SHORTCUTS_COL_NAME, label_copy,
1769 SHORTCUTS_COL_DATA, data,
1770 SHORTCUTS_COL_TYPE, shortcut_type,
1771 SHORTCUTS_COL_REMOVABLE, removable,
1772 SHORTCUTS_COL_CANCELLABLE, NULL,
1775 if (impl->shortcuts_pane_filter_model)
1776 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1778 if (impl->shortcuts_combo_filter_model)
1779 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1781 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1783 /* The current folder is updated via _activate_iter(), don't
1784 * have save_folder_combo_changed_cb() call _activate_iter()
1787 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1789 if (impl->has_search)
1792 if (impl->has_recent)
1795 g_signal_handlers_block_by_func (impl->save_folder_combo,
1796 G_CALLBACK (save_folder_combo_changed_cb),
1799 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1800 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1801 G_CALLBACK (save_folder_combo_changed_cb),
1805 g_free (label_copy);
1808 g_object_unref (pixbuf);
1810 profile_end ("end", NULL);
1814 shortcuts_append_search (GtkFileChooserDefault *impl)
1819 pixbuf = render_search_icon (impl);
1821 gtk_list_store_append (impl->shortcuts_model, &iter);
1822 gtk_list_store_set (impl->shortcuts_model, &iter,
1823 SHORTCUTS_COL_PIXBUF, pixbuf,
1824 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1825 SHORTCUTS_COL_NAME, _("Search"),
1826 SHORTCUTS_COL_DATA, NULL,
1827 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1828 SHORTCUTS_COL_REMOVABLE, FALSE,
1832 g_object_unref (pixbuf);
1834 impl->has_search = TRUE;
1838 shortcuts_append_recent (GtkFileChooserDefault *impl)
1843 pixbuf = render_recent_icon (impl);
1845 gtk_list_store_append (impl->shortcuts_model, &iter);
1846 gtk_list_store_set (impl->shortcuts_model, &iter,
1847 SHORTCUTS_COL_PIXBUF, pixbuf,
1848 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1849 SHORTCUTS_COL_NAME, _("Recently Used"),
1850 SHORTCUTS_COL_DATA, NULL,
1851 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1852 SHORTCUTS_COL_REMOVABLE, FALSE,
1856 g_object_unref (pixbuf);
1858 impl->has_recent = TRUE;
1861 /* Appends an item for the user's home directory to the shortcuts model */
1863 shortcuts_append_home (GtkFileChooserDefault *impl)
1865 const char *home_path;
1868 profile_start ("start", NULL);
1870 home_path = g_get_home_dir ();
1871 if (home_path == NULL)
1873 profile_end ("end - no home directory!?", NULL);
1877 home = g_file_new_for_path (home_path);
1878 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1879 impl->has_home = TRUE;
1881 g_object_unref (home);
1883 profile_end ("end", NULL);
1886 /* Appends the ~/Desktop directory to the shortcuts model */
1888 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1893 profile_start ("start", NULL);
1895 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1896 file = g_file_new_for_path (name);
1897 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1898 impl->has_desktop = TRUE;
1900 /* We do not actually pop up an error dialog if there is no desktop directory
1901 * because some people may really not want to have one.
1904 g_object_unref (file);
1906 profile_end ("end", NULL);
1909 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1911 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1918 profile_start ("start", NULL);
1920 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1923 for (; bookmarks; bookmarks = bookmarks->next)
1927 file = bookmarks->data;
1929 if (impl->local_only && !g_file_is_native (file))
1932 if (shortcut_find_position (impl, file) != -1)
1935 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1937 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1941 profile_end ("end", NULL);
1943 return num_inserted;
1946 /* Returns the index for the corresponding item in the shortcuts bar */
1948 shortcuts_get_index (GtkFileChooserDefault *impl,
1949 ShortcutsIndex where)
1955 if (where == SHORTCUTS_SEARCH)
1958 n += impl->has_search ? 1 : 0;
1960 if (where == SHORTCUTS_RECENT)
1963 n += impl->has_recent ? 1 : 0;
1965 if (where == SHORTCUTS_RECENT_SEPARATOR)
1968 n += impl->has_recent ? 1 : 0;
1970 if (where == SHORTCUTS_HOME)
1973 n += impl->has_home ? 1 : 0;
1975 if (where == SHORTCUTS_DESKTOP)
1978 n += impl->has_desktop ? 1 : 0;
1980 if (where == SHORTCUTS_VOLUMES)
1983 n += impl->num_volumes;
1985 if (where == SHORTCUTS_SHORTCUTS)
1988 n += impl->num_shortcuts;
1990 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1993 /* If there are no bookmarks there won't be a separator */
1994 n += (impl->num_bookmarks > 0) ? 1 : 0;
1996 if (where == SHORTCUTS_BOOKMARKS)
1999 n += impl->num_bookmarks;
2001 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2006 if (where == SHORTCUTS_CURRENT_FOLDER)
2009 g_assert_not_reached ();
2016 /* Adds all the file system volumes to the shortcuts model */
2018 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2023 gboolean old_changing_folders;
2025 profile_start ("start", NULL);
2027 old_changing_folders = impl->changing_folder;
2028 impl->changing_folder = TRUE;
2030 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2031 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2032 impl->num_volumes = 0;
2034 list = _gtk_file_system_list_volumes (impl->file_system);
2038 for (l = list; l; l = l->next)
2040 GtkFileSystemVolume *volume;
2044 if (impl->local_only)
2046 if (_gtk_file_system_volume_is_mounted (volume))
2050 base_file = _gtk_file_system_volume_get_root (volume);
2051 if (base_file != NULL && !g_file_is_native (base_file))
2056 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2060 impl->num_volumes = n;
2061 g_slist_free (list);
2063 if (impl->shortcuts_pane_filter_model)
2064 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2066 if (impl->shortcuts_combo_filter_model)
2067 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2069 impl->changing_folder = old_changing_folders;
2071 profile_end ("end", NULL);
2074 /* Inserts a separator node in the shortcuts list */
2076 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2077 ShortcutsIndex where)
2081 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2082 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2083 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2085 gtk_list_store_insert (impl->shortcuts_model, &iter,
2086 shortcuts_get_index (impl, where));
2087 gtk_list_store_set (impl->shortcuts_model, &iter,
2088 SHORTCUTS_COL_PIXBUF, NULL,
2089 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2090 SHORTCUTS_COL_NAME, NULL,
2091 SHORTCUTS_COL_DATA, NULL,
2092 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2096 /* Updates the list of bookmarks */
2098 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2101 gboolean old_changing_folders;
2103 GFile *list_selected = NULL;
2104 GFile *combo_selected = NULL;
2105 ShortcutType shortcut_type;
2108 profile_start ("start", NULL);
2110 old_changing_folders = impl->changing_folder;
2111 impl->changing_folder = TRUE;
2113 if (shortcuts_get_selected (impl, &iter))
2115 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2117 SHORTCUTS_COL_DATA, &col_data,
2118 SHORTCUTS_COL_TYPE, &shortcut_type,
2121 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2122 list_selected = g_object_ref (col_data);
2125 if (impl->save_folder_combo &&
2126 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2129 GtkTreeIter child_iter;
2131 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2134 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2136 SHORTCUTS_COL_DATA, &col_data,
2137 SHORTCUTS_COL_TYPE, &shortcut_type,
2140 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2141 combo_selected = g_object_ref (col_data);
2144 if (impl->num_bookmarks > 0)
2145 shortcuts_remove_rows (impl,
2146 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2147 impl->num_bookmarks + 1);
2149 impl->num_bookmarks = 0;
2150 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2152 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2153 shortcuts_append_bookmarks (impl, bookmarks);
2154 g_slist_free (bookmarks);
2156 if (impl->num_bookmarks == 0)
2157 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2159 if (impl->shortcuts_pane_filter_model)
2160 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2162 if (impl->shortcuts_combo_filter_model)
2163 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2167 shortcuts_find_folder (impl, list_selected);
2168 g_object_unref (list_selected);
2175 pos = shortcut_find_position (impl, combo_selected);
2178 if (impl->has_search)
2181 if (impl->has_recent)
2184 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2187 g_object_unref (combo_selected);
2190 impl->changing_folder = old_changing_folders;
2192 profile_end ("end", NULL);
2195 /* Appends a separator and a row to the shortcuts list for the current folder */
2197 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2202 g_assert (!impl->shortcuts_current_folder_active);
2206 g_assert (impl->current_folder != NULL);
2208 pos = shortcut_find_position (impl, impl->current_folder);
2211 GtkFileSystemVolume *volume;
2216 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2220 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2222 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2224 base_file = _gtk_file_system_volume_get_root (volume);
2228 if (base_file && g_file_equal (base_file, impl->current_folder))
2229 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2231 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2234 g_object_unref (base_file);
2236 else if (impl->save_folder_combo != NULL)
2238 if (impl->has_search)
2241 if (impl->has_recent)
2242 pos -= 2; /* + separator */
2244 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2248 /* Updates the current folder row in the shortcuts model */
2250 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2254 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2256 if (impl->shortcuts_current_folder_active)
2258 shortcuts_remove_rows (impl, pos, 2);
2259 impl->shortcuts_current_folder_active = FALSE;
2262 shortcuts_add_current_folder (impl);
2265 /* Filter function used for the shortcuts filter model */
2267 shortcuts_pane_filter_cb (GtkTreeModel *model,
2271 GtkFileChooserDefault *impl;
2275 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2277 path = gtk_tree_model_get_path (model, iter);
2281 pos = *gtk_tree_path_get_indices (path);
2282 gtk_tree_path_free (path);
2284 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2287 /* Creates the list model for shortcuts */
2289 shortcuts_model_create (GtkFileChooserDefault *impl)
2291 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2292 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2293 GDK_TYPE_PIXBUF, /* pixbuf */
2294 G_TYPE_STRING, /* name */
2295 G_TYPE_POINTER, /* path or volume */
2296 G_TYPE_INT, /* ShortcutType */
2297 G_TYPE_BOOLEAN, /* removable */
2298 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2299 G_TYPE_POINTER); /* GCancellable */
2301 if (search_is_possible (impl))
2303 shortcuts_append_search (impl);
2306 if (impl->recent_manager)
2308 shortcuts_append_recent (impl);
2309 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2312 if (impl->file_system)
2314 shortcuts_append_home (impl);
2315 shortcuts_append_desktop (impl);
2316 shortcuts_add_volumes (impl);
2319 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2320 GTK_TREE_MODEL (impl->shortcuts_model),
2323 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2324 shortcuts_pane_filter_cb,
2329 /* Callback used when the "New Folder" button is clicked */
2331 new_folder_button_clicked (GtkButton *button,
2332 GtkFileChooserDefault *impl)
2337 if (!impl->browse_files_model)
2338 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2340 /* Prevent button from being clicked twice */
2341 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2343 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2345 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2346 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2347 path, impl->list_name_column,
2350 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2351 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2353 impl->list_name_column,
2356 gtk_tree_path_free (path);
2359 /* Idle handler for creating a new folder after editing its name cell, or for
2360 * canceling the editing.
2363 edited_idle_cb (GtkFileChooserDefault *impl)
2365 GDK_THREADS_ENTER ();
2367 g_source_destroy (impl->edited_idle);
2368 impl->edited_idle = NULL;
2370 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2371 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2373 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2375 if (impl->edited_new_text) /* not cancelled? */
2377 GError *error = NULL;
2380 file = g_file_get_child_for_display_name (impl->current_folder,
2381 impl->edited_new_text,
2385 GError *error = NULL;
2387 if (!g_file_make_directory (file, NULL, &error))
2388 change_folder_and_display_error (impl, file, FALSE);
2390 error_creating_folder_dialog (impl, file, error);
2392 g_object_unref (file);
2395 error_creating_folder_dialog (impl, file, error);
2397 g_free (impl->edited_new_text);
2398 impl->edited_new_text = NULL;
2401 GDK_THREADS_LEAVE ();
2407 queue_edited_idle (GtkFileChooserDefault *impl,
2408 const gchar *new_text)
2410 /* We create the folder in an idle handler so that we don't modify the tree
2414 if (!impl->edited_idle)
2416 impl->edited_idle = g_idle_source_new ();
2417 g_source_set_closure (impl->edited_idle,
2418 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2420 g_source_attach (impl->edited_idle, NULL);
2423 g_free (impl->edited_new_text);
2424 impl->edited_new_text = g_strdup (new_text);
2427 /* Callback used from the text cell renderer when the new folder is named */
2429 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2431 const gchar *new_text,
2432 GtkFileChooserDefault *impl)
2434 /* work around bug #154921 */
2435 g_object_set (cell_renderer_text,
2436 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2437 queue_edited_idle (impl, new_text);
2440 /* Callback used from the text cell renderer when the new folder edition gets
2444 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2445 GtkFileChooserDefault *impl)
2447 /* work around bug #154921 */
2448 g_object_set (cell_renderer_text,
2449 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2450 queue_edited_idle (impl, NULL);
2453 /* Creates the widgets for the filter combo box */
2455 filter_create (GtkFileChooserDefault *impl)
2457 impl->filter_combo = gtk_combo_box_new_text ();
2458 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2460 g_signal_connect (impl->filter_combo, "changed",
2461 G_CALLBACK (filter_combo_changed), impl);
2463 gtk_widget_set_tooltip_text (impl->filter_combo,
2464 _("Select which types of files are shown"));
2466 return impl->filter_combo;
2470 button_new (GtkFileChooserDefault *impl,
2472 const char *stock_id,
2480 button = gtk_button_new_with_mnemonic (text);
2481 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2482 gtk_button_set_image (GTK_BUTTON (button), image);
2484 gtk_widget_set_sensitive (button, sensitive);
2485 g_signal_connect (button, "clicked", callback, impl);
2488 gtk_widget_show (button);
2493 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2495 shortcut_find_position (GtkFileChooserDefault *impl,
2500 int current_folder_separator_idx;
2502 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2505 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2508 /* FIXME: is this still needed? */
2509 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2513 for (i = 0; i < current_folder_separator_idx; i++)
2516 ShortcutType shortcut_type;
2518 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2519 SHORTCUTS_COL_DATA, &col_data,
2520 SHORTCUTS_COL_TYPE, &shortcut_type,
2525 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2527 GtkFileSystemVolume *volume;
2532 base_file = _gtk_file_system_volume_get_root (volume);
2534 exists = base_file && g_file_equal (file, base_file);
2537 g_object_unref (base_file);
2542 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2546 model_file = col_data;
2548 if (model_file && g_file_equal (model_file, file))
2553 if (i < current_folder_separator_idx - 1)
2555 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2556 g_assert_not_reached ();
2563 /* Tries to add a bookmark from a path name */
2565 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2571 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2573 if (shortcut_find_position (impl, file) != -1)
2577 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2579 error_adding_bookmark_dialog (impl, file, error);
2587 add_bookmark_foreach_cb (GtkTreeModel *model,
2592 GtkFileChooserDefault *impl;
2593 GtkFileSystemModel *fs_model;
2594 GtkTreeIter child_iter;
2597 impl = (GtkFileChooserDefault *) data;
2599 switch (impl->operation_mode)
2601 case OPERATION_MODE_BROWSE:
2602 fs_model = impl->browse_files_model;
2603 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2604 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2607 case OPERATION_MODE_SEARCH:
2608 search_get_valid_child_iter (impl, &child_iter, iter);
2609 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2610 SEARCH_MODEL_COL_FILE, &file,
2614 case OPERATION_MODE_RECENT:
2615 recent_get_valid_child_iter (impl, &child_iter, iter);
2616 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2617 RECENT_MODEL_COL_FILE, &file,
2622 shortcuts_add_bookmark_from_file (impl, file, -1);
2625 /* Adds a bookmark from the currently selected item in the file list */
2627 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2629 GtkTreeSelection *selection;
2631 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2633 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2634 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2636 gtk_tree_selection_selected_foreach (selection,
2637 add_bookmark_foreach_cb,
2641 /* Callback used when the "Add bookmark" button is clicked */
2643 add_bookmark_button_clicked_cb (GtkButton *button,
2644 GtkFileChooserDefault *impl)
2646 bookmarks_add_selected_folder (impl);
2649 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2650 * returns FALSE if no shortcut is selected.
2653 shortcuts_get_selected (GtkFileChooserDefault *impl,
2656 GtkTreeSelection *selection;
2657 GtkTreeIter parent_iter;
2659 if (!impl->browse_shortcuts_tree_view)
2662 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2664 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2667 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2673 /* Removes the selected bookmarks */
2675 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2683 if (!shortcuts_get_selected (impl, &iter))
2686 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2687 SHORTCUTS_COL_DATA, &col_data,
2688 SHORTCUTS_COL_REMOVABLE, &removable,
2694 g_assert (col_data != NULL);
2699 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2700 error_removing_bookmark_dialog (impl, file, error);
2703 /* Callback used when the "Remove bookmark" button is clicked */
2705 remove_bookmark_button_clicked_cb (GtkButton *button,
2706 GtkFileChooserDefault *impl)
2708 remove_selected_bookmarks (impl);
2711 struct selection_check_closure {
2712 GtkFileChooserDefault *impl;
2715 gboolean all_folders;
2718 /* Used from gtk_tree_selection_selected_foreach() */
2720 selection_check_foreach_cb (GtkTreeModel *model,
2725 struct selection_check_closure *closure;
2726 GtkTreeIter child_iter;
2731 closure->num_selected++;
2733 switch (closure->impl->operation_mode)
2735 case OPERATION_MODE_BROWSE:
2736 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2737 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2738 is_folder = info ? (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) : FALSE;
2741 case OPERATION_MODE_SEARCH:
2742 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2743 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2744 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2748 case OPERATION_MODE_RECENT:
2749 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2750 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2751 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2756 closure->all_folders = closure->all_folders && is_folder;
2757 closure->all_files = closure->all_files && !is_folder;
2760 /* Checks whether the selected items in the file list are all files or all folders */
2762 selection_check (GtkFileChooserDefault *impl,
2764 gboolean *all_files,
2765 gboolean *all_folders)
2767 struct selection_check_closure closure;
2768 GtkTreeSelection *selection;
2770 closure.impl = impl;
2771 closure.num_selected = 0;
2772 closure.all_files = TRUE;
2773 closure.all_folders = TRUE;
2775 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2776 gtk_tree_selection_selected_foreach (selection,
2777 selection_check_foreach_cb,
2780 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2783 *num_selected = closure.num_selected;
2786 *all_files = closure.all_files;
2789 *all_folders = closure.all_folders;
2792 struct get_selected_file_closure {
2793 GtkFileChooserDefault *impl;
2798 get_selected_file_foreach_cb (GtkTreeModel *model,
2803 struct get_selected_file_closure *closure;
2804 GtkTreeIter child_iter;
2808 switch (closure->impl->operation_mode)
2810 case OPERATION_MODE_BROWSE:
2811 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2812 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2815 case OPERATION_MODE_SEARCH:
2816 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2817 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2818 SEARCH_MODEL_COL_FILE, &closure->file,
2822 case OPERATION_MODE_RECENT:
2823 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2824 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2825 RECENT_MODEL_COL_FILE, &closure->file,
2831 /* Returns a selected path from the file list */
2833 get_selected_file (GtkFileChooserDefault *impl)
2835 struct get_selected_file_closure closure;
2836 GtkTreeSelection *selection;
2838 closure.impl = impl;
2839 closure.file = NULL;
2841 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2842 gtk_tree_selection_selected_foreach (selection,
2843 get_selected_file_foreach_cb,
2846 return closure.file;
2850 GtkFileChooserDefault *impl;
2852 } UpdateTooltipData;
2855 update_tooltip (GtkTreeModel *model,
2860 UpdateTooltipData *udata = data;
2861 GtkTreeIter child_iter;
2864 if (udata->tip == NULL)
2866 const gchar *display_name;
2868 switch (udata->impl->operation_mode)
2870 case OPERATION_MODE_BROWSE:
2871 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2874 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2875 display_name = g_file_info_get_display_name (info);
2878 case OPERATION_MODE_SEARCH:
2879 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2880 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2881 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2885 case OPERATION_MODE_RECENT:
2886 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2887 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2888 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2893 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2899 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2900 * if there are no selected items *and* the current folder is not in the
2901 * bookmarks list. De-sensitize the button otherwise.
2904 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2907 gboolean all_folders;
2911 selection_check (impl, &num_selected, NULL, &all_folders);
2913 if (num_selected == 0)
2914 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2915 else if (num_selected == 1)
2919 file = get_selected_file (impl);
2920 active = all_folders && (shortcut_find_position (impl, file) == -1);
2923 active = all_folders;
2925 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2927 if (impl->browse_files_popup_menu_add_shortcut_item)
2928 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2929 (num_selected == 0) ? FALSE : active);
2933 if (num_selected == 0)
2934 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2935 else if (num_selected > 1)
2936 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2939 GtkTreeSelection *selection;
2940 UpdateTooltipData data;
2942 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2945 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2949 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2954 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2955 * bookmark row is selected in the shortcuts tree.
2958 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2961 gboolean removable = FALSE;
2964 if (shortcuts_get_selected (impl, &iter))
2965 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2966 SHORTCUTS_COL_REMOVABLE, &removable,
2967 SHORTCUTS_COL_NAME, &name,
2970 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2976 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2977 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2985 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2988 gboolean removable = FALSE;
2990 if (impl->browse_shortcuts_popup_menu == NULL)
2993 if (shortcuts_get_selected (impl, &iter))
2994 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2995 SHORTCUTS_COL_REMOVABLE, &removable,
2998 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2999 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3002 /* GtkWidget::drag-begin handler for the shortcuts list. */
3004 shortcuts_drag_begin_cb (GtkWidget *widget,
3005 GdkDragContext *context,
3006 GtkFileChooserDefault *impl)
3009 impl->shortcuts_drag_context = g_object_ref (context);
3014 /* Removes the idle handler for outside drags */
3016 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3018 if (!impl->shortcuts_drag_outside_idle)
3021 g_source_destroy (impl->shortcuts_drag_outside_idle);
3022 impl->shortcuts_drag_outside_idle = NULL;
3026 /* GtkWidget::drag-end handler for the shortcuts list. */
3028 shortcuts_drag_end_cb (GtkWidget *widget,
3029 GdkDragContext *context,
3030 GtkFileChooserDefault *impl)
3033 g_object_unref (impl->shortcuts_drag_context);
3035 shortcuts_cancel_drag_outside_idle (impl);
3037 if (!impl->shortcuts_drag_outside)
3040 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3042 impl->shortcuts_drag_outside = FALSE;
3046 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3048 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3049 GdkDragContext *context,
3050 GtkFileChooserDefault *impl)
3052 g_signal_stop_emission_by_name (widget, "drag_data_delete");
3056 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3060 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3063 GtkTreeView *tree_view;
3066 GdkPixmap *row_pixmap;
3071 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3073 /* Find the selected path and get its drag pixmap */
3075 if (!shortcuts_get_selected (impl, &iter))
3076 g_assert_not_reached ();
3078 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3080 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3081 gtk_tree_path_free (path);
3090 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3096 GdkPixmap *composite;
3097 int row_pixmap_width, row_pixmap_height;
3098 int pixbuf_width, pixbuf_height;
3099 int composite_width, composite_height;
3100 int pixbuf_x, pixbuf_y;
3101 GdkGC *gc, *mask_gc;
3103 GdkBitmap *pixbuf_mask;
3105 /* Create pixmap and mask for composite image */
3107 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3108 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3109 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3111 composite_width = MAX (row_pixmap_width, pixbuf_width);
3112 composite_height = MAX (row_pixmap_height, pixbuf_height);
3114 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3116 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3119 pixbuf_x = composite_width - pixbuf_width;
3121 pixbuf_y = (composite_height - pixbuf_height) / 2;
3123 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3124 gc = gdk_gc_new (composite);
3126 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3127 mask_gc = gdk_gc_new (mask);
3129 gdk_gc_set_foreground (mask_gc, &color);
3130 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3133 color.green = 0xffff;
3134 color.blue = 0xffff;
3135 gdk_gc_set_rgb_fg_color (gc, &color);
3136 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3138 /* Composite the row pixmap and the pixbuf */
3140 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3142 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3143 NULL, &pixbuf_mask, 128);
3144 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3147 pixbuf_width, pixbuf_height);
3148 g_object_unref (pixbuf_mask);
3150 gdk_draw_drawable (composite, gc, row_pixmap,
3153 row_pixmap_width, row_pixmap_height);
3155 gdk_gc_set_foreground (mask_gc, &color);
3156 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3158 gdk_draw_pixbuf (composite, gc, pixbuf,
3161 pixbuf_width, pixbuf_height,
3165 g_object_unref (pixbuf);
3166 g_object_unref (row_pixmap);
3168 row_pixmap = composite;
3172 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3174 gtk_tree_view_get_path_at_pos (tree_view,
3175 tree_view->priv->press_start_x,
3176 tree_view->priv->press_start_y,
3182 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3183 gdk_drawable_get_colormap (row_pixmap),
3186 tree_view->priv->press_start_x + 1,
3187 row_pixmap_y + cell_y + 1);
3189 g_object_unref (row_pixmap);
3191 g_object_unref (mask);
3194 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3195 * handler so that we can tell apart the drag_leave event that comes right
3196 * before a drag_drop, from a normal drag_leave. We don't want to set the
3197 * cursor nor the flag in the latter case.
3200 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3202 GDK_THREADS_ENTER ();
3204 shortcuts_drag_set_delete_cursor (impl, TRUE);
3205 impl->shortcuts_drag_outside = TRUE;
3207 shortcuts_cancel_drag_outside_idle (impl);
3209 GDK_THREADS_LEAVE ();
3215 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3219 shortcuts_drag_leave_cb (GtkWidget *widget,
3220 GdkDragContext *context,
3222 GtkFileChooserDefault *impl)
3225 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3227 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3228 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3229 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3231 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3235 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3237 GTK_TREE_VIEW_DROP_BEFORE);
3239 g_signal_stop_emission_by_name (widget, "drag_leave");
3242 /* Computes the appropriate row and position for dropping */
3244 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3248 GtkTreeViewDropPosition *pos)
3250 GtkTreeView *tree_view;
3251 GtkTreeViewColumn *column;
3255 int bookmarks_index;
3257 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3259 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3261 if (!gtk_tree_view_get_path_at_pos (tree_view,
3263 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3269 row = bookmarks_index + impl->num_bookmarks - 1;
3270 *path = gtk_tree_path_new_from_indices (row, -1);
3271 *pos = GTK_TREE_VIEW_DROP_AFTER;
3275 row = *gtk_tree_path_get_indices (*path);
3276 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3277 gtk_tree_path_free (*path);
3279 if (row < bookmarks_index)
3281 row = bookmarks_index;
3282 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3284 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3286 row = bookmarks_index + impl->num_bookmarks - 1;
3287 *pos = GTK_TREE_VIEW_DROP_AFTER;
3291 if (cell_y < cell.height / 2)
3292 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3294 *pos = GTK_TREE_VIEW_DROP_AFTER;
3297 *path = gtk_tree_path_new_from_indices (row, -1);
3300 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3301 * implement the destination side of DnD by hand, due to limitations in
3302 * GtkTreeView's DnD API.
3305 shortcuts_drag_motion_cb (GtkWidget *widget,
3306 GdkDragContext *context,
3310 GtkFileChooserDefault *impl)
3313 GtkTreeViewDropPosition pos;
3314 GdkDragAction action;
3317 if (gtk_drag_get_source_widget (context) == widget)
3319 shortcuts_cancel_drag_outside_idle (impl);
3321 if (impl->shortcuts_drag_outside)
3323 shortcuts_drag_set_delete_cursor (impl, FALSE);
3324 impl->shortcuts_drag_outside = FALSE;
3329 if (context->suggested_action == GDK_ACTION_COPY ||
3330 (context->actions & GDK_ACTION_COPY) != 0)
3331 action = GDK_ACTION_COPY;
3332 else if (context->suggested_action == GDK_ACTION_MOVE ||
3333 (context->actions & GDK_ACTION_MOVE) != 0)
3334 action = GDK_ACTION_MOVE;
3341 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3342 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3343 gtk_tree_path_free (path);
3347 g_signal_stop_emission_by_name (widget, "drag_motion");
3351 gdk_drag_status (context, action, time_);
3358 /* GtkWidget::drag-drop handler for the shortcuts list. */
3360 shortcuts_drag_drop_cb (GtkWidget *widget,
3361 GdkDragContext *context,
3365 GtkFileChooserDefault *impl)
3368 shortcuts_cancel_drag_outside_idle (impl);
3371 g_signal_stop_emission_by_name (widget, "drag_drop");
3375 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3377 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3378 GtkSelectionData *selection_data,
3384 uris = gtk_selection_data_get_uris (selection_data);
3388 for (i = 0; uris[i]; i++)
3394 file = g_file_new_for_uri (uri);
3396 if (shortcuts_add_bookmark_from_file (impl, file, position))
3399 g_object_unref (file);
3405 /* Reorders the selected bookmark to the specified position */
3407 shortcuts_reorder (GtkFileChooserDefault *impl,
3412 ShortcutType shortcut_type;
3415 int bookmarks_index;
3420 /* Get the selected path */
3422 if (!shortcuts_get_selected (impl, &iter))
3423 g_assert_not_reached ();
3425 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3426 old_position = *gtk_tree_path_get_indices (path);
3427 gtk_tree_path_free (path);
3429 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3430 old_position -= bookmarks_index;
3431 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3433 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3434 SHORTCUTS_COL_NAME, &name,
3435 SHORTCUTS_COL_DATA, &col_data,
3436 SHORTCUTS_COL_TYPE, &shortcut_type,
3438 g_assert (col_data != NULL);
3439 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3442 g_object_ref (file); /* removal below will free file, so we need a new ref */
3444 /* Remove the path from the old position and insert it in the new one */
3446 if (new_position > old_position)
3449 if (old_position == new_position)
3453 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3455 shortcuts_add_bookmark_from_file (impl, file, new_position);
3456 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3459 error_adding_bookmark_dialog (impl, file, error);
3463 g_object_unref (file);
3466 /* Callback used when we get the drag data for the bookmarks list. We add the
3467 * received URIs as bookmarks if they are folders.
3470 shortcuts_drag_data_received_cb (GtkWidget *widget,
3471 GdkDragContext *context,
3474 GtkSelectionData *selection_data,
3479 GtkFileChooserDefault *impl;
3480 GtkTreePath *tree_path;
3481 GtkTreeViewDropPosition tree_pos;
3483 int bookmarks_index;
3485 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3487 /* Compute position */
3489 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3491 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3492 position = *gtk_tree_path_get_indices (tree_path);
3493 gtk_tree_path_free (tree_path);
3495 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3498 g_assert (position >= bookmarks_index);
3499 position -= bookmarks_index;
3501 if (gtk_targets_include_uri (&selection_data->target, 1))
3502 shortcuts_drop_uris (impl, selection_data, position);
3503 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3504 shortcuts_reorder (impl, position);
3506 g_signal_stop_emission_by_name (widget, "drag_data_received");
3509 /* Callback used to display a tooltip in the shortcuts tree */
3511 shortcuts_query_tooltip_cb (GtkWidget *widget,
3514 gboolean keyboard_mode,
3515 GtkTooltip *tooltip,
3516 GtkFileChooserDefault *impl)
3518 GtkTreeModel *model;
3521 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3529 ShortcutType shortcut_type;
3531 gtk_tree_model_get (model, &iter,
3532 SHORTCUTS_COL_DATA, &col_data,
3533 SHORTCUTS_COL_TYPE, &shortcut_type,
3536 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3538 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3542 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3547 file = G_FILE (col_data);
3548 parse_name = g_file_get_parse_name (file);
3550 gtk_tooltip_set_text (tooltip, parse_name);
3552 g_free (parse_name);
3556 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3560 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3570 /* Callback used when the selection in the shortcuts tree changes */
3572 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3573 GtkFileChooserDefault *impl)
3576 GtkTreeIter child_iter;
3578 bookmarks_check_remove_sensitivity (impl);
3579 shortcuts_check_popup_sensitivity (impl);
3581 if (impl->changing_folder)
3584 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3586 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3589 shortcuts_activate_iter (impl, &child_iter);
3594 shortcuts_row_separator_func (GtkTreeModel *model,
3598 ShortcutType shortcut_type;
3600 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3602 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3605 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3606 * keypresses before the TreeView gets them.
3609 tree_view_keybinding_cb (GtkWidget *tree_view,
3611 GtkFileChooserDefault *impl)
3613 if ((event->keyval == GDK_slash
3614 || event->keyval == GDK_KP_Divide
3616 || event->keyval == GDK_asciitilde
3618 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3620 location_popup_handler (impl, event->string);
3627 /* Callback used when the file list's popup menu is detached */
3629 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3632 GtkFileChooserDefault *impl;
3634 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3635 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3637 impl->browse_shortcuts_popup_menu = NULL;
3638 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3639 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3643 remove_shortcut_cb (GtkMenuItem *item,
3644 GtkFileChooserDefault *impl)
3646 remove_selected_bookmarks (impl);
3649 /* Rename the selected bookmark */
3651 rename_selected_bookmark (GtkFileChooserDefault *impl)
3655 GtkTreeViewColumn *column;
3656 GtkCellRenderer *cell;
3659 if (shortcuts_get_selected (impl, &iter))
3661 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3662 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3663 renderers = gtk_tree_view_column_get_cell_renderers (column);
3664 cell = g_list_nth_data (renderers, 1);
3665 g_list_free (renderers);
3666 g_object_set (cell, "editable", TRUE, NULL);
3667 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3668 path, column, cell, TRUE);
3669 gtk_tree_path_free (path);
3674 rename_shortcut_cb (GtkMenuItem *item,
3675 GtkFileChooserDefault *impl)
3677 rename_selected_bookmark (impl);
3680 /* Constructs the popup menu for the file list if needed */
3682 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3686 if (impl->browse_shortcuts_popup_menu)
3689 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3690 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3691 impl->browse_shortcuts_tree_view,
3692 shortcuts_popup_menu_detach_cb);
3694 item = gtk_image_menu_item_new_with_label (_("Remove"));
3695 impl->browse_shortcuts_popup_menu_remove_item = item;
3696 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3697 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3698 g_signal_connect (item, "activate",
3699 G_CALLBACK (remove_shortcut_cb), impl);
3700 gtk_widget_show (item);
3701 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3703 item = gtk_menu_item_new_with_label (_("Rename..."));
3704 impl->browse_shortcuts_popup_menu_rename_item = item;
3705 g_signal_connect (item, "activate",
3706 G_CALLBACK (rename_shortcut_cb), impl);
3707 gtk_widget_show (item);
3708 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3710 shortcuts_check_popup_sensitivity (impl);
3714 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3716 shortcuts_build_popup_menu (impl);
3720 popup_position_func (GtkMenu *menu,
3724 gpointer user_data);
3727 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3728 GdkEventButton *event)
3730 shortcuts_update_popup_menu (impl);
3732 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3733 NULL, NULL, NULL, NULL,
3734 event->button, event->time);
3737 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3739 popup_position_func, impl->browse_shortcuts_tree_view,
3740 0, GDK_CURRENT_TIME);
3741 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3746 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3748 shortcuts_popup_menu_cb (GtkWidget *widget,
3749 GtkFileChooserDefault *impl)
3751 shortcuts_popup_menu (impl, NULL);
3755 /* Callback used when a button is pressed on the shortcuts list.
3756 * We trap button 3 to bring up a popup menu.
3759 shortcuts_button_press_event_cb (GtkWidget *widget,
3760 GdkEventButton *event,
3761 GtkFileChooserDefault *impl)
3763 static gboolean in_press = FALSE;
3769 if (event->button != 3)
3773 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3779 shortcuts_popup_menu (impl, event);
3784 shortcuts_edited (GtkCellRenderer *cell,
3787 GtkFileChooserDefault *impl)
3793 g_object_set (cell, "editable", FALSE, NULL);
3795 path = gtk_tree_path_new_from_string (path_string);
3796 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3797 g_assert_not_reached ();
3799 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3800 SHORTCUTS_COL_DATA, &shortcut,
3802 gtk_tree_path_free (path);
3804 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3808 shortcuts_editing_canceled (GtkCellRenderer *cell,
3809 GtkFileChooserDefault *impl)
3811 g_object_set (cell, "editable", FALSE, NULL);
3814 /* Creates the widgets for the shortcuts and bookmarks tree */
3816 shortcuts_list_create (GtkFileChooserDefault *impl)
3819 GtkTreeSelection *selection;
3820 GtkTreeViewColumn *column;
3821 GtkCellRenderer *renderer;
3823 /* Target types for dragging a row to/from the shortcuts list */
3824 const GtkTargetEntry tree_model_row_targets[] = {
3825 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3828 /* Scrolled window */
3830 swin = gtk_scrolled_window_new (NULL, NULL);
3831 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3832 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3833 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3835 gtk_widget_show (swin);
3839 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3840 #ifdef PROFILE_FILE_CHOOSER
3841 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3843 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3844 G_CALLBACK (tree_view_keybinding_cb), impl);
3845 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3846 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3847 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3848 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3849 /* Accessible object name for the file chooser's shortcuts pane */
3850 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3852 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3854 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3856 tree_model_row_targets,
3857 G_N_ELEMENTS (tree_model_row_targets),
3860 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3861 GTK_DEST_DEFAULT_ALL,
3862 tree_model_row_targets,
3863 G_N_ELEMENTS (tree_model_row_targets),
3864 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3865 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3867 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3868 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3869 gtk_tree_selection_set_select_function (selection,
3870 shortcuts_select_func,
3873 g_signal_connect (selection, "changed",
3874 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3876 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3877 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3879 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3880 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3881 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3882 G_CALLBACK (shortcuts_drag_end_cb), impl);
3883 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3884 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3886 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3887 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3888 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3889 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3890 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3891 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3892 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3893 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3895 /* Support tooltips */
3896 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3897 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3898 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3900 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3901 gtk_widget_show (impl->browse_shortcuts_tree_view);
3905 column = gtk_tree_view_column_new ();
3906 /* Column header for the file chooser's shortcuts pane */
3907 gtk_tree_view_column_set_title (column, _("_Places"));
3909 renderer = gtk_cell_renderer_pixbuf_new ();
3910 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3911 gtk_tree_view_column_set_attributes (column, renderer,
3912 "pixbuf", SHORTCUTS_COL_PIXBUF,
3913 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3916 renderer = gtk_cell_renderer_text_new ();
3917 g_signal_connect (renderer, "edited",
3918 G_CALLBACK (shortcuts_edited), impl);
3919 g_signal_connect (renderer, "editing-canceled",
3920 G_CALLBACK (shortcuts_editing_canceled), impl);
3921 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3922 gtk_tree_view_column_set_attributes (column, renderer,
3923 "text", SHORTCUTS_COL_NAME,
3926 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3927 shortcuts_row_separator_func,
3930 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3935 /* Creates the widgets for the shortcuts/bookmarks pane */
3937 shortcuts_pane_create (GtkFileChooserDefault *impl,
3938 GtkSizeGroup *size_group)
3944 vbox = gtk_vbox_new (FALSE, 6);
3945 gtk_widget_show (vbox);
3947 /* Shortcuts tree */
3949 widget = shortcuts_list_create (impl);
3950 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3952 /* Box for buttons */
3954 hbox = gtk_hbox_new (TRUE, 6);
3955 gtk_size_group_add_widget (size_group, hbox);
3956 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3957 gtk_widget_show (hbox);
3959 /* Add bookmark button */
3961 impl->browse_shortcuts_add_button = button_new (impl,
3966 G_CALLBACK (add_bookmark_button_clicked_cb));
3967 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3968 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3969 _("Add the selected folder to the Bookmarks"));
3971 /* Remove bookmark button */
3973 impl->browse_shortcuts_remove_button = button_new (impl,
3978 G_CALLBACK (remove_bookmark_button_clicked_cb));
3979 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3980 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3981 _("Remove the selected bookmark"));
3986 /* Handles key press events on the file list, so that we can trap Enter to
3987 * activate the default button on our own. Also, checks to see if '/' has been
3988 * pressed. See comment by tree_view_keybinding_cb() for more details.
3991 trap_activate_cb (GtkWidget *widget,
3995 GtkFileChooserDefault *impl;
3998 impl = (GtkFileChooserDefault *) data;
4000 modifiers = gtk_accelerator_get_default_mod_mask ();
4002 if ((event->keyval == GDK_slash
4003 || event->keyval == GDK_KP_Divide
4005 || event->keyval == GDK_asciitilde
4007 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4009 location_popup_handler (impl, event->string);
4013 if ((event->keyval == GDK_Return
4014 || event->keyval == GDK_ISO_Enter
4015 || event->keyval == GDK_KP_Enter
4016 || event->keyval == GDK_space
4017 || event->keyval == GDK_KP_Space)
4018 && ((event->state & modifiers) == 0)
4019 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4020 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4024 window = get_toplevel (widget);
4026 && widget != window->default_widget
4027 && !(widget == window->focus_widget &&
4028 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4030 gtk_window_activate_default (window);
4038 /* Callback used when the file list's popup menu is detached */
4040 popup_menu_detach_cb (GtkWidget *attach_widget,
4043 GtkFileChooserDefault *impl;
4045 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4046 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4048 impl->browse_files_popup_menu = NULL;
4049 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4050 impl->browse_files_popup_menu_hidden_files_item = NULL;
4053 /* Callback used when the "Add to Bookmarks" menu item is activated */
4055 add_to_shortcuts_cb (GtkMenuItem *item,
4056 GtkFileChooserDefault *impl)
4058 bookmarks_add_selected_folder (impl);
4061 /* Callback used when the "Show Hidden Files" menu item is toggled */
4063 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4064 GtkFileChooserDefault *impl)
4067 "show-hidden", gtk_check_menu_item_get_active (item),
4071 /* Shows an error dialog about not being able to select a dragged file */
4073 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4078 _("Could not select file"),
4083 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4088 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4090 for (i = 1; uris[i]; i++)
4093 GError *error = NULL;
4096 file = g_file_new_for_uri (uri);
4098 gtk_file_chooser_default_select_file (chooser, file, &error);
4100 error_selecting_dragged_file_dialog (impl, file, error);
4102 g_object_unref (file);
4106 struct FileListDragData
4108 GtkFileChooserDefault *impl;
4114 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4116 const GError *error,
4119 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4120 struct FileListDragData *data = user_data;
4121 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4123 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4126 data->impl->file_list_drag_data_received_cancellable = NULL;
4128 if (cancelled || error)
4131 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4132 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4133 data->uris[1] == 0 && !error &&
4134 g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
4135 change_folder_and_display_error (data->impl, data->file, FALSE);
4138 GError *error = NULL;
4140 gtk_file_chooser_default_unselect_all (chooser);
4141 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4143 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4145 browse_files_center_selected_row (data->impl);
4148 if (data->impl->select_multiple)
4149 file_list_drag_data_select_uris (data->impl, data->uris);
4152 g_object_unref (data->impl);
4153 g_strfreev (data->uris);
4154 g_object_unref (data->file);
4157 g_object_unref (cancellable);
4161 file_list_drag_data_received_cb (GtkWidget *widget,
4162 GdkDragContext *context,
4165 GtkSelectionData *selection_data,
4170 GtkFileChooserDefault *impl;
4171 GtkFileChooser *chooser;
4176 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4177 chooser = GTK_FILE_CHOOSER (data);
4179 /* Allow only drags from other widgets; see bug #533891. */
4180 if (gtk_drag_get_source_widget (context) == widget)
4183 /* Parse the text/uri-list string, navigate to the first one */
4184 uris = gtk_selection_data_get_uris (selection_data);
4185 if (uris && uris[0])
4187 struct FileListDragData *data;
4190 file = g_file_new_for_uri (uri);
4192 data = g_new0 (struct FileListDragData, 1);
4193 data->impl = g_object_ref (impl);
4197 if (impl->file_list_drag_data_received_cancellable)
4198 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4200 impl->file_list_drag_data_received_cancellable =
4201 _gtk_file_system_get_info (impl->file_system, file,
4203 file_list_drag_data_received_get_info_cb,
4207 g_signal_stop_emission_by_name (widget, "drag_data_received");
4210 /* Don't do anything with the drag_drop signal */
4212 file_list_drag_drop_cb (GtkWidget *widget,
4213 GdkDragContext *context,
4217 GtkFileChooserDefault *impl)
4219 g_signal_stop_emission_by_name (widget, "drag_drop");
4223 /* Disable the normal tree drag motion handler, it makes it look like you're
4224 dropping the dragged item onto a tree item */
4226 file_list_drag_motion_cb (GtkWidget *widget,
4227 GdkDragContext *context,
4231 GtkFileChooserDefault *impl)
4233 g_signal_stop_emission_by_name (widget, "drag_motion");
4237 /* Constructs the popup menu for the file list if needed */
4239 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4243 if (impl->browse_files_popup_menu)
4246 impl->browse_files_popup_menu = gtk_menu_new ();
4247 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4248 impl->browse_files_tree_view,
4249 popup_menu_detach_cb);
4251 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4252 impl->browse_files_popup_menu_add_shortcut_item = item;
4253 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4254 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4255 gtk_widget_set_sensitive (item, FALSE);
4256 g_signal_connect (item, "activate",
4257 G_CALLBACK (add_to_shortcuts_cb), impl);
4258 gtk_widget_show (item);
4259 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4261 item = gtk_separator_menu_item_new ();
4262 gtk_widget_show (item);
4263 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4265 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4266 impl->browse_files_popup_menu_hidden_files_item = item;
4267 g_signal_connect (item, "toggled",
4268 G_CALLBACK (show_hidden_toggled_cb), impl);
4269 gtk_widget_show (item);
4270 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4273 /* Updates the popup menu for the file list, creating it if necessary */
4275 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4277 file_list_build_popup_menu (impl);
4279 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4281 /* The sensitivity of the Add to Bookmarks item is set in
4282 * bookmarks_check_add_sensitivity()
4285 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4286 G_CALLBACK (show_hidden_toggled_cb), impl);
4287 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4289 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4290 G_CALLBACK (show_hidden_toggled_cb), impl);
4294 popup_position_func (GtkMenu *menu,
4300 GtkWidget *widget = GTK_WIDGET (user_data);
4301 GdkScreen *screen = gtk_widget_get_screen (widget);
4304 GdkRectangle monitor;
4306 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4308 gdk_window_get_origin (widget->window, x, y);
4310 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4312 *x += (widget->allocation.width - req.width) / 2;
4313 *y += (widget->allocation.height - req.height) / 2;
4315 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4316 gtk_menu_set_monitor (menu, monitor_num);
4317 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4319 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4320 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4326 file_list_popup_menu (GtkFileChooserDefault *impl,
4327 GdkEventButton *event)
4329 file_list_update_popup_menu (impl);
4331 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4332 NULL, NULL, NULL, NULL,
4333 event->button, event->time);
4336 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4338 popup_position_func, impl->browse_files_tree_view,
4339 0, GDK_CURRENT_TIME);
4340 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4346 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4348 list_popup_menu_cb (GtkWidget *widget,
4349 GtkFileChooserDefault *impl)
4351 file_list_popup_menu (impl, NULL);
4355 /* Callback used when a button is pressed on the file list. We trap button 3 to
4356 * bring up a popup menu.
4359 list_button_press_event_cb (GtkWidget *widget,
4360 GdkEventButton *event,
4361 GtkFileChooserDefault *impl)
4363 static gboolean in_press = FALSE;
4369 if (event->button != 3)
4373 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4376 file_list_popup_menu (impl, event);
4380 /* Sets the sort column IDs for the file list based on the operation mode */
4382 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4384 int name_id, mtime_id;
4386 name_id = mtime_id = 0;
4388 switch (impl->operation_mode)
4390 case OPERATION_MODE_BROWSE:
4391 name_id = FILE_LIST_COL_NAME;
4392 mtime_id = FILE_LIST_COL_MTIME;
4394 case OPERATION_MODE_SEARCH:
4395 name_id = SEARCH_MODEL_COL_FILE;
4396 mtime_id = SEARCH_MODEL_COL_STAT;
4398 case OPERATION_MODE_RECENT:
4399 name_id = RECENT_MODEL_COL_FILE;
4400 mtime_id = RECENT_MODEL_COL_INFO;
4404 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4405 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4409 file_list_query_tooltip_cb (GtkWidget *widget,
4412 gboolean keyboard_tip,
4413 GtkTooltip *tooltip,
4416 GtkFileChooserDefault *impl = user_data;
4417 GtkTreeIter iter, child_iter;
4418 GtkTreePath *path = NULL;
4422 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4426 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4434 switch (impl->operation_mode)
4436 case OPERATION_MODE_SEARCH:
4437 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4439 gtk_tree_path_free (path);
4443 search_get_valid_child_iter (impl, &child_iter, &iter);
4444 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4445 SEARCH_MODEL_COL_FILE, &file,
4449 case OPERATION_MODE_RECENT:
4450 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4452 gtk_tree_path_free (path);
4456 recent_get_valid_child_iter (impl, &child_iter, &iter);
4457 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4458 RECENT_MODEL_COL_FILE, &file,
4462 case OPERATION_MODE_BROWSE:
4463 g_assert_not_reached ();
4469 gtk_tree_path_free (path);
4473 filename = g_file_get_path (file);
4474 gtk_tooltip_set_text (tooltip, filename);
4475 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4480 gtk_tree_path_free (path);
4485 /* Creates the widgets for the file list */
4487 create_file_list (GtkFileChooserDefault *impl)
4490 GtkTreeSelection *selection;
4491 GtkTreeViewColumn *column;
4492 GtkCellRenderer *renderer;
4494 /* Scrolled window */
4495 swin = gtk_scrolled_window_new (NULL, NULL);
4496 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4497 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4498 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4501 /* Tree/list view */
4503 impl->browse_files_tree_view = gtk_tree_view_new ();
4504 #ifdef PROFILE_FILE_CHOOSER
4505 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4507 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4508 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4510 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4511 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4513 gtk_drag_dest_set (impl->browse_files_tree_view,
4514 GTK_DEST_DEFAULT_ALL,
4516 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4517 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4519 g_signal_connect (impl->browse_files_tree_view, "row_activated",
4520 G_CALLBACK (list_row_activated), impl);
4521 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4522 G_CALLBACK (trap_activate_cb), impl);
4523 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4524 G_CALLBACK (list_popup_menu_cb), impl);
4525 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4526 G_CALLBACK (list_button_press_event_cb), impl);
4528 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4529 G_CALLBACK (file_list_drag_data_received_cb), impl);
4530 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4531 G_CALLBACK (file_list_drag_drop_cb), impl);
4532 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4533 G_CALLBACK (file_list_drag_motion_cb), impl);
4535 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4536 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4537 G_CALLBACK (file_list_query_tooltip_cb), impl);
4539 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4540 gtk_tree_selection_set_select_function (selection,
4543 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4546 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4547 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4549 g_signal_connect (selection, "changed",
4550 G_CALLBACK (list_selection_changed), impl);
4552 /* Filename column */
4554 impl->list_name_column = gtk_tree_view_column_new ();
4555 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4556 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4557 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4558 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4560 renderer = gtk_cell_renderer_pixbuf_new ();
4561 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4562 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4563 list_icon_data_func, impl, NULL);
4565 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4566 g_object_set (impl->list_name_renderer,
4567 "ellipsize", PANGO_ELLIPSIZE_END,
4569 g_signal_connect (impl->list_name_renderer, "edited",
4570 G_CALLBACK (renderer_edited_cb), impl);
4571 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4572 G_CALLBACK (renderer_editing_canceled_cb), impl);
4573 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4574 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4575 list_name_data_func, impl, NULL);
4577 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4581 column = gtk_tree_view_column_new ();
4582 gtk_tree_view_column_set_title (column, _("Size"));
4584 renderer = gtk_cell_renderer_text_new ();
4585 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4586 gtk_tree_view_column_set_cell_data_func (column, renderer,
4587 list_size_data_func, impl, NULL);
4588 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4589 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4592 /* Modification time column */
4594 column = gtk_tree_view_column_new ();
4595 gtk_tree_view_column_set_resizable (column, TRUE);
4596 gtk_tree_view_column_set_title (column, _("Modified"));
4598 renderer = gtk_cell_renderer_text_new ();
4599 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4600 gtk_tree_view_column_set_cell_data_func (column, renderer,
4601 list_mtime_data_func, impl, NULL);
4602 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4603 impl->list_mtime_column = column;
4605 file_list_set_sort_column_ids (impl);
4607 gtk_widget_show_all (swin);
4613 create_path_bar (GtkFileChooserDefault *impl)
4615 GtkWidget *path_bar;
4617 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4618 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4623 /* Creates the widgets for the files/folders pane */
4625 file_pane_create (GtkFileChooserDefault *impl,
4626 GtkSizeGroup *size_group)
4632 vbox = gtk_vbox_new (FALSE, 6);
4633 gtk_widget_show (vbox);
4635 /* Box for lists and preview */
4637 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4638 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4639 gtk_widget_show (hbox);
4643 widget = create_file_list (impl);
4644 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4648 impl->preview_box = gtk_vbox_new (FALSE, 12);
4649 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4650 /* Don't show preview box initially */
4654 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4656 widget = filter_create (impl);
4658 gtk_widget_show (widget);
4659 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4661 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4662 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4667 /* Callback used when the "Browse for more folders" expander is toggled */
4669 expander_changed_cb (GtkExpander *expander,
4671 GtkFileChooserDefault *impl)
4673 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4674 update_appearance (impl);
4677 /* Callback used when the selection changes in the save folder combo box */
4679 save_folder_combo_changed_cb (GtkComboBox *combo,
4680 GtkFileChooserDefault *impl)
4684 if (impl->changing_folder)
4687 if (gtk_combo_box_get_active_iter (combo, &iter))
4689 GtkTreeIter child_iter;
4691 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4694 shortcuts_activate_iter (impl, &child_iter);
4698 /* Filter function used to filter out the Search item and its separator.
4699 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4702 shortcuts_combo_filter_func (GtkTreeModel *model,
4706 GtkFileChooserDefault *impl;
4707 GtkTreePath *tree_path;
4712 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4714 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4716 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4717 g_assert (tree_path != NULL);
4719 indices = gtk_tree_path_get_indices (tree_path);
4723 if (impl->has_search)
4725 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4726 if (idx == indices[0])
4730 if (impl->has_recent)
4732 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4733 if (idx == indices[0])
4737 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4738 if (idx == indices[0])
4743 gtk_tree_path_free (tree_path);
4748 /* Creates the combo box with the save folders */
4750 save_folder_combo_create (GtkFileChooserDefault *impl)
4753 GtkCellRenderer *cell;
4755 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4756 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4757 shortcuts_combo_filter_func,
4761 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4762 "model", impl->shortcuts_combo_filter_model,
4763 "focus-on-click", FALSE,
4765 gtk_widget_show (combo);
4767 cell = gtk_cell_renderer_pixbuf_new ();
4768 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4769 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4770 "pixbuf", SHORTCUTS_COL_PIXBUF,
4771 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4772 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4775 cell = gtk_cell_renderer_text_new ();
4776 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4777 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4778 "text", SHORTCUTS_COL_NAME,
4779 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4782 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4783 shortcuts_row_separator_func,
4786 g_signal_connect (combo, "changed",
4787 G_CALLBACK (save_folder_combo_changed_cb), impl);
4792 /* Creates the widgets specific to Save mode */
4794 save_widgets_create (GtkFileChooserDefault *impl)
4799 GtkWidget *alignment;
4801 if (impl->save_widgets != NULL)
4804 location_switch_to_path_bar (impl);
4806 vbox = gtk_vbox_new (FALSE, 12);
4808 table = gtk_table_new (2, 2, FALSE);
4809 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4810 gtk_widget_show (table);
4811 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4812 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4816 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4817 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4818 gtk_table_attach (GTK_TABLE (table), widget,
4822 gtk_widget_show (widget);
4824 /* Location entry */
4826 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4827 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4829 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4830 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4831 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4833 GTK_EXPAND | GTK_FILL, 0,
4835 gtk_widget_show (impl->location_entry);
4836 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4839 impl->save_folder_label = gtk_label_new (NULL);
4840 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4841 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4845 gtk_widget_show (impl->save_folder_label);
4847 impl->save_folder_combo = save_folder_combo_create (impl);
4848 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4850 GTK_EXPAND | GTK_FILL, GTK_FILL,
4852 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4855 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4856 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4858 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4859 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4860 g_signal_connect (impl->save_expander, "notify::expanded",
4861 G_CALLBACK (expander_changed_cb),
4863 gtk_widget_show_all (alignment);
4865 impl->save_widgets = vbox;
4866 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4867 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4868 gtk_widget_show (impl->save_widgets);
4871 /* Destroys the widgets specific to Save mode */
4873 save_widgets_destroy (GtkFileChooserDefault *impl)
4875 if (impl->save_widgets == NULL)
4878 gtk_widget_destroy (impl->save_widgets);
4879 impl->save_widgets = NULL;
4880 impl->location_entry = NULL;
4881 impl->save_folder_label = NULL;
4882 impl->save_folder_combo = NULL;
4883 impl->save_expander = NULL;
4886 /* Turns on the path bar widget. Can be called even if we are already in that
4890 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4892 if (impl->location_entry)
4894 gtk_widget_destroy (impl->location_entry);
4895 impl->location_entry = NULL;
4898 gtk_widget_hide (impl->location_entry_box);
4901 /* Sets the full path of the current folder as the text in the location entry. */
4903 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4905 gchar *text, *filename;
4907 if (!impl->current_folder)
4910 filename = g_file_get_path (impl->current_folder);
4914 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4918 text = g_file_get_uri (impl->current_folder);
4922 gboolean need_slash;
4925 len = strlen (text);
4926 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4932 slash_text = g_new (char, len + 2);
4933 strcpy (slash_text, text);
4934 slash_text[len] = G_DIR_SEPARATOR;
4935 slash_text[len + 1] = 0;
4941 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4948 /* Turns on the location entry. Can be called even if we are already in that
4952 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4954 /* when in search or recent files mode, we are not showing the
4955 * location_entry_box container, so there's no point in switching
4958 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4959 impl->operation_mode == OPERATION_MODE_RECENT)
4962 if (impl->location_entry)
4963 gtk_widget_destroy (impl->location_entry);
4967 gtk_widget_show (impl->location_entry_box);
4971 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4972 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4974 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4975 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4977 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4978 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4980 /* Configure the entry */
4982 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4986 gtk_widget_show (impl->location_entry);
4987 gtk_widget_grab_focus (impl->location_entry);
4990 /* Sets a new location mode. set_buttons determines whether the toggle button
4991 * for the mode will also be changed.
4994 location_mode_set (GtkFileChooserDefault *impl,
4995 LocationMode new_mode,
4996 gboolean set_button)
4998 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4999 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5001 GtkWindow *toplevel;
5002 GtkWidget *current_focus;
5003 gboolean button_active;
5004 gboolean switch_to_file_list;
5008 case LOCATION_MODE_PATH_BAR:
5009 button_active = FALSE;
5011 /* The location_entry will disappear when we switch to path bar mode. So,
5012 * we'll focus the file list in that case, to avoid having a window with
5013 * no focused widget.
5015 toplevel = get_toplevel (GTK_WIDGET (impl));
5016 switch_to_file_list = FALSE;
5019 current_focus = gtk_window_get_focus (toplevel);
5020 if (!current_focus || current_focus == impl->location_entry)
5021 switch_to_file_list = TRUE;
5024 location_switch_to_path_bar (impl);
5026 if (switch_to_file_list)
5027 gtk_widget_grab_focus (impl->browse_files_tree_view);
5031 case LOCATION_MODE_FILENAME_ENTRY:
5032 button_active = TRUE;
5033 location_switch_to_filename_entry (impl);
5037 g_assert_not_reached ();
5043 g_signal_handlers_block_by_func (impl->location_button,
5044 G_CALLBACK (location_button_toggled_cb), impl);
5046 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5048 g_signal_handlers_unblock_by_func (impl->location_button,
5049 G_CALLBACK (location_button_toggled_cb), impl);
5053 impl->location_mode = new_mode;
5057 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5059 /* when in search or recent files mode, we are not showing the
5060 * location_entry_box container, so there's no point in switching
5063 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5064 impl->operation_mode == OPERATION_MODE_RECENT)
5067 /* If the file entry is not visible, show it.
5068 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5070 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5072 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5074 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5076 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5078 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5082 gtk_widget_grab_focus (impl->location_entry);
5087 /* Callback used when one of the location mode buttons is toggled */
5089 location_button_toggled_cb (GtkToggleButton *toggle,
5090 GtkFileChooserDefault *impl)
5093 LocationMode new_mode;
5095 is_active = gtk_toggle_button_get_active (toggle);
5099 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5100 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5104 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5105 new_mode = LOCATION_MODE_PATH_BAR;
5108 location_mode_set (impl, new_mode, FALSE);
5111 /* Creates a toggle button for the location entry. */
5113 location_button_create (GtkFileChooserDefault *impl)
5118 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5119 gtk_widget_show (image);
5121 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5125 g_signal_connect (impl->location_button, "toggled",
5126 G_CALLBACK (location_button_toggled_cb), impl);
5128 str = _("Type a file name");
5130 gtk_widget_set_tooltip_text (impl->location_button, str);
5131 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5134 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5136 browse_widgets_create (GtkFileChooserDefault *impl)
5142 GtkSizeGroup *size_group;
5144 /* size group is used by the [+][-] buttons and the filter combo */
5145 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5146 vbox = gtk_vbox_new (FALSE, 12);
5148 /* Location widgets */
5149 hbox = gtk_hbox_new (FALSE, 12);
5150 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5151 gtk_widget_show (hbox);
5152 impl->browse_path_bar_hbox = hbox;
5154 location_button_create (impl);
5155 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5159 impl->browse_path_bar = create_path_bar (impl);
5160 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5161 gtk_widget_show_all (impl->browse_path_bar);
5162 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5165 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5166 g_signal_connect (impl->browse_new_folder_button, "clicked",
5167 G_CALLBACK (new_folder_button_clicked), impl);
5168 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5170 /* Box for the location label and entry */
5172 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5173 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5175 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5176 gtk_widget_show (impl->location_label);
5177 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5180 hpaned = gtk_hpaned_new ();
5181 gtk_widget_show (hpaned);
5182 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5184 widget = shortcuts_pane_create (impl, size_group);
5185 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5186 widget = file_pane_create (impl, size_group);
5187 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5189 g_object_unref (size_group);
5195 gtk_file_chooser_default_constructor (GType type,
5196 guint n_construct_properties,
5197 GObjectConstructParam *construct_params)
5199 GtkFileChooserDefault *impl;
5202 profile_start ("start", NULL);
5204 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5205 n_construct_properties,
5207 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5209 g_assert (impl->file_system);
5211 gtk_widget_push_composite_child ();
5213 /* Shortcuts model */
5214 shortcuts_model_create (impl);
5216 /* The browse widgets */
5217 impl->browse_widgets = browse_widgets_create (impl);
5218 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5220 /* Alignment to hold extra widget */
5221 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5222 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5224 gtk_widget_pop_composite_child ();
5225 update_appearance (impl);
5227 profile_end ("end", NULL);
5232 /* Sets the extra_widget by packing it in the appropriate place */
5234 set_extra_widget (GtkFileChooserDefault *impl,
5235 GtkWidget *extra_widget)
5239 g_object_ref (extra_widget);
5240 /* FIXME: is this right ? */
5241 gtk_widget_show (extra_widget);
5244 if (impl->extra_widget)
5246 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5247 g_object_unref (impl->extra_widget);
5250 impl->extra_widget = extra_widget;
5251 if (impl->extra_widget)
5253 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5254 gtk_widget_show (impl->extra_align);
5257 gtk_widget_hide (impl->extra_align);
5261 set_local_only (GtkFileChooserDefault *impl,
5262 gboolean local_only)
5264 if (local_only != impl->local_only)
5266 impl->local_only = local_only;
5268 if (impl->shortcuts_model && impl->file_system)
5270 shortcuts_add_volumes (impl);
5271 shortcuts_add_bookmarks (impl);
5274 if (local_only && !g_file_is_native (impl->current_folder))
5276 /* If we are pointing to a non-local folder, make an effort to change
5277 * back to a local folder, but it's really up to the app to not cause
5278 * such a situation, so we ignore errors.
5280 const gchar *home = g_get_home_dir ();
5286 home_file = g_file_new_for_path (home);
5288 _gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5290 g_object_unref (home_file);
5296 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5297 GtkFileChooserDefault *impl)
5299 shortcuts_add_volumes (impl);
5300 shortcuts_add_bookmarks (impl);
5302 bookmarks_check_add_sensitivity (impl);
5303 bookmarks_check_remove_sensitivity (impl);
5304 shortcuts_check_popup_sensitivity (impl);
5307 /* Sets the file chooser to multiple selection mode */
5309 set_select_multiple (GtkFileChooserDefault *impl,
5310 gboolean select_multiple,
5311 gboolean property_notify)
5313 GtkTreeSelection *selection;
5314 GtkSelectionMode mode;
5316 if (select_multiple == impl->select_multiple)
5319 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5321 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5322 gtk_tree_selection_set_mode (selection, mode);
5324 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5326 impl->select_multiple = select_multiple;
5327 g_object_notify (G_OBJECT (impl), "select-multiple");
5329 check_preview_change (impl);
5333 set_file_system_backend (GtkFileChooserDefault *impl)
5335 profile_start ("start for backend", "default");
5337 impl->file_system = _gtk_file_system_new ();
5339 g_signal_connect (impl->file_system, "volumes-changed",
5340 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5341 g_signal_connect (impl->file_system, "bookmarks-changed",
5342 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5344 profile_end ("end", NULL);
5347 /* This function is basically a do_all function.
5349 * It sets the visibility on all the widgets based on the current state, and
5350 * moves the custom_widget if needed.
5353 update_appearance (GtkFileChooserDefault *impl)
5355 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5356 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5360 gtk_widget_hide (impl->location_button);
5361 save_widgets_create (impl);
5363 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5364 text = _("Save in _folder:");
5366 text = _("Create in _folder:");
5368 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5370 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5372 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5373 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5374 gtk_widget_show (impl->browse_widgets);
5378 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5379 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5380 gtk_widget_hide (impl->browse_widgets);
5383 gtk_widget_show (impl->browse_new_folder_button);
5385 if (impl->select_multiple)
5387 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5388 "Re-setting to single selection mode.");
5389 set_select_multiple (impl, FALSE, TRUE);
5392 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5393 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5395 gtk_widget_show (impl->location_button);
5396 save_widgets_destroy (impl);
5397 gtk_widget_show (impl->browse_widgets);
5398 location_mode_set (impl, impl->location_mode, TRUE);
5401 if (impl->location_entry)
5402 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5404 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5405 gtk_widget_hide (impl->browse_new_folder_button);
5407 gtk_widget_show (impl->browse_new_folder_button);
5409 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5410 * of files may change depending whether we are in a file or folder-only mode.
5412 gtk_widget_queue_draw (impl->browse_files_tree_view);
5414 g_signal_emit_by_name (impl, "default-size-changed");
5418 gtk_file_chooser_default_set_property (GObject *object,
5420 const GValue *value,
5424 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5428 case GTK_FILE_CHOOSER_PROP_ACTION:
5430 GtkFileChooserAction action = g_value_get_enum (value);
5432 if (action != impl->action)
5434 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5436 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5437 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5438 && impl->select_multiple)
5440 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5441 "this is not allowed in multiple selection mode. Resetting the file chooser "
5442 "to single selection mode.");
5443 set_select_multiple (impl, FALSE, TRUE);
5445 impl->action = action;
5446 update_appearance (impl);
5447 settings_load (impl);
5452 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5453 /* Ignore property */
5456 case GTK_FILE_CHOOSER_PROP_FILTER:
5457 set_current_filter (impl, g_value_get_object (value));
5460 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5461 set_local_only (impl, g_value_get_boolean (value));
5464 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5465 set_preview_widget (impl, g_value_get_object (value));
5468 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5469 impl->preview_widget_active = g_value_get_boolean (value);
5470 update_preview_widget_visibility (impl);
5473 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5474 impl->use_preview_label = g_value_get_boolean (value);
5475 update_preview_widget_visibility (impl);
5478 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5479 set_extra_widget (impl, g_value_get_object (value));
5482 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5484 gboolean select_multiple = g_value_get_boolean (value);
5485 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5486 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5489 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5490 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5491 "leaving the file chooser in single selection mode.");
5495 set_select_multiple (impl, select_multiple, FALSE);
5499 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5501 gboolean show_hidden = g_value_get_boolean (value);
5502 if (show_hidden != impl->show_hidden)
5504 impl->show_hidden = show_hidden;
5506 if (impl->browse_files_model)
5507 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5512 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5514 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5515 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5526 gtk_file_chooser_default_get_property (GObject *object,
5531 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5535 case GTK_FILE_CHOOSER_PROP_ACTION:
5536 g_value_set_enum (value, impl->action);
5539 case GTK_FILE_CHOOSER_PROP_FILTER:
5540 g_value_set_object (value, impl->current_filter);
5543 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5544 g_value_set_boolean (value, impl->local_only);
5547 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5548 g_value_set_object (value, impl->preview_widget);
5551 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5552 g_value_set_boolean (value, impl->preview_widget_active);
5555 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5556 g_value_set_boolean (value, impl->use_preview_label);
5559 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5560 g_value_set_object (value, impl->extra_widget);
5563 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5564 g_value_set_boolean (value, impl->select_multiple);
5567 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5568 g_value_set_boolean (value, impl->show_hidden);
5571 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5572 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5576 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5581 /* Removes the settings signal handler. It's safe to call multiple times */
5583 remove_settings_signal (GtkFileChooserDefault *impl,
5586 if (impl->settings_signal_id)
5588 GtkSettings *settings;
5590 settings = gtk_settings_get_for_screen (screen);
5591 g_signal_handler_disconnect (settings,
5592 impl->settings_signal_id);
5593 impl->settings_signal_id = 0;
5598 gtk_file_chooser_default_dispose (GObject *object)
5601 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5603 if (impl->extra_widget)
5605 g_object_unref (impl->extra_widget);
5606 impl->extra_widget = NULL;
5609 pending_select_files_free (impl);
5611 /* cancel all pending operations */
5612 if (impl->pending_cancellables)
5614 for (l = impl->pending_cancellables; l; l = l->next)
5616 GCancellable *cancellable = G_CANCELLABLE (l->data);
5617 g_cancellable_cancel (cancellable);
5619 g_slist_free (impl->pending_cancellables);
5620 impl->pending_cancellables = NULL;
5623 if (impl->reload_icon_cancellables)
5625 for (l = impl->reload_icon_cancellables; l; l = l->next)
5627 GCancellable *cancellable = G_CANCELLABLE (l->data);
5628 g_cancellable_cancel (cancellable);
5630 g_slist_free (impl->reload_icon_cancellables);
5631 impl->reload_icon_cancellables = NULL;
5634 if (impl->loading_shortcuts)
5636 for (l = impl->loading_shortcuts; l; l = l->next)
5638 GCancellable *cancellable = G_CANCELLABLE (l->data);
5639 g_cancellable_cancel (cancellable);
5641 g_slist_free (impl->loading_shortcuts);
5642 impl->loading_shortcuts = NULL;
5645 if (impl->file_list_drag_data_received_cancellable)
5647 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5648 impl->file_list_drag_data_received_cancellable = NULL;
5651 if (impl->update_current_folder_cancellable)
5653 g_cancellable_cancel (impl->update_current_folder_cancellable);
5654 impl->update_current_folder_cancellable = NULL;
5657 if (impl->show_and_select_files_cancellable)
5659 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5660 impl->show_and_select_files_cancellable = NULL;
5663 if (impl->should_respond_get_info_cancellable)
5665 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5666 impl->should_respond_get_info_cancellable = NULL;
5669 if (impl->update_from_entry_cancellable)
5671 g_cancellable_cancel (impl->update_from_entry_cancellable);
5672 impl->update_from_entry_cancellable = NULL;
5675 if (impl->shortcuts_activate_iter_cancellable)
5677 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5678 impl->shortcuts_activate_iter_cancellable = NULL;
5681 search_stop_searching (impl, TRUE);
5682 recent_stop_loading (impl);
5684 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5686 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5689 /* We override show-all since we have internal widgets that
5690 * shouldn't be shown when you call show_all(), like the filter
5694 gtk_file_chooser_default_show_all (GtkWidget *widget)
5696 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5698 gtk_widget_show (widget);
5700 if (impl->extra_widget)
5701 gtk_widget_show_all (impl->extra_widget);
5704 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5705 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5708 toplevel_set_focus_cb (GtkWindow *window,
5710 GtkFileChooserDefault *impl)
5712 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5715 /* We monitor the focus widget on our toplevel to be able to know which widget
5716 * was last focused at the time our "should_respond" method gets called.
5719 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5720 GtkWidget *previous_toplevel)
5722 GtkFileChooserDefault *impl;
5723 GtkWidget *toplevel;
5725 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5727 if (previous_toplevel)
5729 g_assert (impl->toplevel_set_focus_id != 0);
5730 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5731 impl->toplevel_set_focus_id = 0;
5732 impl->toplevel_last_focus_widget = NULL;
5735 g_assert (impl->toplevel_set_focus_id == 0);
5737 toplevel = gtk_widget_get_toplevel (widget);
5738 if (GTK_IS_WINDOW (toplevel))
5740 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5741 G_CALLBACK (toplevel_set_focus_cb), impl);
5742 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5746 /* Changes the icons wherever it is needed */
5748 change_icon_theme (GtkFileChooserDefault *impl)
5750 GtkSettings *settings;
5753 profile_start ("start", NULL);
5755 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5757 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5758 impl->icon_size = MAX (width, height);
5760 impl->icon_size = FALLBACK_ICON_SIZE;
5762 shortcuts_reload_icons (impl);
5763 gtk_widget_queue_resize (impl->browse_files_tree_view);
5765 profile_end ("end", NULL);
5768 /* Callback used when a GtkSettings value changes */
5770 settings_notify_cb (GObject *object,
5772 GtkFileChooserDefault *impl)
5776 profile_start ("start", NULL);
5778 name = g_param_spec_get_name (pspec);
5780 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5781 strcmp (name, "gtk-icon-sizes") == 0)
5782 change_icon_theme (impl);
5784 profile_end ("end", NULL);
5787 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5791 check_icon_theme (GtkFileChooserDefault *impl)
5793 GtkSettings *settings;
5795 profile_start ("start", NULL);
5797 if (impl->settings_signal_id)
5799 profile_end ("end", NULL);
5803 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5805 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5806 impl->settings_signal_id = g_signal_connect (settings, "notify",
5807 G_CALLBACK (settings_notify_cb), impl);
5809 change_icon_theme (impl);
5812 profile_end ("end", NULL);
5816 gtk_file_chooser_default_style_set (GtkWidget *widget,
5817 GtkStyle *previous_style)
5819 GtkFileChooserDefault *impl;
5821 profile_start ("start", NULL);
5823 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5825 profile_msg (" parent class style_set start", NULL);
5826 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5827 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5828 profile_msg (" parent class style_set end", NULL);
5830 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5831 change_icon_theme (impl);
5833 profile_msg (" emit default-size-changed start", NULL);
5834 g_signal_emit_by_name (widget, "default-size-changed");
5835 profile_msg (" emit default-size-changed end", NULL);
5837 profile_end ("end", NULL);
5841 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5842 GdkScreen *previous_screen)
5844 GtkFileChooserDefault *impl;
5846 profile_start ("start", NULL);
5848 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5850 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5851 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5853 remove_settings_signal (impl, previous_screen);
5854 check_icon_theme (impl);
5856 g_signal_emit_by_name (widget, "default-size-changed");
5858 profile_end ("end", NULL);
5862 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5863 GtkAllocation *allocation)
5865 GtkFileChooserDefault *impl;
5867 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5869 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5871 impl->default_width = allocation->width;
5872 impl->default_height = allocation->height;
5874 if (impl->preview_widget_active &&
5875 impl->preview_widget &&
5876 GTK_WIDGET_DRAWABLE (impl->preview_widget))
5877 impl->default_width -= impl->preview_widget->allocation.width + PREVIEW_HBOX_SPACING;
5879 if (impl->extra_widget &&
5880 GTK_WIDGET_DRAWABLE (impl->extra_widget))
5881 impl->default_height -= GTK_BOX (widget)->spacing + impl->extra_widget->allocation.height;
5885 get_is_file_filtered (GtkFileChooserDefault *impl,
5887 GFileInfo *file_info)
5889 GtkFileFilterInfo filter_info;
5890 GtkFileFilterFlags needed;
5893 if (!impl->current_filter)
5896 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5898 needed = gtk_file_filter_get_needed (impl->current_filter);
5900 filter_info.display_name = g_file_info_get_display_name (file_info);
5901 filter_info.mime_type = g_file_info_get_content_type (file_info);
5903 if (needed & GTK_FILE_FILTER_FILENAME)
5905 filter_info.filename = g_file_get_path (file);
5906 if (filter_info.filename)
5907 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5910 filter_info.filename = NULL;
5912 if (needed & GTK_FILE_FILTER_URI)
5914 filter_info.uri = g_file_get_uri (file);
5915 if (filter_info.uri)
5916 filter_info.contains |= GTK_FILE_FILTER_URI;
5919 filter_info.uri = NULL;
5921 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5923 if (filter_info.filename)
5924 g_free ((gchar *)filter_info.filename);
5925 if (filter_info.uri)
5926 g_free ((gchar *)filter_info.uri);
5932 settings_load (GtkFileChooserDefault *impl)
5934 GtkFileChooserSettings *settings;
5935 LocationMode location_mode;
5936 gboolean show_hidden;
5937 gboolean expand_folders;
5939 settings = _gtk_file_chooser_settings_new ();
5941 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5942 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5943 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5945 g_object_unref (settings);
5947 location_mode_set (impl, location_mode, TRUE);
5948 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5949 impl->expand_folders = expand_folders;
5950 if (impl->save_expander)
5951 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5955 settings_save (GtkFileChooserDefault *impl)
5957 GtkFileChooserSettings *settings;
5959 settings = _gtk_file_chooser_settings_new ();
5961 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5962 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5963 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
5966 _gtk_file_chooser_settings_save (settings, NULL);
5968 g_object_unref (settings);
5971 /* GtkWidget::map method */
5973 gtk_file_chooser_default_map (GtkWidget *widget)
5975 GtkFileChooserDefault *impl;
5976 char *current_working_dir;
5978 profile_start ("start", NULL);
5980 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5982 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5984 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5986 switch (impl->reload_state)
5989 /* The user didn't explicitly give us a folder to
5990 * display, so we'll use the cwd
5992 current_working_dir = g_get_current_dir ();
5993 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
5994 current_working_dir);
5995 g_free (current_working_dir);
5998 case RELOAD_HAS_FOLDER:
5999 /* Nothing; we are already loading or loaded, so we
6000 * don't need to reload
6004 case RELOAD_WAS_UNMAPPED:
6005 /* Just reload the current folder; else continue
6008 if (impl->current_folder)
6010 pending_select_files_store_selection (impl);
6011 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6016 g_assert_not_reached ();
6020 volumes_bookmarks_changed_cb (impl->file_system, impl);
6022 settings_load (impl);
6024 profile_end ("end", NULL);
6027 /* GtkWidget::unmap method */
6029 gtk_file_chooser_default_unmap (GtkWidget *widget)
6031 GtkFileChooserDefault *impl;
6033 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6035 settings_save (impl);
6037 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6039 impl->reload_state = RELOAD_WAS_UNMAPPED;
6043 list_model_filter_func (GtkFileSystemModel *model,
6045 GFileInfo *file_info,
6048 GtkFileChooserDefault *impl = user_data;
6050 if (!impl->current_filter)
6053 if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
6056 return !get_is_file_filtered (impl, file, file_info);
6060 install_list_model_filter (GtkFileChooserDefault *impl)
6062 GtkFileSystemModelFilter filter;
6065 g_assert (impl->browse_files_model != NULL);
6067 if (impl->current_filter)
6069 filter = list_model_filter_func;
6078 _gtk_file_system_model_set_filter (impl->browse_files_model,
6083 #define COMPARE_DIRECTORIES \
6084 GtkFileChooserDefault *impl = user_data; \
6085 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6086 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6087 gboolean dir_a, dir_b; \
6090 dir_a = (g_file_info_get_file_type (info_a) == G_FILE_TYPE_DIRECTORY); \
6092 return impl->list_sort_ascending ? -1 : 1; \
6095 dir_b = (g_file_info_get_file_type (info_b) == G_FILE_TYPE_DIRECTORY); \
6097 return impl->list_sort_ascending ? 1 : -1; \
6099 if (dir_a != dir_b) \
6100 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6102 /* Sort callback for the filename column */
6104 name_sort_func (GtkTreeModel *model,
6109 COMPARE_DIRECTORIES;
6112 gchar *key_a, *key_b;
6115 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6116 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6117 result = strcmp (key_a, key_b);
6126 /* Sort callback for the size column */
6128 size_sort_func (GtkTreeModel *model,
6133 COMPARE_DIRECTORIES;
6136 goffset size_a = g_file_info_get_size (info_a);
6137 goffset size_b = g_file_info_get_size (info_b);
6139 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6143 /* Sort callback for the mtime column */
6145 mtime_sort_func (GtkTreeModel *model,
6150 COMPARE_DIRECTORIES;
6155 g_file_info_get_modification_time (info_a, &ta);
6156 g_file_info_get_modification_time (info_b, &tb);
6158 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6162 /* Callback used when the sort column changes. We cache the sort order for use
6163 * in name_sort_func().
6166 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6167 GtkFileChooserDefault *impl)
6169 GtkSortType sort_type;
6171 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6172 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6176 set_busy_cursor (GtkFileChooserDefault *impl,
6179 GtkWindow *toplevel;
6180 GdkDisplay *display;
6183 toplevel = get_toplevel (GTK_WIDGET (impl));
6184 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6187 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6190 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6194 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6195 gdk_display_flush (display);
6198 gdk_cursor_unref (cursor);
6201 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6203 load_set_model (GtkFileChooserDefault *impl)
6205 profile_start ("start", NULL);
6207 g_assert (impl->browse_files_model != NULL);
6208 g_assert (impl->sort_model == NULL);
6210 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6211 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6212 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6213 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6214 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6215 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6216 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6217 impl->list_sort_ascending = TRUE;
6218 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6220 g_signal_connect (impl->sort_model, "sort_column_changed",
6221 G_CALLBACK (list_sort_column_changed_cb), impl);
6223 profile_msg (" gtk_tree_view_set_model start", NULL);
6224 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6225 GTK_TREE_MODEL (impl->sort_model));
6226 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6227 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6228 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6229 profile_msg (" gtk_tree_view_set_model end", NULL);
6231 profile_end ("end", NULL);
6234 /* Timeout callback used when the loading timer expires */
6236 load_timeout_cb (gpointer data)
6238 GtkFileChooserDefault *impl;
6240 profile_start ("start", NULL);
6242 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6243 g_assert (impl->load_state == LOAD_PRELOAD);
6244 g_assert (impl->load_timeout_id != 0);
6245 g_assert (impl->browse_files_model != NULL);
6247 impl->load_timeout_id = 0;
6248 impl->load_state = LOAD_LOADING;
6250 load_set_model (impl);
6252 profile_end ("end", NULL);
6257 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6259 load_setup_timer (GtkFileChooserDefault *impl)
6261 g_assert (impl->load_timeout_id == 0);
6262 g_assert (impl->load_state != LOAD_PRELOAD);
6264 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6265 impl->load_state = LOAD_PRELOAD;
6268 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6270 load_remove_timer (GtkFileChooserDefault *impl)
6272 if (impl->load_timeout_id != 0)
6274 g_assert (impl->load_state == LOAD_PRELOAD);
6276 g_source_remove (impl->load_timeout_id);
6277 impl->load_timeout_id = 0;
6278 impl->load_state = LOAD_EMPTY;
6281 g_assert (impl->load_state == LOAD_EMPTY ||
6282 impl->load_state == LOAD_LOADING ||
6283 impl->load_state == LOAD_FINISHED);
6286 /* Selects the first row in the file list */
6288 browse_files_select_first_row (GtkFileChooserDefault *impl)
6292 if (!impl->sort_model)
6295 path = gtk_tree_path_new_from_indices (0, -1);
6296 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6297 gtk_tree_path_free (path);
6300 struct center_selected_row_closure {
6301 GtkFileChooserDefault *impl;
6302 gboolean already_centered;
6305 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6306 * selected row in the tree view.
6309 center_selected_row_foreach_cb (GtkTreeModel *model,
6314 struct center_selected_row_closure *closure;
6317 if (closure->already_centered)
6320 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6321 closure->already_centered = TRUE;
6324 /* Centers the selected row in the tree view */
6326 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6328 struct center_selected_row_closure closure;
6329 GtkTreeSelection *selection;
6331 closure.impl = impl;
6332 closure.already_centered = FALSE;
6334 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6335 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6338 struct ShowAndSelectPathsData
6340 GtkFileChooserDefault *impl;
6345 show_and_select_files_finished_loading (GtkFolder *folder,
6348 gboolean have_hidden;
6349 gboolean have_filtered;
6351 struct ShowAndSelectPathsData *data = user_data;
6353 have_hidden = FALSE;
6354 have_filtered = FALSE;
6356 for (l = data->files; l; l = l->next)
6363 info = _gtk_folder_get_info (folder, file);
6367 have_hidden = g_file_info_get_is_hidden (info);
6370 have_filtered = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY) &&
6371 get_is_file_filtered (data->impl, file, info);
6373 g_object_unref (info);
6375 if (have_hidden && have_filtered)
6376 break; /* we now have all the information we need */
6380 g_signal_handlers_disconnect_by_func (folder,
6381 show_and_select_files_finished_loading,
6385 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6388 set_current_filter (data->impl, NULL);
6390 for (l = data->files; l; l = l->next)
6395 _gtk_file_system_model_path_do (data->impl->browse_files_model, file,
6396 select_func, data->impl);
6399 browse_files_center_selected_row (data->impl);
6401 g_object_unref (data->impl);
6402 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6403 g_slist_free (data->files);
6408 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6410 const GError *error,
6413 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6414 struct ShowAndSelectPathsData *data = user_data;
6416 if (data->impl->show_and_select_files_cancellable != cancellable)
6419 data->impl->show_and_select_files_cancellable = NULL;
6421 if (cancelled || error)
6424 g_object_unref (cancellable);
6426 if (_gtk_folder_is_finished_loading (folder))
6427 show_and_select_files_finished_loading (folder, user_data);
6429 g_signal_connect (folder, "finished-loading",
6430 G_CALLBACK (show_and_select_files_finished_loading),
6436 g_object_unref (data->impl);
6437 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6438 g_slist_free (data->files);
6441 g_object_unref (cancellable);
6445 show_and_select_files (GtkFileChooserDefault *impl,
6450 struct ShowAndSelectPathsData *info;
6452 profile_start ("start", NULL);
6456 profile_end ("end", NULL);
6460 info = g_new (struct ShowAndSelectPathsData, 1);
6461 info->impl = g_object_ref (impl);
6462 info->files = g_slist_copy (files);
6463 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6465 if (impl->show_and_select_files_cancellable)
6466 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6468 impl->show_and_select_files_cancellable =
6469 _gtk_file_system_get_folder (impl->file_system, parent_file,
6470 "standard::is-hidden,standard::type,standard::name",
6471 show_and_select_files_get_folder_cb, info);
6473 profile_end ("end", NULL);
6477 /* Processes the pending operation when a folder is finished loading */
6479 pending_select_files_process (GtkFileChooserDefault *impl)
6481 g_assert (impl->load_state == LOAD_FINISHED);
6482 g_assert (impl->browse_files_model != NULL);
6483 g_assert (impl->sort_model != NULL);
6485 if (impl->pending_select_files)
6488 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6489 pending_select_files_free (impl);
6490 browse_files_center_selected_row (impl);
6494 /* We only select the first row if the chooser is actually mapped ---
6495 * selecting the first row is to help the user when he is interacting with
6496 * the chooser, but sometimes a chooser works not on behalf of the user,
6497 * but rather on behalf of something else like GtkFileChooserButton. In
6498 * that case, the chooser's selection should be what the caller expects,
6499 * as the user can't see that something else got selected. See bug #165264.
6501 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6502 browse_files_select_first_row (impl);
6505 g_assert (impl->pending_select_files == NULL);
6508 /* Callback used when the file system model finishes loading */
6510 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6511 GtkFileChooserDefault *impl)
6513 profile_start ("start", NULL);
6515 if (impl->load_state == LOAD_PRELOAD)
6517 load_remove_timer (impl);
6518 load_set_model (impl);
6520 else if (impl->load_state == LOAD_LOADING)
6526 /* We can't g_assert_not_reached(), as something other than us may have
6527 * initiated a folder reload. See #165556.
6529 profile_end ("end", NULL);
6533 g_assert (impl->load_timeout_id == 0);
6535 impl->load_state = LOAD_FINISHED;
6537 pending_select_files_process (impl);
6538 set_busy_cursor (impl, FALSE);
6539 #ifdef PROFILE_FILE_CHOOSER
6540 access ("MARK: *** FINISHED LOADING", F_OK);
6543 profile_end ("end", NULL);
6547 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6549 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6551 if (impl->browse_files_model)
6553 g_object_unref (impl->browse_files_model);
6554 impl->browse_files_model = NULL;
6557 if (impl->sort_model)
6559 g_object_unref (impl->sort_model);
6560 impl->sort_model = NULL;
6563 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6566 /* Gets rid of the old list model and creates a new one for the current folder */
6568 set_list_model (GtkFileChooserDefault *impl,
6571 g_assert (impl->current_folder != NULL);
6573 profile_start ("start", NULL);
6575 stop_loading_and_clear_list_model (impl);
6577 set_busy_cursor (impl, TRUE);
6578 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6580 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6581 impl->current_folder, 0,
6582 "standard,time,thumbnail::*",
6584 if (!impl->browse_files_model)
6586 set_busy_cursor (impl, FALSE);
6587 profile_end ("end", NULL);
6591 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6593 g_signal_connect (impl->browse_files_model, "finished-loading",
6594 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6596 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6598 install_list_model_filter (impl);
6600 profile_end ("end", NULL);
6605 struct update_chooser_entry_selected_foreach_closure {
6607 GtkTreeIter first_selected_iter;
6611 compare_utf8_filenames (const gchar *a,
6614 gchar *a_folded, *b_folded;
6617 a_folded = g_utf8_strdown (a, -1);
6618 b_folded = g_utf8_strdown (b, -1);
6620 retval = strcmp (a_folded, b_folded);
6629 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6634 struct update_chooser_entry_selected_foreach_closure *closure;
6637 closure->num_selected++;
6639 if (closure->num_selected == 1)
6640 closure->first_selected_iter = *iter;
6644 update_chooser_entry (GtkFileChooserDefault *impl)
6646 GtkTreeSelection *selection;
6647 struct update_chooser_entry_selected_foreach_closure closure;
6648 const char *file_part;
6650 /* no need to update the file chooser's entry if there's no entry */
6651 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6652 impl->operation_mode == OPERATION_MODE_RECENT ||
6653 !impl->location_entry)
6656 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6657 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6658 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6659 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6660 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6663 g_assert (impl->location_entry != NULL);
6665 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6666 closure.num_selected = 0;
6667 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6671 if (closure.num_selected == 0)
6673 goto maybe_clear_entry;
6675 else if (closure.num_selected == 1)
6677 GtkTreeIter child_iter;
6679 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6682 gboolean change_entry;
6684 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6686 &closure.first_selected_iter);
6688 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6690 /* If the cursor moved to the row of the newly created folder,
6691 * retrieving info will return NULL.
6696 g_free (impl->browse_files_last_selected_name);
6697 impl->browse_files_last_selected_name =
6698 g_strdup (g_file_info_get_display_name (info));
6700 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6701 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6702 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6704 /* We don't want the name to change when clicking on a folder... */
6705 change_entry = (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
6708 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6712 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6714 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6715 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6723 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6724 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6726 /* Multiple selection, so just clear the entry. */
6728 g_free (impl->browse_files_last_selected_name);
6729 impl->browse_files_last_selected_name = NULL;
6731 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6737 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6738 && impl->browse_files_last_selected_name)
6740 const char *entry_text;
6742 gboolean clear_entry;
6744 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6745 len = strlen (entry_text);
6748 /* The file chooser entry may have appended a "/" to its text. So
6749 * take it out, and compare the result to the old selection.
6751 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6755 tmp = g_strndup (entry_text, len - 1);
6756 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6760 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6763 clear_entry = FALSE;
6766 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6771 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6775 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6779 struct UpdateCurrentFolderData
6781 GtkFileChooserDefault *impl;
6783 gboolean keep_trail;
6784 gboolean clear_entry;
6785 GFile *original_file;
6786 GError *original_error;
6790 update_current_folder_get_info_cb (GCancellable *cancellable,
6792 const GError *error,
6795 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6796 struct UpdateCurrentFolderData *data = user_data;
6797 GtkFileChooserDefault *impl = data->impl;
6799 if (cancellable != impl->update_current_folder_cancellable)
6802 impl->update_current_folder_cancellable = NULL;
6803 impl->reload_state = RELOAD_EMPTY;
6805 set_busy_cursor (impl, FALSE);
6814 if (!data->original_file)
6816 data->original_file = g_object_ref (data->file);
6817 data->original_error = g_error_copy (error);
6820 parent_file = g_file_get_parent (data->file);
6822 /* get parent path and try to change the folder to that */
6825 g_object_unref (data->file);
6826 data->file = parent_file;
6828 g_object_unref (cancellable);
6830 /* restart the update current folder operation */
6831 impl->reload_state = RELOAD_HAS_FOLDER;
6833 impl->update_current_folder_cancellable =
6834 _gtk_file_system_get_info (impl->file_system, data->file,
6836 update_current_folder_get_info_cb,
6839 set_busy_cursor (impl, TRUE);
6845 /* error and bail out */
6846 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6847 g_object_unref (data->original_file);
6853 if (data->original_file)
6855 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6857 g_object_unref (data->original_file);
6860 if (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
6863 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
6866 if (impl->current_folder != data->file)
6868 if (impl->current_folder)
6869 g_object_unref (impl->current_folder);
6871 impl->current_folder = g_object_ref (data->file);
6873 impl->reload_state = RELOAD_HAS_FOLDER;
6876 /* Update the widgets that may trigger a folder change themselves. */
6878 if (!impl->changing_folder)
6880 impl->changing_folder = TRUE;
6882 shortcuts_update_current_folder (impl);
6884 impl->changing_folder = FALSE;
6887 /* Set the folder on the save entry */
6889 if (impl->location_entry)
6891 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6892 impl->current_folder);
6894 if (data->clear_entry)
6895 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6898 /* Create a new list model. This is slightly evil; we store the result value
6899 * but perform more actions rather than returning immediately even if it
6900 * generates an error.
6902 set_list_model (impl, NULL);
6904 /* Refresh controls */
6906 shortcuts_find_current_folder (impl);
6908 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6910 check_preview_change (impl);
6911 bookmarks_check_add_sensitivity (impl);
6913 g_signal_emit_by_name (impl, "selection-changed", 0);
6916 g_object_unref (data->file);
6919 g_object_unref (cancellable);
6923 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6925 gboolean keep_trail,
6926 gboolean clear_entry,
6929 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6930 struct UpdateCurrentFolderData *data;
6932 profile_start ("start", NULL);
6934 g_object_ref (file);
6936 switch (impl->operation_mode)
6938 case OPERATION_MODE_SEARCH:
6939 search_switch_to_browse_mode (impl);
6941 case OPERATION_MODE_RECENT:
6942 recent_switch_to_browse_mode (impl);
6944 case OPERATION_MODE_BROWSE:
6948 if (impl->local_only && !g_file_is_native (file))
6950 g_set_error_literal (error,
6951 GTK_FILE_CHOOSER_ERROR,
6952 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6953 _("Cannot change to folder because it is not local"));
6955 g_object_unref (file);
6956 profile_end ("end - not local", NULL);
6960 if (impl->update_current_folder_cancellable)
6961 g_cancellable_cancel (impl->update_current_folder_cancellable);
6963 /* Test validity of path here. */
6964 data = g_new0 (struct UpdateCurrentFolderData, 1);
6966 data->file = g_object_ref (file);
6967 data->keep_trail = keep_trail;
6968 data->clear_entry = clear_entry;
6970 impl->reload_state = RELOAD_HAS_FOLDER;
6972 impl->update_current_folder_cancellable =
6973 _gtk_file_system_get_info (impl->file_system, file,
6975 update_current_folder_get_info_cb,
6978 set_busy_cursor (impl, TRUE);
6979 g_object_unref (file);
6981 profile_end ("end", NULL);
6986 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
6988 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6990 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6991 impl->operation_mode == OPERATION_MODE_RECENT)
6994 if (impl->reload_state == RELOAD_EMPTY)
6996 char *current_working_dir;
6999 /* We are unmapped, or we had an error while loading the last folder. We'll return
7000 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7001 * explicitly calls set_current_folder() on us.
7003 current_working_dir = g_get_current_dir ();
7004 file = g_file_new_for_path (current_working_dir);
7005 g_free (current_working_dir);
7009 if (impl->current_folder)
7010 return g_object_ref (impl->current_folder);
7016 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7019 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7021 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7022 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7024 pending_select_files_free (impl);
7025 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7029 select_func (GtkFileSystemModel *model,
7034 GtkFileChooserDefault *impl = user_data;
7035 GtkTreeSelection *selection;
7036 GtkTreeIter sorted_iter;
7038 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7040 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7041 gtk_tree_selection_select_iter (selection, &sorted_iter);
7045 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7049 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7053 parent_file = g_file_get_parent (file);
7056 return _gtk_file_chooser_set_current_folder_file (chooser, file, error);
7058 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7059 impl->operation_mode == OPERATION_MODE_RECENT ||
7060 impl->load_state == LOAD_EMPTY)
7066 g_assert (impl->current_folder != NULL);
7068 same_path = g_file_equal (parent_file, impl->current_folder);
7071 if (same_path && impl->load_state == LOAD_FINISHED)
7076 files.data = (gpointer) file;
7079 result = show_and_select_files (impl, parent_file, &files, error);
7080 g_object_unref (parent_file);
7084 pending_select_files_add (impl, file);
7090 result = _gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7091 g_object_unref (parent_file);
7095 g_object_unref (parent_file);
7100 unselect_func (GtkFileSystemModel *model,
7105 GtkFileChooserDefault *impl = user_data;
7106 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7107 GtkTreePath *sorted_path;
7109 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7111 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7113 gtk_tree_path_free (sorted_path);
7117 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7120 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7122 if (!impl->browse_files_model)
7125 _gtk_file_system_model_path_do (impl->browse_files_model, file,
7126 unselect_func, impl);
7130 maybe_select (GtkTreeModel *model,
7135 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7136 GtkTreeSelection *selection;
7140 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7142 info = get_list_file_info (impl, iter);
7143 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
7145 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7146 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7147 gtk_tree_selection_select_iter (selection, iter);
7149 gtk_tree_selection_unselect_iter (selection, iter);
7155 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7157 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7159 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7160 impl->operation_mode == OPERATION_MODE_RECENT)
7162 GtkTreeSelection *selection;
7164 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7165 gtk_tree_selection_select_all (selection);
7169 if (impl->select_multiple)
7170 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7171 maybe_select, impl);
7175 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7177 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7178 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7180 gtk_tree_selection_unselect_all (selection);
7181 pending_select_files_free (impl);
7184 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7186 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7188 * is_empty_ret - whether the file entry is totally empty
7190 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7191 * the path will be "$cwd/foobar")
7194 check_save_entry (GtkFileChooserDefault *impl,
7196 gboolean *is_well_formed_ret,
7197 gboolean *is_empty_ret,
7198 gboolean *is_file_part_empty_ret,
7199 gboolean *is_folder)
7201 GtkFileChooserEntry *chooser_entry;
7202 GFile *current_folder;
7203 const char *file_part;
7207 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7208 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7209 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7210 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7211 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7213 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7215 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7218 *is_well_formed_ret = TRUE;
7219 *is_empty_ret = TRUE;
7220 *is_file_part_empty_ret = TRUE;
7226 *is_empty_ret = FALSE;
7228 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7229 if (!current_folder)
7232 *is_well_formed_ret = FALSE;
7233 *is_file_part_empty_ret = FALSE;
7239 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7241 if (!file_part || file_part[0] == '\0')
7243 *file_ret = g_object_ref (current_folder);
7244 *is_well_formed_ret = TRUE;
7245 *is_file_part_empty_ret = TRUE;
7251 *is_file_part_empty_ret = FALSE;
7254 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7258 error_building_filename_dialog (impl, error);
7260 *is_well_formed_ret = FALSE;
7267 *is_well_formed_ret = TRUE;
7268 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7271 struct get_files_closure {
7272 GtkFileChooserDefault *impl;
7274 GFile *file_from_entry;
7278 get_files_foreach (GtkTreeModel *model,
7283 struct get_files_closure *info;
7285 GtkFileSystemModel *fs_model;
7286 GtkTreeIter sel_iter;
7289 fs_model = info->impl->browse_files_model;
7290 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7292 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7294 return; /* We are on the editable row */
7296 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7297 info->result = g_slist_prepend (info->result, g_object_ref (file));
7301 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7303 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7304 struct get_files_closure info;
7305 GtkWindow *toplevel;
7306 GtkWidget *current_focus;
7307 gboolean file_list_seen;
7309 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7310 return search_get_selected_files (impl);
7312 if (impl->operation_mode == OPERATION_MODE_RECENT)
7313 return recent_get_selected_files (impl);
7317 info.file_from_entry = NULL;
7319 toplevel = get_toplevel (GTK_WIDGET (impl));
7321 current_focus = gtk_window_get_focus (toplevel);
7323 current_focus = NULL;
7325 file_list_seen = FALSE;
7326 if (current_focus == impl->browse_files_tree_view)
7328 GtkTreeSelection *selection;
7332 file_list_seen = TRUE;
7333 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7334 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7336 /* If there is no selection in the file list, we probably have this situation:
7338 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7339 * 2. He then double-clicked on a folder ("bar") in the file list
7341 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7342 * filename entry to see if that is the case.
7344 if (info.result == NULL && impl->location_entry)
7347 else if (impl->location_entry && current_focus == impl->location_entry)
7349 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7353 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7358 if (!is_well_formed)
7361 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7363 g_object_unref (info.file_from_entry);
7367 if (info.file_from_entry)
7368 info.result = g_slist_prepend (info.result, info.file_from_entry);
7369 else if (!file_list_seen)
7374 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7376 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7380 /* The focus is on a dialog's action area button or something else */
7381 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7382 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7390 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7391 * fall back to the current directory */
7392 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7393 info.result == NULL)
7395 GFile *current_folder;
7397 current_folder = _gtk_file_chooser_get_current_folder_file (chooser);
7400 info.result = g_slist_prepend (info.result, current_folder);
7403 return g_slist_reverse (info.result);
7407 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7409 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7411 if (impl->preview_file)
7412 return g_object_ref (impl->preview_file);
7417 static GtkFileSystem *
7418 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7420 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7422 return impl->file_system;
7425 /* Shows or hides the filter widgets */
7427 show_filters (GtkFileChooserDefault *impl,
7431 gtk_widget_show (impl->filter_combo_hbox);
7433 gtk_widget_hide (impl->filter_combo_hbox);
7437 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7438 GtkFileFilter *filter)
7440 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7443 if (g_slist_find (impl->filters, filter))
7445 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7449 g_object_ref_sink (filter);
7450 impl->filters = g_slist_append (impl->filters, filter);
7452 name = gtk_file_filter_get_name (filter);
7454 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7456 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7458 if (!g_slist_find (impl->filters, impl->current_filter))
7459 set_current_filter (impl, filter);
7461 show_filters (impl, TRUE);
7465 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7466 GtkFileFilter *filter)
7468 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7469 GtkTreeModel *model;
7473 filter_index = g_slist_index (impl->filters, filter);
7475 if (filter_index < 0)
7477 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7481 impl->filters = g_slist_remove (impl->filters, filter);
7483 if (filter == impl->current_filter)
7486 set_current_filter (impl, impl->filters->data);
7488 set_current_filter (impl, NULL);
7491 /* Remove row from the combo box */
7492 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7493 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7494 g_assert_not_reached ();
7496 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7498 g_object_unref (filter);
7501 show_filters (impl, FALSE);
7505 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7507 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7509 return g_slist_copy (impl->filters);
7512 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7514 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7517 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7520 struct AddShortcutData
7522 GtkFileChooserDefault *impl;
7527 add_shortcut_get_info_cb (GCancellable *cancellable,
7529 const GError *error,
7533 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7534 struct AddShortcutData *data = user_data;
7536 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7539 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7541 if (cancelled || error || g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
7544 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7546 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7549 g_object_unref (data->impl);
7550 g_object_unref (data->file);
7553 g_object_unref (cancellable);
7557 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7561 GCancellable *cancellable;
7562 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7563 struct AddShortcutData *data;
7567 /* Avoid adding duplicates */
7568 pos = shortcut_find_position (impl, file);
7569 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7573 uri = g_file_get_uri (file);
7574 /* translators, "Shortcut" means "Bookmark" here */
7576 GTK_FILE_CHOOSER_ERROR,
7577 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7578 _("Shortcut %s already exists"),
7585 for (l = impl->loading_shortcuts; l; l = l->next)
7587 GCancellable *c = l->data;
7590 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7591 if (f && g_file_equal (file, f))
7595 uri = g_file_get_uri (file);
7597 GTK_FILE_CHOOSER_ERROR,
7598 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7599 _("Shortcut %s already exists"),
7607 data = g_new0 (struct AddShortcutData, 1);
7608 data->impl = g_object_ref (impl);
7609 data->file = g_object_ref (file);
7611 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7613 add_shortcut_get_info_cb, data);
7618 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7619 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7625 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7629 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7636 for (l = impl->loading_shortcuts; l; l = l->next)
7638 GCancellable *c = l->data;
7641 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7642 if (f && g_file_equal (file, f))
7644 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7645 g_cancellable_cancel (c);
7650 if (impl->num_shortcuts == 0)
7653 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7654 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7655 g_assert_not_reached ();
7657 for (i = 0; i < impl->num_shortcuts; i++)
7660 ShortcutType shortcut_type;
7663 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7664 SHORTCUTS_COL_DATA, &col_data,
7665 SHORTCUTS_COL_TYPE, &shortcut_type,
7667 g_assert (col_data != NULL);
7668 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7670 shortcut = col_data;
7671 if (g_file_equal (shortcut, file))
7673 shortcuts_remove_rows (impl, pos + i, 1);
7674 impl->num_shortcuts--;
7678 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7679 g_assert_not_reached ();
7684 uri = g_file_get_uri (file);
7685 /* translators, "Shortcut" means "Bookmark" here */
7687 GTK_FILE_CHOOSER_ERROR,
7688 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7689 _("Shortcut %s does not exist"),
7697 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7699 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7705 if (impl->num_shortcuts == 0)
7708 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7709 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7710 g_assert_not_reached ();
7714 for (i = 0; i < impl->num_shortcuts; i++)
7717 ShortcutType shortcut_type;
7720 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7721 SHORTCUTS_COL_DATA, &col_data,
7722 SHORTCUTS_COL_TYPE, &shortcut_type,
7724 g_assert (col_data != NULL);
7725 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7727 shortcut = col_data;
7728 list = g_slist_prepend (list, g_object_ref (shortcut));
7730 if (i != impl->num_shortcuts - 1)
7732 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7733 g_assert_not_reached ();
7737 return g_slist_reverse (list);
7740 /* Guesses a size based upon font sizes */
7742 find_good_size_from_style (GtkWidget *widget,
7746 GtkFileChooserDefault *impl;
7751 g_assert (widget->style != NULL);
7752 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7754 if (impl->default_width == 0 &&
7755 impl->default_height == 0)
7757 screen = gtk_widget_get_screen (widget);
7760 resolution = gdk_screen_get_resolution (screen);
7761 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7765 resolution = 96.0; /* wheeee */
7767 font_size = pango_font_description_get_size (widget->style->font_desc);
7768 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7770 impl->default_width = font_size * NUM_CHARS;
7771 impl->default_height = font_size * NUM_LINES;
7774 *width = impl->default_width;
7775 *height = impl->default_height;
7779 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7780 gint *default_width,
7781 gint *default_height)
7783 GtkFileChooserDefault *impl;
7786 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7787 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7789 if (impl->preview_widget_active &&
7790 impl->preview_widget &&
7791 GTK_WIDGET_VISIBLE (impl->preview_widget))
7793 gtk_widget_size_request (impl->preview_box, &req);
7794 *default_width += PREVIEW_HBOX_SPACING + req.width;
7797 if (impl->extra_widget &&
7798 GTK_WIDGET_VISIBLE (impl->extra_widget))
7800 gtk_widget_size_request (impl->extra_align, &req);
7801 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7805 struct switch_folder_closure {
7806 GtkFileChooserDefault *impl;
7811 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7813 switch_folder_foreach_cb (GtkTreeModel *model,
7818 struct switch_folder_closure *closure;
7819 GtkTreeIter child_iter;
7823 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7825 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
7826 closure->num_selected++;
7829 /* Changes to the selected folder in the list view */
7831 switch_to_selected_folder (GtkFileChooserDefault *impl)
7833 GtkTreeSelection *selection;
7834 struct switch_folder_closure closure;
7836 /* We do this with foreach() rather than get_selected() as we may be in
7837 * multiple selection mode
7840 closure.impl = impl;
7841 closure.file = NULL;
7842 closure.num_selected = 0;
7844 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7845 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7847 g_assert (closure.file && closure.num_selected == 1);
7849 change_folder_and_display_error (impl, closure.file, FALSE);
7852 /* Gets the GFileInfo for the selected row in the file list; assumes single
7856 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7857 gboolean *had_selection)
7859 GtkTreeSelection *selection;
7860 GtkTreeIter iter, child_iter;
7863 g_assert (!impl->select_multiple);
7864 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7865 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7867 *had_selection = FALSE;
7871 *had_selection = TRUE;
7873 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7877 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7881 /* Gets the display name of the selected file in the file list; assumes single
7882 * selection mode and that something is selected.
7884 static const gchar *
7885 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7888 gboolean had_selection;
7890 info = get_selected_file_info_from_file_list (impl, &had_selection);
7891 g_assert (had_selection);
7892 g_assert (info != NULL);
7894 return g_file_info_get_display_name (info);
7898 add_custom_button_to_dialog (GtkDialog *dialog,
7899 const gchar *mnemonic_label,
7900 const gchar *stock_id,
7905 button = gtk_button_new_with_mnemonic (mnemonic_label);
7906 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7907 gtk_button_set_image (GTK_BUTTON (button),
7908 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7909 gtk_widget_show (button);
7911 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7914 /* Presents an overwrite confirmation dialog; returns whether we should accept
7918 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7919 const gchar *file_part,
7920 const gchar *folder_display_name)
7922 GtkWindow *toplevel;
7926 toplevel = get_toplevel (GTK_WIDGET (impl));
7928 dialog = gtk_message_dialog_new (toplevel,
7929 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7930 GTK_MESSAGE_QUESTION,
7932 _("A file named \"%s\" already exists. Do you want to replace it?"),
7934 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7935 _("The file already exists in \"%s\". Replacing it will "
7936 "overwrite its contents."),
7937 folder_display_name);
7939 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7940 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
7941 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7942 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
7943 GTK_RESPONSE_ACCEPT,
7944 GTK_RESPONSE_CANCEL,
7946 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7948 if (toplevel->group)
7949 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7951 response = gtk_dialog_run (GTK_DIALOG (dialog));
7953 gtk_widget_destroy (dialog);
7955 return (response == GTK_RESPONSE_ACCEPT);
7958 struct GetDisplayNameData
7960 GtkFileChooserDefault *impl;
7965 confirmation_confirm_get_info_cb (GCancellable *cancellable,
7967 const GError *error,
7970 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7971 gboolean should_respond = FALSE;
7972 struct GetDisplayNameData *data = user_data;
7974 if (cancellable != data->impl->should_respond_get_info_cancellable)
7977 data->impl->should_respond_get_info_cancellable = NULL;
7983 /* Huh? Did the folder disappear? Let the caller deal with it */
7984 should_respond = TRUE;
7986 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
7988 set_busy_cursor (data->impl, FALSE);
7990 g_signal_emit_by_name (data->impl, "response-requested");
7993 g_object_unref (data->impl);
7994 g_free (data->file_part);
7997 g_object_unref (cancellable);
8000 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8001 * should respond. Can get the file part from the file list or the save entry.
8004 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8005 const gchar *file_part,
8008 GtkFileChooserConfirmation conf;
8010 if (!impl->do_overwrite_confirmation)
8013 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8015 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8019 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8021 struct GetDisplayNameData *data;
8023 g_assert (file_part != NULL);
8025 data = g_new0 (struct GetDisplayNameData, 1);
8026 data->impl = g_object_ref (impl);
8027 data->file_part = g_strdup (file_part);
8029 if (impl->should_respond_get_info_cancellable)
8030 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8032 impl->should_respond_get_info_cancellable =
8033 _gtk_file_system_get_info (impl->file_system, parent_file,
8034 "standard::display-name",
8035 confirmation_confirm_get_info_cb,
8037 set_busy_cursor (data->impl, TRUE);
8041 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8044 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8048 g_assert_not_reached ();
8053 struct FileExistsData
8055 GtkFileChooserDefault *impl;
8056 gboolean file_exists_and_is_not_folder;
8062 save_entry_get_info_cb (GCancellable *cancellable,
8064 const GError *error,
8067 gboolean parent_is_folder;
8068 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8069 struct FileExistsData *data = user_data;
8071 if (cancellable != data->impl->should_respond_get_info_cancellable)
8074 data->impl->should_respond_get_info_cancellable = NULL;
8076 set_busy_cursor (data->impl, FALSE);
8082 parent_is_folder = FALSE;
8084 parent_is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8086 if (parent_is_folder)
8088 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8090 if (data->file_exists_and_is_not_folder)
8093 const char *file_part;
8095 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8096 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8099 g_signal_emit_by_name (data->impl, "response-requested");
8102 g_signal_emit_by_name (data->impl, "response-requested");
8104 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8106 GError *error = NULL;
8108 set_busy_cursor (data->impl, TRUE);
8109 g_file_make_directory (data->file, NULL, &error);
8110 set_busy_cursor (data->impl, FALSE);
8113 g_signal_emit_by_name (data->impl, "response-requested");
8115 error_creating_folder_dialog (data->impl, data->file, error);
8120 /* This will display an error, which is what we want */
8121 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8125 g_object_unref (data->impl);
8126 g_object_unref (data->file);
8127 g_object_unref (data->parent_file);
8130 g_object_unref (cancellable);
8134 file_exists_get_info_cb (GCancellable *cancellable,
8136 const GError *error,
8139 gboolean data_ownership_taken = FALSE;
8140 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8141 gboolean file_exists_and_is_not_folder;
8142 struct FileExistsData *data = user_data;
8144 if (cancellable != data->impl->file_exists_get_info_cancellable)
8147 data->impl->file_exists_get_info_cancellable = NULL;
8149 set_busy_cursor (data->impl, FALSE);
8154 file_exists_and_is_not_folder = info && (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY);
8156 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8157 /* user typed a filename; we are done */
8158 g_signal_emit_by_name (data->impl, "response-requested");
8159 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8160 && file_exists_and_is_not_folder)
8162 /* Oops, the user typed the name of an existing path which is not
8165 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8166 g_error_copy (error));
8170 /* check that everything up to the last component exists */
8172 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8173 data_ownership_taken = TRUE;
8175 if (data->impl->should_respond_get_info_cancellable)
8176 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8178 data->impl->should_respond_get_info_cancellable =
8179 _gtk_file_system_get_info (data->impl->file_system,
8182 save_entry_get_info_cb,
8184 set_busy_cursor (data->impl, TRUE);
8188 if (!data_ownership_taken)
8190 g_object_unref (data->impl);
8191 g_object_unref (data->file);
8192 g_object_unref (data->parent_file);
8196 g_object_unref (cancellable);
8200 paste_text_received (GtkClipboard *clipboard,
8202 GtkFileChooserDefault *impl)
8209 file = g_file_new_for_uri (text);
8211 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8212 location_popup_handler (impl, text);
8214 g_object_unref (file);
8217 /* Handler for the "location-popup-on-paste" keybinding signal */
8219 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8221 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8222 GDK_SELECTION_CLIPBOARD);
8223 gtk_clipboard_request_text (clipboard,
8224 (GtkClipboardTextReceivedFunc) paste_text_received,
8229 /* Implementation for GtkFileChooserEmbed::should_respond() */
8231 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8233 GtkFileChooserDefault *impl;
8234 GtkWidget *toplevel;
8235 GtkWidget *current_focus;
8237 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8239 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8240 g_assert (GTK_IS_WINDOW (toplevel));
8242 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8244 if (current_focus == impl->browse_files_tree_view)
8246 /* The following array encodes what we do based on the impl->action and the
8247 * number of files selected.
8250 NOOP, /* Do nothing (don't respond) */
8251 RESPOND, /* Respond immediately */
8252 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8253 ALL_FILES, /* Respond only if everything selected is a file */
8254 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8255 SAVE_ENTRY, /* Go to the code for handling the save entry */
8256 NOT_REACHED /* Sanity check */
8258 static const ActionToTake what_to_do[4][3] = {
8259 /* 0 selected 1 selected many selected */
8260 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8261 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8262 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8263 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8267 gboolean all_files, all_folders;
8269 ActionToTake action;
8273 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8275 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8276 return search_should_respond (impl);
8278 if (impl->operation_mode == OPERATION_MODE_RECENT)
8279 return recent_should_respond (impl);
8281 selection_check (impl, &num_selected, &all_files, &all_folders);
8283 if (num_selected > 2)
8288 action = what_to_do [impl->action] [k];
8298 case RESPOND_OR_SWITCH:
8299 g_assert (num_selected == 1);
8303 switch_to_selected_folder (impl);
8306 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8307 return should_respond_after_confirm_overwrite (impl,
8308 get_display_name_from_file_list (impl),
8309 impl->current_folder);
8323 g_assert_not_reached ();
8326 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8329 gboolean is_well_formed, is_empty, is_file_part_empty;
8332 GtkFileChooserEntry *entry;
8337 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8338 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8339 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8340 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8341 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8343 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8344 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8346 if (is_empty || !is_well_formed)
8349 g_assert (file != NULL);
8354 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8355 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8357 change_folder_and_display_error (impl, file, TRUE);
8360 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8361 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8363 /* The folder already exists, so we do not need to create it.
8364 * Just respond to terminate the dialog.
8370 g_assert_not_reached ();
8376 struct FileExistsData *data;
8378 /* We need to check whether file exists and is not a folder */
8380 data = g_new0 (struct FileExistsData, 1);
8381 data->impl = g_object_ref (impl);
8382 data->file = g_object_ref (file);
8383 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8385 if (impl->file_exists_get_info_cancellable)
8386 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8388 impl->file_exists_get_info_cancellable =
8389 _gtk_file_system_get_info (impl->file_system, file,
8391 file_exists_get_info_cb,
8394 set_busy_cursor (impl, TRUE);
8398 g_error_free (error);
8401 g_object_unref (file);
8404 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8406 /* The focus is on a dialog's action area button, *and* the widget that
8407 * was focused immediately before it is the file list.
8411 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8413 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8416 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8418 /* The focus is on a dialog's action area button, *and* the widget that
8419 * was focused immediately before it is the location entry.
8424 /* The focus is on a dialog's action area button or something else */
8425 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8426 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8431 g_assert_not_reached ();
8435 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8437 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8439 GtkFileChooserDefault *impl;
8442 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8444 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8445 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8447 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8448 widget = impl->browse_files_tree_view;
8450 widget = impl->location_entry;
8452 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8453 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8454 widget = impl->location_entry;
8457 g_assert_not_reached ();
8461 g_assert (widget != NULL);
8462 gtk_widget_grab_focus (widget);
8465 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8467 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8477 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8478 *list = g_slist_prepend (*list, file);
8481 /* Constructs a list of the selected paths in search mode */
8483 search_get_selected_files (GtkFileChooserDefault *impl)
8486 GtkTreeSelection *selection;
8490 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8491 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8492 result = g_slist_reverse (result);
8497 /* Called from ::should_respond(). We return whether there are selected files
8498 * in the search list.
8501 search_should_respond (GtkFileChooserDefault *impl)
8503 GtkTreeSelection *selection;
8505 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8507 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8508 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8511 struct SearchHitInsertRequest
8513 GtkFileChooserDefault *impl;
8515 GtkTreeRowReference *row_ref;
8519 search_hit_get_info_cb (GCancellable *cancellable,
8521 const GError *error,
8524 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8525 GdkPixbuf *pixbuf = NULL;
8528 GCancellable *model_cancellable;
8529 gboolean is_folder = FALSE;
8532 struct SearchHitInsertRequest *request = data;
8534 if (!request->impl->search_model)
8537 path = gtk_tree_row_reference_get_path (request->row_ref);
8541 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8543 gtk_tree_path_free (path);
8545 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8546 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
8548 if (cancellable != model_cancellable)
8551 /* set the cancellable to NULL in the model */
8552 gtk_list_store_set (request->impl->search_model, &iter,
8553 SEARCH_MODEL_COL_CANCELLABLE, NULL,
8561 gtk_list_store_remove (request->impl->search_model, &iter);
8565 display_name = g_strdup (g_file_info_get_display_name (info));
8566 mime_type = g_strdup (g_file_info_get_content_type (info));
8567 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
8568 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8569 request->impl->icon_size);
8571 gtk_list_store_set (request->impl->search_model, &iter,
8572 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8573 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8574 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8575 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8579 g_object_unref (pixbuf);
8582 g_object_unref (request->impl);
8583 g_object_unref (request->file);
8584 gtk_tree_row_reference_free (request->row_ref);
8587 g_object_unref (cancellable);
8590 /* Adds one hit from the search engine to the search_model */
8592 search_add_hit (GtkFileChooserDefault *impl,
8598 char *collation_key;
8599 struct stat statbuf;
8600 struct stat *statbuf_copy;
8603 GCancellable *cancellable;
8604 struct SearchHitInsertRequest *request;
8606 file = g_file_new_for_uri (uri);
8608 if (!g_file_is_native (file))
8610 g_object_unref (file);
8614 filename = g_file_get_path (file);
8616 if (stat (filename, &statbuf) != 0)
8618 g_object_unref (file);
8623 statbuf_copy = g_new (struct stat, 1);
8624 *statbuf_copy = statbuf;
8626 tmp = g_file_get_parse_name (file);
8627 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8630 request = g_new0 (struct SearchHitInsertRequest, 1);
8631 request->impl = g_object_ref (impl);
8632 request->file = g_object_ref (file);
8634 gtk_list_store_append (impl->search_model, &iter);
8635 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8637 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8638 gtk_tree_path_free (p);
8640 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8641 "standard::type,standard::icon,"
8642 "standard::content-type,standard::display-name",
8643 search_hit_get_info_cb,
8646 gtk_list_store_set (impl->search_model, &iter,
8647 SEARCH_MODEL_COL_FILE, file,
8648 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8649 SEARCH_MODEL_COL_STAT, statbuf_copy,
8650 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
8653 g_object_unref (file);
8657 /* Callback used from GtkSearchEngine when we get new hits */
8659 search_engine_hits_added_cb (GtkSearchEngine *engine,
8663 GtkFileChooserDefault *impl;
8666 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8668 for (l = hits; l; l = l->next)
8669 search_add_hit (impl, (gchar*)l->data);
8672 /* Callback used from GtkSearchEngine when the query is done running */
8674 search_engine_finished_cb (GtkSearchEngine *engine,
8677 GtkFileChooserDefault *impl;
8679 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8682 /* EB: setting the model here will avoid loads of row events,
8683 * but it'll make the search look like blocked.
8685 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8686 GTK_TREE_MODEL (impl->search_model_filter));
8689 /* FMQ: if search was empty, say that we got no hits */
8690 set_busy_cursor (impl, FALSE);
8693 /* Displays a generic error when we cannot create a GtkSearchEngine.
8694 * It would be better if _gtk_search_engine_new() gave us a GError
8695 * with a better message, but it doesn't do that right now.
8698 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8700 error_message (impl,
8701 _("Could not start the search process"),
8702 _("The program was not able to create a connection to the indexer "
8703 "daemon. Please make sure it is running."));
8707 search_engine_error_cb (GtkSearchEngine *engine,
8708 const gchar *message,
8711 GtkFileChooserDefault *impl;
8713 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8715 search_stop_searching (impl, TRUE);
8716 error_message (impl, _("Could not send the search request"), message);
8718 set_busy_cursor (impl, FALSE);
8721 /* Frees the data in the search_model */
8723 search_clear_model (GtkFileChooserDefault *impl,
8724 gboolean remove_from_treeview)
8726 GtkTreeModel *model;
8729 if (!impl->search_model)
8732 model = GTK_TREE_MODEL (impl->search_model);
8734 if (gtk_tree_model_get_iter_first (model, &iter))
8737 GCancellable *cancellable;
8739 gtk_tree_model_get (model, &iter,
8740 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
8744 g_cancellable_cancel (cancellable);
8746 while (gtk_tree_model_iter_next (model, &iter));
8748 g_object_unref (impl->search_model);
8749 impl->search_model = NULL;
8751 g_object_unref (impl->search_model_filter);
8752 impl->search_model_filter = NULL;
8754 g_object_unref (impl->search_model_sort);
8755 impl->search_model_sort = NULL;
8757 if (remove_from_treeview)
8758 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8761 /* Stops any ongoing searches; does not touch the search_model */
8763 search_stop_searching (GtkFileChooserDefault *impl,
8764 gboolean remove_query)
8766 if (remove_query && impl->search_query)
8768 g_object_unref (impl->search_query);
8769 impl->search_query = NULL;
8772 if (impl->search_engine)
8774 _gtk_search_engine_stop (impl->search_engine);
8776 g_object_unref (impl->search_engine);
8777 impl->search_engine = NULL;
8781 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8783 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8785 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8787 search_stop_searching (impl, FALSE);
8788 search_clear_model (impl, TRUE);
8790 gtk_widget_destroy (impl->search_hbox);
8791 impl->search_hbox = NULL;
8792 impl->search_entry = NULL;
8794 gtk_widget_show (impl->browse_path_bar);
8795 gtk_widget_show (impl->browse_new_folder_button);
8797 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8798 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8800 gtk_widget_show (impl->location_button);
8802 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8803 gtk_widget_show (impl->location_entry_box);
8806 impl->operation_mode = OPERATION_MODE_BROWSE;
8808 file_list_set_sort_column_ids (impl);
8811 /* Sort callback from the path column */
8813 search_column_path_sort_func (GtkTreeModel *model,
8818 GtkFileChooserDefault *impl = user_data;
8819 GtkTreeIter child_a, child_b;
8820 const char *collation_key_a, *collation_key_b;
8821 gboolean is_folder_a, is_folder_b;
8823 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8824 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8826 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8827 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8828 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
8830 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8831 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8832 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
8835 if (!collation_key_a)
8838 if (!collation_key_b)
8841 /* always show folders first */
8842 if (is_folder_a != is_folder_b)
8843 return is_folder_a ? 1 : -1;
8845 return strcmp (collation_key_a, collation_key_b);
8848 /* Sort callback from the modification time column */
8850 search_column_mtime_sort_func (GtkTreeModel *model,
8855 GtkFileChooserDefault *impl = user_data;
8856 GtkTreeIter child_a, child_b;
8857 const struct stat *statbuf_a, *statbuf_b;
8858 gboolean is_folder_a, is_folder_b;
8860 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
8861 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
8863 /* Note that although we store a whole struct stat in the model, we only
8864 * compare the mtime here. If we add another column relative to a struct stat
8865 * (e.g. a file size column), we'll want another sort callback similar to this
8868 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
8869 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
8870 SEARCH_MODEL_COL_STAT, &statbuf_a,
8872 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
8873 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
8874 SEARCH_MODEL_COL_STAT, &statbuf_b,
8883 if (is_folder_a != is_folder_b)
8884 return is_folder_a ? 1 : -1;
8886 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
8888 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
8895 search_get_is_filtered (GtkFileChooserDefault *impl,
8897 const gchar *display_name,
8898 const gchar *mime_type)
8900 GtkFileFilterInfo filter_info;
8901 GtkFileFilterFlags needed;
8904 if (!impl->current_filter)
8907 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
8908 needed = gtk_file_filter_get_needed (impl->current_filter);
8910 filter_info.display_name = display_name;
8911 filter_info.mime_type = mime_type;
8913 if (needed & GTK_FILE_FILTER_FILENAME)
8915 filter_info.filename = g_file_get_path (file);
8916 if (filter_info.filename)
8917 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
8920 filter_info.filename = NULL;
8922 if (needed & GTK_FILE_FILTER_URI)
8924 filter_info.uri = g_file_get_uri (file);
8925 if (filter_info.uri)
8926 filter_info.contains |= GTK_FILE_FILTER_URI;
8929 filter_info.uri = NULL;
8931 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
8933 if (filter_info.filename)
8934 g_free ((gchar *) filter_info.filename);
8935 if (filter_info.uri)
8936 g_free ((gchar *) filter_info.uri);
8942 /* Visibility function for the recent filter model */
8944 search_model_visible_func (GtkTreeModel *model,
8948 GtkFileChooserDefault *impl = user_data;
8950 gchar *display_name, *mime_type;
8953 if (!impl->current_filter)
8956 gtk_tree_model_get (model, iter,
8957 SEARCH_MODEL_COL_FILE, &file,
8958 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
8959 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8960 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
8969 return !search_get_is_filtered (impl, file, display_name, mime_type);
8972 /* Creates the search_model and puts it in the tree view */
8974 search_setup_model (GtkFileChooserDefault *impl)
8976 g_assert (impl->search_model == NULL);
8977 g_assert (impl->search_model_filter == NULL);
8978 g_assert (impl->search_model_sort == NULL);
8980 /* We store these columns in the search model:
8982 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
8983 * as a pointer not as a G_TYPE_FILE
8984 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
8985 * as a pointer not as a G_TYPE_STRING
8986 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
8987 * as a pointer not as a G_TYPE_STRING
8988 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
8989 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
8990 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
8991 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
8992 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
8994 * Keep this in sync with the enumeration defined near the beginning
8997 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9007 impl->search_model_filter =
9008 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9009 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9010 search_model_visible_func,
9013 impl->search_model_sort =
9014 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9015 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9016 SEARCH_MODEL_COL_FILE,
9017 search_column_path_sort_func,
9019 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9020 SEARCH_MODEL_COL_STAT,
9021 search_column_mtime_sort_func,
9023 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9024 SEARCH_MODEL_COL_STAT,
9025 GTK_SORT_DESCENDING);
9027 /* EB: setting the model here will make the hits list update feel
9028 * more "alive" than setting the model at the end of the search
9031 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9032 GTK_TREE_MODEL (impl->search_model_sort));
9036 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9037 GtkTreeIter *child_iter,
9042 if (!impl->search_model)
9045 if (!impl->search_model_filter || !impl->search_model_sort)
9048 /* pass 1: get the iterator in the filter model */
9049 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9052 /* pass 2: get the iterator in the real model */
9053 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9054 child_iter, &middle);
9057 /* Creates a new query with the specified text and launches it */
9059 search_start_query (GtkFileChooserDefault *impl,
9060 const gchar *query_text)
9062 search_stop_searching (impl, FALSE);
9063 search_clear_model (impl, TRUE);
9064 search_setup_model (impl);
9065 set_busy_cursor (impl, TRUE);
9067 if (impl->search_engine == NULL)
9068 impl->search_engine = _gtk_search_engine_new ();
9070 if (!impl->search_engine)
9072 set_busy_cursor (impl, FALSE);
9073 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9077 if (!impl->search_query)
9079 impl->search_query = _gtk_query_new ();
9080 _gtk_query_set_text (impl->search_query, query_text);
9083 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9085 g_signal_connect (impl->search_engine, "hits-added",
9086 G_CALLBACK (search_engine_hits_added_cb), impl);
9087 g_signal_connect (impl->search_engine, "finished",
9088 G_CALLBACK (search_engine_finished_cb), impl);
9089 g_signal_connect (impl->search_engine, "error",
9090 G_CALLBACK (search_engine_error_cb), impl);
9092 _gtk_search_engine_start (impl->search_engine);
9095 /* Callback used when the user presses Enter while typing on the search
9096 * entry; starts the query
9099 search_entry_activate_cb (GtkEntry *entry,
9102 GtkFileChooserDefault *impl;
9105 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9107 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9108 if (strlen (text) == 0)
9111 /* reset any existing query object */
9112 if (impl->search_query)
9114 g_object_unref (impl->search_query);
9115 impl->search_query = NULL;
9118 search_start_query (impl, text);
9121 /* Hides the path bar and creates the search entry */
9123 search_setup_widgets (GtkFileChooserDefault *impl)
9127 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9131 label = gtk_label_new_with_mnemonic (_("_Search:"));
9132 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9136 impl->search_entry = gtk_entry_new ();
9137 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9138 g_signal_connect (impl->search_entry, "activate",
9139 G_CALLBACK (search_entry_activate_cb),
9141 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9143 /* if there already is a query, restart it */
9144 if (impl->search_query)
9146 gchar *query = _gtk_query_get_text (impl->search_query);
9150 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9151 search_start_query (impl, query);
9157 g_object_unref (impl->search_query);
9158 impl->search_query = NULL;
9162 gtk_widget_hide (impl->browse_path_bar);
9163 gtk_widget_hide (impl->browse_new_folder_button);
9165 /* Box for search widgets */
9166 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9167 gtk_widget_show_all (impl->search_hbox);
9169 /* Hide the location widgets temporarily */
9171 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9172 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9174 gtk_widget_hide (impl->location_button);
9175 gtk_widget_hide (impl->location_entry_box);
9178 gtk_widget_grab_focus (impl->search_entry);
9180 /* FMQ: hide the filter combo? */
9183 /* Main entry point to the searching functions; this gets called when the user
9184 * activates the Search shortcut.
9187 search_activate (GtkFileChooserDefault *impl)
9189 OperationMode previous_mode;
9191 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9193 gtk_widget_grab_focus (impl->search_entry);
9197 previous_mode = impl->operation_mode;
9198 impl->operation_mode = OPERATION_MODE_SEARCH;
9200 switch (previous_mode)
9202 case OPERATION_MODE_RECENT:
9203 recent_stop_loading (impl);
9204 recent_clear_model (impl, TRUE);
9207 case OPERATION_MODE_BROWSE:
9208 stop_loading_and_clear_list_model (impl);
9211 case OPERATION_MODE_SEARCH:
9212 g_assert_not_reached ();
9216 g_assert (impl->search_hbox == NULL);
9217 g_assert (impl->search_entry == NULL);
9218 g_assert (impl->search_model == NULL);
9219 g_assert (impl->search_model_filter == NULL);
9221 search_setup_widgets (impl);
9222 file_list_set_sort_column_ids (impl);
9226 * Recent files support
9229 /* Frees the data in the recent_model */
9231 recent_clear_model (GtkFileChooserDefault *impl,
9232 gboolean remove_from_treeview)
9234 GtkTreeModel *model;
9237 if (!impl->recent_model)
9240 model = GTK_TREE_MODEL (impl->recent_model);
9242 if (remove_from_treeview)
9243 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9245 if (gtk_tree_model_get_iter_first (model, &iter))
9250 GCancellable *cancellable;
9251 GtkRecentInfo *recent_info;
9252 gchar *display_name;
9254 gtk_tree_model_get (model, &iter,
9255 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9256 RECENT_MODEL_COL_FILE, &file,
9257 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9258 RECENT_MODEL_COL_INFO, &recent_info,
9262 g_cancellable_cancel (cancellable);
9264 g_object_unref (file);
9265 gtk_recent_info_unref (recent_info);
9266 g_free (display_name);
9268 while (gtk_tree_model_iter_next (model, &iter));
9271 g_object_unref (impl->recent_model);
9272 impl->recent_model = NULL;
9274 g_object_unref (impl->recent_model_filter);
9275 impl->recent_model_filter = NULL;
9277 g_object_unref (impl->recent_model_sort);
9278 impl->recent_model_sort = NULL;
9281 /* Stops any ongoing loading of the recent files list; does
9282 * not touch the recent_model
9285 recent_stop_loading (GtkFileChooserDefault *impl)
9287 if (impl->load_recent_id)
9289 g_source_remove (impl->load_recent_id);
9290 impl->load_recent_id = 0;
9294 /* Stops any pending load, clears the file list, and switches
9295 * back to OPERATION_MODE_BROWSE
9298 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9300 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9302 recent_stop_loading (impl);
9303 recent_clear_model (impl, TRUE);
9305 gtk_widget_show (impl->browse_path_bar);
9306 gtk_widget_show (impl->browse_new_folder_button);
9308 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9309 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9311 gtk_widget_show (impl->location_button);
9313 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9314 gtk_widget_show (impl->location_entry_box);
9317 impl->operation_mode = OPERATION_MODE_BROWSE;
9319 file_list_set_sort_column_ids (impl);
9322 /* Sort callback from the modification time column */
9324 recent_column_mtime_sort_func (GtkTreeModel *model,
9329 GtkFileChooserDefault *impl = user_data;
9330 GtkTreeIter child_a, child_b;
9331 GtkRecentInfo *info_a, *info_b;
9332 gboolean is_folder_a, is_folder_b;
9334 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9335 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9337 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9338 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9339 RECENT_MODEL_COL_INFO, &info_a,
9341 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9342 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9343 RECENT_MODEL_COL_INFO, &info_b,
9352 /* folders always go first */
9353 if (is_folder_a != is_folder_b)
9354 return is_folder_a ? 1 : -1;
9356 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9358 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9365 recent_column_path_sort_func (GtkTreeModel *model,
9370 GtkFileChooserDefault *impl = user_data;
9371 GtkTreeIter child_a, child_b;
9372 gboolean is_folder_a, is_folder_b;
9373 gchar *name_a, *name_b;
9375 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9376 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9378 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9379 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9380 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9382 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9383 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9384 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9393 if (is_folder_a != is_folder_b)
9394 return is_folder_a ? 1 : -1;
9396 return strcmp (name_a, name_b);
9400 recent_get_is_filtered (GtkFileChooserDefault *impl,
9402 GtkRecentInfo *recent_info)
9404 GtkFileFilterInfo filter_info;
9405 GtkFileFilterFlags needed;
9408 if (!impl->current_filter)
9411 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9412 needed = gtk_file_filter_get_needed (impl->current_filter);
9414 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9415 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9417 if (needed & GTK_FILE_FILTER_FILENAME)
9419 filter_info.filename = g_file_get_path (file);
9420 if (filter_info.filename)
9421 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9424 filter_info.filename = NULL;
9426 if (needed & GTK_FILE_FILTER_URI)
9428 filter_info.uri = g_file_get_uri (file);
9429 if (filter_info.uri)
9430 filter_info.contains |= GTK_FILE_FILTER_URI;
9433 filter_info.uri = NULL;
9435 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9437 if (filter_info.filename)
9438 g_free ((gchar *) filter_info.filename);
9439 if (filter_info.uri)
9440 g_free ((gchar *) filter_info.uri);
9445 /* Visibility function for the recent filter model */
9447 recent_model_visible_func (GtkTreeModel *model,
9451 GtkFileChooserDefault *impl = user_data;
9453 GtkRecentInfo *recent_info;
9456 if (!impl->current_filter)
9459 gtk_tree_model_get (model, iter,
9460 RECENT_MODEL_COL_INFO, &recent_info,
9461 RECENT_MODEL_COL_FILE, &file,
9462 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9471 return !recent_get_is_filtered (impl, file, recent_info);
9475 recent_setup_model (GtkFileChooserDefault *impl)
9477 g_assert (impl->recent_model == NULL);
9478 g_assert (impl->recent_model_filter == NULL);
9479 g_assert (impl->recent_model_sort == NULL);
9481 /* We store these columns in the search model:
9483 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
9484 * stored as a pointer and not as a G_TYPE_FILE;
9485 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9486 * stored as a pointer and not as a G_TYPE_STRING;
9487 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9488 * as a GTK_TYPE_RECENT_INFO;
9489 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9490 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
9491 * and not as a G_TYPE_CANCELLABLE;
9493 * Keep this in sync with the enumeration defined near the beginning of
9496 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9503 impl->recent_model_filter =
9504 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9505 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9506 recent_model_visible_func,
9510 /* this is the model that will actually be added to
9511 * the browse_files_tree_view widget; remember: we are
9512 * stuffing the real model into a filter model and then
9513 * into a sort model; this means we'll have to translate
9514 * the child iterator *twice* to get from a path or an
9515 * iterator coming from the tree view widget to the
9516 * real data inside the model.
9518 impl->recent_model_sort =
9519 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9520 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9521 RECENT_MODEL_COL_FILE,
9522 recent_column_path_sort_func,
9524 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9525 RECENT_MODEL_COL_INFO,
9526 recent_column_mtime_sort_func,
9528 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9529 RECENT_MODEL_COL_INFO,
9530 GTK_SORT_DESCENDING);
9535 GtkFileChooserDefault *impl;
9538 gint n_loaded_items;
9539 guint needs_sorting : 1;
9543 recent_idle_cleanup (gpointer data)
9545 RecentLoadData *load_data = data;
9546 GtkFileChooserDefault *impl = load_data->impl;
9548 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9549 GTK_TREE_MODEL (impl->recent_model_sort));
9551 set_busy_cursor (impl, FALSE);
9553 impl->load_recent_id = 0;
9555 if (load_data->items)
9557 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9558 g_list_free (load_data->items);
9564 struct RecentItemInsertRequest
9566 GtkFileChooserDefault *impl;
9568 GtkTreeRowReference *row_ref;
9572 recent_item_get_info_cb (GCancellable *cancellable,
9574 const GError *error,
9577 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9580 GCancellable *model_cancellable;
9581 gboolean is_folder = FALSE;
9582 struct RecentItemInsertRequest *request = data;
9584 if (!request->impl->recent_model)
9587 path = gtk_tree_row_reference_get_path (request->row_ref);
9591 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9593 gtk_tree_path_free (path);
9595 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9596 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
9598 if (cancellable != model_cancellable)
9601 gtk_list_store_set (request->impl->recent_model, &iter,
9602 RECENT_MODEL_COL_CANCELLABLE, NULL,
9610 gtk_list_store_remove (request->impl->recent_model, &iter);
9614 is_folder = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
9616 gtk_list_store_set (request->impl->recent_model, &iter,
9617 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9621 g_object_unref (request->impl);
9622 g_object_unref (request->file);
9623 gtk_tree_row_reference_free (request->row_ref);
9626 g_object_unref (cancellable);
9630 recent_sort_mru (gconstpointer a,
9633 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9634 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9636 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9640 get_recent_files_limit (GtkWidget *widget)
9642 GtkSettings *settings;
9645 if (gtk_widget_has_screen (widget))
9646 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9648 settings = gtk_settings_get_default ();
9650 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9656 recent_idle_load (gpointer data)
9658 RecentLoadData *load_data = data;
9659 GtkFileChooserDefault *impl = load_data->impl;
9662 GtkRecentInfo *info;
9663 const gchar *uri, *display_name;
9665 GCancellable *cancellable;
9666 struct RecentItemInsertRequest *request;
9668 if (!impl->recent_manager)
9671 /* first iteration: load all the items */
9672 if (!load_data->items)
9674 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9675 if (!load_data->items)
9678 load_data->needs_sorting = TRUE;
9683 /* second iteration: preliminary MRU sorting and clamping */
9684 if (load_data->needs_sorting)
9688 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9689 load_data->n_items = g_list_length (load_data->items);
9691 limit = get_recent_files_limit (GTK_WIDGET (impl));
9693 if (limit != -1 && (load_data->n_items > limit))
9697 clamp = g_list_nth (load_data->items, limit - 1);
9698 if (G_LIKELY (clamp))
9703 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9706 load_data->n_items = limit;
9710 load_data->n_loaded_items = 0;
9711 load_data->needs_sorting = FALSE;
9716 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9717 g_assert (info != NULL);
9719 uri = gtk_recent_info_get_uri (info);
9720 display_name = gtk_recent_info_get_display_name (info);
9721 file = g_file_new_for_uri (uri);
9723 gtk_list_store_append (impl->recent_model, &iter);
9724 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9726 request = g_new0 (struct RecentItemInsertRequest, 1);
9727 request->impl = g_object_ref (impl);
9728 request->file = g_object_ref (file);
9729 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9730 gtk_tree_path_free (p);
9732 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9734 recent_item_get_info_cb,
9737 gtk_list_store_set (impl->recent_model, &iter,
9738 RECENT_MODEL_COL_FILE, file,
9739 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9740 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9741 RECENT_MODEL_COL_CANCELLABLE, cancellable,
9744 load_data->n_loaded_items += 1;
9746 /* finished loading items */
9747 if (load_data->n_loaded_items == load_data->n_items)
9749 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9750 g_list_free (load_data->items);
9751 load_data->items = NULL;
9760 recent_start_loading (GtkFileChooserDefault *impl)
9762 RecentLoadData *load_data;
9764 recent_stop_loading (impl);
9765 recent_clear_model (impl, TRUE);
9766 recent_setup_model (impl);
9767 set_busy_cursor (impl, TRUE);
9769 g_assert (impl->load_recent_id == 0);
9771 load_data = g_new (RecentLoadData, 1);
9772 load_data->impl = impl;
9773 load_data->items = NULL;
9774 load_data->n_items = 0;
9775 load_data->n_loaded_items = 0;
9776 load_data->needs_sorting = TRUE;
9778 /* begin lazy loading the recent files into the model */
9779 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9782 recent_idle_cleanup);
9786 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9796 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
9797 *list = g_slist_prepend (*list, g_object_ref (file));
9800 /* Constructs a list of the selected paths in recent files mode */
9802 recent_get_selected_files (GtkFileChooserDefault *impl)
9805 GtkTreeSelection *selection;
9809 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9810 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9811 result = g_slist_reverse (result);
9816 /* Called from ::should_respond(). We return whether there are selected
9817 * files in the recent files list.
9820 recent_should_respond (GtkFileChooserDefault *impl)
9822 GtkTreeSelection *selection;
9824 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9826 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9827 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9830 /* Hide the location widgets temporarily */
9832 recent_hide_entry (GtkFileChooserDefault *impl)
9834 gtk_widget_hide (impl->browse_path_bar);
9835 gtk_widget_hide (impl->browse_new_folder_button);
9837 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9838 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9840 gtk_widget_hide (impl->location_button);
9841 gtk_widget_hide (impl->location_entry_box);
9845 /* Main entry point to the recent files functions; this gets called when
9846 * the user activates the Recently Used shortcut.
9849 recent_activate (GtkFileChooserDefault *impl)
9851 OperationMode previous_mode;
9853 if (impl->operation_mode == OPERATION_MODE_RECENT)
9856 previous_mode = impl->operation_mode;
9857 impl->operation_mode = OPERATION_MODE_RECENT;
9859 switch (previous_mode)
9861 case OPERATION_MODE_SEARCH:
9862 search_stop_searching (impl, FALSE);
9863 search_clear_model (impl, TRUE);
9865 gtk_widget_destroy (impl->search_hbox);
9866 impl->search_hbox = NULL;
9867 impl->search_entry = NULL;
9870 case OPERATION_MODE_BROWSE:
9871 stop_loading_and_clear_list_model (impl);
9874 case OPERATION_MODE_RECENT:
9875 g_assert_not_reached ();
9879 recent_hide_entry (impl);
9880 file_list_set_sort_column_ids (impl);
9881 recent_start_loading (impl);
9884 /* convert an iterator coming from the model bound to
9885 * browse_files_tree_view to an interator inside the
9889 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
9890 GtkTreeIter *child_iter,
9895 if (!impl->recent_model)
9898 if (!impl->recent_model_filter || !impl->recent_model_sort)
9901 /* pass 1: get the iterator in the filter model */
9902 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
9905 /* pass 2: get the iterator in the real model */
9906 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
9913 set_current_filter (GtkFileChooserDefault *impl,
9914 GtkFileFilter *filter)
9916 if (impl->current_filter != filter)
9920 /* NULL filters are allowed to reset to non-filtered status
9922 filter_index = g_slist_index (impl->filters, filter);
9923 if (impl->filters && filter && filter_index < 0)
9926 if (impl->current_filter)
9927 g_object_unref (impl->current_filter);
9928 impl->current_filter = filter;
9929 if (impl->current_filter)
9931 g_object_ref_sink (impl->current_filter);
9935 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9938 if (impl->browse_files_model)
9939 install_list_model_filter (impl);
9941 if (impl->search_model_filter)
9942 gtk_tree_model_filter_refilter (impl->search_model_filter);
9944 if (impl->recent_model_filter)
9945 gtk_tree_model_filter_refilter (impl->recent_model_filter);
9947 g_object_notify (G_OBJECT (impl), "filter");
9952 filter_combo_changed (GtkComboBox *combo_box,
9953 GtkFileChooserDefault *impl)
9955 gint new_index = gtk_combo_box_get_active (combo_box);
9956 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9958 set_current_filter (impl, new_filter);
9962 check_preview_change (GtkFileChooserDefault *impl)
9964 GtkTreePath *cursor_path;
9966 const char *new_display_name;
9968 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9970 new_display_name = NULL;
9973 GtkTreeIter child_iter;
9975 if (impl->operation_mode == OPERATION_MODE_BROWSE)
9977 if (impl->sort_model)
9980 GFileInfo *new_info;
9982 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
9983 gtk_tree_path_free (cursor_path);
9985 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
9987 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
9988 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
9990 new_display_name = g_file_info_get_display_name (new_info);
9993 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
9997 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
9998 &iter, cursor_path);
9999 gtk_tree_path_free (cursor_path);
10001 search_get_valid_child_iter (impl, &child_iter, &iter);
10002 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10003 SEARCH_MODEL_COL_FILE, &new_file,
10004 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10007 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10011 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10012 &iter, cursor_path);
10013 gtk_tree_path_free (cursor_path);
10015 recent_get_valid_child_iter (impl, &child_iter, &iter);
10016 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10017 RECENT_MODEL_COL_FILE, &new_file,
10018 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10023 if (new_file != impl->preview_file &&
10024 !(new_file && impl->preview_file &&
10025 g_file_equal (new_file, impl->preview_file)))
10027 if (impl->preview_file)
10029 g_object_unref (impl->preview_file);
10030 g_free (impl->preview_display_name);
10035 impl->preview_file = g_object_ref (new_file);
10036 impl->preview_display_name = g_strdup (new_display_name);
10040 impl->preview_file = NULL;
10041 impl->preview_display_name = NULL;
10044 if (impl->use_preview_label && impl->preview_label)
10045 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10047 g_signal_emit_by_name (impl, "update-preview");
10052 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10053 GtkFileSystemVolume *volume,
10054 const GError *error,
10058 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10059 GtkFileChooserDefault *impl = data;
10061 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10064 impl->shortcuts_activate_iter_cancellable = NULL;
10066 set_busy_cursor (impl, FALSE);
10073 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10077 name = _gtk_file_system_volume_get_display_name (volume);
10078 msg = g_strdup_printf (_("Could not mount %s"), name);
10080 error_message (impl, msg, error->message);
10089 file = _gtk_file_system_volume_get_root (volume);
10092 change_folder_and_display_error (impl, file, FALSE);
10093 g_object_unref (file);
10097 g_object_unref (impl);
10098 g_object_unref (cancellable);
10102 /* Activates a volume by mounting it if necessary and then switching to its
10106 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10107 GtkFileSystemVolume *volume)
10111 switch (impl->operation_mode)
10113 case OPERATION_MODE_BROWSE:
10115 case OPERATION_MODE_SEARCH:
10116 search_switch_to_browse_mode (impl);
10118 case OPERATION_MODE_RECENT:
10119 recent_switch_to_browse_mode (impl);
10123 /* We ref the file chooser since volume_mount() may run a main loop, and the
10124 * user could close the file chooser window in the meantime.
10126 g_object_ref (impl);
10128 if (!_gtk_file_system_volume_is_mounted (volume))
10130 set_busy_cursor (impl, TRUE);
10132 impl->shortcuts_activate_iter_cancellable =
10133 _gtk_file_system_mount_volume (impl->file_system, volume, NULL,
10134 shortcuts_activate_volume_mount_cb,
10135 g_object_ref (impl));
10139 file = _gtk_file_system_volume_get_root (volume);
10142 change_folder_and_display_error (impl, file, FALSE);
10143 g_object_unref (file);
10147 g_object_unref (impl);
10150 /* Opens the folder or volume at the specified iter in the shortcuts model */
10151 struct ShortcutsActivateData
10153 GtkFileChooserDefault *impl;
10158 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10160 const GError *error,
10161 gpointer user_data)
10163 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10164 struct ShortcutsActivateData *data = user_data;
10166 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10169 data->impl->shortcuts_activate_iter_cancellable = NULL;
10174 if (!error && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10175 change_folder_and_display_error (data->impl, data->file, FALSE);
10177 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10182 g_object_unref (data->impl);
10183 g_object_unref (data->file);
10186 g_object_unref (cancellable);
10190 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10191 GtkFileSystemVolume *volume,
10192 const GError *error,
10193 gpointer user_data)
10195 struct ShortcutsActivateData *data = user_data;
10199 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10201 g_object_unref (data->impl);
10202 g_object_unref (data->file);
10208 data->impl->shortcuts_activate_iter_cancellable =
10209 _gtk_file_system_get_info (data->impl->file_system, data->file,
10211 shortcuts_activate_get_info_cb, data);
10215 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10219 ShortcutType shortcut_type;
10221 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10222 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10223 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10224 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10226 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10227 SHORTCUTS_COL_DATA, &col_data,
10228 SHORTCUTS_COL_TYPE, &shortcut_type,
10231 if (impl->shortcuts_activate_iter_cancellable)
10233 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10234 impl->shortcuts_activate_iter_cancellable = NULL;
10237 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10239 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10241 GtkFileSystemVolume *volume;
10245 shortcuts_activate_volume (impl, volume);
10247 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10249 struct ShortcutsActivateData *data;
10250 GtkFileSystemVolume *volume;
10252 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10254 data = g_new0 (struct ShortcutsActivateData, 1);
10255 data->impl = g_object_ref (impl);
10256 data->file = g_object_ref (col_data);
10258 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10260 GMountOperation *mount_operation;
10261 GtkWidget *toplevel;
10263 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10265 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10267 impl->shortcuts_activate_iter_cancellable =
10268 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10270 shortcuts_activate_mount_enclosing_volume,
10275 impl->shortcuts_activate_iter_cancellable =
10276 _gtk_file_system_get_info (impl->file_system, data->file,
10278 shortcuts_activate_get_info_cb, data);
10281 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10283 search_activate (impl);
10285 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10287 recent_activate (impl);
10291 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10293 shortcuts_key_press_event_cb (GtkWidget *widget,
10294 GdkEventKey *event,
10295 GtkFileChooserDefault *impl)
10299 modifiers = gtk_accelerator_get_default_mod_mask ();
10301 if ((event->keyval == GDK_BackSpace
10302 || event->keyval == GDK_Delete
10303 || event->keyval == GDK_KP_Delete)
10304 && (event->state & modifiers) == 0)
10306 remove_selected_bookmarks (impl);
10310 if ((event->keyval == GDK_F2)
10311 && (event->state & modifiers) == 0)
10313 rename_selected_bookmark (impl);
10321 shortcuts_select_func (GtkTreeSelection *selection,
10322 GtkTreeModel *model,
10324 gboolean path_currently_selected,
10327 GtkFileChooserDefault *impl = data;
10328 GtkTreeIter filter_iter;
10329 ShortcutType shortcut_type;
10331 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10332 g_assert_not_reached ();
10334 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10336 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10340 list_select_func (GtkTreeSelection *selection,
10341 GtkTreeModel *model,
10343 gboolean path_currently_selected,
10346 GtkFileChooserDefault *impl = data;
10348 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10349 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10351 GtkTreeIter iter, child_iter;
10353 switch (impl->operation_mode)
10355 case OPERATION_MODE_SEARCH:
10357 gboolean is_folder;
10359 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10362 search_get_valid_child_iter (impl, &child_iter, &iter);
10363 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10364 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10371 case OPERATION_MODE_RECENT:
10373 gboolean is_folder;
10375 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10378 recent_get_valid_child_iter (impl, &child_iter, &iter);
10379 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10380 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10387 case OPERATION_MODE_BROWSE:
10391 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10394 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10395 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10396 if (info && g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
10407 list_selection_changed (GtkTreeSelection *selection,
10408 GtkFileChooserDefault *impl)
10410 /* See if we are in the new folder editable row for Save mode */
10411 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10412 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10415 gboolean had_selection;
10417 info = get_selected_file_info_from_file_list (impl, &had_selection);
10418 if (!had_selection)
10419 goto out; /* normal processing */
10422 return; /* We are on the editable row for New Folder */
10427 if (impl->location_entry)
10428 update_chooser_entry (impl);
10430 check_preview_change (impl);
10431 bookmarks_check_add_sensitivity (impl);
10433 g_signal_emit_by_name (impl, "selection-changed", 0);
10436 /* Callback used when a row in the file list is activated */
10438 list_row_activated (GtkTreeView *tree_view,
10440 GtkTreeViewColumn *column,
10441 GtkFileChooserDefault *impl)
10444 GtkTreeIter child_iter;
10446 switch (impl->operation_mode)
10448 case OPERATION_MODE_SEARCH:
10451 gboolean is_folder;
10453 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10456 search_get_valid_child_iter (impl, &child_iter, &iter);
10457 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10458 SEARCH_MODEL_COL_FILE, &file,
10459 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10464 change_folder_and_display_error (impl, file, FALSE);
10468 g_signal_emit_by_name (impl, "file-activated");
10472 case OPERATION_MODE_RECENT:
10475 gboolean is_folder;
10477 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10480 recent_get_valid_child_iter (impl, &child_iter, &iter);
10481 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10482 RECENT_MODEL_COL_FILE, &file,
10483 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10488 change_folder_and_display_error (impl, file, FALSE);
10492 g_signal_emit_by_name (impl, "file-activated");
10496 case OPERATION_MODE_BROWSE:
10500 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10503 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10504 &child_iter, &iter);
10505 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10508 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10512 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10513 change_folder_and_display_error (impl, file, FALSE);
10517 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10518 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10519 g_signal_emit_by_name (impl, "file-activated");
10526 path_bar_clicked (GtkPathBar *path_bar,
10529 gboolean child_is_hidden,
10530 GtkFileChooserDefault *impl)
10533 pending_select_files_add (impl, child_file);
10535 if (!change_folder_and_display_error (impl, file, FALSE))
10538 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10539 * show hidden files so that ".baz" appears in the file list, as it will still
10540 * be shown in the path bar: "/foo/[bar]/.baz"
10542 if (child_is_hidden)
10543 g_object_set (impl, "show-hidden", TRUE, NULL);
10547 get_list_file_info (GtkFileChooserDefault *impl,
10550 GtkTreeIter child_iter;
10552 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10556 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10560 list_icon_data_func (GtkTreeViewColumn *tree_column,
10561 GtkCellRenderer *cell,
10562 GtkTreeModel *tree_model,
10566 GtkFileChooserDefault *impl = data;
10567 GtkTreeIter child_iter;
10568 GdkPixbuf *pixbuf = NULL;
10569 gboolean sensitive = TRUE;
10571 profile_start ("start", NULL);
10573 switch (impl->operation_mode)
10575 case OPERATION_MODE_SEARCH:
10577 GtkTreeIter child_iter;
10578 gboolean is_folder;
10580 search_get_valid_child_iter (impl, &child_iter, iter);
10581 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10582 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10583 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10586 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10587 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10588 sensitive = is_folder;
10592 case OPERATION_MODE_RECENT:
10594 GtkTreeIter child_iter;
10595 GtkRecentInfo *info;
10596 gboolean is_folder;
10598 recent_get_valid_child_iter (impl, &child_iter, iter);
10599 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10600 RECENT_MODEL_COL_INFO, &info,
10601 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10604 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10606 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10607 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10608 sensitive = is_folder;
10612 case OPERATION_MODE_BROWSE:
10617 info = get_list_file_info (impl, iter);
10619 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10622 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10627 /* FIXME: NULL GError */
10628 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
10633 /* We are on the editable row */
10638 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10639 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10640 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10645 g_object_set (cell,
10647 "sensitive", sensitive,
10651 g_object_unref (pixbuf);
10653 profile_end ("end", NULL);
10657 list_name_data_func (GtkTreeViewColumn *tree_column,
10658 GtkCellRenderer *cell,
10659 GtkTreeModel *tree_model,
10663 GtkFileChooserDefault *impl = data;
10665 gboolean sensitive = TRUE;
10667 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10669 GtkTreeIter child_iter;
10670 gchar *display_name;
10671 gboolean is_folder;
10673 search_get_valid_child_iter (impl, &child_iter, iter);
10674 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10675 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10676 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10679 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10680 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10682 sensitive = is_folder;
10685 g_object_set (cell,
10686 "text", display_name,
10687 "sensitive", sensitive,
10688 "ellipsize", PANGO_ELLIPSIZE_END,
10694 if (impl->operation_mode == OPERATION_MODE_RECENT)
10696 GtkTreeIter child_iter;
10697 GtkRecentInfo *recent_info;
10698 gchar *display_name;
10699 gboolean is_folder;
10701 recent_get_valid_child_iter (impl, &child_iter, iter);
10702 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10703 RECENT_MODEL_COL_INFO, &recent_info,
10704 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10707 display_name = gtk_recent_info_get_short_name (recent_info);
10709 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10710 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10712 sensitive = is_folder;
10715 g_object_set (cell,
10716 "text", display_name,
10717 "sensitive", sensitive,
10718 "ellipsize", PANGO_ELLIPSIZE_END,
10721 g_free (display_name);
10726 info = get_list_file_info (impl, iter);
10731 g_object_set (cell,
10732 "text", _("Type name of new folder"),
10734 "ellipsize", PANGO_ELLIPSIZE_NONE,
10741 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10742 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10744 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10747 g_object_set (cell,
10748 "text", g_file_info_get_display_name (info),
10749 "sensitive", sensitive,
10750 "ellipsize", PANGO_ELLIPSIZE_END,
10756 list_size_data_func (GtkTreeViewColumn *tree_column,
10757 GtkCellRenderer *cell,
10758 GtkTreeModel *tree_model,
10762 GtkFileChooserDefault *impl = data;
10763 GFileInfo *info = get_list_file_info (impl, iter);
10766 gboolean sensitive = TRUE;
10768 if (!info || g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
10770 g_object_set (cell,
10772 "sensitive", sensitive,
10777 size = gtk_file_info_get_size (info);
10779 if (size < (gint64)1024)
10780 str = g_strdup_printf (g_dngettext (GETTEXT_DOMAIN, "%d byte", "%d bytes", (gint)size), (gint)size);
10781 else if (size < (gint64)1024*1024)
10782 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10783 else if (size < (gint64)1024*1024*1024)
10784 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10786 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10788 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10789 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10790 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10793 g_object_set (cell,
10795 "sensitive", sensitive,
10796 "alignment", PANGO_ALIGN_RIGHT,
10803 /* Tree column data callback for the file list; fetches the mtime of a file */
10805 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10806 GtkCellRenderer *cell,
10807 GtkTreeModel *tree_model,
10811 GtkFileChooserDefault *impl;
10812 GTimeVal timeval = { 0, };
10814 gchar *date_str = NULL;
10815 gboolean sensitive = TRUE;
10817 const char *locale, *dot = NULL;
10818 gint64 codepage = -1;
10824 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10826 GtkTreeIter child_iter;
10827 struct stat *statbuf;
10828 gboolean is_folder;
10830 search_get_valid_child_iter (impl, &child_iter, iter);
10831 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10832 SEARCH_MODEL_COL_STAT, &statbuf,
10833 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10836 time_mtime = statbuf->st_mtime;
10841 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10842 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10843 sensitive = is_folder;
10845 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10847 GtkTreeIter child_iter;
10848 GtkRecentInfo *info;
10849 gboolean is_folder;
10851 recent_get_valid_child_iter (impl, &child_iter, iter);
10852 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10853 RECENT_MODEL_COL_INFO, &info,
10854 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10858 time_mtime = gtk_recent_info_get_modified (info);
10862 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10863 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10864 sensitive = is_folder;
10870 info = get_list_file_info (impl, iter);
10873 g_object_set (cell,
10880 g_file_info_get_modification_time (info, &timeval);
10881 time_mtime = timeval.tv_sec;
10883 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10884 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10885 sensitive = (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY);
10888 if (G_UNLIKELY (time_mtime == 0))
10889 date_str = g_strdup (_("Unknown"));
10894 struct tm tm_mtime;
10896 const gchar *format;
10897 gchar *locale_format = NULL;
10900 #ifdef HAVE_LOCALTIME_R
10901 localtime_r ((time_t *) &time_mtime, &tm_mtime);
10904 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
10908 g_warning ("ptm != NULL failed");
10910 g_object_set (cell,
10911 "text", _("Unknown"),
10912 "sensitive", sensitive,
10917 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
10919 #endif /* HAVE_LOCALTIME_R */
10921 g_date_set_time_t (&mtime, time_mtime);
10922 time_now = time (NULL);
10923 g_date_set_time_t (&now, time_now);
10925 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
10927 /* Translators: %H means "hours" and %M means "minutes" */
10928 if (days_diff == 0)
10929 format = _("%H:%M");
10930 else if (days_diff == 1)
10931 format = _("Yesterday at %H:%M");
10934 if (days_diff > 1 && days_diff < 7)
10935 format = "%A"; /* Days from last week */
10937 format = "%x"; /* Any other date */
10941 /* g_locale_from_utf8() returns a string in the system
10942 * code-page, which is not always the same as that used by the C
10943 * library. For instance when running a GTK+ program with
10944 * LANG=ko on an English version of Windows, the system
10945 * code-page is 1252, but the code-page used by the C library is
10946 * 949. (It's GTK+ itself that sets the C library locale when it
10947 * notices the LANG environment variable. See gtkmain.c The
10948 * Microsoft C library doesn't look at any locale environment
10949 * variables.) We need to pass strftime() a string in the C
10950 * library's code-page. See bug #509885.
10952 locale = setlocale (LC_ALL, NULL);
10953 if (locale != NULL)
10954 dot = strchr (locale, '.');
10957 codepage = g_ascii_strtoll (dot+1, NULL, 10);
10959 /* All codepages should fit in 16 bits AFAIK */
10960 if (codepage > 0 && codepage < 65536)
10962 sprintf (charset, "CP%u", (guint) codepage);
10963 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
10967 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
10969 if (locale_format != NULL &&
10970 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
10973 /* As above but in opposite direction... */
10974 if (codepage > 0 && codepage < 65536)
10975 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
10977 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
10981 if (date_str == NULL)
10982 date_str = g_strdup (_("Unknown"));
10984 g_free (locale_format);
10987 g_object_set (cell,
10989 "sensitive", sensitive,
10995 _gtk_file_chooser_default_new (const char *file_system)
10997 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
10998 "file-system-backend", file_system,
11003 location_set_user_text (GtkFileChooserDefault *impl,
11006 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11007 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11011 location_popup_handler (GtkFileChooserDefault *impl,
11014 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11016 GtkWidget *widget_to_focus;
11018 /* This will give us the location widgets back */
11019 switch (impl->operation_mode)
11021 case OPERATION_MODE_SEARCH:
11022 search_switch_to_browse_mode (impl);
11024 case OPERATION_MODE_RECENT:
11025 recent_switch_to_browse_mode (impl);
11027 case OPERATION_MODE_BROWSE:
11028 g_assert_not_reached ();
11032 if (impl->current_folder)
11033 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11035 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11036 widget_to_focus = impl->browse_files_tree_view;
11038 widget_to_focus = impl->location_entry;
11040 gtk_widget_grab_focus (widget_to_focus);
11044 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11045 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11047 LocationMode new_mode;
11051 /* since the user typed something, we unconditionally want to turn on the entry */
11052 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11054 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11055 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11056 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11057 new_mode = LOCATION_MODE_PATH_BAR;
11060 g_assert_not_reached ();
11064 location_mode_set (impl, new_mode, TRUE);
11065 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11068 location_set_user_text (impl, path);
11071 location_entry_set_initial_text (impl);
11072 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11076 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11077 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11079 gtk_widget_grab_focus (impl->location_entry);
11081 location_set_user_text (impl, path);
11084 g_assert_not_reached ();
11087 /* Handler for the "up-folder" keybinding signal */
11089 up_folder_handler (GtkFileChooserDefault *impl)
11091 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11094 /* Handler for the "down-folder" keybinding signal */
11096 down_folder_handler (GtkFileChooserDefault *impl)
11098 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11101 /* Switches to the shortcut in the specified index */
11103 switch_to_shortcut (GtkFileChooserDefault *impl,
11108 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11109 g_assert_not_reached ();
11111 shortcuts_activate_iter (impl, &iter);
11114 /* Handler for the "home-folder" keybinding signal */
11116 home_folder_handler (GtkFileChooserDefault *impl)
11118 if (impl->has_home)
11119 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11122 /* Handler for the "desktop-folder" keybinding signal */
11124 desktop_folder_handler (GtkFileChooserDefault *impl)
11126 if (impl->has_desktop)
11127 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11130 /* Handler for the "search-shortcut" keybinding signal */
11132 search_shortcut_handler (GtkFileChooserDefault *impl)
11134 if (impl->has_search)
11136 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11138 /* we want the entry widget to grab the focus the first
11139 * time, not the browse_files_tree_view widget.
11141 if (impl->search_entry)
11142 gtk_widget_grab_focus (impl->search_entry);
11146 /* Handler for the "recent-shortcut" keybinding signal */
11148 recent_shortcut_handler (GtkFileChooserDefault *impl)
11150 if (impl->has_recent)
11151 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11155 quick_bookmark_handler (GtkFileChooserDefault *impl,
11156 gint bookmark_index)
11161 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11164 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11166 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11167 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11170 gtk_tree_path_free (path);
11172 switch_to_shortcut (impl, bookmark_pos);
11176 show_hidden_handler (GtkFileChooserDefault *impl)
11178 g_object_set (impl,
11179 "show-hidden", !impl->show_hidden,
11184 /* Drag and drop interfaces */
11187 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11192 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11194 model->impl = NULL;
11197 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11199 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11202 ShortcutsPaneModelFilter *model;
11206 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11208 pos = *gtk_tree_path_get_indices (path);
11209 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11211 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11214 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11216 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11218 GtkSelectionData *selection_data)
11220 ShortcutsPaneModelFilter *model;
11222 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11229 /* Fill the GtkTreeDragSourceIface vtable */
11231 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11233 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11234 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11238 /* Fill the GtkTreeDragDestIface vtable */
11240 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11242 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11243 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11247 static GtkTreeModel *
11248 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11249 GtkTreeModel *child_model,
11252 ShortcutsPaneModelFilter *model;
11254 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11255 "child-model", child_model,
11256 "virtual-root", root,
11259 model->impl = impl;
11261 return GTK_TREE_MODEL (model);
11267 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11270 RecentModelSort *model;
11271 GtkTreeIter iter, child_iter;
11272 gboolean is_folder;
11274 model = RECENT_MODEL_SORT (drag_source);
11275 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11278 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11279 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11280 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11287 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11289 GtkSelectionData *selection_data)
11291 RecentModelSort *model;
11292 GtkTreeIter iter, child_iter;
11296 model = RECENT_MODEL_SORT (drag_source);
11297 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11300 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11301 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11302 RECENT_MODEL_COL_FILE, &file,
11304 g_assert (file != NULL);
11306 uris[0] = g_file_get_uri (file);
11309 gtk_selection_data_set_uris (selection_data, uris);
11317 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11319 iface->row_draggable = recent_model_sort_row_draggable;
11320 iface->drag_data_get = recent_model_sort_drag_data_get;
11324 _recent_model_sort_class_init (RecentModelSortClass *klass)
11330 _recent_model_sort_init (RecentModelSort *model)
11332 model->impl = NULL;
11335 static GtkTreeModel *
11336 recent_model_sort_new (GtkFileChooserDefault *impl,
11337 GtkTreeModel *child_model)
11339 RecentModelSort *model;
11341 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11342 "model", child_model,
11344 model->impl = impl;
11346 return GTK_TREE_MODEL (model);
11352 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11355 SearchModelSort *model;
11356 GtkTreeIter iter, child_iter;
11357 gboolean is_folder;
11359 model = SEARCH_MODEL_SORT (drag_source);
11360 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11363 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11364 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11365 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11372 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11374 GtkSelectionData *selection_data)
11376 SearchModelSort *model;
11377 GtkTreeIter iter, child_iter;
11381 model = SEARCH_MODEL_SORT (drag_source);
11382 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11385 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11386 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11387 RECENT_MODEL_COL_FILE, &file,
11389 g_assert (file != NULL);
11391 uris[0] = g_file_get_uri (file);
11394 gtk_selection_data_set_uris (selection_data, uris);
11402 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11404 iface->row_draggable = search_model_sort_row_draggable;
11405 iface->drag_data_get = search_model_sort_drag_data_get;
11409 _search_model_sort_class_init (SearchModelSortClass *klass)
11415 _search_model_sort_init (SearchModelSort *model)
11417 model->impl = NULL;
11420 static GtkTreeModel *
11421 search_model_sort_new (GtkFileChooserDefault *impl,
11422 GtkTreeModel *child_model)
11424 SearchModelSort *model;
11426 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11427 "model", child_model,
11429 model->impl = impl;
11431 return GTK_TREE_MODEL (model);