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>
89 /* Values for GtkSelection-related "info" fields */
90 #define SELECTION_TEXT 0
91 #define SELECTION_URI 1
94 #undef PROFILE_FILE_CHOOSER
95 #ifdef PROFILE_FILE_CHOOSER
102 #define PROFILE_INDENT 4
104 static int profile_indent;
107 profile_add_indent (int indent)
109 profile_indent += indent;
110 if (profile_indent < 0)
111 g_error ("You screwed up your indentation");
115 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
120 profile_add_indent (indent);
122 if (profile_indent == 0)
123 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
125 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
131 profile_add_indent (indent);
134 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
135 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
136 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
138 #define profile_start(x, y)
139 #define profile_end(x, y)
140 #define profile_msg(x, y)
145 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
147 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
148 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
149 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
151 #define MAX_LOADING_TIME 500
153 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
155 struct _GtkFileChooserDefaultClass
157 GtkBoxClass parent_class;
163 LOCATION_POPUP_ON_PASTE,
169 LOCATION_TOGGLE_POPUP,
177 static guint signals[LAST_SIGNAL] = { 0 };
179 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
181 SHORTCUTS_COL_PIXBUF,
185 SHORTCUTS_COL_REMOVABLE,
186 SHORTCUTS_COL_PIXBUF_VISIBLE,
187 SHORTCUTS_COL_CANCELLABLE,
188 SHORTCUTS_COL_NUM_COLUMNS
193 SHORTCUT_TYPE_VOLUME,
194 SHORTCUT_TYPE_SEPARATOR,
195 SHORTCUT_TYPE_SEARCH,
199 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
200 "standard::is-hidden,standard::is-backup,standard::size," \
201 "standard::content-type,time::modified"
203 /* the first 3 must be these due to settings caching sort column */
208 MODEL_COL_NAME_COLLATED,
212 MODEL_COL_MTIME_TEXT,
214 MODEL_COL_NUM_COLUMNS
217 /* This list of types is passed to _gtk_file_system_model_new*() */
218 #define MODEL_COLUMN_TYPES \
219 MODEL_COL_NUM_COLUMNS, \
220 G_TYPE_STRING, /* MODEL_COL_NAME */ \
221 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
222 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
223 G_TYPE_FILE, /* MODEL_COL_FILE */ \
224 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
225 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
226 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
227 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
228 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
229 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
231 /* Identifiers for target types */
236 /* Interesting places in the shortcuts bar */
240 SHORTCUTS_RECENT_SEPARATOR,
245 SHORTCUTS_BOOKMARKS_SEPARATOR,
247 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
248 SHORTCUTS_CURRENT_FOLDER
251 /* Icon size for if we can't get it from the theme */
252 #define FALLBACK_ICON_SIZE 16
254 #define PREVIEW_HBOX_SPACING 12
258 #define SETTINGS_KEY_LAST_FOLDER_URI "last-folder-uri"
259 #define SETTINGS_KEY_LOCATION_MODE "location-mode"
260 #define SETTINGS_KEY_SHOW_HIDDEN "show-hidden"
261 #define SETTINGS_KEY_SHOW_SIZE_COLUMN "show-size-column"
262 #define SETTINGS_KEY_SORT_COLUMN "sort-column"
263 #define SETTINGS_KEY_SORT_ORDER "sort-order"
264 #define SETTINGS_KEY_WINDOW_POSITION "window-position"
265 #define SETTINGS_KEY_WINDOW_SIZE "window-size"
267 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
268 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
270 static GObject* gtk_file_chooser_default_constructor (GType type,
271 guint n_construct_properties,
272 GObjectConstructParam *construct_params);
273 static void gtk_file_chooser_default_finalize (GObject *object);
274 static void gtk_file_chooser_default_set_property (GObject *object,
278 static void gtk_file_chooser_default_get_property (GObject *object,
282 static void gtk_file_chooser_default_dispose (GObject *object);
283 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
284 static void gtk_file_chooser_default_realize (GtkWidget *widget);
285 static void gtk_file_chooser_default_map (GtkWidget *widget);
286 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
287 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
288 GtkWidget *previous_toplevel);
289 static void gtk_file_chooser_default_style_updated (GtkWidget *widget);
290 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
291 GdkScreen *previous_screen);
292 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
293 GtkAllocation *allocation);
295 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
298 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
301 gboolean clear_entry,
303 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
304 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
306 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
309 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
311 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
312 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
313 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
314 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
315 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
316 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
317 GtkFileFilter *filter);
318 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
319 GtkFileFilter *filter);
320 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
321 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
324 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
327 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
329 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
331 gint *default_height);
332 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
333 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
335 static void add_selection_to_recent_list (GtkFileChooserDefault *impl);
337 static void location_popup_handler (GtkFileChooserDefault *impl,
339 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
340 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
341 static void up_folder_handler (GtkFileChooserDefault *impl);
342 static void down_folder_handler (GtkFileChooserDefault *impl);
343 static void home_folder_handler (GtkFileChooserDefault *impl);
344 static void desktop_folder_handler (GtkFileChooserDefault *impl);
345 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
346 gint bookmark_index);
347 static void show_hidden_handler (GtkFileChooserDefault *impl);
348 static void search_shortcut_handler (GtkFileChooserDefault *impl);
349 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
350 static void update_appearance (GtkFileChooserDefault *impl);
352 static void set_current_filter (GtkFileChooserDefault *impl,
353 GtkFileFilter *filter);
354 static void check_preview_change (GtkFileChooserDefault *impl);
356 static void filter_combo_changed (GtkComboBox *combo_box,
357 GtkFileChooserDefault *impl);
359 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
361 GtkFileChooserDefault *impl);
363 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
366 gboolean path_currently_selected,
368 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
370 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
372 static int shortcuts_get_index (GtkFileChooserDefault *impl,
373 ShortcutsIndex where);
374 static int shortcut_find_position (GtkFileChooserDefault *impl,
377 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
379 static gboolean list_select_func (GtkTreeSelection *selection,
382 gboolean path_currently_selected,
385 static void list_selection_changed (GtkTreeSelection *tree_selection,
386 GtkFileChooserDefault *impl);
387 static void list_row_activated (GtkTreeView *tree_view,
389 GtkTreeViewColumn *column,
390 GtkFileChooserDefault *impl);
392 static void path_bar_clicked (GtkPathBar *path_bar,
395 gboolean child_is_hidden,
396 GtkFileChooserDefault *impl);
398 static void add_bookmark_button_clicked_cb (GtkButton *button,
399 GtkFileChooserDefault *impl);
400 static void remove_bookmark_button_clicked_cb (GtkButton *button,
401 GtkFileChooserDefault *impl);
403 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
405 static void load_remove_timer (GtkFileChooserDefault *impl);
406 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
408 static void location_button_toggled_cb (GtkToggleButton *toggle,
409 GtkFileChooserDefault *impl);
410 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
412 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
413 gboolean remove_from_treeview);
415 static void search_setup_widgets (GtkFileChooserDefault *impl);
416 static void search_stop_searching (GtkFileChooserDefault *impl,
417 gboolean remove_query);
418 static void search_clear_model (GtkFileChooserDefault *impl,
419 gboolean remove_from_treeview);
420 static gboolean search_should_respond (GtkFileChooserDefault *impl);
421 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
422 static void search_entry_activate_cb (GtkEntry *entry,
424 static void settings_load (GtkFileChooserDefault *impl);
426 static void recent_start_loading (GtkFileChooserDefault *impl);
427 static void recent_stop_loading (GtkFileChooserDefault *impl);
428 static void recent_clear_model (GtkFileChooserDefault *impl,
429 gboolean remove_from_treeview);
430 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
431 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
432 static void set_file_system_backend (GtkFileChooserDefault *impl);
433 static void unset_file_system_backend (GtkFileChooserDefault *impl);
439 /* Drag and drop interface declarations */
442 GtkTreeModelFilter parent;
444 GtkFileChooserDefault *impl;
445 } ShortcutsPaneModelFilter;
448 GtkTreeModelFilterClass parent_class;
449 } ShortcutsPaneModelFilterClass;
451 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
452 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
454 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
456 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
457 _shortcuts_pane_model_filter,
458 GTK_TYPE_TREE_MODEL_FILTER,
459 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
460 shortcuts_pane_model_filter_drag_source_iface_init))
462 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
463 GtkTreeModel *child_model,
468 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_BOX,
469 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
470 gtk_file_chooser_default_iface_init)
471 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
472 gtk_file_chooser_embed_default_iface_init));
476 add_normal_and_shifted_binding (GtkBindingSet *binding_set,
478 GdkModifierType modifiers,
479 const gchar *signal_name)
481 gtk_binding_entry_add_signal (binding_set,
485 gtk_binding_entry_add_signal (binding_set,
486 keyval, modifiers | GDK_SHIFT_MASK,
491 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
493 static const guint quick_bookmark_keyvals[10] = {
494 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
496 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
497 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
498 GtkBindingSet *binding_set;
501 gobject_class->finalize = gtk_file_chooser_default_finalize;
502 gobject_class->constructor = gtk_file_chooser_default_constructor;
503 gobject_class->set_property = gtk_file_chooser_default_set_property;
504 gobject_class->get_property = gtk_file_chooser_default_get_property;
505 gobject_class->dispose = gtk_file_chooser_default_dispose;
507 widget_class->show_all = gtk_file_chooser_default_show_all;
508 widget_class->realize = gtk_file_chooser_default_realize;
509 widget_class->map = gtk_file_chooser_default_map;
510 widget_class->unmap = gtk_file_chooser_default_unmap;
511 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
512 widget_class->style_updated = gtk_file_chooser_default_style_updated;
513 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
514 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
516 signals[LOCATION_POPUP] =
517 g_signal_new_class_handler (I_("location-popup"),
518 G_OBJECT_CLASS_TYPE (class),
519 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
520 G_CALLBACK (location_popup_handler),
522 _gtk_marshal_VOID__STRING,
523 G_TYPE_NONE, 1, G_TYPE_STRING);
525 signals[LOCATION_POPUP_ON_PASTE] =
526 g_signal_new_class_handler (I_("location-popup-on-paste"),
527 G_OBJECT_CLASS_TYPE (class),
528 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
529 G_CALLBACK (location_popup_on_paste_handler),
531 _gtk_marshal_VOID__VOID,
534 signals[LOCATION_TOGGLE_POPUP] =
535 g_signal_new_class_handler (I_("location-toggle-popup"),
536 G_OBJECT_CLASS_TYPE (class),
537 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
538 G_CALLBACK (location_toggle_popup_handler),
540 _gtk_marshal_VOID__VOID,
544 g_signal_new_class_handler (I_("up-folder"),
545 G_OBJECT_CLASS_TYPE (class),
546 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
547 G_CALLBACK (up_folder_handler),
549 _gtk_marshal_VOID__VOID,
552 signals[DOWN_FOLDER] =
553 g_signal_new_class_handler (I_("down-folder"),
554 G_OBJECT_CLASS_TYPE (class),
555 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
556 G_CALLBACK (down_folder_handler),
558 _gtk_marshal_VOID__VOID,
561 signals[HOME_FOLDER] =
562 g_signal_new_class_handler (I_("home-folder"),
563 G_OBJECT_CLASS_TYPE (class),
564 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
565 G_CALLBACK (home_folder_handler),
567 _gtk_marshal_VOID__VOID,
570 signals[DESKTOP_FOLDER] =
571 g_signal_new_class_handler (I_("desktop-folder"),
572 G_OBJECT_CLASS_TYPE (class),
573 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
574 G_CALLBACK (desktop_folder_handler),
576 _gtk_marshal_VOID__VOID,
579 signals[QUICK_BOOKMARK] =
580 g_signal_new_class_handler (I_("quick-bookmark"),
581 G_OBJECT_CLASS_TYPE (class),
582 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
583 G_CALLBACK (quick_bookmark_handler),
585 _gtk_marshal_VOID__INT,
586 G_TYPE_NONE, 1, G_TYPE_INT);
588 signals[SHOW_HIDDEN] =
589 g_signal_new_class_handler (I_("show-hidden"),
590 G_OBJECT_CLASS_TYPE (class),
591 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
592 G_CALLBACK (show_hidden_handler),
594 _gtk_marshal_VOID__VOID,
597 signals[SEARCH_SHORTCUT] =
598 g_signal_new_class_handler (I_("search-shortcut"),
599 G_OBJECT_CLASS_TYPE (class),
600 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
601 G_CALLBACK (search_shortcut_handler),
603 _gtk_marshal_VOID__VOID,
606 signals[RECENT_SHORTCUT] =
607 g_signal_new_class_handler (I_("recent-shortcut"),
608 G_OBJECT_CLASS_TYPE (class),
609 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
610 G_CALLBACK (recent_shortcut_handler),
612 _gtk_marshal_VOID__VOID,
615 binding_set = gtk_binding_set_by_class (class);
617 gtk_binding_entry_add_signal (binding_set,
618 GDK_KEY_l, GDK_CONTROL_MASK,
619 "location-toggle-popup",
622 gtk_binding_entry_add_signal (binding_set,
625 1, G_TYPE_STRING, "/");
626 gtk_binding_entry_add_signal (binding_set,
627 GDK_KEY_KP_Divide, 0,
629 1, G_TYPE_STRING, "/");
632 gtk_binding_entry_add_signal (binding_set,
633 GDK_KEY_asciitilde, 0,
635 1, G_TYPE_STRING, "~");
638 gtk_binding_entry_add_signal (binding_set,
639 GDK_KEY_v, GDK_CONTROL_MASK,
640 "location-popup-on-paste",
642 gtk_binding_entry_add_signal (binding_set,
643 GDK_KEY_BackSpace, 0,
647 add_normal_and_shifted_binding (binding_set,
648 GDK_KEY_Up, GDK_MOD1_MASK,
651 add_normal_and_shifted_binding (binding_set,
652 GDK_KEY_KP_Up, GDK_MOD1_MASK,
655 add_normal_and_shifted_binding (binding_set,
656 GDK_KEY_Down, GDK_MOD1_MASK,
658 add_normal_and_shifted_binding (binding_set,
659 GDK_KEY_KP_Down, GDK_MOD1_MASK,
662 gtk_binding_entry_add_signal (binding_set,
663 GDK_KEY_Home, GDK_MOD1_MASK,
666 gtk_binding_entry_add_signal (binding_set,
667 GDK_KEY_KP_Home, GDK_MOD1_MASK,
670 gtk_binding_entry_add_signal (binding_set,
671 GDK_KEY_d, GDK_MOD1_MASK,
674 gtk_binding_entry_add_signal (binding_set,
675 GDK_KEY_h, GDK_CONTROL_MASK,
678 gtk_binding_entry_add_signal (binding_set,
679 GDK_KEY_s, GDK_MOD1_MASK,
682 gtk_binding_entry_add_signal (binding_set,
683 GDK_KEY_r, GDK_MOD1_MASK,
687 for (i = 0; i < 10; i++)
688 gtk_binding_entry_add_signal (binding_set,
689 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
693 _gtk_file_chooser_install_properties (gobject_class);
697 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
699 iface->select_file = gtk_file_chooser_default_select_file;
700 iface->unselect_file = gtk_file_chooser_default_unselect_file;
701 iface->select_all = gtk_file_chooser_default_select_all;
702 iface->unselect_all = gtk_file_chooser_default_unselect_all;
703 iface->get_files = gtk_file_chooser_default_get_files;
704 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
705 iface->get_file_system = gtk_file_chooser_default_get_file_system;
706 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
707 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
708 iface->set_current_name = gtk_file_chooser_default_set_current_name;
709 iface->add_filter = gtk_file_chooser_default_add_filter;
710 iface->remove_filter = gtk_file_chooser_default_remove_filter;
711 iface->list_filters = gtk_file_chooser_default_list_filters;
712 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
713 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
714 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
718 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
720 iface->get_default_size = gtk_file_chooser_default_get_default_size;
721 iface->should_respond = gtk_file_chooser_default_should_respond;
722 iface->initial_focus = gtk_file_chooser_default_initial_focus;
726 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
728 profile_start ("start", NULL);
729 #ifdef PROFILE_FILE_CHOOSER
730 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
732 impl->local_only = TRUE;
733 impl->preview_widget_active = TRUE;
734 impl->use_preview_label = TRUE;
735 impl->select_multiple = FALSE;
736 impl->show_hidden = FALSE;
737 impl->show_size_column = TRUE;
738 impl->icon_size = FALLBACK_ICON_SIZE;
739 impl->load_state = LOAD_EMPTY;
740 impl->reload_state = RELOAD_EMPTY;
741 impl->pending_select_files = NULL;
742 impl->location_mode = LOCATION_MODE_PATH_BAR;
743 impl->operation_mode = OPERATION_MODE_BROWSE;
744 impl->sort_column = MODEL_COL_NAME;
745 impl->sort_order = GTK_SORT_ASCENDING;
746 impl->recent_manager = gtk_recent_manager_get_default ();
747 impl->create_folders = TRUE;
749 gtk_orientable_set_orientation (GTK_ORIENTABLE (impl),
750 GTK_ORIENTATION_VERTICAL);
751 gtk_box_set_spacing (GTK_BOX (impl), 12);
753 set_file_system_backend (impl);
755 profile_end ("end", NULL);
758 /* Frees the data columns for the specified iter in the shortcuts model*/
760 shortcuts_free_row_data (GtkFileChooserDefault *impl,
764 ShortcutType shortcut_type;
765 GCancellable *cancellable;
767 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
768 SHORTCUTS_COL_DATA, &col_data,
769 SHORTCUTS_COL_TYPE, &shortcut_type,
770 SHORTCUTS_COL_CANCELLABLE, &cancellable,
775 g_cancellable_cancel (cancellable);
776 g_object_unref (cancellable);
779 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
780 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
784 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
786 GtkFileSystemVolume *volume;
789 _gtk_file_system_volume_unref (volume);
791 if (shortcut_type == SHORTCUT_TYPE_FILE)
796 g_object_unref (file);
800 /* Frees all the data columns in the shortcuts model */
802 shortcuts_free (GtkFileChooserDefault *impl)
806 if (!impl->shortcuts_model)
809 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
812 shortcuts_free_row_data (impl, &iter);
814 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
816 g_object_unref (impl->shortcuts_model);
817 impl->shortcuts_model = NULL;
821 pending_select_files_free (GtkFileChooserDefault *impl)
823 g_slist_free_full (impl->pending_select_files, g_object_unref);
824 impl->pending_select_files = NULL;
828 pending_select_files_add (GtkFileChooserDefault *impl,
831 impl->pending_select_files =
832 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
836 gtk_file_chooser_default_finalize (GObject *object)
838 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
841 unset_file_system_backend (impl);
843 if (impl->shortcuts_pane_filter_model)
844 g_object_unref (impl->shortcuts_pane_filter_model);
846 shortcuts_free (impl);
848 g_free (impl->browse_files_last_selected_name);
850 for (l = impl->filters; l; l = l->next)
852 GtkFileFilter *filter;
854 filter = GTK_FILE_FILTER (l->data);
855 g_object_unref (filter);
857 g_slist_free (impl->filters);
859 if (impl->current_filter)
860 g_object_unref (impl->current_filter);
862 if (impl->current_volume_file)
863 g_object_unref (impl->current_volume_file);
865 if (impl->current_folder)
866 g_object_unref (impl->current_folder);
868 if (impl->preview_file)
869 g_object_unref (impl->preview_file);
871 if (impl->browse_path_bar_size_group)
872 g_object_unref (impl->browse_path_bar_size_group);
874 /* Free all the Models we have */
875 stop_loading_and_clear_list_model (impl, FALSE);
876 search_clear_model (impl, FALSE);
877 recent_clear_model (impl, FALSE);
879 /* stopping the load above should have cleared this */
880 g_assert (impl->load_timeout_id == 0);
882 g_free (impl->preview_display_name);
884 g_free (impl->edited_new_text);
886 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
889 /* Shows an error dialog set as transient for the specified window */
891 error_message_with_parent (GtkWindow *parent,
897 dialog = gtk_message_dialog_new (parent,
898 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
903 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
906 if (parent && gtk_window_has_group (parent))
907 gtk_window_group_add_window (gtk_window_get_group (parent),
908 GTK_WINDOW (dialog));
910 gtk_dialog_run (GTK_DIALOG (dialog));
911 gtk_widget_destroy (dialog);
914 /* Returns a toplevel GtkWindow, or NULL if none */
916 get_toplevel (GtkWidget *widget)
920 toplevel = gtk_widget_get_toplevel (widget);
921 if (!gtk_widget_is_toplevel (toplevel))
924 return GTK_WINDOW (toplevel);
927 /* Shows an error dialog for the file chooser */
929 error_message (GtkFileChooserDefault *impl,
933 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
936 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
938 error_dialog (GtkFileChooserDefault *impl,
949 uri = g_file_get_uri (file);
950 text = g_strdup_printf (msg, uri);
951 error_message (impl, text, error->message);
954 g_error_free (error);
958 /* Displays an error message about not being able to get information for a file.
959 * Frees the GError as well.
962 error_getting_info_dialog (GtkFileChooserDefault *impl,
967 _("Could not retrieve information about the file"),
971 /* Shows an error dialog about not being able to add a bookmark */
973 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
978 _("Could not add a bookmark"),
982 /* Shows an error dialog about not being able to remove a bookmark */
984 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
989 _("Could not remove bookmark"),
993 /* Shows an error dialog about not being able to create a folder */
995 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1000 _("The folder could not be created"),
1004 /* Shows an error about not being able to create a folder because a file with
1005 * the same name is already there.
1008 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1013 _("The folder could not be created, as a file with the same "
1014 "name already exists. Try using a different name for the "
1015 "folder, or rename the file first."),
1020 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1026 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1027 _("You need to choose a valid filename."));
1030 _("Cannot create a file under %s as it is not a folder"),
1031 parent_file, error);
1034 /* Shows an error about not being able to select a folder because a file with
1035 * the same name is already there.
1038 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1042 _("You may only select folders. The item that you selected is not a folder; "
1043 "try using a different item."),
1047 /* Shows an error dialog about not being able to create a filename */
1049 error_building_filename_dialog (GtkFileChooserDefault *impl,
1052 error_dialog (impl, _("Invalid file name"),
1056 /* Shows an error dialog when we cannot switch to a folder */
1058 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1062 error_dialog (impl, _("The folder contents could not be displayed"),
1066 /* Changes folders, displaying an error dialog if this fails */
1068 change_folder_and_display_error (GtkFileChooserDefault *impl,
1070 gboolean clear_entry)
1075 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1077 /* We copy the path because of this case:
1079 * list_row_activated()
1080 * fetches path from model; path belongs to the model (*)
1081 * calls change_folder_and_display_error()
1082 * calls gtk_file_chooser_set_current_folder_file()
1083 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1087 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1090 error_changing_folder_dialog (impl, file, error);
1096 emit_default_size_changed (GtkFileChooserDefault *impl)
1098 profile_msg (" emit default-size-changed start", NULL);
1099 g_signal_emit_by_name (impl, "default-size-changed");
1100 profile_msg (" emit default-size-changed end", NULL);
1104 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1106 if (impl->use_preview_label)
1108 if (!impl->preview_label)
1110 impl->preview_label = gtk_label_new (impl->preview_display_name);
1111 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1112 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1113 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1114 gtk_widget_show (impl->preview_label);
1119 if (impl->preview_label)
1121 gtk_widget_destroy (impl->preview_label);
1122 impl->preview_label = NULL;
1126 if (impl->preview_widget_active && impl->preview_widget)
1127 gtk_widget_show (impl->preview_box);
1129 gtk_widget_hide (impl->preview_box);
1131 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1132 emit_default_size_changed (impl);
1136 set_preview_widget (GtkFileChooserDefault *impl,
1137 GtkWidget *preview_widget)
1139 if (preview_widget == impl->preview_widget)
1142 if (impl->preview_widget)
1143 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1144 impl->preview_widget);
1146 impl->preview_widget = preview_widget;
1147 if (impl->preview_widget)
1149 gtk_widget_show (impl->preview_widget);
1150 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1151 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1152 impl->preview_widget,
1153 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1156 update_preview_widget_visibility (impl);
1159 /* Renders a "Search" icon at an appropriate size for a tree view */
1161 render_search_icon (GtkFileChooserDefault *impl)
1163 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1167 render_recent_icon (GtkFileChooserDefault *impl)
1169 GtkIconTheme *theme;
1172 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1173 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1175 theme = gtk_icon_theme_get_default ();
1177 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1183 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1189 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1190 struct ReloadIconsData
1192 GtkFileChooserDefault *impl;
1193 GtkTreeRowReference *row_ref;
1197 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1199 const GError *error,
1205 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1206 struct ReloadIconsData *data = user_data;
1208 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1211 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1213 if (cancelled || error)
1216 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1218 path = gtk_tree_row_reference_get_path (data->row_ref);
1221 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1222 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1223 SHORTCUTS_COL_PIXBUF, pixbuf,
1225 gtk_tree_path_free (path);
1229 g_object_unref (pixbuf);
1232 gtk_tree_row_reference_free (data->row_ref);
1233 g_object_unref (data->impl);
1236 g_object_unref (cancellable);
1240 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1245 profile_start ("start", NULL);
1247 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1250 for (l = impl->reload_icon_cancellables; l; l = l->next)
1252 GCancellable *cancellable = G_CANCELLABLE (l->data);
1253 g_cancellable_cancel (cancellable);
1255 g_slist_free (impl->reload_icon_cancellables);
1256 impl->reload_icon_cancellables = NULL;
1261 ShortcutType shortcut_type;
1262 gboolean pixbuf_visible;
1265 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1266 SHORTCUTS_COL_DATA, &data,
1267 SHORTCUTS_COL_TYPE, &shortcut_type,
1268 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1274 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1276 GtkFileSystemVolume *volume;
1279 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1280 impl->icon_size, NULL);
1282 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1284 if (g_file_is_native (G_FILE (data)))
1287 struct ReloadIconsData *info;
1288 GtkTreePath *tree_path;
1289 GCancellable *cancellable;
1293 info = g_new0 (struct ReloadIconsData, 1);
1294 info->impl = g_object_ref (impl);
1295 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1296 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1297 gtk_tree_path_free (tree_path);
1299 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1301 shortcuts_reload_icons_get_info_cb,
1303 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1307 GtkIconTheme *icon_theme;
1309 /* Don't call get_info for remote paths to avoid latency and
1311 * If we switch to a better bookmarks file format (XBEL), we
1312 * should use mime info to get a better icon.
1314 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1315 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1316 impl->icon_size, 0, NULL);
1319 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1321 pixbuf = render_search_icon (impl);
1323 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1325 pixbuf = render_recent_icon (impl);
1328 gtk_list_store_set (impl->shortcuts_model, &iter,
1329 SHORTCUTS_COL_PIXBUF, pixbuf,
1333 g_object_unref (pixbuf);
1337 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1341 profile_end ("end", NULL);
1345 shortcuts_find_folder (GtkFileChooserDefault *impl,
1348 GtkTreeSelection *selection;
1352 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1354 g_assert (folder != NULL);
1355 pos = shortcut_find_position (impl, folder);
1358 gtk_tree_selection_unselect_all (selection);
1362 path = gtk_tree_path_new_from_indices (pos, -1);
1363 gtk_tree_selection_select_path (selection, path);
1364 gtk_tree_path_free (path);
1367 /* If a shortcut corresponds to the current folder, selects it */
1369 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1371 shortcuts_find_folder (impl, impl->current_folder);
1374 /* Removes the specified number of rows from the shortcuts list */
1376 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1382 path = gtk_tree_path_new_from_indices (start_row, -1);
1384 for (; n_rows; n_rows--)
1388 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1389 g_assert_not_reached ();
1391 shortcuts_free_row_data (impl, &iter);
1392 gtk_list_store_remove (impl->shortcuts_model, &iter);
1395 gtk_tree_path_free (path);
1399 shortcuts_update_count (GtkFileChooserDefault *impl,
1400 ShortcutsIndex type,
1405 case SHORTCUTS_HOME:
1407 impl->has_home = FALSE;
1409 impl->has_home = TRUE;
1412 case SHORTCUTS_DESKTOP:
1414 impl->has_desktop = FALSE;
1416 impl->has_desktop = TRUE;
1419 case SHORTCUTS_VOLUMES:
1420 impl->num_volumes += value;
1423 case SHORTCUTS_SHORTCUTS:
1424 impl->num_shortcuts += value;
1427 case SHORTCUTS_BOOKMARKS:
1428 impl->num_bookmarks += value;
1431 case SHORTCUTS_CURRENT_FOLDER:
1433 impl->shortcuts_current_folder_active = FALSE;
1435 impl->shortcuts_current_folder_active = TRUE;
1444 struct ShortcutsInsertRequest
1446 GtkFileChooserDefault *impl;
1450 GtkTreeRowReference *row_ref;
1451 ShortcutsIndex type;
1457 get_file_info_finished (GCancellable *cancellable,
1459 const GError *error,
1462 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1466 GCancellable *model_cancellable = NULL;
1467 struct ShortcutsInsertRequest *request = data;
1469 path = gtk_tree_row_reference_get_path (request->row_ref);
1471 /* Handle doesn't exist anymore in the model */
1474 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1476 gtk_tree_path_free (path);
1478 /* validate cancellable, else goto out */
1479 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1480 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1482 if (cancellable != model_cancellable)
1485 /* set the cancellable to NULL in the model (we unref later on) */
1486 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1487 SHORTCUTS_COL_CANCELLABLE, NULL,
1495 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1496 shortcuts_update_count (request->impl, request->type, -1);
1498 if (request->type == SHORTCUTS_HOME)
1502 home = g_file_new_for_path (g_get_home_dir ());
1503 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1504 g_object_unref (home);
1506 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1508 /* Remove the current folder separator */
1509 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1510 shortcuts_remove_rows (request->impl, separator_pos, 1);
1516 if (!request->label_copy)
1517 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1518 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1519 request->impl->icon_size);
1521 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1522 SHORTCUTS_COL_PIXBUF, pixbuf,
1523 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1524 SHORTCUTS_COL_NAME, request->label_copy,
1525 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1526 SHORTCUTS_COL_REMOVABLE, request->removable,
1529 if (request->impl->shortcuts_pane_filter_model)
1530 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1533 g_object_unref (pixbuf);
1536 g_object_unref (request->impl);
1537 g_object_unref (request->file);
1538 gtk_tree_row_reference_free (request->row_ref);
1539 g_free (request->label_copy);
1542 if (model_cancellable)
1543 g_object_unref (model_cancellable);
1546 /* FIXME: GtkFileSystem needs a function to split a remote path
1547 * into hostname and path components, or maybe just have a
1548 * gtk_file_system_path_get_display_name().
1550 * This function is also used in gtkfilechooserbutton.c
1553 _gtk_file_chooser_label_for_file (GFile *file)
1555 const gchar *path, *start, *end, *p;
1556 gchar *uri, *host, *label;
1558 uri = g_file_get_uri (file);
1560 start = strstr (uri, "://");
1564 path = strchr (start, '/');
1569 end = uri + strlen (uri);
1573 /* strip username */
1574 p = strchr (start, '@');
1578 p = strchr (start, ':');
1582 host = g_strndup (start, end - start);
1584 /* Translators: the first string is a path and the second string
1585 * is a hostname. Nautilus and the panel contain the same string
1588 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1594 label = g_strdup (uri);
1602 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1603 * inserts a volume. A position of -1 indicates the end of the tree.
1606 shortcuts_insert_file (GtkFileChooserDefault *impl,
1608 ShortcutType shortcut_type,
1609 GtkFileSystemVolume *volume,
1613 ShortcutsIndex type)
1616 GdkPixbuf *pixbuf = NULL;
1617 gpointer data = NULL;
1619 GtkIconTheme *icon_theme;
1621 profile_start ("start shortcut", NULL);
1623 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1626 label_copy = _gtk_file_system_volume_get_display_name (volume);
1627 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1628 impl->icon_size, NULL);
1630 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1632 if (g_file_is_native (file))
1634 struct ShortcutsInsertRequest *request;
1635 GCancellable *cancellable;
1638 request = g_new0 (struct ShortcutsInsertRequest, 1);
1639 request->impl = g_object_ref (impl);
1640 request->file = g_object_ref (file);
1641 request->name_only = TRUE;
1642 request->removable = removable;
1644 request->type = type;
1646 request->label_copy = g_strdup (label);
1649 gtk_list_store_append (impl->shortcuts_model, &iter);
1651 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1653 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1654 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1655 gtk_tree_path_free (p);
1657 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1658 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1659 get_file_info_finished, request);
1661 gtk_list_store_set (impl->shortcuts_model, &iter,
1662 SHORTCUTS_COL_DATA, g_object_ref (file),
1663 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1664 SHORTCUTS_COL_CANCELLABLE, cancellable,
1667 shortcuts_update_count (impl, type, 1);
1673 /* Don't call get_info for remote paths to avoid latency and
1676 data = g_object_ref (file);
1678 label_copy = g_strdup (label);
1680 label_copy = _gtk_file_chooser_label_for_file (file);
1682 /* If we switch to a better bookmarks file format (XBEL), we
1683 * should use mime info to get a better icon.
1685 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1686 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1687 impl->icon_size, 0, NULL);
1692 g_assert_not_reached ();
1698 gtk_list_store_append (impl->shortcuts_model, &iter);
1700 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1702 shortcuts_update_count (impl, type, 1);
1704 gtk_list_store_set (impl->shortcuts_model, &iter,
1705 SHORTCUTS_COL_PIXBUF, pixbuf,
1706 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1707 SHORTCUTS_COL_NAME, label_copy,
1708 SHORTCUTS_COL_DATA, data,
1709 SHORTCUTS_COL_TYPE, shortcut_type,
1710 SHORTCUTS_COL_REMOVABLE, removable,
1711 SHORTCUTS_COL_CANCELLABLE, NULL,
1714 if (impl->shortcuts_pane_filter_model)
1715 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1717 g_free (label_copy);
1720 g_object_unref (pixbuf);
1722 profile_end ("end", NULL);
1726 shortcuts_append_search (GtkFileChooserDefault *impl)
1731 pixbuf = render_search_icon (impl);
1733 gtk_list_store_append (impl->shortcuts_model, &iter);
1734 gtk_list_store_set (impl->shortcuts_model, &iter,
1735 SHORTCUTS_COL_PIXBUF, pixbuf,
1736 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1737 SHORTCUTS_COL_NAME, _("Search"),
1738 SHORTCUTS_COL_DATA, NULL,
1739 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1740 SHORTCUTS_COL_REMOVABLE, FALSE,
1744 g_object_unref (pixbuf);
1746 impl->has_search = TRUE;
1750 shortcuts_append_recent (GtkFileChooserDefault *impl)
1755 pixbuf = render_recent_icon (impl);
1757 gtk_list_store_append (impl->shortcuts_model, &iter);
1758 gtk_list_store_set (impl->shortcuts_model, &iter,
1759 SHORTCUTS_COL_PIXBUF, pixbuf,
1760 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1761 SHORTCUTS_COL_NAME, _("Recently Used"),
1762 SHORTCUTS_COL_DATA, NULL,
1763 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1764 SHORTCUTS_COL_REMOVABLE, FALSE,
1768 g_object_unref (pixbuf);
1771 /* Appends an item for the user's home directory to the shortcuts model */
1773 shortcuts_append_home (GtkFileChooserDefault *impl)
1775 const char *home_path;
1778 profile_start ("start", NULL);
1780 home_path = g_get_home_dir ();
1781 if (home_path == NULL)
1783 profile_end ("end - no home directory!?", NULL);
1787 home = g_file_new_for_path (home_path);
1788 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1789 impl->has_home = TRUE;
1791 g_object_unref (home);
1793 profile_end ("end", NULL);
1796 /* Appends the ~/Desktop directory to the shortcuts model */
1798 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1803 profile_start ("start", NULL);
1805 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1806 /* "To disable a directory, point it to the homedir."
1807 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1809 if (!g_strcmp0 (name, g_get_home_dir ()))
1811 profile_end ("end", NULL);
1815 file = g_file_new_for_path (name);
1816 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1817 impl->has_desktop = TRUE;
1819 /* We do not actually pop up an error dialog if there is no desktop directory
1820 * because some people may really not want to have one.
1823 g_object_unref (file);
1825 profile_end ("end", NULL);
1828 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1830 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1837 profile_start ("start", NULL);
1839 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1842 for (; bookmarks; bookmarks = bookmarks->next)
1846 file = bookmarks->data;
1848 if (impl->local_only && !g_file_is_native (file))
1851 if (shortcut_find_position (impl, file) != -1)
1854 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1856 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1862 profile_end ("end", NULL);
1864 return num_inserted;
1867 /* Returns the index for the corresponding item in the shortcuts bar */
1869 shortcuts_get_index (GtkFileChooserDefault *impl,
1870 ShortcutsIndex where)
1876 if (where == SHORTCUTS_SEARCH)
1879 n += impl->has_search ? 1 : 0;
1881 if (where == SHORTCUTS_RECENT)
1884 n += 1; /* we always have the recently-used item */
1886 if (where == SHORTCUTS_RECENT_SEPARATOR)
1889 n += 1; /* we always have the separator after the recently-used item */
1891 if (where == SHORTCUTS_HOME)
1894 n += impl->has_home ? 1 : 0;
1896 if (where == SHORTCUTS_DESKTOP)
1899 n += impl->has_desktop ? 1 : 0;
1901 if (where == SHORTCUTS_VOLUMES)
1904 n += impl->num_volumes;
1906 if (where == SHORTCUTS_SHORTCUTS)
1909 n += impl->num_shortcuts;
1911 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1914 /* If there are no bookmarks there won't be a separator */
1915 n += (impl->num_bookmarks > 0) ? 1 : 0;
1917 if (where == SHORTCUTS_BOOKMARKS)
1920 n += impl->num_bookmarks;
1922 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1927 if (where == SHORTCUTS_CURRENT_FOLDER)
1930 g_assert_not_reached ();
1937 /* Adds all the file system volumes to the shortcuts model */
1939 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1944 gboolean old_changing_folders;
1946 profile_start ("start", NULL);
1948 old_changing_folders = impl->changing_folder;
1949 impl->changing_folder = TRUE;
1951 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1952 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1953 impl->num_volumes = 0;
1955 list = _gtk_file_system_list_volumes (impl->file_system);
1959 for (l = list; l; l = l->next)
1961 GtkFileSystemVolume *volume;
1965 if (impl->local_only)
1967 if (_gtk_file_system_volume_is_mounted (volume))
1970 gboolean base_is_native = TRUE;
1972 base_file = _gtk_file_system_volume_get_root (volume);
1973 if (base_file != NULL)
1975 base_is_native = g_file_is_native (base_file);
1976 g_object_unref (base_file);
1979 if (!base_is_native)
1984 shortcuts_insert_file (impl,
1986 SHORTCUT_TYPE_VOLUME,
1987 _gtk_file_system_volume_ref (volume),
1995 impl->num_volumes = n;
1996 g_slist_free (list);
1998 if (impl->shortcuts_pane_filter_model)
1999 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2001 impl->changing_folder = old_changing_folders;
2003 profile_end ("end", NULL);
2006 /* Inserts a separator node in the shortcuts list */
2008 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2009 ShortcutsIndex where)
2013 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2014 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2015 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2017 gtk_list_store_insert (impl->shortcuts_model, &iter,
2018 shortcuts_get_index (impl, where));
2019 gtk_list_store_set (impl->shortcuts_model, &iter,
2020 SHORTCUTS_COL_PIXBUF, NULL,
2021 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2022 SHORTCUTS_COL_NAME, NULL,
2023 SHORTCUTS_COL_DATA, NULL,
2024 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2028 /* Updates the list of bookmarks */
2030 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2033 gboolean old_changing_folders;
2035 GFile *list_selected = NULL;
2036 ShortcutType shortcut_type;
2039 profile_start ("start", NULL);
2041 old_changing_folders = impl->changing_folder;
2042 impl->changing_folder = TRUE;
2044 if (shortcuts_get_selected (impl, &iter))
2046 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2048 SHORTCUTS_COL_DATA, &col_data,
2049 SHORTCUTS_COL_TYPE, &shortcut_type,
2052 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2053 list_selected = g_object_ref (col_data);
2056 if (impl->num_bookmarks > 0)
2057 shortcuts_remove_rows (impl,
2058 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2059 impl->num_bookmarks + 1);
2061 impl->num_bookmarks = 0;
2062 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2064 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2065 shortcuts_append_bookmarks (impl, bookmarks);
2066 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2067 g_slist_free (bookmarks);
2069 if (impl->num_bookmarks == 0)
2070 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2072 if (impl->shortcuts_pane_filter_model)
2073 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2077 shortcuts_find_folder (impl, list_selected);
2078 g_object_unref (list_selected);
2081 impl->changing_folder = old_changing_folders;
2083 profile_end ("end", NULL);
2086 /* Appends a separator and a row to the shortcuts list for the current folder */
2088 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2092 g_assert (!impl->shortcuts_current_folder_active);
2094 g_assert (impl->current_folder != NULL);
2096 pos = shortcut_find_position (impl, impl->current_folder);
2099 GtkFileSystemVolume *volume;
2103 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2106 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2108 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2110 base_file = _gtk_file_system_volume_get_root (volume);
2114 if (base_file && g_file_equal (base_file, impl->current_folder))
2115 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2117 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2120 g_object_unref (base_file);
2124 /* Updates the current folder row in the shortcuts model */
2126 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2130 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2132 if (impl->shortcuts_current_folder_active)
2134 shortcuts_remove_rows (impl, pos, 2);
2135 impl->shortcuts_current_folder_active = FALSE;
2138 shortcuts_add_current_folder (impl);
2141 /* Filter function used for the shortcuts filter model */
2143 shortcuts_pane_filter_cb (GtkTreeModel *model,
2147 GtkFileChooserDefault *impl;
2151 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2153 path = gtk_tree_model_get_path (model, iter);
2157 pos = *gtk_tree_path_get_indices (path);
2158 gtk_tree_path_free (path);
2160 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2163 /* Creates the list model for shortcuts */
2165 shortcuts_model_create (GtkFileChooserDefault *impl)
2167 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2168 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2169 GDK_TYPE_PIXBUF, /* pixbuf */
2170 G_TYPE_STRING, /* name */
2171 G_TYPE_POINTER, /* path or volume */
2172 G_TYPE_INT, /* ShortcutType */
2173 G_TYPE_BOOLEAN, /* removable */
2174 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2175 G_TYPE_POINTER); /* GCancellable */
2177 shortcuts_append_search (impl);
2179 if (impl->recent_manager)
2181 shortcuts_append_recent (impl);
2182 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2185 if (impl->file_system)
2187 shortcuts_append_home (impl);
2188 shortcuts_append_desktop (impl);
2189 shortcuts_add_volumes (impl);
2192 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2193 GTK_TREE_MODEL (impl->shortcuts_model),
2196 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2197 shortcuts_pane_filter_cb,
2202 /* Callback used when the "New Folder" button is clicked */
2204 new_folder_button_clicked (GtkButton *button,
2205 GtkFileChooserDefault *impl)
2210 if (!impl->browse_files_model)
2211 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2213 /* Prevent button from being clicked twice */
2214 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2216 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2218 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2219 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2220 path, impl->list_name_column,
2223 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2224 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2226 impl->list_name_column,
2229 gtk_tree_path_free (path);
2233 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2237 source = g_idle_source_new ();
2238 g_source_set_closure (source,
2239 g_cclosure_new_object (callback, G_OBJECT (impl)));
2240 g_source_attach (source, NULL);
2245 /* Idle handler for creating a new folder after editing its name cell, or for
2246 * canceling the editing.
2249 edited_idle_cb (GtkFileChooserDefault *impl)
2251 GDK_THREADS_ENTER ();
2253 g_source_destroy (impl->edited_idle);
2254 impl->edited_idle = NULL;
2256 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2257 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2259 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2261 if (impl->edited_new_text /* not cancelled? */
2262 && (strlen (impl->edited_new_text) != 0)
2263 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2265 GError *error = NULL;
2268 file = g_file_get_child_for_display_name (impl->current_folder,
2269 impl->edited_new_text,
2273 GError *error = NULL;
2275 if (g_file_make_directory (file, NULL, &error))
2276 change_folder_and_display_error (impl, file, FALSE);
2278 error_creating_folder_dialog (impl, file, error);
2280 g_object_unref (file);
2283 error_creating_folder_dialog (impl, file, error);
2285 g_free (impl->edited_new_text);
2286 impl->edited_new_text = NULL;
2289 GDK_THREADS_LEAVE ();
2295 queue_edited_idle (GtkFileChooserDefault *impl,
2296 const gchar *new_text)
2298 /* We create the folder in an idle handler so that we don't modify the tree
2302 if (!impl->edited_idle)
2303 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2305 g_free (impl->edited_new_text);
2306 impl->edited_new_text = g_strdup (new_text);
2309 /* Callback used from the text cell renderer when the new folder is named */
2311 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2313 const gchar *new_text,
2314 GtkFileChooserDefault *impl)
2316 /* work around bug #154921 */
2317 g_object_set (cell_renderer_text,
2318 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2319 queue_edited_idle (impl, new_text);
2322 /* Callback used from the text cell renderer when the new folder edition gets
2326 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2327 GtkFileChooserDefault *impl)
2329 /* work around bug #154921 */
2330 g_object_set (cell_renderer_text,
2331 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2332 queue_edited_idle (impl, NULL);
2335 /* Creates the widgets for the filter combo box */
2337 filter_create (GtkFileChooserDefault *impl)
2339 GtkCellRenderer *cell;
2342 impl->filter_combo = gtk_combo_box_text_new ();
2343 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2345 /* Get the combo's text renderer and set ellipsize parameters */
2346 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2350 g_object_set (G_OBJECT (cell),
2351 "ellipsize", PANGO_ELLIPSIZE_END,
2354 g_list_free (cells);
2356 g_signal_connect (impl->filter_combo, "changed",
2357 G_CALLBACK (filter_combo_changed), impl);
2359 gtk_widget_set_tooltip_text (impl->filter_combo,
2360 _("Select which types of files are shown"));
2362 return impl->filter_combo;
2366 toolbutton_new (GtkFileChooserDefault *impl,
2375 item = gtk_tool_button_new (NULL, NULL);
2376 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2377 gtk_widget_show (image);
2378 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2380 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2381 g_signal_connect (item, "clicked", callback, impl);
2384 gtk_widget_show (GTK_WIDGET (item));
2386 return GTK_WIDGET (item);
2389 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2391 shortcut_find_position (GtkFileChooserDefault *impl,
2396 int current_folder_separator_idx;
2398 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2401 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2404 /* FIXME: is this still needed? */
2405 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2409 for (i = 0; i < current_folder_separator_idx; i++)
2412 ShortcutType shortcut_type;
2414 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2415 SHORTCUTS_COL_DATA, &col_data,
2416 SHORTCUTS_COL_TYPE, &shortcut_type,
2421 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2423 GtkFileSystemVolume *volume;
2428 base_file = _gtk_file_system_volume_get_root (volume);
2430 exists = base_file && g_file_equal (file, base_file);
2433 g_object_unref (base_file);
2438 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2442 model_file = col_data;
2444 if (model_file && g_file_equal (model_file, file))
2449 if (i < current_folder_separator_idx - 1)
2451 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2452 g_assert_not_reached ();
2459 /* Tries to add a bookmark from a path name */
2461 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2467 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2469 if (shortcut_find_position (impl, file) != -1)
2473 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2475 error_adding_bookmark_dialog (impl, file, error);
2483 add_bookmark_foreach_cb (GtkTreeModel *model,
2488 GtkFileChooserDefault *impl;
2491 impl = (GtkFileChooserDefault *) data;
2493 gtk_tree_model_get (model, iter,
2494 MODEL_COL_FILE, &file,
2497 shortcuts_add_bookmark_from_file (impl, file, -1);
2499 g_object_unref (file);
2502 /* Adds a bookmark from the currently selected item in the file list */
2504 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2506 GtkTreeSelection *selection;
2508 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2510 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2511 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2513 gtk_tree_selection_selected_foreach (selection,
2514 add_bookmark_foreach_cb,
2518 /* Callback used when the "Add bookmark" button is clicked */
2520 add_bookmark_button_clicked_cb (GtkButton *button,
2521 GtkFileChooserDefault *impl)
2523 bookmarks_add_selected_folder (impl);
2526 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2527 * returns FALSE if no shortcut is selected.
2530 shortcuts_get_selected (GtkFileChooserDefault *impl,
2533 GtkTreeSelection *selection;
2534 GtkTreeIter parent_iter;
2536 if (!impl->browse_shortcuts_tree_view)
2539 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2541 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2544 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2550 /* Removes the selected bookmarks */
2552 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2560 if (!shortcuts_get_selected (impl, &iter))
2563 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2564 SHORTCUTS_COL_DATA, &col_data,
2565 SHORTCUTS_COL_REMOVABLE, &removable,
2571 g_assert (col_data != NULL);
2576 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2577 error_removing_bookmark_dialog (impl, file, error);
2580 /* Callback used when the "Remove bookmark" button is clicked */
2582 remove_bookmark_button_clicked_cb (GtkButton *button,
2583 GtkFileChooserDefault *impl)
2585 remove_selected_bookmarks (impl);
2588 struct selection_check_closure {
2589 GtkFileChooserDefault *impl;
2592 gboolean all_folders;
2595 /* Used from gtk_tree_selection_selected_foreach() */
2597 selection_check_foreach_cb (GtkTreeModel *model,
2602 struct selection_check_closure *closure;
2606 gtk_tree_model_get (model, iter,
2607 MODEL_COL_FILE, &file,
2608 MODEL_COL_IS_FOLDER, &is_folder,
2614 g_object_unref (file);
2617 closure->num_selected++;
2619 closure->all_folders = closure->all_folders && is_folder;
2620 closure->all_files = closure->all_files && !is_folder;
2623 /* Checks whether the selected items in the file list are all files or all folders */
2625 selection_check (GtkFileChooserDefault *impl,
2627 gboolean *all_files,
2628 gboolean *all_folders)
2630 struct selection_check_closure closure;
2631 GtkTreeSelection *selection;
2633 closure.impl = impl;
2634 closure.num_selected = 0;
2635 closure.all_files = TRUE;
2636 closure.all_folders = TRUE;
2638 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2639 gtk_tree_selection_selected_foreach (selection,
2640 selection_check_foreach_cb,
2643 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2646 *num_selected = closure.num_selected;
2649 *all_files = closure.all_files;
2652 *all_folders = closure.all_folders;
2655 struct get_selected_file_closure {
2656 GtkFileChooserDefault *impl;
2661 get_selected_file_foreach_cb (GtkTreeModel *model,
2666 struct get_selected_file_closure *closure = data;
2670 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2671 g_object_unref (closure->file);
2672 closure->file = NULL;
2675 gtk_tree_model_get (model, iter,
2676 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2680 /* Returns a selected path from the file list */
2682 get_selected_file (GtkFileChooserDefault *impl)
2684 struct get_selected_file_closure closure;
2685 GtkTreeSelection *selection;
2687 closure.impl = impl;
2688 closure.file = NULL;
2690 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2691 gtk_tree_selection_selected_foreach (selection,
2692 get_selected_file_foreach_cb,
2695 return closure.file;
2699 GtkFileChooserDefault *impl;
2701 } UpdateTooltipData;
2704 update_tooltip (GtkTreeModel *model,
2709 UpdateTooltipData *udata = data;
2711 if (udata->tip == NULL)
2713 gchar *display_name;
2715 gtk_tree_model_get (model, iter,
2716 MODEL_COL_NAME, &display_name,
2719 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2721 g_free (display_name);
2726 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2727 * if there are no selected items *and* the current folder is not in the
2728 * bookmarks list. De-sensitize the button otherwise.
2731 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2734 gboolean all_folders;
2738 selection_check (impl, &num_selected, NULL, &all_folders);
2740 if (num_selected == 0)
2741 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2742 else if (num_selected == 1)
2746 file = get_selected_file (impl);
2747 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2749 g_object_unref (file);
2752 active = all_folders;
2754 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2756 if (impl->browse_files_popup_menu_add_shortcut_item)
2757 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2758 (num_selected == 0) ? FALSE : active);
2762 if (num_selected == 0)
2763 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2764 else if (num_selected > 1)
2765 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2768 GtkTreeSelection *selection;
2769 UpdateTooltipData data;
2771 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2774 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2778 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2783 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2784 * bookmark row is selected in the shortcuts tree.
2787 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2790 gboolean removable = FALSE;
2794 if (shortcuts_get_selected (impl, &iter))
2796 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2797 SHORTCUTS_COL_REMOVABLE, &removable,
2798 SHORTCUTS_COL_NAME, &name,
2800 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2803 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2805 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2807 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2811 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2812 _("Remove the selected bookmark"));
2817 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2820 gboolean removable = FALSE;
2822 if (impl->browse_shortcuts_popup_menu == NULL)
2825 if (shortcuts_get_selected (impl, &iter))
2826 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2827 SHORTCUTS_COL_REMOVABLE, &removable,
2830 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2831 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2834 /* GtkWidget::drag-begin handler for the shortcuts list. */
2836 shortcuts_drag_begin_cb (GtkWidget *widget,
2837 GdkDragContext *context,
2838 GtkFileChooserDefault *impl)
2841 impl->shortcuts_drag_context = g_object_ref (context);
2846 /* Removes the idle handler for outside drags */
2848 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2850 if (!impl->shortcuts_drag_outside_idle)
2853 g_source_destroy (impl->shortcuts_drag_outside_idle);
2854 impl->shortcuts_drag_outside_idle = NULL;
2858 /* GtkWidget::drag-end handler for the shortcuts list. */
2860 shortcuts_drag_end_cb (GtkWidget *widget,
2861 GdkDragContext *context,
2862 GtkFileChooserDefault *impl)
2865 g_object_unref (impl->shortcuts_drag_context);
2867 shortcuts_cancel_drag_outside_idle (impl);
2869 if (!impl->shortcuts_drag_outside)
2872 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2874 impl->shortcuts_drag_outside = FALSE;
2878 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2880 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2881 GdkDragContext *context,
2882 GtkFileChooserDefault *impl)
2884 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2887 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2891 shortcuts_drag_leave_cb (GtkWidget *widget,
2892 GdkDragContext *context,
2894 GtkFileChooserDefault *impl)
2897 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2899 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2903 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2905 GTK_TREE_VIEW_DROP_BEFORE);
2907 g_signal_stop_emission_by_name (widget, "drag-leave");
2910 /* Computes the appropriate row and position for dropping */
2912 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2916 GtkTreeViewDropPosition *pos)
2918 GtkTreeView *tree_view;
2919 GtkTreeViewColumn *column;
2923 int bookmarks_index;
2924 int header_height = 0;
2926 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2928 if (gtk_tree_view_get_headers_visible (tree_view))
2929 header_height = _gtk_tree_view_get_header_height (tree_view);
2931 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2933 if (!gtk_tree_view_get_path_at_pos (tree_view,
2941 row = bookmarks_index + impl->num_bookmarks - 1;
2942 *path = gtk_tree_path_new_from_indices (row, -1);
2943 *pos = GTK_TREE_VIEW_DROP_AFTER;
2947 row = *gtk_tree_path_get_indices (*path);
2948 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2949 gtk_tree_path_free (*path);
2951 if (row < bookmarks_index)
2953 row = bookmarks_index;
2954 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2956 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2958 row = bookmarks_index + impl->num_bookmarks - 1;
2959 *pos = GTK_TREE_VIEW_DROP_AFTER;
2963 if (cell_y < cell.height / 2)
2964 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2966 *pos = GTK_TREE_VIEW_DROP_AFTER;
2969 *path = gtk_tree_path_new_from_indices (row, -1);
2972 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2973 * implement the destination side of DnD by hand, due to limitations in
2974 * GtkTreeView's DnD API.
2977 shortcuts_drag_motion_cb (GtkWidget *widget,
2978 GdkDragContext *context,
2982 GtkFileChooserDefault *impl)
2985 GtkTreeViewDropPosition pos;
2986 GdkDragAction action;
2989 if (gtk_drag_get_source_widget (context) == widget)
2991 shortcuts_cancel_drag_outside_idle (impl);
2993 if (impl->shortcuts_drag_outside)
2995 shortcuts_drag_set_delete_cursor (impl, FALSE);
2996 impl->shortcuts_drag_outside = FALSE;
3001 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3002 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3003 action = GDK_ACTION_COPY;
3004 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3005 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3006 action = GDK_ACTION_MOVE;
3013 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3014 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3015 gtk_tree_path_free (path);
3019 g_signal_stop_emission_by_name (widget, "drag-motion");
3023 gdk_drag_status (context, action, time_);
3030 /* GtkWidget::drag-drop handler for the shortcuts list. */
3032 shortcuts_drag_drop_cb (GtkWidget *widget,
3033 GdkDragContext *context,
3037 GtkFileChooserDefault *impl)
3040 shortcuts_cancel_drag_outside_idle (impl);
3043 g_signal_stop_emission_by_name (widget, "drag-drop");
3047 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3049 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3050 GtkSelectionData *selection_data,
3056 uris = gtk_selection_data_get_uris (selection_data);
3060 for (i = 0; uris[i]; i++)
3066 file = g_file_new_for_uri (uri);
3068 if (shortcuts_add_bookmark_from_file (impl, file, position))
3071 g_object_unref (file);
3077 /* Reorders the selected bookmark to the specified position */
3079 shortcuts_reorder (GtkFileChooserDefault *impl,
3084 ShortcutType shortcut_type;
3087 int bookmarks_index;
3092 /* Get the selected path */
3094 if (!shortcuts_get_selected (impl, &iter))
3095 g_assert_not_reached ();
3097 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3098 old_position = *gtk_tree_path_get_indices (path);
3099 gtk_tree_path_free (path);
3101 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3102 old_position -= bookmarks_index;
3103 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3105 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3106 SHORTCUTS_COL_NAME, &name,
3107 SHORTCUTS_COL_DATA, &col_data,
3108 SHORTCUTS_COL_TYPE, &shortcut_type,
3110 g_assert (col_data != NULL);
3111 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3114 g_object_ref (file); /* removal below will free file, so we need a new ref */
3116 /* Remove the path from the old position and insert it in the new one */
3118 if (new_position > old_position)
3121 if (old_position == new_position)
3125 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3127 shortcuts_add_bookmark_from_file (impl, file, new_position);
3128 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3131 error_adding_bookmark_dialog (impl, file, error);
3135 g_object_unref (file);
3139 /* Callback used when we get the drag data for the bookmarks list. We add the
3140 * received URIs as bookmarks if they are folders.
3143 shortcuts_drag_data_received_cb (GtkWidget *widget,
3144 GdkDragContext *context,
3147 GtkSelectionData *selection_data,
3152 GtkFileChooserDefault *impl;
3153 GtkTreePath *tree_path;
3154 GtkTreeViewDropPosition tree_pos;
3157 int bookmarks_index;
3159 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3161 /* Compute position */
3163 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3165 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3166 position = *gtk_tree_path_get_indices (tree_path);
3167 gtk_tree_path_free (tree_path);
3169 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3172 g_assert (position >= bookmarks_index);
3173 position -= bookmarks_index;
3175 target = gtk_selection_data_get_target (selection_data);
3177 if (gtk_targets_include_uri (&target, 1))
3178 shortcuts_drop_uris (impl, selection_data, position);
3179 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3180 shortcuts_reorder (impl, position);
3182 g_signal_stop_emission_by_name (widget, "drag-data-received");
3185 /* Callback used to display a tooltip in the shortcuts tree */
3187 shortcuts_query_tooltip_cb (GtkWidget *widget,
3190 gboolean keyboard_mode,
3191 GtkTooltip *tooltip,
3192 GtkFileChooserDefault *impl)
3194 GtkTreeModel *model;
3197 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3205 ShortcutType shortcut_type;
3207 gtk_tree_model_get (model, &iter,
3208 SHORTCUTS_COL_DATA, &col_data,
3209 SHORTCUTS_COL_TYPE, &shortcut_type,
3212 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3214 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3216 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3221 file = G_FILE (col_data);
3222 parse_name = g_file_get_parse_name (file);
3224 gtk_tooltip_set_text (tooltip, parse_name);
3226 g_free (parse_name);
3230 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3234 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3244 /* Callback used when the selection in the shortcuts tree changes */
3246 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3247 GtkFileChooserDefault *impl)
3250 GtkTreeIter child_iter;
3252 bookmarks_check_remove_sensitivity (impl);
3253 shortcuts_check_popup_sensitivity (impl);
3255 if (impl->changing_folder)
3258 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3260 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3263 shortcuts_activate_iter (impl, &child_iter);
3268 shortcuts_row_separator_func (GtkTreeModel *model,
3272 ShortcutType shortcut_type;
3274 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3276 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3280 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3282 GtkFileChooserDefault *impl)
3286 /* don't screw up focus switching with Tab */
3287 if (event->keyval == GDK_KEY_Tab
3288 || event->keyval == GDK_KEY_KP_Tab
3289 || event->keyval == GDK_KEY_ISO_Left_Tab
3290 || event->length < 1)
3293 if (impl->location_entry)
3294 entry = impl->location_entry;
3295 else if (impl->search_entry)
3296 entry = impl->search_entry;
3302 gtk_widget_grab_focus (entry);
3303 return gtk_widget_event (entry, (GdkEvent *) event);
3309 /* Callback used when the file list's popup menu is detached */
3311 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3314 GtkFileChooserDefault *impl;
3316 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3317 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3319 impl->browse_shortcuts_popup_menu = NULL;
3320 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3321 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3325 remove_shortcut_cb (GtkMenuItem *item,
3326 GtkFileChooserDefault *impl)
3328 remove_selected_bookmarks (impl);
3331 /* Rename the selected bookmark */
3333 rename_selected_bookmark (GtkFileChooserDefault *impl)
3337 GtkTreeViewColumn *column;
3338 GtkCellRenderer *cell;
3341 if (shortcuts_get_selected (impl, &iter))
3343 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3344 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3345 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3346 cell = g_list_nth_data (renderers, 1);
3347 g_list_free (renderers);
3348 g_object_set (cell, "editable", TRUE, NULL);
3349 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3350 path, column, cell, TRUE);
3351 gtk_tree_path_free (path);
3356 rename_shortcut_cb (GtkMenuItem *item,
3357 GtkFileChooserDefault *impl)
3359 rename_selected_bookmark (impl);
3362 /* Constructs the popup menu for the file list if needed */
3364 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3368 if (impl->browse_shortcuts_popup_menu)
3371 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3372 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3373 impl->browse_shortcuts_tree_view,
3374 shortcuts_popup_menu_detach_cb);
3376 item = gtk_image_menu_item_new_with_label (_("Remove"));
3377 impl->browse_shortcuts_popup_menu_remove_item = item;
3378 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3379 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3380 g_signal_connect (item, "activate",
3381 G_CALLBACK (remove_shortcut_cb), impl);
3382 gtk_widget_show (item);
3383 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3385 item = gtk_menu_item_new_with_label (_("Rename..."));
3386 impl->browse_shortcuts_popup_menu_rename_item = item;
3387 g_signal_connect (item, "activate",
3388 G_CALLBACK (rename_shortcut_cb), impl);
3389 gtk_widget_show (item);
3390 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3394 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3396 shortcuts_build_popup_menu (impl);
3397 shortcuts_check_popup_sensitivity (impl);
3401 popup_position_func (GtkMenu *menu,
3405 gpointer user_data);
3408 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3409 GdkEventButton *event)
3411 shortcuts_update_popup_menu (impl);
3413 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3414 NULL, NULL, NULL, NULL,
3415 event->button, event->time);
3418 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3420 popup_position_func, impl->browse_shortcuts_tree_view,
3421 0, GDK_CURRENT_TIME);
3422 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3427 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3429 shortcuts_popup_menu_cb (GtkWidget *widget,
3430 GtkFileChooserDefault *impl)
3432 shortcuts_popup_menu (impl, NULL);
3436 /* Callback used when a button is pressed on the shortcuts list.
3437 * We trap button 3 to bring up a popup menu.
3440 shortcuts_button_press_event_cb (GtkWidget *widget,
3441 GdkEventButton *event,
3442 GtkFileChooserDefault *impl)
3444 static gboolean in_press = FALSE;
3450 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
3454 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3460 shortcuts_popup_menu (impl, event);
3465 shortcuts_edited (GtkCellRenderer *cell,
3468 GtkFileChooserDefault *impl)
3474 g_object_set (cell, "editable", FALSE, NULL);
3476 path = gtk_tree_path_new_from_string (path_string);
3477 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3478 g_assert_not_reached ();
3480 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3481 SHORTCUTS_COL_DATA, &shortcut,
3483 gtk_tree_path_free (path);
3485 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3489 shortcuts_editing_canceled (GtkCellRenderer *cell,
3490 GtkFileChooserDefault *impl)
3492 g_object_set (cell, "editable", FALSE, NULL);
3495 /* Creates the widgets for the shortcuts and bookmarks tree */
3497 shortcuts_list_create (GtkFileChooserDefault *impl)
3500 GtkTreeSelection *selection;
3501 GtkTreeViewColumn *column;
3502 GtkCellRenderer *renderer;
3504 /* Target types for dragging a row to/from the shortcuts list */
3505 const GtkTargetEntry tree_model_row_targets[] = {
3506 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3509 /* Scrolled window */
3511 swin = gtk_scrolled_window_new (NULL, NULL);
3512 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3513 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3514 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3516 gtk_widget_show (swin);
3519 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3520 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3521 GTK_STYLE_CLASS_SIDEBAR);
3522 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3523 #ifdef PROFILE_FILE_CHOOSER
3524 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3527 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3529 * 1. user brings up a SAVE dialog
3530 * 2. user clicks on a shortcut in the shortcuts pane
3531 * 3. user starts typing a filename
3533 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3534 * support interactive search. However, we'd rather focus the location entry
3535 * so that the user can type *there*.
3537 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3538 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3539 * if the user starts typing while the focus is in the shortcuts pane.
3541 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3542 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3544 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3545 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3546 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3547 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3548 /* Accessible object name for the file chooser's shortcuts pane */
3549 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3551 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3553 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3555 tree_model_row_targets,
3556 G_N_ELEMENTS (tree_model_row_targets),
3559 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3560 GTK_DEST_DEFAULT_ALL,
3561 tree_model_row_targets,
3562 G_N_ELEMENTS (tree_model_row_targets),
3563 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3564 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3566 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3567 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3568 gtk_tree_selection_set_select_function (selection,
3569 shortcuts_select_func,
3572 g_signal_connect (selection, "changed",
3573 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3575 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3576 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3578 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3579 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3580 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3581 G_CALLBACK (shortcuts_drag_end_cb), impl);
3582 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3583 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3585 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3586 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3587 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3588 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3589 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3590 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3591 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3592 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3594 /* Support tooltips */
3595 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3596 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3597 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3599 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3600 gtk_widget_show (impl->browse_shortcuts_tree_view);
3604 column = gtk_tree_view_column_new ();
3605 /* Column header for the file chooser's shortcuts pane */
3606 gtk_tree_view_column_set_title (column, _("_Places"));
3608 renderer = gtk_cell_renderer_pixbuf_new ();
3609 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3610 gtk_tree_view_column_set_attributes (column, renderer,
3611 "pixbuf", SHORTCUTS_COL_PIXBUF,
3612 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3615 renderer = gtk_cell_renderer_text_new ();
3616 g_object_set (renderer,
3618 "ellipsize", PANGO_ELLIPSIZE_END,
3620 g_signal_connect (renderer, "edited",
3621 G_CALLBACK (shortcuts_edited), impl);
3622 g_signal_connect (renderer, "editing-canceled",
3623 G_CALLBACK (shortcuts_editing_canceled), impl);
3624 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3625 gtk_tree_view_column_set_attributes (column, renderer,
3626 "text", SHORTCUTS_COL_NAME,
3629 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3630 shortcuts_row_separator_func,
3633 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3638 /* Creates the widgets for the shortcuts/bookmarks pane */
3640 shortcuts_pane_create (GtkFileChooserDefault *impl,
3641 GtkSizeGroup *size_group)
3646 GtkStyleContext *context;
3649 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3650 gtk_widget_show (vbox);
3652 /* Shortcuts tree */
3654 widget = shortcuts_list_create (impl);
3656 gtk_size_group_add_widget (size_group, widget);
3657 context = gtk_widget_get_style_context (widget);
3658 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3660 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3662 /* Box for buttons */
3664 toolbar = gtk_toolbar_new ();
3665 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_ICONS);
3666 gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
3668 context = gtk_widget_get_style_context (toolbar);
3669 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
3670 gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
3672 gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
3673 gtk_widget_show (toolbar);
3675 /* Add bookmark button */
3676 icon = g_themed_icon_new_with_default_fallbacks ("list-add-symbolic");
3677 impl->browse_shortcuts_add_button = toolbutton_new (impl,
3681 G_CALLBACK (add_bookmark_button_clicked_cb));
3682 g_object_unref (icon);
3684 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_add_button), 0);
3685 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3686 _("Add the selected folder to the Bookmarks"));
3688 /* Remove bookmark button */
3689 icon = g_themed_icon_new_with_default_fallbacks ("list-remove-symbolic");
3690 impl->browse_shortcuts_remove_button = toolbutton_new (impl,
3694 G_CALLBACK (remove_bookmark_button_clicked_cb));
3695 g_object_unref (icon);
3696 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (impl->browse_shortcuts_remove_button), 1);
3697 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3698 _("Remove the selected bookmark"));
3704 key_is_left_or_right (GdkEventKey *event)
3708 modifiers = gtk_accelerator_get_default_mod_mask ();
3710 return ((event->keyval == GDK_KEY_Right
3711 || event->keyval == GDK_KEY_KP_Right
3712 || event->keyval == GDK_KEY_Left
3713 || event->keyval == GDK_KEY_KP_Left)
3714 && (event->state & modifiers) == 0);
3717 /* Handles key press events on the file list, so that we can trap Enter to
3718 * activate the default button on our own. Also, checks to see if '/' has been
3722 browse_files_key_press_event_cb (GtkWidget *widget,
3726 GtkFileChooserDefault *impl;
3727 GdkModifierType no_text_input_mask;
3729 impl = (GtkFileChooserDefault *) data;
3731 no_text_input_mask =
3732 gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_NO_TEXT_INPUT);
3734 if ((event->keyval == GDK_KEY_slash
3735 || event->keyval == GDK_KEY_KP_Divide
3737 || event->keyval == GDK_KEY_asciitilde
3739 ) && !(event->state & no_text_input_mask))
3741 location_popup_handler (impl, event->string);
3745 if (key_is_left_or_right (event))
3747 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3751 if ((event->keyval == GDK_KEY_Return
3752 || event->keyval == GDK_KEY_ISO_Enter
3753 || event->keyval == GDK_KEY_KP_Enter
3754 || event->keyval == GDK_KEY_space
3755 || event->keyval == GDK_KEY_KP_Space)
3756 && !(event->state & gtk_accelerator_get_default_mod_mask ())
3757 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3758 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3762 window = get_toplevel (widget);
3765 GtkWidget *default_widget, *focus_widget;
3767 default_widget = gtk_window_get_default_widget (window);
3768 focus_widget = gtk_window_get_focus (window);
3770 if (widget != default_widget &&
3771 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3773 gtk_window_activate_default (window);
3783 /* Callback used when the file list's popup menu is detached */
3785 popup_menu_detach_cb (GtkWidget *attach_widget,
3788 GtkFileChooserDefault *impl;
3790 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3791 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3793 impl->browse_files_popup_menu = NULL;
3794 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3795 impl->browse_files_popup_menu_hidden_files_item = NULL;
3796 impl->browse_files_popup_menu_copy_file_location_item = NULL;
3799 /* Callback used when the "Add to Bookmarks" menu item is activated */
3801 add_to_shortcuts_cb (GtkMenuItem *item,
3802 GtkFileChooserDefault *impl)
3804 bookmarks_add_selected_folder (impl);
3807 /* callback used to set data to clipboard */
3809 copy_file_get_cb (GtkClipboard *clipboard,
3810 GtkSelectionData *selection_data,
3814 GSList *selected_files = data;
3818 gint num_files = g_slist_length (selected_files);
3823 uris = g_new (gchar *, num_files + 1);
3824 uris[num_files] = NULL; /* null terminator */
3828 for (l = selected_files; l; l = l->next)
3830 GFile *file = (GFile *) l->data;
3832 if (info == SELECTION_URI)
3833 uris[i] = g_file_get_uri (file);
3834 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3835 uris[i] = g_file_get_parse_name (file);
3840 if (info == SELECTION_URI)
3841 gtk_selection_data_set_uris (selection_data, uris);
3842 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3844 char *str = g_strjoinv (" ", uris);
3845 gtk_selection_data_set_text (selection_data, str, -1);
3853 /* callback used to clear the clipboard data */
3855 copy_file_clear_cb (GtkClipboard *clipboard,
3858 GSList *selected_files = data;
3860 g_slist_foreach (selected_files, (GFunc) g_object_unref, NULL);
3861 g_slist_free (selected_files);
3864 /* Callback used when the "Copy file's location" menu item is activated */
3866 copy_file_location_cb (GtkMenuItem *item,
3867 GtkFileChooserDefault *impl)
3869 GSList *selected_files = NULL;
3871 selected_files = search_get_selected_files (impl);
3875 GtkClipboard *clipboard;
3876 GtkTargetList *target_list;
3877 GtkTargetEntry *targets;
3880 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl), GDK_SELECTION_CLIPBOARD);
3882 target_list = gtk_target_list_new (NULL, 0);
3883 gtk_target_list_add_text_targets (target_list, SELECTION_TEXT);
3884 gtk_target_list_add_uri_targets (target_list, SELECTION_URI);
3886 targets = gtk_target_table_new_from_list (target_list, &n_targets);
3887 gtk_target_list_unref (target_list);
3889 gtk_clipboard_set_with_data (clipboard, targets, n_targets,
3894 gtk_target_table_free (targets, n_targets);
3898 /* Callback used when the "Visit this file" menu item is activated */
3900 visit_file_cb (GtkMenuItem *item,
3901 GtkFileChooserDefault *impl)
3905 files = search_get_selected_files (impl);
3907 /* Sigh, just use the first one */
3910 GFile *file = files->data;
3912 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL); /* NULL-GError */
3915 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
3916 g_slist_free (files);
3919 /* callback used when the "Show Hidden Files" menu item is toggled */
3921 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3922 GtkFileChooserDefault *impl)
3925 "show-hidden", gtk_check_menu_item_get_active (item),
3929 /* Callback used when the "Show Size Column" menu item is toggled */
3931 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3932 GtkFileChooserDefault *impl)
3934 impl->show_size_column = gtk_check_menu_item_get_active (item);
3936 gtk_tree_view_column_set_visible (impl->list_size_column,
3937 impl->show_size_column);
3940 /* Shows an error dialog about not being able to select a dragged file */
3942 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3947 _("Could not select file"),
3952 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3957 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3959 for (i = 1; uris[i]; i++)
3962 GError *error = NULL;
3965 file = g_file_new_for_uri (uri);
3967 gtk_file_chooser_default_select_file (chooser, file, &error);
3969 error_selecting_dragged_file_dialog (impl, file, error);
3971 g_object_unref (file);
3975 struct FileListDragData
3977 GtkFileChooserDefault *impl;
3983 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3985 const GError *error,
3988 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3989 struct FileListDragData *data = user_data;
3990 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3992 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3995 data->impl->file_list_drag_data_received_cancellable = NULL;
3997 if (cancelled || error)
4000 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4001 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4002 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4003 change_folder_and_display_error (data->impl, data->file, FALSE);
4006 GError *error = NULL;
4008 gtk_file_chooser_default_unselect_all (chooser);
4009 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4011 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4013 browse_files_center_selected_row (data->impl);
4016 if (data->impl->select_multiple)
4017 file_list_drag_data_select_uris (data->impl, data->uris);
4020 g_object_unref (data->impl);
4021 g_strfreev (data->uris);
4022 g_object_unref (data->file);
4025 g_object_unref (cancellable);
4029 file_list_drag_data_received_cb (GtkWidget *widget,
4030 GdkDragContext *context,
4033 GtkSelectionData *selection_data,
4038 GtkFileChooserDefault *impl;
4043 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4045 /* Allow only drags from other widgets; see bug #533891. */
4046 if (gtk_drag_get_source_widget (context) == widget)
4048 g_signal_stop_emission_by_name (widget, "drag-data-received");
4052 /* Parse the text/uri-list string, navigate to the first one */
4053 uris = gtk_selection_data_get_uris (selection_data);
4054 if (uris && uris[0])
4056 struct FileListDragData *data;
4059 file = g_file_new_for_uri (uri);
4061 data = g_new0 (struct FileListDragData, 1);
4062 data->impl = g_object_ref (impl);
4066 if (impl->file_list_drag_data_received_cancellable)
4067 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4069 impl->file_list_drag_data_received_cancellable =
4070 _gtk_file_system_get_info (impl->file_system, file,
4072 file_list_drag_data_received_get_info_cb,
4076 g_signal_stop_emission_by_name (widget, "drag-data-received");
4079 /* Don't do anything with the drag_drop signal */
4081 file_list_drag_drop_cb (GtkWidget *widget,
4082 GdkDragContext *context,
4086 GtkFileChooserDefault *impl)
4088 g_signal_stop_emission_by_name (widget, "drag-drop");
4092 /* Disable the normal tree drag motion handler, it makes it look like you're
4093 dropping the dragged item onto a tree item */
4095 file_list_drag_motion_cb (GtkWidget *widget,
4096 GdkDragContext *context,
4100 GtkFileChooserDefault *impl)
4102 g_signal_stop_emission_by_name (widget, "drag-motion");
4106 /* Sensitizes the "Copy file's location" context menu item if there is actually
4107 * a selection active.
4110 check_copy_file_location_sensitivity (GtkFileChooserDefault *impl)
4112 GtkTreeSelection *selection;
4115 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4116 if (gtk_tree_selection_count_selected_rows (selection) == 0)
4121 if (impl->browse_files_popup_menu_copy_file_location_item)
4122 gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_file_location_item, active);
4126 file_list_add_image_menu_item (GtkFileChooserDefault *impl,
4127 const char *stock_name,
4128 const char *mnemonic_label,
4133 item = gtk_image_menu_item_new_with_mnemonic (mnemonic_label);
4134 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4135 gtk_image_new_from_stock (stock_name, GTK_ICON_SIZE_MENU));
4136 g_signal_connect (item, "activate", callback, impl);
4137 gtk_widget_show (item);
4138 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4144 file_list_add_check_menu_item (GtkFileChooserDefault *impl,
4145 const char *mnemonic_label,
4150 item = gtk_check_menu_item_new_with_mnemonic (mnemonic_label);
4151 g_signal_connect (item, "toggled", callback, impl);
4152 gtk_widget_show (item);
4153 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4158 /* Constructs the popup menu for the file list if needed */
4160 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4164 if (impl->browse_files_popup_menu)
4167 impl->browse_files_popup_menu = gtk_menu_new ();
4168 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4169 impl->browse_files_tree_view,
4170 popup_menu_detach_cb);
4172 impl->browse_files_popup_menu_visit_file_item = file_list_add_image_menu_item (impl, GTK_STOCK_DIRECTORY, _("_Visit this file"),
4173 G_CALLBACK (visit_file_cb));
4175 impl->browse_files_popup_menu_copy_file_location_item = file_list_add_image_menu_item (impl, GTK_STOCK_COPY, _("_Copy file's location"),
4176 G_CALLBACK (copy_file_location_cb));
4178 impl->browse_files_popup_menu_add_shortcut_item = file_list_add_image_menu_item (impl, GTK_STOCK_ADD, _("_Add to Bookmarks"),
4179 G_CALLBACK (add_to_shortcuts_cb));
4181 item = gtk_separator_menu_item_new ();
4182 gtk_widget_show (item);
4183 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4185 impl->browse_files_popup_menu_hidden_files_item = file_list_add_check_menu_item (impl, _("Show _Hidden Files"),
4186 G_CALLBACK (show_hidden_toggled_cb));
4188 impl->browse_files_popup_menu_size_column_item = file_list_add_check_menu_item (impl, _("Show _Size Column"),
4189 G_CALLBACK (show_size_column_toggled_cb));
4191 bookmarks_check_add_sensitivity (impl);
4192 check_copy_file_location_sensitivity (impl);
4195 /* Updates the popup menu for the file list, creating it if necessary */
4197 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4199 file_list_build_popup_menu (impl);
4201 /* The sensitivity of the Add to Bookmarks item is set in
4202 * bookmarks_check_add_sensitivity()
4205 /* 'Visit this file' */
4206 gtk_widget_set_visible (impl->browse_files_popup_menu_visit_file_item, (impl->operation_mode != OPERATION_MODE_BROWSE));
4208 /* 'Show Hidden Files' */
4209 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4210 G_CALLBACK (show_hidden_toggled_cb), impl);
4211 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4213 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4214 G_CALLBACK (show_hidden_toggled_cb), impl);
4216 /* 'Show Size Column' */
4217 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4218 G_CALLBACK (show_size_column_toggled_cb), impl);
4219 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4220 impl->show_size_column);
4221 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4222 G_CALLBACK (show_size_column_toggled_cb), impl);
4226 popup_position_func (GtkMenu *menu,
4232 GtkAllocation allocation;
4233 GtkWidget *widget = GTK_WIDGET (user_data);
4234 GdkScreen *screen = gtk_widget_get_screen (widget);
4237 GdkRectangle monitor;
4239 g_return_if_fail (gtk_widget_get_realized (widget));
4241 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4243 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4246 gtk_widget_get_allocation (widget, &allocation);
4247 *x += (allocation.width - req.width) / 2;
4248 *y += (allocation.height - req.height) / 2;
4250 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4251 gtk_menu_set_monitor (menu, monitor_num);
4252 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
4254 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4255 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4261 file_list_popup_menu (GtkFileChooserDefault *impl,
4262 GdkEventButton *event)
4264 file_list_update_popup_menu (impl);
4266 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4267 NULL, NULL, NULL, NULL,
4268 event->button, event->time);
4271 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4273 popup_position_func, impl->browse_files_tree_view,
4274 0, GDK_CURRENT_TIME);
4275 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4281 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4283 list_popup_menu_cb (GtkWidget *widget,
4284 GtkFileChooserDefault *impl)
4286 file_list_popup_menu (impl, NULL);
4290 /* Callback used when a button is pressed on the file list. We trap button 3 to
4291 * bring up a popup menu.
4294 list_button_press_event_cb (GtkWidget *widget,
4295 GdkEventButton *event,
4296 GtkFileChooserDefault *impl)
4298 static gboolean in_press = FALSE;
4303 if (!gdk_event_triggers_context_menu ((GdkEvent *) event))
4307 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4310 file_list_popup_menu (impl, event);
4315 OperationMode operation_mode;
4316 gint general_column;
4320 /* Sets the sort column IDs for the file list; needs to be done whenever we
4321 * change the model on the treeview.
4324 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4326 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4327 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4328 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4332 file_list_query_tooltip_cb (GtkWidget *widget,
4335 gboolean keyboard_tip,
4336 GtkTooltip *tooltip,
4339 GtkFileChooserDefault *impl = user_data;
4340 GtkTreeModel *model;
4346 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4350 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4353 &model, &path, &iter))
4356 gtk_tree_model_get (model, &iter,
4357 MODEL_COL_FILE, &file,
4362 gtk_tree_path_free (path);
4366 filename = g_file_get_path (file);
4367 gtk_tooltip_set_text (tooltip, filename);
4368 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4373 g_object_unref (file);
4374 gtk_tree_path_free (path);
4380 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4384 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4385 gtk_cell_renderer_set_fixed_size (renderer,
4386 xpad * 2 + impl->icon_size,
4387 ypad * 2 + impl->icon_size);
4390 /* Creates the widgets for the file list */
4392 create_file_list (GtkFileChooserDefault *impl)
4395 GtkTreeSelection *selection;
4396 GtkTreeViewColumn *column;
4397 GtkCellRenderer *renderer;
4399 /* Scrolled window */
4400 swin = gtk_scrolled_window_new (NULL, NULL);
4401 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4402 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4403 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4406 /* Tree/list view */
4408 impl->browse_files_tree_view = gtk_tree_view_new ();
4409 #ifdef PROFILE_FILE_CHOOSER
4410 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4412 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4413 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4415 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4416 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4418 gtk_drag_dest_set (impl->browse_files_tree_view,
4419 GTK_DEST_DEFAULT_ALL,
4421 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4422 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4424 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4425 G_CALLBACK (list_row_activated), impl);
4426 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4427 G_CALLBACK (browse_files_key_press_event_cb), impl);
4428 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4429 G_CALLBACK (list_popup_menu_cb), impl);
4430 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4431 G_CALLBACK (list_button_press_event_cb), impl);
4433 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4434 G_CALLBACK (file_list_drag_data_received_cb), impl);
4435 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4436 G_CALLBACK (file_list_drag_drop_cb), impl);
4437 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4438 G_CALLBACK (file_list_drag_motion_cb), impl);
4440 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4441 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4442 G_CALLBACK (file_list_query_tooltip_cb), impl);
4444 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4445 gtk_tree_selection_set_select_function (selection,
4448 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4451 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4452 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4454 g_signal_connect (selection, "changed",
4455 G_CALLBACK (list_selection_changed), impl);
4457 /* Keep the column order in sync with update_cell_renderer_attributes() */
4459 /* Filename column */
4461 impl->list_name_column = gtk_tree_view_column_new ();
4462 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4463 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4464 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4466 renderer = gtk_cell_renderer_pixbuf_new ();
4467 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4468 set_icon_cell_renderer_fixed_size (impl, renderer);
4469 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4471 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4472 g_object_set (impl->list_name_renderer,
4473 "ellipsize", PANGO_ELLIPSIZE_END,
4475 g_signal_connect (impl->list_name_renderer, "edited",
4476 G_CALLBACK (renderer_edited_cb), impl);
4477 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4478 G_CALLBACK (renderer_editing_canceled_cb), impl);
4479 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4481 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4485 column = gtk_tree_view_column_new ();
4486 gtk_tree_view_column_set_resizable (column, TRUE);
4487 gtk_tree_view_column_set_title (column, _("Size"));
4489 renderer = gtk_cell_renderer_text_new ();
4490 g_object_set (renderer,
4491 "alignment", PANGO_ALIGN_RIGHT,
4493 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4494 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4495 impl->list_size_column = column;
4497 /* Modification time column */
4499 column = gtk_tree_view_column_new ();
4500 gtk_tree_view_column_set_resizable (column, TRUE);
4501 gtk_tree_view_column_set_title (column, _("Modified"));
4503 renderer = gtk_cell_renderer_text_new ();
4504 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4505 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4506 impl->list_mtime_column = column;
4508 file_list_set_sort_column_ids (impl);
4509 update_cell_renderer_attributes (impl);
4511 gtk_widget_show_all (swin);
4516 /* Creates the widgets for the files/folders pane */
4518 file_pane_create (GtkFileChooserDefault *impl,
4519 GtkSizeGroup *size_group)
4525 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4526 gtk_widget_show (vbox);
4528 /* Box for lists and preview */
4530 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4531 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4532 gtk_widget_show (hbox);
4536 widget = create_file_list (impl);
4537 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4538 gtk_size_group_add_widget (size_group, widget);
4542 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4543 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4544 /* Don't show preview box initially */
4548 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4550 widget = filter_create (impl);
4552 gtk_widget_show (widget);
4553 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4555 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4561 location_entry_create (GtkFileChooserDefault *impl)
4563 if (!impl->location_entry)
4564 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4566 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4567 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4568 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4569 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4572 /* Creates the widgets specific to Save mode */
4574 save_widgets_create (GtkFileChooserDefault *impl)
4579 if (impl->save_widgets != NULL)
4582 location_switch_to_path_bar (impl);
4584 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4586 impl->save_widgets_table = gtk_grid_new ();
4587 gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4588 gtk_widget_show (impl->save_widgets_table);
4589 gtk_grid_set_row_spacing (GTK_GRID (impl->save_widgets_table), 12);
4590 gtk_grid_set_column_spacing (GTK_GRID (impl->save_widgets_table), 12);
4594 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4595 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4596 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4597 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), widget, 0, 0, 1, 1);
4598 gtk_widget_show (widget);
4600 /* Location entry */
4602 location_entry_create (impl);
4603 gtk_widget_set_hexpand (impl->location_entry, TRUE);
4604 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->location_entry, 1, 0, 1, 1);
4605 gtk_widget_show (impl->location_entry);
4606 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4609 impl->save_folder_label = gtk_label_new (NULL);
4610 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4611 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4612 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->save_folder_label, 0, 1, 1, 1);
4613 gtk_widget_show (impl->save_folder_label);
4615 impl->save_widgets = vbox;
4616 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4617 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4618 gtk_widget_show (impl->save_widgets);
4621 /* Destroys the widgets specific to Save mode */
4623 save_widgets_destroy (GtkFileChooserDefault *impl)
4625 if (impl->save_widgets == NULL)
4628 gtk_widget_destroy (impl->save_widgets);
4629 impl->save_widgets = NULL;
4630 impl->save_widgets_table = NULL;
4631 impl->location_entry = NULL;
4632 impl->save_folder_label = NULL;
4635 /* Turns on the path bar widget. Can be called even if we are already in that
4639 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4641 if (impl->location_entry)
4643 gtk_widget_destroy (impl->location_entry);
4644 impl->location_entry = NULL;
4647 gtk_widget_hide (impl->location_entry_box);
4650 /* Turns on the location entry. Can be called even if we are already in that
4654 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4656 /* when in search or recent files mode, we are not showing the
4657 * location_entry_box container, so there's no point in switching
4660 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4661 impl->operation_mode == OPERATION_MODE_RECENT)
4666 gtk_widget_show (impl->location_entry_box);
4670 if (!impl->location_entry)
4672 location_entry_create (impl);
4673 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4674 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4677 /* Configure the entry */
4679 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4683 gtk_widget_show (impl->location_entry);
4684 gtk_widget_grab_focus (impl->location_entry);
4687 /* Sets a new location mode. set_buttons determines whether the toggle button
4688 * for the mode will also be changed.
4691 location_mode_set (GtkFileChooserDefault *impl,
4692 LocationMode new_mode,
4693 gboolean set_button)
4695 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4696 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4698 GtkWindow *toplevel;
4699 GtkWidget *current_focus;
4700 gboolean button_active;
4701 gboolean switch_to_file_list;
4705 case LOCATION_MODE_PATH_BAR:
4706 button_active = FALSE;
4708 /* The location_entry will disappear when we switch to path bar mode. So,
4709 * we'll focus the file list in that case, to avoid having a window with
4710 * no focused widget.
4712 toplevel = get_toplevel (GTK_WIDGET (impl));
4713 switch_to_file_list = FALSE;
4716 current_focus = gtk_window_get_focus (toplevel);
4717 if (!current_focus || current_focus == impl->location_entry)
4718 switch_to_file_list = TRUE;
4721 location_switch_to_path_bar (impl);
4723 if (switch_to_file_list)
4724 gtk_widget_grab_focus (impl->browse_files_tree_view);
4728 case LOCATION_MODE_FILENAME_ENTRY:
4729 button_active = TRUE;
4730 location_switch_to_filename_entry (impl);
4734 g_assert_not_reached ();
4740 g_signal_handlers_block_by_func (impl->location_button,
4741 G_CALLBACK (location_button_toggled_cb), impl);
4743 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4745 g_signal_handlers_unblock_by_func (impl->location_button,
4746 G_CALLBACK (location_button_toggled_cb), impl);
4750 impl->location_mode = new_mode;
4754 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4756 /* when in search or recent files mode, we are not showing the
4757 * location_entry_box container, so there's no point in switching
4760 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4761 impl->operation_mode == OPERATION_MODE_RECENT)
4764 /* If the file entry is not visible, show it.
4765 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4767 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4769 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4771 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4773 if (gtk_widget_has_focus (impl->location_entry))
4775 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4779 gtk_widget_grab_focus (impl->location_entry);
4784 /* Callback used when one of the location mode buttons is toggled */
4786 location_button_toggled_cb (GtkToggleButton *toggle,
4787 GtkFileChooserDefault *impl)
4790 LocationMode new_mode;
4792 is_active = gtk_toggle_button_get_active (toggle);
4796 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4797 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4801 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4802 new_mode = LOCATION_MODE_PATH_BAR;
4805 location_mode_set (impl, new_mode, FALSE);
4808 /* Creates a toggle button for the location entry. */
4810 location_button_create (GtkFileChooserDefault *impl)
4815 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4816 gtk_widget_show (image);
4818 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4822 g_signal_connect (impl->location_button, "toggled",
4823 G_CALLBACK (location_button_toggled_cb), impl);
4825 str = _("Type a file name");
4827 gtk_widget_set_tooltip_text (impl->location_button, str);
4828 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4832 PATH_BAR_FOLDER_PATH,
4833 PATH_BAR_SELECT_A_FOLDER,
4834 PATH_BAR_ERROR_NO_FILENAME,
4835 PATH_BAR_ERROR_NO_FOLDER,
4836 PATH_BAR_RECENTLY_USED,
4840 /* Creates the info bar for informational messages or warnings, with its icon and label */
4842 info_bar_create (GtkFileChooserDefault *impl)
4844 GtkWidget *content_area;
4846 impl->browse_select_a_folder_info_bar = gtk_info_bar_new ();
4847 impl->browse_select_a_folder_icon = gtk_image_new_from_stock (GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU);
4848 impl->browse_select_a_folder_label = gtk_label_new (NULL);
4850 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar));
4852 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_icon, FALSE, FALSE, 0);
4853 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_label, FALSE, FALSE, 0);
4855 gtk_widget_show (impl->browse_select_a_folder_icon);
4856 gtk_widget_show (impl->browse_select_a_folder_label);
4859 /* Sets the info bar to show the appropriate informational or warning message */
4861 info_bar_set (GtkFileChooserDefault *impl, PathBarMode mode)
4865 GtkMessageType message_type;
4871 case PATH_BAR_SELECT_A_FOLDER:
4872 str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
4874 message_type = GTK_MESSAGE_OTHER;
4877 case PATH_BAR_ERROR_NO_FILENAME:
4878 str = _("Please type a file name");
4879 message_type = GTK_MESSAGE_WARNING;
4882 case PATH_BAR_ERROR_NO_FOLDER:
4883 str = _("Please select a folder below");
4884 message_type = GTK_MESSAGE_WARNING;
4888 g_assert_not_reached ();
4892 gtk_info_bar_set_message_type (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar), message_type);
4893 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_select_a_folder_icon),
4894 (message_type == GTK_MESSAGE_WARNING) ? GTK_STOCK_DIALOG_WARNING : GTK_STOCK_DIRECTORY,
4895 GTK_ICON_SIZE_MENU);
4896 gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
4902 /* Creates the icon and label used to show that the file chooser is in Search or Recently-used mode */
4904 special_mode_widgets_create (GtkFileChooserDefault *impl)
4906 impl->browse_special_mode_icon = gtk_image_new ();
4907 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_icon);
4908 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_icon, FALSE, FALSE, 0);
4910 impl->browse_special_mode_label = gtk_label_new (NULL);
4911 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_label);
4912 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_label, FALSE, FALSE, 0);
4915 /* Creates the path bar's container and eveyrthing that goes in it: location button, pathbar, info bar, and Create Folder button */
4917 path_bar_widgets_create (GtkFileChooserDefault *impl)
4919 /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4920 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4921 gtk_widget_show (impl->browse_path_bar_hbox);
4923 /* Size group that allows the path bar to be the same size between modes */
4924 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4925 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4927 /* Location button */
4928 location_button_create (impl);
4929 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4930 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4933 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4934 _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system);
4935 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4937 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4938 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4941 info_bar_create (impl);
4942 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_select_a_folder_info_bar);
4943 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_select_a_folder_info_bar, TRUE, TRUE, 0);
4945 /* Widgets for special modes (recently-used in Open mode, Search mode) */
4946 special_mode_widgets_create (impl);
4949 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4950 g_signal_connect (impl->browse_new_folder_button, "clicked",
4951 G_CALLBACK (new_folder_button_clicked), impl);
4952 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_new_folder_button);
4953 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4956 /* Sets the path bar's mode to show a label, the actual folder path, or a
4957 * warning message. You may call this function with PATH_BAR_ERROR_* directly
4958 * if the pathbar is already showing the widgets you expect; otherwise, call
4959 * path_bar_update() instead to set the appropriate widgets automatically.
4962 path_bar_set_mode (GtkFileChooserDefault *impl, PathBarMode mode)
4964 gboolean path_bar_visible = FALSE;
4965 gboolean special_mode_widgets_visible = FALSE;
4966 gboolean info_bar_visible = FALSE;
4967 gboolean create_folder_visible = FALSE;
4973 case PATH_BAR_FOLDER_PATH:
4974 path_bar_visible = TRUE;
4977 case PATH_BAR_SELECT_A_FOLDER:
4978 case PATH_BAR_ERROR_NO_FILENAME:
4979 case PATH_BAR_ERROR_NO_FOLDER:
4980 info_bar_set (impl, mode);
4981 info_bar_visible = TRUE;
4984 case PATH_BAR_RECENTLY_USED:
4985 gtk_image_set_from_icon_name (GTK_IMAGE (impl->browse_special_mode_icon), "document-open-recent", GTK_ICON_SIZE_BUTTON);
4987 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
4988 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4991 special_mode_widgets_visible = TRUE;
4994 case PATH_BAR_SEARCH:
4995 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_special_mode_icon), GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
4997 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
4998 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
5001 special_mode_widgets_visible = TRUE;
5005 g_assert_not_reached ();
5008 gtk_widget_set_visible (impl->browse_path_bar, path_bar_visible);
5009 gtk_widget_set_visible (impl->browse_special_mode_icon, special_mode_widgets_visible);
5010 gtk_widget_set_visible (impl->browse_special_mode_label, special_mode_widgets_visible);
5011 gtk_widget_set_visible (impl->browse_select_a_folder_info_bar, info_bar_visible);
5013 if (path_bar_visible)
5015 if (impl->create_folders
5016 && impl->action != GTK_FILE_CHOOSER_ACTION_OPEN
5017 && impl->operation_mode != OPERATION_MODE_RECENT)
5018 create_folder_visible = TRUE;
5021 gtk_widget_set_visible (impl->browse_new_folder_button, create_folder_visible);
5024 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5026 browse_widgets_create (GtkFileChooserDefault *impl)
5030 GtkSizeGroup *size_group;
5032 impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5033 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
5034 gtk_widget_show (impl->browse_widgets_box);
5036 impl->browse_header_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5037 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_header_box, FALSE, FALSE, 0);
5038 gtk_widget_show (impl->browse_header_box);
5040 /* Path bar, info bar, and their respective machinery - the browse_path_bar_hbox will get packed elsewhere */
5041 path_bar_widgets_create (impl);
5043 /* Box for the location label and entry */
5045 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5046 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->location_entry_box, FALSE, FALSE, 0);
5048 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5049 gtk_widget_show (impl->location_label);
5050 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5052 /* size group is used by the scrolled windows of the panes */
5053 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5057 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5058 gtk_widget_show (hpaned);
5059 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
5061 widget = shortcuts_pane_create (impl, size_group);
5062 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5063 widget = file_pane_create (impl, size_group);
5064 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5065 gtk_paned_set_position (GTK_PANED (hpaned), 148);
5066 g_object_unref (size_group);
5070 gtk_file_chooser_default_constructor (GType type,
5071 guint n_construct_properties,
5072 GObjectConstructParam *construct_params)
5074 GtkFileChooserDefault *impl;
5077 profile_start ("start", NULL);
5079 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5080 n_construct_properties,
5082 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5084 g_assert (impl->file_system);
5086 gtk_widget_push_composite_child ();
5088 /* Shortcuts model */
5089 shortcuts_model_create (impl);
5091 /* The browse widgets */
5092 browse_widgets_create (impl);
5094 /* Alignment to hold extra widget */
5095 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
5096 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
5097 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
5098 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5100 gtk_widget_pop_composite_child ();
5101 update_appearance (impl);
5103 profile_end ("end", NULL);
5108 /* Sets the extra_widget by packing it in the appropriate place */
5110 set_extra_widget (GtkFileChooserDefault *impl,
5111 GtkWidget *extra_widget)
5115 g_object_ref (extra_widget);
5116 /* FIXME: is this right ? */
5117 gtk_widget_show (extra_widget);
5120 if (impl->extra_widget)
5122 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5123 g_object_unref (impl->extra_widget);
5126 impl->extra_widget = extra_widget;
5127 if (impl->extra_widget)
5129 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5130 gtk_widget_show (impl->extra_align);
5133 gtk_widget_hide (impl->extra_align);
5137 set_local_only (GtkFileChooserDefault *impl,
5138 gboolean local_only)
5140 if (local_only != impl->local_only)
5142 impl->local_only = local_only;
5144 if (impl->location_entry)
5145 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5147 if (impl->shortcuts_model && impl->file_system)
5149 shortcuts_add_volumes (impl);
5150 shortcuts_add_bookmarks (impl);
5153 if (local_only && impl->current_folder &&
5154 !g_file_is_native (impl->current_folder))
5156 /* If we are pointing to a non-local folder, make an effort to change
5157 * back to a local folder, but it's really up to the app to not cause
5158 * such a situation, so we ignore errors.
5160 const gchar *home = g_get_home_dir ();
5166 home_file = g_file_new_for_path (home);
5168 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5170 g_object_unref (home_file);
5176 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5177 GtkFileChooserDefault *impl)
5179 shortcuts_add_volumes (impl);
5180 shortcuts_add_bookmarks (impl);
5182 bookmarks_check_add_sensitivity (impl);
5183 bookmarks_check_remove_sensitivity (impl);
5184 shortcuts_check_popup_sensitivity (impl);
5187 /* Sets the file chooser to multiple selection mode */
5189 set_select_multiple (GtkFileChooserDefault *impl,
5190 gboolean select_multiple,
5191 gboolean property_notify)
5193 GtkTreeSelection *selection;
5194 GtkSelectionMode mode;
5196 if (select_multiple == impl->select_multiple)
5199 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
5201 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5202 gtk_tree_selection_set_mode (selection, mode);
5204 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5206 impl->select_multiple = select_multiple;
5207 g_object_notify (G_OBJECT (impl), "select-multiple");
5209 check_preview_change (impl);
5213 set_file_system_backend (GtkFileChooserDefault *impl)
5215 profile_start ("start for backend", "default");
5217 impl->file_system = _gtk_file_system_new ();
5219 g_signal_connect (impl->file_system, "volumes-changed",
5220 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5221 g_signal_connect (impl->file_system, "bookmarks-changed",
5222 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5224 profile_end ("end", NULL);
5228 unset_file_system_backend (GtkFileChooserDefault *impl)
5230 g_signal_handlers_disconnect_by_func (impl->file_system,
5231 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5233 g_object_unref (impl->file_system);
5235 impl->file_system = NULL;
5238 /* Saves the widgets around the pathbar so they can be reparented later
5239 * in the correct place. This function must be called paired with
5240 * restore_path_bar().
5243 save_path_bar (GtkFileChooserDefault *impl)
5247 g_object_ref (impl->browse_path_bar_hbox);
5249 parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
5251 gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
5254 /* Reparents the path bar and the "Create folder" button to the right place:
5255 * Above the file list in Open mode, or to the right of the "Save in folder:"
5256 * label in Save mode. The save_path_bar() function must be called before this
5260 restore_path_bar (GtkFileChooserDefault *impl)
5262 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5263 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5265 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5266 gtk_box_reorder_child (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, 0);
5268 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5269 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5271 gtk_widget_set_hexpand (impl->browse_path_bar_hbox, TRUE);
5272 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->browse_path_bar_hbox, 1, 1, 1, 1);
5273 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5276 g_assert_not_reached ();
5278 g_object_unref (impl->browse_path_bar_hbox);
5281 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5283 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5287 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5288 MODEL_COL_FILE, &file,
5290 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE);
5291 g_object_unref (file);
5294 /* Sets the pathbar in the appropriate mode according to the current operation mode and action. This is the central function for
5295 * dealing with the pathbar's widgets; as long as impl->action and impl->operation_mode are set correctly, then calling this
5296 * function will update all the pathbar's widgets.
5299 path_bar_update (GtkFileChooserDefault *impl)
5303 switch (impl->operation_mode)
5305 case OPERATION_MODE_BROWSE:
5306 mode = PATH_BAR_FOLDER_PATH;
5309 case OPERATION_MODE_RECENT:
5310 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5312 GtkTreeSelection *selection;
5313 gboolean have_selected;
5316 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5318 /* Save mode means single-selection mode, so the following is valid */
5319 have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5323 mode = PATH_BAR_FOLDER_PATH;
5324 put_recent_folder_in_pathbar (impl, &iter);
5327 mode = PATH_BAR_SELECT_A_FOLDER;
5330 mode = PATH_BAR_RECENTLY_USED;
5334 case OPERATION_MODE_SEARCH:
5335 mode = PATH_BAR_SEARCH;
5339 g_assert_not_reached ();
5343 path_bar_set_mode (impl, mode);
5347 operation_mode_discard_search_widgets (GtkFileChooserDefault *impl)
5349 if (impl->search_hbox)
5351 gtk_widget_destroy (impl->search_hbox);
5353 impl->search_hbox = NULL;
5354 impl->search_entry = NULL;
5358 /* Stops running operations like populating the browse model, searches, and the recent-files model */
5360 operation_mode_stop (GtkFileChooserDefault *impl, OperationMode mode)
5364 case OPERATION_MODE_BROWSE:
5365 stop_loading_and_clear_list_model (impl, TRUE);
5368 case OPERATION_MODE_SEARCH:
5369 search_stop_searching (impl, FALSE);
5370 search_clear_model (impl, TRUE);
5372 operation_mode_discard_search_widgets (impl);
5375 case OPERATION_MODE_RECENT:
5376 recent_stop_loading (impl);
5377 recent_clear_model (impl, TRUE);
5381 g_assert_not_reached ();
5386 operation_mode_set_browse (GtkFileChooserDefault *impl)
5388 path_bar_update (impl);
5390 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5391 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5393 gtk_widget_show (impl->location_button);
5394 location_mode_set (impl, impl->location_mode, TRUE);
5396 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5397 gtk_widget_show (impl->location_entry_box);
5402 operation_mode_set_search (GtkFileChooserDefault *impl)
5404 g_assert (impl->search_hbox == NULL);
5405 g_assert (impl->search_entry == NULL);
5406 g_assert (impl->search_model == NULL);
5408 search_setup_widgets (impl);
5412 operation_mode_set_recent (GtkFileChooserDefault *impl)
5414 path_bar_update (impl);
5416 /* Hide the location widgets temporarily */
5417 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5418 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5420 gtk_widget_hide (impl->location_button);
5421 gtk_widget_hide (impl->location_entry_box);
5424 recent_start_loading (impl);
5427 /* Sometimes we need to frob the selection in the shortcuts list manually */
5429 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
5431 GtkTreeSelection *selection;
5434 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
5436 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5438 path = gtk_tree_path_new_from_indices (pos, -1);
5439 gtk_tree_selection_select_path (selection, path);
5440 gtk_tree_path_free (path);
5442 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5446 operation_mode_set (GtkFileChooserDefault *impl, OperationMode mode)
5448 ShortcutsIndex shortcut_to_select;
5450 operation_mode_stop (impl, impl->operation_mode);
5452 impl->operation_mode = mode;
5454 switch (impl->operation_mode)
5456 case OPERATION_MODE_BROWSE:
5457 operation_mode_set_browse (impl);
5458 shortcut_to_select = SHORTCUTS_CURRENT_FOLDER;
5461 case OPERATION_MODE_SEARCH:
5462 operation_mode_set_search (impl);
5463 shortcut_to_select = SHORTCUTS_SEARCH;
5466 case OPERATION_MODE_RECENT:
5467 operation_mode_set_recent (impl);
5468 shortcut_to_select = SHORTCUTS_RECENT;
5472 g_assert_not_reached ();
5476 if (shortcut_to_select != SHORTCUTS_CURRENT_FOLDER)
5477 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, shortcut_to_select));
5480 /* This function is basically a do_all function.
5482 * It sets the visibility on all the widgets based on the current state, and
5483 * moves the custom_widget if needed.
5486 update_appearance (GtkFileChooserDefault *impl)
5488 save_path_bar (impl);
5490 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5491 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5495 gtk_widget_hide (impl->location_button);
5496 save_widgets_create (impl);
5498 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5499 text = _("Save in _folder:");
5501 text = _("Create in _folder:");
5503 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5505 if (impl->select_multiple)
5507 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5508 "Re-setting to single selection mode.");
5509 set_select_multiple (impl, FALSE, TRUE);
5512 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5513 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5515 gtk_widget_show (impl->location_button);
5516 save_widgets_destroy (impl);
5517 location_mode_set (impl, impl->location_mode, TRUE);
5520 if (impl->location_entry)
5521 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5523 restore_path_bar (impl);
5524 path_bar_update (impl);
5526 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5527 * of files may change depending whether we are in a file or folder-only mode.
5529 gtk_widget_queue_draw (impl->browse_files_tree_view);
5531 emit_default_size_changed (impl);
5535 gtk_file_chooser_default_set_property (GObject *object,
5537 const GValue *value,
5541 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5545 case GTK_FILE_CHOOSER_PROP_ACTION:
5547 GtkFileChooserAction action = g_value_get_enum (value);
5549 if (action != impl->action)
5551 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5553 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5554 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5555 && impl->select_multiple)
5557 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5558 "this is not allowed in multiple selection mode. Resetting the file chooser "
5559 "to single selection mode.");
5560 set_select_multiple (impl, FALSE, TRUE);
5562 impl->action = action;
5563 update_cell_renderer_attributes (impl);
5564 update_appearance (impl);
5565 settings_load (impl);
5570 case GTK_FILE_CHOOSER_PROP_FILTER:
5571 set_current_filter (impl, g_value_get_object (value));
5574 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5575 set_local_only (impl, g_value_get_boolean (value));
5578 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5579 set_preview_widget (impl, g_value_get_object (value));
5582 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5583 impl->preview_widget_active = g_value_get_boolean (value);
5584 update_preview_widget_visibility (impl);
5587 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5588 impl->use_preview_label = g_value_get_boolean (value);
5589 update_preview_widget_visibility (impl);
5592 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5593 set_extra_widget (impl, g_value_get_object (value));
5596 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5598 gboolean select_multiple = g_value_get_boolean (value);
5599 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5600 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5603 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5604 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5605 "leaving the file chooser in single selection mode.");
5609 set_select_multiple (impl, select_multiple, FALSE);
5613 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5615 gboolean show_hidden = g_value_get_boolean (value);
5616 if (show_hidden != impl->show_hidden)
5618 impl->show_hidden = show_hidden;
5620 if (impl->browse_files_model)
5621 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5626 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5628 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5629 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5633 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5635 gboolean create_folders = g_value_get_boolean (value);
5636 impl->create_folders = create_folders;
5637 update_appearance (impl);
5642 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5648 gtk_file_chooser_default_get_property (GObject *object,
5653 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5657 case GTK_FILE_CHOOSER_PROP_ACTION:
5658 g_value_set_enum (value, impl->action);
5661 case GTK_FILE_CHOOSER_PROP_FILTER:
5662 g_value_set_object (value, impl->current_filter);
5665 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5666 g_value_set_boolean (value, impl->local_only);
5669 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5670 g_value_set_object (value, impl->preview_widget);
5673 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5674 g_value_set_boolean (value, impl->preview_widget_active);
5677 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5678 g_value_set_boolean (value, impl->use_preview_label);
5681 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5682 g_value_set_object (value, impl->extra_widget);
5685 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5686 g_value_set_boolean (value, impl->select_multiple);
5689 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5690 g_value_set_boolean (value, impl->show_hidden);
5693 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5694 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5697 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5698 g_value_set_boolean (value, impl->create_folders);
5702 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5707 /* This cancels everything that may be going on in the background. */
5709 cancel_all_operations (GtkFileChooserDefault *impl)
5713 pending_select_files_free (impl);
5715 /* cancel all pending operations */
5716 if (impl->pending_cancellables)
5718 for (l = impl->pending_cancellables; l; l = l->next)
5720 GCancellable *cancellable = G_CANCELLABLE (l->data);
5721 g_cancellable_cancel (cancellable);
5723 g_slist_free (impl->pending_cancellables);
5724 impl->pending_cancellables = NULL;
5727 if (impl->reload_icon_cancellables)
5729 for (l = impl->reload_icon_cancellables; l; l = l->next)
5731 GCancellable *cancellable = G_CANCELLABLE (l->data);
5732 g_cancellable_cancel (cancellable);
5734 g_slist_free (impl->reload_icon_cancellables);
5735 impl->reload_icon_cancellables = NULL;
5738 if (impl->loading_shortcuts)
5740 for (l = impl->loading_shortcuts; l; l = l->next)
5742 GCancellable *cancellable = G_CANCELLABLE (l->data);
5743 g_cancellable_cancel (cancellable);
5745 g_slist_free (impl->loading_shortcuts);
5746 impl->loading_shortcuts = NULL;
5749 if (impl->file_list_drag_data_received_cancellable)
5751 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5752 impl->file_list_drag_data_received_cancellable = NULL;
5755 if (impl->update_current_folder_cancellable)
5757 g_cancellable_cancel (impl->update_current_folder_cancellable);
5758 impl->update_current_folder_cancellable = NULL;
5761 if (impl->should_respond_get_info_cancellable)
5763 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5764 impl->should_respond_get_info_cancellable = NULL;
5767 if (impl->update_from_entry_cancellable)
5769 g_cancellable_cancel (impl->update_from_entry_cancellable);
5770 impl->update_from_entry_cancellable = NULL;
5773 if (impl->shortcuts_activate_iter_cancellable)
5775 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5776 impl->shortcuts_activate_iter_cancellable = NULL;
5779 search_stop_searching (impl, TRUE);
5780 recent_stop_loading (impl);
5783 /* Removes the settings signal handler. It's safe to call multiple times */
5785 remove_settings_signal (GtkFileChooserDefault *impl,
5788 if (impl->settings_signal_id)
5790 GtkSettings *settings;
5792 settings = gtk_settings_get_for_screen (screen);
5793 g_signal_handler_disconnect (settings,
5794 impl->settings_signal_id);
5795 impl->settings_signal_id = 0;
5800 gtk_file_chooser_default_dispose (GObject *object)
5802 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5804 cancel_all_operations (impl);
5806 if (impl->extra_widget)
5808 g_object_unref (impl->extra_widget);
5809 impl->extra_widget = NULL;
5812 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5814 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5817 /* We override show-all since we have internal widgets that
5818 * shouldn't be shown when you call show_all(), like the filter
5822 gtk_file_chooser_default_show_all (GtkWidget *widget)
5824 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5826 gtk_widget_show (widget);
5828 if (impl->extra_widget)
5829 gtk_widget_show_all (impl->extra_widget);
5832 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5833 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5836 toplevel_set_focus_cb (GtkWindow *window,
5838 GtkFileChooserDefault *impl)
5840 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5843 /* We monitor the focus widget on our toplevel to be able to know which widget
5844 * was last focused at the time our "should_respond" method gets called.
5847 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5848 GtkWidget *previous_toplevel)
5850 GtkFileChooserDefault *impl;
5851 GtkWidget *toplevel;
5853 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5854 toplevel = gtk_widget_get_toplevel (widget);
5856 if (previous_toplevel &&
5857 impl->toplevel_set_focus_id != 0)
5859 g_signal_handler_disconnect (previous_toplevel,
5860 impl->toplevel_set_focus_id);
5861 impl->toplevel_set_focus_id = 0;
5862 impl->toplevel_last_focus_widget = NULL;
5865 if (gtk_widget_is_toplevel (toplevel))
5867 g_assert (impl->toplevel_set_focus_id == 0);
5868 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5869 G_CALLBACK (toplevel_set_focus_cb), impl);
5870 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5874 /* Changes the icons wherever it is needed */
5876 change_icon_theme (GtkFileChooserDefault *impl)
5878 GtkSettings *settings;
5880 GtkCellRenderer *renderer;
5883 profile_start ("start", NULL);
5885 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5887 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5888 impl->icon_size = MAX (width, height);
5890 impl->icon_size = FALLBACK_ICON_SIZE;
5892 shortcuts_reload_icons (impl);
5893 /* the first cell in the first column is the icon column, and we have a fixed size there */
5894 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5895 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5896 renderer = GTK_CELL_RENDERER (cells->data);
5897 set_icon_cell_renderer_fixed_size (impl, renderer);
5898 g_list_free (cells);
5899 if (impl->browse_files_model)
5900 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5901 gtk_widget_queue_resize (impl->browse_files_tree_view);
5903 profile_end ("end", NULL);
5906 /* Callback used when a GtkSettings value changes */
5908 settings_notify_cb (GObject *object,
5910 GtkFileChooserDefault *impl)
5914 profile_start ("start", NULL);
5916 name = g_param_spec_get_name (pspec);
5918 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5919 strcmp (name, "gtk-icon-sizes") == 0)
5920 change_icon_theme (impl);
5922 profile_end ("end", NULL);
5925 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5929 check_icon_theme (GtkFileChooserDefault *impl)
5931 GtkSettings *settings;
5933 profile_start ("start", NULL);
5935 if (impl->settings_signal_id)
5937 profile_end ("end", NULL);
5941 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5943 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5944 impl->settings_signal_id = g_signal_connect (settings, "notify",
5945 G_CALLBACK (settings_notify_cb), impl);
5947 change_icon_theme (impl);
5950 profile_end ("end", NULL);
5954 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5956 GtkFileChooserDefault *impl;
5958 profile_start ("start", NULL);
5960 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5962 profile_msg (" parent class style_udpated start", NULL);
5963 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5964 profile_msg (" parent class style_updated end", NULL);
5966 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5967 change_icon_theme (impl);
5969 emit_default_size_changed (impl);
5971 profile_end ("end", NULL);
5975 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5976 GdkScreen *previous_screen)
5978 GtkFileChooserDefault *impl;
5980 profile_start ("start", NULL);
5982 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5984 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5985 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5987 remove_settings_signal (impl, previous_screen);
5988 check_icon_theme (impl);
5990 emit_default_size_changed (impl);
5992 profile_end ("end", NULL);
5996 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5997 GtkAllocation *allocation)
5999 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
6003 set_sort_column (GtkFileChooserDefault *impl)
6005 GtkTreeSortable *sortable;
6007 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6009 /* can happen when we're still populating the model */
6010 if (sortable == NULL)
6013 gtk_tree_sortable_set_sort_column_id (sortable,
6019 settings_ensure (GtkFileChooserDefault *impl)
6021 if (impl->settings != NULL)
6024 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
6025 "/org/gtk/settings/file-chooser/");
6026 g_settings_delay (impl->settings);
6030 settings_load (GtkFileChooserDefault *impl)
6032 LocationMode location_mode;
6033 gboolean show_hidden;
6034 gboolean show_size_column;
6036 GtkSortType sort_order;
6038 settings_ensure (impl);
6040 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
6041 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
6042 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
6043 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
6044 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
6046 location_mode_set (impl, location_mode, TRUE);
6048 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6050 impl->show_size_column = show_size_column;
6051 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6053 impl->sort_column = sort_column;
6054 impl->sort_order = sort_order;
6055 /* We don't call set_sort_column() here as the models may not have been
6056 * created yet. The individual functions that create and set the models will
6057 * call set_sort_column() themselves.
6062 save_dialog_geometry (GtkFileChooserDefault *impl)
6064 GtkWindow *toplevel;
6065 int x, y, width, height;
6067 toplevel = get_toplevel (GTK_WIDGET (impl));
6069 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
6072 gtk_window_get_position (toplevel, &x, &y);
6073 gtk_window_get_size (toplevel, &width, &height);
6075 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
6076 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
6080 settings_save (GtkFileChooserDefault *impl)
6082 char *current_folder_uri;
6084 settings_ensure (impl);
6086 /* Current folder */
6088 if (impl->current_folder)
6089 current_folder_uri = g_file_get_uri (impl->current_folder);
6091 current_folder_uri = "";
6093 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
6095 if (impl->current_folder)
6096 g_free (current_folder_uri);
6098 /* All the other state */
6100 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
6101 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
6102 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6103 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
6104 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
6105 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
6107 save_dialog_geometry (impl);
6109 /* Now apply the settings */
6110 g_settings_apply (impl->settings);
6112 g_object_unref (impl->settings);
6113 impl->settings = NULL;
6116 /* GtkWidget::realize method */
6118 gtk_file_chooser_default_realize (GtkWidget *widget)
6120 GtkFileChooserDefault *impl;
6122 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6124 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6126 emit_default_size_changed (impl);
6130 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
6132 char *last_folder_uri;
6135 settings_ensure (impl);
6137 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
6139 /* If no last folder is set, we use the user's home directory, since
6140 * this is the starting point for most documents.
6142 if (last_folder_uri[0] == '\0')
6143 file = g_file_new_for_path (g_get_home_dir ());
6145 file = g_file_new_for_uri (last_folder_uri);
6147 g_free (last_folder_uri);
6152 /* GtkWidget::map method */
6154 gtk_file_chooser_default_map (GtkWidget *widget)
6156 GtkFileChooserDefault *impl;
6158 profile_start ("start", NULL);
6160 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6162 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6164 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6166 switch (impl->reload_state)
6169 recent_shortcut_handler (impl);
6172 case RELOAD_HAS_FOLDER:
6173 /* Nothing; we are already loading or loaded, so we
6174 * don't need to reload
6179 g_assert_not_reached ();
6183 volumes_bookmarks_changed_cb (impl->file_system, impl);
6185 settings_load (impl);
6187 profile_end ("end", NULL);
6190 /* GtkWidget::unmap method */
6192 gtk_file_chooser_default_unmap (GtkWidget *widget)
6194 GtkFileChooserDefault *impl;
6196 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6198 settings_save (impl);
6200 cancel_all_operations (impl);
6201 impl->reload_state = RELOAD_EMPTY;
6203 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6207 install_list_model_filter (GtkFileChooserDefault *impl)
6209 _gtk_file_system_model_set_filter (impl->browse_files_model,
6210 impl->current_filter);
6213 #define COMPARE_DIRECTORIES \
6214 GtkFileChooserDefault *impl = user_data; \
6215 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6216 gboolean dir_a, dir_b; \
6218 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6219 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6221 if (dir_a != dir_b) \
6222 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6224 /* Sort callback for the filename column */
6226 name_sort_func (GtkTreeModel *model,
6231 COMPARE_DIRECTORIES;
6234 const char *key_a, *key_b;
6237 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6238 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6241 result = strcmp (key_a, key_b);
6253 /* Sort callback for the size column */
6255 size_sort_func (GtkTreeModel *model,
6260 COMPARE_DIRECTORIES;
6263 gint64 size_a, size_b;
6265 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6266 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6268 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6272 /* Sort callback for the mtime column */
6274 mtime_sort_func (GtkTreeModel *model,
6279 COMPARE_DIRECTORIES;
6284 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6285 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6287 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6291 /* Callback used when the sort column changes. We cache the sort order for use
6292 * in name_sort_func().
6295 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6296 GtkFileChooserDefault *impl)
6298 gint sort_column_id;
6299 GtkSortType sort_type;
6301 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6303 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6304 impl->sort_column = sort_column_id;
6305 impl->sort_order = sort_type;
6310 set_busy_cursor (GtkFileChooserDefault *impl,
6314 GtkWindow *toplevel;
6315 GdkDisplay *display;
6318 toplevel = get_toplevel (GTK_WIDGET (impl));
6319 widget = GTK_WIDGET (toplevel);
6320 if (!toplevel || !gtk_widget_get_realized (widget))
6323 display = gtk_widget_get_display (widget);
6326 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6330 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6331 gdk_display_flush (display);
6334 g_object_unref (cursor);
6337 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6339 load_set_model (GtkFileChooserDefault *impl)
6341 profile_start ("start", NULL);
6343 g_assert (impl->browse_files_model != NULL);
6345 profile_msg (" gtk_tree_view_set_model start", NULL);
6346 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6347 GTK_TREE_MODEL (impl->browse_files_model));
6348 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6349 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6351 file_list_set_sort_column_ids (impl);
6352 set_sort_column (impl);
6353 profile_msg (" gtk_tree_view_set_model end", NULL);
6354 impl->list_sort_ascending = TRUE;
6356 profile_end ("end", NULL);
6359 /* Timeout callback used when the loading timer expires */
6361 load_timeout_cb (gpointer data)
6363 GtkFileChooserDefault *impl;
6365 profile_start ("start", NULL);
6367 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6368 g_assert (impl->load_state == LOAD_PRELOAD);
6369 g_assert (impl->load_timeout_id != 0);
6370 g_assert (impl->browse_files_model != NULL);
6372 impl->load_timeout_id = 0;
6373 impl->load_state = LOAD_LOADING;
6375 load_set_model (impl);
6377 profile_end ("end", NULL);
6382 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6384 load_setup_timer (GtkFileChooserDefault *impl)
6386 g_assert (impl->load_timeout_id == 0);
6387 g_assert (impl->load_state != LOAD_PRELOAD);
6389 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6390 impl->load_state = LOAD_PRELOAD;
6393 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6395 load_remove_timer (GtkFileChooserDefault *impl)
6397 if (impl->load_timeout_id != 0)
6399 g_assert (impl->load_state == LOAD_PRELOAD);
6401 g_source_remove (impl->load_timeout_id);
6402 impl->load_timeout_id = 0;
6403 impl->load_state = LOAD_EMPTY;
6406 g_assert (impl->load_state == LOAD_EMPTY ||
6407 impl->load_state == LOAD_LOADING ||
6408 impl->load_state == LOAD_FINISHED);
6411 /* Selects the first row in the file list */
6413 browse_files_select_first_row (GtkFileChooserDefault *impl)
6416 GtkTreeIter dummy_iter;
6417 GtkTreeModel *tree_model;
6419 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6424 path = gtk_tree_path_new_from_indices (0, -1);
6426 /* If the list is empty, do nothing. */
6427 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6428 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6430 gtk_tree_path_free (path);
6433 struct center_selected_row_closure {
6434 GtkFileChooserDefault *impl;
6435 gboolean already_centered;
6438 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6439 * selected row in the tree view.
6442 center_selected_row_foreach_cb (GtkTreeModel *model,
6447 struct center_selected_row_closure *closure;
6450 if (closure->already_centered)
6453 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6454 closure->already_centered = TRUE;
6457 /* Centers the selected row in the tree view */
6459 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6461 struct center_selected_row_closure closure;
6462 GtkTreeSelection *selection;
6464 closure.impl = impl;
6465 closure.already_centered = FALSE;
6467 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6468 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6472 show_and_select_files (GtkFileChooserDefault *impl,
6475 GtkTreeSelection *selection;
6476 GtkFileSystemModel *fsmodel;
6477 gboolean enabled_hidden, removed_filters;
6478 gboolean selected_a_file;
6481 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6482 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6484 enabled_hidden = impl->show_hidden;
6485 removed_filters = (impl->current_filter == NULL);
6487 selected_a_file = FALSE;
6489 for (walk = files; walk; walk = walk->next)
6491 GFile *file = walk->data;
6494 /* Is it a hidden file? */
6496 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6499 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6501 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6503 if (!enabled_hidden &&
6504 (g_file_info_get_is_hidden (info) ||
6505 g_file_info_get_is_backup (info)))
6507 g_object_set (impl, "show-hidden", TRUE, NULL);
6508 enabled_hidden = TRUE;
6512 /* Is it a filtered file? */
6514 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6515 continue; /* re-get the iter as it may change when the model refilters */
6517 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6519 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6520 if (!removed_filters)
6522 set_current_filter (impl, NULL);
6523 removed_filters = TRUE;
6527 /* Okay, can we select the file now? */
6529 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6532 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6536 gtk_tree_selection_select_iter (selection, &iter);
6538 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6539 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6541 gtk_tree_path_free (path);
6543 selected_a_file = TRUE;
6547 browse_files_center_selected_row (impl);
6549 return selected_a_file;
6552 /* Processes the pending operation when a folder is finished loading */
6554 pending_select_files_process (GtkFileChooserDefault *impl)
6556 g_assert (impl->load_state == LOAD_FINISHED);
6557 g_assert (impl->browse_files_model != NULL);
6559 if (impl->pending_select_files)
6561 show_and_select_files (impl, impl->pending_select_files);
6562 pending_select_files_free (impl);
6563 browse_files_center_selected_row (impl);
6567 /* We only select the first row if the chooser is actually mapped ---
6568 * selecting the first row is to help the user when he is interacting with
6569 * the chooser, but sometimes a chooser works not on behalf of the user,
6570 * but rather on behalf of something else like GtkFileChooserButton. In
6571 * that case, the chooser's selection should be what the caller expects,
6572 * as the user can't see that something else got selected. See bug #165264.
6574 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6575 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6576 browse_files_select_first_row (impl);
6579 g_assert (impl->pending_select_files == NULL);
6583 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6588 info = g_file_query_info (impl->current_folder,
6589 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6590 G_FILE_QUERY_INFO_NONE,
6595 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6596 g_object_unref (info);
6599 msg = g_strdup (_("Could not read the contents of the folder"));
6601 error_message (impl, msg, error->message);
6605 /* Callback used when the file system model finishes loading */
6607 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6609 GtkFileChooserDefault *impl)
6611 profile_start ("start", NULL);
6614 show_error_on_reading_current_folder (impl, error);
6616 if (impl->load_state == LOAD_PRELOAD)
6618 load_remove_timer (impl);
6619 load_set_model (impl);
6621 else if (impl->load_state == LOAD_LOADING)
6627 /* We can't g_assert_not_reached(), as something other than us may have
6628 * initiated a folder reload. See #165556.
6630 profile_end ("end", NULL);
6634 g_assert (impl->load_timeout_id == 0);
6636 impl->load_state = LOAD_FINISHED;
6638 pending_select_files_process (impl);
6639 set_busy_cursor (impl, FALSE);
6640 #ifdef PROFILE_FILE_CHOOSER
6641 access ("MARK: *** FINISHED LOADING", F_OK);
6644 profile_end ("end", NULL);
6648 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6649 gboolean remove_from_treeview)
6651 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6653 if (impl->browse_files_model)
6655 g_object_unref (impl->browse_files_model);
6656 impl->browse_files_model = NULL;
6659 if (remove_from_treeview)
6660 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6664 my_g_format_time_for_display (glong secs)
6669 time_t time_mtime, time_now;
6670 const gchar *format;
6671 gchar *locale_format = NULL;
6673 char *date_str = NULL;
6675 const char *locale, *dot = NULL;
6676 gint64 codepage = -1;
6682 #ifdef HAVE_LOCALTIME_R
6683 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6686 struct tm *ptm = localtime ((time_t *) &time_mtime);
6690 g_warning ("ptm != NULL failed");
6692 return g_strdup (_("Unknown"));
6695 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6697 #endif /* HAVE_LOCALTIME_R */
6699 g_date_set_time_t (&mtime, time_mtime);
6700 time_now = time (NULL);
6701 g_date_set_time_t (&now, time_now);
6703 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6705 /* Translators: %H means "hours" and %M means "minutes" */
6707 format = _("%H:%M");
6708 else if (days_diff == 1)
6709 format = _("Yesterday at %H:%M");
6712 if (days_diff > 1 && days_diff < 7)
6713 format = "%A"; /* Days from last week */
6715 format = "%x"; /* Any other date */
6719 /* g_locale_from_utf8() returns a string in the system
6720 * code-page, which is not always the same as that used by the C
6721 * library. For instance when running a GTK+ program with
6722 * LANG=ko on an English version of Windows, the system
6723 * code-page is 1252, but the code-page used by the C library is
6724 * 949. (It's GTK+ itself that sets the C library locale when it
6725 * notices the LANG environment variable. See gtkmain.c The
6726 * Microsoft C library doesn't look at any locale environment
6727 * variables.) We need to pass strftime() a string in the C
6728 * library's code-page. See bug #509885.
6730 locale = setlocale (LC_ALL, NULL);
6732 dot = strchr (locale, '.');
6735 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6737 /* All codepages should fit in 16 bits AFAIK */
6738 if (codepage > 0 && codepage < 65536)
6740 sprintf (charset, "CP%u", (guint) codepage);
6741 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6745 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6747 if (locale_format != NULL &&
6748 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6751 /* As above but in opposite direction... */
6752 if (codepage > 0 && codepage < 65536)
6753 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6755 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6759 if (date_str == NULL)
6760 date_str = g_strdup (_("Unknown"));
6762 g_free (locale_format);
6767 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6769 GFileAttributeType type;
6772 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6773 g_file_info_set_attribute (to, attribute, type, value);
6777 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6779 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6780 GFile *file = G_FILE (object);
6781 GFileInfo *queried, *info;
6784 queried = g_file_query_info_finish (file, res, NULL);
6785 if (queried == NULL)
6788 GDK_THREADS_ENTER ();
6790 /* now we know model is valid */
6792 /* file was deleted */
6793 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6795 GDK_THREADS_LEAVE ();
6799 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6801 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6802 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6803 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6805 _gtk_file_system_model_update_file (model, file, info, FALSE);
6807 g_object_unref (info);
6809 GDK_THREADS_LEAVE ();
6813 file_system_model_set (GtkFileSystemModel *model,
6820 GtkFileChooserDefault *impl = data;
6824 case MODEL_COL_FILE:
6825 g_value_set_object (value, file);
6827 case MODEL_COL_NAME:
6829 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6831 g_value_set_string (value, g_file_info_get_display_name (info));
6833 case MODEL_COL_NAME_COLLATED:
6835 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6837 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6839 case MODEL_COL_IS_FOLDER:
6840 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6842 case MODEL_COL_PIXBUF:
6845 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6847 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6851 GtkTreeModel *tree_model;
6852 GtkTreePath *path, *start, *end;
6855 if (impl->browse_files_tree_view == NULL ||
6856 g_file_info_has_attribute (info, "filechooser::queried"))
6859 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6860 if (tree_model != GTK_TREE_MODEL (model))
6863 if (!_gtk_file_system_model_get_iter_for_file (model,
6866 g_assert_not_reached ();
6867 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6869 path = gtk_tree_model_get_path (tree_model, &iter);
6870 if (gtk_tree_path_compare (start, path) != 1 &&
6871 gtk_tree_path_compare (path, end) != 1)
6873 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6874 g_file_query_info_async (file,
6875 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6876 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6877 G_FILE_ATTRIBUTE_STANDARD_ICON,
6878 G_FILE_QUERY_INFO_NONE,
6880 _gtk_file_system_model_get_cancellable (model),
6881 file_system_model_got_thumbnail,
6884 gtk_tree_path_free (path);
6885 gtk_tree_path_free (start);
6886 gtk_tree_path_free (end);
6891 g_value_set_object (value, NULL);
6893 case MODEL_COL_SIZE:
6894 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6896 case MODEL_COL_SIZE_TEXT:
6897 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6898 g_value_set_string (value, NULL);
6900 g_value_take_string (value, g_format_size (g_file_info_get_size (info)));
6902 case MODEL_COL_MTIME:
6903 case MODEL_COL_MTIME_TEXT:
6908 g_file_info_get_modification_time (info, &tv);
6909 if (column == MODEL_COL_MTIME)
6910 g_value_set_long (value, tv.tv_sec);
6911 else if (tv.tv_sec == 0)
6912 g_value_set_static_string (value, _("Unknown"));
6914 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6917 case MODEL_COL_ELLIPSIZE:
6918 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6921 g_assert_not_reached ();
6928 /* Gets rid of the old list model and creates a new one for the current folder */
6930 set_list_model (GtkFileChooserDefault *impl,
6933 g_assert (impl->current_folder != NULL);
6935 profile_start ("start", NULL);
6937 stop_loading_and_clear_list_model (impl, TRUE);
6939 set_busy_cursor (impl, TRUE);
6941 impl->browse_files_model =
6942 _gtk_file_system_model_new_for_directory (impl->current_folder,
6944 file_system_model_set,
6946 MODEL_COLUMN_TYPES);
6948 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6950 profile_msg (" set sort function", NULL);
6951 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6952 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6953 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6954 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6955 set_sort_column (impl);
6956 impl->list_sort_ascending = TRUE;
6957 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6958 G_CALLBACK (list_sort_column_changed_cb), impl);
6960 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6962 g_signal_connect (impl->browse_files_model, "finished-loading",
6963 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6965 install_list_model_filter (impl);
6967 profile_end ("end", NULL);
6972 struct update_chooser_entry_selected_foreach_closure {
6974 GtkTreeIter first_selected_iter;
6978 compare_utf8_filenames (const gchar *a,
6981 gchar *a_folded, *b_folded;
6984 a_folded = g_utf8_strdown (a, -1);
6985 b_folded = g_utf8_strdown (b, -1);
6987 retval = strcmp (a_folded, b_folded);
6996 update_chooser_entry_selected_foreach (GtkTreeModel *model,
7001 struct update_chooser_entry_selected_foreach_closure *closure;
7004 closure->num_selected++;
7006 if (closure->num_selected == 1)
7007 closure->first_selected_iter = *iter;
7011 update_chooser_entry (GtkFileChooserDefault *impl)
7013 GtkTreeSelection *selection;
7014 struct update_chooser_entry_selected_foreach_closure closure;
7016 /* no need to update the file chooser's entry if there's no entry */
7017 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7018 !impl->location_entry)
7021 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7022 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7023 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7024 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7025 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
7028 g_assert (impl->location_entry != NULL);
7030 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7031 closure.num_selected = 0;
7032 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
7034 if (closure.num_selected == 0)
7036 if (impl->operation_mode == OPERATION_MODE_RECENT)
7037 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
7039 goto maybe_clear_entry;
7041 else if (closure.num_selected == 1)
7043 if (impl->operation_mode == OPERATION_MODE_BROWSE)
7046 gboolean change_entry;
7048 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
7050 /* If the cursor moved to the row of the newly created folder,
7051 * retrieving info will return NULL.
7056 g_free (impl->browse_files_last_selected_name);
7057 impl->browse_files_last_selected_name =
7058 g_strdup (g_file_info_get_display_name (info));
7060 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7061 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7062 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7064 /* Don't change the name when clicking on a folder... */
7065 change_entry = (! _gtk_file_info_consider_as_directory (info));
7068 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7072 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7074 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7075 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7080 else if (impl->operation_mode == OPERATION_MODE_RECENT
7081 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7085 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
7087 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
7088 MODEL_COL_FILE, &folder,
7090 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
7091 g_object_unref (folder);
7097 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7098 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7100 /* Multiple selection, so just clear the entry. */
7101 g_free (impl->browse_files_last_selected_name);
7102 impl->browse_files_last_selected_name = NULL;
7104 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7110 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7111 && impl->browse_files_last_selected_name)
7113 const char *entry_text;
7115 gboolean clear_entry;
7117 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7118 len = strlen (entry_text);
7121 /* The file chooser entry may have appended a "/" to its text.
7122 * So take it out, and compare the result to the old selection.
7124 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7128 tmp = g_strndup (entry_text, len - 1);
7129 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7133 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7136 clear_entry = FALSE;
7139 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7144 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7148 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7152 struct UpdateCurrentFolderData
7154 GtkFileChooserDefault *impl;
7156 gboolean keep_trail;
7157 gboolean clear_entry;
7158 GFile *original_file;
7159 GError *original_error;
7163 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7164 GtkFileSystemVolume *volume,
7165 const GError *error,
7168 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7169 struct UpdateCurrentFolderData *data = user_data;
7170 GtkFileChooserDefault *impl = data->impl;
7172 if (cancellable != impl->update_current_folder_cancellable)
7175 impl->update_current_folder_cancellable = NULL;
7176 set_busy_cursor (impl, FALSE);
7183 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7184 impl->reload_state = RELOAD_EMPTY;
7188 change_folder_and_display_error (impl, data->file, data->clear_entry);
7191 g_object_unref (data->file);
7194 g_object_unref (cancellable);
7198 update_current_folder_get_info_cb (GCancellable *cancellable,
7200 const GError *error,
7203 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7204 struct UpdateCurrentFolderData *data = user_data;
7205 GtkFileChooserDefault *impl = data->impl;
7207 if (cancellable != impl->update_current_folder_cancellable)
7210 impl->update_current_folder_cancellable = NULL;
7211 impl->reload_state = RELOAD_EMPTY;
7213 set_busy_cursor (impl, FALSE);
7222 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7224 GMountOperation *mount_operation;
7225 GtkWidget *toplevel;
7227 g_object_unref (cancellable);
7228 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7230 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7232 set_busy_cursor (impl, TRUE);
7234 impl->update_current_folder_cancellable =
7235 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7237 update_current_folder_mount_enclosing_volume_cb,
7243 if (!data->original_file)
7245 data->original_file = g_object_ref (data->file);
7246 data->original_error = g_error_copy (error);
7249 parent_file = g_file_get_parent (data->file);
7251 /* get parent path and try to change the folder to that */
7254 g_object_unref (data->file);
7255 data->file = parent_file;
7257 g_object_unref (cancellable);
7259 /* restart the update current folder operation */
7260 impl->reload_state = RELOAD_HAS_FOLDER;
7262 impl->update_current_folder_cancellable =
7263 _gtk_file_system_get_info (impl->file_system, data->file,
7265 update_current_folder_get_info_cb,
7268 set_busy_cursor (impl, TRUE);
7274 /* Error and bail out, ignoring "not found" errors since they're useless:
7275 * they only happen when a program defaults to a folder that has been (re)moved.
7277 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7278 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7280 g_error_free (data->original_error);
7282 g_object_unref (data->original_file);
7288 if (data->original_file)
7290 /* Error and bail out, ignoring "not found" errors since they're useless:
7291 * they only happen when a program defaults to a folder that has been (re)moved.
7293 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7294 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7296 g_error_free (data->original_error);
7298 g_object_unref (data->original_file);
7301 if (! _gtk_file_info_consider_as_directory (info))
7304 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail);
7306 if (impl->current_folder != data->file)
7308 if (impl->current_folder)
7309 g_object_unref (impl->current_folder);
7311 impl->current_folder = g_object_ref (data->file);
7314 impl->reload_state = RELOAD_HAS_FOLDER;
7316 /* Update the widgets that may trigger a folder change themselves. */
7318 if (!impl->changing_folder)
7320 impl->changing_folder = TRUE;
7322 shortcuts_update_current_folder (impl);
7324 impl->changing_folder = FALSE;
7327 /* Set the folder on the save entry */
7329 if (impl->location_entry)
7331 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7332 impl->current_folder);
7334 if (data->clear_entry)
7335 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
7338 /* Create a new list model. This is slightly evil; we store the result value
7339 * but perform more actions rather than returning immediately even if it
7340 * generates an error.
7342 set_list_model (impl, NULL);
7344 /* Refresh controls */
7346 shortcuts_find_current_folder (impl);
7348 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7350 check_preview_change (impl);
7351 bookmarks_check_add_sensitivity (impl);
7353 g_signal_emit_by_name (impl, "selection-changed", 0);
7356 g_object_unref (data->file);
7359 g_object_unref (cancellable);
7363 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7365 gboolean keep_trail,
7366 gboolean clear_entry,
7369 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7370 struct UpdateCurrentFolderData *data;
7372 profile_start ("start", NULL);
7374 g_object_ref (file);
7376 operation_mode_set (impl, OPERATION_MODE_BROWSE);
7378 if (impl->local_only && !g_file_is_native (file))
7380 g_set_error_literal (error,
7381 GTK_FILE_CHOOSER_ERROR,
7382 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7383 _("Cannot change to folder because it is not local"));
7385 g_object_unref (file);
7386 profile_end ("end - not local", NULL);
7390 if (impl->update_current_folder_cancellable)
7391 g_cancellable_cancel (impl->update_current_folder_cancellable);
7393 /* Test validity of path here. */
7394 data = g_new0 (struct UpdateCurrentFolderData, 1);
7396 data->file = g_object_ref (file);
7397 data->keep_trail = keep_trail;
7398 data->clear_entry = clear_entry;
7400 impl->reload_state = RELOAD_HAS_FOLDER;
7402 impl->update_current_folder_cancellable =
7403 _gtk_file_system_get_info (impl->file_system, file,
7405 update_current_folder_get_info_cb,
7408 set_busy_cursor (impl, TRUE);
7409 g_object_unref (file);
7411 profile_end ("end", NULL);
7416 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7418 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7420 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7421 impl->operation_mode == OPERATION_MODE_RECENT)
7424 if (impl->reload_state == RELOAD_EMPTY)
7426 /* We are unmapped, or we had an error while loading the last folder.
7427 * We'll return the folder used by the last invocation of the file chooser
7428 * since once we get (re)mapped, we'll load *that* folder anyway unless
7429 * the caller explicitly calls set_current_folder() on us.
7431 return get_file_for_last_folder_opened (impl);
7434 if (impl->current_folder)
7435 return g_object_ref (impl->current_folder);
7441 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7444 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7446 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7447 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7449 pending_select_files_free (impl);
7450 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), name);
7454 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7458 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7462 parent_file = g_file_get_parent (file);
7465 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7467 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7468 impl->operation_mode == OPERATION_MODE_RECENT ||
7469 impl->load_state == LOAD_EMPTY)
7475 g_assert (impl->current_folder != NULL);
7477 same_path = g_file_equal (parent_file, impl->current_folder);
7480 if (same_path && impl->load_state == LOAD_FINISHED)
7485 files.data = (gpointer) file;
7488 result = show_and_select_files (impl, &files);
7489 g_object_unref (parent_file);
7493 pending_select_files_add (impl, file);
7499 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7500 g_object_unref (parent_file);
7504 g_object_unref (parent_file);
7509 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7512 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7513 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7516 if (!impl->browse_files_model)
7519 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7524 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7529 maybe_select (GtkTreeModel *model,
7534 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7535 GtkTreeSelection *selection;
7538 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7540 gtk_tree_model_get (model, iter,
7541 MODEL_COL_IS_FOLDER, &is_folder,
7544 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7545 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7546 gtk_tree_selection_select_iter (selection, iter);
7548 gtk_tree_selection_unselect_iter (selection, iter);
7554 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7556 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7558 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7559 impl->operation_mode == OPERATION_MODE_RECENT)
7561 GtkTreeSelection *selection;
7563 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7564 gtk_tree_selection_select_all (selection);
7568 if (impl->select_multiple)
7569 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7570 maybe_select, impl);
7574 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7576 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7577 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7579 gtk_tree_selection_unselect_all (selection);
7580 pending_select_files_free (impl);
7583 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7585 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7587 * is_empty_ret - whether the file entry is totally empty
7589 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7590 * the path will be "$cwd/foobar")
7593 check_save_entry (GtkFileChooserDefault *impl,
7595 gboolean *is_well_formed_ret,
7596 gboolean *is_empty_ret,
7597 gboolean *is_file_part_empty_ret,
7598 gboolean *is_folder)
7600 GtkFileChooserEntry *chooser_entry;
7601 GFile *current_folder;
7602 const char *file_part;
7606 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7607 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7608 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7609 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7610 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7612 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7614 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7617 *is_well_formed_ret = TRUE;
7618 *is_empty_ret = TRUE;
7619 *is_file_part_empty_ret = TRUE;
7625 *is_empty_ret = FALSE;
7627 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7628 if (!current_folder)
7631 *is_well_formed_ret = FALSE;
7632 *is_file_part_empty_ret = FALSE;
7638 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7640 if (!file_part || file_part[0] == '\0')
7642 *file_ret = current_folder;
7643 *is_well_formed_ret = TRUE;
7644 *is_file_part_empty_ret = TRUE;
7650 *is_file_part_empty_ret = FALSE;
7653 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7654 g_object_unref (current_folder);
7658 error_building_filename_dialog (impl, error);
7660 *is_well_formed_ret = FALSE;
7667 *is_well_formed_ret = TRUE;
7668 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7671 struct get_files_closure {
7672 GtkFileChooserDefault *impl;
7674 GFile *file_from_entry;
7678 get_files_foreach (GtkTreeModel *model,
7683 struct get_files_closure *info;
7685 GtkFileSystemModel *fs_model;
7688 fs_model = info->impl->browse_files_model;
7690 file = _gtk_file_system_model_get_file (fs_model, iter);
7692 return; /* We are on the editable row */
7694 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7695 info->result = g_slist_prepend (info->result, g_object_ref (file));
7699 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7701 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7702 struct get_files_closure info;
7703 GtkWindow *toplevel;
7704 GtkWidget *current_focus;
7705 gboolean file_list_seen;
7709 info.file_from_entry = NULL;
7711 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7712 return search_get_selected_files (impl);
7714 if (impl->operation_mode == OPERATION_MODE_RECENT)
7716 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7718 file_list_seen = TRUE;
7722 return recent_get_selected_files (impl);
7725 toplevel = get_toplevel (GTK_WIDGET (impl));
7727 current_focus = gtk_window_get_focus (toplevel);
7729 current_focus = NULL;
7731 file_list_seen = FALSE;
7732 if (current_focus == impl->browse_files_tree_view)
7734 GtkTreeSelection *selection;
7738 file_list_seen = TRUE;
7739 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7740 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7742 /* If there is no selection in the file list, we probably have this situation:
7744 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7745 * 2. He then double-clicked on a folder ("bar") in the file list
7747 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7748 * filename entry to see if that is the case.
7750 if (info.result == NULL && impl->location_entry)
7753 else if (impl->location_entry && current_focus == impl->location_entry)
7755 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7759 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7764 if (!is_well_formed)
7767 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7769 g_object_unref (info.file_from_entry);
7773 if (info.file_from_entry)
7774 info.result = g_slist_prepend (info.result, info.file_from_entry);
7775 else if (!file_list_seen)
7780 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7782 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7786 /* The focus is on a dialog's action area button or something else */
7787 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7788 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7796 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7797 * fall back to the current directory */
7798 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7799 info.result == NULL)
7801 GFile *current_folder;
7803 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7806 info.result = g_slist_prepend (info.result, current_folder);
7809 return g_slist_reverse (info.result);
7813 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7815 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7817 if (impl->preview_file)
7818 return g_object_ref (impl->preview_file);
7823 static GtkFileSystem *
7824 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7826 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7828 return impl->file_system;
7831 /* Shows or hides the filter widgets */
7833 show_filters (GtkFileChooserDefault *impl,
7837 gtk_widget_show (impl->filter_combo_hbox);
7839 gtk_widget_hide (impl->filter_combo_hbox);
7843 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7844 GtkFileFilter *filter)
7846 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7849 if (g_slist_find (impl->filters, filter))
7851 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7855 g_object_ref_sink (filter);
7856 impl->filters = g_slist_append (impl->filters, filter);
7858 name = gtk_file_filter_get_name (filter);
7860 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7862 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7864 if (!g_slist_find (impl->filters, impl->current_filter))
7865 set_current_filter (impl, filter);
7867 show_filters (impl, TRUE);
7871 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7872 GtkFileFilter *filter)
7874 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7875 GtkTreeModel *model;
7879 filter_index = g_slist_index (impl->filters, filter);
7881 if (filter_index < 0)
7883 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7887 impl->filters = g_slist_remove (impl->filters, filter);
7889 if (filter == impl->current_filter)
7892 set_current_filter (impl, impl->filters->data);
7894 set_current_filter (impl, NULL);
7897 /* Remove row from the combo box */
7898 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7899 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7900 g_assert_not_reached ();
7902 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7904 g_object_unref (filter);
7907 show_filters (impl, FALSE);
7911 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7913 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7915 return g_slist_copy (impl->filters);
7918 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7920 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7923 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7926 struct AddShortcutData
7928 GtkFileChooserDefault *impl;
7933 add_shortcut_get_info_cb (GCancellable *cancellable,
7935 const GError *error,
7939 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7940 struct AddShortcutData *data = user_data;
7942 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7945 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7947 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7950 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7952 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7955 g_object_unref (data->impl);
7956 g_object_unref (data->file);
7959 g_object_unref (cancellable);
7963 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7967 GCancellable *cancellable;
7968 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7969 struct AddShortcutData *data;
7973 /* Avoid adding duplicates */
7974 pos = shortcut_find_position (impl, file);
7975 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7979 uri = g_file_get_uri (file);
7980 /* translators, "Shortcut" means "Bookmark" here */
7982 GTK_FILE_CHOOSER_ERROR,
7983 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7984 _("Shortcut %s already exists"),
7991 for (l = impl->loading_shortcuts; l; l = l->next)
7993 GCancellable *c = l->data;
7996 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7997 if (f && g_file_equal (file, f))
8001 uri = g_file_get_uri (file);
8003 GTK_FILE_CHOOSER_ERROR,
8004 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
8005 _("Shortcut %s already exists"),
8013 data = g_new0 (struct AddShortcutData, 1);
8014 data->impl = g_object_ref (impl);
8015 data->file = g_object_ref (file);
8017 cancellable = _gtk_file_system_get_info (impl->file_system, file,
8019 add_shortcut_get_info_cb, data);
8024 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
8025 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
8031 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
8035 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8042 for (l = impl->loading_shortcuts; l; l = l->next)
8044 GCancellable *c = l->data;
8047 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8048 if (f && g_file_equal (file, f))
8050 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8051 g_cancellable_cancel (c);
8056 if (impl->num_shortcuts == 0)
8059 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8060 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8061 g_assert_not_reached ();
8063 for (i = 0; i < impl->num_shortcuts; i++)
8066 ShortcutType shortcut_type;
8069 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8070 SHORTCUTS_COL_DATA, &col_data,
8071 SHORTCUTS_COL_TYPE, &shortcut_type,
8073 g_assert (col_data != NULL);
8074 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8076 shortcut = col_data;
8077 if (g_file_equal (shortcut, file))
8079 shortcuts_remove_rows (impl, pos + i, 1);
8080 impl->num_shortcuts--;
8084 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8085 g_assert_not_reached ();
8090 uri = g_file_get_uri (file);
8091 /* translators, "Shortcut" means "Bookmark" here */
8093 GTK_FILE_CHOOSER_ERROR,
8094 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8095 _("Shortcut %s does not exist"),
8103 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8105 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8111 if (impl->num_shortcuts == 0)
8114 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8115 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8116 g_assert_not_reached ();
8120 for (i = 0; i < impl->num_shortcuts; i++)
8123 ShortcutType shortcut_type;
8126 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8127 SHORTCUTS_COL_DATA, &col_data,
8128 SHORTCUTS_COL_TYPE, &shortcut_type,
8130 g_assert (col_data != NULL);
8131 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8133 shortcut = col_data;
8134 list = g_slist_prepend (list, g_object_ref (shortcut));
8136 if (i != impl->num_shortcuts - 1)
8138 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8139 g_assert_not_reached ();
8143 return g_slist_reverse (list);
8146 /* Guesses a size based upon font sizes */
8148 find_good_size_from_style (GtkWidget *widget,
8152 GtkStyleContext *context;
8153 GtkStateFlags state;
8158 context = gtk_widget_get_style_context (widget);
8159 state = gtk_widget_get_state_flags (widget);
8161 screen = gtk_widget_get_screen (widget);
8164 resolution = gdk_screen_get_resolution (screen);
8165 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8169 resolution = 96.0; /* wheeee */
8171 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
8172 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
8174 *width = font_size * NUM_CHARS;
8175 *height = font_size * NUM_LINES;
8179 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8180 gint *default_width,
8181 gint *default_height)
8183 GtkFileChooserDefault *impl;
8185 int x, y, width, height;
8187 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8189 settings_ensure (impl);
8191 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
8192 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
8194 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8196 *default_width = width;
8197 *default_height = height;
8201 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8203 if (impl->preview_widget_active &&
8204 impl->preview_widget &&
8205 gtk_widget_get_visible (impl->preview_widget))
8207 gtk_widget_get_preferred_size (impl->preview_box,
8209 *default_width += PREVIEW_HBOX_SPACING + req.width;
8212 if (impl->extra_widget &&
8213 gtk_widget_get_visible (impl->extra_widget))
8215 gtk_widget_get_preferred_size (impl->extra_align,
8217 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8221 struct switch_folder_closure {
8222 GtkFileChooserDefault *impl;
8227 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8229 switch_folder_foreach_cb (GtkTreeModel *model,
8234 struct switch_folder_closure *closure;
8238 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8239 closure->num_selected++;
8242 /* Changes to the selected folder in the list view */
8244 switch_to_selected_folder (GtkFileChooserDefault *impl)
8246 GtkTreeSelection *selection;
8247 struct switch_folder_closure closure;
8249 /* We do this with foreach() rather than get_selected() as we may be in
8250 * multiple selection mode
8253 closure.impl = impl;
8254 closure.file = NULL;
8255 closure.num_selected = 0;
8257 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8258 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8260 g_assert (closure.file && closure.num_selected == 1);
8262 change_folder_and_display_error (impl, closure.file, FALSE);
8265 /* Gets the GFileInfo for the selected row in the file list; assumes single
8269 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8270 gboolean *had_selection)
8272 GtkTreeSelection *selection;
8276 g_assert (!impl->select_multiple);
8277 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8278 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8280 *had_selection = FALSE;
8284 *had_selection = TRUE;
8286 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8290 /* Gets the display name of the selected file in the file list; assumes single
8291 * selection mode and that something is selected.
8293 static const gchar *
8294 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8297 gboolean had_selection;
8299 info = get_selected_file_info_from_file_list (impl, &had_selection);
8300 g_assert (had_selection);
8301 g_assert (info != NULL);
8303 return g_file_info_get_display_name (info);
8307 add_custom_button_to_dialog (GtkDialog *dialog,
8308 const gchar *mnemonic_label,
8309 const gchar *stock_id,
8314 button = gtk_button_new_with_mnemonic (mnemonic_label);
8315 gtk_widget_set_can_default (button, TRUE);
8316 gtk_button_set_image (GTK_BUTTON (button),
8317 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8318 gtk_widget_show (button);
8320 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8323 /* Presents an overwrite confirmation dialog; returns whether we should accept
8327 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8328 const gchar *file_part,
8329 const gchar *folder_display_name)
8331 GtkWindow *toplevel;
8335 toplevel = get_toplevel (GTK_WIDGET (impl));
8337 dialog = gtk_message_dialog_new (toplevel,
8338 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8339 GTK_MESSAGE_QUESTION,
8341 _("A file named \"%s\" already exists. Do you want to replace it?"),
8343 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8344 _("The file already exists in \"%s\". Replacing it will "
8345 "overwrite its contents."),
8346 folder_display_name);
8348 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8349 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8350 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8351 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8352 GTK_RESPONSE_ACCEPT,
8353 GTK_RESPONSE_CANCEL,
8355 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8357 if (gtk_window_has_group (toplevel))
8358 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8359 GTK_WINDOW (dialog));
8361 response = gtk_dialog_run (GTK_DIALOG (dialog));
8363 gtk_widget_destroy (dialog);
8365 return (response == GTK_RESPONSE_ACCEPT);
8368 struct GetDisplayNameData
8370 GtkFileChooserDefault *impl;
8374 /* Every time we request a response explicitly, we need to save the selection to the recently-used list,
8375 * as requesting a response means, "the dialog is confirmed".
8378 request_response_and_add_to_recent_list (GtkFileChooserDefault *impl)
8380 g_signal_emit_by_name (impl, "response-requested");
8381 add_selection_to_recent_list (impl);
8385 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8387 const GError *error,
8390 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8391 gboolean should_respond = FALSE;
8392 struct GetDisplayNameData *data = user_data;
8394 if (cancellable != data->impl->should_respond_get_info_cancellable)
8397 data->impl->should_respond_get_info_cancellable = NULL;
8403 /* Huh? Did the folder disappear? Let the caller deal with it */
8404 should_respond = TRUE;
8406 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8408 set_busy_cursor (data->impl, FALSE);
8410 request_response_and_add_to_recent_list (data->impl);
8413 g_object_unref (data->impl);
8414 g_free (data->file_part);
8417 g_object_unref (cancellable);
8420 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8421 * should respond. Can get the file part from the file list or the save entry.
8424 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8425 const gchar *file_part,
8428 GtkFileChooserConfirmation conf;
8430 if (!impl->do_overwrite_confirmation)
8433 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8435 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8439 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8441 struct GetDisplayNameData *data;
8443 g_assert (file_part != NULL);
8445 data = g_new0 (struct GetDisplayNameData, 1);
8446 data->impl = g_object_ref (impl);
8447 data->file_part = g_strdup (file_part);
8449 if (impl->should_respond_get_info_cancellable)
8450 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8452 impl->should_respond_get_info_cancellable =
8453 _gtk_file_system_get_info (impl->file_system, parent_file,
8454 "standard::display-name",
8455 confirmation_confirm_get_info_cb,
8457 set_busy_cursor (data->impl, TRUE);
8461 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8464 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8468 g_assert_not_reached ();
8473 struct FileExistsData
8475 GtkFileChooserDefault *impl;
8476 gboolean file_exists_and_is_not_folder;
8482 name_entry_get_parent_info_cb (GCancellable *cancellable,
8484 const GError *error,
8487 gboolean parent_is_folder;
8488 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8489 struct FileExistsData *data = user_data;
8491 if (cancellable != data->impl->should_respond_get_info_cancellable)
8494 data->impl->should_respond_get_info_cancellable = NULL;
8496 set_busy_cursor (data->impl, FALSE);
8502 parent_is_folder = FALSE;
8504 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8506 if (parent_is_folder)
8508 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8510 request_response_and_add_to_recent_list (data->impl); /* even if the file doesn't exist, apps can make good use of that (e.g. Emacs) */
8512 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8514 if (data->file_exists_and_is_not_folder)
8519 /* Dup the string because the string may be modified
8520 * depending on what clients do in the confirm-overwrite
8521 * signal and this corrupts the pointer
8523 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8524 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8528 request_response_and_add_to_recent_list (data->impl);
8531 request_response_and_add_to_recent_list (data->impl);
8533 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8534 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8536 GError *mkdir_error = NULL;
8538 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8539 * "/blah/nonexistent" you *will* want a folder created.
8542 set_busy_cursor (data->impl, TRUE);
8543 g_file_make_directory (data->file, NULL, &mkdir_error);
8544 set_busy_cursor (data->impl, FALSE);
8547 request_response_and_add_to_recent_list (data->impl);
8549 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8552 g_assert_not_reached ();
8558 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8559 error_with_file_under_nonfolder (data->impl, data->parent_file);
8565 /* The parent folder is not readable for some reason */
8567 error_copy = g_error_copy (error);
8568 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8573 g_object_unref (data->impl);
8574 g_object_unref (data->file);
8575 g_object_unref (data->parent_file);
8578 g_object_unref (cancellable);
8582 file_exists_get_info_cb (GCancellable *cancellable,
8584 const GError *error,
8587 gboolean data_ownership_taken = FALSE;
8588 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8589 gboolean file_exists;
8591 gboolean needs_parent_check = FALSE;
8592 struct FileExistsData *data = user_data;
8594 if (cancellable != data->impl->file_exists_get_info_cancellable)
8597 data->impl->file_exists_get_info_cancellable = NULL;
8599 set_busy_cursor (data->impl, FALSE);
8604 file_exists = (info != NULL);
8605 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8607 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8610 change_folder_and_display_error (data->impl, data->file, TRUE);
8614 request_response_and_add_to_recent_list (data->impl); /* user typed an existing filename; we are done */
8616 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8619 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8621 if (file_exists && !is_folder)
8623 /* Oops, the user typed the name of an existing path which is not
8626 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8627 g_error_copy (error));
8631 needs_parent_check = TRUE;
8634 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8638 needs_parent_check = TRUE;
8644 /* User typed a folder; we are done */
8645 request_response_and_add_to_recent_list (data->impl);
8648 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8651 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8654 change_folder_and_display_error (data->impl, data->file, TRUE);
8656 needs_parent_check = TRUE;
8660 g_assert_not_reached();
8663 if (needs_parent_check) {
8664 /* check that everything up to the last path component exists (i.e. the parent) */
8666 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8667 data_ownership_taken = TRUE;
8669 if (data->impl->should_respond_get_info_cancellable)
8670 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8672 data->impl->should_respond_get_info_cancellable =
8673 _gtk_file_system_get_info (data->impl->file_system,
8676 name_entry_get_parent_info_cb,
8678 set_busy_cursor (data->impl, TRUE);
8682 if (!data_ownership_taken)
8684 g_object_unref (data->impl);
8685 g_object_unref (data->file);
8686 g_object_unref (data->parent_file);
8690 g_object_unref (cancellable);
8694 paste_text_received (GtkClipboard *clipboard,
8696 GtkFileChooserDefault *impl)
8703 file = g_file_new_for_uri (text);
8705 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8706 location_popup_handler (impl, text);
8708 g_object_unref (file);
8711 /* Handler for the "location-popup-on-paste" keybinding signal */
8713 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8715 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8716 GDK_SELECTION_CLIPBOARD);
8717 gtk_clipboard_request_text (clipboard,
8718 (GtkClipboardTextReceivedFunc) paste_text_received,
8722 /* Implementation for GtkFileChooserEmbed::should_respond() */
8724 add_selection_to_recent_list (GtkFileChooserDefault *impl)
8729 files = gtk_file_chooser_default_get_files (GTK_FILE_CHOOSER (impl));
8731 for (l = files; l; l = l->next)
8733 GFile *file = l->data;
8736 uri = g_file_get_uri (file);
8739 gtk_recent_manager_add_item (impl->recent_manager, uri);
8744 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
8745 g_slist_free (files);
8749 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8751 GtkFileChooserDefault *impl;
8752 GtkWidget *toplevel;
8753 GtkWidget *current_focus;
8756 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8758 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8759 g_assert (GTK_IS_WINDOW (toplevel));
8763 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8765 if (current_focus == impl->browse_files_tree_view)
8767 /* The following array encodes what we do based on the impl->action and the
8768 * number of files selected.
8771 NOOP, /* Do nothing (don't respond) */
8772 RESPOND, /* Respond immediately */
8773 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8774 ALL_FILES, /* Respond only if everything selected is a file */
8775 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8776 SAVE_ENTRY, /* Go to the code for handling the save entry */
8777 NOT_REACHED /* Sanity check */
8779 static const ActionToTake what_to_do[4][3] = {
8780 /* 0 selected 1 selected many selected */
8781 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8782 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8783 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8784 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8788 gboolean all_files, all_folders;
8790 ActionToTake action;
8794 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8796 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8798 retval = search_should_respond (impl);
8802 if (impl->operation_mode == OPERATION_MODE_RECENT)
8804 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8808 retval = recent_should_respond (impl);
8813 selection_check (impl, &num_selected, &all_files, &all_folders);
8815 if (num_selected > 2)
8820 action = what_to_do [impl->action] [k];
8831 case RESPOND_OR_SWITCH:
8832 g_assert (num_selected == 1);
8836 switch_to_selected_folder (impl);
8839 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8841 retval = should_respond_after_confirm_overwrite (impl,
8842 get_display_name_from_file_list (impl),
8843 impl->current_folder);
8857 retval = all_folders;
8864 g_assert_not_reached ();
8867 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8870 gboolean is_well_formed, is_empty, is_file_part_empty;
8872 GtkFileChooserEntry *entry;
8877 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8878 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8879 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8880 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8881 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8883 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8884 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8886 if (!is_well_formed)
8889 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8890 && impl->operation_mode == OPERATION_MODE_RECENT)
8892 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FOLDER);
8894 /* We'll #ifdef this out, as the fucking treeview selects its first row,
8895 * thus changing our assumption that no selection is present - setting
8896 * a selection causes the error message from path_bar_set_mode() to go away,
8897 * but we want the user to see that message!
8899 gtk_widget_grab_focus (impl->browse_files_tree_view);
8902 /* FIXME: else show an "invalid filename" error as the pathbar mode? */
8909 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8910 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8912 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FILENAME);
8913 gtk_widget_grab_focus (impl->location_entry);
8920 g_assert (file != NULL);
8925 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8926 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8928 change_folder_and_display_error (impl, file, TRUE);
8930 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8931 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8933 /* The folder already exists, so we do not need to create it.
8934 * Just respond to terminate the dialog.
8940 g_assert_not_reached ();
8945 struct FileExistsData *data;
8947 /* We need to check whether file exists and whether it is a folder -
8948 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8949 * negative (it doesn't know yet if your last path component is a
8953 data = g_new0 (struct FileExistsData, 1);
8954 data->impl = g_object_ref (impl);
8955 data->file = g_object_ref (file);
8956 data->parent_file = _gtk_file_chooser_entry_get_current_folder (entry);
8958 if (impl->file_exists_get_info_cancellable)
8959 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8961 impl->file_exists_get_info_cancellable =
8962 _gtk_file_system_get_info (impl->file_system, file,
8964 file_exists_get_info_cb,
8967 set_busy_cursor (impl, TRUE);
8970 g_error_free (error);
8973 g_object_unref (file);
8975 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8977 /* The focus is on a dialog's action area button, *and* the widget that
8978 * was focused immediately before it is the file list.
8982 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8984 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8987 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8989 /* The focus is on a dialog's action area button, *and* the widget that
8990 * was focused immediately before it is the location entry.
8995 /* The focus is on a dialog's action area button or something else */
8996 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8997 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9005 add_selection_to_recent_list (impl);
9010 /* Implementation for GtkFileChooserEmbed::initial_focus() */
9012 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
9014 GtkFileChooserDefault *impl;
9017 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
9019 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9020 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9022 if (impl->location_mode == LOCATION_MODE_PATH_BAR
9023 || impl->operation_mode == OPERATION_MODE_RECENT)
9024 widget = impl->browse_files_tree_view;
9026 widget = impl->location_entry;
9028 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
9029 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9030 widget = impl->location_entry;
9033 g_assert_not_reached ();
9037 g_assert (widget != NULL);
9038 gtk_widget_grab_focus (widget);
9041 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
9043 search_selected_foreach_get_file_cb (GtkTreeModel *model,
9053 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9054 *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 */
9057 /* Constructs a list of the selected paths in search mode */
9059 search_get_selected_files (GtkFileChooserDefault *impl)
9062 GtkTreeSelection *selection;
9066 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9067 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
9068 result = g_slist_reverse (result);
9073 /* Called from ::should_respond(). We return whether there are selected files
9074 * in the search list.
9077 search_should_respond (GtkFileChooserDefault *impl)
9079 GtkTreeSelection *selection;
9081 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
9083 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9084 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9087 /* Adds one hit from the search engine to the search_model */
9089 search_add_hit (GtkFileChooserDefault *impl,
9094 file = g_file_new_for_uri (uri);
9098 if (!g_file_is_native (file))
9100 g_object_unref (file);
9104 _gtk_file_system_model_add_and_query_file (impl->search_model,
9108 g_object_unref (file);
9111 /* Callback used from GtkSearchEngine when we get new hits */
9113 search_engine_hits_added_cb (GtkSearchEngine *engine,
9117 GtkFileChooserDefault *impl;
9120 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9122 for (l = hits; l; l = l->next)
9123 search_add_hit (impl, (gchar*)l->data);
9126 /* Callback used from GtkSearchEngine when the query is done running */
9128 search_engine_finished_cb (GtkSearchEngine *engine,
9131 GtkFileChooserDefault *impl;
9133 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9136 /* EB: setting the model here will avoid loads of row events,
9137 * but it'll make the search look like blocked.
9139 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9140 GTK_TREE_MODEL (impl->search_model));
9141 file_list_set_sort_column_ids (impl);
9144 /* FMQ: if search was empty, say that we got no hits */
9145 set_busy_cursor (impl, FALSE);
9148 /* Displays a generic error when we cannot create a GtkSearchEngine.
9149 * It would be better if _gtk_search_engine_new() gave us a GError
9150 * with a better message, but it doesn't do that right now.
9153 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9155 error_message (impl,
9156 _("Could not start the search process"),
9157 _("The program was not able to create a connection to the indexer "
9158 "daemon. Please make sure it is running."));
9162 search_engine_error_cb (GtkSearchEngine *engine,
9163 const gchar *message,
9166 GtkFileChooserDefault *impl;
9168 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9170 search_stop_searching (impl, TRUE);
9171 error_message (impl, _("Could not send the search request"), message);
9173 set_busy_cursor (impl, FALSE);
9176 /* Frees the data in the search_model */
9178 search_clear_model (GtkFileChooserDefault *impl,
9179 gboolean remove_from_treeview)
9181 if (!impl->search_model)
9184 g_object_unref (impl->search_model);
9185 impl->search_model = NULL;
9187 if (remove_from_treeview)
9188 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9191 /* Stops any ongoing searches; does not touch the search_model */
9193 search_stop_searching (GtkFileChooserDefault *impl,
9194 gboolean remove_query)
9196 if (remove_query && impl->search_query)
9198 g_object_unref (impl->search_query);
9199 impl->search_query = NULL;
9202 if (impl->search_engine)
9204 _gtk_search_engine_stop (impl->search_engine);
9206 g_object_unref (impl->search_engine);
9207 impl->search_engine = NULL;
9211 /* Creates the search_model and puts it in the tree view */
9213 search_setup_model (GtkFileChooserDefault *impl)
9215 g_assert (impl->search_model == NULL);
9217 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9219 MODEL_COLUMN_TYPES);
9221 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9225 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9229 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9233 set_sort_column (impl);
9235 /* EB: setting the model here will make the hits list update feel
9236 * more "alive" than setting the model at the end of the search
9239 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9240 GTK_TREE_MODEL (impl->search_model));
9241 file_list_set_sort_column_ids (impl);
9244 /* Creates a new query with the specified text and launches it */
9246 search_start_query (GtkFileChooserDefault *impl,
9247 const gchar *query_text)
9249 search_stop_searching (impl, FALSE);
9250 search_clear_model (impl, TRUE);
9251 search_setup_model (impl);
9252 set_busy_cursor (impl, TRUE);
9254 if (impl->search_engine == NULL)
9255 impl->search_engine = _gtk_search_engine_new ();
9257 if (!impl->search_engine)
9259 set_busy_cursor (impl, FALSE);
9260 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9264 if (!impl->search_query)
9266 impl->search_query = _gtk_query_new ();
9267 _gtk_query_set_text (impl->search_query, query_text);
9270 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9272 g_signal_connect (impl->search_engine, "hits-added",
9273 G_CALLBACK (search_engine_hits_added_cb), impl);
9274 g_signal_connect (impl->search_engine, "finished",
9275 G_CALLBACK (search_engine_finished_cb), impl);
9276 g_signal_connect (impl->search_engine, "error",
9277 G_CALLBACK (search_engine_error_cb), impl);
9279 _gtk_search_engine_start (impl->search_engine);
9282 /* Callback used when the user presses Enter while typing on the search
9283 * entry; starts the query
9286 search_entry_activate_cb (GtkEntry *entry,
9289 GtkFileChooserDefault *impl;
9292 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9294 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9295 if (strlen (text) == 0)
9298 /* reset any existing query object */
9299 if (impl->search_query)
9301 g_object_unref (impl->search_query);
9302 impl->search_query = NULL;
9305 search_start_query (impl, text);
9309 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9311 GDK_THREADS_ENTER ();
9313 g_source_destroy (impl->focus_entry_idle);
9314 impl->focus_entry_idle = NULL;
9316 if (impl->search_entry)
9317 gtk_widget_grab_focus (impl->search_entry);
9319 GDK_THREADS_LEAVE ();
9325 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9327 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9328 * pane, we get a selection-changed signal and we set up the search widgets.
9329 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9330 * the change to the selection. So, we need to re-focus the search entry
9331 * after the treeview has finished doing its work; we'll do that in an idle
9335 if (!impl->focus_entry_idle)
9336 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9339 /* Hides the path bar and creates the search entry */
9341 search_setup_widgets (GtkFileChooserDefault *impl)
9343 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9345 path_bar_update (impl);
9347 impl->search_entry = gtk_entry_new ();
9348 g_signal_connect (impl->search_entry, "activate",
9349 G_CALLBACK (search_entry_activate_cb),
9351 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9353 /* if there already is a query, restart it */
9354 if (impl->search_query)
9356 gchar *query = _gtk_query_get_text (impl->search_query);
9360 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9361 search_start_query (impl, query);
9367 g_object_unref (impl->search_query);
9368 impl->search_query = NULL;
9372 /* Box for search widgets */
9373 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9374 gtk_widget_show_all (impl->search_hbox);
9375 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9377 /* Hide the location widgets temporarily */
9379 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9380 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9382 gtk_widget_hide (impl->location_button);
9383 gtk_widget_hide (impl->location_entry_box);
9386 focus_search_entry_in_idle (impl);
9388 /* FMQ: hide the filter combo? */
9392 * Recent files support
9395 /* Frees the data in the recent_model */
9397 recent_clear_model (GtkFileChooserDefault *impl,
9398 gboolean remove_from_treeview)
9400 if (!impl->recent_model)
9403 if (remove_from_treeview)
9404 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9406 g_object_unref (impl->recent_model);
9407 impl->recent_model = NULL;
9410 /* Stops any ongoing loading of the recent files list; does
9411 * not touch the recent_model
9414 recent_stop_loading (GtkFileChooserDefault *impl)
9416 if (impl->load_recent_id)
9418 g_source_remove (impl->load_recent_id);
9419 impl->load_recent_id = 0;
9424 recent_setup_model (GtkFileChooserDefault *impl)
9426 g_assert (impl->recent_model == NULL);
9428 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9430 MODEL_COLUMN_TYPES);
9432 _gtk_file_system_model_set_filter (impl->recent_model,
9433 impl->current_filter);
9434 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9438 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9442 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9446 set_sort_column (impl);
9451 GtkFileChooserDefault *impl;
9456 recent_idle_cleanup (gpointer data)
9458 RecentLoadData *load_data = data;
9459 GtkFileChooserDefault *impl = load_data->impl;
9461 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9462 GTK_TREE_MODEL (impl->recent_model));
9463 file_list_set_sort_column_ids (impl);
9464 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model), MODEL_COL_MTIME, GTK_SORT_DESCENDING);
9466 set_busy_cursor (impl, FALSE);
9468 impl->load_recent_id = 0;
9474 get_recent_files_limit (GtkWidget *widget)
9476 GtkSettings *settings;
9479 if (gtk_widget_has_screen (widget))
9480 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9482 settings = gtk_settings_get_default ();
9484 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9489 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9491 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9497 limit = get_recent_files_limit (GTK_WIDGET (impl));
9501 for (l = items; l; l = l->next)
9503 GtkRecentInfo *info = l->data;
9506 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9507 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9510 g_object_unref (file);
9513 if (limit != -1 && n >= limit)
9519 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9524 folders = _gtk_file_chooser_extract_recent_folders (items);
9526 for (l = folders; l; l = l->next)
9528 GFile *folder = l->data;
9530 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9535 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9536 g_list_free (folders);
9540 recent_idle_load (gpointer data)
9542 RecentLoadData *load_data = data;
9543 GtkFileChooserDefault *impl = load_data->impl;
9545 if (!impl->recent_manager)
9548 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9549 if (!load_data->items)
9552 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9553 populate_model_with_recent_items (impl, load_data->items);
9555 populate_model_with_folders (impl, load_data->items);
9557 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9558 g_list_free (load_data->items);
9559 load_data->items = NULL;
9565 recent_start_loading (GtkFileChooserDefault *impl)
9567 RecentLoadData *load_data;
9569 recent_stop_loading (impl);
9570 recent_clear_model (impl, TRUE);
9571 recent_setup_model (impl);
9572 set_busy_cursor (impl, TRUE);
9574 g_assert (impl->load_recent_id == 0);
9576 load_data = g_new (RecentLoadData, 1);
9577 load_data->impl = impl;
9578 load_data->items = NULL;
9580 /* begin lazy loading the recent files into the model */
9581 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9584 recent_idle_cleanup);
9588 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9598 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9599 *list = g_slist_prepend (*list, file);
9602 /* Constructs a list of the selected paths in recent files mode */
9604 recent_get_selected_files (GtkFileChooserDefault *impl)
9607 GtkTreeSelection *selection;
9611 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9612 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9613 result = g_slist_reverse (result);
9618 /* Called from ::should_respond(). We return whether there are selected
9619 * files in the recent files list.
9622 recent_should_respond (GtkFileChooserDefault *impl)
9624 GtkTreeSelection *selection;
9626 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9628 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9629 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9633 set_current_filter (GtkFileChooserDefault *impl,
9634 GtkFileFilter *filter)
9636 if (impl->current_filter != filter)
9640 /* NULL filters are allowed to reset to non-filtered status
9642 filter_index = g_slist_index (impl->filters, filter);
9643 if (impl->filters && filter && filter_index < 0)
9646 if (impl->current_filter)
9647 g_object_unref (impl->current_filter);
9648 impl->current_filter = filter;
9649 if (impl->current_filter)
9651 g_object_ref_sink (impl->current_filter);
9655 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9658 if (impl->browse_files_model)
9659 install_list_model_filter (impl);
9661 if (impl->search_model)
9662 _gtk_file_system_model_set_filter (impl->search_model, filter);
9664 if (impl->recent_model)
9665 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9667 g_object_notify (G_OBJECT (impl), "filter");
9672 filter_combo_changed (GtkComboBox *combo_box,
9673 GtkFileChooserDefault *impl)
9675 gint new_index = gtk_combo_box_get_active (combo_box);
9676 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9678 set_current_filter (impl, new_filter);
9682 check_preview_change (GtkFileChooserDefault *impl)
9684 GtkTreePath *cursor_path;
9686 char *new_display_name;
9687 GtkTreeModel *model;
9689 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9690 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9695 gtk_tree_model_get_iter (model, &iter, cursor_path);
9696 gtk_tree_model_get (model, &iter,
9697 MODEL_COL_FILE, &new_file,
9698 MODEL_COL_NAME, &new_display_name,
9701 gtk_tree_path_free (cursor_path);
9706 new_display_name = NULL;
9709 if (new_file != impl->preview_file &&
9710 !(new_file && impl->preview_file &&
9711 g_file_equal (new_file, impl->preview_file)))
9713 if (impl->preview_file)
9715 g_object_unref (impl->preview_file);
9716 g_free (impl->preview_display_name);
9721 impl->preview_file = new_file;
9722 impl->preview_display_name = new_display_name;
9726 impl->preview_file = NULL;
9727 impl->preview_display_name = NULL;
9728 g_free (new_display_name);
9731 if (impl->use_preview_label && impl->preview_label)
9732 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9734 g_signal_emit_by_name (impl, "update-preview");
9739 g_object_unref (new_file);
9741 g_free (new_display_name);
9746 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9747 GtkFileSystemVolume *volume,
9748 const GError *error,
9752 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9753 GtkFileChooserDefault *impl = data;
9755 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9758 impl->shortcuts_activate_iter_cancellable = NULL;
9760 set_busy_cursor (impl, FALSE);
9767 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9771 name = _gtk_file_system_volume_get_display_name (volume);
9772 msg = g_strdup_printf (_("Could not mount %s"), name);
9774 error_message (impl, msg, error->message);
9783 file = _gtk_file_system_volume_get_root (volume);
9786 change_folder_and_display_error (impl, file, FALSE);
9787 g_object_unref (file);
9791 g_object_unref (impl);
9792 g_object_unref (cancellable);
9796 /* Activates a volume by mounting it if necessary and then switching to its
9800 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9801 GtkFileSystemVolume *volume)
9805 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9807 /* We ref the file chooser since volume_mount() may run a main loop, and the
9808 * user could close the file chooser window in the meantime.
9810 g_object_ref (impl);
9812 if (!_gtk_file_system_volume_is_mounted (volume))
9814 GMountOperation *mount_op;
9816 set_busy_cursor (impl, TRUE);
9818 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9819 impl->shortcuts_activate_iter_cancellable =
9820 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9821 shortcuts_activate_volume_mount_cb,
9822 g_object_ref (impl));
9823 g_object_unref (mount_op);
9827 file = _gtk_file_system_volume_get_root (volume);
9830 change_folder_and_display_error (impl, file, FALSE);
9831 g_object_unref (file);
9835 g_object_unref (impl);
9838 /* Opens the folder or volume at the specified iter in the shortcuts model */
9839 struct ShortcutsActivateData
9841 GtkFileChooserDefault *impl;
9846 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9848 const GError *error,
9851 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9852 struct ShortcutsActivateData *data = user_data;
9854 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9857 data->impl->shortcuts_activate_iter_cancellable = NULL;
9862 if (!error && _gtk_file_info_consider_as_directory (info))
9863 change_folder_and_display_error (data->impl, data->file, FALSE);
9865 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9870 g_object_unref (data->impl);
9871 g_object_unref (data->file);
9874 g_object_unref (cancellable);
9878 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9879 GtkFileSystemVolume *volume,
9880 const GError *error,
9883 struct ShortcutsActivateData *data = user_data;
9887 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9889 g_object_unref (data->impl);
9890 g_object_unref (data->file);
9896 data->impl->shortcuts_activate_iter_cancellable =
9897 _gtk_file_system_get_info (data->impl->file_system, data->file,
9899 shortcuts_activate_get_info_cb, data);
9902 _gtk_file_system_volume_unref (volume);
9906 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9910 ShortcutType shortcut_type;
9912 /* In the Save modes, we want to preserve what the uesr typed in the filename
9913 * entry, so that he may choose another folder without erasing his typed name.
9915 if (impl->location_entry
9916 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9917 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9918 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), "");
9920 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9921 SHORTCUTS_COL_DATA, &col_data,
9922 SHORTCUTS_COL_TYPE, &shortcut_type,
9925 if (impl->shortcuts_activate_iter_cancellable)
9927 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9928 impl->shortcuts_activate_iter_cancellable = NULL;
9931 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9933 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9935 GtkFileSystemVolume *volume;
9939 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9941 shortcuts_activate_volume (impl, volume);
9943 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9945 struct ShortcutsActivateData *data;
9946 GtkFileSystemVolume *volume;
9948 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9950 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9952 data = g_new0 (struct ShortcutsActivateData, 1);
9953 data->impl = g_object_ref (impl);
9954 data->file = g_object_ref (col_data);
9956 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9958 GMountOperation *mount_operation;
9959 GtkWidget *toplevel;
9961 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9963 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9965 impl->shortcuts_activate_iter_cancellable =
9966 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9968 shortcuts_activate_mount_enclosing_volume,
9973 impl->shortcuts_activate_iter_cancellable =
9974 _gtk_file_system_get_info (impl->file_system, data->file,
9976 shortcuts_activate_get_info_cb, data);
9979 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9981 operation_mode_set (impl, OPERATION_MODE_SEARCH);
9983 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9985 operation_mode_set (impl, OPERATION_MODE_RECENT);
9989 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9991 shortcuts_key_press_event_cb (GtkWidget *widget,
9993 GtkFileChooserDefault *impl)
9997 modifiers = gtk_accelerator_get_default_mod_mask ();
9999 if (key_is_left_or_right (event))
10001 gtk_widget_grab_focus (impl->browse_files_tree_view);
10005 if ((event->keyval == GDK_KEY_BackSpace
10006 || event->keyval == GDK_KEY_Delete
10007 || event->keyval == GDK_KEY_KP_Delete)
10008 && (event->state & modifiers) == 0)
10010 remove_selected_bookmarks (impl);
10014 if ((event->keyval == GDK_KEY_F2)
10015 && (event->state & modifiers) == 0)
10017 rename_selected_bookmark (impl);
10025 shortcuts_select_func (GtkTreeSelection *selection,
10026 GtkTreeModel *model,
10028 gboolean path_currently_selected,
10031 GtkFileChooserDefault *impl = data;
10032 GtkTreeIter filter_iter;
10033 ShortcutType shortcut_type;
10035 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10036 g_assert_not_reached ();
10038 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10040 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10044 list_select_func (GtkTreeSelection *selection,
10045 GtkTreeModel *model,
10047 gboolean path_currently_selected,
10050 GtkFileChooserDefault *impl = data;
10052 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10053 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10056 gboolean is_folder;
10058 if (!gtk_tree_model_get_iter (model, &iter, path))
10060 gtk_tree_model_get (model, &iter,
10061 MODEL_COL_IS_FOLDER, &is_folder,
10071 list_selection_changed (GtkTreeSelection *selection,
10072 GtkFileChooserDefault *impl)
10074 /* See if we are in the new folder editable row for Save mode */
10075 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10076 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10079 gboolean had_selection;
10081 info = get_selected_file_info_from_file_list (impl, &had_selection);
10082 if (!had_selection)
10083 goto out; /* normal processing */
10086 return; /* We are on the editable row for New Folder */
10091 if (impl->location_entry)
10092 update_chooser_entry (impl);
10094 path_bar_update (impl);
10096 check_preview_change (impl);
10097 bookmarks_check_add_sensitivity (impl);
10098 check_copy_file_location_sensitivity (impl);
10100 g_signal_emit_by_name (impl, "selection-changed", 0);
10103 /* Callback used when a row in the file list is activated */
10105 list_row_activated (GtkTreeView *tree_view,
10107 GtkTreeViewColumn *column,
10108 GtkFileChooserDefault *impl)
10112 GtkTreeModel *model;
10113 gboolean is_folder;
10115 model = gtk_tree_view_get_model (tree_view);
10117 if (!gtk_tree_model_get_iter (model, &iter, path))
10120 gtk_tree_model_get (model, &iter,
10121 MODEL_COL_FILE, &file,
10122 MODEL_COL_IS_FOLDER, &is_folder,
10125 if (is_folder && file)
10127 change_folder_and_display_error (impl, file, FALSE);
10131 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10132 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10133 g_signal_emit_by_name (impl, "file-activated");
10138 g_object_unref (file);
10142 path_bar_clicked (GtkPathBar *path_bar,
10145 gboolean child_is_hidden,
10146 GtkFileChooserDefault *impl)
10149 pending_select_files_add (impl, child_file);
10151 if (!change_folder_and_display_error (impl, file, FALSE))
10154 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10155 * show hidden files so that ".baz" appears in the file list, as it will still
10156 * be shown in the path bar: "/foo/[bar]/.baz"
10158 if (child_is_hidden)
10159 g_object_set (impl, "show-hidden", TRUE, NULL);
10163 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10165 GtkTreeViewColumn *column;
10166 GtkCellRenderer *renderer;
10167 GList *walk, *list;
10168 gboolean always_sensitive;
10170 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10171 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10173 /* Keep the following column numbers in sync with create_file_list() */
10176 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10177 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10178 for (walk = list; walk; walk = walk->next)
10180 renderer = walk->data;
10181 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10183 gtk_tree_view_column_set_attributes (column, renderer,
10184 "pixbuf", MODEL_COL_PIXBUF,
10189 gtk_tree_view_column_set_attributes (column, renderer,
10190 "text", MODEL_COL_NAME,
10191 "ellipsize", MODEL_COL_ELLIPSIZE,
10194 if (always_sensitive)
10195 g_object_set (renderer, "sensitive", TRUE, NULL);
10197 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10199 g_list_free (list);
10202 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10203 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10204 renderer = list->data;
10205 gtk_tree_view_column_set_attributes (column, renderer,
10206 "text", MODEL_COL_SIZE_TEXT,
10208 if (always_sensitive)
10209 g_object_set (renderer, "sensitive", TRUE, NULL);
10211 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10212 g_list_free (list);
10215 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10216 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10217 renderer = list->data;
10218 gtk_tree_view_column_set_attributes (column, renderer,
10219 "text", MODEL_COL_MTIME_TEXT,
10221 if (always_sensitive)
10222 g_object_set (renderer, "sensitive", TRUE, NULL);
10224 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10225 g_list_free (list);
10229 _gtk_file_chooser_default_new (void)
10231 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10235 location_set_user_text (GtkFileChooserDefault *impl,
10238 gtk_entry_set_text (GTK_ENTRY (impl->location_entry), path);
10239 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10243 location_popup_handler (GtkFileChooserDefault *impl,
10246 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10248 GtkWidget *widget_to_focus;
10250 operation_mode_set (impl, OPERATION_MODE_BROWSE);
10252 if (impl->current_folder)
10253 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10255 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10256 widget_to_focus = impl->browse_files_tree_view;
10258 widget_to_focus = impl->location_entry;
10260 gtk_widget_grab_focus (widget_to_focus);
10264 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10265 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10270 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
10271 location_set_user_text (impl, path);
10273 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10274 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10276 gtk_widget_grab_focus (impl->location_entry);
10278 location_set_user_text (impl, path);
10281 g_assert_not_reached ();
10284 /* Handler for the "up-folder" keybinding signal */
10286 up_folder_handler (GtkFileChooserDefault *impl)
10288 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10291 /* Handler for the "down-folder" keybinding signal */
10293 down_folder_handler (GtkFileChooserDefault *impl)
10295 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10298 /* Switches to the shortcut in the specified index */
10300 switch_to_shortcut (GtkFileChooserDefault *impl,
10305 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10306 g_assert_not_reached ();
10308 shortcuts_activate_iter (impl, &iter);
10311 /* Handler for the "home-folder" keybinding signal */
10313 home_folder_handler (GtkFileChooserDefault *impl)
10315 if (impl->has_home)
10316 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10319 /* Handler for the "desktop-folder" keybinding signal */
10321 desktop_folder_handler (GtkFileChooserDefault *impl)
10323 if (impl->has_desktop)
10324 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10327 /* Handler for the "search-shortcut" keybinding signal */
10329 search_shortcut_handler (GtkFileChooserDefault *impl)
10331 if (impl->has_search)
10333 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10335 /* we want the entry widget to grab the focus the first
10336 * time, not the browse_files_tree_view widget.
10338 if (impl->search_entry)
10339 gtk_widget_grab_focus (impl->search_entry);
10343 /* Handler for the "recent-shortcut" keybinding signal */
10345 recent_shortcut_handler (GtkFileChooserDefault *impl)
10347 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10351 quick_bookmark_handler (GtkFileChooserDefault *impl,
10352 gint bookmark_index)
10357 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10360 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10362 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10363 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10366 gtk_tree_path_free (path);
10368 switch_to_shortcut (impl, bookmark_pos);
10372 show_hidden_handler (GtkFileChooserDefault *impl)
10374 g_object_set (impl,
10375 "show-hidden", !impl->show_hidden,
10380 /* Drag and drop interfaces */
10383 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10388 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10390 model->impl = NULL;
10393 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10395 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10398 ShortcutsPaneModelFilter *model;
10402 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10404 pos = *gtk_tree_path_get_indices (path);
10405 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10407 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10410 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10414 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10416 GtkSelectionData *selection_data)
10423 /* Fill the GtkTreeDragSourceIface vtable */
10425 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10427 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10428 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10432 /* Fill the GtkTreeDragDestIface vtable */
10434 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10436 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10437 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10441 static GtkTreeModel *
10442 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10443 GtkTreeModel *child_model,
10446 ShortcutsPaneModelFilter *model;
10448 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10449 "child-model", child_model,
10450 "virtual-root", root,
10453 model->impl = impl;
10455 return GTK_TREE_MODEL (model);