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)
76 #include "gtkfilesystemwin32.h"
85 #include <sys/types.h>
96 #undef PROFILE_FILE_CHOOSER
97 #ifdef PROFILE_FILE_CHOOSER
104 #define PROFILE_INDENT 4
105 static int profile_indent;
108 profile_add_indent (int indent)
110 profile_indent += indent;
111 if (profile_indent < 0)
112 g_error ("You screwed up your indentation");
116 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
121 profile_add_indent (indent);
123 if (profile_indent == 0)
124 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
126 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
132 profile_add_indent (indent);
135 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
136 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
137 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
139 #define profile_start(x, y)
140 #define profile_end(x, y)
141 #define profile_msg(x, y)
146 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
148 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
149 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
150 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
152 #define MAX_LOADING_TIME 500
154 struct _GtkFileChooserDefaultClass
156 GtkVBoxClass parent_class;
162 LOCATION_POPUP_ON_PASTE,
168 LOCATION_TOGGLE_POPUP,
176 static guint signals[LAST_SIGNAL] = { 0 };
178 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
180 SHORTCUTS_COL_PIXBUF,
184 SHORTCUTS_COL_REMOVABLE,
185 SHORTCUTS_COL_PIXBUF_VISIBLE,
186 SHORTCUTS_COL_HANDLE,
187 SHORTCUTS_COL_NUM_COLUMNS
192 SHORTCUT_TYPE_VOLUME,
193 SHORTCUT_TYPE_SEPARATOR,
194 SHORTCUT_TYPE_SEARCH,
198 /* Column numbers for the file list */
203 FILE_LIST_COL_NUM_COLUMNS
206 /* Column numbers for the search model.
207 * Keep this in sync with search_setup_model()
210 SEARCH_MODEL_COL_PATH,
211 SEARCH_MODEL_COL_DISPLAY_NAME,
212 SEARCH_MODEL_COL_COLLATION_KEY,
213 SEARCH_MODEL_COL_STAT,
214 SEARCH_MODEL_COL_HANDLE,
215 SEARCH_MODEL_COL_PIXBUF,
216 SEARCH_MODEL_COL_MIME_TYPE,
217 SEARCH_MODEL_COL_IS_FOLDER,
218 SEARCH_MODEL_COL_NUM_COLUMNS
222 RECENT_MODEL_COL_PATH,
223 RECENT_MODEL_COL_DISPLAY_NAME,
224 RECENT_MODEL_COL_INFO,
225 RECENT_MODEL_COL_IS_FOLDER,
226 RECENT_MODEL_COL_HANDLE,
227 RECENT_MODEL_COL_NUM_COLUMNS
230 /* Identifiers for target types */
236 /* Target types for dragging from the shortcuts list */
237 static const GtkTargetEntry shortcuts_source_targets[] = {
238 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
241 static const int num_shortcuts_source_targets = G_N_ELEMENTS (shortcuts_source_targets);
243 /* Target types for dropping into the shortcuts list */
244 static const GtkTargetEntry shortcuts_dest_targets[] = {
245 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
246 { "text/uri-list", 0, TEXT_URI_LIST }
249 static const int num_shortcuts_dest_targets = G_N_ELEMENTS (shortcuts_dest_targets);
251 /* Target types for DnD from the file list */
252 static const GtkTargetEntry file_list_source_targets[] = {
253 { "text/uri-list", 0, TEXT_URI_LIST }
256 static const int num_file_list_source_targets = G_N_ELEMENTS (file_list_source_targets);
258 /* Target types for dropping into the file list */
259 static const GtkTargetEntry file_list_dest_targets[] = {
260 { "text/uri-list", 0, TEXT_URI_LIST }
263 static const int num_file_list_dest_targets = G_N_ELEMENTS (file_list_dest_targets);
265 /* Target types for dragging from the recent files list */
266 static const GtkTargetEntry recent_list_source_targets[] = {
267 { "text/uri-list", 0, TEXT_URI_LIST }
270 static const int num_recent_list_source_targets = G_N_ELEMENTS (recent_list_source_targets);
273 search_is_possible (GtkFileChooserDefault *impl)
275 if (impl->search_engine == NULL)
276 impl->search_engine = _gtk_search_engine_new ();
278 return impl->search_engine != NULL;
281 /* Interesting places in the shortcuts bar */
285 SHORTCUTS_RECENT_SEPARATOR,
290 SHORTCUTS_BOOKMARKS_SEPARATOR,
292 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
293 SHORTCUTS_CURRENT_FOLDER
296 /* Icon size for if we can't get it from the theme */
297 #define FALLBACK_ICON_SIZE 16
299 #define PREVIEW_HBOX_SPACING 12
303 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
304 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
306 static GObject* gtk_file_chooser_default_constructor (GType type,
307 guint n_construct_properties,
308 GObjectConstructParam *construct_params);
309 static void gtk_file_chooser_default_finalize (GObject *object);
310 static void gtk_file_chooser_default_set_property (GObject *object,
314 static void gtk_file_chooser_default_get_property (GObject *object,
318 static void gtk_file_chooser_default_dispose (GObject *object);
319 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
320 static void gtk_file_chooser_default_map (GtkWidget *widget);
321 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
322 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
323 GtkWidget *previous_toplevel);
324 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
325 GtkStyle *previous_style);
326 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
327 GdkScreen *previous_screen);
328 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
329 GtkAllocation *allocation);
331 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
332 const GtkFilePath *path,
334 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
335 const GtkFilePath *path,
337 gboolean clear_entry,
339 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
340 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
342 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
343 const GtkFilePath *path,
345 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
346 const GtkFilePath *path);
347 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
348 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
349 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
350 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
351 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
352 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
353 GtkFileFilter *filter);
354 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
355 GtkFileFilter *filter);
356 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
357 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
358 const GtkFilePath *path,
360 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
361 const GtkFilePath *path,
363 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
365 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
367 gint *default_height);
368 static gboolean gtk_file_chooser_default_get_resizable (GtkFileChooserEmbed *chooser_embed);
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->get_resizable = gtk_file_chooser_default_get_resizable;
814 iface->should_respond = gtk_file_chooser_default_should_respond;
815 iface->initial_focus = gtk_file_chooser_default_initial_focus;
819 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
821 profile_start ("start", NULL);
822 #ifdef PROFILE_FILE_CHOOSER
823 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
825 impl->local_only = TRUE;
826 impl->preview_widget_active = TRUE;
827 impl->use_preview_label = TRUE;
828 impl->select_multiple = FALSE;
829 impl->show_hidden = FALSE;
830 impl->icon_size = FALLBACK_ICON_SIZE;
831 impl->load_state = LOAD_EMPTY;
832 impl->reload_state = RELOAD_EMPTY;
833 impl->pending_select_paths = NULL;
834 impl->location_mode = LOCATION_MODE_PATH_BAR;
835 impl->operation_mode = OPERATION_MODE_BROWSE;
837 gtk_box_set_spacing (GTK_BOX (impl), 12);
839 impl->tooltips = gtk_tooltips_new ();
840 g_object_ref_sink (impl->tooltips);
842 profile_end ("end", NULL);
845 /* Frees the data columns for the specified iter in the shortcuts model*/
847 shortcuts_free_row_data (GtkFileChooserDefault *impl,
851 ShortcutType shortcut_type;
852 GtkFileSystemHandle *handle;
854 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
855 SHORTCUTS_COL_DATA, &col_data,
856 SHORTCUTS_COL_TYPE, &shortcut_type,
857 SHORTCUTS_COL_HANDLE, &handle,
861 gtk_file_system_cancel_operation (handle);
863 if (!(shortcut_type == SHORTCUT_TYPE_PATH ||
864 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
868 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
870 GtkFileSystemVolume *volume;
873 gtk_file_system_volume_free (impl->file_system, volume);
879 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
882 gtk_file_path_free (path);
886 /* Frees all the data columns in the shortcuts model */
888 shortcuts_free (GtkFileChooserDefault *impl)
892 if (!impl->shortcuts_model)
895 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
898 shortcuts_free_row_data (impl, &iter);
900 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
902 g_object_unref (impl->shortcuts_model);
903 impl->shortcuts_model = NULL;
907 pending_select_paths_free (GtkFileChooserDefault *impl)
911 for (l = impl->pending_select_paths; l; l = l->next)
916 gtk_file_path_free (path);
919 g_slist_free (impl->pending_select_paths);
920 impl->pending_select_paths = NULL;
924 pending_select_paths_add (GtkFileChooserDefault *impl,
925 const GtkFilePath *path)
927 impl->pending_select_paths =
928 g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
931 /* Used from gtk_tree_selection_selected_foreach() */
933 store_selection_foreach (GtkTreeModel *model,
938 GtkFileChooserDefault *impl;
939 GtkTreeIter child_iter;
940 const GtkFilePath *file_path;
942 impl = GTK_FILE_CHOOSER_DEFAULT (data);
944 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
946 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
947 pending_select_paths_add (impl, file_path);
950 /* Stores the current selection in the list of paths to select; this is used to
951 * preserve the selection when reloading the current folder.
954 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
956 GtkTreeSelection *selection;
958 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
959 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
963 gtk_file_chooser_default_finalize (GObject *object)
965 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
968 if (impl->shortcuts_pane_filter_model)
969 g_object_unref (impl->shortcuts_pane_filter_model);
971 if (impl->shortcuts_combo_filter_model)
972 g_object_unref (impl->shortcuts_combo_filter_model);
974 shortcuts_free (impl);
976 g_object_unref (impl->file_system);
978 g_free (impl->browse_files_last_selected_name);
980 for (l = impl->filters; l; l = l->next)
982 GtkFileFilter *filter;
984 filter = GTK_FILE_FILTER (l->data);
985 g_object_unref (filter);
987 g_slist_free (impl->filters);
989 if (impl->current_filter)
990 g_object_unref (impl->current_filter);
992 if (impl->current_volume_path)
993 gtk_file_path_free (impl->current_volume_path);
995 if (impl->current_folder)
996 gtk_file_path_free (impl->current_folder);
998 if (impl->preview_path)
999 gtk_file_path_free (impl->preview_path);
1001 load_remove_timer (impl);
1003 /* Free all the Models we have */
1004 if (impl->browse_files_model)
1005 g_object_unref (impl->browse_files_model);
1007 if (impl->sort_model)
1008 g_object_unref (impl->sort_model);
1010 search_clear_model (impl, FALSE);
1011 recent_clear_model (impl, FALSE);
1013 g_free (impl->preview_display_name);
1015 g_free (impl->edited_new_text);
1017 g_object_unref (impl->tooltips);
1019 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
1022 /* Shows an error dialog set as transient for the specified window */
1024 error_message_with_parent (GtkWindow *parent,
1030 dialog = gtk_message_dialog_new (parent,
1031 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1036 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1040 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
1042 gtk_dialog_run (GTK_DIALOG (dialog));
1043 gtk_widget_destroy (dialog);
1046 /* Returns a toplevel GtkWindow, or NULL if none */
1048 get_toplevel (GtkWidget *widget)
1050 GtkWidget *toplevel;
1052 toplevel = gtk_widget_get_toplevel (widget);
1053 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1056 return GTK_WINDOW (toplevel);
1059 /* Shows an error dialog for the file chooser */
1061 error_message (GtkFileChooserDefault *impl,
1065 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1068 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1070 error_dialog (GtkFileChooserDefault *impl,
1072 const GtkFilePath *path,
1081 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1082 text = g_strdup_printf (msg, uri);
1083 error_message (impl, text, error->message);
1086 g_error_free (error);
1090 /* Displays an error message about not being able to get information for a file.
1091 * Frees the GError as well.
1094 error_getting_info_dialog (GtkFileChooserDefault *impl,
1095 const GtkFilePath *path,
1099 _("Could not retrieve information about the file"),
1103 /* Shows an error dialog about not being able to add a bookmark */
1105 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1106 const GtkFilePath *path,
1110 _("Could not add a bookmark"),
1114 /* Shows an error dialog about not being able to remove a bookmark */
1116 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1117 const GtkFilePath *path,
1121 _("Could not remove bookmark"),
1125 /* Shows an error dialog about not being able to create a folder */
1127 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1128 const GtkFilePath *path,
1132 _("The folder could not be created"),
1136 /* Shows an error about not being able to create a folder because a file with
1137 * the same name is already there.
1140 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1141 const GtkFilePath *path,
1145 _("The folder could not be created, as a file with the same "
1146 "name already exists. Try using a different name for the "
1147 "folder, or rename the file first."),
1151 /* Shows an error dialog about not being able to create a filename */
1153 error_building_filename_dialog (GtkFileChooserDefault *impl,
1154 const GtkFilePath *folder_part,
1155 const char *file_part,
1158 error_dialog (impl, _("Invalid file name"),
1162 /* Shows an error dialog when we cannot switch to a folder */
1164 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1165 const GtkFilePath *path,
1168 error_dialog (impl, _("The folder contents could not be displayed"),
1172 /* Changes folders, displaying an error dialog if this fails */
1174 change_folder_and_display_error (GtkFileChooserDefault *impl,
1175 const GtkFilePath *path,
1176 gboolean clear_entry)
1180 GtkFilePath *path_copy;
1182 g_return_val_if_fail (path != NULL, FALSE);
1184 profile_start ("start", (char *) path);
1186 /* We copy the path because of this case:
1188 * list_row_activated()
1189 * fetches path from model; path belongs to the model (*)
1190 * calls change_folder_and_display_error()
1191 * calls _gtk_file_chooser_set_current_folder_path()
1192 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1195 path_copy = gtk_file_path_copy (path);
1198 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), path_copy, TRUE, clear_entry, &error);
1201 error_changing_folder_dialog (impl, path_copy, error);
1203 gtk_file_path_free (path_copy);
1205 profile_end ("end", (char *) path);
1211 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1213 if (impl->use_preview_label)
1215 if (!impl->preview_label)
1217 impl->preview_label = gtk_label_new (impl->preview_display_name);
1218 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1219 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1220 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1221 gtk_widget_show (impl->preview_label);
1226 if (impl->preview_label)
1228 gtk_widget_destroy (impl->preview_label);
1229 impl->preview_label = NULL;
1233 if (impl->preview_widget_active && impl->preview_widget)
1234 gtk_widget_show (impl->preview_box);
1236 gtk_widget_hide (impl->preview_box);
1238 g_signal_emit_by_name (impl, "default-size-changed");
1242 set_preview_widget (GtkFileChooserDefault *impl,
1243 GtkWidget *preview_widget)
1245 if (preview_widget == impl->preview_widget)
1248 if (impl->preview_widget)
1249 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1250 impl->preview_widget);
1252 impl->preview_widget = preview_widget;
1253 if (impl->preview_widget)
1255 gtk_widget_show (impl->preview_widget);
1256 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1257 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1258 impl->preview_widget,
1259 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1262 update_preview_widget_visibility (impl);
1265 /* Renders a "Search" icon at an appropriate size for a tree view */
1267 render_search_icon (GtkFileChooserDefault *impl)
1269 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1273 render_recent_icon (GtkFileChooserDefault *impl)
1275 GtkIconTheme *theme;
1278 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1279 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1281 theme = gtk_icon_theme_get_default ();
1283 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1289 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1295 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1296 struct ReloadIconsData
1298 GtkFileChooserDefault *impl;
1299 GtkTreeRowReference *row_ref;
1303 shortcuts_reload_icons_get_info_cb (GtkFileSystemHandle *handle,
1304 const GtkFileInfo *info,
1305 const GError *error,
1311 gboolean cancelled = handle->cancelled;
1312 struct ReloadIconsData *data = user_data;
1314 if (!g_slist_find (data->impl->reload_icon_handles, handle))
1317 data->impl->reload_icon_handles = g_slist_remove (data->impl->reload_icon_handles, handle);
1319 if (cancelled || error)
1322 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->impl),
1323 data->impl->icon_size, NULL);
1325 path = gtk_tree_row_reference_get_path (data->row_ref);
1326 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1327 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1328 SHORTCUTS_COL_PIXBUF, pixbuf,
1330 gtk_tree_path_free (path);
1333 g_object_unref (pixbuf);
1336 gtk_tree_row_reference_free (data->row_ref);
1337 g_object_unref (data->impl);
1340 g_object_unref (handle);
1344 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1349 profile_start ("start", NULL);
1351 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1354 for (l = impl->reload_icon_handles; l; l = l->next)
1356 GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1357 gtk_file_system_cancel_operation (handle);
1359 g_slist_free (impl->reload_icon_handles);
1360 impl->reload_icon_handles = NULL;
1365 ShortcutType shortcut_type;
1366 gboolean pixbuf_visible;
1369 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1370 SHORTCUTS_COL_DATA, &data,
1371 SHORTCUTS_COL_TYPE, &shortcut_type,
1372 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1378 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1380 GtkFileSystemVolume *volume;
1383 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1384 impl->icon_size, NULL);
1386 else if (shortcut_type == SHORTCUT_TYPE_PATH)
1388 if (gtk_file_system_path_is_local (impl->file_system, (GtkFilePath *)data))
1390 const GtkFilePath *path;
1391 struct ReloadIconsData *info;
1392 GtkTreePath *tree_path;
1393 GtkFileSystemHandle *handle;
1397 info = g_new0 (struct ReloadIconsData, 1);
1398 info->impl = g_object_ref (impl);
1399 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1400 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1401 gtk_tree_path_free (tree_path);
1403 handle = gtk_file_system_get_info (impl->file_system, path,
1405 shortcuts_reload_icons_get_info_cb,
1407 impl->reload_icon_handles = g_slist_append (impl->reload_icon_handles, handle);
1411 GtkIconTheme *icon_theme;
1413 /* Don't call get_info for remote paths to avoid latency and
1415 * If we switch to a better bookmarks file format (XBEL), we
1416 * should use mime info to get a better icon.
1418 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1419 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-share",
1420 impl->icon_size, 0, NULL);
1423 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1425 pixbuf = render_search_icon (impl);
1427 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1429 pixbuf = render_recent_icon (impl);
1432 gtk_list_store_set (impl->shortcuts_model, &iter,
1433 SHORTCUTS_COL_PIXBUF, pixbuf,
1437 g_object_unref (pixbuf);
1441 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1445 profile_end ("end", NULL);
1449 shortcuts_find_folder (GtkFileChooserDefault *impl,
1450 GtkFilePath *folder)
1452 GtkTreeSelection *selection;
1456 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1458 g_assert (folder != NULL);
1459 pos = shortcut_find_position (impl, folder);
1462 gtk_tree_selection_unselect_all (selection);
1466 path = gtk_tree_path_new_from_indices (pos, -1);
1467 gtk_tree_selection_select_path (selection, path);
1468 gtk_tree_path_free (path);
1471 /* If a shortcut corresponds to the current folder, selects it */
1473 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1475 shortcuts_find_folder (impl, impl->current_folder);
1478 /* Removes the specified number of rows from the shortcuts list */
1480 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1486 path = gtk_tree_path_new_from_indices (start_row, -1);
1488 for (; n_rows; n_rows--)
1492 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1493 g_assert_not_reached ();
1495 shortcuts_free_row_data (impl, &iter);
1496 gtk_list_store_remove (impl->shortcuts_model, &iter);
1499 gtk_tree_path_free (path);
1503 shortcuts_update_count (GtkFileChooserDefault *impl,
1504 ShortcutsIndex type,
1509 case SHORTCUTS_HOME:
1511 impl->has_home = FALSE;
1513 impl->has_home = TRUE;
1516 case SHORTCUTS_DESKTOP:
1518 impl->has_desktop = FALSE;
1520 impl->has_desktop = TRUE;
1523 case SHORTCUTS_VOLUMES:
1524 impl->num_volumes += value;
1527 case SHORTCUTS_SHORTCUTS:
1528 impl->num_shortcuts += value;
1531 case SHORTCUTS_BOOKMARKS:
1532 impl->num_bookmarks += value;
1535 case SHORTCUTS_CURRENT_FOLDER:
1537 impl->shortcuts_current_folder_active = FALSE;
1539 impl->shortcuts_current_folder_active = TRUE;
1548 struct ShortcutsInsertRequest
1550 GtkFileChooserDefault *impl;
1551 GtkFilePath *parent_path;
1555 GtkTreeRowReference *row_ref;
1556 ShortcutsIndex type;
1562 get_file_info_finished (GtkFileSystemHandle *handle,
1563 const GtkFileInfo *info,
1564 const GError *error,
1568 gboolean cancelled = handle->cancelled;
1572 GtkFileSystemHandle *model_handle;
1573 struct ShortcutsInsertRequest *request = data;
1575 path = gtk_tree_row_reference_get_path (request->row_ref);
1577 /* Handle doesn't exist anymore in the model */
1580 pos = gtk_tree_path_get_indices (path)[0];
1581 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1583 gtk_tree_path_free (path);
1585 /* validate handle, else goto out */
1586 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1587 SHORTCUTS_COL_HANDLE, &model_handle,
1589 if (handle != model_handle)
1592 /* set the handle to NULL in the model (we unref later on) */
1593 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1594 SHORTCUTS_COL_HANDLE, NULL,
1602 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1603 shortcuts_update_count (request->impl, request->type, -1);
1605 if (request->type == SHORTCUTS_HOME)
1607 const char *home = g_get_home_dir ();
1608 GtkFilePath *home_path;
1610 home_path = gtk_file_system_filename_to_path (request->impl->file_system, home);
1611 error_getting_info_dialog (request->impl, home_path, g_error_copy (error));
1612 gtk_file_path_free (home_path);
1614 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1616 /* Remove the current folder separator */
1617 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1618 shortcuts_remove_rows (request->impl, separator_pos, 1);
1624 if (!request->label_copy)
1625 request->label_copy = g_strdup (gtk_file_info_get_display_name (info));
1626 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1627 request->impl->icon_size, NULL);
1629 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1630 SHORTCUTS_COL_PIXBUF, pixbuf,
1631 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1632 SHORTCUTS_COL_NAME, request->label_copy,
1633 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_PATH,
1634 SHORTCUTS_COL_REMOVABLE, request->removable,
1637 if (request->impl->shortcuts_pane_filter_model)
1638 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1640 if (request->impl->shortcuts_combo_filter_model)
1641 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1643 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1644 request->impl->save_folder_combo != NULL)
1646 /* The current folder is updated via _activate_iter(), don't
1647 * have save_folder_combo_changed_cb() call _activate_iter()
1650 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1651 G_CALLBACK (save_folder_combo_changed_cb),
1654 if (request->impl->has_search)
1657 if (request->impl->has_recent)
1660 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1661 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1662 G_CALLBACK (save_folder_combo_changed_cb),
1667 g_object_unref (pixbuf);
1670 g_object_unref (request->impl);
1671 gtk_file_path_free (request->parent_path);
1672 gtk_file_path_free (request->path);
1673 gtk_tree_row_reference_free (request->row_ref);
1674 g_free (request->label_copy);
1677 g_object_unref (handle);
1680 /* FIXME: GtkFileSystem needs a function to split a remote path
1681 * into hostname and path components, or maybe just have a
1682 * gtk_file_system_path_get_display_name().
1684 * This function is also used in gtkfilechooserbutton.c
1687 _gtk_file_chooser_label_for_uri (const gchar *uri)
1689 const gchar *path, *start, *end, *p;
1690 gchar *host, *label;
1692 start = strstr (uri, "://");
1694 path = strchr (start, '/');
1700 end = uri + strlen (uri);
1704 /* strip username */
1705 p = strchr (start, '@');
1711 p = strchr (start, ':');
1715 host = g_strndup (start, end - start);
1717 /* Translators: the first string is a path and the second string
1718 * is a hostname. Nautilus and the panel contain the same string
1721 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1728 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1729 * inserts a volume. A position of -1 indicates the end of the tree.
1732 shortcuts_insert_path (GtkFileChooserDefault *impl,
1734 ShortcutType shortcut_type,
1735 GtkFileSystemVolume *volume,
1736 const GtkFilePath *path,
1739 ShortcutsIndex type)
1742 GdkPixbuf *pixbuf = NULL;
1743 gpointer data = NULL;
1745 GtkIconTheme *icon_theme;
1747 profile_start ("start", (shortcut_type == SHORTCUT_TYPE_VOLUME) ? "volume"
1748 : ((shortcut_type == SHORTCUT_TYPE_PATH) ? (char *) path : NULL));
1750 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1753 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1754 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1755 impl->icon_size, NULL);
1757 else if (shortcut_type == SHORTCUT_TYPE_PATH)
1759 if (gtk_file_system_path_is_local (impl->file_system, path))
1761 struct ShortcutsInsertRequest *request;
1762 GtkFileSystemHandle *handle;
1765 request = g_new0 (struct ShortcutsInsertRequest, 1);
1766 request->impl = g_object_ref (impl);
1767 request->path = gtk_file_path_copy (path);
1768 request->name_only = TRUE;
1769 request->removable = removable;
1771 request->type = type;
1773 request->label_copy = g_strdup (label);
1776 gtk_list_store_append (impl->shortcuts_model, &iter);
1778 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1780 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1781 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1782 gtk_tree_path_free (p);
1784 handle = gtk_file_system_get_info (request->impl->file_system, request->path,
1785 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN | GTK_FILE_INFO_ICON,
1786 get_file_info_finished, request);
1788 gtk_list_store_set (impl->shortcuts_model, &iter,
1789 SHORTCUTS_COL_DATA, gtk_file_path_copy (path),
1790 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_PATH,
1791 SHORTCUTS_COL_HANDLE, handle,
1794 shortcuts_update_count (impl, type, 1);
1800 /* Don't call get_info for remote paths to avoid latency and
1803 data = gtk_file_path_copy (path);
1805 label_copy = g_strdup (label);
1810 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1812 label_copy = _gtk_file_chooser_label_for_uri (uri);
1817 /* If we switch to a better bookmarks file format (XBEL), we
1818 * should use mime info to get a better icon.
1820 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1821 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-share",
1822 impl->icon_size, 0, NULL);
1827 g_assert_not_reached ();
1833 gtk_list_store_append (impl->shortcuts_model, &iter);
1835 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1837 shortcuts_update_count (impl, type, 1);
1839 gtk_list_store_set (impl->shortcuts_model, &iter,
1840 SHORTCUTS_COL_PIXBUF, pixbuf,
1841 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1842 SHORTCUTS_COL_NAME, label_copy,
1843 SHORTCUTS_COL_DATA, data,
1844 SHORTCUTS_COL_TYPE, shortcut_type,
1845 SHORTCUTS_COL_REMOVABLE, removable,
1846 SHORTCUTS_COL_HANDLE, NULL,
1849 if (impl->shortcuts_pane_filter_model)
1850 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1852 if (impl->shortcuts_combo_filter_model)
1853 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1855 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1857 /* The current folder is updated via _activate_iter(), don't
1858 * have save_folder_combo_changed_cb() call _activate_iter()
1861 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1863 if (impl->has_search)
1866 if (impl->has_recent)
1869 g_signal_handlers_block_by_func (impl->save_folder_combo,
1870 G_CALLBACK (save_folder_combo_changed_cb),
1873 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1874 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1875 G_CALLBACK (save_folder_combo_changed_cb),
1879 g_free (label_copy);
1882 g_object_unref (pixbuf);
1884 profile_end ("end", NULL);
1888 shortcuts_append_search (GtkFileChooserDefault *impl)
1893 pixbuf = render_search_icon (impl);
1895 gtk_list_store_append (impl->shortcuts_model, &iter);
1896 gtk_list_store_set (impl->shortcuts_model, &iter,
1897 SHORTCUTS_COL_PIXBUF, pixbuf,
1898 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1899 SHORTCUTS_COL_NAME, _("Search"),
1900 SHORTCUTS_COL_DATA, NULL,
1901 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1902 SHORTCUTS_COL_REMOVABLE, FALSE,
1906 g_object_unref (pixbuf);
1908 impl->has_search = TRUE;
1912 shortcuts_append_recent (GtkFileChooserDefault *impl)
1917 pixbuf = render_recent_icon (impl);
1919 gtk_list_store_append (impl->shortcuts_model, &iter);
1920 gtk_list_store_set (impl->shortcuts_model, &iter,
1921 SHORTCUTS_COL_PIXBUF, pixbuf,
1922 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1923 SHORTCUTS_COL_NAME, _("Recently Used"),
1924 SHORTCUTS_COL_DATA, NULL,
1925 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1926 SHORTCUTS_COL_REMOVABLE, FALSE,
1930 g_object_unref (pixbuf);
1932 impl->has_recent = TRUE;
1935 /* Appends an item for the user's home directory to the shortcuts model */
1937 shortcuts_append_home (GtkFileChooserDefault *impl)
1940 GtkFilePath *home_path;
1942 profile_start ("start", NULL);
1944 home = g_get_home_dir ();
1947 profile_end ("end - no home directory!?", NULL);
1951 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1953 shortcuts_insert_path (impl, -1, SHORTCUT_TYPE_PATH, NULL, home_path, NULL, FALSE, SHORTCUTS_HOME);
1954 impl->has_home = TRUE;
1956 gtk_file_path_free (home_path);
1958 profile_end ("end", NULL);
1961 /* Appends the ~/Desktop directory to the shortcuts model */
1963 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1968 profile_start ("start", NULL);
1970 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1971 path = gtk_file_system_filename_to_path (impl->file_system, name);
1972 shortcuts_insert_path (impl, -1, SHORTCUT_TYPE_PATH, NULL, path, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1973 impl->has_desktop = TRUE;
1975 /* We do not actually pop up an error dialog if there is no desktop directory
1976 * because some people may really not want to have one.
1979 gtk_file_path_free (path);
1981 profile_end ("end", NULL);
1984 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1986 shortcuts_append_paths (GtkFileChooserDefault *impl,
1993 profile_start ("start", NULL);
1995 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1998 for (; paths; paths = paths->next)
2004 if (impl->local_only &&
2005 !gtk_file_system_path_is_local (impl->file_system, path))
2007 if (shortcut_find_position (impl, path) != -1)
2010 label = gtk_file_system_get_bookmark_label (impl->file_system, path);
2012 /* NULL GError, but we don't really want to show error boxes here */
2013 shortcuts_insert_path (impl, start_row + num_inserted, SHORTCUT_TYPE_PATH, NULL, path, label, TRUE, SHORTCUTS_BOOKMARKS);
2019 profile_end ("end", NULL);
2021 return num_inserted;
2024 /* Returns the index for the corresponding item in the shortcuts bar */
2026 shortcuts_get_index (GtkFileChooserDefault *impl,
2027 ShortcutsIndex where)
2033 if (where == SHORTCUTS_SEARCH)
2036 n += impl->has_search ? 1 : 0;
2038 if (where == SHORTCUTS_RECENT)
2041 n += impl->has_recent ? 1 : 0;
2043 if (where == SHORTCUTS_RECENT_SEPARATOR)
2046 n += impl->has_recent ? 1 : 0;
2048 if (where == SHORTCUTS_HOME)
2051 n += impl->has_home ? 1 : 0;
2053 if (where == SHORTCUTS_DESKTOP)
2056 n += impl->has_desktop ? 1 : 0;
2058 if (where == SHORTCUTS_VOLUMES)
2061 n += impl->num_volumes;
2063 if (where == SHORTCUTS_SHORTCUTS)
2066 n += impl->num_shortcuts;
2068 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2071 /* If there are no bookmarks there won't be a separator */
2072 n += (impl->num_bookmarks > 0) ? 1 : 0;
2074 if (where == SHORTCUTS_BOOKMARKS)
2077 n += impl->num_bookmarks;
2079 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2084 if (where == SHORTCUTS_CURRENT_FOLDER)
2087 g_assert_not_reached ();
2094 /* Adds all the file system volumes to the shortcuts model */
2096 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2101 gboolean old_changing_folders;
2103 profile_start ("start", NULL);
2106 old_changing_folders = impl->changing_folder;
2107 impl->changing_folder = TRUE;
2109 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2110 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2111 impl->num_volumes = 0;
2113 list = gtk_file_system_list_volumes (impl->file_system);
2117 for (l = list; l; l = l->next)
2119 GtkFileSystemVolume *volume;
2123 if (impl->local_only)
2125 if (gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
2127 GtkFilePath *base_path;
2129 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2130 if (base_path != NULL)
2132 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
2133 gtk_file_path_free (base_path);
2137 gtk_file_system_volume_free (impl->file_system, volume);
2144 shortcuts_insert_path (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2148 impl->num_volumes = n;
2149 g_slist_free (list);
2151 if (impl->shortcuts_pane_filter_model)
2152 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2154 if (impl->shortcuts_combo_filter_model)
2155 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2157 impl->changing_folder = old_changing_folders;
2159 profile_end ("end", NULL);
2162 /* Inserts a separator node in the shortcuts list */
2164 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2165 ShortcutsIndex where)
2169 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2170 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2171 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2173 gtk_list_store_insert (impl->shortcuts_model, &iter,
2174 shortcuts_get_index (impl, where));
2175 gtk_list_store_set (impl->shortcuts_model, &iter,
2176 SHORTCUTS_COL_PIXBUF, NULL,
2177 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2178 SHORTCUTS_COL_NAME, NULL,
2179 SHORTCUTS_COL_DATA, NULL,
2180 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2184 /* Updates the list of bookmarks */
2186 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2189 gboolean old_changing_folders;
2191 GtkFilePath *list_selected = NULL;
2192 GtkFilePath *combo_selected = NULL;
2193 ShortcutType shortcut_type;
2196 profile_start ("start", NULL);
2198 old_changing_folders = impl->changing_folder;
2199 impl->changing_folder = TRUE;
2201 if (shortcuts_get_selected (impl, &iter))
2203 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2205 SHORTCUTS_COL_DATA, &col_data,
2206 SHORTCUTS_COL_TYPE, &shortcut_type,
2209 if (col_data && shortcut_type == SHORTCUT_TYPE_PATH)
2210 list_selected = gtk_file_path_copy (col_data);
2213 if (impl->save_folder_combo &&
2214 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2217 GtkTreeIter child_iter;
2219 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2222 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2224 SHORTCUTS_COL_DATA, &col_data,
2225 SHORTCUTS_COL_TYPE, &shortcut_type,
2228 if (col_data && shortcut_type == SHORTCUT_TYPE_PATH)
2229 combo_selected = gtk_file_path_copy (col_data);
2232 if (impl->num_bookmarks > 0)
2233 shortcuts_remove_rows (impl,
2234 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2235 impl->num_bookmarks + 1);
2237 impl->num_bookmarks = 0;
2238 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2240 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
2241 shortcuts_append_paths (impl, bookmarks);
2242 gtk_file_paths_free (bookmarks);
2244 if (impl->num_bookmarks == 0)
2245 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2247 if (impl->shortcuts_pane_filter_model)
2248 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2250 if (impl->shortcuts_combo_filter_model)
2251 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2255 shortcuts_find_folder (impl, list_selected);
2256 gtk_file_path_free (list_selected);
2263 pos = shortcut_find_position (impl, combo_selected);
2266 if (impl->has_search)
2269 if (impl->has_recent)
2272 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2275 gtk_file_path_free (combo_selected);
2278 impl->changing_folder = old_changing_folders;
2280 profile_end ("end", NULL);
2283 /* Appends a separator and a row to the shortcuts list for the current folder */
2285 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2290 g_assert (!impl->shortcuts_current_folder_active);
2294 g_assert (impl->current_folder != NULL);
2296 pos = shortcut_find_position (impl, impl->current_folder);
2299 GtkFileSystemVolume *volume;
2300 GtkFilePath *base_path;
2304 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2308 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2310 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
2312 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2317 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
2319 shortcuts_insert_path (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2323 shortcuts_insert_path (impl, pos, SHORTCUT_TYPE_PATH, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2325 gtk_file_system_volume_free (impl->file_system, volume);
2329 gtk_file_path_free (base_path);
2331 else if (impl->save_folder_combo != NULL)
2333 if (impl->has_search)
2336 if (impl->has_recent)
2337 pos -= 2; /* + separator */
2339 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2343 /* Updates the current folder row in the shortcuts model */
2345 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2349 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2351 if (impl->shortcuts_current_folder_active)
2353 shortcuts_remove_rows (impl, pos, 2);
2354 impl->shortcuts_current_folder_active = FALSE;
2357 shortcuts_add_current_folder (impl);
2360 /* Filter function used for the shortcuts filter model */
2362 shortcuts_pane_filter_cb (GtkTreeModel *model,
2366 GtkFileChooserDefault *impl;
2370 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2372 path = gtk_tree_model_get_path (model, iter);
2376 pos = *gtk_tree_path_get_indices (path);
2377 gtk_tree_path_free (path);
2379 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2382 /* Creates the list model for shortcuts */
2384 shortcuts_model_create (GtkFileChooserDefault *impl)
2386 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2387 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2388 GDK_TYPE_PIXBUF, /* pixbuf */
2389 G_TYPE_STRING, /* name */
2390 G_TYPE_POINTER, /* path or volume */
2391 G_TYPE_INT, /* ShortcutType */
2392 G_TYPE_BOOLEAN, /* removable */
2393 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2394 G_TYPE_POINTER); /* GtkFileSystemHandle */
2396 if (search_is_possible (impl))
2398 shortcuts_append_search (impl);
2401 if (impl->recent_manager)
2403 shortcuts_append_recent (impl);
2404 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2407 if (impl->file_system)
2409 shortcuts_append_home (impl);
2410 shortcuts_append_desktop (impl);
2411 shortcuts_add_volumes (impl);
2414 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2415 GTK_TREE_MODEL (impl->shortcuts_model),
2418 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2419 shortcuts_pane_filter_cb,
2424 /* Callback used when the "New Folder" button is clicked */
2426 new_folder_button_clicked (GtkButton *button,
2427 GtkFileChooserDefault *impl)
2432 if (!impl->browse_files_model)
2433 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2435 /* Prevent button from being clicked twice */
2436 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2438 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2440 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2441 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2442 path, impl->list_name_column,
2445 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2446 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2448 impl->list_name_column,
2451 gtk_tree_path_free (path);
2455 edited_idle_create_folder_cb (GtkFileSystemHandle *handle,
2456 const GtkFilePath *path,
2457 const GError *error,
2460 gboolean cancelled = handle->cancelled;
2461 GtkFileChooserDefault *impl = data;
2463 if (!g_slist_find (impl->pending_handles, handle))
2466 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
2472 change_folder_and_display_error (impl, path, FALSE);
2474 error_creating_folder_dialog (impl, path, g_error_copy (error));
2477 g_object_unref (impl);
2478 g_object_unref (handle);
2481 /* Idle handler for creating a new folder after editing its name cell, or for
2482 * canceling the editing.
2485 edited_idle_cb (GtkFileChooserDefault *impl)
2487 GDK_THREADS_ENTER ();
2489 g_source_destroy (impl->edited_idle);
2490 impl->edited_idle = NULL;
2492 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2493 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2495 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2497 if (impl->edited_new_text) /* not cancelled? */
2500 GtkFilePath *file_path;
2503 file_path = gtk_file_system_make_path (impl->file_system,
2504 impl->current_folder,
2505 impl->edited_new_text,
2509 GtkFileSystemHandle *handle;
2511 handle = gtk_file_system_create_folder (impl->file_system, file_path,
2512 edited_idle_create_folder_cb,
2513 g_object_ref (impl));
2514 impl->pending_handles = g_slist_append (impl->pending_handles, handle);
2516 gtk_file_path_free (file_path);
2519 error_creating_folder_dialog (impl, file_path, error);
2521 g_free (impl->edited_new_text);
2522 impl->edited_new_text = NULL;
2525 GDK_THREADS_LEAVE ();
2531 queue_edited_idle (GtkFileChooserDefault *impl,
2532 const gchar *new_text)
2534 /* We create the folder in an idle handler so that we don't modify the tree
2538 if (!impl->edited_idle)
2540 impl->edited_idle = g_idle_source_new ();
2541 g_source_set_closure (impl->edited_idle,
2542 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2544 g_source_attach (impl->edited_idle, NULL);
2547 g_free (impl->edited_new_text);
2548 impl->edited_new_text = g_strdup (new_text);
2551 /* Callback used from the text cell renderer when the new folder is named */
2553 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2555 const gchar *new_text,
2556 GtkFileChooserDefault *impl)
2558 /* work around bug #154921 */
2559 g_object_set (cell_renderer_text,
2560 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2561 queue_edited_idle (impl, new_text);
2564 /* Callback used from the text cell renderer when the new folder edition gets
2568 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2569 GtkFileChooserDefault *impl)
2571 /* work around bug #154921 */
2572 g_object_set (cell_renderer_text,
2573 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2574 queue_edited_idle (impl, NULL);
2577 /* Creates the widgets for the filter combo box */
2579 filter_create (GtkFileChooserDefault *impl)
2581 impl->filter_combo = gtk_combo_box_new_text ();
2582 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2584 g_signal_connect (impl->filter_combo, "changed",
2585 G_CALLBACK (filter_combo_changed), impl);
2587 gtk_widget_set_tooltip_text (impl->filter_combo,
2588 _("Select which types of files are shown"));
2590 return impl->filter_combo;
2594 button_new (GtkFileChooserDefault *impl,
2596 const char *stock_id,
2604 button = gtk_button_new_with_mnemonic (text);
2605 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2606 gtk_button_set_image (GTK_BUTTON (button), image);
2608 gtk_widget_set_sensitive (button, sensitive);
2609 g_signal_connect (button, "clicked", callback, impl);
2612 gtk_widget_show (button);
2617 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2619 shortcut_find_position (GtkFileChooserDefault *impl,
2620 const GtkFilePath *path)
2624 int current_folder_separator_idx;
2626 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2629 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2632 /* FIXME: is this still needed? */
2633 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2637 for (i = 0; i < current_folder_separator_idx; i++)
2640 ShortcutType shortcut_type;
2642 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2643 SHORTCUTS_COL_DATA, &col_data,
2644 SHORTCUTS_COL_TYPE, &shortcut_type,
2649 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2651 GtkFileSystemVolume *volume;
2652 GtkFilePath *base_path;
2656 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2658 exists = base_path && strcmp (gtk_file_path_get_string (path),
2659 gtk_file_path_get_string (base_path)) == 0;
2665 else if (shortcut_type == SHORTCUT_TYPE_PATH)
2667 GtkFilePath *model_path;
2669 model_path = col_data;
2671 if (model_path && gtk_file_path_compare (model_path, path) == 0)
2676 if (i < current_folder_separator_idx - 1)
2678 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2679 g_assert_not_reached ();
2686 /* Tries to add a bookmark from a path name */
2688 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
2689 const GtkFilePath *path,
2694 g_return_val_if_fail (path != NULL, FALSE);
2696 if (shortcut_find_position (impl, path) != -1)
2700 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
2702 error_adding_bookmark_dialog (impl, path, error);
2710 add_bookmark_foreach_cb (GtkTreeModel *model,
2715 GtkFileChooserDefault *impl;
2716 GtkFileSystemModel *fs_model;
2717 GtkTreeIter child_iter;
2718 const GtkFilePath *file_path;
2720 impl = (GtkFileChooserDefault *) data;
2722 switch (impl->operation_mode)
2724 case OPERATION_MODE_BROWSE:
2725 fs_model = impl->browse_files_model;
2726 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2727 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
2730 case OPERATION_MODE_SEARCH:
2731 search_get_valid_child_iter (impl, &child_iter, iter);
2732 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2733 SEARCH_MODEL_COL_PATH, &file_path,
2737 case OPERATION_MODE_RECENT:
2738 recent_get_valid_child_iter (impl, &child_iter, iter);
2739 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2740 RECENT_MODEL_COL_PATH, &file_path,
2745 shortcuts_add_bookmark_from_path (impl, file_path, -1);
2748 /* Adds a bookmark from the currently selected item in the file list */
2750 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2752 GtkTreeSelection *selection;
2754 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2756 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2757 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2759 gtk_tree_selection_selected_foreach (selection,
2760 add_bookmark_foreach_cb,
2764 /* Callback used when the "Add bookmark" button is clicked */
2766 add_bookmark_button_clicked_cb (GtkButton *button,
2767 GtkFileChooserDefault *impl)
2769 bookmarks_add_selected_folder (impl);
2772 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2773 * returns FALSE if no shortcut is selected.
2776 shortcuts_get_selected (GtkFileChooserDefault *impl,
2779 GtkTreeSelection *selection;
2780 GtkTreeIter parent_iter;
2782 if (!impl->browse_shortcuts_tree_view)
2785 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2787 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2790 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2796 /* Removes the selected bookmarks */
2798 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2806 if (!shortcuts_get_selected (impl, &iter))
2809 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2810 SHORTCUTS_COL_DATA, &col_data,
2811 SHORTCUTS_COL_REMOVABLE, &removable,
2817 g_assert (col_data != NULL);
2822 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2823 error_removing_bookmark_dialog (impl, path, error);
2826 /* Callback used when the "Remove bookmark" button is clicked */
2828 remove_bookmark_button_clicked_cb (GtkButton *button,
2829 GtkFileChooserDefault *impl)
2831 remove_selected_bookmarks (impl);
2834 struct selection_check_closure {
2835 GtkFileChooserDefault *impl;
2838 gboolean all_folders;
2841 /* Used from gtk_tree_selection_selected_foreach() */
2843 selection_check_foreach_cb (GtkTreeModel *model,
2848 struct selection_check_closure *closure;
2849 GtkTreeIter child_iter;
2850 const GtkFileInfo *info;
2854 closure->num_selected++;
2856 switch (closure->impl->operation_mode)
2858 case OPERATION_MODE_BROWSE:
2859 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2860 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2861 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2864 case OPERATION_MODE_SEARCH:
2865 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2866 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2867 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2871 case OPERATION_MODE_RECENT:
2872 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2873 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2874 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2879 closure->all_folders = closure->all_folders && is_folder;
2880 closure->all_files = closure->all_files && !is_folder;
2883 /* Checks whether the selected items in the file list are all files or all folders */
2885 selection_check (GtkFileChooserDefault *impl,
2887 gboolean *all_files,
2888 gboolean *all_folders)
2890 struct selection_check_closure closure;
2891 GtkTreeSelection *selection;
2893 closure.impl = impl;
2894 closure.num_selected = 0;
2895 closure.all_files = TRUE;
2896 closure.all_folders = TRUE;
2898 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2899 gtk_tree_selection_selected_foreach (selection,
2900 selection_check_foreach_cb,
2903 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2906 *num_selected = closure.num_selected;
2909 *all_files = closure.all_files;
2912 *all_folders = closure.all_folders;
2915 struct get_selected_path_closure {
2916 GtkFileChooserDefault *impl;
2917 const GtkFilePath *path;
2921 get_selected_path_foreach_cb (GtkTreeModel *model,
2926 struct get_selected_path_closure *closure;
2927 GtkTreeIter child_iter;
2931 switch (closure->impl->operation_mode)
2933 case OPERATION_MODE_BROWSE:
2934 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2935 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2938 case OPERATION_MODE_SEARCH:
2939 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2940 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2941 SEARCH_MODEL_COL_PATH, &closure->path,
2945 case OPERATION_MODE_RECENT:
2946 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2947 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2948 RECENT_MODEL_COL_PATH, &closure->path,
2954 /* Returns a selected path from the file list */
2955 static const GtkFilePath *
2956 get_selected_path (GtkFileChooserDefault *impl)
2958 struct get_selected_path_closure closure;
2959 GtkTreeSelection *selection;
2961 closure.impl = impl;
2962 closure.path = NULL;
2964 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2965 gtk_tree_selection_selected_foreach (selection,
2966 get_selected_path_foreach_cb,
2969 return closure.path;
2973 GtkFileChooserDefault *impl;
2975 } UpdateTooltipData;
2978 update_tooltip (GtkTreeModel *model,
2983 UpdateTooltipData *udata = data;
2984 GtkTreeIter child_iter;
2985 const GtkFileInfo *info;
2987 if (udata->tip == NULL)
2989 const gchar *display_name;
2991 switch (udata->impl->operation_mode)
2993 case OPERATION_MODE_BROWSE:
2994 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2997 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2998 display_name = gtk_file_info_get_display_name (info);
3001 case OPERATION_MODE_SEARCH:
3002 search_get_valid_child_iter (udata->impl, &child_iter, iter);
3003 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
3004 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
3008 case OPERATION_MODE_RECENT:
3009 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
3010 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
3011 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
3016 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
3022 /* Sensitize the "add bookmark" button if all the selected items are folders, or
3023 * if there are no selected items *and* the current folder is not in the
3024 * bookmarks list. De-sensitize the button otherwise.
3027 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
3030 gboolean all_folders;
3034 selection_check (impl, &num_selected, NULL, &all_folders);
3036 if (num_selected == 0)
3037 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
3038 else if (num_selected == 1)
3040 const GtkFilePath *path;
3042 path = get_selected_path (impl);
3043 active = all_folders && (shortcut_find_position (impl, path) == -1);
3046 active = all_folders;
3048 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
3050 if (impl->browse_files_popup_menu_add_shortcut_item)
3051 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
3052 (num_selected == 0) ? FALSE : active);
3056 if (num_selected == 0)
3057 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
3058 else if (num_selected > 1)
3059 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
3062 GtkTreeSelection *selection;
3063 UpdateTooltipData data;
3065 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3068 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
3072 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
3077 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
3078 * bookmark row is selected in the shortcuts tree.
3081 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
3084 gboolean removable = FALSE;
3087 if (shortcuts_get_selected (impl, &iter))
3088 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3089 SHORTCUTS_COL_REMOVABLE, &removable,
3090 SHORTCUTS_COL_NAME, &name,
3093 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
3099 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3100 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3108 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3111 gboolean removable = FALSE;
3113 if (impl->browse_shortcuts_popup_menu == NULL)
3116 if (shortcuts_get_selected (impl, &iter))
3117 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3118 SHORTCUTS_COL_REMOVABLE, &removable,
3121 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3122 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3125 /* GtkWidget::drag-begin handler for the shortcuts list. */
3127 shortcuts_drag_begin_cb (GtkWidget *widget,
3128 GdkDragContext *context,
3129 GtkFileChooserDefault *impl)
3132 impl->shortcuts_drag_context = g_object_ref (context);
3137 /* Removes the idle handler for outside drags */
3139 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3141 if (!impl->shortcuts_drag_outside_idle)
3144 g_source_destroy (impl->shortcuts_drag_outside_idle);
3145 impl->shortcuts_drag_outside_idle = NULL;
3149 /* GtkWidget::drag-end handler for the shortcuts list. */
3151 shortcuts_drag_end_cb (GtkWidget *widget,
3152 GdkDragContext *context,
3153 GtkFileChooserDefault *impl)
3156 g_object_unref (impl->shortcuts_drag_context);
3158 shortcuts_cancel_drag_outside_idle (impl);
3160 if (!impl->shortcuts_drag_outside)
3163 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3165 impl->shortcuts_drag_outside = FALSE;
3169 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3171 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3172 GdkDragContext *context,
3173 GtkFileChooserDefault *impl)
3175 g_signal_stop_emission_by_name (widget, "drag_data_delete");
3179 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3183 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3186 GtkTreeView *tree_view;
3189 GdkPixmap *row_pixmap;
3194 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3196 /* Find the selected path and get its drag pixmap */
3198 if (!shortcuts_get_selected (impl, &iter))
3199 g_assert_not_reached ();
3201 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3203 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3204 gtk_tree_path_free (path);
3213 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3219 GdkPixmap *composite;
3220 int row_pixmap_width, row_pixmap_height;
3221 int pixbuf_width, pixbuf_height;
3222 int composite_width, composite_height;
3223 int pixbuf_x, pixbuf_y;
3224 GdkGC *gc, *mask_gc;
3226 GdkBitmap *pixbuf_mask;
3228 /* Create pixmap and mask for composite image */
3230 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3231 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3232 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3234 composite_width = MAX (row_pixmap_width, pixbuf_width);
3235 composite_height = MAX (row_pixmap_height, pixbuf_height);
3237 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3239 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3242 pixbuf_x = composite_width - pixbuf_width;
3244 pixbuf_y = (composite_height - pixbuf_height) / 2;
3246 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3247 gc = gdk_gc_new (composite);
3249 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3250 mask_gc = gdk_gc_new (mask);
3252 gdk_gc_set_foreground (mask_gc, &color);
3253 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3256 color.green = 0xffff;
3257 color.blue = 0xffff;
3258 gdk_gc_set_rgb_fg_color (gc, &color);
3259 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3261 /* Composite the row pixmap and the pixbuf */
3263 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3265 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3266 NULL, &pixbuf_mask, 128);
3267 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3270 pixbuf_width, pixbuf_height);
3271 g_object_unref (pixbuf_mask);
3273 gdk_draw_drawable (composite, gc, row_pixmap,
3276 row_pixmap_width, row_pixmap_height);
3278 gdk_gc_set_foreground (mask_gc, &color);
3279 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3281 gdk_draw_pixbuf (composite, gc, pixbuf,
3284 pixbuf_width, pixbuf_height,
3288 g_object_unref (pixbuf);
3289 g_object_unref (row_pixmap);
3291 row_pixmap = composite;
3295 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3297 gtk_tree_view_get_path_at_pos (tree_view,
3298 tree_view->priv->press_start_x,
3299 tree_view->priv->press_start_y,
3305 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3306 gdk_drawable_get_colormap (row_pixmap),
3309 tree_view->priv->press_start_x + 1,
3310 row_pixmap_y + cell_y + 1);
3312 g_object_unref (row_pixmap);
3314 g_object_unref (mask);
3317 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3318 * handler so that we can tell apart the drag_leave event that comes right
3319 * before a drag_drop, from a normal drag_leave. We don't want to set the
3320 * cursor nor the flag in the latter case.
3323 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3325 GDK_THREADS_ENTER ();
3327 shortcuts_drag_set_delete_cursor (impl, TRUE);
3328 impl->shortcuts_drag_outside = TRUE;
3330 shortcuts_cancel_drag_outside_idle (impl);
3332 GDK_THREADS_LEAVE ();
3338 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3342 shortcuts_drag_leave_cb (GtkWidget *widget,
3343 GdkDragContext *context,
3345 GtkFileChooserDefault *impl)
3348 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3350 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3351 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3352 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3354 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3358 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3360 GTK_TREE_VIEW_DROP_BEFORE);
3362 g_signal_stop_emission_by_name (widget, "drag_leave");
3365 /* Computes the appropriate row and position for dropping */
3367 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3371 GtkTreeViewDropPosition *pos)
3373 GtkTreeView *tree_view;
3374 GtkTreeViewColumn *column;
3378 int bookmarks_index;
3380 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3382 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3384 if (!gtk_tree_view_get_path_at_pos (tree_view,
3386 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3392 row = bookmarks_index + impl->num_bookmarks - 1;
3393 *path = gtk_tree_path_new_from_indices (row, -1);
3394 *pos = GTK_TREE_VIEW_DROP_AFTER;
3398 row = *gtk_tree_path_get_indices (*path);
3399 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3400 gtk_tree_path_free (*path);
3402 if (row < bookmarks_index)
3404 row = bookmarks_index;
3405 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3407 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3409 row = bookmarks_index + impl->num_bookmarks - 1;
3410 *pos = GTK_TREE_VIEW_DROP_AFTER;
3414 if (cell_y < cell.height / 2)
3415 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3417 *pos = GTK_TREE_VIEW_DROP_AFTER;
3420 *path = gtk_tree_path_new_from_indices (row, -1);
3423 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3424 * implement the destination side of DnD by hand, due to limitations in
3425 * GtkTreeView's DnD API.
3428 shortcuts_drag_motion_cb (GtkWidget *widget,
3429 GdkDragContext *context,
3433 GtkFileChooserDefault *impl)
3436 GtkTreeViewDropPosition pos;
3437 GdkDragAction action;
3440 if (gtk_drag_get_source_widget (context) == widget)
3442 shortcuts_cancel_drag_outside_idle (impl);
3444 if (impl->shortcuts_drag_outside)
3446 shortcuts_drag_set_delete_cursor (impl, FALSE);
3447 impl->shortcuts_drag_outside = FALSE;
3452 if (context->suggested_action == GDK_ACTION_COPY ||
3453 (context->actions & GDK_ACTION_COPY) != 0)
3454 action = GDK_ACTION_COPY;
3455 else if (context->suggested_action == GDK_ACTION_MOVE ||
3456 (context->actions & GDK_ACTION_MOVE) != 0)
3457 action = GDK_ACTION_MOVE;
3464 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3465 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3466 gtk_tree_path_free (path);
3470 g_signal_stop_emission_by_name (widget, "drag_motion");
3474 gdk_drag_status (context, action, time_);
3481 /* GtkWidget::drag-drop handler for the shortcuts list. */
3483 shortcuts_drag_drop_cb (GtkWidget *widget,
3484 GdkDragContext *context,
3488 GtkFileChooserDefault *impl)
3491 shortcuts_cancel_drag_outside_idle (impl);
3494 g_signal_stop_emission_by_name (widget, "drag_drop");
3498 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3500 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3507 uris = g_uri_list_extract_uris (data);
3509 for (i = 0; uris[i]; i++)
3515 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3519 if (shortcuts_add_bookmark_from_path (impl, path, position))
3522 gtk_file_path_free (path);
3526 GError *error = NULL;
3528 g_set_error (&error,
3529 GTK_FILE_CHOOSER_ERROR,
3530 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3531 _("Could not add a bookmark for '%s' "
3532 "because it is an invalid path name."),
3534 error_adding_bookmark_dialog (impl, path, error);
3541 /* Reorders the selected bookmark to the specified position */
3543 shortcuts_reorder (GtkFileChooserDefault *impl,
3548 ShortcutType shortcut_type;
3551 int bookmarks_index;
3552 const GtkFilePath *file_path;
3553 GtkFilePath *file_path_copy;
3557 /* Get the selected path */
3559 if (!shortcuts_get_selected (impl, &iter))
3560 g_assert_not_reached ();
3562 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3563 old_position = *gtk_tree_path_get_indices (path);
3564 gtk_tree_path_free (path);
3566 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3567 old_position -= bookmarks_index;
3568 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3570 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3571 SHORTCUTS_COL_NAME, &name,
3572 SHORTCUTS_COL_DATA, &col_data,
3573 SHORTCUTS_COL_TYPE, &shortcut_type,
3575 g_assert (col_data != NULL);
3576 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
3578 file_path = col_data;
3579 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
3581 /* Remove the path from the old position and insert it in the new one */
3583 if (new_position > old_position)
3586 if (old_position == new_position)
3590 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
3592 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
3593 gtk_file_system_set_bookmark_label (impl->file_system, file_path_copy, name);
3596 error_adding_bookmark_dialog (impl, file_path_copy, error);
3600 gtk_file_path_free (file_path_copy);
3603 /* Callback used when we get the drag data for the bookmarks list. We add the
3604 * received URIs as bookmarks if they are folders.
3607 shortcuts_drag_data_received_cb (GtkWidget *widget,
3608 GdkDragContext *context,
3611 GtkSelectionData *selection_data,
3616 GtkFileChooserDefault *impl;
3617 GtkTreePath *tree_path;
3618 GtkTreeViewDropPosition tree_pos;
3620 int bookmarks_index;
3622 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3624 /* Compute position */
3626 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3628 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3629 position = *gtk_tree_path_get_indices (tree_path);
3630 gtk_tree_path_free (tree_path);
3632 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3635 g_assert (position >= bookmarks_index);
3636 position -= bookmarks_index;
3638 if (selection_data->target == gdk_atom_intern_static_string ("text/uri-list"))
3639 shortcuts_drop_uris (impl, (const char *) selection_data->data, position);
3640 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3641 shortcuts_reorder (impl, position);
3643 g_signal_stop_emission_by_name (widget, "drag_data_received");
3646 /* Callback used when the selection in the shortcuts tree changes */
3648 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3649 GtkFileChooserDefault *impl)
3652 GtkTreeIter child_iter;
3654 bookmarks_check_remove_sensitivity (impl);
3655 shortcuts_check_popup_sensitivity (impl);
3657 if (impl->changing_folder)
3660 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3662 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3665 shortcuts_activate_iter (impl, &child_iter);
3670 shortcuts_row_separator_func (GtkTreeModel *model,
3674 ShortcutType shortcut_type;
3676 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3678 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3681 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3682 * keypresses before the TreeView gets them.
3685 tree_view_keybinding_cb (GtkWidget *tree_view,
3687 GtkFileChooserDefault *impl)
3689 if ((event->keyval == GDK_slash
3690 || event->keyval == GDK_KP_Divide
3692 || event->keyval == GDK_asciitilde
3694 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3696 location_popup_handler (impl, event->string);
3703 /* Callback used when the file list's popup menu is detached */
3705 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3708 GtkFileChooserDefault *impl;
3710 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3711 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3713 impl->browse_shortcuts_popup_menu = NULL;
3714 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3715 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3719 remove_shortcut_cb (GtkMenuItem *item,
3720 GtkFileChooserDefault *impl)
3722 remove_selected_bookmarks (impl);
3725 /* Rename the selected bookmark */
3727 rename_selected_bookmark (GtkFileChooserDefault *impl)
3731 GtkTreeViewColumn *column;
3732 GtkCellRenderer *cell;
3735 if (shortcuts_get_selected (impl, &iter))
3737 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3738 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3739 renderers = gtk_tree_view_column_get_cell_renderers (column);
3740 cell = g_list_nth_data (renderers, 1);
3741 g_list_free (renderers);
3742 g_object_set (cell, "editable", TRUE, NULL);
3743 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3744 path, column, cell, TRUE);
3745 gtk_tree_path_free (path);
3750 rename_shortcut_cb (GtkMenuItem *item,
3751 GtkFileChooserDefault *impl)
3753 rename_selected_bookmark (impl);
3756 /* Constructs the popup menu for the file list if needed */
3758 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3762 if (impl->browse_shortcuts_popup_menu)
3765 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3766 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3767 impl->browse_shortcuts_tree_view,
3768 shortcuts_popup_menu_detach_cb);
3770 item = gtk_image_menu_item_new_with_label (_("Remove"));
3771 impl->browse_shortcuts_popup_menu_remove_item = item;
3772 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3773 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3774 g_signal_connect (item, "activate",
3775 G_CALLBACK (remove_shortcut_cb), impl);
3776 gtk_widget_show (item);
3777 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3779 item = gtk_menu_item_new_with_label (_("Rename..."));
3780 impl->browse_shortcuts_popup_menu_rename_item = item;
3781 g_signal_connect (item, "activate",
3782 G_CALLBACK (rename_shortcut_cb), impl);
3783 gtk_widget_show (item);
3784 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3786 shortcuts_check_popup_sensitivity (impl);
3790 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3792 shortcuts_build_popup_menu (impl);
3796 popup_position_func (GtkMenu *menu,
3800 gpointer user_data);
3803 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3804 GdkEventButton *event)
3806 shortcuts_update_popup_menu (impl);
3808 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3809 NULL, NULL, NULL, NULL,
3810 event->button, event->time);
3813 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3815 popup_position_func, impl->browse_shortcuts_tree_view,
3816 0, GDK_CURRENT_TIME);
3817 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3822 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3824 shortcuts_popup_menu_cb (GtkWidget *widget,
3825 GtkFileChooserDefault *impl)
3827 shortcuts_popup_menu (impl, NULL);
3831 /* Callback used when a button is pressed on the shortcuts list.
3832 * We trap button 3 to bring up a popup menu.
3835 shortcuts_button_press_event_cb (GtkWidget *widget,
3836 GdkEventButton *event,
3837 GtkFileChooserDefault *impl)
3839 static gboolean in_press = FALSE;
3845 if (event->button != 3)
3849 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3855 shortcuts_popup_menu (impl, event);
3860 shortcuts_edited (GtkCellRenderer *cell,
3863 GtkFileChooserDefault *impl)
3867 GtkFilePath *shortcut;
3869 g_object_set (cell, "editable", FALSE, NULL);
3871 path = gtk_tree_path_new_from_string (path_string);
3872 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3873 g_assert_not_reached ();
3875 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3876 SHORTCUTS_COL_DATA, &shortcut,
3878 gtk_tree_path_free (path);
3880 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3884 shortcuts_editing_canceled (GtkCellRenderer *cell,
3885 GtkFileChooserDefault *impl)
3887 g_object_set (cell, "editable", FALSE, NULL);
3890 /* Creates the widgets for the shortcuts and bookmarks tree */
3892 shortcuts_list_create (GtkFileChooserDefault *impl)
3895 GtkTreeSelection *selection;
3896 GtkTreeViewColumn *column;
3897 GtkCellRenderer *renderer;
3899 /* Scrolled window */
3901 swin = gtk_scrolled_window_new (NULL, NULL);
3902 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3903 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3904 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3906 gtk_widget_show (swin);
3910 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3911 #ifdef PROFILE_FILE_CHOOSER
3912 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3914 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3915 G_CALLBACK (tree_view_keybinding_cb), impl);
3916 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3917 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3918 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3919 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3920 /* Accessible object name for the file chooser's shortcuts pane */
3921 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3923 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3925 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3927 shortcuts_source_targets,
3928 num_shortcuts_source_targets,
3931 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3932 GTK_DEST_DEFAULT_ALL,
3933 shortcuts_dest_targets,
3934 num_shortcuts_dest_targets,
3935 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3937 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3938 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3939 gtk_tree_selection_set_select_function (selection,
3940 shortcuts_select_func,
3943 g_signal_connect (selection, "changed",
3944 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3946 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3947 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3949 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3950 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3951 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3952 G_CALLBACK (shortcuts_drag_end_cb), impl);
3953 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3954 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3956 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3957 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3958 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3959 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3960 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3961 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3962 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3963 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3965 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3966 gtk_widget_show (impl->browse_shortcuts_tree_view);
3970 column = gtk_tree_view_column_new ();
3971 /* Column header for the file chooser's shortcuts pane */
3972 gtk_tree_view_column_set_title (column, _("_Places"));
3974 renderer = gtk_cell_renderer_pixbuf_new ();
3975 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3976 gtk_tree_view_column_set_attributes (column, renderer,
3977 "pixbuf", SHORTCUTS_COL_PIXBUF,
3978 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3981 renderer = gtk_cell_renderer_text_new ();
3982 g_signal_connect (renderer, "edited",
3983 G_CALLBACK (shortcuts_edited), impl);
3984 g_signal_connect (renderer, "editing-canceled",
3985 G_CALLBACK (shortcuts_editing_canceled), impl);
3986 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3987 gtk_tree_view_column_set_attributes (column, renderer,
3988 "text", SHORTCUTS_COL_NAME,
3991 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3992 shortcuts_row_separator_func,
3995 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
4000 /* Creates the widgets for the shortcuts/bookmarks pane */
4002 shortcuts_pane_create (GtkFileChooserDefault *impl,
4003 GtkSizeGroup *size_group)
4009 vbox = gtk_vbox_new (FALSE, 6);
4010 gtk_widget_show (vbox);
4012 /* Shortcuts tree */
4014 widget = shortcuts_list_create (impl);
4015 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
4017 /* Box for buttons */
4019 hbox = gtk_hbox_new (TRUE, 6);
4020 gtk_size_group_add_widget (size_group, hbox);
4021 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4022 gtk_widget_show (hbox);
4024 /* Add bookmark button */
4026 impl->browse_shortcuts_add_button = button_new (impl,
4031 G_CALLBACK (add_bookmark_button_clicked_cb));
4032 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
4033 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
4034 _("Add the selected folder to the Bookmarks"));
4036 /* Remove bookmark button */
4038 impl->browse_shortcuts_remove_button = button_new (impl,
4043 G_CALLBACK (remove_bookmark_button_clicked_cb));
4044 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4045 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4046 _("Remove the selected bookmark"));
4051 /* Handles key press events on the file list, so that we can trap Enter to
4052 * activate the default button on our own. Also, checks to see if '/' has been
4053 * pressed. See comment by tree_view_keybinding_cb() for more details.
4056 trap_activate_cb (GtkWidget *widget,
4060 GtkFileChooserDefault *impl;
4063 impl = (GtkFileChooserDefault *) data;
4065 modifiers = gtk_accelerator_get_default_mod_mask ();
4067 if ((event->keyval == GDK_slash
4068 || event->keyval == GDK_KP_Divide
4070 || event->keyval == GDK_asciitilde
4072 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4074 location_popup_handler (impl, event->string);
4078 if ((event->keyval == GDK_Return
4079 || event->keyval == GDK_ISO_Enter
4080 || event->keyval == GDK_KP_Enter
4081 || event->keyval == GDK_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 if (!gtk_file_chooser_default_get_resizable (GTK_FILE_CHOOSER_EMBED (impl)))
6035 /* The dialog is not resizable, we shouldn't
6036 * trust in the size it has in this stage
6041 impl->default_width = allocation->width;
6042 impl->default_height = allocation->height;
6044 if (impl->preview_widget_active &&
6045 impl->preview_widget &&
6046 GTK_WIDGET_DRAWABLE (impl->preview_widget))
6047 impl->default_width -= impl->preview_widget->allocation.width + PREVIEW_HBOX_SPACING;
6049 if (impl->extra_widget &&
6050 GTK_WIDGET_DRAWABLE (impl->extra_widget))
6051 impl->default_height -= GTK_BOX (widget)->spacing + impl->extra_widget->allocation.height;
6055 get_is_file_filtered (GtkFileChooserDefault *impl,
6056 const GtkFilePath *path,
6057 GtkFileInfo *file_info)
6059 GtkFileFilterInfo filter_info;
6060 GtkFileFilterFlags needed;
6063 if (!impl->current_filter)
6066 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
6068 needed = gtk_file_filter_get_needed (impl->current_filter);
6070 filter_info.display_name = gtk_file_info_get_display_name (file_info);
6071 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
6073 if (needed & GTK_FILE_FILTER_FILENAME)
6075 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
6076 if (filter_info.filename)
6077 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
6080 filter_info.filename = NULL;
6082 if (needed & GTK_FILE_FILTER_URI)
6084 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
6085 if (filter_info.uri)
6086 filter_info.contains |= GTK_FILE_FILTER_URI;
6089 filter_info.uri = NULL;
6091 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
6093 if (filter_info.filename)
6094 g_free ((gchar *)filter_info.filename);
6095 if (filter_info.uri)
6096 g_free ((gchar *)filter_info.uri);
6102 settings_load (GtkFileChooserDefault *impl)
6104 GtkFileChooserSettings *settings;
6105 LocationMode location_mode;
6106 gboolean show_hidden;
6107 gboolean expand_folders;
6109 settings = _gtk_file_chooser_settings_new ();
6111 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
6112 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
6113 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
6115 g_object_unref (settings);
6117 location_mode_set (impl, location_mode, TRUE);
6118 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6119 impl->expand_folders = expand_folders;
6120 if (impl->save_expander)
6121 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
6125 settings_save (GtkFileChooserDefault *impl)
6127 GtkFileChooserSettings *settings;
6129 settings = _gtk_file_chooser_settings_new ();
6131 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6132 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6133 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6136 _gtk_file_chooser_settings_save (settings, NULL);
6138 g_object_unref (settings);
6141 /* GtkWidget::map method */
6143 gtk_file_chooser_default_map (GtkWidget *widget)
6145 GtkFileChooserDefault *impl;
6146 char *current_working_dir;
6148 profile_start ("start", NULL);
6150 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6152 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6154 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6156 switch (impl->reload_state)
6159 /* The user didn't explicitly give us a folder to
6160 * display, so we'll use the cwd
6162 current_working_dir = g_get_current_dir ();
6163 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6164 current_working_dir);
6165 g_free (current_working_dir);
6168 case RELOAD_HAS_FOLDER:
6169 /* Nothing; we are already loading or loaded, so we
6170 * don't need to reload
6174 case RELOAD_WAS_UNMAPPED:
6175 /* Just reload the current folder; else continue
6178 if (impl->current_folder)
6180 pending_select_paths_store_selection (impl);
6181 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6186 g_assert_not_reached ();
6190 bookmarks_changed_cb (impl->file_system, impl);
6192 settings_load (impl);
6194 profile_end ("end", NULL);
6197 /* GtkWidget::unmap method */
6199 gtk_file_chooser_default_unmap (GtkWidget *widget)
6201 GtkFileChooserDefault *impl;
6203 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6205 settings_save (impl);
6207 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6209 impl->reload_state = RELOAD_WAS_UNMAPPED;
6213 list_model_filter_func (GtkFileSystemModel *model,
6215 const GtkFileInfo *file_info,
6218 GtkFileChooserDefault *impl = user_data;
6220 if (!impl->current_filter)
6223 if (gtk_file_info_get_is_folder (file_info))
6226 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
6230 install_list_model_filter (GtkFileChooserDefault *impl)
6232 GtkFileSystemModelFilter filter;
6235 g_assert (impl->browse_files_model != NULL);
6237 if (impl->current_filter)
6239 filter = list_model_filter_func;
6248 _gtk_file_system_model_set_filter (impl->browse_files_model,
6253 #define COMPARE_DIRECTORIES \
6254 GtkFileChooserDefault *impl = user_data; \
6255 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6256 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6257 gboolean dir_a, dir_b; \
6260 dir_a = gtk_file_info_get_is_folder (info_a); \
6262 return impl->list_sort_ascending ? -1 : 1; \
6265 dir_b = gtk_file_info_get_is_folder (info_b); \
6267 return impl->list_sort_ascending ? 1 : -1; \
6269 if (dir_a != dir_b) \
6270 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6272 /* Sort callback for the filename column */
6274 name_sort_func (GtkTreeModel *model,
6279 COMPARE_DIRECTORIES;
6281 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
6284 /* Sort callback for the size column */
6286 size_sort_func (GtkTreeModel *model,
6291 COMPARE_DIRECTORIES;
6294 gint64 size_a = gtk_file_info_get_size (info_a);
6295 gint64 size_b = gtk_file_info_get_size (info_b);
6297 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6301 /* Sort callback for the mtime column */
6303 mtime_sort_func (GtkTreeModel *model,
6308 COMPARE_DIRECTORIES;
6311 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
6312 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
6314 return ta > tb ? -1 : (ta == tb ? 0 : 1);
6318 /* Callback used when the sort column changes. We cache the sort order for use
6319 * in name_sort_func().
6322 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6323 GtkFileChooserDefault *impl)
6325 GtkSortType sort_type;
6327 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6328 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6332 set_busy_cursor (GtkFileChooserDefault *impl,
6335 GtkWindow *toplevel;
6336 GdkDisplay *display;
6339 toplevel = get_toplevel (GTK_WIDGET (impl));
6340 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6343 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6346 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6350 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6351 gdk_display_flush (display);
6354 gdk_cursor_unref (cursor);
6357 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6359 load_set_model (GtkFileChooserDefault *impl)
6361 profile_start ("start", NULL);
6363 g_assert (impl->browse_files_model != NULL);
6364 g_assert (impl->sort_model == NULL);
6366 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6367 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6368 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6369 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6370 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6371 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6372 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6373 impl->list_sort_ascending = TRUE;
6374 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6376 g_signal_connect (impl->sort_model, "sort_column_changed",
6377 G_CALLBACK (list_sort_column_changed_cb), impl);
6379 profile_msg (" gtk_tree_view_set_model start", NULL);
6380 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6381 GTK_TREE_MODEL (impl->sort_model));
6382 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6383 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6384 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6385 profile_msg (" gtk_tree_view_set_model end", NULL);
6387 profile_end ("end", NULL);
6390 /* Timeout callback used when the loading timer expires */
6392 load_timeout_cb (gpointer data)
6394 GtkFileChooserDefault *impl;
6396 profile_start ("start", NULL);
6398 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6399 g_assert (impl->load_state == LOAD_PRELOAD);
6400 g_assert (impl->load_timeout_id != 0);
6401 g_assert (impl->browse_files_model != NULL);
6403 impl->load_timeout_id = 0;
6404 impl->load_state = LOAD_LOADING;
6406 load_set_model (impl);
6408 profile_end ("end", NULL);
6413 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6415 load_setup_timer (GtkFileChooserDefault *impl)
6417 g_assert (impl->load_timeout_id == 0);
6418 g_assert (impl->load_state != LOAD_PRELOAD);
6420 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6421 impl->load_state = LOAD_PRELOAD;
6424 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6426 load_remove_timer (GtkFileChooserDefault *impl)
6428 if (impl->load_timeout_id != 0)
6430 g_assert (impl->load_state == LOAD_PRELOAD);
6432 g_source_remove (impl->load_timeout_id);
6433 impl->load_timeout_id = 0;
6434 impl->load_state = LOAD_EMPTY;
6437 g_assert (impl->load_state == LOAD_EMPTY ||
6438 impl->load_state == LOAD_LOADING ||
6439 impl->load_state == LOAD_FINISHED);
6442 /* Selects the first row in the file list */
6444 browse_files_select_first_row (GtkFileChooserDefault *impl)
6448 if (!impl->sort_model)
6451 path = gtk_tree_path_new_from_indices (0, -1);
6452 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6453 gtk_tree_path_free (path);
6456 struct center_selected_row_closure {
6457 GtkFileChooserDefault *impl;
6458 gboolean already_centered;
6461 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6462 * selected row in the tree view.
6465 center_selected_row_foreach_cb (GtkTreeModel *model,
6470 struct center_selected_row_closure *closure;
6473 if (closure->already_centered)
6476 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6477 closure->already_centered = TRUE;
6480 /* Centers the selected row in the tree view */
6482 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6484 struct center_selected_row_closure closure;
6485 GtkTreeSelection *selection;
6487 closure.impl = impl;
6488 closure.already_centered = FALSE;
6490 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6491 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6494 struct ShowAndSelectPathsData
6496 GtkFileChooserDefault *impl;
6501 show_and_select_paths_finished_loading (GtkFileFolder *folder,
6504 gboolean have_hidden;
6505 gboolean have_filtered;
6507 struct ShowAndSelectPathsData *data = user_data;
6509 have_hidden = FALSE;
6510 have_filtered = FALSE;
6512 for (l = data->paths; l; l = l->next)
6514 const GtkFilePath *path;
6520 info = gtk_file_folder_get_info (folder, path, NULL);
6524 have_hidden = gtk_file_info_get_is_hidden (info);
6527 have_filtered = !gtk_file_info_get_is_folder (info) && get_is_file_filtered (data->impl, path, info);
6529 gtk_file_info_free (info);
6531 if (have_hidden && have_filtered)
6532 break; /* we now have all the information we need */
6536 g_signal_handlers_disconnect_by_func (folder,
6537 show_and_select_paths_finished_loading,
6540 g_object_unref (folder);
6543 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6546 set_current_filter (data->impl, NULL);
6548 for (l = data->paths; l; l = l->next)
6550 const GtkFilePath *path;
6553 _gtk_file_system_model_path_do (data->impl->browse_files_model, path,
6554 select_func, data->impl);
6557 browse_files_center_selected_row (data->impl);
6559 g_object_unref (data->impl);
6560 gtk_file_paths_free (data->paths);
6565 show_and_select_paths_get_folder_cb (GtkFileSystemHandle *handle,
6566 GtkFileFolder *folder,
6567 const GError *error,
6570 gboolean cancelled = handle->cancelled;
6571 struct ShowAndSelectPathsData *data = user_data;
6573 if (data->impl->show_and_select_paths_handle != handle)
6576 data->impl->show_and_select_paths_handle = NULL;
6578 if (cancelled || error)
6581 g_object_unref (handle);
6583 if (gtk_file_folder_is_finished_loading (folder))
6584 show_and_select_paths_finished_loading (folder, user_data);
6586 g_signal_connect (folder, "finished-loading",
6587 G_CALLBACK (show_and_select_paths_finished_loading),
6593 g_object_unref (data->impl);
6594 gtk_file_paths_free (data->paths);
6597 g_object_unref (handle);
6601 show_and_select_paths (GtkFileChooserDefault *impl,
6602 const GtkFilePath *parent_path,
6606 struct ShowAndSelectPathsData *info;
6608 profile_start ("start", NULL);
6612 profile_end ("end", NULL);
6616 info = g_new (struct ShowAndSelectPathsData, 1);
6617 info->impl = g_object_ref (impl);
6618 info->paths = gtk_file_paths_copy (paths);
6620 if (impl->show_and_select_paths_handle)
6621 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
6623 impl->show_and_select_paths_handle =
6624 gtk_file_system_get_folder (impl->file_system, parent_path,
6625 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN,
6626 show_and_select_paths_get_folder_cb, info);
6628 profile_end ("end", NULL);
6632 /* Processes the pending operation when a folder is finished loading */
6634 pending_select_paths_process (GtkFileChooserDefault *impl)
6636 g_assert (impl->load_state == LOAD_FINISHED);
6637 g_assert (impl->browse_files_model != NULL);
6638 g_assert (impl->sort_model != NULL);
6640 if (impl->pending_select_paths)
6643 show_and_select_paths (impl, impl->current_folder, impl->pending_select_paths, NULL);
6644 pending_select_paths_free (impl);
6645 browse_files_center_selected_row (impl);
6649 /* We only select the first row if the chooser is actually mapped ---
6650 * selecting the first row is to help the user when he is interacting with
6651 * the chooser, but sometimes a chooser works not on behalf of the user,
6652 * but rather on behalf of something else like GtkFileChooserButton. In
6653 * that case, the chooser's selection should be what the caller expects,
6654 * as the user can't see that something else got selected. See bug #165264.
6656 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6657 browse_files_select_first_row (impl);
6660 g_assert (impl->pending_select_paths == NULL);
6663 /* Callback used when the file system model finishes loading */
6665 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6666 GtkFileChooserDefault *impl)
6668 profile_start ("start", NULL);
6670 if (impl->load_state == LOAD_PRELOAD)
6672 load_remove_timer (impl);
6673 load_set_model (impl);
6675 else if (impl->load_state == LOAD_LOADING)
6681 /* We can't g_assert_not_reached(), as something other than us may have
6682 * initiated a folder reload. See #165556.
6684 profile_end ("end", NULL);
6688 g_assert (impl->load_timeout_id == 0);
6690 impl->load_state = LOAD_FINISHED;
6692 pending_select_paths_process (impl);
6693 set_busy_cursor (impl, FALSE);
6694 #ifdef PROFILE_FILE_CHOOSER
6695 access ("MARK: *** FINISHED LOADING", F_OK);
6698 profile_end ("end", NULL);
6702 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6704 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6706 if (impl->browse_files_model)
6708 g_object_unref (impl->browse_files_model);
6709 impl->browse_files_model = NULL;
6712 if (impl->sort_model)
6714 g_object_unref (impl->sort_model);
6715 impl->sort_model = NULL;
6718 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6721 /* Gets rid of the old list model and creates a new one for the current folder */
6723 set_list_model (GtkFileChooserDefault *impl,
6726 g_assert (impl->current_folder != NULL);
6728 profile_start ("start", NULL);
6730 stop_loading_and_clear_list_model (impl);
6732 set_busy_cursor (impl, TRUE);
6733 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6735 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6736 impl->current_folder, 0,
6739 if (!impl->browse_files_model)
6741 set_busy_cursor (impl, FALSE);
6742 profile_end ("end", NULL);
6746 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6748 g_signal_connect (impl->browse_files_model, "finished-loading",
6749 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6751 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6753 install_list_model_filter (impl);
6755 profile_end ("end", NULL);
6760 struct update_chooser_entry_selected_foreach_closure {
6762 GtkTreeIter first_selected_iter;
6766 compare_utf8_filenames (const gchar *a,
6769 gchar *a_folded, *b_folded;
6772 a_folded = g_utf8_strdown (a, -1);
6773 b_folded = g_utf8_strdown (b, -1);
6775 retval = strcmp (a_folded, b_folded);
6784 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6789 struct update_chooser_entry_selected_foreach_closure *closure;
6792 closure->num_selected++;
6794 if (closure->num_selected == 1)
6795 closure->first_selected_iter = *iter;
6799 update_chooser_entry (GtkFileChooserDefault *impl)
6801 GtkTreeSelection *selection;
6802 struct update_chooser_entry_selected_foreach_closure closure;
6803 const char *file_part;
6805 /* no need to update the file chooser's entry if there's no entry */
6806 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6807 impl->operation_mode == OPERATION_MODE_RECENT ||
6808 !impl->location_entry)
6811 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6812 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6813 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6814 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6815 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6818 g_assert (impl->location_entry != NULL);
6820 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6821 closure.num_selected = 0;
6822 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6826 if (closure.num_selected == 0)
6828 goto maybe_clear_entry;
6830 else if (closure.num_selected == 1)
6832 GtkTreeIter child_iter;
6834 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6836 const GtkFileInfo *info;
6837 gboolean change_entry;
6839 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6841 &closure.first_selected_iter);
6843 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6845 /* If the cursor moved to the row of the newly created folder,
6846 * retrieving info will return NULL.
6851 g_free (impl->browse_files_last_selected_name);
6852 impl->browse_files_last_selected_name =
6853 g_strdup (gtk_file_info_get_display_name (info));
6855 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6856 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6857 change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
6859 change_entry = TRUE; /* ... unless we are in one of the folder modes */
6862 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6863 impl->browse_files_last_selected_name);
6870 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6871 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6873 /* Multiple selection, so just clear the entry. */
6875 g_free (impl->browse_files_last_selected_name);
6876 impl->browse_files_last_selected_name = NULL;
6878 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6884 if (impl->browse_files_last_selected_name)
6886 const char *entry_text;
6888 gboolean clear_entry;
6890 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6891 len = strlen (entry_text);
6894 /* The file chooser entry may have appended a "/" to its text. So
6895 * take it out, and compare the result to the old selection.
6897 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6901 tmp = g_strndup (entry_text, len - 1);
6902 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6906 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6909 clear_entry = FALSE;
6912 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6917 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6918 const GtkFilePath *path,
6921 return gtk_file_chooser_default_update_current_folder (chooser, path, FALSE, FALSE, error);
6925 struct UpdateCurrentFolderData
6927 GtkFileChooserDefault *impl;
6929 gboolean keep_trail;
6930 gboolean clear_entry;
6931 GtkFilePath *original_path;
6932 GError *original_error;
6936 update_current_folder_get_info_cb (GtkFileSystemHandle *handle,
6937 const GtkFileInfo *info,
6938 const GError *error,
6941 gboolean cancelled = handle->cancelled;
6942 struct UpdateCurrentFolderData *data = user_data;
6943 GtkFileChooserDefault *impl = data->impl;
6945 if (handle != impl->update_current_folder_handle)
6948 impl->update_current_folder_handle = NULL;
6949 impl->reload_state = RELOAD_EMPTY;
6951 set_busy_cursor (impl, FALSE);
6958 GtkFilePath *parent_path;
6960 if (!data->original_path)
6962 data->original_path = gtk_file_path_copy (data->path);
6963 data->original_error = g_error_copy (error);
6966 /* get parent path and try to change the folder to that */
6967 if (gtk_file_system_get_parent (impl->file_system, data->path, &parent_path, NULL) &&
6968 parent_path != NULL)
6970 gtk_file_path_free (data->path);
6971 data->path = parent_path;
6973 g_object_unref (handle);
6975 /* restart the update current folder operation */
6976 impl->reload_state = RELOAD_HAS_FOLDER;
6978 impl->update_current_folder_handle =
6979 gtk_file_system_get_info (impl->file_system, data->path,
6980 GTK_FILE_INFO_IS_FOLDER,
6981 update_current_folder_get_info_cb,
6984 set_busy_cursor (impl, TRUE);
6990 /* error and bail out */
6991 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6993 gtk_file_path_free (data->original_path);
6999 if (data->original_path)
7001 error_changing_folder_dialog (impl, data->original_path, data->original_error);
7003 gtk_file_path_free (data->original_path);
7006 if (!gtk_file_info_get_is_folder (info))
7009 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), data->path, data->keep_trail, NULL))
7012 if (impl->current_folder != data->path)
7014 if (impl->current_folder)
7015 gtk_file_path_free (impl->current_folder);
7017 impl->current_folder = gtk_file_path_copy (data->path);
7019 impl->reload_state = RELOAD_HAS_FOLDER;
7022 /* Update the widgets that may trigger a folder change themselves. */
7024 if (!impl->changing_folder)
7026 impl->changing_folder = TRUE;
7028 shortcuts_update_current_folder (impl);
7030 impl->changing_folder = FALSE;
7033 /* Set the folder on the save entry */
7035 if (impl->location_entry)
7037 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7038 impl->current_folder);
7040 if (data->clear_entry)
7041 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7044 /* Create a new list model. This is slightly evil; we store the result value
7045 * but perform more actions rather than returning immediately even if it
7046 * generates an error.
7048 set_list_model (impl, NULL);
7050 /* Refresh controls */
7052 shortcuts_find_current_folder (impl);
7054 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7056 check_preview_change (impl);
7057 bookmarks_check_add_sensitivity (impl);
7059 g_signal_emit_by_name (impl, "selection-changed", 0);
7062 gtk_file_path_free (data->path);
7065 g_object_unref (handle);
7069 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7070 const GtkFilePath *path,
7071 gboolean keep_trail,
7072 gboolean clear_entry,
7075 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7076 struct UpdateCurrentFolderData *data;
7078 profile_start ("start", (char *) path);
7080 switch (impl->operation_mode)
7082 case OPERATION_MODE_SEARCH:
7083 search_switch_to_browse_mode (impl);
7085 case OPERATION_MODE_RECENT:
7086 recent_switch_to_browse_mode (impl);
7088 case OPERATION_MODE_BROWSE:
7092 g_assert (path != NULL);
7094 if (impl->local_only &&
7095 !gtk_file_system_path_is_local (impl->file_system, path))
7098 GTK_FILE_CHOOSER_ERROR,
7099 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7100 _("Cannot change to folder because it is not local"));
7102 profile_end ("end - not local", (char *) path);
7106 if (impl->update_current_folder_handle)
7107 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
7109 /* Test validity of path here. */
7110 data = g_new0 (struct UpdateCurrentFolderData, 1);
7112 data->path = gtk_file_path_copy (path);
7113 data->keep_trail = keep_trail;
7114 data->clear_entry = clear_entry;
7116 impl->reload_state = RELOAD_HAS_FOLDER;
7118 impl->update_current_folder_handle =
7119 gtk_file_system_get_info (impl->file_system, path, GTK_FILE_INFO_IS_FOLDER,
7120 update_current_folder_get_info_cb,
7123 set_busy_cursor (impl, TRUE);
7125 profile_end ("end", NULL);
7129 static GtkFilePath *
7130 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7132 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7134 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7135 impl->operation_mode == OPERATION_MODE_RECENT)
7138 if (impl->reload_state == RELOAD_EMPTY)
7140 char *current_working_dir;
7143 /* We are unmapped, or we had an error while loading the last folder. We'll return
7144 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7145 * explicitly calls set_current_folder() on us.
7147 current_working_dir = g_get_current_dir ();
7148 path = gtk_file_system_filename_to_path (impl->file_system, current_working_dir);
7149 g_free (current_working_dir);
7153 return gtk_file_path_copy (impl->current_folder);
7157 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7160 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7162 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7163 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7165 pending_select_paths_free (impl);
7166 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7170 select_func (GtkFileSystemModel *model,
7175 GtkFileChooserDefault *impl = user_data;
7176 GtkTreeSelection *selection;
7177 GtkTreeIter sorted_iter;
7179 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7181 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7182 gtk_tree_selection_select_iter (selection, &sorted_iter);
7186 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
7187 const GtkFilePath *path,
7190 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7191 GtkFilePath *parent_path;
7194 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
7198 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
7200 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7201 impl->operation_mode == OPERATION_MODE_RECENT ||
7202 impl->load_state == LOAD_EMPTY)
7208 g_assert (impl->current_folder != NULL);
7210 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
7213 if (same_path && impl->load_state == LOAD_FINISHED)
7218 paths.data = (gpointer) path;
7221 result = show_and_select_paths (impl, parent_path, &paths, error);
7222 gtk_file_path_free (parent_path);
7226 pending_select_paths_add (impl, path);
7232 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
7233 gtk_file_path_free (parent_path);
7237 gtk_file_path_free (parent_path);
7242 unselect_func (GtkFileSystemModel *model,
7247 GtkFileChooserDefault *impl = user_data;
7248 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7249 GtkTreePath *sorted_path;
7251 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7253 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7255 gtk_tree_path_free (sorted_path);
7259 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
7260 const GtkFilePath *path)
7262 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7264 if (!impl->browse_files_model)
7267 _gtk_file_system_model_path_do (impl->browse_files_model, path,
7268 unselect_func, impl);
7272 maybe_select (GtkTreeModel *model,
7277 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7278 GtkTreeSelection *selection;
7279 const GtkFileInfo *info;
7282 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7284 info = get_list_file_info (impl, iter);
7285 is_folder = gtk_file_info_get_is_folder (info);
7287 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7288 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7289 gtk_tree_selection_select_iter (selection, iter);
7291 gtk_tree_selection_unselect_iter (selection, iter);
7297 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7299 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7301 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7302 impl->operation_mode == OPERATION_MODE_RECENT)
7304 GtkTreeSelection *selection;
7306 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7307 gtk_tree_selection_select_all (selection);
7311 if (impl->select_multiple)
7312 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7313 maybe_select, impl);
7317 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7319 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7320 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7322 gtk_tree_selection_unselect_all (selection);
7323 pending_select_paths_free (impl);
7326 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7328 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7330 * is_empty_ret - whether the file entry is totally empty
7332 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7333 * the path will be "$cwd/foobar")
7336 check_save_entry (GtkFileChooserDefault *impl,
7337 GtkFilePath **path_ret,
7338 gboolean *is_well_formed_ret,
7339 gboolean *is_empty_ret,
7340 gboolean *is_file_part_empty_ret,
7341 gboolean *is_folder)
7343 GtkFileChooserEntry *chooser_entry;
7344 const GtkFilePath *current_folder;
7345 const char *file_part;
7349 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7350 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7351 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7352 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7353 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7355 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7357 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7360 *is_well_formed_ret = TRUE;
7361 *is_empty_ret = TRUE;
7362 *is_file_part_empty_ret = TRUE;
7368 *is_empty_ret = FALSE;
7370 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7371 if (!current_folder)
7374 *is_well_formed_ret = FALSE;
7375 *is_file_part_empty_ret = FALSE;
7381 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7383 if (!file_part || file_part[0] == '\0')
7385 *path_ret = gtk_file_path_copy (current_folder);
7386 *is_well_formed_ret = TRUE;
7387 *is_file_part_empty_ret = TRUE;
7393 *is_file_part_empty_ret = FALSE;
7396 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
7400 error_building_filename_dialog (impl, current_folder, file_part, error);
7402 *is_well_formed_ret = FALSE;
7409 *is_well_formed_ret = TRUE;
7410 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, path);
7413 struct get_paths_closure {
7414 GtkFileChooserDefault *impl;
7416 GtkFilePath *path_from_entry;
7420 get_paths_foreach (GtkTreeModel *model,
7425 struct get_paths_closure *info;
7426 const GtkFilePath *file_path;
7427 GtkFileSystemModel *fs_model;
7428 GtkTreeIter sel_iter;
7431 fs_model = info->impl->browse_files_model;
7432 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7434 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
7436 return; /* We are on the editable row */
7438 if (!info->path_from_entry
7439 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
7440 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
7444 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
7446 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7447 struct get_paths_closure info;
7448 GtkWindow *toplevel;
7449 GtkWidget *current_focus;
7450 gboolean file_list_seen;
7452 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7453 return search_get_selected_paths (impl);
7455 if (impl->operation_mode == OPERATION_MODE_RECENT)
7456 return recent_get_selected_paths (impl);
7460 info.path_from_entry = NULL;
7462 toplevel = get_toplevel (GTK_WIDGET (impl));
7464 current_focus = gtk_window_get_focus (toplevel);
7466 current_focus = NULL;
7468 file_list_seen = FALSE;
7469 if (current_focus == impl->browse_files_tree_view)
7471 GtkTreeSelection *selection;
7475 file_list_seen = TRUE;
7476 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7477 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
7479 /* If there is no selection in the file list, we probably have this situation:
7481 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7482 * 2. He then double-clicked on a folder ("bar") in the file list
7484 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7485 * filename entry to see if that is the case.
7487 if (info.result == NULL && impl->location_entry)
7490 else if (impl->location_entry && current_focus == impl->location_entry)
7492 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7496 check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7501 if (!is_well_formed)
7504 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7506 gtk_file_path_free (info.path_from_entry);
7510 if (info.path_from_entry)
7511 info.result = g_slist_prepend (info.result, info.path_from_entry);
7512 else if (!file_list_seen)
7517 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7519 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7523 /* The focus is on a dialog's action area button or something else */
7524 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7525 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7533 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7534 * fall back to the current directory */
7535 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7536 info.result == NULL)
7538 info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_path (chooser));
7541 return g_slist_reverse (info.result);
7544 static GtkFilePath *
7545 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
7547 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7549 if (impl->preview_path)
7550 return gtk_file_path_copy (impl->preview_path);
7555 static GtkFileSystem *
7556 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7558 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7560 return impl->file_system;
7563 /* Shows or hides the filter widgets */
7565 show_filters (GtkFileChooserDefault *impl,
7569 gtk_widget_show (impl->filter_combo_hbox);
7571 gtk_widget_hide (impl->filter_combo_hbox);
7575 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7576 GtkFileFilter *filter)
7578 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7581 if (g_slist_find (impl->filters, filter))
7583 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7587 g_object_ref_sink (filter);
7588 impl->filters = g_slist_append (impl->filters, filter);
7590 name = gtk_file_filter_get_name (filter);
7592 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7594 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7596 if (!g_slist_find (impl->filters, impl->current_filter))
7597 set_current_filter (impl, filter);
7599 show_filters (impl, TRUE);
7603 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7604 GtkFileFilter *filter)
7606 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7607 GtkTreeModel *model;
7611 filter_index = g_slist_index (impl->filters, filter);
7613 if (filter_index < 0)
7615 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7619 impl->filters = g_slist_remove (impl->filters, filter);
7621 if (filter == impl->current_filter)
7624 set_current_filter (impl, impl->filters->data);
7626 set_current_filter (impl, NULL);
7629 /* Remove row from the combo box */
7630 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7631 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7632 g_assert_not_reached ();
7634 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7636 g_object_unref (filter);
7639 show_filters (impl, FALSE);
7643 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7645 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7647 return g_slist_copy (impl->filters);
7650 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7652 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7655 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7658 struct AddShortcutData
7660 GtkFileChooserDefault *impl;
7665 add_shortcut_get_info_cb (GtkFileSystemHandle *handle,
7666 const GtkFileInfo *info,
7667 const GError *error,
7671 gboolean cancelled = handle->cancelled;
7672 struct AddShortcutData *data = user_data;
7674 if (!g_slist_find (data->impl->loading_shortcuts, handle))
7677 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, handle);
7679 if (cancelled || error || !gtk_file_info_get_is_folder (info))
7682 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7684 shortcuts_insert_path (data->impl, pos, SHORTCUT_TYPE_PATH, NULL, data->path, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7687 g_object_unref (data->impl);
7688 gtk_file_path_free (data->path);
7691 g_object_unref (handle);
7695 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7696 const GtkFilePath *path,
7699 GtkFileSystemHandle *handle;
7700 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7701 struct AddShortcutData *data;
7705 /* Avoid adding duplicates */
7706 pos = shortcut_find_position (impl, path);
7707 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7711 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7712 /* translators, "Shortcut" means "Bookmark" here */
7714 GTK_FILE_CHOOSER_ERROR,
7715 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7716 _("Shortcut %s already exists"),
7723 for (l = impl->loading_shortcuts; l; l = l->next)
7725 GtkFileSystemHandle *h = l->data;
7728 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7729 if (p && !gtk_file_path_compare (path, p))
7733 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7735 GTK_FILE_CHOOSER_ERROR,
7736 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7737 _("Shortcut %s already exists"),
7745 data = g_new0 (struct AddShortcutData, 1);
7746 data->impl = g_object_ref (impl);
7747 data->path = gtk_file_path_copy (path);
7749 handle = gtk_file_system_get_info (impl->file_system, path,
7750 GTK_FILE_INFO_IS_FOLDER,
7751 add_shortcut_get_info_cb, data);
7756 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, handle);
7757 g_object_set_data (G_OBJECT (handle), "add-shortcut-path-key", data->path);
7763 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7764 const GtkFilePath *path,
7767 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7774 for (l = impl->loading_shortcuts; l; l = l->next)
7776 GtkFileSystemHandle *h = l->data;
7779 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7780 if (p && !gtk_file_path_compare (path, p))
7782 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, h);
7783 gtk_file_system_cancel_operation (h);
7788 if (impl->num_shortcuts == 0)
7791 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7792 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7793 g_assert_not_reached ();
7795 for (i = 0; i < impl->num_shortcuts; i++)
7798 ShortcutType shortcut_type;
7799 GtkFilePath *shortcut;
7801 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7802 SHORTCUTS_COL_DATA, &col_data,
7803 SHORTCUTS_COL_TYPE, &shortcut_type,
7805 g_assert (col_data != NULL);
7806 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
7808 shortcut = col_data;
7809 if (gtk_file_path_compare (shortcut, path) == 0)
7811 shortcuts_remove_rows (impl, pos + i, 1);
7812 impl->num_shortcuts--;
7816 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7817 g_assert_not_reached ();
7822 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7823 /* translators, "Shortcut" means "Bookmark" here */
7825 GTK_FILE_CHOOSER_ERROR,
7826 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7827 _("Shortcut %s does not exist"),
7835 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7837 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7843 if (impl->num_shortcuts == 0)
7846 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7847 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7848 g_assert_not_reached ();
7852 for (i = 0; i < impl->num_shortcuts; i++)
7855 ShortcutType shortcut_type;
7856 GtkFilePath *shortcut;
7858 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7859 SHORTCUTS_COL_DATA, &col_data,
7860 SHORTCUTS_COL_TYPE, &shortcut_type,
7862 g_assert (col_data != NULL);
7863 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
7865 shortcut = col_data;
7866 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
7868 if (i != impl->num_shortcuts - 1)
7870 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7871 g_assert_not_reached ();
7875 return g_slist_reverse (list);
7878 /* Guesses a size based upon font sizes */
7880 find_good_size_from_style (GtkWidget *widget,
7884 GtkFileChooserDefault *impl;
7889 g_assert (widget->style != NULL);
7890 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7892 if (impl->default_width == 0 &&
7893 impl->default_height == 0)
7895 screen = gtk_widget_get_screen (widget);
7898 resolution = gdk_screen_get_resolution (screen);
7899 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7903 resolution = 96.0; /* wheeee */
7905 font_size = pango_font_description_get_size (widget->style->font_desc);
7906 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7908 impl->default_width = font_size * NUM_CHARS;
7909 impl->default_height = font_size * NUM_LINES;
7912 *width = impl->default_width;
7913 *height = impl->default_height;
7917 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7918 gint *default_width,
7919 gint *default_height)
7921 GtkFileChooserDefault *impl;
7924 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7925 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7927 if (impl->preview_widget_active &&
7928 impl->preview_widget &&
7929 GTK_WIDGET_VISIBLE (impl->preview_widget))
7931 gtk_widget_size_request (impl->preview_box, &req);
7932 *default_width += PREVIEW_HBOX_SPACING + req.width;
7935 if (impl->extra_widget &&
7936 GTK_WIDGET_VISIBLE (impl->extra_widget))
7938 gtk_widget_size_request (impl->extra_align, &req);
7939 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
7944 gtk_file_chooser_default_get_resizable (GtkFileChooserEmbed *chooser_embed)
7946 GtkFileChooserDefault *impl;
7948 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7950 return (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7951 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
7952 gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)));
7955 struct switch_folder_closure {
7956 GtkFileChooserDefault *impl;
7957 const GtkFilePath *path;
7961 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7963 switch_folder_foreach_cb (GtkTreeModel *model,
7968 struct switch_folder_closure *closure;
7969 GtkTreeIter child_iter;
7973 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7975 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
7976 closure->num_selected++;
7979 /* Changes to the selected folder in the list view */
7981 switch_to_selected_folder (GtkFileChooserDefault *impl)
7983 GtkTreeSelection *selection;
7984 struct switch_folder_closure closure;
7986 /* We do this with foreach() rather than get_selected() as we may be in
7987 * multiple selection mode
7990 closure.impl = impl;
7991 closure.path = NULL;
7992 closure.num_selected = 0;
7994 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7995 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7997 g_assert (closure.path && closure.num_selected == 1);
7999 change_folder_and_display_error (impl, closure.path, FALSE);
8002 /* Gets the GtkFileInfo for the selected row in the file list; assumes single
8005 static const GtkFileInfo *
8006 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8007 gboolean *had_selection)
8009 GtkTreeSelection *selection;
8010 GtkTreeIter iter, child_iter;
8011 const GtkFileInfo *info;
8013 g_assert (!impl->select_multiple);
8014 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8015 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8017 *had_selection = FALSE;
8021 *had_selection = TRUE;
8023 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8027 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8031 /* Gets the display name of the selected file in the file list; assumes single
8032 * selection mode and that something is selected.
8034 static const gchar *
8035 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8037 const GtkFileInfo *info;
8038 gboolean had_selection;
8040 info = get_selected_file_info_from_file_list (impl, &had_selection);
8041 g_assert (had_selection);
8042 g_assert (info != NULL);
8044 return gtk_file_info_get_display_name (info);
8048 add_custom_button_to_dialog (GtkDialog *dialog,
8049 const gchar *mnemonic_label,
8050 const gchar *stock_id,
8055 button = gtk_button_new_with_mnemonic (mnemonic_label);
8056 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8057 gtk_button_set_image (GTK_BUTTON (button),
8058 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8059 gtk_widget_show (button);
8061 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8064 /* Presents an overwrite confirmation dialog; returns whether we should accept
8068 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8069 const gchar *file_part,
8070 const gchar *folder_display_name)
8072 GtkWindow *toplevel;
8076 toplevel = get_toplevel (GTK_WIDGET (impl));
8078 dialog = gtk_message_dialog_new (toplevel,
8079 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8080 GTK_MESSAGE_QUESTION,
8082 _("A file named \"%s\" already exists. Do you want to replace it?"),
8084 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8085 _("The file already exists in \"%s\". Replacing it will "
8086 "overwrite its contents."),
8087 folder_display_name);
8089 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8090 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"), GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8091 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8093 if (toplevel->group)
8094 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8096 response = gtk_dialog_run (GTK_DIALOG (dialog));
8098 gtk_widget_destroy (dialog);
8100 return (response == GTK_RESPONSE_ACCEPT);
8103 struct GetDisplayNameData
8105 GtkFileChooserDefault *impl;
8110 confirmation_confirm_get_info_cb (GtkFileSystemHandle *handle,
8111 const GtkFileInfo *info,
8112 const GError *error,
8115 gboolean cancelled = handle->cancelled;
8116 gboolean should_respond = FALSE;
8117 struct GetDisplayNameData *data = user_data;
8119 if (handle != data->impl->should_respond_get_info_handle)
8122 data->impl->should_respond_get_info_handle = NULL;
8128 /* Huh? Did the folder disappear? Let the caller deal with it */
8129 should_respond = TRUE;
8131 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, gtk_file_info_get_display_name (info));
8133 set_busy_cursor (data->impl, FALSE);
8135 g_signal_emit_by_name (data->impl, "response-requested");
8138 g_object_unref (data->impl);
8139 g_free (data->file_part);
8142 g_object_unref (handle);
8145 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8146 * should respond. Can get the file part from the file list or the save entry.
8149 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8150 const gchar *file_part,
8151 const GtkFilePath *parent_path)
8153 GtkFileChooserConfirmation conf;
8155 if (!impl->do_overwrite_confirmation)
8158 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8160 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8164 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8166 struct GetDisplayNameData *data;
8168 g_assert (file_part != NULL);
8170 data = g_new0 (struct GetDisplayNameData, 1);
8171 data->impl = g_object_ref (impl);
8172 data->file_part = g_strdup (file_part);
8174 if (impl->should_respond_get_info_handle)
8175 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
8177 impl->should_respond_get_info_handle =
8178 gtk_file_system_get_info (impl->file_system, parent_path,
8179 GTK_FILE_INFO_DISPLAY_NAME,
8180 confirmation_confirm_get_info_cb,
8182 set_busy_cursor (data->impl, TRUE);
8186 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8189 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8193 g_assert_not_reached ();
8198 /* Gives the focus to the browse tree view only if it is visible */
8200 focus_browse_tree_view_if_possible (GtkFileChooserDefault *impl)
8204 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8205 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8206 && !gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
8212 gtk_widget_grab_focus (impl->browse_files_tree_view);
8216 action_create_folder_cb (GtkFileSystemHandle *handle,
8217 const GtkFilePath *path,
8218 const GError *error,
8221 gboolean cancelled = handle->cancelled;
8222 GtkFileChooserDefault *impl = user_data;
8224 if (!g_slist_find (impl->pending_handles, handle))
8227 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
8229 set_busy_cursor (impl, FALSE);
8235 error_creating_folder_dialog (impl, path, g_error_copy (error));
8237 g_signal_emit_by_name (impl, "response-requested");
8240 g_object_unref (impl);
8241 g_object_unref (handle);
8244 struct FileExistsData
8246 GtkFileChooserDefault *impl;
8247 gboolean file_exists_and_is_not_folder;
8248 GtkFilePath *parent_path;
8253 save_entry_get_info_cb (GtkFileSystemHandle *handle,
8254 const GtkFileInfo *info,
8255 const GError *error,
8258 gboolean parent_is_folder;
8259 gboolean cancelled = handle->cancelled;
8260 struct FileExistsData *data = user_data;
8262 if (handle != data->impl->should_respond_get_info_handle)
8265 data->impl->should_respond_get_info_handle = NULL;
8267 set_busy_cursor (data->impl, FALSE);
8273 parent_is_folder = FALSE;
8275 parent_is_folder = gtk_file_info_get_is_folder (info);
8277 if (parent_is_folder)
8279 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8281 if (data->file_exists_and_is_not_folder)
8284 const char *file_part;
8286 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8287 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_path);
8290 g_signal_emit_by_name (data->impl, "response-requested");
8293 g_signal_emit_by_name (data->impl, "response-requested");
8295 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8297 GtkFileSystemHandle *handle;
8299 g_object_ref (data->impl);
8300 handle = gtk_file_system_create_folder (data->impl->file_system,
8302 action_create_folder_cb,
8304 data->impl->pending_handles = g_slist_append (data->impl->pending_handles, handle);
8305 set_busy_cursor (data->impl, TRUE);
8310 /* This will display an error, which is what we want */
8311 change_folder_and_display_error (data->impl, data->parent_path, FALSE);
8315 g_object_unref (data->impl);
8316 gtk_file_path_free (data->path);
8317 gtk_file_path_free (data->parent_path);
8320 g_object_unref (handle);
8324 file_exists_get_info_cb (GtkFileSystemHandle *handle,
8325 const GtkFileInfo *info,
8326 const GError *error,
8329 gboolean data_ownership_taken = FALSE;
8330 gboolean cancelled = handle->cancelled;
8331 gboolean file_exists_and_is_not_folder;
8332 struct FileExistsData *data = user_data;
8334 if (handle != data->impl->file_exists_get_info_handle)
8337 data->impl->file_exists_get_info_handle = NULL;
8339 set_busy_cursor (data->impl, FALSE);
8344 file_exists_and_is_not_folder = info && !gtk_file_info_get_is_folder (info);
8346 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8347 /* user typed a filename; we are done */
8348 g_signal_emit_by_name (data->impl, "response-requested");
8349 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8350 && file_exists_and_is_not_folder)
8352 /* Oops, the user typed the name of an existing path which is not
8355 error_creating_folder_over_existing_file_dialog (data->impl, data->path,
8356 g_error_copy (error));
8360 /* check that everything up to the last component exists */
8362 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8363 data_ownership_taken = TRUE;
8365 if (data->impl->should_respond_get_info_handle)
8366 gtk_file_system_cancel_operation (data->impl->should_respond_get_info_handle);
8368 data->impl->should_respond_get_info_handle =
8369 gtk_file_system_get_info (data->impl->file_system,
8371 GTK_FILE_INFO_IS_FOLDER,
8372 save_entry_get_info_cb,
8374 set_busy_cursor (data->impl, TRUE);
8378 if (!data_ownership_taken)
8380 g_object_unref (data->impl);
8381 gtk_file_path_free (data->path);
8382 gtk_file_path_free (data->parent_path);
8386 g_object_unref (handle);
8390 paste_text_received (GtkClipboard *clipboard,
8392 GtkFileChooserDefault *impl)
8399 path = gtk_file_system_uri_to_path (impl->file_system, text);
8402 if (!g_path_is_absolute (text))
8404 location_popup_handler (impl, text);
8408 path = gtk_file_system_filename_to_path (impl->file_system, text);
8411 location_popup_handler (impl, text);
8416 if (!gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (impl), path, NULL))
8417 location_popup_handler (impl, text);
8419 gtk_file_path_free (path);
8422 /* Handler for the "location-popup-on-paste" keybinding signal */
8424 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8426 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8427 GDK_SELECTION_CLIPBOARD);
8428 gtk_clipboard_request_text (clipboard,
8429 (GtkClipboardTextReceivedFunc) paste_text_received,
8434 /* Implementation for GtkFileChooserEmbed::should_respond() */
8436 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8438 GtkFileChooserDefault *impl;
8439 GtkWidget *toplevel;
8440 GtkWidget *current_focus;
8442 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8444 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8445 g_assert (GTK_IS_WINDOW (toplevel));
8447 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8449 if (current_focus == impl->browse_files_tree_view)
8451 /* The following array encodes what we do based on the impl->action and the
8452 * number of files selected.
8455 NOOP, /* Do nothing (don't respond) */
8456 RESPOND, /* Respond immediately */
8457 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8458 ALL_FILES, /* Respond only if everything selected is a file */
8459 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8460 SAVE_ENTRY, /* Go to the code for handling the save entry */
8461 NOT_REACHED /* Sanity check */
8463 static const ActionToTake what_to_do[4][3] = {
8464 /* 0 selected 1 selected many selected */
8465 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8466 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8467 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8468 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8472 gboolean all_files, all_folders;
8474 ActionToTake action;
8478 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8480 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8481 return search_should_respond (impl);
8483 if (impl->operation_mode == OPERATION_MODE_RECENT)
8484 return recent_should_respond (impl);
8486 selection_check (impl, &num_selected, &all_files, &all_folders);
8488 if (num_selected > 2)
8493 action = what_to_do [impl->action] [k];
8503 case RESPOND_OR_SWITCH:
8504 g_assert (num_selected == 1);
8508 switch_to_selected_folder (impl);
8511 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8512 return should_respond_after_confirm_overwrite (impl,
8513 get_display_name_from_file_list (impl),
8514 impl->current_folder);
8528 g_assert_not_reached ();
8531 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8534 gboolean is_well_formed, is_empty, is_file_part_empty;
8537 GtkFileChooserEntry *entry;
8542 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8543 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8544 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8545 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8546 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8548 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8549 check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8551 if (is_empty || !is_well_formed)
8554 g_assert (path != NULL);
8559 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8560 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8562 change_folder_and_display_error (impl, path, TRUE);
8565 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8566 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8568 /* The folder already exists, so we do not need to create it.
8569 * Just respond to terminate the dialog.
8575 g_assert_not_reached ();
8581 struct FileExistsData *data;
8583 /* We need to check whether path exists and is not a folder */
8585 data = g_new0 (struct FileExistsData, 1);
8586 data->impl = g_object_ref (impl);
8587 data->path = gtk_file_path_copy (path);
8588 data->parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
8590 if (impl->file_exists_get_info_handle)
8591 gtk_file_system_cancel_operation (impl->file_exists_get_info_handle);
8593 impl->file_exists_get_info_handle =
8594 gtk_file_system_get_info (impl->file_system, path,
8595 GTK_FILE_INFO_IS_FOLDER,
8596 file_exists_get_info_cb,
8599 set_busy_cursor (impl, TRUE);
8603 g_error_free (error);
8606 gtk_file_path_free (path);
8609 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8611 /* The focus is on a dialog's action area button, *and* the widget that
8612 * was focused immediately before it is the file list.
8616 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8618 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8621 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8623 /* The focus is on a dialog's action area button, *and* the widget that
8624 * was focused immediately before it is the location entry.
8629 /* The focus is on a dialog's action area button or something else */
8630 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8631 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8636 g_assert_not_reached ();
8640 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8642 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8644 GtkFileChooserDefault *impl;
8647 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8649 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8650 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8652 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8653 widget = impl->browse_files_tree_view;
8655 widget = impl->location_entry;
8657 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8658 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8659 widget = impl->location_entry;
8662 g_assert_not_reached ();
8666 g_assert (widget != NULL);
8667 gtk_widget_grab_focus (widget);
8670 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GtkFilePaths */
8672 search_selected_foreach_get_path_cb (GtkTreeModel *model,
8678 const GtkFilePath *file_path;
8679 GtkFilePath *file_path_copy;
8683 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_PATH, &file_path, -1);
8684 file_path_copy = gtk_file_path_copy (file_path);
8685 *list = g_slist_prepend (*list, file_path_copy);
8688 /* Constructs a list of the selected paths in search mode */
8690 search_get_selected_paths (GtkFileChooserDefault *impl)
8693 GtkTreeSelection *selection;
8697 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8698 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_path_cb, &result);
8699 result = g_slist_reverse (result);
8704 /* Called from ::should_respond(). We return whether there are selected files
8705 * in the search list.
8708 search_should_respond (GtkFileChooserDefault *impl)
8710 GtkTreeSelection *selection;
8712 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8714 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8715 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8718 struct SearchHitInsertRequest
8720 GtkFileChooserDefault *impl;
8722 GtkTreeRowReference *row_ref;
8726 search_hit_get_info_cb (GtkFileSystemHandle *handle,
8727 const GtkFileInfo *info,
8728 const GError *error,
8731 gboolean cancelled = handle->cancelled;
8732 GdkPixbuf *pixbuf = NULL;
8735 GtkFileSystemHandle *model_handle;
8736 gboolean is_folder = FALSE;
8739 struct SearchHitInsertRequest *request = data;
8741 if (!request->impl->search_model)
8744 path = gtk_tree_row_reference_get_path (request->row_ref);
8748 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8750 gtk_tree_path_free (path);
8752 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8753 SEARCH_MODEL_COL_HANDLE, &model_handle,
8755 if (handle != model_handle)
8758 /* set the handle to NULL in the model */
8759 gtk_list_store_set (request->impl->search_model, &iter,
8760 SEARCH_MODEL_COL_HANDLE, NULL,
8768 gtk_list_store_remove (request->impl->search_model, &iter);
8772 display_name = g_strdup (gtk_file_info_get_display_name (info));
8773 mime_type = g_strdup (gtk_file_info_get_mime_type (info));
8774 is_folder = gtk_file_info_get_is_folder (info);
8775 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8776 request->impl->icon_size, NULL);
8778 gtk_list_store_set (request->impl->search_model, &iter,
8779 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8780 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8781 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8782 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8786 g_object_unref (pixbuf);
8789 g_object_unref (request->impl);
8790 gtk_file_path_free (request->path);
8791 gtk_tree_row_reference_free (request->row_ref);
8794 g_object_unref (handle);
8797 /* Adds one hit from the search engine to the search_model */
8799 search_add_hit (GtkFileChooserDefault *impl,
8805 char *collation_key;
8806 struct stat statbuf;
8807 struct stat *statbuf_copy;
8810 GtkFileSystemHandle *handle;
8811 struct SearchHitInsertRequest *request;
8813 path = gtk_file_system_uri_to_path (impl->file_system, uri);
8817 filename = gtk_file_system_path_to_filename (impl->file_system, path);
8820 gtk_file_path_free (path);
8824 if (stat (filename, &statbuf) != 0)
8826 gtk_file_path_free (path);
8831 statbuf_copy = g_new (struct stat, 1);
8832 *statbuf_copy = statbuf;
8834 tmp = g_filename_display_name (filename);
8835 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
8838 request = g_new0 (struct SearchHitInsertRequest, 1);
8839 request->impl = g_object_ref (impl);
8840 request->path = gtk_file_path_copy (path);
8842 gtk_list_store_append (impl->search_model, &iter);
8843 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8845 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8846 gtk_tree_path_free (p);
8848 handle = gtk_file_system_get_info (impl->file_system, path,
8849 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_ICON | GTK_FILE_INFO_MIME_TYPE | GTK_FILE_INFO_DISPLAY_NAME,
8850 search_hit_get_info_cb,
8853 gtk_list_store_set (impl->search_model, &iter,
8854 SEARCH_MODEL_COL_PATH, path,
8855 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8856 SEARCH_MODEL_COL_STAT, statbuf_copy,
8857 SEARCH_MODEL_COL_HANDLE, handle,
8861 /* Callback used from GtkSearchEngine when we get new hits */
8863 search_engine_hits_added_cb (GtkSearchEngine *engine,
8867 GtkFileChooserDefault *impl;
8870 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8872 for (l = hits; l; l = l->next)
8873 search_add_hit (impl, (gchar*)l->data);
8876 /* Callback used from GtkSearchEngine when the query is done running */
8878 search_engine_finished_cb (GtkSearchEngine *engine,
8881 GtkFileChooserDefault *impl;
8883 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8886 /* EB: setting the model here will avoid loads of row events,
8887 * but it'll make the search look like blocked.
8889 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8890 GTK_TREE_MODEL (impl->search_model_filter));
8893 /* FMQ: if search was empty, say that we got no hits */
8894 set_busy_cursor (impl, FALSE);
8897 /* Displays a generic error when we cannot create a GtkSearchEngine.
8898 * It would be better if _gtk_search_engine_new() gave us a GError
8899 * with a better message, but it doesn't do that right now.
8902 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8904 error_message (impl,
8905 _("Could not start the search process"),
8906 _("The program was not able to create a connection to the indexer "
8907 "daemon. Please make sure it is running."));
8911 search_engine_error_cb (GtkSearchEngine *engine,
8912 const gchar *message,
8915 GtkFileChooserDefault *impl;
8917 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8919 search_stop_searching (impl, TRUE);
8920 error_message (impl, _("Could not send the search request"), message);
8922 set_busy_cursor (impl, FALSE);
8925 /* Frees the data in the search_model */
8927 search_clear_model (GtkFileChooserDefault *impl,
8928 gboolean remove_from_treeview)
8930 GtkTreeModel *model;
8933 if (!impl->search_model)
8936 model = GTK_TREE_MODEL (impl->search_model);
8938 if (gtk_tree_model_get_iter_first (model, &iter))
8942 gchar *display_name;
8943 gchar *collation_key;
8944 struct stat *statbuf;
8945 GtkFileSystemHandle *handle;
8947 gtk_tree_model_get (model, &iter,
8948 SEARCH_MODEL_COL_PATH, &path,
8949 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8950 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key,
8951 SEARCH_MODEL_COL_STAT, &statbuf,
8952 SEARCH_MODEL_COL_HANDLE, &handle,
8956 gtk_file_system_cancel_operation (handle);
8958 gtk_file_path_free (path);
8959 g_free (display_name);
8960 g_free (collation_key);
8963 while (gtk_tree_model_iter_next (model, &iter));
8965 g_object_unref (impl->search_model);
8966 impl->search_model = NULL;
8968 g_object_unref (impl->search_model_filter);
8969 impl->search_model_filter = NULL;
8971 g_object_unref (impl->search_model_sort);
8972 impl->search_model_sort = NULL;
8974 if (remove_from_treeview)
8975 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8978 /* Stops any ongoing searches; does not touch the search_model */
8980 search_stop_searching (GtkFileChooserDefault *impl,
8981 gboolean remove_query)
8983 if (remove_query && impl->search_query)
8985 g_object_unref (impl->search_query);
8986 impl->search_query = NULL;
8989 if (impl->search_engine)
8991 _gtk_search_engine_stop (impl->search_engine);
8993 g_object_unref (impl->search_engine);
8994 impl->search_engine = NULL;
8998 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
9000 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
9002 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9004 search_stop_searching (impl, FALSE);
9005 search_clear_model (impl, TRUE);
9007 gtk_widget_destroy (impl->search_hbox);
9008 impl->search_hbox = NULL;
9009 impl->search_entry = NULL;
9011 gtk_widget_show (impl->browse_path_bar);
9012 gtk_widget_show (impl->browse_new_folder_button);
9014 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9015 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9017 gtk_widget_show (impl->location_button);
9019 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9020 gtk_widget_show (impl->location_entry_box);
9023 impl->operation_mode = OPERATION_MODE_BROWSE;
9025 file_list_set_sort_column_ids (impl);
9028 /* Sort callback from the path column */
9030 search_column_path_sort_func (GtkTreeModel *model,
9035 GtkFileChooserDefault *impl = user_data;
9036 GtkTreeIter child_a, child_b;
9037 const char *collation_key_a, *collation_key_b;
9038 gboolean is_folder_a, is_folder_b;
9040 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9041 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9043 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9044 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9045 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
9047 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9048 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9049 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
9052 if (!collation_key_a)
9055 if (!collation_key_b)
9058 /* always show folders first */
9059 if (is_folder_a != is_folder_b)
9060 return is_folder_a ? 1 : -1;
9062 return strcmp (collation_key_a, collation_key_b);
9065 /* Sort callback from the modification time column */
9067 search_column_mtime_sort_func (GtkTreeModel *model,
9072 GtkFileChooserDefault *impl = user_data;
9073 GtkTreeIter child_a, child_b;
9074 const struct stat *statbuf_a, *statbuf_b;
9075 gboolean is_folder_a, is_folder_b;
9077 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9078 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9080 /* Note that although we store a whole struct stat in the model, we only
9081 * compare the mtime here. If we add another column relative to a struct stat
9082 * (e.g. a file size column), we'll want another sort callback similar to this
9085 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9086 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9087 SEARCH_MODEL_COL_STAT, &statbuf_a,
9089 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9090 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9091 SEARCH_MODEL_COL_STAT, &statbuf_b,
9100 if (is_folder_a != is_folder_b)
9101 return is_folder_a ? 1 : -1;
9103 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
9105 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
9112 search_get_is_filtered (GtkFileChooserDefault *impl,
9113 const GtkFilePath *path,
9114 const gchar *display_name,
9115 const gchar *mime_type)
9117 GtkFileFilterInfo filter_info;
9118 GtkFileFilterFlags needed;
9121 if (!impl->current_filter)
9124 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9125 needed = gtk_file_filter_get_needed (impl->current_filter);
9127 filter_info.display_name = display_name;
9128 filter_info.mime_type = mime_type;
9130 if (needed & GTK_FILE_FILTER_FILENAME)
9132 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
9133 if (filter_info.filename)
9134 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9137 filter_info.filename = NULL;
9139 if (needed & GTK_FILE_FILTER_URI)
9141 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
9142 if (filter_info.uri)
9143 filter_info.contains |= GTK_FILE_FILTER_URI;
9146 filter_info.uri = NULL;
9148 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9150 if (filter_info.filename)
9151 g_free ((gchar *) filter_info.filename);
9152 if (filter_info.uri)
9153 g_free ((gchar *) filter_info.uri);
9159 /* Visibility function for the recent filter model */
9161 search_model_visible_func (GtkTreeModel *model,
9165 GtkFileChooserDefault *impl = user_data;
9166 GtkFilePath *file_path;
9167 gchar *display_name, *mime_type;
9170 if (!impl->current_filter)
9173 gtk_tree_model_get (model, iter,
9174 SEARCH_MODEL_COL_PATH, &file_path,
9175 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
9176 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9177 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9186 return !search_get_is_filtered (impl, file_path, display_name, mime_type);
9189 /* Creates the search_model and puts it in the tree view */
9191 search_setup_model (GtkFileChooserDefault *impl)
9193 g_assert (impl->search_model == NULL);
9194 g_assert (impl->search_model_filter == NULL);
9195 g_assert (impl->search_model_sort == NULL);
9197 /* We store these columns in the search model:
9199 * SEARCH_MODEL_COL_PATH - a GtkFilePath for the hit's URI, stored as a
9200 * pointer not as a GTK_TYPE_FILE_PATH
9201 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9202 * as a pointer not as a G_TYPE_STRING
9203 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9204 * as a pointer not as a G_TYPE_STRING
9205 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
9206 * SEARCH_MODEL_COL_HANDLE - handle used when getting the hit's info
9207 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9208 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9209 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9211 * Keep this in sync with the enumeration defined near the beginning
9214 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9224 impl->search_model_filter =
9225 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9226 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9227 search_model_visible_func,
9230 impl->search_model_sort =
9231 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9232 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9233 SEARCH_MODEL_COL_PATH,
9234 search_column_path_sort_func,
9236 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9237 SEARCH_MODEL_COL_STAT,
9238 search_column_mtime_sort_func,
9240 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model_sort),
9241 SEARCH_MODEL_COL_STAT,
9242 GTK_SORT_DESCENDING);
9244 /* EB: setting the model here will make the hits list update feel
9245 * more "alive" than setting the model at the end of the search
9248 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9249 GTK_TREE_MODEL (impl->search_model_sort));
9253 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9254 GtkTreeIter *child_iter,
9259 if (!impl->search_model)
9262 if (!impl->search_model_filter || !impl->search_model_sort)
9265 /* pass 1: get the iterator in the filter model */
9266 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9269 /* pass 2: get the iterator in the real model */
9270 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9271 child_iter, &middle);
9274 /* Creates a new query with the specified text and launches it */
9276 search_start_query (GtkFileChooserDefault *impl,
9277 const gchar *query_text)
9279 search_stop_searching (impl, FALSE);
9280 search_clear_model (impl, TRUE);
9281 search_setup_model (impl);
9282 set_busy_cursor (impl, TRUE);
9284 if (impl->search_engine == NULL)
9285 impl->search_engine = _gtk_search_engine_new ();
9287 if (!impl->search_engine)
9289 set_busy_cursor (impl, FALSE);
9290 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9294 if (!impl->search_query)
9296 impl->search_query = _gtk_query_new ();
9297 _gtk_query_set_text (impl->search_query, query_text);
9300 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9302 g_signal_connect (impl->search_engine, "hits-added",
9303 G_CALLBACK (search_engine_hits_added_cb), impl);
9304 g_signal_connect (impl->search_engine, "finished",
9305 G_CALLBACK (search_engine_finished_cb), impl);
9306 g_signal_connect (impl->search_engine, "error",
9307 G_CALLBACK (search_engine_error_cb), impl);
9309 _gtk_search_engine_start (impl->search_engine);
9312 /* Callback used when the user presses Enter while typing on the search
9313 * entry; starts the query
9316 search_entry_activate_cb (GtkEntry *entry,
9319 GtkFileChooserDefault *impl;
9322 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9324 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9325 if (strlen (text) == 0)
9328 /* reset any existing query object */
9329 if (impl->search_query)
9331 g_object_unref (impl->search_query);
9332 impl->search_query = NULL;
9335 search_start_query (impl, text);
9338 /* Hides the path bar and creates the search entry */
9340 search_setup_widgets (GtkFileChooserDefault *impl)
9344 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9348 label = gtk_label_new_with_mnemonic (_("_Search:"));
9349 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9353 impl->search_entry = gtk_entry_new ();
9354 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9355 g_signal_connect (impl->search_entry, "activate",
9356 G_CALLBACK (search_entry_activate_cb),
9358 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9360 /* if there already is a query, restart it */
9361 if (impl->search_query)
9363 gchar *query = _gtk_query_get_text (impl->search_query);
9367 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9368 search_start_query (impl, query);
9374 g_object_unref (impl->search_query);
9375 impl->search_query = NULL;
9379 gtk_widget_hide (impl->browse_path_bar);
9380 gtk_widget_hide (impl->browse_new_folder_button);
9382 /* Box for search widgets */
9383 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9384 gtk_widget_show_all (impl->search_hbox);
9386 /* Hide the location widgets temporarily */
9388 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9389 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9391 gtk_widget_hide (impl->location_button);
9392 gtk_widget_hide (impl->location_entry_box);
9395 gtk_widget_grab_focus (impl->search_entry);
9397 /* FMQ: hide the filter combo? */
9400 /* Main entry point to the searching functions; this gets called when the user
9401 * activates the Search shortcut.
9404 search_activate (GtkFileChooserDefault *impl)
9406 OperationMode previous_mode;
9408 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9410 gtk_widget_grab_focus (impl->search_entry);
9414 previous_mode = impl->operation_mode;
9415 impl->operation_mode = OPERATION_MODE_SEARCH;
9417 switch (previous_mode)
9419 case OPERATION_MODE_RECENT:
9420 recent_stop_loading (impl);
9421 recent_clear_model (impl, TRUE);
9424 case OPERATION_MODE_BROWSE:
9425 stop_loading_and_clear_list_model (impl);
9428 case OPERATION_MODE_SEARCH:
9429 g_assert_not_reached ();
9433 g_assert (impl->search_hbox == NULL);
9434 g_assert (impl->search_entry == NULL);
9435 g_assert (impl->search_model == NULL);
9436 g_assert (impl->search_model_filter == NULL);
9438 search_setup_widgets (impl);
9439 file_list_set_sort_column_ids (impl);
9443 * Recent files support
9446 /* Frees the data in the recent_model */
9448 recent_clear_model (GtkFileChooserDefault *impl,
9449 gboolean remove_from_treeview)
9451 GtkTreeModel *model;
9454 if (!impl->recent_model)
9457 model = GTK_TREE_MODEL (impl->recent_model);
9459 if (remove_from_treeview)
9460 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9462 if (gtk_tree_model_get_iter_first (model, &iter))
9466 GtkFilePath *file_path;
9467 GtkFileSystemHandle *handle;
9468 GtkRecentInfo *recent_info;
9469 gchar *display_name;
9471 gtk_tree_model_get (model, &iter,
9472 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9473 RECENT_MODEL_COL_PATH, &file_path,
9474 RECENT_MODEL_COL_HANDLE, &handle,
9475 RECENT_MODEL_COL_INFO, &recent_info,
9479 gtk_file_system_cancel_operation (handle);
9481 gtk_file_path_free (file_path);
9482 gtk_recent_info_unref (recent_info);
9483 g_free (display_name);
9485 while (gtk_tree_model_iter_next (model, &iter));
9488 g_object_unref (impl->recent_model);
9489 impl->recent_model = NULL;
9491 g_object_unref (impl->recent_model_filter);
9492 impl->recent_model_filter = NULL;
9494 g_object_unref (impl->recent_model_sort);
9495 impl->recent_model_sort = NULL;
9498 /* Stops any ongoing loading of the recent files list; does
9499 * not touch the recent_model
9502 recent_stop_loading (GtkFileChooserDefault *impl)
9504 if (impl->load_recent_id)
9506 g_source_remove (impl->load_recent_id);
9507 impl->load_recent_id = 0;
9511 /* Stops any pending load, clears the file list, and switches
9512 * back to OPERATION_MODE_BROWSE
9515 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9517 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9519 recent_stop_loading (impl);
9520 recent_clear_model (impl, TRUE);
9522 gtk_widget_show (impl->browse_path_bar);
9523 gtk_widget_show (impl->browse_new_folder_button);
9525 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9526 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9528 gtk_widget_show (impl->location_button);
9530 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9531 gtk_widget_show (impl->location_entry_box);
9534 impl->operation_mode = OPERATION_MODE_BROWSE;
9536 file_list_set_sort_column_ids (impl);
9539 /* Sort callback from the modification time column */
9541 recent_column_mtime_sort_func (GtkTreeModel *model,
9546 GtkFileChooserDefault *impl = user_data;
9547 GtkTreeIter child_a, child_b;
9548 GtkRecentInfo *info_a, *info_b;
9549 gboolean is_folder_a, is_folder_b;
9551 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9552 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9554 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9555 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9556 RECENT_MODEL_COL_INFO, &info_a,
9558 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9559 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9560 RECENT_MODEL_COL_INFO, &info_b,
9569 /* folders always go first */
9570 if (is_folder_a != is_folder_b)
9571 return is_folder_a ? 1 : -1;
9573 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9575 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9582 recent_column_path_sort_func (GtkTreeModel *model,
9587 GtkFileChooserDefault *impl = user_data;
9588 GtkTreeIter child_a, child_b;
9589 gboolean is_folder_a, is_folder_b;
9590 gchar *name_a, *name_b;
9592 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9593 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9595 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9596 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9597 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9599 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9600 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9601 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9610 if (is_folder_a != is_folder_b)
9611 return is_folder_a ? 1 : -1;
9613 return strcmp (name_a, name_b);
9617 recent_get_is_filtered (GtkFileChooserDefault *impl,
9618 const GtkFilePath *path,
9619 GtkRecentInfo *recent_info)
9621 GtkFileFilterInfo filter_info;
9622 GtkFileFilterFlags needed;
9625 if (!impl->current_filter)
9628 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9629 needed = gtk_file_filter_get_needed (impl->current_filter);
9631 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9632 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9634 if (needed & GTK_FILE_FILTER_FILENAME)
9636 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
9637 if (filter_info.filename)
9638 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9641 filter_info.filename = NULL;
9643 if (needed & GTK_FILE_FILTER_URI)
9645 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
9646 if (filter_info.uri)
9647 filter_info.contains |= GTK_FILE_FILTER_URI;
9650 filter_info.uri = NULL;
9652 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9654 if (filter_info.filename)
9655 g_free ((gchar *) filter_info.filename);
9656 if (filter_info.uri)
9657 g_free ((gchar *) filter_info.uri);
9662 /* Visibility function for the recent filter model */
9664 recent_model_visible_func (GtkTreeModel *model,
9668 GtkFileChooserDefault *impl = user_data;
9669 GtkFilePath *file_path;
9670 GtkRecentInfo *recent_info;
9673 if (!impl->current_filter)
9676 gtk_tree_model_get (model, iter,
9677 RECENT_MODEL_COL_INFO, &recent_info,
9678 RECENT_MODEL_COL_PATH, &file_path,
9679 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
9688 return !recent_get_is_filtered (impl, file_path, recent_info);
9692 recent_setup_model (GtkFileChooserDefault *impl)
9694 g_assert (impl->recent_model == NULL);
9695 g_assert (impl->recent_model_filter == NULL);
9696 g_assert (impl->recent_model_sort == NULL);
9698 /* We store these columns in the search model:
9700 * RECENT_MODEL_COL_PATH - a pointer to GtkFilePath for the hit's URI,
9701 * stored as a pointer and not as a GTK_TYPE_FILE_PATH;
9702 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9703 * stored as a pointer and not as a G_TYPE_STRING;
9704 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9705 * as a GTK_TYPE_RECENT_INFO;
9706 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9707 * RECENT_MODEL_COL_HANDLE - GtkFileSystemHandle, stored as a pointer
9708 * and not as a GTK_TYPE_FILE_SYSTEM_HANDLE;
9710 * Keep this in sync with the enumeration defined near the beginning of
9713 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9720 impl->recent_model_filter =
9721 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
9722 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
9723 recent_model_visible_func,
9727 /* this is the model that will actually be added to
9728 * the browse_files_tree_view widget; remember: we are
9729 * stuffing the real model into a filter model and then
9730 * into a sort model; this means we'll have to translate
9731 * the child iterator *twice* to get from a path or an
9732 * iterator coming from the tree view widget to the
9733 * real data inside the model.
9735 impl->recent_model_sort =
9736 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
9737 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9738 RECENT_MODEL_COL_PATH,
9739 recent_column_path_sort_func,
9741 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
9742 RECENT_MODEL_COL_INFO,
9743 recent_column_mtime_sort_func,
9745 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model_sort),
9746 RECENT_MODEL_COL_INFO,
9747 GTK_SORT_DESCENDING);
9752 GtkFileChooserDefault *impl;
9755 gint n_loaded_items;
9756 guint needs_sorting : 1;
9760 recent_idle_cleanup (gpointer data)
9762 RecentLoadData *load_data = data;
9763 GtkFileChooserDefault *impl = load_data->impl;
9765 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9766 GTK_TREE_MODEL (impl->recent_model_sort));
9768 set_busy_cursor (impl, FALSE);
9770 impl->load_recent_id = 0;
9772 if (load_data->items)
9774 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9775 g_list_free (load_data->items);
9781 struct RecentItemInsertRequest
9783 GtkFileChooserDefault *impl;
9785 GtkTreeRowReference *row_ref;
9789 recent_item_get_info_cb (GtkFileSystemHandle *handle,
9790 const GtkFileInfo *info,
9791 const GError *error,
9794 gboolean cancelled = handle->cancelled;
9797 GtkFileSystemHandle *model_handle;
9798 gboolean is_folder = FALSE;
9799 struct RecentItemInsertRequest *request = data;
9801 if (!request->impl->recent_model)
9804 path = gtk_tree_row_reference_get_path (request->row_ref);
9808 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9810 gtk_tree_path_free (path);
9812 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9813 RECENT_MODEL_COL_HANDLE, &model_handle,
9815 if (handle != model_handle)
9818 gtk_list_store_set (request->impl->recent_model, &iter,
9819 RECENT_MODEL_COL_HANDLE, NULL,
9827 gtk_list_store_remove (request->impl->recent_model, &iter);
9831 is_folder = gtk_file_info_get_is_folder (info);
9833 gtk_list_store_set (request->impl->recent_model, &iter,
9834 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9838 g_object_unref (request->impl);
9839 gtk_file_path_free (request->path);
9840 gtk_tree_row_reference_free (request->row_ref);
9843 g_object_unref (handle);
9847 recent_sort_mru (gconstpointer a,
9850 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9851 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9853 return (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b));
9857 get_recent_files_limit (GtkWidget *widget)
9859 GtkSettings *settings;
9862 if (gtk_widget_has_screen (widget))
9863 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9865 settings = gtk_settings_get_default ();
9867 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9873 recent_idle_load (gpointer data)
9875 RecentLoadData *load_data = data;
9876 GtkFileChooserDefault *impl = load_data->impl;
9879 GtkRecentInfo *info;
9880 const gchar *uri, *display_name;
9882 GtkFileSystemHandle *handle;
9883 struct RecentItemInsertRequest *request;
9885 if (!impl->recent_manager)
9888 /* first iteration: load all the items */
9889 if (!load_data->items)
9891 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9892 if (!load_data->items)
9895 load_data->needs_sorting = TRUE;
9900 /* second iteration: preliminary MRU sorting and clamping */
9901 if (load_data->needs_sorting)
9905 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9906 load_data->n_items = g_list_length (load_data->items);
9908 limit = get_recent_files_limit (GTK_WIDGET (impl));
9910 if (limit != -1 && (load_data->n_items > limit))
9914 clamp = g_list_nth (load_data->items, limit - 1);
9915 if (G_LIKELY (clamp))
9920 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9923 load_data->n_items = limit;
9927 load_data->n_loaded_items = 0;
9928 load_data->needs_sorting = FALSE;
9933 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9934 g_assert (info != NULL);
9936 uri = gtk_recent_info_get_uri (info);
9937 display_name = gtk_recent_info_get_display_name (info);
9938 path = gtk_file_system_uri_to_path (impl->file_system, uri);
9942 gtk_list_store_append (impl->recent_model, &iter);
9943 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9945 request = g_new0 (struct RecentItemInsertRequest, 1);
9946 request->impl = g_object_ref (impl);
9947 request->path = gtk_file_path_copy (path);
9948 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9949 gtk_tree_path_free (p);
9951 handle = gtk_file_system_get_info (impl->file_system, path,
9952 GTK_FILE_INFO_IS_FOLDER,
9953 recent_item_get_info_cb,
9956 gtk_list_store_set (impl->recent_model, &iter,
9957 RECENT_MODEL_COL_PATH, path,
9958 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9959 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9960 RECENT_MODEL_COL_HANDLE, handle,
9965 load_data->n_loaded_items += 1;
9967 /* finished loading items */
9968 if (load_data->n_loaded_items == load_data->n_items)
9970 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9971 g_list_free (load_data->items);
9972 load_data->items = NULL;
9981 recent_start_loading (GtkFileChooserDefault *impl)
9983 RecentLoadData *load_data;
9985 recent_stop_loading (impl);
9986 recent_clear_model (impl, TRUE);
9987 recent_setup_model (impl);
9988 set_busy_cursor (impl, TRUE);
9990 if (!impl->recent_manager)
9991 recent_manager_update (impl);
9993 g_assert (impl->load_recent_id == 0);
9995 load_data = g_new (RecentLoadData, 1);
9996 load_data->impl = impl;
9997 load_data->items = NULL;
9998 load_data->n_items = 0;
9999 load_data->n_loaded_items = 0;
10000 load_data->needs_sorting = TRUE;
10002 /* begin lazy loading the recent files into the model */
10003 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
10006 recent_idle_cleanup);
10010 recent_selected_foreach_get_path_cb (GtkTreeModel *model,
10016 const GtkFilePath *file_path;
10017 GtkFilePath *file_path_copy;
10021 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_PATH, &file_path, -1);
10022 file_path_copy = gtk_file_path_copy (file_path);
10023 *list = g_slist_prepend (*list, file_path_copy);
10026 /* Constructs a list of the selected paths in recent files mode */
10028 recent_get_selected_paths (GtkFileChooserDefault *impl)
10031 GtkTreeSelection *selection;
10035 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10036 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_path_cb, &result);
10037 result = g_slist_reverse (result);
10042 /* Called from ::should_respond(). We return whether there are selected
10043 * files in the recent files list.
10046 recent_should_respond (GtkFileChooserDefault *impl)
10048 GtkTreeSelection *selection;
10050 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
10052 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10053 return (gtk_tree_selection_count_selected_rows (selection) != 0);
10056 /* Hide the location widgets temporarily */
10058 recent_hide_entry (GtkFileChooserDefault *impl)
10060 gtk_widget_hide (impl->browse_path_bar);
10061 gtk_widget_hide (impl->browse_new_folder_button);
10063 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10064 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10066 gtk_widget_hide (impl->location_button);
10067 gtk_widget_hide (impl->location_entry_box);
10071 /* Main entry point to the recent files functions; this gets called when
10072 * the user activates the Recently Used shortcut.
10075 recent_activate (GtkFileChooserDefault *impl)
10077 OperationMode previous_mode;
10079 if (impl->operation_mode == OPERATION_MODE_RECENT)
10082 previous_mode = impl->operation_mode;
10083 impl->operation_mode = OPERATION_MODE_RECENT;
10085 switch (previous_mode)
10087 case OPERATION_MODE_SEARCH:
10088 search_stop_searching (impl, FALSE);
10089 search_clear_model (impl, TRUE);
10091 gtk_widget_destroy (impl->search_hbox);
10092 impl->search_hbox = NULL;
10093 impl->search_entry = NULL;
10096 case OPERATION_MODE_BROWSE:
10097 stop_loading_and_clear_list_model (impl);
10100 case OPERATION_MODE_RECENT:
10101 g_assert_not_reached ();
10105 recent_hide_entry (impl);
10106 file_list_set_sort_column_ids (impl);
10107 recent_start_loading (impl);
10110 /* convert an iterator coming from the model bound to
10111 * browse_files_tree_view to an interator inside the
10112 * real recent_model
10115 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
10116 GtkTreeIter *child_iter,
10119 GtkTreeIter middle;
10121 if (!impl->recent_model)
10124 if (!impl->recent_model_filter || !impl->recent_model_sort)
10127 /* pass 1: get the iterator in the filter model */
10128 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
10131 /* pass 2: get the iterator in the real model */
10132 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
10139 set_current_filter (GtkFileChooserDefault *impl,
10140 GtkFileFilter *filter)
10142 if (impl->current_filter != filter)
10146 /* NULL filters are allowed to reset to non-filtered status
10148 filter_index = g_slist_index (impl->filters, filter);
10149 if (impl->filters && filter && filter_index < 0)
10152 if (impl->current_filter)
10153 g_object_unref (impl->current_filter);
10154 impl->current_filter = filter;
10155 if (impl->current_filter)
10157 g_object_ref_sink (impl->current_filter);
10161 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
10164 if (impl->browse_files_model)
10165 install_list_model_filter (impl);
10167 if (impl->search_model_filter)
10168 gtk_tree_model_filter_refilter (impl->search_model_filter);
10170 if (impl->recent_model_filter)
10171 gtk_tree_model_filter_refilter (impl->recent_model_filter);
10173 g_object_notify (G_OBJECT (impl), "filter");
10178 filter_combo_changed (GtkComboBox *combo_box,
10179 GtkFileChooserDefault *impl)
10181 gint new_index = gtk_combo_box_get_active (combo_box);
10182 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10184 set_current_filter (impl, new_filter);
10188 check_preview_change (GtkFileChooserDefault *impl)
10190 GtkTreePath *cursor_path;
10191 const GtkFilePath *new_path;
10192 const char *new_display_name;
10194 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10196 new_display_name = NULL;
10199 GtkTreeIter child_iter;
10201 if (impl->operation_mode == OPERATION_MODE_BROWSE)
10203 if (impl->sort_model)
10206 const GtkFileInfo *new_info;
10208 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10209 gtk_tree_path_free (cursor_path);
10211 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10213 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
10214 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10216 new_display_name = gtk_file_info_get_display_name (new_info);
10219 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10223 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10224 &iter, cursor_path);
10225 gtk_tree_path_free (cursor_path);
10227 search_get_valid_child_iter (impl, &child_iter, &iter);
10228 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10229 SEARCH_MODEL_COL_PATH, &new_path,
10230 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10233 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10237 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10238 &iter, cursor_path);
10239 gtk_tree_path_free (cursor_path);
10241 recent_get_valid_child_iter (impl, &child_iter, &iter);
10242 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10243 RECENT_MODEL_COL_PATH, &new_path,
10244 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10249 if (new_path != impl->preview_path &&
10250 !(new_path && impl->preview_path &&
10251 gtk_file_path_compare (new_path, impl->preview_path) == 0))
10253 if (impl->preview_path)
10255 gtk_file_path_free (impl->preview_path);
10256 g_free (impl->preview_display_name);
10261 impl->preview_path = gtk_file_path_copy (new_path);
10262 impl->preview_display_name = g_strdup (new_display_name);
10266 impl->preview_path = NULL;
10267 impl->preview_display_name = NULL;
10270 if (impl->use_preview_label && impl->preview_label)
10271 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10273 g_signal_emit_by_name (impl, "update-preview");
10278 shortcuts_activate_volume_mount_cb (GtkFileSystemHandle *handle,
10279 GtkFileSystemVolume *volume,
10280 const GError *error,
10284 gboolean cancelled = handle->cancelled;
10285 GtkFileChooserDefault *impl = data;
10287 if (handle != impl->shortcuts_activate_iter_handle)
10290 impl->shortcuts_activate_iter_handle = NULL;
10292 set_busy_cursor (impl, FALSE);
10301 msg = g_strdup_printf (_("Could not mount %s"),
10302 gtk_file_system_volume_get_display_name (impl->file_system, volume));
10303 error_message (impl, msg, error->message);
10309 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
10312 change_folder_and_display_error (impl, path, FALSE);
10313 focus_browse_tree_view_if_possible (impl);
10315 gtk_file_path_free (path);
10319 g_object_unref (impl);
10320 g_object_unref (handle);
10324 /* Activates a volume by mounting it if necessary and then switching to its
10328 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10329 GtkFileSystemVolume *volume)
10333 switch (impl->operation_mode)
10335 case OPERATION_MODE_BROWSE:
10337 case OPERATION_MODE_SEARCH:
10338 search_switch_to_browse_mode (impl);
10340 case OPERATION_MODE_RECENT:
10341 recent_switch_to_browse_mode (impl);
10345 /* We ref the file chooser since volume_mount() may run a main loop, and the
10346 * user could close the file chooser window in the meantime.
10348 g_object_ref (impl);
10350 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
10352 set_busy_cursor (impl, TRUE);
10354 impl->shortcuts_activate_iter_handle =
10355 gtk_file_system_volume_mount (impl->file_system, volume,
10356 shortcuts_activate_volume_mount_cb,
10357 g_object_ref (impl));
10361 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
10364 change_folder_and_display_error (impl, path, FALSE);
10365 gtk_file_path_free (path);
10369 g_object_unref (impl);
10372 /* Opens the folder or volume at the specified iter in the shortcuts model */
10373 struct ShortcutsActivateData
10375 GtkFileChooserDefault *impl;
10380 shortcuts_activate_get_info_cb (GtkFileSystemHandle *handle,
10381 const GtkFileInfo *info,
10382 const GError *error,
10383 gpointer user_data)
10385 gboolean cancelled = handle->cancelled;
10386 struct ShortcutsActivateData *data = user_data;
10388 if (handle != data->impl->shortcuts_activate_iter_handle)
10391 data->impl->shortcuts_activate_iter_handle = NULL;
10396 if (!error && gtk_file_info_get_is_folder (info))
10398 change_folder_and_display_error (data->impl, data->path, FALSE);
10399 focus_browse_tree_view_if_possible (data->impl);
10402 gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (data->impl),
10407 g_object_unref (data->impl);
10408 gtk_file_path_free (data->path);
10411 g_object_unref (handle);
10415 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10419 ShortcutType shortcut_type;
10421 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY && impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
10422 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10424 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10425 SHORTCUTS_COL_DATA, &col_data,
10426 SHORTCUTS_COL_TYPE, &shortcut_type,
10429 if (impl->shortcuts_activate_iter_handle)
10431 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
10432 impl->shortcuts_activate_iter_handle = NULL;
10435 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10437 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10439 GtkFileSystemVolume *volume;
10443 shortcuts_activate_volume (impl, volume);
10445 else if (shortcut_type == SHORTCUT_TYPE_PATH)
10447 struct ShortcutsActivateData *data;
10449 data = g_new0 (struct ShortcutsActivateData, 1);
10450 data->impl = g_object_ref (impl);
10451 data->path = gtk_file_path_copy (col_data);
10453 impl->shortcuts_activate_iter_handle =
10454 gtk_file_system_get_info (impl->file_system, data->path,
10455 GTK_FILE_INFO_IS_FOLDER,
10456 shortcuts_activate_get_info_cb, data);
10458 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10460 search_activate (impl);
10462 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10464 recent_activate (impl);
10468 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10470 shortcuts_key_press_event_cb (GtkWidget *widget,
10471 GdkEventKey *event,
10472 GtkFileChooserDefault *impl)
10476 modifiers = gtk_accelerator_get_default_mod_mask ();
10478 if ((event->keyval == GDK_BackSpace
10479 || event->keyval == GDK_Delete
10480 || event->keyval == GDK_KP_Delete)
10481 && (event->state & modifiers) == 0)
10483 remove_selected_bookmarks (impl);
10487 if ((event->keyval == GDK_F2)
10488 && (event->state & modifiers) == 0)
10490 rename_selected_bookmark (impl);
10498 shortcuts_select_func (GtkTreeSelection *selection,
10499 GtkTreeModel *model,
10501 gboolean path_currently_selected,
10504 GtkFileChooserDefault *impl = data;
10505 GtkTreeIter filter_iter;
10506 ShortcutType shortcut_type;
10508 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10509 g_assert_not_reached ();
10511 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10513 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10517 list_select_func (GtkTreeSelection *selection,
10518 GtkTreeModel *model,
10520 gboolean path_currently_selected,
10523 GtkFileChooserDefault *impl = data;
10525 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10526 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10528 GtkTreeIter iter, child_iter;
10530 switch (impl->operation_mode)
10532 case OPERATION_MODE_SEARCH:
10534 gboolean is_folder;
10536 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10539 search_get_valid_child_iter (impl, &child_iter, &iter);
10540 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10541 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10548 case OPERATION_MODE_RECENT:
10550 gboolean is_folder;
10552 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10555 recent_get_valid_child_iter (impl, &child_iter, &iter);
10556 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10557 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10564 case OPERATION_MODE_BROWSE:
10566 const GtkFileInfo *info;
10568 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10571 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10572 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10573 if (info && !gtk_file_info_get_is_folder (info))
10584 list_selection_changed (GtkTreeSelection *selection,
10585 GtkFileChooserDefault *impl)
10587 /* See if we are in the new folder editable row for Save mode */
10588 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10589 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10591 const GtkFileInfo *info;
10592 gboolean had_selection;
10594 info = get_selected_file_info_from_file_list (impl, &had_selection);
10595 if (!had_selection)
10596 goto out; /* normal processing */
10599 return; /* We are on the editable row for New Folder */
10604 if (impl->location_entry)
10605 update_chooser_entry (impl);
10607 check_preview_change (impl);
10608 bookmarks_check_add_sensitivity (impl);
10610 g_signal_emit_by_name (impl, "selection-changed", 0);
10613 /* Callback used when a row in the file list is activated */
10615 list_row_activated (GtkTreeView *tree_view,
10617 GtkTreeViewColumn *column,
10618 GtkFileChooserDefault *impl)
10621 GtkTreeIter child_iter;
10623 switch (impl->operation_mode)
10625 case OPERATION_MODE_SEARCH:
10627 GtkFilePath *file_path;
10628 gboolean is_folder;
10630 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10633 search_get_valid_child_iter (impl, &child_iter, &iter);
10634 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10635 SEARCH_MODEL_COL_PATH, &file_path,
10636 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10641 change_folder_and_display_error (impl, file_path, FALSE);
10645 g_signal_emit_by_name (impl, "file-activated");
10649 case OPERATION_MODE_RECENT:
10651 GtkFilePath *file_path;
10652 gboolean is_folder;
10654 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10657 recent_get_valid_child_iter (impl, &child_iter, &iter);
10658 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10659 RECENT_MODEL_COL_PATH, &file_path,
10660 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10665 change_folder_and_display_error (impl, file_path, FALSE);
10669 g_signal_emit_by_name (impl, "file-activated");
10673 case OPERATION_MODE_BROWSE:
10675 const GtkFileInfo *info;
10677 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10680 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10681 &child_iter, &iter);
10682 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10685 if (gtk_file_info_get_is_folder (info))
10687 const GtkFilePath *file_path;
10689 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
10690 change_folder_and_display_error (impl, file_path, FALSE);
10694 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10695 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10696 g_signal_emit_by_name (impl, "file-activated");
10703 path_bar_clicked (GtkPathBar *path_bar,
10704 GtkFilePath *file_path,
10705 GtkFilePath *child_path,
10706 gboolean child_is_hidden,
10707 GtkFileChooserDefault *impl)
10710 pending_select_paths_add (impl, child_path);
10712 if (!change_folder_and_display_error (impl, file_path, FALSE))
10715 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10716 * show hidden files so that ".baz" appears in the file list, as it will still
10717 * be shown in the path bar: "/foo/[bar]/.baz"
10719 if (child_is_hidden)
10720 g_object_set (impl, "show-hidden", TRUE, NULL);
10723 static const GtkFileInfo *
10724 get_list_file_info (GtkFileChooserDefault *impl,
10727 GtkTreeIter child_iter;
10729 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10733 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10737 list_icon_data_func (GtkTreeViewColumn *tree_column,
10738 GtkCellRenderer *cell,
10739 GtkTreeModel *tree_model,
10743 GtkFileChooserDefault *impl = data;
10744 GtkTreeIter child_iter;
10745 GdkPixbuf *pixbuf = NULL;
10746 gboolean sensitive = TRUE;
10748 profile_start ("start", NULL);
10750 switch (impl->operation_mode)
10752 case OPERATION_MODE_SEARCH:
10754 GtkTreeIter child_iter;
10755 gboolean is_folder;
10757 search_get_valid_child_iter (impl, &child_iter, iter);
10758 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10759 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10760 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10763 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10764 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10765 sensitive = is_folder;
10769 case OPERATION_MODE_RECENT:
10771 GtkTreeIter child_iter;
10772 GtkRecentInfo *info;
10773 gboolean is_folder;
10775 recent_get_valid_child_iter (impl, &child_iter, iter);
10776 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10777 RECENT_MODEL_COL_INFO, &info,
10778 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10781 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10783 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10784 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10785 sensitive = is_folder;
10789 case OPERATION_MODE_BROWSE:
10791 const GtkFileInfo *info;
10792 const GtkFilePath *path;
10794 info = get_list_file_info (impl, iter);
10796 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10799 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
10804 /* FIXME: NULL GError */
10805 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (impl),
10806 impl->icon_size, NULL);
10811 /* We are on the editable row */
10816 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10817 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10818 sensitive = gtk_file_info_get_is_folder (info);
10823 g_object_set (cell,
10825 "sensitive", sensitive,
10829 g_object_unref (pixbuf);
10831 profile_end ("end", NULL);
10835 list_name_data_func (GtkTreeViewColumn *tree_column,
10836 GtkCellRenderer *cell,
10837 GtkTreeModel *tree_model,
10841 GtkFileChooserDefault *impl = data;
10842 const GtkFileInfo *info;
10843 gboolean sensitive = TRUE;
10845 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10847 GtkTreeIter child_iter;
10848 gchar *display_name;
10849 gboolean is_folder;
10851 search_get_valid_child_iter (impl, &child_iter, iter);
10852 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10853 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10854 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10857 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10858 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10860 sensitive = is_folder;
10863 g_object_set (cell,
10864 "text", display_name,
10865 "sensitive", sensitive,
10866 "ellipsize", PANGO_ELLIPSIZE_END,
10872 if (impl->operation_mode == OPERATION_MODE_RECENT)
10874 GtkTreeIter child_iter;
10875 GtkRecentInfo *recent_info;
10876 gchar *display_name;
10877 gboolean is_folder;
10879 recent_get_valid_child_iter (impl, &child_iter, iter);
10880 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10881 RECENT_MODEL_COL_INFO, &recent_info,
10882 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10885 display_name = gtk_recent_info_get_short_name (recent_info);
10887 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10888 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10890 sensitive = is_folder;
10893 g_object_set (cell,
10894 "text", display_name,
10895 "sensitive", sensitive,
10896 "ellipsize", PANGO_ELLIPSIZE_END,
10899 g_free (display_name);
10904 info = get_list_file_info (impl, iter);
10909 g_object_set (cell,
10910 "text", _("Type name of new folder"),
10912 "ellipsize", PANGO_ELLIPSIZE_NONE,
10919 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10920 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10922 sensitive = gtk_file_info_get_is_folder (info);
10925 g_object_set (cell,
10926 "text", gtk_file_info_get_display_name (info),
10927 "sensitive", sensitive,
10928 "ellipsize", PANGO_ELLIPSIZE_END,
10934 list_size_data_func (GtkTreeViewColumn *tree_column,
10935 GtkCellRenderer *cell,
10936 GtkTreeModel *tree_model,
10940 GtkFileChooserDefault *impl = data;
10941 const GtkFileInfo *info = get_list_file_info (impl, iter);
10944 gboolean sensitive = TRUE;
10946 if (!info || gtk_file_info_get_is_folder (info))
10948 g_object_set (cell,
10950 "sensitive", sensitive,
10955 size = gtk_file_info_get_size (info);
10957 if (size < (gint64)1024)
10958 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
10959 else if (size < (gint64)1024*1024)
10960 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10961 else if (size < (gint64)1024*1024*1024)
10962 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10964 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10966 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10967 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10968 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10971 g_object_set (cell,
10973 "sensitive", sensitive,
10974 "alignment", PANGO_ALIGN_RIGHT,
10981 /* Tree column data callback for the file list; fetches the mtime of a file */
10983 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10984 GtkCellRenderer *cell,
10985 GtkTreeModel *tree_model,
10989 GtkFileChooserDefault *impl;
10991 gchar *date_str = NULL;
10992 gboolean sensitive = TRUE;
10996 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10998 GtkTreeIter child_iter;
10999 struct stat *statbuf;
11000 gboolean is_folder;
11002 search_get_valid_child_iter (impl, &child_iter, iter);
11003 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11004 SEARCH_MODEL_COL_STAT, &statbuf,
11005 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11008 time_mtime = statbuf->st_mtime;
11013 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11014 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11015 sensitive = is_folder;
11017 else if (impl->operation_mode == OPERATION_MODE_RECENT)
11019 GtkTreeIter child_iter;
11020 GtkRecentInfo *info;
11021 gboolean is_folder;
11023 recent_get_valid_child_iter (impl, &child_iter, iter);
11024 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11025 RECENT_MODEL_COL_INFO, &info,
11026 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11030 time_mtime = gtk_recent_info_get_modified (info);
11034 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11035 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11036 sensitive = is_folder;
11040 const GtkFileInfo *info;
11042 info = get_list_file_info (impl, iter);
11045 g_object_set (cell,
11052 time_mtime = (time_t) gtk_file_info_get_modification_time (info);
11054 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11055 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11056 sensitive = gtk_file_info_get_is_folder (info);
11059 if (G_UNLIKELY (time_mtime == 0))
11060 date_str = g_strdup (_("Unknown"));
11065 struct tm tm_mtime;
11067 const gchar *format;
11068 gchar *locale_format = NULL;
11071 #ifdef HAVE_LOCALTIME_R
11072 localtime_r ((time_t *) &time_mtime, &tm_mtime);
11075 struct tm *ptm = localtime ((time_t *) &time_mtime);
11079 g_warning ("ptm != NULL failed");
11081 g_object_set (cell,
11082 "text", _("Unknown"),
11083 "sensitive", sensitive,
11088 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
11090 #endif /* HAVE_LOCALTIME_R */
11092 g_date_set_time_t (&mtime, time_mtime);
11093 time_now = time (NULL);
11094 g_date_set_time_t (&now, time_now);
11096 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
11098 /* Translators: %H means "hours" and %M means "minutes" */
11099 if (days_diff == 0)
11100 format = _("Today at %H:%M");
11101 else if (days_diff == 1)
11102 format = _("Yesterday at %H:%M");
11105 if (days_diff > 1 && days_diff < 7)
11106 format = "%A"; /* Days from last week */
11108 format = "%x"; /* Any other date */
11111 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
11113 if (strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
11114 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11116 date_str = g_strdup (_("Unknown"));
11118 g_free (locale_format);
11121 g_object_set (cell,
11123 "sensitive", sensitive,
11129 _gtk_file_chooser_default_new (const char *file_system)
11131 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
11132 "file-system-backend", file_system,
11137 location_set_user_text (GtkFileChooserDefault *impl,
11140 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11141 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11145 location_popup_handler (GtkFileChooserDefault *impl,
11148 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11150 GtkWidget *widget_to_focus;
11152 /* This will give us the location widgets back */
11153 switch (impl->operation_mode)
11155 case OPERATION_MODE_SEARCH:
11156 search_switch_to_browse_mode (impl);
11158 case OPERATION_MODE_RECENT:
11159 recent_switch_to_browse_mode (impl);
11161 case OPERATION_MODE_BROWSE:
11162 g_assert_not_reached ();
11166 if (impl->current_folder)
11167 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11169 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11170 widget_to_focus = impl->browse_files_tree_view;
11172 widget_to_focus = impl->location_entry;
11174 gtk_widget_grab_focus (widget_to_focus);
11178 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11179 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11181 LocationMode new_mode;
11185 /* since the user typed something, we unconditionally want to turn on the entry */
11186 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11188 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11189 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11190 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11191 new_mode = LOCATION_MODE_PATH_BAR;
11194 g_assert_not_reached ();
11198 location_mode_set (impl, new_mode, TRUE);
11199 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11202 location_set_user_text (impl, path);
11205 location_entry_set_initial_text (impl);
11206 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11210 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11211 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11213 gtk_widget_grab_focus (impl->location_entry);
11215 location_set_user_text (impl, path);
11218 g_assert_not_reached ();
11221 /* Handler for the "up-folder" keybinding signal */
11223 up_folder_handler (GtkFileChooserDefault *impl)
11225 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11228 /* Handler for the "down-folder" keybinding signal */
11230 down_folder_handler (GtkFileChooserDefault *impl)
11232 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11235 /* Switches to the shortcut in the specified index */
11237 switch_to_shortcut (GtkFileChooserDefault *impl,
11242 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11243 g_assert_not_reached ();
11245 shortcuts_activate_iter (impl, &iter);
11246 focus_browse_tree_view_if_possible (impl);
11249 /* Handler for the "home-folder" keybinding signal */
11251 home_folder_handler (GtkFileChooserDefault *impl)
11253 if (impl->has_home)
11254 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11257 /* Handler for the "desktop-folder" keybinding signal */
11259 desktop_folder_handler (GtkFileChooserDefault *impl)
11261 if (impl->has_desktop)
11262 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11265 /* Handler for the "search-shortcut" keybinding signal */
11267 search_shortcut_handler (GtkFileChooserDefault *impl)
11269 if (impl->has_search)
11271 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11273 /* we want the entry widget to grab the focus the first
11274 * time, not the browse_files_tree_view widget.
11276 if (impl->search_entry)
11277 gtk_widget_grab_focus (impl->search_entry);
11281 /* Handler for the "recent-shortcut" keybinding signal */
11283 recent_shortcut_handler (GtkFileChooserDefault *impl)
11285 if (impl->has_recent)
11286 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11290 quick_bookmark_handler (GtkFileChooserDefault *impl,
11291 gint bookmark_index)
11296 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11299 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11301 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11302 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11305 gtk_tree_path_free (path);
11307 switch_to_shortcut (impl, bookmark_pos);
11311 show_hidden_handler (GtkFileChooserDefault *impl)
11313 g_object_set (impl,
11314 "show-hidden", !impl->show_hidden,
11319 /* Drag and drop interfaces */
11322 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11327 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11329 model->impl = NULL;
11332 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11334 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11337 ShortcutsPaneModelFilter *model;
11341 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11343 pos = *gtk_tree_path_get_indices (path);
11344 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11346 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11349 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11351 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11353 GtkSelectionData *selection_data)
11355 ShortcutsPaneModelFilter *model;
11357 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11364 /* Fill the GtkTreeDragSourceIface vtable */
11366 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11368 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11369 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11373 /* Fill the GtkTreeDragDestIface vtable */
11375 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11377 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11378 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11382 static GtkTreeModel *
11383 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11384 GtkTreeModel *child_model,
11387 ShortcutsPaneModelFilter *model;
11389 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11390 "child-model", child_model,
11391 "virtual-root", root,
11394 model->impl = impl;
11396 return GTK_TREE_MODEL (model);
11402 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11405 RecentModelSort *model;
11406 GtkTreeIter iter, child_iter;
11407 gboolean is_folder;
11409 model = RECENT_MODEL_SORT (drag_source);
11410 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11413 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11414 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11415 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11422 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11424 GtkSelectionData *selection_data)
11426 RecentModelSort *model;
11427 GtkTreeIter iter, child_iter;
11428 GtkFilePath *file_path;
11431 model = RECENT_MODEL_SORT (drag_source);
11432 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11435 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11436 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11437 RECENT_MODEL_COL_PATH, &file_path,
11439 g_assert (file_path != NULL);
11441 uris = g_new (gchar *, 2);
11442 uris[0] = gtk_file_system_path_to_uri (model->impl->file_system, file_path);
11445 gtk_selection_data_set_uris (selection_data, uris);
11453 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11455 iface->row_draggable = recent_model_sort_row_draggable;
11456 iface->drag_data_get = recent_model_sort_drag_data_get;
11460 _recent_model_sort_class_init (RecentModelSortClass *klass)
11466 _recent_model_sort_init (RecentModelSort *model)
11468 model->impl = NULL;
11471 static GtkTreeModel *
11472 recent_model_sort_new (GtkFileChooserDefault *impl,
11473 GtkTreeModel *child_model)
11475 RecentModelSort *model;
11477 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11478 "model", child_model,
11480 model->impl = impl;
11482 return GTK_TREE_MODEL (model);
11488 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11491 SearchModelSort *model;
11492 GtkTreeIter iter, child_iter;
11493 gboolean is_folder;
11495 model = SEARCH_MODEL_SORT (drag_source);
11496 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11499 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11500 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11501 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11508 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11510 GtkSelectionData *selection_data)
11512 SearchModelSort *model;
11513 GtkTreeIter iter, child_iter;
11514 GtkFilePath *file_path;
11517 model = SEARCH_MODEL_SORT (drag_source);
11518 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11521 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11522 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11523 RECENT_MODEL_COL_PATH, &file_path,
11525 g_assert (file_path != NULL);
11527 uris = g_new (gchar *, 2);
11528 uris[0] = gtk_file_system_path_to_uri (model->impl->file_system, file_path);
11531 gtk_selection_data_set_uris (selection_data, uris);
11539 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11541 iface->row_draggable = search_model_sort_row_draggable;
11542 iface->drag_data_get = search_model_sort_drag_data_get;
11546 _search_model_sort_class_init (SearchModelSortClass *klass)
11552 _search_model_sort_init (SearchModelSort *model)
11554 model->impl = NULL;
11557 static GtkTreeModel *
11558 search_model_sort_new (GtkFileChooserDefault *impl,
11559 GtkTreeModel *child_model)
11561 SearchModelSort *model;
11563 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
11564 "model", child_model,
11566 model->impl = impl;
11568 return GTK_TREE_MODEL (model);