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 "gtkfilesystemmodel.h"
46 #include "gtkhpaned.h"
47 #include "gtkiconfactory.h"
48 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
53 #include "gtkmarshalers.h"
54 #include "gtkmenuitem.h"
55 #include "gtkmessagedialog.h"
56 #include "gtkpathbar.h"
57 #include "gtkprivate.h"
58 #include "gtkradiobutton.h"
59 #include "gtkrecentfilter.h"
60 #include "gtkrecentmanager.h"
61 #include "gtkscrolledwindow.h"
62 #include "gtkseparatormenuitem.h"
63 #include "gtksizegroup.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
70 #include "gtktypebuiltins.h"
73 #if defined (G_OS_UNIX)
74 #include "gtkfilesystemunix.h"
75 #elif defined (G_OS_WIN32)
77 #include "gtkfilesystemwin32.h"
86 #include <sys/types.h>
97 #undef PROFILE_FILE_CHOOSER
98 #ifdef PROFILE_FILE_CHOOSER
105 #define PROFILE_INDENT 4
106 static int profile_indent;
109 profile_add_indent (int indent)
111 profile_indent += indent;
112 if (profile_indent < 0)
113 g_error ("You screwed up your indentation");
117 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
122 profile_add_indent (indent);
124 if (profile_indent == 0)
125 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
127 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
133 profile_add_indent (indent);
136 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
137 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
138 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
140 #define profile_start(x, y)
141 #define profile_end(x, y)
142 #define profile_msg(x, y)
147 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
149 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
150 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
151 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
153 #define MAX_LOADING_TIME 500
155 struct _GtkFileChooserDefaultClass
157 GtkVBoxClass parent_class;
163 LOCATION_POPUP_ON_PASTE,
169 LOCATION_TOGGLE_POPUP,
177 static guint signals[LAST_SIGNAL] = { 0 };
179 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
181 SHORTCUTS_COL_PIXBUF,
185 SHORTCUTS_COL_REMOVABLE,
186 SHORTCUTS_COL_PIXBUF_VISIBLE,
187 SHORTCUTS_COL_HANDLE,
188 SHORTCUTS_COL_NUM_COLUMNS
193 SHORTCUT_TYPE_VOLUME,
194 SHORTCUT_TYPE_SEPARATOR,
195 SHORTCUT_TYPE_SEARCH,
199 /* Column numbers for the file list */
204 FILE_LIST_COL_NUM_COLUMNS
207 /* Column numbers for the search model.
208 * Keep this in sync with search_setup_model()
211 SEARCH_MODEL_COL_PATH,
212 SEARCH_MODEL_COL_DISPLAY_NAME,
213 SEARCH_MODEL_COL_COLLATION_KEY,
214 SEARCH_MODEL_COL_STAT,
215 SEARCH_MODEL_COL_HANDLE,
216 SEARCH_MODEL_COL_PIXBUF,
217 SEARCH_MODEL_COL_MIME_TYPE,
218 SEARCH_MODEL_COL_IS_FOLDER,
219 SEARCH_MODEL_COL_NUM_COLUMNS
223 RECENT_MODEL_COL_PATH,
224 RECENT_MODEL_COL_DISPLAY_NAME,
225 RECENT_MODEL_COL_INFO,
226 RECENT_MODEL_COL_IS_FOLDER,
227 RECENT_MODEL_COL_HANDLE,
228 RECENT_MODEL_COL_NUM_COLUMNS
231 /* Identifiers for target types */
237 /* Target types for dragging from the shortcuts list */
238 static const GtkTargetEntry shortcuts_source_targets[] = {
239 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
242 static const int num_shortcuts_source_targets = G_N_ELEMENTS (shortcuts_source_targets);
244 /* Target types for dropping into the shortcuts list */
245 static const GtkTargetEntry shortcuts_dest_targets[] = {
246 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
247 { "text/uri-list", 0, TEXT_URI_LIST }
250 static const int num_shortcuts_dest_targets = G_N_ELEMENTS (shortcuts_dest_targets);
252 /* Target types for DnD from the file list */
253 static const GtkTargetEntry file_list_source_targets[] = {
254 { "text/uri-list", 0, TEXT_URI_LIST }
257 static const int num_file_list_source_targets = G_N_ELEMENTS (file_list_source_targets);
259 /* Target types for dropping into the file list */
260 static const GtkTargetEntry file_list_dest_targets[] = {
261 { "text/uri-list", 0, TEXT_URI_LIST }
264 static const int num_file_list_dest_targets = G_N_ELEMENTS (file_list_dest_targets);
266 /* Target types for dragging from the recent files list */
267 static const GtkTargetEntry recent_list_source_targets[] = {
268 { "text/uri-list", 0, TEXT_URI_LIST }
271 static const int num_recent_list_source_targets = G_N_ELEMENTS (recent_list_source_targets);
274 search_is_possible (GtkFileChooserDefault *impl)
276 if (impl->search_engine == NULL)
277 impl->search_engine = _gtk_search_engine_new ();
279 return impl->search_engine != NULL;
282 /* Interesting places in the shortcuts bar */
286 SHORTCUTS_RECENT_SEPARATOR,
291 SHORTCUTS_BOOKMARKS_SEPARATOR,
293 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
294 SHORTCUTS_CURRENT_FOLDER
297 /* Icon size for if we can't get it from the theme */
298 #define FALLBACK_ICON_SIZE 16
300 #define PREVIEW_HBOX_SPACING 12
304 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
305 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
307 static GObject* gtk_file_chooser_default_constructor (GType type,
308 guint n_construct_properties,
309 GObjectConstructParam *construct_params);
310 static void gtk_file_chooser_default_finalize (GObject *object);
311 static void gtk_file_chooser_default_set_property (GObject *object,
315 static void gtk_file_chooser_default_get_property (GObject *object,
319 static void gtk_file_chooser_default_dispose (GObject *object);
320 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
321 static void gtk_file_chooser_default_map (GtkWidget *widget);
322 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
323 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
324 GtkWidget *previous_toplevel);
325 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
326 GtkStyle *previous_style);
327 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
328 GdkScreen *previous_screen);
329 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
330 GtkAllocation *allocation);
332 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
333 const GtkFilePath *path,
335 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
336 const GtkFilePath *path,
338 gboolean clear_entry,
340 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
341 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
343 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
344 const GtkFilePath *path,
346 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
347 const GtkFilePath *path);
348 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
349 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
350 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
351 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
352 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
353 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
354 GtkFileFilter *filter);
355 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
356 GtkFileFilter *filter);
357 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
358 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
359 const GtkFilePath *path,
361 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
362 const GtkFilePath *path,
364 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
366 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
368 gint *default_height);
369 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
370 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
372 static void location_popup_handler (GtkFileChooserDefault *impl,
374 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
375 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
376 static void up_folder_handler (GtkFileChooserDefault *impl);
377 static void down_folder_handler (GtkFileChooserDefault *impl);
378 static void home_folder_handler (GtkFileChooserDefault *impl);
379 static void desktop_folder_handler (GtkFileChooserDefault *impl);
380 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
381 gint bookmark_index);
382 static void show_hidden_handler (GtkFileChooserDefault *impl);
383 static void search_shortcut_handler (GtkFileChooserDefault *impl);
384 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
385 static void update_appearance (GtkFileChooserDefault *impl);
387 static void set_current_filter (GtkFileChooserDefault *impl,
388 GtkFileFilter *filter);
389 static void check_preview_change (GtkFileChooserDefault *impl);
391 static void filter_combo_changed (GtkComboBox *combo_box,
392 GtkFileChooserDefault *impl);
394 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
396 GtkFileChooserDefault *impl);
398 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
401 gboolean path_currently_selected,
403 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
405 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
407 static int shortcuts_get_index (GtkFileChooserDefault *impl,
408 ShortcutsIndex where);
409 static int shortcut_find_position (GtkFileChooserDefault *impl,
410 const GtkFilePath *path);
412 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
414 static gboolean list_select_func (GtkTreeSelection *selection,
417 gboolean path_currently_selected,
420 static void list_selection_changed (GtkTreeSelection *tree_selection,
421 GtkFileChooserDefault *impl);
422 static void list_row_activated (GtkTreeView *tree_view,
424 GtkTreeViewColumn *column,
425 GtkFileChooserDefault *impl);
427 static void select_func (GtkFileSystemModel *model,
432 static void path_bar_clicked (GtkPathBar *path_bar,
433 GtkFilePath *file_path,
434 GtkFilePath *child_path,
435 gboolean child_is_hidden,
436 GtkFileChooserDefault *impl);
438 static void add_bookmark_button_clicked_cb (GtkButton *button,
439 GtkFileChooserDefault *impl);
440 static void remove_bookmark_button_clicked_cb (GtkButton *button,
441 GtkFileChooserDefault *impl);
442 static void save_folder_combo_changed_cb (GtkComboBox *combo,
443 GtkFileChooserDefault *impl);
445 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
446 GtkCellRenderer *cell,
447 GtkTreeModel *tree_model,
450 static void list_name_data_func (GtkTreeViewColumn *tree_column,
451 GtkCellRenderer *cell,
452 GtkTreeModel *tree_model,
456 static void list_size_data_func (GtkTreeViewColumn *tree_column,
457 GtkCellRenderer *cell,
458 GtkTreeModel *tree_model,
462 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
463 GtkCellRenderer *cell,
464 GtkTreeModel *tree_model,
468 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
471 static void load_remove_timer (GtkFileChooserDefault *impl);
472 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
474 static void location_button_toggled_cb (GtkToggleButton *toggle,
475 GtkFileChooserDefault *impl);
476 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
478 static void search_stop_searching (GtkFileChooserDefault *impl,
479 gboolean remove_query);
480 static void search_clear_model (GtkFileChooserDefault *impl,
481 gboolean remove_from_treeview);
482 static gboolean search_should_respond (GtkFileChooserDefault *impl);
483 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
484 static GSList *search_get_selected_paths (GtkFileChooserDefault *impl);
485 static void search_entry_activate_cb (GtkEntry *entry,
487 static void settings_load (GtkFileChooserDefault *impl);
488 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
489 GtkTreeIter *child_iter,
492 static void recent_manager_update (GtkFileChooserDefault *impl);
493 static void recent_stop_loading (GtkFileChooserDefault *impl);
494 static void recent_clear_model (GtkFileChooserDefault *impl,
495 gboolean remove_from_treeview);
496 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
497 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
498 static GSList * recent_get_selected_paths (GtkFileChooserDefault *impl);
499 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
500 GtkTreeIter *child_iter,
506 /* Drag and drop interface declarations */
509 GtkTreeModelFilter parent;
511 GtkFileChooserDefault *impl;
512 } ShortcutsPaneModelFilter;
515 GtkTreeModelFilterClass parent_class;
516 } ShortcutsPaneModelFilterClass;
518 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
519 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
521 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
523 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
524 _shortcuts_pane_model_filter,
525 GTK_TYPE_TREE_MODEL_FILTER,
526 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
527 shortcuts_pane_model_filter_drag_source_iface_init))
529 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
530 GtkTreeModel *child_model,
535 GtkTreeModelSort parent;
537 GtkFileChooserDefault *impl;
541 GtkTreeModelSortClass parent_class;
542 } RecentModelSortClass;
544 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
545 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
547 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
549 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
551 GTK_TYPE_TREE_MODEL_SORT,
552 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
553 recent_model_sort_drag_source_iface_init));
555 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
556 GtkTreeModel *child_model);
560 GtkTreeModelSort parent;
562 GtkFileChooserDefault *impl;
566 GtkTreeModelSortClass parent_class;
567 } SearchModelSortClass;
569 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
570 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
572 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
574 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
576 GTK_TYPE_TREE_MODEL_SORT,
577 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
578 search_model_sort_drag_source_iface_init));
580 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
581 GtkTreeModel *child_model);
585 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
586 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
587 gtk_file_chooser_default_iface_init)
588 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
589 gtk_file_chooser_embed_default_iface_init));
592 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
594 static const guint quick_bookmark_keyvals[10] = {
595 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
597 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
598 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
599 GtkBindingSet *binding_set;
602 gobject_class->finalize = gtk_file_chooser_default_finalize;
603 gobject_class->constructor = gtk_file_chooser_default_constructor;
604 gobject_class->set_property = gtk_file_chooser_default_set_property;
605 gobject_class->get_property = gtk_file_chooser_default_get_property;
606 gobject_class->dispose = gtk_file_chooser_default_dispose;
608 widget_class->show_all = gtk_file_chooser_default_show_all;
609 widget_class->map = gtk_file_chooser_default_map;
610 widget_class->unmap = gtk_file_chooser_default_unmap;
611 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
612 widget_class->style_set = gtk_file_chooser_default_style_set;
613 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
614 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
616 signals[LOCATION_POPUP] =
617 _gtk_binding_signal_new (I_("location-popup"),
618 G_OBJECT_CLASS_TYPE (class),
619 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
620 G_CALLBACK (location_popup_handler),
622 _gtk_marshal_VOID__STRING,
623 G_TYPE_NONE, 1, G_TYPE_STRING);
624 signals[LOCATION_POPUP_ON_PASTE] =
625 _gtk_binding_signal_new ("location-popup-on-paste",
626 G_OBJECT_CLASS_TYPE (class),
627 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
628 G_CALLBACK (location_popup_on_paste_handler),
630 _gtk_marshal_VOID__VOID,
632 signals[LOCATION_TOGGLE_POPUP] =
633 _gtk_binding_signal_new (I_("location-toggle-popup"),
634 G_OBJECT_CLASS_TYPE (class),
635 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
636 G_CALLBACK (location_toggle_popup_handler),
638 _gtk_marshal_VOID__VOID,
641 _gtk_binding_signal_new (I_("up-folder"),
642 G_OBJECT_CLASS_TYPE (class),
643 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
644 G_CALLBACK (up_folder_handler),
646 _gtk_marshal_VOID__VOID,
648 signals[DOWN_FOLDER] =
649 _gtk_binding_signal_new (I_("down-folder"),
650 G_OBJECT_CLASS_TYPE (class),
651 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
652 G_CALLBACK (down_folder_handler),
654 _gtk_marshal_VOID__VOID,
656 signals[HOME_FOLDER] =
657 _gtk_binding_signal_new (I_("home-folder"),
658 G_OBJECT_CLASS_TYPE (class),
659 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
660 G_CALLBACK (home_folder_handler),
662 _gtk_marshal_VOID__VOID,
664 signals[DESKTOP_FOLDER] =
665 _gtk_binding_signal_new (I_("desktop-folder"),
666 G_OBJECT_CLASS_TYPE (class),
667 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
668 G_CALLBACK (desktop_folder_handler),
670 _gtk_marshal_VOID__VOID,
672 signals[QUICK_BOOKMARK] =
673 _gtk_binding_signal_new (I_("quick-bookmark"),
674 G_OBJECT_CLASS_TYPE (class),
675 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
676 G_CALLBACK (quick_bookmark_handler),
678 _gtk_marshal_VOID__INT,
679 G_TYPE_NONE, 1, G_TYPE_INT);
680 signals[SHOW_HIDDEN] =
681 _gtk_binding_signal_new ("show-hidden",
682 G_OBJECT_CLASS_TYPE (class),
683 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
684 G_CALLBACK (show_hidden_handler),
686 _gtk_marshal_VOID__VOID,
688 signals[SEARCH_SHORTCUT] =
689 _gtk_binding_signal_new ("search-shortcut",
690 G_OBJECT_CLASS_TYPE (class),
691 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
692 G_CALLBACK (search_shortcut_handler),
694 _gtk_marshal_VOID__VOID,
696 signals[RECENT_SHORTCUT] =
697 _gtk_binding_signal_new ("recent-shortcut",
698 G_OBJECT_CLASS_TYPE (class),
699 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
700 G_CALLBACK (recent_shortcut_handler),
702 _gtk_marshal_VOID__VOID,
705 binding_set = gtk_binding_set_by_class (class);
707 gtk_binding_entry_add_signal (binding_set,
708 GDK_l, GDK_CONTROL_MASK,
709 "location-toggle-popup",
712 gtk_binding_entry_add_signal (binding_set,
715 1, G_TYPE_STRING, "/");
716 gtk_binding_entry_add_signal (binding_set,
719 1, G_TYPE_STRING, "/");
722 gtk_binding_entry_add_signal (binding_set,
725 1, G_TYPE_STRING, "~");
728 gtk_binding_entry_add_signal (binding_set,
729 GDK_v, GDK_CONTROL_MASK,
730 "location-popup-on-paste",
732 gtk_binding_entry_add_signal (binding_set,
733 GDK_Up, GDK_MOD1_MASK,
736 gtk_binding_entry_add_signal (binding_set,
740 gtk_binding_entry_add_signal (binding_set,
741 GDK_KP_Up, GDK_MOD1_MASK,
745 gtk_binding_entry_add_signal (binding_set,
746 GDK_Down, GDK_MOD1_MASK,
749 gtk_binding_entry_add_signal (binding_set,
750 GDK_KP_Down, GDK_MOD1_MASK,
754 gtk_binding_entry_add_signal (binding_set,
755 GDK_Home, GDK_MOD1_MASK,
758 gtk_binding_entry_add_signal (binding_set,
759 GDK_KP_Home, GDK_MOD1_MASK,
762 gtk_binding_entry_add_signal (binding_set,
763 GDK_d, GDK_MOD1_MASK,
766 gtk_binding_entry_add_signal (binding_set,
767 GDK_h, GDK_CONTROL_MASK,
770 gtk_binding_entry_add_signal (binding_set,
771 GDK_s, GDK_MOD1_MASK,
774 gtk_binding_entry_add_signal (binding_set,
775 GDK_r, GDK_MOD1_MASK,
779 for (i = 0; i < 10; i++)
780 gtk_binding_entry_add_signal (binding_set,
781 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
785 _gtk_file_chooser_install_properties (gobject_class);
789 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
791 iface->select_path = gtk_file_chooser_default_select_path;
792 iface->unselect_path = gtk_file_chooser_default_unselect_path;
793 iface->select_all = gtk_file_chooser_default_select_all;
794 iface->unselect_all = gtk_file_chooser_default_unselect_all;
795 iface->get_paths = gtk_file_chooser_default_get_paths;
796 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
797 iface->get_file_system = gtk_file_chooser_default_get_file_system;
798 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
799 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
800 iface->set_current_name = gtk_file_chooser_default_set_current_name;
801 iface->add_filter = gtk_file_chooser_default_add_filter;
802 iface->remove_filter = gtk_file_chooser_default_remove_filter;
803 iface->list_filters = gtk_file_chooser_default_list_filters;
804 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
805 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
806 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
810 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
812 iface->get_default_size = gtk_file_chooser_default_get_default_size;
813 iface->should_respond = gtk_file_chooser_default_should_respond;
814 iface->initial_focus = gtk_file_chooser_default_initial_focus;
818 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
820 profile_start ("start", NULL);
821 #ifdef PROFILE_FILE_CHOOSER
822 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
824 impl->local_only = TRUE;
825 impl->preview_widget_active = TRUE;
826 impl->use_preview_label = TRUE;
827 impl->select_multiple = FALSE;
828 impl->show_hidden = FALSE;
829 impl->icon_size = FALLBACK_ICON_SIZE;
830 impl->load_state = LOAD_EMPTY;
831 impl->reload_state = RELOAD_EMPTY;
832 impl->pending_select_paths = NULL;
833 impl->location_mode = LOCATION_MODE_PATH_BAR;
834 impl->operation_mode = OPERATION_MODE_BROWSE;
836 gtk_box_set_spacing (GTK_BOX (impl), 12);
838 impl->tooltips = gtk_tooltips_new ();
839 g_object_ref_sink (impl->tooltips);
841 profile_end ("end", NULL);
844 /* Frees the data columns for the specified iter in the shortcuts model*/
846 shortcuts_free_row_data (GtkFileChooserDefault *impl,
850 ShortcutType shortcut_type;
851 GtkFileSystemHandle *handle;
853 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
854 SHORTCUTS_COL_DATA, &col_data,
855 SHORTCUTS_COL_TYPE, &shortcut_type,
856 SHORTCUTS_COL_HANDLE, &handle,
860 gtk_file_system_cancel_operation (handle);
862 if (!(shortcut_type == SHORTCUT_TYPE_PATH ||
863 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
867 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
869 GtkFileSystemVolume *volume;
872 gtk_file_system_volume_free (impl->file_system, volume);
878 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
881 gtk_file_path_free (path);
885 /* Frees all the data columns in the shortcuts model */
887 shortcuts_free (GtkFileChooserDefault *impl)
891 if (!impl->shortcuts_model)
894 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
897 shortcuts_free_row_data (impl, &iter);
899 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
901 g_object_unref (impl->shortcuts_model);
902 impl->shortcuts_model = NULL;
906 pending_select_paths_free (GtkFileChooserDefault *impl)
910 for (l = impl->pending_select_paths; l; l = l->next)
915 gtk_file_path_free (path);
918 g_slist_free (impl->pending_select_paths);
919 impl->pending_select_paths = NULL;
923 pending_select_paths_add (GtkFileChooserDefault *impl,
924 const GtkFilePath *path)
926 impl->pending_select_paths =
927 g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
930 /* Used from gtk_tree_selection_selected_foreach() */
932 store_selection_foreach (GtkTreeModel *model,
937 GtkFileChooserDefault *impl;
938 GtkTreeIter child_iter;
939 const GtkFilePath *file_path;
941 impl = GTK_FILE_CHOOSER_DEFAULT (data);
943 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
945 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
946 pending_select_paths_add (impl, file_path);
949 /* Stores the current selection in the list of paths to select; this is used to
950 * preserve the selection when reloading the current folder.
953 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
955 GtkTreeSelection *selection;
957 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
958 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
962 gtk_file_chooser_default_finalize (GObject *object)
964 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
967 if (impl->shortcuts_pane_filter_model)
968 g_object_unref (impl->shortcuts_pane_filter_model);
970 if (impl->shortcuts_combo_filter_model)
971 g_object_unref (impl->shortcuts_combo_filter_model);
973 shortcuts_free (impl);
975 g_object_unref (impl->file_system);
977 g_free (impl->browse_files_last_selected_name);
979 for (l = impl->filters; l; l = l->next)
981 GtkFileFilter *filter;
983 filter = GTK_FILE_FILTER (l->data);
984 g_object_unref (filter);
986 g_slist_free (impl->filters);
988 if (impl->current_filter)
989 g_object_unref (impl->current_filter);
991 if (impl->current_volume_path)
992 gtk_file_path_free (impl->current_volume_path);
994 if (impl->current_folder)
995 gtk_file_path_free (impl->current_folder);
997 if (impl->preview_path)
998 gtk_file_path_free (impl->preview_path);
1000 load_remove_timer (impl);
1002 /* Free all the Models we have */
1003 if (impl->browse_files_model)
1004 g_object_unref (impl->browse_files_model);
1006 if (impl->sort_model)
1007 g_object_unref (impl->sort_model);
1009 search_clear_model (impl, FALSE);
1010 recent_clear_model (impl, FALSE);
1012 g_free (impl->preview_display_name);
1014 g_free (impl->edited_new_text);
1016 g_object_unref (impl->tooltips);
1018 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
1021 /* Shows an error dialog set as transient for the specified window */
1023 error_message_with_parent (GtkWindow *parent,
1029 dialog = gtk_message_dialog_new (parent,
1030 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1035 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1039 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
1041 gtk_dialog_run (GTK_DIALOG (dialog));
1042 gtk_widget_destroy (dialog);
1045 /* Returns a toplevel GtkWindow, or NULL if none */
1047 get_toplevel (GtkWidget *widget)
1049 GtkWidget *toplevel;
1051 toplevel = gtk_widget_get_toplevel (widget);
1052 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1055 return GTK_WINDOW (toplevel);
1058 /* Shows an error dialog for the file chooser */
1060 error_message (GtkFileChooserDefault *impl,
1064 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1067 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1069 error_dialog (GtkFileChooserDefault *impl,
1071 const GtkFilePath *path,
1080 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1081 text = g_strdup_printf (msg, uri);
1082 error_message (impl, text, error->message);
1085 g_error_free (error);
1089 /* Displays an error message about not being able to get information for a file.
1090 * Frees the GError as well.
1093 error_getting_info_dialog (GtkFileChooserDefault *impl,
1094 const GtkFilePath *path,
1098 _("Could not retrieve information about the file"),
1102 /* Shows an error dialog about not being able to add a bookmark */
1104 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1105 const GtkFilePath *path,
1109 _("Could not add a bookmark"),
1113 /* Shows an error dialog about not being able to remove a bookmark */
1115 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1116 const GtkFilePath *path,
1120 _("Could not remove bookmark"),
1124 /* Shows an error dialog about not being able to create a folder */
1126 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1127 const GtkFilePath *path,
1131 _("The folder could not be created"),
1135 /* Shows an error about not being able to create a folder because a file with
1136 * the same name is already there.
1139 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1140 const GtkFilePath *path,
1144 _("The folder could not be created, as a file with the same "
1145 "name already exists. Try using a different name for the "
1146 "folder, or rename the file first."),
1150 /* Shows an error dialog about not being able to create a filename */
1152 error_building_filename_dialog (GtkFileChooserDefault *impl,
1153 const GtkFilePath *folder_part,
1154 const char *file_part,
1157 error_dialog (impl, _("Invalid file name"),
1161 /* Shows an error dialog when we cannot switch to a folder */
1163 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1164 const GtkFilePath *path,
1167 error_dialog (impl, _("The folder contents could not be displayed"),
1171 /* Changes folders, displaying an error dialog if this fails */
1173 change_folder_and_display_error (GtkFileChooserDefault *impl,
1174 const GtkFilePath *path,
1175 gboolean clear_entry)
1179 GtkFilePath *path_copy;
1181 g_return_val_if_fail (path != NULL, FALSE);
1183 profile_start ("start", (char *) path);
1185 /* We copy the path because of this case:
1187 * list_row_activated()
1188 * fetches path from model; path belongs to the model (*)
1189 * calls change_folder_and_display_error()
1190 * calls _gtk_file_chooser_set_current_folder_path()
1191 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1194 path_copy = gtk_file_path_copy (path);
1197 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), path_copy, TRUE, clear_entry, &error);
1200 error_changing_folder_dialog (impl, path_copy, error);
1202 gtk_file_path_free (path_copy);
1204 profile_end ("end", (char *) path);
1210 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1212 if (impl->use_preview_label)
1214 if (!impl->preview_label)
1216 impl->preview_label = gtk_label_new (impl->preview_display_name);
1217 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1218 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1219 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1220 gtk_widget_show (impl->preview_label);
1225 if (impl->preview_label)
1227 gtk_widget_destroy (impl->preview_label);
1228 impl->preview_label = NULL;
1232 if (impl->preview_widget_active && impl->preview_widget)
1233 gtk_widget_show (impl->preview_box);
1235 gtk_widget_hide (impl->preview_box);
1237 g_signal_emit_by_name (impl, "default-size-changed");
1241 set_preview_widget (GtkFileChooserDefault *impl,
1242 GtkWidget *preview_widget)
1244 if (preview_widget == impl->preview_widget)
1247 if (impl->preview_widget)
1248 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1249 impl->preview_widget);
1251 impl->preview_widget = preview_widget;
1252 if (impl->preview_widget)
1254 gtk_widget_show (impl->preview_widget);
1255 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1256 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1257 impl->preview_widget,
1258 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1261 update_preview_widget_visibility (impl);
1264 /* Renders a "Search" icon at an appropriate size for a tree view */
1266 render_search_icon (GtkFileChooserDefault *impl)
1268 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1272 render_recent_icon (GtkFileChooserDefault *impl)
1274 GtkIconTheme *theme;
1277 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1278 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1280 theme = gtk_icon_theme_get_default ();
1282 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1288 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1294 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1295 struct ReloadIconsData
1297 GtkFileChooserDefault *impl;
1298 GtkTreeRowReference *row_ref;
1302 shortcuts_reload_icons_get_info_cb (GtkFileSystemHandle *handle,
1303 const GtkFileInfo *info,
1304 const GError *error,
1310 gboolean cancelled = handle->cancelled;
1311 struct ReloadIconsData *data = user_data;
1313 if (!g_slist_find (data->impl->reload_icon_handles, handle))
1316 data->impl->reload_icon_handles = g_slist_remove (data->impl->reload_icon_handles, handle);
1318 if (cancelled || error)
1321 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->impl),
1322 data->impl->icon_size, NULL);
1324 path = gtk_tree_row_reference_get_path (data->row_ref);
1325 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1326 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1327 SHORTCUTS_COL_PIXBUF, pixbuf,
1329 gtk_tree_path_free (path);
1332 g_object_unref (pixbuf);
1335 gtk_tree_row_reference_free (data->row_ref);
1336 g_object_unref (data->impl);
1339 g_object_unref (handle);
1343 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1348 profile_start ("start", NULL);
1350 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1353 for (l = impl->reload_icon_handles; l; l = l->next)
1355 GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1356 gtk_file_system_cancel_operation (handle);
1358 g_slist_free (impl->reload_icon_handles);
1359 impl->reload_icon_handles = NULL;
1364 ShortcutType shortcut_type;
1365 gboolean pixbuf_visible;
1368 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1369 SHORTCUTS_COL_DATA, &data,
1370 SHORTCUTS_COL_TYPE, &shortcut_type,
1371 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1377 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1379 GtkFileSystemVolume *volume;
1382 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1383 impl->icon_size, NULL);
1385 else if (shortcut_type == SHORTCUT_TYPE_PATH)
1387 if (gtk_file_system_path_is_local (impl->file_system, (GtkFilePath *)data))
1389 const GtkFilePath *path;
1390 struct ReloadIconsData *info;
1391 GtkTreePath *tree_path;
1392 GtkFileSystemHandle *handle;
1396 info = g_new0 (struct ReloadIconsData, 1);
1397 info->impl = g_object_ref (impl);
1398 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1399 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1400 gtk_tree_path_free (tree_path);
1402 handle = gtk_file_system_get_info (impl->file_system, path,
1404 shortcuts_reload_icons_get_info_cb,
1406 impl->reload_icon_handles = g_slist_append (impl->reload_icon_handles, handle);
1410 GtkIconTheme *icon_theme;
1412 /* Don't call get_info for remote paths to avoid latency and
1414 * If we switch to a better bookmarks file format (XBEL), we
1415 * should use mime info to get a better icon.
1417 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1418 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-share",
1419 impl->icon_size, 0, NULL);
1422 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1424 pixbuf = render_search_icon (impl);
1426 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1428 pixbuf = render_recent_icon (impl);
1431 gtk_list_store_set (impl->shortcuts_model, &iter,
1432 SHORTCUTS_COL_PIXBUF, pixbuf,
1436 g_object_unref (pixbuf);
1440 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1444 profile_end ("end", NULL);
1448 shortcuts_find_folder (GtkFileChooserDefault *impl,
1449 GtkFilePath *folder)
1451 GtkTreeSelection *selection;
1455 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1457 g_assert (folder != NULL);
1458 pos = shortcut_find_position (impl, folder);
1461 gtk_tree_selection_unselect_all (selection);
1465 path = gtk_tree_path_new_from_indices (pos, -1);
1466 gtk_tree_selection_select_path (selection, path);
1467 gtk_tree_path_free (path);
1470 /* If a shortcut corresponds to the current folder, selects it */
1472 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1474 shortcuts_find_folder (impl, impl->current_folder);
1477 /* Removes the specified number of rows from the shortcuts list */
1479 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1485 path = gtk_tree_path_new_from_indices (start_row, -1);
1487 for (; n_rows; n_rows--)
1491 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1492 g_assert_not_reached ();
1494 shortcuts_free_row_data (impl, &iter);
1495 gtk_list_store_remove (impl->shortcuts_model, &iter);
1498 gtk_tree_path_free (path);
1502 shortcuts_update_count (GtkFileChooserDefault *impl,
1503 ShortcutsIndex type,
1508 case SHORTCUTS_HOME:
1510 impl->has_home = FALSE;
1512 impl->has_home = TRUE;
1515 case SHORTCUTS_DESKTOP:
1517 impl->has_desktop = FALSE;
1519 impl->has_desktop = TRUE;
1522 case SHORTCUTS_VOLUMES:
1523 impl->num_volumes += value;
1526 case SHORTCUTS_SHORTCUTS:
1527 impl->num_shortcuts += value;
1530 case SHORTCUTS_BOOKMARKS:
1531 impl->num_bookmarks += value;
1534 case SHORTCUTS_CURRENT_FOLDER:
1536 impl->shortcuts_current_folder_active = FALSE;
1538 impl->shortcuts_current_folder_active = TRUE;
1547 struct ShortcutsInsertRequest
1549 GtkFileChooserDefault *impl;
1550 GtkFilePath *parent_path;
1554 GtkTreeRowReference *row_ref;
1555 ShortcutsIndex type;
1561 get_file_info_finished (GtkFileSystemHandle *handle,
1562 const GtkFileInfo *info,
1563 const GError *error,
1567 gboolean cancelled = handle->cancelled;
1571 GtkFileSystemHandle *model_handle;
1572 struct ShortcutsInsertRequest *request = data;
1574 path = gtk_tree_row_reference_get_path (request->row_ref);
1576 /* Handle doesn't exist anymore in the model */
1579 pos = gtk_tree_path_get_indices (path)[0];
1580 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1582 gtk_tree_path_free (path);
1584 /* validate handle, else goto out */
1585 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1586 SHORTCUTS_COL_HANDLE, &model_handle,
1588 if (handle != model_handle)
1591 /* set the handle to NULL in the model (we unref later on) */
1592 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1593 SHORTCUTS_COL_HANDLE, NULL,
1601 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1602 shortcuts_update_count (request->impl, request->type, -1);
1604 if (request->type == SHORTCUTS_HOME)
1606 const char *home = g_get_home_dir ();
1607 GtkFilePath *home_path;
1609 home_path = gtk_file_system_filename_to_path (request->impl->file_system, home);
1610 error_getting_info_dialog (request->impl, home_path, g_error_copy (error));
1611 gtk_file_path_free (home_path);
1613 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1615 /* Remove the current folder separator */
1616 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1617 shortcuts_remove_rows (request->impl, separator_pos, 1);
1623 if (!request->label_copy)
1624 request->label_copy = g_strdup (gtk_file_info_get_display_name (info));
1625 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1626 request->impl->icon_size, NULL);
1628 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1629 SHORTCUTS_COL_PIXBUF, pixbuf,
1630 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1631 SHORTCUTS_COL_NAME, request->label_copy,
1632 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_PATH,
1633 SHORTCUTS_COL_REMOVABLE, request->removable,
1636 if (request->impl->shortcuts_pane_filter_model)
1637 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1639 if (request->impl->shortcuts_combo_filter_model)
1640 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1642 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1643 request->impl->save_folder_combo != NULL)
1645 /* The current folder is updated via _activate_iter(), don't
1646 * have save_folder_combo_changed_cb() call _activate_iter()
1649 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1650 G_CALLBACK (save_folder_combo_changed_cb),
1653 if (request->impl->has_search)
1656 if (request->impl->has_recent)
1659 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1660 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1661 G_CALLBACK (save_folder_combo_changed_cb),
1666 g_object_unref (pixbuf);
1669 g_object_unref (request->impl);
1670 gtk_file_path_free (request->parent_path);
1671 gtk_file_path_free (request->path);
1672 gtk_tree_row_reference_free (request->row_ref);
1673 g_free (request->label_copy);
1676 g_object_unref (handle);
1679 /* FIXME: GtkFileSystem needs a function to split a remote path
1680 * into hostname and path components, or maybe just have a
1681 * gtk_file_system_path_get_display_name().
1683 * This function is also used in gtkfilechooserbutton.c
1686 _gtk_file_chooser_label_for_uri (const gchar *uri)
1688 const gchar *path, *start, *end, *p;
1689 gchar *host, *label;
1691 start = strstr (uri, "://");
1693 path = strchr (start, '/');
1699 end = uri + strlen (uri);
1703 /* strip username */
1704 p = strchr (start, '@');
1710 p = strchr (start, ':');
1714 host = g_strndup (start, end - start);
1716 /* Translators: the first string is a path and the second string
1717 * is a hostname. Nautilus and the panel contain the same string
1720 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1727 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1728 * inserts a volume. A position of -1 indicates the end of the tree.
1731 shortcuts_insert_path (GtkFileChooserDefault *impl,
1733 ShortcutType shortcut_type,
1734 GtkFileSystemVolume *volume,
1735 const GtkFilePath *path,
1738 ShortcutsIndex type)
1741 GdkPixbuf *pixbuf = NULL;
1742 gpointer data = NULL;
1744 GtkIconTheme *icon_theme;
1746 profile_start ("start", (shortcut_type == SHORTCUT_TYPE_VOLUME) ? "volume"
1747 : ((shortcut_type == SHORTCUT_TYPE_PATH) ? (char *) path : NULL));
1749 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1752 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1753 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1754 impl->icon_size, NULL);
1756 else if (shortcut_type == SHORTCUT_TYPE_PATH)
1758 if (gtk_file_system_path_is_local (impl->file_system, path))
1760 struct ShortcutsInsertRequest *request;
1761 GtkFileSystemHandle *handle;
1764 request = g_new0 (struct ShortcutsInsertRequest, 1);
1765 request->impl = g_object_ref (impl);
1766 request->path = gtk_file_path_copy (path);
1767 request->name_only = TRUE;
1768 request->removable = removable;
1770 request->type = type;
1772 request->label_copy = g_strdup (label);
1775 gtk_list_store_append (impl->shortcuts_model, &iter);
1777 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1779 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1780 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1781 gtk_tree_path_free (p);
1783 handle = gtk_file_system_get_info (request->impl->file_system, request->path,
1784 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN | GTK_FILE_INFO_ICON,
1785 get_file_info_finished, request);
1787 gtk_list_store_set (impl->shortcuts_model, &iter,
1788 SHORTCUTS_COL_DATA, gtk_file_path_copy (path),
1789 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_PATH,
1790 SHORTCUTS_COL_HANDLE, handle,
1793 shortcuts_update_count (impl, type, 1);
1799 /* Don't call get_info for remote paths to avoid latency and
1802 data = gtk_file_path_copy (path);
1804 label_copy = g_strdup (label);
1809 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1811 label_copy = _gtk_file_chooser_label_for_uri (uri);
1816 /* If we switch to a better bookmarks file format (XBEL), we
1817 * should use mime info to get a better icon.
1819 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1820 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-share",
1821 impl->icon_size, 0, NULL);
1826 g_assert_not_reached ();
1832 gtk_list_store_append (impl->shortcuts_model, &iter);
1834 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1836 shortcuts_update_count (impl, type, 1);
1838 gtk_list_store_set (impl->shortcuts_model, &iter,
1839 SHORTCUTS_COL_PIXBUF, pixbuf,
1840 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1841 SHORTCUTS_COL_NAME, label_copy,
1842 SHORTCUTS_COL_DATA, data,
1843 SHORTCUTS_COL_TYPE, shortcut_type,
1844 SHORTCUTS_COL_REMOVABLE, removable,
1845 SHORTCUTS_COL_HANDLE, NULL,
1848 if (impl->shortcuts_pane_filter_model)
1849 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1851 if (impl->shortcuts_combo_filter_model)
1852 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1854 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1856 /* The current folder is updated via _activate_iter(), don't
1857 * have save_folder_combo_changed_cb() call _activate_iter()
1860 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1862 if (impl->has_search)
1865 if (impl->has_recent)
1868 g_signal_handlers_block_by_func (impl->save_folder_combo,
1869 G_CALLBACK (save_folder_combo_changed_cb),
1872 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1873 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1874 G_CALLBACK (save_folder_combo_changed_cb),
1878 g_free (label_copy);
1881 g_object_unref (pixbuf);
1883 profile_end ("end", NULL);
1887 shortcuts_append_search (GtkFileChooserDefault *impl)
1892 pixbuf = render_search_icon (impl);
1894 gtk_list_store_append (impl->shortcuts_model, &iter);
1895 gtk_list_store_set (impl->shortcuts_model, &iter,
1896 SHORTCUTS_COL_PIXBUF, pixbuf,
1897 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1898 SHORTCUTS_COL_NAME, _("Search"),
1899 SHORTCUTS_COL_DATA, NULL,
1900 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1901 SHORTCUTS_COL_REMOVABLE, FALSE,
1905 g_object_unref (pixbuf);
1907 impl->has_search = TRUE;
1911 shortcuts_append_recent (GtkFileChooserDefault *impl)
1916 pixbuf = render_recent_icon (impl);
1918 gtk_list_store_append (impl->shortcuts_model, &iter);
1919 gtk_list_store_set (impl->shortcuts_model, &iter,
1920 SHORTCUTS_COL_PIXBUF, pixbuf,
1921 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1922 SHORTCUTS_COL_NAME, _("Recently Used"),
1923 SHORTCUTS_COL_DATA, NULL,
1924 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1925 SHORTCUTS_COL_REMOVABLE, FALSE,
1929 g_object_unref (pixbuf);
1931 impl->has_recent = TRUE;
1934 /* Appends an item for the user's home directory to the shortcuts model */
1936 shortcuts_append_home (GtkFileChooserDefault *impl)
1939 GtkFilePath *home_path;
1941 profile_start ("start", NULL);
1943 home = g_get_home_dir ();
1946 profile_end ("end - no home directory!?", NULL);
1950 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1952 shortcuts_insert_path (impl, -1, SHORTCUT_TYPE_PATH, NULL, home_path, NULL, FALSE, SHORTCUTS_HOME);
1953 impl->has_home = TRUE;
1955 gtk_file_path_free (home_path);
1957 profile_end ("end", NULL);
1960 /* Appends the ~/Desktop directory to the shortcuts model */
1962 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1967 profile_start ("start", NULL);
1969 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1970 path = gtk_file_system_filename_to_path (impl->file_system, name);
1971 shortcuts_insert_path (impl, -1, SHORTCUT_TYPE_PATH, NULL, path, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1972 impl->has_desktop = TRUE;
1974 /* We do not actually pop up an error dialog if there is no desktop directory
1975 * because some people may really not want to have one.
1978 gtk_file_path_free (path);
1980 profile_end ("end", NULL);
1983 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1985 shortcuts_append_paths (GtkFileChooserDefault *impl,
1992 profile_start ("start", NULL);
1994 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1997 for (; paths; paths = paths->next)
2003 if (impl->local_only &&
2004 !gtk_file_system_path_is_local (impl->file_system, path))
2006 if (shortcut_find_position (impl, path) != -1)
2009 label = gtk_file_system_get_bookmark_label (impl->file_system, path);
2011 /* NULL GError, but we don't really want to show error boxes here */
2012 shortcuts_insert_path (impl, start_row + num_inserted, SHORTCUT_TYPE_PATH, NULL, path, label, TRUE, SHORTCUTS_BOOKMARKS);
2018 profile_end ("end", NULL);
2020 return num_inserted;
2023 /* Returns the index for the corresponding item in the shortcuts bar */
2025 shortcuts_get_index (GtkFileChooserDefault *impl,
2026 ShortcutsIndex where)
2032 if (where == SHORTCUTS_SEARCH)
2035 n += impl->has_search ? 1 : 0;
2037 if (where == SHORTCUTS_RECENT)
2040 n += impl->has_recent ? 1 : 0;
2042 if (where == SHORTCUTS_RECENT_SEPARATOR)
2045 n += impl->has_recent ? 1 : 0;
2047 if (where == SHORTCUTS_HOME)
2050 n += impl->has_home ? 1 : 0;
2052 if (where == SHORTCUTS_DESKTOP)
2055 n += impl->has_desktop ? 1 : 0;
2057 if (where == SHORTCUTS_VOLUMES)
2060 n += impl->num_volumes;
2062 if (where == SHORTCUTS_SHORTCUTS)
2065 n += impl->num_shortcuts;
2067 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2070 /* If there are no bookmarks there won't be a separator */
2071 n += (impl->num_bookmarks > 0) ? 1 : 0;
2073 if (where == SHORTCUTS_BOOKMARKS)
2076 n += impl->num_bookmarks;
2078 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2083 if (where == SHORTCUTS_CURRENT_FOLDER)
2086 g_assert_not_reached ();
2093 /* Adds all the file system volumes to the shortcuts model */
2095 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2100 gboolean old_changing_folders;
2102 profile_start ("start", NULL);
2105 old_changing_folders = impl->changing_folder;
2106 impl->changing_folder = TRUE;
2108 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2109 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2110 impl->num_volumes = 0;
2112 list = gtk_file_system_list_volumes (impl->file_system);
2116 for (l = list; l; l = l->next)
2118 GtkFileSystemVolume *volume;
2122 if (impl->local_only)
2124 if (gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
2126 GtkFilePath *base_path;
2128 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2129 if (base_path != NULL)
2131 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
2132 gtk_file_path_free (base_path);
2136 gtk_file_system_volume_free (impl->file_system, volume);
2143 shortcuts_insert_path (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2147 impl->num_volumes = n;
2148 g_slist_free (list);
2150 if (impl->shortcuts_pane_filter_model)
2151 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2153 if (impl->shortcuts_combo_filter_model)
2154 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2156 impl->changing_folder = old_changing_folders;
2158 profile_end ("end", NULL);
2161 /* Inserts a separator node in the shortcuts list */
2163 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2164 ShortcutsIndex where)
2168 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2169 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2170 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2172 gtk_list_store_insert (impl->shortcuts_model, &iter,
2173 shortcuts_get_index (impl, where));
2174 gtk_list_store_set (impl->shortcuts_model, &iter,
2175 SHORTCUTS_COL_PIXBUF, NULL,
2176 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2177 SHORTCUTS_COL_NAME, NULL,
2178 SHORTCUTS_COL_DATA, NULL,
2179 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2183 /* Updates the list of bookmarks */
2185 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2188 gboolean old_changing_folders;
2190 GtkFilePath *list_selected = NULL;
2191 GtkFilePath *combo_selected = NULL;
2192 ShortcutType shortcut_type;
2195 profile_start ("start", NULL);
2197 old_changing_folders = impl->changing_folder;
2198 impl->changing_folder = TRUE;
2200 if (shortcuts_get_selected (impl, &iter))
2202 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2204 SHORTCUTS_COL_DATA, &col_data,
2205 SHORTCUTS_COL_TYPE, &shortcut_type,
2208 if (col_data && shortcut_type == SHORTCUT_TYPE_PATH)
2209 list_selected = gtk_file_path_copy (col_data);
2212 if (impl->save_folder_combo &&
2213 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2216 GtkTreeIter child_iter;
2218 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2221 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2223 SHORTCUTS_COL_DATA, &col_data,
2224 SHORTCUTS_COL_TYPE, &shortcut_type,
2227 if (col_data && shortcut_type == SHORTCUT_TYPE_PATH)
2228 combo_selected = gtk_file_path_copy (col_data);
2231 if (impl->num_bookmarks > 0)
2232 shortcuts_remove_rows (impl,
2233 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2234 impl->num_bookmarks + 1);
2236 impl->num_bookmarks = 0;
2237 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2239 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
2240 shortcuts_append_paths (impl, bookmarks);
2241 gtk_file_paths_free (bookmarks);
2243 if (impl->num_bookmarks == 0)
2244 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2246 if (impl->shortcuts_pane_filter_model)
2247 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2249 if (impl->shortcuts_combo_filter_model)
2250 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2254 shortcuts_find_folder (impl, list_selected);
2255 gtk_file_path_free (list_selected);
2262 pos = shortcut_find_position (impl, combo_selected);
2265 if (impl->has_search)
2268 if (impl->has_recent)
2271 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2274 gtk_file_path_free (combo_selected);
2277 impl->changing_folder = old_changing_folders;
2279 profile_end ("end", NULL);
2282 /* Appends a separator and a row to the shortcuts list for the current folder */
2284 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2289 g_assert (!impl->shortcuts_current_folder_active);
2293 g_assert (impl->current_folder != NULL);
2295 pos = shortcut_find_position (impl, impl->current_folder);
2298 GtkFileSystemVolume *volume;
2299 GtkFilePath *base_path;
2303 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2307 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2309 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
2311 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2316 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
2318 shortcuts_insert_path (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2322 shortcuts_insert_path (impl, pos, SHORTCUT_TYPE_PATH, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2324 gtk_file_system_volume_free (impl->file_system, volume);
2328 gtk_file_path_free (base_path);
2330 else if (impl->save_folder_combo != NULL)
2332 if (impl->has_search)
2335 if (impl->has_recent)
2336 pos -= 2; /* + separator */
2338 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2342 /* Updates the current folder row in the shortcuts model */
2344 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2348 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2350 if (impl->shortcuts_current_folder_active)
2352 shortcuts_remove_rows (impl, pos, 2);
2353 impl->shortcuts_current_folder_active = FALSE;
2356 shortcuts_add_current_folder (impl);
2359 /* Filter function used for the shortcuts filter model */
2361 shortcuts_pane_filter_cb (GtkTreeModel *model,
2365 GtkFileChooserDefault *impl;
2369 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2371 path = gtk_tree_model_get_path (model, iter);
2375 pos = *gtk_tree_path_get_indices (path);
2376 gtk_tree_path_free (path);
2378 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2381 /* Creates the list model for shortcuts */
2383 shortcuts_model_create (GtkFileChooserDefault *impl)
2385 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2386 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2387 GDK_TYPE_PIXBUF, /* pixbuf */
2388 G_TYPE_STRING, /* name */
2389 G_TYPE_POINTER, /* path or volume */
2390 G_TYPE_INT, /* ShortcutType */
2391 G_TYPE_BOOLEAN, /* removable */
2392 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2393 G_TYPE_POINTER); /* GtkFileSystemHandle */
2395 if (search_is_possible (impl))
2397 shortcuts_append_search (impl);
2400 if (impl->recent_manager)
2402 shortcuts_append_recent (impl);
2403 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2406 if (impl->file_system)
2408 shortcuts_append_home (impl);
2409 shortcuts_append_desktop (impl);
2410 shortcuts_add_volumes (impl);
2413 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2414 GTK_TREE_MODEL (impl->shortcuts_model),
2417 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2418 shortcuts_pane_filter_cb,
2423 /* Callback used when the "New Folder" button is clicked */
2425 new_folder_button_clicked (GtkButton *button,
2426 GtkFileChooserDefault *impl)
2431 if (!impl->browse_files_model)
2432 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2434 /* Prevent button from being clicked twice */
2435 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2437 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2439 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2440 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2441 path, impl->list_name_column,
2444 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2445 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2447 impl->list_name_column,
2450 gtk_tree_path_free (path);
2454 edited_idle_create_folder_cb (GtkFileSystemHandle *handle,
2455 const GtkFilePath *path,
2456 const GError *error,
2459 gboolean cancelled = handle->cancelled;
2460 GtkFileChooserDefault *impl = data;
2462 if (!g_slist_find (impl->pending_handles, handle))
2465 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
2471 change_folder_and_display_error (impl, path, FALSE);
2473 error_creating_folder_dialog (impl, path, g_error_copy (error));
2476 g_object_unref (impl);
2477 g_object_unref (handle);
2480 /* Idle handler for creating a new folder after editing its name cell, or for
2481 * canceling the editing.
2484 edited_idle_cb (GtkFileChooserDefault *impl)
2486 GDK_THREADS_ENTER ();
2488 g_source_destroy (impl->edited_idle);
2489 impl->edited_idle = NULL;
2491 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2492 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2494 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2496 if (impl->edited_new_text) /* not cancelled? */
2499 GtkFilePath *file_path;
2502 file_path = gtk_file_system_make_path (impl->file_system,
2503 impl->current_folder,
2504 impl->edited_new_text,
2508 GtkFileSystemHandle *handle;
2510 handle = gtk_file_system_create_folder (impl->file_system, file_path,
2511 edited_idle_create_folder_cb,
2512 g_object_ref (impl));
2513 impl->pending_handles = g_slist_append (impl->pending_handles, handle);
2515 gtk_file_path_free (file_path);
2518 error_creating_folder_dialog (impl, file_path, error);
2520 g_free (impl->edited_new_text);
2521 impl->edited_new_text = NULL;
2524 GDK_THREADS_LEAVE ();
2530 queue_edited_idle (GtkFileChooserDefault *impl,
2531 const gchar *new_text)
2533 /* We create the folder in an idle handler so that we don't modify the tree
2537 if (!impl->edited_idle)
2539 impl->edited_idle = g_idle_source_new ();
2540 g_source_set_closure (impl->edited_idle,
2541 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2543 g_source_attach (impl->edited_idle, NULL);
2546 g_free (impl->edited_new_text);
2547 impl->edited_new_text = g_strdup (new_text);
2550 /* Callback used from the text cell renderer when the new folder is named */
2552 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2554 const gchar *new_text,
2555 GtkFileChooserDefault *impl)
2557 /* work around bug #154921 */
2558 g_object_set (cell_renderer_text,
2559 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2560 queue_edited_idle (impl, new_text);
2563 /* Callback used from the text cell renderer when the new folder edition gets
2567 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2568 GtkFileChooserDefault *impl)
2570 /* work around bug #154921 */
2571 g_object_set (cell_renderer_text,
2572 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2573 queue_edited_idle (impl, NULL);
2576 /* Creates the widgets for the filter combo box */
2578 filter_create (GtkFileChooserDefault *impl)
2580 impl->filter_combo = gtk_combo_box_new_text ();
2581 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2583 g_signal_connect (impl->filter_combo, "changed",
2584 G_CALLBACK (filter_combo_changed), impl);
2586 gtk_widget_set_tooltip_text (impl->filter_combo,
2587 _("Select which types of files are shown"));
2589 return impl->filter_combo;
2593 button_new (GtkFileChooserDefault *impl,
2595 const char *stock_id,
2603 button = gtk_button_new_with_mnemonic (text);
2604 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2605 gtk_button_set_image (GTK_BUTTON (button), image);
2607 gtk_widget_set_sensitive (button, sensitive);
2608 g_signal_connect (button, "clicked", callback, impl);
2611 gtk_widget_show (button);
2616 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2618 shortcut_find_position (GtkFileChooserDefault *impl,
2619 const GtkFilePath *path)
2623 int current_folder_separator_idx;
2625 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2628 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2631 /* FIXME: is this still needed? */
2632 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2636 for (i = 0; i < current_folder_separator_idx; i++)
2639 ShortcutType shortcut_type;
2641 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2642 SHORTCUTS_COL_DATA, &col_data,
2643 SHORTCUTS_COL_TYPE, &shortcut_type,
2648 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2650 GtkFileSystemVolume *volume;
2651 GtkFilePath *base_path;
2655 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2657 exists = base_path && strcmp (gtk_file_path_get_string (path),
2658 gtk_file_path_get_string (base_path)) == 0;
2664 else if (shortcut_type == SHORTCUT_TYPE_PATH)
2666 GtkFilePath *model_path;
2668 model_path = col_data;
2670 if (model_path && gtk_file_path_compare (model_path, path) == 0)
2675 if (i < current_folder_separator_idx - 1)
2677 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2678 g_assert_not_reached ();
2685 /* Tries to add a bookmark from a path name */
2687 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
2688 const GtkFilePath *path,
2693 g_return_val_if_fail (path != NULL, FALSE);
2695 if (shortcut_find_position (impl, path) != -1)
2699 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
2701 error_adding_bookmark_dialog (impl, path, error);
2709 add_bookmark_foreach_cb (GtkTreeModel *model,
2714 GtkFileChooserDefault *impl;
2715 GtkFileSystemModel *fs_model;
2716 GtkTreeIter child_iter;
2717 const GtkFilePath *file_path;
2719 impl = (GtkFileChooserDefault *) data;
2721 switch (impl->operation_mode)
2723 case OPERATION_MODE_BROWSE:
2724 fs_model = impl->browse_files_model;
2725 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2726 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
2729 case OPERATION_MODE_SEARCH:
2730 search_get_valid_child_iter (impl, &child_iter, iter);
2731 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2732 SEARCH_MODEL_COL_PATH, &file_path,
2736 case OPERATION_MODE_RECENT:
2737 recent_get_valid_child_iter (impl, &child_iter, iter);
2738 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2739 RECENT_MODEL_COL_PATH, &file_path,
2744 shortcuts_add_bookmark_from_path (impl, file_path, -1);
2747 /* Adds a bookmark from the currently selected item in the file list */
2749 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2751 GtkTreeSelection *selection;
2753 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2755 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2756 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2758 gtk_tree_selection_selected_foreach (selection,
2759 add_bookmark_foreach_cb,
2763 /* Callback used when the "Add bookmark" button is clicked */
2765 add_bookmark_button_clicked_cb (GtkButton *button,
2766 GtkFileChooserDefault *impl)
2768 bookmarks_add_selected_folder (impl);
2771 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2772 * returns FALSE if no shortcut is selected.
2775 shortcuts_get_selected (GtkFileChooserDefault *impl,
2778 GtkTreeSelection *selection;
2779 GtkTreeIter parent_iter;
2781 if (!impl->browse_shortcuts_tree_view)
2784 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2786 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2789 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2795 /* Removes the selected bookmarks */
2797 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2805 if (!shortcuts_get_selected (impl, &iter))
2808 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2809 SHORTCUTS_COL_DATA, &col_data,
2810 SHORTCUTS_COL_REMOVABLE, &removable,
2816 g_assert (col_data != NULL);
2821 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2822 error_removing_bookmark_dialog (impl, path, error);
2825 /* Callback used when the "Remove bookmark" button is clicked */
2827 remove_bookmark_button_clicked_cb (GtkButton *button,
2828 GtkFileChooserDefault *impl)
2830 remove_selected_bookmarks (impl);
2833 struct selection_check_closure {
2834 GtkFileChooserDefault *impl;
2837 gboolean all_folders;
2840 /* Used from gtk_tree_selection_selected_foreach() */
2842 selection_check_foreach_cb (GtkTreeModel *model,
2847 struct selection_check_closure *closure;
2848 GtkTreeIter child_iter;
2849 const GtkFileInfo *info;
2853 closure->num_selected++;
2855 switch (closure->impl->operation_mode)
2857 case OPERATION_MODE_BROWSE:
2858 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2859 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2860 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2863 case OPERATION_MODE_SEARCH:
2864 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2865 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2866 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2870 case OPERATION_MODE_RECENT:
2871 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2872 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2873 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2878 closure->all_folders = closure->all_folders && is_folder;
2879 closure->all_files = closure->all_files && !is_folder;
2882 /* Checks whether the selected items in the file list are all files or all folders */
2884 selection_check (GtkFileChooserDefault *impl,
2886 gboolean *all_files,
2887 gboolean *all_folders)
2889 struct selection_check_closure closure;
2890 GtkTreeSelection *selection;
2892 closure.impl = impl;
2893 closure.num_selected = 0;
2894 closure.all_files = TRUE;
2895 closure.all_folders = TRUE;
2897 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2898 gtk_tree_selection_selected_foreach (selection,
2899 selection_check_foreach_cb,
2902 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2905 *num_selected = closure.num_selected;
2908 *all_files = closure.all_files;
2911 *all_folders = closure.all_folders;
2914 struct get_selected_path_closure {
2915 GtkFileChooserDefault *impl;
2916 const GtkFilePath *path;
2920 get_selected_path_foreach_cb (GtkTreeModel *model,
2925 struct get_selected_path_closure *closure;
2926 GtkTreeIter child_iter;
2930 switch (closure->impl->operation_mode)
2932 case OPERATION_MODE_BROWSE:
2933 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2934 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2937 case OPERATION_MODE_SEARCH:
2938 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2939 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2940 SEARCH_MODEL_COL_PATH, &closure->path,
2944 case OPERATION_MODE_RECENT:
2945 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2946 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2947 RECENT_MODEL_COL_PATH, &closure->path,
2953 /* Returns a selected path from the file list */
2954 static const GtkFilePath *
2955 get_selected_path (GtkFileChooserDefault *impl)
2957 struct get_selected_path_closure closure;
2958 GtkTreeSelection *selection;
2960 closure.impl = impl;
2961 closure.path = NULL;
2963 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2964 gtk_tree_selection_selected_foreach (selection,
2965 get_selected_path_foreach_cb,
2968 return closure.path;
2972 GtkFileChooserDefault *impl;
2974 } UpdateTooltipData;
2977 update_tooltip (GtkTreeModel *model,
2982 UpdateTooltipData *udata = data;
2983 GtkTreeIter child_iter;
2984 const GtkFileInfo *info;
2986 if (udata->tip == NULL)
2988 const gchar *display_name;
2990 switch (udata->impl->operation_mode)
2992 case OPERATION_MODE_BROWSE:
2993 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2996 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2997 display_name = gtk_file_info_get_display_name (info);
3000 case OPERATION_MODE_SEARCH:
3001 search_get_valid_child_iter (udata->impl, &child_iter, iter);
3002 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
3003 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
3007 case OPERATION_MODE_RECENT:
3008 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
3009 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
3010 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
3015 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
3021 /* Sensitize the "add bookmark" button if all the selected items are folders, or
3022 * if there are no selected items *and* the current folder is not in the
3023 * bookmarks list. De-sensitize the button otherwise.
3026 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
3029 gboolean all_folders;
3033 selection_check (impl, &num_selected, NULL, &all_folders);
3035 if (num_selected == 0)
3036 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
3037 else if (num_selected == 1)
3039 const GtkFilePath *path;
3041 path = get_selected_path (impl);
3042 active = all_folders && (shortcut_find_position (impl, path) == -1);
3045 active = all_folders;
3047 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
3049 if (impl->browse_files_popup_menu_add_shortcut_item)
3050 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
3051 (num_selected == 0) ? FALSE : active);
3055 if (num_selected == 0)
3056 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
3057 else if (num_selected > 1)
3058 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
3061 GtkTreeSelection *selection;
3062 UpdateTooltipData data;
3064 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3067 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
3071 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
3076 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
3077 * bookmark row is selected in the shortcuts tree.
3080 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
3083 gboolean removable = FALSE;
3086 if (shortcuts_get_selected (impl, &iter))
3087 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3088 SHORTCUTS_COL_REMOVABLE, &removable,
3089 SHORTCUTS_COL_NAME, &name,
3092 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
3098 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3099 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3107 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3110 gboolean removable = FALSE;
3112 if (impl->browse_shortcuts_popup_menu == NULL)
3115 if (shortcuts_get_selected (impl, &iter))
3116 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3117 SHORTCUTS_COL_REMOVABLE, &removable,
3120 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3121 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3124 /* GtkWidget::drag-begin handler for the shortcuts list. */
3126 shortcuts_drag_begin_cb (GtkWidget *widget,
3127 GdkDragContext *context,
3128 GtkFileChooserDefault *impl)
3131 impl->shortcuts_drag_context = g_object_ref (context);
3136 /* Removes the idle handler for outside drags */
3138 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3140 if (!impl->shortcuts_drag_outside_idle)
3143 g_source_destroy (impl->shortcuts_drag_outside_idle);
3144 impl->shortcuts_drag_outside_idle = NULL;
3148 /* GtkWidget::drag-end handler for the shortcuts list. */
3150 shortcuts_drag_end_cb (GtkWidget *widget,
3151 GdkDragContext *context,
3152 GtkFileChooserDefault *impl)
3155 g_object_unref (impl->shortcuts_drag_context);
3157 shortcuts_cancel_drag_outside_idle (impl);
3159 if (!impl->shortcuts_drag_outside)
3162 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3164 impl->shortcuts_drag_outside = FALSE;
3168 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3170 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3171 GdkDragContext *context,
3172 GtkFileChooserDefault *impl)
3174 g_signal_stop_emission_by_name (widget, "drag_data_delete");
3178 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3182 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3185 GtkTreeView *tree_view;
3188 GdkPixmap *row_pixmap;
3193 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3195 /* Find the selected path and get its drag pixmap */
3197 if (!shortcuts_get_selected (impl, &iter))
3198 g_assert_not_reached ();
3200 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3202 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3203 gtk_tree_path_free (path);
3212 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3218 GdkPixmap *composite;
3219 int row_pixmap_width, row_pixmap_height;
3220 int pixbuf_width, pixbuf_height;
3221 int composite_width, composite_height;
3222 int pixbuf_x, pixbuf_y;
3223 GdkGC *gc, *mask_gc;
3225 GdkBitmap *pixbuf_mask;
3227 /* Create pixmap and mask for composite image */
3229 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3230 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3231 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3233 composite_width = MAX (row_pixmap_width, pixbuf_width);
3234 composite_height = MAX (row_pixmap_height, pixbuf_height);
3236 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3238 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3241 pixbuf_x = composite_width - pixbuf_width;
3243 pixbuf_y = (composite_height - pixbuf_height) / 2;
3245 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3246 gc = gdk_gc_new (composite);
3248 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3249 mask_gc = gdk_gc_new (mask);
3251 gdk_gc_set_foreground (mask_gc, &color);
3252 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3255 color.green = 0xffff;
3256 color.blue = 0xffff;
3257 gdk_gc_set_rgb_fg_color (gc, &color);
3258 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3260 /* Composite the row pixmap and the pixbuf */
3262 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3264 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3265 NULL, &pixbuf_mask, 128);
3266 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3269 pixbuf_width, pixbuf_height);
3270 g_object_unref (pixbuf_mask);
3272 gdk_draw_drawable (composite, gc, row_pixmap,
3275 row_pixmap_width, row_pixmap_height);
3277 gdk_gc_set_foreground (mask_gc, &color);
3278 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3280 gdk_draw_pixbuf (composite, gc, pixbuf,
3283 pixbuf_width, pixbuf_height,
3287 g_object_unref (pixbuf);
3288 g_object_unref (row_pixmap);
3290 row_pixmap = composite;
3294 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3296 gtk_tree_view_get_path_at_pos (tree_view,
3297 tree_view->priv->press_start_x,
3298 tree_view->priv->press_start_y,
3304 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3305 gdk_drawable_get_colormap (row_pixmap),
3308 tree_view->priv->press_start_x + 1,
3309 row_pixmap_y + cell_y + 1);
3311 g_object_unref (row_pixmap);
3313 g_object_unref (mask);
3316 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3317 * handler so that we can tell apart the drag_leave event that comes right
3318 * before a drag_drop, from a normal drag_leave. We don't want to set the
3319 * cursor nor the flag in the latter case.
3322 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3324 GDK_THREADS_ENTER ();
3326 shortcuts_drag_set_delete_cursor (impl, TRUE);
3327 impl->shortcuts_drag_outside = TRUE;
3329 shortcuts_cancel_drag_outside_idle (impl);
3331 GDK_THREADS_LEAVE ();
3337 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3341 shortcuts_drag_leave_cb (GtkWidget *widget,
3342 GdkDragContext *context,
3344 GtkFileChooserDefault *impl)
3347 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3349 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3350 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3351 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3353 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3357 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3359 GTK_TREE_VIEW_DROP_BEFORE);
3361 g_signal_stop_emission_by_name (widget, "drag_leave");
3364 /* Computes the appropriate row and position for dropping */
3366 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3370 GtkTreeViewDropPosition *pos)
3372 GtkTreeView *tree_view;
3373 GtkTreeViewColumn *column;
3377 int bookmarks_index;
3379 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3381 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3383 if (!gtk_tree_view_get_path_at_pos (tree_view,
3385 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3391 row = bookmarks_index + impl->num_bookmarks - 1;
3392 *path = gtk_tree_path_new_from_indices (row, -1);
3393 *pos = GTK_TREE_VIEW_DROP_AFTER;
3397 row = *gtk_tree_path_get_indices (*path);
3398 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3399 gtk_tree_path_free (*path);
3401 if (row < bookmarks_index)
3403 row = bookmarks_index;
3404 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3406 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3408 row = bookmarks_index + impl->num_bookmarks - 1;
3409 *pos = GTK_TREE_VIEW_DROP_AFTER;
3413 if (cell_y < cell.height / 2)
3414 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3416 *pos = GTK_TREE_VIEW_DROP_AFTER;
3419 *path = gtk_tree_path_new_from_indices (row, -1);
3422 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3423 * implement the destination side of DnD by hand, due to limitations in
3424 * GtkTreeView's DnD API.
3427 shortcuts_drag_motion_cb (GtkWidget *widget,
3428 GdkDragContext *context,
3432 GtkFileChooserDefault *impl)
3435 GtkTreeViewDropPosition pos;
3436 GdkDragAction action;
3439 if (gtk_drag_get_source_widget (context) == widget)
3441 shortcuts_cancel_drag_outside_idle (impl);
3443 if (impl->shortcuts_drag_outside)
3445 shortcuts_drag_set_delete_cursor (impl, FALSE);
3446 impl->shortcuts_drag_outside = FALSE;
3451 if (context->suggested_action == GDK_ACTION_COPY ||
3452 (context->actions & GDK_ACTION_COPY) != 0)
3453 action = GDK_ACTION_COPY;
3454 else if (context->suggested_action == GDK_ACTION_MOVE ||
3455 (context->actions & GDK_ACTION_MOVE) != 0)
3456 action = GDK_ACTION_MOVE;
3463 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3464 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3465 gtk_tree_path_free (path);
3469 g_signal_stop_emission_by_name (widget, "drag_motion");
3473 gdk_drag_status (context, action, time_);
3480 /* GtkWidget::drag-drop handler for the shortcuts list. */
3482 shortcuts_drag_drop_cb (GtkWidget *widget,
3483 GdkDragContext *context,
3487 GtkFileChooserDefault *impl)
3490 shortcuts_cancel_drag_outside_idle (impl);
3493 g_signal_stop_emission_by_name (widget, "drag_drop");
3497 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3499 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3506 uris = g_uri_list_extract_uris (data);
3508 for (i = 0; uris[i]; i++)
3514 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3518 if (shortcuts_add_bookmark_from_path (impl, path, position))
3521 gtk_file_path_free (path);
3525 GError *error = NULL;
3527 g_set_error (&error,
3528 GTK_FILE_CHOOSER_ERROR,
3529 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3530 _("Could not add a bookmark for '%s' "
3531 "because it is an invalid path name."),
3533 error_adding_bookmark_dialog (impl, path, error);
3540 /* Reorders the selected bookmark to the specified position */
3542 shortcuts_reorder (GtkFileChooserDefault *impl,
3547 ShortcutType shortcut_type;
3550 int bookmarks_index;
3551 const GtkFilePath *file_path;
3552 GtkFilePath *file_path_copy;
3556 /* Get the selected path */
3558 if (!shortcuts_get_selected (impl, &iter))
3559 g_assert_not_reached ();
3561 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3562 old_position = *gtk_tree_path_get_indices (path);
3563 gtk_tree_path_free (path);
3565 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3566 old_position -= bookmarks_index;
3567 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3569 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3570 SHORTCUTS_COL_NAME, &name,
3571 SHORTCUTS_COL_DATA, &col_data,
3572 SHORTCUTS_COL_TYPE, &shortcut_type,
3574 g_assert (col_data != NULL);
3575 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
3577 file_path = col_data;
3578 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
3580 /* Remove the path from the old position and insert it in the new one */
3582 if (new_position > old_position)
3585 if (old_position == new_position)
3589 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
3591 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
3592 gtk_file_system_set_bookmark_label (impl->file_system, file_path_copy, name);
3595 error_adding_bookmark_dialog (impl, file_path_copy, error);
3599 gtk_file_path_free (file_path_copy);
3602 /* Callback used when we get the drag data for the bookmarks list. We add the
3603 * received URIs as bookmarks if they are folders.
3606 shortcuts_drag_data_received_cb (GtkWidget *widget,
3607 GdkDragContext *context,
3610 GtkSelectionData *selection_data,
3615 GtkFileChooserDefault *impl;
3616 GtkTreePath *tree_path;
3617 GtkTreeViewDropPosition tree_pos;
3619 int bookmarks_index;
3621 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3623 /* Compute position */
3625 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3627 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3628 position = *gtk_tree_path_get_indices (tree_path);
3629 gtk_tree_path_free (tree_path);
3631 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3634 g_assert (position >= bookmarks_index);
3635 position -= bookmarks_index;
3637 if (selection_data->target == gdk_atom_intern_static_string ("text/uri-list"))
3638 shortcuts_drop_uris (impl, (const char *) selection_data->data, position);
3639 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3640 shortcuts_reorder (impl, position);
3642 g_signal_stop_emission_by_name (widget, "drag_data_received");
3645 /* Callback used when the selection in the shortcuts tree changes */
3647 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3648 GtkFileChooserDefault *impl)
3651 GtkTreeIter child_iter;
3653 bookmarks_check_remove_sensitivity (impl);
3654 shortcuts_check_popup_sensitivity (impl);
3656 if (impl->changing_folder)
3659 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3661 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3664 shortcuts_activate_iter (impl, &child_iter);
3669 shortcuts_row_separator_func (GtkTreeModel *model,
3673 ShortcutType shortcut_type;
3675 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3677 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3680 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3681 * keypresses before the TreeView gets them.
3684 tree_view_keybinding_cb (GtkWidget *tree_view,
3686 GtkFileChooserDefault *impl)
3688 if ((event->keyval == GDK_slash
3689 || event->keyval == GDK_KP_Divide
3691 || event->keyval == GDK_asciitilde
3693 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3695 location_popup_handler (impl, event->string);
3702 /* Callback used when the file list's popup menu is detached */
3704 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3707 GtkFileChooserDefault *impl;
3709 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3710 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3712 impl->browse_shortcuts_popup_menu = NULL;
3713 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3714 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3718 remove_shortcut_cb (GtkMenuItem *item,
3719 GtkFileChooserDefault *impl)
3721 remove_selected_bookmarks (impl);
3724 /* Rename the selected bookmark */
3726 rename_selected_bookmark (GtkFileChooserDefault *impl)
3730 GtkTreeViewColumn *column;
3731 GtkCellRenderer *cell;
3734 if (shortcuts_get_selected (impl, &iter))
3736 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3737 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3738 renderers = gtk_tree_view_column_get_cell_renderers (column);
3739 cell = g_list_nth_data (renderers, 1);
3740 g_list_free (renderers);
3741 g_object_set (cell, "editable", TRUE, NULL);
3742 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3743 path, column, cell, TRUE);
3744 gtk_tree_path_free (path);
3749 rename_shortcut_cb (GtkMenuItem *item,
3750 GtkFileChooserDefault *impl)
3752 rename_selected_bookmark (impl);
3755 /* Constructs the popup menu for the file list if needed */
3757 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3761 if (impl->browse_shortcuts_popup_menu)
3764 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3765 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3766 impl->browse_shortcuts_tree_view,
3767 shortcuts_popup_menu_detach_cb);
3769 item = gtk_image_menu_item_new_with_label (_("Remove"));
3770 impl->browse_shortcuts_popup_menu_remove_item = item;
3771 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3772 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3773 g_signal_connect (item, "activate",
3774 G_CALLBACK (remove_shortcut_cb), impl);
3775 gtk_widget_show (item);
3776 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3778 item = gtk_menu_item_new_with_label (_("Rename..."));
3779 impl->browse_shortcuts_popup_menu_rename_item = item;
3780 g_signal_connect (item, "activate",
3781 G_CALLBACK (rename_shortcut_cb), impl);
3782 gtk_widget_show (item);
3783 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3785 shortcuts_check_popup_sensitivity (impl);
3789 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3791 shortcuts_build_popup_menu (impl);
3795 popup_position_func (GtkMenu *menu,
3799 gpointer user_data);
3802 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3803 GdkEventButton *event)
3805 shortcuts_update_popup_menu (impl);
3807 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3808 NULL, NULL, NULL, NULL,
3809 event->button, event->time);
3812 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3814 popup_position_func, impl->browse_shortcuts_tree_view,
3815 0, GDK_CURRENT_TIME);
3816 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3821 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3823 shortcuts_popup_menu_cb (GtkWidget *widget,
3824 GtkFileChooserDefault *impl)
3826 shortcuts_popup_menu (impl, NULL);
3830 /* Callback used when a button is pressed on the shortcuts list.
3831 * We trap button 3 to bring up a popup menu.
3834 shortcuts_button_press_event_cb (GtkWidget *widget,
3835 GdkEventButton *event,
3836 GtkFileChooserDefault *impl)
3838 static gboolean in_press = FALSE;
3844 if (event->button != 3)
3848 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3854 shortcuts_popup_menu (impl, event);
3859 shortcuts_edited (GtkCellRenderer *cell,
3862 GtkFileChooserDefault *impl)
3866 GtkFilePath *shortcut;
3868 g_object_set (cell, "editable", FALSE, NULL);
3870 path = gtk_tree_path_new_from_string (path_string);
3871 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3872 g_assert_not_reached ();
3874 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3875 SHORTCUTS_COL_DATA, &shortcut,
3877 gtk_tree_path_free (path);
3879 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3883 shortcuts_editing_canceled (GtkCellRenderer *cell,
3884 GtkFileChooserDefault *impl)
3886 g_object_set (cell, "editable", FALSE, NULL);
3889 /* Creates the widgets for the shortcuts and bookmarks tree */
3891 shortcuts_list_create (GtkFileChooserDefault *impl)
3894 GtkTreeSelection *selection;
3895 GtkTreeViewColumn *column;
3896 GtkCellRenderer *renderer;
3898 /* Scrolled window */
3900 swin = gtk_scrolled_window_new (NULL, NULL);
3901 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3902 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3903 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3905 gtk_widget_show (swin);
3909 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3910 #ifdef PROFILE_FILE_CHOOSER
3911 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3913 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3914 G_CALLBACK (tree_view_keybinding_cb), impl);
3915 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3916 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3917 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3918 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3919 /* Accessible object name for the file chooser's shortcuts pane */
3920 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3922 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3924 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3926 shortcuts_source_targets,
3927 num_shortcuts_source_targets,
3930 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3931 GTK_DEST_DEFAULT_ALL,
3932 shortcuts_dest_targets,
3933 num_shortcuts_dest_targets,
3934 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3936 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3937 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3938 gtk_tree_selection_set_select_function (selection,
3939 shortcuts_select_func,
3942 g_signal_connect (selection, "changed",
3943 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3945 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3946 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3948 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3949 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3950 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3951 G_CALLBACK (shortcuts_drag_end_cb), impl);
3952 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3953 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3955 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3956 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3957 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3958 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3959 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3960 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3961 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3962 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3964 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3965 gtk_widget_show (impl->browse_shortcuts_tree_view);
3969 column = gtk_tree_view_column_new ();
3970 /* Column header for the file chooser's shortcuts pane */
3971 gtk_tree_view_column_set_title (column, _("_Places"));
3973 renderer = gtk_cell_renderer_pixbuf_new ();
3974 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3975 gtk_tree_view_column_set_attributes (column, renderer,
3976 "pixbuf", SHORTCUTS_COL_PIXBUF,
3977 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3980 renderer = gtk_cell_renderer_text_new ();
3981 g_signal_connect (renderer, "edited",
3982 G_CALLBACK (shortcuts_edited), impl);
3983 g_signal_connect (renderer, "editing-canceled",
3984 G_CALLBACK (shortcuts_editing_canceled), impl);
3985 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3986 gtk_tree_view_column_set_attributes (column, renderer,
3987 "text", SHORTCUTS_COL_NAME,
3990 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3991 shortcuts_row_separator_func,
3994 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3999 /* Creates the widgets for the shortcuts/bookmarks pane */
4001 shortcuts_pane_create (GtkFileChooserDefault *impl,
4002 GtkSizeGroup *size_group)
4008 vbox = gtk_vbox_new (FALSE, 6);
4009 gtk_widget_show (vbox);
4011 /* Shortcuts tree */
4013 widget = shortcuts_list_create (impl);
4014 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
4016 /* Box for buttons */
4018 hbox = gtk_hbox_new (TRUE, 6);
4019 gtk_size_group_add_widget (size_group, hbox);
4020 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4021 gtk_widget_show (hbox);
4023 /* Add bookmark button */
4025 impl->browse_shortcuts_add_button = button_new (impl,
4030 G_CALLBACK (add_bookmark_button_clicked_cb));
4031 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
4032 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
4033 _("Add the selected folder to the Bookmarks"));
4035 /* Remove bookmark button */
4037 impl->browse_shortcuts_remove_button = button_new (impl,
4042 G_CALLBACK (remove_bookmark_button_clicked_cb));
4043 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4044 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4045 _("Remove the selected bookmark"));
4050 /* Handles key press events on the file list, so that we can trap Enter to
4051 * activate the default button on our own. Also, checks to see if '/' has been
4052 * pressed. See comment by tree_view_keybinding_cb() for more details.
4055 trap_activate_cb (GtkWidget *widget,
4059 GtkFileChooserDefault *impl;
4062 impl = (GtkFileChooserDefault *) data;
4064 modifiers = gtk_accelerator_get_default_mod_mask ();
4066 if ((event->keyval == GDK_slash
4067 || event->keyval == GDK_KP_Divide
4069 || event->keyval == GDK_asciitilde
4071 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4073 location_popup_handler (impl, event->string);
4077 if ((event->keyval == GDK_Return
4078 || event->keyval == GDK_ISO_Enter
4079 || event->keyval == GDK_KP_Enter
4080 || event->keyval == GDK_space
4081 || event->keyval == GDK_KP_Space)
4082 && ((event->state & modifiers) == 0)
4083 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4084 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4088 window = get_toplevel (widget);
4090 && widget != window->default_widget
4091 && !(widget == window->focus_widget &&
4092 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4094 gtk_window_activate_default (window);
4102 /* Callback used when the file list's popup menu is detached */
4104 popup_menu_detach_cb (GtkWidget *attach_widget,
4107 GtkFileChooserDefault *impl;
4109 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4110 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4112 impl->browse_files_popup_menu = NULL;
4113 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4114 impl->browse_files_popup_menu_hidden_files_item = NULL;
4117 /* Callback used when the "Add to Bookmarks" menu item is activated */
4119 add_to_shortcuts_cb (GtkMenuItem *item,
4120 GtkFileChooserDefault *impl)
4122 bookmarks_add_selected_folder (impl);
4125 /* Callback used when the "Show Hidden Files" menu item is toggled */
4127 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4128 GtkFileChooserDefault *impl)
4131 "show-hidden", gtk_check_menu_item_get_active (item),
4135 /* Shows an error dialog about not being able to select a dragged file */
4137 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4138 const GtkFilePath *path,
4142 _("Could not select file"),
4147 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4152 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4154 for (i = 1; uris[i]; i++)
4159 path = gtk_file_system_uri_to_path (impl->file_system, uri);
4163 GError *error = NULL;
4165 gtk_file_chooser_default_select_path (chooser, path, &error);
4167 error_selecting_dragged_file_dialog (impl, path, error);
4169 gtk_file_path_free (path);
4174 struct FileListDragData
4176 GtkFileChooserDefault *impl;
4182 file_list_drag_data_received_get_info_cb (GtkFileSystemHandle *handle,
4183 const GtkFileInfo *info,
4184 const GError *error,
4187 gboolean cancelled = handle->cancelled;
4188 struct FileListDragData *data = user_data;
4189 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4191 if (handle != data->impl->file_list_drag_data_received_handle)
4194 data->impl->file_list_drag_data_received_handle = NULL;
4196 if (cancelled || error)
4199 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4200 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4201 data->uris[1] == 0 && !error &&
4202 gtk_file_info_get_is_folder (info))
4203 change_folder_and_display_error (data->impl, data->path, FALSE);
4206 GError *error = NULL;
4208 gtk_file_chooser_default_unselect_all (chooser);
4209 gtk_file_chooser_default_select_path (chooser, data->path, &error);
4211 error_selecting_dragged_file_dialog (data->impl, data->path, error);
4213 browse_files_center_selected_row (data->impl);
4216 if (data->impl->select_multiple)
4217 file_list_drag_data_select_uris (data->impl, data->uris);
4220 g_object_unref (data->impl);
4221 g_strfreev (data->uris);
4222 gtk_file_path_free (data->path);
4225 g_object_unref (handle);
4229 file_list_drag_data_received_cb (GtkWidget *widget,
4230 GdkDragContext *context,
4233 GtkSelectionData *selection_data,
4238 GtkFileChooserDefault *impl;
4239 GtkFileChooser *chooser;
4243 GError *error = NULL;
4245 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4246 chooser = GTK_FILE_CHOOSER (data);
4248 /* Parse the text/uri-list string, navigate to the first one */
4249 uris = g_uri_list_extract_uris ((const char *) selection_data->data);
4253 path = gtk_file_system_uri_to_path (impl->file_system, uri);
4257 struct FileListDragData *data;
4259 data = g_new0 (struct FileListDragData, 1);
4260 data->impl = g_object_ref (impl);
4264 if (impl->file_list_drag_data_received_handle)
4265 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
4267 impl->file_list_drag_data_received_handle =
4268 gtk_file_system_get_info (impl->file_system, path,
4269 GTK_FILE_INFO_IS_FOLDER,
4270 file_list_drag_data_received_get_info_cb,
4276 g_set_error (&error,
4277 GTK_FILE_CHOOSER_ERROR,
4278 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
4279 _("Could not select file '%s' "
4280 "because it is an invalid path name."),
4282 error_selecting_dragged_file_dialog (impl, NULL, error);
4285 if (impl->select_multiple)
4286 file_list_drag_data_select_uris (impl, uris);
4292 g_signal_stop_emission_by_name (widget, "drag_data_received");
4295 /* Don't do anything with the drag_drop signal */
4297 file_list_drag_drop_cb (GtkWidget *widget,
4298 GdkDragContext *context,
4302 GtkFileChooserDefault *impl)
4304 g_signal_stop_emission_by_name (widget, "drag_drop");
4308 /* Disable the normal tree drag motion handler, it makes it look like you're
4309 dropping the dragged item onto a tree item */
4311 file_list_drag_motion_cb (GtkWidget *widget,
4312 GdkDragContext *context,
4316 GtkFileChooserDefault *impl)
4318 g_signal_stop_emission_by_name (widget, "drag_motion");
4322 /* Constructs the popup menu for the file list if needed */
4324 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4328 if (impl->browse_files_popup_menu)
4331 impl->browse_files_popup_menu = gtk_menu_new ();
4332 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4333 impl->browse_files_tree_view,
4334 popup_menu_detach_cb);
4336 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4337 impl->browse_files_popup_menu_add_shortcut_item = item;
4338 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4339 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4340 gtk_widget_set_sensitive (item, FALSE);
4341 g_signal_connect (item, "activate",
4342 G_CALLBACK (add_to_shortcuts_cb), impl);
4343 gtk_widget_show (item);
4344 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4346 item = gtk_separator_menu_item_new ();
4347 gtk_widget_show (item);
4348 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4350 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4351 impl->browse_files_popup_menu_hidden_files_item = item;
4352 g_signal_connect (item, "toggled",
4353 G_CALLBACK (show_hidden_toggled_cb), impl);
4354 gtk_widget_show (item);
4355 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4358 /* Updates the popup menu for the file list, creating it if necessary */
4360 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4362 file_list_build_popup_menu (impl);
4364 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4366 /* The sensitivity of the Add to Bookmarks item is set in
4367 * bookmarks_check_add_sensitivity()
4370 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4371 G_CALLBACK (show_hidden_toggled_cb), impl);
4372 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4374 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4375 G_CALLBACK (show_hidden_toggled_cb), impl);
4379 popup_position_func (GtkMenu *menu,
4385 GtkWidget *widget = GTK_WIDGET (user_data);
4386 GdkScreen *screen = gtk_widget_get_screen (widget);
4389 GdkRectangle monitor;
4391 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4393 gdk_window_get_origin (widget->window, x, y);
4395 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4397 *x += (widget->allocation.width - req.width) / 2;
4398 *y += (widget->allocation.height - req.height) / 2;
4400 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4401 gtk_menu_set_monitor (menu, monitor_num);
4402 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4404 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4405 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4411 file_list_popup_menu (GtkFileChooserDefault *impl,
4412 GdkEventButton *event)
4414 file_list_update_popup_menu (impl);
4416 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4417 NULL, NULL, NULL, NULL,
4418 event->button, event->time);
4421 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4423 popup_position_func, impl->browse_files_tree_view,
4424 0, GDK_CURRENT_TIME);
4425 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4431 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4433 list_popup_menu_cb (GtkWidget *widget,
4434 GtkFileChooserDefault *impl)
4436 file_list_popup_menu (impl, NULL);
4440 /* Callback used when a button is pressed on the file list. We trap button 3 to
4441 * bring up a popup menu.
4444 list_button_press_event_cb (GtkWidget *widget,
4445 GdkEventButton *event,
4446 GtkFileChooserDefault *impl)
4448 static gboolean in_press = FALSE;
4454 if (event->button != 3)
4458 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4461 file_list_popup_menu (impl, event);
4465 /* Sets the sort column IDs for the file list based on the operation mode */
4467 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4469 int name_id, mtime_id;
4471 name_id = mtime_id = 0;
4473 switch (impl->operation_mode)
4475 case OPERATION_MODE_BROWSE:
4476 name_id = FILE_LIST_COL_NAME;
4477 mtime_id = FILE_LIST_COL_MTIME;
4479 case OPERATION_MODE_SEARCH:
4480 name_id = SEARCH_MODEL_COL_PATH;
4481 mtime_id = SEARCH_MODEL_COL_STAT;
4483 case OPERATION_MODE_RECENT:
4484 name_id = RECENT_MODEL_COL_PATH;
4485 mtime_id = RECENT_MODEL_COL_INFO;
4489 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4490 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4494 file_list_query_tooltip_cb (GtkWidget *widget,
4497 gboolean keyboard_tip,
4498 GtkTooltip *tooltip,
4501 GtkFileChooserDefault *impl = user_data;
4502 GtkTreeIter iter, child_iter;
4503 GtkTreePath *path = NULL;
4504 GtkFilePath *file_path = NULL;
4507 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4511 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4519 switch (impl->operation_mode)
4521 case OPERATION_MODE_SEARCH:
4522 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4524 gtk_tree_path_free (path);
4528 search_get_valid_child_iter (impl, &child_iter, &iter);
4529 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4530 SEARCH_MODEL_COL_PATH, &file_path,
4534 case OPERATION_MODE_RECENT:
4535 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4537 gtk_tree_path_free (path);
4541 recent_get_valid_child_iter (impl, &child_iter, &iter);
4542 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4543 RECENT_MODEL_COL_PATH, &file_path,
4547 case OPERATION_MODE_BROWSE:
4548 g_assert_not_reached ();
4554 gtk_tree_path_free (path);
4558 filename = gtk_file_system_path_to_filename (impl->file_system, file_path);
4559 gtk_tooltip_set_text (tooltip, filename);
4560 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4565 gtk_tree_path_free (path);
4570 /* Creates the widgets for the file list */
4572 create_file_list (GtkFileChooserDefault *impl)
4575 GtkTreeSelection *selection;
4576 GtkTreeViewColumn *column;
4577 GtkCellRenderer *renderer;
4579 /* Scrolled window */
4581 swin = gtk_scrolled_window_new (NULL, NULL);
4582 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4583 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4584 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4587 /* Tree/list view */
4589 impl->browse_files_tree_view = gtk_tree_view_new ();
4590 #ifdef PROFILE_FILE_CHOOSER
4591 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4593 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4594 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4596 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4597 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4599 gtk_drag_dest_set (impl->browse_files_tree_view,
4600 GTK_DEST_DEFAULT_ALL,
4601 file_list_dest_targets,
4602 num_file_list_dest_targets,
4603 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4605 g_signal_connect (impl->browse_files_tree_view, "row_activated",
4606 G_CALLBACK (list_row_activated), impl);
4607 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4608 G_CALLBACK (trap_activate_cb), impl);
4609 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4610 G_CALLBACK (list_popup_menu_cb), impl);
4611 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4612 G_CALLBACK (list_button_press_event_cb), impl);
4614 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4615 G_CALLBACK (file_list_drag_data_received_cb), impl);
4616 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4617 G_CALLBACK (file_list_drag_drop_cb), impl);
4618 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4619 G_CALLBACK (file_list_drag_motion_cb), impl);
4621 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4622 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4623 G_CALLBACK (file_list_query_tooltip_cb), impl);
4625 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4626 gtk_tree_selection_set_select_function (selection,
4629 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4631 file_list_source_targets,
4632 num_file_list_source_targets,
4635 g_signal_connect (selection, "changed",
4636 G_CALLBACK (list_selection_changed), impl);
4638 /* Filename column */
4640 impl->list_name_column = gtk_tree_view_column_new ();
4641 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4642 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4643 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4644 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4646 renderer = gtk_cell_renderer_pixbuf_new ();
4647 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4648 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4649 list_icon_data_func, impl, NULL);
4651 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4652 g_object_set (impl->list_name_renderer,
4653 "ellipsize", PANGO_ELLIPSIZE_END,
4655 g_signal_connect (impl->list_name_renderer, "edited",
4656 G_CALLBACK (renderer_edited_cb), impl);
4657 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4658 G_CALLBACK (renderer_editing_canceled_cb), impl);
4659 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4660 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4661 list_name_data_func, impl, NULL);
4663 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4667 column = gtk_tree_view_column_new ();
4668 gtk_tree_view_column_set_title (column, _("Size"));
4670 renderer = gtk_cell_renderer_text_new ();
4671 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4672 gtk_tree_view_column_set_cell_data_func (column, renderer,
4673 list_size_data_func, impl, NULL);
4674 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4675 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4678 /* Modification time column */
4680 column = gtk_tree_view_column_new ();
4681 gtk_tree_view_column_set_resizable (column, TRUE);
4682 gtk_tree_view_column_set_title (column, _("Modified"));
4684 renderer = gtk_cell_renderer_text_new ();
4685 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4686 gtk_tree_view_column_set_cell_data_func (column, renderer,
4687 list_mtime_data_func, impl, NULL);
4688 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4689 impl->list_mtime_column = column;
4691 file_list_set_sort_column_ids (impl);
4693 gtk_widget_show_all (swin);
4699 create_path_bar (GtkFileChooserDefault *impl)
4701 GtkWidget *path_bar;
4703 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4704 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4709 /* Creates the widgets for the files/folders pane */
4711 file_pane_create (GtkFileChooserDefault *impl,
4712 GtkSizeGroup *size_group)
4718 vbox = gtk_vbox_new (FALSE, 6);
4719 gtk_widget_show (vbox);
4721 /* Box for lists and preview */
4723 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4724 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4725 gtk_widget_show (hbox);
4729 widget = create_file_list (impl);
4730 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4734 impl->preview_box = gtk_vbox_new (FALSE, 12);
4735 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4736 /* Don't show preview box initially */
4740 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4742 widget = filter_create (impl);
4744 gtk_widget_show (widget);
4745 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4747 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4748 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4753 /* Callback used when the "Browse for more folders" expander is toggled */
4755 expander_changed_cb (GtkExpander *expander,
4757 GtkFileChooserDefault *impl)
4759 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4760 update_appearance (impl);
4763 /* Callback used when the selection changes in the save folder combo box */
4765 save_folder_combo_changed_cb (GtkComboBox *combo,
4766 GtkFileChooserDefault *impl)
4770 if (impl->changing_folder)
4773 if (gtk_combo_box_get_active_iter (combo, &iter))
4775 GtkTreeIter child_iter;
4777 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4780 shortcuts_activate_iter (impl, &child_iter);
4784 /* Filter function used to filter out the Search item and its separator.
4785 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4788 shortcuts_combo_filter_func (GtkTreeModel *model,
4792 GtkFileChooserDefault *impl;
4793 GtkTreePath *tree_path;
4798 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4800 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4802 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4803 g_assert (tree_path != NULL);
4805 indices = gtk_tree_path_get_indices (tree_path);
4809 if (impl->has_search)
4811 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4812 if (idx == indices[0])
4816 if (impl->has_recent)
4818 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4819 if (idx == indices[0])
4823 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4824 if (idx == indices[0])
4829 gtk_tree_path_free (tree_path);
4834 /* Creates the combo box with the save folders */
4836 save_folder_combo_create (GtkFileChooserDefault *impl)
4839 GtkCellRenderer *cell;
4841 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4842 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4843 shortcuts_combo_filter_func,
4847 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4848 "model", impl->shortcuts_combo_filter_model,
4849 "focus-on-click", FALSE,
4851 gtk_widget_show (combo);
4853 cell = gtk_cell_renderer_pixbuf_new ();
4854 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4855 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4856 "pixbuf", SHORTCUTS_COL_PIXBUF,
4857 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4858 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4861 cell = gtk_cell_renderer_text_new ();
4862 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4863 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4864 "text", SHORTCUTS_COL_NAME,
4865 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4868 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4869 shortcuts_row_separator_func,
4872 g_signal_connect (combo, "changed",
4873 G_CALLBACK (save_folder_combo_changed_cb), impl);
4878 /* Creates the widgets specific to Save mode */
4880 save_widgets_create (GtkFileChooserDefault *impl)
4885 GtkWidget *alignment;
4887 if (impl->save_widgets != NULL)
4890 location_switch_to_path_bar (impl);
4892 vbox = gtk_vbox_new (FALSE, 12);
4894 table = gtk_table_new (2, 2, FALSE);
4895 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4896 gtk_widget_show (table);
4897 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4898 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4902 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4903 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4904 gtk_table_attach (GTK_TABLE (table), widget,
4908 gtk_widget_show (widget);
4910 /* Location entry */
4912 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4913 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4915 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4916 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4917 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4919 GTK_EXPAND | GTK_FILL, 0,
4921 gtk_widget_show (impl->location_entry);
4922 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4925 impl->save_folder_label = gtk_label_new (NULL);
4926 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4927 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4931 gtk_widget_show (impl->save_folder_label);
4933 impl->save_folder_combo = save_folder_combo_create (impl);
4934 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4936 GTK_EXPAND | GTK_FILL, GTK_FILL,
4938 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4941 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4942 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4944 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4945 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4946 g_signal_connect (impl->save_expander, "notify::expanded",
4947 G_CALLBACK (expander_changed_cb),
4949 gtk_widget_show_all (alignment);
4951 impl->save_widgets = vbox;
4952 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4953 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4954 gtk_widget_show (impl->save_widgets);
4957 /* Destroys the widgets specific to Save mode */
4959 save_widgets_destroy (GtkFileChooserDefault *impl)
4961 if (impl->save_widgets == NULL)
4964 gtk_widget_destroy (impl->save_widgets);
4965 impl->save_widgets = NULL;
4966 impl->location_entry = NULL;
4967 impl->save_folder_label = NULL;
4968 impl->save_folder_combo = NULL;
4969 impl->save_expander = NULL;
4972 /* Turns on the path bar widget. Can be called even if we are already in that
4976 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4978 if (impl->location_entry)
4980 gtk_widget_destroy (impl->location_entry);
4981 impl->location_entry = NULL;
4984 gtk_widget_hide (impl->location_entry_box);
4987 /* Sets the full path of the current folder as the text in the location entry. */
4989 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4993 if (!impl->current_folder)
4996 if (gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
5000 filename = gtk_file_system_path_to_filename (impl->file_system, impl->current_folder);
5003 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
5010 text = gtk_file_system_path_to_uri (impl->file_system, impl->current_folder);
5014 gboolean need_slash;
5017 len = strlen (text);
5018 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
5024 slash_text = g_new (char, len + 2);
5025 strcpy (slash_text, text);
5026 slash_text[len] = G_DIR_SEPARATOR;
5027 slash_text[len + 1] = 0;
5033 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
5038 /* Turns on the location entry. Can be called even if we are already in that
5042 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
5044 /* when in search or recent files mode, we are not showing the
5045 * location_entry_box container, so there's no point in switching
5048 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5049 impl->operation_mode == OPERATION_MODE_RECENT)
5052 if (impl->location_entry)
5053 gtk_widget_destroy (impl->location_entry);
5057 gtk_widget_show (impl->location_entry_box);
5061 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
5062 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
5064 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5065 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5067 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
5068 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5070 /* Configure the entry */
5072 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5076 gtk_widget_show (impl->location_entry);
5077 gtk_widget_grab_focus (impl->location_entry);
5080 /* Sets a new location mode. set_buttons determines whether the toggle button
5081 * for the mode will also be changed.
5084 location_mode_set (GtkFileChooserDefault *impl,
5085 LocationMode new_mode,
5086 gboolean set_button)
5088 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5089 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5091 GtkWindow *toplevel;
5092 GtkWidget *current_focus;
5093 gboolean button_active;
5094 gboolean switch_to_file_list;
5098 case LOCATION_MODE_PATH_BAR:
5099 button_active = FALSE;
5101 /* The location_entry will disappear when we switch to path bar mode. So,
5102 * we'll focus the file list in that case, to avoid having a window with
5103 * no focused widget.
5105 toplevel = get_toplevel (GTK_WIDGET (impl));
5106 switch_to_file_list = FALSE;
5109 current_focus = gtk_window_get_focus (toplevel);
5110 if (!current_focus || current_focus == impl->location_entry)
5111 switch_to_file_list = TRUE;
5114 location_switch_to_path_bar (impl);
5116 if (switch_to_file_list)
5117 gtk_widget_grab_focus (impl->browse_files_tree_view);
5121 case LOCATION_MODE_FILENAME_ENTRY:
5122 button_active = TRUE;
5123 location_switch_to_filename_entry (impl);
5127 g_assert_not_reached ();
5133 g_signal_handlers_block_by_func (impl->location_button,
5134 G_CALLBACK (location_button_toggled_cb), impl);
5136 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5138 g_signal_handlers_unblock_by_func (impl->location_button,
5139 G_CALLBACK (location_button_toggled_cb), impl);
5143 impl->location_mode = new_mode;
5147 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5149 /* If the file entry is not visible, show it.
5150 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5152 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5154 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5156 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5158 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5160 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5164 gtk_widget_grab_focus (impl->location_entry);
5169 /* Callback used when one of the location mode buttons is toggled */
5171 location_button_toggled_cb (GtkToggleButton *toggle,
5172 GtkFileChooserDefault *impl)
5175 LocationMode new_mode;
5177 is_active = gtk_toggle_button_get_active (toggle);
5181 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5182 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5186 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5187 new_mode = LOCATION_MODE_PATH_BAR;
5190 location_mode_set (impl, new_mode, FALSE);
5193 /* Creates a toggle button for the location entry. */
5195 location_button_create (GtkFileChooserDefault *impl)
5200 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5201 gtk_widget_show (image);
5203 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5207 g_signal_connect (impl->location_button, "toggled",
5208 G_CALLBACK (location_button_toggled_cb), impl);
5210 str = _("Type a file name");
5212 gtk_widget_set_tooltip_text (impl->location_button, str);
5213 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5216 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5218 browse_widgets_create (GtkFileChooserDefault *impl)
5224 GtkSizeGroup *size_group;
5226 /* size group is used by the [+][-] buttons and the filter combo */
5227 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5228 vbox = gtk_vbox_new (FALSE, 12);
5230 /* Location widgets */
5231 hbox = gtk_hbox_new (FALSE, 12);
5232 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5233 gtk_widget_show (hbox);
5234 impl->browse_path_bar_hbox = hbox;
5236 location_button_create (impl);
5237 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5241 impl->browse_path_bar = create_path_bar (impl);
5242 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5243 gtk_widget_show_all (impl->browse_path_bar);
5244 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5247 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5248 g_signal_connect (impl->browse_new_folder_button, "clicked",
5249 G_CALLBACK (new_folder_button_clicked), impl);
5250 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5252 /* Box for the location label and entry */
5254 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5255 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5257 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5258 gtk_widget_show (impl->location_label);
5259 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5262 hpaned = gtk_hpaned_new ();
5263 gtk_widget_show (hpaned);
5264 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5266 widget = shortcuts_pane_create (impl, size_group);
5267 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5268 widget = file_pane_create (impl, size_group);
5269 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5271 g_object_unref (size_group);
5277 gtk_file_chooser_default_constructor (GType type,
5278 guint n_construct_properties,
5279 GObjectConstructParam *construct_params)
5281 GtkFileChooserDefault *impl;
5284 profile_start ("start", NULL);
5286 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5287 n_construct_properties,
5289 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5291 g_assert (impl->file_system);
5293 gtk_widget_push_composite_child ();
5295 /* Recent files manager */
5296 recent_manager_update (impl);
5298 /* Shortcuts model */
5299 shortcuts_model_create (impl);
5301 /* The browse widgets */
5302 impl->browse_widgets = browse_widgets_create (impl);
5303 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5305 /* Alignment to hold extra widget */
5306 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5307 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5309 gtk_widget_pop_composite_child ();
5310 update_appearance (impl);
5312 profile_end ("end", NULL);
5317 /* Sets the extra_widget by packing it in the appropriate place */
5319 set_extra_widget (GtkFileChooserDefault *impl,
5320 GtkWidget *extra_widget)
5324 g_object_ref (extra_widget);
5325 /* FIXME: is this right ? */
5326 gtk_widget_show (extra_widget);
5329 if (impl->extra_widget)
5331 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5332 g_object_unref (impl->extra_widget);
5335 impl->extra_widget = extra_widget;
5336 if (impl->extra_widget)
5338 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5339 gtk_widget_show (impl->extra_align);
5342 gtk_widget_hide (impl->extra_align);
5346 set_local_only (GtkFileChooserDefault *impl,
5347 gboolean local_only)
5349 if (local_only != impl->local_only)
5351 impl->local_only = local_only;
5353 if (impl->shortcuts_model && impl->file_system)
5355 shortcuts_add_volumes (impl);
5356 shortcuts_add_bookmarks (impl);
5360 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
5362 /* If we are pointing to a non-local folder, make an effort to change
5363 * back to a local folder, but it's really up to the app to not cause
5364 * such a situation, so we ignore errors.
5366 const gchar *home = g_get_home_dir ();
5367 GtkFilePath *home_path;
5372 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
5374 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
5376 gtk_file_path_free (home_path);
5382 volumes_changed_cb (GtkFileSystem *file_system,
5383 GtkFileChooserDefault *impl)
5385 shortcuts_add_volumes (impl);
5388 /* Callback used when the set of bookmarks changes in the file system */
5390 bookmarks_changed_cb (GtkFileSystem *file_system,
5391 GtkFileChooserDefault *impl)
5393 shortcuts_add_bookmarks (impl);
5395 bookmarks_check_add_sensitivity (impl);
5396 bookmarks_check_remove_sensitivity (impl);
5397 shortcuts_check_popup_sensitivity (impl);
5400 /* Sets the file chooser to multiple selection mode */
5402 set_select_multiple (GtkFileChooserDefault *impl,
5403 gboolean select_multiple,
5404 gboolean property_notify)
5406 GtkTreeSelection *selection;
5407 GtkSelectionMode mode;
5409 if (select_multiple == impl->select_multiple)
5412 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5414 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5415 gtk_tree_selection_set_mode (selection, mode);
5417 impl->select_multiple = select_multiple;
5418 g_object_notify (G_OBJECT (impl), "select-multiple");
5420 check_preview_change (impl);
5424 set_file_system_backend (GtkFileChooserDefault *impl,
5425 const char *backend)
5427 profile_start ("start for backend", backend ? backend : "default");
5429 if (impl->file_system)
5431 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
5432 impl->volumes_changed_id = 0;
5433 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
5434 impl->bookmarks_changed_id = 0;
5435 g_object_unref (impl->file_system);
5438 impl->file_system = NULL;
5440 impl->file_system = gtk_file_system_create (backend);
5443 GtkSettings *settings = gtk_settings_get_default ();
5444 gchar *default_backend = NULL;
5446 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
5447 if (default_backend)
5449 impl->file_system = gtk_file_system_create (default_backend);
5450 g_free (default_backend);
5454 if (!impl->file_system)
5456 #if defined (G_OS_UNIX)
5457 impl->file_system = gtk_file_system_unix_new ();
5458 #elif defined (G_OS_WIN32)
5459 impl->file_system = gtk_file_system_win32_new ();
5461 #error "No default filesystem implementation on the platform"
5465 if (impl->file_system)
5467 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
5468 G_CALLBACK (volumes_changed_cb),
5470 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
5471 G_CALLBACK (bookmarks_changed_cb),
5475 profile_end ("end", NULL);
5478 /* This function is basically a do_all function.
5480 * It sets the visibility on all the widgets based on the current state, and
5481 * moves the custom_widget if needed.
5484 update_appearance (GtkFileChooserDefault *impl)
5486 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5487 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5491 gtk_widget_hide (impl->location_button);
5492 save_widgets_create (impl);
5494 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5495 text = _("Save in _folder:");
5497 text = _("Create in _folder:");
5499 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5501 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5503 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5504 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5505 gtk_widget_show (impl->browse_widgets);
5509 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5510 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5511 gtk_widget_hide (impl->browse_widgets);
5514 gtk_widget_show (impl->browse_new_folder_button);
5516 if (impl->select_multiple)
5518 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5519 "Re-setting to single selection mode.");
5520 set_select_multiple (impl, FALSE, TRUE);
5523 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5524 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5526 gtk_widget_show (impl->location_button);
5527 save_widgets_destroy (impl);
5528 gtk_widget_show (impl->browse_widgets);
5529 location_mode_set (impl, impl->location_mode, TRUE);
5532 if (impl->location_entry)
5533 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5535 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5536 gtk_widget_hide (impl->browse_new_folder_button);
5538 gtk_widget_show (impl->browse_new_folder_button);
5540 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5541 * of files may change depending whether we are in a file or folder-only mode.
5543 gtk_widget_queue_draw (impl->browse_files_tree_view);
5545 g_signal_emit_by_name (impl, "default-size-changed");
5549 gtk_file_chooser_default_set_property (GObject *object,
5551 const GValue *value,
5555 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5559 case GTK_FILE_CHOOSER_PROP_ACTION:
5561 GtkFileChooserAction action = g_value_get_enum (value);
5563 if (action != impl->action)
5565 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5567 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5568 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5569 && impl->select_multiple)
5571 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5572 "this is not allowed in multiple selection mode. Resetting the file chooser "
5573 "to single selection mode.");
5574 set_select_multiple (impl, FALSE, TRUE);
5576 impl->action = action;
5577 update_appearance (impl);
5578 settings_load (impl);
5583 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5584 set_file_system_backend (impl, g_value_get_string (value));
5587 case GTK_FILE_CHOOSER_PROP_FILTER:
5588 set_current_filter (impl, g_value_get_object (value));
5591 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5592 set_local_only (impl, g_value_get_boolean (value));
5595 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5596 set_preview_widget (impl, g_value_get_object (value));
5599 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5600 impl->preview_widget_active = g_value_get_boolean (value);
5601 update_preview_widget_visibility (impl);
5604 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5605 impl->use_preview_label = g_value_get_boolean (value);
5606 update_preview_widget_visibility (impl);
5609 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5610 set_extra_widget (impl, g_value_get_object (value));
5613 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5615 gboolean select_multiple = g_value_get_boolean (value);
5616 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5617 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5620 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5621 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5622 "leaving the file chooser in single selection mode.");
5626 set_select_multiple (impl, select_multiple, FALSE);
5630 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5632 gboolean show_hidden = g_value_get_boolean (value);
5633 if (show_hidden != impl->show_hidden)
5635 impl->show_hidden = show_hidden;
5637 if (impl->browse_files_model)
5638 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5643 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5645 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5646 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5651 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5657 gtk_file_chooser_default_get_property (GObject *object,
5662 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5666 case GTK_FILE_CHOOSER_PROP_ACTION:
5667 g_value_set_enum (value, impl->action);
5670 case GTK_FILE_CHOOSER_PROP_FILTER:
5671 g_value_set_object (value, impl->current_filter);
5674 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5675 g_value_set_boolean (value, impl->local_only);
5678 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5679 g_value_set_object (value, impl->preview_widget);
5682 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5683 g_value_set_boolean (value, impl->preview_widget_active);
5686 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5687 g_value_set_boolean (value, impl->use_preview_label);
5690 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5691 g_value_set_object (value, impl->extra_widget);
5694 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5695 g_value_set_boolean (value, impl->select_multiple);
5698 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5699 g_value_set_boolean (value, impl->show_hidden);
5702 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5703 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5707 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5712 /* Removes the settings signal handler. It's safe to call multiple times */
5714 remove_settings_signal (GtkFileChooserDefault *impl,
5717 if (impl->settings_signal_id)
5719 GtkSettings *settings;
5721 settings = gtk_settings_get_for_screen (screen);
5722 g_signal_handler_disconnect (settings,
5723 impl->settings_signal_id);
5724 impl->settings_signal_id = 0;
5729 gtk_file_chooser_default_dispose (GObject *object)
5732 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5734 if (impl->extra_widget)
5736 g_object_unref (impl->extra_widget);
5737 impl->extra_widget = NULL;
5740 if (impl->volumes_changed_id > 0)
5742 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
5743 impl->volumes_changed_id = 0;
5746 if (impl->bookmarks_changed_id > 0)
5748 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
5749 impl->bookmarks_changed_id = 0;
5752 pending_select_paths_free (impl);
5754 /* cancel all pending operations */
5755 if (impl->pending_handles)
5757 for (l = impl->pending_handles; l; l = l->next)
5759 GtkFileSystemHandle *handle =l->data;
5760 gtk_file_system_cancel_operation (handle);
5762 g_slist_free (impl->pending_handles);
5763 impl->pending_handles = NULL;
5766 if (impl->reload_icon_handles)
5768 for (l = impl->reload_icon_handles; l; l = l->next)
5770 GtkFileSystemHandle *handle =l->data;
5771 gtk_file_system_cancel_operation (handle);
5773 g_slist_free (impl->reload_icon_handles);
5774 impl->reload_icon_handles = NULL;
5777 if (impl->loading_shortcuts)
5779 for (l = impl->loading_shortcuts; l; l = l->next)
5781 GtkFileSystemHandle *handle =l->data;
5782 gtk_file_system_cancel_operation (handle);
5784 g_slist_free (impl->loading_shortcuts);
5785 impl->loading_shortcuts = NULL;
5788 if (impl->file_list_drag_data_received_handle)
5790 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
5791 impl->file_list_drag_data_received_handle = NULL;
5794 if (impl->update_current_folder_handle)
5796 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
5797 impl->update_current_folder_handle = NULL;
5800 if (impl->show_and_select_paths_handle)
5802 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5803 impl->show_and_select_paths_handle = NULL;
5806 if (impl->should_respond_get_info_handle)
5808 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
5809 impl->should_respond_get_info_handle = NULL;
5812 if (impl->update_from_entry_handle)
5814 gtk_file_system_cancel_operation (impl->update_from_entry_handle);
5815 impl->update_from_entry_handle = NULL;
5818 if (impl->shortcuts_activate_iter_handle)
5820 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
5821 impl->shortcuts_activate_iter_handle = NULL;
5824 search_stop_searching (impl, TRUE);
5825 recent_stop_loading (impl);
5827 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5829 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5832 /* We override show-all since we have internal widgets that
5833 * shouldn't be shown when you call show_all(), like the filter
5837 gtk_file_chooser_default_show_all (GtkWidget *widget)
5839 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5841 gtk_widget_show (widget);
5843 if (impl->extra_widget)
5844 gtk_widget_show_all (impl->extra_widget);
5847 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5848 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5851 toplevel_set_focus_cb (GtkWindow *window,
5853 GtkFileChooserDefault *impl)
5855 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5858 /* We monitor the focus widget on our toplevel to be able to know which widget
5859 * was last focused at the time our "should_respond" method gets called.
5862 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5863 GtkWidget *previous_toplevel)
5865 GtkFileChooserDefault *impl;
5866 GtkWidget *toplevel;
5868 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5870 if (previous_toplevel)
5872 g_assert (impl->toplevel_set_focus_id != 0);
5873 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5874 impl->toplevel_set_focus_id = 0;
5875 impl->toplevel_last_focus_widget = NULL;
5878 g_assert (impl->toplevel_set_focus_id == 0);
5880 toplevel = gtk_widget_get_toplevel (widget);
5881 if (GTK_IS_WINDOW (toplevel))
5883 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5884 G_CALLBACK (toplevel_set_focus_cb), impl);
5885 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5889 /* Changes the icons wherever it is needed */
5891 change_icon_theme (GtkFileChooserDefault *impl)
5893 GtkSettings *settings;
5896 profile_start ("start", NULL);
5898 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5900 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5901 impl->icon_size = MAX (width, height);
5903 impl->icon_size = FALLBACK_ICON_SIZE;
5905 shortcuts_reload_icons (impl);
5906 gtk_widget_queue_resize (impl->browse_files_tree_view);
5908 profile_end ("end", NULL);
5911 /* Callback used when a GtkSettings value changes */
5913 settings_notify_cb (GObject *object,
5915 GtkFileChooserDefault *impl)
5919 profile_start ("start", NULL);
5921 name = g_param_spec_get_name (pspec);
5923 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5924 strcmp (name, "gtk-icon-sizes") == 0)
5925 change_icon_theme (impl);
5927 profile_end ("end", NULL);
5930 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5934 check_icon_theme (GtkFileChooserDefault *impl)
5936 GtkSettings *settings;
5938 profile_start ("start", NULL);
5940 if (impl->settings_signal_id)
5942 profile_end ("end", NULL);
5946 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5948 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5949 impl->settings_signal_id = g_signal_connect (settings, "notify",
5950 G_CALLBACK (settings_notify_cb), impl);
5952 change_icon_theme (impl);
5955 profile_end ("end", NULL);
5959 recent_manager_update (GtkFileChooserDefault *impl)
5961 GtkRecentManager *manager;
5963 profile_start ("start", NULL);
5965 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5966 manager = gtk_recent_manager_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5968 manager = gtk_recent_manager_get_default ();
5970 if (impl->recent_manager != manager)
5971 impl->recent_manager = manager;
5973 profile_end ("end", NULL);
5977 gtk_file_chooser_default_style_set (GtkWidget *widget,
5978 GtkStyle *previous_style)
5980 GtkFileChooserDefault *impl;
5982 profile_start ("start", NULL);
5984 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5986 profile_msg (" parent class style_set start", NULL);
5987 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5988 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5989 profile_msg (" parent class style_set end", NULL);
5991 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5992 change_icon_theme (impl);
5994 profile_msg (" emit default-size-changed start", NULL);
5995 g_signal_emit_by_name (widget, "default-size-changed");
5996 profile_msg (" emit default-size-changed end", NULL);
5998 profile_end ("end", NULL);
6002 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
6003 GdkScreen *previous_screen)
6005 GtkFileChooserDefault *impl;
6007 profile_start ("start", NULL);
6009 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6011 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
6012 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
6014 remove_settings_signal (impl, previous_screen);
6015 check_icon_theme (impl);
6016 recent_manager_update (impl);
6018 g_signal_emit_by_name (widget, "default-size-changed");
6020 profile_end ("end", NULL);
6024 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
6025 GtkAllocation *allocation)
6027 GtkFileChooserDefault *impl;
6029 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6031 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
6033 impl->default_width = allocation->width;
6034 impl->default_height = allocation->height;
6036 if (impl->preview_widget_active &&
6037 impl->preview_widget &&
6038 GTK_WIDGET_DRAWABLE (impl->preview_widget))
6039 impl->default_width -= impl->preview_widget->allocation.width + PREVIEW_HBOX_SPACING;
6041 if (impl->extra_widget &&
6042 GTK_WIDGET_DRAWABLE (impl->extra_widget))
6043 impl->default_height -= GTK_BOX (widget)->spacing + impl->extra_widget->allocation.height;
6047 get_is_file_filtered (GtkFileChooserDefault *impl,
6048 const GtkFilePath *path,
6049 GtkFileInfo *file_info)
6051 GtkFileFilterInfo filter_info;
6052 GtkFileFilterFlags needed;
6055 if (!impl->current_filter)
6058 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
6060 needed = gtk_file_filter_get_needed (impl->current_filter);
6062 filter_info.display_name = gtk_file_info_get_display_name (file_info);
6063 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
6065 if (needed & GTK_FILE_FILTER_FILENAME)
6067 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
6068 if (filter_info.filename)
6069 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
6072 filter_info.filename = NULL;
6074 if (needed & GTK_FILE_FILTER_URI)
6076 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
6077 if (filter_info.uri)
6078 filter_info.contains |= GTK_FILE_FILTER_URI;
6081 filter_info.uri = NULL;
6083 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
6085 if (filter_info.filename)
6086 g_free ((gchar *)filter_info.filename);
6087 if (filter_info.uri)
6088 g_free ((gchar *)filter_info.uri);
6094 settings_load (GtkFileChooserDefault *impl)
6096 GtkFileChooserSettings *settings;
6097 LocationMode location_mode;
6098 gboolean show_hidden;
6099 gboolean expand_folders;
6101 settings = _gtk_file_chooser_settings_new ();
6103 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
6104 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
6105 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
6107 g_object_unref (settings);
6109 location_mode_set (impl, location_mode, TRUE);
6110 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6111 impl->expand_folders = expand_folders;
6112 if (impl->save_expander)
6113 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
6117 settings_save (GtkFileChooserDefault *impl)
6119 GtkFileChooserSettings *settings;
6121 settings = _gtk_file_chooser_settings_new ();
6123 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6124 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6125 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6128 _gtk_file_chooser_settings_save (settings, NULL);
6130 g_object_unref (settings);
6133 /* GtkWidget::map method */
6135 gtk_file_chooser_default_map (GtkWidget *widget)
6137 GtkFileChooserDefault *impl;
6138 char *current_working_dir;
6140 profile_start ("start", NULL);
6142 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6144 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6146 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6148 switch (impl->reload_state)
6151 /* The user didn't explicitly give us a folder to
6152 * display, so we'll use the cwd
6154 current_working_dir = g_get_current_dir ();
6155 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6156 current_working_dir);
6157 g_free (current_working_dir);
6160 case RELOAD_HAS_FOLDER:
6161 /* Nothing; we are already loading or loaded, so we
6162 * don't need to reload
6166 case RELOAD_WAS_UNMAPPED:
6167 /* Just reload the current folder; else continue
6170 if (impl->current_folder)
6172 pending_select_paths_store_selection (impl);
6173 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6178 g_assert_not_reached ();
6182 bookmarks_changed_cb (impl->file_system, impl);
6184 settings_load (impl);
6186 profile_end ("end", NULL);
6189 /* GtkWidget::unmap method */
6191 gtk_file_chooser_default_unmap (GtkWidget *widget)
6193 GtkFileChooserDefault *impl;
6195 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6197 settings_save (impl);
6199 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6201 impl->reload_state = RELOAD_WAS_UNMAPPED;
6205 list_model_filter_func (GtkFileSystemModel *model,
6207 const GtkFileInfo *file_info,
6210 GtkFileChooserDefault *impl = user_data;
6212 if (!impl->current_filter)
6215 if (gtk_file_info_get_is_folder (file_info))
6218 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
6222 install_list_model_filter (GtkFileChooserDefault *impl)
6224 GtkFileSystemModelFilter filter;
6227 g_assert (impl->browse_files_model != NULL);
6229 if (impl->current_filter)
6231 filter = list_model_filter_func;
6240 _gtk_file_system_model_set_filter (impl->browse_files_model,
6245 #define COMPARE_DIRECTORIES \
6246 GtkFileChooserDefault *impl = user_data; \
6247 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6248 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6249 gboolean dir_a, dir_b; \
6252 dir_a = gtk_file_info_get_is_folder (info_a); \
6254 return impl->list_sort_ascending ? -1 : 1; \
6257 dir_b = gtk_file_info_get_is_folder (info_b); \
6259 return impl->list_sort_ascending ? 1 : -1; \
6261 if (dir_a != dir_b) \
6262 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6264 /* Sort callback for the filename column */
6266 name_sort_func (GtkTreeModel *model,
6271 COMPARE_DIRECTORIES;
6273 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
6276 /* Sort callback for the size column */
6278 size_sort_func (GtkTreeModel *model,
6283 COMPARE_DIRECTORIES;
6286 gint64 size_a = gtk_file_info_get_size (info_a);
6287 gint64 size_b = gtk_file_info_get_size (info_b);
6289 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6293 /* Sort callback for the mtime column */
6295 mtime_sort_func (GtkTreeModel *model,
6300 COMPARE_DIRECTORIES;
6303 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
6304 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
6306 return ta > tb ? -1 : (ta == tb ? 0 : 1);
6310 /* Callback used when the sort column changes. We cache the sort order for use
6311 * in name_sort_func().
6314 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6315 GtkFileChooserDefault *impl)
6317 GtkSortType sort_type;
6319 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6320 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6324 set_busy_cursor (GtkFileChooserDefault *impl,
6327 GtkWindow *toplevel;
6328 GdkDisplay *display;
6331 toplevel = get_toplevel (GTK_WIDGET (impl));
6332 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6335 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6338 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6342 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6343 gdk_display_flush (display);
6346 gdk_cursor_unref (cursor);
6349 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6351 load_set_model (GtkFileChooserDefault *impl)
6353 profile_start ("start", NULL);
6355 g_assert (impl->browse_files_model != NULL);
6356 g_assert (impl->sort_model == NULL);
6358 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6359 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6360 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6361 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6362 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6363 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6364 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6365 impl->list_sort_ascending = TRUE;
6366 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6368 g_signal_connect (impl->sort_model, "sort_column_changed",
6369 G_CALLBACK (list_sort_column_changed_cb), impl);
6371 profile_msg (" gtk_tree_view_set_model start", NULL);
6372 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6373 GTK_TREE_MODEL (impl->sort_model));
6374 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6375 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6376 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6377 profile_msg (" gtk_tree_view_set_model end", NULL);
6379 profile_end ("end", NULL);
6382 /* Timeout callback used when the loading timer expires */
6384 load_timeout_cb (gpointer data)
6386 GtkFileChooserDefault *impl;
6388 profile_start ("start", NULL);
6390 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6391 g_assert (impl->load_state == LOAD_PRELOAD);
6392 g_assert (impl->load_timeout_id != 0);
6393 g_assert (impl->browse_files_model != NULL);
6395 impl->load_timeout_id = 0;
6396 impl->load_state = LOAD_LOADING;
6398 load_set_model (impl);
6400 profile_end ("end", NULL);
6405 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6407 load_setup_timer (GtkFileChooserDefault *impl)
6409 g_assert (impl->load_timeout_id == 0);
6410 g_assert (impl->load_state != LOAD_PRELOAD);
6412 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6413 impl->load_state = LOAD_PRELOAD;
6416 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6418 load_remove_timer (GtkFileChooserDefault *impl)
6420 if (impl->load_timeout_id != 0)
6422 g_assert (impl->load_state == LOAD_PRELOAD);
6424 g_source_remove (impl->load_timeout_id);
6425 impl->load_timeout_id = 0;
6426 impl->load_state = LOAD_EMPTY;
6429 g_assert (impl->load_state == LOAD_EMPTY ||
6430 impl->load_state == LOAD_LOADING ||
6431 impl->load_state == LOAD_FINISHED);
6434 /* Selects the first row in the file list */
6436 browse_files_select_first_row (GtkFileChooserDefault *impl)
6440 if (!impl->sort_model)
6443 path = gtk_tree_path_new_from_indices (0, -1);
6444 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6445 gtk_tree_path_free (path);
6448 struct center_selected_row_closure {
6449 GtkFileChooserDefault *impl;
6450 gboolean already_centered;
6453 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6454 * selected row in the tree view.
6457 center_selected_row_foreach_cb (GtkTreeModel *model,
6462 struct center_selected_row_closure *closure;
6465 if (closure->already_centered)
6468 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6469 closure->already_centered = TRUE;
6472 /* Centers the selected row in the tree view */
6474 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6476 struct center_selected_row_closure closure;
6477 GtkTreeSelection *selection;
6479 closure.impl = impl;
6480 closure.already_centered = FALSE;
6482 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6483 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6486 struct ShowAndSelectPathsData
6488 GtkFileChooserDefault *impl;
6493 show_and_select_paths_finished_loading (GtkFileFolder *folder,
6496 gboolean have_hidden;
6497 gboolean have_filtered;
6499 struct ShowAndSelectPathsData *data = user_data;
6501 have_hidden = FALSE;
6502 have_filtered = FALSE;
6504 for (l = data->paths; l; l = l->next)
6506 const GtkFilePath *path;
6512 info = gtk_file_folder_get_info (folder, path, NULL);
6516 have_hidden = gtk_file_info_get_is_hidden (info);
6519 have_filtered = !gtk_file_info_get_is_folder (info) && get_is_file_filtered (data->impl, path, info);
6521 gtk_file_info_free (info);
6523 if (have_hidden && have_filtered)
6524 break; /* we now have all the information we need */
6528 g_signal_handlers_disconnect_by_func (folder,
6529 show_and_select_paths_finished_loading,
6532 g_object_unref (folder);
6535 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6538 set_current_filter (data->impl, NULL);
6540 for (l = data->paths; l; l = l->next)
6542 const GtkFilePath *path;
6545 _gtk_file_system_model_path_do (data->impl->browse_files_model, path,
6546 select_func, data->impl);
6549 browse_files_center_selected_row (data->impl);
6551 g_object_unref (data->impl);
6552 gtk_file_paths_free (data->paths);
6557 show_and_select_paths_get_folder_cb (GtkFileSystemHandle *handle,
6558 GtkFileFolder *folder,
6559 const GError *error,
6562 gboolean cancelled = handle->cancelled;
6563 struct ShowAndSelectPathsData *data = user_data;
6565 if (data->impl->show_and_select_paths_handle != handle)
6568 data->impl->show_and_select_paths_handle = NULL;
6570 if (cancelled || error)
6573 g_object_unref (handle);
6575 if (gtk_file_folder_is_finished_loading (folder))
6576 show_and_select_paths_finished_loading (folder, user_data);
6578 g_signal_connect (folder, "finished-loading",
6579 G_CALLBACK (show_and_select_paths_finished_loading),
6585 g_object_unref (data->impl);
6586 gtk_file_paths_free (data->paths);
6589 g_object_unref (handle);
6593 show_and_select_paths (GtkFileChooserDefault *impl,
6594 const GtkFilePath *parent_path,
6598 struct ShowAndSelectPathsData *info;
6600 profile_start ("start", NULL);
6604 profile_end ("end", NULL);
6608 info = g_new (struct ShowAndSelectPathsData, 1);
6609 info->impl = g_object_ref (impl);
6610 info->paths = gtk_file_paths_copy (paths);
6612 if (impl->show_and_select_paths_handle)
6613 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
6615 impl->show_and_select_paths_handle =
6616 gtk_file_system_get_folder (impl->file_system, parent_path,
6617 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN,
6618 show_and_select_paths_get_folder_cb, info);
6620 profile_end ("end", NULL);
6624 /* Processes the pending operation when a folder is finished loading */
6626 pending_select_paths_process (GtkFileChooserDefault *impl)
6628 g_assert (impl->load_state == LOAD_FINISHED);
6629 g_assert (impl->browse_files_model != NULL);
6630 g_assert (impl->sort_model != NULL);
6632 if (impl->pending_select_paths)
6635 show_and_select_paths (impl, impl->current_folder, impl->pending_select_paths, NULL);
6636 pending_select_paths_free (impl);
6637 browse_files_center_selected_row (impl);
6641 /* We only select the first row if the chooser is actually mapped ---
6642 * selecting the first row is to help the user when he is interacting with
6643 * the chooser, but sometimes a chooser works not on behalf of the user,
6644 * but rather on behalf of something else like GtkFileChooserButton. In
6645 * that case, the chooser's selection should be what the caller expects,
6646 * as the user can't see that something else got selected. See bug #165264.
6648 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6649 browse_files_select_first_row (impl);
6652 g_assert (impl->pending_select_paths == NULL);
6655 /* Callback used when the file system model finishes loading */
6657 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6658 GtkFileChooserDefault *impl)
6660 profile_start ("start", NULL);
6662 if (impl->load_state == LOAD_PRELOAD)
6664 load_remove_timer (impl);
6665 load_set_model (impl);
6667 else if (impl->load_state == LOAD_LOADING)
6673 /* We can't g_assert_not_reached(), as something other than us may have
6674 * initiated a folder reload. See #165556.
6676 profile_end ("end", NULL);
6680 g_assert (impl->load_timeout_id == 0);
6682 impl->load_state = LOAD_FINISHED;
6684 pending_select_paths_process (impl);
6685 set_busy_cursor (impl, FALSE);
6686 #ifdef PROFILE_FILE_CHOOSER
6687 access ("MARK: *** FINISHED LOADING", F_OK);
6690 profile_end ("end", NULL);
6694 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6696 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6698 if (impl->browse_files_model)
6700 g_object_unref (impl->browse_files_model);
6701 impl->browse_files_model = NULL;
6704 if (impl->sort_model)
6706 g_object_unref (impl->sort_model);
6707 impl->sort_model = NULL;
6710 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6713 /* Gets rid of the old list model and creates a new one for the current folder */
6715 set_list_model (GtkFileChooserDefault *impl,
6718 g_assert (impl->current_folder != NULL);
6720 profile_start ("start", NULL);
6722 stop_loading_and_clear_list_model (impl);
6724 set_busy_cursor (impl, TRUE);
6725 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6727 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6728 impl->current_folder, 0,
6731 if (!impl->browse_files_model)
6733 set_busy_cursor (impl, FALSE);
6734 profile_end ("end", NULL);
6738 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6740 g_signal_connect (impl->browse_files_model, "finished-loading",
6741 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6743 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6745 install_list_model_filter (impl);
6747 profile_end ("end", NULL);
6752 struct update_chooser_entry_selected_foreach_closure {
6754 GtkTreeIter first_selected_iter;
6758 compare_utf8_filenames (const gchar *a,
6761 gchar *a_folded, *b_folded;
6764 a_folded = g_utf8_strdown (a, -1);
6765 b_folded = g_utf8_strdown (b, -1);
6767 retval = strcmp (a_folded, b_folded);
6776 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6781 struct update_chooser_entry_selected_foreach_closure *closure;
6784 closure->num_selected++;
6786 if (closure->num_selected == 1)
6787 closure->first_selected_iter = *iter;
6791 update_chooser_entry (GtkFileChooserDefault *impl)
6793 GtkTreeSelection *selection;
6794 struct update_chooser_entry_selected_foreach_closure closure;
6795 const char *file_part;
6797 /* no need to update the file chooser's entry if there's no entry */
6798 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6799 impl->operation_mode == OPERATION_MODE_RECENT ||
6800 !impl->location_entry)
6803 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6804 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6805 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6806 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6807 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6810 g_assert (impl->location_entry != NULL);
6812 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6813 closure.num_selected = 0;
6814 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6818 if (closure.num_selected == 0)
6820 goto maybe_clear_entry;
6822 else if (closure.num_selected == 1)
6824 GtkTreeIter child_iter;
6826 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6828 const GtkFileInfo *info;
6829 gboolean change_entry;
6831 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6833 &closure.first_selected_iter);
6835 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6837 /* If the cursor moved to the row of the newly created folder,
6838 * retrieving info will return NULL.
6843 g_free (impl->browse_files_last_selected_name);
6844 impl->browse_files_last_selected_name =
6845 g_strdup (gtk_file_info_get_display_name (info));
6847 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6848 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6849 change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
6851 change_entry = TRUE; /* ... unless we are in one of the folder modes */
6855 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6857 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6858 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6866 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6867 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6869 /* Multiple selection, so just clear the entry. */
6871 g_free (impl->browse_files_last_selected_name);
6872 impl->browse_files_last_selected_name = NULL;
6874 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6880 if (impl->browse_files_last_selected_name)
6882 const char *entry_text;
6884 gboolean clear_entry;
6886 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6887 len = strlen (entry_text);
6890 /* The file chooser entry may have appended a "/" to its text. So
6891 * take it out, and compare the result to the old selection.
6893 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6897 tmp = g_strndup (entry_text, len - 1);
6898 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6902 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6905 clear_entry = FALSE;
6908 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6913 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6914 const GtkFilePath *path,
6917 return gtk_file_chooser_default_update_current_folder (chooser, path, FALSE, FALSE, error);
6921 struct UpdateCurrentFolderData
6923 GtkFileChooserDefault *impl;
6925 gboolean keep_trail;
6926 gboolean clear_entry;
6927 GtkFilePath *original_path;
6928 GError *original_error;
6932 update_current_folder_get_info_cb (GtkFileSystemHandle *handle,
6933 const GtkFileInfo *info,
6934 const GError *error,
6937 gboolean cancelled = handle->cancelled;
6938 struct UpdateCurrentFolderData *data = user_data;
6939 GtkFileChooserDefault *impl = data->impl;
6941 if (handle != impl->update_current_folder_handle)
6944 impl->update_current_folder_handle = NULL;
6945 impl->reload_state = RELOAD_EMPTY;
6947 set_busy_cursor (impl, FALSE);
6954 GtkFilePath *parent_path;
6956 if (!data->original_path)
6958 data->original_path = gtk_file_path_copy (data->path);
6959 data->original_error = g_error_copy (error);
6962 /* get parent path and try to change the folder to that */
6963 if (gtk_file_system_get_parent (impl->file_system, data->path, &parent_path, NULL) &&
6964 parent_path != NULL)
6966 gtk_file_path_free (data->path);
6967 data->path = parent_path;
6969 g_object_unref (handle);
6971 /* restart the update current folder operation */
6972 impl->reload_state = RELOAD_HAS_FOLDER;
6974 impl->update_current_folder_handle =
6975 gtk_file_system_get_info (impl->file_system, data->path,
6976 GTK_FILE_INFO_IS_FOLDER,
6977 update_current_folder_get_info_cb,
6980 set_busy_cursor (impl, TRUE);
6986 /* error and bail out */
6987 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6989 gtk_file_path_free (data->original_path);
6995 if (data->original_path)
6997 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6999 gtk_file_path_free (data->original_path);
7002 if (!gtk_file_info_get_is_folder (info))
7005 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), data->path, data->keep_trail, NULL))
7008 if (impl->current_folder != data->path)
7010 if (impl->current_folder)
7011 gtk_file_path_free (impl->current_folder);
7013 impl->current_folder = gtk_file_path_copy (data->path);
7015 impl->reload_state = RELOAD_HAS_FOLDER;
7018 /* Update the widgets that may trigger a folder change themselves. */
7020 if (!impl->changing_folder)
7022 impl->changing_folder = TRUE;
7024 shortcuts_update_current_folder (impl);
7026 impl->changing_folder = FALSE;
7029 /* Set the folder on the save entry */
7031 if (impl->location_entry)
7033 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7034 impl->current_folder);
7036 if (data->clear_entry)
7037 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7040 /* Create a new list model. This is slightly evil; we store the result value
7041 * but perform more actions rather than returning immediately even if it
7042 * generates an error.
7044 set_list_model (impl, NULL);
7046 /* Refresh controls */
7048 shortcuts_find_current_folder (impl);
7050 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7052 check_preview_change (impl);
7053 bookmarks_check_add_sensitivity (impl);
7055 g_signal_emit_by_name (impl, "selection-changed", 0);
7058 gtk_file_path_free (data->path);
7061 g_object_unref (handle);
7065 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7066 const GtkFilePath *path,
7067 gboolean keep_trail,
7068 gboolean clear_entry,
7071 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7072 struct UpdateCurrentFolderData *data;
7074 profile_start ("start", (char *) path);
7076 switch (impl->operation_mode)
7078 case OPERATION_MODE_SEARCH:
7079 search_switch_to_browse_mode (impl);
7081 case OPERATION_MODE_RECENT:
7082 recent_switch_to_browse_mode (impl);
7084 case OPERATION_MODE_BROWSE:
7088 g_assert (path != NULL);
7090 if (impl->local_only &&
7091 !gtk_file_system_path_is_local (impl->file_system, path))
7094 GTK_FILE_CHOOSER_ERROR,
7095 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7096 _("Cannot change to folder because it is not local"));
7098 profile_end ("end - not local", (char *) path);
7102 if (impl->update_current_folder_handle)
7103 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
7105 /* Test validity of path here. */
7106 data = g_new0 (struct UpdateCurrentFolderData, 1);
7108 data->path = gtk_file_path_copy (path);
7109 data->keep_trail = keep_trail;
7110 data->clear_entry = clear_entry;
7112 impl->reload_state = RELOAD_HAS_FOLDER;
7114 impl->update_current_folder_handle =
7115 gtk_file_system_get_info (impl->file_system, path, GTK_FILE_INFO_IS_FOLDER,
7116 update_current_folder_get_info_cb,
7119 set_busy_cursor (impl, TRUE);
7121 profile_end ("end", NULL);
7125 static GtkFilePath *
7126 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7128 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7130 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7131 impl->operation_mode == OPERATION_MODE_RECENT)
7134 if (impl->reload_state == RELOAD_EMPTY)
7136 char *current_working_dir;
7139 /* We are unmapped, or we had an error while loading the last folder. We'll return
7140 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7141 * explicitly calls set_current_folder() on us.
7143 current_working_dir = g_get_current_dir ();
7144 path = gtk_file_system_filename_to_path (impl->file_system, current_working_dir);
7145 g_free (current_working_dir);
7149 return gtk_file_path_copy (impl->current_folder);
7153 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7156 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7158 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7159 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7161 pending_select_paths_free (impl);
7162 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7166 select_func (GtkFileSystemModel *model,
7171 GtkFileChooserDefault *impl = user_data;
7172 GtkTreeSelection *selection;
7173 GtkTreeIter sorted_iter;
7175 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7177 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7178 gtk_tree_selection_select_iter (selection, &sorted_iter);
7182 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
7183 const GtkFilePath *path,
7186 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7187 GtkFilePath *parent_path;
7190 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
7194 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
7196 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7197 impl->operation_mode == OPERATION_MODE_RECENT ||
7198 impl->load_state == LOAD_EMPTY)
7204 g_assert (impl->current_folder != NULL);
7206 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
7209 if (same_path && impl->load_state == LOAD_FINISHED)
7214 paths.data = (gpointer) path;
7217 result = show_and_select_paths (impl, parent_path, &paths, error);
7218 gtk_file_path_free (parent_path);
7222 pending_select_paths_add (impl, path);
7228 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
7229 gtk_file_path_free (parent_path);
7233 gtk_file_path_free (parent_path);
7238 unselect_func (GtkFileSystemModel *model,
7243 GtkFileChooserDefault *impl = user_data;
7244 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7245 GtkTreePath *sorted_path;
7247 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7249 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7251 gtk_tree_path_free (sorted_path);
7255 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
7256 const GtkFilePath *path)
7258 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7260 if (!impl->browse_files_model)
7263 _gtk_file_system_model_path_do (impl->browse_files_model, path,
7264 unselect_func, impl);
7268 maybe_select (GtkTreeModel *model,
7273 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7274 GtkTreeSelection *selection;
7275 const GtkFileInfo *info;
7278 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7280 info = get_list_file_info (impl, iter);
7281 is_folder = gtk_file_info_get_is_folder (info);
7283 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7284 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7285 gtk_tree_selection_select_iter (selection, iter);
7287 gtk_tree_selection_unselect_iter (selection, iter);
7293 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7295 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7297 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7298 impl->operation_mode == OPERATION_MODE_RECENT)
7300 GtkTreeSelection *selection;
7302 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7303 gtk_tree_selection_select_all (selection);
7307 if (impl->select_multiple)
7308 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7309 maybe_select, impl);
7313 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7315 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7316 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7318 gtk_tree_selection_unselect_all (selection);
7319 pending_select_paths_free (impl);
7322 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7324 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7326 * is_empty_ret - whether the file entry is totally empty
7328 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7329 * the path will be "$cwd/foobar")
7332 check_save_entry (GtkFileChooserDefault *impl,
7333 GtkFilePath **path_ret,
7334 gboolean *is_well_formed_ret,
7335 gboolean *is_empty_ret,
7336 gboolean *is_file_part_empty_ret,
7337 gboolean *is_folder)
7339 GtkFileChooserEntry *chooser_entry;
7340 const GtkFilePath *current_folder;
7341 const char *file_part;
7345 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7346 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7347 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7348 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7349 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7351 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7353 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7356 *is_well_formed_ret = TRUE;
7357 *is_empty_ret = TRUE;
7358 *is_file_part_empty_ret = TRUE;
7364 *is_empty_ret = FALSE;
7366 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7367 if (!current_folder)
7370 *is_well_formed_ret = FALSE;
7371 *is_file_part_empty_ret = FALSE;
7377 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7379 if (!file_part || file_part[0] == '\0')
7381 *path_ret = gtk_file_path_copy (current_folder);
7382 *is_well_formed_ret = TRUE;
7383 *is_file_part_empty_ret = TRUE;
7389 *is_file_part_empty_ret = FALSE;
7392 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
7396 error_building_filename_dialog (impl, current_folder, file_part, error);
7398 *is_well_formed_ret = FALSE;
7405 *is_well_formed_ret = TRUE;
7406 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, path);
7409 struct get_paths_closure {
7410 GtkFileChooserDefault *impl;
7412 GtkFilePath *path_from_entry;
7416 get_paths_foreach (GtkTreeModel *model,
7421 struct get_paths_closure *info;
7422 const GtkFilePath *file_path;
7423 GtkFileSystemModel *fs_model;
7424 GtkTreeIter sel_iter;
7427 fs_model = info->impl->browse_files_model;
7428 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7430 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
7432 return; /* We are on the editable row */
7434 if (!info->path_from_entry
7435 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
7436 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
7440 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
7442 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7443 struct get_paths_closure info;
7444 GtkWindow *toplevel;
7445 GtkWidget *current_focus;
7446 gboolean file_list_seen;
7448 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7449 return search_get_selected_paths (impl);
7451 if (impl->operation_mode == OPERATION_MODE_RECENT)
7452 return recent_get_selected_paths (impl);
7456 info.path_from_entry = NULL;
7458 toplevel = get_toplevel (GTK_WIDGET (impl));
7460 current_focus = gtk_window_get_focus (toplevel);
7462 current_focus = NULL;
7464 file_list_seen = FALSE;
7465 if (current_focus == impl->browse_files_tree_view)
7467 GtkTreeSelection *selection;
7471 file_list_seen = TRUE;
7472 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7473 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
7475 /* If there is no selection in the file list, we probably have this situation:
7477 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7478 * 2. He then double-clicked on a folder ("bar") in the file list
7480 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7481 * filename entry to see if that is the case.
7483 if (info.result == NULL && impl->location_entry)
7486 else if (impl->location_entry && current_focus == impl->location_entry)
7488 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7492 check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7497 if (!is_well_formed)
7500 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7502 gtk_file_path_free (info.path_from_entry);
7506 if (info.path_from_entry)
7507 info.result = g_slist_prepend (info.result, info.path_from_entry);
7508 else if (!file_list_seen)
7513 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7515 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7519 /* The focus is on a dialog's action area button or something else */
7520 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7521 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7529 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7530 * fall back to the current directory */
7531 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7532 info.result == NULL)
7534 info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_path (chooser));
7537 return g_slist_reverse (info.result);
7540 static GtkFilePath *
7541 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
7543 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7545 if (impl->preview_path)
7546 return gtk_file_path_copy (impl->preview_path);
7551 static GtkFileSystem *
7552 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7554 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7556 return impl->file_system;
7559 /* Shows or hides the filter widgets */
7561 show_filters (GtkFileChooserDefault *impl,
7565 gtk_widget_show (impl->filter_combo_hbox);
7567 gtk_widget_hide (impl->filter_combo_hbox);
7571 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7572 GtkFileFilter *filter)
7574 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7577 if (g_slist_find (impl->filters, filter))
7579 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7583 g_object_ref_sink (filter);
7584 impl->filters = g_slist_append (impl->filters, filter);
7586 name = gtk_file_filter_get_name (filter);
7588 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7590 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7592 if (!g_slist_find (impl->filters, impl->current_filter))
7593 set_current_filter (impl, filter);
7595 show_filters (impl, TRUE);
7599 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7600 GtkFileFilter *filter)
7602 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7603 GtkTreeModel *model;
7607 filter_index = g_slist_index (impl->filters, filter);
7609 if (filter_index < 0)
7611 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7615 impl->filters = g_slist_remove (impl->filters, filter);
7617 if (filter == impl->current_filter)
7620 set_current_filter (impl, impl->filters->data);
7622 set_current_filter (impl, NULL);
7625 /* Remove row from the combo box */
7626 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7627 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7628 g_assert_not_reached ();
7630 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7632 g_object_unref (filter);
7635 show_filters (impl, FALSE);
7639 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7641 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7643 return g_slist_copy (impl->filters);
7646 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7648 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7651 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7654 struct AddShortcutData
7656 GtkFileChooserDefault *impl;
7661 add_shortcut_get_info_cb (GtkFileSystemHandle *handle,
7662 const GtkFileInfo *info,
7663 const GError *error,
7667 gboolean cancelled = handle->cancelled;
7668 struct AddShortcutData *data = user_data;
7670 if (!g_slist_find (data->impl->loading_shortcuts, handle))
7673 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, handle);
7675 if (cancelled || error || !gtk_file_info_get_is_folder (info))
7678 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7680 shortcuts_insert_path (data->impl, pos, SHORTCUT_TYPE_PATH, NULL, data->path, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7683 g_object_unref (data->impl);
7684 gtk_file_path_free (data->path);
7687 g_object_unref (handle);
7691 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7692 const GtkFilePath *path,
7695 GtkFileSystemHandle *handle;
7696 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7697 struct AddShortcutData *data;
7701 /* Avoid adding duplicates */
7702 pos = shortcut_find_position (impl, path);
7703 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7707 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7708 /* translators, "Shortcut" means "Bookmark" here */
7710 GTK_FILE_CHOOSER_ERROR,
7711 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7712 _("Shortcut %s already exists"),
7719 for (l = impl->loading_shortcuts; l; l = l->next)
7721 GtkFileSystemHandle *h = l->data;
7724 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7725 if (p && !gtk_file_path_compare (path, p))
7729 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7731 GTK_FILE_CHOOSER_ERROR,
7732 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7733 _("Shortcut %s already exists"),
7741 data = g_new0 (struct AddShortcutData, 1);
7742 data->impl = g_object_ref (impl);
7743 data->path = gtk_file_path_copy (path);
7745 handle = gtk_file_system_get_info (impl->file_system, path,
7746 GTK_FILE_INFO_IS_FOLDER,
7747 add_shortcut_get_info_cb, data);
7752 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, handle);
7753 g_object_set_data (G_OBJECT (handle), "add-shortcut-path-key", data->path);
7759 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7760 const GtkFilePath *path,
7763 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7770 for (l = impl->loading_shortcuts; l; l = l->next)
7772 GtkFileSystemHandle *h = l->data;
7775 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7776 if (p && !gtk_file_path_compare (path, p))
7778 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, h);
7779 gtk_file_system_cancel_operation (h);
7784 if (impl->num_shortcuts == 0)
7787 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7788 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7789 g_assert_not_reached ();
7791 for (i = 0; i < impl->num_shortcuts; i++)
7794 ShortcutType shortcut_type;
7795 GtkFilePath *shortcut;
7797 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7798 SHORTCUTS_COL_DATA, &col_data,
7799 SHORTCUTS_COL_TYPE, &shortcut_type,
7801 g_assert (col_data != NULL);
7802 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
7804 shortcut = col_data;
7805 if (gtk_file_path_compare (shortcut, path) == 0)
7807 shortcuts_remove_rows (impl, pos + i, 1);
7808 impl->num_shortcuts--;
7812 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7813 g_assert_not_reached ();
7818 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7819 /* translators, "Shortcut" means "Bookmark" here */
7821 GTK_FILE_CHOOSER_ERROR,
7822 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7823 _("Shortcut %s does not exist"),
7831 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7833 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7839 if (impl->num_shortcuts == 0)
7842 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7843 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7844 g_assert_not_reached ();
7848 for (i = 0; i < impl->num_shortcuts; i++)
7851 ShortcutType shortcut_type;
7852 GtkFilePath *shortcut;
7854 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7855 SHORTCUTS_COL_DATA, &col_data,
7856 SHORTCUTS_COL_TYPE, &shortcut_type,
7858 g_assert (col_data != NULL);
7859 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
7861 shortcut = col_data;
7862 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
7864 if (i != impl->num_shortcuts - 1)
7866 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7867 g_assert_not_reached ();
7871 return g_slist_reverse (list);
7874 /* Guesses a size based upon font sizes */
7876 find_good_size_from_style (GtkWidget *widget,
7880 GtkFileChooserDefault *impl;
7885 g_assert (widget->style != NULL);
7886 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7888 if (impl->default_width == 0 &&
7889 impl->default_height == 0)
7891 screen = gtk_widget_get_screen (widget);
7894 resolution = gdk_screen_get_resolution (screen);
7895 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7899 resolution = 96.0; /* wheeee */
7901 font_size = pango_font_description_get_size (widget->style->font_desc);
7902 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7904 impl->default_width = font_size * NUM_CHARS;
7905 impl->default_height = font_size * NUM_LINES;
7908 *width = impl->default_width;
7909 *height = impl->default_height;
7913 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7914 gint *default_width,
7915 gint *default_height)
7917 GtkFileChooserDefault *impl;
7920 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7921 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7923 if (impl->preview_widget_active &&
7924 impl->preview_widget &&
7925 GTK_WIDGET_VISIBLE (impl->preview_widget))
7927 gtk_widget_size_request (impl->preview_box, &req);
7928 *default_width += PREVIEW_HBOX_SPACING + req.width;
7931 if (impl->extra_widget &&
7932 GTK_WIDGET_VISIBLE (impl->extra_widget))
7934 gtk_widget_size_request (impl->extra_align, &req);
7935 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7939 struct switch_folder_closure {
7940 GtkFileChooserDefault *impl;
7941 const GtkFilePath *path;
7945 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7947 switch_folder_foreach_cb (GtkTreeModel *model,
7952 struct switch_folder_closure *closure;
7953 GtkTreeIter child_iter;
7957 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7959 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
7960 closure->num_selected++;
7963 /* Changes to the selected folder in the list view */
7965 switch_to_selected_folder (GtkFileChooserDefault *impl)
7967 GtkTreeSelection *selection;
7968 struct switch_folder_closure closure;
7970 /* We do this with foreach() rather than get_selected() as we may be in
7971 * multiple selection mode
7974 closure.impl = impl;
7975 closure.path = NULL;
7976 closure.num_selected = 0;
7978 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7979 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7981 g_assert (closure.path && closure.num_selected == 1);
7983 change_folder_and_display_error (impl, closure.path, FALSE);
7986 /* Gets the GtkFileInfo for the selected row in the file list; assumes single
7989 static const GtkFileInfo *
7990 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7991 gboolean *had_selection)
7993 GtkTreeSelection *selection;
7994 GtkTreeIter iter, child_iter;
7995 const GtkFileInfo *info;
7997 g_assert (!impl->select_multiple);
7998 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7999 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8001 *had_selection = FALSE;
8005 *had_selection = TRUE;
8007 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8011 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8015 /* Gets the display name of the selected file in the file list; assumes single
8016 * selection mode and that something is selected.
8018 static const gchar *
8019 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8021 const GtkFileInfo *info;
8022 gboolean had_selection;
8024 info = get_selected_file_info_from_file_list (impl, &had_selection);
8025 g_assert (had_selection);
8026 g_assert (info != NULL);
8028 return gtk_file_info_get_display_name (info);
8032 add_custom_button_to_dialog (GtkDialog *dialog,
8033 const gchar *mnemonic_label,
8034 const gchar *stock_id,
8039 button = gtk_button_new_with_mnemonic (mnemonic_label);
8040 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8041 gtk_button_set_image (GTK_BUTTON (button),
8042 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8043 gtk_widget_show (button);
8045 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8048 /* Presents an overwrite confirmation dialog; returns whether we should accept
8052 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8053 const gchar *file_part,
8054 const gchar *folder_display_name)
8056 GtkWindow *toplevel;
8060 toplevel = get_toplevel (GTK_WIDGET (impl));
8062 dialog = gtk_message_dialog_new (toplevel,
8063 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8064 GTK_MESSAGE_QUESTION,
8066 _("A file named \"%s\" already exists. Do you want to replace it?"),
8068 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8069 _("The file already exists in \"%s\". Replacing it will "
8070 "overwrite its contents."),
8071 folder_display_name);
8073 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8074 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8075 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8076 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8077 GTK_RESPONSE_ACCEPT,
8078 GTK_RESPONSE_CANCEL,
8080 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8082 if (toplevel->group)
8083 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8085 response = gtk_dialog_run (GTK_DIALOG (dialog));
8087 gtk_widget_destroy (dialog);
8089 return (response == GTK_RESPONSE_ACCEPT);
8092 struct GetDisplayNameData
8094 GtkFileChooserDefault *impl;
8099 confirmation_confirm_get_info_cb (GtkFileSystemHandle *handle,
8100 const GtkFileInfo *info,
8101 const GError *error,
8104 gboolean cancelled = handle->cancelled;
8105 gboolean should_respond = FALSE;
8106 struct GetDisplayNameData *data = user_data;
8108 if (handle != data->impl->should_respond_get_info_handle)
8111 data->impl->should_respond_get_info_handle = NULL;
8117 /* Huh? Did the folder disappear? Let the caller deal with it */
8118 should_respond = TRUE;
8120 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, gtk_file_info_get_display_name (info));
8122 set_busy_cursor (data->impl, FALSE);
8124 g_signal_emit_by_name (data->impl, "response-requested");
8127 g_object_unref (data->impl);
8128 g_free (data->file_part);
8131 g_object_unref (handle);
8134 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8135 * should respond. Can get the file part from the file list or the save entry.
8138 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8139 const gchar *file_part,
8140 const GtkFilePath *parent_path)
8142 GtkFileChooserConfirmation conf;
8144 if (!impl->do_overwrite_confirmation)
8147 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8149 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8153 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8155 struct GetDisplayNameData *data;
8157 g_assert (file_part != NULL);
8159 data = g_new0 (struct GetDisplayNameData, 1);
8160 data->impl = g_object_ref (impl);
8161 data->file_part = g_strdup (file_part);
8163 if (impl->should_respond_get_info_handle)
8164 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
8166 impl->should_respond_get_info_handle =
8167 gtk_file_system_get_info (impl->file_system, parent_path,
8168 GTK_FILE_INFO_DISPLAY_NAME,
8169 confirmation_confirm_get_info_cb,
8171 set_busy_cursor (data->impl, TRUE);
8175 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8178 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8182 g_assert_not_reached ();
8188 action_create_folder_cb (GtkFileSystemHandle *handle,
8189 const GtkFilePath *path,
8190 const GError *error,
8193 gboolean cancelled = handle->cancelled;
8194 GtkFileChooserDefault *impl = user_data;
8196 if (!g_slist_find (impl->pending_handles, handle))
8199 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
8201 set_busy_cursor (impl, FALSE);
8207 error_creating_folder_dialog (impl, path, g_error_copy (error));
8209 g_signal_emit_by_name (impl, "response-requested");
8212 g_object_unref (impl);
8213 g_object_unref (handle);
8216 struct FileExistsData
8218 GtkFileChooserDefault *impl;
8219 gboolean file_exists_and_is_not_folder;
8220 GtkFilePath *parent_path;
8225 save_entry_get_info_cb (GtkFileSystemHandle *handle,
8226 const GtkFileInfo *info,
8227 const GError *error,
8230 gboolean parent_is_folder;
8231 gboolean cancelled = handle->cancelled;
8232 struct FileExistsData *data = user_data;
8234 if (handle != data->impl->should_respond_get_info_handle)
8237 data->impl->should_respond_get_info_handle = NULL;
8239 set_busy_cursor (data->impl, FALSE);
8245 parent_is_folder = FALSE;
8247 parent_is_folder = gtk_file_info_get_is_folder (info);
8249 if (parent_is_folder)
8251 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8253 if (data->file_exists_and_is_not_folder)
8256 const char *file_part;
8258 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8259 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_path);
8262 g_signal_emit_by_name (data->impl, "response-requested");
8265 g_signal_emit_by_name (data->impl, "response-requested");
8267 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8269 GtkFileSystemHandle *handle;
8271 g_object_ref (data->impl);
8272 handle = gtk_file_system_create_folder (data->impl->file_system,
8274 action_create_folder_cb,
8276 data->impl->pending_handles = g_slist_append (data->impl->pending_handles, handle);
8277 set_busy_cursor (data->impl, TRUE);
8282 /* This will display an error, which is what we want */
8283 change_folder_and_display_error (data->impl, data->parent_path, FALSE);
8287 g_object_unref (data->impl);
8288 gtk_file_path_free (data->path);
8289 gtk_file_path_free (data->parent_path);
8292 g_object_unref (handle);
8296 file_exists_get_info_cb (GtkFileSystemHandle *handle,
8297 const GtkFileInfo *info,
8298 const GError *error,
8301 gboolean data_ownership_taken = FALSE;
8302 gboolean cancelled = handle->cancelled;
8303 gboolean file_exists_and_is_not_folder;
8304 struct FileExistsData *data = user_data;
8306 if (handle != data->impl->file_exists_get_info_handle)
8309 data->impl->file_exists_get_info_handle = NULL;
8311 set_busy_cursor (data->impl, FALSE);
8316 file_exists_and_is_not_folder = info && !gtk_file_info_get_is_folder (info);
8318 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8319 /* user typed a filename; we are done */
8320 g_signal_emit_by_name (data->impl, "response-requested");
8321 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8322 && file_exists_and_is_not_folder)
8324 /* Oops, the user typed the name of an existing path which is not
8327 error_creating_folder_over_existing_file_dialog (data->impl, data->path,
8328 g_error_copy (error));
8332 /* check that everything up to the last component exists */
8334 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8335 data_ownership_taken = TRUE;
8337 if (data->impl->should_respond_get_info_handle)
8338 gtk_file_system_cancel_operation (data->impl->should_respond_get_info_handle);
8340 data->impl->should_respond_get_info_handle =
8341 gtk_file_system_get_info (data->impl->file_system,
8343 GTK_FILE_INFO_IS_FOLDER,
8344 save_entry_get_info_cb,
8346 set_busy_cursor (data->impl, TRUE);
8350 if (!data_ownership_taken)
8352 g_object_unref (data->impl);
8353 gtk_file_path_free (data->path);
8354 gtk_file_path_free (data->parent_path);
8358 g_object_unref (handle);
8362 paste_text_received (GtkClipboard *clipboard,
8364 GtkFileChooserDefault *impl)
8371 path = gtk_file_system_uri_to_path (impl->file_system, text);
8374 if (!g_path_is_absolute (text))
8376 location_popup_handler (impl, text);
8380 path = gtk_file_system_filename_to_path (impl->file_system, text);
8383 location_popup_handler (impl, text);
8388 if (!gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (impl), path, NULL))
8389 location_popup_handler (impl, text);
8391 gtk_file_path_free (path);
8394 /* Handler for the "location-popup-on-paste" keybinding signal */
8396 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8398 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8399 GDK_SELECTION_CLIPBOARD);
8400 gtk_clipboard_request_text (clipboard,
8401 (GtkClipboardTextReceivedFunc) paste_text_received,
8406 /* Implementation for GtkFileChooserEmbed::should_respond() */
8408 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8410 GtkFileChooserDefault *impl;
8411 GtkWidget *toplevel;
8412 GtkWidget *current_focus;
8414 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8416 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8417 g_assert (GTK_IS_WINDOW (toplevel));
8419 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8421 if (current_focus == impl->browse_files_tree_view)
8423 /* The following array encodes what we do based on the impl->action and the
8424 * number of files selected.
8427 NOOP, /* Do nothing (don't respond) */
8428 RESPOND, /* Respond immediately */
8429 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8430 ALL_FILES, /* Respond only if everything selected is a file */
8431 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8432 SAVE_ENTRY, /* Go to the code for handling the save entry */
8433 NOT_REACHED /* Sanity check */
8435 static const ActionToTake what_to_do[4][3] = {
8436 /* 0 selected 1 selected many selected */
8437 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8438 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8439 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8440 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8444 gboolean all_files, all_folders;
8446 ActionToTake action;
8450 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8452 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8453 return search_should_respond (impl);
8455 if (impl->operation_mode == OPERATION_MODE_RECENT)
8456 return recent_should_respond (impl);
8458 selection_check (impl, &num_selected, &all_files, &all_folders);
8460 if (num_selected > 2)
8465 action = what_to_do [impl->action] [k];
8475 case RESPOND_OR_SWITCH:
8476 g_assert (num_selected == 1);
8480 switch_to_selected_folder (impl);
8483 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8484 return should_respond_after_confirm_overwrite (impl,
8485 get_display_name_from_file_list (impl),
8486 impl->current_folder);
8500 g_assert_not_reached ();
8503 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8506 gboolean is_well_formed, is_empty, is_file_part_empty;
8509 GtkFileChooserEntry *entry;
8514 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8515 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8516 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8517 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8518 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8520 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8521 check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8523 if (is_empty || !is_well_formed)
8526 g_assert (path != NULL);
8531 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8532 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8534 change_folder_and_display_error (impl, path, TRUE);
8537 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8538 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8540 /* The folder already exists, so we do not need to create it.
8541 * Just respond to terminate the dialog.
8547 g_assert_not_reached ();
8553 struct FileExistsData *data;
8555 /* We need to check whether path exists and is not a folder */
8557 data = g_new0 (struct FileExistsData, 1);
8558 data->impl = g_object_ref (impl);
8559 data->path = gtk_file_path_copy (path);
8560 data->parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
8562 if (impl->file_exists_get_info_handle)
8563 gtk_file_system_cancel_operation (impl->file_exists_get_info_handle);
8565 impl->file_exists_get_info_handle =
8566 gtk_file_system_get_info (impl->file_system, path,
8567 GTK_FILE_INFO_IS_FOLDER,
8568 file_exists_get_info_cb,
8571 set_busy_cursor (impl, TRUE);
8575 g_error_free (error);
8578 gtk_file_path_free (path);
8581 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8583 /* The focus is on a dialog's action area button, *and* the widget that
8584 * was focused immediately before it is the file list.
8588 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8590 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8593 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8595 /* The focus is on a dialog's action area button, *and* the widget that
8596 * was focused immediately before it is the location entry.
8601 /* The focus is on a dialog's action area button or something else */
8602 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8603 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8608 g_assert_not_reached ();
8612 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8614 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8616 GtkFileChooserDefault *impl;
8619 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8621 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8622 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8624 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8625 widget = impl->browse_files_tree_view;
8627 widget = impl->location_entry;
8629 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8630 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8631 widget = impl->location_entry;
8634 g_assert_not_reached ();
8638 g_assert (widget != NULL);
8639 gtk_widget_grab_focus (widget);
8642 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GtkFilePaths */
8644 search_selected_foreach_get_path_cb (GtkTreeModel *model,
8650 const GtkFilePath *file_path;
8651 GtkFilePath *file_path_copy;
8655 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_PATH, &file_path, -1);
8656 file_path_copy = gtk_file_path_copy (file_path);
8657 *list = g_slist_prepend (*list, file_path_copy);
8660 /* Constructs a list of the selected paths in search mode */
8662 search_get_selected_paths (GtkFileChooserDefault *impl)
8665 GtkTreeSelection *selection;
8669 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8670 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_path_cb, &result);
8671 result = g_slist_reverse (result);
8676 /* Called from ::should_respond(). We return whether there are selected files
8677 * in the search list.
8680 search_should_respond (GtkFileChooserDefault *impl)
8682 GtkTreeSelection *selection;
8684 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8686 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8687 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8690 struct SearchHitInsertRequest
8692 GtkFileChooserDefault *impl;
8694 GtkTreeRowReference *row_ref;
8698 search_hit_get_info_cb (GtkFileSystemHandle *handle,
8699 const GtkFileInfo *info,
8700 const GError *error,
8703 gboolean cancelled = handle->cancelled;
8704 GdkPixbuf *pixbuf = NULL;
8707 GtkFileSystemHandle *model_handle;
8708 gboolean is_folder = FALSE;
8711 struct SearchHitInsertRequest *request = data;
8713 if (!request->impl->search_model)
8716 path = gtk_tree_row_reference_get_path (request->row_ref);
8720 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8722 gtk_tree_path_free (path);
8724 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8725 SEARCH_MODEL_COL_HANDLE, &model_handle,
8727 if (handle != model_handle)
8730 /* set the handle to NULL in the model */
8731 gtk_list_store_set (request->impl->search_model, &iter,
8732 SEARCH_MODEL_COL_HANDLE, NULL,
8740 gtk_list_store_remove (request->impl->search_model, &iter);
8744 display_name = g_strdup (gtk_file_info_get_display_name (info));
8745 mime_type = g_strdup (gtk_file_info_get_mime_type (info));
8746 is_folder = gtk_file_info_get_is_folder (info);
8747 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8748 request->impl->icon_size, NULL);
8750 gtk_list_store_set (request->impl->search_model, &iter,
8751 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8752 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8753 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8754 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8758 g_object_unref (pixbuf);
8761 g_object_unref (request->impl);
8762 gtk_file_path_free (request->path);
8763 gtk_tree_row_reference_free (request->row_ref);
8766 g_object_unref (handle);
8769 /* Adds one hit from the search engine to the search_model */
8771 search_add_hit (GtkFileChooserDefault *impl,
8777 char *collation_key;
8778 struct stat statbuf;
8779 struct stat *statbuf_copy;
8782 GtkFileSystemHandle *handle;
8783 struct SearchHitInsertRequest *request;
8785 path = gtk_file_system_uri_to_path (impl->file_system, uri);
8789 filename = gtk_file_system_path_to_filename (impl->file_system, path);
8792 gtk_file_path_free (path);
8796 if (stat (filename, &statbuf) != 0)
8798 gtk_file_path_free (path);
8803 statbuf_copy = g_new (struct stat, 1);
8804 *statbuf_copy = statbuf;
8806 tmp = g_filename_display_name (filename);
8807 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8810 request = g_new0 (struct SearchHitInsertRequest, 1);
8811 request->impl = g_object_ref (impl);
8812 request->path = gtk_file_path_copy (path);
8814 gtk_list_store_append (impl->search_model, &iter);
8815 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8817 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8818 gtk_tree_path_free (p);
8820 handle = gtk_file_system_get_info (impl->file_system, path,
8821 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_ICON | GTK_FILE_INFO_MIME_TYPE | GTK_FILE_INFO_DISPLAY_NAME,
8822 search_hit_get_info_cb,
8825 gtk_list_store_set (impl->search_model, &iter,
8826 SEARCH_MODEL_COL_PATH, path,
8827 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8828 SEARCH_MODEL_COL_STAT, statbuf_copy,
8829 SEARCH_MODEL_COL_HANDLE, handle,
8833 /* Callback used from GtkSearchEngine when we get new hits */
8835 search_engine_hits_added_cb (GtkSearchEngine *engine,
8839 GtkFileChooserDefault *impl;
8842 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8844 for (l = hits; l; l = l->next)
8845 search_add_hit (impl, (gchar*)l->data);
8848 /* Callback used from GtkSearchEngine when the query is done running */
8850 search_engine_finished_cb (GtkSearchEngine *engine,
8853 GtkFileChooserDefault *impl;
8855 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8858 /* EB: setting the model here will avoid loads of row events,
8859 * but it'll make the search look like blocked.
8861 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8862 GTK_TREE_MODEL (impl->search_model_filter));
8865 /* FMQ: if search was empty, say that we got no hits */
8866 set_busy_cursor (impl, FALSE);
8869 /* Displays a generic error when we cannot create a GtkSearchEngine.
8870 * It would be better if _gtk_search_engine_new() gave us a GError
8871 * with a better message, but it doesn't do that right now.
8874 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8876 error_message (impl,
8877 _("Could not start the search process"),
8878 _("The program was not able to create a connection to the indexer "
8879 "daemon. Please make sure it is running."));
8883 search_engine_error_cb (GtkSearchEngine *engine,
8884 const gchar *message,
8887 GtkFileChooserDefault *impl;
8889 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8891 search_stop_searching (impl, TRUE);
8892 error_message (impl, _("Could not send the search request"), message);
8894 set_busy_cursor (impl, FALSE);
8897 /* Frees the data in the search_model */
8899 search_clear_model (GtkFileChooserDefault *impl,
8900 gboolean remove_from_treeview)
8902 GtkTreeModel *model;
8905 if (!impl->search_model)
8908 model = GTK_TREE_MODEL (impl->search_model);
8910 if (gtk_tree_model_get_iter_first (model, &iter))
8914 gchar *display_name;
8915 gchar *collation_key;
8916 struct stat *statbuf;
8917 GtkFileSystemHandle *handle;
8919 gtk_tree_model_get (model, &iter,
8920 SEARCH_MODEL_COL_PATH, &path,
8921 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8922 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key,
8923 SEARCH_MODEL_COL_STAT, &statbuf,
8924 SEARCH_MODEL_COL_HANDLE, &handle,
8928 gtk_file_system_cancel_operation (handle);
8930 gtk_file_path_free (path);
8931 g_free (display_name);
8932 g_free (collation_key);
8935 while (gtk_tree_model_iter_next (model, &iter));
8937 g_object_unref (impl->search_model);
8938 impl->search_model = NULL;
8940 g_object_unref (impl->search_model_filter);
8941 impl->search_model_filter = NULL;
8943 g_object_unref (impl->search_model_sort);
8944 impl->search_model_sort = NULL;
8946 if (remove_from_treeview)
8947 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8950 /* Stops any ongoing searches; does not touch the search_model */
8952 search_stop_searching (GtkFileChooserDefault *impl,
8953 gboolean remove_query)
8955 if (remove_query && impl->search_query)
8957 g_object_unref (impl->search_query);
8958 impl->search_query = NULL;
8961 if (impl->search_engine)
8963 _gtk_search_engine_stop (impl->search_engine);
8965 g_object_unref (impl->search_engine);
8966 impl->search_engine = NULL;
8970 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8972 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8974 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8976 search_stop_searching (impl, FALSE);
8977 search_clear_model (impl, TRUE);
8979 gtk_widget_destroy (impl->search_hbox);
8980 impl->search_hbox = NULL;
8981 impl->search_entry = NULL;
8983 gtk_widget_show (impl->browse_path_bar);
8984 gtk_widget_show (impl->browse_new_folder_button);
8986 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8987 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8989 gtk_widget_show (impl->location_button);
8991 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8992 gtk_widget_show (impl->location_entry_box);
8995 impl->operation_mode = OPERATION_MODE_BROWSE;
8997 file_list_set_sort_column_ids (impl);
9000 /* Sort callback from the path column */
9002 search_column_path_sort_func (GtkTreeModel *model,
9007 GtkFileChooserDefault *impl = user_data;
9008 GtkTreeIter child_a, child_b;
9009 const char *collation_key_a, *collation_key_b;
9010 gboolean is_folder_a, is_folder_b;
9012 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9013 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9015 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9016 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9017 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
9019 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9020 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9021 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
9024 if (!collation_key_a)
9027 if (!collation_key_b)
9030 /* always show folders first */
9031 if (is_folder_a != is_folder_b)
9032 return is_folder_a ? 1 : -1;
9034 return strcmp (collation_key_a, collation_key_b);
9037 /* Sort callback from the modification time column */
9039 search_column_mtime_sort_func (GtkTreeModel *model,
9044 GtkFileChooserDefault *impl = user_data;
9045 GtkTreeIter child_a, child_b;
9046 const struct stat *statbuf_a, *statbuf_b;
9047 gboolean is_folder_a, is_folder_b;
9049 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9050 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9052 /* Note that although we store a whole struct stat in the model, we only
9053 * compare the mtime here. If we add another column relative to a struct stat
9054 * (e.g. a file size column), we'll want another sort callback similar to this
9057 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9058 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9059 SEARCH_MODEL_COL_STAT, &statbuf_a,
9061 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9062 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9063 SEARCH_MODEL_COL_STAT, &statbuf_b,
9072 if (is_folder_a != is_folder_b)
9073 return is_folder_a ? 1 : -1;
9075 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
9077 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
9084 search_get_is_filtered (GtkFileChooserDefault *impl,
9085 const GtkFilePath *path,
9086 const gchar *display_name,
9087 const gchar *mime_type)
9089 GtkFileFilterInfo filter_info;
9090 GtkFileFilterFlags needed;
9093 if (!impl->current_filter)
9096 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9097 needed = gtk_file_filter_get_needed (impl->current_filter);
9099 filter_info.display_name = display_name;
9100 filter_info.mime_type = mime_type;
9102 if (needed & GTK_FILE_FILTER_FILENAME)
9104 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
9105 if (filter_info.filename)
9106 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9109 filter_info.filename = NULL;
9111 if (needed & GTK_FILE_FILTER_URI)
9113 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
9114 if (filter_info.uri)
9115 filter_info.contains |= GTK_FILE_FILTER_URI;
9118 filter_info.uri = NULL;
9120 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9122 if (filter_info.filename)
9123 g_free ((gchar *) filter_info.filename);
9124 if (filter_info.uri)
9125 g_free ((gchar *) filter_info.uri);
9131 /* Visibility function for the recent filter model */
9133 search_model_visible_func (GtkTreeModel *model,
9137 GtkFileChooserDefault *impl = user_data;
9138 GtkFilePath *file_path;
9139 gchar *display_name, *mime_type;
9142 if (!impl->current_filter)
9145 gtk_tree_model_get (model, iter,
9146 SEARCH_MODEL_COL_PATH, &file_path,
9147 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
9148 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9149 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9158 return !search_get_is_filtered (impl, file_path, display_name, mime_type);
9161 /* Creates the search_model and puts it in the tree view */
9163 search_setup_model (GtkFileChooserDefault *impl)
9165 g_assert (impl->search_model == NULL);
9166 g_assert (impl->search_model_filter == NULL);
9167 g_assert (impl->search_model_sort == NULL);
9169 /* We store these columns in the search model:
9171 * SEARCH_MODEL_COL_PATH - a GtkFilePath for the hit's URI, stored as a
9172 * pointer not as a GTK_TYPE_FILE_PATH
9173 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9174 * as a pointer not as a G_TYPE_STRING
9175 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9176 * as a pointer not as a G_TYPE_STRING
9177 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
9178 * SEARCH_MODEL_COL_HANDLE - handle used when getting the hit's info
9179 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9180 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9181 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9183 * Keep this in sync with the enumeration defined near the beginning
9186 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9196 impl->search_model_filter =
9197 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9198 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9199 search_model_visible_func,
9202 impl->search_model_sort =
9203 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9204 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9205 SEARCH_MODEL_COL_PATH,
9206 search_column_path_sort_func,
9208 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9209 SEARCH_MODEL_COL_STAT,
9210 search_column_mtime_sort_func,
9212 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9213 SEARCH_MODEL_COL_STAT,
9214 GTK_SORT_DESCENDING);
9216 /* EB: setting the model here will make the hits list update feel
9217 * more "alive" than setting the model at the end of the search
9220 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9221 GTK_TREE_MODEL (impl->search_model_sort));
9225 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9226 GtkTreeIter *child_iter,
9231 if (!impl->search_model)
9234 if (!impl->search_model_filter || !impl->search_model_sort)
9237 /* pass 1: get the iterator in the filter model */
9238 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9241 /* pass 2: get the iterator in the real model */
9242 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9243 child_iter, &middle);
9246 /* Creates a new query with the specified text and launches it */
9248 search_start_query (GtkFileChooserDefault *impl,
9249 const gchar *query_text)
9251 search_stop_searching (impl, FALSE);
9252 search_clear_model (impl, TRUE);
9253 search_setup_model (impl);
9254 set_busy_cursor (impl, TRUE);
9256 if (impl->search_engine == NULL)
9257 impl->search_engine = _gtk_search_engine_new ();
9259 if (!impl->search_engine)
9261 set_busy_cursor (impl, FALSE);
9262 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9266 if (!impl->search_query)
9268 impl->search_query = _gtk_query_new ();
9269 _gtk_query_set_text (impl->search_query, query_text);
9272 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9274 g_signal_connect (impl->search_engine, "hits-added",
9275 G_CALLBACK (search_engine_hits_added_cb), impl);
9276 g_signal_connect (impl->search_engine, "finished",
9277 G_CALLBACK (search_engine_finished_cb), impl);
9278 g_signal_connect (impl->search_engine, "error",
9279 G_CALLBACK (search_engine_error_cb), impl);
9281 _gtk_search_engine_start (impl->search_engine);
9284 /* Callback used when the user presses Enter while typing on the search
9285 * entry; starts the query
9288 search_entry_activate_cb (GtkEntry *entry,
9291 GtkFileChooserDefault *impl;
9294 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9296 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9297 if (strlen (text) == 0)
9300 /* reset any existing query object */
9301 if (impl->search_query)
9303 g_object_unref (impl->search_query);
9304 impl->search_query = NULL;
9307 search_start_query (impl, text);
9310 /* Hides the path bar and creates the search entry */
9312 search_setup_widgets (GtkFileChooserDefault *impl)
9316 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9320 label = gtk_label_new_with_mnemonic (_("_Search:"));
9321 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9325 impl->search_entry = gtk_entry_new ();
9326 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9327 g_signal_connect (impl->search_entry, "activate",
9328 G_CALLBACK (search_entry_activate_cb),
9330 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9332 /* if there already is a query, restart it */
9333 if (impl->search_query)
9335 gchar *query = _gtk_query_get_text (impl->search_query);
9339 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9340 search_start_query (impl, query);
9346 g_object_unref (impl->search_query);
9347 impl->search_query = NULL;
9351 gtk_widget_hide (impl->browse_path_bar);
9352 gtk_widget_hide (impl->browse_new_folder_button);
9354 /* Box for search widgets */
9355 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9356 gtk_widget_show_all (impl->search_hbox);
9358 /* Hide the location widgets temporarily */
9360 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9361 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9363 gtk_widget_hide (impl->location_button);
9364 gtk_widget_hide (impl->location_entry_box);
9367 gtk_widget_grab_focus (impl->search_entry);
9369 /* FMQ: hide the filter combo? */
9372 /* Main entry point to the searching functions; this gets called when the user
9373 * activates the Search shortcut.
9376 search_activate (GtkFileChooserDefault *impl)
9378 OperationMode previous_mode;
9380 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9382 gtk_widget_grab_focus (impl->search_entry);
9386 previous_mode = impl->operation_mode;
9387 impl->operation_mode = OPERATION_MODE_SEARCH;
9389 switch (previous_mode)
9391 case OPERATION_MODE_RECENT:
9392 recent_stop_loading (impl);
9393 recent_clear_model (impl, TRUE);
9396 case OPERATION_MODE_BROWSE:
9397 stop_loading_and_clear_list_model (impl);
9400 case OPERATION_MODE_SEARCH:
9401 g_assert_not_reached ();
9405 g_assert (impl->search_hbox == NULL);
9406 g_assert (impl->search_entry == NULL);
9407 g_assert (impl->search_model == NULL);
9408 g_assert (impl->search_model_filter == NULL);
9410 search_setup_widgets (impl);
9411 file_list_set_sort_column_ids (impl);
9415 * Recent files support
9418 /* Frees the data in the recent_model */
9420 recent_clear_model (GtkFileChooserDefault *impl,
9421 gboolean remove_from_treeview)
9423 GtkTreeModel *model;
9426 if (!impl->recent_model)
9429 model = GTK_TREE_MODEL (impl->recent_model);
9431 if (remove_from_treeview)
9432 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9434 if (gtk_tree_model_get_iter_first (model, &iter))
9438 GtkFilePath *file_path;
9439 GtkFileSystemHandle *handle;
9440 GtkRecentInfo *recent_info;
9441 gchar *display_name;
9443 gtk_tree_model_get (model, &iter,
9444 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9445 RECENT_MODEL_COL_PATH, &file_path,
9446 RECENT_MODEL_COL_HANDLE, &handle,
9447 RECENT_MODEL_COL_INFO, &recent_info,
9451 gtk_file_system_cancel_operation (handle);
9453 gtk_file_path_free (file_path);
9454 gtk_recent_info_unref (recent_info);
9455 g_free (display_name);
9457 while (gtk_tree_model_iter_next (model, &iter));
9460 g_object_unref (impl->recent_model);
9461 impl->recent_model = NULL;
9463 g_object_unref (impl->recent_model_filter);
9464 impl->recent_model_filter = NULL;
9466 g_object_unref (impl->recent_model_sort);
9467 impl->recent_model_sort = NULL;
9470 /* Stops any ongoing loading of the recent files list; does
9471 * not touch the recent_model
9474 recent_stop_loading (GtkFileChooserDefault *impl)
9476 if (impl->load_recent_id)
9478 g_source_remove (impl->load_recent_id);
9479 impl->load_recent_id = 0;
9483 /* Stops any pending load, clears the file list, and switches
9484 * back to OPERATION_MODE_BROWSE
9487 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9489 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9491 recent_stop_loading (impl);
9492 recent_clear_model (impl, TRUE);
9494 gtk_widget_show (impl->browse_path_bar);
9495 gtk_widget_show (impl->browse_new_folder_button);
9497 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9498 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9500 gtk_widget_show (impl->location_button);
9502 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9503 gtk_widget_show (impl->location_entry_box);
9506 impl->operation_mode = OPERATION_MODE_BROWSE;
9508 file_list_set_sort_column_ids (impl);
9511 /* Sort callback from the modification time column */
9513 recent_column_mtime_sort_func (GtkTreeModel *model,
9518 GtkFileChooserDefault *impl = user_data;
9519 GtkTreeIter child_a, child_b;
9520 GtkRecentInfo *info_a, *info_b;
9521 gboolean is_folder_a, is_folder_b;
9523 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9524 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9526 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9527 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9528 RECENT_MODEL_COL_INFO, &info_a,
9530 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9531 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9532 RECENT_MODEL_COL_INFO, &info_b,
9541 /* folders always go first */
9542 if (is_folder_a != is_folder_b)
9543 return is_folder_a ? 1 : -1;
9545 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9547 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9554 recent_column_path_sort_func (GtkTreeModel *model,
9559 GtkFileChooserDefault *impl = user_data;
9560 GtkTreeIter child_a, child_b;
9561 gboolean is_folder_a, is_folder_b;
9562 gchar *name_a, *name_b;
9564 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9565 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9567 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9568 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9569 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9571 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9572 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9573 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9582 if (is_folder_a != is_folder_b)
9583 return is_folder_a ? 1 : -1;
9585 return strcmp (name_a, name_b);
9589 recent_get_is_filtered (GtkFileChooserDefault *impl,
9590 const GtkFilePath *path,
9591 GtkRecentInfo *recent_info)
9593 GtkFileFilterInfo filter_info;
9594 GtkFileFilterFlags needed;
9597 if (!impl->current_filter)
9600 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9601 needed = gtk_file_filter_get_needed (impl->current_filter);
9603 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9604 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9606 if (needed & GTK_FILE_FILTER_FILENAME)
9608 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
9609 if (filter_info.filename)
9610 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9613 filter_info.filename = NULL;
9615 if (needed & GTK_FILE_FILTER_URI)
9617 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
9618 if (filter_info.uri)
9619 filter_info.contains |= GTK_FILE_FILTER_URI;
9622 filter_info.uri = NULL;
9624 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9626 if (filter_info.filename)
9627 g_free ((gchar *) filter_info.filename);
9628 if (filter_info.uri)
9629 g_free ((gchar *) filter_info.uri);
9634 /* Visibility function for the recent filter model */
9636 recent_model_visible_func (GtkTreeModel *model,
9640 GtkFileChooserDefault *impl = user_data;
9641 GtkFilePath *file_path;
9642 GtkRecentInfo *recent_info;
9645 if (!impl->current_filter)
9648 gtk_tree_model_get (model, iter,
9649 RECENT_MODEL_COL_INFO, &recent_info,
9650 RECENT_MODEL_COL_PATH, &file_path,
9651 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9660 return !recent_get_is_filtered (impl, file_path, recent_info);
9664 recent_setup_model (GtkFileChooserDefault *impl)
9666 g_assert (impl->recent_model == NULL);
9667 g_assert (impl->recent_model_filter == NULL);
9668 g_assert (impl->recent_model_sort == NULL);
9670 /* We store these columns in the search model:
9672 * RECENT_MODEL_COL_PATH - a pointer to GtkFilePath for the hit's URI,
9673 * stored as a pointer and not as a GTK_TYPE_FILE_PATH;
9674 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9675 * stored as a pointer and not as a G_TYPE_STRING;
9676 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9677 * as a GTK_TYPE_RECENT_INFO;
9678 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9679 * RECENT_MODEL_COL_HANDLE - GtkFileSystemHandle, stored as a pointer
9680 * and not as a GTK_TYPE_FILE_SYSTEM_HANDLE;
9682 * Keep this in sync with the enumeration defined near the beginning of
9685 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9692 impl->recent_model_filter =
9693 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9694 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9695 recent_model_visible_func,
9699 /* this is the model that will actually be added to
9700 * the browse_files_tree_view widget; remember: we are
9701 * stuffing the real model into a filter model and then
9702 * into a sort model; this means we'll have to translate
9703 * the child iterator *twice* to get from a path or an
9704 * iterator coming from the tree view widget to the
9705 * real data inside the model.
9707 impl->recent_model_sort =
9708 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9709 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9710 RECENT_MODEL_COL_PATH,
9711 recent_column_path_sort_func,
9713 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9714 RECENT_MODEL_COL_INFO,
9715 recent_column_mtime_sort_func,
9717 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9718 RECENT_MODEL_COL_INFO,
9719 GTK_SORT_DESCENDING);
9724 GtkFileChooserDefault *impl;
9727 gint n_loaded_items;
9728 guint needs_sorting : 1;
9732 recent_idle_cleanup (gpointer data)
9734 RecentLoadData *load_data = data;
9735 GtkFileChooserDefault *impl = load_data->impl;
9737 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9738 GTK_TREE_MODEL (impl->recent_model_sort));
9740 set_busy_cursor (impl, FALSE);
9742 impl->load_recent_id = 0;
9744 if (load_data->items)
9746 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9747 g_list_free (load_data->items);
9753 struct RecentItemInsertRequest
9755 GtkFileChooserDefault *impl;
9757 GtkTreeRowReference *row_ref;
9761 recent_item_get_info_cb (GtkFileSystemHandle *handle,
9762 const GtkFileInfo *info,
9763 const GError *error,
9766 gboolean cancelled = handle->cancelled;
9769 GtkFileSystemHandle *model_handle;
9770 gboolean is_folder = FALSE;
9771 struct RecentItemInsertRequest *request = data;
9773 if (!request->impl->recent_model)
9776 path = gtk_tree_row_reference_get_path (request->row_ref);
9780 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9782 gtk_tree_path_free (path);
9784 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9785 RECENT_MODEL_COL_HANDLE, &model_handle,
9787 if (handle != model_handle)
9790 gtk_list_store_set (request->impl->recent_model, &iter,
9791 RECENT_MODEL_COL_HANDLE, NULL,
9799 gtk_list_store_remove (request->impl->recent_model, &iter);
9803 is_folder = gtk_file_info_get_is_folder (info);
9805 gtk_list_store_set (request->impl->recent_model, &iter,
9806 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9810 g_object_unref (request->impl);
9811 gtk_file_path_free (request->path);
9812 gtk_tree_row_reference_free (request->row_ref);
9815 g_object_unref (handle);
9819 recent_sort_mru (gconstpointer a,
9822 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9823 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9825 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9829 get_recent_files_limit (GtkWidget *widget)
9831 GtkSettings *settings;
9834 if (gtk_widget_has_screen (widget))
9835 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9837 settings = gtk_settings_get_default ();
9839 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9845 recent_idle_load (gpointer data)
9847 RecentLoadData *load_data = data;
9848 GtkFileChooserDefault *impl = load_data->impl;
9851 GtkRecentInfo *info;
9852 const gchar *uri, *display_name;
9854 GtkFileSystemHandle *handle;
9855 struct RecentItemInsertRequest *request;
9857 if (!impl->recent_manager)
9860 /* first iteration: load all the items */
9861 if (!load_data->items)
9863 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9864 if (!load_data->items)
9867 load_data->needs_sorting = TRUE;
9872 /* second iteration: preliminary MRU sorting and clamping */
9873 if (load_data->needs_sorting)
9877 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9878 load_data->n_items = g_list_length (load_data->items);
9880 limit = get_recent_files_limit (GTK_WIDGET (impl));
9882 if (limit != -1 && (load_data->n_items > limit))
9886 clamp = g_list_nth (load_data->items, limit - 1);
9887 if (G_LIKELY (clamp))
9892 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9895 load_data->n_items = limit;
9899 load_data->n_loaded_items = 0;
9900 load_data->needs_sorting = FALSE;
9905 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9906 g_assert (info != NULL);
9908 uri = gtk_recent_info_get_uri (info);
9909 display_name = gtk_recent_info_get_display_name (info);
9910 path = gtk_file_system_uri_to_path (impl->file_system, uri);
9914 gtk_list_store_append (impl->recent_model, &iter);
9915 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9917 request = g_new0 (struct RecentItemInsertRequest, 1);
9918 request->impl = g_object_ref (impl);
9919 request->path = gtk_file_path_copy (path);
9920 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9921 gtk_tree_path_free (p);
9923 handle = gtk_file_system_get_info (impl->file_system, path,
9924 GTK_FILE_INFO_IS_FOLDER,
9925 recent_item_get_info_cb,
9928 gtk_list_store_set (impl->recent_model, &iter,
9929 RECENT_MODEL_COL_PATH, path,
9930 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9931 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9932 RECENT_MODEL_COL_HANDLE, handle,
9937 load_data->n_loaded_items += 1;
9939 /* finished loading items */
9940 if (load_data->n_loaded_items == load_data->n_items)
9942 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9943 g_list_free (load_data->items);
9944 load_data->items = NULL;
9953 recent_start_loading (GtkFileChooserDefault *impl)
9955 RecentLoadData *load_data;
9957 recent_stop_loading (impl);
9958 recent_clear_model (impl, TRUE);
9959 recent_setup_model (impl);
9960 set_busy_cursor (impl, TRUE);
9962 if (!impl->recent_manager)
9963 recent_manager_update (impl);
9965 g_assert (impl->load_recent_id == 0);
9967 load_data = g_new (RecentLoadData, 1);
9968 load_data->impl = impl;
9969 load_data->items = NULL;
9970 load_data->n_items = 0;
9971 load_data->n_loaded_items = 0;
9972 load_data->needs_sorting = TRUE;
9974 /* begin lazy loading the recent files into the model */
9975 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9978 recent_idle_cleanup);
9982 recent_selected_foreach_get_path_cb (GtkTreeModel *model,
9988 const GtkFilePath *file_path;
9989 GtkFilePath *file_path_copy;
9993 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_PATH, &file_path, -1);
9994 file_path_copy = gtk_file_path_copy (file_path);
9995 *list = g_slist_prepend (*list, file_path_copy);
9998 /* Constructs a list of the selected paths in recent files mode */
10000 recent_get_selected_paths (GtkFileChooserDefault *impl)
10003 GtkTreeSelection *selection;
10007 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10008 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_path_cb, &result);
10009 result = g_slist_reverse (result);
10014 /* Called from ::should_respond(). We return whether there are selected
10015 * files in the recent files list.
10018 recent_should_respond (GtkFileChooserDefault *impl)
10020 GtkTreeSelection *selection;
10022 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
10024 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10025 return (gtk_tree_selection_count_selected_rows (selection) != 0);
10028 /* Hide the location widgets temporarily */
10030 recent_hide_entry (GtkFileChooserDefault *impl)
10032 gtk_widget_hide (impl->browse_path_bar);
10033 gtk_widget_hide (impl->browse_new_folder_button);
10035 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10036 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10038 gtk_widget_hide (impl->location_button);
10039 gtk_widget_hide (impl->location_entry_box);
10043 /* Main entry point to the recent files functions; this gets called when
10044 * the user activates the Recently Used shortcut.
10047 recent_activate (GtkFileChooserDefault *impl)
10049 OperationMode previous_mode;
10051 if (impl->operation_mode == OPERATION_MODE_RECENT)
10054 previous_mode = impl->operation_mode;
10055 impl->operation_mode = OPERATION_MODE_RECENT;
10057 switch (previous_mode)
10059 case OPERATION_MODE_SEARCH:
10060 search_stop_searching (impl, FALSE);
10061 search_clear_model (impl, TRUE);
10063 gtk_widget_destroy (impl->search_hbox);
10064 impl->search_hbox = NULL;
10065 impl->search_entry = NULL;
10068 case OPERATION_MODE_BROWSE:
10069 stop_loading_and_clear_list_model (impl);
10072 case OPERATION_MODE_RECENT:
10073 g_assert_not_reached ();
10077 recent_hide_entry (impl);
10078 file_list_set_sort_column_ids (impl);
10079 recent_start_loading (impl);
10082 /* convert an iterator coming from the model bound to
10083 * browse_files_tree_view to an interator inside the
10084 * real recent_model
10087 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
10088 GtkTreeIter *child_iter,
10091 GtkTreeIter middle;
10093 if (!impl->recent_model)
10096 if (!impl->recent_model_filter || !impl->recent_model_sort)
10099 /* pass 1: get the iterator in the filter model */
10100 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
10103 /* pass 2: get the iterator in the real model */
10104 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
10111 set_current_filter (GtkFileChooserDefault *impl,
10112 GtkFileFilter *filter)
10114 if (impl->current_filter != filter)
10118 /* NULL filters are allowed to reset to non-filtered status
10120 filter_index = g_slist_index (impl->filters, filter);
10121 if (impl->filters && filter && filter_index < 0)
10124 if (impl->current_filter)
10125 g_object_unref (impl->current_filter);
10126 impl->current_filter = filter;
10127 if (impl->current_filter)
10129 g_object_ref_sink (impl->current_filter);
10133 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
10136 if (impl->browse_files_model)
10137 install_list_model_filter (impl);
10139 if (impl->search_model_filter)
10140 gtk_tree_model_filter_refilter (impl->search_model_filter);
10142 if (impl->recent_model_filter)
10143 gtk_tree_model_filter_refilter (impl->recent_model_filter);
10145 g_object_notify (G_OBJECT (impl), "filter");
10150 filter_combo_changed (GtkComboBox *combo_box,
10151 GtkFileChooserDefault *impl)
10153 gint new_index = gtk_combo_box_get_active (combo_box);
10154 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10156 set_current_filter (impl, new_filter);
10160 check_preview_change (GtkFileChooserDefault *impl)
10162 GtkTreePath *cursor_path;
10163 const GtkFilePath *new_path;
10164 const char *new_display_name;
10166 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10168 new_display_name = NULL;
10171 GtkTreeIter child_iter;
10173 if (impl->operation_mode == OPERATION_MODE_BROWSE)
10175 if (impl->sort_model)
10178 const GtkFileInfo *new_info;
10180 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10181 gtk_tree_path_free (cursor_path);
10183 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10185 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
10186 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10188 new_display_name = gtk_file_info_get_display_name (new_info);
10191 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10195 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10196 &iter, cursor_path);
10197 gtk_tree_path_free (cursor_path);
10199 search_get_valid_child_iter (impl, &child_iter, &iter);
10200 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10201 SEARCH_MODEL_COL_PATH, &new_path,
10202 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10205 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10209 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10210 &iter, cursor_path);
10211 gtk_tree_path_free (cursor_path);
10213 recent_get_valid_child_iter (impl, &child_iter, &iter);
10214 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10215 RECENT_MODEL_COL_PATH, &new_path,
10216 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10221 if (new_path != impl->preview_path &&
10222 !(new_path && impl->preview_path &&
10223 gtk_file_path_compare (new_path, impl->preview_path) == 0))
10225 if (impl->preview_path)
10227 gtk_file_path_free (impl->preview_path);
10228 g_free (impl->preview_display_name);
10233 impl->preview_path = gtk_file_path_copy (new_path);
10234 impl->preview_display_name = g_strdup (new_display_name);
10238 impl->preview_path = NULL;
10239 impl->preview_display_name = NULL;
10242 if (impl->use_preview_label && impl->preview_label)
10243 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10245 g_signal_emit_by_name (impl, "update-preview");
10250 shortcuts_activate_volume_mount_cb (GtkFileSystemHandle *handle,
10251 GtkFileSystemVolume *volume,
10252 const GError *error,
10256 gboolean cancelled = handle->cancelled;
10257 GtkFileChooserDefault *impl = data;
10259 if (handle != impl->shortcuts_activate_iter_handle)
10262 impl->shortcuts_activate_iter_handle = NULL;
10264 set_busy_cursor (impl, FALSE);
10273 msg = g_strdup_printf (_("Could not mount %s"),
10274 gtk_file_system_volume_get_display_name (impl->file_system, volume));
10275 error_message (impl, msg, error->message);
10281 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
10284 change_folder_and_display_error (impl, path, FALSE);
10286 gtk_file_path_free (path);
10290 g_object_unref (impl);
10291 g_object_unref (handle);
10295 /* Activates a volume by mounting it if necessary and then switching to its
10299 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10300 GtkFileSystemVolume *volume)
10304 switch (impl->operation_mode)
10306 case OPERATION_MODE_BROWSE:
10308 case OPERATION_MODE_SEARCH:
10309 search_switch_to_browse_mode (impl);
10311 case OPERATION_MODE_RECENT:
10312 recent_switch_to_browse_mode (impl);
10316 /* We ref the file chooser since volume_mount() may run a main loop, and the
10317 * user could close the file chooser window in the meantime.
10319 g_object_ref (impl);
10321 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
10323 set_busy_cursor (impl, TRUE);
10325 impl->shortcuts_activate_iter_handle =
10326 gtk_file_system_volume_mount (impl->file_system, volume,
10327 shortcuts_activate_volume_mount_cb,
10328 g_object_ref (impl));
10332 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
10335 change_folder_and_display_error (impl, path, FALSE);
10336 gtk_file_path_free (path);
10340 g_object_unref (impl);
10343 /* Opens the folder or volume at the specified iter in the shortcuts model */
10344 struct ShortcutsActivateData
10346 GtkFileChooserDefault *impl;
10351 shortcuts_activate_get_info_cb (GtkFileSystemHandle *handle,
10352 const GtkFileInfo *info,
10353 const GError *error,
10354 gpointer user_data)
10356 gboolean cancelled = handle->cancelled;
10357 struct ShortcutsActivateData *data = user_data;
10359 if (handle != data->impl->shortcuts_activate_iter_handle)
10362 data->impl->shortcuts_activate_iter_handle = NULL;
10367 if (!error && gtk_file_info_get_is_folder (info))
10368 change_folder_and_display_error (data->impl, data->path, FALSE);
10370 gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (data->impl),
10375 g_object_unref (data->impl);
10376 gtk_file_path_free (data->path);
10379 g_object_unref (handle);
10383 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10387 ShortcutType shortcut_type;
10389 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY && impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
10390 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10392 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10393 SHORTCUTS_COL_DATA, &col_data,
10394 SHORTCUTS_COL_TYPE, &shortcut_type,
10397 if (impl->shortcuts_activate_iter_handle)
10399 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
10400 impl->shortcuts_activate_iter_handle = NULL;
10403 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10405 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10407 GtkFileSystemVolume *volume;
10411 shortcuts_activate_volume (impl, volume);
10413 else if (shortcut_type == SHORTCUT_TYPE_PATH)
10415 struct ShortcutsActivateData *data;
10417 data = g_new0 (struct ShortcutsActivateData, 1);
10418 data->impl = g_object_ref (impl);
10419 data->path = gtk_file_path_copy (col_data);
10421 impl->shortcuts_activate_iter_handle =
10422 gtk_file_system_get_info (impl->file_system, data->path,
10423 GTK_FILE_INFO_IS_FOLDER,
10424 shortcuts_activate_get_info_cb, data);
10426 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10428 search_activate (impl);
10430 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10432 recent_activate (impl);
10436 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10438 shortcuts_key_press_event_cb (GtkWidget *widget,
10439 GdkEventKey *event,
10440 GtkFileChooserDefault *impl)
10444 modifiers = gtk_accelerator_get_default_mod_mask ();
10446 if ((event->keyval == GDK_BackSpace
10447 || event->keyval == GDK_Delete
10448 || event->keyval == GDK_KP_Delete)
10449 && (event->state & modifiers) == 0)
10451 remove_selected_bookmarks (impl);
10455 if ((event->keyval == GDK_F2)
10456 && (event->state & modifiers) == 0)
10458 rename_selected_bookmark (impl);
10466 shortcuts_select_func (GtkTreeSelection *selection,
10467 GtkTreeModel *model,
10469 gboolean path_currently_selected,
10472 GtkFileChooserDefault *impl = data;
10473 GtkTreeIter filter_iter;
10474 ShortcutType shortcut_type;
10476 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10477 g_assert_not_reached ();
10479 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10481 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10485 list_select_func (GtkTreeSelection *selection,
10486 GtkTreeModel *model,
10488 gboolean path_currently_selected,
10491 GtkFileChooserDefault *impl = data;
10493 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10494 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10496 GtkTreeIter iter, child_iter;
10498 switch (impl->operation_mode)
10500 case OPERATION_MODE_SEARCH:
10502 gboolean is_folder;
10504 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10507 search_get_valid_child_iter (impl, &child_iter, &iter);
10508 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10509 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10516 case OPERATION_MODE_RECENT:
10518 gboolean is_folder;
10520 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10523 recent_get_valid_child_iter (impl, &child_iter, &iter);
10524 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10525 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10532 case OPERATION_MODE_BROWSE:
10534 const GtkFileInfo *info;
10536 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10539 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10540 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10541 if (info && !gtk_file_info_get_is_folder (info))
10552 list_selection_changed (GtkTreeSelection *selection,
10553 GtkFileChooserDefault *impl)
10555 /* See if we are in the new folder editable row for Save mode */
10556 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10557 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10559 const GtkFileInfo *info;
10560 gboolean had_selection;
10562 info = get_selected_file_info_from_file_list (impl, &had_selection);
10563 if (!had_selection)
10564 goto out; /* normal processing */
10567 return; /* We are on the editable row for New Folder */
10572 if (impl->location_entry)
10573 update_chooser_entry (impl);
10575 check_preview_change (impl);
10576 bookmarks_check_add_sensitivity (impl);
10578 g_signal_emit_by_name (impl, "selection-changed", 0);
10581 /* Callback used when a row in the file list is activated */
10583 list_row_activated (GtkTreeView *tree_view,
10585 GtkTreeViewColumn *column,
10586 GtkFileChooserDefault *impl)
10589 GtkTreeIter child_iter;
10591 switch (impl->operation_mode)
10593 case OPERATION_MODE_SEARCH:
10595 GtkFilePath *file_path;
10596 gboolean is_folder;
10598 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10601 search_get_valid_child_iter (impl, &child_iter, &iter);
10602 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10603 SEARCH_MODEL_COL_PATH, &file_path,
10604 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10609 change_folder_and_display_error (impl, file_path, FALSE);
10613 g_signal_emit_by_name (impl, "file-activated");
10617 case OPERATION_MODE_RECENT:
10619 GtkFilePath *file_path;
10620 gboolean is_folder;
10622 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10625 recent_get_valid_child_iter (impl, &child_iter, &iter);
10626 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10627 RECENT_MODEL_COL_PATH, &file_path,
10628 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10633 change_folder_and_display_error (impl, file_path, FALSE);
10637 g_signal_emit_by_name (impl, "file-activated");
10641 case OPERATION_MODE_BROWSE:
10643 const GtkFileInfo *info;
10645 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10648 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10649 &child_iter, &iter);
10650 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10653 if (gtk_file_info_get_is_folder (info))
10655 const GtkFilePath *file_path;
10657 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
10658 change_folder_and_display_error (impl, file_path, FALSE);
10662 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10663 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10664 g_signal_emit_by_name (impl, "file-activated");
10671 path_bar_clicked (GtkPathBar *path_bar,
10672 GtkFilePath *file_path,
10673 GtkFilePath *child_path,
10674 gboolean child_is_hidden,
10675 GtkFileChooserDefault *impl)
10678 pending_select_paths_add (impl, child_path);
10680 if (!change_folder_and_display_error (impl, file_path, FALSE))
10683 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10684 * show hidden files so that ".baz" appears in the file list, as it will still
10685 * be shown in the path bar: "/foo/[bar]/.baz"
10687 if (child_is_hidden)
10688 g_object_set (impl, "show-hidden", TRUE, NULL);
10691 static const GtkFileInfo *
10692 get_list_file_info (GtkFileChooserDefault *impl,
10695 GtkTreeIter child_iter;
10697 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10701 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10705 list_icon_data_func (GtkTreeViewColumn *tree_column,
10706 GtkCellRenderer *cell,
10707 GtkTreeModel *tree_model,
10711 GtkFileChooserDefault *impl = data;
10712 GtkTreeIter child_iter;
10713 GdkPixbuf *pixbuf = NULL;
10714 gboolean sensitive = TRUE;
10716 profile_start ("start", NULL);
10718 switch (impl->operation_mode)
10720 case OPERATION_MODE_SEARCH:
10722 GtkTreeIter child_iter;
10723 gboolean is_folder;
10725 search_get_valid_child_iter (impl, &child_iter, iter);
10726 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10727 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10728 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10731 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10732 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10733 sensitive = is_folder;
10737 case OPERATION_MODE_RECENT:
10739 GtkTreeIter child_iter;
10740 GtkRecentInfo *info;
10741 gboolean is_folder;
10743 recent_get_valid_child_iter (impl, &child_iter, iter);
10744 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10745 RECENT_MODEL_COL_INFO, &info,
10746 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10749 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10751 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10752 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10753 sensitive = is_folder;
10757 case OPERATION_MODE_BROWSE:
10759 const GtkFileInfo *info;
10760 const GtkFilePath *path;
10762 info = get_list_file_info (impl, iter);
10764 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10767 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
10772 /* FIXME: NULL GError */
10773 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (impl),
10774 impl->icon_size, NULL);
10779 /* We are on the editable row */
10784 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10785 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10786 sensitive = gtk_file_info_get_is_folder (info);
10791 g_object_set (cell,
10793 "sensitive", sensitive,
10797 g_object_unref (pixbuf);
10799 profile_end ("end", NULL);
10803 list_name_data_func (GtkTreeViewColumn *tree_column,
10804 GtkCellRenderer *cell,
10805 GtkTreeModel *tree_model,
10809 GtkFileChooserDefault *impl = data;
10810 const GtkFileInfo *info;
10811 gboolean sensitive = TRUE;
10813 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10815 GtkTreeIter child_iter;
10816 gchar *display_name;
10817 gboolean is_folder;
10819 search_get_valid_child_iter (impl, &child_iter, iter);
10820 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10821 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10822 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10825 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10826 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10828 sensitive = is_folder;
10831 g_object_set (cell,
10832 "text", display_name,
10833 "sensitive", sensitive,
10834 "ellipsize", PANGO_ELLIPSIZE_END,
10840 if (impl->operation_mode == OPERATION_MODE_RECENT)
10842 GtkTreeIter child_iter;
10843 GtkRecentInfo *recent_info;
10844 gchar *display_name;
10845 gboolean is_folder;
10847 recent_get_valid_child_iter (impl, &child_iter, iter);
10848 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10849 RECENT_MODEL_COL_INFO, &recent_info,
10850 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10853 display_name = gtk_recent_info_get_short_name (recent_info);
10855 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10856 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10858 sensitive = is_folder;
10861 g_object_set (cell,
10862 "text", display_name,
10863 "sensitive", sensitive,
10864 "ellipsize", PANGO_ELLIPSIZE_END,
10867 g_free (display_name);
10872 info = get_list_file_info (impl, iter);
10877 g_object_set (cell,
10878 "text", _("Type name of new folder"),
10880 "ellipsize", PANGO_ELLIPSIZE_NONE,
10887 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10888 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10890 sensitive = gtk_file_info_get_is_folder (info);
10893 g_object_set (cell,
10894 "text", gtk_file_info_get_display_name (info),
10895 "sensitive", sensitive,
10896 "ellipsize", PANGO_ELLIPSIZE_END,
10902 list_size_data_func (GtkTreeViewColumn *tree_column,
10903 GtkCellRenderer *cell,
10904 GtkTreeModel *tree_model,
10908 GtkFileChooserDefault *impl = data;
10909 const GtkFileInfo *info = get_list_file_info (impl, iter);
10912 gboolean sensitive = TRUE;
10914 if (!info || gtk_file_info_get_is_folder (info))
10916 g_object_set (cell,
10918 "sensitive", sensitive,
10923 size = gtk_file_info_get_size (info);
10925 if (size < (gint64)1024)
10926 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
10927 else if (size < (gint64)1024*1024)
10928 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10929 else if (size < (gint64)1024*1024*1024)
10930 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10932 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10934 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10935 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10936 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10939 g_object_set (cell,
10941 "sensitive", sensitive,
10942 "alignment", PANGO_ALIGN_RIGHT,
10949 /* Tree column data callback for the file list; fetches the mtime of a file */
10951 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10952 GtkCellRenderer *cell,
10953 GtkTreeModel *tree_model,
10957 GtkFileChooserDefault *impl;
10959 gchar *date_str = NULL;
10960 gboolean sensitive = TRUE;
10962 const char *locale, *dot = NULL;
10963 gint64 codepage = -1;
10969 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10971 GtkTreeIter child_iter;
10972 struct stat *statbuf;
10973 gboolean is_folder;
10975 search_get_valid_child_iter (impl, &child_iter, iter);
10976 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10977 SEARCH_MODEL_COL_STAT, &statbuf,
10978 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10981 time_mtime = statbuf->st_mtime;
10986 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10987 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10988 sensitive = is_folder;
10990 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10992 GtkTreeIter child_iter;
10993 GtkRecentInfo *info;
10994 gboolean is_folder;
10996 recent_get_valid_child_iter (impl, &child_iter, iter);
10997 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10998 RECENT_MODEL_COL_INFO, &info,
10999 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11003 time_mtime = gtk_recent_info_get_modified (info);
11007 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11008 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11009 sensitive = is_folder;
11013 const GtkFileInfo *info;
11015 info = get_list_file_info (impl, iter);
11018 g_object_set (cell,
11025 time_mtime = (time_t) gtk_file_info_get_modification_time (info);
11027 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11028 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11029 sensitive = gtk_file_info_get_is_folder (info);
11032 if (G_UNLIKELY (time_mtime == 0))
11033 date_str = g_strdup (_("Unknown"));
11038 struct tm tm_mtime;
11040 const gchar *format;
11041 gchar *locale_format = NULL;
11044 #ifdef HAVE_LOCALTIME_R
11045 localtime_r ((time_t *) &time_mtime, &tm_mtime);
11048 struct tm *ptm = localtime ((time_t *) &time_mtime);
11052 g_warning ("ptm != NULL failed");
11054 g_object_set (cell,
11055 "text", _("Unknown"),
11056 "sensitive", sensitive,
11061 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
11063 #endif /* HAVE_LOCALTIME_R */
11065 g_date_set_time_t (&mtime, time_mtime);
11066 time_now = time (NULL);
11067 g_date_set_time_t (&now, time_now);
11069 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
11071 /* Translators: %H means "hours" and %M means "minutes" */
11072 if (days_diff == 0)
11073 format = _("%H:%M");
11074 else if (days_diff == 1)
11075 format = _("Yesterday at %H:%M");
11078 if (days_diff > 1 && days_diff < 7)
11079 format = "%A"; /* Days from last week */
11081 format = "%x"; /* Any other date */
11085 /* g_locale_from_utf8() returns a string in the system
11086 * code-page, which is not always the same as that used by the C
11087 * library. For instance when running a GTK+ program with
11088 * LANG=ko on an English version of Windows, the system
11089 * code-page is 1252, but the code-page used by the C library is
11090 * 949. (It's GTK+ itself that sets the C library locale when it
11091 * notices the LANG environment variable. See gtkmain.c The
11092 * Microsoft C library doesn't look at any locale environment
11093 * variables.) We need to pass strftime() a string in the C
11094 * library's code-page. See bug #509885.
11096 locale = setlocale (LC_ALL, NULL);
11097 if (locale != NULL)
11098 dot = strchr (locale, '.');
11101 codepage = g_ascii_strtoll (dot+1, NULL, 10);
11103 /* All codepages should fit in 16 bits AFAIK */
11104 if (codepage > 0 && codepage < 65536)
11106 sprintf (charset, "CP%u", (guint) codepage);
11107 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
11111 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
11113 if (locale_format != NULL &&
11114 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
11117 /* As above but in opposite direction... */
11118 if (codepage > 0 && codepage < 65536)
11119 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
11121 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11125 if (date_str == NULL)
11126 date_str = g_strdup (_("Unknown"));
11128 g_free (locale_format);
11131 g_object_set (cell,
11133 "sensitive", sensitive,
11139 _gtk_file_chooser_default_new (const char *file_system)
11141 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
11142 "file-system-backend", file_system,
11147 location_set_user_text (GtkFileChooserDefault *impl,
11150 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11151 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11155 location_popup_handler (GtkFileChooserDefault *impl,
11158 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11160 GtkWidget *widget_to_focus;
11162 /* This will give us the location widgets back */
11163 switch (impl->operation_mode)
11165 case OPERATION_MODE_SEARCH:
11166 search_switch_to_browse_mode (impl);
11168 case OPERATION_MODE_RECENT:
11169 recent_switch_to_browse_mode (impl);
11171 case OPERATION_MODE_BROWSE:
11172 g_assert_not_reached ();
11176 if (impl->current_folder)
11177 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11179 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11180 widget_to_focus = impl->browse_files_tree_view;
11182 widget_to_focus = impl->location_entry;
11184 gtk_widget_grab_focus (widget_to_focus);
11188 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11189 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11191 LocationMode new_mode;
11195 /* since the user typed something, we unconditionally want to turn on the entry */
11196 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11198 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11199 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11200 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11201 new_mode = LOCATION_MODE_PATH_BAR;
11204 g_assert_not_reached ();
11208 location_mode_set (impl, new_mode, TRUE);
11209 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11212 location_set_user_text (impl, path);
11215 location_entry_set_initial_text (impl);
11216 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11220 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11221 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11223 gtk_widget_grab_focus (impl->location_entry);
11225 location_set_user_text (impl, path);
11228 g_assert_not_reached ();
11231 /* Handler for the "up-folder" keybinding signal */
11233 up_folder_handler (GtkFileChooserDefault *impl)
11235 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11238 /* Handler for the "down-folder" keybinding signal */
11240 down_folder_handler (GtkFileChooserDefault *impl)
11242 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11245 /* Switches to the shortcut in the specified index */
11247 switch_to_shortcut (GtkFileChooserDefault *impl,
11252 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11253 g_assert_not_reached ();
11255 shortcuts_activate_iter (impl, &iter);
11258 /* Handler for the "home-folder" keybinding signal */
11260 home_folder_handler (GtkFileChooserDefault *impl)
11262 if (impl->has_home)
11263 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11266 /* Handler for the "desktop-folder" keybinding signal */
11268 desktop_folder_handler (GtkFileChooserDefault *impl)
11270 if (impl->has_desktop)
11271 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11274 /* Handler for the "search-shortcut" keybinding signal */
11276 search_shortcut_handler (GtkFileChooserDefault *impl)
11278 if (impl->has_search)
11280 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11282 /* we want the entry widget to grab the focus the first
11283 * time, not the browse_files_tree_view widget.
11285 if (impl->search_entry)
11286 gtk_widget_grab_focus (impl->search_entry);
11290 /* Handler for the "recent-shortcut" keybinding signal */
11292 recent_shortcut_handler (GtkFileChooserDefault *impl)
11294 if (impl->has_recent)
11295 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11299 quick_bookmark_handler (GtkFileChooserDefault *impl,
11300 gint bookmark_index)
11305 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11308 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11310 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11311 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11314 gtk_tree_path_free (path);
11316 switch_to_shortcut (impl, bookmark_pos);
11320 show_hidden_handler (GtkFileChooserDefault *impl)
11322 g_object_set (impl,
11323 "show-hidden", !impl->show_hidden,
11328 /* Drag and drop interfaces */
11331 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11336 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11338 model->impl = NULL;
11341 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11343 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11346 ShortcutsPaneModelFilter *model;
11350 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11352 pos = *gtk_tree_path_get_indices (path);
11353 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11355 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11358 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11360 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11362 GtkSelectionData *selection_data)
11364 ShortcutsPaneModelFilter *model;
11366 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11373 /* Fill the GtkTreeDragSourceIface vtable */
11375 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11377 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11378 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11382 /* Fill the GtkTreeDragDestIface vtable */
11384 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11386 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11387 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11391 static GtkTreeModel *
11392 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11393 GtkTreeModel *child_model,
11396 ShortcutsPaneModelFilter *model;
11398 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11399 "child-model", child_model,
11400 "virtual-root", root,
11403 model->impl = impl;
11405 return GTK_TREE_MODEL (model);
11411 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11414 RecentModelSort *model;
11415 GtkTreeIter iter, child_iter;
11416 gboolean is_folder;
11418 model = RECENT_MODEL_SORT (drag_source);
11419 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11422 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11423 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11424 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11431 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11433 GtkSelectionData *selection_data)
11435 RecentModelSort *model;
11436 GtkTreeIter iter, child_iter;
11437 GtkFilePath *file_path;
11440 model = RECENT_MODEL_SORT (drag_source);
11441 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11444 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11445 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11446 RECENT_MODEL_COL_PATH, &file_path,
11448 g_assert (file_path != NULL);
11450 uris[0] = gtk_file_system_path_to_uri (model->impl->file_system, file_path);
11453 gtk_selection_data_set_uris (selection_data, uris);
11461 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11463 iface->row_draggable = recent_model_sort_row_draggable;
11464 iface->drag_data_get = recent_model_sort_drag_data_get;
11468 _recent_model_sort_class_init (RecentModelSortClass *klass)
11474 _recent_model_sort_init (RecentModelSort *model)
11476 model->impl = NULL;
11479 static GtkTreeModel *
11480 recent_model_sort_new (GtkFileChooserDefault *impl,
11481 GtkTreeModel *child_model)
11483 RecentModelSort *model;
11485 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11486 "model", child_model,
11488 model->impl = impl;
11490 return GTK_TREE_MODEL (model);
11496 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11499 SearchModelSort *model;
11500 GtkTreeIter iter, child_iter;
11501 gboolean is_folder;
11503 model = SEARCH_MODEL_SORT (drag_source);
11504 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11507 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11508 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11509 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11516 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11518 GtkSelectionData *selection_data)
11520 SearchModelSort *model;
11521 GtkTreeIter iter, child_iter;
11522 GtkFilePath *file_path;
11525 model = SEARCH_MODEL_SORT (drag_source);
11526 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11529 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11530 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11531 RECENT_MODEL_COL_PATH, &file_path,
11533 g_assert (file_path != NULL);
11535 uris[0] = gtk_file_system_path_to_uri (model->impl->file_system, file_path);
11538 gtk_selection_data_set_uris (selection_data, uris);
11546 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11548 iface->row_draggable = search_model_sort_row_draggable;
11549 iface->drag_data_get = search_model_sort_drag_data_get;
11553 _search_model_sort_class_init (SearchModelSortClass *klass)
11559 _search_model_sort_init (SearchModelSort *model)
11561 model->impl = NULL;
11564 static GtkTreeModel *
11565 search_model_sort_new (GtkFileChooserDefault *impl,
11566 GtkTreeModel *child_model)
11568 SearchModelSort *model;
11570 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11571 "model", child_model,
11573 model->impl = impl;
11575 return GTK_TREE_MODEL (model);