1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gdk/gdkkeysyms.h"
25 #include "gtkalignment.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcombobox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserdialog.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechoosersettings.h"
41 #include "gtkfilechooserutils.h"
42 #include "gtkfilechooser.h"
43 #include "gtkfilesystem.h"
44 #include "gtkfilesystemmodel.h"
47 #include "gtkhpaned.h"
48 #include "gtkiconfactory.h"
49 #include "gtkicontheme.h"
51 #include "gtkimagemenuitem.h"
53 #include "gtkmarshalers.h"
54 #include "gtkmessagedialog.h"
55 #include "gtkmountoperation.h"
56 #include "gtkpathbar.h"
57 #include "gtkprivate.h"
58 #include "gtkradiobutton.h"
59 #include "gtkrecentfilter.h"
60 #include "gtkrecentmanager.h"
61 #include "gtkscrolledwindow.h"
62 #include "gtkseparatormenuitem.h"
63 #include "gtksizegroup.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
79 #include <sys/types.h>
90 #undef PROFILE_FILE_CHOOSER
91 #ifdef PROFILE_FILE_CHOOSER
98 #define PROFILE_INDENT 4
99 static int profile_indent;
102 profile_add_indent (int indent)
104 profile_indent += indent;
105 if (profile_indent < 0)
106 g_error ("You screwed up your indentation");
110 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
115 profile_add_indent (indent);
117 if (profile_indent == 0)
118 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
120 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
126 profile_add_indent (indent);
129 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
130 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
131 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
133 #define profile_start(x, y)
134 #define profile_end(x, y)
135 #define profile_msg(x, y)
140 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
142 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
143 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
144 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146 #define MAX_LOADING_TIME 500
148 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
150 struct _GtkFileChooserDefaultClass
152 GtkVBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
172 static guint signals[LAST_SIGNAL] = { 0 };
174 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
176 SHORTCUTS_COL_PIXBUF,
180 SHORTCUTS_COL_REMOVABLE,
181 SHORTCUTS_COL_PIXBUF_VISIBLE,
182 SHORTCUTS_COL_CANCELLABLE,
183 SHORTCUTS_COL_NUM_COLUMNS
188 SHORTCUT_TYPE_VOLUME,
189 SHORTCUT_TYPE_SEPARATOR,
190 SHORTCUT_TYPE_SEARCH,
194 /* Column numbers for the search model.
195 * Keep this in sync with search_setup_model()
198 SEARCH_MODEL_COL_FILE,
199 SEARCH_MODEL_COL_DISPLAY_NAME,
200 SEARCH_MODEL_COL_COLLATION_KEY,
201 SEARCH_MODEL_COL_MTIME,
202 SEARCH_MODEL_COL_SIZE,
203 SEARCH_MODEL_COL_CANCELLABLE,
204 SEARCH_MODEL_COL_PIXBUF,
205 SEARCH_MODEL_COL_MIME_TYPE,
206 SEARCH_MODEL_COL_IS_FOLDER,
207 SEARCH_MODEL_COL_NUM_COLUMNS
211 RECENT_MODEL_COL_FILE,
212 RECENT_MODEL_COL_DISPLAY_NAME,
213 RECENT_MODEL_COL_INFO,
214 RECENT_MODEL_COL_IS_FOLDER,
215 RECENT_MODEL_COL_CANCELLABLE,
216 RECENT_MODEL_COL_NUM_COLUMNS
220 GTK_FILE_SYSTEM_MODEL_INFO,
221 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME,
222 GTK_FILE_SYSTEM_MODEL_N_COLUMNS
223 } GtkFileSystemModelColumns;
225 /* Identifiers for target types */
231 search_is_possible (GtkFileChooserDefault *impl)
233 if (impl->search_engine == NULL)
234 impl->search_engine = _gtk_search_engine_new ();
236 return impl->search_engine != NULL;
239 /* Interesting places in the shortcuts bar */
243 SHORTCUTS_RECENT_SEPARATOR,
248 SHORTCUTS_BOOKMARKS_SEPARATOR,
250 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
251 SHORTCUTS_CURRENT_FOLDER
254 /* Icon size for if we can't get it from the theme */
255 #define FALLBACK_ICON_SIZE 16
257 #define PREVIEW_HBOX_SPACING 12
261 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
262 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
264 static GObject* gtk_file_chooser_default_constructor (GType type,
265 guint n_construct_properties,
266 GObjectConstructParam *construct_params);
267 static void gtk_file_chooser_default_finalize (GObject *object);
268 static void gtk_file_chooser_default_set_property (GObject *object,
272 static void gtk_file_chooser_default_get_property (GObject *object,
276 static void gtk_file_chooser_default_dispose (GObject *object);
277 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
278 static void gtk_file_chooser_default_realize (GtkWidget *widget);
279 static void gtk_file_chooser_default_map (GtkWidget *widget);
280 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
281 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
282 GtkWidget *previous_toplevel);
283 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
284 GtkStyle *previous_style);
285 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
286 GdkScreen *previous_screen);
287 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
288 GtkAllocation *allocation);
290 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
293 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
296 gboolean clear_entry,
298 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
299 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
301 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
304 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
306 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
307 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
308 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
309 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
310 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
311 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
312 GtkFileFilter *filter);
313 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
314 GtkFileFilter *filter);
315 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
316 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
319 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
322 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
324 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
326 gint *default_height);
327 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
328 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
330 static void location_popup_handler (GtkFileChooserDefault *impl,
332 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
333 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
334 static void up_folder_handler (GtkFileChooserDefault *impl);
335 static void down_folder_handler (GtkFileChooserDefault *impl);
336 static void home_folder_handler (GtkFileChooserDefault *impl);
337 static void desktop_folder_handler (GtkFileChooserDefault *impl);
338 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
339 gint bookmark_index);
340 static void show_hidden_handler (GtkFileChooserDefault *impl);
341 static void search_shortcut_handler (GtkFileChooserDefault *impl);
342 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
343 static void update_appearance (GtkFileChooserDefault *impl);
345 static void set_current_filter (GtkFileChooserDefault *impl,
346 GtkFileFilter *filter);
347 static void check_preview_change (GtkFileChooserDefault *impl);
349 static void filter_combo_changed (GtkComboBox *combo_box,
350 GtkFileChooserDefault *impl);
352 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
354 GtkFileChooserDefault *impl);
356 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
359 gboolean path_currently_selected,
361 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
363 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
365 static int shortcuts_get_index (GtkFileChooserDefault *impl,
366 ShortcutsIndex where);
367 static int shortcut_find_position (GtkFileChooserDefault *impl,
370 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
372 static gboolean list_select_func (GtkTreeSelection *selection,
375 gboolean path_currently_selected,
378 static void list_selection_changed (GtkTreeSelection *tree_selection,
379 GtkFileChooserDefault *impl);
380 static void list_row_activated (GtkTreeView *tree_view,
382 GtkTreeViewColumn *column,
383 GtkFileChooserDefault *impl);
385 static void select_func (GtkFileSystemModel *model,
390 static void path_bar_clicked (GtkPathBar *path_bar,
393 gboolean child_is_hidden,
394 GtkFileChooserDefault *impl);
396 static void add_bookmark_button_clicked_cb (GtkButton *button,
397 GtkFileChooserDefault *impl);
398 static void remove_bookmark_button_clicked_cb (GtkButton *button,
399 GtkFileChooserDefault *impl);
400 static void save_folder_combo_changed_cb (GtkComboBox *combo,
401 GtkFileChooserDefault *impl);
403 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
404 GtkCellRenderer *cell,
405 GtkTreeModel *tree_model,
408 static void list_name_data_func (GtkTreeViewColumn *tree_column,
409 GtkCellRenderer *cell,
410 GtkTreeModel *tree_model,
413 static void list_size_data_func (GtkTreeViewColumn *tree_column,
414 GtkCellRenderer *cell,
415 GtkTreeModel *tree_model,
418 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
419 GtkCellRenderer *cell,
420 GtkTreeModel *tree_model,
424 static GFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
427 static void load_remove_timer (GtkFileChooserDefault *impl);
428 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
430 static void location_button_toggled_cb (GtkToggleButton *toggle,
431 GtkFileChooserDefault *impl);
432 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
434 static void search_stop_searching (GtkFileChooserDefault *impl,
435 gboolean remove_query);
436 static void search_clear_model_row (GtkTreeModel *model,
438 static void search_clear_model (GtkFileChooserDefault *impl,
439 gboolean remove_from_treeview);
440 static gboolean search_should_respond (GtkFileChooserDefault *impl);
441 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
442 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
443 static void search_entry_activate_cb (GtkEntry *entry,
445 static void settings_load (GtkFileChooserDefault *impl);
446 static void search_get_valid_child_iter (GtkFileChooserDefault *impl,
447 GtkTreeIter *child_iter,
450 static void recent_stop_loading (GtkFileChooserDefault *impl);
451 static void recent_clear_model (GtkFileChooserDefault *impl,
452 gboolean remove_from_treeview);
453 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
454 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
455 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
456 static void recent_get_valid_child_iter (GtkFileChooserDefault *impl,
457 GtkTreeIter *child_iter,
459 static void set_file_system_backend (GtkFileChooserDefault *impl);
460 static void unset_file_system_backend (GtkFileChooserDefault *impl);
466 /* Drag and drop interface declarations */
469 GtkTreeModelFilter parent;
471 GtkFileChooserDefault *impl;
472 } ShortcutsPaneModelFilter;
475 GtkTreeModelFilterClass parent_class;
476 } ShortcutsPaneModelFilterClass;
478 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
479 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
481 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
483 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
484 _shortcuts_pane_model_filter,
485 GTK_TYPE_TREE_MODEL_FILTER,
486 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
487 shortcuts_pane_model_filter_drag_source_iface_init))
489 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
490 GtkTreeModel *child_model,
495 GtkTreeModelSort parent;
497 GtkFileChooserDefault *impl;
501 GtkTreeModelSortClass parent_class;
502 } RecentModelSortClass;
504 #define RECENT_MODEL_SORT_TYPE (_recent_model_sort_get_type ())
505 #define RECENT_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECENT_MODEL_SORT_TYPE, RecentModelSort))
507 static void recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
509 G_DEFINE_TYPE_WITH_CODE (RecentModelSort,
511 GTK_TYPE_TREE_MODEL_SORT,
512 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
513 recent_model_sort_drag_source_iface_init));
515 static GtkTreeModel *recent_model_sort_new (GtkFileChooserDefault *impl,
516 GtkTreeModel *child_model);
520 GtkTreeModelSort parent;
522 GtkFileChooserDefault *impl;
526 GtkTreeModelSortClass parent_class;
527 } SearchModelSortClass;
529 #define SEARCH_MODEL_SORT_TYPE (_search_model_sort_get_type ())
530 #define SEARCH_MODEL_SORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEARCH_MODEL_SORT_TYPE, SearchModelSort))
532 static void search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface);
534 G_DEFINE_TYPE_WITH_CODE (SearchModelSort,
536 GTK_TYPE_TREE_MODEL_SORT,
537 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
538 search_model_sort_drag_source_iface_init));
540 static GtkTreeModel *search_model_sort_new (GtkFileChooserDefault *impl,
541 GtkTreeModel *child_model);
545 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
546 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
547 gtk_file_chooser_default_iface_init)
548 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
549 gtk_file_chooser_embed_default_iface_init));
552 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
554 static const guint quick_bookmark_keyvals[10] = {
555 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
557 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
558 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
559 GtkBindingSet *binding_set;
562 gobject_class->finalize = gtk_file_chooser_default_finalize;
563 gobject_class->constructor = gtk_file_chooser_default_constructor;
564 gobject_class->set_property = gtk_file_chooser_default_set_property;
565 gobject_class->get_property = gtk_file_chooser_default_get_property;
566 gobject_class->dispose = gtk_file_chooser_default_dispose;
568 widget_class->show_all = gtk_file_chooser_default_show_all;
569 widget_class->realize = gtk_file_chooser_default_realize;
570 widget_class->map = gtk_file_chooser_default_map;
571 widget_class->unmap = gtk_file_chooser_default_unmap;
572 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
573 widget_class->style_set = gtk_file_chooser_default_style_set;
574 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
575 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
577 signals[LOCATION_POPUP] =
578 g_signal_new_class_handler (I_("location-popup"),
579 G_OBJECT_CLASS_TYPE (class),
580 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
581 G_CALLBACK (location_popup_handler),
583 _gtk_marshal_VOID__STRING,
584 G_TYPE_NONE, 1, G_TYPE_STRING);
586 signals[LOCATION_POPUP_ON_PASTE] =
587 g_signal_new_class_handler (I_("location-popup-on-paste"),
588 G_OBJECT_CLASS_TYPE (class),
589 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
590 G_CALLBACK (location_popup_on_paste_handler),
592 _gtk_marshal_VOID__VOID,
595 signals[LOCATION_TOGGLE_POPUP] =
596 g_signal_new_class_handler (I_("location-toggle-popup"),
597 G_OBJECT_CLASS_TYPE (class),
598 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
599 G_CALLBACK (location_toggle_popup_handler),
601 _gtk_marshal_VOID__VOID,
605 g_signal_new_class_handler (I_("up-folder"),
606 G_OBJECT_CLASS_TYPE (class),
607 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
608 G_CALLBACK (up_folder_handler),
610 _gtk_marshal_VOID__VOID,
613 signals[DOWN_FOLDER] =
614 g_signal_new_class_handler (I_("down-folder"),
615 G_OBJECT_CLASS_TYPE (class),
616 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
617 G_CALLBACK (down_folder_handler),
619 _gtk_marshal_VOID__VOID,
622 signals[HOME_FOLDER] =
623 g_signal_new_class_handler (I_("home-folder"),
624 G_OBJECT_CLASS_TYPE (class),
625 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
626 G_CALLBACK (home_folder_handler),
628 _gtk_marshal_VOID__VOID,
631 signals[DESKTOP_FOLDER] =
632 g_signal_new_class_handler (I_("desktop-folder"),
633 G_OBJECT_CLASS_TYPE (class),
634 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
635 G_CALLBACK (desktop_folder_handler),
637 _gtk_marshal_VOID__VOID,
640 signals[QUICK_BOOKMARK] =
641 g_signal_new_class_handler (I_("quick-bookmark"),
642 G_OBJECT_CLASS_TYPE (class),
643 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
644 G_CALLBACK (quick_bookmark_handler),
646 _gtk_marshal_VOID__INT,
647 G_TYPE_NONE, 1, G_TYPE_INT);
649 signals[SHOW_HIDDEN] =
650 g_signal_new_class_handler (I_("show-hidden"),
651 G_OBJECT_CLASS_TYPE (class),
652 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
653 G_CALLBACK (show_hidden_handler),
655 _gtk_marshal_VOID__VOID,
658 signals[SEARCH_SHORTCUT] =
659 g_signal_new_class_handler (I_("search-shortcut"),
660 G_OBJECT_CLASS_TYPE (class),
661 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
662 G_CALLBACK (search_shortcut_handler),
664 _gtk_marshal_VOID__VOID,
667 signals[RECENT_SHORTCUT] =
668 g_signal_new_class_handler (I_("recent-shortcut"),
669 G_OBJECT_CLASS_TYPE (class),
670 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
671 G_CALLBACK (recent_shortcut_handler),
673 _gtk_marshal_VOID__VOID,
676 binding_set = gtk_binding_set_by_class (class);
678 gtk_binding_entry_add_signal (binding_set,
679 GDK_l, GDK_CONTROL_MASK,
680 "location-toggle-popup",
683 gtk_binding_entry_add_signal (binding_set,
686 1, G_TYPE_STRING, "/");
687 gtk_binding_entry_add_signal (binding_set,
690 1, G_TYPE_STRING, "/");
693 gtk_binding_entry_add_signal (binding_set,
696 1, G_TYPE_STRING, "~");
699 gtk_binding_entry_add_signal (binding_set,
700 GDK_v, GDK_CONTROL_MASK,
701 "location-popup-on-paste",
703 gtk_binding_entry_add_signal (binding_set,
704 GDK_Up, GDK_MOD1_MASK,
707 gtk_binding_entry_add_signal (binding_set,
711 gtk_binding_entry_add_signal (binding_set,
712 GDK_KP_Up, GDK_MOD1_MASK,
716 gtk_binding_entry_add_signal (binding_set,
717 GDK_Down, GDK_MOD1_MASK,
720 gtk_binding_entry_add_signal (binding_set,
721 GDK_KP_Down, GDK_MOD1_MASK,
725 gtk_binding_entry_add_signal (binding_set,
726 GDK_Home, GDK_MOD1_MASK,
729 gtk_binding_entry_add_signal (binding_set,
730 GDK_KP_Home, GDK_MOD1_MASK,
733 gtk_binding_entry_add_signal (binding_set,
734 GDK_d, GDK_MOD1_MASK,
737 gtk_binding_entry_add_signal (binding_set,
738 GDK_h, GDK_CONTROL_MASK,
741 gtk_binding_entry_add_signal (binding_set,
742 GDK_s, GDK_MOD1_MASK,
745 gtk_binding_entry_add_signal (binding_set,
746 GDK_r, GDK_MOD1_MASK,
750 for (i = 0; i < 10; i++)
751 gtk_binding_entry_add_signal (binding_set,
752 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
756 _gtk_file_chooser_install_properties (gobject_class);
760 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
762 iface->select_file = gtk_file_chooser_default_select_file;
763 iface->unselect_file = gtk_file_chooser_default_unselect_file;
764 iface->select_all = gtk_file_chooser_default_select_all;
765 iface->unselect_all = gtk_file_chooser_default_unselect_all;
766 iface->get_files = gtk_file_chooser_default_get_files;
767 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
768 iface->get_file_system = gtk_file_chooser_default_get_file_system;
769 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
770 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
771 iface->set_current_name = gtk_file_chooser_default_set_current_name;
772 iface->add_filter = gtk_file_chooser_default_add_filter;
773 iface->remove_filter = gtk_file_chooser_default_remove_filter;
774 iface->list_filters = gtk_file_chooser_default_list_filters;
775 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
776 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
777 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
781 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
783 iface->get_default_size = gtk_file_chooser_default_get_default_size;
784 iface->should_respond = gtk_file_chooser_default_should_respond;
785 iface->initial_focus = gtk_file_chooser_default_initial_focus;
789 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
791 profile_start ("start", NULL);
792 #ifdef PROFILE_FILE_CHOOSER
793 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
795 impl->local_only = TRUE;
796 impl->preview_widget_active = TRUE;
797 impl->use_preview_label = TRUE;
798 impl->select_multiple = FALSE;
799 impl->show_hidden = FALSE;
800 impl->show_size_column = TRUE;
801 impl->icon_size = FALLBACK_ICON_SIZE;
802 impl->load_state = LOAD_EMPTY;
803 impl->reload_state = RELOAD_EMPTY;
804 impl->pending_select_files = NULL;
805 impl->location_mode = LOCATION_MODE_PATH_BAR;
806 impl->operation_mode = OPERATION_MODE_BROWSE;
807 impl->sort_column = FILE_LIST_COL_NAME;
808 impl->sort_order = GTK_SORT_ASCENDING;
809 impl->recent_manager = gtk_recent_manager_get_default ();
810 impl->create_folders = TRUE;
812 gtk_box_set_spacing (GTK_BOX (impl), 12);
814 set_file_system_backend (impl);
816 profile_end ("end", NULL);
819 /* Frees the data columns for the specified iter in the shortcuts model*/
821 shortcuts_free_row_data (GtkFileChooserDefault *impl,
825 ShortcutType shortcut_type;
826 GCancellable *cancellable;
828 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
829 SHORTCUTS_COL_DATA, &col_data,
830 SHORTCUTS_COL_TYPE, &shortcut_type,
831 SHORTCUTS_COL_CANCELLABLE, &cancellable,
835 g_cancellable_cancel (cancellable);
837 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
838 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
842 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
844 GtkFileSystemVolume *volume;
847 _gtk_file_system_volume_free (volume);
853 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
856 g_object_unref (file);
860 /* Frees all the data columns in the shortcuts model */
862 shortcuts_free (GtkFileChooserDefault *impl)
866 if (!impl->shortcuts_model)
869 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
872 shortcuts_free_row_data (impl, &iter);
874 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
876 g_object_unref (impl->shortcuts_model);
877 impl->shortcuts_model = NULL;
881 pending_select_files_free (GtkFileChooserDefault *impl)
883 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
884 g_slist_free (impl->pending_select_files);
885 impl->pending_select_files = NULL;
889 pending_select_files_add (GtkFileChooserDefault *impl,
892 impl->pending_select_files =
893 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
896 /* Used from gtk_tree_selection_selected_foreach() */
898 store_selection_foreach (GtkTreeModel *model,
903 GtkFileChooserDefault *impl;
904 GtkTreeIter child_iter;
907 impl = GTK_FILE_CHOOSER_DEFAULT (data);
909 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
911 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
912 pending_select_files_add (impl, file);
915 /* Stores the current selection in the list of paths to select; this is used to
916 * preserve the selection when reloading the current folder.
919 pending_select_files_store_selection (GtkFileChooserDefault *impl)
921 GtkTreeSelection *selection;
923 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
924 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
928 gtk_file_chooser_default_finalize (GObject *object)
930 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
933 unset_file_system_backend (impl);
935 if (impl->shortcuts_pane_filter_model)
936 g_object_unref (impl->shortcuts_pane_filter_model);
938 if (impl->shortcuts_combo_filter_model)
939 g_object_unref (impl->shortcuts_combo_filter_model);
941 shortcuts_free (impl);
943 g_free (impl->browse_files_last_selected_name);
945 for (l = impl->filters; l; l = l->next)
947 GtkFileFilter *filter;
949 filter = GTK_FILE_FILTER (l->data);
950 g_object_unref (filter);
952 g_slist_free (impl->filters);
954 if (impl->current_filter)
955 g_object_unref (impl->current_filter);
957 if (impl->current_volume_file)
958 g_object_unref (impl->current_volume_file);
960 if (impl->current_folder)
961 g_object_unref (impl->current_folder);
963 if (impl->preview_file)
964 g_object_unref (impl->preview_file);
966 if (impl->browse_path_bar_size_group)
967 g_object_unref (impl->browse_path_bar_size_group);
969 load_remove_timer (impl);
971 /* Free all the Models we have */
972 if (impl->browse_files_model)
973 g_object_unref (impl->browse_files_model);
975 if (impl->sort_model)
976 g_object_unref (impl->sort_model);
978 search_clear_model (impl, FALSE);
979 recent_clear_model (impl, FALSE);
981 g_free (impl->preview_display_name);
983 g_free (impl->edited_new_text);
985 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
988 /* Shows an error dialog set as transient for the specified window */
990 error_message_with_parent (GtkWindow *parent,
996 dialog = gtk_message_dialog_new (parent,
997 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1002 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1005 if (parent && parent->group)
1006 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
1008 gtk_dialog_run (GTK_DIALOG (dialog));
1009 gtk_widget_destroy (dialog);
1012 /* Returns a toplevel GtkWindow, or NULL if none */
1014 get_toplevel (GtkWidget *widget)
1016 GtkWidget *toplevel;
1018 toplevel = gtk_widget_get_toplevel (widget);
1019 if (!GTK_WIDGET_TOPLEVEL (toplevel))
1022 return GTK_WINDOW (toplevel);
1025 /* Shows an error dialog for the file chooser */
1027 error_message (GtkFileChooserDefault *impl,
1031 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1034 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1036 error_dialog (GtkFileChooserDefault *impl,
1047 uri = g_file_get_uri (file);
1048 text = g_strdup_printf (msg, uri);
1049 error_message (impl, text, error->message);
1052 g_error_free (error);
1056 /* Displays an error message about not being able to get information for a file.
1057 * Frees the GError as well.
1060 error_getting_info_dialog (GtkFileChooserDefault *impl,
1065 _("Could not retrieve information about the file"),
1069 /* Shows an error dialog about not being able to add a bookmark */
1071 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1076 _("Could not add a bookmark"),
1080 /* Shows an error dialog about not being able to remove a bookmark */
1082 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1087 _("Could not remove bookmark"),
1091 /* Shows an error dialog about not being able to create a folder */
1093 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1098 _("The folder could not be created"),
1102 /* Shows an error about not being able to create a folder because a file with
1103 * the same name is already there.
1106 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1111 _("The folder could not be created, as a file with the same "
1112 "name already exists. Try using a different name for the "
1113 "folder, or rename the file first."),
1117 /* Shows an error dialog about not being able to create a filename */
1119 error_building_filename_dialog (GtkFileChooserDefault *impl,
1122 error_dialog (impl, _("Invalid file name"),
1126 /* Shows an error dialog when we cannot switch to a folder */
1128 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1132 error_dialog (impl, _("The folder contents could not be displayed"),
1136 /* Changes folders, displaying an error dialog if this fails */
1138 change_folder_and_display_error (GtkFileChooserDefault *impl,
1140 gboolean clear_entry)
1145 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1147 /* We copy the path because of this case:
1149 * list_row_activated()
1150 * fetches path from model; path belongs to the model (*)
1151 * calls change_folder_and_display_error()
1152 * calls gtk_file_chooser_set_current_folder_file()
1153 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1157 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1160 error_changing_folder_dialog (impl, file, error);
1166 emit_default_size_changed (GtkFileChooserDefault *impl)
1168 profile_msg (" emit default-size-changed start", NULL);
1169 g_signal_emit_by_name (impl, "default-size-changed");
1170 profile_msg (" emit default-size-changed end", NULL);
1174 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1176 if (impl->use_preview_label)
1178 if (!impl->preview_label)
1180 impl->preview_label = gtk_label_new (impl->preview_display_name);
1181 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1182 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1183 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1184 gtk_widget_show (impl->preview_label);
1189 if (impl->preview_label)
1191 gtk_widget_destroy (impl->preview_label);
1192 impl->preview_label = NULL;
1196 if (impl->preview_widget_active && impl->preview_widget)
1197 gtk_widget_show (impl->preview_box);
1199 gtk_widget_hide (impl->preview_box);
1201 if (!GTK_WIDGET_MAPPED (impl))
1202 emit_default_size_changed (impl);
1206 set_preview_widget (GtkFileChooserDefault *impl,
1207 GtkWidget *preview_widget)
1209 if (preview_widget == impl->preview_widget)
1212 if (impl->preview_widget)
1213 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1214 impl->preview_widget);
1216 impl->preview_widget = preview_widget;
1217 if (impl->preview_widget)
1219 gtk_widget_show (impl->preview_widget);
1220 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1221 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1222 impl->preview_widget,
1223 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1226 update_preview_widget_visibility (impl);
1229 /* Renders a "Search" icon at an appropriate size for a tree view */
1231 render_search_icon (GtkFileChooserDefault *impl)
1233 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1237 render_recent_icon (GtkFileChooserDefault *impl)
1239 GtkIconTheme *theme;
1242 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1243 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1245 theme = gtk_icon_theme_get_default ();
1247 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1253 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1259 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1260 struct ReloadIconsData
1262 GtkFileChooserDefault *impl;
1263 GtkTreeRowReference *row_ref;
1267 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1269 const GError *error,
1275 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1276 struct ReloadIconsData *data = user_data;
1278 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1281 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1283 if (cancelled || error)
1286 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1288 path = gtk_tree_row_reference_get_path (data->row_ref);
1289 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1290 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1291 SHORTCUTS_COL_PIXBUF, pixbuf,
1293 gtk_tree_path_free (path);
1296 g_object_unref (pixbuf);
1299 gtk_tree_row_reference_free (data->row_ref);
1300 g_object_unref (data->impl);
1303 g_object_unref (cancellable);
1307 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1312 profile_start ("start", NULL);
1314 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1317 for (l = impl->reload_icon_cancellables; l; l = l->next)
1319 GCancellable *cancellable = G_CANCELLABLE (l->data);
1320 g_cancellable_cancel (cancellable);
1322 g_slist_free (impl->reload_icon_cancellables);
1323 impl->reload_icon_cancellables = NULL;
1328 ShortcutType shortcut_type;
1329 gboolean pixbuf_visible;
1332 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1333 SHORTCUTS_COL_DATA, &data,
1334 SHORTCUTS_COL_TYPE, &shortcut_type,
1335 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1341 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1343 GtkFileSystemVolume *volume;
1346 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1347 impl->icon_size, NULL);
1349 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1351 if (g_file_is_native (G_FILE (data)))
1354 struct ReloadIconsData *info;
1355 GtkTreePath *tree_path;
1356 GCancellable *cancellable;
1360 info = g_new0 (struct ReloadIconsData, 1);
1361 info->impl = g_object_ref (impl);
1362 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1363 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1364 gtk_tree_path_free (tree_path);
1366 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1368 shortcuts_reload_icons_get_info_cb,
1370 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1374 GtkIconTheme *icon_theme;
1376 /* Don't call get_info for remote paths to avoid latency and
1378 * If we switch to a better bookmarks file format (XBEL), we
1379 * should use mime info to get a better icon.
1381 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1382 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1383 impl->icon_size, 0, NULL);
1386 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1388 pixbuf = render_search_icon (impl);
1390 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1392 pixbuf = render_recent_icon (impl);
1395 gtk_list_store_set (impl->shortcuts_model, &iter,
1396 SHORTCUTS_COL_PIXBUF, pixbuf,
1400 g_object_unref (pixbuf);
1404 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1408 profile_end ("end", NULL);
1412 shortcuts_find_folder (GtkFileChooserDefault *impl,
1415 GtkTreeSelection *selection;
1419 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1421 g_assert (folder != NULL);
1422 pos = shortcut_find_position (impl, folder);
1425 gtk_tree_selection_unselect_all (selection);
1429 path = gtk_tree_path_new_from_indices (pos, -1);
1430 gtk_tree_selection_select_path (selection, path);
1431 gtk_tree_path_free (path);
1434 /* If a shortcut corresponds to the current folder, selects it */
1436 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1438 shortcuts_find_folder (impl, impl->current_folder);
1441 /* Removes the specified number of rows from the shortcuts list */
1443 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1449 path = gtk_tree_path_new_from_indices (start_row, -1);
1451 for (; n_rows; n_rows--)
1455 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1456 g_assert_not_reached ();
1458 shortcuts_free_row_data (impl, &iter);
1459 gtk_list_store_remove (impl->shortcuts_model, &iter);
1462 gtk_tree_path_free (path);
1466 shortcuts_update_count (GtkFileChooserDefault *impl,
1467 ShortcutsIndex type,
1472 case SHORTCUTS_HOME:
1474 impl->has_home = FALSE;
1476 impl->has_home = TRUE;
1479 case SHORTCUTS_DESKTOP:
1481 impl->has_desktop = FALSE;
1483 impl->has_desktop = TRUE;
1486 case SHORTCUTS_VOLUMES:
1487 impl->num_volumes += value;
1490 case SHORTCUTS_SHORTCUTS:
1491 impl->num_shortcuts += value;
1494 case SHORTCUTS_BOOKMARKS:
1495 impl->num_bookmarks += value;
1498 case SHORTCUTS_CURRENT_FOLDER:
1500 impl->shortcuts_current_folder_active = FALSE;
1502 impl->shortcuts_current_folder_active = TRUE;
1511 struct ShortcutsInsertRequest
1513 GtkFileChooserDefault *impl;
1517 GtkTreeRowReference *row_ref;
1518 ShortcutsIndex type;
1524 get_file_info_finished (GCancellable *cancellable,
1526 const GError *error,
1530 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1534 GCancellable *model_cancellable;
1535 struct ShortcutsInsertRequest *request = data;
1537 path = gtk_tree_row_reference_get_path (request->row_ref);
1539 /* Handle doesn't exist anymore in the model */
1542 pos = gtk_tree_path_get_indices (path)[0];
1543 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1545 gtk_tree_path_free (path);
1547 /* validate cancellable, else goto out */
1548 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1549 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1551 if (cancellable != model_cancellable)
1554 /* set the cancellable to NULL in the model (we unref later on) */
1555 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1556 SHORTCUTS_COL_CANCELLABLE, NULL,
1564 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1565 shortcuts_update_count (request->impl, request->type, -1);
1567 if (request->type == SHORTCUTS_HOME)
1571 home = g_file_new_for_path (g_get_home_dir ());
1572 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1573 g_object_unref (home);
1575 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1577 /* Remove the current folder separator */
1578 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1579 shortcuts_remove_rows (request->impl, separator_pos, 1);
1585 if (!request->label_copy)
1586 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1587 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1588 request->impl->icon_size);
1590 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1591 SHORTCUTS_COL_PIXBUF, pixbuf,
1592 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1593 SHORTCUTS_COL_NAME, request->label_copy,
1594 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1595 SHORTCUTS_COL_REMOVABLE, request->removable,
1598 if (request->impl->shortcuts_pane_filter_model)
1599 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1601 if (request->impl->shortcuts_combo_filter_model)
1602 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1604 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1605 request->impl->save_folder_combo != NULL)
1607 /* The current folder is updated via _activate_iter(), don't
1608 * have save_folder_combo_changed_cb() call _activate_iter()
1611 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1612 G_CALLBACK (save_folder_combo_changed_cb),
1615 if (request->impl->has_search)
1618 if (request->impl->has_recent)
1621 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1622 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1623 G_CALLBACK (save_folder_combo_changed_cb),
1628 g_object_unref (pixbuf);
1631 g_object_unref (request->impl);
1632 g_object_unref (request->file);
1633 gtk_tree_row_reference_free (request->row_ref);
1634 g_free (request->label_copy);
1637 g_object_unref (cancellable);
1640 /* FIXME: GtkFileSystem needs a function to split a remote path
1641 * into hostname and path components, or maybe just have a
1642 * gtk_file_system_path_get_display_name().
1644 * This function is also used in gtkfilechooserbutton.c
1647 _gtk_file_chooser_label_for_file (GFile *file)
1649 const gchar *path, *start, *end, *p;
1650 gchar *uri, *host, *label;
1652 uri = g_file_get_uri (file);
1654 start = strstr (uri, "://");
1658 path = strchr (start, '/');
1663 end = uri + strlen (uri);
1667 /* strip username */
1668 p = strchr (start, '@');
1672 p = strchr (start, ':');
1676 host = g_strndup (start, end - start);
1678 /* Translators: the first string is a path and the second string
1679 * is a hostname. Nautilus and the panel contain the same string
1682 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1688 label = g_strdup (uri);
1696 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1697 * inserts a volume. A position of -1 indicates the end of the tree.
1700 shortcuts_insert_file (GtkFileChooserDefault *impl,
1702 ShortcutType shortcut_type,
1703 GtkFileSystemVolume *volume,
1707 ShortcutsIndex type)
1710 GdkPixbuf *pixbuf = NULL;
1711 gpointer data = NULL;
1713 GtkIconTheme *icon_theme;
1715 profile_start ("start shortcut", NULL);
1717 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1720 label_copy = _gtk_file_system_volume_get_display_name (volume);
1721 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1722 impl->icon_size, NULL);
1724 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1726 if (g_file_is_native (file))
1728 struct ShortcutsInsertRequest *request;
1729 GCancellable *cancellable;
1732 request = g_new0 (struct ShortcutsInsertRequest, 1);
1733 request->impl = g_object_ref (impl);
1734 request->file = g_object_ref (file);
1735 request->name_only = TRUE;
1736 request->removable = removable;
1738 request->type = type;
1740 request->label_copy = g_strdup (label);
1743 gtk_list_store_append (impl->shortcuts_model, &iter);
1745 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1747 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1748 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1749 gtk_tree_path_free (p);
1751 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1752 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1753 get_file_info_finished, request);
1755 gtk_list_store_set (impl->shortcuts_model, &iter,
1756 SHORTCUTS_COL_DATA, g_object_ref (file),
1757 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1758 SHORTCUTS_COL_CANCELLABLE, cancellable,
1761 shortcuts_update_count (impl, type, 1);
1767 /* Don't call get_info for remote paths to avoid latency and
1770 data = g_object_ref (file);
1772 label_copy = g_strdup (label);
1774 label_copy = _gtk_file_chooser_label_for_file (file);
1776 /* If we switch to a better bookmarks file format (XBEL), we
1777 * should use mime info to get a better icon.
1779 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1780 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1781 impl->icon_size, 0, NULL);
1786 g_assert_not_reached ();
1792 gtk_list_store_append (impl->shortcuts_model, &iter);
1794 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1796 shortcuts_update_count (impl, type, 1);
1798 gtk_list_store_set (impl->shortcuts_model, &iter,
1799 SHORTCUTS_COL_PIXBUF, pixbuf,
1800 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1801 SHORTCUTS_COL_NAME, label_copy,
1802 SHORTCUTS_COL_DATA, data,
1803 SHORTCUTS_COL_TYPE, shortcut_type,
1804 SHORTCUTS_COL_REMOVABLE, removable,
1805 SHORTCUTS_COL_CANCELLABLE, NULL,
1808 if (impl->shortcuts_pane_filter_model)
1809 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1811 if (impl->shortcuts_combo_filter_model)
1812 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1814 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1816 /* The current folder is updated via _activate_iter(), don't
1817 * have save_folder_combo_changed_cb() call _activate_iter()
1820 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1822 if (impl->has_search)
1825 if (impl->has_recent)
1828 g_signal_handlers_block_by_func (impl->save_folder_combo,
1829 G_CALLBACK (save_folder_combo_changed_cb),
1832 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1833 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1834 G_CALLBACK (save_folder_combo_changed_cb),
1838 g_free (label_copy);
1841 g_object_unref (pixbuf);
1843 profile_end ("end", NULL);
1847 shortcuts_append_search (GtkFileChooserDefault *impl)
1852 pixbuf = render_search_icon (impl);
1854 gtk_list_store_append (impl->shortcuts_model, &iter);
1855 gtk_list_store_set (impl->shortcuts_model, &iter,
1856 SHORTCUTS_COL_PIXBUF, pixbuf,
1857 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1858 SHORTCUTS_COL_NAME, _("Search"),
1859 SHORTCUTS_COL_DATA, NULL,
1860 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1861 SHORTCUTS_COL_REMOVABLE, FALSE,
1865 g_object_unref (pixbuf);
1867 impl->has_search = TRUE;
1871 shortcuts_append_recent (GtkFileChooserDefault *impl)
1876 pixbuf = render_recent_icon (impl);
1878 gtk_list_store_append (impl->shortcuts_model, &iter);
1879 gtk_list_store_set (impl->shortcuts_model, &iter,
1880 SHORTCUTS_COL_PIXBUF, pixbuf,
1881 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1882 SHORTCUTS_COL_NAME, _("Recently Used"),
1883 SHORTCUTS_COL_DATA, NULL,
1884 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1885 SHORTCUTS_COL_REMOVABLE, FALSE,
1889 g_object_unref (pixbuf);
1891 impl->has_recent = TRUE;
1894 /* Appends an item for the user's home directory to the shortcuts model */
1896 shortcuts_append_home (GtkFileChooserDefault *impl)
1898 const char *home_path;
1901 profile_start ("start", NULL);
1903 home_path = g_get_home_dir ();
1904 if (home_path == NULL)
1906 profile_end ("end - no home directory!?", NULL);
1910 home = g_file_new_for_path (home_path);
1911 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1912 impl->has_home = TRUE;
1914 g_object_unref (home);
1916 profile_end ("end", NULL);
1919 /* Appends the ~/Desktop directory to the shortcuts model */
1921 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1926 profile_start ("start", NULL);
1928 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1929 /* "To disable a directory, point it to the homedir."
1930 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1932 if (!g_strcmp0 (name, g_get_home_dir ()))
1934 profile_end ("end", NULL);
1938 file = g_file_new_for_path (name);
1939 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1940 impl->has_desktop = TRUE;
1942 /* We do not actually pop up an error dialog if there is no desktop directory
1943 * because some people may really not want to have one.
1946 g_object_unref (file);
1948 profile_end ("end", NULL);
1951 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1953 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1960 profile_start ("start", NULL);
1962 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1965 for (; bookmarks; bookmarks = bookmarks->next)
1969 file = bookmarks->data;
1971 if (impl->local_only && !g_file_is_native (file))
1974 if (shortcut_find_position (impl, file) != -1)
1977 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1979 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1985 profile_end ("end", NULL);
1987 return num_inserted;
1990 /* Returns the index for the corresponding item in the shortcuts bar */
1992 shortcuts_get_index (GtkFileChooserDefault *impl,
1993 ShortcutsIndex where)
1999 if (where == SHORTCUTS_SEARCH)
2002 n += impl->has_search ? 1 : 0;
2004 if (where == SHORTCUTS_RECENT)
2007 n += impl->has_recent ? 1 : 0;
2009 if (where == SHORTCUTS_RECENT_SEPARATOR)
2012 n += impl->has_recent ? 1 : 0;
2014 if (where == SHORTCUTS_HOME)
2017 n += impl->has_home ? 1 : 0;
2019 if (where == SHORTCUTS_DESKTOP)
2022 n += impl->has_desktop ? 1 : 0;
2024 if (where == SHORTCUTS_VOLUMES)
2027 n += impl->num_volumes;
2029 if (where == SHORTCUTS_SHORTCUTS)
2032 n += impl->num_shortcuts;
2034 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2037 /* If there are no bookmarks there won't be a separator */
2038 n += (impl->num_bookmarks > 0) ? 1 : 0;
2040 if (where == SHORTCUTS_BOOKMARKS)
2043 n += impl->num_bookmarks;
2045 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2050 if (where == SHORTCUTS_CURRENT_FOLDER)
2053 g_assert_not_reached ();
2060 /* Adds all the file system volumes to the shortcuts model */
2062 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2067 gboolean old_changing_folders;
2069 profile_start ("start", NULL);
2071 old_changing_folders = impl->changing_folder;
2072 impl->changing_folder = TRUE;
2074 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2075 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2076 impl->num_volumes = 0;
2078 list = _gtk_file_system_list_volumes (impl->file_system);
2082 for (l = list; l; l = l->next)
2084 GtkFileSystemVolume *volume;
2088 if (impl->local_only)
2090 if (_gtk_file_system_volume_is_mounted (volume))
2093 gboolean base_is_native = TRUE;
2095 base_file = _gtk_file_system_volume_get_root (volume);
2096 if (base_file != NULL)
2098 base_is_native = g_file_is_native (base_file);
2099 g_object_unref (base_file);
2102 if (!base_is_native)
2107 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2111 impl->num_volumes = n;
2112 g_slist_free (list);
2114 if (impl->shortcuts_pane_filter_model)
2115 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2117 if (impl->shortcuts_combo_filter_model)
2118 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2120 impl->changing_folder = old_changing_folders;
2122 profile_end ("end", NULL);
2125 /* Inserts a separator node in the shortcuts list */
2127 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2128 ShortcutsIndex where)
2132 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2133 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2134 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2136 gtk_list_store_insert (impl->shortcuts_model, &iter,
2137 shortcuts_get_index (impl, where));
2138 gtk_list_store_set (impl->shortcuts_model, &iter,
2139 SHORTCUTS_COL_PIXBUF, NULL,
2140 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2141 SHORTCUTS_COL_NAME, NULL,
2142 SHORTCUTS_COL_DATA, NULL,
2143 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2147 /* Updates the list of bookmarks */
2149 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2152 gboolean old_changing_folders;
2154 GFile *list_selected = NULL;
2155 GFile *combo_selected = NULL;
2156 ShortcutType shortcut_type;
2159 profile_start ("start", NULL);
2161 old_changing_folders = impl->changing_folder;
2162 impl->changing_folder = TRUE;
2164 if (shortcuts_get_selected (impl, &iter))
2166 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2168 SHORTCUTS_COL_DATA, &col_data,
2169 SHORTCUTS_COL_TYPE, &shortcut_type,
2172 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2173 list_selected = g_object_ref (col_data);
2176 if (impl->save_folder_combo &&
2177 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2180 GtkTreeIter child_iter;
2182 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2185 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2187 SHORTCUTS_COL_DATA, &col_data,
2188 SHORTCUTS_COL_TYPE, &shortcut_type,
2191 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2192 combo_selected = g_object_ref (col_data);
2195 if (impl->num_bookmarks > 0)
2196 shortcuts_remove_rows (impl,
2197 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2198 impl->num_bookmarks + 1);
2200 impl->num_bookmarks = 0;
2201 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2203 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2204 shortcuts_append_bookmarks (impl, bookmarks);
2205 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2206 g_slist_free (bookmarks);
2208 if (impl->num_bookmarks == 0)
2209 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2211 if (impl->shortcuts_pane_filter_model)
2212 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2214 if (impl->shortcuts_combo_filter_model)
2215 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2219 shortcuts_find_folder (impl, list_selected);
2220 g_object_unref (list_selected);
2227 pos = shortcut_find_position (impl, combo_selected);
2230 if (impl->has_search)
2233 if (impl->has_recent)
2236 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2239 g_object_unref (combo_selected);
2242 impl->changing_folder = old_changing_folders;
2244 profile_end ("end", NULL);
2247 /* Appends a separator and a row to the shortcuts list for the current folder */
2249 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2254 g_assert (!impl->shortcuts_current_folder_active);
2258 g_assert (impl->current_folder != NULL);
2260 pos = shortcut_find_position (impl, impl->current_folder);
2263 GtkFileSystemVolume *volume;
2268 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2272 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2274 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2276 base_file = _gtk_file_system_volume_get_root (volume);
2280 if (base_file && g_file_equal (base_file, impl->current_folder))
2281 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2283 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2286 g_object_unref (base_file);
2288 else if (impl->save_folder_combo != NULL)
2290 if (impl->has_search)
2293 if (impl->has_recent)
2294 pos -= 2; /* + separator */
2296 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2300 /* Updates the current folder row in the shortcuts model */
2302 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2306 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2308 if (impl->shortcuts_current_folder_active)
2310 shortcuts_remove_rows (impl, pos, 2);
2311 impl->shortcuts_current_folder_active = FALSE;
2314 shortcuts_add_current_folder (impl);
2317 /* Filter function used for the shortcuts filter model */
2319 shortcuts_pane_filter_cb (GtkTreeModel *model,
2323 GtkFileChooserDefault *impl;
2327 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2329 path = gtk_tree_model_get_path (model, iter);
2333 pos = *gtk_tree_path_get_indices (path);
2334 gtk_tree_path_free (path);
2336 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2339 /* Creates the list model for shortcuts */
2341 shortcuts_model_create (GtkFileChooserDefault *impl)
2343 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2344 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2345 GDK_TYPE_PIXBUF, /* pixbuf */
2346 G_TYPE_STRING, /* name */
2347 G_TYPE_POINTER, /* path or volume */
2348 G_TYPE_INT, /* ShortcutType */
2349 G_TYPE_BOOLEAN, /* removable */
2350 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2351 G_TYPE_POINTER); /* GCancellable */
2353 if (search_is_possible (impl))
2355 shortcuts_append_search (impl);
2358 if (impl->recent_manager)
2360 shortcuts_append_recent (impl);
2361 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2364 if (impl->file_system)
2366 shortcuts_append_home (impl);
2367 shortcuts_append_desktop (impl);
2368 shortcuts_add_volumes (impl);
2371 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2372 GTK_TREE_MODEL (impl->shortcuts_model),
2375 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2376 shortcuts_pane_filter_cb,
2381 /* Callback used when the "New Folder" button is clicked */
2383 new_folder_button_clicked (GtkButton *button,
2384 GtkFileChooserDefault *impl)
2389 if (!impl->browse_files_model)
2390 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2392 /* Prevent button from being clicked twice */
2393 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2395 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2397 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2398 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2399 path, impl->list_name_column,
2402 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2403 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2405 impl->list_name_column,
2408 gtk_tree_path_free (path);
2411 /* Idle handler for creating a new folder after editing its name cell, or for
2412 * canceling the editing.
2415 edited_idle_cb (GtkFileChooserDefault *impl)
2417 GDK_THREADS_ENTER ();
2419 g_source_destroy (impl->edited_idle);
2420 impl->edited_idle = NULL;
2422 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2423 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2425 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2427 if (impl->edited_new_text /* not cancelled? */
2428 && (strlen (impl->edited_new_text) != 0)
2429 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2431 GError *error = NULL;
2434 file = g_file_get_child_for_display_name (impl->current_folder,
2435 impl->edited_new_text,
2439 GError *error = NULL;
2441 if (g_file_make_directory (file, NULL, &error))
2442 change_folder_and_display_error (impl, file, FALSE);
2444 error_creating_folder_dialog (impl, file, error);
2446 g_object_unref (file);
2449 error_creating_folder_dialog (impl, file, error);
2451 g_free (impl->edited_new_text);
2452 impl->edited_new_text = NULL;
2455 GDK_THREADS_LEAVE ();
2461 queue_edited_idle (GtkFileChooserDefault *impl,
2462 const gchar *new_text)
2464 /* We create the folder in an idle handler so that we don't modify the tree
2468 if (!impl->edited_idle)
2470 impl->edited_idle = g_idle_source_new ();
2471 g_source_set_closure (impl->edited_idle,
2472 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2474 g_source_attach (impl->edited_idle, NULL);
2477 g_free (impl->edited_new_text);
2478 impl->edited_new_text = g_strdup (new_text);
2481 /* Callback used from the text cell renderer when the new folder is named */
2483 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2485 const gchar *new_text,
2486 GtkFileChooserDefault *impl)
2488 /* work around bug #154921 */
2489 g_object_set (cell_renderer_text,
2490 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2491 queue_edited_idle (impl, new_text);
2494 /* Callback used from the text cell renderer when the new folder edition gets
2498 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2499 GtkFileChooserDefault *impl)
2501 /* work around bug #154921 */
2502 g_object_set (cell_renderer_text,
2503 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2504 queue_edited_idle (impl, NULL);
2507 /* Creates the widgets for the filter combo box */
2509 filter_create (GtkFileChooserDefault *impl)
2511 impl->filter_combo = gtk_combo_box_new_text ();
2512 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2514 g_signal_connect (impl->filter_combo, "changed",
2515 G_CALLBACK (filter_combo_changed), impl);
2517 gtk_widget_set_tooltip_text (impl->filter_combo,
2518 _("Select which types of files are shown"));
2520 return impl->filter_combo;
2524 button_new (GtkFileChooserDefault *impl,
2526 const char *stock_id,
2534 button = gtk_button_new_with_mnemonic (text);
2535 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2536 gtk_button_set_image (GTK_BUTTON (button), image);
2538 gtk_widget_set_sensitive (button, sensitive);
2539 g_signal_connect (button, "clicked", callback, impl);
2542 gtk_widget_show (button);
2547 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2549 shortcut_find_position (GtkFileChooserDefault *impl,
2554 int current_folder_separator_idx;
2556 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2559 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2562 /* FIXME: is this still needed? */
2563 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2567 for (i = 0; i < current_folder_separator_idx; i++)
2570 ShortcutType shortcut_type;
2572 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2573 SHORTCUTS_COL_DATA, &col_data,
2574 SHORTCUTS_COL_TYPE, &shortcut_type,
2579 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2581 GtkFileSystemVolume *volume;
2586 base_file = _gtk_file_system_volume_get_root (volume);
2588 exists = base_file && g_file_equal (file, base_file);
2591 g_object_unref (base_file);
2596 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2600 model_file = col_data;
2602 if (model_file && g_file_equal (model_file, file))
2607 if (i < current_folder_separator_idx - 1)
2609 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2610 g_assert_not_reached ();
2617 /* Tries to add a bookmark from a path name */
2619 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2625 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2627 if (shortcut_find_position (impl, file) != -1)
2631 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2633 error_adding_bookmark_dialog (impl, file, error);
2641 add_bookmark_foreach_cb (GtkTreeModel *model,
2646 GtkFileChooserDefault *impl;
2647 GtkFileSystemModel *fs_model;
2648 GtkTreeIter child_iter;
2651 impl = (GtkFileChooserDefault *) data;
2653 switch (impl->operation_mode)
2655 case OPERATION_MODE_BROWSE:
2656 fs_model = impl->browse_files_model;
2657 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2658 file = _gtk_file_system_model_get_file (fs_model, &child_iter);
2661 case OPERATION_MODE_SEARCH:
2662 search_get_valid_child_iter (impl, &child_iter, iter);
2663 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
2664 SEARCH_MODEL_COL_FILE, &file,
2668 case OPERATION_MODE_RECENT:
2669 recent_get_valid_child_iter (impl, &child_iter, iter);
2670 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
2671 RECENT_MODEL_COL_FILE, &file,
2676 shortcuts_add_bookmark_from_file (impl, file, -1);
2679 /* Adds a bookmark from the currently selected item in the file list */
2681 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2683 GtkTreeSelection *selection;
2685 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2687 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2688 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2690 gtk_tree_selection_selected_foreach (selection,
2691 add_bookmark_foreach_cb,
2695 /* Callback used when the "Add bookmark" button is clicked */
2697 add_bookmark_button_clicked_cb (GtkButton *button,
2698 GtkFileChooserDefault *impl)
2700 bookmarks_add_selected_folder (impl);
2703 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2704 * returns FALSE if no shortcut is selected.
2707 shortcuts_get_selected (GtkFileChooserDefault *impl,
2710 GtkTreeSelection *selection;
2711 GtkTreeIter parent_iter;
2713 if (!impl->browse_shortcuts_tree_view)
2716 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2718 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2721 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2727 /* Removes the selected bookmarks */
2729 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2737 if (!shortcuts_get_selected (impl, &iter))
2740 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2741 SHORTCUTS_COL_DATA, &col_data,
2742 SHORTCUTS_COL_REMOVABLE, &removable,
2748 g_assert (col_data != NULL);
2753 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2754 error_removing_bookmark_dialog (impl, file, error);
2757 /* Callback used when the "Remove bookmark" button is clicked */
2759 remove_bookmark_button_clicked_cb (GtkButton *button,
2760 GtkFileChooserDefault *impl)
2762 remove_selected_bookmarks (impl);
2765 struct selection_check_closure {
2766 GtkFileChooserDefault *impl;
2769 gboolean all_folders;
2772 /* Used from gtk_tree_selection_selected_foreach() */
2774 selection_check_foreach_cb (GtkTreeModel *model,
2779 struct selection_check_closure *closure;
2780 GtkTreeIter child_iter;
2785 closure->num_selected++;
2787 switch (closure->impl->operation_mode)
2789 case OPERATION_MODE_BROWSE:
2790 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2791 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2792 is_folder = info ? (_gtk_file_info_consider_as_directory (info)) : FALSE;
2795 case OPERATION_MODE_SEARCH:
2796 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2797 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2798 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
2802 case OPERATION_MODE_RECENT:
2803 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2804 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2805 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
2810 closure->all_folders = closure->all_folders && is_folder;
2811 closure->all_files = closure->all_files && !is_folder;
2814 /* Checks whether the selected items in the file list are all files or all folders */
2816 selection_check (GtkFileChooserDefault *impl,
2818 gboolean *all_files,
2819 gboolean *all_folders)
2821 struct selection_check_closure closure;
2822 GtkTreeSelection *selection;
2824 closure.impl = impl;
2825 closure.num_selected = 0;
2826 closure.all_files = TRUE;
2827 closure.all_folders = TRUE;
2829 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2830 gtk_tree_selection_selected_foreach (selection,
2831 selection_check_foreach_cb,
2834 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2837 *num_selected = closure.num_selected;
2840 *all_files = closure.all_files;
2843 *all_folders = closure.all_folders;
2846 struct get_selected_file_closure {
2847 GtkFileChooserDefault *impl;
2852 get_selected_file_foreach_cb (GtkTreeModel *model,
2857 struct get_selected_file_closure *closure;
2858 GtkTreeIter child_iter;
2862 switch (closure->impl->operation_mode)
2864 case OPERATION_MODE_BROWSE:
2865 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2866 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
2869 case OPERATION_MODE_SEARCH:
2870 search_get_valid_child_iter (closure->impl, &child_iter, iter);
2871 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->search_model), &child_iter,
2872 SEARCH_MODEL_COL_FILE, &closure->file,
2876 case OPERATION_MODE_RECENT:
2877 recent_get_valid_child_iter (closure->impl, &child_iter, iter);
2878 gtk_tree_model_get (GTK_TREE_MODEL (closure->impl->recent_model), &child_iter,
2879 RECENT_MODEL_COL_FILE, &closure->file,
2885 /* Returns a selected path from the file list */
2887 get_selected_file (GtkFileChooserDefault *impl)
2889 struct get_selected_file_closure closure;
2890 GtkTreeSelection *selection;
2892 closure.impl = impl;
2893 closure.file = NULL;
2895 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2896 gtk_tree_selection_selected_foreach (selection,
2897 get_selected_file_foreach_cb,
2900 return closure.file;
2904 GtkFileChooserDefault *impl;
2906 } UpdateTooltipData;
2909 update_tooltip (GtkTreeModel *model,
2914 UpdateTooltipData *udata = data;
2915 GtkTreeIter child_iter;
2918 if (udata->tip == NULL)
2920 const gchar *display_name;
2922 switch (udata->impl->operation_mode)
2924 case OPERATION_MODE_BROWSE:
2925 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2928 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2929 display_name = g_file_info_get_display_name (info);
2932 case OPERATION_MODE_SEARCH:
2933 search_get_valid_child_iter (udata->impl, &child_iter, iter);
2934 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->search_model), &child_iter,
2935 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
2939 case OPERATION_MODE_RECENT:
2940 recent_get_valid_child_iter (udata->impl, &child_iter, iter);
2941 gtk_tree_model_get (GTK_TREE_MODEL (udata->impl->recent_model), &child_iter,
2942 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
2947 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2953 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2954 * if there are no selected items *and* the current folder is not in the
2955 * bookmarks list. De-sensitize the button otherwise.
2958 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2961 gboolean all_folders;
2965 selection_check (impl, &num_selected, NULL, &all_folders);
2967 if (num_selected == 0)
2968 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2969 else if (num_selected == 1)
2973 file = get_selected_file (impl);
2974 active = all_folders && (shortcut_find_position (impl, file) == -1);
2977 active = all_folders;
2979 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2981 if (impl->browse_files_popup_menu_add_shortcut_item)
2982 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2983 (num_selected == 0) ? FALSE : active);
2987 if (num_selected == 0)
2988 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2989 else if (num_selected > 1)
2990 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2993 GtkTreeSelection *selection;
2994 UpdateTooltipData data;
2996 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2999 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
3003 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
3008 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
3009 * bookmark row is selected in the shortcuts tree.
3012 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
3015 gboolean removable = FALSE;
3019 if (shortcuts_get_selected (impl, &iter))
3021 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3022 SHORTCUTS_COL_REMOVABLE, &removable,
3023 SHORTCUTS_COL_NAME, &name,
3025 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
3028 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
3030 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
3032 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
3036 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3037 _("Remove the selected bookmark"));
3042 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
3045 gboolean removable = FALSE;
3047 if (impl->browse_shortcuts_popup_menu == NULL)
3050 if (shortcuts_get_selected (impl, &iter))
3051 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3052 SHORTCUTS_COL_REMOVABLE, &removable,
3055 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
3056 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
3059 /* GtkWidget::drag-begin handler for the shortcuts list. */
3061 shortcuts_drag_begin_cb (GtkWidget *widget,
3062 GdkDragContext *context,
3063 GtkFileChooserDefault *impl)
3066 impl->shortcuts_drag_context = g_object_ref (context);
3071 /* Removes the idle handler for outside drags */
3073 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
3075 if (!impl->shortcuts_drag_outside_idle)
3078 g_source_destroy (impl->shortcuts_drag_outside_idle);
3079 impl->shortcuts_drag_outside_idle = NULL;
3083 /* GtkWidget::drag-end handler for the shortcuts list. */
3085 shortcuts_drag_end_cb (GtkWidget *widget,
3086 GdkDragContext *context,
3087 GtkFileChooserDefault *impl)
3090 g_object_unref (impl->shortcuts_drag_context);
3092 shortcuts_cancel_drag_outside_idle (impl);
3094 if (!impl->shortcuts_drag_outside)
3097 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3099 impl->shortcuts_drag_outside = FALSE;
3103 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3105 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3106 GdkDragContext *context,
3107 GtkFileChooserDefault *impl)
3109 g_signal_stop_emission_by_name (widget, "drag-data-delete");
3113 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3117 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3120 GtkTreeView *tree_view;
3123 GdkPixmap *row_pixmap;
3128 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3130 /* Find the selected path and get its drag pixmap */
3132 if (!shortcuts_get_selected (impl, &iter))
3133 g_assert_not_reached ();
3135 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3137 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3138 gtk_tree_path_free (path);
3147 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3153 GdkPixmap *composite;
3154 int row_pixmap_width, row_pixmap_height;
3155 int pixbuf_width, pixbuf_height;
3156 int composite_width, composite_height;
3157 int pixbuf_x, pixbuf_y;
3158 GdkGC *gc, *mask_gc;
3160 GdkBitmap *pixbuf_mask;
3162 /* Create pixmap and mask for composite image */
3164 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3165 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3166 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3168 composite_width = MAX (row_pixmap_width, pixbuf_width);
3169 composite_height = MAX (row_pixmap_height, pixbuf_height);
3171 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3173 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3176 pixbuf_x = composite_width - pixbuf_width;
3178 pixbuf_y = (composite_height - pixbuf_height) / 2;
3180 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3181 gc = gdk_gc_new (composite);
3183 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3184 mask_gc = gdk_gc_new (mask);
3186 gdk_gc_set_foreground (mask_gc, &color);
3187 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3190 color.green = 0xffff;
3191 color.blue = 0xffff;
3192 gdk_gc_set_rgb_fg_color (gc, &color);
3193 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3195 /* Composite the row pixmap and the pixbuf */
3197 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3199 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3200 NULL, &pixbuf_mask, 128);
3201 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3204 pixbuf_width, pixbuf_height);
3205 g_object_unref (pixbuf_mask);
3207 gdk_draw_drawable (composite, gc, row_pixmap,
3210 row_pixmap_width, row_pixmap_height);
3212 gdk_gc_set_foreground (mask_gc, &color);
3213 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3215 gdk_draw_pixbuf (composite, gc, pixbuf,
3218 pixbuf_width, pixbuf_height,
3222 g_object_unref (pixbuf);
3223 g_object_unref (row_pixmap);
3225 row_pixmap = composite;
3229 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3231 gtk_tree_view_get_path_at_pos (tree_view,
3232 tree_view->priv->press_start_x,
3233 tree_view->priv->press_start_y,
3239 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3240 gdk_drawable_get_colormap (row_pixmap),
3243 tree_view->priv->press_start_x + 1,
3244 row_pixmap_y + cell_y + 1);
3246 g_object_unref (row_pixmap);
3248 g_object_unref (mask);
3251 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3252 * handler so that we can tell apart the drag_leave event that comes right
3253 * before a drag_drop, from a normal drag_leave. We don't want to set the
3254 * cursor nor the flag in the latter case.
3257 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3259 GDK_THREADS_ENTER ();
3261 shortcuts_drag_set_delete_cursor (impl, TRUE);
3262 impl->shortcuts_drag_outside = TRUE;
3264 shortcuts_cancel_drag_outside_idle (impl);
3266 GDK_THREADS_LEAVE ();
3272 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3276 shortcuts_drag_leave_cb (GtkWidget *widget,
3277 GdkDragContext *context,
3279 GtkFileChooserDefault *impl)
3282 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3284 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3285 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3286 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3288 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3292 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3294 GTK_TREE_VIEW_DROP_BEFORE);
3296 g_signal_stop_emission_by_name (widget, "drag-leave");
3299 /* Computes the appropriate row and position for dropping */
3301 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3305 GtkTreeViewDropPosition *pos)
3307 GtkTreeView *tree_view;
3308 GtkTreeViewColumn *column;
3312 int bookmarks_index;
3314 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3316 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3318 if (!gtk_tree_view_get_path_at_pos (tree_view,
3320 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3326 row = bookmarks_index + impl->num_bookmarks - 1;
3327 *path = gtk_tree_path_new_from_indices (row, -1);
3328 *pos = GTK_TREE_VIEW_DROP_AFTER;
3332 row = *gtk_tree_path_get_indices (*path);
3333 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3334 gtk_tree_path_free (*path);
3336 if (row < bookmarks_index)
3338 row = bookmarks_index;
3339 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3341 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3343 row = bookmarks_index + impl->num_bookmarks - 1;
3344 *pos = GTK_TREE_VIEW_DROP_AFTER;
3348 if (cell_y < cell.height / 2)
3349 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3351 *pos = GTK_TREE_VIEW_DROP_AFTER;
3354 *path = gtk_tree_path_new_from_indices (row, -1);
3357 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3358 * implement the destination side of DnD by hand, due to limitations in
3359 * GtkTreeView's DnD API.
3362 shortcuts_drag_motion_cb (GtkWidget *widget,
3363 GdkDragContext *context,
3367 GtkFileChooserDefault *impl)
3370 GtkTreeViewDropPosition pos;
3371 GdkDragAction action;
3374 if (gtk_drag_get_source_widget (context) == widget)
3376 shortcuts_cancel_drag_outside_idle (impl);
3378 if (impl->shortcuts_drag_outside)
3380 shortcuts_drag_set_delete_cursor (impl, FALSE);
3381 impl->shortcuts_drag_outside = FALSE;
3386 if (context->suggested_action == GDK_ACTION_COPY ||
3387 (context->actions & GDK_ACTION_COPY) != 0)
3388 action = GDK_ACTION_COPY;
3389 else if (context->suggested_action == GDK_ACTION_MOVE ||
3390 (context->actions & GDK_ACTION_MOVE) != 0)
3391 action = GDK_ACTION_MOVE;
3398 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3399 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3400 gtk_tree_path_free (path);
3404 g_signal_stop_emission_by_name (widget, "drag-motion");
3408 gdk_drag_status (context, action, time_);
3415 /* GtkWidget::drag-drop handler for the shortcuts list. */
3417 shortcuts_drag_drop_cb (GtkWidget *widget,
3418 GdkDragContext *context,
3422 GtkFileChooserDefault *impl)
3425 shortcuts_cancel_drag_outside_idle (impl);
3428 g_signal_stop_emission_by_name (widget, "drag-drop");
3432 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3434 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3435 GtkSelectionData *selection_data,
3441 uris = gtk_selection_data_get_uris (selection_data);
3445 for (i = 0; uris[i]; i++)
3451 file = g_file_new_for_uri (uri);
3453 if (shortcuts_add_bookmark_from_file (impl, file, position))
3456 g_object_unref (file);
3462 /* Reorders the selected bookmark to the specified position */
3464 shortcuts_reorder (GtkFileChooserDefault *impl,
3469 ShortcutType shortcut_type;
3472 int bookmarks_index;
3477 /* Get the selected path */
3479 if (!shortcuts_get_selected (impl, &iter))
3480 g_assert_not_reached ();
3482 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3483 old_position = *gtk_tree_path_get_indices (path);
3484 gtk_tree_path_free (path);
3486 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3487 old_position -= bookmarks_index;
3488 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3490 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3491 SHORTCUTS_COL_NAME, &name,
3492 SHORTCUTS_COL_DATA, &col_data,
3493 SHORTCUTS_COL_TYPE, &shortcut_type,
3495 g_assert (col_data != NULL);
3496 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3499 g_object_ref (file); /* removal below will free file, so we need a new ref */
3501 /* Remove the path from the old position and insert it in the new one */
3503 if (new_position > old_position)
3506 if (old_position == new_position)
3510 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3512 shortcuts_add_bookmark_from_file (impl, file, new_position);
3513 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3516 error_adding_bookmark_dialog (impl, file, error);
3520 g_object_unref (file);
3523 /* Callback used when we get the drag data for the bookmarks list. We add the
3524 * received URIs as bookmarks if they are folders.
3527 shortcuts_drag_data_received_cb (GtkWidget *widget,
3528 GdkDragContext *context,
3531 GtkSelectionData *selection_data,
3536 GtkFileChooserDefault *impl;
3537 GtkTreePath *tree_path;
3538 GtkTreeViewDropPosition tree_pos;
3540 int bookmarks_index;
3542 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3544 /* Compute position */
3546 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3548 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3549 position = *gtk_tree_path_get_indices (tree_path);
3550 gtk_tree_path_free (tree_path);
3552 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3555 g_assert (position >= bookmarks_index);
3556 position -= bookmarks_index;
3558 if (gtk_targets_include_uri (&selection_data->target, 1))
3559 shortcuts_drop_uris (impl, selection_data, position);
3560 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3561 shortcuts_reorder (impl, position);
3563 g_signal_stop_emission_by_name (widget, "drag-data-received");
3566 /* Callback used to display a tooltip in the shortcuts tree */
3568 shortcuts_query_tooltip_cb (GtkWidget *widget,
3571 gboolean keyboard_mode,
3572 GtkTooltip *tooltip,
3573 GtkFileChooserDefault *impl)
3575 GtkTreeModel *model;
3578 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3586 ShortcutType shortcut_type;
3588 gtk_tree_model_get (model, &iter,
3589 SHORTCUTS_COL_DATA, &col_data,
3590 SHORTCUTS_COL_TYPE, &shortcut_type,
3593 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3595 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3599 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3604 file = G_FILE (col_data);
3605 parse_name = g_file_get_parse_name (file);
3607 gtk_tooltip_set_text (tooltip, parse_name);
3609 g_free (parse_name);
3613 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3617 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3627 /* Callback used when the selection in the shortcuts tree changes */
3629 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3630 GtkFileChooserDefault *impl)
3633 GtkTreeIter child_iter;
3635 bookmarks_check_remove_sensitivity (impl);
3636 shortcuts_check_popup_sensitivity (impl);
3638 if (impl->changing_folder)
3641 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3643 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3646 shortcuts_activate_iter (impl, &child_iter);
3651 shortcuts_row_separator_func (GtkTreeModel *model,
3655 ShortcutType shortcut_type;
3657 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3659 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3663 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3665 GtkFileChooserDefault *impl)
3669 /* don't screw up focus switching with Tab */
3670 if (event->keyval == GDK_Tab
3671 || event->keyval == GDK_KP_Tab
3672 || event->keyval == GDK_ISO_Left_Tab
3673 || event->length < 1)
3676 if (impl->location_entry)
3677 entry = impl->location_entry;
3678 else if (impl->search_entry)
3679 entry = impl->search_entry;
3685 gtk_widget_grab_focus (entry);
3686 return gtk_widget_event (entry, (GdkEvent *) event);
3692 /* Callback used when the file list's popup menu is detached */
3694 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3697 GtkFileChooserDefault *impl;
3699 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3700 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3702 impl->browse_shortcuts_popup_menu = NULL;
3703 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3704 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3708 remove_shortcut_cb (GtkMenuItem *item,
3709 GtkFileChooserDefault *impl)
3711 remove_selected_bookmarks (impl);
3714 /* Rename the selected bookmark */
3716 rename_selected_bookmark (GtkFileChooserDefault *impl)
3720 GtkTreeViewColumn *column;
3721 GtkCellRenderer *cell;
3724 if (shortcuts_get_selected (impl, &iter))
3726 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3727 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3728 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3729 cell = g_list_nth_data (renderers, 1);
3730 g_list_free (renderers);
3731 g_object_set (cell, "editable", TRUE, NULL);
3732 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3733 path, column, cell, TRUE);
3734 gtk_tree_path_free (path);
3739 rename_shortcut_cb (GtkMenuItem *item,
3740 GtkFileChooserDefault *impl)
3742 rename_selected_bookmark (impl);
3745 /* Constructs the popup menu for the file list if needed */
3747 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3751 if (impl->browse_shortcuts_popup_menu)
3754 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3755 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3756 impl->browse_shortcuts_tree_view,
3757 shortcuts_popup_menu_detach_cb);
3759 item = gtk_image_menu_item_new_with_label (_("Remove"));
3760 impl->browse_shortcuts_popup_menu_remove_item = item;
3761 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3762 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3763 g_signal_connect (item, "activate",
3764 G_CALLBACK (remove_shortcut_cb), impl);
3765 gtk_widget_show (item);
3766 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3768 item = gtk_menu_item_new_with_label (_("Rename..."));
3769 impl->browse_shortcuts_popup_menu_rename_item = item;
3770 g_signal_connect (item, "activate",
3771 G_CALLBACK (rename_shortcut_cb), impl);
3772 gtk_widget_show (item);
3773 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3777 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3779 shortcuts_build_popup_menu (impl);
3780 shortcuts_check_popup_sensitivity (impl);
3784 popup_position_func (GtkMenu *menu,
3788 gpointer user_data);
3791 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3792 GdkEventButton *event)
3794 shortcuts_update_popup_menu (impl);
3796 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3797 NULL, NULL, NULL, NULL,
3798 event->button, event->time);
3801 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3803 popup_position_func, impl->browse_shortcuts_tree_view,
3804 0, GDK_CURRENT_TIME);
3805 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3810 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3812 shortcuts_popup_menu_cb (GtkWidget *widget,
3813 GtkFileChooserDefault *impl)
3815 shortcuts_popup_menu (impl, NULL);
3819 /* Callback used when a button is pressed on the shortcuts list.
3820 * We trap button 3 to bring up a popup menu.
3823 shortcuts_button_press_event_cb (GtkWidget *widget,
3824 GdkEventButton *event,
3825 GtkFileChooserDefault *impl)
3827 static gboolean in_press = FALSE;
3833 if (event->button != 3)
3837 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3843 shortcuts_popup_menu (impl, event);
3848 shortcuts_edited (GtkCellRenderer *cell,
3851 GtkFileChooserDefault *impl)
3857 g_object_set (cell, "editable", FALSE, NULL);
3859 path = gtk_tree_path_new_from_string (path_string);
3860 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3861 g_assert_not_reached ();
3863 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3864 SHORTCUTS_COL_DATA, &shortcut,
3866 gtk_tree_path_free (path);
3868 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3872 shortcuts_editing_canceled (GtkCellRenderer *cell,
3873 GtkFileChooserDefault *impl)
3875 g_object_set (cell, "editable", FALSE, NULL);
3878 /* Creates the widgets for the shortcuts and bookmarks tree */
3880 shortcuts_list_create (GtkFileChooserDefault *impl)
3883 GtkTreeSelection *selection;
3884 GtkTreeViewColumn *column;
3885 GtkCellRenderer *renderer;
3887 /* Target types for dragging a row to/from the shortcuts list */
3888 const GtkTargetEntry tree_model_row_targets[] = {
3889 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3892 /* Scrolled window */
3894 swin = gtk_scrolled_window_new (NULL, NULL);
3895 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3896 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3897 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3899 gtk_widget_show (swin);
3903 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3904 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3905 #ifdef PROFILE_FILE_CHOOSER
3906 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3909 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3911 * 1. user brings up a SAVE dialog
3912 * 2. user clicks on a shortcut in the shortcuts pane
3913 * 3. user starts typing a filename
3915 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3916 * support interactive search. However, we'd rather focus the location entry
3917 * so that the user can type *there*.
3919 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3920 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3921 * if the user starts typing while the focus is in the shortcuts pane.
3923 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3924 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3926 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3927 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3928 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3929 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3930 /* Accessible object name for the file chooser's shortcuts pane */
3931 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3933 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3935 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3937 tree_model_row_targets,
3938 G_N_ELEMENTS (tree_model_row_targets),
3941 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3942 GTK_DEST_DEFAULT_ALL,
3943 tree_model_row_targets,
3944 G_N_ELEMENTS (tree_model_row_targets),
3945 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3946 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3948 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3949 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3950 gtk_tree_selection_set_select_function (selection,
3951 shortcuts_select_func,
3954 g_signal_connect (selection, "changed",
3955 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3957 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3958 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3960 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3961 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3962 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3963 G_CALLBACK (shortcuts_drag_end_cb), impl);
3964 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3965 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3967 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3968 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3969 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3970 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3971 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3972 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3973 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3974 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3976 /* Support tooltips */
3977 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3978 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3979 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3981 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3982 gtk_widget_show (impl->browse_shortcuts_tree_view);
3986 column = gtk_tree_view_column_new ();
3987 /* Column header for the file chooser's shortcuts pane */
3988 gtk_tree_view_column_set_title (column, _("_Places"));
3990 renderer = gtk_cell_renderer_pixbuf_new ();
3991 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3992 gtk_tree_view_column_set_attributes (column, renderer,
3993 "pixbuf", SHORTCUTS_COL_PIXBUF,
3994 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3997 renderer = gtk_cell_renderer_text_new ();
3998 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
3999 g_signal_connect (renderer, "edited",
4000 G_CALLBACK (shortcuts_edited), impl);
4001 g_signal_connect (renderer, "editing-canceled",
4002 G_CALLBACK (shortcuts_editing_canceled), impl);
4003 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4004 gtk_tree_view_column_set_attributes (column, renderer,
4005 "text", SHORTCUTS_COL_NAME,
4008 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
4009 shortcuts_row_separator_func,
4012 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
4017 /* Creates the widgets for the shortcuts/bookmarks pane */
4019 shortcuts_pane_create (GtkFileChooserDefault *impl,
4020 GtkSizeGroup *size_group)
4026 vbox = gtk_vbox_new (FALSE, 6);
4027 gtk_widget_show (vbox);
4029 /* Shortcuts tree */
4031 widget = shortcuts_list_create (impl);
4032 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
4034 /* Box for buttons */
4036 hbox = gtk_hbox_new (TRUE, 6);
4037 gtk_size_group_add_widget (size_group, hbox);
4038 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4039 gtk_widget_show (hbox);
4041 /* Add bookmark button */
4043 impl->browse_shortcuts_add_button = button_new (impl,
4048 G_CALLBACK (add_bookmark_button_clicked_cb));
4049 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
4050 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
4051 _("Add the selected folder to the Bookmarks"));
4053 /* Remove bookmark button */
4055 impl->browse_shortcuts_remove_button = button_new (impl,
4060 G_CALLBACK (remove_bookmark_button_clicked_cb));
4061 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
4062 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
4063 _("Remove the selected bookmark"));
4069 key_is_left_or_right (GdkEventKey *event)
4073 modifiers = gtk_accelerator_get_default_mod_mask ();
4075 return ((event->keyval == GDK_Right
4076 || event->keyval == GDK_KP_Right
4077 || event->keyval == GDK_Left
4078 || event->keyval == GDK_KP_Left)
4079 && (event->state & modifiers) == 0);
4082 /* Handles key press events on the file list, so that we can trap Enter to
4083 * activate the default button on our own. Also, checks to see if '/' has been
4084 * pressed. See comment by tree_view_keybinding_cb() for more details.
4087 browse_files_key_press_event_cb (GtkWidget *widget,
4091 GtkFileChooserDefault *impl;
4094 impl = (GtkFileChooserDefault *) data;
4096 modifiers = gtk_accelerator_get_default_mod_mask ();
4098 if ((event->keyval == GDK_slash
4099 || event->keyval == GDK_KP_Divide
4101 || event->keyval == GDK_asciitilde
4103 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
4105 location_popup_handler (impl, event->string);
4109 if (key_is_left_or_right (event))
4111 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
4115 if ((event->keyval == GDK_Return
4116 || event->keyval == GDK_ISO_Enter
4117 || event->keyval == GDK_KP_Enter
4118 || event->keyval == GDK_space
4119 || event->keyval == GDK_KP_Space)
4120 && ((event->state & modifiers) == 0)
4121 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
4122 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4126 window = get_toplevel (widget);
4128 && widget != window->default_widget
4129 && !(widget == window->focus_widget &&
4130 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4132 gtk_window_activate_default (window);
4140 /* Callback used when the file list's popup menu is detached */
4142 popup_menu_detach_cb (GtkWidget *attach_widget,
4145 GtkFileChooserDefault *impl;
4147 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
4148 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
4150 impl->browse_files_popup_menu = NULL;
4151 impl->browse_files_popup_menu_add_shortcut_item = NULL;
4152 impl->browse_files_popup_menu_hidden_files_item = NULL;
4155 /* Callback used when the "Add to Bookmarks" menu item is activated */
4157 add_to_shortcuts_cb (GtkMenuItem *item,
4158 GtkFileChooserDefault *impl)
4160 bookmarks_add_selected_folder (impl);
4163 /* Callback used when the "Show Hidden Files" menu item is toggled */
4165 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4166 GtkFileChooserDefault *impl)
4169 "show-hidden", gtk_check_menu_item_get_active (item),
4173 /* Callback used when the "Show Size Column" menu item is toggled */
4175 show_size_column_toggled_cb (GtkCheckMenuItem *item,
4176 GtkFileChooserDefault *impl)
4178 impl->show_size_column = gtk_check_menu_item_get_active (item);
4180 if (impl->list_size_column)
4181 gtk_tree_view_column_set_visible (impl->list_size_column,
4182 impl->show_size_column);
4185 /* Shows an error dialog about not being able to select a dragged file */
4187 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4192 _("Could not select file"),
4197 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4202 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4204 for (i = 1; uris[i]; i++)
4207 GError *error = NULL;
4210 file = g_file_new_for_uri (uri);
4212 gtk_file_chooser_default_select_file (chooser, file, &error);
4214 error_selecting_dragged_file_dialog (impl, file, error);
4216 g_object_unref (file);
4220 struct FileListDragData
4222 GtkFileChooserDefault *impl;
4228 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4230 const GError *error,
4233 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4234 struct FileListDragData *data = user_data;
4235 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4237 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4240 data->impl->file_list_drag_data_received_cancellable = NULL;
4242 if (cancelled || error)
4245 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4246 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4247 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4248 change_folder_and_display_error (data->impl, data->file, FALSE);
4251 GError *error = NULL;
4253 gtk_file_chooser_default_unselect_all (chooser);
4254 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4256 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4258 browse_files_center_selected_row (data->impl);
4261 if (data->impl->select_multiple)
4262 file_list_drag_data_select_uris (data->impl, data->uris);
4265 g_object_unref (data->impl);
4266 g_strfreev (data->uris);
4267 g_object_unref (data->file);
4270 g_object_unref (cancellable);
4274 file_list_drag_data_received_cb (GtkWidget *widget,
4275 GdkDragContext *context,
4278 GtkSelectionData *selection_data,
4283 GtkFileChooserDefault *impl;
4284 GtkFileChooser *chooser;
4289 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4290 chooser = GTK_FILE_CHOOSER (data);
4292 /* Allow only drags from other widgets; see bug #533891. */
4293 if (gtk_drag_get_source_widget (context) == widget)
4295 g_signal_stop_emission_by_name (widget, "drag-data-received");
4299 /* Parse the text/uri-list string, navigate to the first one */
4300 uris = gtk_selection_data_get_uris (selection_data);
4301 if (uris && uris[0])
4303 struct FileListDragData *data;
4306 file = g_file_new_for_uri (uri);
4308 data = g_new0 (struct FileListDragData, 1);
4309 data->impl = g_object_ref (impl);
4313 if (impl->file_list_drag_data_received_cancellable)
4314 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4316 impl->file_list_drag_data_received_cancellable =
4317 _gtk_file_system_get_info (impl->file_system, file,
4319 file_list_drag_data_received_get_info_cb,
4323 g_signal_stop_emission_by_name (widget, "drag-data-received");
4326 /* Don't do anything with the drag_drop signal */
4328 file_list_drag_drop_cb (GtkWidget *widget,
4329 GdkDragContext *context,
4333 GtkFileChooserDefault *impl)
4335 g_signal_stop_emission_by_name (widget, "drag-drop");
4339 /* Disable the normal tree drag motion handler, it makes it look like you're
4340 dropping the dragged item onto a tree item */
4342 file_list_drag_motion_cb (GtkWidget *widget,
4343 GdkDragContext *context,
4347 GtkFileChooserDefault *impl)
4349 g_signal_stop_emission_by_name (widget, "drag-motion");
4353 /* Constructs the popup menu for the file list if needed */
4355 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4359 if (impl->browse_files_popup_menu)
4362 impl->browse_files_popup_menu = gtk_menu_new ();
4363 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4364 impl->browse_files_tree_view,
4365 popup_menu_detach_cb);
4367 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4368 impl->browse_files_popup_menu_add_shortcut_item = item;
4369 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4370 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4371 g_signal_connect (item, "activate",
4372 G_CALLBACK (add_to_shortcuts_cb), impl);
4373 gtk_widget_show (item);
4374 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4376 item = gtk_separator_menu_item_new ();
4377 gtk_widget_show (item);
4378 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4380 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4381 impl->browse_files_popup_menu_hidden_files_item = item;
4382 g_signal_connect (item, "toggled",
4383 G_CALLBACK (show_hidden_toggled_cb), impl);
4384 gtk_widget_show (item);
4385 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4387 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4388 impl->browse_files_popup_menu_size_column_item = item;
4389 g_signal_connect (item, "toggled",
4390 G_CALLBACK (show_size_column_toggled_cb), impl);
4391 gtk_widget_show (item);
4392 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4394 bookmarks_check_add_sensitivity (impl);
4397 /* Updates the popup menu for the file list, creating it if necessary */
4399 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4401 file_list_build_popup_menu (impl);
4403 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4405 /* The sensitivity of the Add to Bookmarks item is set in
4406 * bookmarks_check_add_sensitivity()
4409 /* 'Show Hidden Files' */
4410 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4411 G_CALLBACK (show_hidden_toggled_cb), impl);
4412 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4414 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4415 G_CALLBACK (show_hidden_toggled_cb), impl);
4417 /* 'Show Size Column' */
4418 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4419 G_CALLBACK (show_size_column_toggled_cb), impl);
4420 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4421 impl->show_size_column);
4422 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4423 G_CALLBACK (show_size_column_toggled_cb), impl);
4427 popup_position_func (GtkMenu *menu,
4433 GtkWidget *widget = GTK_WIDGET (user_data);
4434 GdkScreen *screen = gtk_widget_get_screen (widget);
4437 GdkRectangle monitor;
4439 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4441 gdk_window_get_origin (widget->window, x, y);
4443 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4445 *x += (widget->allocation.width - req.width) / 2;
4446 *y += (widget->allocation.height - req.height) / 2;
4448 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4449 gtk_menu_set_monitor (menu, monitor_num);
4450 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4452 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4453 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4459 file_list_popup_menu (GtkFileChooserDefault *impl,
4460 GdkEventButton *event)
4462 file_list_update_popup_menu (impl);
4464 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4465 NULL, NULL, NULL, NULL,
4466 event->button, event->time);
4469 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4471 popup_position_func, impl->browse_files_tree_view,
4472 0, GDK_CURRENT_TIME);
4473 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4479 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4481 list_popup_menu_cb (GtkWidget *widget,
4482 GtkFileChooserDefault *impl)
4484 file_list_popup_menu (impl, NULL);
4488 /* Callback used when a button is pressed on the file list. We trap button 3 to
4489 * bring up a popup menu.
4492 list_button_press_event_cb (GtkWidget *widget,
4493 GdkEventButton *event,
4494 GtkFileChooserDefault *impl)
4496 static gboolean in_press = FALSE;
4502 if (event->button != 3)
4506 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4509 file_list_popup_menu (impl, event);
4514 OperationMode operation_mode;
4515 gint general_column;
4519 /* Sigh. Each operation mode has different sort column IDs. This table
4520 * translates between them.
4522 static const ColumnMap column_map[] = {
4523 { OPERATION_MODE_BROWSE, FILE_LIST_COL_NAME, FILE_LIST_COL_NAME },
4524 { OPERATION_MODE_BROWSE, FILE_LIST_COL_SIZE, FILE_LIST_COL_SIZE },
4525 { OPERATION_MODE_BROWSE, FILE_LIST_COL_MTIME, FILE_LIST_COL_MTIME },
4527 { OPERATION_MODE_SEARCH, FILE_LIST_COL_NAME, SEARCH_MODEL_COL_FILE },
4528 { OPERATION_MODE_SEARCH, FILE_LIST_COL_SIZE, SEARCH_MODEL_COL_SIZE },
4529 { OPERATION_MODE_SEARCH, FILE_LIST_COL_MTIME, SEARCH_MODEL_COL_MTIME },
4531 { OPERATION_MODE_RECENT, FILE_LIST_COL_NAME, RECENT_MODEL_COL_FILE },
4532 { OPERATION_MODE_RECENT, FILE_LIST_COL_SIZE, 0 },
4533 { OPERATION_MODE_RECENT, FILE_LIST_COL_MTIME, RECENT_MODEL_COL_INFO }
4537 general_column_to_model_column (GtkFileChooserDefault *impl, gint general_column)
4541 for (i = 0; i < G_N_ELEMENTS (column_map); i++)
4542 if (column_map[i].operation_mode == impl->operation_mode
4543 && column_map[i].general_column == general_column)
4544 return column_map[i].model_column;
4546 g_assert_not_reached ();
4551 model_column_to_general_column (GtkFileChooserDefault *impl, gint model_column)
4555 for (i = 0; i < G_N_ELEMENTS (column_map); i++)
4556 if (column_map[i].operation_mode == impl->operation_mode
4557 && column_map[i].model_column == model_column)
4558 return column_map[i].general_column;
4560 g_assert_not_reached ();
4564 /* Sets the sort column IDs for the file list based on the operation mode */
4566 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4568 int name_id, mtime_id, size_id;
4570 name_id = general_column_to_model_column (impl, FILE_LIST_COL_NAME);
4571 mtime_id = general_column_to_model_column (impl, FILE_LIST_COL_MTIME);
4572 size_id = general_column_to_model_column (impl, FILE_LIST_COL_SIZE);
4574 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4575 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4576 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, size_id);
4580 file_list_query_tooltip_cb (GtkWidget *widget,
4583 gboolean keyboard_tip,
4584 GtkTooltip *tooltip,
4587 GtkFileChooserDefault *impl = user_data;
4588 GtkTreeIter iter, child_iter;
4589 GtkTreePath *path = NULL;
4593 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4597 gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4605 switch (impl->operation_mode)
4607 case OPERATION_MODE_SEARCH:
4608 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
4610 gtk_tree_path_free (path);
4614 search_get_valid_child_iter (impl, &child_iter, &iter);
4615 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
4616 SEARCH_MODEL_COL_FILE, &file,
4620 case OPERATION_MODE_RECENT:
4621 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
4623 gtk_tree_path_free (path);
4627 recent_get_valid_child_iter (impl, &child_iter, &iter);
4628 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
4629 RECENT_MODEL_COL_FILE, &file,
4633 case OPERATION_MODE_BROWSE:
4634 g_assert_not_reached ();
4640 gtk_tree_path_free (path);
4644 filename = g_file_get_path (file);
4645 gtk_tooltip_set_text (tooltip, filename);
4646 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4651 gtk_tree_path_free (path);
4656 /* Creates the widgets for the file list */
4658 create_file_list (GtkFileChooserDefault *impl)
4661 GtkTreeSelection *selection;
4662 GtkTreeViewColumn *column;
4663 GtkCellRenderer *renderer;
4665 /* Scrolled window */
4666 swin = gtk_scrolled_window_new (NULL, NULL);
4667 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4668 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4669 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4672 /* Tree/list view */
4674 impl->browse_files_tree_view = gtk_tree_view_new ();
4675 #ifdef PROFILE_FILE_CHOOSER
4676 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4678 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4679 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4681 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4682 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4684 gtk_drag_dest_set (impl->browse_files_tree_view,
4685 GTK_DEST_DEFAULT_ALL,
4687 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4688 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4690 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4691 G_CALLBACK (list_row_activated), impl);
4692 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4693 G_CALLBACK (browse_files_key_press_event_cb), impl);
4694 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4695 G_CALLBACK (list_popup_menu_cb), impl);
4696 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4697 G_CALLBACK (list_button_press_event_cb), impl);
4699 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4700 G_CALLBACK (file_list_drag_data_received_cb), impl);
4701 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4702 G_CALLBACK (file_list_drag_drop_cb), impl);
4703 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4704 G_CALLBACK (file_list_drag_motion_cb), impl);
4706 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4707 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4708 G_CALLBACK (file_list_query_tooltip_cb), impl);
4710 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4711 gtk_tree_selection_set_select_function (selection,
4714 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4717 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4718 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4720 g_signal_connect (selection, "changed",
4721 G_CALLBACK (list_selection_changed), impl);
4723 /* Filename column */
4725 impl->list_name_column = gtk_tree_view_column_new ();
4726 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4727 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4728 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4730 renderer = gtk_cell_renderer_pixbuf_new ();
4731 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4732 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4733 list_icon_data_func, impl, NULL);
4735 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4736 g_object_set (impl->list_name_renderer,
4737 "ellipsize", PANGO_ELLIPSIZE_END,
4739 g_signal_connect (impl->list_name_renderer, "edited",
4740 G_CALLBACK (renderer_edited_cb), impl);
4741 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4742 G_CALLBACK (renderer_editing_canceled_cb), impl);
4743 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4744 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4745 list_name_data_func, impl, NULL);
4747 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4751 column = gtk_tree_view_column_new ();
4752 gtk_tree_view_column_set_resizable (column, TRUE);
4753 gtk_tree_view_column_set_title (column, _("Size"));
4755 renderer = gtk_cell_renderer_text_new ();
4756 g_object_set (renderer,
4757 "alignment", PANGO_ALIGN_RIGHT,
4759 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4760 gtk_tree_view_column_set_cell_data_func (column, renderer,
4761 list_size_data_func, impl, NULL);
4762 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4763 impl->list_size_column = column;
4765 /* Modification time column */
4767 column = gtk_tree_view_column_new ();
4768 gtk_tree_view_column_set_resizable (column, TRUE);
4769 gtk_tree_view_column_set_title (column, _("Modified"));
4771 renderer = gtk_cell_renderer_text_new ();
4772 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4773 gtk_tree_view_column_set_cell_data_func (column, renderer,
4774 list_mtime_data_func, impl, NULL);
4775 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4776 impl->list_mtime_column = column;
4778 file_list_set_sort_column_ids (impl);
4780 gtk_widget_show_all (swin);
4786 create_path_bar (GtkFileChooserDefault *impl)
4788 GtkWidget *path_bar;
4790 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4791 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4796 /* Creates the widgets for the files/folders pane */
4798 file_pane_create (GtkFileChooserDefault *impl,
4799 GtkSizeGroup *size_group)
4805 vbox = gtk_vbox_new (FALSE, 6);
4806 gtk_widget_show (vbox);
4808 /* Box for lists and preview */
4810 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4811 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4812 gtk_widget_show (hbox);
4816 widget = create_file_list (impl);
4817 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4821 impl->preview_box = gtk_vbox_new (FALSE, 12);
4822 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4823 /* Don't show preview box initially */
4827 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4829 widget = filter_create (impl);
4831 gtk_widget_show (widget);
4832 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4834 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4835 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4840 /* Callback used when the "Browse for more folders" expander is toggled */
4842 expander_changed_cb (GtkExpander *expander,
4844 GtkFileChooserDefault *impl)
4846 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4847 update_appearance (impl);
4850 /* Callback used when the selection changes in the save folder combo box */
4852 save_folder_combo_changed_cb (GtkComboBox *combo,
4853 GtkFileChooserDefault *impl)
4857 if (impl->changing_folder)
4860 if (gtk_combo_box_get_active_iter (combo, &iter))
4862 GtkTreeIter child_iter;
4864 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4867 shortcuts_activate_iter (impl, &child_iter);
4872 save_folder_update_tooltip (GtkComboBox *combo,
4873 GtkFileChooserDefault *impl)
4880 if (gtk_combo_box_get_active_iter (combo, &iter))
4882 GtkTreeIter child_iter;
4884 ShortcutType shortcut_type;
4886 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4889 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4890 SHORTCUTS_COL_DATA, &col_data,
4891 SHORTCUTS_COL_TYPE, &shortcut_type,
4894 if (shortcut_type == SHORTCUT_TYPE_FILE)
4895 tooltip = g_file_get_parse_name (G_FILE (col_data));
4898 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4899 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4900 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4904 /* Filter function used to filter out the Search item and its separator.
4905 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4908 shortcuts_combo_filter_func (GtkTreeModel *model,
4912 GtkFileChooserDefault *impl;
4913 GtkTreePath *tree_path;
4918 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4920 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4922 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4923 g_assert (tree_path != NULL);
4925 indices = gtk_tree_path_get_indices (tree_path);
4929 if (impl->has_search)
4931 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4932 if (idx == indices[0])
4936 if (impl->has_recent)
4938 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4939 if (idx == indices[0])
4943 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4944 if (idx == indices[0])
4949 gtk_tree_path_free (tree_path);
4954 /* Creates the combo box with the save folders */
4956 save_folder_combo_create (GtkFileChooserDefault *impl)
4959 GtkCellRenderer *cell;
4961 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4962 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4963 shortcuts_combo_filter_func,
4967 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4968 "model", impl->shortcuts_combo_filter_model,
4969 "focus-on-click", FALSE,
4971 gtk_widget_show (combo);
4973 cell = gtk_cell_renderer_pixbuf_new ();
4974 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4975 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4976 "pixbuf", SHORTCUTS_COL_PIXBUF,
4977 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4978 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4981 cell = gtk_cell_renderer_text_new ();
4982 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4983 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4984 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4985 "text", SHORTCUTS_COL_NAME,
4986 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4989 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4990 shortcuts_row_separator_func,
4993 g_signal_connect (combo, "changed",
4994 G_CALLBACK (save_folder_combo_changed_cb), impl);
4995 g_signal_connect (combo, "changed",
4996 G_CALLBACK (save_folder_update_tooltip), impl);
5001 /* Creates the widgets specific to Save mode */
5003 save_widgets_create (GtkFileChooserDefault *impl)
5008 GtkWidget *alignment;
5010 if (impl->save_widgets != NULL)
5013 location_switch_to_path_bar (impl);
5015 vbox = gtk_vbox_new (FALSE, 12);
5017 table = gtk_table_new (2, 2, FALSE);
5018 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
5019 gtk_widget_show (table);
5020 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
5021 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
5025 widget = gtk_label_new_with_mnemonic (_("_Name:"));
5026 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
5027 gtk_table_attach (GTK_TABLE (table), widget,
5031 gtk_widget_show (widget);
5033 /* Location entry */
5035 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
5036 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
5038 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
5039 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
5040 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5041 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
5043 GTK_EXPAND | GTK_FILL, 0,
5045 gtk_widget_show (impl->location_entry);
5046 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
5049 impl->save_folder_label = gtk_label_new (NULL);
5050 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
5051 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
5055 gtk_widget_show (impl->save_folder_label);
5057 impl->save_folder_combo = save_folder_combo_create (impl);
5058 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
5060 GTK_EXPAND | GTK_FILL, GTK_FILL,
5062 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
5065 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5066 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
5068 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
5069 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
5070 g_signal_connect (impl->save_expander, "notify::expanded",
5071 G_CALLBACK (expander_changed_cb),
5073 gtk_widget_show_all (alignment);
5075 impl->save_widgets = vbox;
5076 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
5077 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
5078 gtk_widget_show (impl->save_widgets);
5081 /* Destroys the widgets specific to Save mode */
5083 save_widgets_destroy (GtkFileChooserDefault *impl)
5085 if (impl->save_widgets == NULL)
5088 gtk_widget_destroy (impl->save_widgets);
5089 impl->save_widgets = NULL;
5090 impl->location_entry = NULL;
5091 impl->save_folder_label = NULL;
5092 impl->save_folder_combo = NULL;
5093 impl->save_expander = NULL;
5096 /* Turns on the path bar widget. Can be called even if we are already in that
5100 location_switch_to_path_bar (GtkFileChooserDefault *impl)
5102 if (impl->location_entry)
5104 gtk_widget_destroy (impl->location_entry);
5105 impl->location_entry = NULL;
5108 gtk_widget_hide (impl->location_entry_box);
5111 /* Sets the full path of the current folder as the text in the location entry. */
5113 location_entry_set_initial_text (GtkFileChooserDefault *impl)
5115 gchar *text, *filename;
5117 if (!impl->current_folder)
5120 filename = g_file_get_path (impl->current_folder);
5124 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
5128 text = g_file_get_uri (impl->current_folder);
5132 gboolean need_slash;
5135 len = strlen (text);
5136 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
5142 slash_text = g_new (char, len + 2);
5143 strcpy (slash_text, text);
5144 slash_text[len] = G_DIR_SEPARATOR;
5145 slash_text[len + 1] = 0;
5151 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
5158 /* Turns on the location entry. Can be called even if we are already in that
5162 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
5164 /* when in search or recent files mode, we are not showing the
5165 * location_entry_box container, so there's no point in switching
5168 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5169 impl->operation_mode == OPERATION_MODE_RECENT)
5172 if (impl->location_entry)
5173 gtk_widget_destroy (impl->location_entry);
5177 gtk_widget_show (impl->location_entry_box);
5181 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
5182 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
5184 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
5185 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5187 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
5188 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
5190 /* Configure the entry */
5192 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
5196 gtk_widget_show (impl->location_entry);
5197 gtk_widget_grab_focus (impl->location_entry);
5200 /* Sets a new location mode. set_buttons determines whether the toggle button
5201 * for the mode will also be changed.
5204 location_mode_set (GtkFileChooserDefault *impl,
5205 LocationMode new_mode,
5206 gboolean set_button)
5208 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5209 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5211 GtkWindow *toplevel;
5212 GtkWidget *current_focus;
5213 gboolean button_active;
5214 gboolean switch_to_file_list;
5218 case LOCATION_MODE_PATH_BAR:
5219 button_active = FALSE;
5221 /* The location_entry will disappear when we switch to path bar mode. So,
5222 * we'll focus the file list in that case, to avoid having a window with
5223 * no focused widget.
5225 toplevel = get_toplevel (GTK_WIDGET (impl));
5226 switch_to_file_list = FALSE;
5229 current_focus = gtk_window_get_focus (toplevel);
5230 if (!current_focus || current_focus == impl->location_entry)
5231 switch_to_file_list = TRUE;
5234 location_switch_to_path_bar (impl);
5236 if (switch_to_file_list)
5237 gtk_widget_grab_focus (impl->browse_files_tree_view);
5241 case LOCATION_MODE_FILENAME_ENTRY:
5242 button_active = TRUE;
5243 location_switch_to_filename_entry (impl);
5247 g_assert_not_reached ();
5253 g_signal_handlers_block_by_func (impl->location_button,
5254 G_CALLBACK (location_button_toggled_cb), impl);
5256 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5258 g_signal_handlers_unblock_by_func (impl->location_button,
5259 G_CALLBACK (location_button_toggled_cb), impl);
5263 impl->location_mode = new_mode;
5267 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5269 /* when in search or recent files mode, we are not showing the
5270 * location_entry_box container, so there's no point in switching
5273 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5274 impl->operation_mode == OPERATION_MODE_RECENT)
5277 /* If the file entry is not visible, show it.
5278 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5280 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5282 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5284 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5286 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5288 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5292 gtk_widget_grab_focus (impl->location_entry);
5297 /* Callback used when one of the location mode buttons is toggled */
5299 location_button_toggled_cb (GtkToggleButton *toggle,
5300 GtkFileChooserDefault *impl)
5303 LocationMode new_mode;
5305 is_active = gtk_toggle_button_get_active (toggle);
5309 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5310 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5314 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5315 new_mode = LOCATION_MODE_PATH_BAR;
5318 location_mode_set (impl, new_mode, FALSE);
5321 /* Creates a toggle button for the location entry. */
5323 location_button_create (GtkFileChooserDefault *impl)
5328 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5329 gtk_widget_show (image);
5331 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5335 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5337 g_signal_connect (impl->location_button, "toggled",
5338 G_CALLBACK (location_button_toggled_cb), impl);
5340 str = _("Type a file name");
5342 gtk_widget_set_tooltip_text (impl->location_button, str);
5343 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5346 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5348 browse_widgets_create (GtkFileChooserDefault *impl)
5353 GtkSizeGroup *size_group;
5355 /* size group is used by the [+][-] buttons and the filter combo */
5356 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5357 vbox = gtk_vbox_new (FALSE, 12);
5359 /* Location widgets */
5360 impl->browse_path_bar_hbox = gtk_hbox_new (FALSE, 12);
5361 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5362 gtk_widget_show (impl->browse_path_bar_hbox);
5364 /* Size group that allows the path bar to be the same size between modes */
5365 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5366 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5368 /* Location button */
5370 location_button_create (impl);
5371 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5375 impl->browse_path_bar = create_path_bar (impl);
5376 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5377 gtk_widget_show_all (impl->browse_path_bar);
5378 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5381 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5382 g_signal_connect (impl->browse_new_folder_button, "clicked",
5383 G_CALLBACK (new_folder_button_clicked), impl);
5384 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5386 /* Box for the location label and entry */
5388 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5389 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5391 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5392 gtk_widget_show (impl->location_label);
5393 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5396 hpaned = gtk_hpaned_new ();
5397 gtk_widget_show (hpaned);
5398 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5400 widget = shortcuts_pane_create (impl, size_group);
5401 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5402 widget = file_pane_create (impl, size_group);
5403 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5405 g_object_unref (size_group);
5411 gtk_file_chooser_default_constructor (GType type,
5412 guint n_construct_properties,
5413 GObjectConstructParam *construct_params)
5415 GtkFileChooserDefault *impl;
5418 profile_start ("start", NULL);
5420 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5421 n_construct_properties,
5423 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5425 g_assert (impl->file_system);
5427 gtk_widget_push_composite_child ();
5429 /* Shortcuts model */
5430 shortcuts_model_create (impl);
5432 /* The browse widgets */
5433 impl->browse_widgets = browse_widgets_create (impl);
5434 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5436 /* Alignment to hold extra widget */
5437 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5438 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5440 gtk_widget_pop_composite_child ();
5441 update_appearance (impl);
5443 profile_end ("end", NULL);
5448 /* Sets the extra_widget by packing it in the appropriate place */
5450 set_extra_widget (GtkFileChooserDefault *impl,
5451 GtkWidget *extra_widget)
5455 g_object_ref (extra_widget);
5456 /* FIXME: is this right ? */
5457 gtk_widget_show (extra_widget);
5460 if (impl->extra_widget)
5462 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5463 g_object_unref (impl->extra_widget);
5466 impl->extra_widget = extra_widget;
5467 if (impl->extra_widget)
5469 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5470 gtk_widget_show (impl->extra_align);
5473 gtk_widget_hide (impl->extra_align);
5477 set_local_only (GtkFileChooserDefault *impl,
5478 gboolean local_only)
5480 if (local_only != impl->local_only)
5482 impl->local_only = local_only;
5484 if (impl->location_entry)
5485 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5487 if (impl->shortcuts_model && impl->file_system)
5489 shortcuts_add_volumes (impl);
5490 shortcuts_add_bookmarks (impl);
5493 if (local_only && impl->current_folder &&
5494 !g_file_is_native (impl->current_folder))
5496 /* If we are pointing to a non-local folder, make an effort to change
5497 * back to a local folder, but it's really up to the app to not cause
5498 * such a situation, so we ignore errors.
5500 const gchar *home = g_get_home_dir ();
5506 home_file = g_file_new_for_path (home);
5508 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5510 g_object_unref (home_file);
5516 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5517 GtkFileChooserDefault *impl)
5519 shortcuts_add_volumes (impl);
5520 shortcuts_add_bookmarks (impl);
5522 bookmarks_check_add_sensitivity (impl);
5523 bookmarks_check_remove_sensitivity (impl);
5524 shortcuts_check_popup_sensitivity (impl);
5527 /* Sets the file chooser to multiple selection mode */
5529 set_select_multiple (GtkFileChooserDefault *impl,
5530 gboolean select_multiple,
5531 gboolean property_notify)
5533 GtkTreeSelection *selection;
5534 GtkSelectionMode mode;
5536 if (select_multiple == impl->select_multiple)
5539 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5541 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5542 gtk_tree_selection_set_mode (selection, mode);
5544 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5546 impl->select_multiple = select_multiple;
5547 g_object_notify (G_OBJECT (impl), "select-multiple");
5549 check_preview_change (impl);
5553 set_file_system_backend (GtkFileChooserDefault *impl)
5555 profile_start ("start for backend", "default");
5557 impl->file_system = _gtk_file_system_new ();
5559 g_signal_connect (impl->file_system, "volumes-changed",
5560 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5561 g_signal_connect (impl->file_system, "bookmarks-changed",
5562 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5564 profile_end ("end", NULL);
5568 unset_file_system_backend (GtkFileChooserDefault *impl)
5570 g_signal_handlers_disconnect_by_func (impl->file_system,
5571 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5573 g_object_unref (impl->file_system);
5575 impl->file_system = NULL;
5578 /* This function is basically a do_all function.
5580 * It sets the visibility on all the widgets based on the current state, and
5581 * moves the custom_widget if needed.
5584 update_appearance (GtkFileChooserDefault *impl)
5586 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5587 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5591 gtk_widget_hide (impl->location_button);
5592 save_widgets_create (impl);
5594 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5595 text = _("Save in _folder:");
5597 text = _("Create in _folder:");
5599 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5601 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5603 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5604 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5605 gtk_widget_set_has_tooltip (impl->save_folder_combo, FALSE);
5606 gtk_widget_show (impl->browse_widgets);
5610 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5611 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5612 gtk_widget_set_has_tooltip (impl->save_folder_combo, TRUE);
5613 gtk_widget_hide (impl->browse_widgets);
5616 if (impl->select_multiple)
5618 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5619 "Re-setting to single selection mode.");
5620 set_select_multiple (impl, FALSE, TRUE);
5623 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5624 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5626 gtk_widget_show (impl->location_button);
5627 save_widgets_destroy (impl);
5628 gtk_widget_show (impl->browse_widgets);
5629 location_mode_set (impl, impl->location_mode, TRUE);
5632 if (impl->location_entry)
5633 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5635 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5636 gtk_widget_hide (impl->browse_new_folder_button);
5638 gtk_widget_show (impl->browse_new_folder_button);
5640 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5641 * of files may change depending whether we are in a file or folder-only mode.
5643 gtk_widget_queue_draw (impl->browse_files_tree_view);
5645 emit_default_size_changed (impl);
5649 gtk_file_chooser_default_set_property (GObject *object,
5651 const GValue *value,
5655 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5659 case GTK_FILE_CHOOSER_PROP_ACTION:
5661 GtkFileChooserAction action = g_value_get_enum (value);
5663 if (action != impl->action)
5665 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5667 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5668 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5669 && impl->select_multiple)
5671 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5672 "this is not allowed in multiple selection mode. Resetting the file chooser "
5673 "to single selection mode.");
5674 set_select_multiple (impl, FALSE, TRUE);
5676 impl->action = action;
5677 update_appearance (impl);
5678 settings_load (impl);
5683 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5684 /* Ignore property */
5687 case GTK_FILE_CHOOSER_PROP_FILTER:
5688 set_current_filter (impl, g_value_get_object (value));
5691 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5692 set_local_only (impl, g_value_get_boolean (value));
5695 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5696 set_preview_widget (impl, g_value_get_object (value));
5699 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5700 impl->preview_widget_active = g_value_get_boolean (value);
5701 update_preview_widget_visibility (impl);
5704 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5705 impl->use_preview_label = g_value_get_boolean (value);
5706 update_preview_widget_visibility (impl);
5709 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5710 set_extra_widget (impl, g_value_get_object (value));
5713 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5715 gboolean select_multiple = g_value_get_boolean (value);
5716 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5717 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5720 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5721 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5722 "leaving the file chooser in single selection mode.");
5726 set_select_multiple (impl, select_multiple, FALSE);
5730 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5732 gboolean show_hidden = g_value_get_boolean (value);
5733 if (show_hidden != impl->show_hidden)
5735 impl->show_hidden = show_hidden;
5737 if (impl->browse_files_model)
5738 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5743 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5745 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5746 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5750 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5752 gboolean create_folders = g_value_get_boolean (value);
5753 impl->create_folders = create_folders;
5754 update_appearance (impl);
5759 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5765 gtk_file_chooser_default_get_property (GObject *object,
5770 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5774 case GTK_FILE_CHOOSER_PROP_ACTION:
5775 g_value_set_enum (value, impl->action);
5778 case GTK_FILE_CHOOSER_PROP_FILTER:
5779 g_value_set_object (value, impl->current_filter);
5782 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5783 g_value_set_boolean (value, impl->local_only);
5786 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5787 g_value_set_object (value, impl->preview_widget);
5790 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5791 g_value_set_boolean (value, impl->preview_widget_active);
5794 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5795 g_value_set_boolean (value, impl->use_preview_label);
5798 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5799 g_value_set_object (value, impl->extra_widget);
5802 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5803 g_value_set_boolean (value, impl->select_multiple);
5806 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5807 g_value_set_boolean (value, impl->show_hidden);
5810 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5811 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5814 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5815 g_value_set_boolean (value, impl->create_folders);
5819 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5824 /* Removes the settings signal handler. It's safe to call multiple times */
5826 remove_settings_signal (GtkFileChooserDefault *impl,
5829 if (impl->settings_signal_id)
5831 GtkSettings *settings;
5833 settings = gtk_settings_get_for_screen (screen);
5834 g_signal_handler_disconnect (settings,
5835 impl->settings_signal_id);
5836 impl->settings_signal_id = 0;
5841 gtk_file_chooser_default_dispose (GObject *object)
5844 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5846 if (impl->extra_widget)
5848 g_object_unref (impl->extra_widget);
5849 impl->extra_widget = NULL;
5852 pending_select_files_free (impl);
5854 /* cancel all pending operations */
5855 if (impl->pending_cancellables)
5857 for (l = impl->pending_cancellables; l; l = l->next)
5859 GCancellable *cancellable = G_CANCELLABLE (l->data);
5860 g_cancellable_cancel (cancellable);
5862 g_slist_free (impl->pending_cancellables);
5863 impl->pending_cancellables = NULL;
5866 if (impl->reload_icon_cancellables)
5868 for (l = impl->reload_icon_cancellables; l; l = l->next)
5870 GCancellable *cancellable = G_CANCELLABLE (l->data);
5871 g_cancellable_cancel (cancellable);
5873 g_slist_free (impl->reload_icon_cancellables);
5874 impl->reload_icon_cancellables = NULL;
5877 if (impl->loading_shortcuts)
5879 for (l = impl->loading_shortcuts; l; l = l->next)
5881 GCancellable *cancellable = G_CANCELLABLE (l->data);
5882 g_cancellable_cancel (cancellable);
5884 g_slist_free (impl->loading_shortcuts);
5885 impl->loading_shortcuts = NULL;
5888 if (impl->file_list_drag_data_received_cancellable)
5890 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5891 impl->file_list_drag_data_received_cancellable = NULL;
5894 if (impl->update_current_folder_cancellable)
5896 g_cancellable_cancel (impl->update_current_folder_cancellable);
5897 impl->update_current_folder_cancellable = NULL;
5900 if (impl->show_and_select_files_cancellable)
5902 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5903 impl->show_and_select_files_cancellable = NULL;
5906 if (impl->should_respond_get_info_cancellable)
5908 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5909 impl->should_respond_get_info_cancellable = NULL;
5912 if (impl->update_from_entry_cancellable)
5914 g_cancellable_cancel (impl->update_from_entry_cancellable);
5915 impl->update_from_entry_cancellable = NULL;
5918 if (impl->shortcuts_activate_iter_cancellable)
5920 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5921 impl->shortcuts_activate_iter_cancellable = NULL;
5924 search_stop_searching (impl, TRUE);
5925 recent_stop_loading (impl);
5927 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5929 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5932 /* We override show-all since we have internal widgets that
5933 * shouldn't be shown when you call show_all(), like the filter
5937 gtk_file_chooser_default_show_all (GtkWidget *widget)
5939 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5941 gtk_widget_show (widget);
5943 if (impl->extra_widget)
5944 gtk_widget_show_all (impl->extra_widget);
5947 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5948 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5951 toplevel_set_focus_cb (GtkWindow *window,
5953 GtkFileChooserDefault *impl)
5955 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5958 /* We monitor the focus widget on our toplevel to be able to know which widget
5959 * was last focused at the time our "should_respond" method gets called.
5962 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5963 GtkWidget *previous_toplevel)
5965 GtkFileChooserDefault *impl;
5966 GtkWidget *toplevel;
5968 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5970 if (previous_toplevel)
5972 g_assert (impl->toplevel_set_focus_id != 0);
5973 g_signal_handler_disconnect (previous_toplevel,
5974 impl->toplevel_set_focus_id);
5975 impl->toplevel_set_focus_id = 0;
5976 impl->toplevel_last_focus_widget = NULL;
5979 g_assert (impl->toplevel_set_focus_id == 0);
5981 toplevel = gtk_widget_get_toplevel (widget);
5982 if (GTK_IS_WINDOW (toplevel))
5984 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5985 G_CALLBACK (toplevel_set_focus_cb), impl);
5986 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5990 /* Changes the icons wherever it is needed */
5992 change_icon_theme (GtkFileChooserDefault *impl)
5994 GtkSettings *settings;
5997 profile_start ("start", NULL);
5999 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
6001 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
6002 impl->icon_size = MAX (width, height);
6004 impl->icon_size = FALLBACK_ICON_SIZE;
6006 shortcuts_reload_icons (impl);
6007 gtk_widget_queue_resize (impl->browse_files_tree_view);
6009 profile_end ("end", NULL);
6012 /* Callback used when a GtkSettings value changes */
6014 settings_notify_cb (GObject *object,
6016 GtkFileChooserDefault *impl)
6020 profile_start ("start", NULL);
6022 name = g_param_spec_get_name (pspec);
6024 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
6025 strcmp (name, "gtk-icon-sizes") == 0)
6026 change_icon_theme (impl);
6028 profile_end ("end", NULL);
6031 /* Installs a signal handler for GtkSettings so that we can monitor changes in
6035 check_icon_theme (GtkFileChooserDefault *impl)
6037 GtkSettings *settings;
6039 profile_start ("start", NULL);
6041 if (impl->settings_signal_id)
6043 profile_end ("end", NULL);
6047 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
6049 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
6050 impl->settings_signal_id = g_signal_connect (settings, "notify",
6051 G_CALLBACK (settings_notify_cb), impl);
6053 change_icon_theme (impl);
6056 profile_end ("end", NULL);
6060 gtk_file_chooser_default_style_set (GtkWidget *widget,
6061 GtkStyle *previous_style)
6063 GtkFileChooserDefault *impl;
6065 profile_start ("start", NULL);
6067 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6069 profile_msg (" parent class style_set start", NULL);
6070 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
6071 profile_msg (" parent class style_set end", NULL);
6073 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
6074 change_icon_theme (impl);
6076 emit_default_size_changed (impl);
6078 profile_end ("end", NULL);
6082 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
6083 GdkScreen *previous_screen)
6085 GtkFileChooserDefault *impl;
6087 profile_start ("start", NULL);
6089 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6091 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
6092 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
6094 remove_settings_signal (impl, previous_screen);
6095 check_icon_theme (impl);
6097 emit_default_size_changed (impl);
6099 profile_end ("end", NULL);
6103 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
6104 GtkAllocation *allocation)
6106 GtkFileChooserDefault *impl;
6108 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6110 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
6114 get_is_file_filtered (GtkFileChooserDefault *impl,
6116 GFileInfo *file_info)
6118 GtkFileFilterInfo filter_info;
6119 GtkFileFilterFlags needed;
6122 if (!impl->current_filter)
6125 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
6127 needed = gtk_file_filter_get_needed (impl->current_filter);
6129 filter_info.display_name = g_file_info_get_display_name (file_info);
6130 filter_info.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
6132 if (needed & GTK_FILE_FILTER_FILENAME)
6134 filter_info.filename = g_file_get_path (file);
6135 if (filter_info.filename)
6136 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
6139 filter_info.filename = NULL;
6141 if (needed & GTK_FILE_FILTER_URI)
6143 filter_info.uri = g_file_get_uri (file);
6144 if (filter_info.uri)
6145 filter_info.contains |= GTK_FILE_FILTER_URI;
6148 filter_info.uri = NULL;
6150 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
6152 g_free ((gchar *)filter_info.filename);
6153 g_free ((gchar *)filter_info.uri);
6154 g_free ((gchar *)filter_info.mime_type);
6160 set_sort_column (GtkFileChooserDefault *impl)
6162 GtkTreeSortable *sortable;
6164 switch (impl->operation_mode)
6166 case OPERATION_MODE_BROWSE:
6167 sortable = GTK_TREE_SORTABLE (impl->sort_model);
6170 case OPERATION_MODE_SEARCH:
6171 sortable = GTK_TREE_SORTABLE (impl->search_model_sort);
6174 case OPERATION_MODE_RECENT:
6175 sortable = GTK_TREE_SORTABLE (impl->recent_model_sort);
6179 g_assert_not_reached ();
6183 gtk_tree_sortable_set_sort_column_id (sortable,
6184 general_column_to_model_column (impl, impl->sort_column),
6189 settings_load (GtkFileChooserDefault *impl)
6191 GtkFileChooserSettings *settings;
6192 LocationMode location_mode;
6193 gboolean show_hidden;
6194 gboolean expand_folders;
6195 gboolean show_size_column;
6197 GtkSortType sort_order;
6199 settings = _gtk_file_chooser_settings_new ();
6201 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
6202 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
6203 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
6204 show_size_column = _gtk_file_chooser_settings_get_show_size_column (settings);
6205 sort_column = _gtk_file_chooser_settings_get_sort_column (settings);
6206 sort_order = _gtk_file_chooser_settings_get_sort_order (settings);
6208 g_object_unref (settings);
6210 location_mode_set (impl, location_mode, TRUE);
6212 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6214 impl->expand_folders = expand_folders;
6215 if (impl->save_expander)
6216 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
6218 impl->show_size_column = show_size_column;
6219 if (impl->list_size_column)
6220 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6222 impl->sort_column = sort_column;
6223 impl->sort_order = sort_order;
6224 /* We don't call set_sort_column() here as the models may not have been
6225 * created yet. The individual functions that create and set the models will
6226 * call set_sort_column() themselves.
6231 save_dialog_geometry (GtkFileChooserDefault *impl, GtkFileChooserSettings *settings)
6233 GtkWindow *toplevel;
6234 int x, y, width, height;
6236 /* We don't save the geometry in non-expanded "save" mode, so that the "little
6237 * dialog" won't make future Open dialogs too small.
6239 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6240 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6241 || impl->expand_folders))
6244 toplevel = get_toplevel (GTK_WIDGET (impl));
6246 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
6249 gtk_window_get_position (toplevel, &x, &y);
6250 gtk_window_get_size (toplevel, &width, &height);
6252 _gtk_file_chooser_settings_set_geometry (settings, x, y, width, height);
6256 settings_save (GtkFileChooserDefault *impl)
6258 GtkFileChooserSettings *settings;
6260 settings = _gtk_file_chooser_settings_new ();
6262 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6263 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6264 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6265 _gtk_file_chooser_settings_set_show_size_column (settings, impl->show_size_column);
6266 _gtk_file_chooser_settings_set_sort_column (settings, impl->sort_column);
6267 _gtk_file_chooser_settings_set_sort_order (settings, impl->sort_order);
6269 save_dialog_geometry (impl, settings);
6272 _gtk_file_chooser_settings_save (settings, NULL);
6274 g_object_unref (settings);
6277 /* GtkWidget::realize method */
6279 gtk_file_chooser_default_realize (GtkWidget *widget)
6281 GtkFileChooserDefault *impl;
6283 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6285 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6287 emit_default_size_changed (impl);
6290 /* GtkWidget::map method */
6292 gtk_file_chooser_default_map (GtkWidget *widget)
6294 GtkFileChooserDefault *impl;
6295 char *current_working_dir;
6297 profile_start ("start", NULL);
6299 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6301 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6303 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6305 switch (impl->reload_state)
6308 /* The user didn't explicitly give us a folder to
6309 * display, so we'll use the cwd
6311 current_working_dir = g_get_current_dir ();
6312 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6313 current_working_dir);
6314 g_free (current_working_dir);
6317 case RELOAD_HAS_FOLDER:
6318 /* Nothing; we are already loading or loaded, so we
6319 * don't need to reload
6323 case RELOAD_WAS_UNMAPPED:
6324 /* Just reload the current folder; else continue
6327 if (impl->current_folder)
6329 pending_select_files_store_selection (impl);
6330 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6335 g_assert_not_reached ();
6339 volumes_bookmarks_changed_cb (impl->file_system, impl);
6341 settings_load (impl);
6343 profile_end ("end", NULL);
6346 /* GtkWidget::unmap method */
6348 gtk_file_chooser_default_unmap (GtkWidget *widget)
6350 GtkFileChooserDefault *impl;
6352 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6354 settings_save (impl);
6356 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6358 impl->reload_state = RELOAD_WAS_UNMAPPED;
6362 list_model_filter_func (GtkFileSystemModel *model,
6364 GFileInfo *file_info,
6367 GtkFileChooserDefault *impl = user_data;
6369 if (!impl->current_filter)
6372 if (_gtk_file_info_consider_as_directory (file_info))
6375 return !get_is_file_filtered (impl, file, file_info);
6379 install_list_model_filter (GtkFileChooserDefault *impl)
6381 GtkFileSystemModelFilter filter;
6384 g_assert (impl->browse_files_model != NULL);
6386 if (impl->current_filter)
6388 filter = list_model_filter_func;
6397 _gtk_file_system_model_set_filter (impl->browse_files_model,
6402 #define COMPARE_DIRECTORIES \
6403 GtkFileChooserDefault *impl = user_data; \
6404 GFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6405 GFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6406 gboolean dir_a, dir_b; \
6409 dir_a = _gtk_file_info_consider_as_directory (info_a); \
6411 return impl->list_sort_ascending ? -1 : 1; \
6414 dir_b = _gtk_file_info_consider_as_directory (info_b); \
6416 return impl->list_sort_ascending ? 1 : -1; \
6418 if (dir_a != dir_b) \
6419 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6421 /* Sort callback for the filename column */
6423 name_sort_func (GtkTreeModel *model,
6428 COMPARE_DIRECTORIES;
6431 gchar *key_a, *key_b;
6434 key_a = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_a), -1);
6435 key_b = g_utf8_collate_key_for_filename (g_file_info_get_display_name (info_b), -1);
6436 result = strcmp (key_a, key_b);
6445 /* Sort callback for the size column */
6447 size_sort_func (GtkTreeModel *model,
6452 COMPARE_DIRECTORIES;
6455 goffset size_a = g_file_info_get_size (info_a);
6456 goffset size_b = g_file_info_get_size (info_b);
6458 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6462 /* Sort callback for the mtime column */
6464 mtime_sort_func (GtkTreeModel *model,
6469 COMPARE_DIRECTORIES;
6474 g_file_info_get_modification_time (info_a, &ta);
6475 g_file_info_get_modification_time (info_b, &tb);
6477 return ta.tv_sec > tb.tv_sec ? -1 : (ta.tv_sec == tb.tv_sec ? 0 : 1);
6481 /* Callback used when the sort column changes. We cache the sort order for use
6482 * in name_sort_func().
6485 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6486 GtkFileChooserDefault *impl)
6488 gint sort_column_id;
6489 GtkSortType sort_type;
6491 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6493 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6494 impl->sort_column = model_column_to_general_column (impl, sort_column_id);
6495 impl->sort_order = sort_type;
6500 set_busy_cursor (GtkFileChooserDefault *impl,
6503 GtkWindow *toplevel;
6504 GdkDisplay *display;
6507 toplevel = get_toplevel (GTK_WIDGET (impl));
6508 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6511 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6514 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6518 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6519 gdk_display_flush (display);
6522 gdk_cursor_unref (cursor);
6525 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6527 load_set_model (GtkFileChooserDefault *impl)
6529 profile_start ("start", NULL);
6531 g_assert (impl->browse_files_model != NULL);
6532 g_assert (impl->sort_model == NULL);
6534 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6535 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6536 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6537 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6538 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6539 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6540 set_sort_column (impl);
6541 impl->list_sort_ascending = TRUE;
6542 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6544 g_signal_connect (impl->sort_model, "sort-column-changed",
6545 G_CALLBACK (list_sort_column_changed_cb), impl);
6547 profile_msg (" gtk_tree_view_set_model start", NULL);
6548 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6549 GTK_TREE_MODEL (impl->sort_model));
6550 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6551 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6552 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6553 profile_msg (" gtk_tree_view_set_model end", NULL);
6555 profile_end ("end", NULL);
6558 /* Timeout callback used when the loading timer expires */
6560 load_timeout_cb (gpointer data)
6562 GtkFileChooserDefault *impl;
6564 profile_start ("start", NULL);
6566 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6567 g_assert (impl->load_state == LOAD_PRELOAD);
6568 g_assert (impl->load_timeout_id != 0);
6569 g_assert (impl->browse_files_model != NULL);
6571 impl->load_timeout_id = 0;
6572 impl->load_state = LOAD_LOADING;
6574 load_set_model (impl);
6576 profile_end ("end", NULL);
6581 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6583 load_setup_timer (GtkFileChooserDefault *impl)
6585 g_assert (impl->load_timeout_id == 0);
6586 g_assert (impl->load_state != LOAD_PRELOAD);
6588 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6589 impl->load_state = LOAD_PRELOAD;
6592 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6594 load_remove_timer (GtkFileChooserDefault *impl)
6596 if (impl->load_timeout_id != 0)
6598 g_assert (impl->load_state == LOAD_PRELOAD);
6600 g_source_remove (impl->load_timeout_id);
6601 impl->load_timeout_id = 0;
6602 impl->load_state = LOAD_EMPTY;
6605 g_assert (impl->load_state == LOAD_EMPTY ||
6606 impl->load_state == LOAD_LOADING ||
6607 impl->load_state == LOAD_FINISHED);
6610 /* Selects the first row in the file list */
6612 browse_files_select_first_row (GtkFileChooserDefault *impl)
6615 GtkTreeIter dummy_iter;
6616 GtkTreeModel *tree_model;
6618 if (!impl->sort_model)
6621 path = gtk_tree_path_new_from_indices (0, -1);
6622 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6624 /* If the list is empty, do nothing. */
6625 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6626 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6628 gtk_tree_path_free (path);
6631 struct center_selected_row_closure {
6632 GtkFileChooserDefault *impl;
6633 gboolean already_centered;
6636 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6637 * selected row in the tree view.
6640 center_selected_row_foreach_cb (GtkTreeModel *model,
6645 struct center_selected_row_closure *closure;
6648 if (closure->already_centered)
6651 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6652 closure->already_centered = TRUE;
6655 /* Centers the selected row in the tree view */
6657 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6659 struct center_selected_row_closure closure;
6660 GtkTreeSelection *selection;
6662 closure.impl = impl;
6663 closure.already_centered = FALSE;
6665 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6666 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6669 struct ShowAndSelectPathsData
6671 GtkFileChooserDefault *impl;
6676 show_and_select_files_finished_loading (GtkFolder *folder,
6679 gboolean have_hidden;
6680 gboolean have_filtered;
6682 struct ShowAndSelectPathsData *data = user_data;
6684 have_hidden = FALSE;
6685 have_filtered = FALSE;
6687 for (l = data->files; l; l = l->next)
6694 info = _gtk_folder_get_info (folder, file);
6698 have_hidden = g_file_info_get_is_hidden (info)
6699 || g_file_info_get_is_backup (info);
6702 have_filtered = (! _gtk_file_info_consider_as_directory (info)) &&
6703 get_is_file_filtered (data->impl, file, info);
6705 g_object_unref (info);
6707 if (have_hidden && have_filtered)
6708 break; /* we now have all the information we need */
6712 g_signal_handlers_disconnect_by_func (folder,
6713 show_and_select_files_finished_loading,
6717 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6720 set_current_filter (data->impl, NULL);
6722 for (l = data->files; l; l = l->next)
6729 if (!_gtk_file_system_model_get_iter_for_file (data->impl->browse_files_model,
6734 path = gtk_tree_model_get_path (GTK_TREE_MODEL (data->impl->browse_files_model), &iter);
6735 select_func (data->impl->browse_files_model, path, &iter, data->impl);
6736 gtk_tree_path_free (path);
6739 browse_files_center_selected_row (data->impl);
6741 g_object_unref (data->impl);
6742 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6743 g_slist_free (data->files);
6748 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6750 const GError *error,
6753 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6754 struct ShowAndSelectPathsData *data = user_data;
6756 if (data->impl->show_and_select_files_cancellable != cancellable)
6759 data->impl->show_and_select_files_cancellable = NULL;
6761 if (cancelled || error)
6764 g_object_unref (cancellable);
6766 if (_gtk_folder_is_finished_loading (folder))
6767 show_and_select_files_finished_loading (folder, user_data);
6769 g_signal_connect (folder, "finished-loading",
6770 G_CALLBACK (show_and_select_files_finished_loading),
6776 g_object_unref (data->impl);
6777 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6778 g_slist_free (data->files);
6781 g_object_unref (cancellable);
6785 show_and_select_files (GtkFileChooserDefault *impl,
6790 struct ShowAndSelectPathsData *info;
6792 profile_start ("start", NULL);
6796 profile_end ("end", NULL);
6800 info = g_new (struct ShowAndSelectPathsData, 1);
6801 info->impl = g_object_ref (impl);
6802 info->files = g_slist_copy (files);
6803 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6805 if (impl->show_and_select_files_cancellable)
6806 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6808 impl->show_and_select_files_cancellable =
6809 _gtk_file_system_get_folder (impl->file_system, parent_file,
6810 "standard::is-hidden,standard::is-backup,standard::type,standard::name,standard::content-type",
6811 show_and_select_files_get_folder_cb, info);
6813 profile_end ("end", NULL);
6817 /* Processes the pending operation when a folder is finished loading */
6819 pending_select_files_process (GtkFileChooserDefault *impl)
6821 g_assert (impl->load_state == LOAD_FINISHED);
6822 g_assert (impl->browse_files_model != NULL);
6823 g_assert (impl->sort_model != NULL);
6825 if (impl->pending_select_files)
6828 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6829 pending_select_files_free (impl);
6830 browse_files_center_selected_row (impl);
6834 /* We only select the first row if the chooser is actually mapped ---
6835 * selecting the first row is to help the user when he is interacting with
6836 * the chooser, but sometimes a chooser works not on behalf of the user,
6837 * but rather on behalf of something else like GtkFileChooserButton. In
6838 * that case, the chooser's selection should be what the caller expects,
6839 * as the user can't see that something else got selected. See bug #165264.
6841 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6842 browse_files_select_first_row (impl);
6845 g_assert (impl->pending_select_files == NULL);
6848 /* Callback used when the file system model finishes loading */
6850 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6852 GtkFileChooserDefault *impl)
6854 profile_start ("start", NULL);
6856 if (impl->load_state == LOAD_PRELOAD)
6858 load_remove_timer (impl);
6859 load_set_model (impl);
6861 else if (impl->load_state == LOAD_LOADING)
6867 /* We can't g_assert_not_reached(), as something other than us may have
6868 * initiated a folder reload. See #165556.
6870 profile_end ("end", NULL);
6874 g_assert (impl->load_timeout_id == 0);
6876 impl->load_state = LOAD_FINISHED;
6878 pending_select_files_process (impl);
6879 set_busy_cursor (impl, FALSE);
6880 #ifdef PROFILE_FILE_CHOOSER
6881 access ("MARK: *** FINISHED LOADING", F_OK);
6884 profile_end ("end", NULL);
6888 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6890 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6892 if (impl->browse_files_model)
6894 g_object_unref (impl->browse_files_model);
6895 impl->browse_files_model = NULL;
6898 if (impl->sort_model)
6900 g_object_unref (impl->sort_model);
6901 impl->sort_model = NULL;
6904 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6908 file_system_model_set (GtkFileSystemModel *model,
6917 case GTK_FILE_SYSTEM_MODEL_INFO:
6918 g_value_set_object (value, info);
6920 case GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME:
6922 g_value_set_string (value, g_file_info_get_display_name (info));
6925 g_assert_not_reached ();
6931 /* Gets rid of the old list model and creates a new one for the current folder */
6933 set_list_model (GtkFileChooserDefault *impl,
6936 g_assert (impl->current_folder != NULL);
6938 profile_start ("start", NULL);
6940 stop_loading_and_clear_list_model (impl);
6942 set_busy_cursor (impl, TRUE);
6943 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6945 impl->browse_files_model =
6946 _gtk_file_system_model_new (impl->current_folder,
6947 "standard,time,thumbnail::*",
6948 file_system_model_set,
6950 GTK_FILE_SYSTEM_MODEL_N_COLUMNS,
6954 if (!impl->browse_files_model)
6956 set_busy_cursor (impl, FALSE);
6957 profile_end ("end", NULL);
6961 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6963 g_signal_connect (impl->browse_files_model, "finished-loading",
6964 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6966 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6968 install_list_model_filter (impl);
6970 profile_end ("end", NULL);
6975 struct update_chooser_entry_selected_foreach_closure {
6977 GtkTreeIter first_selected_iter;
6981 compare_utf8_filenames (const gchar *a,
6984 gchar *a_folded, *b_folded;
6987 a_folded = g_utf8_strdown (a, -1);
6988 b_folded = g_utf8_strdown (b, -1);
6990 retval = strcmp (a_folded, b_folded);
6999 update_chooser_entry_selected_foreach (GtkTreeModel *model,
7004 struct update_chooser_entry_selected_foreach_closure *closure;
7007 closure->num_selected++;
7009 if (closure->num_selected == 1)
7010 closure->first_selected_iter = *iter;
7014 update_chooser_entry (GtkFileChooserDefault *impl)
7016 GtkTreeSelection *selection;
7017 struct update_chooser_entry_selected_foreach_closure closure;
7018 const char *file_part;
7020 /* no need to update the file chooser's entry if there's no entry */
7021 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7022 impl->operation_mode == OPERATION_MODE_RECENT ||
7023 !impl->location_entry)
7026 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7027 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7028 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7029 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7030 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
7033 g_assert (impl->location_entry != NULL);
7035 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7036 closure.num_selected = 0;
7037 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
7041 if (closure.num_selected == 0)
7043 goto maybe_clear_entry;
7045 else if (closure.num_selected == 1)
7047 GtkTreeIter child_iter;
7049 if (impl->operation_mode == OPERATION_MODE_BROWSE)
7052 gboolean change_entry;
7054 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7056 &closure.first_selected_iter);
7058 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7060 /* If the cursor moved to the row of the newly created folder,
7061 * retrieving info will return NULL.
7066 g_free (impl->browse_files_last_selected_name);
7067 impl->browse_files_last_selected_name =
7068 g_strdup (g_file_info_get_display_name (info));
7070 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7071 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7072 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7074 /* We don't want the name to change when clicking on a folder... */
7075 change_entry = (! _gtk_file_info_consider_as_directory (info));
7078 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7082 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7084 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7085 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7093 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7094 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7096 /* Multiple selection, so just clear the entry. */
7098 g_free (impl->browse_files_last_selected_name);
7099 impl->browse_files_last_selected_name = NULL;
7101 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7107 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7108 && impl->browse_files_last_selected_name)
7110 const char *entry_text;
7112 gboolean clear_entry;
7114 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7115 len = strlen (entry_text);
7118 /* The file chooser entry may have appended a "/" to its text. So
7119 * take it out, and compare the result to the old selection.
7121 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7125 tmp = g_strndup (entry_text, len - 1);
7126 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7130 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7133 clear_entry = FALSE;
7136 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7141 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7145 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7149 struct UpdateCurrentFolderData
7151 GtkFileChooserDefault *impl;
7153 gboolean keep_trail;
7154 gboolean clear_entry;
7155 GFile *original_file;
7156 GError *original_error;
7160 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7161 GtkFileSystemVolume *volume,
7162 const GError *error,
7165 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7166 struct UpdateCurrentFolderData *data = user_data;
7167 GtkFileChooserDefault *impl = data->impl;
7169 if (cancellable != impl->update_current_folder_cancellable)
7172 impl->update_current_folder_cancellable = NULL;
7173 set_busy_cursor (impl, FALSE);
7180 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7181 impl->reload_state = RELOAD_EMPTY;
7185 change_folder_and_display_error (impl, data->file, data->clear_entry);
7188 g_object_unref (data->file);
7191 g_object_unref (cancellable);
7195 update_current_folder_get_info_cb (GCancellable *cancellable,
7197 const GError *error,
7200 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7201 struct UpdateCurrentFolderData *data = user_data;
7202 GtkFileChooserDefault *impl = data->impl;
7204 if (cancellable != impl->update_current_folder_cancellable)
7207 impl->update_current_folder_cancellable = NULL;
7208 impl->reload_state = RELOAD_EMPTY;
7210 set_busy_cursor (impl, FALSE);
7219 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7221 GMountOperation *mount_operation;
7222 GtkWidget *toplevel;
7224 g_object_unref (cancellable);
7225 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7227 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7229 set_busy_cursor (impl, TRUE);
7231 impl->update_current_folder_cancellable =
7232 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7234 update_current_folder_mount_enclosing_volume_cb,
7240 if (!data->original_file)
7242 data->original_file = g_object_ref (data->file);
7243 data->original_error = g_error_copy (error);
7246 parent_file = g_file_get_parent (data->file);
7248 /* get parent path and try to change the folder to that */
7251 g_object_unref (data->file);
7252 data->file = parent_file;
7254 g_object_unref (cancellable);
7256 /* restart the update current folder operation */
7257 impl->reload_state = RELOAD_HAS_FOLDER;
7259 impl->update_current_folder_cancellable =
7260 _gtk_file_system_get_info (impl->file_system, data->file,
7262 update_current_folder_get_info_cb,
7265 set_busy_cursor (impl, TRUE);
7271 /* Error and bail out, ignoring "not found" errors since they're useless:
7272 * they only happen when a program defaults to a folder that has been (re)moved.
7274 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7275 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7277 g_error_free (data->original_error);
7279 g_object_unref (data->original_file);
7285 if (data->original_file)
7287 /* Error and bail out, ignoring "not found" errors since they're useless:
7288 * they only happen when a program defaults to a folder that has been (re)moved.
7290 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7291 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7293 g_error_free (data->original_error);
7295 g_object_unref (data->original_file);
7298 if (! _gtk_file_info_consider_as_directory (info))
7301 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7304 if (impl->current_folder != data->file)
7306 if (impl->current_folder)
7307 g_object_unref (impl->current_folder);
7309 impl->current_folder = g_object_ref (data->file);
7312 impl->reload_state = RELOAD_HAS_FOLDER;
7314 /* Update the widgets that may trigger a folder change themselves. */
7316 if (!impl->changing_folder)
7318 impl->changing_folder = TRUE;
7320 shortcuts_update_current_folder (impl);
7322 impl->changing_folder = FALSE;
7325 /* Set the folder on the save entry */
7327 if (impl->location_entry)
7329 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7330 impl->current_folder);
7332 if (data->clear_entry)
7333 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7336 /* Create a new list model. This is slightly evil; we store the result value
7337 * but perform more actions rather than returning immediately even if it
7338 * generates an error.
7340 set_list_model (impl, NULL);
7342 /* Refresh controls */
7344 shortcuts_find_current_folder (impl);
7346 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7348 check_preview_change (impl);
7349 bookmarks_check_add_sensitivity (impl);
7351 g_signal_emit_by_name (impl, "selection-changed", 0);
7354 g_object_unref (data->file);
7357 g_object_unref (cancellable);
7361 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7363 gboolean keep_trail,
7364 gboolean clear_entry,
7367 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7368 struct UpdateCurrentFolderData *data;
7370 profile_start ("start", NULL);
7372 g_object_ref (file);
7374 switch (impl->operation_mode)
7376 case OPERATION_MODE_SEARCH:
7377 search_switch_to_browse_mode (impl);
7379 case OPERATION_MODE_RECENT:
7380 recent_switch_to_browse_mode (impl);
7382 case OPERATION_MODE_BROWSE:
7386 if (impl->local_only && !g_file_is_native (file))
7388 g_set_error_literal (error,
7389 GTK_FILE_CHOOSER_ERROR,
7390 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7391 _("Cannot change to folder because it is not local"));
7393 g_object_unref (file);
7394 profile_end ("end - not local", NULL);
7398 if (impl->update_current_folder_cancellable)
7399 g_cancellable_cancel (impl->update_current_folder_cancellable);
7401 /* Test validity of path here. */
7402 data = g_new0 (struct UpdateCurrentFolderData, 1);
7404 data->file = g_object_ref (file);
7405 data->keep_trail = keep_trail;
7406 data->clear_entry = clear_entry;
7408 impl->reload_state = RELOAD_HAS_FOLDER;
7410 impl->update_current_folder_cancellable =
7411 _gtk_file_system_get_info (impl->file_system, file,
7413 update_current_folder_get_info_cb,
7416 set_busy_cursor (impl, TRUE);
7417 g_object_unref (file);
7419 profile_end ("end", NULL);
7424 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7426 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7428 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7429 impl->operation_mode == OPERATION_MODE_RECENT)
7432 if (impl->reload_state == RELOAD_EMPTY)
7434 char *current_working_dir;
7437 /* We are unmapped, or we had an error while loading the last folder. We'll return
7438 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7439 * explicitly calls set_current_folder() on us.
7441 current_working_dir = g_get_current_dir ();
7442 file = g_file_new_for_path (current_working_dir);
7443 g_free (current_working_dir);
7447 if (impl->current_folder)
7448 return g_object_ref (impl->current_folder);
7454 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7457 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7459 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7460 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7462 pending_select_files_free (impl);
7463 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7467 select_func (GtkFileSystemModel *model,
7472 GtkFileChooserDefault *impl = user_data;
7473 GtkTreeSelection *selection;
7474 GtkTreeIter sorted_iter;
7476 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7478 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
7479 gtk_tree_selection_select_iter (selection, &sorted_iter);
7483 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7487 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7491 parent_file = g_file_get_parent (file);
7494 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7496 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7497 impl->operation_mode == OPERATION_MODE_RECENT ||
7498 impl->load_state == LOAD_EMPTY)
7504 g_assert (impl->current_folder != NULL);
7506 same_path = g_file_equal (parent_file, impl->current_folder);
7509 if (same_path && impl->load_state == LOAD_FINISHED)
7514 files.data = (gpointer) file;
7517 result = show_and_select_files (impl, parent_file, &files, error);
7518 g_object_unref (parent_file);
7522 pending_select_files_add (impl, file);
7528 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7529 g_object_unref (parent_file);
7533 g_object_unref (parent_file);
7538 unselect_func (GtkFileSystemModel *model,
7543 GtkFileChooserDefault *impl = user_data;
7544 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7545 GtkTreePath *sorted_path;
7547 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7549 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7551 gtk_tree_path_free (sorted_path);
7555 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7558 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7562 if (!impl->browse_files_model)
7565 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7570 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
7571 unselect_func (impl->browse_files_model, path, &iter, impl);
7572 gtk_tree_path_free (path);
7576 maybe_select (GtkTreeModel *model,
7581 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7582 GtkTreeSelection *selection;
7586 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7588 info = get_list_file_info (impl, iter);
7589 is_folder = _gtk_file_info_consider_as_directory (info);
7591 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7592 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7593 gtk_tree_selection_select_iter (selection, iter);
7595 gtk_tree_selection_unselect_iter (selection, iter);
7601 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7603 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7605 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7606 impl->operation_mode == OPERATION_MODE_RECENT)
7608 GtkTreeSelection *selection;
7610 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7611 gtk_tree_selection_select_all (selection);
7615 if (impl->select_multiple)
7616 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7617 maybe_select, impl);
7621 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7623 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7624 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7626 gtk_tree_selection_unselect_all (selection);
7627 pending_select_files_free (impl);
7630 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7632 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7634 * is_empty_ret - whether the file entry is totally empty
7636 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7637 * the path will be "$cwd/foobar")
7640 check_save_entry (GtkFileChooserDefault *impl,
7642 gboolean *is_well_formed_ret,
7643 gboolean *is_empty_ret,
7644 gboolean *is_file_part_empty_ret,
7645 gboolean *is_folder)
7647 GtkFileChooserEntry *chooser_entry;
7648 GFile *current_folder;
7649 const char *file_part;
7653 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7654 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7655 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7656 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7657 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7659 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7661 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7664 *is_well_formed_ret = TRUE;
7665 *is_empty_ret = TRUE;
7666 *is_file_part_empty_ret = TRUE;
7672 *is_empty_ret = FALSE;
7674 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7675 if (!current_folder)
7678 *is_well_formed_ret = FALSE;
7679 *is_file_part_empty_ret = FALSE;
7685 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7687 if (!file_part || file_part[0] == '\0')
7689 *file_ret = g_object_ref (current_folder);
7690 *is_well_formed_ret = TRUE;
7691 *is_file_part_empty_ret = TRUE;
7697 *is_file_part_empty_ret = FALSE;
7700 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7704 error_building_filename_dialog (impl, error);
7706 *is_well_formed_ret = FALSE;
7713 *is_well_formed_ret = TRUE;
7714 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7717 struct get_files_closure {
7718 GtkFileChooserDefault *impl;
7720 GFile *file_from_entry;
7724 get_files_foreach (GtkTreeModel *model,
7729 struct get_files_closure *info;
7731 GtkFileSystemModel *fs_model;
7732 GtkTreeIter sel_iter;
7735 fs_model = info->impl->browse_files_model;
7736 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7738 file = _gtk_file_system_model_get_file (fs_model, &sel_iter);
7740 return; /* We are on the editable row */
7742 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7743 info->result = g_slist_prepend (info->result, g_object_ref (file));
7747 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7749 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7750 struct get_files_closure info;
7751 GtkWindow *toplevel;
7752 GtkWidget *current_focus;
7753 gboolean file_list_seen;
7755 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7756 return search_get_selected_files (impl);
7758 if (impl->operation_mode == OPERATION_MODE_RECENT)
7759 return recent_get_selected_files (impl);
7763 info.file_from_entry = NULL;
7765 toplevel = get_toplevel (GTK_WIDGET (impl));
7767 current_focus = gtk_window_get_focus (toplevel);
7769 current_focus = NULL;
7771 file_list_seen = FALSE;
7772 if (current_focus == impl->browse_files_tree_view)
7774 GtkTreeSelection *selection;
7778 file_list_seen = TRUE;
7779 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7780 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7782 /* If there is no selection in the file list, we probably have this situation:
7784 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7785 * 2. He then double-clicked on a folder ("bar") in the file list
7787 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7788 * filename entry to see if that is the case.
7790 if (info.result == NULL && impl->location_entry)
7793 else if (impl->location_entry && current_focus == impl->location_entry)
7795 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7799 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7804 if (!is_well_formed)
7807 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7809 g_object_unref (info.file_from_entry);
7813 if (info.file_from_entry)
7814 info.result = g_slist_prepend (info.result, info.file_from_entry);
7815 else if (!file_list_seen)
7820 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7822 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7826 /* The focus is on a dialog's action area button or something else */
7827 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7828 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7836 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7837 * fall back to the current directory */
7838 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7839 info.result == NULL)
7841 GFile *current_folder;
7843 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7846 info.result = g_slist_prepend (info.result, current_folder);
7849 return g_slist_reverse (info.result);
7853 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7855 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7857 if (impl->preview_file)
7858 return g_object_ref (impl->preview_file);
7863 static GtkFileSystem *
7864 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7866 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7868 return impl->file_system;
7871 /* Shows or hides the filter widgets */
7873 show_filters (GtkFileChooserDefault *impl,
7877 gtk_widget_show (impl->filter_combo_hbox);
7879 gtk_widget_hide (impl->filter_combo_hbox);
7883 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7884 GtkFileFilter *filter)
7886 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7889 if (g_slist_find (impl->filters, filter))
7891 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7895 g_object_ref_sink (filter);
7896 impl->filters = g_slist_append (impl->filters, filter);
7898 name = gtk_file_filter_get_name (filter);
7900 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7902 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7904 if (!g_slist_find (impl->filters, impl->current_filter))
7905 set_current_filter (impl, filter);
7907 show_filters (impl, TRUE);
7911 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7912 GtkFileFilter *filter)
7914 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7915 GtkTreeModel *model;
7919 filter_index = g_slist_index (impl->filters, filter);
7921 if (filter_index < 0)
7923 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7927 impl->filters = g_slist_remove (impl->filters, filter);
7929 if (filter == impl->current_filter)
7932 set_current_filter (impl, impl->filters->data);
7934 set_current_filter (impl, NULL);
7937 /* Remove row from the combo box */
7938 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7939 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7940 g_assert_not_reached ();
7942 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7944 g_object_unref (filter);
7947 show_filters (impl, FALSE);
7951 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7953 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7955 return g_slist_copy (impl->filters);
7958 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7960 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7963 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7966 struct AddShortcutData
7968 GtkFileChooserDefault *impl;
7973 add_shortcut_get_info_cb (GCancellable *cancellable,
7975 const GError *error,
7979 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7980 struct AddShortcutData *data = user_data;
7982 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7985 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7987 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7990 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7992 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7995 g_object_unref (data->impl);
7996 g_object_unref (data->file);
7999 g_object_unref (cancellable);
8003 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
8007 GCancellable *cancellable;
8008 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8009 struct AddShortcutData *data;
8013 /* Avoid adding duplicates */
8014 pos = shortcut_find_position (impl, file);
8015 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
8019 uri = g_file_get_uri (file);
8020 /* translators, "Shortcut" means "Bookmark" here */
8022 GTK_FILE_CHOOSER_ERROR,
8023 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8024 _("Shortcut %s already exists"),
8031 for (l = impl->loading_shortcuts; l; l = l->next)
8033 GCancellable *c = l->data;
8036 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8037 if (f && g_file_equal (file, f))
8041 uri = g_file_get_uri (file);
8043 GTK_FILE_CHOOSER_ERROR,
8044 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8045 _("Shortcut %s already exists"),
8053 data = g_new0 (struct AddShortcutData, 1);
8054 data->impl = g_object_ref (impl);
8055 data->file = g_object_ref (file);
8057 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8059 add_shortcut_get_info_cb, data);
8064 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
8065 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
8071 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
8075 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8082 for (l = impl->loading_shortcuts; l; l = l->next)
8084 GCancellable *c = l->data;
8087 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8088 if (f && g_file_equal (file, f))
8090 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8091 g_cancellable_cancel (c);
8096 if (impl->num_shortcuts == 0)
8099 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8100 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8101 g_assert_not_reached ();
8103 for (i = 0; i < impl->num_shortcuts; i++)
8106 ShortcutType shortcut_type;
8109 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8110 SHORTCUTS_COL_DATA, &col_data,
8111 SHORTCUTS_COL_TYPE, &shortcut_type,
8113 g_assert (col_data != NULL);
8114 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8116 shortcut = col_data;
8117 if (g_file_equal (shortcut, file))
8119 shortcuts_remove_rows (impl, pos + i, 1);
8120 impl->num_shortcuts--;
8124 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8125 g_assert_not_reached ();
8130 uri = g_file_get_uri (file);
8131 /* translators, "Shortcut" means "Bookmark" here */
8133 GTK_FILE_CHOOSER_ERROR,
8134 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8135 _("Shortcut %s does not exist"),
8143 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8145 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8151 if (impl->num_shortcuts == 0)
8154 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8155 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8156 g_assert_not_reached ();
8160 for (i = 0; i < impl->num_shortcuts; i++)
8163 ShortcutType shortcut_type;
8166 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8167 SHORTCUTS_COL_DATA, &col_data,
8168 SHORTCUTS_COL_TYPE, &shortcut_type,
8170 g_assert (col_data != NULL);
8171 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8173 shortcut = col_data;
8174 list = g_slist_prepend (list, g_object_ref (shortcut));
8176 if (i != impl->num_shortcuts - 1)
8178 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8179 g_assert_not_reached ();
8183 return g_slist_reverse (list);
8186 /* Guesses a size based upon font sizes */
8188 find_good_size_from_style (GtkWidget *widget,
8192 GtkFileChooserDefault *impl;
8197 g_assert (widget->style != NULL);
8198 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
8200 screen = gtk_widget_get_screen (widget);
8203 resolution = gdk_screen_get_resolution (screen);
8204 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8208 resolution = 96.0; /* wheeee */
8210 font_size = pango_font_description_get_size (widget->style->font_desc);
8211 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
8213 *width = font_size * NUM_CHARS;
8214 *height = font_size * NUM_LINES;
8218 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8219 gint *default_width,
8220 gint *default_height)
8222 GtkFileChooserDefault *impl;
8225 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8227 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8228 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8229 || impl->expand_folders)
8231 GtkFileChooserSettings *settings;
8232 int x, y, width, height;
8234 settings = _gtk_file_chooser_settings_new ();
8235 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
8236 g_object_unref (settings);
8238 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8240 *default_width = width;
8241 *default_height = height;
8245 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8247 if (impl->preview_widget_active &&
8248 impl->preview_widget &&
8249 GTK_WIDGET_VISIBLE (impl->preview_widget))
8251 gtk_widget_size_request (impl->preview_box, &req);
8252 *default_width += PREVIEW_HBOX_SPACING + req.width;
8255 if (impl->extra_widget &&
8256 GTK_WIDGET_VISIBLE (impl->extra_widget))
8258 gtk_widget_size_request (impl->extra_align, &req);
8259 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
8264 gtk_widget_size_request (GTK_WIDGET (impl), &req);
8265 *default_width = req.width;
8266 *default_height = req.height;
8270 struct switch_folder_closure {
8271 GtkFileChooserDefault *impl;
8276 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8278 switch_folder_foreach_cb (GtkTreeModel *model,
8283 struct switch_folder_closure *closure;
8284 GtkTreeIter child_iter;
8288 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
8290 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, &child_iter);
8291 closure->num_selected++;
8294 /* Changes to the selected folder in the list view */
8296 switch_to_selected_folder (GtkFileChooserDefault *impl)
8298 GtkTreeSelection *selection;
8299 struct switch_folder_closure closure;
8301 /* We do this with foreach() rather than get_selected() as we may be in
8302 * multiple selection mode
8305 closure.impl = impl;
8306 closure.file = NULL;
8307 closure.num_selected = 0;
8309 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8310 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8312 g_assert (closure.file && closure.num_selected == 1);
8314 change_folder_and_display_error (impl, closure.file, FALSE);
8317 /* Gets the GFileInfo for the selected row in the file list; assumes single
8321 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8322 gboolean *had_selection)
8324 GtkTreeSelection *selection;
8325 GtkTreeIter iter, child_iter;
8328 g_assert (!impl->select_multiple);
8329 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8330 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8332 *had_selection = FALSE;
8336 *had_selection = TRUE;
8338 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8342 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8346 /* Gets the display name of the selected file in the file list; assumes single
8347 * selection mode and that something is selected.
8349 static const gchar *
8350 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8353 gboolean had_selection;
8355 info = get_selected_file_info_from_file_list (impl, &had_selection);
8356 g_assert (had_selection);
8357 g_assert (info != NULL);
8359 return g_file_info_get_display_name (info);
8363 add_custom_button_to_dialog (GtkDialog *dialog,
8364 const gchar *mnemonic_label,
8365 const gchar *stock_id,
8370 button = gtk_button_new_with_mnemonic (mnemonic_label);
8371 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8372 gtk_button_set_image (GTK_BUTTON (button),
8373 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8374 gtk_widget_show (button);
8376 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8379 /* Presents an overwrite confirmation dialog; returns whether we should accept
8383 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8384 const gchar *file_part,
8385 const gchar *folder_display_name)
8387 GtkWindow *toplevel;
8391 toplevel = get_toplevel (GTK_WIDGET (impl));
8393 dialog = gtk_message_dialog_new (toplevel,
8394 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8395 GTK_MESSAGE_QUESTION,
8397 _("A file named \"%s\" already exists. Do you want to replace it?"),
8399 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8400 _("The file already exists in \"%s\". Replacing it will "
8401 "overwrite its contents."),
8402 folder_display_name);
8404 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8405 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8406 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8407 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8408 GTK_RESPONSE_ACCEPT,
8409 GTK_RESPONSE_CANCEL,
8411 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8413 if (toplevel->group)
8414 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8416 response = gtk_dialog_run (GTK_DIALOG (dialog));
8418 gtk_widget_destroy (dialog);
8420 return (response == GTK_RESPONSE_ACCEPT);
8423 struct GetDisplayNameData
8425 GtkFileChooserDefault *impl;
8430 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8432 const GError *error,
8435 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8436 gboolean should_respond = FALSE;
8437 struct GetDisplayNameData *data = user_data;
8439 if (cancellable != data->impl->should_respond_get_info_cancellable)
8442 data->impl->should_respond_get_info_cancellable = NULL;
8448 /* Huh? Did the folder disappear? Let the caller deal with it */
8449 should_respond = TRUE;
8451 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8453 set_busy_cursor (data->impl, FALSE);
8455 g_signal_emit_by_name (data->impl, "response-requested");
8458 g_object_unref (data->impl);
8459 g_free (data->file_part);
8462 g_object_unref (cancellable);
8465 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8466 * should respond. Can get the file part from the file list or the save entry.
8469 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8470 const gchar *file_part,
8473 GtkFileChooserConfirmation conf;
8475 if (!impl->do_overwrite_confirmation)
8478 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8480 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8484 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8486 struct GetDisplayNameData *data;
8488 g_assert (file_part != NULL);
8490 data = g_new0 (struct GetDisplayNameData, 1);
8491 data->impl = g_object_ref (impl);
8492 data->file_part = g_strdup (file_part);
8494 if (impl->should_respond_get_info_cancellable)
8495 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8497 impl->should_respond_get_info_cancellable =
8498 _gtk_file_system_get_info (impl->file_system, parent_file,
8499 "standard::display-name",
8500 confirmation_confirm_get_info_cb,
8502 set_busy_cursor (data->impl, TRUE);
8506 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8509 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8513 g_assert_not_reached ();
8518 struct FileExistsData
8520 GtkFileChooserDefault *impl;
8521 gboolean file_exists_and_is_not_folder;
8527 save_entry_get_info_cb (GCancellable *cancellable,
8529 const GError *error,
8532 gboolean parent_is_folder;
8533 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8534 struct FileExistsData *data = user_data;
8536 if (cancellable != data->impl->should_respond_get_info_cancellable)
8539 data->impl->should_respond_get_info_cancellable = NULL;
8541 set_busy_cursor (data->impl, FALSE);
8547 parent_is_folder = FALSE;
8549 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8551 if (parent_is_folder)
8553 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8555 if (data->file_exists_and_is_not_folder)
8560 /* Dup the string because the string may be modified
8561 * depending on what clients do in the confirm-overwrite
8562 * signal and this corrupts the pointer
8564 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8565 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8569 g_signal_emit_by_name (data->impl, "response-requested");
8572 g_signal_emit_by_name (data->impl, "response-requested");
8574 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8576 GError *error = NULL;
8578 set_busy_cursor (data->impl, TRUE);
8579 g_file_make_directory (data->file, NULL, &error);
8580 set_busy_cursor (data->impl, FALSE);
8583 g_signal_emit_by_name (data->impl, "response-requested");
8585 error_creating_folder_dialog (data->impl, data->file, error);
8590 /* This will display an error, which is what we want */
8591 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8595 g_object_unref (data->impl);
8596 g_object_unref (data->file);
8597 g_object_unref (data->parent_file);
8600 g_object_unref (cancellable);
8604 file_exists_get_info_cb (GCancellable *cancellable,
8606 const GError *error,
8609 gboolean data_ownership_taken = FALSE;
8610 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8611 gboolean file_exists_and_is_not_folder;
8612 struct FileExistsData *data = user_data;
8614 if (cancellable != data->impl->file_exists_get_info_cancellable)
8617 data->impl->file_exists_get_info_cancellable = NULL;
8619 set_busy_cursor (data->impl, FALSE);
8624 file_exists_and_is_not_folder = info && (! _gtk_file_info_consider_as_directory (info));
8626 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8627 /* user typed a filename; we are done */
8628 g_signal_emit_by_name (data->impl, "response-requested");
8629 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8630 && file_exists_and_is_not_folder)
8632 /* Oops, the user typed the name of an existing path which is not
8635 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8636 g_error_copy (error));
8640 /* check that everything up to the last component exists */
8642 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8643 data_ownership_taken = TRUE;
8645 if (data->impl->should_respond_get_info_cancellable)
8646 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8648 data->impl->should_respond_get_info_cancellable =
8649 _gtk_file_system_get_info (data->impl->file_system,
8652 save_entry_get_info_cb,
8654 set_busy_cursor (data->impl, TRUE);
8658 if (!data_ownership_taken)
8660 g_object_unref (data->impl);
8661 g_object_unref (data->file);
8662 g_object_unref (data->parent_file);
8666 g_object_unref (cancellable);
8670 paste_text_received (GtkClipboard *clipboard,
8672 GtkFileChooserDefault *impl)
8679 file = g_file_new_for_uri (text);
8681 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8682 location_popup_handler (impl, text);
8684 g_object_unref (file);
8687 /* Handler for the "location-popup-on-paste" keybinding signal */
8689 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8691 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8692 GDK_SELECTION_CLIPBOARD);
8693 gtk_clipboard_request_text (clipboard,
8694 (GtkClipboardTextReceivedFunc) paste_text_received,
8699 /* Implementation for GtkFileChooserEmbed::should_respond() */
8701 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8703 GtkFileChooserDefault *impl;
8704 GtkWidget *toplevel;
8705 GtkWidget *current_focus;
8707 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8709 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8710 g_assert (GTK_IS_WINDOW (toplevel));
8712 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8714 if (current_focus == impl->browse_files_tree_view)
8716 /* The following array encodes what we do based on the impl->action and the
8717 * number of files selected.
8720 NOOP, /* Do nothing (don't respond) */
8721 RESPOND, /* Respond immediately */
8722 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8723 ALL_FILES, /* Respond only if everything selected is a file */
8724 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8725 SAVE_ENTRY, /* Go to the code for handling the save entry */
8726 NOT_REACHED /* Sanity check */
8728 static const ActionToTake what_to_do[4][3] = {
8729 /* 0 selected 1 selected many selected */
8730 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8731 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8732 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8733 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8737 gboolean all_files, all_folders;
8739 ActionToTake action;
8743 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8745 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8746 return search_should_respond (impl);
8748 if (impl->operation_mode == OPERATION_MODE_RECENT)
8749 return recent_should_respond (impl);
8751 selection_check (impl, &num_selected, &all_files, &all_folders);
8753 if (num_selected > 2)
8758 action = what_to_do [impl->action] [k];
8768 case RESPOND_OR_SWITCH:
8769 g_assert (num_selected == 1);
8773 switch_to_selected_folder (impl);
8776 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8777 return should_respond_after_confirm_overwrite (impl,
8778 get_display_name_from_file_list (impl),
8779 impl->current_folder);
8793 g_assert_not_reached ();
8796 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8799 gboolean is_well_formed, is_empty, is_file_part_empty;
8802 GtkFileChooserEntry *entry;
8807 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8808 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8809 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8810 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8811 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8813 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8814 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8816 if (is_empty || !is_well_formed)
8819 g_assert (file != NULL);
8824 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8825 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8827 change_folder_and_display_error (impl, file, TRUE);
8830 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8831 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8833 /* The folder already exists, so we do not need to create it.
8834 * Just respond to terminate the dialog.
8840 g_assert_not_reached ();
8846 struct FileExistsData *data;
8848 /* We need to check whether file exists and is not a folder */
8850 data = g_new0 (struct FileExistsData, 1);
8851 data->impl = g_object_ref (impl);
8852 data->file = g_object_ref (file);
8853 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8855 if (impl->file_exists_get_info_cancellable)
8856 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8858 impl->file_exists_get_info_cancellable =
8859 _gtk_file_system_get_info (impl->file_system, file,
8861 file_exists_get_info_cb,
8864 set_busy_cursor (impl, TRUE);
8868 g_error_free (error);
8871 g_object_unref (file);
8874 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8876 /* The focus is on a dialog's action area button, *and* the widget that
8877 * was focused immediately before it is the file list.
8881 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8883 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8886 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8888 /* The focus is on a dialog's action area button, *and* the widget that
8889 * was focused immediately before it is the location entry.
8894 /* The focus is on a dialog's action area button or something else */
8895 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8896 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8901 g_assert_not_reached ();
8905 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8907 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8909 GtkFileChooserDefault *impl;
8912 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8914 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8915 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8917 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8918 widget = impl->browse_files_tree_view;
8920 widget = impl->location_entry;
8922 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8923 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8924 widget = impl->location_entry;
8927 g_assert_not_reached ();
8931 g_assert (widget != NULL);
8932 gtk_widget_grab_focus (widget);
8935 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8937 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8947 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_FILE, &file, -1);
8948 *list = g_slist_prepend (*list, g_object_ref (file));
8951 /* Constructs a list of the selected paths in search mode */
8953 search_get_selected_files (GtkFileChooserDefault *impl)
8956 GtkTreeSelection *selection;
8960 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8961 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8962 result = g_slist_reverse (result);
8967 /* Called from ::should_respond(). We return whether there are selected files
8968 * in the search list.
8971 search_should_respond (GtkFileChooserDefault *impl)
8973 GtkTreeSelection *selection;
8975 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8977 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8978 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8981 struct SearchHitInsertRequest
8983 GtkFileChooserDefault *impl;
8985 GtkTreeRowReference *row_ref;
8989 search_hit_get_info_cb (GCancellable *cancellable,
8991 const GError *error,
8994 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8995 GdkPixbuf *pixbuf = NULL;
8998 GCancellable *model_cancellable;
8999 gboolean is_folder = FALSE;
9001 guint64 modification_time = 0;
9005 struct SearchHitInsertRequest *request = data;
9007 if (!request->impl->search_model)
9010 path = gtk_tree_row_reference_get_path (request->row_ref);
9014 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
9016 gtk_tree_path_free (path);
9018 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
9019 SEARCH_MODEL_COL_CANCELLABLE, &model_cancellable,
9021 if (cancellable != model_cancellable)
9024 /* set the cancellable to NULL in the model */
9025 gtk_list_store_set (request->impl->search_model, &iter,
9026 SEARCH_MODEL_COL_CANCELLABLE, NULL,
9034 search_clear_model_row (GTK_TREE_MODEL (request->impl->search_model), &iter);
9035 gtk_list_store_remove (request->impl->search_model, &iter);
9039 display_name = g_strdup (g_file_info_get_display_name (info));
9040 mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (info));
9041 g_file_info_get_modification_time (info, &mtime);
9042 modification_time = (guint64) mtime.tv_sec;
9043 size = g_file_info_get_size (info);
9044 is_folder = _gtk_file_info_consider_as_directory (info);
9045 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
9046 request->impl->icon_size);
9048 gtk_list_store_set (request->impl->search_model, &iter,
9049 SEARCH_MODEL_COL_PIXBUF, pixbuf,
9050 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
9051 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
9052 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
9053 SEARCH_MODEL_COL_MTIME, modification_time,
9054 SEARCH_MODEL_COL_SIZE, size,
9058 g_object_unref (pixbuf);
9061 g_object_unref (request->impl);
9062 g_object_unref (request->file);
9063 gtk_tree_row_reference_free (request->row_ref);
9066 g_object_unref (cancellable);
9069 /* Adds one hit from the search engine to the search_model */
9071 search_add_hit (GtkFileChooserDefault *impl,
9076 char *collation_key;
9079 GCancellable *cancellable;
9080 struct SearchHitInsertRequest *request;
9082 file = g_file_new_for_uri (uri);
9086 if (!g_file_is_native (file))
9088 g_object_unref (file);
9092 tmp = g_file_get_parse_name (file);
9093 collation_key = g_utf8_collate_key_for_filename (tmp, -1);
9096 request = g_new0 (struct SearchHitInsertRequest, 1);
9097 request->impl = g_object_ref (impl);
9098 request->file = g_object_ref (file);
9100 gtk_list_store_append (impl->search_model, &iter);
9101 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
9103 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
9104 gtk_tree_path_free (p);
9106 cancellable = _gtk_file_system_get_info (impl->file_system, file,
9109 "standard::content-type,"
9110 "standard::display-name,"
9113 search_hit_get_info_cb,
9116 gtk_list_store_set (impl->search_model, &iter,
9117 SEARCH_MODEL_COL_FILE, file,
9118 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
9119 SEARCH_MODEL_COL_CANCELLABLE, cancellable,
9123 /* Callback used from GtkSearchEngine when we get new hits */
9125 search_engine_hits_added_cb (GtkSearchEngine *engine,
9129 GtkFileChooserDefault *impl;
9132 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9134 for (l = hits; l; l = l->next)
9135 search_add_hit (impl, (gchar*)l->data);
9138 /* Callback used from GtkSearchEngine when the query is done running */
9140 search_engine_finished_cb (GtkSearchEngine *engine,
9143 GtkFileChooserDefault *impl;
9145 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9148 /* EB: setting the model here will avoid loads of row events,
9149 * but it'll make the search look like blocked.
9151 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9152 GTK_TREE_MODEL (impl->search_model_filter));
9155 /* FMQ: if search was empty, say that we got no hits */
9156 set_busy_cursor (impl, FALSE);
9159 /* Displays a generic error when we cannot create a GtkSearchEngine.
9160 * It would be better if _gtk_search_engine_new() gave us a GError
9161 * with a better message, but it doesn't do that right now.
9164 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9166 error_message (impl,
9167 _("Could not start the search process"),
9168 _("The program was not able to create a connection to the indexer "
9169 "daemon. Please make sure it is running."));
9173 search_engine_error_cb (GtkSearchEngine *engine,
9174 const gchar *message,
9177 GtkFileChooserDefault *impl;
9179 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9181 search_stop_searching (impl, TRUE);
9182 error_message (impl, _("Could not send the search request"), message);
9184 set_busy_cursor (impl, FALSE);
9188 search_clear_model_row (GtkTreeModel *model,
9192 gchar *display_name;
9193 gchar *collation_key;
9194 GCancellable *cancellable;
9197 gtk_tree_model_get (model, iter,
9198 SEARCH_MODEL_COL_FILE, &file,
9199 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9200 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key,
9201 SEARCH_MODEL_COL_CANCELLABLE, &cancellable,
9202 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9206 g_object_unref (file);
9208 g_free (display_name);
9209 g_free (collation_key);
9213 g_cancellable_cancel (cancellable);
9216 /* Frees the data in the search_model */
9218 search_clear_model (GtkFileChooserDefault *impl,
9219 gboolean remove_from_treeview)
9221 GtkTreeModel *model;
9224 if (!impl->search_model)
9227 model = GTK_TREE_MODEL (impl->search_model);
9229 if (gtk_tree_model_get_iter_first (model, &iter))
9232 search_clear_model_row (model, &iter);
9234 while (gtk_tree_model_iter_next (model, &iter));
9236 g_object_unref (impl->search_model);
9237 impl->search_model = NULL;
9239 g_object_unref (impl->search_model_filter);
9240 impl->search_model_filter = NULL;
9242 g_object_unref (impl->search_model_sort);
9243 impl->search_model_sort = NULL;
9245 if (remove_from_treeview)
9246 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9249 /* Stops any ongoing searches; does not touch the search_model */
9251 search_stop_searching (GtkFileChooserDefault *impl,
9252 gboolean remove_query)
9254 if (remove_query && impl->search_query)
9256 g_object_unref (impl->search_query);
9257 impl->search_query = NULL;
9260 if (impl->search_engine)
9262 _gtk_search_engine_stop (impl->search_engine);
9264 g_object_unref (impl->search_engine);
9265 impl->search_engine = NULL;
9269 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
9271 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
9273 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9275 search_stop_searching (impl, FALSE);
9276 search_clear_model (impl, TRUE);
9278 gtk_widget_destroy (impl->search_hbox);
9279 impl->search_hbox = NULL;
9280 impl->search_entry = NULL;
9282 gtk_widget_show (impl->browse_path_bar);
9283 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9284 gtk_widget_hide (impl->browse_new_folder_button);
9286 gtk_widget_show (impl->browse_new_folder_button);
9289 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9290 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9292 gtk_widget_show (impl->location_button);
9294 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9295 gtk_widget_show (impl->location_entry_box);
9298 impl->operation_mode = OPERATION_MODE_BROWSE;
9300 file_list_set_sort_column_ids (impl);
9303 /* Sort callback from the path column */
9305 search_column_path_sort_func (GtkTreeModel *model,
9310 GtkFileChooserDefault *impl = user_data;
9311 GtkTreeIter child_a, child_b;
9312 const char *collation_key_a, *collation_key_b;
9313 gboolean is_folder_a, is_folder_b;
9315 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9316 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9318 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9319 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9320 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a,
9322 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9323 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9324 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b,
9327 if (!collation_key_a)
9330 if (!collation_key_b)
9333 /* always show folders first */
9334 if (is_folder_a != is_folder_b)
9335 return is_folder_a ? 1 : -1;
9337 return strcmp (collation_key_a, collation_key_b);
9340 /* Sort callback from the size column */
9342 search_column_size_sort_func (GtkTreeModel *model,
9347 GtkFileChooserDefault *impl = user_data;
9348 GtkTreeIter child_a, child_b;
9349 gboolean is_folder_a, is_folder_b;
9350 goffset size_a, size_b;
9352 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9353 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9355 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9356 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9357 SEARCH_MODEL_COL_SIZE, &size_a,
9359 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9360 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9361 SEARCH_MODEL_COL_SIZE, &size_b,
9364 if (is_folder_a != is_folder_b)
9365 return is_folder_a ? 1 : -1;
9367 if (size_a < size_b)
9369 else if (size_a > size_b)
9375 /* Sort callback from the modification time column */
9377 search_column_mtime_sort_func (GtkTreeModel *model,
9382 GtkFileChooserDefault *impl = user_data;
9383 GtkTreeIter child_a, child_b;
9384 gboolean is_folder_a, is_folder_b;
9385 guint64 mtime_a, mtime_b;
9387 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9388 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9390 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_a,
9391 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_a,
9392 SEARCH_MODEL_COL_MTIME, &mtime_a,
9394 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_b,
9395 SEARCH_MODEL_COL_IS_FOLDER, &is_folder_b,
9396 SEARCH_MODEL_COL_MTIME, &mtime_b,
9399 if (is_folder_a != is_folder_b)
9400 return is_folder_a ? 1 : -1;
9402 if (mtime_a < mtime_b)
9404 else if (mtime_a > mtime_b)
9411 search_get_is_filtered (GtkFileChooserDefault *impl,
9413 const gchar *display_name,
9414 const gchar *mime_type)
9416 GtkFileFilterInfo filter_info;
9417 GtkFileFilterFlags needed;
9420 if (!impl->current_filter)
9423 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9424 needed = gtk_file_filter_get_needed (impl->current_filter);
9426 filter_info.display_name = display_name;
9427 filter_info.mime_type = mime_type;
9429 if (needed & GTK_FILE_FILTER_FILENAME)
9431 filter_info.filename = g_file_get_path (file);
9432 if (filter_info.filename)
9433 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9436 filter_info.filename = NULL;
9438 if (needed & GTK_FILE_FILTER_URI)
9440 filter_info.uri = g_file_get_uri (file);
9441 if (filter_info.uri)
9442 filter_info.contains |= GTK_FILE_FILTER_URI;
9445 filter_info.uri = NULL;
9447 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9449 if (filter_info.filename)
9450 g_free ((gchar *) filter_info.filename);
9451 if (filter_info.uri)
9452 g_free ((gchar *) filter_info.uri);
9458 /* Visibility function for the recent filter model */
9460 search_model_visible_func (GtkTreeModel *model,
9464 GtkFileChooserDefault *impl = user_data;
9466 gchar *display_name, *mime_type;
9469 if (!impl->current_filter)
9472 gtk_tree_model_get (model, iter,
9473 SEARCH_MODEL_COL_FILE, &file,
9474 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
9475 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
9476 SEARCH_MODEL_COL_MIME_TYPE, &mime_type,
9485 return !search_get_is_filtered (impl, file, display_name, mime_type);
9488 /* Creates the search_model and puts it in the tree view */
9490 search_setup_model (GtkFileChooserDefault *impl)
9492 g_assert (impl->search_model == NULL);
9493 g_assert (impl->search_model_filter == NULL);
9494 g_assert (impl->search_model_sort == NULL);
9496 /* We store these columns in the search model:
9498 * SEARCH_MODEL_COL_FILE - a GFile for the hit's URI, stored
9499 * as a pointer not as a G_TYPE_FILE
9500 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
9501 * as a pointer not as a G_TYPE_STRING
9502 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
9503 * as a pointer not as a G_TYPE_STRING
9504 * SEARCH_MODEL_COL_MTIME - G_TYPE_UINT64 for the modification time
9505 * SEARCH_MODEL_COL_SIZE - G_TYPE_INT64 for the size
9506 * SEARCH_MODEL_COL_CANCELLABLE - cancellable used when getting the hit's info
9507 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
9508 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
9509 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
9511 * Keep this in sync with the enumeration defined near the beginning
9514 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
9515 G_TYPE_POINTER, /* file */
9516 G_TYPE_POINTER, /* display-name */
9517 G_TYPE_POINTER, /* collation-key */
9518 G_TYPE_UINT64, /* mtime */
9519 G_TYPE_INT64, /* size */
9520 G_TYPE_POINTER, /* cancellable */
9521 GDK_TYPE_PIXBUF, /* pixbuf */
9522 G_TYPE_POINTER, /* mime-type */
9523 G_TYPE_BOOLEAN /*is-folder */);
9525 impl->search_model_filter =
9526 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->search_model), NULL));
9527 gtk_tree_model_filter_set_visible_func (impl->search_model_filter,
9528 search_model_visible_func,
9531 impl->search_model_sort =
9532 GTK_TREE_MODEL_SORT (search_model_sort_new (impl, GTK_TREE_MODEL (impl->search_model_filter)));
9533 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9534 SEARCH_MODEL_COL_FILE,
9535 search_column_path_sort_func,
9537 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9538 SEARCH_MODEL_COL_MTIME,
9539 search_column_mtime_sort_func,
9541 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model_sort),
9542 SEARCH_MODEL_COL_SIZE,
9543 search_column_size_sort_func,
9545 set_sort_column (impl);
9547 /* EB: setting the model here will make the hits list update feel
9548 * more "alive" than setting the model at the end of the search
9551 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9552 GTK_TREE_MODEL (impl->search_model_sort));
9556 search_get_valid_child_iter (GtkFileChooserDefault *impl,
9557 GtkTreeIter *child_iter,
9562 if (!impl->search_model)
9565 if (!impl->search_model_filter || !impl->search_model_sort)
9568 /* pass 1: get the iterator in the filter model */
9569 gtk_tree_model_sort_convert_iter_to_child_iter (impl->search_model_sort,
9572 /* pass 2: get the iterator in the real model */
9573 gtk_tree_model_filter_convert_iter_to_child_iter (impl->search_model_filter,
9574 child_iter, &middle);
9577 /* Creates a new query with the specified text and launches it */
9579 search_start_query (GtkFileChooserDefault *impl,
9580 const gchar *query_text)
9582 search_stop_searching (impl, FALSE);
9583 search_clear_model (impl, TRUE);
9584 search_setup_model (impl);
9585 set_busy_cursor (impl, TRUE);
9587 if (impl->search_engine == NULL)
9588 impl->search_engine = _gtk_search_engine_new ();
9590 if (!impl->search_engine)
9592 set_busy_cursor (impl, FALSE);
9593 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9597 if (!impl->search_query)
9599 impl->search_query = _gtk_query_new ();
9600 _gtk_query_set_text (impl->search_query, query_text);
9603 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9605 g_signal_connect (impl->search_engine, "hits-added",
9606 G_CALLBACK (search_engine_hits_added_cb), impl);
9607 g_signal_connect (impl->search_engine, "finished",
9608 G_CALLBACK (search_engine_finished_cb), impl);
9609 g_signal_connect (impl->search_engine, "error",
9610 G_CALLBACK (search_engine_error_cb), impl);
9612 _gtk_search_engine_start (impl->search_engine);
9615 /* Callback used when the user presses Enter while typing on the search
9616 * entry; starts the query
9619 search_entry_activate_cb (GtkEntry *entry,
9622 GtkFileChooserDefault *impl;
9625 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9627 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9628 if (strlen (text) == 0)
9631 /* reset any existing query object */
9632 if (impl->search_query)
9634 g_object_unref (impl->search_query);
9635 impl->search_query = NULL;
9638 search_start_query (impl, text);
9641 /* Hides the path bar and creates the search entry */
9643 search_setup_widgets (GtkFileChooserDefault *impl)
9649 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9653 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9654 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), image);
9655 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9659 label = gtk_label_new (NULL);
9660 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9661 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9662 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9667 impl->search_entry = gtk_entry_new ();
9668 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9669 g_signal_connect (impl->search_entry, "activate",
9670 G_CALLBACK (search_entry_activate_cb),
9672 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9674 /* if there already is a query, restart it */
9675 if (impl->search_query)
9677 gchar *query = _gtk_query_get_text (impl->search_query);
9681 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9682 search_start_query (impl, query);
9688 g_object_unref (impl->search_query);
9689 impl->search_query = NULL;
9693 gtk_widget_hide (impl->browse_path_bar);
9694 gtk_widget_hide (impl->browse_new_folder_button);
9696 /* Box for search widgets */
9697 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9698 gtk_widget_show_all (impl->search_hbox);
9700 /* Hide the location widgets temporarily */
9702 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9703 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9705 gtk_widget_hide (impl->location_button);
9706 gtk_widget_hide (impl->location_entry_box);
9709 gtk_widget_grab_focus (impl->search_entry);
9711 /* FMQ: hide the filter combo? */
9714 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9716 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9720 case OPERATION_MODE_BROWSE:
9721 stop_loading_and_clear_list_model (impl);
9724 case OPERATION_MODE_SEARCH:
9725 search_stop_searching (impl, FALSE);
9726 search_clear_model (impl, TRUE);
9728 gtk_widget_destroy (impl->search_hbox);
9729 impl->search_hbox = NULL;
9730 impl->search_entry = NULL;
9733 case OPERATION_MODE_RECENT:
9734 recent_stop_loading (impl);
9735 recent_clear_model (impl, TRUE);
9737 gtk_widget_destroy (impl->recent_hbox);
9738 impl->recent_hbox = NULL;
9743 /* Main entry point to the searching functions; this gets called when the user
9744 * activates the Search shortcut.
9747 search_activate (GtkFileChooserDefault *impl)
9749 OperationMode previous_mode;
9751 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9753 gtk_widget_grab_focus (impl->search_entry);
9757 previous_mode = impl->operation_mode;
9758 impl->operation_mode = OPERATION_MODE_SEARCH;
9760 stop_operation (impl, previous_mode);
9762 g_assert (impl->search_hbox == NULL);
9763 g_assert (impl->search_entry == NULL);
9764 g_assert (impl->search_model == NULL);
9765 g_assert (impl->search_model_filter == NULL);
9767 search_setup_widgets (impl);
9768 file_list_set_sort_column_ids (impl);
9772 * Recent files support
9775 /* Frees the data in the recent_model */
9777 recent_clear_model (GtkFileChooserDefault *impl,
9778 gboolean remove_from_treeview)
9780 GtkTreeModel *model;
9783 if (!impl->recent_model)
9786 model = GTK_TREE_MODEL (impl->recent_model);
9788 if (remove_from_treeview)
9789 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9791 if (gtk_tree_model_get_iter_first (model, &iter))
9796 GCancellable *cancellable;
9797 GtkRecentInfo *recent_info;
9798 gchar *display_name;
9800 gtk_tree_model_get (model, &iter,
9801 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9802 RECENT_MODEL_COL_FILE, &file,
9803 RECENT_MODEL_COL_CANCELLABLE, &cancellable,
9804 RECENT_MODEL_COL_INFO, &recent_info,
9808 g_cancellable_cancel (cancellable);
9810 g_object_unref (file);
9811 gtk_recent_info_unref (recent_info);
9812 g_free (display_name);
9814 while (gtk_tree_model_iter_next (model, &iter));
9817 g_object_unref (impl->recent_model);
9818 impl->recent_model = NULL;
9820 g_object_unref (impl->recent_model_filter);
9821 impl->recent_model_filter = NULL;
9823 g_object_unref (impl->recent_model_sort);
9824 impl->recent_model_sort = NULL;
9827 /* Stops any ongoing loading of the recent files list; does
9828 * not touch the recent_model
9831 recent_stop_loading (GtkFileChooserDefault *impl)
9833 if (impl->load_recent_id)
9835 g_source_remove (impl->load_recent_id);
9836 impl->load_recent_id = 0;
9840 /* Stops any pending load, clears the file list, and switches
9841 * back to OPERATION_MODE_BROWSE
9844 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9846 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9848 recent_stop_loading (impl);
9849 recent_clear_model (impl, TRUE);
9851 gtk_widget_destroy (impl->recent_hbox);
9852 impl->recent_hbox = NULL;
9854 gtk_widget_show (impl->browse_path_bar);
9855 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9856 gtk_widget_hide (impl->browse_new_folder_button);
9858 gtk_widget_show (impl->browse_new_folder_button);
9860 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9861 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9863 gtk_widget_show (impl->location_button);
9865 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9866 gtk_widget_show (impl->location_entry_box);
9869 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9871 impl->operation_mode = OPERATION_MODE_BROWSE;
9873 file_list_set_sort_column_ids (impl);
9876 /* Sort callback from the modification time column */
9878 recent_column_mtime_sort_func (GtkTreeModel *model,
9883 GtkFileChooserDefault *impl = user_data;
9884 GtkTreeIter child_a, child_b;
9885 GtkRecentInfo *info_a, *info_b;
9886 gboolean is_folder_a, is_folder_b;
9888 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9889 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9891 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9892 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9893 RECENT_MODEL_COL_INFO, &info_a,
9895 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9896 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9897 RECENT_MODEL_COL_INFO, &info_b,
9906 /* folders always go first */
9907 if (is_folder_a != is_folder_b)
9908 return is_folder_a ? 1 : -1;
9910 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9912 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9919 recent_column_path_sort_func (GtkTreeModel *model,
9924 GtkFileChooserDefault *impl = user_data;
9925 GtkTreeIter child_a, child_b;
9926 gboolean is_folder_a, is_folder_b;
9927 gchar *name_a, *name_b;
9929 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_a, a);
9930 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_b, b);
9932 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_a,
9933 RECENT_MODEL_COL_IS_FOLDER, &is_folder_a,
9934 RECENT_MODEL_COL_DISPLAY_NAME, &name_a,
9936 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_b,
9937 RECENT_MODEL_COL_IS_FOLDER, &is_folder_b,
9938 RECENT_MODEL_COL_DISPLAY_NAME, &name_b,
9947 if (is_folder_a != is_folder_b)
9948 return is_folder_a ? 1 : -1;
9950 return strcmp (name_a, name_b);
9954 recent_get_is_filtered (GtkFileChooserDefault *impl,
9956 GtkRecentInfo *recent_info)
9958 GtkFileFilterInfo filter_info;
9959 GtkFileFilterFlags needed;
9962 if (!impl->current_filter)
9965 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
9966 needed = gtk_file_filter_get_needed (impl->current_filter);
9968 filter_info.display_name = gtk_recent_info_get_display_name (recent_info);
9969 filter_info.mime_type = gtk_recent_info_get_mime_type (recent_info);
9971 if (needed & GTK_FILE_FILTER_FILENAME)
9973 filter_info.filename = g_file_get_path (file);
9974 if (filter_info.filename)
9975 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
9978 filter_info.filename = NULL;
9980 if (needed & GTK_FILE_FILTER_URI)
9982 filter_info.uri = g_file_get_uri (file);
9983 if (filter_info.uri)
9984 filter_info.contains |= GTK_FILE_FILTER_URI;
9987 filter_info.uri = NULL;
9989 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
9991 if (filter_info.filename)
9992 g_free ((gchar *) filter_info.filename);
9993 if (filter_info.uri)
9994 g_free ((gchar *) filter_info.uri);
9999 /* Visibility function for the recent filter model */
10001 recent_model_visible_func (GtkTreeModel *model,
10003 gpointer user_data)
10005 GtkFileChooserDefault *impl = user_data;
10007 GtkRecentInfo *recent_info;
10008 gboolean is_folder;
10010 if (!impl->current_filter)
10013 gtk_tree_model_get (model, iter,
10014 RECENT_MODEL_COL_INFO, &recent_info,
10015 RECENT_MODEL_COL_FILE, &file,
10016 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10025 return !recent_get_is_filtered (impl, file, recent_info);
10029 recent_setup_model (GtkFileChooserDefault *impl)
10031 g_assert (impl->recent_model == NULL);
10032 g_assert (impl->recent_model_filter == NULL);
10033 g_assert (impl->recent_model_sort == NULL);
10035 /* We store these columns in the search model:
10037 * RECENT_MODEL_COL_FILE - a pointer to GFile for the hit's URI,
10038 * stored as a pointer and not as a G_TYPE_FILE;
10039 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
10040 * stored as a pointer and not as a G_TYPE_STRING;
10041 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
10042 * as a GTK_TYPE_RECENT_INFO;
10043 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
10044 * RECENT_MODEL_COL_CANCELLABLE - GCancellable, stored as a pointer
10045 * and not as a G_TYPE_CANCELLABLE;
10047 * Keep this in sync with the enumeration defined near the beginning of
10050 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
10057 impl->recent_model_filter =
10058 GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->recent_model), NULL));
10059 gtk_tree_model_filter_set_visible_func (impl->recent_model_filter,
10060 recent_model_visible_func,
10064 /* this is the model that will actually be added to
10065 * the browse_files_tree_view widget; remember: we are
10066 * stuffing the real model into a filter model and then
10067 * into a sort model; this means we'll have to translate
10068 * the child iterator *twice* to get from a path or an
10069 * iterator coming from the tree view widget to the
10070 * real data inside the model.
10072 impl->recent_model_sort =
10073 GTK_TREE_MODEL_SORT (recent_model_sort_new (impl, GTK_TREE_MODEL (impl->recent_model_filter)));
10074 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
10075 RECENT_MODEL_COL_FILE,
10076 recent_column_path_sort_func,
10078 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model_sort),
10079 RECENT_MODEL_COL_INFO,
10080 recent_column_mtime_sort_func,
10082 set_sort_column (impl);
10087 GtkFileChooserDefault *impl;
10090 gint n_loaded_items;
10091 guint needs_sorting : 1;
10095 recent_idle_cleanup (gpointer data)
10097 RecentLoadData *load_data = data;
10098 GtkFileChooserDefault *impl = load_data->impl;
10100 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
10101 GTK_TREE_MODEL (impl->recent_model_sort));
10103 set_busy_cursor (impl, FALSE);
10105 impl->load_recent_id = 0;
10107 if (load_data->items)
10109 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
10110 g_list_free (load_data->items);
10113 g_free (load_data);
10116 struct RecentItemInsertRequest
10118 GtkFileChooserDefault *impl;
10120 GtkTreeRowReference *row_ref;
10124 recent_item_get_info_cb (GCancellable *cancellable,
10126 const GError *error,
10129 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10132 GCancellable *model_cancellable;
10133 gboolean is_folder = FALSE;
10134 struct RecentItemInsertRequest *request = data;
10136 if (!request->impl->recent_model)
10139 path = gtk_tree_row_reference_get_path (request->row_ref);
10143 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
10145 gtk_tree_path_free (path);
10147 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
10148 RECENT_MODEL_COL_CANCELLABLE, &model_cancellable,
10150 if (cancellable != model_cancellable)
10153 gtk_list_store_set (request->impl->recent_model, &iter,
10154 RECENT_MODEL_COL_CANCELLABLE, NULL,
10162 gtk_list_store_remove (request->impl->recent_model, &iter);
10166 is_folder = _gtk_file_info_consider_as_directory (info);
10168 gtk_list_store_set (request->impl->recent_model, &iter,
10169 RECENT_MODEL_COL_IS_FOLDER, is_folder,
10173 g_object_unref (request->impl);
10174 g_object_unref (request->file);
10175 gtk_tree_row_reference_free (request->row_ref);
10178 g_object_unref (cancellable);
10182 recent_sort_mru (gconstpointer a,
10185 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
10186 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
10188 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
10192 get_recent_files_limit (GtkWidget *widget)
10194 GtkSettings *settings;
10197 if (gtk_widget_has_screen (widget))
10198 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
10200 settings = gtk_settings_get_default ();
10202 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
10208 recent_idle_load (gpointer data)
10210 RecentLoadData *load_data = data;
10211 GtkFileChooserDefault *impl = load_data->impl;
10214 GtkRecentInfo *info;
10215 const gchar *uri, *display_name;
10217 GCancellable *cancellable;
10218 struct RecentItemInsertRequest *request;
10220 if (!impl->recent_manager)
10223 /* first iteration: load all the items */
10224 if (!load_data->items)
10226 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
10227 if (!load_data->items)
10230 load_data->needs_sorting = TRUE;
10235 /* second iteration: preliminary MRU sorting and clamping */
10236 if (load_data->needs_sorting)
10240 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
10241 load_data->n_items = g_list_length (load_data->items);
10243 limit = get_recent_files_limit (GTK_WIDGET (impl));
10245 if (limit != -1 && (load_data->n_items > limit))
10249 clamp = g_list_nth (load_data->items, limit - 1);
10250 if (G_LIKELY (clamp))
10253 clamp->next = NULL;
10255 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
10258 load_data->n_items = limit;
10262 load_data->n_loaded_items = 0;
10263 load_data->needs_sorting = FALSE;
10268 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
10269 g_assert (info != NULL);
10271 uri = gtk_recent_info_get_uri (info);
10272 display_name = gtk_recent_info_get_display_name (info);
10273 file = g_file_new_for_uri (uri);
10275 gtk_list_store_append (impl->recent_model, &iter);
10276 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
10278 request = g_new0 (struct RecentItemInsertRequest, 1);
10279 request->impl = g_object_ref (impl);
10280 request->file = g_object_ref (file);
10281 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
10282 gtk_tree_path_free (p);
10284 cancellable = _gtk_file_system_get_info (impl->file_system, file,
10286 recent_item_get_info_cb,
10289 gtk_list_store_set (impl->recent_model, &iter,
10290 RECENT_MODEL_COL_FILE, file,
10291 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
10292 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
10293 RECENT_MODEL_COL_CANCELLABLE, cancellable,
10296 load_data->n_loaded_items += 1;
10298 /* finished loading items */
10299 if (load_data->n_loaded_items == load_data->n_items)
10301 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
10302 g_list_free (load_data->items);
10303 load_data->items = NULL;
10312 recent_start_loading (GtkFileChooserDefault *impl)
10314 RecentLoadData *load_data;
10316 recent_stop_loading (impl);
10317 recent_clear_model (impl, TRUE);
10318 recent_setup_model (impl);
10319 set_busy_cursor (impl, TRUE);
10321 g_assert (impl->load_recent_id == 0);
10323 load_data = g_new (RecentLoadData, 1);
10324 load_data->impl = impl;
10325 load_data->items = NULL;
10326 load_data->n_items = 0;
10327 load_data->n_loaded_items = 0;
10328 load_data->needs_sorting = TRUE;
10330 /* begin lazy loading the recent files into the model */
10331 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
10334 recent_idle_cleanup);
10338 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
10348 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_FILE, &file, -1);
10349 *list = g_slist_prepend (*list, g_object_ref (file));
10352 /* Constructs a list of the selected paths in recent files mode */
10354 recent_get_selected_files (GtkFileChooserDefault *impl)
10357 GtkTreeSelection *selection;
10361 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10362 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
10363 result = g_slist_reverse (result);
10368 /* Called from ::should_respond(). We return whether there are selected
10369 * files in the recent files list.
10372 recent_should_respond (GtkFileChooserDefault *impl)
10374 GtkTreeSelection *selection;
10376 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
10378 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
10379 return (gtk_tree_selection_count_selected_rows (selection) != 0);
10382 /* Hide the location widgets temporarily */
10384 recent_hide_entry (GtkFileChooserDefault *impl)
10390 impl->recent_hbox = gtk_hbox_new (FALSE, 12);
10393 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
10394 gtk_size_group_add_widget (impl->browse_path_bar_size_group, image);
10395 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
10398 label = gtk_label_new (NULL);
10399 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
10400 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
10401 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
10404 gtk_widget_hide (impl->browse_path_bar);
10405 gtk_widget_hide (impl->browse_new_folder_button);
10407 /* Box for recent widgets */
10408 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
10409 gtk_widget_show_all (impl->recent_hbox);
10411 /* Hide the location widgets temporarily */
10412 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10413 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10415 gtk_widget_hide (impl->location_button);
10416 gtk_widget_hide (impl->location_entry_box);
10420 /* Main entry point to the recent files functions; this gets called when
10421 * the user activates the Recently Used shortcut.
10424 recent_activate (GtkFileChooserDefault *impl)
10426 OperationMode previous_mode;
10428 if (impl->operation_mode == OPERATION_MODE_RECENT)
10431 previous_mode = impl->operation_mode;
10432 impl->operation_mode = OPERATION_MODE_RECENT;
10434 stop_operation (impl, previous_mode);
10436 recent_hide_entry (impl);
10438 /* hide the file size column if it's visible */
10439 gtk_tree_view_column_set_visible (impl->list_size_column, FALSE);
10441 file_list_set_sort_column_ids (impl);
10442 recent_start_loading (impl);
10445 /* convert an iterator coming from the model bound to
10446 * browse_files_tree_view to an interator inside the
10447 * real recent_model
10450 recent_get_valid_child_iter (GtkFileChooserDefault *impl,
10451 GtkTreeIter *child_iter,
10454 GtkTreeIter middle;
10456 if (!impl->recent_model)
10459 if (!impl->recent_model_filter || !impl->recent_model_sort)
10462 /* pass 1: get the iterator in the filter model */
10463 gtk_tree_model_sort_convert_iter_to_child_iter (impl->recent_model_sort,
10466 /* pass 2: get the iterator in the real model */
10467 gtk_tree_model_filter_convert_iter_to_child_iter (impl->recent_model_filter,
10474 set_current_filter (GtkFileChooserDefault *impl,
10475 GtkFileFilter *filter)
10477 if (impl->current_filter != filter)
10481 /* NULL filters are allowed to reset to non-filtered status
10483 filter_index = g_slist_index (impl->filters, filter);
10484 if (impl->filters && filter && filter_index < 0)
10487 if (impl->current_filter)
10488 g_object_unref (impl->current_filter);
10489 impl->current_filter = filter;
10490 if (impl->current_filter)
10492 g_object_ref_sink (impl->current_filter);
10496 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
10499 if (impl->browse_files_model)
10500 install_list_model_filter (impl);
10502 if (impl->operation_mode == OPERATION_MODE_SEARCH &&
10503 impl->search_model_filter != NULL)
10504 gtk_tree_model_filter_refilter (impl->search_model_filter);
10506 /* we want to have all the matching results, and not just a
10507 * filter of the previous model
10509 if (impl->operation_mode == OPERATION_MODE_RECENT)
10510 recent_start_loading (impl);
10512 g_object_notify (G_OBJECT (impl), "filter");
10517 filter_combo_changed (GtkComboBox *combo_box,
10518 GtkFileChooserDefault *impl)
10520 gint new_index = gtk_combo_box_get_active (combo_box);
10521 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
10523 set_current_filter (impl, new_filter);
10527 check_preview_change (GtkFileChooserDefault *impl)
10529 GtkTreePath *cursor_path;
10531 const char *new_display_name;
10533 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
10535 new_display_name = NULL;
10538 GtkTreeIter child_iter;
10540 if (impl->operation_mode == OPERATION_MODE_BROWSE)
10542 if (impl->sort_model)
10545 GFileInfo *new_info;
10547 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
10548 gtk_tree_path_free (cursor_path);
10550 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10552 new_file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
10553 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10555 new_display_name = g_file_info_get_display_name (new_info);
10558 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
10562 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort),
10563 &iter, cursor_path);
10564 gtk_tree_path_free (cursor_path);
10566 search_get_valid_child_iter (impl, &child_iter, &iter);
10567 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10568 SEARCH_MODEL_COL_FILE, &new_file,
10569 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
10572 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10576 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort),
10577 &iter, cursor_path);
10578 gtk_tree_path_free (cursor_path);
10580 recent_get_valid_child_iter (impl, &child_iter, &iter);
10581 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10582 RECENT_MODEL_COL_FILE, &new_file,
10583 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
10588 if (new_file != impl->preview_file &&
10589 !(new_file && impl->preview_file &&
10590 g_file_equal (new_file, impl->preview_file)))
10592 if (impl->preview_file)
10594 g_object_unref (impl->preview_file);
10595 g_free (impl->preview_display_name);
10600 impl->preview_file = g_object_ref (new_file);
10601 impl->preview_display_name = g_strdup (new_display_name);
10605 impl->preview_file = NULL;
10606 impl->preview_display_name = NULL;
10609 if (impl->use_preview_label && impl->preview_label)
10610 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
10612 g_signal_emit_by_name (impl, "update-preview");
10617 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
10618 GtkFileSystemVolume *volume,
10619 const GError *error,
10623 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10624 GtkFileChooserDefault *impl = data;
10626 if (cancellable != impl->shortcuts_activate_iter_cancellable)
10629 impl->shortcuts_activate_iter_cancellable = NULL;
10631 set_busy_cursor (impl, FALSE);
10638 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
10642 name = _gtk_file_system_volume_get_display_name (volume);
10643 msg = g_strdup_printf (_("Could not mount %s"), name);
10645 error_message (impl, msg, error->message);
10654 file = _gtk_file_system_volume_get_root (volume);
10657 change_folder_and_display_error (impl, file, FALSE);
10658 g_object_unref (file);
10662 g_object_unref (impl);
10663 g_object_unref (cancellable);
10667 /* Activates a volume by mounting it if necessary and then switching to its
10671 shortcuts_activate_volume (GtkFileChooserDefault *impl,
10672 GtkFileSystemVolume *volume)
10676 switch (impl->operation_mode)
10678 case OPERATION_MODE_BROWSE:
10680 case OPERATION_MODE_SEARCH:
10681 search_switch_to_browse_mode (impl);
10683 case OPERATION_MODE_RECENT:
10684 recent_switch_to_browse_mode (impl);
10688 /* We ref the file chooser since volume_mount() may run a main loop, and the
10689 * user could close the file chooser window in the meantime.
10691 g_object_ref (impl);
10693 if (!_gtk_file_system_volume_is_mounted (volume))
10695 GMountOperation *mount_op;
10697 set_busy_cursor (impl, TRUE);
10699 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
10700 impl->shortcuts_activate_iter_cancellable =
10701 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
10702 shortcuts_activate_volume_mount_cb,
10703 g_object_ref (impl));
10704 g_object_unref (mount_op);
10708 file = _gtk_file_system_volume_get_root (volume);
10711 change_folder_and_display_error (impl, file, FALSE);
10712 g_object_unref (file);
10716 g_object_unref (impl);
10719 /* Opens the folder or volume at the specified iter in the shortcuts model */
10720 struct ShortcutsActivateData
10722 GtkFileChooserDefault *impl;
10727 shortcuts_activate_get_info_cb (GCancellable *cancellable,
10729 const GError *error,
10730 gpointer user_data)
10732 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
10733 struct ShortcutsActivateData *data = user_data;
10735 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
10738 data->impl->shortcuts_activate_iter_cancellable = NULL;
10743 if (!error && _gtk_file_info_consider_as_directory (info))
10744 change_folder_and_display_error (data->impl, data->file, FALSE);
10746 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
10751 g_object_unref (data->impl);
10752 g_object_unref (data->file);
10755 g_object_unref (cancellable);
10759 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
10760 GtkFileSystemVolume *volume,
10761 const GError *error,
10762 gpointer user_data)
10764 struct ShortcutsActivateData *data = user_data;
10768 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
10770 g_object_unref (data->impl);
10771 g_object_unref (data->file);
10777 data->impl->shortcuts_activate_iter_cancellable =
10778 _gtk_file_system_get_info (data->impl->file_system, data->file,
10780 shortcuts_activate_get_info_cb, data);
10784 shortcuts_activate_iter (GtkFileChooserDefault *impl,
10788 ShortcutType shortcut_type;
10790 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
10791 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
10792 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10793 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
10795 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
10796 SHORTCUTS_COL_DATA, &col_data,
10797 SHORTCUTS_COL_TYPE, &shortcut_type,
10800 if (impl->shortcuts_activate_iter_cancellable)
10802 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
10803 impl->shortcuts_activate_iter_cancellable = NULL;
10806 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
10808 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
10810 GtkFileSystemVolume *volume;
10814 shortcuts_activate_volume (impl, volume);
10816 else if (shortcut_type == SHORTCUT_TYPE_FILE)
10818 struct ShortcutsActivateData *data;
10819 GtkFileSystemVolume *volume;
10821 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
10823 data = g_new0 (struct ShortcutsActivateData, 1);
10824 data->impl = g_object_ref (impl);
10825 data->file = g_object_ref (col_data);
10827 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
10829 GMountOperation *mount_operation;
10830 GtkWidget *toplevel;
10832 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
10834 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
10836 impl->shortcuts_activate_iter_cancellable =
10837 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
10839 shortcuts_activate_mount_enclosing_volume,
10844 impl->shortcuts_activate_iter_cancellable =
10845 _gtk_file_system_get_info (impl->file_system, data->file,
10847 shortcuts_activate_get_info_cb, data);
10850 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
10852 search_activate (impl);
10854 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
10856 recent_activate (impl);
10860 /* Handler for GtkWidget::key-press-event on the shortcuts list */
10862 shortcuts_key_press_event_cb (GtkWidget *widget,
10863 GdkEventKey *event,
10864 GtkFileChooserDefault *impl)
10868 modifiers = gtk_accelerator_get_default_mod_mask ();
10870 if (key_is_left_or_right (event))
10872 gtk_widget_grab_focus (impl->browse_files_tree_view);
10876 if ((event->keyval == GDK_BackSpace
10877 || event->keyval == GDK_Delete
10878 || event->keyval == GDK_KP_Delete)
10879 && (event->state & modifiers) == 0)
10881 remove_selected_bookmarks (impl);
10885 if ((event->keyval == GDK_F2)
10886 && (event->state & modifiers) == 0)
10888 rename_selected_bookmark (impl);
10896 shortcuts_select_func (GtkTreeSelection *selection,
10897 GtkTreeModel *model,
10899 gboolean path_currently_selected,
10902 GtkFileChooserDefault *impl = data;
10903 GtkTreeIter filter_iter;
10904 ShortcutType shortcut_type;
10906 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10907 g_assert_not_reached ();
10909 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10911 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10915 list_select_func (GtkTreeSelection *selection,
10916 GtkTreeModel *model,
10918 gboolean path_currently_selected,
10921 GtkFileChooserDefault *impl = data;
10923 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10924 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10926 GtkTreeIter iter, child_iter;
10928 switch (impl->operation_mode)
10930 case OPERATION_MODE_SEARCH:
10932 gboolean is_folder;
10934 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
10937 search_get_valid_child_iter (impl, &child_iter, &iter);
10938 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
10939 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
10946 case OPERATION_MODE_RECENT:
10948 gboolean is_folder;
10950 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
10953 recent_get_valid_child_iter (impl, &child_iter, &iter);
10954 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
10955 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10962 case OPERATION_MODE_BROWSE:
10966 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10969 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
10970 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10971 if (info && (! _gtk_file_info_consider_as_directory (info)))
10982 list_selection_changed (GtkTreeSelection *selection,
10983 GtkFileChooserDefault *impl)
10985 /* See if we are in the new folder editable row for Save mode */
10986 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10987 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10990 gboolean had_selection;
10992 info = get_selected_file_info_from_file_list (impl, &had_selection);
10993 if (!had_selection)
10994 goto out; /* normal processing */
10997 return; /* We are on the editable row for New Folder */
11002 if (impl->location_entry)
11003 update_chooser_entry (impl);
11005 check_preview_change (impl);
11006 bookmarks_check_add_sensitivity (impl);
11008 g_signal_emit_by_name (impl, "selection-changed", 0);
11011 /* Callback used when a row in the file list is activated */
11013 list_row_activated (GtkTreeView *tree_view,
11015 GtkTreeViewColumn *column,
11016 GtkFileChooserDefault *impl)
11019 GtkTreeIter child_iter;
11021 switch (impl->operation_mode)
11023 case OPERATION_MODE_SEARCH:
11026 gboolean is_folder;
11028 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model_sort), &iter, path))
11031 search_get_valid_child_iter (impl, &child_iter, &iter);
11032 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11033 SEARCH_MODEL_COL_FILE, &file,
11034 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11039 change_folder_and_display_error (impl, file, FALSE);
11043 g_signal_emit_by_name (impl, "file-activated");
11047 case OPERATION_MODE_RECENT:
11050 gboolean is_folder;
11052 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model_sort), &iter, path))
11055 recent_get_valid_child_iter (impl, &child_iter, &iter);
11056 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11057 RECENT_MODEL_COL_FILE, &file,
11058 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11063 change_folder_and_display_error (impl, file, FALSE);
11067 g_signal_emit_by_name (impl, "file-activated");
11071 case OPERATION_MODE_BROWSE:
11075 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
11078 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
11079 &child_iter, &iter);
11080 info = _gtk_file_system_model_get_info (impl->browse_files_model,
11083 if (_gtk_file_info_consider_as_directory (info))
11085 GFile *file, *target_file;
11086 const gchar *target_uri;
11088 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
11089 if (g_file_info_get_file_type (info) == G_FILE_TYPE_MOUNTABLE ||
11090 g_file_info_get_file_type (info) == G_FILE_TYPE_SHORTCUT)
11092 target_uri = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
11095 target_file = g_file_new_for_uri (target_uri);
11098 g_object_unref (file);
11099 file = target_file;
11105 change_folder_and_display_error (impl, file, FALSE);
11109 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11110 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
11111 g_signal_emit_by_name (impl, "file-activated");
11118 path_bar_clicked (GtkPathBar *path_bar,
11121 gboolean child_is_hidden,
11122 GtkFileChooserDefault *impl)
11125 pending_select_files_add (impl, child_file);
11127 if (!change_folder_and_display_error (impl, file, FALSE))
11130 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
11131 * show hidden files so that ".baz" appears in the file list, as it will still
11132 * be shown in the path bar: "/foo/[bar]/.baz"
11134 if (child_is_hidden)
11135 g_object_set (impl, "show-hidden", TRUE, NULL);
11139 get_list_file_info (GtkFileChooserDefault *impl,
11142 GtkTreeIter child_iter;
11144 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
11148 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
11152 list_icon_data_func (GtkTreeViewColumn *tree_column,
11153 GtkCellRenderer *cell,
11154 GtkTreeModel *tree_model,
11158 GtkFileChooserDefault *impl = data;
11159 GtkTreeIter child_iter;
11160 GdkPixbuf *pixbuf = NULL;
11161 gboolean sensitive = TRUE;
11163 profile_start ("start", NULL);
11165 switch (impl->operation_mode)
11167 case OPERATION_MODE_SEARCH:
11169 GtkTreeIter child_iter;
11170 gboolean is_folder;
11172 search_get_valid_child_iter (impl, &child_iter, iter);
11173 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11174 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
11175 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11178 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11179 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11180 sensitive = is_folder;
11184 case OPERATION_MODE_RECENT:
11186 GtkTreeIter child_iter;
11187 GtkRecentInfo *info;
11188 gboolean is_folder;
11190 recent_get_valid_child_iter (impl, &child_iter, iter);
11191 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11192 RECENT_MODEL_COL_INFO, &info,
11193 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11196 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
11198 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11199 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11200 sensitive = is_folder;
11204 case OPERATION_MODE_BROWSE:
11209 info = get_list_file_info (impl, iter);
11211 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
11214 file = _gtk_file_system_model_get_file (impl->browse_files_model, &child_iter);
11219 /* FIXME: NULL GError */
11220 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size);
11225 /* We are on the editable row */
11230 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11231 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
11232 sensitive = _gtk_file_info_consider_as_directory (info);
11237 g_object_set (cell,
11239 "sensitive", sensitive,
11243 g_object_unref (pixbuf);
11245 profile_end ("end", NULL);
11249 list_name_data_func (GtkTreeViewColumn *tree_column,
11250 GtkCellRenderer *cell,
11251 GtkTreeModel *tree_model,
11255 GtkFileChooserDefault *impl = data;
11257 gboolean sensitive = TRUE;
11259 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11261 GtkTreeIter child_iter;
11262 gchar *display_name;
11263 gboolean is_folder;
11265 search_get_valid_child_iter (impl, &child_iter, iter);
11266 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11267 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
11268 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11271 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11272 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11274 sensitive = is_folder;
11277 g_object_set (cell,
11278 "text", display_name,
11279 "sensitive", sensitive,
11280 "ellipsize", PANGO_ELLIPSIZE_END,
11286 if (impl->operation_mode == OPERATION_MODE_RECENT)
11288 GtkTreeIter child_iter;
11289 GtkRecentInfo *recent_info;
11290 gchar *display_name;
11291 gboolean is_folder;
11293 recent_get_valid_child_iter (impl, &child_iter, iter);
11294 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11295 RECENT_MODEL_COL_INFO, &recent_info,
11296 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11299 display_name = gtk_recent_info_get_short_name (recent_info);
11301 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11302 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11304 sensitive = is_folder;
11307 g_object_set (cell,
11308 "text", display_name,
11309 "sensitive", sensitive,
11310 "ellipsize", PANGO_ELLIPSIZE_END,
11313 g_free (display_name);
11318 info = get_list_file_info (impl, iter);
11323 g_object_set (cell,
11324 "text", DEFAULT_NEW_FOLDER_NAME,
11326 "ellipsize", PANGO_ELLIPSIZE_NONE,
11333 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11334 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11336 sensitive = _gtk_file_info_consider_as_directory (info);
11339 g_object_set (cell,
11340 "text", g_file_info_get_display_name (info),
11341 "sensitive", sensitive,
11342 "ellipsize", PANGO_ELLIPSIZE_END,
11347 list_size_data_func (GtkTreeViewColumn *tree_column,
11348 GtkCellRenderer *cell,
11349 GtkTreeModel *tree_model,
11353 GtkFileChooserDefault *impl = data;
11357 gboolean sensitive = TRUE;
11359 if (impl->operation_mode == OPERATION_MODE_RECENT)
11362 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11364 GtkTreeIter child_iter;
11365 gboolean is_folder = FALSE;
11367 search_get_valid_child_iter (impl, &child_iter, iter);
11368 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11369 SEARCH_MODEL_COL_SIZE, &size,
11370 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11373 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11374 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11375 sensitive = is_folder ? TRUE : FALSE;
11378 str = g_format_size_for_display (size);
11382 g_object_set (cell,
11384 "sensitive", sensitive,
11392 info = get_list_file_info (impl, iter);
11394 if (!info || _gtk_file_info_consider_as_directory (info))
11396 g_object_set (cell,
11398 "sensitive", sensitive,
11403 size = g_file_info_get_size (info);
11404 str = g_format_size_for_display (size);
11406 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11407 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11410 g_object_set (cell,
11412 "sensitive", sensitive,
11418 /* Tree column data callback for the file list; fetches the mtime of a file */
11420 list_mtime_data_func (GtkTreeViewColumn *tree_column,
11421 GtkCellRenderer *cell,
11422 GtkTreeModel *tree_model,
11426 GtkFileChooserDefault *impl;
11427 GTimeVal timeval = { 0, };
11429 gchar *date_str = NULL;
11430 gboolean sensitive = TRUE;
11432 const char *locale, *dot = NULL;
11433 gint64 codepage = -1;
11439 if (impl->operation_mode == OPERATION_MODE_SEARCH)
11441 GtkTreeIter child_iter;
11443 gboolean is_folder;
11445 search_get_valid_child_iter (impl, &child_iter, iter);
11446 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &child_iter,
11447 SEARCH_MODEL_COL_MTIME, &mtime,
11448 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11451 time_mtime = (time_t) mtime;
11453 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11454 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11455 sensitive = is_folder;
11457 else if (impl->operation_mode == OPERATION_MODE_RECENT)
11459 GtkTreeIter child_iter;
11460 GtkRecentInfo *info;
11461 gboolean is_folder;
11463 recent_get_valid_child_iter (impl, &child_iter, iter);
11464 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &child_iter,
11465 RECENT_MODEL_COL_INFO, &info,
11466 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11470 time_mtime = gtk_recent_info_get_modified (info);
11474 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11475 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11476 sensitive = is_folder;
11482 info = get_list_file_info (impl, iter);
11485 g_object_set (cell,
11492 g_file_info_get_modification_time (info, &timeval);
11493 time_mtime = timeval.tv_sec;
11495 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
11496 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11497 sensitive = _gtk_file_info_consider_as_directory (info);
11500 if (G_UNLIKELY (time_mtime == 0))
11501 date_str = g_strdup (_("Unknown"));
11506 struct tm tm_mtime;
11508 const gchar *format;
11509 gchar *locale_format = NULL;
11512 #ifdef HAVE_LOCALTIME_R
11513 localtime_r ((time_t *) &time_mtime, &tm_mtime);
11516 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
11520 g_warning ("ptm != NULL failed");
11522 g_object_set (cell,
11523 "text", _("Unknown"),
11524 "sensitive", sensitive,
11529 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
11531 #endif /* HAVE_LOCALTIME_R */
11533 g_date_set_time_t (&mtime, time_mtime);
11534 time_now = time (NULL);
11535 g_date_set_time_t (&now, time_now);
11537 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
11539 /* Translators: %H means "hours" and %M means "minutes" */
11540 if (days_diff == 0)
11541 format = _("%H:%M");
11542 else if (days_diff == 1)
11543 format = _("Yesterday at %H:%M");
11546 if (days_diff > 1 && days_diff < 7)
11547 format = "%A"; /* Days from last week */
11549 format = "%x"; /* Any other date */
11553 /* g_locale_from_utf8() returns a string in the system
11554 * code-page, which is not always the same as that used by the C
11555 * library. For instance when running a GTK+ program with
11556 * LANG=ko on an English version of Windows, the system
11557 * code-page is 1252, but the code-page used by the C library is
11558 * 949. (It's GTK+ itself that sets the C library locale when it
11559 * notices the LANG environment variable. See gtkmain.c The
11560 * Microsoft C library doesn't look at any locale environment
11561 * variables.) We need to pass strftime() a string in the C
11562 * library's code-page. See bug #509885.
11564 locale = setlocale (LC_ALL, NULL);
11565 if (locale != NULL)
11566 dot = strchr (locale, '.');
11569 codepage = g_ascii_strtoll (dot+1, NULL, 10);
11571 /* All codepages should fit in 16 bits AFAIK */
11572 if (codepage > 0 && codepage < 65536)
11574 sprintf (charset, "CP%u", (guint) codepage);
11575 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
11579 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
11581 if (locale_format != NULL &&
11582 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
11585 /* As above but in opposite direction... */
11586 if (codepage > 0 && codepage < 65536)
11587 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
11589 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
11593 if (date_str == NULL)
11594 date_str = g_strdup (_("Unknown"));
11596 g_free (locale_format);
11599 g_object_set (cell,
11601 "sensitive", sensitive,
11607 _gtk_file_chooser_default_new (void)
11609 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
11613 location_set_user_text (GtkFileChooserDefault *impl,
11616 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
11617 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
11621 location_popup_handler (GtkFileChooserDefault *impl,
11624 if (impl->operation_mode != OPERATION_MODE_BROWSE)
11626 GtkWidget *widget_to_focus;
11628 /* This will give us the location widgets back */
11629 switch (impl->operation_mode)
11631 case OPERATION_MODE_SEARCH:
11632 search_switch_to_browse_mode (impl);
11634 case OPERATION_MODE_RECENT:
11635 recent_switch_to_browse_mode (impl);
11637 case OPERATION_MODE_BROWSE:
11638 g_assert_not_reached ();
11642 if (impl->current_folder)
11643 change_folder_and_display_error (impl, impl->current_folder, FALSE);
11645 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11646 widget_to_focus = impl->browse_files_tree_view;
11648 widget_to_focus = impl->location_entry;
11650 gtk_widget_grab_focus (widget_to_focus);
11654 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
11655 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
11657 LocationMode new_mode;
11661 /* since the user typed something, we unconditionally want to turn on the entry */
11662 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11664 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
11665 new_mode = LOCATION_MODE_FILENAME_ENTRY;
11666 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
11667 new_mode = LOCATION_MODE_PATH_BAR;
11670 g_assert_not_reached ();
11674 location_mode_set (impl, new_mode, TRUE);
11675 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
11678 location_set_user_text (impl, path);
11681 location_entry_set_initial_text (impl);
11682 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
11686 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
11687 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
11689 gtk_widget_grab_focus (impl->location_entry);
11691 location_set_user_text (impl, path);
11694 g_assert_not_reached ();
11697 /* Handler for the "up-folder" keybinding signal */
11699 up_folder_handler (GtkFileChooserDefault *impl)
11701 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
11704 /* Handler for the "down-folder" keybinding signal */
11706 down_folder_handler (GtkFileChooserDefault *impl)
11708 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
11711 /* Switches to the shortcut in the specified index */
11713 switch_to_shortcut (GtkFileChooserDefault *impl,
11718 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
11719 g_assert_not_reached ();
11721 shortcuts_activate_iter (impl, &iter);
11724 /* Handler for the "home-folder" keybinding signal */
11726 home_folder_handler (GtkFileChooserDefault *impl)
11728 if (impl->has_home)
11729 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
11732 /* Handler for the "desktop-folder" keybinding signal */
11734 desktop_folder_handler (GtkFileChooserDefault *impl)
11736 if (impl->has_desktop)
11737 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
11740 /* Handler for the "search-shortcut" keybinding signal */
11742 search_shortcut_handler (GtkFileChooserDefault *impl)
11744 if (impl->has_search)
11746 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
11748 /* we want the entry widget to grab the focus the first
11749 * time, not the browse_files_tree_view widget.
11751 if (impl->search_entry)
11752 gtk_widget_grab_focus (impl->search_entry);
11756 /* Handler for the "recent-shortcut" keybinding signal */
11758 recent_shortcut_handler (GtkFileChooserDefault *impl)
11760 if (impl->has_recent)
11761 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
11765 quick_bookmark_handler (GtkFileChooserDefault *impl,
11766 gint bookmark_index)
11771 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
11774 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
11776 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
11777 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
11780 gtk_tree_path_free (path);
11782 switch_to_shortcut (impl, bookmark_pos);
11786 show_hidden_handler (GtkFileChooserDefault *impl)
11788 g_object_set (impl,
11789 "show-hidden", !impl->show_hidden,
11794 /* Drag and drop interfaces */
11797 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
11802 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
11804 model->impl = NULL;
11807 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
11809 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
11812 ShortcutsPaneModelFilter *model;
11816 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11818 pos = *gtk_tree_path_get_indices (path);
11819 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
11821 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
11824 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
11826 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
11828 GtkSelectionData *selection_data)
11830 ShortcutsPaneModelFilter *model;
11832 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
11839 /* Fill the GtkTreeDragSourceIface vtable */
11841 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11843 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
11844 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
11848 /* Fill the GtkTreeDragDestIface vtable */
11850 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
11852 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
11853 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
11857 static GtkTreeModel *
11858 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
11859 GtkTreeModel *child_model,
11862 ShortcutsPaneModelFilter *model;
11864 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
11865 "child-model", child_model,
11866 "virtual-root", root,
11869 model->impl = impl;
11871 return GTK_TREE_MODEL (model);
11877 recent_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11880 RecentModelSort *model;
11881 GtkTreeIter iter, child_iter;
11882 gboolean is_folder;
11884 model = RECENT_MODEL_SORT (drag_source);
11885 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11888 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11889 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11890 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
11897 recent_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11899 GtkSelectionData *selection_data)
11901 RecentModelSort *model;
11902 GtkTreeIter iter, child_iter;
11906 model = RECENT_MODEL_SORT (drag_source);
11907 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11910 recent_get_valid_child_iter (model->impl, &child_iter, &iter);
11911 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->recent_model), &child_iter,
11912 RECENT_MODEL_COL_FILE, &file,
11914 g_assert (file != NULL);
11916 uris[0] = g_file_get_uri (file);
11919 gtk_selection_data_set_uris (selection_data, uris);
11927 recent_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
11929 iface->row_draggable = recent_model_sort_row_draggable;
11930 iface->drag_data_get = recent_model_sort_drag_data_get;
11934 _recent_model_sort_class_init (RecentModelSortClass *klass)
11940 _recent_model_sort_init (RecentModelSort *model)
11942 model->impl = NULL;
11945 static GtkTreeModel *
11946 recent_model_sort_new (GtkFileChooserDefault *impl,
11947 GtkTreeModel *child_model)
11949 RecentModelSort *model;
11951 model = g_object_new (RECENT_MODEL_SORT_TYPE,
11952 "model", child_model,
11954 model->impl = impl;
11956 return GTK_TREE_MODEL (model);
11962 search_model_sort_row_draggable (GtkTreeDragSource *drag_source,
11965 SearchModelSort *model;
11966 GtkTreeIter iter, child_iter;
11967 gboolean is_folder;
11969 model = SEARCH_MODEL_SORT (drag_source);
11970 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11973 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11974 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11975 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
11982 search_model_sort_drag_data_get (GtkTreeDragSource *drag_source,
11984 GtkSelectionData *selection_data)
11986 SearchModelSort *model;
11987 GtkTreeIter iter, child_iter;
11991 model = SEARCH_MODEL_SORT (drag_source);
11992 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
11995 search_get_valid_child_iter (model->impl, &child_iter, &iter);
11996 gtk_tree_model_get (GTK_TREE_MODEL (model->impl->search_model), &child_iter,
11997 RECENT_MODEL_COL_FILE, &file,
11999 g_assert (file != NULL);
12001 uris[0] = g_file_get_uri (file);
12004 gtk_selection_data_set_uris (selection_data, uris);
12012 search_model_sort_drag_source_iface_init (GtkTreeDragSourceIface *iface)
12014 iface->row_draggable = search_model_sort_row_draggable;
12015 iface->drag_data_get = search_model_sort_drag_data_get;
12019 _search_model_sort_class_init (SearchModelSortClass *klass)
12025 _search_model_sort_init (SearchModelSort *model)
12027 model->impl = NULL;
12030 static GtkTreeModel *
12031 search_model_sort_new (GtkFileChooserDefault *impl,
12032 GtkTreeModel *child_model)
12034 SearchModelSort *model;
12036 model = g_object_new (SEARCH_MODEL_SORT_TYPE,
12037 "model", child_model,
12039 model->impl = impl;
12041 return GTK_TREE_MODEL (model);