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"
49 #include "gtkinfobar.h"
51 #include "gtkmarshalers.h"
52 #include "gtkmessagedialog.h"
53 #include "gtkmountoperation.h"
54 #include "gtkpathbar.h"
55 #include "gtkprivate.h"
56 #include "gtkradiobutton.h"
57 #include "gtkrecentfilter.h"
58 #include "gtkrecentmanager.h"
59 #include "gtkscrolledwindow.h"
60 #include "gtkseparatormenuitem.h"
61 #include "gtksizegroup.h"
62 #include "gtksizerequest.h"
65 #include "gtktoolbar.h"
66 #include "gtktoolbutton.h"
67 #include "gtktooltip.h"
68 #include "gtktreednd.h"
69 #include "gtktreeprivate.h"
70 #include "gtktreeselection.h"
72 #include "gtkorientable.h"
79 #include <sys/types.h>
90 #undef PROFILE_FILE_CHOOSER
91 #ifdef PROFILE_FILE_CHOOSER
98 #define PROFILE_INDENT 4
99 static int profile_indent;
102 profile_add_indent (int indent)
104 profile_indent += indent;
105 if (profile_indent < 0)
106 g_error ("You screwed up your indentation");
110 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
115 profile_add_indent (indent);
117 if (profile_indent == 0)
118 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
120 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
126 profile_add_indent (indent);
129 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
130 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
131 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
133 #define profile_start(x, y)
134 #define profile_end(x, y)
135 #define profile_msg(x, y)
140 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
142 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
143 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
144 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146 #define MAX_LOADING_TIME 500
148 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
150 struct _GtkFileChooserDefaultClass
152 GtkBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
172 static guint signals[LAST_SIGNAL] = { 0 };
174 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
176 SHORTCUTS_COL_PIXBUF,
180 SHORTCUTS_COL_REMOVABLE,
181 SHORTCUTS_COL_PIXBUF_VISIBLE,
182 SHORTCUTS_COL_CANCELLABLE,
183 SHORTCUTS_COL_NUM_COLUMNS
188 SHORTCUT_TYPE_VOLUME,
189 SHORTCUT_TYPE_SEPARATOR,
190 SHORTCUT_TYPE_SEARCH,
194 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
195 "standard::is-hidden,standard::is-backup,standard::size," \
196 "standard::content-type,time::modified"
198 /* the first 3 must be these due to settings caching sort column */
203 MODEL_COL_NAME_COLLATED,
207 MODEL_COL_MTIME_TEXT,
209 MODEL_COL_NUM_COLUMNS
212 /* This list of types is passed to _gtk_file_system_model_new*() */
213 #define MODEL_COLUMN_TYPES \
214 MODEL_COL_NUM_COLUMNS, \
215 G_TYPE_STRING, /* MODEL_COL_NAME */ \
216 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
217 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
218 G_TYPE_FILE, /* MODEL_COL_FILE */ \
219 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
220 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
221 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
222 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
223 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
224 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
226 /* Identifiers for target types */
231 /* Interesting places in the shortcuts bar */
235 SHORTCUTS_RECENT_SEPARATOR,
240 SHORTCUTS_BOOKMARKS_SEPARATOR,
242 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
243 SHORTCUTS_CURRENT_FOLDER
246 /* Icon size for if we can't get it from the theme */
247 #define FALLBACK_ICON_SIZE 16
249 #define PREVIEW_HBOX_SPACING 12
253 #define SETTINGS_KEY_LAST_FOLDER_URI "last-folder-uri"
254 #define SETTINGS_KEY_LOCATION_MODE "location-mode"
255 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
256 #define SETTINGS_KEY_SHOW_SIZE_COLUMN "show-size-column"
257 #define SETTINGS_KEY_SORT_COLUMN "sort-column"
258 #define SETTINGS_KEY_SORT_ORDER "sort-order"
259 #define SETTINGS_KEY_WINDOW_POSITION "window-position"
260 #define SETTINGS_KEY_WINDOW_SIZE "window-size"
262 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
263 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
265 static GObject* gtk_file_chooser_default_constructor (GType type,
266 guint n_construct_properties,
267 GObjectConstructParam *construct_params);
268 static void gtk_file_chooser_default_finalize (GObject *object);
269 static void gtk_file_chooser_default_set_property (GObject *object,
273 static void gtk_file_chooser_default_get_property (GObject *object,
277 static void gtk_file_chooser_default_dispose (GObject *object);
278 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
279 static void gtk_file_chooser_default_realize (GtkWidget *widget);
280 static void gtk_file_chooser_default_map (GtkWidget *widget);
281 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
282 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
283 GtkWidget *previous_toplevel);
284 static void gtk_file_chooser_default_style_updated (GtkWidget *widget);
285 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
286 GdkScreen *previous_screen);
287 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
288 GtkAllocation *allocation);
290 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
293 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
296 gboolean clear_entry,
298 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
299 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
301 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
304 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
306 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
307 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
308 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
309 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
310 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
311 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
312 GtkFileFilter *filter);
313 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
314 GtkFileFilter *filter);
315 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
316 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
319 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
322 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
324 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
326 gint *default_height);
327 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
328 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
330 static void location_popup_handler (GtkFileChooserDefault *impl,
332 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
333 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
334 static void up_folder_handler (GtkFileChooserDefault *impl);
335 static void down_folder_handler (GtkFileChooserDefault *impl);
336 static void home_folder_handler (GtkFileChooserDefault *impl);
337 static void desktop_folder_handler (GtkFileChooserDefault *impl);
338 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
339 gint bookmark_index);
340 static void show_hidden_handler (GtkFileChooserDefault *impl);
341 static void search_shortcut_handler (GtkFileChooserDefault *impl);
342 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
343 static void update_appearance (GtkFileChooserDefault *impl);
345 static void set_current_filter (GtkFileChooserDefault *impl,
346 GtkFileFilter *filter);
347 static void check_preview_change (GtkFileChooserDefault *impl);
349 static void filter_combo_changed (GtkComboBox *combo_box,
350 GtkFileChooserDefault *impl);
352 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
354 GtkFileChooserDefault *impl);
356 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
359 gboolean path_currently_selected,
361 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
363 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
365 static int shortcuts_get_index (GtkFileChooserDefault *impl,
366 ShortcutsIndex where);
367 static int shortcut_find_position (GtkFileChooserDefault *impl,
370 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
372 static gboolean list_select_func (GtkTreeSelection *selection,
375 gboolean path_currently_selected,
378 static void list_selection_changed (GtkTreeSelection *tree_selection,
379 GtkFileChooserDefault *impl);
380 static void list_row_activated (GtkTreeView *tree_view,
382 GtkTreeViewColumn *column,
383 GtkFileChooserDefault *impl);
385 static void path_bar_clicked (GtkPathBar *path_bar,
388 gboolean child_is_hidden,
389 GtkFileChooserDefault *impl);
391 static void add_bookmark_button_clicked_cb (GtkButton *button,
392 GtkFileChooserDefault *impl);
393 static void remove_bookmark_button_clicked_cb (GtkButton *button,
394 GtkFileChooserDefault *impl);
396 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
398 static void load_remove_timer (GtkFileChooserDefault *impl);
399 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
401 static void location_button_toggled_cb (GtkToggleButton *toggle,
402 GtkFileChooserDefault *impl);
403 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
405 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
406 gboolean remove_from_treeview);
407 static void search_stop_searching (GtkFileChooserDefault *impl,
408 gboolean remove_query);
409 static void search_clear_model (GtkFileChooserDefault *impl,
410 gboolean remove_from_treeview);
411 static gboolean search_should_respond (GtkFileChooserDefault *impl);
412 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
413 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
414 static void search_entry_activate_cb (GtkEntry *entry,
416 static void settings_load (GtkFileChooserDefault *impl);
418 static void recent_stop_loading (GtkFileChooserDefault *impl);
419 static void recent_clear_model (GtkFileChooserDefault *impl,
420 gboolean remove_from_treeview);
421 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
422 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
423 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
424 static void set_file_system_backend (GtkFileChooserDefault *impl);
425 static void unset_file_system_backend (GtkFileChooserDefault *impl);
431 /* Drag and drop interface declarations */
434 GtkTreeModelFilter parent;
436 GtkFileChooserDefault *impl;
437 } ShortcutsPaneModelFilter;
440 GtkTreeModelFilterClass parent_class;
441 } ShortcutsPaneModelFilterClass;
443 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
444 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
446 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
448 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
449 _shortcuts_pane_model_filter,
450 GTK_TYPE_TREE_MODEL_FILTER,
451 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
452 shortcuts_pane_model_filter_drag_source_iface_init))
454 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
455 GtkTreeModel *child_model,
460 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_BOX,
461 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
462 gtk_file_chooser_default_iface_init)
463 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
464 gtk_file_chooser_embed_default_iface_init));
468 add_normal_and_shifted_binding (GtkBindingSet *binding_set,
470 GdkModifierType modifiers,
471 const gchar *signal_name)
473 gtk_binding_entry_add_signal (binding_set,
477 gtk_binding_entry_add_signal (binding_set,
478 keyval, modifiers | GDK_SHIFT_MASK,
483 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
485 static const guint quick_bookmark_keyvals[10] = {
486 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
488 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
489 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
490 GtkBindingSet *binding_set;
493 gobject_class->finalize = gtk_file_chooser_default_finalize;
494 gobject_class->constructor = gtk_file_chooser_default_constructor;
495 gobject_class->set_property = gtk_file_chooser_default_set_property;
496 gobject_class->get_property = gtk_file_chooser_default_get_property;
497 gobject_class->dispose = gtk_file_chooser_default_dispose;
499 widget_class->show_all = gtk_file_chooser_default_show_all;
500 widget_class->realize = gtk_file_chooser_default_realize;
501 widget_class->map = gtk_file_chooser_default_map;
502 widget_class->unmap = gtk_file_chooser_default_unmap;
503 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
504 widget_class->style_updated = gtk_file_chooser_default_style_updated;
505 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
506 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
508 signals[LOCATION_POPUP] =
509 g_signal_new_class_handler (I_("location-popup"),
510 G_OBJECT_CLASS_TYPE (class),
511 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
512 G_CALLBACK (location_popup_handler),
514 _gtk_marshal_VOID__STRING,
515 G_TYPE_NONE, 1, G_TYPE_STRING);
517 signals[LOCATION_POPUP_ON_PASTE] =
518 g_signal_new_class_handler (I_("location-popup-on-paste"),
519 G_OBJECT_CLASS_TYPE (class),
520 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
521 G_CALLBACK (location_popup_on_paste_handler),
523 _gtk_marshal_VOID__VOID,
526 signals[LOCATION_TOGGLE_POPUP] =
527 g_signal_new_class_handler (I_("location-toggle-popup"),
528 G_OBJECT_CLASS_TYPE (class),
529 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
530 G_CALLBACK (location_toggle_popup_handler),
532 _gtk_marshal_VOID__VOID,
536 g_signal_new_class_handler (I_("up-folder"),
537 G_OBJECT_CLASS_TYPE (class),
538 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
539 G_CALLBACK (up_folder_handler),
541 _gtk_marshal_VOID__VOID,
544 signals[DOWN_FOLDER] =
545 g_signal_new_class_handler (I_("down-folder"),
546 G_OBJECT_CLASS_TYPE (class),
547 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
548 G_CALLBACK (down_folder_handler),
550 _gtk_marshal_VOID__VOID,
553 signals[HOME_FOLDER] =
554 g_signal_new_class_handler (I_("home-folder"),
555 G_OBJECT_CLASS_TYPE (class),
556 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
557 G_CALLBACK (home_folder_handler),
559 _gtk_marshal_VOID__VOID,
562 signals[DESKTOP_FOLDER] =
563 g_signal_new_class_handler (I_("desktop-folder"),
564 G_OBJECT_CLASS_TYPE (class),
565 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
566 G_CALLBACK (desktop_folder_handler),
568 _gtk_marshal_VOID__VOID,
571 signals[QUICK_BOOKMARK] =
572 g_signal_new_class_handler (I_("quick-bookmark"),
573 G_OBJECT_CLASS_TYPE (class),
574 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
575 G_CALLBACK (quick_bookmark_handler),
577 _gtk_marshal_VOID__INT,
578 G_TYPE_NONE, 1, G_TYPE_INT);
580 signals[SHOW_HIDDEN] =
581 g_signal_new_class_handler (I_("show-hidden"),
582 G_OBJECT_CLASS_TYPE (class),
583 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
584 G_CALLBACK (show_hidden_handler),
586 _gtk_marshal_VOID__VOID,
589 signals[SEARCH_SHORTCUT] =
590 g_signal_new_class_handler (I_("search-shortcut"),
591 G_OBJECT_CLASS_TYPE (class),
592 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
593 G_CALLBACK (search_shortcut_handler),
595 _gtk_marshal_VOID__VOID,
598 signals[RECENT_SHORTCUT] =
599 g_signal_new_class_handler (I_("recent-shortcut"),
600 G_OBJECT_CLASS_TYPE (class),
601 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
602 G_CALLBACK (recent_shortcut_handler),
604 _gtk_marshal_VOID__VOID,
607 binding_set = gtk_binding_set_by_class (class);
609 gtk_binding_entry_add_signal (binding_set,
610 GDK_KEY_l, GDK_CONTROL_MASK,
611 "location-toggle-popup",
614 gtk_binding_entry_add_signal (binding_set,
617 1, G_TYPE_STRING, "/");
618 gtk_binding_entry_add_signal (binding_set,
619 GDK_KEY_KP_Divide, 0,
621 1, G_TYPE_STRING, "/");
624 gtk_binding_entry_add_signal (binding_set,
625 GDK_KEY_asciitilde, 0,
627 1, G_TYPE_STRING, "~");
630 gtk_binding_entry_add_signal (binding_set,
631 GDK_KEY_v, GDK_CONTROL_MASK,
632 "location-popup-on-paste",
634 gtk_binding_entry_add_signal (binding_set,
635 GDK_KEY_BackSpace, 0,
639 add_normal_and_shifted_binding (binding_set,
640 GDK_KEY_Up, GDK_MOD1_MASK,
643 add_normal_and_shifted_binding (binding_set,
644 GDK_KEY_KP_Up, GDK_MOD1_MASK,
647 add_normal_and_shifted_binding (binding_set,
648 GDK_KEY_Down, GDK_MOD1_MASK,
650 add_normal_and_shifted_binding (binding_set,
651 GDK_KEY_KP_Down, GDK_MOD1_MASK,
654 gtk_binding_entry_add_signal (binding_set,
655 GDK_KEY_Home, GDK_MOD1_MASK,
658 gtk_binding_entry_add_signal (binding_set,
659 GDK_KEY_KP_Home, GDK_MOD1_MASK,
662 gtk_binding_entry_add_signal (binding_set,
663 GDK_KEY_d, GDK_MOD1_MASK,
666 gtk_binding_entry_add_signal (binding_set,
667 GDK_KEY_h, GDK_CONTROL_MASK,
670 gtk_binding_entry_add_signal (binding_set,
671 GDK_KEY_s, GDK_MOD1_MASK,
674 gtk_binding_entry_add_signal (binding_set,
675 GDK_KEY_r, GDK_MOD1_MASK,
679 for (i = 0; i < 10; i++)
680 gtk_binding_entry_add_signal (binding_set,
681 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
685 _gtk_file_chooser_install_properties (gobject_class);
689 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
691 iface->select_file = gtk_file_chooser_default_select_file;
692 iface->unselect_file = gtk_file_chooser_default_unselect_file;
693 iface->select_all = gtk_file_chooser_default_select_all;
694 iface->unselect_all = gtk_file_chooser_default_unselect_all;
695 iface->get_files = gtk_file_chooser_default_get_files;
696 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
697 iface->get_file_system = gtk_file_chooser_default_get_file_system;
698 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
699 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
700 iface->set_current_name = gtk_file_chooser_default_set_current_name;
701 iface->add_filter = gtk_file_chooser_default_add_filter;
702 iface->remove_filter = gtk_file_chooser_default_remove_filter;
703 iface->list_filters = gtk_file_chooser_default_list_filters;
704 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
705 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
706 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
710 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
712 iface->get_default_size = gtk_file_chooser_default_get_default_size;
713 iface->should_respond = gtk_file_chooser_default_should_respond;
714 iface->initial_focus = gtk_file_chooser_default_initial_focus;
718 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
720 profile_start ("start", NULL);
721 #ifdef PROFILE_FILE_CHOOSER
722 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
724 impl->local_only = TRUE;
725 impl->preview_widget_active = TRUE;
726 impl->use_preview_label = TRUE;
727 impl->select_multiple = FALSE;
728 impl->show_hidden = FALSE;
729 impl->show_size_column = TRUE;
730 impl->icon_size = FALLBACK_ICON_SIZE;
731 impl->load_state = LOAD_EMPTY;
732 impl->reload_state = RELOAD_EMPTY;
733 impl->pending_select_files = NULL;
734 impl->location_mode = LOCATION_MODE_PATH_BAR;
735 impl->operation_mode = OPERATION_MODE_BROWSE;
736 impl->sort_column = MODEL_COL_NAME;
737 impl->sort_order = GTK_SORT_ASCENDING;
738 impl->recent_manager = gtk_recent_manager_get_default ();
739 impl->create_folders = TRUE;
741 gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
742 GTK_ORIENTATION_VERTICAL);
743 gtk_box_set_spacing (GTK_BOX (impl), 12);
745 set_file_system_backend (impl);
747 profile_end ("end", NULL);
750 /* Frees the data columns for the specified iter in the shortcuts model*/
752 shortcuts_free_row_data (GtkFileChooserDefault *impl,
756 ShortcutType shortcut_type;
757 GCancellable *cancellable;
759 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
760 SHORTCUTS_COL_DATA, &col_data,
761 SHORTCUTS_COL_TYPE, &shortcut_type,
762 SHORTCUTS_COL_CANCELLABLE, &cancellable,
767 g_cancellable_cancel (cancellable);
768 g_object_unref (cancellable);
771 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
772 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
776 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
778 GtkFileSystemVolume *volume;
781 _gtk_file_system_volume_unref (volume);
783 if (shortcut_type == SHORTCUT_TYPE_FILE)
788 g_object_unref (file);
792 /* Frees all the data columns in the shortcuts model */
794 shortcuts_free (GtkFileChooserDefault *impl)
798 if (!impl->shortcuts_model)
801 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
804 shortcuts_free_row_data (impl, &iter);
806 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
808 g_object_unref (impl->shortcuts_model);
809 impl->shortcuts_model = NULL;
813 pending_select_files_free (GtkFileChooserDefault *impl)
815 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
816 g_slist_free (impl->pending_select_files);
817 impl->pending_select_files = NULL;
821 pending_select_files_add (GtkFileChooserDefault *impl,
824 impl->pending_select_files =
825 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
829 gtk_file_chooser_default_finalize (GObject *object)
831 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
834 unset_file_system_backend (impl);
836 if (impl->shortcuts_pane_filter_model)
837 g_object_unref (impl->shortcuts_pane_filter_model);
839 shortcuts_free (impl);
841 g_free (impl->browse_files_last_selected_name);
843 for (l = impl->filters; l; l = l->next)
845 GtkFileFilter *filter;
847 filter = GTK_FILE_FILTER (l->data);
848 g_object_unref (filter);
850 g_slist_free (impl->filters);
852 if (impl->current_filter)
853 g_object_unref (impl->current_filter);
855 if (impl->current_volume_file)
856 g_object_unref (impl->current_volume_file);
858 if (impl->current_folder)
859 g_object_unref (impl->current_folder);
861 if (impl->preview_file)
862 g_object_unref (impl->preview_file);
864 if (impl->browse_path_bar_size_group)
865 g_object_unref (impl->browse_path_bar_size_group);
867 /* Free all the Models we have */
868 stop_loading_and_clear_list_model (impl, FALSE);
869 search_clear_model (impl, FALSE);
870 recent_clear_model (impl, FALSE);
872 /* stopping the load above should have cleared this */
873 g_assert (impl->load_timeout_id == 0);
875 g_free (impl->preview_display_name);
877 g_free (impl->edited_new_text);
879 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
882 /* Shows an error dialog set as transient for the specified window */
884 error_message_with_parent (GtkWindow *parent,
890 dialog = gtk_message_dialog_new (parent,
891 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
896 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
899 if (parent && gtk_window_has_group (parent))
900 gtk_window_group_add_window (gtk_window_get_group (parent),
901 GTK_WINDOW (dialog));
903 gtk_dialog_run (GTK_DIALOG (dialog));
904 gtk_widget_destroy (dialog);
907 /* Returns a toplevel GtkWindow, or NULL if none */
909 get_toplevel (GtkWidget *widget)
913 toplevel = gtk_widget_get_toplevel (widget);
914 if (!gtk_widget_is_toplevel (toplevel))
917 return GTK_WINDOW (toplevel);
920 /* Shows an error dialog for the file chooser */
922 error_message (GtkFileChooserDefault *impl,
926 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
929 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
931 error_dialog (GtkFileChooserDefault *impl,
942 uri = g_file_get_uri (file);
943 text = g_strdup_printf (msg, uri);
944 error_message (impl, text, error->message);
947 g_error_free (error);
951 /* Displays an error message about not being able to get information for a file.
952 * Frees the GError as well.
955 error_getting_info_dialog (GtkFileChooserDefault *impl,
960 _("Could not retrieve information about the file"),
964 /* Shows an error dialog about not being able to add a bookmark */
966 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
971 _("Could not add a bookmark"),
975 /* Shows an error dialog about not being able to remove a bookmark */
977 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
982 _("Could not remove bookmark"),
986 /* Shows an error dialog about not being able to create a folder */
988 error_creating_folder_dialog (GtkFileChooserDefault *impl,
993 _("The folder could not be created"),
997 /* Shows an error about not being able to create a folder because a file with
998 * the same name is already there.
1001 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1006 _("The folder could not be created, as a file with the same "
1007 "name already exists. Try using a different name for the "
1008 "folder, or rename the file first."),
1013 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1019 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1020 _("You need to choose a valid filename."));
1023 _("Cannot create a file under %s as it is not a folder"),
1024 parent_file, error);
1027 /* Shows an error about not being able to select a folder because a file with
1028 * the same name is already there.
1031 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1035 _("You may only select folders. The item that you selected is not a folder; "
1036 "try using a different item."),
1040 /* Shows an error dialog about not being able to create a filename */
1042 error_building_filename_dialog (GtkFileChooserDefault *impl,
1045 error_dialog (impl, _("Invalid file name"),
1049 /* Shows an error dialog when we cannot switch to a folder */
1051 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1055 error_dialog (impl, _("The folder contents could not be displayed"),
1059 /* Changes folders, displaying an error dialog if this fails */
1061 change_folder_and_display_error (GtkFileChooserDefault *impl,
1063 gboolean clear_entry)
1068 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1070 /* We copy the path because of this case:
1072 * list_row_activated()
1073 * fetches path from model; path belongs to the model (*)
1074 * calls change_folder_and_display_error()
1075 * calls gtk_file_chooser_set_current_folder_file()
1076 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1080 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1083 error_changing_folder_dialog (impl, file, error);
1089 emit_default_size_changed (GtkFileChooserDefault *impl)
1091 profile_msg (" emit default-size-changed start", NULL);
1092 g_signal_emit_by_name (impl, "default-size-changed");
1093 profile_msg (" emit default-size-changed end", NULL);
1097 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1099 if (impl->use_preview_label)
1101 if (!impl->preview_label)
1103 impl->preview_label = gtk_label_new (impl->preview_display_name);
1104 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1105 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1106 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1107 gtk_widget_show (impl->preview_label);
1112 if (impl->preview_label)
1114 gtk_widget_destroy (impl->preview_label);
1115 impl->preview_label = NULL;
1119 if (impl->preview_widget_active && impl->preview_widget)
1120 gtk_widget_show (impl->preview_box);
1122 gtk_widget_hide (impl->preview_box);
1124 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1125 emit_default_size_changed (impl);
1129 set_preview_widget (GtkFileChooserDefault *impl,
1130 GtkWidget *preview_widget)
1132 if (preview_widget == impl->preview_widget)
1135 if (impl->preview_widget)
1136 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1137 impl->preview_widget);
1139 impl->preview_widget = preview_widget;
1140 if (impl->preview_widget)
1142 gtk_widget_show (impl->preview_widget);
1143 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1144 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1145 impl->preview_widget,
1146 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1149 update_preview_widget_visibility (impl);
1152 /* Renders a "Search" icon at an appropriate size for a tree view */
1154 render_search_icon (GtkFileChooserDefault *impl)
1156 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1160 render_recent_icon (GtkFileChooserDefault *impl)
1162 GtkIconTheme *theme;
1165 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1166 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1168 theme = gtk_icon_theme_get_default ();
1170 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1176 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1182 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1183 struct ReloadIconsData
1185 GtkFileChooserDefault *impl;
1186 GtkTreeRowReference *row_ref;
1190 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1192 const GError *error,
1198 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1199 struct ReloadIconsData *data = user_data;
1201 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1204 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1206 if (cancelled || error)
1209 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1211 path = gtk_tree_row_reference_get_path (data->row_ref);
1214 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1215 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1216 SHORTCUTS_COL_PIXBUF, pixbuf,
1218 gtk_tree_path_free (path);
1222 g_object_unref (pixbuf);
1225 gtk_tree_row_reference_free (data->row_ref);
1226 g_object_unref (data->impl);
1229 g_object_unref (cancellable);
1233 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1238 profile_start ("start", NULL);
1240 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1243 for (l = impl->reload_icon_cancellables; l; l = l->next)
1245 GCancellable *cancellable = G_CANCELLABLE (l->data);
1246 g_cancellable_cancel (cancellable);
1248 g_slist_free (impl->reload_icon_cancellables);
1249 impl->reload_icon_cancellables = NULL;
1254 ShortcutType shortcut_type;
1255 gboolean pixbuf_visible;
1258 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1259 SHORTCUTS_COL_DATA, &data,
1260 SHORTCUTS_COL_TYPE, &shortcut_type,
1261 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1267 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1269 GtkFileSystemVolume *volume;
1272 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1273 impl->icon_size, NULL);
1275 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1277 if (g_file_is_native (G_FILE (data)))
1280 struct ReloadIconsData *info;
1281 GtkTreePath *tree_path;
1282 GCancellable *cancellable;
1286 info = g_new0 (struct ReloadIconsData, 1);
1287 info->impl = g_object_ref (impl);
1288 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1289 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1290 gtk_tree_path_free (tree_path);
1292 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1294 shortcuts_reload_icons_get_info_cb,
1296 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1300 GtkIconTheme *icon_theme;
1302 /* Don't call get_info for remote paths to avoid latency and
1304 * If we switch to a better bookmarks file format (XBEL), we
1305 * should use mime info to get a better icon.
1307 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1308 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1309 impl->icon_size, 0, NULL);
1312 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1314 pixbuf = render_search_icon (impl);
1316 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1318 pixbuf = render_recent_icon (impl);
1321 gtk_list_store_set (impl->shortcuts_model, &iter,
1322 SHORTCUTS_COL_PIXBUF, pixbuf,
1326 g_object_unref (pixbuf);
1330 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1334 profile_end ("end", NULL);
1338 shortcuts_find_folder (GtkFileChooserDefault *impl,
1341 GtkTreeSelection *selection;
1345 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1347 g_assert (folder != NULL);
1348 pos = shortcut_find_position (impl, folder);
1351 gtk_tree_selection_unselect_all (selection);
1355 path = gtk_tree_path_new_from_indices (pos, -1);
1356 gtk_tree_selection_select_path (selection, path);
1357 gtk_tree_path_free (path);
1360 /* If a shortcut corresponds to the current folder, selects it */
1362 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1364 shortcuts_find_folder (impl, impl->current_folder);
1367 /* Removes the specified number of rows from the shortcuts list */
1369 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1375 path = gtk_tree_path_new_from_indices (start_row, -1);
1377 for (; n_rows; n_rows--)
1381 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1382 g_assert_not_reached ();
1384 shortcuts_free_row_data (impl, &iter);
1385 gtk_list_store_remove (impl->shortcuts_model, &iter);
1388 gtk_tree_path_free (path);
1392 shortcuts_update_count (GtkFileChooserDefault *impl,
1393 ShortcutsIndex type,
1398 case SHORTCUTS_HOME:
1400 impl->has_home = FALSE;
1402 impl->has_home = TRUE;
1405 case SHORTCUTS_DESKTOP:
1407 impl->has_desktop = FALSE;
1409 impl->has_desktop = TRUE;
1412 case SHORTCUTS_VOLUMES:
1413 impl->num_volumes += value;
1416 case SHORTCUTS_SHORTCUTS:
1417 impl->num_shortcuts += value;
1420 case SHORTCUTS_BOOKMARKS:
1421 impl->num_bookmarks += value;
1424 case SHORTCUTS_CURRENT_FOLDER:
1426 impl->shortcuts_current_folder_active = FALSE;
1428 impl->shortcuts_current_folder_active = TRUE;
1437 struct ShortcutsInsertRequest
1439 GtkFileChooserDefault *impl;
1443 GtkTreeRowReference *row_ref;
1444 ShortcutsIndex type;
1450 get_file_info_finished (GCancellable *cancellable,
1452 const GError *error,
1456 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1460 GCancellable *model_cancellable = NULL;
1461 struct ShortcutsInsertRequest *request = data;
1463 path = gtk_tree_row_reference_get_path (request->row_ref);
1465 /* Handle doesn't exist anymore in the model */
1468 pos = gtk_tree_path_get_indices (path)[0];
1469 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1471 gtk_tree_path_free (path);
1473 /* validate cancellable, else goto out */
1474 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1475 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1477 if (cancellable != model_cancellable)
1480 /* set the cancellable to NULL in the model (we unref later on) */
1481 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1482 SHORTCUTS_COL_CANCELLABLE, NULL,
1490 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1491 shortcuts_update_count (request->impl, request->type, -1);
1493 if (request->type == SHORTCUTS_HOME)
1497 home = g_file_new_for_path (g_get_home_dir ());
1498 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1499 g_object_unref (home);
1501 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1503 /* Remove the current folder separator */
1504 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1505 shortcuts_remove_rows (request->impl, separator_pos, 1);
1511 if (!request->label_copy)
1512 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1513 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1514 request->impl->icon_size);
1516 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1517 SHORTCUTS_COL_PIXBUF, pixbuf,
1518 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1519 SHORTCUTS_COL_NAME, request->label_copy,
1520 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1521 SHORTCUTS_COL_REMOVABLE, request->removable,
1524 if (request->impl->shortcuts_pane_filter_model)
1525 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1528 g_object_unref (pixbuf);
1531 g_object_unref (request->impl);
1532 g_object_unref (request->file);
1533 gtk_tree_row_reference_free (request->row_ref);
1534 g_free (request->label_copy);
1537 if (model_cancellable)
1538 g_object_unref (model_cancellable);
1541 /* FIXME: GtkFileSystem needs a function to split a remote path
1542 * into hostname and path components, or maybe just have a
1543 * gtk_file_system_path_get_display_name().
1545 * This function is also used in gtkfilechooserbutton.c
1548 _gtk_file_chooser_label_for_file (GFile *file)
1550 const gchar *path, *start, *end, *p;
1551 gchar *uri, *host, *label;
1553 uri = g_file_get_uri (file);
1555 start = strstr (uri, "://");
1559 path = strchr (start, '/');
1564 end = uri + strlen (uri);
1568 /* strip username */
1569 p = strchr (start, '@');
1573 p = strchr (start, ':');
1577 host = g_strndup (start, end - start);
1579 /* Translators: the first string is a path and the second string
1580 * is a hostname. Nautilus and the panel contain the same string
1583 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1589 label = g_strdup (uri);
1597 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1598 * inserts a volume. A position of -1 indicates the end of the tree.
1601 shortcuts_insert_file (GtkFileChooserDefault *impl,
1603 ShortcutType shortcut_type,
1604 GtkFileSystemVolume *volume,
1608 ShortcutsIndex type)
1611 GdkPixbuf *pixbuf = NULL;
1612 gpointer data = NULL;
1614 GtkIconTheme *icon_theme;
1616 profile_start ("start shortcut", NULL);
1618 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1621 label_copy = _gtk_file_system_volume_get_display_name (volume);
1622 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1623 impl->icon_size, NULL);
1625 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1627 if (g_file_is_native (file))
1629 struct ShortcutsInsertRequest *request;
1630 GCancellable *cancellable;
1633 request = g_new0 (struct ShortcutsInsertRequest, 1);
1634 request->impl = g_object_ref (impl);
1635 request->file = g_object_ref (file);
1636 request->name_only = TRUE;
1637 request->removable = removable;
1639 request->type = type;
1641 request->label_copy = g_strdup (label);
1644 gtk_list_store_append (impl->shortcuts_model, &iter);
1646 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1648 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1649 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1650 gtk_tree_path_free (p);
1652 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1653 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1654 get_file_info_finished, request);
1656 gtk_list_store_set (impl->shortcuts_model, &iter,
1657 SHORTCUTS_COL_DATA, g_object_ref (file),
1658 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1659 SHORTCUTS_COL_CANCELLABLE, cancellable,
1662 shortcuts_update_count (impl, type, 1);
1668 /* Don't call get_info for remote paths to avoid latency and
1671 data = g_object_ref (file);
1673 label_copy = g_strdup (label);
1675 label_copy = _gtk_file_chooser_label_for_file (file);
1677 /* If we switch to a better bookmarks file format (XBEL), we
1678 * should use mime info to get a better icon.
1680 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1681 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1682 impl->icon_size, 0, NULL);
1687 g_assert_not_reached ();
1693 gtk_list_store_append (impl->shortcuts_model, &iter);
1695 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1697 shortcuts_update_count (impl, type, 1);
1699 gtk_list_store_set (impl->shortcuts_model, &iter,
1700 SHORTCUTS_COL_PIXBUF, pixbuf,
1701 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1702 SHORTCUTS_COL_NAME, label_copy,
1703 SHORTCUTS_COL_DATA, data,
1704 SHORTCUTS_COL_TYPE, shortcut_type,
1705 SHORTCUTS_COL_REMOVABLE, removable,
1706 SHORTCUTS_COL_CANCELLABLE, NULL,
1709 if (impl->shortcuts_pane_filter_model)
1710 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1712 g_free (label_copy);
1715 g_object_unref (pixbuf);
1717 profile_end ("end", NULL);
1721 shortcuts_append_search (GtkFileChooserDefault *impl)
1726 pixbuf = render_search_icon (impl);
1728 gtk_list_store_append (impl->shortcuts_model, &iter);
1729 gtk_list_store_set (impl->shortcuts_model, &iter,
1730 SHORTCUTS_COL_PIXBUF, pixbuf,
1731 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1732 SHORTCUTS_COL_NAME, _("Search"),
1733 SHORTCUTS_COL_DATA, NULL,
1734 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1735 SHORTCUTS_COL_REMOVABLE, FALSE,
1739 g_object_unref (pixbuf);
1741 impl->has_search = TRUE;
1745 shortcuts_append_recent (GtkFileChooserDefault *impl)
1750 pixbuf = render_recent_icon (impl);
1752 gtk_list_store_append (impl->shortcuts_model, &iter);
1753 gtk_list_store_set (impl->shortcuts_model, &iter,
1754 SHORTCUTS_COL_PIXBUF, pixbuf,
1755 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1756 SHORTCUTS_COL_NAME, _("Recently Used"),
1757 SHORTCUTS_COL_DATA, NULL,
1758 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1759 SHORTCUTS_COL_REMOVABLE, FALSE,
1763 g_object_unref (pixbuf);
1766 /* Appends an item for the user's home directory to the shortcuts model */
1768 shortcuts_append_home (GtkFileChooserDefault *impl)
1770 const char *home_path;
1773 profile_start ("start", NULL);
1775 home_path = g_get_home_dir ();
1776 if (home_path == NULL)
1778 profile_end ("end - no home directory!?", NULL);
1782 home = g_file_new_for_path (home_path);
1783 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1784 impl->has_home = TRUE;
1786 g_object_unref (home);
1788 profile_end ("end", NULL);
1791 /* Appends the ~/Desktop directory to the shortcuts model */
1793 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1798 profile_start ("start", NULL);
1800 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1801 /* "To disable a directory, point it to the homedir."
1802 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1804 if (!g_strcmp0 (name, g_get_home_dir ()))
1806 profile_end ("end", NULL);
1810 file = g_file_new_for_path (name);
1811 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1812 impl->has_desktop = TRUE;
1814 /* We do not actually pop up an error dialog if there is no desktop directory
1815 * because some people may really not want to have one.
1818 g_object_unref (file);
1820 profile_end ("end", NULL);
1823 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1825 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1832 profile_start ("start", NULL);
1834 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1837 for (; bookmarks; bookmarks = bookmarks->next)
1841 file = bookmarks->data;
1843 if (impl->local_only && !g_file_is_native (file))
1846 if (shortcut_find_position (impl, file) != -1)
1849 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1851 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1857 profile_end ("end", NULL);
1859 return num_inserted;
1862 /* Returns the index for the corresponding item in the shortcuts bar */
1864 shortcuts_get_index (GtkFileChooserDefault *impl,
1865 ShortcutsIndex where)
1871 if (where == SHORTCUTS_SEARCH)
1874 n += impl->has_search ? 1 : 0;
1876 if (where == SHORTCUTS_RECENT)
1879 n += 1; /* we always have the recently-used item */
1881 if (where == SHORTCUTS_RECENT_SEPARATOR)
1884 n += 1; /* we always have the separator after the recently-used item */
1886 if (where == SHORTCUTS_HOME)
1889 n += impl->has_home ? 1 : 0;
1891 if (where == SHORTCUTS_DESKTOP)
1894 n += impl->has_desktop ? 1 : 0;
1896 if (where == SHORTCUTS_VOLUMES)
1899 n += impl->num_volumes;
1901 if (where == SHORTCUTS_SHORTCUTS)
1904 n += impl->num_shortcuts;
1906 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1909 /* If there are no bookmarks there won't be a separator */
1910 n += (impl->num_bookmarks > 0) ? 1 : 0;
1912 if (where == SHORTCUTS_BOOKMARKS)
1915 n += impl->num_bookmarks;
1917 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1922 if (where == SHORTCUTS_CURRENT_FOLDER)
1925 g_assert_not_reached ();
1932 /* Adds all the file system volumes to the shortcuts model */
1934 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1939 gboolean old_changing_folders;
1941 profile_start ("start", NULL);
1943 old_changing_folders = impl->changing_folder;
1944 impl->changing_folder = TRUE;
1946 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1947 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1948 impl->num_volumes = 0;
1950 list = _gtk_file_system_list_volumes (impl->file_system);
1954 for (l = list; l; l = l->next)
1956 GtkFileSystemVolume *volume;
1960 if (impl->local_only)
1962 if (_gtk_file_system_volume_is_mounted (volume))
1965 gboolean base_is_native = TRUE;
1967 base_file = _gtk_file_system_volume_get_root (volume);
1968 if (base_file != NULL)
1970 base_is_native = g_file_is_native (base_file);
1971 g_object_unref (base_file);
1974 if (!base_is_native)
1979 shortcuts_insert_file (impl,
1981 SHORTCUT_TYPE_VOLUME,
1982 _gtk_file_system_volume_ref (volume),
1990 impl->num_volumes = n;
1991 g_slist_free (list);
1993 if (impl->shortcuts_pane_filter_model)
1994 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1996 impl->changing_folder = old_changing_folders;
1998 profile_end ("end", NULL);
2001 /* Inserts a separator node in the shortcuts list */
2003 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2004 ShortcutsIndex where)
2008 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2009 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2010 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2012 gtk_list_store_insert (impl->shortcuts_model, &iter,
2013 shortcuts_get_index (impl, where));
2014 gtk_list_store_set (impl->shortcuts_model, &iter,
2015 SHORTCUTS_COL_PIXBUF, NULL,
2016 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2017 SHORTCUTS_COL_NAME, NULL,
2018 SHORTCUTS_COL_DATA, NULL,
2019 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2023 /* Updates the list of bookmarks */
2025 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2028 gboolean old_changing_folders;
2030 GFile *list_selected = NULL;
2031 ShortcutType shortcut_type;
2034 profile_start ("start", NULL);
2036 old_changing_folders = impl->changing_folder;
2037 impl->changing_folder = TRUE;
2039 if (shortcuts_get_selected (impl, &iter))
2041 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2043 SHORTCUTS_COL_DATA, &col_data,
2044 SHORTCUTS_COL_TYPE, &shortcut_type,
2047 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2048 list_selected = g_object_ref (col_data);
2051 if (impl->num_bookmarks > 0)
2052 shortcuts_remove_rows (impl,
2053 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2054 impl->num_bookmarks + 1);
2056 impl->num_bookmarks = 0;
2057 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2059 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2060 shortcuts_append_bookmarks (impl, bookmarks);
2061 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2062 g_slist_free (bookmarks);
2064 if (impl->num_bookmarks == 0)
2065 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2067 if (impl->shortcuts_pane_filter_model)
2068 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2072 shortcuts_find_folder (impl, list_selected);
2073 g_object_unref (list_selected);
2076 impl->changing_folder = old_changing_folders;
2078 profile_end ("end", NULL);
2081 /* Appends a separator and a row to the shortcuts list for the current folder */
2083 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2087 g_assert (!impl->shortcuts_current_folder_active);
2089 g_assert (impl->current_folder != NULL);
2091 pos = shortcut_find_position (impl, impl->current_folder);
2094 GtkFileSystemVolume *volume;
2098 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2101 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2103 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2105 base_file = _gtk_file_system_volume_get_root (volume);
2109 if (base_file && g_file_equal (base_file, impl->current_folder))
2110 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2112 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2115 g_object_unref (base_file);
2119 /* Updates the current folder row in the shortcuts model */
2121 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2125 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2127 if (impl->shortcuts_current_folder_active)
2129 shortcuts_remove_rows (impl, pos, 2);
2130 impl->shortcuts_current_folder_active = FALSE;
2133 shortcuts_add_current_folder (impl);
2136 /* Filter function used for the shortcuts filter model */
2138 shortcuts_pane_filter_cb (GtkTreeModel *model,
2142 GtkFileChooserDefault *impl;
2146 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2148 path = gtk_tree_model_get_path (model, iter);
2152 pos = *gtk_tree_path_get_indices (path);
2153 gtk_tree_path_free (path);
2155 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2158 /* Creates the list model for shortcuts */
2160 shortcuts_model_create (GtkFileChooserDefault *impl)
2162 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2163 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2164 GDK_TYPE_PIXBUF, /* pixbuf */
2165 G_TYPE_STRING, /* name */
2166 G_TYPE_POINTER, /* path or volume */
2167 G_TYPE_INT, /* ShortcutType */
2168 G_TYPE_BOOLEAN, /* removable */
2169 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2170 G_TYPE_POINTER); /* GCancellable */
2172 shortcuts_append_search (impl);
2174 if (impl->recent_manager)
2176 shortcuts_append_recent (impl);
2177 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2180 if (impl->file_system)
2182 shortcuts_append_home (impl);
2183 shortcuts_append_desktop (impl);
2184 shortcuts_add_volumes (impl);
2187 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2188 GTK_TREE_MODEL (impl->shortcuts_model),
2191 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2192 shortcuts_pane_filter_cb,
2197 /* Callback used when the "New Folder" button is clicked */
2199 new_folder_button_clicked (GtkButton *button,
2200 GtkFileChooserDefault *impl)
2205 if (!impl->browse_files_model)
2206 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2208 /* Prevent button from being clicked twice */
2209 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2211 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2213 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2214 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2215 path, impl->list_name_column,
2218 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2219 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2221 impl->list_name_column,
2224 gtk_tree_path_free (path);
2228 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2232 source = g_idle_source_new ();
2233 g_source_set_closure (source,
2234 g_cclosure_new_object (callback, G_OBJECT (impl)));
2235 g_source_attach (source, NULL);
2240 /* Idle handler for creating a new folder after editing its name cell, or for
2241 * canceling the editing.
2244 edited_idle_cb (GtkFileChooserDefault *impl)
2246 GDK_THREADS_ENTER ();
2248 g_source_destroy (impl->edited_idle);
2249 impl->edited_idle = NULL;
2251 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2252 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2254 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2256 if (impl->edited_new_text /* not cancelled? */
2257 && (strlen (impl->edited_new_text) != 0)
2258 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2260 GError *error = NULL;
2263 file = g_file_get_child_for_display_name (impl->current_folder,
2264 impl->edited_new_text,
2268 GError *error = NULL;
2270 if (g_file_make_directory (file, NULL, &error))
2271 change_folder_and_display_error (impl, file, FALSE);
2273 error_creating_folder_dialog (impl, file, error);
2275 g_object_unref (file);
2278 error_creating_folder_dialog (impl, file, error);
2280 g_free (impl->edited_new_text);
2281 impl->edited_new_text = NULL;
2284 GDK_THREADS_LEAVE ();
2290 queue_edited_idle (GtkFileChooserDefault *impl,
2291 const gchar *new_text)
2293 /* We create the folder in an idle handler so that we don't modify the tree
2297 if (!impl->edited_idle)
2298 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2300 g_free (impl->edited_new_text);
2301 impl->edited_new_text = g_strdup (new_text);
2304 /* Callback used from the text cell renderer when the new folder is named */
2306 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2308 const gchar *new_text,
2309 GtkFileChooserDefault *impl)
2311 /* work around bug #154921 */
2312 g_object_set (cell_renderer_text,
2313 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2314 queue_edited_idle (impl, new_text);
2317 /* Callback used from the text cell renderer when the new folder edition gets
2321 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2322 GtkFileChooserDefault *impl)
2324 /* work around bug #154921 */
2325 g_object_set (cell_renderer_text,
2326 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2327 queue_edited_idle (impl, NULL);
2330 /* Creates the widgets for the filter combo box */
2332 filter_create (GtkFileChooserDefault *impl)
2334 GtkCellRenderer *cell;
2337 impl->filter_combo = gtk_combo_box_text_new ();
2338 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2340 /* Get the combo's text renderer and set ellipsize parameters */
2341 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2345 g_object_set (G_OBJECT (cell),
2346 "ellipsize", PANGO_ELLIPSIZE_END,
2349 g_list_free (cells);
2351 g_signal_connect (impl->filter_combo, "changed",
2352 G_CALLBACK (filter_combo_changed), impl);
2354 gtk_widget_set_tooltip_text (impl->filter_combo,
2355 _("Select which types of files are shown"));
2357 return impl->filter_combo;
2361 toolbutton_new (GtkFileChooserDefault *impl,
2370 item = gtk_tool_button_new (NULL, NULL);
2371 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2372 gtk_widget_show (image);
2373 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2375 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2376 g_signal_connect (item, "clicked", callback, impl);
2379 gtk_widget_show (GTK_WIDGET (item));
2381 return GTK_WIDGET (item);
2384 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2386 shortcut_find_position (GtkFileChooserDefault *impl,
2391 int current_folder_separator_idx;
2393 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2396 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2399 /* FIXME: is this still needed? */
2400 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2404 for (i = 0; i < current_folder_separator_idx; i++)
2407 ShortcutType shortcut_type;
2409 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2410 SHORTCUTS_COL_DATA, &col_data,
2411 SHORTCUTS_COL_TYPE, &shortcut_type,
2416 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2418 GtkFileSystemVolume *volume;
2423 base_file = _gtk_file_system_volume_get_root (volume);
2425 exists = base_file && g_file_equal (file, base_file);
2428 g_object_unref (base_file);
2433 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2437 model_file = col_data;
2439 if (model_file && g_file_equal (model_file, file))
2444 if (i < current_folder_separator_idx - 1)
2446 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2447 g_assert_not_reached ();
2454 /* Tries to add a bookmark from a path name */
2456 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2462 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2464 if (shortcut_find_position (impl, file) != -1)
2468 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2470 error_adding_bookmark_dialog (impl, file, error);
2478 add_bookmark_foreach_cb (GtkTreeModel *model,
2483 GtkFileChooserDefault *impl;
2486 impl = (GtkFileChooserDefault *) data;
2488 gtk_tree_model_get (model, iter,
2489 MODEL_COL_FILE, &file,
2492 shortcuts_add_bookmark_from_file (impl, file, -1);
2494 g_object_unref (file);
2497 /* Adds a bookmark from the currently selected item in the file list */
2499 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2501 GtkTreeSelection *selection;
2503 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2505 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2506 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2508 gtk_tree_selection_selected_foreach (selection,
2509 add_bookmark_foreach_cb,
2513 /* Callback used when the "Add bookmark" button is clicked */
2515 add_bookmark_button_clicked_cb (GtkButton *button,
2516 GtkFileChooserDefault *impl)
2518 bookmarks_add_selected_folder (impl);
2521 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2522 * returns FALSE if no shortcut is selected.
2525 shortcuts_get_selected (GtkFileChooserDefault *impl,
2528 GtkTreeSelection *selection;
2529 GtkTreeIter parent_iter;
2531 if (!impl->browse_shortcuts_tree_view)
2534 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2536 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2539 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2545 /* Removes the selected bookmarks */
2547 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2555 if (!shortcuts_get_selected (impl, &iter))
2558 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2559 SHORTCUTS_COL_DATA, &col_data,
2560 SHORTCUTS_COL_REMOVABLE, &removable,
2566 g_assert (col_data != NULL);
2571 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2572 error_removing_bookmark_dialog (impl, file, error);
2575 /* Callback used when the "Remove bookmark" button is clicked */
2577 remove_bookmark_button_clicked_cb (GtkButton *button,
2578 GtkFileChooserDefault *impl)
2580 remove_selected_bookmarks (impl);
2583 struct selection_check_closure {
2584 GtkFileChooserDefault *impl;
2587 gboolean all_folders;
2590 /* Used from gtk_tree_selection_selected_foreach() */
2592 selection_check_foreach_cb (GtkTreeModel *model,
2597 struct selection_check_closure *closure;
2601 gtk_tree_model_get (model, iter,
2602 MODEL_COL_FILE, &file,
2603 MODEL_COL_IS_FOLDER, &is_folder,
2609 g_object_unref (file);
2612 closure->num_selected++;
2614 closure->all_folders = closure->all_folders && is_folder;
2615 closure->all_files = closure->all_files && !is_folder;
2618 /* Checks whether the selected items in the file list are all files or all folders */
2620 selection_check (GtkFileChooserDefault *impl,
2622 gboolean *all_files,
2623 gboolean *all_folders)
2625 struct selection_check_closure closure;
2626 GtkTreeSelection *selection;
2628 closure.impl = impl;
2629 closure.num_selected = 0;
2630 closure.all_files = TRUE;
2631 closure.all_folders = TRUE;
2633 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2634 gtk_tree_selection_selected_foreach (selection,
2635 selection_check_foreach_cb,
2638 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2641 *num_selected = closure.num_selected;
2644 *all_files = closure.all_files;
2647 *all_folders = closure.all_folders;
2650 struct get_selected_file_closure {
2651 GtkFileChooserDefault *impl;
2656 get_selected_file_foreach_cb (GtkTreeModel *model,
2661 struct get_selected_file_closure *closure = data;
2665 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2666 g_object_unref (closure->file);
2667 closure->file = NULL;
2670 gtk_tree_model_get (model, iter,
2671 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2675 /* Returns a selected path from the file list */
2677 get_selected_file (GtkFileChooserDefault *impl)
2679 struct get_selected_file_closure closure;
2680 GtkTreeSelection *selection;
2682 closure.impl = impl;
2683 closure.file = NULL;
2685 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2686 gtk_tree_selection_selected_foreach (selection,
2687 get_selected_file_foreach_cb,
2690 return closure.file;
2694 GtkFileChooserDefault *impl;
2696 } UpdateTooltipData;
2699 update_tooltip (GtkTreeModel *model,
2704 UpdateTooltipData *udata = data;
2706 if (udata->tip == NULL)
2708 gchar *display_name;
2710 gtk_tree_model_get (model, iter,
2711 MODEL_COL_NAME, &display_name,
2714 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2716 g_free (display_name);
2721 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2722 * if there are no selected items *and* the current folder is not in the
2723 * bookmarks list. De-sensitize the button otherwise.
2726 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2729 gboolean all_folders;
2733 selection_check (impl, &num_selected, NULL, &all_folders);
2735 if (num_selected == 0)
2736 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2737 else if (num_selected == 1)
2741 file = get_selected_file (impl);
2742 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2744 g_object_unref (file);
2747 active = all_folders;
2749 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2751 if (impl->browse_files_popup_menu_add_shortcut_item)
2752 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2753 (num_selected == 0) ? FALSE : active);
2757 if (num_selected == 0)
2758 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2759 else if (num_selected > 1)
2760 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2763 GtkTreeSelection *selection;
2764 UpdateTooltipData data;
2766 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2769 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2773 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2778 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2779 * bookmark row is selected in the shortcuts tree.
2782 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2785 gboolean removable = FALSE;
2789 if (shortcuts_get_selected (impl, &iter))
2791 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2792 SHORTCUTS_COL_REMOVABLE, &removable,
2793 SHORTCUTS_COL_NAME, &name,
2795 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2798 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2800 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2802 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2806 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2807 _("Remove the selected bookmark"));
2812 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2815 gboolean removable = FALSE;
2817 if (impl->browse_shortcuts_popup_menu == NULL)
2820 if (shortcuts_get_selected (impl, &iter))
2821 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2822 SHORTCUTS_COL_REMOVABLE, &removable,
2825 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2826 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2829 /* GtkWidget::drag-begin handler for the shortcuts list. */
2831 shortcuts_drag_begin_cb (GtkWidget *widget,
2832 GdkDragContext *context,
2833 GtkFileChooserDefault *impl)
2836 impl->shortcuts_drag_context = g_object_ref (context);
2841 /* Removes the idle handler for outside drags */
2843 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2845 if (!impl->shortcuts_drag_outside_idle)
2848 g_source_destroy (impl->shortcuts_drag_outside_idle);
2849 impl->shortcuts_drag_outside_idle = NULL;
2853 /* GtkWidget::drag-end handler for the shortcuts list. */
2855 shortcuts_drag_end_cb (GtkWidget *widget,
2856 GdkDragContext *context,
2857 GtkFileChooserDefault *impl)
2860 g_object_unref (impl->shortcuts_drag_context);
2862 shortcuts_cancel_drag_outside_idle (impl);
2864 if (!impl->shortcuts_drag_outside)
2867 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2869 impl->shortcuts_drag_outside = FALSE;
2873 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2875 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2876 GdkDragContext *context,
2877 GtkFileChooserDefault *impl)
2879 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2882 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2886 shortcuts_drag_leave_cb (GtkWidget *widget,
2887 GdkDragContext *context,
2889 GtkFileChooserDefault *impl)
2892 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2894 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2898 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2900 GTK_TREE_VIEW_DROP_BEFORE);
2902 g_signal_stop_emission_by_name (widget, "drag-leave");
2905 /* Computes the appropriate row and position for dropping */
2907 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2911 GtkTreeViewDropPosition *pos)
2913 GtkTreeView *tree_view;
2914 GtkTreeViewColumn *column;
2918 int bookmarks_index;
2919 int header_height = 0;
2921 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2923 if (gtk_tree_view_get_headers_visible (tree_view))
2924 header_height = _gtk_tree_view_get_header_height (tree_view);
2926 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2928 if (!gtk_tree_view_get_path_at_pos (tree_view,
2936 row = bookmarks_index + impl->num_bookmarks - 1;
2937 *path = gtk_tree_path_new_from_indices (row, -1);
2938 *pos = GTK_TREE_VIEW_DROP_AFTER;
2942 row = *gtk_tree_path_get_indices (*path);
2943 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2944 gtk_tree_path_free (*path);
2946 if (row < bookmarks_index)
2948 row = bookmarks_index;
2949 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2951 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2953 row = bookmarks_index + impl->num_bookmarks - 1;
2954 *pos = GTK_TREE_VIEW_DROP_AFTER;
2958 if (cell_y < cell.height / 2)
2959 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2961 *pos = GTK_TREE_VIEW_DROP_AFTER;
2964 *path = gtk_tree_path_new_from_indices (row, -1);
2967 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2968 * implement the destination side of DnD by hand, due to limitations in
2969 * GtkTreeView's DnD API.
2972 shortcuts_drag_motion_cb (GtkWidget *widget,
2973 GdkDragContext *context,
2977 GtkFileChooserDefault *impl)
2980 GtkTreeViewDropPosition pos;
2981 GdkDragAction action;
2984 if (gtk_drag_get_source_widget (context) == widget)
2986 shortcuts_cancel_drag_outside_idle (impl);
2988 if (impl->shortcuts_drag_outside)
2990 shortcuts_drag_set_delete_cursor (impl, FALSE);
2991 impl->shortcuts_drag_outside = FALSE;
2996 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
2997 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
2998 action = GDK_ACTION_COPY;
2999 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3000 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3001 action = GDK_ACTION_MOVE;
3008 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3009 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3010 gtk_tree_path_free (path);
3014 g_signal_stop_emission_by_name (widget, "drag-motion");
3018 gdk_drag_status (context, action, time_);
3025 /* GtkWidget::drag-drop handler for the shortcuts list. */
3027 shortcuts_drag_drop_cb (GtkWidget *widget,
3028 GdkDragContext *context,
3032 GtkFileChooserDefault *impl)
3035 shortcuts_cancel_drag_outside_idle (impl);
3038 g_signal_stop_emission_by_name (widget, "drag-drop");
3042 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3044 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3045 GtkSelectionData *selection_data,
3051 uris = gtk_selection_data_get_uris (selection_data);
3055 for (i = 0; uris[i]; i++)
3061 file = g_file_new_for_uri (uri);
3063 if (shortcuts_add_bookmark_from_file (impl, file, position))
3066 g_object_unref (file);
3072 /* Reorders the selected bookmark to the specified position */
3074 shortcuts_reorder (GtkFileChooserDefault *impl,
3079 ShortcutType shortcut_type;
3082 int bookmarks_index;
3087 /* Get the selected path */
3089 if (!shortcuts_get_selected (impl, &iter))
3090 g_assert_not_reached ();
3092 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3093 old_position = *gtk_tree_path_get_indices (path);
3094 gtk_tree_path_free (path);
3096 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3097 old_position -= bookmarks_index;
3098 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3100 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3101 SHORTCUTS_COL_NAME, &name,
3102 SHORTCUTS_COL_DATA, &col_data,
3103 SHORTCUTS_COL_TYPE, &shortcut_type,
3105 g_assert (col_data != NULL);
3106 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3109 g_object_ref (file); /* removal below will free file, so we need a new ref */
3111 /* Remove the path from the old position and insert it in the new one */
3113 if (new_position > old_position)
3116 if (old_position == new_position)
3120 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3122 shortcuts_add_bookmark_from_file (impl, file, new_position);
3123 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3126 error_adding_bookmark_dialog (impl, file, error);
3130 g_object_unref (file);
3134 /* Callback used when we get the drag data for the bookmarks list. We add the
3135 * received URIs as bookmarks if they are folders.
3138 shortcuts_drag_data_received_cb (GtkWidget *widget,
3139 GdkDragContext *context,
3142 GtkSelectionData *selection_data,
3147 GtkFileChooserDefault *impl;
3148 GtkTreePath *tree_path;
3149 GtkTreeViewDropPosition tree_pos;
3152 int bookmarks_index;
3154 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3156 /* Compute position */
3158 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3160 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3161 position = *gtk_tree_path_get_indices (tree_path);
3162 gtk_tree_path_free (tree_path);
3164 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3167 g_assert (position >= bookmarks_index);
3168 position -= bookmarks_index;
3170 target = gtk_selection_data_get_target (selection_data);
3172 if (gtk_targets_include_uri (&target, 1))
3173 shortcuts_drop_uris (impl, selection_data, position);
3174 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3175 shortcuts_reorder (impl, position);
3177 g_signal_stop_emission_by_name (widget, "drag-data-received");
3180 /* Callback used to display a tooltip in the shortcuts tree */
3182 shortcuts_query_tooltip_cb (GtkWidget *widget,
3185 gboolean keyboard_mode,
3186 GtkTooltip *tooltip,
3187 GtkFileChooserDefault *impl)
3189 GtkTreeModel *model;
3192 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3200 ShortcutType shortcut_type;
3202 gtk_tree_model_get (model, &iter,
3203 SHORTCUTS_COL_DATA, &col_data,
3204 SHORTCUTS_COL_TYPE, &shortcut_type,
3207 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3209 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3211 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3216 file = G_FILE (col_data);
3217 parse_name = g_file_get_parse_name (file);
3219 gtk_tooltip_set_text (tooltip, parse_name);
3221 g_free (parse_name);
3225 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3229 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3239 /* Callback used when the selection in the shortcuts tree changes */
3241 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3242 GtkFileChooserDefault *impl)
3245 GtkTreeIter child_iter;
3247 bookmarks_check_remove_sensitivity (impl);
3248 shortcuts_check_popup_sensitivity (impl);
3250 if (impl->changing_folder)
3253 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3255 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3258 shortcuts_activate_iter (impl, &child_iter);
3263 shortcuts_row_separator_func (GtkTreeModel *model,
3267 ShortcutType shortcut_type;
3269 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3271 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3275 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3277 GtkFileChooserDefault *impl)
3281 /* don't screw up focus switching with Tab */
3282 if (event->keyval == GDK_KEY_Tab
3283 || event->keyval == GDK_KEY_KP_Tab
3284 || event->keyval == GDK_KEY_ISO_Left_Tab
3285 || event->length < 1)
3288 if (impl->location_entry)
3289 entry = impl->location_entry;
3290 else if (impl->search_entry)
3291 entry = impl->search_entry;
3297 gtk_widget_grab_focus (entry);
3298 return gtk_widget_event (entry, (GdkEvent *) event);
3304 /* Callback used when the file list's popup menu is detached */
3306 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3309 GtkFileChooserDefault *impl;
3311 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3312 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3314 impl->browse_shortcuts_popup_menu = NULL;
3315 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3316 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3320 remove_shortcut_cb (GtkMenuItem *item,
3321 GtkFileChooserDefault *impl)
3323 remove_selected_bookmarks (impl);
3326 /* Rename the selected bookmark */
3328 rename_selected_bookmark (GtkFileChooserDefault *impl)
3332 GtkTreeViewColumn *column;
3333 GtkCellRenderer *cell;
3336 if (shortcuts_get_selected (impl, &iter))
3338 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3339 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3340 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3341 cell = g_list_nth_data (renderers, 1);
3342 g_list_free (renderers);
3343 g_object_set (cell, "editable", TRUE, NULL);
3344 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3345 path, column, cell, TRUE);
3346 gtk_tree_path_free (path);
3351 rename_shortcut_cb (GtkMenuItem *item,
3352 GtkFileChooserDefault *impl)
3354 rename_selected_bookmark (impl);
3357 /* Constructs the popup menu for the file list if needed */
3359 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3363 if (impl->browse_shortcuts_popup_menu)
3366 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3367 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3368 impl->browse_shortcuts_tree_view,
3369 shortcuts_popup_menu_detach_cb);
3371 item = gtk_image_menu_item_new_with_label (_("Remove"));
3372 impl->browse_shortcuts_popup_menu_remove_item = item;
3373 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3374 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3375 g_signal_connect (item, "activate",
3376 G_CALLBACK (remove_shortcut_cb), impl);
3377 gtk_widget_show (item);
3378 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3380 item = gtk_menu_item_new_with_label (_("Rename..."));
3381 impl->browse_shortcuts_popup_menu_rename_item = item;
3382 g_signal_connect (item, "activate",
3383 G_CALLBACK (rename_shortcut_cb), impl);
3384 gtk_widget_show (item);
3385 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3389 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3391 shortcuts_build_popup_menu (impl);
3392 shortcuts_check_popup_sensitivity (impl);
3396 popup_position_func (GtkMenu *menu,
3400 gpointer user_data);
3403 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3404 GdkEventButton *event)
3406 shortcuts_update_popup_menu (impl);
3408 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3409 NULL, NULL, NULL, NULL,
3410 event->button, event->time);
3413 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3415 popup_position_func, impl->browse_shortcuts_tree_view,
3416 0, GDK_CURRENT_TIME);
3417 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3422 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3424 shortcuts_popup_menu_cb (GtkWidget *widget,
3425 GtkFileChooserDefault *impl)
3427 shortcuts_popup_menu (impl, NULL);
3431 /* Callback used when a button is pressed on the shortcuts list.
3432 * We trap button 3 to bring up a popup menu.
3435 shortcuts_button_press_event_cb (GtkWidget *widget,
3436 GdkEventButton *event,
3437 GtkFileChooserDefault *impl)
3439 static gboolean in_press = FALSE;
3445 if (event->button != 3)
3449 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3455 shortcuts_popup_menu (impl, event);
3460 shortcuts_edited (GtkCellRenderer *cell,
3463 GtkFileChooserDefault *impl)
3469 g_object_set (cell, "editable", FALSE, NULL);
3471 path = gtk_tree_path_new_from_string (path_string);
3472 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3473 g_assert_not_reached ();
3475 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3476 SHORTCUTS_COL_DATA, &shortcut,
3478 gtk_tree_path_free (path);
3480 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3484 shortcuts_editing_canceled (GtkCellRenderer *cell,
3485 GtkFileChooserDefault *impl)
3487 g_object_set (cell, "editable", FALSE, NULL);
3490 /* Creates the widgets for the shortcuts and bookmarks tree */
3492 shortcuts_list_create (GtkFileChooserDefault *impl)
3495 GtkTreeSelection *selection;
3496 GtkTreeViewColumn *column;
3497 GtkCellRenderer *renderer;
3499 /* Target types for dragging a row to/from the shortcuts list */
3500 const GtkTargetEntry tree_model_row_targets[] = {
3501 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3504 /* Scrolled window */
3506 swin = gtk_scrolled_window_new (NULL, NULL);
3507 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3508 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3509 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3511 gtk_widget_show (swin);
3514 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3515 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3516 GTK_STYLE_CLASS_SIDEBAR);
3517 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3518 #ifdef PROFILE_FILE_CHOOSER
3519 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3522 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3524 * 1. user brings up a SAVE dialog
3525 * 2. user clicks on a shortcut in the shortcuts pane
3526 * 3. user starts typing a filename
3528 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3529 * support interactive search. However, we'd rather focus the location entry
3530 * so that the user can type *there*.
3532 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3533 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3534 * if the user starts typing while the focus is in the shortcuts pane.
3536 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3537 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3539 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3540 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3541 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3542 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3543 /* Accessible object name for the file chooser's shortcuts pane */
3544 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3546 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3548 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3550 tree_model_row_targets,
3551 G_N_ELEMENTS (tree_model_row_targets),
3554 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3555 GTK_DEST_DEFAULT_ALL,
3556 tree_model_row_targets,
3557 G_N_ELEMENTS (tree_model_row_targets),
3558 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3559 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3561 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3562 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3563 gtk_tree_selection_set_select_function (selection,
3564 shortcuts_select_func,
3567 g_signal_connect (selection, "changed",
3568 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3570 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3571 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3573 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3574 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3575 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3576 G_CALLBACK (shortcuts_drag_end_cb), impl);
3577 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3578 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3580 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3581 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3582 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3583 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3584 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3585 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3586 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3587 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3589 /* Support tooltips */
3590 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3591 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3592 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3594 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3595 gtk_widget_show (impl->browse_shortcuts_tree_view);
3599 column = gtk_tree_view_column_new ();
3600 /* Column header for the file chooser's shortcuts pane */
3601 gtk_tree_view_column_set_title (column, _("_Places"));
3603 renderer = gtk_cell_renderer_pixbuf_new ();
3604 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3605 gtk_tree_view_column_set_attributes (column, renderer,
3606 "pixbuf", SHORTCUTS_COL_PIXBUF,
3607 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3610 renderer = gtk_cell_renderer_text_new ();
3611 g_object_set (renderer,
3613 "ellipsize", PANGO_ELLIPSIZE_END,
3615 g_signal_connect (renderer, "edited",
3616 G_CALLBACK (shortcuts_edited), impl);
3617 g_signal_connect (renderer, "editing-canceled",
3618 G_CALLBACK (shortcuts_editing_canceled), impl);
3619 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3620 gtk_tree_view_column_set_attributes (column, renderer,
3621 "text", SHORTCUTS_COL_NAME,
3624 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3625 shortcuts_row_separator_func,
3628 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3633 /* Creates the widgets for the shortcuts/bookmarks pane */
3635 shortcuts_pane_create (GtkFileChooserDefault *impl,
3636 GtkSizeGroup *size_group)
3641 GtkStyleContext *context;
3644 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3645 gtk_widget_show (vbox);
3647 /* Shortcuts tree */
3649 widget = shortcuts_list_create (impl);
3651 gtk_size_group_add_widget (size_group, widget);
3652 context = gtk_widget_get_style_context (widget);
3653 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3655 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3657 /* Box for buttons */
3659 toolbar = gtk_toolbar_new ();
3660 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3662 context = gtk_widget_get_style_context (toolbar);
3663 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3664 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3666 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3667 gtk_widget_show (toolbar);
3669 /* Add bookmark button */
3670 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3671 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3675 G_CALLBACK (add_bookmark_button_clicked_cb));
3676 g_object_unref (icon);
3678 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3679 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3680 _("Add the selected folder to the Bookmarks"));
3682 /* Remove bookmark button */
3683 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3684 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3688 G_CALLBACK (remove_bookmark_button_clicked_cb));
3689 g_object_unref (icon);
3690 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3691 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3692 _("Remove the selected bookmark"));
3698 key_is_left_or_right (GdkEventKey *event)
3702 modifiers = gtk_accelerator_get_default_mod_mask ();
3704 return ((event->keyval == GDK_KEY_Right
3705 || event->keyval == GDK_KEY_KP_Right
3706 || event->keyval == GDK_KEY_Left
3707 || event->keyval == GDK_KEY_KP_Left)
3708 && (event->state & modifiers) == 0);
3711 /* Handles key press events on the file list, so that we can trap Enter to
3712 * activate the default button on our own. Also, checks to see if '/' has been
3713 * pressed. See comment by tree_view_keybinding_cb() for more details.
3716 browse_files_key_press_event_cb (GtkWidget *widget,
3720 GtkFileChooserDefault *impl;
3723 impl = (GtkFileChooserDefault *) data;
3725 modifiers = gtk_accelerator_get_default_mod_mask ();
3727 if ((event->keyval == GDK_KEY_slash
3728 || event->keyval == GDK_KEY_KP_Divide
3730 || event->keyval == GDK_KEY_asciitilde
3732 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3734 location_popup_handler (impl, event->string);
3738 if (key_is_left_or_right (event))
3740 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3744 if ((event->keyval == GDK_KEY_Return
3745 || event->keyval == GDK_KEY_ISO_Enter
3746 || event->keyval == GDK_KEY_KP_Enter
3747 || event->keyval == GDK_KEY_space
3748 || event->keyval == GDK_KEY_KP_Space)
3749 && ((event->state & modifiers) == 0)
3750 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3751 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3755 window = get_toplevel (widget);
3758 GtkWidget *default_widget, *focus_widget;
3760 default_widget = gtk_window_get_default_widget (window);
3761 focus_widget = gtk_window_get_focus (window);
3763 if (widget != default_widget &&
3764 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3766 gtk_window_activate_default (window);
3776 /* Callback used when the file list's popup menu is detached */
3778 popup_menu_detach_cb (GtkWidget *attach_widget,
3781 GtkFileChooserDefault *impl;
3783 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3784 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3786 impl->browse_files_popup_menu = NULL;
3787 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3788 impl->browse_files_popup_menu_hidden_files_item = NULL;
3791 /* Callback used when the "Add to Bookmarks" menu item is activated */
3793 add_to_shortcuts_cb (GtkMenuItem *item,
3794 GtkFileChooserDefault *impl)
3796 bookmarks_add_selected_folder (impl);
3799 /* Callback used when the "Show Hidden Files" menu item is toggled */
3801 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3802 GtkFileChooserDefault *impl)
3805 "show-hidden", gtk_check_menu_item_get_active (item),
3809 /* Callback used when the "Show Size Column" menu item is toggled */
3811 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3812 GtkFileChooserDefault *impl)
3814 impl->show_size_column = gtk_check_menu_item_get_active (item);
3816 gtk_tree_view_column_set_visible (impl->list_size_column,
3817 impl->show_size_column);
3820 /* Shows an error dialog about not being able to select a dragged file */
3822 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3827 _("Could not select file"),
3832 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3837 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3839 for (i = 1; uris[i]; i++)
3842 GError *error = NULL;
3845 file = g_file_new_for_uri (uri);
3847 gtk_file_chooser_default_select_file (chooser, file, &error);
3849 error_selecting_dragged_file_dialog (impl, file, error);
3851 g_object_unref (file);
3855 struct FileListDragData
3857 GtkFileChooserDefault *impl;
3863 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3865 const GError *error,
3868 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3869 struct FileListDragData *data = user_data;
3870 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3872 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3875 data->impl->file_list_drag_data_received_cancellable = NULL;
3877 if (cancelled || error)
3880 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3881 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3882 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
3883 change_folder_and_display_error (data->impl, data->file, FALSE);
3886 GError *error = NULL;
3888 gtk_file_chooser_default_unselect_all (chooser);
3889 gtk_file_chooser_default_select_file (chooser, data->file, &error);
3891 error_selecting_dragged_file_dialog (data->impl, data->file, error);
3893 browse_files_center_selected_row (data->impl);
3896 if (data->impl->select_multiple)
3897 file_list_drag_data_select_uris (data->impl, data->uris);
3900 g_object_unref (data->impl);
3901 g_strfreev (data->uris);
3902 g_object_unref (data->file);
3905 g_object_unref (cancellable);
3909 file_list_drag_data_received_cb (GtkWidget *widget,
3910 GdkDragContext *context,
3913 GtkSelectionData *selection_data,
3918 GtkFileChooserDefault *impl;
3923 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3925 /* Allow only drags from other widgets; see bug #533891. */
3926 if (gtk_drag_get_source_widget (context) == widget)
3928 g_signal_stop_emission_by_name (widget, "drag-data-received");
3932 /* Parse the text/uri-list string, navigate to the first one */
3933 uris = gtk_selection_data_get_uris (selection_data);
3934 if (uris && uris[0])
3936 struct FileListDragData *data;
3939 file = g_file_new_for_uri (uri);
3941 data = g_new0 (struct FileListDragData, 1);
3942 data->impl = g_object_ref (impl);
3946 if (impl->file_list_drag_data_received_cancellable)
3947 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
3949 impl->file_list_drag_data_received_cancellable =
3950 _gtk_file_system_get_info (impl->file_system, file,
3952 file_list_drag_data_received_get_info_cb,
3956 g_signal_stop_emission_by_name (widget, "drag-data-received");
3959 /* Don't do anything with the drag_drop signal */
3961 file_list_drag_drop_cb (GtkWidget *widget,
3962 GdkDragContext *context,
3966 GtkFileChooserDefault *impl)
3968 g_signal_stop_emission_by_name (widget, "drag-drop");
3972 /* Disable the normal tree drag motion handler, it makes it look like you're
3973 dropping the dragged item onto a tree item */
3975 file_list_drag_motion_cb (GtkWidget *widget,
3976 GdkDragContext *context,
3980 GtkFileChooserDefault *impl)
3982 g_signal_stop_emission_by_name (widget, "drag-motion");
3986 /* Constructs the popup menu for the file list if needed */
3988 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3992 if (impl->browse_files_popup_menu)
3995 impl->browse_files_popup_menu = gtk_menu_new ();
3996 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3997 impl->browse_files_tree_view,
3998 popup_menu_detach_cb);
4000 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4001 impl->browse_files_popup_menu_add_shortcut_item = item;
4002 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4003 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4004 g_signal_connect (item, "activate",
4005 G_CALLBACK (add_to_shortcuts_cb), impl);
4006 gtk_widget_show (item);
4007 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4009 item = gtk_separator_menu_item_new ();
4010 gtk_widget_show (item);
4011 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4013 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4014 impl->browse_files_popup_menu_hidden_files_item = item;
4015 g_signal_connect (item, "toggled",
4016 G_CALLBACK (show_hidden_toggled_cb), impl);
4017 gtk_widget_show (item);
4018 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4020 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4021 impl->browse_files_popup_menu_size_column_item = item;
4022 g_signal_connect (item, "toggled",
4023 G_CALLBACK (show_size_column_toggled_cb), impl);
4024 gtk_widget_show (item);
4025 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4027 bookmarks_check_add_sensitivity (impl);
4030 /* Updates the popup menu for the file list, creating it if necessary */
4032 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4034 file_list_build_popup_menu (impl);
4036 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4038 /* The sensitivity of the Add to Bookmarks item is set in
4039 * bookmarks_check_add_sensitivity()
4042 /* 'Show Hidden Files' */
4043 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4044 G_CALLBACK (show_hidden_toggled_cb), impl);
4045 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4047 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4048 G_CALLBACK (show_hidden_toggled_cb), impl);
4050 /* 'Show Size Column' */
4051 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4052 G_CALLBACK (show_size_column_toggled_cb), impl);
4053 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4054 impl->show_size_column);
4055 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4056 G_CALLBACK (show_size_column_toggled_cb), impl);
4060 popup_position_func (GtkMenu *menu,
4066 GtkAllocation allocation;
4067 GtkWidget *widget = GTK_WIDGET (user_data);
4068 GdkScreen *screen = gtk_widget_get_screen (widget);
4071 GdkRectangle monitor;
4073 g_return_if_fail (gtk_widget_get_realized (widget));
4075 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4077 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4080 gtk_widget_get_allocation (widget, &allocation);
4081 *x += (allocation.width - req.width) / 2;
4082 *y += (allocation.height - req.height) / 2;
4084 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4085 gtk_menu_set_monitor (menu, monitor_num);
4086 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4088 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4089 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4095 file_list_popup_menu (GtkFileChooserDefault *impl,
4096 GdkEventButton *event)
4098 file_list_update_popup_menu (impl);
4100 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4101 NULL, NULL, NULL, NULL,
4102 event->button, event->time);
4105 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4107 popup_position_func, impl->browse_files_tree_view,
4108 0, GDK_CURRENT_TIME);
4109 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4115 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4117 list_popup_menu_cb (GtkWidget *widget,
4118 GtkFileChooserDefault *impl)
4120 file_list_popup_menu (impl, NULL);
4124 /* Callback used when a button is pressed on the file list. We trap button 3 to
4125 * bring up a popup menu.
4128 list_button_press_event_cb (GtkWidget *widget,
4129 GdkEventButton *event,
4130 GtkFileChooserDefault *impl)
4132 static gboolean in_press = FALSE;
4137 if (event->button != 3)
4141 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4144 file_list_popup_menu (impl, event);
4149 OperationMode operation_mode;
4150 gint general_column;
4154 /* Sets the sort column IDs for the file list based on the operation mode */
4156 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4158 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4159 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4160 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4164 file_list_query_tooltip_cb (GtkWidget *widget,
4167 gboolean keyboard_tip,
4168 GtkTooltip *tooltip,
4171 GtkFileChooserDefault *impl = user_data;
4172 GtkTreeModel *model;
4178 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4182 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4185 &model, &path, &iter))
4188 gtk_tree_model_get (model, &iter,
4189 MODEL_COL_FILE, &file,
4194 gtk_tree_path_free (path);
4198 filename = g_file_get_path (file);
4199 gtk_tooltip_set_text (tooltip, filename);
4200 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4205 g_object_unref (file);
4206 gtk_tree_path_free (path);
4212 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4216 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4217 gtk_cell_renderer_set_fixed_size (renderer,
4218 xpad * 2 + impl->icon_size,
4219 ypad * 2 + impl->icon_size);
4222 /* Creates the widgets for the file list */
4224 create_file_list (GtkFileChooserDefault *impl)
4227 GtkTreeSelection *selection;
4228 GtkTreeViewColumn *column;
4229 GtkCellRenderer *renderer;
4231 /* Scrolled window */
4232 swin = gtk_scrolled_window_new (NULL, NULL);
4233 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4234 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4235 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4238 /* Tree/list view */
4240 impl->browse_files_tree_view = gtk_tree_view_new ();
4241 #ifdef PROFILE_FILE_CHOOSER
4242 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4244 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4245 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4247 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4248 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4250 gtk_drag_dest_set (impl->browse_files_tree_view,
4251 GTK_DEST_DEFAULT_ALL,
4253 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4254 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4256 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4257 G_CALLBACK (list_row_activated), impl);
4258 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4259 G_CALLBACK (browse_files_key_press_event_cb), impl);
4260 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4261 G_CALLBACK (list_popup_menu_cb), impl);
4262 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4263 G_CALLBACK (list_button_press_event_cb), impl);
4265 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4266 G_CALLBACK (file_list_drag_data_received_cb), impl);
4267 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4268 G_CALLBACK (file_list_drag_drop_cb), impl);
4269 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4270 G_CALLBACK (file_list_drag_motion_cb), impl);
4272 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4273 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4274 G_CALLBACK (file_list_query_tooltip_cb), impl);
4276 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4277 gtk_tree_selection_set_select_function (selection,
4280 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4283 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4284 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4286 g_signal_connect (selection, "changed",
4287 G_CALLBACK (list_selection_changed), impl);
4289 /* Keep the column order in sync with update_cell_renderer_attributes() */
4291 /* Filename column */
4293 impl->list_name_column = gtk_tree_view_column_new ();
4294 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4295 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4296 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4298 renderer = gtk_cell_renderer_pixbuf_new ();
4299 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4300 set_icon_cell_renderer_fixed_size (impl, renderer);
4301 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4303 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4304 g_object_set (impl->list_name_renderer,
4305 "ellipsize", PANGO_ELLIPSIZE_END,
4307 g_signal_connect (impl->list_name_renderer, "edited",
4308 G_CALLBACK (renderer_edited_cb), impl);
4309 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4310 G_CALLBACK (renderer_editing_canceled_cb), impl);
4311 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4313 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4317 column = gtk_tree_view_column_new ();
4318 gtk_tree_view_column_set_resizable (column, TRUE);
4319 gtk_tree_view_column_set_title (column, _("Size"));
4321 renderer = gtk_cell_renderer_text_new ();
4322 g_object_set (renderer,
4323 "alignment", PANGO_ALIGN_RIGHT,
4325 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4326 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4327 impl->list_size_column = column;
4329 /* Modification time column */
4331 column = gtk_tree_view_column_new ();
4332 gtk_tree_view_column_set_resizable (column, TRUE);
4333 gtk_tree_view_column_set_title (column, _("Modified"));
4335 renderer = gtk_cell_renderer_text_new ();
4336 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4337 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4338 impl->list_mtime_column = column;
4340 file_list_set_sort_column_ids (impl);
4341 update_cell_renderer_attributes (impl);
4343 gtk_widget_show_all (swin);
4348 /* Creates the widgets for the files/folders pane */
4350 file_pane_create (GtkFileChooserDefault *impl,
4351 GtkSizeGroup *size_group)
4357 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4358 gtk_widget_show (vbox);
4360 /* Box for lists and preview */
4362 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4363 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4364 gtk_widget_show (hbox);
4368 widget = create_file_list (impl);
4369 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4370 gtk_size_group_add_widget (size_group, widget);
4374 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4375 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4376 /* Don't show preview box initially */
4380 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4382 widget = filter_create (impl);
4384 gtk_widget_show (widget);
4385 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4387 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4392 /* Creates the widgets specific to Save mode */
4394 save_widgets_create (GtkFileChooserDefault *impl)
4399 if (impl->save_widgets != NULL)
4402 location_switch_to_path_bar (impl);
4404 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4406 impl->save_widgets_table = gtk_table_new (2, 2, FALSE);
4407 gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4408 gtk_widget_show (impl->save_widgets_table);
4409 gtk_table_set_row_spacings (GTK_TABLE (impl->save_widgets_table), 12);
4410 gtk_table_set_col_spacings (GTK_TABLE (impl->save_widgets_table), 12);
4414 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4415 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4416 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4417 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), widget,
4421 gtk_widget_show (widget);
4423 /* Location entry */
4425 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4426 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4428 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4429 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4430 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4431 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), impl->location_entry,
4433 GTK_EXPAND | GTK_FILL, 0,
4435 gtk_widget_show (impl->location_entry);
4436 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4439 impl->save_folder_label = gtk_label_new (NULL);
4440 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4441 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4442 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), impl->save_folder_label,
4446 gtk_widget_show (impl->save_folder_label);
4448 impl->save_widgets = vbox;
4449 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4450 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4451 gtk_widget_show (impl->save_widgets);
4454 /* Destroys the widgets specific to Save mode */
4456 save_widgets_destroy (GtkFileChooserDefault *impl)
4458 if (impl->save_widgets == NULL)
4461 gtk_widget_destroy (impl->save_widgets);
4462 impl->save_widgets = NULL;
4463 impl->save_widgets_table = NULL;
4464 impl->location_entry = NULL;
4465 impl->save_folder_label = NULL;
4468 /* Turns on the path bar widget. Can be called even if we are already in that
4472 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4474 if (impl->location_entry)
4476 gtk_widget_destroy (impl->location_entry);
4477 impl->location_entry = NULL;
4480 gtk_widget_hide (impl->location_entry_box);
4483 /* Sets the full path of the current folder as the text in the location entry. */
4485 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4487 gchar *text, *filename;
4489 if (!impl->current_folder)
4492 filename = g_file_get_path (impl->current_folder);
4496 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4500 text = g_file_get_uri (impl->current_folder);
4504 gboolean need_slash;
4507 len = strlen (text);
4508 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4514 slash_text = g_new (char, len + 2);
4515 strcpy (slash_text, text);
4516 slash_text[len] = G_DIR_SEPARATOR;
4517 slash_text[len + 1] = 0;
4523 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4530 /* Turns on the location entry. Can be called even if we are already in that
4534 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4536 /* when in search or recent files mode, we are not showing the
4537 * location_entry_box container, so there's no point in switching
4540 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4541 impl->operation_mode == OPERATION_MODE_RECENT)
4544 if (impl->location_entry)
4545 gtk_widget_destroy (impl->location_entry);
4549 gtk_widget_show (impl->location_entry_box);
4553 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4554 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4556 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4557 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4559 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4560 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4562 /* Configure the entry */
4564 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4565 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4569 gtk_widget_show (impl->location_entry);
4570 gtk_widget_grab_focus (impl->location_entry);
4573 /* Sets a new location mode. set_buttons determines whether the toggle button
4574 * for the mode will also be changed.
4577 location_mode_set (GtkFileChooserDefault *impl,
4578 LocationMode new_mode,
4579 gboolean set_button)
4581 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4582 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4584 GtkWindow *toplevel;
4585 GtkWidget *current_focus;
4586 gboolean button_active;
4587 gboolean switch_to_file_list;
4591 case LOCATION_MODE_PATH_BAR:
4592 button_active = FALSE;
4594 /* The location_entry will disappear when we switch to path bar mode. So,
4595 * we'll focus the file list in that case, to avoid having a window with
4596 * no focused widget.
4598 toplevel = get_toplevel (GTK_WIDGET (impl));
4599 switch_to_file_list = FALSE;
4602 current_focus = gtk_window_get_focus (toplevel);
4603 if (!current_focus || current_focus == impl->location_entry)
4604 switch_to_file_list = TRUE;
4607 location_switch_to_path_bar (impl);
4609 if (switch_to_file_list)
4610 gtk_widget_grab_focus (impl->browse_files_tree_view);
4614 case LOCATION_MODE_FILENAME_ENTRY:
4615 button_active = TRUE;
4616 location_switch_to_filename_entry (impl);
4620 g_assert_not_reached ();
4626 g_signal_handlers_block_by_func (impl->location_button,
4627 G_CALLBACK (location_button_toggled_cb), impl);
4629 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4631 g_signal_handlers_unblock_by_func (impl->location_button,
4632 G_CALLBACK (location_button_toggled_cb), impl);
4636 impl->location_mode = new_mode;
4640 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4642 /* when in search or recent files mode, we are not showing the
4643 * location_entry_box container, so there's no point in switching
4646 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4647 impl->operation_mode == OPERATION_MODE_RECENT)
4650 /* If the file entry is not visible, show it.
4651 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4653 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4655 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4657 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4659 if (gtk_widget_has_focus (impl->location_entry))
4661 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4665 gtk_widget_grab_focus (impl->location_entry);
4670 /* Callback used when one of the location mode buttons is toggled */
4672 location_button_toggled_cb (GtkToggleButton *toggle,
4673 GtkFileChooserDefault *impl)
4676 LocationMode new_mode;
4678 is_active = gtk_toggle_button_get_active (toggle);
4682 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4683 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4687 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4688 new_mode = LOCATION_MODE_PATH_BAR;
4691 location_mode_set (impl, new_mode, FALSE);
4694 /* Creates a toggle button for the location entry. */
4696 location_button_create (GtkFileChooserDefault *impl)
4701 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4702 gtk_widget_show (image);
4704 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4708 g_signal_connect (impl->location_button, "toggled",
4709 G_CALLBACK (location_button_toggled_cb), impl);
4711 str = _("Type a file name");
4713 gtk_widget_set_tooltip_text (impl->location_button, str);
4714 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4717 /* Creates the info bar for informational messages or warnings, with its icon and label */
4719 info_bar_create (GtkFileChooserDefault *impl)
4721 GtkWidget *content_area;
4723 impl->browse_select_a_folder_info_bar = gtk_info_bar_new ();
4724 impl->browse_select_a_folder_warning_icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_MENU);
4725 impl->browse_select_a_folder_label = gtk_label_new (NULL);
4727 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar));
4728 gtk_box_set_spacing (GTK_BOX (content_area), 6);
4730 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_warning_icon, FALSE, FALSE, 0);
4731 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_label, TRUE, TRUE, 0);
4733 gtk_widget_show (impl->browse_select_a_folder_label);
4736 /* Creates the recently-used informational widgets, including the info bar */
4738 recent_widgets_create (GtkFileChooserDefault *impl)
4740 /* Box for recent widgets */
4741 impl->recent_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4744 info_bar_create (impl);
4745 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), impl->browse_select_a_folder_info_bar, FALSE, FALSE, 0);
4748 /* Creates the path bar's container and eveyrthing that goes in it: location button, pathbar, info bar, and Create Folder button */
4750 path_bar_widgets_create (GtkFileChooserDefault *impl)
4752 /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4753 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4754 gtk_widget_show (impl->browse_path_bar_hbox);
4756 /* Size group that allows the path bar to be the same size between modes */
4757 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4758 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4760 /* Location button */
4761 location_button_create (impl);
4762 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4763 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4766 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4767 _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system);
4768 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4770 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4771 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4773 /* Recent widgets and info bar */
4774 recent_widgets_create (impl);
4775 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->recent_hbox);
4776 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
4779 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4780 g_signal_connect (impl->browse_new_folder_button, "clicked",
4781 G_CALLBACK (new_folder_button_clicked), impl);
4782 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_new_folder_button);
4783 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4786 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4788 browse_widgets_create (GtkFileChooserDefault *impl)
4792 GtkSizeGroup *size_group;
4794 /* size group is used by the scrolled windows of the panes */
4795 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4796 impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4798 /* Path bar, info bar, and their respective machinery - the browse_path_bar_hbox will get packed elsewhere */
4799 path_bar_widgets_create (impl);
4801 /* Box for the location label and entry */
4803 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4804 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->location_entry_box, FALSE, FALSE, 0);
4806 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
4807 gtk_widget_show (impl->location_label);
4808 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
4812 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
4813 gtk_widget_show (hpaned);
4814 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
4816 widget = shortcuts_pane_create (impl, size_group);
4817 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
4818 widget = file_pane_create (impl, size_group);
4819 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
4820 gtk_paned_set_position (GTK_PANED (hpaned), 148);
4821 g_object_unref (size_group);
4825 gtk_file_chooser_default_constructor (GType type,
4826 guint n_construct_properties,
4827 GObjectConstructParam *construct_params)
4829 GtkFileChooserDefault *impl;
4832 profile_start ("start", NULL);
4834 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
4835 n_construct_properties,
4837 impl = GTK_FILE_CHOOSER_DEFAULT (object);
4839 g_assert (impl->file_system);
4841 gtk_widget_push_composite_child ();
4843 /* Shortcuts model */
4844 shortcuts_model_create (impl);
4846 /* The browse widgets */
4847 browse_widgets_create (impl);
4848 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
4850 /* Alignment to hold extra widget */
4851 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
4852 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
4853 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
4854 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
4856 gtk_widget_pop_composite_child ();
4857 update_appearance (impl);
4859 profile_end ("end", NULL);
4864 /* Sets the extra_widget by packing it in the appropriate place */
4866 set_extra_widget (GtkFileChooserDefault *impl,
4867 GtkWidget *extra_widget)
4871 g_object_ref (extra_widget);
4872 /* FIXME: is this right ? */
4873 gtk_widget_show (extra_widget);
4876 if (impl->extra_widget)
4878 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4879 g_object_unref (impl->extra_widget);
4882 impl->extra_widget = extra_widget;
4883 if (impl->extra_widget)
4885 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4886 gtk_widget_show (impl->extra_align);
4889 gtk_widget_hide (impl->extra_align);
4893 set_local_only (GtkFileChooserDefault *impl,
4894 gboolean local_only)
4896 if (local_only != impl->local_only)
4898 impl->local_only = local_only;
4900 if (impl->location_entry)
4901 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
4903 if (impl->shortcuts_model && impl->file_system)
4905 shortcuts_add_volumes (impl);
4906 shortcuts_add_bookmarks (impl);
4909 if (local_only && impl->current_folder &&
4910 !g_file_is_native (impl->current_folder))
4912 /* If we are pointing to a non-local folder, make an effort to change
4913 * back to a local folder, but it's really up to the app to not cause
4914 * such a situation, so we ignore errors.
4916 const gchar *home = g_get_home_dir ();
4922 home_file = g_file_new_for_path (home);
4924 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
4926 g_object_unref (home_file);
4932 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
4933 GtkFileChooserDefault *impl)
4935 shortcuts_add_volumes (impl);
4936 shortcuts_add_bookmarks (impl);
4938 bookmarks_check_add_sensitivity (impl);
4939 bookmarks_check_remove_sensitivity (impl);
4940 shortcuts_check_popup_sensitivity (impl);
4943 /* Sets the file chooser to multiple selection mode */
4945 set_select_multiple (GtkFileChooserDefault *impl,
4946 gboolean select_multiple,
4947 gboolean property_notify)
4949 GtkTreeSelection *selection;
4950 GtkSelectionMode mode;
4952 if (select_multiple == impl->select_multiple)
4955 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
4957 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4958 gtk_tree_selection_set_mode (selection, mode);
4960 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
4962 impl->select_multiple = select_multiple;
4963 g_object_notify (G_OBJECT (impl), "select-multiple");
4965 check_preview_change (impl);
4969 set_file_system_backend (GtkFileChooserDefault *impl)
4971 profile_start ("start for backend", "default");
4973 impl->file_system = _gtk_file_system_new ();
4975 g_signal_connect (impl->file_system, "volumes-changed",
4976 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
4977 g_signal_connect (impl->file_system, "bookmarks-changed",
4978 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
4980 profile_end ("end", NULL);
4984 unset_file_system_backend (GtkFileChooserDefault *impl)
4986 g_signal_handlers_disconnect_by_func (impl->file_system,
4987 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
4989 g_object_unref (impl->file_system);
4991 impl->file_system = NULL;
4995 INFO_BAR_SELECT_A_FOLDER,
4996 INFO_BAR_ERROR_NO_FILENAME,
4997 INFO_BAR_ERROR_NO_FOLDER
5001 set_info_bar (GtkFileChooserDefault *impl, InfoBarMode mode)
5005 GtkMessageType message_type;
5007 if (!impl->browse_select_a_folder_info_bar)
5014 case INFO_BAR_SELECT_A_FOLDER:
5015 str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
5017 message_type = GTK_MESSAGE_OTHER;
5020 case INFO_BAR_ERROR_NO_FILENAME:
5021 str = _("Please type a file name");
5022 message_type = GTK_MESSAGE_WARNING;
5025 case INFO_BAR_ERROR_NO_FOLDER:
5026 str = _("Please select a folder below");
5027 message_type = GTK_MESSAGE_WARNING;
5031 g_assert_not_reached ();
5035 gtk_info_bar_set_message_type (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar), message_type);
5036 gtk_widget_set_visible (impl->browse_select_a_folder_warning_icon, message_type == GTK_MESSAGE_WARNING);
5037 gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
5043 /* Saves the widgets around the pathbar so they can be reparented later
5044 * in the correct place. This function must be called paired with
5045 * restore_path_bar().
5048 save_path_bar (GtkFileChooserDefault *impl)
5052 g_object_ref (impl->browse_path_bar_hbox);
5054 parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
5056 gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
5059 /* Reparents the path bar and the "Create folder" button to the right place:
5060 * Above the file list in Open mode, or to the right of the "Save in folder:"
5061 * label in Save mode. The save_path_bar() function must be called before this
5065 restore_path_bar (GtkFileChooserDefault *impl)
5067 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5068 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5070 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5071 gtk_box_reorder_child (GTK_BOX (impl->browse_widgets_box), impl->browse_path_bar_hbox, 0);
5073 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5074 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5076 gtk_table_attach (GTK_TABLE (impl->save_widgets_table), impl->browse_path_bar_hbox,
5078 GTK_EXPAND | GTK_FILL, GTK_FILL,
5080 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5083 g_assert_not_reached ();
5085 g_object_unref (impl->browse_path_bar_hbox);
5088 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5090 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5094 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5095 MODEL_COL_FILE, &file,
5097 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE, NULL); /* NULL-GError */
5098 g_object_unref (file);
5101 /* For recently-used mode, updates the path in the pathbar with the currently-selected item */
5103 update_path_bar (GtkFileChooserDefault *impl)
5105 if (impl->operation_mode == OPERATION_MODE_RECENT
5106 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5108 GtkTreeSelection *selection;
5109 gboolean have_selected;
5112 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5114 /* Save mode means single-selection mode, so the following is valid */
5115 have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5119 put_recent_folder_in_pathbar (impl, &iter);
5120 gtk_widget_show (impl->browse_path_bar);
5121 gtk_widget_hide (impl->browse_select_a_folder_info_bar);
5125 gtk_widget_hide (impl->browse_path_bar);
5126 set_info_bar (impl, INFO_BAR_SELECT_A_FOLDER);
5127 gtk_widget_show (impl->browse_select_a_folder_info_bar);
5132 /* This function is basically a do_all function.
5134 * It sets the visibility on all the widgets based on the current state, and
5135 * moves the custom_widget if needed.
5138 update_appearance (GtkFileChooserDefault *impl)
5140 save_path_bar (impl);
5142 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5143 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5147 gtk_widget_hide (impl->location_button);
5148 save_widgets_create (impl);
5150 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5151 text = _("Save in _folder:");
5153 text = _("Create in _folder:");
5155 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5157 if (impl->select_multiple)
5159 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5160 "Re-setting to single selection mode.");
5161 set_select_multiple (impl, FALSE, TRUE);
5164 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5165 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5167 gtk_widget_show (impl->location_button);
5168 save_widgets_destroy (impl);
5169 gtk_widget_show (impl->browse_widgets_box);
5170 location_mode_set (impl, impl->location_mode, TRUE);
5173 if (impl->location_entry)
5174 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5176 restore_path_bar (impl);
5177 update_path_bar (impl);
5179 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5180 gtk_widget_hide (impl->browse_new_folder_button);
5182 gtk_widget_show (impl->browse_new_folder_button);
5184 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5185 * of files may change depending whether we are in a file or folder-only mode.
5187 gtk_widget_queue_draw (impl->browse_files_tree_view);
5189 emit_default_size_changed (impl);
5193 gtk_file_chooser_default_set_property (GObject *object,
5195 const GValue *value,
5199 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5203 case GTK_FILE_CHOOSER_PROP_ACTION:
5205 GtkFileChooserAction action = g_value_get_enum (value);
5207 if (action != impl->action)
5209 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5211 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5212 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5213 && impl->select_multiple)
5215 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5216 "this is not allowed in multiple selection mode. Resetting the file chooser "
5217 "to single selection mode.");
5218 set_select_multiple (impl, FALSE, TRUE);
5220 impl->action = action;
5221 update_cell_renderer_attributes (impl);
5222 update_appearance (impl);
5223 settings_load (impl);
5228 case GTK_FILE_CHOOSER_PROP_FILTER:
5229 set_current_filter (impl, g_value_get_object (value));
5232 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5233 set_local_only (impl, g_value_get_boolean (value));
5236 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5237 set_preview_widget (impl, g_value_get_object (value));
5240 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5241 impl->preview_widget_active = g_value_get_boolean (value);
5242 update_preview_widget_visibility (impl);
5245 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5246 impl->use_preview_label = g_value_get_boolean (value);
5247 update_preview_widget_visibility (impl);
5250 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5251 set_extra_widget (impl, g_value_get_object (value));
5254 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5256 gboolean select_multiple = g_value_get_boolean (value);
5257 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5258 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5261 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5262 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5263 "leaving the file chooser in single selection mode.");
5267 set_select_multiple (impl, select_multiple, FALSE);
5271 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5273 gboolean show_hidden = g_value_get_boolean (value);
5274 if (show_hidden != impl->show_hidden)
5276 impl->show_hidden = show_hidden;
5278 if (impl->browse_files_model)
5279 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5284 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5286 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5287 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5291 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5293 gboolean create_folders = g_value_get_boolean (value);
5294 impl->create_folders = create_folders;
5295 update_appearance (impl);
5300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5306 gtk_file_chooser_default_get_property (GObject *object,
5311 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5315 case GTK_FILE_CHOOSER_PROP_ACTION:
5316 g_value_set_enum (value, impl->action);
5319 case GTK_FILE_CHOOSER_PROP_FILTER:
5320 g_value_set_object (value, impl->current_filter);
5323 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5324 g_value_set_boolean (value, impl->local_only);
5327 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5328 g_value_set_object (value, impl->preview_widget);
5331 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5332 g_value_set_boolean (value, impl->preview_widget_active);
5335 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5336 g_value_set_boolean (value, impl->use_preview_label);
5339 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5340 g_value_set_object (value, impl->extra_widget);
5343 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5344 g_value_set_boolean (value, impl->select_multiple);
5347 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5348 g_value_set_boolean (value, impl->show_hidden);
5351 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5352 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5355 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5356 g_value_set_boolean (value, impl->create_folders);
5360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5365 /* This cancels everything that may be going on in the background. */
5367 cancel_all_operations (GtkFileChooserDefault *impl)
5371 pending_select_files_free (impl);
5373 /* cancel all pending operations */
5374 if (impl->pending_cancellables)
5376 for (l = impl->pending_cancellables; l; l = l->next)
5378 GCancellable *cancellable = G_CANCELLABLE (l->data);
5379 g_cancellable_cancel (cancellable);
5381 g_slist_free (impl->pending_cancellables);
5382 impl->pending_cancellables = NULL;
5385 if (impl->reload_icon_cancellables)
5387 for (l = impl->reload_icon_cancellables; l; l = l->next)
5389 GCancellable *cancellable = G_CANCELLABLE (l->data);
5390 g_cancellable_cancel (cancellable);
5392 g_slist_free (impl->reload_icon_cancellables);
5393 impl->reload_icon_cancellables = NULL;
5396 if (impl->loading_shortcuts)
5398 for (l = impl->loading_shortcuts; l; l = l->next)
5400 GCancellable *cancellable = G_CANCELLABLE (l->data);
5401 g_cancellable_cancel (cancellable);
5403 g_slist_free (impl->loading_shortcuts);
5404 impl->loading_shortcuts = NULL;
5407 if (impl->file_list_drag_data_received_cancellable)
5409 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5410 impl->file_list_drag_data_received_cancellable = NULL;
5413 if (impl->update_current_folder_cancellable)
5415 g_cancellable_cancel (impl->update_current_folder_cancellable);
5416 impl->update_current_folder_cancellable = NULL;
5419 if (impl->should_respond_get_info_cancellable)
5421 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5422 impl->should_respond_get_info_cancellable = NULL;
5425 if (impl->update_from_entry_cancellable)
5427 g_cancellable_cancel (impl->update_from_entry_cancellable);
5428 impl->update_from_entry_cancellable = NULL;
5431 if (impl->shortcuts_activate_iter_cancellable)
5433 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5434 impl->shortcuts_activate_iter_cancellable = NULL;
5437 search_stop_searching (impl, TRUE);
5438 recent_stop_loading (impl);
5441 /* Removes the settings signal handler. It's safe to call multiple times */
5443 remove_settings_signal (GtkFileChooserDefault *impl,
5446 if (impl->settings_signal_id)
5448 GtkSettings *settings;
5450 settings = gtk_settings_get_for_screen (screen);
5451 g_signal_handler_disconnect (settings,
5452 impl->settings_signal_id);
5453 impl->settings_signal_id = 0;
5458 gtk_file_chooser_default_dispose (GObject *object)
5460 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5462 cancel_all_operations (impl);
5464 if (impl->extra_widget)
5466 g_object_unref (impl->extra_widget);
5467 impl->extra_widget = NULL;
5470 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5472 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5475 /* We override show-all since we have internal widgets that
5476 * shouldn't be shown when you call show_all(), like the filter
5480 gtk_file_chooser_default_show_all (GtkWidget *widget)
5482 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5484 gtk_widget_show (widget);
5486 if (impl->extra_widget)
5487 gtk_widget_show_all (impl->extra_widget);
5490 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5491 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5494 toplevel_set_focus_cb (GtkWindow *window,
5496 GtkFileChooserDefault *impl)
5498 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5501 /* We monitor the focus widget on our toplevel to be able to know which widget
5502 * was last focused at the time our "should_respond" method gets called.
5505 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5506 GtkWidget *previous_toplevel)
5508 GtkFileChooserDefault *impl;
5509 GtkWidget *toplevel;
5511 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5512 toplevel = gtk_widget_get_toplevel (widget);
5514 if (previous_toplevel &&
5515 impl->toplevel_set_focus_id != 0)
5517 g_signal_handler_disconnect (previous_toplevel,
5518 impl->toplevel_set_focus_id);
5519 impl->toplevel_set_focus_id = 0;
5520 impl->toplevel_last_focus_widget = NULL;
5523 if (gtk_widget_is_toplevel (toplevel))
5525 g_assert (impl->toplevel_set_focus_id == 0);
5526 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5527 G_CALLBACK (toplevel_set_focus_cb), impl);
5528 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5532 /* Changes the icons wherever it is needed */
5534 change_icon_theme (GtkFileChooserDefault *impl)
5536 GtkSettings *settings;
5538 GtkCellRenderer *renderer;
5541 profile_start ("start", NULL);
5543 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5545 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5546 impl->icon_size = MAX (width, height);
5548 impl->icon_size = FALLBACK_ICON_SIZE;
5550 shortcuts_reload_icons (impl);
5551 /* the first cell in the first column is the icon column, and we have a fixed size there */
5552 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5553 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5554 renderer = GTK_CELL_RENDERER (cells->data);
5555 set_icon_cell_renderer_fixed_size (impl, renderer);
5556 g_list_free (cells);
5557 if (impl->browse_files_model)
5558 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5559 gtk_widget_queue_resize (impl->browse_files_tree_view);
5561 profile_end ("end", NULL);
5564 /* Callback used when a GtkSettings value changes */
5566 settings_notify_cb (GObject *object,
5568 GtkFileChooserDefault *impl)
5572 profile_start ("start", NULL);
5574 name = g_param_spec_get_name (pspec);
5576 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5577 strcmp (name, "gtk-icon-sizes") == 0)
5578 change_icon_theme (impl);
5580 profile_end ("end", NULL);
5583 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5587 check_icon_theme (GtkFileChooserDefault *impl)
5589 GtkSettings *settings;
5591 profile_start ("start", NULL);
5593 if (impl->settings_signal_id)
5595 profile_end ("end", NULL);
5599 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5601 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5602 impl->settings_signal_id = g_signal_connect (settings, "notify",
5603 G_CALLBACK (settings_notify_cb), impl);
5605 change_icon_theme (impl);
5608 profile_end ("end", NULL);
5612 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5614 GtkFileChooserDefault *impl;
5616 profile_start ("start", NULL);
5618 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5620 profile_msg (" parent class style_udpated start", NULL);
5621 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5622 profile_msg (" parent class style_updated end", NULL);
5624 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5625 change_icon_theme (impl);
5627 emit_default_size_changed (impl);
5629 profile_end ("end", NULL);
5633 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5634 GdkScreen *previous_screen)
5636 GtkFileChooserDefault *impl;
5638 profile_start ("start", NULL);
5640 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5642 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5643 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5645 remove_settings_signal (impl, previous_screen);
5646 check_icon_theme (impl);
5648 emit_default_size_changed (impl);
5650 profile_end ("end", NULL);
5654 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5655 GtkAllocation *allocation)
5657 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5661 set_sort_column (GtkFileChooserDefault *impl)
5663 GtkTreeSortable *sortable;
5665 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5667 /* can happen when we're still populating the model */
5668 if (sortable == NULL)
5671 gtk_tree_sortable_set_sort_column_id (sortable,
5677 settings_ensure (GtkFileChooserDefault *impl)
5679 if (impl->settings != NULL)
5682 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
5683 "/org/gtk/settings/file-chooser/");
5684 g_settings_delay (impl->settings);
5688 settings_load (GtkFileChooserDefault *impl)
5690 LocationMode location_mode;
5691 gboolean show_hidden;
5692 gboolean show_size_column;
5694 GtkSortType sort_order;
5696 settings_ensure (impl);
5698 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
5699 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
5700 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
5701 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
5702 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
5704 location_mode_set (impl, location_mode, TRUE);
5706 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5708 impl->show_size_column = show_size_column;
5709 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5711 impl->sort_column = sort_column;
5712 impl->sort_order = sort_order;
5713 /* We don't call set_sort_column() here as the models may not have been
5714 * created yet. The individual functions that create and set the models will
5715 * call set_sort_column() themselves.
5720 save_dialog_geometry (GtkFileChooserDefault *impl)
5722 GtkWindow *toplevel;
5723 int x, y, width, height;
5725 toplevel = get_toplevel (GTK_WIDGET (impl));
5727 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5730 gtk_window_get_position (toplevel, &x, &y);
5731 gtk_window_get_size (toplevel, &width, &height);
5733 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
5734 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
5738 settings_save (GtkFileChooserDefault *impl)
5740 char *current_folder_uri;
5742 settings_ensure (impl);
5744 /* Current folder */
5746 if (impl->current_folder)
5747 current_folder_uri = g_file_get_uri (impl->current_folder);
5749 current_folder_uri = "";
5751 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
5753 if (impl->current_folder)
5754 g_free (current_folder_uri);
5756 /* All the other state */
5758 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
5759 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
5760 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5761 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
5762 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
5763 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
5765 save_dialog_geometry (impl);
5767 /* Now apply the settings */
5768 g_settings_apply (impl->settings);
5770 g_object_unref (impl->settings);
5771 impl->settings = NULL;
5774 /* GtkWidget::realize method */
5776 gtk_file_chooser_default_realize (GtkWidget *widget)
5778 GtkFileChooserDefault *impl;
5780 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5782 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
5784 emit_default_size_changed (impl);
5788 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
5790 char *last_folder_uri;
5793 settings_ensure (impl);
5795 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
5797 /* If no last folder is set, we use the user's home directory, since
5798 * this is the starting point for most documents.
5800 if (last_folder_uri[0] == '\0')
5801 file = g_file_new_for_path (g_get_home_dir ());
5803 file = g_file_new_for_uri (last_folder_uri);
5805 g_free (last_folder_uri);
5810 /* GtkWidget::map method */
5812 gtk_file_chooser_default_map (GtkWidget *widget)
5814 GtkFileChooserDefault *impl;
5816 profile_start ("start", NULL);
5818 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5820 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5822 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5824 switch (impl->reload_state)
5827 recent_shortcut_handler (impl);
5830 case RELOAD_HAS_FOLDER:
5831 /* Nothing; we are already loading or loaded, so we
5832 * don't need to reload
5837 g_assert_not_reached ();
5841 volumes_bookmarks_changed_cb (impl->file_system, impl);
5843 settings_load (impl);
5845 profile_end ("end", NULL);
5848 /* GtkWidget::unmap method */
5850 gtk_file_chooser_default_unmap (GtkWidget *widget)
5852 GtkFileChooserDefault *impl;
5854 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5856 settings_save (impl);
5858 cancel_all_operations (impl);
5859 impl->reload_state = RELOAD_EMPTY;
5861 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5865 install_list_model_filter (GtkFileChooserDefault *impl)
5867 _gtk_file_system_model_set_filter (impl->browse_files_model,
5868 impl->current_filter);
5871 #define COMPARE_DIRECTORIES \
5872 GtkFileChooserDefault *impl = user_data; \
5873 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
5874 gboolean dir_a, dir_b; \
5876 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
5877 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
5879 if (dir_a != dir_b) \
5880 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5882 /* Sort callback for the filename column */
5884 name_sort_func (GtkTreeModel *model,
5889 COMPARE_DIRECTORIES;
5892 const char *key_a, *key_b;
5895 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
5896 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
5899 result = strcmp (key_a, key_b);
5911 /* Sort callback for the size column */
5913 size_sort_func (GtkTreeModel *model,
5918 COMPARE_DIRECTORIES;
5921 gint64 size_a, size_b;
5923 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
5924 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
5926 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
5930 /* Sort callback for the mtime column */
5932 mtime_sort_func (GtkTreeModel *model,
5937 COMPARE_DIRECTORIES;
5942 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
5943 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
5945 return ta < tb ? -1 : (ta == tb ? 0 : 1);
5949 /* Callback used when the sort column changes. We cache the sort order for use
5950 * in name_sort_func().
5953 list_sort_column_changed_cb (GtkTreeSortable *sortable,
5954 GtkFileChooserDefault *impl)
5956 gint sort_column_id;
5957 GtkSortType sort_type;
5959 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
5961 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
5962 impl->sort_column = sort_column_id;
5963 impl->sort_order = sort_type;
5968 set_busy_cursor (GtkFileChooserDefault *impl,
5972 GtkWindow *toplevel;
5973 GdkDisplay *display;
5976 toplevel = get_toplevel (GTK_WIDGET (impl));
5977 widget = GTK_WIDGET (toplevel);
5978 if (!toplevel || !gtk_widget_get_realized (widget))
5981 display = gtk_widget_get_display (widget);
5984 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
5988 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
5989 gdk_display_flush (display);
5992 g_object_unref (cursor);
5995 /* Creates a sort model to wrap the file system model and sets it on the tree view */
5997 load_set_model (GtkFileChooserDefault *impl)
5999 profile_start ("start", NULL);
6001 g_assert (impl->browse_files_model != NULL);
6003 profile_msg (" gtk_tree_view_set_model start", NULL);
6004 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6005 GTK_TREE_MODEL (impl->browse_files_model));
6006 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6007 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6009 set_sort_column (impl);
6010 profile_msg (" gtk_tree_view_set_model end", NULL);
6011 impl->list_sort_ascending = TRUE;
6013 profile_end ("end", NULL);
6016 /* Timeout callback used when the loading timer expires */
6018 load_timeout_cb (gpointer data)
6020 GtkFileChooserDefault *impl;
6022 profile_start ("start", NULL);
6024 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6025 g_assert (impl->load_state == LOAD_PRELOAD);
6026 g_assert (impl->load_timeout_id != 0);
6027 g_assert (impl->browse_files_model != NULL);
6029 impl->load_timeout_id = 0;
6030 impl->load_state = LOAD_LOADING;
6032 load_set_model (impl);
6034 profile_end ("end", NULL);
6039 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6041 load_setup_timer (GtkFileChooserDefault *impl)
6043 g_assert (impl->load_timeout_id == 0);
6044 g_assert (impl->load_state != LOAD_PRELOAD);
6046 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6047 impl->load_state = LOAD_PRELOAD;
6050 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6052 load_remove_timer (GtkFileChooserDefault *impl)
6054 if (impl->load_timeout_id != 0)
6056 g_assert (impl->load_state == LOAD_PRELOAD);
6058 g_source_remove (impl->load_timeout_id);
6059 impl->load_timeout_id = 0;
6060 impl->load_state = LOAD_EMPTY;
6063 g_assert (impl->load_state == LOAD_EMPTY ||
6064 impl->load_state == LOAD_LOADING ||
6065 impl->load_state == LOAD_FINISHED);
6068 /* Selects the first row in the file list */
6070 browse_files_select_first_row (GtkFileChooserDefault *impl)
6073 GtkTreeIter dummy_iter;
6074 GtkTreeModel *tree_model;
6076 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6081 path = gtk_tree_path_new_from_indices (0, -1);
6083 /* If the list is empty, do nothing. */
6084 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6085 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6087 gtk_tree_path_free (path);
6090 struct center_selected_row_closure {
6091 GtkFileChooserDefault *impl;
6092 gboolean already_centered;
6095 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6096 * selected row in the tree view.
6099 center_selected_row_foreach_cb (GtkTreeModel *model,
6104 struct center_selected_row_closure *closure;
6107 if (closure->already_centered)
6110 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6111 closure->already_centered = TRUE;
6114 /* Centers the selected row in the tree view */
6116 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6118 struct center_selected_row_closure closure;
6119 GtkTreeSelection *selection;
6121 closure.impl = impl;
6122 closure.already_centered = FALSE;
6124 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6125 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6129 show_and_select_files (GtkFileChooserDefault *impl,
6132 GtkTreeSelection *selection;
6133 GtkFileSystemModel *fsmodel;
6134 gboolean enabled_hidden, removed_filters;
6135 gboolean selected_a_file;
6138 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6139 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6141 enabled_hidden = impl->show_hidden;
6142 removed_filters = (impl->current_filter == NULL);
6144 selected_a_file = FALSE;
6146 for (walk = files; walk; walk = walk->next)
6148 GFile *file = walk->data;
6151 /* Is it a hidden file? */
6153 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6156 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6158 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6160 if (!enabled_hidden &&
6161 (g_file_info_get_is_hidden (info) ||
6162 g_file_info_get_is_backup (info)))
6164 g_object_set (impl, "show-hidden", TRUE, NULL);
6165 enabled_hidden = TRUE;
6169 /* Is it a filtered file? */
6171 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6172 continue; /* re-get the iter as it may change when the model refilters */
6174 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6176 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6177 if (!removed_filters)
6179 set_current_filter (impl, NULL);
6180 removed_filters = TRUE;
6184 /* Okay, can we select the file now? */
6186 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6189 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6193 gtk_tree_selection_select_iter (selection, &iter);
6195 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6196 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6198 gtk_tree_path_free (path);
6200 selected_a_file = TRUE;
6204 browse_files_center_selected_row (impl);
6206 return selected_a_file;
6209 /* Processes the pending operation when a folder is finished loading */
6211 pending_select_files_process (GtkFileChooserDefault *impl)
6213 g_assert (impl->load_state == LOAD_FINISHED);
6214 g_assert (impl->browse_files_model != NULL);
6216 if (impl->pending_select_files)
6218 show_and_select_files (impl, impl->pending_select_files);
6219 pending_select_files_free (impl);
6220 browse_files_center_selected_row (impl);
6224 /* We only select the first row if the chooser is actually mapped ---
6225 * selecting the first row is to help the user when he is interacting with
6226 * the chooser, but sometimes a chooser works not on behalf of the user,
6227 * but rather on behalf of something else like GtkFileChooserButton. In
6228 * that case, the chooser's selection should be what the caller expects,
6229 * as the user can't see that something else got selected. See bug #165264.
6231 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6232 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6233 browse_files_select_first_row (impl);
6236 g_assert (impl->pending_select_files == NULL);
6240 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6245 info = g_file_query_info (impl->current_folder,
6246 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6247 G_FILE_QUERY_INFO_NONE,
6252 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6253 g_object_unref (info);
6256 msg = g_strdup (_("Could not read the contents of the folder"));
6258 error_message (impl, msg, error->message);
6262 /* Callback used when the file system model finishes loading */
6264 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6266 GtkFileChooserDefault *impl)
6268 profile_start ("start", NULL);
6271 show_error_on_reading_current_folder (impl, error);
6273 if (impl->load_state == LOAD_PRELOAD)
6275 load_remove_timer (impl);
6276 load_set_model (impl);
6278 else if (impl->load_state == LOAD_LOADING)
6284 /* We can't g_assert_not_reached(), as something other than us may have
6285 * initiated a folder reload. See #165556.
6287 profile_end ("end", NULL);
6291 g_assert (impl->load_timeout_id == 0);
6293 impl->load_state = LOAD_FINISHED;
6295 pending_select_files_process (impl);
6296 set_busy_cursor (impl, FALSE);
6297 #ifdef PROFILE_FILE_CHOOSER
6298 access ("MARK: *** FINISHED LOADING", F_OK);
6301 profile_end ("end", NULL);
6305 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6306 gboolean remove_from_treeview)
6308 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6310 if (impl->browse_files_model)
6312 g_object_unref (impl->browse_files_model);
6313 impl->browse_files_model = NULL;
6316 if (remove_from_treeview)
6317 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6321 my_g_format_time_for_display (glong secs)
6326 time_t time_mtime, time_now;
6327 const gchar *format;
6328 gchar *locale_format = NULL;
6330 char *date_str = NULL;
6332 const char *locale, *dot = NULL;
6333 gint64 codepage = -1;
6339 #ifdef HAVE_LOCALTIME_R
6340 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6343 struct tm *ptm = localtime ((time_t *) &time_mtime);
6347 g_warning ("ptm != NULL failed");
6349 return g_strdup (_("Unknown"));
6352 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6354 #endif /* HAVE_LOCALTIME_R */
6356 g_date_set_time_t (&mtime, time_mtime);
6357 time_now = time (NULL);
6358 g_date_set_time_t (&now, time_now);
6360 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6362 /* Translators: %H means "hours" and %M means "minutes" */
6364 format = _("%H:%M");
6365 else if (days_diff == 1)
6366 format = _("Yesterday at %H:%M");
6369 if (days_diff > 1 && days_diff < 7)
6370 format = "%A"; /* Days from last week */
6372 format = "%x"; /* Any other date */
6376 /* g_locale_from_utf8() returns a string in the system
6377 * code-page, which is not always the same as that used by the C
6378 * library. For instance when running a GTK+ program with
6379 * LANG=ko on an English version of Windows, the system
6380 * code-page is 1252, but the code-page used by the C library is
6381 * 949. (It's GTK+ itself that sets the C library locale when it
6382 * notices the LANG environment variable. See gtkmain.c The
6383 * Microsoft C library doesn't look at any locale environment
6384 * variables.) We need to pass strftime() a string in the C
6385 * library's code-page. See bug #509885.
6387 locale = setlocale (LC_ALL, NULL);
6389 dot = strchr (locale, '.');
6392 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6394 /* All codepages should fit in 16 bits AFAIK */
6395 if (codepage > 0 && codepage < 65536)
6397 sprintf (charset, "CP%u", (guint) codepage);
6398 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6402 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6404 if (locale_format != NULL &&
6405 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6408 /* As above but in opposite direction... */
6409 if (codepage > 0 && codepage < 65536)
6410 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6412 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6416 if (date_str == NULL)
6417 date_str = g_strdup (_("Unknown"));
6419 g_free (locale_format);
6424 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6426 GFileAttributeType type;
6429 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6430 g_file_info_set_attribute (to, attribute, type, value);
6434 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6436 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6437 GFile *file = G_FILE (object);
6438 GFileInfo *queried, *info;
6441 queried = g_file_query_info_finish (file, res, NULL);
6442 if (queried == NULL)
6445 GDK_THREADS_ENTER ();
6447 /* now we know model is valid */
6449 /* file was deleted */
6450 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6452 GDK_THREADS_LEAVE ();
6456 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6458 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6459 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6460 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6462 _gtk_file_system_model_update_file (model, file, info, FALSE);
6464 g_object_unref (info);
6466 GDK_THREADS_LEAVE ();
6470 file_system_model_set (GtkFileSystemModel *model,
6477 GtkFileChooserDefault *impl = data;
6481 case MODEL_COL_FILE:
6482 g_value_set_object (value, file);
6484 case MODEL_COL_NAME:
6486 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6488 g_value_set_string (value, g_file_info_get_display_name (info));
6490 case MODEL_COL_NAME_COLLATED:
6492 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6494 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6496 case MODEL_COL_IS_FOLDER:
6497 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6499 case MODEL_COL_PIXBUF:
6502 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6504 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6508 GtkTreeModel *tree_model;
6509 GtkTreePath *path, *start, *end;
6512 if (impl->browse_files_tree_view == NULL ||
6513 g_file_info_has_attribute (info, "filechooser::queried"))
6516 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6517 if (tree_model != GTK_TREE_MODEL (model))
6520 if (!_gtk_file_system_model_get_iter_for_file (model,
6523 g_assert_not_reached ();
6524 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6526 path = gtk_tree_model_get_path (tree_model, &iter);
6527 if (gtk_tree_path_compare (start, path) != 1 &&
6528 gtk_tree_path_compare (path, end) != 1)
6530 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6531 g_file_query_info_async (file,
6532 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6533 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6534 G_FILE_ATTRIBUTE_STANDARD_ICON,
6535 G_FILE_QUERY_INFO_NONE,
6537 _gtk_file_system_model_get_cancellable (model),
6538 file_system_model_got_thumbnail,
6541 gtk_tree_path_free (path);
6542 gtk_tree_path_free (start);
6543 gtk_tree_path_free (end);
6548 g_value_set_object (value, NULL);
6550 case MODEL_COL_SIZE:
6551 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6553 case MODEL_COL_SIZE_TEXT:
6554 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6555 g_value_set_string (value, NULL);
6557 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6559 case MODEL_COL_MTIME:
6560 case MODEL_COL_MTIME_TEXT:
6565 g_file_info_get_modification_time (info, &tv);
6566 if (column == MODEL_COL_MTIME)
6567 g_value_set_long (value, tv.tv_sec);
6568 else if (tv.tv_sec == 0)
6569 g_value_set_static_string (value, _("Unknown"));
6571 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6574 case MODEL_COL_ELLIPSIZE:
6575 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6578 g_assert_not_reached ();
6585 /* Gets rid of the old list model and creates a new one for the current folder */
6587 set_list_model (GtkFileChooserDefault *impl,
6590 g_assert (impl->current_folder != NULL);
6592 profile_start ("start", NULL);
6594 stop_loading_and_clear_list_model (impl, TRUE);
6596 set_busy_cursor (impl, TRUE);
6598 impl->browse_files_model =
6599 _gtk_file_system_model_new_for_directory (impl->current_folder,
6601 file_system_model_set,
6603 MODEL_COLUMN_TYPES);
6605 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6607 profile_msg (" set sort function", NULL);
6608 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6609 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6610 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6611 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6612 set_sort_column (impl);
6613 impl->list_sort_ascending = TRUE;
6614 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6615 G_CALLBACK (list_sort_column_changed_cb), impl);
6617 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6619 g_signal_connect (impl->browse_files_model, "finished-loading",
6620 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6622 install_list_model_filter (impl);
6624 profile_end ("end", NULL);
6629 struct update_chooser_entry_selected_foreach_closure {
6631 GtkTreeIter first_selected_iter;
6635 compare_utf8_filenames (const gchar *a,
6638 gchar *a_folded, *b_folded;
6641 a_folded = g_utf8_strdown (a, -1);
6642 b_folded = g_utf8_strdown (b, -1);
6644 retval = strcmp (a_folded, b_folded);
6653 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6658 struct update_chooser_entry_selected_foreach_closure *closure;
6661 closure->num_selected++;
6663 if (closure->num_selected == 1)
6664 closure->first_selected_iter = *iter;
6668 update_chooser_entry (GtkFileChooserDefault *impl)
6670 GtkTreeSelection *selection;
6671 struct update_chooser_entry_selected_foreach_closure closure;
6673 /* no need to update the file chooser's entry if there's no entry */
6674 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6675 !impl->location_entry)
6678 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6679 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6680 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6681 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6682 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6685 g_assert (impl->location_entry != NULL);
6687 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6688 closure.num_selected = 0;
6689 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6691 if (closure.num_selected == 0)
6693 if (impl->operation_mode == OPERATION_MODE_RECENT)
6694 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
6696 goto maybe_clear_entry;
6698 else if (closure.num_selected == 1)
6700 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6703 gboolean change_entry;
6705 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6707 /* If the cursor moved to the row of the newly created folder,
6708 * retrieving info will return NULL.
6713 g_free (impl->browse_files_last_selected_name);
6714 impl->browse_files_last_selected_name =
6715 g_strdup (g_file_info_get_display_name (info));
6717 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6718 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6719 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6721 /* Don't change the name when clicking on a folder... */
6722 change_entry = (! _gtk_file_info_consider_as_directory (info));
6725 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6729 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6731 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6732 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6737 else if (impl->operation_mode == OPERATION_MODE_RECENT
6738 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6742 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
6744 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
6745 MODEL_COL_FILE, &folder,
6747 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
6748 g_object_unref (folder);
6754 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6755 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6757 /* Multiple selection, so just clear the entry. */
6758 g_free (impl->browse_files_last_selected_name);
6759 impl->browse_files_last_selected_name = NULL;
6761 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6767 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6768 && impl->browse_files_last_selected_name)
6770 const char *entry_text;
6772 gboolean clear_entry;
6774 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6775 len = strlen (entry_text);
6778 /* The file chooser entry may have appended a "/" to its text.
6779 * So take it out, and compare the result to the old selection.
6781 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6785 tmp = g_strndup (entry_text, len - 1);
6786 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6790 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6793 clear_entry = FALSE;
6796 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6801 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6805 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6809 struct UpdateCurrentFolderData
6811 GtkFileChooserDefault *impl;
6813 gboolean keep_trail;
6814 gboolean clear_entry;
6815 GFile *original_file;
6816 GError *original_error;
6820 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6821 GtkFileSystemVolume *volume,
6822 const GError *error,
6825 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6826 struct UpdateCurrentFolderData *data = user_data;
6827 GtkFileChooserDefault *impl = data->impl;
6829 if (cancellable != impl->update_current_folder_cancellable)
6832 impl->update_current_folder_cancellable = NULL;
6833 set_busy_cursor (impl, FALSE);
6840 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
6841 impl->reload_state = RELOAD_EMPTY;
6845 change_folder_and_display_error (impl, data->file, data->clear_entry);
6848 g_object_unref (data->file);
6851 g_object_unref (cancellable);
6855 update_current_folder_get_info_cb (GCancellable *cancellable,
6857 const GError *error,
6860 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6861 struct UpdateCurrentFolderData *data = user_data;
6862 GtkFileChooserDefault *impl = data->impl;
6864 if (cancellable != impl->update_current_folder_cancellable)
6867 impl->update_current_folder_cancellable = NULL;
6868 impl->reload_state = RELOAD_EMPTY;
6870 set_busy_cursor (impl, FALSE);
6879 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
6881 GMountOperation *mount_operation;
6882 GtkWidget *toplevel;
6884 g_object_unref (cancellable);
6885 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
6887 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
6889 set_busy_cursor (impl, TRUE);
6891 impl->update_current_folder_cancellable =
6892 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
6894 update_current_folder_mount_enclosing_volume_cb,
6900 if (!data->original_file)
6902 data->original_file = g_object_ref (data->file);
6903 data->original_error = g_error_copy (error);
6906 parent_file = g_file_get_parent (data->file);
6908 /* get parent path and try to change the folder to that */
6911 g_object_unref (data->file);
6912 data->file = parent_file;
6914 g_object_unref (cancellable);
6916 /* restart the update current folder operation */
6917 impl->reload_state = RELOAD_HAS_FOLDER;
6919 impl->update_current_folder_cancellable =
6920 _gtk_file_system_get_info (impl->file_system, data->file,
6922 update_current_folder_get_info_cb,
6925 set_busy_cursor (impl, TRUE);
6931 /* Error and bail out, ignoring "not found" errors since they're useless:
6932 * they only happen when a program defaults to a folder that has been (re)moved.
6934 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
6935 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6937 g_error_free (data->original_error);
6939 g_object_unref (data->original_file);
6945 if (data->original_file)
6947 /* Error and bail out, ignoring "not found" errors since they're useless:
6948 * they only happen when a program defaults to a folder that has been (re)moved.
6950 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
6951 error_changing_folder_dialog (impl, data->original_file, data->original_error);
6953 g_error_free (data->original_error);
6955 g_object_unref (data->original_file);
6958 if (! _gtk_file_info_consider_as_directory (info))
6961 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
6964 if (impl->current_folder != data->file)
6966 if (impl->current_folder)
6967 g_object_unref (impl->current_folder);
6969 impl->current_folder = g_object_ref (data->file);
6972 impl->reload_state = RELOAD_HAS_FOLDER;
6974 /* Update the widgets that may trigger a folder change themselves. */
6976 if (!impl->changing_folder)
6978 impl->changing_folder = TRUE;
6980 shortcuts_update_current_folder (impl);
6982 impl->changing_folder = FALSE;
6985 /* Set the folder on the save entry */
6987 if (impl->location_entry)
6989 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6990 impl->current_folder);
6992 if (data->clear_entry)
6993 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6996 /* Create a new list model. This is slightly evil; we store the result value
6997 * but perform more actions rather than returning immediately even if it
6998 * generates an error.
7000 set_list_model (impl, NULL);
7002 /* Refresh controls */
7004 shortcuts_find_current_folder (impl);
7006 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7008 check_preview_change (impl);
7009 bookmarks_check_add_sensitivity (impl);
7011 g_signal_emit_by_name (impl, "selection-changed", 0);
7014 g_object_unref (data->file);
7017 g_object_unref (cancellable);
7021 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7023 gboolean keep_trail,
7024 gboolean clear_entry,
7027 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7028 struct UpdateCurrentFolderData *data;
7030 profile_start ("start", NULL);
7032 g_object_ref (file);
7034 switch (impl->operation_mode)
7036 case OPERATION_MODE_SEARCH:
7037 search_switch_to_browse_mode (impl);
7039 case OPERATION_MODE_RECENT:
7040 recent_switch_to_browse_mode (impl);
7042 case OPERATION_MODE_BROWSE:
7046 if (impl->local_only && !g_file_is_native (file))
7048 g_set_error_literal (error,
7049 GTK_FILE_CHOOSER_ERROR,
7050 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7051 _("Cannot change to folder because it is not local"));
7053 g_object_unref (file);
7054 profile_end ("end - not local", NULL);
7058 if (impl->update_current_folder_cancellable)
7059 g_cancellable_cancel (impl->update_current_folder_cancellable);
7061 /* Test validity of path here. */
7062 data = g_new0 (struct UpdateCurrentFolderData, 1);
7064 data->file = g_object_ref (file);
7065 data->keep_trail = keep_trail;
7066 data->clear_entry = clear_entry;
7068 impl->reload_state = RELOAD_HAS_FOLDER;
7070 impl->update_current_folder_cancellable =
7071 _gtk_file_system_get_info (impl->file_system, file,
7073 update_current_folder_get_info_cb,
7076 set_busy_cursor (impl, TRUE);
7077 g_object_unref (file);
7079 profile_end ("end", NULL);
7084 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7086 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7088 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7089 impl->operation_mode == OPERATION_MODE_RECENT)
7092 if (impl->reload_state == RELOAD_EMPTY)
7094 /* We are unmapped, or we had an error while loading the last folder.
7095 * We'll return the folder used by the last invocation of the file chooser
7096 * since once we get (re)mapped, we'll load *that* folder anyway unless
7097 * the caller explicitly calls set_current_folder() on us.
7099 return get_file_for_last_folder_opened (impl);
7102 if (impl->current_folder)
7103 return g_object_ref (impl->current_folder);
7109 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7112 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7114 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7115 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7117 pending_select_files_free (impl);
7118 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7122 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7126 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7130 parent_file = g_file_get_parent (file);
7133 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7135 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7136 impl->operation_mode == OPERATION_MODE_RECENT ||
7137 impl->load_state == LOAD_EMPTY)
7143 g_assert (impl->current_folder != NULL);
7145 same_path = g_file_equal (parent_file, impl->current_folder);
7148 if (same_path && impl->load_state == LOAD_FINISHED)
7153 files.data = (gpointer) file;
7156 result = show_and_select_files (impl, &files);
7157 g_object_unref (parent_file);
7161 pending_select_files_add (impl, file);
7167 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7168 g_object_unref (parent_file);
7172 g_object_unref (parent_file);
7177 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7180 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7181 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7184 if (!impl->browse_files_model)
7187 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7192 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7197 maybe_select (GtkTreeModel *model,
7202 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7203 GtkTreeSelection *selection;
7206 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7208 gtk_tree_model_get (model, iter,
7209 MODEL_COL_IS_FOLDER, &is_folder,
7212 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7213 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7214 gtk_tree_selection_select_iter (selection, iter);
7216 gtk_tree_selection_unselect_iter (selection, iter);
7222 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7224 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7226 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7227 impl->operation_mode == OPERATION_MODE_RECENT)
7229 GtkTreeSelection *selection;
7231 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7232 gtk_tree_selection_select_all (selection);
7236 if (impl->select_multiple)
7237 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7238 maybe_select, impl);
7242 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7244 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7245 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7247 gtk_tree_selection_unselect_all (selection);
7248 pending_select_files_free (impl);
7251 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7253 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7255 * is_empty_ret - whether the file entry is totally empty
7257 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7258 * the path will be "$cwd/foobar")
7261 check_save_entry (GtkFileChooserDefault *impl,
7263 gboolean *is_well_formed_ret,
7264 gboolean *is_empty_ret,
7265 gboolean *is_file_part_empty_ret,
7266 gboolean *is_folder)
7268 GtkFileChooserEntry *chooser_entry;
7269 GFile *current_folder;
7270 const char *file_part;
7274 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7275 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7276 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7277 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7278 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7280 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7282 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7285 *is_well_formed_ret = TRUE;
7286 *is_empty_ret = TRUE;
7287 *is_file_part_empty_ret = TRUE;
7293 *is_empty_ret = FALSE;
7295 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7296 if (!current_folder)
7299 *is_well_formed_ret = FALSE;
7300 *is_file_part_empty_ret = FALSE;
7306 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7308 if (!file_part || file_part[0] == '\0')
7310 *file_ret = g_object_ref (current_folder);
7311 *is_well_formed_ret = TRUE;
7312 *is_file_part_empty_ret = TRUE;
7318 *is_file_part_empty_ret = FALSE;
7321 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7325 error_building_filename_dialog (impl, error);
7327 *is_well_formed_ret = FALSE;
7334 *is_well_formed_ret = TRUE;
7335 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7338 struct get_files_closure {
7339 GtkFileChooserDefault *impl;
7341 GFile *file_from_entry;
7345 get_files_foreach (GtkTreeModel *model,
7350 struct get_files_closure *info;
7352 GtkFileSystemModel *fs_model;
7355 fs_model = info->impl->browse_files_model;
7357 file = _gtk_file_system_model_get_file (fs_model, iter);
7359 return; /* We are on the editable row */
7361 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7362 info->result = g_slist_prepend (info->result, g_object_ref (file));
7366 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7368 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7369 struct get_files_closure info;
7370 GtkWindow *toplevel;
7371 GtkWidget *current_focus;
7372 gboolean file_list_seen;
7376 info.file_from_entry = NULL;
7378 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7379 return search_get_selected_files (impl);
7381 if (impl->operation_mode == OPERATION_MODE_RECENT)
7383 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7385 file_list_seen = TRUE;
7389 return recent_get_selected_files (impl);
7392 toplevel = get_toplevel (GTK_WIDGET (impl));
7394 current_focus = gtk_window_get_focus (toplevel);
7396 current_focus = NULL;
7398 file_list_seen = FALSE;
7399 if (current_focus == impl->browse_files_tree_view)
7401 GtkTreeSelection *selection;
7405 file_list_seen = TRUE;
7406 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7407 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7409 /* If there is no selection in the file list, we probably have this situation:
7411 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7412 * 2. He then double-clicked on a folder ("bar") in the file list
7414 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7415 * filename entry to see if that is the case.
7417 if (info.result == NULL && impl->location_entry)
7420 else if (impl->location_entry && current_focus == impl->location_entry)
7422 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7426 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7431 if (!is_well_formed)
7434 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7436 g_object_unref (info.file_from_entry);
7440 if (info.file_from_entry)
7441 info.result = g_slist_prepend (info.result, info.file_from_entry);
7442 else if (!file_list_seen)
7447 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7449 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7453 /* The focus is on a dialog's action area button or something else */
7454 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7455 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7463 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7464 * fall back to the current directory */
7465 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7466 info.result == NULL)
7468 GFile *current_folder;
7470 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7473 info.result = g_slist_prepend (info.result, current_folder);
7476 return g_slist_reverse (info.result);
7480 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7482 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7484 if (impl->preview_file)
7485 return g_object_ref (impl->preview_file);
7490 static GtkFileSystem *
7491 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7493 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7495 return impl->file_system;
7498 /* Shows or hides the filter widgets */
7500 show_filters (GtkFileChooserDefault *impl,
7504 gtk_widget_show (impl->filter_combo_hbox);
7506 gtk_widget_hide (impl->filter_combo_hbox);
7510 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7511 GtkFileFilter *filter)
7513 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7516 if (g_slist_find (impl->filters, filter))
7518 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7522 g_object_ref_sink (filter);
7523 impl->filters = g_slist_append (impl->filters, filter);
7525 name = gtk_file_filter_get_name (filter);
7527 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7529 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7531 if (!g_slist_find (impl->filters, impl->current_filter))
7532 set_current_filter (impl, filter);
7534 show_filters (impl, TRUE);
7538 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7539 GtkFileFilter *filter)
7541 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7542 GtkTreeModel *model;
7546 filter_index = g_slist_index (impl->filters, filter);
7548 if (filter_index < 0)
7550 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7554 impl->filters = g_slist_remove (impl->filters, filter);
7556 if (filter == impl->current_filter)
7559 set_current_filter (impl, impl->filters->data);
7561 set_current_filter (impl, NULL);
7564 /* Remove row from the combo box */
7565 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7566 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7567 g_assert_not_reached ();
7569 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7571 g_object_unref (filter);
7574 show_filters (impl, FALSE);
7578 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7580 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7582 return g_slist_copy (impl->filters);
7585 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7587 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7590 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7593 struct AddShortcutData
7595 GtkFileChooserDefault *impl;
7600 add_shortcut_get_info_cb (GCancellable *cancellable,
7602 const GError *error,
7606 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7607 struct AddShortcutData *data = user_data;
7609 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7612 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7614 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7617 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7619 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7622 g_object_unref (data->impl);
7623 g_object_unref (data->file);
7626 g_object_unref (cancellable);
7630 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7634 GCancellable *cancellable;
7635 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7636 struct AddShortcutData *data;
7640 /* Avoid adding duplicates */
7641 pos = shortcut_find_position (impl, file);
7642 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7646 uri = g_file_get_uri (file);
7647 /* translators, "Shortcut" means "Bookmark" here */
7649 GTK_FILE_CHOOSER_ERROR,
7650 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7651 _("Shortcut %s already exists"),
7658 for (l = impl->loading_shortcuts; l; l = l->next)
7660 GCancellable *c = l->data;
7663 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7664 if (f && g_file_equal (file, f))
7668 uri = g_file_get_uri (file);
7670 GTK_FILE_CHOOSER_ERROR,
7671 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7672 _("Shortcut %s already exists"),
7680 data = g_new0 (struct AddShortcutData, 1);
7681 data->impl = g_object_ref (impl);
7682 data->file = g_object_ref (file);
7684 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7686 add_shortcut_get_info_cb, data);
7691 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7692 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7698 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7702 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7709 for (l = impl->loading_shortcuts; l; l = l->next)
7711 GCancellable *c = l->data;
7714 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7715 if (f && g_file_equal (file, f))
7717 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7718 g_cancellable_cancel (c);
7723 if (impl->num_shortcuts == 0)
7726 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7727 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7728 g_assert_not_reached ();
7730 for (i = 0; i < impl->num_shortcuts; i++)
7733 ShortcutType shortcut_type;
7736 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7737 SHORTCUTS_COL_DATA, &col_data,
7738 SHORTCUTS_COL_TYPE, &shortcut_type,
7740 g_assert (col_data != NULL);
7741 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7743 shortcut = col_data;
7744 if (g_file_equal (shortcut, file))
7746 shortcuts_remove_rows (impl, pos + i, 1);
7747 impl->num_shortcuts--;
7751 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7752 g_assert_not_reached ();
7757 uri = g_file_get_uri (file);
7758 /* translators, "Shortcut" means "Bookmark" here */
7760 GTK_FILE_CHOOSER_ERROR,
7761 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7762 _("Shortcut %s does not exist"),
7770 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7772 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7778 if (impl->num_shortcuts == 0)
7781 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7782 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7783 g_assert_not_reached ();
7787 for (i = 0; i < impl->num_shortcuts; i++)
7790 ShortcutType shortcut_type;
7793 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7794 SHORTCUTS_COL_DATA, &col_data,
7795 SHORTCUTS_COL_TYPE, &shortcut_type,
7797 g_assert (col_data != NULL);
7798 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7800 shortcut = col_data;
7801 list = g_slist_prepend (list, g_object_ref (shortcut));
7803 if (i != impl->num_shortcuts - 1)
7805 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7806 g_assert_not_reached ();
7810 return g_slist_reverse (list);
7813 /* Guesses a size based upon font sizes */
7815 find_good_size_from_style (GtkWidget *widget,
7819 GtkStyleContext *context;
7820 GtkStateFlags state;
7825 context = gtk_widget_get_style_context (widget);
7826 state = gtk_widget_get_state_flags (widget);
7828 screen = gtk_widget_get_screen (widget);
7831 resolution = gdk_screen_get_resolution (screen);
7832 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7836 resolution = 96.0; /* wheeee */
7838 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
7839 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7841 *width = font_size * NUM_CHARS;
7842 *height = font_size * NUM_LINES;
7846 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7847 gint *default_width,
7848 gint *default_height)
7850 GtkFileChooserDefault *impl;
7852 int x, y, width, height;
7854 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7856 settings_ensure (impl);
7858 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
7859 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
7861 if (x >= 0 && y >= 0 && width > 0 && height > 0)
7863 *default_width = width;
7864 *default_height = height;
7868 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7870 if (impl->preview_widget_active &&
7871 impl->preview_widget &&
7872 gtk_widget_get_visible (impl->preview_widget))
7874 gtk_widget_get_preferred_size (impl->preview_box,
7876 *default_width += PREVIEW_HBOX_SPACING + req.width;
7879 if (impl->extra_widget &&
7880 gtk_widget_get_visible (impl->extra_widget))
7882 gtk_widget_get_preferred_size (impl->extra_align,
7884 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
7888 struct switch_folder_closure {
7889 GtkFileChooserDefault *impl;
7894 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7896 switch_folder_foreach_cb (GtkTreeModel *model,
7901 struct switch_folder_closure *closure;
7905 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
7906 closure->num_selected++;
7909 /* Changes to the selected folder in the list view */
7911 switch_to_selected_folder (GtkFileChooserDefault *impl)
7913 GtkTreeSelection *selection;
7914 struct switch_folder_closure closure;
7916 /* We do this with foreach() rather than get_selected() as we may be in
7917 * multiple selection mode
7920 closure.impl = impl;
7921 closure.file = NULL;
7922 closure.num_selected = 0;
7924 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7925 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7927 g_assert (closure.file && closure.num_selected == 1);
7929 change_folder_and_display_error (impl, closure.file, FALSE);
7932 /* Gets the GFileInfo for the selected row in the file list; assumes single
7936 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7937 gboolean *had_selection)
7939 GtkTreeSelection *selection;
7943 g_assert (!impl->select_multiple);
7944 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7945 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7947 *had_selection = FALSE;
7951 *had_selection = TRUE;
7953 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
7957 /* Gets the display name of the selected file in the file list; assumes single
7958 * selection mode and that something is selected.
7960 static const gchar *
7961 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7964 gboolean had_selection;
7966 info = get_selected_file_info_from_file_list (impl, &had_selection);
7967 g_assert (had_selection);
7968 g_assert (info != NULL);
7970 return g_file_info_get_display_name (info);
7974 add_custom_button_to_dialog (GtkDialog *dialog,
7975 const gchar *mnemonic_label,
7976 const gchar *stock_id,
7981 button = gtk_button_new_with_mnemonic (mnemonic_label);
7982 gtk_widget_set_can_default (button, TRUE);
7983 gtk_button_set_image (GTK_BUTTON (button),
7984 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7985 gtk_widget_show (button);
7987 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7990 /* Presents an overwrite confirmation dialog; returns whether we should accept
7994 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7995 const gchar *file_part,
7996 const gchar *folder_display_name)
7998 GtkWindow *toplevel;
8002 toplevel = get_toplevel (GTK_WIDGET (impl));
8004 dialog = gtk_message_dialog_new (toplevel,
8005 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8006 GTK_MESSAGE_QUESTION,
8008 _("A file named \"%s\" already exists. Do you want to replace it?"),
8010 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8011 _("The file already exists in \"%s\". Replacing it will "
8012 "overwrite its contents."),
8013 folder_display_name);
8015 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8016 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8017 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8018 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8019 GTK_RESPONSE_ACCEPT,
8020 GTK_RESPONSE_CANCEL,
8022 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8024 if (gtk_window_has_group (toplevel))
8025 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8026 GTK_WINDOW (dialog));
8028 response = gtk_dialog_run (GTK_DIALOG (dialog));
8030 gtk_widget_destroy (dialog);
8032 return (response == GTK_RESPONSE_ACCEPT);
8035 struct GetDisplayNameData
8037 GtkFileChooserDefault *impl;
8042 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8044 const GError *error,
8047 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8048 gboolean should_respond = FALSE;
8049 struct GetDisplayNameData *data = user_data;
8051 if (cancellable != data->impl->should_respond_get_info_cancellable)
8054 data->impl->should_respond_get_info_cancellable = NULL;
8060 /* Huh? Did the folder disappear? Let the caller deal with it */
8061 should_respond = TRUE;
8063 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8065 set_busy_cursor (data->impl, FALSE);
8067 g_signal_emit_by_name (data->impl, "response-requested");
8070 g_object_unref (data->impl);
8071 g_free (data->file_part);
8074 g_object_unref (cancellable);
8077 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8078 * should respond. Can get the file part from the file list or the save entry.
8081 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8082 const gchar *file_part,
8085 GtkFileChooserConfirmation conf;
8087 if (!impl->do_overwrite_confirmation)
8090 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8092 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8096 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8098 struct GetDisplayNameData *data;
8100 g_assert (file_part != NULL);
8102 data = g_new0 (struct GetDisplayNameData, 1);
8103 data->impl = g_object_ref (impl);
8104 data->file_part = g_strdup (file_part);
8106 if (impl->should_respond_get_info_cancellable)
8107 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8109 impl->should_respond_get_info_cancellable =
8110 _gtk_file_system_get_info (impl->file_system, parent_file,
8111 "standard::display-name",
8112 confirmation_confirm_get_info_cb,
8114 set_busy_cursor (data->impl, TRUE);
8118 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8121 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8125 g_assert_not_reached ();
8130 struct FileExistsData
8132 GtkFileChooserDefault *impl;
8133 gboolean file_exists_and_is_not_folder;
8139 name_entry_get_parent_info_cb (GCancellable *cancellable,
8141 const GError *error,
8144 gboolean parent_is_folder;
8145 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8146 struct FileExistsData *data = user_data;
8148 if (cancellable != data->impl->should_respond_get_info_cancellable)
8151 data->impl->should_respond_get_info_cancellable = NULL;
8153 set_busy_cursor (data->impl, FALSE);
8159 parent_is_folder = FALSE;
8161 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8163 if (parent_is_folder)
8165 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8167 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) */
8169 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8171 if (data->file_exists_and_is_not_folder)
8176 /* Dup the string because the string may be modified
8177 * depending on what clients do in the confirm-overwrite
8178 * signal and this corrupts the pointer
8180 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8181 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8185 g_signal_emit_by_name (data->impl, "response-requested");
8188 g_signal_emit_by_name (data->impl, "response-requested");
8190 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8191 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8193 GError *mkdir_error = NULL;
8195 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8196 * "/blah/nonexistent" you *will* want a folder created.
8199 set_busy_cursor (data->impl, TRUE);
8200 g_file_make_directory (data->file, NULL, &mkdir_error);
8201 set_busy_cursor (data->impl, FALSE);
8204 g_signal_emit_by_name (data->impl, "response-requested");
8206 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8209 g_assert_not_reached ();
8215 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8216 error_with_file_under_nonfolder (data->impl, data->parent_file);
8222 /* The parent folder is not readable for some reason */
8224 error_copy = g_error_copy (error);
8225 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8230 g_object_unref (data->impl);
8231 g_object_unref (data->file);
8232 g_object_unref (data->parent_file);
8235 g_object_unref (cancellable);
8239 file_exists_get_info_cb (GCancellable *cancellable,
8241 const GError *error,
8244 gboolean data_ownership_taken = FALSE;
8245 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8246 gboolean file_exists;
8248 gboolean needs_parent_check = FALSE;
8249 struct FileExistsData *data = user_data;
8251 if (cancellable != data->impl->file_exists_get_info_cancellable)
8254 data->impl->file_exists_get_info_cancellable = NULL;
8256 set_busy_cursor (data->impl, FALSE);
8261 file_exists = (info != NULL);
8262 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8264 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8267 change_folder_and_display_error (data->impl, data->file, TRUE);
8271 g_signal_emit_by_name (data->impl, "response-requested"); /* user typed an existing filename; we are done */
8273 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8276 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8278 if (file_exists && !is_folder)
8280 /* Oops, the user typed the name of an existing path which is not
8283 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8284 g_error_copy (error));
8288 needs_parent_check = TRUE;
8291 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8295 needs_parent_check = TRUE;
8301 /* User typed a folder; we are done */
8302 g_signal_emit_by_name (data->impl, "response-requested");
8305 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8308 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8311 change_folder_and_display_error (data->impl, data->file, TRUE);
8313 needs_parent_check = TRUE;
8317 g_assert_not_reached();
8320 if (needs_parent_check) {
8321 /* check that everything up to the last path component exists (i.e. the parent) */
8323 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8324 data_ownership_taken = TRUE;
8326 if (data->impl->should_respond_get_info_cancellable)
8327 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8329 data->impl->should_respond_get_info_cancellable =
8330 _gtk_file_system_get_info (data->impl->file_system,
8333 name_entry_get_parent_info_cb,
8335 set_busy_cursor (data->impl, TRUE);
8339 if (!data_ownership_taken)
8341 g_object_unref (data->impl);
8342 g_object_unref (data->file);
8343 g_object_unref (data->parent_file);
8347 g_object_unref (cancellable);
8351 paste_text_received (GtkClipboard *clipboard,
8353 GtkFileChooserDefault *impl)
8360 file = g_file_new_for_uri (text);
8362 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8363 location_popup_handler (impl, text);
8365 g_object_unref (file);
8368 /* Handler for the "location-popup-on-paste" keybinding signal */
8370 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8372 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8373 GDK_SELECTION_CLIPBOARD);
8374 gtk_clipboard_request_text (clipboard,
8375 (GtkClipboardTextReceivedFunc) paste_text_received,
8379 /* Implementation for GtkFileChooserEmbed::should_respond() */
8381 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8383 GtkFileChooserDefault *impl;
8384 GtkWidget *toplevel;
8385 GtkWidget *current_focus;
8387 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8389 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8390 g_assert (GTK_IS_WINDOW (toplevel));
8392 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8394 if (current_focus == impl->browse_files_tree_view)
8396 /* The following array encodes what we do based on the impl->action and the
8397 * number of files selected.
8400 NOOP, /* Do nothing (don't respond) */
8401 RESPOND, /* Respond immediately */
8402 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8403 ALL_FILES, /* Respond only if everything selected is a file */
8404 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8405 SAVE_ENTRY, /* Go to the code for handling the save entry */
8406 NOT_REACHED /* Sanity check */
8408 static const ActionToTake what_to_do[4][3] = {
8409 /* 0 selected 1 selected many selected */
8410 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8411 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8412 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8413 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8417 gboolean all_files, all_folders;
8419 ActionToTake action;
8423 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8425 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8426 return search_should_respond (impl);
8428 if (impl->operation_mode == OPERATION_MODE_RECENT)
8430 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8433 return recent_should_respond (impl);
8436 selection_check (impl, &num_selected, &all_files, &all_folders);
8438 if (num_selected > 2)
8443 action = what_to_do [impl->action] [k];
8453 case RESPOND_OR_SWITCH:
8454 g_assert (num_selected == 1);
8458 switch_to_selected_folder (impl);
8461 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8462 return should_respond_after_confirm_overwrite (impl,
8463 get_display_name_from_file_list (impl),
8464 impl->current_folder);
8478 g_assert_not_reached ();
8481 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8484 gboolean is_well_formed, is_empty, is_file_part_empty;
8487 GtkFileChooserEntry *entry;
8492 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8493 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8494 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8495 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8496 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8498 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8499 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8501 if (!is_well_formed)
8504 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8505 && impl->operation_mode == OPERATION_MODE_RECENT)
8506 set_info_bar (impl, INFO_BAR_ERROR_NO_FOLDER);
8513 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8514 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8516 set_info_bar (impl, INFO_BAR_ERROR_NO_FILENAME);
8517 gtk_widget_grab_focus (impl->location_entry);
8524 g_assert (file != NULL);
8529 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8530 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8532 change_folder_and_display_error (impl, file, TRUE);
8535 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8536 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8538 /* The folder already exists, so we do not need to create it.
8539 * Just respond to terminate the dialog.
8545 g_assert_not_reached ();
8551 struct FileExistsData *data;
8553 /* We need to check whether file exists and whether it is a folder -
8554 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8555 * negative (it doesn't know yet if your last path component is a
8559 data = g_new0 (struct FileExistsData, 1);
8560 data->impl = g_object_ref (impl);
8561 data->file = g_object_ref (file);
8562 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8564 if (impl->file_exists_get_info_cancellable)
8565 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8567 impl->file_exists_get_info_cancellable =
8568 _gtk_file_system_get_info (impl->file_system, file,
8570 file_exists_get_info_cb,
8573 set_busy_cursor (impl, TRUE);
8577 g_error_free (error);
8580 g_object_unref (file);
8583 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8585 /* The focus is on a dialog's action area button, *and* the widget that
8586 * was focused immediately before it is the file list.
8590 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8592 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8595 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8597 /* The focus is on a dialog's action area button, *and* the widget that
8598 * was focused immediately before it is the location entry.
8603 /* The focus is on a dialog's action area button or something else */
8604 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8605 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8610 g_assert_not_reached ();
8614 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8616 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8618 GtkFileChooserDefault *impl;
8621 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8623 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8624 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8626 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8627 widget = impl->browse_files_tree_view;
8629 widget = impl->location_entry;
8631 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8632 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8633 widget = impl->location_entry;
8636 g_assert_not_reached ();
8640 g_assert (widget != NULL);
8641 gtk_widget_grab_focus (widget);
8644 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8646 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8656 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8657 *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 */
8660 /* Constructs a list of the selected paths in search mode */
8662 search_get_selected_files (GtkFileChooserDefault *impl)
8665 GtkTreeSelection *selection;
8669 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8670 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8671 result = g_slist_reverse (result);
8676 /* Called from ::should_respond(). We return whether there are selected files
8677 * in the search list.
8680 search_should_respond (GtkFileChooserDefault *impl)
8682 GtkTreeSelection *selection;
8684 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8686 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8687 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8690 /* Adds one hit from the search engine to the search_model */
8692 search_add_hit (GtkFileChooserDefault *impl,
8697 file = g_file_new_for_uri (uri);
8701 if (!g_file_is_native (file))
8703 g_object_unref (file);
8707 _gtk_file_system_model_add_and_query_file (impl->search_model,
8711 g_object_unref (file);
8714 /* Callback used from GtkSearchEngine when we get new hits */
8716 search_engine_hits_added_cb (GtkSearchEngine *engine,
8720 GtkFileChooserDefault *impl;
8723 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8725 for (l = hits; l; l = l->next)
8726 search_add_hit (impl, (gchar*)l->data);
8729 /* Callback used from GtkSearchEngine when the query is done running */
8731 search_engine_finished_cb (GtkSearchEngine *engine,
8734 GtkFileChooserDefault *impl;
8736 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8739 /* EB: setting the model here will avoid loads of row events,
8740 * but it'll make the search look like blocked.
8742 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8743 GTK_TREE_MODEL (impl->search_model));
8746 /* FMQ: if search was empty, say that we got no hits */
8747 set_busy_cursor (impl, FALSE);
8750 /* Displays a generic error when we cannot create a GtkSearchEngine.
8751 * It would be better if _gtk_search_engine_new() gave us a GError
8752 * with a better message, but it doesn't do that right now.
8755 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8757 error_message (impl,
8758 _("Could not start the search process"),
8759 _("The program was not able to create a connection to the indexer "
8760 "daemon. Please make sure it is running."));
8764 search_engine_error_cb (GtkSearchEngine *engine,
8765 const gchar *message,
8768 GtkFileChooserDefault *impl;
8770 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8772 search_stop_searching (impl, TRUE);
8773 error_message (impl, _("Could not send the search request"), message);
8775 set_busy_cursor (impl, FALSE);
8778 /* Frees the data in the search_model */
8780 search_clear_model (GtkFileChooserDefault *impl,
8781 gboolean remove_from_treeview)
8783 if (!impl->search_model)
8786 g_object_unref (impl->search_model);
8787 impl->search_model = NULL;
8789 if (remove_from_treeview)
8790 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8793 /* Stops any ongoing searches; does not touch the search_model */
8795 search_stop_searching (GtkFileChooserDefault *impl,
8796 gboolean remove_query)
8798 if (remove_query && impl->search_query)
8800 g_object_unref (impl->search_query);
8801 impl->search_query = NULL;
8804 if (impl->search_engine)
8806 _gtk_search_engine_stop (impl->search_engine);
8808 g_object_unref (impl->search_engine);
8809 impl->search_engine = NULL;
8813 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8815 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8817 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8819 search_stop_searching (impl, FALSE);
8820 search_clear_model (impl, TRUE);
8822 gtk_widget_destroy (impl->search_hbox);
8823 impl->search_hbox = NULL;
8824 impl->search_entry = NULL;
8826 gtk_widget_show (impl->browse_path_bar);
8827 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
8828 gtk_widget_hide (impl->browse_new_folder_button);
8830 gtk_widget_show (impl->browse_new_folder_button);
8833 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8834 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8836 gtk_widget_show (impl->location_button);
8838 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8839 gtk_widget_show (impl->location_entry_box);
8842 impl->operation_mode = OPERATION_MODE_BROWSE;
8844 file_list_set_sort_column_ids (impl);
8847 /* Creates the search_model and puts it in the tree view */
8849 search_setup_model (GtkFileChooserDefault *impl)
8851 g_assert (impl->search_model == NULL);
8853 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
8855 MODEL_COLUMN_TYPES);
8857 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8861 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8865 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8869 set_sort_column (impl);
8871 /* EB: setting the model here will make the hits list update feel
8872 * more "alive" than setting the model at the end of the search
8875 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8876 GTK_TREE_MODEL (impl->search_model));
8879 /* Creates a new query with the specified text and launches it */
8881 search_start_query (GtkFileChooserDefault *impl,
8882 const gchar *query_text)
8884 search_stop_searching (impl, FALSE);
8885 search_clear_model (impl, TRUE);
8886 search_setup_model (impl);
8887 set_busy_cursor (impl, TRUE);
8889 if (impl->search_engine == NULL)
8890 impl->search_engine = _gtk_search_engine_new ();
8892 if (!impl->search_engine)
8894 set_busy_cursor (impl, FALSE);
8895 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
8899 if (!impl->search_query)
8901 impl->search_query = _gtk_query_new ();
8902 _gtk_query_set_text (impl->search_query, query_text);
8905 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
8907 g_signal_connect (impl->search_engine, "hits-added",
8908 G_CALLBACK (search_engine_hits_added_cb), impl);
8909 g_signal_connect (impl->search_engine, "finished",
8910 G_CALLBACK (search_engine_finished_cb), impl);
8911 g_signal_connect (impl->search_engine, "error",
8912 G_CALLBACK (search_engine_error_cb), impl);
8914 _gtk_search_engine_start (impl->search_engine);
8917 /* Callback used when the user presses Enter while typing on the search
8918 * entry; starts the query
8921 search_entry_activate_cb (GtkEntry *entry,
8924 GtkFileChooserDefault *impl;
8927 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8929 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
8930 if (strlen (text) == 0)
8933 /* reset any existing query object */
8934 if (impl->search_query)
8936 g_object_unref (impl->search_query);
8937 impl->search_query = NULL;
8940 search_start_query (impl, text);
8944 focus_entry_idle_cb (GtkFileChooserDefault *impl)
8946 GDK_THREADS_ENTER ();
8948 g_source_destroy (impl->focus_entry_idle);
8949 impl->focus_entry_idle = NULL;
8951 if (impl->search_entry)
8952 gtk_widget_grab_focus (impl->search_entry);
8954 GDK_THREADS_LEAVE ();
8960 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
8962 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
8963 * pane, we get a selection-changed signal and we set up the search widgets.
8964 * However, gtk_tree_view_button_press() focuses the treeview *after* making
8965 * the change to the selection. So, we need to re-focus the search entry
8966 * after the treeview has finished doing its work; we'll do that in an idle
8970 if (!impl->focus_entry_idle)
8971 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
8974 /* Hides the path bar and creates the search entry */
8976 search_setup_widgets (GtkFileChooserDefault *impl)
8982 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
8986 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
8987 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
8991 label = gtk_label_new (NULL);
8992 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
8993 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
8994 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
8999 impl->search_entry = gtk_entry_new ();
9000 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9001 g_signal_connect (impl->search_entry, "activate",
9002 G_CALLBACK (search_entry_activate_cb),
9004 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9006 /* if there already is a query, restart it */
9007 if (impl->search_query)
9009 gchar *query = _gtk_query_get_text (impl->search_query);
9013 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9014 search_start_query (impl, query);
9020 g_object_unref (impl->search_query);
9021 impl->search_query = NULL;
9025 gtk_widget_hide (impl->browse_path_bar);
9026 gtk_widget_hide (impl->browse_new_folder_button);
9028 /* Box for search widgets */
9029 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9030 gtk_widget_show_all (impl->search_hbox);
9031 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9033 /* Hide the location widgets temporarily */
9035 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9036 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9038 gtk_widget_hide (impl->location_button);
9039 gtk_widget_hide (impl->location_entry_box);
9042 focus_search_entry_in_idle (impl);
9044 /* FMQ: hide the filter combo? */
9047 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9049 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9053 case OPERATION_MODE_BROWSE:
9054 stop_loading_and_clear_list_model (impl, TRUE);
9057 case OPERATION_MODE_SEARCH:
9058 search_stop_searching (impl, FALSE);
9059 search_clear_model (impl, TRUE);
9061 gtk_widget_destroy (impl->search_hbox);
9062 impl->search_hbox = NULL;
9063 impl->search_entry = NULL;
9066 case OPERATION_MODE_RECENT:
9067 recent_stop_loading (impl);
9068 recent_clear_model (impl, TRUE);
9070 gtk_widget_destroy (impl->recent_hbox);
9071 impl->recent_hbox = NULL;
9076 /* Sometimes we need to frob the selection in the shortcuts list manually */
9078 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
9080 GtkTreeSelection *selection;
9083 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
9085 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
9087 path = gtk_tree_path_new_from_indices (pos, -1);
9088 gtk_tree_selection_select_path (selection, path);
9089 gtk_tree_path_free (path);
9091 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
9094 /* Main entry point to the searching functions; this gets called when the user
9095 * activates the Search shortcut.
9098 search_activate (GtkFileChooserDefault *impl)
9100 OperationMode previous_mode;
9102 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9104 focus_search_entry_in_idle (impl);
9108 previous_mode = impl->operation_mode;
9109 impl->operation_mode = OPERATION_MODE_SEARCH;
9111 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
9113 stop_operation (impl, previous_mode);
9115 g_assert (impl->search_hbox == NULL);
9116 g_assert (impl->search_entry == NULL);
9117 g_assert (impl->search_model == NULL);
9119 search_setup_widgets (impl);
9120 file_list_set_sort_column_ids (impl);
9124 * Recent files support
9127 /* Frees the data in the recent_model */
9129 recent_clear_model (GtkFileChooserDefault *impl,
9130 gboolean remove_from_treeview)
9132 if (!impl->recent_model)
9135 if (remove_from_treeview)
9136 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9138 g_object_unref (impl->recent_model);
9139 impl->recent_model = NULL;
9142 /* Stops any ongoing loading of the recent files list; does
9143 * not touch the recent_model
9146 recent_stop_loading (GtkFileChooserDefault *impl)
9148 if (impl->load_recent_id)
9150 g_source_remove (impl->load_recent_id);
9151 impl->load_recent_id = 0;
9155 /* Stops any pending load, clears the file list, and switches
9156 * back to OPERATION_MODE_BROWSE
9159 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9161 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9163 recent_stop_loading (impl);
9164 recent_clear_model (impl, TRUE);
9166 gtk_widget_destroy (impl->recent_hbox);
9167 impl->recent_hbox = NULL;
9169 gtk_widget_show (impl->browse_path_bar);
9170 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9171 gtk_widget_hide (impl->browse_new_folder_button);
9173 gtk_widget_show (impl->browse_new_folder_button);
9175 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9176 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9178 gtk_widget_show (impl->location_button);
9180 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9181 gtk_widget_show (impl->location_entry_box);
9184 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9186 impl->operation_mode = OPERATION_MODE_BROWSE;
9188 file_list_set_sort_column_ids (impl);
9192 recent_setup_model (GtkFileChooserDefault *impl)
9194 g_assert (impl->recent_model == NULL);
9196 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9198 MODEL_COLUMN_TYPES);
9200 _gtk_file_system_model_set_filter (impl->recent_model,
9201 impl->current_filter);
9202 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9206 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9210 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9214 set_sort_column (impl);
9219 GtkFileChooserDefault *impl;
9221 guint needs_sorting : 1;
9225 recent_idle_cleanup (gpointer data)
9227 RecentLoadData *load_data = data;
9228 GtkFileChooserDefault *impl = load_data->impl;
9230 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9231 GTK_TREE_MODEL (impl->recent_model));
9233 set_busy_cursor (impl, FALSE);
9235 impl->load_recent_id = 0;
9241 recent_sort_mru (gconstpointer a,
9244 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9245 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9247 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9251 get_recent_files_limit (GtkWidget *widget)
9253 GtkSettings *settings;
9256 if (gtk_widget_has_screen (widget))
9257 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9259 settings = gtk_settings_get_default ();
9261 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9266 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9268 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9274 limit = get_recent_files_limit (GTK_WIDGET (impl));
9278 for (l = items; l; l = l->next)
9280 GtkRecentInfo *info = l->data;
9283 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9284 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9287 g_object_unref (file);
9290 if (limit != -1 && n >= limit)
9296 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9301 folders = _gtk_file_chooser_extract_recent_folders (items);
9303 for (l = folders; l; l = l->next)
9305 GFile *folder = l->data;
9307 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9312 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9313 g_list_free (folders);
9317 recent_idle_load (gpointer data)
9319 RecentLoadData *load_data = data;
9320 GtkFileChooserDefault *impl = load_data->impl;
9322 if (!impl->recent_manager)
9325 /* first iteration: load all the items */
9326 if (!load_data->items)
9328 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9329 if (!load_data->items)
9332 load_data->needs_sorting = TRUE;
9337 /* second iteration: MRU sorting and clamping, and populating the model */
9338 if (load_data->needs_sorting)
9340 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9342 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9343 populate_model_with_recent_items (impl, load_data->items);
9345 populate_model_with_folders (impl, load_data->items);
9347 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9348 g_list_free (load_data->items);
9349 load_data->items = NULL;
9356 recent_start_loading (GtkFileChooserDefault *impl)
9358 RecentLoadData *load_data;
9360 recent_stop_loading (impl);
9361 recent_clear_model (impl, TRUE);
9362 recent_setup_model (impl);
9363 set_busy_cursor (impl, TRUE);
9365 g_assert (impl->load_recent_id == 0);
9367 load_data = g_new (RecentLoadData, 1);
9368 load_data->impl = impl;
9369 load_data->items = NULL;
9370 load_data->needs_sorting = TRUE;
9372 /* begin lazy loading the recent files into the model */
9373 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9376 recent_idle_cleanup);
9380 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9390 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9391 *list = g_slist_prepend (*list, file);
9394 /* Constructs a list of the selected paths in recent files mode */
9396 recent_get_selected_files (GtkFileChooserDefault *impl)
9399 GtkTreeSelection *selection;
9403 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9404 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9405 result = g_slist_reverse (result);
9410 /* Called from ::should_respond(). We return whether there are selected
9411 * files in the recent files list.
9414 recent_should_respond (GtkFileChooserDefault *impl)
9416 GtkTreeSelection *selection;
9418 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9420 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9421 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9424 /* Hide the location widgets temporarily */
9426 recent_hide_entry (GtkFileChooserDefault *impl)
9432 /* For Save mode, we don't want this icon/label - we want update_path_bar() to do its thing instead */
9433 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9435 set_info_bar (impl, INFO_BAR_SELECT_A_FOLDER);
9440 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
9441 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
9442 gtk_widget_show (image);
9445 label = gtk_label_new (NULL);
9446 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
9447 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9448 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
9449 gtk_widget_show (label);
9453 gtk_widget_hide (impl->browse_path_bar);
9454 gtk_widget_hide (impl->browse_new_folder_button);
9456 /* Hide the location widgets temporarily */
9457 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9458 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9460 gtk_widget_hide (impl->location_button);
9461 gtk_widget_hide (impl->location_entry_box);
9465 /* Main entry point to the recent files functions; this gets called when
9466 * the user activates the Recently Used shortcut.
9469 recent_activate (GtkFileChooserDefault *impl)
9471 OperationMode previous_mode;
9473 if (impl->operation_mode == OPERATION_MODE_RECENT)
9476 previous_mode = impl->operation_mode;
9477 impl->operation_mode = OPERATION_MODE_RECENT;
9479 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
9481 stop_operation (impl, previous_mode);
9483 recent_hide_entry (impl);
9485 file_list_set_sort_column_ids (impl);
9486 recent_start_loading (impl);
9490 set_current_filter (GtkFileChooserDefault *impl,
9491 GtkFileFilter *filter)
9493 if (impl->current_filter != filter)
9497 /* NULL filters are allowed to reset to non-filtered status
9499 filter_index = g_slist_index (impl->filters, filter);
9500 if (impl->filters && filter && filter_index < 0)
9503 if (impl->current_filter)
9504 g_object_unref (impl->current_filter);
9505 impl->current_filter = filter;
9506 if (impl->current_filter)
9508 g_object_ref_sink (impl->current_filter);
9512 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9515 if (impl->browse_files_model)
9516 install_list_model_filter (impl);
9518 if (impl->search_model)
9519 _gtk_file_system_model_set_filter (impl->search_model, filter);
9521 if (impl->recent_model)
9522 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9524 g_object_notify (G_OBJECT (impl), "filter");
9529 filter_combo_changed (GtkComboBox *combo_box,
9530 GtkFileChooserDefault *impl)
9532 gint new_index = gtk_combo_box_get_active (combo_box);
9533 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9535 set_current_filter (impl, new_filter);
9539 check_preview_change (GtkFileChooserDefault *impl)
9541 GtkTreePath *cursor_path;
9543 char *new_display_name;
9544 GtkTreeModel *model;
9546 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9547 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9552 gtk_tree_model_get_iter (model, &iter, cursor_path);
9553 gtk_tree_model_get (model, &iter,
9554 MODEL_COL_FILE, &new_file,
9555 MODEL_COL_NAME, &new_display_name,
9558 gtk_tree_path_free (cursor_path);
9563 new_display_name = NULL;
9566 if (new_file != impl->preview_file &&
9567 !(new_file && impl->preview_file &&
9568 g_file_equal (new_file, impl->preview_file)))
9570 if (impl->preview_file)
9572 g_object_unref (impl->preview_file);
9573 g_free (impl->preview_display_name);
9578 impl->preview_file = new_file;
9579 impl->preview_display_name = new_display_name;
9583 impl->preview_file = NULL;
9584 impl->preview_display_name = NULL;
9585 g_free (new_display_name);
9588 if (impl->use_preview_label && impl->preview_label)
9589 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9591 g_signal_emit_by_name (impl, "update-preview");
9596 g_object_unref (new_file);
9598 g_free (new_display_name);
9603 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9604 GtkFileSystemVolume *volume,
9605 const GError *error,
9609 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9610 GtkFileChooserDefault *impl = data;
9612 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9615 impl->shortcuts_activate_iter_cancellable = NULL;
9617 set_busy_cursor (impl, FALSE);
9624 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9628 name = _gtk_file_system_volume_get_display_name (volume);
9629 msg = g_strdup_printf (_("Could not mount %s"), name);
9631 error_message (impl, msg, error->message);
9640 file = _gtk_file_system_volume_get_root (volume);
9643 change_folder_and_display_error (impl, file, FALSE);
9644 g_object_unref (file);
9648 g_object_unref (impl);
9649 g_object_unref (cancellable);
9653 /* Activates a volume by mounting it if necessary and then switching to its
9657 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9658 GtkFileSystemVolume *volume)
9662 switch (impl->operation_mode)
9664 case OPERATION_MODE_BROWSE:
9666 case OPERATION_MODE_SEARCH:
9667 search_switch_to_browse_mode (impl);
9669 case OPERATION_MODE_RECENT:
9670 recent_switch_to_browse_mode (impl);
9674 /* We ref the file chooser since volume_mount() may run a main loop, and the
9675 * user could close the file chooser window in the meantime.
9677 g_object_ref (impl);
9679 if (!_gtk_file_system_volume_is_mounted (volume))
9681 GMountOperation *mount_op;
9683 set_busy_cursor (impl, TRUE);
9685 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9686 impl->shortcuts_activate_iter_cancellable =
9687 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9688 shortcuts_activate_volume_mount_cb,
9689 g_object_ref (impl));
9690 g_object_unref (mount_op);
9694 file = _gtk_file_system_volume_get_root (volume);
9697 change_folder_and_display_error (impl, file, FALSE);
9698 g_object_unref (file);
9702 g_object_unref (impl);
9705 /* Opens the folder or volume at the specified iter in the shortcuts model */
9706 struct ShortcutsActivateData
9708 GtkFileChooserDefault *impl;
9713 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9715 const GError *error,
9718 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9719 struct ShortcutsActivateData *data = user_data;
9721 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9724 data->impl->shortcuts_activate_iter_cancellable = NULL;
9729 if (!error && _gtk_file_info_consider_as_directory (info))
9730 change_folder_and_display_error (data->impl, data->file, FALSE);
9732 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9737 g_object_unref (data->impl);
9738 g_object_unref (data->file);
9741 g_object_unref (cancellable);
9745 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9746 GtkFileSystemVolume *volume,
9747 const GError *error,
9750 struct ShortcutsActivateData *data = user_data;
9754 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9756 g_object_unref (data->impl);
9757 g_object_unref (data->file);
9763 data->impl->shortcuts_activate_iter_cancellable =
9764 _gtk_file_system_get_info (data->impl->file_system, data->file,
9766 shortcuts_activate_get_info_cb, data);
9769 _gtk_file_system_volume_unref (volume);
9773 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9777 ShortcutType shortcut_type;
9779 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9780 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9781 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9782 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9784 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9785 SHORTCUTS_COL_DATA, &col_data,
9786 SHORTCUTS_COL_TYPE, &shortcut_type,
9789 if (impl->shortcuts_activate_iter_cancellable)
9791 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9792 impl->shortcuts_activate_iter_cancellable = NULL;
9795 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9797 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9799 GtkFileSystemVolume *volume;
9803 shortcuts_activate_volume (impl, volume);
9805 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9807 struct ShortcutsActivateData *data;
9808 GtkFileSystemVolume *volume;
9810 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9812 data = g_new0 (struct ShortcutsActivateData, 1);
9813 data->impl = g_object_ref (impl);
9814 data->file = g_object_ref (col_data);
9816 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9818 GMountOperation *mount_operation;
9819 GtkWidget *toplevel;
9821 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9823 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9825 impl->shortcuts_activate_iter_cancellable =
9826 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9828 shortcuts_activate_mount_enclosing_volume,
9833 impl->shortcuts_activate_iter_cancellable =
9834 _gtk_file_system_get_info (impl->file_system, data->file,
9836 shortcuts_activate_get_info_cb, data);
9839 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9841 search_activate (impl);
9843 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9845 recent_activate (impl);
9849 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9851 shortcuts_key_press_event_cb (GtkWidget *widget,
9853 GtkFileChooserDefault *impl)
9857 modifiers = gtk_accelerator_get_default_mod_mask ();
9859 if (key_is_left_or_right (event))
9861 gtk_widget_grab_focus (impl->browse_files_tree_view);
9865 if ((event->keyval == GDK_KEY_BackSpace
9866 || event->keyval == GDK_KEY_Delete
9867 || event->keyval == GDK_KEY_KP_Delete)
9868 && (event->state & modifiers) == 0)
9870 remove_selected_bookmarks (impl);
9874 if ((event->keyval == GDK_KEY_F2)
9875 && (event->state & modifiers) == 0)
9877 rename_selected_bookmark (impl);
9885 shortcuts_select_func (GtkTreeSelection *selection,
9886 GtkTreeModel *model,
9888 gboolean path_currently_selected,
9891 GtkFileChooserDefault *impl = data;
9892 GtkTreeIter filter_iter;
9893 ShortcutType shortcut_type;
9895 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9896 g_assert_not_reached ();
9898 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9900 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9904 list_select_func (GtkTreeSelection *selection,
9905 GtkTreeModel *model,
9907 gboolean path_currently_selected,
9910 GtkFileChooserDefault *impl = data;
9912 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
9913 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9918 if (!gtk_tree_model_get_iter (model, &iter, path))
9920 gtk_tree_model_get (model, &iter,
9921 MODEL_COL_IS_FOLDER, &is_folder,
9931 list_selection_changed (GtkTreeSelection *selection,
9932 GtkFileChooserDefault *impl)
9934 /* See if we are in the new folder editable row for Save mode */
9935 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
9936 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9939 gboolean had_selection;
9941 info = get_selected_file_info_from_file_list (impl, &had_selection);
9943 goto out; /* normal processing */
9946 return; /* We are on the editable row for New Folder */
9951 if (impl->location_entry)
9952 update_chooser_entry (impl);
9954 update_path_bar (impl);
9956 check_preview_change (impl);
9957 bookmarks_check_add_sensitivity (impl);
9959 g_signal_emit_by_name (impl, "selection-changed", 0);
9962 /* Callback used when a row in the file list is activated */
9964 list_row_activated (GtkTreeView *tree_view,
9966 GtkTreeViewColumn *column,
9967 GtkFileChooserDefault *impl)
9971 GtkTreeModel *model;
9974 model = gtk_tree_view_get_model (tree_view);
9976 if (!gtk_tree_model_get_iter (model, &iter, path))
9979 gtk_tree_model_get (model, &iter,
9980 MODEL_COL_FILE, &file,
9981 MODEL_COL_IS_FOLDER, &is_folder,
9984 if (is_folder && file)
9986 change_folder_and_display_error (impl, file, FALSE);
9987 g_object_unref (file);
9991 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9992 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9993 g_signal_emit_by_name (impl, "file-activated");
9998 g_object_unref (file);
10002 path_bar_clicked (GtkPathBar *path_bar,
10005 gboolean child_is_hidden,
10006 GtkFileChooserDefault *impl)
10009 pending_select_files_add (impl, child_file);
10011 if (!change_folder_and_display_error (impl, file, FALSE))
10014 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10015 * show hidden files so that ".baz" appears in the file list, as it will still
10016 * be shown in the path bar: "/foo/[bar]/.baz"
10018 if (child_is_hidden)
10019 g_object_set (impl, "show-hidden", TRUE, NULL);
10023 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10025 GtkTreeViewColumn *column;
10026 GtkCellRenderer *renderer;
10027 GList *walk, *list;
10028 gboolean always_sensitive;
10030 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10031 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10033 /* Keep the following column numbers in sync with create_file_list() */
10036 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10037 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10038 for (walk = list; walk; walk = walk->next)
10040 renderer = walk->data;
10041 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10043 gtk_tree_view_column_set_attributes (column, renderer,
10044 "pixbuf", MODEL_COL_PIXBUF,
10049 gtk_tree_view_column_set_attributes (column, renderer,
10050 "text", MODEL_COL_NAME,
10051 "ellipsize", MODEL_COL_ELLIPSIZE,
10054 if (always_sensitive)
10055 g_object_set (renderer, "sensitive", TRUE, NULL);
10057 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10059 g_list_free (list);
10062 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10063 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10064 renderer = list->data;
10065 gtk_tree_view_column_set_attributes (column, renderer,
10066 "text", MODEL_COL_SIZE_TEXT,
10068 if (always_sensitive)
10069 g_object_set (renderer, "sensitive", TRUE, NULL);
10071 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10072 g_list_free (list);
10075 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10076 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10077 renderer = list->data;
10078 gtk_tree_view_column_set_attributes (column, renderer,
10079 "text", MODEL_COL_MTIME_TEXT,
10081 if (always_sensitive)
10082 g_object_set (renderer, "sensitive", TRUE, NULL);
10084 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10085 g_list_free (list);
10089 _gtk_file_chooser_default_new (void)
10091 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10095 location_set_user_text (GtkFileChooserDefault *impl,
10098 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10099 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10103 location_popup_handler (GtkFileChooserDefault *impl,
10106 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10108 GtkWidget *widget_to_focus;
10110 /* This will give us the location widgets back */
10111 switch (impl->operation_mode)
10113 case OPERATION_MODE_SEARCH:
10114 search_switch_to_browse_mode (impl);
10116 case OPERATION_MODE_RECENT:
10117 recent_switch_to_browse_mode (impl);
10119 case OPERATION_MODE_BROWSE:
10120 g_assert_not_reached ();
10124 if (impl->current_folder)
10125 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10127 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10128 widget_to_focus = impl->browse_files_tree_view;
10130 widget_to_focus = impl->location_entry;
10132 gtk_widget_grab_focus (widget_to_focus);
10136 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10137 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10139 LocationMode new_mode;
10143 /* since the user typed something, we unconditionally want to turn on the entry */
10144 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10146 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10147 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10148 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10149 new_mode = LOCATION_MODE_PATH_BAR;
10152 g_assert_not_reached ();
10156 location_mode_set (impl, new_mode, TRUE);
10157 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10160 location_set_user_text (impl, path);
10163 location_entry_set_initial_text (impl);
10164 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10168 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10169 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10171 gtk_widget_grab_focus (impl->location_entry);
10173 location_set_user_text (impl, path);
10176 g_assert_not_reached ();
10179 /* Handler for the "up-folder" keybinding signal */
10181 up_folder_handler (GtkFileChooserDefault *impl)
10183 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10186 /* Handler for the "down-folder" keybinding signal */
10188 down_folder_handler (GtkFileChooserDefault *impl)
10190 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10193 /* Switches to the shortcut in the specified index */
10195 switch_to_shortcut (GtkFileChooserDefault *impl,
10200 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10201 g_assert_not_reached ();
10203 shortcuts_activate_iter (impl, &iter);
10206 /* Handler for the "home-folder" keybinding signal */
10208 home_folder_handler (GtkFileChooserDefault *impl)
10210 if (impl->has_home)
10211 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10214 /* Handler for the "desktop-folder" keybinding signal */
10216 desktop_folder_handler (GtkFileChooserDefault *impl)
10218 if (impl->has_desktop)
10219 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10222 /* Handler for the "search-shortcut" keybinding signal */
10224 search_shortcut_handler (GtkFileChooserDefault *impl)
10226 if (impl->has_search)
10228 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10230 /* we want the entry widget to grab the focus the first
10231 * time, not the browse_files_tree_view widget.
10233 if (impl->search_entry)
10234 gtk_widget_grab_focus (impl->search_entry);
10238 /* Handler for the "recent-shortcut" keybinding signal */
10240 recent_shortcut_handler (GtkFileChooserDefault *impl)
10242 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10246 quick_bookmark_handler (GtkFileChooserDefault *impl,
10247 gint bookmark_index)
10252 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10255 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10257 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10258 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10261 gtk_tree_path_free (path);
10263 switch_to_shortcut (impl, bookmark_pos);
10267 show_hidden_handler (GtkFileChooserDefault *impl)
10269 g_object_set (impl,
10270 "show-hidden", !impl->show_hidden,
10275 /* Drag and drop interfaces */
10278 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10283 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10285 model->impl = NULL;
10288 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10290 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10293 ShortcutsPaneModelFilter *model;
10297 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10299 pos = *gtk_tree_path_get_indices (path);
10300 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10302 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10305 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10309 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10311 GtkSelectionData *selection_data)
10318 /* Fill the GtkTreeDragSourceIface vtable */
10320 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10322 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10323 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10327 /* Fill the GtkTreeDragDestIface vtable */
10329 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10331 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10332 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10336 static GtkTreeModel *
10337 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10338 GtkTreeModel *child_model,
10341 ShortcutsPaneModelFilter *model;
10343 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10344 "child-model", child_model,
10345 "virtual-root", root,
10348 model->impl = impl;
10350 return GTK_TREE_MODEL (model);