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 "gtkfilechooserdefault.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 "gtkcomboboxtext.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdialog.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechooserutils.h"
40 #include "gtkfilechooser.h"
41 #include "gtkfilesystem.h"
42 #include "gtkfilesystemmodel.h"
45 #include "gtkiconfactory.h"
46 #include "gtkicontheme.h"
48 #include "gtkimagemenuitem.h"
50 #include "gtkmarshalers.h"
51 #include "gtkmessagedialog.h"
52 #include "gtkmountoperation.h"
53 #include "gtkpathbar.h"
54 #include "gtkprivate.h"
55 #include "gtkradiobutton.h"
56 #include "gtkrecentfilter.h"
57 #include "gtkrecentmanager.h"
58 #include "gtkscrolledwindow.h"
59 #include "gtkseparatormenuitem.h"
60 #include "gtksizegroup.h"
61 #include "gtksizerequest.h"
64 #include "gtktoolbar.h"
65 #include "gtktoolbutton.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
71 #include "gtkorientable.h"
78 #include <sys/types.h>
89 #undef PROFILE_FILE_CHOOSER
90 #ifdef PROFILE_FILE_CHOOSER
97 #define PROFILE_INDENT 4
98 static int profile_indent;
101 profile_add_indent (int indent)
103 profile_indent += indent;
104 if (profile_indent < 0)
105 g_error ("You screwed up your indentation");
109 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
114 profile_add_indent (indent);
116 if (profile_indent == 0)
117 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
119 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
125 profile_add_indent (indent);
128 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
129 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
130 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
132 #define profile_start(x, y)
133 #define profile_end(x, y)
134 #define profile_msg(x, y)
139 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
141 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
142 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
143 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define MAX_LOADING_TIME 500
147 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
149 struct _GtkFileChooserDefaultClass
151 GtkBoxClass parent_class;
157 LOCATION_POPUP_ON_PASTE,
163 LOCATION_TOGGLE_POPUP,
171 static guint signals[LAST_SIGNAL] = { 0 };
173 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
175 SHORTCUTS_COL_PIXBUF,
179 SHORTCUTS_COL_REMOVABLE,
180 SHORTCUTS_COL_PIXBUF_VISIBLE,
181 SHORTCUTS_COL_CANCELLABLE,
182 SHORTCUTS_COL_NUM_COLUMNS
187 SHORTCUT_TYPE_VOLUME,
188 SHORTCUT_TYPE_SEPARATOR,
189 SHORTCUT_TYPE_SEARCH,
193 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
194 "standard::is-hidden,standard::is-backup,standard::size," \
195 "standard::content-type,time::modified"
197 /* the first 3 must be these due to settings caching sort column */
202 MODEL_COL_NAME_COLLATED,
206 MODEL_COL_MTIME_TEXT,
208 MODEL_COL_NUM_COLUMNS
211 /* This list of types is passed to _gtk_file_system_model_new*() */
212 #define MODEL_COLUMN_TYPES \
213 MODEL_COL_NUM_COLUMNS, \
214 G_TYPE_STRING, /* MODEL_COL_NAME */ \
215 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
216 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
217 G_TYPE_FILE, /* MODEL_COL_FILE */ \
218 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
219 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
220 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
221 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
222 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
223 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
225 /* Identifiers for target types */
230 /* Interesting places in the shortcuts bar */
234 SHORTCUTS_RECENT_SEPARATOR,
239 SHORTCUTS_BOOKMARKS_SEPARATOR,
241 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
242 SHORTCUTS_CURRENT_FOLDER
245 /* Icon size for if we can't get it from the theme */
246 #define FALLBACK_ICON_SIZE 16
248 #define PREVIEW_HBOX_SPACING 12
252 #define SETTINGS_KEY_LAST_FOLDER_URI "last-folder-uri"
253 #define SETTINGS_KEY_LOCATION_MODE "location-mode"
254 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
255 #define SETTINGS_KEY_SHOW_SIZE_COLUMN "show-size-column"
256 #define SETTINGS_KEY_SORT_COLUMN "sort-column"
257 #define SETTINGS_KEY_SORT_ORDER "sort-order"
258 #define SETTINGS_KEY_WINDOW_POSITION "window-position"
259 #define SETTINGS_KEY_WINDOW_SIZE "window-size"
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_updated (GtkWidget *widget);
284 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
285 GdkScreen *previous_screen);
286 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
287 GtkAllocation *allocation);
289 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
292 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
295 gboolean clear_entry,
297 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
298 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
300 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
303 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
305 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
306 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
307 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
308 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
309 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
310 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
311 GtkFileFilter *filter);
312 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
313 GtkFileFilter *filter);
314 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
315 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
318 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
321 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
323 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
325 gint *default_height);
326 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
327 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
329 static void location_popup_handler (GtkFileChooserDefault *impl,
331 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
332 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
333 static void up_folder_handler (GtkFileChooserDefault *impl);
334 static void down_folder_handler (GtkFileChooserDefault *impl);
335 static void home_folder_handler (GtkFileChooserDefault *impl);
336 static void desktop_folder_handler (GtkFileChooserDefault *impl);
337 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
338 gint bookmark_index);
339 static void show_hidden_handler (GtkFileChooserDefault *impl);
340 static void search_shortcut_handler (GtkFileChooserDefault *impl);
341 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
342 static void update_appearance (GtkFileChooserDefault *impl);
344 static void set_current_filter (GtkFileChooserDefault *impl,
345 GtkFileFilter *filter);
346 static void check_preview_change (GtkFileChooserDefault *impl);
348 static void filter_combo_changed (GtkComboBox *combo_box,
349 GtkFileChooserDefault *impl);
351 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
353 GtkFileChooserDefault *impl);
355 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
358 gboolean path_currently_selected,
360 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
362 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
364 static int shortcuts_get_index (GtkFileChooserDefault *impl,
365 ShortcutsIndex where);
366 static int shortcut_find_position (GtkFileChooserDefault *impl,
369 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
371 static gboolean list_select_func (GtkTreeSelection *selection,
374 gboolean path_currently_selected,
377 static void list_selection_changed (GtkTreeSelection *tree_selection,
378 GtkFileChooserDefault *impl);
379 static void list_row_activated (GtkTreeView *tree_view,
381 GtkTreeViewColumn *column,
382 GtkFileChooserDefault *impl);
384 static void path_bar_clicked (GtkPathBar *path_bar,
387 gboolean child_is_hidden,
388 GtkFileChooserDefault *impl);
390 static void add_bookmark_button_clicked_cb (GtkButton *button,
391 GtkFileChooserDefault *impl);
392 static void remove_bookmark_button_clicked_cb (GtkButton *button,
393 GtkFileChooserDefault *impl);
395 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
397 static void load_remove_timer (GtkFileChooserDefault *impl);
398 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
400 static void location_button_toggled_cb (GtkToggleButton *toggle,
401 GtkFileChooserDefault *impl);
402 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
404 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
405 gboolean remove_from_treeview);
406 static void search_stop_searching (GtkFileChooserDefault *impl,
407 gboolean remove_query);
408 static void search_clear_model (GtkFileChooserDefault *impl,
409 gboolean remove_from_treeview);
410 static gboolean search_should_respond (GtkFileChooserDefault *impl);
411 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
412 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
413 static void search_entry_activate_cb (GtkEntry *entry,
415 static void settings_load (GtkFileChooserDefault *impl);
417 static void recent_stop_loading (GtkFileChooserDefault *impl);
418 static void recent_clear_model (GtkFileChooserDefault *impl,
419 gboolean remove_from_treeview);
420 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
421 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
422 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
423 static void set_file_system_backend (GtkFileChooserDefault *impl);
424 static void unset_file_system_backend (GtkFileChooserDefault *impl);
430 /* Drag and drop interface declarations */
433 GtkTreeModelFilter parent;
435 GtkFileChooserDefault *impl;
436 } ShortcutsPaneModelFilter;
439 GtkTreeModelFilterClass parent_class;
440 } ShortcutsPaneModelFilterClass;
442 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
443 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
445 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
447 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
448 _shortcuts_pane_model_filter,
449 GTK_TYPE_TREE_MODEL_FILTER,
450 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
451 shortcuts_pane_model_filter_drag_source_iface_init))
453 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
454 GtkTreeModel *child_model,
459 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_BOX,
460 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
461 gtk_file_chooser_default_iface_init)
462 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
463 gtk_file_chooser_embed_default_iface_init));
467 add_normal_and_shifted_binding (GtkBindingSet *binding_set,
469 GdkModifierType modifiers,
470 const gchar *signal_name)
472 gtk_binding_entry_add_signal (binding_set,
476 gtk_binding_entry_add_signal (binding_set,
477 keyval, modifiers | GDK_SHIFT_MASK,
482 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
484 static const guint quick_bookmark_keyvals[10] = {
485 GDK_KEY_1, GDK_KEY_2, GDK_KEY_3, GDK_KEY_4, GDK_KEY_5, GDK_KEY_6, GDK_KEY_7, GDK_KEY_8, GDK_KEY_9, GDK_KEY_0
487 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
488 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
489 GtkBindingSet *binding_set;
492 gobject_class->finalize = gtk_file_chooser_default_finalize;
493 gobject_class->constructor = gtk_file_chooser_default_constructor;
494 gobject_class->set_property = gtk_file_chooser_default_set_property;
495 gobject_class->get_property = gtk_file_chooser_default_get_property;
496 gobject_class->dispose = gtk_file_chooser_default_dispose;
498 widget_class->show_all = gtk_file_chooser_default_show_all;
499 widget_class->realize = gtk_file_chooser_default_realize;
500 widget_class->map = gtk_file_chooser_default_map;
501 widget_class->unmap = gtk_file_chooser_default_unmap;
502 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
503 widget_class->style_updated = gtk_file_chooser_default_style_updated;
504 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
505 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
507 signals[LOCATION_POPUP] =
508 g_signal_new_class_handler (I_("location-popup"),
509 G_OBJECT_CLASS_TYPE (class),
510 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
511 G_CALLBACK (location_popup_handler),
513 _gtk_marshal_VOID__STRING,
514 G_TYPE_NONE, 1, G_TYPE_STRING);
516 signals[LOCATION_POPUP_ON_PASTE] =
517 g_signal_new_class_handler (I_("location-popup-on-paste"),
518 G_OBJECT_CLASS_TYPE (class),
519 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
520 G_CALLBACK (location_popup_on_paste_handler),
522 _gtk_marshal_VOID__VOID,
525 signals[LOCATION_TOGGLE_POPUP] =
526 g_signal_new_class_handler (I_("location-toggle-popup"),
527 G_OBJECT_CLASS_TYPE (class),
528 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
529 G_CALLBACK (location_toggle_popup_handler),
531 _gtk_marshal_VOID__VOID,
535 g_signal_new_class_handler (I_("up-folder"),
536 G_OBJECT_CLASS_TYPE (class),
537 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
538 G_CALLBACK (up_folder_handler),
540 _gtk_marshal_VOID__VOID,
543 signals[DOWN_FOLDER] =
544 g_signal_new_class_handler (I_("down-folder"),
545 G_OBJECT_CLASS_TYPE (class),
546 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
547 G_CALLBACK (down_folder_handler),
549 _gtk_marshal_VOID__VOID,
552 signals[HOME_FOLDER] =
553 g_signal_new_class_handler (I_("home-folder"),
554 G_OBJECT_CLASS_TYPE (class),
555 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
556 G_CALLBACK (home_folder_handler),
558 _gtk_marshal_VOID__VOID,
561 signals[DESKTOP_FOLDER] =
562 g_signal_new_class_handler (I_("desktop-folder"),
563 G_OBJECT_CLASS_TYPE (class),
564 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
565 G_CALLBACK (desktop_folder_handler),
567 _gtk_marshal_VOID__VOID,
570 signals[QUICK_BOOKMARK] =
571 g_signal_new_class_handler (I_("quick-bookmark"),
572 G_OBJECT_CLASS_TYPE (class),
573 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
574 G_CALLBACK (quick_bookmark_handler),
576 _gtk_marshal_VOID__INT,
577 G_TYPE_NONE, 1, G_TYPE_INT);
579 signals[SHOW_HIDDEN] =
580 g_signal_new_class_handler (I_("show-hidden"),
581 G_OBJECT_CLASS_TYPE (class),
582 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
583 G_CALLBACK (show_hidden_handler),
585 _gtk_marshal_VOID__VOID,
588 signals[SEARCH_SHORTCUT] =
589 g_signal_new_class_handler (I_("search-shortcut"),
590 G_OBJECT_CLASS_TYPE (class),
591 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
592 G_CALLBACK (search_shortcut_handler),
594 _gtk_marshal_VOID__VOID,
597 signals[RECENT_SHORTCUT] =
598 g_signal_new_class_handler (I_("recent-shortcut"),
599 G_OBJECT_CLASS_TYPE (class),
600 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
601 G_CALLBACK (recent_shortcut_handler),
603 _gtk_marshal_VOID__VOID,
606 binding_set = gtk_binding_set_by_class (class);
608 gtk_binding_entry_add_signal (binding_set,
609 GDK_KEY_l, GDK_CONTROL_MASK,
610 "location-toggle-popup",
613 gtk_binding_entry_add_signal (binding_set,
616 1, G_TYPE_STRING, "/");
617 gtk_binding_entry_add_signal (binding_set,
618 GDK_KEY_KP_Divide, 0,
620 1, G_TYPE_STRING, "/");
623 gtk_binding_entry_add_signal (binding_set,
624 GDK_KEY_asciitilde, 0,
626 1, G_TYPE_STRING, "~");
629 gtk_binding_entry_add_signal (binding_set,
630 GDK_KEY_v, GDK_CONTROL_MASK,
631 "location-popup-on-paste",
633 gtk_binding_entry_add_signal (binding_set,
634 GDK_KEY_BackSpace, 0,
638 add_normal_and_shifted_binding (binding_set,
639 GDK_KEY_Up, GDK_MOD1_MASK,
642 add_normal_and_shifted_binding (binding_set,
643 GDK_KEY_KP_Up, GDK_MOD1_MASK,
646 add_normal_and_shifted_binding (binding_set,
647 GDK_KEY_Down, GDK_MOD1_MASK,
649 add_normal_and_shifted_binding (binding_set,
650 GDK_KEY_KP_Down, GDK_MOD1_MASK,
653 gtk_binding_entry_add_signal (binding_set,
654 GDK_KEY_Home, GDK_MOD1_MASK,
657 gtk_binding_entry_add_signal (binding_set,
658 GDK_KEY_KP_Home, GDK_MOD1_MASK,
661 gtk_binding_entry_add_signal (binding_set,
662 GDK_KEY_d, GDK_MOD1_MASK,
665 gtk_binding_entry_add_signal (binding_set,
666 GDK_KEY_h, GDK_CONTROL_MASK,
669 gtk_binding_entry_add_signal (binding_set,
670 GDK_KEY_s, GDK_MOD1_MASK,
673 gtk_binding_entry_add_signal (binding_set,
674 GDK_KEY_r, GDK_MOD1_MASK,
678 for (i = 0; i < 10; i++)
679 gtk_binding_entry_add_signal (binding_set,
680 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
684 _gtk_file_chooser_install_properties (gobject_class);
688 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
690 iface->select_file = gtk_file_chooser_default_select_file;
691 iface->unselect_file = gtk_file_chooser_default_unselect_file;
692 iface->select_all = gtk_file_chooser_default_select_all;
693 iface->unselect_all = gtk_file_chooser_default_unselect_all;
694 iface->get_files = gtk_file_chooser_default_get_files;
695 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
696 iface->get_file_system = gtk_file_chooser_default_get_file_system;
697 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
698 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
699 iface->set_current_name = gtk_file_chooser_default_set_current_name;
700 iface->add_filter = gtk_file_chooser_default_add_filter;
701 iface->remove_filter = gtk_file_chooser_default_remove_filter;
702 iface->list_filters = gtk_file_chooser_default_list_filters;
703 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
704 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
705 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
709 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
711 iface->get_default_size = gtk_file_chooser_default_get_default_size;
712 iface->should_respond = gtk_file_chooser_default_should_respond;
713 iface->initial_focus = gtk_file_chooser_default_initial_focus;
717 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
719 profile_start ("start", NULL);
720 #ifdef PROFILE_FILE_CHOOSER
721 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
723 impl->local_only = TRUE;
724 impl->preview_widget_active = TRUE;
725 impl->use_preview_label = TRUE;
726 impl->select_multiple = FALSE;
727 impl->show_hidden = FALSE;
728 impl->show_size_column = TRUE;
729 impl->icon_size = FALLBACK_ICON_SIZE;
730 impl->load_state = LOAD_EMPTY;
731 impl->reload_state = RELOAD_EMPTY;
732 impl->pending_select_files = NULL;
733 impl->location_mode = LOCATION_MODE_PATH_BAR;
734 impl->operation_mode = OPERATION_MODE_BROWSE;
735 impl->sort_column = MODEL_COL_NAME;
736 impl->sort_order = GTK_SORT_ASCENDING;
737 impl->recent_manager = gtk_recent_manager_get_default ();
738 impl->create_folders = TRUE;
740 gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
741 GTK_ORIENTATION_VERTICAL);
742 gtk_box_set_spacing (GTK_BOX (impl), 12);
744 set_file_system_backend (impl);
746 profile_end ("end", NULL);
749 /* Frees the data columns for the specified iter in the shortcuts model*/
751 shortcuts_free_row_data (GtkFileChooserDefault *impl,
755 ShortcutType shortcut_type;
756 GCancellable *cancellable;
758 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
759 SHORTCUTS_COL_DATA, &col_data,
760 SHORTCUTS_COL_TYPE, &shortcut_type,
761 SHORTCUTS_COL_CANCELLABLE, &cancellable,
766 g_cancellable_cancel (cancellable);
767 g_object_unref (cancellable);
770 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
771 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
775 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
777 GtkFileSystemVolume *volume;
780 _gtk_file_system_volume_unref (volume);
782 if (shortcut_type == SHORTCUT_TYPE_FILE)
787 g_object_unref (file);
791 /* Frees all the data columns in the shortcuts model */
793 shortcuts_free (GtkFileChooserDefault *impl)
797 if (!impl->shortcuts_model)
800 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
803 shortcuts_free_row_data (impl, &iter);
805 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
807 g_object_unref (impl->shortcuts_model);
808 impl->shortcuts_model = NULL;
812 pending_select_files_free (GtkFileChooserDefault *impl)
814 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
815 g_slist_free (impl->pending_select_files);
816 impl->pending_select_files = NULL;
820 pending_select_files_add (GtkFileChooserDefault *impl,
823 impl->pending_select_files =
824 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
828 gtk_file_chooser_default_finalize (GObject *object)
830 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
833 unset_file_system_backend (impl);
835 if (impl->shortcuts_pane_filter_model)
836 g_object_unref (impl->shortcuts_pane_filter_model);
838 shortcuts_free (impl);
840 g_free (impl->browse_files_last_selected_name);
842 for (l = impl->filters; l; l = l->next)
844 GtkFileFilter *filter;
846 filter = GTK_FILE_FILTER (l->data);
847 g_object_unref (filter);
849 g_slist_free (impl->filters);
851 if (impl->current_filter)
852 g_object_unref (impl->current_filter);
854 if (impl->current_volume_file)
855 g_object_unref (impl->current_volume_file);
857 if (impl->current_folder)
858 g_object_unref (impl->current_folder);
860 if (impl->preview_file)
861 g_object_unref (impl->preview_file);
863 if (impl->browse_path_bar_size_group)
864 g_object_unref (impl->browse_path_bar_size_group);
866 /* Free all the Models we have */
867 stop_loading_and_clear_list_model (impl, FALSE);
868 search_clear_model (impl, FALSE);
869 recent_clear_model (impl, FALSE);
871 /* stopping the load above should have cleared this */
872 g_assert (impl->load_timeout_id == 0);
874 g_free (impl->preview_display_name);
876 g_free (impl->edited_new_text);
878 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
881 /* Shows an error dialog set as transient for the specified window */
883 error_message_with_parent (GtkWindow *parent,
889 dialog = gtk_message_dialog_new (parent,
890 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
895 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
898 if (parent && gtk_window_has_group (parent))
899 gtk_window_group_add_window (gtk_window_get_group (parent),
900 GTK_WINDOW (dialog));
902 gtk_dialog_run (GTK_DIALOG (dialog));
903 gtk_widget_destroy (dialog);
906 /* Returns a toplevel GtkWindow, or NULL if none */
908 get_toplevel (GtkWidget *widget)
912 toplevel = gtk_widget_get_toplevel (widget);
913 if (!gtk_widget_is_toplevel (toplevel))
916 return GTK_WINDOW (toplevel);
919 /* Shows an error dialog for the file chooser */
921 error_message (GtkFileChooserDefault *impl,
925 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
928 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
930 error_dialog (GtkFileChooserDefault *impl,
941 uri = g_file_get_uri (file);
942 text = g_strdup_printf (msg, uri);
943 error_message (impl, text, error->message);
946 g_error_free (error);
950 /* Displays an error message about not being able to get information for a file.
951 * Frees the GError as well.
954 error_getting_info_dialog (GtkFileChooserDefault *impl,
959 _("Could not retrieve information about the file"),
963 /* Shows an error dialog about not being able to add a bookmark */
965 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
970 _("Could not add a bookmark"),
974 /* Shows an error dialog about not being able to remove a bookmark */
976 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
981 _("Could not remove bookmark"),
985 /* Shows an error dialog about not being able to create a folder */
987 error_creating_folder_dialog (GtkFileChooserDefault *impl,
992 _("The folder could not be created"),
996 /* Shows an error about not being able to create a folder because a file with
997 * the same name is already there.
1000 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1005 _("The folder could not be created, as a file with the same "
1006 "name already exists. Try using a different name for the "
1007 "folder, or rename the file first."),
1012 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1018 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1019 _("You need to choose a valid filename."));
1022 _("Cannot create a file under %s as it is not a folder"),
1023 parent_file, error);
1026 /* Shows an error about not being able to select a folder because a file with
1027 * the same name is already there.
1030 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1034 _("You may only select folders. The item that you selected is not a folder; "
1035 "try using a different item."),
1039 /* Shows an error dialog about not being able to create a filename */
1041 error_building_filename_dialog (GtkFileChooserDefault *impl,
1044 error_dialog (impl, _("Invalid file name"),
1048 /* Shows an error dialog when we cannot switch to a folder */
1050 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1054 error_dialog (impl, _("The folder contents could not be displayed"),
1058 /* Changes folders, displaying an error dialog if this fails */
1060 change_folder_and_display_error (GtkFileChooserDefault *impl,
1062 gboolean clear_entry)
1067 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1069 /* We copy the path because of this case:
1071 * list_row_activated()
1072 * fetches path from model; path belongs to the model (*)
1073 * calls change_folder_and_display_error()
1074 * calls gtk_file_chooser_set_current_folder_file()
1075 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1079 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1082 error_changing_folder_dialog (impl, file, error);
1088 emit_default_size_changed (GtkFileChooserDefault *impl)
1090 profile_msg (" emit default-size-changed start", NULL);
1091 g_signal_emit_by_name (impl, "default-size-changed");
1092 profile_msg (" emit default-size-changed end", NULL);
1096 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1098 if (impl->use_preview_label)
1100 if (!impl->preview_label)
1102 impl->preview_label = gtk_label_new (impl->preview_display_name);
1103 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1104 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1105 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1106 gtk_widget_show (impl->preview_label);
1111 if (impl->preview_label)
1113 gtk_widget_destroy (impl->preview_label);
1114 impl->preview_label = NULL;
1118 if (impl->preview_widget_active && impl->preview_widget)
1119 gtk_widget_show (impl->preview_box);
1121 gtk_widget_hide (impl->preview_box);
1123 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1124 emit_default_size_changed (impl);
1128 set_preview_widget (GtkFileChooserDefault *impl,
1129 GtkWidget *preview_widget)
1131 if (preview_widget == impl->preview_widget)
1134 if (impl->preview_widget)
1135 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1136 impl->preview_widget);
1138 impl->preview_widget = preview_widget;
1139 if (impl->preview_widget)
1141 gtk_widget_show (impl->preview_widget);
1142 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1143 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1144 impl->preview_widget,
1145 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1148 update_preview_widget_visibility (impl);
1151 /* Renders a "Search" icon at an appropriate size for a tree view */
1153 render_search_icon (GtkFileChooserDefault *impl)
1155 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1159 render_recent_icon (GtkFileChooserDefault *impl)
1161 GtkIconTheme *theme;
1164 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1165 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1167 theme = gtk_icon_theme_get_default ();
1169 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1175 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1181 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1182 struct ReloadIconsData
1184 GtkFileChooserDefault *impl;
1185 GtkTreeRowReference *row_ref;
1189 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1191 const GError *error,
1197 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1198 struct ReloadIconsData *data = user_data;
1200 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1203 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1205 if (cancelled || error)
1208 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1210 path = gtk_tree_row_reference_get_path (data->row_ref);
1213 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1214 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1215 SHORTCUTS_COL_PIXBUF, pixbuf,
1217 gtk_tree_path_free (path);
1221 g_object_unref (pixbuf);
1224 gtk_tree_row_reference_free (data->row_ref);
1225 g_object_unref (data->impl);
1228 g_object_unref (cancellable);
1232 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1237 profile_start ("start", NULL);
1239 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1242 for (l = impl->reload_icon_cancellables; l; l = l->next)
1244 GCancellable *cancellable = G_CANCELLABLE (l->data);
1245 g_cancellable_cancel (cancellable);
1247 g_slist_free (impl->reload_icon_cancellables);
1248 impl->reload_icon_cancellables = NULL;
1253 ShortcutType shortcut_type;
1254 gboolean pixbuf_visible;
1257 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1258 SHORTCUTS_COL_DATA, &data,
1259 SHORTCUTS_COL_TYPE, &shortcut_type,
1260 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1266 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1268 GtkFileSystemVolume *volume;
1271 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1272 impl->icon_size, NULL);
1274 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1276 if (g_file_is_native (G_FILE (data)))
1279 struct ReloadIconsData *info;
1280 GtkTreePath *tree_path;
1281 GCancellable *cancellable;
1285 info = g_new0 (struct ReloadIconsData, 1);
1286 info->impl = g_object_ref (impl);
1287 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1288 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1289 gtk_tree_path_free (tree_path);
1291 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1293 shortcuts_reload_icons_get_info_cb,
1295 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1299 GtkIconTheme *icon_theme;
1301 /* Don't call get_info for remote paths to avoid latency and
1303 * If we switch to a better bookmarks file format (XBEL), we
1304 * should use mime info to get a better icon.
1306 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1307 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1308 impl->icon_size, 0, NULL);
1311 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1313 pixbuf = render_search_icon (impl);
1315 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1317 pixbuf = render_recent_icon (impl);
1320 gtk_list_store_set (impl->shortcuts_model, &iter,
1321 SHORTCUTS_COL_PIXBUF, pixbuf,
1325 g_object_unref (pixbuf);
1329 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1333 profile_end ("end", NULL);
1337 shortcuts_find_folder (GtkFileChooserDefault *impl,
1340 GtkTreeSelection *selection;
1344 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1346 g_assert (folder != NULL);
1347 pos = shortcut_find_position (impl, folder);
1350 gtk_tree_selection_unselect_all (selection);
1354 path = gtk_tree_path_new_from_indices (pos, -1);
1355 gtk_tree_selection_select_path (selection, path);
1356 gtk_tree_path_free (path);
1359 /* If a shortcut corresponds to the current folder, selects it */
1361 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1363 shortcuts_find_folder (impl, impl->current_folder);
1366 /* Removes the specified number of rows from the shortcuts list */
1368 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1374 path = gtk_tree_path_new_from_indices (start_row, -1);
1376 for (; n_rows; n_rows--)
1380 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1381 g_assert_not_reached ();
1383 shortcuts_free_row_data (impl, &iter);
1384 gtk_list_store_remove (impl->shortcuts_model, &iter);
1387 gtk_tree_path_free (path);
1391 shortcuts_update_count (GtkFileChooserDefault *impl,
1392 ShortcutsIndex type,
1397 case SHORTCUTS_HOME:
1399 impl->has_home = FALSE;
1401 impl->has_home = TRUE;
1404 case SHORTCUTS_DESKTOP:
1406 impl->has_desktop = FALSE;
1408 impl->has_desktop = TRUE;
1411 case SHORTCUTS_VOLUMES:
1412 impl->num_volumes += value;
1415 case SHORTCUTS_SHORTCUTS:
1416 impl->num_shortcuts += value;
1419 case SHORTCUTS_BOOKMARKS:
1420 impl->num_bookmarks += value;
1423 case SHORTCUTS_CURRENT_FOLDER:
1425 impl->shortcuts_current_folder_active = FALSE;
1427 impl->shortcuts_current_folder_active = TRUE;
1436 struct ShortcutsInsertRequest
1438 GtkFileChooserDefault *impl;
1442 GtkTreeRowReference *row_ref;
1443 ShortcutsIndex type;
1449 get_file_info_finished (GCancellable *cancellable,
1451 const GError *error,
1455 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1459 GCancellable *model_cancellable = NULL;
1460 struct ShortcutsInsertRequest *request = data;
1462 path = gtk_tree_row_reference_get_path (request->row_ref);
1464 /* Handle doesn't exist anymore in the model */
1467 pos = gtk_tree_path_get_indices (path)[0];
1468 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1470 gtk_tree_path_free (path);
1472 /* validate cancellable, else goto out */
1473 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1474 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1476 if (cancellable != model_cancellable)
1479 /* set the cancellable to NULL in the model (we unref later on) */
1480 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1481 SHORTCUTS_COL_CANCELLABLE, NULL,
1489 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1490 shortcuts_update_count (request->impl, request->type, -1);
1492 if (request->type == SHORTCUTS_HOME)
1496 home = g_file_new_for_path (g_get_home_dir ());
1497 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1498 g_object_unref (home);
1500 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1502 /* Remove the current folder separator */
1503 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1504 shortcuts_remove_rows (request->impl, separator_pos, 1);
1510 if (!request->label_copy)
1511 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1512 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1513 request->impl->icon_size);
1515 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1516 SHORTCUTS_COL_PIXBUF, pixbuf,
1517 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1518 SHORTCUTS_COL_NAME, request->label_copy,
1519 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1520 SHORTCUTS_COL_REMOVABLE, request->removable,
1523 if (request->impl->shortcuts_pane_filter_model)
1524 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1527 g_object_unref (pixbuf);
1530 g_object_unref (request->impl);
1531 g_object_unref (request->file);
1532 gtk_tree_row_reference_free (request->row_ref);
1533 g_free (request->label_copy);
1536 if (model_cancellable)
1537 g_object_unref (model_cancellable);
1540 /* FIXME: GtkFileSystem needs a function to split a remote path
1541 * into hostname and path components, or maybe just have a
1542 * gtk_file_system_path_get_display_name().
1544 * This function is also used in gtkfilechooserbutton.c
1547 _gtk_file_chooser_label_for_file (GFile *file)
1549 const gchar *path, *start, *end, *p;
1550 gchar *uri, *host, *label;
1552 uri = g_file_get_uri (file);
1554 start = strstr (uri, "://");
1558 path = strchr (start, '/');
1563 end = uri + strlen (uri);
1567 /* strip username */
1568 p = strchr (start, '@');
1572 p = strchr (start, ':');
1576 host = g_strndup (start, end - start);
1578 /* Translators: the first string is a path and the second string
1579 * is a hostname. Nautilus and the panel contain the same string
1582 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1588 label = g_strdup (uri);
1596 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1597 * inserts a volume. A position of -1 indicates the end of the tree.
1600 shortcuts_insert_file (GtkFileChooserDefault *impl,
1602 ShortcutType shortcut_type,
1603 GtkFileSystemVolume *volume,
1607 ShortcutsIndex type)
1610 GdkPixbuf *pixbuf = NULL;
1611 gpointer data = NULL;
1613 GtkIconTheme *icon_theme;
1615 profile_start ("start shortcut", NULL);
1617 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1620 label_copy = _gtk_file_system_volume_get_display_name (volume);
1621 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1622 impl->icon_size, NULL);
1624 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1626 if (g_file_is_native (file))
1628 struct ShortcutsInsertRequest *request;
1629 GCancellable *cancellable;
1632 request = g_new0 (struct ShortcutsInsertRequest, 1);
1633 request->impl = g_object_ref (impl);
1634 request->file = g_object_ref (file);
1635 request->name_only = TRUE;
1636 request->removable = removable;
1638 request->type = type;
1640 request->label_copy = g_strdup (label);
1643 gtk_list_store_append (impl->shortcuts_model, &iter);
1645 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1647 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1648 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1649 gtk_tree_path_free (p);
1651 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1652 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1653 get_file_info_finished, request);
1655 gtk_list_store_set (impl->shortcuts_model, &iter,
1656 SHORTCUTS_COL_DATA, g_object_ref (file),
1657 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1658 SHORTCUTS_COL_CANCELLABLE, cancellable,
1661 shortcuts_update_count (impl, type, 1);
1667 /* Don't call get_info for remote paths to avoid latency and
1670 data = g_object_ref (file);
1672 label_copy = g_strdup (label);
1674 label_copy = _gtk_file_chooser_label_for_file (file);
1676 /* If we switch to a better bookmarks file format (XBEL), we
1677 * should use mime info to get a better icon.
1679 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1680 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1681 impl->icon_size, 0, NULL);
1686 g_assert_not_reached ();
1692 gtk_list_store_append (impl->shortcuts_model, &iter);
1694 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1696 shortcuts_update_count (impl, type, 1);
1698 gtk_list_store_set (impl->shortcuts_model, &iter,
1699 SHORTCUTS_COL_PIXBUF, pixbuf,
1700 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1701 SHORTCUTS_COL_NAME, label_copy,
1702 SHORTCUTS_COL_DATA, data,
1703 SHORTCUTS_COL_TYPE, shortcut_type,
1704 SHORTCUTS_COL_REMOVABLE, removable,
1705 SHORTCUTS_COL_CANCELLABLE, NULL,
1708 if (impl->shortcuts_pane_filter_model)
1709 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1711 g_free (label_copy);
1714 g_object_unref (pixbuf);
1716 profile_end ("end", NULL);
1720 shortcuts_append_search (GtkFileChooserDefault *impl)
1725 pixbuf = render_search_icon (impl);
1727 gtk_list_store_append (impl->shortcuts_model, &iter);
1728 gtk_list_store_set (impl->shortcuts_model, &iter,
1729 SHORTCUTS_COL_PIXBUF, pixbuf,
1730 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1731 SHORTCUTS_COL_NAME, _("Search"),
1732 SHORTCUTS_COL_DATA, NULL,
1733 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1734 SHORTCUTS_COL_REMOVABLE, FALSE,
1738 g_object_unref (pixbuf);
1740 impl->has_search = TRUE;
1744 shortcuts_append_recent (GtkFileChooserDefault *impl)
1749 pixbuf = render_recent_icon (impl);
1751 gtk_list_store_append (impl->shortcuts_model, &iter);
1752 gtk_list_store_set (impl->shortcuts_model, &iter,
1753 SHORTCUTS_COL_PIXBUF, pixbuf,
1754 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1755 SHORTCUTS_COL_NAME, _("Recently Used"),
1756 SHORTCUTS_COL_DATA, NULL,
1757 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1758 SHORTCUTS_COL_REMOVABLE, FALSE,
1762 g_object_unref (pixbuf);
1765 /* Appends an item for the user's home directory to the shortcuts model */
1767 shortcuts_append_home (GtkFileChooserDefault *impl)
1769 const char *home_path;
1772 profile_start ("start", NULL);
1774 home_path = g_get_home_dir ();
1775 if (home_path == NULL)
1777 profile_end ("end - no home directory!?", NULL);
1781 home = g_file_new_for_path (home_path);
1782 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1783 impl->has_home = TRUE;
1785 g_object_unref (home);
1787 profile_end ("end", NULL);
1790 /* Appends the ~/Desktop directory to the shortcuts model */
1792 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1797 profile_start ("start", NULL);
1799 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1800 /* "To disable a directory, point it to the homedir."
1801 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1803 if (!g_strcmp0 (name, g_get_home_dir ()))
1805 profile_end ("end", NULL);
1809 file = g_file_new_for_path (name);
1810 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1811 impl->has_desktop = TRUE;
1813 /* We do not actually pop up an error dialog if there is no desktop directory
1814 * because some people may really not want to have one.
1817 g_object_unref (file);
1819 profile_end ("end", NULL);
1822 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1824 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1831 profile_start ("start", NULL);
1833 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1836 for (; bookmarks; bookmarks = bookmarks->next)
1840 file = bookmarks->data;
1842 if (impl->local_only && !g_file_is_native (file))
1845 if (shortcut_find_position (impl, file) != -1)
1848 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1850 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1856 profile_end ("end", NULL);
1858 return num_inserted;
1861 /* Returns the index for the corresponding item in the shortcuts bar */
1863 shortcuts_get_index (GtkFileChooserDefault *impl,
1864 ShortcutsIndex where)
1870 if (where == SHORTCUTS_SEARCH)
1873 n += impl->has_search ? 1 : 0;
1875 if (where == SHORTCUTS_RECENT)
1878 n += 1; /* we always have the recently-used item */
1880 if (where == SHORTCUTS_RECENT_SEPARATOR)
1883 n += 1; /* we always have the separator after the recently-used item */
1885 if (where == SHORTCUTS_HOME)
1888 n += impl->has_home ? 1 : 0;
1890 if (where == SHORTCUTS_DESKTOP)
1893 n += impl->has_desktop ? 1 : 0;
1895 if (where == SHORTCUTS_VOLUMES)
1898 n += impl->num_volumes;
1900 if (where == SHORTCUTS_SHORTCUTS)
1903 n += impl->num_shortcuts;
1905 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1908 /* If there are no bookmarks there won't be a separator */
1909 n += (impl->num_bookmarks > 0) ? 1 : 0;
1911 if (where == SHORTCUTS_BOOKMARKS)
1914 n += impl->num_bookmarks;
1916 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1921 if (where == SHORTCUTS_CURRENT_FOLDER)
1924 g_assert_not_reached ();
1931 /* Adds all the file system volumes to the shortcuts model */
1933 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1938 gboolean old_changing_folders;
1940 profile_start ("start", NULL);
1942 old_changing_folders = impl->changing_folder;
1943 impl->changing_folder = TRUE;
1945 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1946 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1947 impl->num_volumes = 0;
1949 list = _gtk_file_system_list_volumes (impl->file_system);
1953 for (l = list; l; l = l->next)
1955 GtkFileSystemVolume *volume;
1959 if (impl->local_only)
1961 if (_gtk_file_system_volume_is_mounted (volume))
1964 gboolean base_is_native = TRUE;
1966 base_file = _gtk_file_system_volume_get_root (volume);
1967 if (base_file != NULL)
1969 base_is_native = g_file_is_native (base_file);
1970 g_object_unref (base_file);
1973 if (!base_is_native)
1978 shortcuts_insert_file (impl,
1980 SHORTCUT_TYPE_VOLUME,
1981 _gtk_file_system_volume_ref (volume),
1989 impl->num_volumes = n;
1990 g_slist_free (list);
1992 if (impl->shortcuts_pane_filter_model)
1993 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1995 impl->changing_folder = old_changing_folders;
1997 profile_end ("end", NULL);
2000 /* Inserts a separator node in the shortcuts list */
2002 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2003 ShortcutsIndex where)
2007 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2008 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2009 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2011 gtk_list_store_insert (impl->shortcuts_model, &iter,
2012 shortcuts_get_index (impl, where));
2013 gtk_list_store_set (impl->shortcuts_model, &iter,
2014 SHORTCUTS_COL_PIXBUF, NULL,
2015 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2016 SHORTCUTS_COL_NAME, NULL,
2017 SHORTCUTS_COL_DATA, NULL,
2018 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2022 /* Updates the list of bookmarks */
2024 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2027 gboolean old_changing_folders;
2029 GFile *list_selected = NULL;
2030 ShortcutType shortcut_type;
2033 profile_start ("start", NULL);
2035 old_changing_folders = impl->changing_folder;
2036 impl->changing_folder = TRUE;
2038 if (shortcuts_get_selected (impl, &iter))
2040 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2042 SHORTCUTS_COL_DATA, &col_data,
2043 SHORTCUTS_COL_TYPE, &shortcut_type,
2046 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2047 list_selected = g_object_ref (col_data);
2050 if (impl->num_bookmarks > 0)
2051 shortcuts_remove_rows (impl,
2052 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2053 impl->num_bookmarks + 1);
2055 impl->num_bookmarks = 0;
2056 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2058 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2059 shortcuts_append_bookmarks (impl, bookmarks);
2060 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2061 g_slist_free (bookmarks);
2063 if (impl->num_bookmarks == 0)
2064 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2066 if (impl->shortcuts_pane_filter_model)
2067 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2071 shortcuts_find_folder (impl, list_selected);
2072 g_object_unref (list_selected);
2075 impl->changing_folder = old_changing_folders;
2077 profile_end ("end", NULL);
2080 /* Appends a separator and a row to the shortcuts list for the current folder */
2082 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2086 g_assert (!impl->shortcuts_current_folder_active);
2088 g_assert (impl->current_folder != NULL);
2090 pos = shortcut_find_position (impl, impl->current_folder);
2093 GtkFileSystemVolume *volume;
2097 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2100 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2102 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2104 base_file = _gtk_file_system_volume_get_root (volume);
2108 if (base_file && g_file_equal (base_file, impl->current_folder))
2109 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2111 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2114 g_object_unref (base_file);
2118 /* Updates the current folder row in the shortcuts model */
2120 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2124 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2126 if (impl->shortcuts_current_folder_active)
2128 shortcuts_remove_rows (impl, pos, 2);
2129 impl->shortcuts_current_folder_active = FALSE;
2132 shortcuts_add_current_folder (impl);
2135 /* Filter function used for the shortcuts filter model */
2137 shortcuts_pane_filter_cb (GtkTreeModel *model,
2141 GtkFileChooserDefault *impl;
2145 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2147 path = gtk_tree_model_get_path (model, iter);
2151 pos = *gtk_tree_path_get_indices (path);
2152 gtk_tree_path_free (path);
2154 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2157 /* Creates the list model for shortcuts */
2159 shortcuts_model_create (GtkFileChooserDefault *impl)
2161 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2162 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2163 GDK_TYPE_PIXBUF, /* pixbuf */
2164 G_TYPE_STRING, /* name */
2165 G_TYPE_POINTER, /* path or volume */
2166 G_TYPE_INT, /* ShortcutType */
2167 G_TYPE_BOOLEAN, /* removable */
2168 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2169 G_TYPE_POINTER); /* GCancellable */
2171 shortcuts_append_search (impl);
2173 if (impl->recent_manager)
2175 shortcuts_append_recent (impl);
2176 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2179 if (impl->file_system)
2181 shortcuts_append_home (impl);
2182 shortcuts_append_desktop (impl);
2183 shortcuts_add_volumes (impl);
2186 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2187 GTK_TREE_MODEL (impl->shortcuts_model),
2190 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2191 shortcuts_pane_filter_cb,
2196 /* Callback used when the "New Folder" button is clicked */
2198 new_folder_button_clicked (GtkButton *button,
2199 GtkFileChooserDefault *impl)
2204 if (!impl->browse_files_model)
2205 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2207 /* Prevent button from being clicked twice */
2208 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2210 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2212 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2213 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2214 path, impl->list_name_column,
2217 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2218 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2220 impl->list_name_column,
2223 gtk_tree_path_free (path);
2227 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2231 source = g_idle_source_new ();
2232 g_source_set_closure (source,
2233 g_cclosure_new_object (callback, G_OBJECT (impl)));
2234 g_source_attach (source, NULL);
2239 /* Idle handler for creating a new folder after editing its name cell, or for
2240 * canceling the editing.
2243 edited_idle_cb (GtkFileChooserDefault *impl)
2245 GDK_THREADS_ENTER ();
2247 g_source_destroy (impl->edited_idle);
2248 impl->edited_idle = NULL;
2250 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2251 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2253 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2255 if (impl->edited_new_text /* not cancelled? */
2256 && (strlen (impl->edited_new_text) != 0)
2257 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2259 GError *error = NULL;
2262 file = g_file_get_child_for_display_name (impl->current_folder,
2263 impl->edited_new_text,
2267 GError *error = NULL;
2269 if (g_file_make_directory (file, NULL, &error))
2270 change_folder_and_display_error (impl, file, FALSE);
2272 error_creating_folder_dialog (impl, file, error);
2274 g_object_unref (file);
2277 error_creating_folder_dialog (impl, file, error);
2279 g_free (impl->edited_new_text);
2280 impl->edited_new_text = NULL;
2283 GDK_THREADS_LEAVE ();
2289 queue_edited_idle (GtkFileChooserDefault *impl,
2290 const gchar *new_text)
2292 /* We create the folder in an idle handler so that we don't modify the tree
2296 if (!impl->edited_idle)
2297 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2299 g_free (impl->edited_new_text);
2300 impl->edited_new_text = g_strdup (new_text);
2303 /* Callback used from the text cell renderer when the new folder is named */
2305 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2307 const gchar *new_text,
2308 GtkFileChooserDefault *impl)
2310 /* work around bug #154921 */
2311 g_object_set (cell_renderer_text,
2312 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2313 queue_edited_idle (impl, new_text);
2316 /* Callback used from the text cell renderer when the new folder edition gets
2320 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2321 GtkFileChooserDefault *impl)
2323 /* work around bug #154921 */
2324 g_object_set (cell_renderer_text,
2325 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2326 queue_edited_idle (impl, NULL);
2329 /* Creates the widgets for the filter combo box */
2331 filter_create (GtkFileChooserDefault *impl)
2333 GtkCellRenderer *cell;
2336 impl->filter_combo = gtk_combo_box_text_new ();
2337 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2339 /* Get the combo's text renderer and set ellipsize parameters */
2340 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2344 g_object_set (G_OBJECT (cell),
2345 "ellipsize", PANGO_ELLIPSIZE_END,
2348 g_list_free (cells);
2350 g_signal_connect (impl->filter_combo, "changed",
2351 G_CALLBACK (filter_combo_changed), impl);
2353 gtk_widget_set_tooltip_text (impl->filter_combo,
2354 _("Select which types of files are shown"));
2356 return impl->filter_combo;
2360 toolbutton_new (GtkFileChooserDefault *impl,
2369 item = gtk_tool_button_new (NULL, NULL);
2370 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2371 gtk_widget_show (image);
2372 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2374 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2375 g_signal_connect (item, "clicked", callback, impl);
2378 gtk_widget_show (GTK_WIDGET (item));
2380 return GTK_WIDGET (item);
2383 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2385 shortcut_find_position (GtkFileChooserDefault *impl,
2390 int current_folder_separator_idx;
2392 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2395 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2398 /* FIXME: is this still needed? */
2399 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2403 for (i = 0; i < current_folder_separator_idx; i++)
2406 ShortcutType shortcut_type;
2408 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2409 SHORTCUTS_COL_DATA, &col_data,
2410 SHORTCUTS_COL_TYPE, &shortcut_type,
2415 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2417 GtkFileSystemVolume *volume;
2422 base_file = _gtk_file_system_volume_get_root (volume);
2424 exists = base_file && g_file_equal (file, base_file);
2427 g_object_unref (base_file);
2432 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2436 model_file = col_data;
2438 if (model_file && g_file_equal (model_file, file))
2443 if (i < current_folder_separator_idx - 1)
2445 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2446 g_assert_not_reached ();
2453 /* Tries to add a bookmark from a path name */
2455 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2461 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2463 if (shortcut_find_position (impl, file) != -1)
2467 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2469 error_adding_bookmark_dialog (impl, file, error);
2477 add_bookmark_foreach_cb (GtkTreeModel *model,
2482 GtkFileChooserDefault *impl;
2485 impl = (GtkFileChooserDefault *) data;
2487 gtk_tree_model_get (model, iter,
2488 MODEL_COL_FILE, &file,
2491 shortcuts_add_bookmark_from_file (impl, file, -1);
2493 g_object_unref (file);
2496 /* Adds a bookmark from the currently selected item in the file list */
2498 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2500 GtkTreeSelection *selection;
2502 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2504 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2505 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2507 gtk_tree_selection_selected_foreach (selection,
2508 add_bookmark_foreach_cb,
2512 /* Callback used when the "Add bookmark" button is clicked */
2514 add_bookmark_button_clicked_cb (GtkButton *button,
2515 GtkFileChooserDefault *impl)
2517 bookmarks_add_selected_folder (impl);
2520 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2521 * returns FALSE if no shortcut is selected.
2524 shortcuts_get_selected (GtkFileChooserDefault *impl,
2527 GtkTreeSelection *selection;
2528 GtkTreeIter parent_iter;
2530 if (!impl->browse_shortcuts_tree_view)
2533 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2535 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2538 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2544 /* Removes the selected bookmarks */
2546 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2554 if (!shortcuts_get_selected (impl, &iter))
2557 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2558 SHORTCUTS_COL_DATA, &col_data,
2559 SHORTCUTS_COL_REMOVABLE, &removable,
2565 g_assert (col_data != NULL);
2570 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2571 error_removing_bookmark_dialog (impl, file, error);
2574 /* Callback used when the "Remove bookmark" button is clicked */
2576 remove_bookmark_button_clicked_cb (GtkButton *button,
2577 GtkFileChooserDefault *impl)
2579 remove_selected_bookmarks (impl);
2582 struct selection_check_closure {
2583 GtkFileChooserDefault *impl;
2586 gboolean all_folders;
2589 /* Used from gtk_tree_selection_selected_foreach() */
2591 selection_check_foreach_cb (GtkTreeModel *model,
2596 struct selection_check_closure *closure;
2600 gtk_tree_model_get (model, iter,
2601 MODEL_COL_FILE, &file,
2602 MODEL_COL_IS_FOLDER, &is_folder,
2608 g_object_unref (file);
2611 closure->num_selected++;
2613 closure->all_folders = closure->all_folders && is_folder;
2614 closure->all_files = closure->all_files && !is_folder;
2617 /* Checks whether the selected items in the file list are all files or all folders */
2619 selection_check (GtkFileChooserDefault *impl,
2621 gboolean *all_files,
2622 gboolean *all_folders)
2624 struct selection_check_closure closure;
2625 GtkTreeSelection *selection;
2627 closure.impl = impl;
2628 closure.num_selected = 0;
2629 closure.all_files = TRUE;
2630 closure.all_folders = TRUE;
2632 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2633 gtk_tree_selection_selected_foreach (selection,
2634 selection_check_foreach_cb,
2637 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2640 *num_selected = closure.num_selected;
2643 *all_files = closure.all_files;
2646 *all_folders = closure.all_folders;
2649 struct get_selected_file_closure {
2650 GtkFileChooserDefault *impl;
2655 get_selected_file_foreach_cb (GtkTreeModel *model,
2660 struct get_selected_file_closure *closure = data;
2664 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2665 g_object_unref (closure->file);
2666 closure->file = NULL;
2669 gtk_tree_model_get (model, iter,
2670 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2674 /* Returns a selected path from the file list */
2676 get_selected_file (GtkFileChooserDefault *impl)
2678 struct get_selected_file_closure closure;
2679 GtkTreeSelection *selection;
2681 closure.impl = impl;
2682 closure.file = NULL;
2684 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2685 gtk_tree_selection_selected_foreach (selection,
2686 get_selected_file_foreach_cb,
2689 return closure.file;
2693 GtkFileChooserDefault *impl;
2695 } UpdateTooltipData;
2698 update_tooltip (GtkTreeModel *model,
2703 UpdateTooltipData *udata = data;
2705 if (udata->tip == NULL)
2707 gchar *display_name;
2709 gtk_tree_model_get (model, iter,
2710 MODEL_COL_NAME, &display_name,
2713 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2715 g_free (display_name);
2720 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2721 * if there are no selected items *and* the current folder is not in the
2722 * bookmarks list. De-sensitize the button otherwise.
2725 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2728 gboolean all_folders;
2732 selection_check (impl, &num_selected, NULL, &all_folders);
2734 if (num_selected == 0)
2735 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2736 else if (num_selected == 1)
2740 file = get_selected_file (impl);
2741 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2743 g_object_unref (file);
2746 active = all_folders;
2748 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2750 if (impl->browse_files_popup_menu_add_shortcut_item)
2751 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2752 (num_selected == 0) ? FALSE : active);
2756 if (num_selected == 0)
2757 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2758 else if (num_selected > 1)
2759 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2762 GtkTreeSelection *selection;
2763 UpdateTooltipData data;
2765 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2768 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2772 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2777 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2778 * bookmark row is selected in the shortcuts tree.
2781 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2784 gboolean removable = FALSE;
2788 if (shortcuts_get_selected (impl, &iter))
2790 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2791 SHORTCUTS_COL_REMOVABLE, &removable,
2792 SHORTCUTS_COL_NAME, &name,
2794 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2797 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2799 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2801 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2805 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2806 _("Remove the selected bookmark"));
2811 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2814 gboolean removable = FALSE;
2816 if (impl->browse_shortcuts_popup_menu == NULL)
2819 if (shortcuts_get_selected (impl, &iter))
2820 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2821 SHORTCUTS_COL_REMOVABLE, &removable,
2824 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2825 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2828 /* GtkWidget::drag-begin handler for the shortcuts list. */
2830 shortcuts_drag_begin_cb (GtkWidget *widget,
2831 GdkDragContext *context,
2832 GtkFileChooserDefault *impl)
2835 impl->shortcuts_drag_context = g_object_ref (context);
2840 /* Removes the idle handler for outside drags */
2842 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2844 if (!impl->shortcuts_drag_outside_idle)
2847 g_source_destroy (impl->shortcuts_drag_outside_idle);
2848 impl->shortcuts_drag_outside_idle = NULL;
2852 /* GtkWidget::drag-end handler for the shortcuts list. */
2854 shortcuts_drag_end_cb (GtkWidget *widget,
2855 GdkDragContext *context,
2856 GtkFileChooserDefault *impl)
2859 g_object_unref (impl->shortcuts_drag_context);
2861 shortcuts_cancel_drag_outside_idle (impl);
2863 if (!impl->shortcuts_drag_outside)
2866 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2868 impl->shortcuts_drag_outside = FALSE;
2872 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2874 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2875 GdkDragContext *context,
2876 GtkFileChooserDefault *impl)
2878 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2881 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2885 shortcuts_drag_leave_cb (GtkWidget *widget,
2886 GdkDragContext *context,
2888 GtkFileChooserDefault *impl)
2891 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2893 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2897 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2899 GTK_TREE_VIEW_DROP_BEFORE);
2901 g_signal_stop_emission_by_name (widget, "drag-leave");
2904 /* Computes the appropriate row and position for dropping */
2906 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2910 GtkTreeViewDropPosition *pos)
2912 GtkTreeView *tree_view;
2913 GtkTreeViewColumn *column;
2917 int bookmarks_index;
2918 int header_height = 0;
2920 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2922 if (gtk_tree_view_get_headers_visible (tree_view))
2923 header_height = _gtk_tree_view_get_header_height (tree_view);
2925 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2927 if (!gtk_tree_view_get_path_at_pos (tree_view,
2935 row = bookmarks_index + impl->num_bookmarks - 1;
2936 *path = gtk_tree_path_new_from_indices (row, -1);
2937 *pos = GTK_TREE_VIEW_DROP_AFTER;
2941 row = *gtk_tree_path_get_indices (*path);
2942 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2943 gtk_tree_path_free (*path);
2945 if (row < bookmarks_index)
2947 row = bookmarks_index;
2948 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2950 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2952 row = bookmarks_index + impl->num_bookmarks - 1;
2953 *pos = GTK_TREE_VIEW_DROP_AFTER;
2957 if (cell_y < cell.height / 2)
2958 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2960 *pos = GTK_TREE_VIEW_DROP_AFTER;
2963 *path = gtk_tree_path_new_from_indices (row, -1);
2966 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2967 * implement the destination side of DnD by hand, due to limitations in
2968 * GtkTreeView's DnD API.
2971 shortcuts_drag_motion_cb (GtkWidget *widget,
2972 GdkDragContext *context,
2976 GtkFileChooserDefault *impl)
2979 GtkTreeViewDropPosition pos;
2980 GdkDragAction action;
2983 if (gtk_drag_get_source_widget (context) == widget)
2985 shortcuts_cancel_drag_outside_idle (impl);
2987 if (impl->shortcuts_drag_outside)
2989 shortcuts_drag_set_delete_cursor (impl, FALSE);
2990 impl->shortcuts_drag_outside = FALSE;
2995 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
2996 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
2997 action = GDK_ACTION_COPY;
2998 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
2999 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3000 action = GDK_ACTION_MOVE;
3007 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3008 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3009 gtk_tree_path_free (path);
3013 g_signal_stop_emission_by_name (widget, "drag-motion");
3017 gdk_drag_status (context, action, time_);
3024 /* GtkWidget::drag-drop handler for the shortcuts list. */
3026 shortcuts_drag_drop_cb (GtkWidget *widget,
3027 GdkDragContext *context,
3031 GtkFileChooserDefault *impl)
3034 shortcuts_cancel_drag_outside_idle (impl);
3037 g_signal_stop_emission_by_name (widget, "drag-drop");
3041 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3043 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3044 GtkSelectionData *selection_data,
3050 uris = gtk_selection_data_get_uris (selection_data);
3054 for (i = 0; uris[i]; i++)
3060 file = g_file_new_for_uri (uri);
3062 if (shortcuts_add_bookmark_from_file (impl, file, position))
3065 g_object_unref (file);
3071 /* Reorders the selected bookmark to the specified position */
3073 shortcuts_reorder (GtkFileChooserDefault *impl,
3078 ShortcutType shortcut_type;
3081 int bookmarks_index;
3086 /* Get the selected path */
3088 if (!shortcuts_get_selected (impl, &iter))
3089 g_assert_not_reached ();
3091 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3092 old_position = *gtk_tree_path_get_indices (path);
3093 gtk_tree_path_free (path);
3095 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3096 old_position -= bookmarks_index;
3097 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3099 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3100 SHORTCUTS_COL_NAME, &name,
3101 SHORTCUTS_COL_DATA, &col_data,
3102 SHORTCUTS_COL_TYPE, &shortcut_type,
3104 g_assert (col_data != NULL);
3105 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3108 g_object_ref (file); /* removal below will free file, so we need a new ref */
3110 /* Remove the path from the old position and insert it in the new one */
3112 if (new_position > old_position)
3115 if (old_position == new_position)
3119 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3121 shortcuts_add_bookmark_from_file (impl, file, new_position);
3122 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3125 error_adding_bookmark_dialog (impl, file, error);
3129 g_object_unref (file);
3133 /* Callback used when we get the drag data for the bookmarks list. We add the
3134 * received URIs as bookmarks if they are folders.
3137 shortcuts_drag_data_received_cb (GtkWidget *widget,
3138 GdkDragContext *context,
3141 GtkSelectionData *selection_data,
3146 GtkFileChooserDefault *impl;
3147 GtkTreePath *tree_path;
3148 GtkTreeViewDropPosition tree_pos;
3151 int bookmarks_index;
3153 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3155 /* Compute position */
3157 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3159 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3160 position = *gtk_tree_path_get_indices (tree_path);
3161 gtk_tree_path_free (tree_path);
3163 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3166 g_assert (position >= bookmarks_index);
3167 position -= bookmarks_index;
3169 target = gtk_selection_data_get_target (selection_data);
3171 if (gtk_targets_include_uri (&target, 1))
3172 shortcuts_drop_uris (impl, selection_data, position);
3173 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3174 shortcuts_reorder (impl, position);
3176 g_signal_stop_emission_by_name (widget, "drag-data-received");
3179 /* Callback used to display a tooltip in the shortcuts tree */
3181 shortcuts_query_tooltip_cb (GtkWidget *widget,
3184 gboolean keyboard_mode,
3185 GtkTooltip *tooltip,
3186 GtkFileChooserDefault *impl)
3188 GtkTreeModel *model;
3191 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3199 ShortcutType shortcut_type;
3201 gtk_tree_model_get (model, &iter,
3202 SHORTCUTS_COL_DATA, &col_data,
3203 SHORTCUTS_COL_TYPE, &shortcut_type,
3206 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3208 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3210 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3215 file = G_FILE (col_data);
3216 parse_name = g_file_get_parse_name (file);
3218 gtk_tooltip_set_text (tooltip, parse_name);
3220 g_free (parse_name);
3224 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3228 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3238 /* Callback used when the selection in the shortcuts tree changes */
3240 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3241 GtkFileChooserDefault *impl)
3244 GtkTreeIter child_iter;
3246 bookmarks_check_remove_sensitivity (impl);
3247 shortcuts_check_popup_sensitivity (impl);
3249 if (impl->changing_folder)
3252 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3254 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3257 shortcuts_activate_iter (impl, &child_iter);
3262 shortcuts_row_separator_func (GtkTreeModel *model,
3266 ShortcutType shortcut_type;
3268 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3270 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3274 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3276 GtkFileChooserDefault *impl)
3280 /* don't screw up focus switching with Tab */
3281 if (event->keyval == GDK_KEY_Tab
3282 || event->keyval == GDK_KEY_KP_Tab
3283 || event->keyval == GDK_KEY_ISO_Left_Tab
3284 || event->length < 1)
3287 if (impl->location_entry)
3288 entry = impl->location_entry;
3289 else if (impl->search_entry)
3290 entry = impl->search_entry;
3296 gtk_widget_grab_focus (entry);
3297 return gtk_widget_event (entry, (GdkEvent *) event);
3303 /* Callback used when the file list's popup menu is detached */
3305 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3308 GtkFileChooserDefault *impl;
3310 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3311 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3313 impl->browse_shortcuts_popup_menu = NULL;
3314 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3315 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3319 remove_shortcut_cb (GtkMenuItem *item,
3320 GtkFileChooserDefault *impl)
3322 remove_selected_bookmarks (impl);
3325 /* Rename the selected bookmark */
3327 rename_selected_bookmark (GtkFileChooserDefault *impl)
3331 GtkTreeViewColumn *column;
3332 GtkCellRenderer *cell;
3335 if (shortcuts_get_selected (impl, &iter))
3337 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3338 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3339 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3340 cell = g_list_nth_data (renderers, 1);
3341 g_list_free (renderers);
3342 g_object_set (cell, "editable", TRUE, NULL);
3343 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3344 path, column, cell, TRUE);
3345 gtk_tree_path_free (path);
3350 rename_shortcut_cb (GtkMenuItem *item,
3351 GtkFileChooserDefault *impl)
3353 rename_selected_bookmark (impl);
3356 /* Constructs the popup menu for the file list if needed */
3358 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3362 if (impl->browse_shortcuts_popup_menu)
3365 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3366 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3367 impl->browse_shortcuts_tree_view,
3368 shortcuts_popup_menu_detach_cb);
3370 item = gtk_image_menu_item_new_with_label (_("Remove"));
3371 impl->browse_shortcuts_popup_menu_remove_item = item;
3372 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3373 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3374 g_signal_connect (item, "activate",
3375 G_CALLBACK (remove_shortcut_cb), impl);
3376 gtk_widget_show (item);
3377 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3379 item = gtk_menu_item_new_with_label (_("Rename..."));
3380 impl->browse_shortcuts_popup_menu_rename_item = item;
3381 g_signal_connect (item, "activate",
3382 G_CALLBACK (rename_shortcut_cb), impl);
3383 gtk_widget_show (item);
3384 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3388 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3390 shortcuts_build_popup_menu (impl);
3391 shortcuts_check_popup_sensitivity (impl);
3395 popup_position_func (GtkMenu *menu,
3399 gpointer user_data);
3402 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3403 GdkEventButton *event)
3405 shortcuts_update_popup_menu (impl);
3407 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3408 NULL, NULL, NULL, NULL,
3409 event->button, event->time);
3412 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3414 popup_position_func, impl->browse_shortcuts_tree_view,
3415 0, GDK_CURRENT_TIME);
3416 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3421 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3423 shortcuts_popup_menu_cb (GtkWidget *widget,
3424 GtkFileChooserDefault *impl)
3426 shortcuts_popup_menu (impl, NULL);
3430 /* Callback used when a button is pressed on the shortcuts list.
3431 * We trap button 3 to bring up a popup menu.
3434 shortcuts_button_press_event_cb (GtkWidget *widget,
3435 GdkEventButton *event,
3436 GtkFileChooserDefault *impl)
3438 static gboolean in_press = FALSE;
3444 if (event->button != 3)
3448 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3454 shortcuts_popup_menu (impl, event);
3459 shortcuts_edited (GtkCellRenderer *cell,
3462 GtkFileChooserDefault *impl)
3468 g_object_set (cell, "editable", FALSE, NULL);
3470 path = gtk_tree_path_new_from_string (path_string);
3471 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3472 g_assert_not_reached ();
3474 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3475 SHORTCUTS_COL_DATA, &shortcut,
3477 gtk_tree_path_free (path);
3479 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3483 shortcuts_editing_canceled (GtkCellRenderer *cell,
3484 GtkFileChooserDefault *impl)
3486 g_object_set (cell, "editable", FALSE, NULL);
3489 /* Creates the widgets for the shortcuts and bookmarks tree */
3491 shortcuts_list_create (GtkFileChooserDefault *impl)
3494 GtkTreeSelection *selection;
3495 GtkTreeViewColumn *column;
3496 GtkCellRenderer *renderer;
3498 /* Target types for dragging a row to/from the shortcuts list */
3499 const GtkTargetEntry tree_model_row_targets[] = {
3500 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3503 /* Scrolled window */
3505 swin = gtk_scrolled_window_new (NULL, NULL);
3506 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3507 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3508 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3510 gtk_widget_show (swin);
3513 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3514 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3515 GTK_STYLE_CLASS_SIDEBAR);
3516 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3517 #ifdef PROFILE_FILE_CHOOSER
3518 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3521 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3523 * 1. user brings up a SAVE dialog
3524 * 2. user clicks on a shortcut in the shortcuts pane
3525 * 3. user starts typing a filename
3527 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3528 * support interactive search. However, we'd rather focus the location entry
3529 * so that the user can type *there*.
3531 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3532 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3533 * if the user starts typing while the focus is in the shortcuts pane.
3535 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3536 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3538 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3539 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3540 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3541 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3542 /* Accessible object name for the file chooser's shortcuts pane */
3543 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3545 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3547 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3549 tree_model_row_targets,
3550 G_N_ELEMENTS (tree_model_row_targets),
3553 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3554 GTK_DEST_DEFAULT_ALL,
3555 tree_model_row_targets,
3556 G_N_ELEMENTS (tree_model_row_targets),
3557 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3558 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3560 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3561 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3562 gtk_tree_selection_set_select_function (selection,
3563 shortcuts_select_func,
3566 g_signal_connect (selection, "changed",
3567 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3569 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3570 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3572 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3573 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3574 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3575 G_CALLBACK (shortcuts_drag_end_cb), impl);
3576 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3577 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3579 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3580 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3581 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3582 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3583 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3584 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3585 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3586 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3588 /* Support tooltips */
3589 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3590 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3591 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3593 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3594 gtk_widget_show (impl->browse_shortcuts_tree_view);
3598 column = gtk_tree_view_column_new ();
3599 /* Column header for the file chooser's shortcuts pane */
3600 gtk_tree_view_column_set_title (column, _("_Places"));
3602 renderer = gtk_cell_renderer_pixbuf_new ();
3603 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3604 gtk_tree_view_column_set_attributes (column, renderer,
3605 "pixbuf", SHORTCUTS_COL_PIXBUF,
3606 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3609 renderer = gtk_cell_renderer_text_new ();
3610 g_object_set (renderer,
3612 "ellipsize", PANGO_ELLIPSIZE_END,
3614 g_signal_connect (renderer, "edited",
3615 G_CALLBACK (shortcuts_edited), impl);
3616 g_signal_connect (renderer, "editing-canceled",
3617 G_CALLBACK (shortcuts_editing_canceled), impl);
3618 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3619 gtk_tree_view_column_set_attributes (column, renderer,
3620 "text", SHORTCUTS_COL_NAME,
3623 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3624 shortcuts_row_separator_func,
3627 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3632 /* Creates the widgets for the shortcuts/bookmarks pane */
3634 shortcuts_pane_create (GtkFileChooserDefault *impl,
3635 GtkSizeGroup *size_group)
3640 GtkStyleContext *context;
3643 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3644 gtk_widget_show (vbox);
3646 /* Shortcuts tree */
3648 widget = shortcuts_list_create (impl);
3650 gtk_size_group_add_widget (size_group, widget);
3651 context = gtk_widget_get_style_context (widget);
3652 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3654 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3656 /* Box for buttons */
3658 toolbar = gtk_toolbar_new ();
3659 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3661 context = gtk_widget_get_style_context (toolbar);
3662 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3663 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3665 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3666 gtk_widget_show (toolbar);
3668 /* Add bookmark button */
3669 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3670 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3674 G_CALLBACK (add_bookmark_button_clicked_cb));
3675 g_object_unref (icon);
3677 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3678 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3679 _("Add the selected folder to the Bookmarks"));
3681 /* Remove bookmark button */
3682 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3683 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3687 G_CALLBACK (remove_bookmark_button_clicked_cb));
3688 g_object_unref (icon);
3689 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3690 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3691 _("Remove the selected bookmark"));
3697 key_is_left_or_right (GdkEventKey *event)
3701 modifiers = gtk_accelerator_get_default_mod_mask ();
3703 return ((event->keyval == GDK_KEY_Right
3704 || event->keyval == GDK_KEY_KP_Right
3705 || event->keyval == GDK_KEY_Left
3706 || event->keyval == GDK_KEY_KP_Left)
3707 && (event->state & modifiers) == 0);
3710 /* Handles key press events on the file list, so that we can trap Enter to
3711 * activate the default button on our own. Also, checks to see if '/' has been
3712 * pressed. See comment by tree_view_keybinding_cb() for more details.
3715 browse_files_key_press_event_cb (GtkWidget *widget,
3719 GtkFileChooserDefault *impl;
3722 impl = (GtkFileChooserDefault *) data;
3724 modifiers = gtk_accelerator_get_default_mod_mask ();
3726 if ((event->keyval == GDK_KEY_slash
3727 || event->keyval == GDK_KEY_KP_Divide
3729 || event->keyval == GDK_KEY_asciitilde
3731 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3733 location_popup_handler (impl, event->string);
3737 if (key_is_left_or_right (event))
3739 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3743 if ((event->keyval == GDK_KEY_Return
3744 || event->keyval == GDK_KEY_ISO_Enter
3745 || event->keyval == GDK_KEY_KP_Enter
3746 || event->keyval == GDK_KEY_space
3747 || event->keyval == GDK_KEY_KP_Space)
3748 && ((event->state & modifiers) == 0)
3749 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3750 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3754 window = get_toplevel (widget);
3757 GtkWidget *default_widget, *focus_widget;
3759 default_widget = gtk_window_get_default_widget (window);
3760 focus_widget = gtk_window_get_focus (window);
3762 if (widget != default_widget &&
3763 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3765 gtk_window_activate_default (window);
3775 /* Callback used when the file list's popup menu is detached */
3777 popup_menu_detach_cb (GtkWidget *attach_widget,
3780 GtkFileChooserDefault *impl;
3782 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3783 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3785 impl->browse_files_popup_menu = NULL;
3786 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3787 impl->browse_files_popup_menu_hidden_files_item = NULL;
3790 /* Callback used when the "Add to Bookmarks" menu item is activated */
3792 add_to_shortcuts_cb (GtkMenuItem *item,
3793 GtkFileChooserDefault *impl)
3795 bookmarks_add_selected_folder (impl);
3798 /* Callback used when the "Show Hidden Files" menu item is toggled */
3800 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3801 GtkFileChooserDefault *impl)
3804 "show-hidden", gtk_check_menu_item_get_active (item),
3808 /* Callback used when the "Show Size Column" menu item is toggled */
3810 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3811 GtkFileChooserDefault *impl)
3813 impl->show_size_column = gtk_check_menu_item_get_active (item);
3815 gtk_tree_view_column_set_visible (impl->list_size_column,
3816 impl->show_size_column);
3819 /* Shows an error dialog about not being able to select a dragged file */
3821 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3826 _("Could not select file"),
3831 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3836 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3838 for (i = 1; uris[i]; i++)
3841 GError *error = NULL;
3844 file = g_file_new_for_uri (uri);
3846 gtk_file_chooser_default_select_file (chooser, file, &error);
3848 error_selecting_dragged_file_dialog (impl, file, error);
3850 g_object_unref (file);
3854 struct FileListDragData
3856 GtkFileChooserDefault *impl;
3862 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3864 const GError *error,
3867 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3868 struct FileListDragData *data = user_data;
3869 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3871 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3874 data->impl->file_list_drag_data_received_cancellable = NULL;
3876 if (cancelled || error)
3879 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3880 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3881 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
3882 change_folder_and_display_error (data->impl, data->file, FALSE);
3885 GError *error = NULL;
3887 gtk_file_chooser_default_unselect_all (chooser);
3888 gtk_file_chooser_default_select_file (chooser, data->file, &error);
3890 error_selecting_dragged_file_dialog (data->impl, data->file, error);
3892 browse_files_center_selected_row (data->impl);
3895 if (data->impl->select_multiple)
3896 file_list_drag_data_select_uris (data->impl, data->uris);
3899 g_object_unref (data->impl);
3900 g_strfreev (data->uris);
3901 g_object_unref (data->file);
3904 g_object_unref (cancellable);
3908 file_list_drag_data_received_cb (GtkWidget *widget,
3909 GdkDragContext *context,
3912 GtkSelectionData *selection_data,
3917 GtkFileChooserDefault *impl;
3922 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3924 /* Allow only drags from other widgets; see bug #533891. */
3925 if (gtk_drag_get_source_widget (context) == widget)
3927 g_signal_stop_emission_by_name (widget, "drag-data-received");
3931 /* Parse the text/uri-list string, navigate to the first one */
3932 uris = gtk_selection_data_get_uris (selection_data);
3933 if (uris && uris[0])
3935 struct FileListDragData *data;
3938 file = g_file_new_for_uri (uri);
3940 data = g_new0 (struct FileListDragData, 1);
3941 data->impl = g_object_ref (impl);
3945 if (impl->file_list_drag_data_received_cancellable)
3946 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
3948 impl->file_list_drag_data_received_cancellable =
3949 _gtk_file_system_get_info (impl->file_system, file,
3951 file_list_drag_data_received_get_info_cb,
3955 g_signal_stop_emission_by_name (widget, "drag-data-received");
3958 /* Don't do anything with the drag_drop signal */
3960 file_list_drag_drop_cb (GtkWidget *widget,
3961 GdkDragContext *context,
3965 GtkFileChooserDefault *impl)
3967 g_signal_stop_emission_by_name (widget, "drag-drop");
3971 /* Disable the normal tree drag motion handler, it makes it look like you're
3972 dropping the dragged item onto a tree item */
3974 file_list_drag_motion_cb (GtkWidget *widget,
3975 GdkDragContext *context,
3979 GtkFileChooserDefault *impl)
3981 g_signal_stop_emission_by_name (widget, "drag-motion");
3985 /* Constructs the popup menu for the file list if needed */
3987 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3991 if (impl->browse_files_popup_menu)
3994 impl->browse_files_popup_menu = gtk_menu_new ();
3995 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3996 impl->browse_files_tree_view,
3997 popup_menu_detach_cb);
3999 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4000 impl->browse_files_popup_menu_add_shortcut_item = item;
4001 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4002 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4003 g_signal_connect (item, "activate",
4004 G_CALLBACK (add_to_shortcuts_cb), impl);
4005 gtk_widget_show (item);
4006 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4008 item = gtk_separator_menu_item_new ();
4009 gtk_widget_show (item);
4010 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4012 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4013 impl->browse_files_popup_menu_hidden_files_item = item;
4014 g_signal_connect (item, "toggled",
4015 G_CALLBACK (show_hidden_toggled_cb), impl);
4016 gtk_widget_show (item);
4017 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4019 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4020 impl->browse_files_popup_menu_size_column_item = item;
4021 g_signal_connect (item, "toggled",
4022 G_CALLBACK (show_size_column_toggled_cb), impl);
4023 gtk_widget_show (item);
4024 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4026 bookmarks_check_add_sensitivity (impl);
4029 /* Updates the popup menu for the file list, creating it if necessary */
4031 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4033 file_list_build_popup_menu (impl);
4035 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4037 /* The sensitivity of the Add to Bookmarks item is set in
4038 * bookmarks_check_add_sensitivity()
4041 /* 'Show Hidden Files' */
4042 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4043 G_CALLBACK (show_hidden_toggled_cb), impl);
4044 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4046 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4047 G_CALLBACK (show_hidden_toggled_cb), impl);
4049 /* 'Show Size Column' */
4050 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4051 G_CALLBACK (show_size_column_toggled_cb), impl);
4052 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4053 impl->show_size_column);
4054 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4055 G_CALLBACK (show_size_column_toggled_cb), impl);
4059 popup_position_func (GtkMenu *menu,
4065 GtkAllocation allocation;
4066 GtkWidget *widget = GTK_WIDGET (user_data);
4067 GdkScreen *screen = gtk_widget_get_screen (widget);
4070 GdkRectangle monitor;
4072 g_return_if_fail (gtk_widget_get_realized (widget));
4074 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4076 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4079 gtk_widget_get_allocation (widget, &allocation);
4080 *x += (allocation.width - req.width) / 2;
4081 *y += (allocation.height - req.height) / 2;
4083 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4084 gtk_menu_set_monitor (menu, monitor_num);
4085 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4087 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4088 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4094 file_list_popup_menu (GtkFileChooserDefault *impl,
4095 GdkEventButton *event)
4097 file_list_update_popup_menu (impl);
4099 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4100 NULL, NULL, NULL, NULL,
4101 event->button, event->time);
4104 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4106 popup_position_func, impl->browse_files_tree_view,
4107 0, GDK_CURRENT_TIME);
4108 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4114 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4116 list_popup_menu_cb (GtkWidget *widget,
4117 GtkFileChooserDefault *impl)
4119 file_list_popup_menu (impl, NULL);
4123 /* Callback used when a button is pressed on the file list. We trap button 3 to
4124 * bring up a popup menu.
4127 list_button_press_event_cb (GtkWidget *widget,
4128 GdkEventButton *event,
4129 GtkFileChooserDefault *impl)
4131 static gboolean in_press = FALSE;
4136 if (event->button != 3)
4140 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4143 file_list_popup_menu (impl, event);
4148 OperationMode operation_mode;
4149 gint general_column;
4153 /* Sets the sort column IDs for the file list based on the operation mode */
4155 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4157 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4158 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4159 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4163 file_list_query_tooltip_cb (GtkWidget *widget,
4166 gboolean keyboard_tip,
4167 GtkTooltip *tooltip,
4170 GtkFileChooserDefault *impl = user_data;
4171 GtkTreeModel *model;
4177 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4181 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4184 &model, &path, &iter))
4187 gtk_tree_model_get (model, &iter,
4188 MODEL_COL_FILE, &file,
4193 gtk_tree_path_free (path);
4197 filename = g_file_get_path (file);
4198 gtk_tooltip_set_text (tooltip, filename);
4199 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4204 g_object_unref (file);
4205 gtk_tree_path_free (path);
4211 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4215 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4216 gtk_cell_renderer_set_fixed_size (renderer,
4217 xpad * 2 + impl->icon_size,
4218 ypad * 2 + impl->icon_size);
4221 /* Creates the widgets for the file list */
4223 create_file_list (GtkFileChooserDefault *impl)
4226 GtkTreeSelection *selection;
4227 GtkTreeViewColumn *column;
4228 GtkCellRenderer *renderer;
4230 /* Scrolled window */
4231 swin = gtk_scrolled_window_new (NULL, NULL);
4232 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4233 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4234 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4237 /* Tree/list view */
4239 impl->browse_files_tree_view = gtk_tree_view_new ();
4240 #ifdef PROFILE_FILE_CHOOSER
4241 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4243 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4244 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4246 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4247 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4249 gtk_drag_dest_set (impl->browse_files_tree_view,
4250 GTK_DEST_DEFAULT_ALL,
4252 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4253 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4255 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4256 G_CALLBACK (list_row_activated), impl);
4257 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4258 G_CALLBACK (browse_files_key_press_event_cb), impl);
4259 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4260 G_CALLBACK (list_popup_menu_cb), impl);
4261 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4262 G_CALLBACK (list_button_press_event_cb), impl);
4264 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4265 G_CALLBACK (file_list_drag_data_received_cb), impl);
4266 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4267 G_CALLBACK (file_list_drag_drop_cb), impl);
4268 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4269 G_CALLBACK (file_list_drag_motion_cb), impl);
4271 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4272 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4273 G_CALLBACK (file_list_query_tooltip_cb), impl);
4275 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4276 gtk_tree_selection_set_select_function (selection,
4279 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4282 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4283 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4285 g_signal_connect (selection, "changed",
4286 G_CALLBACK (list_selection_changed), impl);
4288 /* Keep the column order in sync with update_cell_renderer_attributes() */
4290 /* Filename column */
4292 impl->list_name_column = gtk_tree_view_column_new ();
4293 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4294 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4295 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4297 renderer = gtk_cell_renderer_pixbuf_new ();
4298 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4299 set_icon_cell_renderer_fixed_size (impl, renderer);
4300 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4302 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4303 g_object_set (impl->list_name_renderer,
4304 "ellipsize", PANGO_ELLIPSIZE_END,
4306 g_signal_connect (impl->list_name_renderer, "edited",
4307 G_CALLBACK (renderer_edited_cb), impl);
4308 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4309 G_CALLBACK (renderer_editing_canceled_cb), impl);
4310 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4312 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4316 column = gtk_tree_view_column_new ();
4317 gtk_tree_view_column_set_resizable (column, TRUE);
4318 gtk_tree_view_column_set_title (column, _("Size"));
4320 renderer = gtk_cell_renderer_text_new ();
4321 g_object_set (renderer,
4322 "alignment", PANGO_ALIGN_RIGHT,
4324 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4325 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4326 impl->list_size_column = column;
4328 /* Modification time column */
4330 column = gtk_tree_view_column_new ();
4331 gtk_tree_view_column_set_resizable (column, TRUE);
4332 gtk_tree_view_column_set_title (column, _("Modified"));
4334 renderer = gtk_cell_renderer_text_new ();
4335 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4336 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4337 impl->list_mtime_column = column;
4339 file_list_set_sort_column_ids (impl);
4340 update_cell_renderer_attributes (impl);
4342 gtk_widget_show_all (swin);
4348 create_path_bar (GtkFileChooserDefault *impl)
4350 GtkWidget *path_bar;
4352 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4353 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4358 /* Creates the widgets for the files/folders pane */
4360 file_pane_create (GtkFileChooserDefault *impl,
4361 GtkSizeGroup *size_group)
4367 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4368 gtk_widget_show (vbox);
4370 /* Box for lists and preview */
4372 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4373 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4374 gtk_widget_show (hbox);
4378 widget = create_file_list (impl);
4379 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4380 gtk_size_group_add_widget (size_group, widget);
4384 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4385 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4386 /* Don't show preview box initially */
4390 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4392 widget = filter_create (impl);
4394 gtk_widget_show (widget);
4395 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4397 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4402 /* Creates the widgets specific to Save mode */
4404 save_widgets_create (GtkFileChooserDefault *impl)
4409 if (impl->save_widgets != NULL)
4412 location_switch_to_path_bar (impl);
4414 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4416 impl->save_widgets_table = gtk_table_new (2, 2, FALSE);
4417 gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4418 gtk_widget_show (impl->save_widgets_table);
4419 gtk_table_set_row_spacings (GTK_TABLE (impl->save_widgets_table), 12);
4420 gtk_table_set_col_spacings (GTK_TABLE (impl->save_widgets_table), 12);
4424 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4425 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4426 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4427 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), widget,
4431 gtk_widget_show (widget);
4433 /* Location entry */
4435 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4436 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4438 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4439 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4440 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4441 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), impl->location_entry,
4443 GTK_EXPAND | GTK_FILL, 0,
4445 gtk_widget_show (impl->location_entry);
4446 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4449 impl->save_folder_label = gtk_label_new (NULL);
4450 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4451 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4452 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), impl->save_folder_label,
4456 gtk_widget_show (impl->save_folder_label);
4458 impl->save_widgets = vbox;
4459 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4460 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4461 gtk_widget_show (impl->save_widgets);
4464 /* Destroys the widgets specific to Save mode */
4466 save_widgets_destroy (GtkFileChooserDefault *impl)
4468 if (impl->save_widgets == NULL)
4471 gtk_widget_destroy (impl->save_widgets);
4472 impl->save_widgets = NULL;
4473 impl->save_widgets_table = NULL;
4474 impl->location_entry = NULL;
4475 impl->save_folder_label = NULL;
4478 /* Turns on the path bar widget. Can be called even if we are already in that
4482 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4484 if (impl->location_entry)
4486 gtk_widget_destroy (impl->location_entry);
4487 impl->location_entry = NULL;
4490 gtk_widget_hide (impl->location_entry_box);
4493 /* Sets the full path of the current folder as the text in the location entry. */
4495 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4497 gchar *text, *filename;
4499 if (!impl->current_folder)
4502 filename = g_file_get_path (impl->current_folder);
4506 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4510 text = g_file_get_uri (impl->current_folder);
4514 gboolean need_slash;
4517 len = strlen (text);
4518 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4524 slash_text = g_new (char, len + 2);
4525 strcpy (slash_text, text);
4526 slash_text[len] = G_DIR_SEPARATOR;
4527 slash_text[len + 1] = 0;
4533 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4540 /* Turns on the location entry. Can be called even if we are already in that
4544 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4546 /* when in search or recent files mode, we are not showing the
4547 * location_entry_box container, so there's no point in switching
4550 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4551 impl->operation_mode == OPERATION_MODE_RECENT)
4554 if (impl->location_entry)
4555 gtk_widget_destroy (impl->location_entry);
4559 gtk_widget_show (impl->location_entry_box);
4563 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4564 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4566 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4567 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4569 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4570 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4572 /* Configure the entry */
4574 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4575 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4579 gtk_widget_show (impl->location_entry);
4580 gtk_widget_grab_focus (impl->location_entry);
4583 /* Sets a new location mode. set_buttons determines whether the toggle button
4584 * for the mode will also be changed.
4587 location_mode_set (GtkFileChooserDefault *impl,
4588 LocationMode new_mode,
4589 gboolean set_button)
4591 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4592 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4594 GtkWindow *toplevel;
4595 GtkWidget *current_focus;
4596 gboolean button_active;
4597 gboolean switch_to_file_list;
4601 case LOCATION_MODE_PATH_BAR:
4602 button_active = FALSE;
4604 /* The location_entry will disappear when we switch to path bar mode. So,
4605 * we'll focus the file list in that case, to avoid having a window with
4606 * no focused widget.
4608 toplevel = get_toplevel (GTK_WIDGET (impl));
4609 switch_to_file_list = FALSE;
4612 current_focus = gtk_window_get_focus (toplevel);
4613 if (!current_focus || current_focus == impl->location_entry)
4614 switch_to_file_list = TRUE;
4617 location_switch_to_path_bar (impl);
4619 if (switch_to_file_list)
4620 gtk_widget_grab_focus (impl->browse_files_tree_view);
4624 case LOCATION_MODE_FILENAME_ENTRY:
4625 button_active = TRUE;
4626 location_switch_to_filename_entry (impl);
4630 g_assert_not_reached ();
4636 g_signal_handlers_block_by_func (impl->location_button,
4637 G_CALLBACK (location_button_toggled_cb), impl);
4639 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4641 g_signal_handlers_unblock_by_func (impl->location_button,
4642 G_CALLBACK (location_button_toggled_cb), impl);
4646 impl->location_mode = new_mode;
4650 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4652 /* when in search or recent files mode, we are not showing the
4653 * location_entry_box container, so there's no point in switching
4656 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4657 impl->operation_mode == OPERATION_MODE_RECENT)
4660 /* If the file entry is not visible, show it.
4661 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4663 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4665 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4667 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4669 if (gtk_widget_has_focus (impl->location_entry))
4671 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4675 gtk_widget_grab_focus (impl->location_entry);
4680 /* Callback used when one of the location mode buttons is toggled */
4682 location_button_toggled_cb (GtkToggleButton *toggle,
4683 GtkFileChooserDefault *impl)
4686 LocationMode new_mode;
4688 is_active = gtk_toggle_button_get_active (toggle);
4692 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4693 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4697 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4698 new_mode = LOCATION_MODE_PATH_BAR;
4701 location_mode_set (impl, new_mode, FALSE);
4704 /* Creates a toggle button for the location entry. */
4706 location_button_create (GtkFileChooserDefault *impl)
4711 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4712 gtk_widget_show (image);
4714 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4718 g_signal_connect (impl->location_button, "toggled",
4719 G_CALLBACK (location_button_toggled_cb), impl);
4721 str = _("Type a file name");
4723 gtk_widget_set_tooltip_text (impl->location_button, str);
4724 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4727 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4729 browse_widgets_create (GtkFileChooserDefault *impl)
4733 GtkSizeGroup *size_group;
4735 /* size group is used by the scrolled windows of the panes */
4736 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4737 impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4739 /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4740 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4741 gtk_widget_show (impl->browse_path_bar_hbox);
4743 /* Size group that allows the path bar to be the same size between modes */
4744 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4745 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4747 /* Location button */
4749 location_button_create (impl);
4750 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4751 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4755 impl->browse_path_bar = create_path_bar (impl);
4756 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4757 gtk_widget_show_all (impl->browse_path_bar);
4758 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4759 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4762 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4763 g_signal_connect (impl->browse_new_folder_button, "clicked",
4764 G_CALLBACK (new_folder_button_clicked), impl);
4765 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4767 /* Box for the location label and entry */
4769 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4770 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->location_entry_box, FALSE, FALSE, 0);
4772 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
4773 gtk_widget_show (impl->location_label);
4774 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
4778 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
4779 gtk_widget_show (hpaned);
4780 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
4782 widget = shortcuts_pane_create (impl, size_group);
4783 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
4784 widget = file_pane_create (impl, size_group);
4785 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
4786 gtk_paned_set_position (GTK_PANED (hpaned), 148);
4787 g_object_unref (size_group);
4791 gtk_file_chooser_default_constructor (GType type,
4792 guint n_construct_properties,
4793 GObjectConstructParam *construct_params)
4795 GtkFileChooserDefault *impl;
4798 profile_start ("start", NULL);
4800 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
4801 n_construct_properties,
4803 impl = GTK_FILE_CHOOSER_DEFAULT (object);
4805 g_assert (impl->file_system);
4807 gtk_widget_push_composite_child ();
4809 /* Shortcuts model */
4810 shortcuts_model_create (impl);
4812 /* The browse widgets */
4813 browse_widgets_create (impl);
4814 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
4816 /* Alignment to hold extra widget */
4817 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
4818 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
4819 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
4820 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
4822 gtk_widget_pop_composite_child ();
4823 update_appearance (impl);
4825 profile_end ("end", NULL);
4830 /* Sets the extra_widget by packing it in the appropriate place */
4832 set_extra_widget (GtkFileChooserDefault *impl,
4833 GtkWidget *extra_widget)
4837 g_object_ref (extra_widget);
4838 /* FIXME: is this right ? */
4839 gtk_widget_show (extra_widget);
4842 if (impl->extra_widget)
4844 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4845 g_object_unref (impl->extra_widget);
4848 impl->extra_widget = extra_widget;
4849 if (impl->extra_widget)
4851 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4852 gtk_widget_show (impl->extra_align);
4855 gtk_widget_hide (impl->extra_align);
4859 set_local_only (GtkFileChooserDefault *impl,
4860 gboolean local_only)
4862 if (local_only != impl->local_only)
4864 impl->local_only = local_only;
4866 if (impl->location_entry)
4867 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
4869 if (impl->shortcuts_model && impl->file_system)
4871 shortcuts_add_volumes (impl);
4872 shortcuts_add_bookmarks (impl);
4875 if (local_only && impl->current_folder &&
4876 !g_file_is_native (impl->current_folder))
4878 /* If we are pointing to a non-local folder, make an effort to change
4879 * back to a local folder, but it's really up to the app to not cause
4880 * such a situation, so we ignore errors.
4882 const gchar *home = g_get_home_dir ();
4888 home_file = g_file_new_for_path (home);
4890 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
4892 g_object_unref (home_file);
4898 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
4899 GtkFileChooserDefault *impl)
4901 shortcuts_add_volumes (impl);
4902 shortcuts_add_bookmarks (impl);
4904 bookmarks_check_add_sensitivity (impl);
4905 bookmarks_check_remove_sensitivity (impl);
4906 shortcuts_check_popup_sensitivity (impl);
4909 /* Sets the file chooser to multiple selection mode */
4911 set_select_multiple (GtkFileChooserDefault *impl,
4912 gboolean select_multiple,
4913 gboolean property_notify)
4915 GtkTreeSelection *selection;
4916 GtkSelectionMode mode;
4918 if (select_multiple == impl->select_multiple)
4921 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
4923 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4924 gtk_tree_selection_set_mode (selection, mode);
4926 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
4928 impl->select_multiple = select_multiple;
4929 g_object_notify (G_OBJECT (impl), "select-multiple");
4931 check_preview_change (impl);
4935 set_file_system_backend (GtkFileChooserDefault *impl)
4937 profile_start ("start for backend", "default");
4939 impl->file_system = _gtk_file_system_new ();
4941 g_signal_connect (impl->file_system, "volumes-changed",
4942 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
4943 g_signal_connect (impl->file_system, "bookmarks-changed",
4944 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
4946 profile_end ("end", NULL);
4950 unset_file_system_backend (GtkFileChooserDefault *impl)
4952 g_signal_handlers_disconnect_by_func (impl->file_system,
4953 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
4955 g_object_unref (impl->file_system);
4957 impl->file_system = NULL;
4960 /* Saves the widgets around the pathbar so they can be reparented later
4961 * in the correct place. This function must be called paired with
4962 * restore_path_bar().
4965 save_path_bar (GtkFileChooserDefault *impl)
4969 g_object_ref (impl->browse_path_bar_hbox);
4971 parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
4973 gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
4976 /* Reparents the path bar and the "Create folder" button to the right place:
4977 * Above the file list in Open mode, or to the right of the "Save in folder:"
4978 * label in Save mode. The save_path_bar() function must be called before this
4982 restore_path_bar (GtkFileChooserDefault *impl)
4984 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4985 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4987 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
4988 gtk_box_reorder_child (GTK_BOX (impl->browse_widgets_box), impl->browse_path_bar_hbox, 0);
4990 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4991 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4993 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), impl->browse_path_bar_hbox,
4995 GTK_EXPAND | GTK_FILL, GTK_FILL,
4997 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5000 g_assert_not_reached ();
5002 g_object_unref (impl->browse_path_bar_hbox);
5005 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5007 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5011 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5012 MODEL_COL_FILE, &file,
5014 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE, NULL); /* NULL-GError */
5015 g_object_unref (file);
5018 /* For recently-used mode, updates the path in the pathbar with the currently-selected item */
5020 update_path_bar (GtkFileChooserDefault *impl)
5022 if (impl->operation_mode == OPERATION_MODE_RECENT
5023 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5025 GtkTreeSelection *selection;
5026 gboolean have_selected;
5029 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5031 /* Save mode means single-selection mode, so the following is valid */
5032 have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5036 put_recent_folder_in_pathbar (impl, &iter);
5037 gtk_widget_show (impl->browse_path_bar);
5038 gtk_widget_hide (impl->browse_select_a_folder_label);
5042 gtk_widget_hide (impl->browse_path_bar);
5043 gtk_widget_show (impl->browse_select_a_folder_label);
5048 /* This function is basically a do_all function.
5050 * It sets the visibility on all the widgets based on the current state, and
5051 * moves the custom_widget if needed.
5054 update_appearance (GtkFileChooserDefault *impl)
5056 save_path_bar (impl);
5058 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5059 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5063 gtk_widget_hide (impl->location_button);
5064 save_widgets_create (impl);
5066 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5067 text = _("Save in _folder:");
5069 text = _("Create in _folder:");
5071 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5073 if (impl->select_multiple)
5075 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5076 "Re-setting to single selection mode.");
5077 set_select_multiple (impl, FALSE, TRUE);
5080 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5081 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5083 gtk_widget_show (impl->location_button);
5084 save_widgets_destroy (impl);
5085 gtk_widget_show (impl->browse_widgets_box);
5086 location_mode_set (impl, impl->location_mode, TRUE);
5089 if (impl->location_entry)
5090 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5092 restore_path_bar (impl);
5093 update_path_bar (impl);
5095 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5096 gtk_widget_hide (impl->browse_new_folder_button);
5098 gtk_widget_show (impl->browse_new_folder_button);
5100 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5101 * of files may change depending whether we are in a file or folder-only mode.
5103 gtk_widget_queue_draw (impl->browse_files_tree_view);
5105 emit_default_size_changed (impl);
5109 gtk_file_chooser_default_set_property (GObject *object,
5111 const GValue *value,
5115 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5119 case GTK_FILE_CHOOSER_PROP_ACTION:
5121 GtkFileChooserAction action = g_value_get_enum (value);
5123 if (action != impl->action)
5125 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5127 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5128 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5129 && impl->select_multiple)
5131 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5132 "this is not allowed in multiple selection mode. Resetting the file chooser "
5133 "to single selection mode.");
5134 set_select_multiple (impl, FALSE, TRUE);
5136 impl->action = action;
5137 update_cell_renderer_attributes (impl);
5138 update_appearance (impl);
5139 settings_load (impl);
5144 case GTK_FILE_CHOOSER_PROP_FILTER:
5145 set_current_filter (impl, g_value_get_object (value));
5148 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5149 set_local_only (impl, g_value_get_boolean (value));
5152 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5153 set_preview_widget (impl, g_value_get_object (value));
5156 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5157 impl->preview_widget_active = g_value_get_boolean (value);
5158 update_preview_widget_visibility (impl);
5161 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5162 impl->use_preview_label = g_value_get_boolean (value);
5163 update_preview_widget_visibility (impl);
5166 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5167 set_extra_widget (impl, g_value_get_object (value));
5170 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5172 gboolean select_multiple = g_value_get_boolean (value);
5173 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5174 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5177 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5178 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5179 "leaving the file chooser in single selection mode.");
5183 set_select_multiple (impl, select_multiple, FALSE);
5187 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5189 gboolean show_hidden = g_value_get_boolean (value);
5190 if (show_hidden != impl->show_hidden)
5192 impl->show_hidden = show_hidden;
5194 if (impl->browse_files_model)
5195 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5200 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5202 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5203 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5207 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5209 gboolean create_folders = g_value_get_boolean (value);
5210 impl->create_folders = create_folders;
5211 update_appearance (impl);
5216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5222 gtk_file_chooser_default_get_property (GObject *object,
5227 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5231 case GTK_FILE_CHOOSER_PROP_ACTION:
5232 g_value_set_enum (value, impl->action);
5235 case GTK_FILE_CHOOSER_PROP_FILTER:
5236 g_value_set_object (value, impl->current_filter);
5239 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5240 g_value_set_boolean (value, impl->local_only);
5243 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5244 g_value_set_object (value, impl->preview_widget);
5247 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5248 g_value_set_boolean (value, impl->preview_widget_active);
5251 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5252 g_value_set_boolean (value, impl->use_preview_label);
5255 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5256 g_value_set_object (value, impl->extra_widget);
5259 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5260 g_value_set_boolean (value, impl->select_multiple);
5263 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5264 g_value_set_boolean (value, impl->show_hidden);
5267 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5268 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5271 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5272 g_value_set_boolean (value, impl->create_folders);
5276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5281 /* This cancels everything that may be going on in the background. */
5283 cancel_all_operations (GtkFileChooserDefault *impl)
5287 pending_select_files_free (impl);
5289 /* cancel all pending operations */
5290 if (impl->pending_cancellables)
5292 for (l = impl->pending_cancellables; l; l = l->next)
5294 GCancellable *cancellable = G_CANCELLABLE (l->data);
5295 g_cancellable_cancel (cancellable);
5297 g_slist_free (impl->pending_cancellables);
5298 impl->pending_cancellables = NULL;
5301 if (impl->reload_icon_cancellables)
5303 for (l = impl->reload_icon_cancellables; l; l = l->next)
5305 GCancellable *cancellable = G_CANCELLABLE (l->data);
5306 g_cancellable_cancel (cancellable);
5308 g_slist_free (impl->reload_icon_cancellables);
5309 impl->reload_icon_cancellables = NULL;
5312 if (impl->loading_shortcuts)
5314 for (l = impl->loading_shortcuts; l; l = l->next)
5316 GCancellable *cancellable = G_CANCELLABLE (l->data);
5317 g_cancellable_cancel (cancellable);
5319 g_slist_free (impl->loading_shortcuts);
5320 impl->loading_shortcuts = NULL;
5323 if (impl->file_list_drag_data_received_cancellable)
5325 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5326 impl->file_list_drag_data_received_cancellable = NULL;
5329 if (impl->update_current_folder_cancellable)
5331 g_cancellable_cancel (impl->update_current_folder_cancellable);
5332 impl->update_current_folder_cancellable = NULL;
5335 if (impl->should_respond_get_info_cancellable)
5337 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5338 impl->should_respond_get_info_cancellable = NULL;
5341 if (impl->update_from_entry_cancellable)
5343 g_cancellable_cancel (impl->update_from_entry_cancellable);
5344 impl->update_from_entry_cancellable = NULL;
5347 if (impl->shortcuts_activate_iter_cancellable)
5349 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5350 impl->shortcuts_activate_iter_cancellable = NULL;
5353 search_stop_searching (impl, TRUE);
5354 recent_stop_loading (impl);
5357 /* Removes the settings signal handler. It's safe to call multiple times */
5359 remove_settings_signal (GtkFileChooserDefault *impl,
5362 if (impl->settings_signal_id)
5364 GtkSettings *settings;
5366 settings = gtk_settings_get_for_screen (screen);
5367 g_signal_handler_disconnect (settings,
5368 impl->settings_signal_id);
5369 impl->settings_signal_id = 0;
5374 gtk_file_chooser_default_dispose (GObject *object)
5376 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5378 cancel_all_operations (impl);
5380 if (impl->extra_widget)
5382 g_object_unref (impl->extra_widget);
5383 impl->extra_widget = NULL;
5386 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5388 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5391 /* We override show-all since we have internal widgets that
5392 * shouldn't be shown when you call show_all(), like the filter
5396 gtk_file_chooser_default_show_all (GtkWidget *widget)
5398 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5400 gtk_widget_show (widget);
5402 if (impl->extra_widget)
5403 gtk_widget_show_all (impl->extra_widget);
5406 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5407 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5410 toplevel_set_focus_cb (GtkWindow *window,
5412 GtkFileChooserDefault *impl)
5414 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5417 /* We monitor the focus widget on our toplevel to be able to know which widget
5418 * was last focused at the time our "should_respond" method gets called.
5421 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5422 GtkWidget *previous_toplevel)
5424 GtkFileChooserDefault *impl;
5425 GtkWidget *toplevel;
5427 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5428 toplevel = gtk_widget_get_toplevel (widget);
5430 if (previous_toplevel &&
5431 impl->toplevel_set_focus_id != 0)
5433 g_signal_handler_disconnect (previous_toplevel,
5434 impl->toplevel_set_focus_id);
5435 impl->toplevel_set_focus_id = 0;
5436 impl->toplevel_last_focus_widget = NULL;
5439 if (gtk_widget_is_toplevel (toplevel))
5441 g_assert (impl->toplevel_set_focus_id == 0);
5442 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5443 G_CALLBACK (toplevel_set_focus_cb), impl);
5444 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5448 /* Changes the icons wherever it is needed */
5450 change_icon_theme (GtkFileChooserDefault *impl)
5452 GtkSettings *settings;
5454 GtkCellRenderer *renderer;
5457 profile_start ("start", NULL);
5459 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5461 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5462 impl->icon_size = MAX (width, height);
5464 impl->icon_size = FALLBACK_ICON_SIZE;
5466 shortcuts_reload_icons (impl);
5467 /* the first cell in the first column is the icon column, and we have a fixed size there */
5468 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5469 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5470 renderer = GTK_CELL_RENDERER (cells->data);
5471 set_icon_cell_renderer_fixed_size (impl, renderer);
5472 g_list_free (cells);
5473 if (impl->browse_files_model)
5474 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5475 gtk_widget_queue_resize (impl->browse_files_tree_view);
5477 profile_end ("end", NULL);
5480 /* Callback used when a GtkSettings value changes */
5482 settings_notify_cb (GObject *object,
5484 GtkFileChooserDefault *impl)
5488 profile_start ("start", NULL);
5490 name = g_param_spec_get_name (pspec);
5492 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5493 strcmp (name, "gtk-icon-sizes") == 0)
5494 change_icon_theme (impl);
5496 profile_end ("end", NULL);
5499 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5503 check_icon_theme (GtkFileChooserDefault *impl)
5505 GtkSettings *settings;
5507 profile_start ("start", NULL);
5509 if (impl->settings_signal_id)
5511 profile_end ("end", NULL);
5515 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5517 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5518 impl->settings_signal_id = g_signal_connect (settings, "notify",
5519 G_CALLBACK (settings_notify_cb), impl);
5521 change_icon_theme (impl);
5524 profile_end ("end", NULL);
5528 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5530 GtkFileChooserDefault *impl;
5532 profile_start ("start", NULL);
5534 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5536 profile_msg (" parent class style_udpated start", NULL);
5537 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5538 profile_msg (" parent class style_updated end", NULL);
5540 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5541 change_icon_theme (impl);
5543 emit_default_size_changed (impl);
5545 profile_end ("end", NULL);
5549 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5550 GdkScreen *previous_screen)
5552 GtkFileChooserDefault *impl;
5554 profile_start ("start", NULL);
5556 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5558 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5559 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5561 remove_settings_signal (impl, previous_screen);
5562 check_icon_theme (impl);
5564 emit_default_size_changed (impl);
5566 profile_end ("end", NULL);
5570 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5571 GtkAllocation *allocation)
5573 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5577 set_sort_column (GtkFileChooserDefault *impl)
5579 GtkTreeSortable *sortable;
5581 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5583 /* can happen when we're still populating the model */
5584 if (sortable == NULL)
5587 gtk_tree_sortable_set_sort_column_id (sortable,
5593 settings_ensure (GtkFileChooserDefault *impl)
5595 if (impl->settings != NULL)
5598 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
5599 "/org/gtk/settings/file-chooser/");
5600 g_settings_delay (impl->settings);
5604 settings_load (GtkFileChooserDefault *impl)
5606 LocationMode location_mode;
5607 gboolean show_hidden;
5608 gboolean show_size_column;
5610 GtkSortType sort_order;
5612 settings_ensure (impl);
5614 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
5615 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
5616 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
5617 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
5618 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
5620 location_mode_set (impl, location_mode, TRUE);
5622 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5624 impl->show_size_column = show_size_column;
5625 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5627 impl->sort_column = sort_column;
5628 impl->sort_order = sort_order;
5629 /* We don't call set_sort_column() here as the models may not have been
5630 * created yet. The individual functions that create and set the models will
5631 * call set_sort_column() themselves.
5636 save_dialog_geometry (GtkFileChooserDefault *impl)
5638 GtkWindow *toplevel;
5639 int x, y, width, height;
5641 toplevel = get_toplevel (GTK_WIDGET (impl));
5643 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5646 gtk_window_get_position (toplevel, &x, &y);
5647 gtk_window_get_size (toplevel, &width, &height);
5649 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
5650 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
5654 settings_save (GtkFileChooserDefault *impl)
5656 char *current_folder_uri;
5658 settings_ensure (impl);
5660 /* Current folder */
5662 if (impl->current_folder)
5663 current_folder_uri = g_file_get_uri (impl->current_folder);
5665 current_folder_uri = "";
5667 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
5669 if (impl->current_folder)
5670 g_free (current_folder_uri);
5672 /* All the other state */
5674 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
5675 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
5676 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5677 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
5678 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
5679 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
5681 save_dialog_geometry (impl);
5683 /* Now apply the settings */
5684 g_settings_apply (impl->settings);
5686 g_object_unref (impl->settings);
5687 impl->settings = NULL;
5690 /* GtkWidget::realize method */
5692 gtk_file_chooser_default_realize (GtkWidget *widget)
5694 GtkFileChooserDefault *impl;
5696 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5698 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
5700 emit_default_size_changed (impl);
5704 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
5706 char *last_folder_uri;
5709 settings_ensure (impl);
5711 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
5713 /* If no last folder is set, we use the user's home directory, since
5714 * this is the starting point for most documents.
5716 if (last_folder_uri[0] == '\0')
5717 file = g_file_new_for_path (g_get_home_dir ());
5719 file = g_file_new_for_uri (last_folder_uri);
5721 g_free (last_folder_uri);
5726 /* GtkWidget::map method */
5728 gtk_file_chooser_default_map (GtkWidget *widget)
5730 GtkFileChooserDefault *impl;
5732 profile_start ("start", NULL);
5734 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5736 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5738 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5740 switch (impl->reload_state)
5743 recent_shortcut_handler (impl);
5746 case RELOAD_HAS_FOLDER:
5747 /* Nothing; we are already loading or loaded, so we
5748 * don't need to reload
5753 g_assert_not_reached ();
5757 volumes_bookmarks_changed_cb (impl->file_system, impl);
5759 settings_load (impl);
5761 profile_end ("end", NULL);
5764 /* GtkWidget::unmap method */
5766 gtk_file_chooser_default_unmap (GtkWidget *widget)
5768 GtkFileChooserDefault *impl;
5770 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5772 settings_save (impl);
5774 cancel_all_operations (impl);
5775 impl->reload_state = RELOAD_EMPTY;
5777 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5781 install_list_model_filter (GtkFileChooserDefault *impl)
5783 _gtk_file_system_model_set_filter (impl->browse_files_model,
5784 impl->current_filter);
5787 #define COMPARE_DIRECTORIES \
5788 GtkFileChooserDefault *impl = user_data; \
5789 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
5790 gboolean dir_a, dir_b; \
5792 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
5793 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
5795 if (dir_a != dir_b) \
5796 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5798 /* Sort callback for the filename column */
5800 name_sort_func (GtkTreeModel *model,
5805 COMPARE_DIRECTORIES;
5808 const char *key_a, *key_b;
5811 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
5812 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
5815 result = strcmp (key_a, key_b);
5827 /* Sort callback for the size column */
5829 size_sort_func (GtkTreeModel *model,
5834 COMPARE_DIRECTORIES;
5837 gint64 size_a, size_b;
5839 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
5840 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
5842 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
5846 /* Sort callback for the mtime column */
5848 mtime_sort_func (GtkTreeModel *model,
5853 COMPARE_DIRECTORIES;
5858 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
5859 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
5861 return ta < tb ? -1 : (ta == tb ? 0 : 1);
5865 /* Callback used when the sort column changes. We cache the sort order for use
5866 * in name_sort_func().
5869 list_sort_column_changed_cb (GtkTreeSortable *sortable,
5870 GtkFileChooserDefault *impl)
5872 gint sort_column_id;
5873 GtkSortType sort_type;
5875 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
5877 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
5878 impl->sort_column = sort_column_id;
5879 impl->sort_order = sort_type;
5884 set_busy_cursor (GtkFileChooserDefault *impl,
5888 GtkWindow *toplevel;
5889 GdkDisplay *display;
5892 toplevel = get_toplevel (GTK_WIDGET (impl));
5893 widget = GTK_WIDGET (toplevel);
5894 if (!toplevel || !gtk_widget_get_realized (widget))
5897 display = gtk_widget_get_display (widget);
5900 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
5904 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
5905 gdk_display_flush (display);
5908 g_object_unref (cursor);
5911 /* Creates a sort model to wrap the file system model and sets it on the tree view */
5913 load_set_model (GtkFileChooserDefault *impl)
5915 profile_start ("start", NULL);
5917 g_assert (impl->browse_files_model != NULL);
5919 profile_msg (" gtk_tree_view_set_model start", NULL);
5920 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
5921 GTK_TREE_MODEL (impl->browse_files_model));
5922 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
5923 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
5925 set_sort_column (impl);
5926 profile_msg (" gtk_tree_view_set_model end", NULL);
5927 impl->list_sort_ascending = TRUE;
5929 profile_end ("end", NULL);
5932 /* Timeout callback used when the loading timer expires */
5934 load_timeout_cb (gpointer data)
5936 GtkFileChooserDefault *impl;
5938 profile_start ("start", NULL);
5940 impl = GTK_FILE_CHOOSER_DEFAULT (data);
5941 g_assert (impl->load_state == LOAD_PRELOAD);
5942 g_assert (impl->load_timeout_id != 0);
5943 g_assert (impl->browse_files_model != NULL);
5945 impl->load_timeout_id = 0;
5946 impl->load_state = LOAD_LOADING;
5948 load_set_model (impl);
5950 profile_end ("end", NULL);
5955 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
5957 load_setup_timer (GtkFileChooserDefault *impl)
5959 g_assert (impl->load_timeout_id == 0);
5960 g_assert (impl->load_state != LOAD_PRELOAD);
5962 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
5963 impl->load_state = LOAD_PRELOAD;
5966 /* Removes the load timeout and switches to the LOAD_FINISHED state */
5968 load_remove_timer (GtkFileChooserDefault *impl)
5970 if (impl->load_timeout_id != 0)
5972 g_assert (impl->load_state == LOAD_PRELOAD);
5974 g_source_remove (impl->load_timeout_id);
5975 impl->load_timeout_id = 0;
5976 impl->load_state = LOAD_EMPTY;
5979 g_assert (impl->load_state == LOAD_EMPTY ||
5980 impl->load_state == LOAD_LOADING ||
5981 impl->load_state == LOAD_FINISHED);
5984 /* Selects the first row in the file list */
5986 browse_files_select_first_row (GtkFileChooserDefault *impl)
5989 GtkTreeIter dummy_iter;
5990 GtkTreeModel *tree_model;
5992 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
5997 path = gtk_tree_path_new_from_indices (0, -1);
5999 /* If the list is empty, do nothing. */
6000 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6001 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6003 gtk_tree_path_free (path);
6006 struct center_selected_row_closure {
6007 GtkFileChooserDefault *impl;
6008 gboolean already_centered;
6011 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6012 * selected row in the tree view.
6015 center_selected_row_foreach_cb (GtkTreeModel *model,
6020 struct center_selected_row_closure *closure;
6023 if (closure->already_centered)
6026 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6027 closure->already_centered = TRUE;
6030 /* Centers the selected row in the tree view */
6032 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6034 struct center_selected_row_closure closure;
6035 GtkTreeSelection *selection;
6037 closure.impl = impl;
6038 closure.already_centered = FALSE;
6040 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6041 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6045 show_and_select_files (GtkFileChooserDefault *impl,
6048 GtkTreeSelection *selection;
6049 GtkFileSystemModel *fsmodel;
6050 gboolean enabled_hidden, removed_filters;
6051 gboolean selected_a_file;
6054 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6055 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6057 enabled_hidden = impl->show_hidden;
6058 removed_filters = (impl->current_filter == NULL);
6060 selected_a_file = FALSE;
6062 for (walk = files; walk; walk = walk->next)
6064 GFile *file = walk->data;
6067 /* Is it a hidden file? */
6069 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6072 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6074 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6076 if (!enabled_hidden &&
6077 (g_file_info_get_is_hidden (info) ||
6078 g_file_info_get_is_backup (info)))
6080 g_object_set (impl, "show-hidden", TRUE, NULL);
6081 enabled_hidden = TRUE;
6085 /* Is it a filtered file? */
6087 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6088 continue; /* re-get the iter as it may change when the model refilters */
6090 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6092 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6093 if (!removed_filters)
6095 set_current_filter (impl, NULL);
6096 removed_filters = TRUE;
6100 /* Okay, can we select the file now? */
6102 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6105 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6109 gtk_tree_selection_select_iter (selection, &iter);
6111 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6112 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6114 gtk_tree_path_free (path);
6116 selected_a_file = TRUE;
6120 browse_files_center_selected_row (impl);
6122 return selected_a_file;
6125 /* Processes the pending operation when a folder is finished loading */
6127 pending_select_files_process (GtkFileChooserDefault *impl)
6129 g_assert (impl->load_state == LOAD_FINISHED);
6130 g_assert (impl->browse_files_model != NULL);
6132 if (impl->pending_select_files)
6134 show_and_select_files (impl, impl->pending_select_files);
6135 pending_select_files_free (impl);
6136 browse_files_center_selected_row (impl);
6140 /* We only select the first row if the chooser is actually mapped ---
6141 * selecting the first row is to help the user when he is interacting with
6142 * the chooser, but sometimes a chooser works not on behalf of the user,
6143 * but rather on behalf of something else like GtkFileChooserButton. In
6144 * that case, the chooser's selection should be what the caller expects,
6145 * as the user can't see that something else got selected. See bug #165264.
6147 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6148 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6149 browse_files_select_first_row (impl);
6152 g_assert (impl->pending_select_files == NULL);
6156 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6161 info = g_file_query_info (impl->current_folder,
6162 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6163 G_FILE_QUERY_INFO_NONE,
6168 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6169 g_object_unref (info);
6172 msg = g_strdup (_("Could not read the contents of the folder"));
6174 error_message (impl, msg, error->message);
6178 /* Callback used when the file system model finishes loading */
6180 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6182 GtkFileChooserDefault *impl)
6184 profile_start ("start", NULL);
6187 show_error_on_reading_current_folder (impl, error);
6189 if (impl->load_state == LOAD_PRELOAD)
6191 load_remove_timer (impl);
6192 load_set_model (impl);
6194 else if (impl->load_state == LOAD_LOADING)
6200 /* We can't g_assert_not_reached(), as something other than us may have
6201 * initiated a folder reload. See #165556.
6203 profile_end ("end", NULL);
6207 g_assert (impl->load_timeout_id == 0);
6209 impl->load_state = LOAD_FINISHED;
6211 pending_select_files_process (impl);
6212 set_busy_cursor (impl, FALSE);
6213 #ifdef PROFILE_FILE_CHOOSER
6214 access ("MARK: *** FINISHED LOADING", F_OK);
6217 profile_end ("end", NULL);
6221 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6222 gboolean remove_from_treeview)
6224 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6226 if (impl->browse_files_model)
6228 g_object_unref (impl->browse_files_model);
6229 impl->browse_files_model = NULL;
6232 if (remove_from_treeview)
6233 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6237 my_g_format_time_for_display (glong secs)
6242 time_t time_mtime, time_now;
6243 const gchar *format;
6244 gchar *locale_format = NULL;
6246 char *date_str = NULL;
6248 const char *locale, *dot = NULL;
6249 gint64 codepage = -1;
6255 #ifdef HAVE_LOCALTIME_R
6256 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6259 struct tm *ptm = localtime ((time_t *) &time_mtime);
6263 g_warning ("ptm != NULL failed");
6265 return g_strdup (_("Unknown"));
6268 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6270 #endif /* HAVE_LOCALTIME_R */
6272 g_date_set_time_t (&mtime, time_mtime);
6273 time_now = time (NULL);
6274 g_date_set_time_t (&now, time_now);
6276 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6278 /* Translators: %H means "hours" and %M means "minutes" */
6280 format = _("%H:%M");
6281 else if (days_diff == 1)
6282 format = _("Yesterday at %H:%M");
6285 if (days_diff > 1 && days_diff < 7)
6286 format = "%A"; /* Days from last week */
6288 format = "%x"; /* Any other date */
6292 /* g_locale_from_utf8() returns a string in the system
6293 * code-page, which is not always the same as that used by the C
6294 * library. For instance when running a GTK+ program with
6295 * LANG=ko on an English version of Windows, the system
6296 * code-page is 1252, but the code-page used by the C library is
6297 * 949. (It's GTK+ itself that sets the C library locale when it
6298 * notices the LANG environment variable. See gtkmain.c The
6299 * Microsoft C library doesn't look at any locale environment
6300 * variables.) We need to pass strftime() a string in the C
6301 * library's code-page. See bug #509885.
6303 locale = setlocale (LC_ALL, NULL);
6305 dot = strchr (locale, '.');
6308 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6310 /* All codepages should fit in 16 bits AFAIK */
6311 if (codepage > 0 && codepage < 65536)
6313 sprintf (charset, "CP%u", (guint) codepage);
6314 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6318 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6320 if (locale_format != NULL &&
6321 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6324 /* As above but in opposite direction... */
6325 if (codepage > 0 && codepage < 65536)
6326 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6328 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6332 if (date_str == NULL)
6333 date_str = g_strdup (_("Unknown"));
6335 g_free (locale_format);
6340 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6342 GFileAttributeType type;
6345 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6346 g_file_info_set_attribute (to, attribute, type, value);
6350 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6352 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6353 GFile *file = G_FILE (object);
6354 GFileInfo *queried, *info;
6357 queried = g_file_query_info_finish (file, res, NULL);
6358 if (queried == NULL)
6361 GDK_THREADS_ENTER ();
6363 /* now we know model is valid */
6365 /* file was deleted */
6366 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6368 GDK_THREADS_LEAVE ();
6372 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6374 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6375 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6376 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6378 _gtk_file_system_model_update_file (model, file, info, FALSE);
6380 g_object_unref (info);
6382 GDK_THREADS_LEAVE ();
6386 file_system_model_set (GtkFileSystemModel *model,
6393 GtkFileChooserDefault *impl = data;
6397 case MODEL_COL_FILE:
6398 g_value_set_object (value, file);
6400 case MODEL_COL_NAME:
6402 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6404 g_value_set_string (value, g_file_info_get_display_name (info));
6406 case MODEL_COL_NAME_COLLATED:
6408 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6410 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6412 case MODEL_COL_IS_FOLDER:
6413 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6415 case MODEL_COL_PIXBUF:
6418 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6420 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6424 GtkTreeModel *tree_model;
6425 GtkTreePath *path, *start, *end;
6428 if (impl->browse_files_tree_view == NULL ||
6429 g_file_info_has_attribute (info, "filechooser::queried"))
6432 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6433 if (tree_model != GTK_TREE_MODEL (model))
6436 if (!_gtk_file_system_model_get_iter_for_file (model,
6439 g_assert_not_reached ();
6440 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6442 path = gtk_tree_model_get_path (tree_model, &iter);
6443 if (gtk_tree_path_compare (start, path) != 1 &&
6444 gtk_tree_path_compare (path, end) != 1)
6446 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6447 g_file_query_info_async (file,
6448 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6449 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6450 G_FILE_ATTRIBUTE_STANDARD_ICON,
6451 G_FILE_QUERY_INFO_NONE,
6453 _gtk_file_system_model_get_cancellable (model),
6454 file_system_model_got_thumbnail,
6457 gtk_tree_path_free (path);
6458 gtk_tree_path_free (start);
6459 gtk_tree_path_free (end);
6464 g_value_set_object (value, NULL);
6466 case MODEL_COL_SIZE:
6467 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6469 case MODEL_COL_SIZE_TEXT:
6470 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6471 g_value_set_string (value, NULL);
6473 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6475 case MODEL_COL_MTIME:
6476 case MODEL_COL_MTIME_TEXT:
6481 g_file_info_get_modification_time (info, &tv);
6482 if (column == MODEL_COL_MTIME)
6483 g_value_set_long (value, tv.tv_sec);
6484 else if (tv.tv_sec == 0)
6485 g_value_set_static_string (value, _("Unknown"));
6487 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6490 case MODEL_COL_ELLIPSIZE:
6491 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6494 g_assert_not_reached ();
6501 /* Gets rid of the old list model and creates a new one for the current folder */
6503 set_list_model (GtkFileChooserDefault *impl,
6506 g_assert (impl->current_folder != NULL);
6508 profile_start ("start", NULL);
6510 stop_loading_and_clear_list_model (impl, TRUE);
6512 set_busy_cursor (impl, TRUE);
6514 impl->browse_files_model =
6515 _gtk_file_system_model_new_for_directory (impl->current_folder,
6517 file_system_model_set,
6519 MODEL_COLUMN_TYPES);
6521 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6523 profile_msg (" set sort function", NULL);
6524 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6525 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6526 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6527 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6528 set_sort_column (impl);
6529 impl->list_sort_ascending = TRUE;
6530 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6531 G_CALLBACK (list_sort_column_changed_cb), impl);
6533 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6535 g_signal_connect (impl->browse_files_model, "finished-loading",
6536 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6538 install_list_model_filter (impl);
6540 profile_end ("end", NULL);
6545 struct update_chooser_entry_selected_foreach_closure {
6547 GtkTreeIter first_selected_iter;
6551 compare_utf8_filenames (const gchar *a,
6554 gchar *a_folded, *b_folded;
6557 a_folded = g_utf8_strdown (a, -1);
6558 b_folded = g_utf8_strdown (b, -1);
6560 retval = strcmp (a_folded, b_folded);
6569 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6574 struct update_chooser_entry_selected_foreach_closure *closure;
6577 closure->num_selected++;
6579 if (closure->num_selected == 1)
6580 closure->first_selected_iter = *iter;
6584 update_chooser_entry (GtkFileChooserDefault *impl)
6586 GtkTreeSelection *selection;
6587 struct update_chooser_entry_selected_foreach_closure closure;
6589 /* no need to update the file chooser's entry if there's no entry */
6590 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6591 !impl->location_entry)
6594 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6595 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6596 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6597 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6598 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6601 g_assert (impl->location_entry != NULL);
6603 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6604 closure.num_selected = 0;
6605 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6607 if (closure.num_selected == 0)
6609 if (impl->operation_mode == OPERATION_MODE_RECENT)
6610 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
6612 goto maybe_clear_entry;
6614 else if (closure.num_selected == 1)
6616 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6619 gboolean change_entry;
6621 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6623 /* If the cursor moved to the row of the newly created folder,
6624 * retrieving info will return NULL.
6629 g_free (impl->browse_files_last_selected_name);
6630 impl->browse_files_last_selected_name =
6631 g_strdup (g_file_info_get_display_name (info));
6633 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6634 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6635 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6637 /* Don't change the name when clicking on a folder... */
6638 change_entry = (! _gtk_file_info_consider_as_directory (info));
6641 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6645 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6647 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6648 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6653 else if (impl->operation_mode == OPERATION_MODE_RECENT
6654 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6658 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
6660 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
6661 MODEL_COL_FILE, &folder,
6663 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
6664 g_object_unref (folder);
6670 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6671 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6673 /* Multiple selection, so just clear the entry. */
6674 g_free (impl->browse_files_last_selected_name);
6675 impl->browse_files_last_selected_name = NULL;
6677 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6683 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6684 && impl->browse_files_last_selected_name)
6686 const char *entry_text;
6688 gboolean clear_entry;
6690 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6691 len = strlen (entry_text);
6694 /* The file chooser entry may have appended a "/" to its text.
6695 * So take it out, and compare the result to the old selection.
6697 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6701 tmp = g_strndup (entry_text, len - 1);
6702 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6706 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6709 clear_entry = FALSE;
6712 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6717 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6721 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6725 struct UpdateCurrentFolderData
6727 GtkFileChooserDefault *impl;
6729 gboolean keep_trail;
6730 gboolean clear_entry;
6731 GFile *original_file;
6732 GError *original_error;
6736 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6737 GtkFileSystemVolume *volume,
6738 const GError *error,
6741 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6742 struct UpdateCurrentFolderData *data = user_data;
6743 GtkFileChooserDefault *impl = data->impl;
6745 if (cancellable != impl->update_current_folder_cancellable)
6748 impl->update_current_folder_cancellable = NULL;
6749 set_busy_cursor (impl, FALSE);
6756 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6757 impl->reload_state = RELOAD_EMPTY;
6761 change_folder_and_display_error (impl, data->file, data->clear_entry);
6764 g_object_unref (data->file);
6767 g_object_unref (cancellable);
6771 update_current_folder_get_info_cb (GCancellable *cancellable,
6773 const GError *error,
6776 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6777 struct UpdateCurrentFolderData *data = user_data;
6778 GtkFileChooserDefault *impl = data->impl;
6780 if (cancellable != impl->update_current_folder_cancellable)
6783 impl->update_current_folder_cancellable = NULL;
6784 impl->reload_state = RELOAD_EMPTY;
6786 set_busy_cursor (impl, FALSE);
6795 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
6797 GMountOperation *mount_operation;
6798 GtkWidget *toplevel;
6800 g_object_unref (cancellable);
6801 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
6803 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
6805 set_busy_cursor (impl, TRUE);
6807 impl->update_current_folder_cancellable =
6808 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
6810 update_current_folder_mount_enclosing_volume_cb,
6816 if (!data->original_file)
6818 data->original_file = g_object_ref (data->file);
6819 data->original_error = g_error_copy (error);
6822 parent_file = g_file_get_parent (data->file);
6824 /* get parent path and try to change the folder to that */
6827 g_object_unref (data->file);
6828 data->file = parent_file;
6830 g_object_unref (cancellable);
6832 /* restart the update current folder operation */
6833 impl->reload_state = RELOAD_HAS_FOLDER;
6835 impl->update_current_folder_cancellable =
6836 _gtk_file_system_get_info (impl->file_system, data->file,
6838 update_current_folder_get_info_cb,
6841 set_busy_cursor (impl, TRUE);
6847 /* Error and bail out, ignoring "not found" errors since they're useless:
6848 * they only happen when a program defaults to a folder that has been (re)moved.
6850 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
6851 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6853 g_error_free (data->original_error);
6855 g_object_unref (data->original_file);
6861 if (data->original_file)
6863 /* Error and bail out, ignoring "not found" errors since they're useless:
6864 * they only happen when a program defaults to a folder that has been (re)moved.
6866 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
6867 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6869 g_error_free (data->original_error);
6871 g_object_unref (data->original_file);
6874 if (! _gtk_file_info_consider_as_directory (info))
6877 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
6880 if (impl->current_folder != data->file)
6882 if (impl->current_folder)
6883 g_object_unref (impl->current_folder);
6885 impl->current_folder = g_object_ref (data->file);
6888 impl->reload_state = RELOAD_HAS_FOLDER;
6890 /* Update the widgets that may trigger a folder change themselves. */
6892 if (!impl->changing_folder)
6894 impl->changing_folder = TRUE;
6896 shortcuts_update_current_folder (impl);
6898 impl->changing_folder = FALSE;
6901 /* Set the folder on the save entry */
6903 if (impl->location_entry)
6905 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6906 impl->current_folder);
6908 if (data->clear_entry)
6909 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6912 /* Create a new list model. This is slightly evil; we store the result value
6913 * but perform more actions rather than returning immediately even if it
6914 * generates an error.
6916 set_list_model (impl, NULL);
6918 /* Refresh controls */
6920 shortcuts_find_current_folder (impl);
6922 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6924 check_preview_change (impl);
6925 bookmarks_check_add_sensitivity (impl);
6927 g_signal_emit_by_name (impl, "selection-changed", 0);
6930 g_object_unref (data->file);
6933 g_object_unref (cancellable);
6937 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6939 gboolean keep_trail,
6940 gboolean clear_entry,
6943 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6944 struct UpdateCurrentFolderData *data;
6946 profile_start ("start", NULL);
6948 g_object_ref (file);
6950 switch (impl->operation_mode)
6952 case OPERATION_MODE_SEARCH:
6953 search_switch_to_browse_mode (impl);
6955 case OPERATION_MODE_RECENT:
6956 recent_switch_to_browse_mode (impl);
6958 case OPERATION_MODE_BROWSE:
6962 if (impl->local_only && !g_file_is_native (file))
6964 g_set_error_literal (error,
6965 GTK_FILE_CHOOSER_ERROR,
6966 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6967 _("Cannot change to folder because it is not local"));
6969 g_object_unref (file);
6970 profile_end ("end - not local", NULL);
6974 if (impl->update_current_folder_cancellable)
6975 g_cancellable_cancel (impl->update_current_folder_cancellable);
6977 /* Test validity of path here. */
6978 data = g_new0 (struct UpdateCurrentFolderData, 1);
6980 data->file = g_object_ref (file);
6981 data->keep_trail = keep_trail;
6982 data->clear_entry = clear_entry;
6984 impl->reload_state = RELOAD_HAS_FOLDER;
6986 impl->update_current_folder_cancellable =
6987 _gtk_file_system_get_info (impl->file_system, file,
6989 update_current_folder_get_info_cb,
6992 set_busy_cursor (impl, TRUE);
6993 g_object_unref (file);
6995 profile_end ("end", NULL);
7000 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7002 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7004 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7005 impl->operation_mode == OPERATION_MODE_RECENT)
7008 if (impl->reload_state == RELOAD_EMPTY)
7010 /* We are unmapped, or we had an error while loading the last folder.
7011 * We'll return the folder used by the last invocation of the file chooser
7012 * since once we get (re)mapped, we'll load *that* folder anyway unless
7013 * the caller explicitly calls set_current_folder() on us.
7015 return get_file_for_last_folder_opened (impl);
7018 if (impl->current_folder)
7019 return g_object_ref (impl->current_folder);
7025 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7028 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7030 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7031 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7033 pending_select_files_free (impl);
7034 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7038 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7042 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7046 parent_file = g_file_get_parent (file);
7049 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7051 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7052 impl->operation_mode == OPERATION_MODE_RECENT ||
7053 impl->load_state == LOAD_EMPTY)
7059 g_assert (impl->current_folder != NULL);
7061 same_path = g_file_equal (parent_file, impl->current_folder);
7064 if (same_path && impl->load_state == LOAD_FINISHED)
7069 files.data = (gpointer) file;
7072 result = show_and_select_files (impl, &files);
7073 g_object_unref (parent_file);
7077 pending_select_files_add (impl, file);
7083 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7084 g_object_unref (parent_file);
7088 g_object_unref (parent_file);
7093 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7096 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7097 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7100 if (!impl->browse_files_model)
7103 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7108 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7113 maybe_select (GtkTreeModel *model,
7118 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7119 GtkTreeSelection *selection;
7122 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7124 gtk_tree_model_get (model, iter,
7125 MODEL_COL_IS_FOLDER, &is_folder,
7128 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7129 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7130 gtk_tree_selection_select_iter (selection, iter);
7132 gtk_tree_selection_unselect_iter (selection, iter);
7138 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7140 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7142 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7143 impl->operation_mode == OPERATION_MODE_RECENT)
7145 GtkTreeSelection *selection;
7147 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7148 gtk_tree_selection_select_all (selection);
7152 if (impl->select_multiple)
7153 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7154 maybe_select, impl);
7158 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7160 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7161 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7163 gtk_tree_selection_unselect_all (selection);
7164 pending_select_files_free (impl);
7167 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7169 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7171 * is_empty_ret - whether the file entry is totally empty
7173 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7174 * the path will be "$cwd/foobar")
7177 check_save_entry (GtkFileChooserDefault *impl,
7179 gboolean *is_well_formed_ret,
7180 gboolean *is_empty_ret,
7181 gboolean *is_file_part_empty_ret,
7182 gboolean *is_folder)
7184 GtkFileChooserEntry *chooser_entry;
7185 GFile *current_folder;
7186 const char *file_part;
7190 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7191 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7192 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7193 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7194 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7196 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7198 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7201 *is_well_formed_ret = TRUE;
7202 *is_empty_ret = TRUE;
7203 *is_file_part_empty_ret = TRUE;
7209 *is_empty_ret = FALSE;
7211 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7212 if (!current_folder)
7215 *is_well_formed_ret = FALSE;
7216 *is_file_part_empty_ret = FALSE;
7222 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7224 if (!file_part || file_part[0] == '\0')
7226 *file_ret = g_object_ref (current_folder);
7227 *is_well_formed_ret = TRUE;
7228 *is_file_part_empty_ret = TRUE;
7234 *is_file_part_empty_ret = FALSE;
7237 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7241 error_building_filename_dialog (impl, error);
7243 *is_well_formed_ret = FALSE;
7250 *is_well_formed_ret = TRUE;
7251 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7254 struct get_files_closure {
7255 GtkFileChooserDefault *impl;
7257 GFile *file_from_entry;
7261 get_files_foreach (GtkTreeModel *model,
7266 struct get_files_closure *info;
7268 GtkFileSystemModel *fs_model;
7271 fs_model = info->impl->browse_files_model;
7273 file = _gtk_file_system_model_get_file (fs_model, iter);
7275 return; /* We are on the editable row */
7277 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7278 info->result = g_slist_prepend (info->result, g_object_ref (file));
7282 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7284 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7285 struct get_files_closure info;
7286 GtkWindow *toplevel;
7287 GtkWidget *current_focus;
7288 gboolean file_list_seen;
7290 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7291 return search_get_selected_files (impl);
7293 if (impl->operation_mode == OPERATION_MODE_RECENT)
7294 return recent_get_selected_files (impl);
7298 info.file_from_entry = NULL;
7300 toplevel = get_toplevel (GTK_WIDGET (impl));
7302 current_focus = gtk_window_get_focus (toplevel);
7304 current_focus = NULL;
7306 file_list_seen = FALSE;
7307 if (current_focus == impl->browse_files_tree_view)
7309 GtkTreeSelection *selection;
7313 file_list_seen = TRUE;
7314 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7315 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7317 /* If there is no selection in the file list, we probably have this situation:
7319 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7320 * 2. He then double-clicked on a folder ("bar") in the file list
7322 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7323 * filename entry to see if that is the case.
7325 if (info.result == NULL && impl->location_entry)
7328 else if (impl->location_entry && current_focus == impl->location_entry)
7330 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7334 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7339 if (!is_well_formed)
7342 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7344 g_object_unref (info.file_from_entry);
7348 if (info.file_from_entry)
7349 info.result = g_slist_prepend (info.result, info.file_from_entry);
7350 else if (!file_list_seen)
7355 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7357 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7361 /* The focus is on a dialog's action area button or something else */
7362 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7363 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7371 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7372 * fall back to the current directory */
7373 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7374 info.result == NULL)
7376 GFile *current_folder;
7378 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7381 info.result = g_slist_prepend (info.result, current_folder);
7384 return g_slist_reverse (info.result);
7388 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7390 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7392 if (impl->preview_file)
7393 return g_object_ref (impl->preview_file);
7398 static GtkFileSystem *
7399 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7401 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7403 return impl->file_system;
7406 /* Shows or hides the filter widgets */
7408 show_filters (GtkFileChooserDefault *impl,
7412 gtk_widget_show (impl->filter_combo_hbox);
7414 gtk_widget_hide (impl->filter_combo_hbox);
7418 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7419 GtkFileFilter *filter)
7421 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7424 if (g_slist_find (impl->filters, filter))
7426 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7430 g_object_ref_sink (filter);
7431 impl->filters = g_slist_append (impl->filters, filter);
7433 name = gtk_file_filter_get_name (filter);
7435 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7437 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7439 if (!g_slist_find (impl->filters, impl->current_filter))
7440 set_current_filter (impl, filter);
7442 show_filters (impl, TRUE);
7446 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7447 GtkFileFilter *filter)
7449 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7450 GtkTreeModel *model;
7454 filter_index = g_slist_index (impl->filters, filter);
7456 if (filter_index < 0)
7458 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7462 impl->filters = g_slist_remove (impl->filters, filter);
7464 if (filter == impl->current_filter)
7467 set_current_filter (impl, impl->filters->data);
7469 set_current_filter (impl, NULL);
7472 /* Remove row from the combo box */
7473 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7474 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7475 g_assert_not_reached ();
7477 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7479 g_object_unref (filter);
7482 show_filters (impl, FALSE);
7486 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7488 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7490 return g_slist_copy (impl->filters);
7493 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7495 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7498 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7501 struct AddShortcutData
7503 GtkFileChooserDefault *impl;
7508 add_shortcut_get_info_cb (GCancellable *cancellable,
7510 const GError *error,
7514 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7515 struct AddShortcutData *data = user_data;
7517 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7520 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7522 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7525 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7527 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7530 g_object_unref (data->impl);
7531 g_object_unref (data->file);
7534 g_object_unref (cancellable);
7538 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7542 GCancellable *cancellable;
7543 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7544 struct AddShortcutData *data;
7548 /* Avoid adding duplicates */
7549 pos = shortcut_find_position (impl, file);
7550 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7554 uri = g_file_get_uri (file);
7555 /* translators, "Shortcut" means "Bookmark" here */
7557 GTK_FILE_CHOOSER_ERROR,
7558 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7559 _("Shortcut %s already exists"),
7566 for (l = impl->loading_shortcuts; l; l = l->next)
7568 GCancellable *c = l->data;
7571 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7572 if (f && g_file_equal (file, f))
7576 uri = g_file_get_uri (file);
7578 GTK_FILE_CHOOSER_ERROR,
7579 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7580 _("Shortcut %s already exists"),
7588 data = g_new0 (struct AddShortcutData, 1);
7589 data->impl = g_object_ref (impl);
7590 data->file = g_object_ref (file);
7592 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7594 add_shortcut_get_info_cb, data);
7599 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7600 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7606 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7610 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7617 for (l = impl->loading_shortcuts; l; l = l->next)
7619 GCancellable *c = l->data;
7622 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7623 if (f && g_file_equal (file, f))
7625 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7626 g_cancellable_cancel (c);
7631 if (impl->num_shortcuts == 0)
7634 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7635 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7636 g_assert_not_reached ();
7638 for (i = 0; i < impl->num_shortcuts; i++)
7641 ShortcutType shortcut_type;
7644 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7645 SHORTCUTS_COL_DATA, &col_data,
7646 SHORTCUTS_COL_TYPE, &shortcut_type,
7648 g_assert (col_data != NULL);
7649 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7651 shortcut = col_data;
7652 if (g_file_equal (shortcut, file))
7654 shortcuts_remove_rows (impl, pos + i, 1);
7655 impl->num_shortcuts--;
7659 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7660 g_assert_not_reached ();
7665 uri = g_file_get_uri (file);
7666 /* translators, "Shortcut" means "Bookmark" here */
7668 GTK_FILE_CHOOSER_ERROR,
7669 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7670 _("Shortcut %s does not exist"),
7678 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7680 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7686 if (impl->num_shortcuts == 0)
7689 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7690 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7691 g_assert_not_reached ();
7695 for (i = 0; i < impl->num_shortcuts; i++)
7698 ShortcutType shortcut_type;
7701 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7702 SHORTCUTS_COL_DATA, &col_data,
7703 SHORTCUTS_COL_TYPE, &shortcut_type,
7705 g_assert (col_data != NULL);
7706 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7708 shortcut = col_data;
7709 list = g_slist_prepend (list, g_object_ref (shortcut));
7711 if (i != impl->num_shortcuts - 1)
7713 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7714 g_assert_not_reached ();
7718 return g_slist_reverse (list);
7721 /* Guesses a size based upon font sizes */
7723 find_good_size_from_style (GtkWidget *widget,
7727 GtkStyleContext *context;
7728 GtkStateFlags state;
7733 context = gtk_widget_get_style_context (widget);
7734 state = gtk_widget_get_state_flags (widget);
7736 screen = gtk_widget_get_screen (widget);
7739 resolution = gdk_screen_get_resolution (screen);
7740 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7744 resolution = 96.0; /* wheeee */
7746 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
7747 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7749 *width = font_size * NUM_CHARS;
7750 *height = font_size * NUM_LINES;
7754 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7755 gint *default_width,
7756 gint *default_height)
7758 GtkFileChooserDefault *impl;
7760 int x, y, width, height;
7762 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7764 settings_ensure (impl);
7766 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
7767 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
7769 if (x >= 0 && y >= 0 && width > 0 && height > 0)
7771 *default_width = width;
7772 *default_height = height;
7776 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7778 if (impl->preview_widget_active &&
7779 impl->preview_widget &&
7780 gtk_widget_get_visible (impl->preview_widget))
7782 gtk_widget_get_preferred_size (impl->preview_box,
7784 *default_width += PREVIEW_HBOX_SPACING + req.width;
7787 if (impl->extra_widget &&
7788 gtk_widget_get_visible (impl->extra_widget))
7790 gtk_widget_get_preferred_size (impl->extra_align,
7792 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
7796 struct switch_folder_closure {
7797 GtkFileChooserDefault *impl;
7802 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7804 switch_folder_foreach_cb (GtkTreeModel *model,
7809 struct switch_folder_closure *closure;
7813 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
7814 closure->num_selected++;
7817 /* Changes to the selected folder in the list view */
7819 switch_to_selected_folder (GtkFileChooserDefault *impl)
7821 GtkTreeSelection *selection;
7822 struct switch_folder_closure closure;
7824 /* We do this with foreach() rather than get_selected() as we may be in
7825 * multiple selection mode
7828 closure.impl = impl;
7829 closure.file = NULL;
7830 closure.num_selected = 0;
7832 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7833 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7835 g_assert (closure.file && closure.num_selected == 1);
7837 change_folder_and_display_error (impl, closure.file, FALSE);
7840 /* Gets the GFileInfo for the selected row in the file list; assumes single
7844 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7845 gboolean *had_selection)
7847 GtkTreeSelection *selection;
7851 g_assert (!impl->select_multiple);
7852 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7853 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7855 *had_selection = FALSE;
7859 *had_selection = TRUE;
7861 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
7865 /* Gets the display name of the selected file in the file list; assumes single
7866 * selection mode and that something is selected.
7868 static const gchar *
7869 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7872 gboolean had_selection;
7874 info = get_selected_file_info_from_file_list (impl, &had_selection);
7875 g_assert (had_selection);
7876 g_assert (info != NULL);
7878 return g_file_info_get_display_name (info);
7882 add_custom_button_to_dialog (GtkDialog *dialog,
7883 const gchar *mnemonic_label,
7884 const gchar *stock_id,
7889 button = gtk_button_new_with_mnemonic (mnemonic_label);
7890 gtk_widget_set_can_default (button, TRUE);
7891 gtk_button_set_image (GTK_BUTTON (button),
7892 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7893 gtk_widget_show (button);
7895 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7898 /* Presents an overwrite confirmation dialog; returns whether we should accept
7902 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7903 const gchar *file_part,
7904 const gchar *folder_display_name)
7906 GtkWindow *toplevel;
7910 toplevel = get_toplevel (GTK_WIDGET (impl));
7912 dialog = gtk_message_dialog_new (toplevel,
7913 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7914 GTK_MESSAGE_QUESTION,
7916 _("A file named \"%s\" already exists. Do you want to replace it?"),
7918 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7919 _("The file already exists in \"%s\". Replacing it will "
7920 "overwrite its contents."),
7921 folder_display_name);
7923 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7924 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
7925 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7926 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
7927 GTK_RESPONSE_ACCEPT,
7928 GTK_RESPONSE_CANCEL,
7930 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7932 if (gtk_window_has_group (toplevel))
7933 gtk_window_group_add_window (gtk_window_get_group (toplevel),
7934 GTK_WINDOW (dialog));
7936 response = gtk_dialog_run (GTK_DIALOG (dialog));
7938 gtk_widget_destroy (dialog);
7940 return (response == GTK_RESPONSE_ACCEPT);
7943 struct GetDisplayNameData
7945 GtkFileChooserDefault *impl;
7950 confirmation_confirm_get_info_cb (GCancellable *cancellable,
7952 const GError *error,
7955 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7956 gboolean should_respond = FALSE;
7957 struct GetDisplayNameData *data = user_data;
7959 if (cancellable != data->impl->should_respond_get_info_cancellable)
7962 data->impl->should_respond_get_info_cancellable = NULL;
7968 /* Huh? Did the folder disappear? Let the caller deal with it */
7969 should_respond = TRUE;
7971 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
7973 set_busy_cursor (data->impl, FALSE);
7975 g_signal_emit_by_name (data->impl, "response-requested");
7978 g_object_unref (data->impl);
7979 g_free (data->file_part);
7982 g_object_unref (cancellable);
7985 /* Does overwrite confirmation if appropriate, and returns whether the dialog
7986 * should respond. Can get the file part from the file list or the save entry.
7989 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
7990 const gchar *file_part,
7993 GtkFileChooserConfirmation conf;
7995 if (!impl->do_overwrite_confirmation)
7998 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8000 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8004 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8006 struct GetDisplayNameData *data;
8008 g_assert (file_part != NULL);
8010 data = g_new0 (struct GetDisplayNameData, 1);
8011 data->impl = g_object_ref (impl);
8012 data->file_part = g_strdup (file_part);
8014 if (impl->should_respond_get_info_cancellable)
8015 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8017 impl->should_respond_get_info_cancellable =
8018 _gtk_file_system_get_info (impl->file_system, parent_file,
8019 "standard::display-name",
8020 confirmation_confirm_get_info_cb,
8022 set_busy_cursor (data->impl, TRUE);
8026 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8029 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8033 g_assert_not_reached ();
8038 struct FileExistsData
8040 GtkFileChooserDefault *impl;
8041 gboolean file_exists_and_is_not_folder;
8047 name_entry_get_parent_info_cb (GCancellable *cancellable,
8049 const GError *error,
8052 gboolean parent_is_folder;
8053 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8054 struct FileExistsData *data = user_data;
8056 if (cancellable != data->impl->should_respond_get_info_cancellable)
8059 data->impl->should_respond_get_info_cancellable = NULL;
8061 set_busy_cursor (data->impl, FALSE);
8067 parent_is_folder = FALSE;
8069 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8071 if (parent_is_folder)
8073 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8075 g_signal_emit_by_name (data->impl, "response-requested"); /* even if the file doesn't exist, apps can make good use of that (e.g. Emacs) */
8077 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8079 if (data->file_exists_and_is_not_folder)
8084 /* Dup the string because the string may be modified
8085 * depending on what clients do in the confirm-overwrite
8086 * signal and this corrupts the pointer
8088 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8089 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8093 g_signal_emit_by_name (data->impl, "response-requested");
8096 g_signal_emit_by_name (data->impl, "response-requested");
8098 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8099 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8101 GError *mkdir_error = NULL;
8103 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8104 * "/blah/nonexistent" you *will* want a folder created.
8107 set_busy_cursor (data->impl, TRUE);
8108 g_file_make_directory (data->file, NULL, &mkdir_error);
8109 set_busy_cursor (data->impl, FALSE);
8112 g_signal_emit_by_name (data->impl, "response-requested");
8114 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8117 g_assert_not_reached ();
8123 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8124 error_with_file_under_nonfolder (data->impl, data->parent_file);
8130 /* The parent folder is not readable for some reason */
8132 error_copy = g_error_copy (error);
8133 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8138 g_object_unref (data->impl);
8139 g_object_unref (data->file);
8140 g_object_unref (data->parent_file);
8143 g_object_unref (cancellable);
8147 file_exists_get_info_cb (GCancellable *cancellable,
8149 const GError *error,
8152 gboolean data_ownership_taken = FALSE;
8153 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8154 gboolean file_exists;
8156 gboolean needs_parent_check = FALSE;
8157 struct FileExistsData *data = user_data;
8159 if (cancellable != data->impl->file_exists_get_info_cancellable)
8162 data->impl->file_exists_get_info_cancellable = NULL;
8164 set_busy_cursor (data->impl, FALSE);
8169 file_exists = (info != NULL);
8170 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8172 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8175 change_folder_and_display_error (data->impl, data->file, TRUE);
8179 g_signal_emit_by_name (data->impl, "response-requested"); /* user typed an existing filename; we are done */
8181 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8184 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8186 if (file_exists && !is_folder)
8188 /* Oops, the user typed the name of an existing path which is not
8191 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8192 g_error_copy (error));
8196 needs_parent_check = TRUE;
8199 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8203 needs_parent_check = TRUE;
8209 /* User typed a folder; we are done */
8210 g_signal_emit_by_name (data->impl, "response-requested");
8213 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8216 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8219 change_folder_and_display_error (data->impl, data->file, TRUE);
8221 needs_parent_check = TRUE;
8225 g_assert_not_reached();
8228 if (needs_parent_check) {
8229 /* check that everything up to the last path component exists (i.e. the parent) */
8231 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8232 data_ownership_taken = TRUE;
8234 if (data->impl->should_respond_get_info_cancellable)
8235 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8237 data->impl->should_respond_get_info_cancellable =
8238 _gtk_file_system_get_info (data->impl->file_system,
8241 name_entry_get_parent_info_cb,
8243 set_busy_cursor (data->impl, TRUE);
8247 if (!data_ownership_taken)
8249 g_object_unref (data->impl);
8250 g_object_unref (data->file);
8251 g_object_unref (data->parent_file);
8255 g_object_unref (cancellable);
8259 paste_text_received (GtkClipboard *clipboard,
8261 GtkFileChooserDefault *impl)
8268 file = g_file_new_for_uri (text);
8270 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8271 location_popup_handler (impl, text);
8273 g_object_unref (file);
8276 /* Handler for the "location-popup-on-paste" keybinding signal */
8278 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8280 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8281 GDK_SELECTION_CLIPBOARD);
8282 gtk_clipboard_request_text (clipboard,
8283 (GtkClipboardTextReceivedFunc) paste_text_received,
8288 /* Implementation for GtkFileChooserEmbed::should_respond() */
8290 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8292 GtkFileChooserDefault *impl;
8293 GtkWidget *toplevel;
8294 GtkWidget *current_focus;
8296 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8298 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8299 g_assert (GTK_IS_WINDOW (toplevel));
8301 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8303 if (current_focus == impl->browse_files_tree_view)
8305 /* The following array encodes what we do based on the impl->action and the
8306 * number of files selected.
8309 NOOP, /* Do nothing (don't respond) */
8310 RESPOND, /* Respond immediately */
8311 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8312 ALL_FILES, /* Respond only if everything selected is a file */
8313 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8314 SAVE_ENTRY, /* Go to the code for handling the save entry */
8315 NOT_REACHED /* Sanity check */
8317 static const ActionToTake what_to_do[4][3] = {
8318 /* 0 selected 1 selected many selected */
8319 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8320 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8321 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8322 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8326 gboolean all_files, all_folders;
8328 ActionToTake action;
8332 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8334 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8335 return search_should_respond (impl);
8337 if (impl->operation_mode == OPERATION_MODE_RECENT)
8338 return recent_should_respond (impl);
8340 selection_check (impl, &num_selected, &all_files, &all_folders);
8342 if (num_selected > 2)
8347 action = what_to_do [impl->action] [k];
8357 case RESPOND_OR_SWITCH:
8358 g_assert (num_selected == 1);
8362 switch_to_selected_folder (impl);
8365 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8366 return should_respond_after_confirm_overwrite (impl,
8367 get_display_name_from_file_list (impl),
8368 impl->current_folder);
8382 g_assert_not_reached ();
8385 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8388 gboolean is_well_formed, is_empty, is_file_part_empty;
8391 GtkFileChooserEntry *entry;
8396 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8397 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8398 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8399 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8400 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8402 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8403 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8405 if (!is_well_formed)
8410 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8411 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8417 g_assert (file != NULL);
8422 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8423 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8425 change_folder_and_display_error (impl, file, TRUE);
8428 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8429 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8431 /* The folder already exists, so we do not need to create it.
8432 * Just respond to terminate the dialog.
8438 g_assert_not_reached ();
8444 struct FileExistsData *data;
8446 /* We need to check whether file exists and whether it is a folder -
8447 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8448 * negative (it doesn't know yet if your last path component is a
8452 data = g_new0 (struct FileExistsData, 1);
8453 data->impl = g_object_ref (impl);
8454 data->file = g_object_ref (file);
8455 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8457 if (impl->file_exists_get_info_cancellable)
8458 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8460 impl->file_exists_get_info_cancellable =
8461 _gtk_file_system_get_info (impl->file_system, file,
8463 file_exists_get_info_cb,
8466 set_busy_cursor (impl, TRUE);
8470 g_error_free (error);
8473 g_object_unref (file);
8476 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8478 /* The focus is on a dialog's action area button, *and* the widget that
8479 * was focused immediately before it is the file list.
8483 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8485 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8488 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8490 /* The focus is on a dialog's action area button, *and* the widget that
8491 * was focused immediately before it is the location entry.
8496 /* The focus is on a dialog's action area button or something else */
8497 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8498 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8503 g_assert_not_reached ();
8507 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8509 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8511 GtkFileChooserDefault *impl;
8514 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8516 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8517 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8519 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8520 widget = impl->browse_files_tree_view;
8522 widget = impl->location_entry;
8524 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8525 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8526 widget = impl->location_entry;
8529 g_assert_not_reached ();
8533 g_assert (widget != NULL);
8534 gtk_widget_grab_focus (widget);
8537 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8539 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8549 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8550 *list = g_slist_prepend (*list, file); /* The file already has a new ref courtesy of gtk_tree_model_get(); this will be unreffed by the caller */
8553 /* Constructs a list of the selected paths in search mode */
8555 search_get_selected_files (GtkFileChooserDefault *impl)
8558 GtkTreeSelection *selection;
8562 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8563 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8564 result = g_slist_reverse (result);
8569 /* Called from ::should_respond(). We return whether there are selected files
8570 * in the search list.
8573 search_should_respond (GtkFileChooserDefault *impl)
8575 GtkTreeSelection *selection;
8577 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8579 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8580 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8583 /* Adds one hit from the search engine to the search_model */
8585 search_add_hit (GtkFileChooserDefault *impl,
8590 file = g_file_new_for_uri (uri);
8594 if (!g_file_is_native (file))
8596 g_object_unref (file);
8600 _gtk_file_system_model_add_and_query_file (impl->search_model,
8604 g_object_unref (file);
8607 /* Callback used from GtkSearchEngine when we get new hits */
8609 search_engine_hits_added_cb (GtkSearchEngine *engine,
8613 GtkFileChooserDefault *impl;
8616 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8618 for (l = hits; l; l = l->next)
8619 search_add_hit (impl, (gchar*)l->data);
8622 /* Callback used from GtkSearchEngine when the query is done running */
8624 search_engine_finished_cb (GtkSearchEngine *engine,
8627 GtkFileChooserDefault *impl;
8629 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8632 /* EB: setting the model here will avoid loads of row events,
8633 * but it'll make the search look like blocked.
8635 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8636 GTK_TREE_MODEL (impl->search_model));
8639 /* FMQ: if search was empty, say that we got no hits */
8640 set_busy_cursor (impl, FALSE);
8643 /* Displays a generic error when we cannot create a GtkSearchEngine.
8644 * It would be better if _gtk_search_engine_new() gave us a GError
8645 * with a better message, but it doesn't do that right now.
8648 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8650 error_message (impl,
8651 _("Could not start the search process"),
8652 _("The program was not able to create a connection to the indexer "
8653 "daemon. Please make sure it is running."));
8657 search_engine_error_cb (GtkSearchEngine *engine,
8658 const gchar *message,
8661 GtkFileChooserDefault *impl;
8663 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8665 search_stop_searching (impl, TRUE);
8666 error_message (impl, _("Could not send the search request"), message);
8668 set_busy_cursor (impl, FALSE);
8671 /* Frees the data in the search_model */
8673 search_clear_model (GtkFileChooserDefault *impl,
8674 gboolean remove_from_treeview)
8676 if (!impl->search_model)
8679 g_object_unref (impl->search_model);
8680 impl->search_model = NULL;
8682 if (remove_from_treeview)
8683 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8686 /* Stops any ongoing searches; does not touch the search_model */
8688 search_stop_searching (GtkFileChooserDefault *impl,
8689 gboolean remove_query)
8691 if (remove_query && impl->search_query)
8693 g_object_unref (impl->search_query);
8694 impl->search_query = NULL;
8697 if (impl->search_engine)
8699 _gtk_search_engine_stop (impl->search_engine);
8701 g_object_unref (impl->search_engine);
8702 impl->search_engine = NULL;
8706 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8708 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8710 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8712 search_stop_searching (impl, FALSE);
8713 search_clear_model (impl, TRUE);
8715 gtk_widget_destroy (impl->search_hbox);
8716 impl->search_hbox = NULL;
8717 impl->search_entry = NULL;
8719 gtk_widget_show (impl->browse_path_bar);
8720 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
8721 gtk_widget_hide (impl->browse_new_folder_button);
8723 gtk_widget_show (impl->browse_new_folder_button);
8726 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8727 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8729 gtk_widget_show (impl->location_button);
8731 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8732 gtk_widget_show (impl->location_entry_box);
8735 impl->operation_mode = OPERATION_MODE_BROWSE;
8737 file_list_set_sort_column_ids (impl);
8740 /* Creates the search_model and puts it in the tree view */
8742 search_setup_model (GtkFileChooserDefault *impl)
8744 g_assert (impl->search_model == NULL);
8746 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
8748 MODEL_COLUMN_TYPES);
8750 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8754 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8758 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8762 set_sort_column (impl);
8764 /* EB: setting the model here will make the hits list update feel
8765 * more "alive" than setting the model at the end of the search
8768 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8769 GTK_TREE_MODEL (impl->search_model));
8772 /* Creates a new query with the specified text and launches it */
8774 search_start_query (GtkFileChooserDefault *impl,
8775 const gchar *query_text)
8777 search_stop_searching (impl, FALSE);
8778 search_clear_model (impl, TRUE);
8779 search_setup_model (impl);
8780 set_busy_cursor (impl, TRUE);
8782 if (impl->search_engine == NULL)
8783 impl->search_engine = _gtk_search_engine_new ();
8785 if (!impl->search_engine)
8787 set_busy_cursor (impl, FALSE);
8788 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
8792 if (!impl->search_query)
8794 impl->search_query = _gtk_query_new ();
8795 _gtk_query_set_text (impl->search_query, query_text);
8798 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
8800 g_signal_connect (impl->search_engine, "hits-added",
8801 G_CALLBACK (search_engine_hits_added_cb), impl);
8802 g_signal_connect (impl->search_engine, "finished",
8803 G_CALLBACK (search_engine_finished_cb), impl);
8804 g_signal_connect (impl->search_engine, "error",
8805 G_CALLBACK (search_engine_error_cb), impl);
8807 _gtk_search_engine_start (impl->search_engine);
8810 /* Callback used when the user presses Enter while typing on the search
8811 * entry; starts the query
8814 search_entry_activate_cb (GtkEntry *entry,
8817 GtkFileChooserDefault *impl;
8820 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8822 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
8823 if (strlen (text) == 0)
8826 /* reset any existing query object */
8827 if (impl->search_query)
8829 g_object_unref (impl->search_query);
8830 impl->search_query = NULL;
8833 search_start_query (impl, text);
8837 focus_entry_idle_cb (GtkFileChooserDefault *impl)
8839 GDK_THREADS_ENTER ();
8841 g_source_destroy (impl->focus_entry_idle);
8842 impl->focus_entry_idle = NULL;
8844 if (impl->search_entry)
8845 gtk_widget_grab_focus (impl->search_entry);
8847 GDK_THREADS_LEAVE ();
8853 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
8855 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
8856 * pane, we get a selection-changed signal and we set up the search widgets.
8857 * However, gtk_tree_view_button_press() focuses the treeview *after* making
8858 * the change to the selection. So, we need to re-focus the search entry
8859 * after the treeview has finished doing its work; we'll do that in an idle
8863 if (!impl->focus_entry_idle)
8864 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
8867 /* Hides the path bar and creates the search entry */
8869 search_setup_widgets (GtkFileChooserDefault *impl)
8875 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
8879 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
8880 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
8884 label = gtk_label_new (NULL);
8885 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
8886 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
8887 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
8892 impl->search_entry = gtk_entry_new ();
8893 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
8894 g_signal_connect (impl->search_entry, "activate",
8895 G_CALLBACK (search_entry_activate_cb),
8897 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
8899 /* if there already is a query, restart it */
8900 if (impl->search_query)
8902 gchar *query = _gtk_query_get_text (impl->search_query);
8906 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
8907 search_start_query (impl, query);
8913 g_object_unref (impl->search_query);
8914 impl->search_query = NULL;
8918 gtk_widget_hide (impl->browse_path_bar);
8919 gtk_widget_hide (impl->browse_new_folder_button);
8921 /* Box for search widgets */
8922 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
8923 gtk_widget_show_all (impl->search_hbox);
8924 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
8926 /* Hide the location widgets temporarily */
8928 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8929 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8931 gtk_widget_hide (impl->location_button);
8932 gtk_widget_hide (impl->location_entry_box);
8935 focus_search_entry_in_idle (impl);
8937 /* FMQ: hide the filter combo? */
8940 /* Stops running operations like populating the browse model, searches, and the recent-files model */
8942 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
8946 case OPERATION_MODE_BROWSE:
8947 stop_loading_and_clear_list_model (impl, TRUE);
8950 case OPERATION_MODE_SEARCH:
8951 search_stop_searching (impl, FALSE);
8952 search_clear_model (impl, TRUE);
8954 gtk_widget_destroy (impl->search_hbox);
8955 impl->search_hbox = NULL;
8956 impl->search_entry = NULL;
8959 case OPERATION_MODE_RECENT:
8960 recent_stop_loading (impl);
8961 recent_clear_model (impl, TRUE);
8963 gtk_widget_destroy (impl->recent_hbox);
8964 impl->recent_hbox = NULL;
8969 /* Sometimes we need to frob the selection in the shortcuts list manually */
8971 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
8973 GtkTreeSelection *selection;
8976 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
8978 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
8980 path = gtk_tree_path_new_from_indices (pos, -1);
8981 gtk_tree_selection_select_path (selection, path);
8982 gtk_tree_path_free (path);
8984 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
8987 /* Main entry point to the searching functions; this gets called when the user
8988 * activates the Search shortcut.
8991 search_activate (GtkFileChooserDefault *impl)
8993 OperationMode previous_mode;
8995 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8997 focus_search_entry_in_idle (impl);
9001 previous_mode = impl->operation_mode;
9002 impl->operation_mode = OPERATION_MODE_SEARCH;
9004 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
9006 stop_operation (impl, previous_mode);
9008 g_assert (impl->search_hbox == NULL);
9009 g_assert (impl->search_entry == NULL);
9010 g_assert (impl->search_model == NULL);
9012 search_setup_widgets (impl);
9013 file_list_set_sort_column_ids (impl);
9017 * Recent files support
9020 /* Frees the data in the recent_model */
9022 recent_clear_model (GtkFileChooserDefault *impl,
9023 gboolean remove_from_treeview)
9025 if (!impl->recent_model)
9028 if (remove_from_treeview)
9029 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9031 g_object_unref (impl->recent_model);
9032 impl->recent_model = NULL;
9035 /* Stops any ongoing loading of the recent files list; does
9036 * not touch the recent_model
9039 recent_stop_loading (GtkFileChooserDefault *impl)
9041 if (impl->load_recent_id)
9043 g_source_remove (impl->load_recent_id);
9044 impl->load_recent_id = 0;
9048 /* Stops any pending load, clears the file list, and switches
9049 * back to OPERATION_MODE_BROWSE
9052 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9054 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9056 recent_stop_loading (impl);
9057 recent_clear_model (impl, TRUE);
9059 gtk_widget_destroy (impl->recent_hbox);
9060 impl->recent_hbox = NULL;
9062 gtk_widget_show (impl->browse_path_bar);
9063 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9064 gtk_widget_hide (impl->browse_new_folder_button);
9066 gtk_widget_show (impl->browse_new_folder_button);
9068 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9069 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9071 gtk_widget_show (impl->location_button);
9073 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9074 gtk_widget_show (impl->location_entry_box);
9077 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9079 impl->operation_mode = OPERATION_MODE_BROWSE;
9081 file_list_set_sort_column_ids (impl);
9085 recent_setup_model (GtkFileChooserDefault *impl)
9087 g_assert (impl->recent_model == NULL);
9089 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9091 MODEL_COLUMN_TYPES);
9093 _gtk_file_system_model_set_filter (impl->recent_model,
9094 impl->current_filter);
9095 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9099 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9103 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9107 set_sort_column (impl);
9112 GtkFileChooserDefault *impl;
9114 guint needs_sorting : 1;
9118 recent_idle_cleanup (gpointer data)
9120 RecentLoadData *load_data = data;
9121 GtkFileChooserDefault *impl = load_data->impl;
9123 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9124 GTK_TREE_MODEL (impl->recent_model));
9126 set_busy_cursor (impl, FALSE);
9128 impl->load_recent_id = 0;
9134 recent_sort_mru (gconstpointer a,
9137 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9138 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9140 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9144 get_recent_files_limit (GtkWidget *widget)
9146 GtkSettings *settings;
9149 if (gtk_widget_has_screen (widget))
9150 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9152 settings = gtk_settings_get_default ();
9154 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9159 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9161 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9167 limit = get_recent_files_limit (GTK_WIDGET (impl));
9171 for (l = items; l; l = l->next)
9173 GtkRecentInfo *info = l->data;
9176 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9177 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9180 g_object_unref (file);
9183 if (limit != -1 && n >= limit)
9189 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9194 folders = _gtk_file_chooser_extract_recent_folders (items);
9196 for (l = folders; l; l = l->next)
9198 GFile *folder = l->data;
9200 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9205 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9206 g_list_free (folders);
9210 recent_idle_load (gpointer data)
9212 RecentLoadData *load_data = data;
9213 GtkFileChooserDefault *impl = load_data->impl;
9215 if (!impl->recent_manager)
9218 /* first iteration: load all the items */
9219 if (!load_data->items)
9221 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9222 if (!load_data->items)
9225 load_data->needs_sorting = TRUE;
9230 /* second iteration: MRU sorting and clamping, and populating the model */
9231 if (load_data->needs_sorting)
9233 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9235 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9236 populate_model_with_recent_items (impl, load_data->items);
9238 populate_model_with_folders (impl, load_data->items);
9240 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9241 g_list_free (load_data->items);
9242 load_data->items = NULL;
9249 recent_start_loading (GtkFileChooserDefault *impl)
9251 RecentLoadData *load_data;
9253 recent_stop_loading (impl);
9254 recent_clear_model (impl, TRUE);
9255 recent_setup_model (impl);
9256 set_busy_cursor (impl, TRUE);
9258 g_assert (impl->load_recent_id == 0);
9260 load_data = g_new (RecentLoadData, 1);
9261 load_data->impl = impl;
9262 load_data->items = NULL;
9263 load_data->needs_sorting = TRUE;
9265 /* begin lazy loading the recent files into the model */
9266 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9269 recent_idle_cleanup);
9273 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9283 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9284 *list = g_slist_prepend (*list, file);
9287 /* Constructs a list of the selected paths in recent files mode */
9289 recent_get_selected_files (GtkFileChooserDefault *impl)
9292 GtkTreeSelection *selection;
9296 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9297 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9298 result = g_slist_reverse (result);
9303 /* Called from ::should_respond(). We return whether there are selected
9304 * files in the recent files list.
9307 recent_should_respond (GtkFileChooserDefault *impl)
9309 GtkTreeSelection *selection;
9311 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9313 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9314 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9317 /* Hide the location widgets temporarily */
9319 recent_hide_entry (GtkFileChooserDefault *impl)
9325 /* Box for recent widgets */
9326 impl->recent_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9328 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
9329 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->recent_hbox);
9330 gtk_widget_show (impl->recent_hbox);
9332 /* For Save mode, we don't want this icon/label - we want update_path_bar() to do its thing instead */
9333 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9337 str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
9338 impl->browse_select_a_folder_label = gtk_label_new (NULL);
9339 gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
9341 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), impl->browse_select_a_folder_label, FALSE, FALSE, 0);
9346 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
9347 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
9348 gtk_widget_show (image);
9351 label = gtk_label_new (NULL);
9352 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
9353 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9354 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
9355 gtk_widget_show (label);
9359 gtk_widget_hide (impl->browse_path_bar);
9360 gtk_widget_hide (impl->browse_new_folder_button);
9362 /* Hide the location widgets temporarily */
9363 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9364 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9366 gtk_widget_hide (impl->location_button);
9367 gtk_widget_hide (impl->location_entry_box);
9371 /* Main entry point to the recent files functions; this gets called when
9372 * the user activates the Recently Used shortcut.
9375 recent_activate (GtkFileChooserDefault *impl)
9377 OperationMode previous_mode;
9379 if (impl->operation_mode == OPERATION_MODE_RECENT)
9382 previous_mode = impl->operation_mode;
9383 impl->operation_mode = OPERATION_MODE_RECENT;
9385 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
9387 stop_operation (impl, previous_mode);
9389 recent_hide_entry (impl);
9391 file_list_set_sort_column_ids (impl);
9392 recent_start_loading (impl);
9396 set_current_filter (GtkFileChooserDefault *impl,
9397 GtkFileFilter *filter)
9399 if (impl->current_filter != filter)
9403 /* NULL filters are allowed to reset to non-filtered status
9405 filter_index = g_slist_index (impl->filters, filter);
9406 if (impl->filters && filter && filter_index < 0)
9409 if (impl->current_filter)
9410 g_object_unref (impl->current_filter);
9411 impl->current_filter = filter;
9412 if (impl->current_filter)
9414 g_object_ref_sink (impl->current_filter);
9418 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9421 if (impl->browse_files_model)
9422 install_list_model_filter (impl);
9424 if (impl->search_model)
9425 _gtk_file_system_model_set_filter (impl->search_model, filter);
9427 if (impl->recent_model)
9428 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9430 g_object_notify (G_OBJECT (impl), "filter");
9435 filter_combo_changed (GtkComboBox *combo_box,
9436 GtkFileChooserDefault *impl)
9438 gint new_index = gtk_combo_box_get_active (combo_box);
9439 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9441 set_current_filter (impl, new_filter);
9445 check_preview_change (GtkFileChooserDefault *impl)
9447 GtkTreePath *cursor_path;
9449 char *new_display_name;
9450 GtkTreeModel *model;
9452 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9453 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9458 gtk_tree_model_get_iter (model, &iter, cursor_path);
9459 gtk_tree_model_get (model, &iter,
9460 MODEL_COL_FILE, &new_file,
9461 MODEL_COL_NAME, &new_display_name,
9464 gtk_tree_path_free (cursor_path);
9469 new_display_name = NULL;
9472 if (new_file != impl->preview_file &&
9473 !(new_file && impl->preview_file &&
9474 g_file_equal (new_file, impl->preview_file)))
9476 if (impl->preview_file)
9478 g_object_unref (impl->preview_file);
9479 g_free (impl->preview_display_name);
9484 impl->preview_file = new_file;
9485 impl->preview_display_name = new_display_name;
9489 impl->preview_file = NULL;
9490 impl->preview_display_name = NULL;
9491 g_free (new_display_name);
9494 if (impl->use_preview_label && impl->preview_label)
9495 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9497 g_signal_emit_by_name (impl, "update-preview");
9502 g_object_unref (new_file);
9504 g_free (new_display_name);
9509 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9510 GtkFileSystemVolume *volume,
9511 const GError *error,
9515 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9516 GtkFileChooserDefault *impl = data;
9518 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9521 impl->shortcuts_activate_iter_cancellable = NULL;
9523 set_busy_cursor (impl, FALSE);
9530 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9534 name = _gtk_file_system_volume_get_display_name (volume);
9535 msg = g_strdup_printf (_("Could not mount %s"), name);
9537 error_message (impl, msg, error->message);
9546 file = _gtk_file_system_volume_get_root (volume);
9549 change_folder_and_display_error (impl, file, FALSE);
9550 g_object_unref (file);
9554 g_object_unref (impl);
9555 g_object_unref (cancellable);
9559 /* Activates a volume by mounting it if necessary and then switching to its
9563 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9564 GtkFileSystemVolume *volume)
9568 switch (impl->operation_mode)
9570 case OPERATION_MODE_BROWSE:
9572 case OPERATION_MODE_SEARCH:
9573 search_switch_to_browse_mode (impl);
9575 case OPERATION_MODE_RECENT:
9576 recent_switch_to_browse_mode (impl);
9580 /* We ref the file chooser since volume_mount() may run a main loop, and the
9581 * user could close the file chooser window in the meantime.
9583 g_object_ref (impl);
9585 if (!_gtk_file_system_volume_is_mounted (volume))
9587 GMountOperation *mount_op;
9589 set_busy_cursor (impl, TRUE);
9591 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9592 impl->shortcuts_activate_iter_cancellable =
9593 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9594 shortcuts_activate_volume_mount_cb,
9595 g_object_ref (impl));
9596 g_object_unref (mount_op);
9600 file = _gtk_file_system_volume_get_root (volume);
9603 change_folder_and_display_error (impl, file, FALSE);
9604 g_object_unref (file);
9608 g_object_unref (impl);
9611 /* Opens the folder or volume at the specified iter in the shortcuts model */
9612 struct ShortcutsActivateData
9614 GtkFileChooserDefault *impl;
9619 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9621 const GError *error,
9624 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9625 struct ShortcutsActivateData *data = user_data;
9627 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9630 data->impl->shortcuts_activate_iter_cancellable = NULL;
9635 if (!error && _gtk_file_info_consider_as_directory (info))
9636 change_folder_and_display_error (data->impl, data->file, FALSE);
9638 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9643 g_object_unref (data->impl);
9644 g_object_unref (data->file);
9647 g_object_unref (cancellable);
9651 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9652 GtkFileSystemVolume *volume,
9653 const GError *error,
9656 struct ShortcutsActivateData *data = user_data;
9660 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9662 g_object_unref (data->impl);
9663 g_object_unref (data->file);
9669 data->impl->shortcuts_activate_iter_cancellable =
9670 _gtk_file_system_get_info (data->impl->file_system, data->file,
9672 shortcuts_activate_get_info_cb, data);
9675 _gtk_file_system_volume_unref (volume);
9679 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9683 ShortcutType shortcut_type;
9685 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9686 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9687 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9688 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9690 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9691 SHORTCUTS_COL_DATA, &col_data,
9692 SHORTCUTS_COL_TYPE, &shortcut_type,
9695 if (impl->shortcuts_activate_iter_cancellable)
9697 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9698 impl->shortcuts_activate_iter_cancellable = NULL;
9701 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9703 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9705 GtkFileSystemVolume *volume;
9709 shortcuts_activate_volume (impl, volume);
9711 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9713 struct ShortcutsActivateData *data;
9714 GtkFileSystemVolume *volume;
9716 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9718 data = g_new0 (struct ShortcutsActivateData, 1);
9719 data->impl = g_object_ref (impl);
9720 data->file = g_object_ref (col_data);
9722 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9724 GMountOperation *mount_operation;
9725 GtkWidget *toplevel;
9727 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9729 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9731 impl->shortcuts_activate_iter_cancellable =
9732 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9734 shortcuts_activate_mount_enclosing_volume,
9739 impl->shortcuts_activate_iter_cancellable =
9740 _gtk_file_system_get_info (impl->file_system, data->file,
9742 shortcuts_activate_get_info_cb, data);
9745 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9747 search_activate (impl);
9749 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9751 recent_activate (impl);
9755 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9757 shortcuts_key_press_event_cb (GtkWidget *widget,
9759 GtkFileChooserDefault *impl)
9763 modifiers = gtk_accelerator_get_default_mod_mask ();
9765 if (key_is_left_or_right (event))
9767 gtk_widget_grab_focus (impl->browse_files_tree_view);
9771 if ((event->keyval == GDK_KEY_BackSpace
9772 || event->keyval == GDK_KEY_Delete
9773 || event->keyval == GDK_KEY_KP_Delete)
9774 && (event->state & modifiers) == 0)
9776 remove_selected_bookmarks (impl);
9780 if ((event->keyval == GDK_KEY_F2)
9781 && (event->state & modifiers) == 0)
9783 rename_selected_bookmark (impl);
9791 shortcuts_select_func (GtkTreeSelection *selection,
9792 GtkTreeModel *model,
9794 gboolean path_currently_selected,
9797 GtkFileChooserDefault *impl = data;
9798 GtkTreeIter filter_iter;
9799 ShortcutType shortcut_type;
9801 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9802 g_assert_not_reached ();
9804 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9806 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9810 list_select_func (GtkTreeSelection *selection,
9811 GtkTreeModel *model,
9813 gboolean path_currently_selected,
9816 GtkFileChooserDefault *impl = data;
9818 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
9819 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9824 if (!gtk_tree_model_get_iter (model, &iter, path))
9826 gtk_tree_model_get (model, &iter,
9827 MODEL_COL_IS_FOLDER, &is_folder,
9837 list_selection_changed (GtkTreeSelection *selection,
9838 GtkFileChooserDefault *impl)
9840 /* See if we are in the new folder editable row for Save mode */
9841 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
9842 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9845 gboolean had_selection;
9847 info = get_selected_file_info_from_file_list (impl, &had_selection);
9849 goto out; /* normal processing */
9852 return; /* We are on the editable row for New Folder */
9857 if (impl->location_entry)
9858 update_chooser_entry (impl);
9860 update_path_bar (impl);
9862 check_preview_change (impl);
9863 bookmarks_check_add_sensitivity (impl);
9865 g_signal_emit_by_name (impl, "selection-changed", 0);
9868 /* Callback used when a row in the file list is activated */
9870 list_row_activated (GtkTreeView *tree_view,
9872 GtkTreeViewColumn *column,
9873 GtkFileChooserDefault *impl)
9877 GtkTreeModel *model;
9880 model = gtk_tree_view_get_model (tree_view);
9882 if (!gtk_tree_model_get_iter (model, &iter, path))
9885 gtk_tree_model_get (model, &iter,
9886 MODEL_COL_FILE, &file,
9887 MODEL_COL_IS_FOLDER, &is_folder,
9890 if (is_folder && file)
9892 change_folder_and_display_error (impl, file, FALSE);
9893 g_object_unref (file);
9897 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9898 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9899 g_signal_emit_by_name (impl, "file-activated");
9904 g_object_unref (file);
9908 path_bar_clicked (GtkPathBar *path_bar,
9911 gboolean child_is_hidden,
9912 GtkFileChooserDefault *impl)
9915 pending_select_files_add (impl, child_file);
9917 if (!change_folder_and_display_error (impl, file, FALSE))
9920 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
9921 * show hidden files so that ".baz" appears in the file list, as it will still
9922 * be shown in the path bar: "/foo/[bar]/.baz"
9924 if (child_is_hidden)
9925 g_object_set (impl, "show-hidden", TRUE, NULL);
9929 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
9931 GtkTreeViewColumn *column;
9932 GtkCellRenderer *renderer;
9934 gboolean always_sensitive;
9936 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
9937 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
9939 /* Keep the following column numbers in sync with create_file_list() */
9942 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
9943 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
9944 for (walk = list; walk; walk = walk->next)
9946 renderer = walk->data;
9947 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
9949 gtk_tree_view_column_set_attributes (column, renderer,
9950 "pixbuf", MODEL_COL_PIXBUF,
9955 gtk_tree_view_column_set_attributes (column, renderer,
9956 "text", MODEL_COL_NAME,
9957 "ellipsize", MODEL_COL_ELLIPSIZE,
9960 if (always_sensitive)
9961 g_object_set (renderer, "sensitive", TRUE, NULL);
9963 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
9968 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
9969 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
9970 renderer = list->data;
9971 gtk_tree_view_column_set_attributes (column, renderer,
9972 "text", MODEL_COL_SIZE_TEXT,
9974 if (always_sensitive)
9975 g_object_set (renderer, "sensitive", TRUE, NULL);
9977 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
9981 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
9982 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
9983 renderer = list->data;
9984 gtk_tree_view_column_set_attributes (column, renderer,
9985 "text", MODEL_COL_MTIME_TEXT,
9987 if (always_sensitive)
9988 g_object_set (renderer, "sensitive", TRUE, NULL);
9990 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
9995 _gtk_file_chooser_default_new (void)
9997 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10001 location_set_user_text (GtkFileChooserDefault *impl,
10004 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10005 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10009 location_popup_handler (GtkFileChooserDefault *impl,
10012 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10014 GtkWidget *widget_to_focus;
10016 /* This will give us the location widgets back */
10017 switch (impl->operation_mode)
10019 case OPERATION_MODE_SEARCH:
10020 search_switch_to_browse_mode (impl);
10022 case OPERATION_MODE_RECENT:
10023 recent_switch_to_browse_mode (impl);
10025 case OPERATION_MODE_BROWSE:
10026 g_assert_not_reached ();
10030 if (impl->current_folder)
10031 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10033 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10034 widget_to_focus = impl->browse_files_tree_view;
10036 widget_to_focus = impl->location_entry;
10038 gtk_widget_grab_focus (widget_to_focus);
10042 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10043 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10045 LocationMode new_mode;
10049 /* since the user typed something, we unconditionally want to turn on the entry */
10050 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10052 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10053 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10054 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10055 new_mode = LOCATION_MODE_PATH_BAR;
10058 g_assert_not_reached ();
10062 location_mode_set (impl, new_mode, TRUE);
10063 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10066 location_set_user_text (impl, path);
10069 location_entry_set_initial_text (impl);
10070 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10074 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10075 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10077 gtk_widget_grab_focus (impl->location_entry);
10079 location_set_user_text (impl, path);
10082 g_assert_not_reached ();
10085 /* Handler for the "up-folder" keybinding signal */
10087 up_folder_handler (GtkFileChooserDefault *impl)
10089 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10092 /* Handler for the "down-folder" keybinding signal */
10094 down_folder_handler (GtkFileChooserDefault *impl)
10096 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10099 /* Switches to the shortcut in the specified index */
10101 switch_to_shortcut (GtkFileChooserDefault *impl,
10106 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10107 g_assert_not_reached ();
10109 shortcuts_activate_iter (impl, &iter);
10112 /* Handler for the "home-folder" keybinding signal */
10114 home_folder_handler (GtkFileChooserDefault *impl)
10116 if (impl->has_home)
10117 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10120 /* Handler for the "desktop-folder" keybinding signal */
10122 desktop_folder_handler (GtkFileChooserDefault *impl)
10124 if (impl->has_desktop)
10125 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10128 /* Handler for the "search-shortcut" keybinding signal */
10130 search_shortcut_handler (GtkFileChooserDefault *impl)
10132 if (impl->has_search)
10134 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10136 /* we want the entry widget to grab the focus the first
10137 * time, not the browse_files_tree_view widget.
10139 if (impl->search_entry)
10140 gtk_widget_grab_focus (impl->search_entry);
10144 /* Handler for the "recent-shortcut" keybinding signal */
10146 recent_shortcut_handler (GtkFileChooserDefault *impl)
10148 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10152 quick_bookmark_handler (GtkFileChooserDefault *impl,
10153 gint bookmark_index)
10158 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10161 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10163 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10164 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10167 gtk_tree_path_free (path);
10169 switch_to_shortcut (impl, bookmark_pos);
10173 show_hidden_handler (GtkFileChooserDefault *impl)
10175 g_object_set (impl,
10176 "show-hidden", !impl->show_hidden,
10181 /* Drag and drop interfaces */
10184 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10189 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10191 model->impl = NULL;
10194 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10196 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10199 ShortcutsPaneModelFilter *model;
10203 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10205 pos = *gtk_tree_path_get_indices (path);
10206 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10208 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10211 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10215 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10217 GtkSelectionData *selection_data)
10224 /* Fill the GtkTreeDragSourceIface vtable */
10226 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10228 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10229 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10233 /* Fill the GtkTreeDragDestIface vtable */
10235 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10237 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10238 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10242 static GtkTreeModel *
10243 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10244 GtkTreeModel *child_model,
10247 ShortcutsPaneModelFilter *model;
10249 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10250 "child-model", child_model,
10251 "virtual-root", root,
10254 model->impl = impl;
10256 return GTK_TREE_MODEL (model);