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_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
824 g_slist_free (impl->pending_select_files);
825 impl->pending_select_files = NULL;
829 pending_select_files_add (GtkFileChooserDefault *impl,
832 impl->pending_select_files =
833 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
837 gtk_file_chooser_default_finalize (GObject *object)
839 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
842 unset_file_system_backend (impl);
844 if (impl->shortcuts_pane_filter_model)
845 g_object_unref (impl->shortcuts_pane_filter_model);
847 shortcuts_free (impl);
849 g_free (impl->browse_files_last_selected_name);
851 for (l = impl->filters; l; l = l->next)
853 GtkFileFilter *filter;
855 filter = GTK_FILE_FILTER (l->data);
856 g_object_unref (filter);
858 g_slist_free (impl->filters);
860 if (impl->current_filter)
861 g_object_unref (impl->current_filter);
863 if (impl->current_volume_file)
864 g_object_unref (impl->current_volume_file);
866 if (impl->current_folder)
867 g_object_unref (impl->current_folder);
869 if (impl->preview_file)
870 g_object_unref (impl->preview_file);
872 if (impl->browse_path_bar_size_group)
873 g_object_unref (impl->browse_path_bar_size_group);
875 /* Free all the Models we have */
876 stop_loading_and_clear_list_model (impl, FALSE);
877 search_clear_model (impl, FALSE);
878 recent_clear_model (impl, FALSE);
880 /* stopping the load above should have cleared this */
881 g_assert (impl->load_timeout_id == 0);
883 g_free (impl->preview_display_name);
885 g_free (impl->edited_new_text);
887 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
890 /* Shows an error dialog set as transient for the specified window */
892 error_message_with_parent (GtkWindow *parent,
898 dialog = gtk_message_dialog_new (parent,
899 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
904 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
907 if (parent && gtk_window_has_group (parent))
908 gtk_window_group_add_window (gtk_window_get_group (parent),
909 GTK_WINDOW (dialog));
911 gtk_dialog_run (GTK_DIALOG (dialog));
912 gtk_widget_destroy (dialog);
915 /* Returns a toplevel GtkWindow, or NULL if none */
917 get_toplevel (GtkWidget *widget)
921 toplevel = gtk_widget_get_toplevel (widget);
922 if (!gtk_widget_is_toplevel (toplevel))
925 return GTK_WINDOW (toplevel);
928 /* Shows an error dialog for the file chooser */
930 error_message (GtkFileChooserDefault *impl,
934 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
937 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
939 error_dialog (GtkFileChooserDefault *impl,
950 uri = g_file_get_uri (file);
951 text = g_strdup_printf (msg, uri);
952 error_message (impl, text, error->message);
955 g_error_free (error);
959 /* Displays an error message about not being able to get information for a file.
960 * Frees the GError as well.
963 error_getting_info_dialog (GtkFileChooserDefault *impl,
968 _("Could not retrieve information about the file"),
972 /* Shows an error dialog about not being able to add a bookmark */
974 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
979 _("Could not add a bookmark"),
983 /* Shows an error dialog about not being able to remove a bookmark */
985 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
990 _("Could not remove bookmark"),
994 /* Shows an error dialog about not being able to create a folder */
996 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1001 _("The folder could not be created"),
1005 /* Shows an error about not being able to create a folder because a file with
1006 * the same name is already there.
1009 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1014 _("The folder could not be created, as a file with the same "
1015 "name already exists. Try using a different name for the "
1016 "folder, or rename the file first."),
1021 error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
1027 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
1028 _("You need to choose a valid filename."));
1031 _("Cannot create a file under %s as it is not a folder"),
1032 parent_file, error);
1035 /* Shows an error about not being able to select a folder because a file with
1036 * the same name is already there.
1039 error_selecting_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1043 _("You may only select folders. The item that you selected is not a folder; "
1044 "try using a different item."),
1048 /* Shows an error dialog about not being able to create a filename */
1050 error_building_filename_dialog (GtkFileChooserDefault *impl,
1053 error_dialog (impl, _("Invalid file name"),
1057 /* Shows an error dialog when we cannot switch to a folder */
1059 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1063 error_dialog (impl, _("The folder contents could not be displayed"),
1067 /* Changes folders, displaying an error dialog if this fails */
1069 change_folder_and_display_error (GtkFileChooserDefault *impl,
1071 gboolean clear_entry)
1076 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1078 /* We copy the path because of this case:
1080 * list_row_activated()
1081 * fetches path from model; path belongs to the model (*)
1082 * calls change_folder_and_display_error()
1083 * calls gtk_file_chooser_set_current_folder_file()
1084 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1088 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1091 error_changing_folder_dialog (impl, file, error);
1097 emit_default_size_changed (GtkFileChooserDefault *impl)
1099 profile_msg (" emit default-size-changed start", NULL);
1100 g_signal_emit_by_name (impl, "default-size-changed");
1101 profile_msg (" emit default-size-changed end", NULL);
1105 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1107 if (impl->use_preview_label)
1109 if (!impl->preview_label)
1111 impl->preview_label = gtk_label_new (impl->preview_display_name);
1112 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1113 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1114 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1115 gtk_widget_show (impl->preview_label);
1120 if (impl->preview_label)
1122 gtk_widget_destroy (impl->preview_label);
1123 impl->preview_label = NULL;
1127 if (impl->preview_widget_active && impl->preview_widget)
1128 gtk_widget_show (impl->preview_box);
1130 gtk_widget_hide (impl->preview_box);
1132 if (!gtk_widget_get_mapped (GTK_WIDGET (impl)))
1133 emit_default_size_changed (impl);
1137 set_preview_widget (GtkFileChooserDefault *impl,
1138 GtkWidget *preview_widget)
1140 if (preview_widget == impl->preview_widget)
1143 if (impl->preview_widget)
1144 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1145 impl->preview_widget);
1147 impl->preview_widget = preview_widget;
1148 if (impl->preview_widget)
1150 gtk_widget_show (impl->preview_widget);
1151 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1152 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1153 impl->preview_widget,
1154 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1157 update_preview_widget_visibility (impl);
1160 /* Renders a "Search" icon at an appropriate size for a tree view */
1162 render_search_icon (GtkFileChooserDefault *impl)
1164 return gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
1168 render_recent_icon (GtkFileChooserDefault *impl)
1170 GtkIconTheme *theme;
1173 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1174 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1176 theme = gtk_icon_theme_get_default ();
1178 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1184 retval = gtk_widget_render_icon_pixbuf (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU);
1190 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1191 struct ReloadIconsData
1193 GtkFileChooserDefault *impl;
1194 GtkTreeRowReference *row_ref;
1198 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1200 const GError *error,
1206 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1207 struct ReloadIconsData *data = user_data;
1209 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1212 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1214 if (cancelled || error)
1217 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1219 path = gtk_tree_row_reference_get_path (data->row_ref);
1222 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1223 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1224 SHORTCUTS_COL_PIXBUF, pixbuf,
1226 gtk_tree_path_free (path);
1230 g_object_unref (pixbuf);
1233 gtk_tree_row_reference_free (data->row_ref);
1234 g_object_unref (data->impl);
1237 g_object_unref (cancellable);
1241 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1246 profile_start ("start", NULL);
1248 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1251 for (l = impl->reload_icon_cancellables; l; l = l->next)
1253 GCancellable *cancellable = G_CANCELLABLE (l->data);
1254 g_cancellable_cancel (cancellable);
1256 g_slist_free (impl->reload_icon_cancellables);
1257 impl->reload_icon_cancellables = NULL;
1262 ShortcutType shortcut_type;
1263 gboolean pixbuf_visible;
1266 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1267 SHORTCUTS_COL_DATA, &data,
1268 SHORTCUTS_COL_TYPE, &shortcut_type,
1269 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1275 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1277 GtkFileSystemVolume *volume;
1280 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1281 impl->icon_size, NULL);
1283 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1285 if (g_file_is_native (G_FILE (data)))
1288 struct ReloadIconsData *info;
1289 GtkTreePath *tree_path;
1290 GCancellable *cancellable;
1294 info = g_new0 (struct ReloadIconsData, 1);
1295 info->impl = g_object_ref (impl);
1296 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1297 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1298 gtk_tree_path_free (tree_path);
1300 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1302 shortcuts_reload_icons_get_info_cb,
1304 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1308 GtkIconTheme *icon_theme;
1310 /* Don't call get_info for remote paths to avoid latency and
1312 * If we switch to a better bookmarks file format (XBEL), we
1313 * should use mime info to get a better icon.
1315 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1316 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1317 impl->icon_size, 0, NULL);
1320 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1322 pixbuf = render_search_icon (impl);
1324 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1326 pixbuf = render_recent_icon (impl);
1329 gtk_list_store_set (impl->shortcuts_model, &iter,
1330 SHORTCUTS_COL_PIXBUF, pixbuf,
1334 g_object_unref (pixbuf);
1338 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1342 profile_end ("end", NULL);
1346 shortcuts_find_folder (GtkFileChooserDefault *impl,
1349 GtkTreeSelection *selection;
1353 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1355 g_assert (folder != NULL);
1356 pos = shortcut_find_position (impl, folder);
1359 gtk_tree_selection_unselect_all (selection);
1363 path = gtk_tree_path_new_from_indices (pos, -1);
1364 gtk_tree_selection_select_path (selection, path);
1365 gtk_tree_path_free (path);
1368 /* If a shortcut corresponds to the current folder, selects it */
1370 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1372 shortcuts_find_folder (impl, impl->current_folder);
1375 /* Removes the specified number of rows from the shortcuts list */
1377 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1383 path = gtk_tree_path_new_from_indices (start_row, -1);
1385 for (; n_rows; n_rows--)
1389 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1390 g_assert_not_reached ();
1392 shortcuts_free_row_data (impl, &iter);
1393 gtk_list_store_remove (impl->shortcuts_model, &iter);
1396 gtk_tree_path_free (path);
1400 shortcuts_update_count (GtkFileChooserDefault *impl,
1401 ShortcutsIndex type,
1406 case SHORTCUTS_HOME:
1408 impl->has_home = FALSE;
1410 impl->has_home = TRUE;
1413 case SHORTCUTS_DESKTOP:
1415 impl->has_desktop = FALSE;
1417 impl->has_desktop = TRUE;
1420 case SHORTCUTS_VOLUMES:
1421 impl->num_volumes += value;
1424 case SHORTCUTS_SHORTCUTS:
1425 impl->num_shortcuts += value;
1428 case SHORTCUTS_BOOKMARKS:
1429 impl->num_bookmarks += value;
1432 case SHORTCUTS_CURRENT_FOLDER:
1434 impl->shortcuts_current_folder_active = FALSE;
1436 impl->shortcuts_current_folder_active = TRUE;
1445 struct ShortcutsInsertRequest
1447 GtkFileChooserDefault *impl;
1451 GtkTreeRowReference *row_ref;
1452 ShortcutsIndex type;
1458 get_file_info_finished (GCancellable *cancellable,
1460 const GError *error,
1463 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1467 GCancellable *model_cancellable = NULL;
1468 struct ShortcutsInsertRequest *request = data;
1470 path = gtk_tree_row_reference_get_path (request->row_ref);
1472 /* Handle doesn't exist anymore in the model */
1475 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1477 gtk_tree_path_free (path);
1479 /* validate cancellable, else goto out */
1480 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1481 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1483 if (cancellable != model_cancellable)
1486 /* set the cancellable to NULL in the model (we unref later on) */
1487 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1488 SHORTCUTS_COL_CANCELLABLE, NULL,
1496 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1497 shortcuts_update_count (request->impl, request->type, -1);
1499 if (request->type == SHORTCUTS_HOME)
1503 home = g_file_new_for_path (g_get_home_dir ());
1504 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1505 g_object_unref (home);
1507 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1509 /* Remove the current folder separator */
1510 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1511 shortcuts_remove_rows (request->impl, separator_pos, 1);
1517 if (!request->label_copy)
1518 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1519 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1520 request->impl->icon_size);
1522 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1523 SHORTCUTS_COL_PIXBUF, pixbuf,
1524 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1525 SHORTCUTS_COL_NAME, request->label_copy,
1526 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1527 SHORTCUTS_COL_REMOVABLE, request->removable,
1530 if (request->impl->shortcuts_pane_filter_model)
1531 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1534 g_object_unref (pixbuf);
1537 g_object_unref (request->impl);
1538 g_object_unref (request->file);
1539 gtk_tree_row_reference_free (request->row_ref);
1540 g_free (request->label_copy);
1543 if (model_cancellable)
1544 g_object_unref (model_cancellable);
1547 /* FIXME: GtkFileSystem needs a function to split a remote path
1548 * into hostname and path components, or maybe just have a
1549 * gtk_file_system_path_get_display_name().
1551 * This function is also used in gtkfilechooserbutton.c
1554 _gtk_file_chooser_label_for_file (GFile *file)
1556 const gchar *path, *start, *end, *p;
1557 gchar *uri, *host, *label;
1559 uri = g_file_get_uri (file);
1561 start = strstr (uri, "://");
1565 path = strchr (start, '/');
1570 end = uri + strlen (uri);
1574 /* strip username */
1575 p = strchr (start, '@');
1579 p = strchr (start, ':');
1583 host = g_strndup (start, end - start);
1585 /* Translators: the first string is a path and the second string
1586 * is a hostname. Nautilus and the panel contain the same string
1589 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1595 label = g_strdup (uri);
1603 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1604 * inserts a volume. A position of -1 indicates the end of the tree.
1607 shortcuts_insert_file (GtkFileChooserDefault *impl,
1609 ShortcutType shortcut_type,
1610 GtkFileSystemVolume *volume,
1614 ShortcutsIndex type)
1617 GdkPixbuf *pixbuf = NULL;
1618 gpointer data = NULL;
1620 GtkIconTheme *icon_theme;
1622 profile_start ("start shortcut", NULL);
1624 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1627 label_copy = _gtk_file_system_volume_get_display_name (volume);
1628 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1629 impl->icon_size, NULL);
1631 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1633 if (g_file_is_native (file))
1635 struct ShortcutsInsertRequest *request;
1636 GCancellable *cancellable;
1639 request = g_new0 (struct ShortcutsInsertRequest, 1);
1640 request->impl = g_object_ref (impl);
1641 request->file = g_object_ref (file);
1642 request->name_only = TRUE;
1643 request->removable = removable;
1645 request->type = type;
1647 request->label_copy = g_strdup (label);
1650 gtk_list_store_append (impl->shortcuts_model, &iter);
1652 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1654 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1655 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1656 gtk_tree_path_free (p);
1658 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1659 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1660 get_file_info_finished, request);
1662 gtk_list_store_set (impl->shortcuts_model, &iter,
1663 SHORTCUTS_COL_DATA, g_object_ref (file),
1664 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1665 SHORTCUTS_COL_CANCELLABLE, cancellable,
1668 shortcuts_update_count (impl, type, 1);
1674 /* Don't call get_info for remote paths to avoid latency and
1677 data = g_object_ref (file);
1679 label_copy = g_strdup (label);
1681 label_copy = _gtk_file_chooser_label_for_file (file);
1683 /* If we switch to a better bookmarks file format (XBEL), we
1684 * should use mime info to get a better icon.
1686 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1687 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1688 impl->icon_size, 0, NULL);
1693 g_assert_not_reached ();
1699 gtk_list_store_append (impl->shortcuts_model, &iter);
1701 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1703 shortcuts_update_count (impl, type, 1);
1705 gtk_list_store_set (impl->shortcuts_model, &iter,
1706 SHORTCUTS_COL_PIXBUF, pixbuf,
1707 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1708 SHORTCUTS_COL_NAME, label_copy,
1709 SHORTCUTS_COL_DATA, data,
1710 SHORTCUTS_COL_TYPE, shortcut_type,
1711 SHORTCUTS_COL_REMOVABLE, removable,
1712 SHORTCUTS_COL_CANCELLABLE, NULL,
1715 if (impl->shortcuts_pane_filter_model)
1716 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1718 g_free (label_copy);
1721 g_object_unref (pixbuf);
1723 profile_end ("end", NULL);
1727 shortcuts_append_search (GtkFileChooserDefault *impl)
1732 pixbuf = render_search_icon (impl);
1734 gtk_list_store_append (impl->shortcuts_model, &iter);
1735 gtk_list_store_set (impl->shortcuts_model, &iter,
1736 SHORTCUTS_COL_PIXBUF, pixbuf,
1737 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1738 SHORTCUTS_COL_NAME, _("Search"),
1739 SHORTCUTS_COL_DATA, NULL,
1740 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1741 SHORTCUTS_COL_REMOVABLE, FALSE,
1745 g_object_unref (pixbuf);
1747 impl->has_search = TRUE;
1751 shortcuts_append_recent (GtkFileChooserDefault *impl)
1756 pixbuf = render_recent_icon (impl);
1758 gtk_list_store_append (impl->shortcuts_model, &iter);
1759 gtk_list_store_set (impl->shortcuts_model, &iter,
1760 SHORTCUTS_COL_PIXBUF, pixbuf,
1761 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1762 SHORTCUTS_COL_NAME, _("Recently Used"),
1763 SHORTCUTS_COL_DATA, NULL,
1764 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1765 SHORTCUTS_COL_REMOVABLE, FALSE,
1769 g_object_unref (pixbuf);
1772 /* Appends an item for the user's home directory to the shortcuts model */
1774 shortcuts_append_home (GtkFileChooserDefault *impl)
1776 const char *home_path;
1779 profile_start ("start", NULL);
1781 home_path = g_get_home_dir ();
1782 if (home_path == NULL)
1784 profile_end ("end - no home directory!?", NULL);
1788 home = g_file_new_for_path (home_path);
1789 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1790 impl->has_home = TRUE;
1792 g_object_unref (home);
1794 profile_end ("end", NULL);
1797 /* Appends the ~/Desktop directory to the shortcuts model */
1799 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1804 profile_start ("start", NULL);
1806 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1807 /* "To disable a directory, point it to the homedir."
1808 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1810 if (!g_strcmp0 (name, g_get_home_dir ()))
1812 profile_end ("end", NULL);
1816 file = g_file_new_for_path (name);
1817 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1818 impl->has_desktop = TRUE;
1820 /* We do not actually pop up an error dialog if there is no desktop directory
1821 * because some people may really not want to have one.
1824 g_object_unref (file);
1826 profile_end ("end", NULL);
1829 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1831 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1838 profile_start ("start", NULL);
1840 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1843 for (; bookmarks; bookmarks = bookmarks->next)
1847 file = bookmarks->data;
1849 if (impl->local_only && !g_file_is_native (file))
1852 if (shortcut_find_position (impl, file) != -1)
1855 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1857 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1863 profile_end ("end", NULL);
1865 return num_inserted;
1868 /* Returns the index for the corresponding item in the shortcuts bar */
1870 shortcuts_get_index (GtkFileChooserDefault *impl,
1871 ShortcutsIndex where)
1877 if (where == SHORTCUTS_SEARCH)
1880 n += impl->has_search ? 1 : 0;
1882 if (where == SHORTCUTS_RECENT)
1885 n += 1; /* we always have the recently-used item */
1887 if (where == SHORTCUTS_RECENT_SEPARATOR)
1890 n += 1; /* we always have the separator after the recently-used item */
1892 if (where == SHORTCUTS_HOME)
1895 n += impl->has_home ? 1 : 0;
1897 if (where == SHORTCUTS_DESKTOP)
1900 n += impl->has_desktop ? 1 : 0;
1902 if (where == SHORTCUTS_VOLUMES)
1905 n += impl->num_volumes;
1907 if (where == SHORTCUTS_SHORTCUTS)
1910 n += impl->num_shortcuts;
1912 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1915 /* If there are no bookmarks there won't be a separator */
1916 n += (impl->num_bookmarks > 0) ? 1 : 0;
1918 if (where == SHORTCUTS_BOOKMARKS)
1921 n += impl->num_bookmarks;
1923 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1928 if (where == SHORTCUTS_CURRENT_FOLDER)
1931 g_assert_not_reached ();
1938 /* Adds all the file system volumes to the shortcuts model */
1940 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1945 gboolean old_changing_folders;
1947 profile_start ("start", NULL);
1949 old_changing_folders = impl->changing_folder;
1950 impl->changing_folder = TRUE;
1952 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1953 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1954 impl->num_volumes = 0;
1956 list = _gtk_file_system_list_volumes (impl->file_system);
1960 for (l = list; l; l = l->next)
1962 GtkFileSystemVolume *volume;
1966 if (impl->local_only)
1968 if (_gtk_file_system_volume_is_mounted (volume))
1971 gboolean base_is_native = TRUE;
1973 base_file = _gtk_file_system_volume_get_root (volume);
1974 if (base_file != NULL)
1976 base_is_native = g_file_is_native (base_file);
1977 g_object_unref (base_file);
1980 if (!base_is_native)
1985 shortcuts_insert_file (impl,
1987 SHORTCUT_TYPE_VOLUME,
1988 _gtk_file_system_volume_ref (volume),
1996 impl->num_volumes = n;
1997 g_slist_free (list);
1999 if (impl->shortcuts_pane_filter_model)
2000 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2002 impl->changing_folder = old_changing_folders;
2004 profile_end ("end", NULL);
2007 /* Inserts a separator node in the shortcuts list */
2009 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2010 ShortcutsIndex where)
2014 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2015 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2016 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2018 gtk_list_store_insert (impl->shortcuts_model, &iter,
2019 shortcuts_get_index (impl, where));
2020 gtk_list_store_set (impl->shortcuts_model, &iter,
2021 SHORTCUTS_COL_PIXBUF, NULL,
2022 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2023 SHORTCUTS_COL_NAME, NULL,
2024 SHORTCUTS_COL_DATA, NULL,
2025 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2029 /* Updates the list of bookmarks */
2031 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2034 gboolean old_changing_folders;
2036 GFile *list_selected = NULL;
2037 ShortcutType shortcut_type;
2040 profile_start ("start", NULL);
2042 old_changing_folders = impl->changing_folder;
2043 impl->changing_folder = TRUE;
2045 if (shortcuts_get_selected (impl, &iter))
2047 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2049 SHORTCUTS_COL_DATA, &col_data,
2050 SHORTCUTS_COL_TYPE, &shortcut_type,
2053 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2054 list_selected = g_object_ref (col_data);
2057 if (impl->num_bookmarks > 0)
2058 shortcuts_remove_rows (impl,
2059 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2060 impl->num_bookmarks + 1);
2062 impl->num_bookmarks = 0;
2063 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2065 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2066 shortcuts_append_bookmarks (impl, bookmarks);
2067 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2068 g_slist_free (bookmarks);
2070 if (impl->num_bookmarks == 0)
2071 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2073 if (impl->shortcuts_pane_filter_model)
2074 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2078 shortcuts_find_folder (impl, list_selected);
2079 g_object_unref (list_selected);
2082 impl->changing_folder = old_changing_folders;
2084 profile_end ("end", NULL);
2087 /* Appends a separator and a row to the shortcuts list for the current folder */
2089 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2093 g_assert (!impl->shortcuts_current_folder_active);
2095 g_assert (impl->current_folder != NULL);
2097 pos = shortcut_find_position (impl, impl->current_folder);
2100 GtkFileSystemVolume *volume;
2104 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2107 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2109 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2111 base_file = _gtk_file_system_volume_get_root (volume);
2115 if (base_file && g_file_equal (base_file, impl->current_folder))
2116 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2118 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2121 g_object_unref (base_file);
2125 /* Updates the current folder row in the shortcuts model */
2127 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2131 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2133 if (impl->shortcuts_current_folder_active)
2135 shortcuts_remove_rows (impl, pos, 2);
2136 impl->shortcuts_current_folder_active = FALSE;
2139 shortcuts_add_current_folder (impl);
2142 /* Filter function used for the shortcuts filter model */
2144 shortcuts_pane_filter_cb (GtkTreeModel *model,
2148 GtkFileChooserDefault *impl;
2152 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2154 path = gtk_tree_model_get_path (model, iter);
2158 pos = *gtk_tree_path_get_indices (path);
2159 gtk_tree_path_free (path);
2161 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2164 /* Creates the list model for shortcuts */
2166 shortcuts_model_create (GtkFileChooserDefault *impl)
2168 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2169 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2170 GDK_TYPE_PIXBUF, /* pixbuf */
2171 G_TYPE_STRING, /* name */
2172 G_TYPE_POINTER, /* path or volume */
2173 G_TYPE_INT, /* ShortcutType */
2174 G_TYPE_BOOLEAN, /* removable */
2175 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2176 G_TYPE_POINTER); /* GCancellable */
2178 shortcuts_append_search (impl);
2180 if (impl->recent_manager)
2182 shortcuts_append_recent (impl);
2183 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2186 if (impl->file_system)
2188 shortcuts_append_home (impl);
2189 shortcuts_append_desktop (impl);
2190 shortcuts_add_volumes (impl);
2193 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2194 GTK_TREE_MODEL (impl->shortcuts_model),
2197 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2198 shortcuts_pane_filter_cb,
2203 /* Callback used when the "New Folder" button is clicked */
2205 new_folder_button_clicked (GtkButton *button,
2206 GtkFileChooserDefault *impl)
2211 if (!impl->browse_files_model)
2212 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2214 /* Prevent button from being clicked twice */
2215 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2217 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2219 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2220 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2221 path, impl->list_name_column,
2224 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2225 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2227 impl->list_name_column,
2230 gtk_tree_path_free (path);
2234 add_idle_while_impl_is_alive (GtkFileChooserDefault *impl, GCallback callback)
2238 source = g_idle_source_new ();
2239 g_source_set_closure (source,
2240 g_cclosure_new_object (callback, G_OBJECT (impl)));
2241 g_source_attach (source, NULL);
2246 /* Idle handler for creating a new folder after editing its name cell, or for
2247 * canceling the editing.
2250 edited_idle_cb (GtkFileChooserDefault *impl)
2252 GDK_THREADS_ENTER ();
2254 g_source_destroy (impl->edited_idle);
2255 impl->edited_idle = NULL;
2257 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2258 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2260 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2262 if (impl->edited_new_text /* not cancelled? */
2263 && (strlen (impl->edited_new_text) != 0)
2264 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2266 GError *error = NULL;
2269 file = g_file_get_child_for_display_name (impl->current_folder,
2270 impl->edited_new_text,
2274 GError *error = NULL;
2276 if (g_file_make_directory (file, NULL, &error))
2277 change_folder_and_display_error (impl, file, FALSE);
2279 error_creating_folder_dialog (impl, file, error);
2281 g_object_unref (file);
2284 error_creating_folder_dialog (impl, file, error);
2286 g_free (impl->edited_new_text);
2287 impl->edited_new_text = NULL;
2290 GDK_THREADS_LEAVE ();
2296 queue_edited_idle (GtkFileChooserDefault *impl,
2297 const gchar *new_text)
2299 /* We create the folder in an idle handler so that we don't modify the tree
2303 if (!impl->edited_idle)
2304 impl->edited_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (edited_idle_cb));
2306 g_free (impl->edited_new_text);
2307 impl->edited_new_text = g_strdup (new_text);
2310 /* Callback used from the text cell renderer when the new folder is named */
2312 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2314 const gchar *new_text,
2315 GtkFileChooserDefault *impl)
2317 /* work around bug #154921 */
2318 g_object_set (cell_renderer_text,
2319 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2320 queue_edited_idle (impl, new_text);
2323 /* Callback used from the text cell renderer when the new folder edition gets
2327 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2328 GtkFileChooserDefault *impl)
2330 /* work around bug #154921 */
2331 g_object_set (cell_renderer_text,
2332 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2333 queue_edited_idle (impl, NULL);
2336 /* Creates the widgets for the filter combo box */
2338 filter_create (GtkFileChooserDefault *impl)
2340 GtkCellRenderer *cell;
2343 impl->filter_combo = gtk_combo_box_text_new ();
2344 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2346 /* Get the combo's text renderer and set ellipsize parameters */
2347 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (impl->filter_combo));
2351 g_object_set (G_OBJECT (cell),
2352 "ellipsize", PANGO_ELLIPSIZE_END,
2355 g_list_free (cells);
2357 g_signal_connect (impl->filter_combo, "changed",
2358 G_CALLBACK (filter_combo_changed), impl);
2360 gtk_widget_set_tooltip_text (impl->filter_combo,
2361 _("Select which types of files are shown"));
2363 return impl->filter_combo;
2367 toolbutton_new (GtkFileChooserDefault *impl,
2376 item = gtk_tool_button_new (NULL, NULL);
2377 image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_SMALL_TOOLBAR);
2378 gtk_widget_show (image);
2379 gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), image);
2381 gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
2382 g_signal_connect (item, "clicked", callback, impl);
2385 gtk_widget_show (GTK_WIDGET (item));
2387 return GTK_WIDGET (item);
2390 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2392 shortcut_find_position (GtkFileChooserDefault *impl,
2397 int current_folder_separator_idx;
2399 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2402 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2405 /* FIXME: is this still needed? */
2406 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2410 for (i = 0; i < current_folder_separator_idx; i++)
2413 ShortcutType shortcut_type;
2415 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2416 SHORTCUTS_COL_DATA, &col_data,
2417 SHORTCUTS_COL_TYPE, &shortcut_type,
2422 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2424 GtkFileSystemVolume *volume;
2429 base_file = _gtk_file_system_volume_get_root (volume);
2431 exists = base_file && g_file_equal (file, base_file);
2434 g_object_unref (base_file);
2439 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2443 model_file = col_data;
2445 if (model_file && g_file_equal (model_file, file))
2450 if (i < current_folder_separator_idx - 1)
2452 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2453 g_assert_not_reached ();
2460 /* Tries to add a bookmark from a path name */
2462 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2468 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2470 if (shortcut_find_position (impl, file) != -1)
2474 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2476 error_adding_bookmark_dialog (impl, file, error);
2484 add_bookmark_foreach_cb (GtkTreeModel *model,
2489 GtkFileChooserDefault *impl;
2492 impl = (GtkFileChooserDefault *) data;
2494 gtk_tree_model_get (model, iter,
2495 MODEL_COL_FILE, &file,
2498 shortcuts_add_bookmark_from_file (impl, file, -1);
2500 g_object_unref (file);
2503 /* Adds a bookmark from the currently selected item in the file list */
2505 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2507 GtkTreeSelection *selection;
2509 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2511 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2512 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2514 gtk_tree_selection_selected_foreach (selection,
2515 add_bookmark_foreach_cb,
2519 /* Callback used when the "Add bookmark" button is clicked */
2521 add_bookmark_button_clicked_cb (GtkButton *button,
2522 GtkFileChooserDefault *impl)
2524 bookmarks_add_selected_folder (impl);
2527 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2528 * returns FALSE if no shortcut is selected.
2531 shortcuts_get_selected (GtkFileChooserDefault *impl,
2534 GtkTreeSelection *selection;
2535 GtkTreeIter parent_iter;
2537 if (!impl->browse_shortcuts_tree_view)
2540 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2542 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2545 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2551 /* Removes the selected bookmarks */
2553 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2561 if (!shortcuts_get_selected (impl, &iter))
2564 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2565 SHORTCUTS_COL_DATA, &col_data,
2566 SHORTCUTS_COL_REMOVABLE, &removable,
2572 g_assert (col_data != NULL);
2577 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2578 error_removing_bookmark_dialog (impl, file, error);
2581 /* Callback used when the "Remove bookmark" button is clicked */
2583 remove_bookmark_button_clicked_cb (GtkButton *button,
2584 GtkFileChooserDefault *impl)
2586 remove_selected_bookmarks (impl);
2589 struct selection_check_closure {
2590 GtkFileChooserDefault *impl;
2593 gboolean all_folders;
2596 /* Used from gtk_tree_selection_selected_foreach() */
2598 selection_check_foreach_cb (GtkTreeModel *model,
2603 struct selection_check_closure *closure;
2607 gtk_tree_model_get (model, iter,
2608 MODEL_COL_FILE, &file,
2609 MODEL_COL_IS_FOLDER, &is_folder,
2615 g_object_unref (file);
2618 closure->num_selected++;
2620 closure->all_folders = closure->all_folders && is_folder;
2621 closure->all_files = closure->all_files && !is_folder;
2624 /* Checks whether the selected items in the file list are all files or all folders */
2626 selection_check (GtkFileChooserDefault *impl,
2628 gboolean *all_files,
2629 gboolean *all_folders)
2631 struct selection_check_closure closure;
2632 GtkTreeSelection *selection;
2634 closure.impl = impl;
2635 closure.num_selected = 0;
2636 closure.all_files = TRUE;
2637 closure.all_folders = TRUE;
2639 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2640 gtk_tree_selection_selected_foreach (selection,
2641 selection_check_foreach_cb,
2644 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2647 *num_selected = closure.num_selected;
2650 *all_files = closure.all_files;
2653 *all_folders = closure.all_folders;
2656 struct get_selected_file_closure {
2657 GtkFileChooserDefault *impl;
2662 get_selected_file_foreach_cb (GtkTreeModel *model,
2667 struct get_selected_file_closure *closure = data;
2671 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2672 g_object_unref (closure->file);
2673 closure->file = NULL;
2676 gtk_tree_model_get (model, iter,
2677 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2681 /* Returns a selected path from the file list */
2683 get_selected_file (GtkFileChooserDefault *impl)
2685 struct get_selected_file_closure closure;
2686 GtkTreeSelection *selection;
2688 closure.impl = impl;
2689 closure.file = NULL;
2691 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2692 gtk_tree_selection_selected_foreach (selection,
2693 get_selected_file_foreach_cb,
2696 return closure.file;
2700 GtkFileChooserDefault *impl;
2702 } UpdateTooltipData;
2705 update_tooltip (GtkTreeModel *model,
2710 UpdateTooltipData *udata = data;
2712 if (udata->tip == NULL)
2714 gchar *display_name;
2716 gtk_tree_model_get (model, iter,
2717 MODEL_COL_NAME, &display_name,
2720 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2722 g_free (display_name);
2727 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2728 * if there are no selected items *and* the current folder is not in the
2729 * bookmarks list. De-sensitize the button otherwise.
2732 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2735 gboolean all_folders;
2739 selection_check (impl, &num_selected, NULL, &all_folders);
2741 if (num_selected == 0)
2742 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2743 else if (num_selected == 1)
2747 file = get_selected_file (impl);
2748 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2750 g_object_unref (file);
2753 active = all_folders;
2755 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2757 if (impl->browse_files_popup_menu_add_shortcut_item)
2758 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2759 (num_selected == 0) ? FALSE : active);
2763 if (num_selected == 0)
2764 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2765 else if (num_selected > 1)
2766 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2769 GtkTreeSelection *selection;
2770 UpdateTooltipData data;
2772 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2775 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2779 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2784 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2785 * bookmark row is selected in the shortcuts tree.
2788 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2791 gboolean removable = FALSE;
2795 if (shortcuts_get_selected (impl, &iter))
2797 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2798 SHORTCUTS_COL_REMOVABLE, &removable,
2799 SHORTCUTS_COL_NAME, &name,
2801 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2804 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2806 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2808 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2812 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2813 _("Remove the selected bookmark"));
2818 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2821 gboolean removable = FALSE;
2823 if (impl->browse_shortcuts_popup_menu == NULL)
2826 if (shortcuts_get_selected (impl, &iter))
2827 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2828 SHORTCUTS_COL_REMOVABLE, &removable,
2831 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2832 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2835 /* GtkWidget::drag-begin handler for the shortcuts list. */
2837 shortcuts_drag_begin_cb (GtkWidget *widget,
2838 GdkDragContext *context,
2839 GtkFileChooserDefault *impl)
2842 impl->shortcuts_drag_context = g_object_ref (context);
2847 /* Removes the idle handler for outside drags */
2849 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2851 if (!impl->shortcuts_drag_outside_idle)
2854 g_source_destroy (impl->shortcuts_drag_outside_idle);
2855 impl->shortcuts_drag_outside_idle = NULL;
2859 /* GtkWidget::drag-end handler for the shortcuts list. */
2861 shortcuts_drag_end_cb (GtkWidget *widget,
2862 GdkDragContext *context,
2863 GtkFileChooserDefault *impl)
2866 g_object_unref (impl->shortcuts_drag_context);
2868 shortcuts_cancel_drag_outside_idle (impl);
2870 if (!impl->shortcuts_drag_outside)
2873 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2875 impl->shortcuts_drag_outside = FALSE;
2879 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2881 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2882 GdkDragContext *context,
2883 GtkFileChooserDefault *impl)
2885 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2888 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2892 shortcuts_drag_leave_cb (GtkWidget *widget,
2893 GdkDragContext *context,
2895 GtkFileChooserDefault *impl)
2898 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2900 impl->shortcuts_drag_outside_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (shortcuts_drag_outside_idle_cb));
2904 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2906 GTK_TREE_VIEW_DROP_BEFORE);
2908 g_signal_stop_emission_by_name (widget, "drag-leave");
2911 /* Computes the appropriate row and position for dropping */
2913 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2917 GtkTreeViewDropPosition *pos)
2919 GtkTreeView *tree_view;
2920 GtkTreeViewColumn *column;
2924 int bookmarks_index;
2925 int header_height = 0;
2927 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2929 if (gtk_tree_view_get_headers_visible (tree_view))
2930 header_height = _gtk_tree_view_get_header_height (tree_view);
2932 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2934 if (!gtk_tree_view_get_path_at_pos (tree_view,
2942 row = bookmarks_index + impl->num_bookmarks - 1;
2943 *path = gtk_tree_path_new_from_indices (row, -1);
2944 *pos = GTK_TREE_VIEW_DROP_AFTER;
2948 row = *gtk_tree_path_get_indices (*path);
2949 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2950 gtk_tree_path_free (*path);
2952 if (row < bookmarks_index)
2954 row = bookmarks_index;
2955 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2957 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2959 row = bookmarks_index + impl->num_bookmarks - 1;
2960 *pos = GTK_TREE_VIEW_DROP_AFTER;
2964 if (cell_y < cell.height / 2)
2965 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2967 *pos = GTK_TREE_VIEW_DROP_AFTER;
2970 *path = gtk_tree_path_new_from_indices (row, -1);
2973 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2974 * implement the destination side of DnD by hand, due to limitations in
2975 * GtkTreeView's DnD API.
2978 shortcuts_drag_motion_cb (GtkWidget *widget,
2979 GdkDragContext *context,
2983 GtkFileChooserDefault *impl)
2986 GtkTreeViewDropPosition pos;
2987 GdkDragAction action;
2990 if (gtk_drag_get_source_widget (context) == widget)
2992 shortcuts_cancel_drag_outside_idle (impl);
2994 if (impl->shortcuts_drag_outside)
2996 shortcuts_drag_set_delete_cursor (impl, FALSE);
2997 impl->shortcuts_drag_outside = FALSE;
3002 if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_COPY ||
3003 (gdk_drag_context_get_actions (context) & GDK_ACTION_COPY) != 0)
3004 action = GDK_ACTION_COPY;
3005 else if (gdk_drag_context_get_suggested_action (context) == GDK_ACTION_MOVE ||
3006 (gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
3007 action = GDK_ACTION_MOVE;
3014 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3015 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3016 gtk_tree_path_free (path);
3020 g_signal_stop_emission_by_name (widget, "drag-motion");
3024 gdk_drag_status (context, action, time_);
3031 /* GtkWidget::drag-drop handler for the shortcuts list. */
3033 shortcuts_drag_drop_cb (GtkWidget *widget,
3034 GdkDragContext *context,
3038 GtkFileChooserDefault *impl)
3041 shortcuts_cancel_drag_outside_idle (impl);
3044 g_signal_stop_emission_by_name (widget, "drag-drop");
3048 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3050 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3051 GtkSelectionData *selection_data,
3057 uris = gtk_selection_data_get_uris (selection_data);
3061 for (i = 0; uris[i]; i++)
3067 file = g_file_new_for_uri (uri);
3069 if (shortcuts_add_bookmark_from_file (impl, file, position))
3072 g_object_unref (file);
3078 /* Reorders the selected bookmark to the specified position */
3080 shortcuts_reorder (GtkFileChooserDefault *impl,
3085 ShortcutType shortcut_type;
3088 int bookmarks_index;
3093 /* Get the selected path */
3095 if (!shortcuts_get_selected (impl, &iter))
3096 g_assert_not_reached ();
3098 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3099 old_position = *gtk_tree_path_get_indices (path);
3100 gtk_tree_path_free (path);
3102 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3103 old_position -= bookmarks_index;
3104 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3106 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3107 SHORTCUTS_COL_NAME, &name,
3108 SHORTCUTS_COL_DATA, &col_data,
3109 SHORTCUTS_COL_TYPE, &shortcut_type,
3111 g_assert (col_data != NULL);
3112 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3115 g_object_ref (file); /* removal below will free file, so we need a new ref */
3117 /* Remove the path from the old position and insert it in the new one */
3119 if (new_position > old_position)
3122 if (old_position == new_position)
3126 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3128 shortcuts_add_bookmark_from_file (impl, file, new_position);
3129 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3132 error_adding_bookmark_dialog (impl, file, error);
3136 g_object_unref (file);
3140 /* Callback used when we get the drag data for the bookmarks list. We add the
3141 * received URIs as bookmarks if they are folders.
3144 shortcuts_drag_data_received_cb (GtkWidget *widget,
3145 GdkDragContext *context,
3148 GtkSelectionData *selection_data,
3153 GtkFileChooserDefault *impl;
3154 GtkTreePath *tree_path;
3155 GtkTreeViewDropPosition tree_pos;
3158 int bookmarks_index;
3160 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3162 /* Compute position */
3164 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3166 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3167 position = *gtk_tree_path_get_indices (tree_path);
3168 gtk_tree_path_free (tree_path);
3170 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3173 g_assert (position >= bookmarks_index);
3174 position -= bookmarks_index;
3176 target = gtk_selection_data_get_target (selection_data);
3178 if (gtk_targets_include_uri (&target, 1))
3179 shortcuts_drop_uris (impl, selection_data, position);
3180 else if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3181 shortcuts_reorder (impl, position);
3183 g_signal_stop_emission_by_name (widget, "drag-data-received");
3186 /* Callback used to display a tooltip in the shortcuts tree */
3188 shortcuts_query_tooltip_cb (GtkWidget *widget,
3191 gboolean keyboard_mode,
3192 GtkTooltip *tooltip,
3193 GtkFileChooserDefault *impl)
3195 GtkTreeModel *model;
3198 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3206 ShortcutType shortcut_type;
3208 gtk_tree_model_get (model, &iter,
3209 SHORTCUTS_COL_DATA, &col_data,
3210 SHORTCUTS_COL_TYPE, &shortcut_type,
3213 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3215 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3217 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3222 file = G_FILE (col_data);
3223 parse_name = g_file_get_parse_name (file);
3225 gtk_tooltip_set_text (tooltip, parse_name);
3227 g_free (parse_name);
3231 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3235 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3245 /* Callback used when the selection in the shortcuts tree changes */
3247 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3248 GtkFileChooserDefault *impl)
3251 GtkTreeIter child_iter;
3253 bookmarks_check_remove_sensitivity (impl);
3254 shortcuts_check_popup_sensitivity (impl);
3256 if (impl->changing_folder)
3259 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3261 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3264 shortcuts_activate_iter (impl, &child_iter);
3269 shortcuts_row_separator_func (GtkTreeModel *model,
3273 ShortcutType shortcut_type;
3275 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3277 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3281 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3283 GtkFileChooserDefault *impl)
3287 /* don't screw up focus switching with Tab */
3288 if (event->keyval == GDK_KEY_Tab
3289 || event->keyval == GDK_KEY_KP_Tab
3290 || event->keyval == GDK_KEY_ISO_Left_Tab
3291 || event->length < 1)
3294 if (impl->location_entry)
3295 entry = impl->location_entry;
3296 else if (impl->search_entry)
3297 entry = impl->search_entry;
3303 gtk_widget_grab_focus (entry);
3304 return gtk_widget_event (entry, (GdkEvent *) event);
3310 /* Callback used when the file list's popup menu is detached */
3312 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3315 GtkFileChooserDefault *impl;
3317 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3318 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3320 impl->browse_shortcuts_popup_menu = NULL;
3321 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3322 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3326 remove_shortcut_cb (GtkMenuItem *item,
3327 GtkFileChooserDefault *impl)
3329 remove_selected_bookmarks (impl);
3332 /* Rename the selected bookmark */
3334 rename_selected_bookmark (GtkFileChooserDefault *impl)
3338 GtkTreeViewColumn *column;
3339 GtkCellRenderer *cell;
3342 if (shortcuts_get_selected (impl, &iter))
3344 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3345 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3346 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3347 cell = g_list_nth_data (renderers, 1);
3348 g_list_free (renderers);
3349 g_object_set (cell, "editable", TRUE, NULL);
3350 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3351 path, column, cell, TRUE);
3352 gtk_tree_path_free (path);
3357 rename_shortcut_cb (GtkMenuItem *item,
3358 GtkFileChooserDefault *impl)
3360 rename_selected_bookmark (impl);
3363 /* Constructs the popup menu for the file list if needed */
3365 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3369 if (impl->browse_shortcuts_popup_menu)
3372 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3373 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3374 impl->browse_shortcuts_tree_view,
3375 shortcuts_popup_menu_detach_cb);
3377 item = gtk_image_menu_item_new_with_label (_("Remove"));
3378 impl->browse_shortcuts_popup_menu_remove_item = item;
3379 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3380 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3381 g_signal_connect (item, "activate",
3382 G_CALLBACK (remove_shortcut_cb), impl);
3383 gtk_widget_show (item);
3384 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3386 item = gtk_menu_item_new_with_label (_("Rename..."));
3387 impl->browse_shortcuts_popup_menu_rename_item = item;
3388 g_signal_connect (item, "activate",
3389 G_CALLBACK (rename_shortcut_cb), impl);
3390 gtk_widget_show (item);
3391 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3395 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3397 shortcuts_build_popup_menu (impl);
3398 shortcuts_check_popup_sensitivity (impl);
3402 popup_position_func (GtkMenu *menu,
3406 gpointer user_data);
3409 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3410 GdkEventButton *event)
3412 shortcuts_update_popup_menu (impl);
3414 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3415 NULL, NULL, NULL, NULL,
3416 event->button, event->time);
3419 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3421 popup_position_func, impl->browse_shortcuts_tree_view,
3422 0, GDK_CURRENT_TIME);
3423 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3428 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3430 shortcuts_popup_menu_cb (GtkWidget *widget,
3431 GtkFileChooserDefault *impl)
3433 shortcuts_popup_menu (impl, NULL);
3437 /* Callback used when a button is pressed on the shortcuts list.
3438 * We trap button 3 to bring up a popup menu.
3441 shortcuts_button_press_event_cb (GtkWidget *widget,
3442 GdkEventButton *event,
3443 GtkFileChooserDefault *impl)
3445 static gboolean in_press = FALSE;
3451 if (event->button != 3)
3455 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3461 shortcuts_popup_menu (impl, event);
3466 shortcuts_edited (GtkCellRenderer *cell,
3469 GtkFileChooserDefault *impl)
3475 g_object_set (cell, "editable", FALSE, NULL);
3477 path = gtk_tree_path_new_from_string (path_string);
3478 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3479 g_assert_not_reached ();
3481 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3482 SHORTCUTS_COL_DATA, &shortcut,
3484 gtk_tree_path_free (path);
3486 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3490 shortcuts_editing_canceled (GtkCellRenderer *cell,
3491 GtkFileChooserDefault *impl)
3493 g_object_set (cell, "editable", FALSE, NULL);
3496 /* Creates the widgets for the shortcuts and bookmarks tree */
3498 shortcuts_list_create (GtkFileChooserDefault *impl)
3501 GtkTreeSelection *selection;
3502 GtkTreeViewColumn *column;
3503 GtkCellRenderer *renderer;
3505 /* Target types for dragging a row to/from the shortcuts list */
3506 const GtkTargetEntry tree_model_row_targets[] = {
3507 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3510 /* Scrolled window */
3512 swin = gtk_scrolled_window_new (NULL, NULL);
3513 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3514 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3515 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3517 gtk_widget_show (swin);
3520 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3521 gtk_style_context_add_class (gtk_widget_get_style_context (impl->browse_shortcuts_tree_view),
3522 GTK_STYLE_CLASS_SIDEBAR);
3523 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3524 #ifdef PROFILE_FILE_CHOOSER
3525 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3528 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3530 * 1. user brings up a SAVE dialog
3531 * 2. user clicks on a shortcut in the shortcuts pane
3532 * 3. user starts typing a filename
3534 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3535 * support interactive search. However, we'd rather focus the location entry
3536 * so that the user can type *there*.
3538 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3539 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3540 * if the user starts typing while the focus is in the shortcuts pane.
3542 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3543 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3545 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3546 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3547 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3548 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3549 /* Accessible object name for the file chooser's shortcuts pane */
3550 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3552 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3554 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3556 tree_model_row_targets,
3557 G_N_ELEMENTS (tree_model_row_targets),
3560 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3561 GTK_DEST_DEFAULT_ALL,
3562 tree_model_row_targets,
3563 G_N_ELEMENTS (tree_model_row_targets),
3564 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3565 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3567 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3568 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3569 gtk_tree_selection_set_select_function (selection,
3570 shortcuts_select_func,
3573 g_signal_connect (selection, "changed",
3574 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3576 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3577 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3579 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3580 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3581 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3582 G_CALLBACK (shortcuts_drag_end_cb), impl);
3583 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3584 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3586 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3587 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3588 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3589 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3590 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3591 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3592 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3593 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3595 /* Support tooltips */
3596 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3597 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3598 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3600 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3601 gtk_widget_show (impl->browse_shortcuts_tree_view);
3605 column = gtk_tree_view_column_new ();
3606 /* Column header for the file chooser's shortcuts pane */
3607 gtk_tree_view_column_set_title (column, _("_Places"));
3609 renderer = gtk_cell_renderer_pixbuf_new ();
3610 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3611 gtk_tree_view_column_set_attributes (column, renderer,
3612 "pixbuf", SHORTCUTS_COL_PIXBUF,
3613 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3616 renderer = gtk_cell_renderer_text_new ();
3617 g_object_set (renderer,
3619 "ellipsize", PANGO_ELLIPSIZE_END,
3621 g_signal_connect (renderer, "edited",
3622 G_CALLBACK (shortcuts_edited), impl);
3623 g_signal_connect (renderer, "editing-canceled",
3624 G_CALLBACK (shortcuts_editing_canceled), impl);
3625 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3626 gtk_tree_view_column_set_attributes (column, renderer,
3627 "text", SHORTCUTS_COL_NAME,
3630 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3631 shortcuts_row_separator_func,
3634 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3639 /* Creates the widgets for the shortcuts/bookmarks pane */
3641 shortcuts_pane_create (GtkFileChooserDefault *impl,
3642 GtkSizeGroup *size_group)
3647 GtkStyleContext *context;
3650 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
3651 gtk_widget_show (vbox);
3653 /* Shortcuts tree */
3655 widget = shortcuts_list_create (impl);
3657 gtk_size_group_add_widget (size_group, widget);
3658 context = gtk_widget_get_style_context (widget);
3659 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
3661 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3663 /* Box for buttons */
3665 toolbar = gtk_toolbar_new ();
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;
3729 impl = (GtkFileChooserDefault *) data;
3731 modifiers = gtk_accelerator_get_default_mod_mask ();
3733 if ((event->keyval == GDK_KEY_slash
3734 || event->keyval == GDK_KEY_KP_Divide
3736 || event->keyval == GDK_KEY_asciitilde
3738 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3740 location_popup_handler (impl, event->string);
3744 if (key_is_left_or_right (event))
3746 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3750 if ((event->keyval == GDK_KEY_Return
3751 || event->keyval == GDK_KEY_ISO_Enter
3752 || event->keyval == GDK_KEY_KP_Enter
3753 || event->keyval == GDK_KEY_space
3754 || event->keyval == GDK_KEY_KP_Space)
3755 && ((event->state & modifiers) == 0)
3756 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3757 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3761 window = get_toplevel (widget);
3764 GtkWidget *default_widget, *focus_widget;
3766 default_widget = gtk_window_get_default_widget (window);
3767 focus_widget = gtk_window_get_focus (window);
3769 if (widget != default_widget &&
3770 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
3772 gtk_window_activate_default (window);
3782 /* Callback used when the file list's popup menu is detached */
3784 popup_menu_detach_cb (GtkWidget *attach_widget,
3787 GtkFileChooserDefault *impl;
3789 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3790 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3792 impl->browse_files_popup_menu = NULL;
3793 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3794 impl->browse_files_popup_menu_hidden_files_item = NULL;
3795 impl->browse_files_popup_menu_copy_file_location_item = NULL;
3798 /* Callback used when the "Add to Bookmarks" menu item is activated */
3800 add_to_shortcuts_cb (GtkMenuItem *item,
3801 GtkFileChooserDefault *impl)
3803 bookmarks_add_selected_folder (impl);
3806 /* callback used to set data to clipboard */
3808 copy_file_get_cb (GtkClipboard *clipboard,
3809 GtkSelectionData *selection_data,
3813 GSList *selected_files = data;
3817 gint num_files = g_slist_length (selected_files);
3822 uris = g_new (gchar *, num_files + 1);
3823 uris[num_files] = NULL; /* null terminator */
3827 for (l = selected_files; l; l = l->next)
3829 GFile *file = (GFile *) l->data;
3831 if (info == SELECTION_URI)
3832 uris[i] = g_file_get_uri (file);
3833 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3834 uris[i] = g_file_get_parse_name (file);
3839 if (info == SELECTION_URI)
3840 gtk_selection_data_set_uris (selection_data, uris);
3841 else /* if (info == SELECTION_TEXT) - let this be the fallback */
3843 char *str = g_strjoinv (" ", uris);
3844 gtk_selection_data_set_text (selection_data, str, -1);
3852 /* callback used to clear the clipboard data */
3854 copy_file_clear_cb (GtkClipboard *clipboard,
3857 GSList *selected_files = data;
3859 g_slist_foreach (selected_files, (GFunc) g_object_unref, NULL);
3860 g_slist_free (selected_files);
3863 /* Callback used when the "Copy file's location" menu item is activated */
3865 copy_file_location_cb (GtkMenuItem *item,
3866 GtkFileChooserDefault *impl)
3868 GSList *selected_files = NULL;
3870 selected_files = search_get_selected_files (impl);
3874 GtkClipboard *clipboard;
3875 GtkTargetList *target_list;
3876 GtkTargetEntry *targets;
3879 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl), GDK_SELECTION_CLIPBOARD);
3881 target_list = gtk_target_list_new (NULL, 0);
3882 gtk_target_list_add_text_targets (target_list, SELECTION_TEXT);
3883 gtk_target_list_add_uri_targets (target_list, SELECTION_URI);
3885 targets = gtk_target_table_new_from_list (target_list, &n_targets);
3886 gtk_target_list_unref (target_list);
3888 gtk_clipboard_set_with_data (clipboard, targets, n_targets,
3893 gtk_target_table_free (targets, n_targets);
3897 /* Callback used when the "Show Hidden Files" menu item is toggled */
3899 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3900 GtkFileChooserDefault *impl)
3903 "show-hidden", gtk_check_menu_item_get_active (item),
3907 /* Callback used when the "Show Size Column" menu item is toggled */
3909 show_size_column_toggled_cb (GtkCheckMenuItem *item,
3910 GtkFileChooserDefault *impl)
3912 impl->show_size_column = gtk_check_menu_item_get_active (item);
3914 gtk_tree_view_column_set_visible (impl->list_size_column,
3915 impl->show_size_column);
3918 /* Shows an error dialog about not being able to select a dragged file */
3920 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3925 _("Could not select file"),
3930 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3935 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3937 for (i = 1; uris[i]; i++)
3940 GError *error = NULL;
3943 file = g_file_new_for_uri (uri);
3945 gtk_file_chooser_default_select_file (chooser, file, &error);
3947 error_selecting_dragged_file_dialog (impl, file, error);
3949 g_object_unref (file);
3953 struct FileListDragData
3955 GtkFileChooserDefault *impl;
3961 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
3963 const GError *error,
3966 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
3967 struct FileListDragData *data = user_data;
3968 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3970 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
3973 data->impl->file_list_drag_data_received_cancellable = NULL;
3975 if (cancelled || error)
3978 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3979 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3980 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
3981 change_folder_and_display_error (data->impl, data->file, FALSE);
3984 GError *error = NULL;
3986 gtk_file_chooser_default_unselect_all (chooser);
3987 gtk_file_chooser_default_select_file (chooser, data->file, &error);
3989 error_selecting_dragged_file_dialog (data->impl, data->file, error);
3991 browse_files_center_selected_row (data->impl);
3994 if (data->impl->select_multiple)
3995 file_list_drag_data_select_uris (data->impl, data->uris);
3998 g_object_unref (data->impl);
3999 g_strfreev (data->uris);
4000 g_object_unref (data->file);
4003 g_object_unref (cancellable);
4007 file_list_drag_data_received_cb (GtkWidget *widget,
4008 GdkDragContext *context,
4011 GtkSelectionData *selection_data,
4016 GtkFileChooserDefault *impl;
4021 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4023 /* Allow only drags from other widgets; see bug #533891. */
4024 if (gtk_drag_get_source_widget (context) == widget)
4026 g_signal_stop_emission_by_name (widget, "drag-data-received");
4030 /* Parse the text/uri-list string, navigate to the first one */
4031 uris = gtk_selection_data_get_uris (selection_data);
4032 if (uris && uris[0])
4034 struct FileListDragData *data;
4037 file = g_file_new_for_uri (uri);
4039 data = g_new0 (struct FileListDragData, 1);
4040 data->impl = g_object_ref (impl);
4044 if (impl->file_list_drag_data_received_cancellable)
4045 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4047 impl->file_list_drag_data_received_cancellable =
4048 _gtk_file_system_get_info (impl->file_system, file,
4050 file_list_drag_data_received_get_info_cb,
4054 g_signal_stop_emission_by_name (widget, "drag-data-received");
4057 /* Don't do anything with the drag_drop signal */
4059 file_list_drag_drop_cb (GtkWidget *widget,
4060 GdkDragContext *context,
4064 GtkFileChooserDefault *impl)
4066 g_signal_stop_emission_by_name (widget, "drag-drop");
4070 /* Disable the normal tree drag motion handler, it makes it look like you're
4071 dropping the dragged item onto a tree item */
4073 file_list_drag_motion_cb (GtkWidget *widget,
4074 GdkDragContext *context,
4078 GtkFileChooserDefault *impl)
4080 g_signal_stop_emission_by_name (widget, "drag-motion");
4084 /* Sensitizes the "Copy file's location" context menu item if there is actually
4085 * a selection active.
4088 check_copy_file_location_sensitivity (GtkFileChooserDefault *impl)
4090 GtkTreeSelection *selection;
4093 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4094 if (gtk_tree_selection_count_selected_rows (selection) == 0)
4099 if (impl->browse_files_popup_menu_copy_file_location_item)
4100 gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_file_location_item, active);
4103 /* Constructs the popup menu for the file list if needed */
4105 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4109 if (impl->browse_files_popup_menu)
4112 impl->browse_files_popup_menu = gtk_menu_new ();
4113 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4114 impl->browse_files_tree_view,
4115 popup_menu_detach_cb);
4117 item = gtk_image_menu_item_new_with_mnemonic (_("_Copy file's location"));
4118 impl->browse_files_popup_menu_copy_file_location_item = item;
4119 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4120 gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU));
4121 g_signal_connect (item, "activate",
4122 G_CALLBACK (copy_file_location_cb), impl);
4123 gtk_widget_show (item);
4124 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4126 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4127 impl->browse_files_popup_menu_add_shortcut_item = item;
4128 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4129 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4130 g_signal_connect (item, "activate",
4131 G_CALLBACK (add_to_shortcuts_cb), impl);
4132 gtk_widget_show (item);
4133 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4135 item = gtk_separator_menu_item_new ();
4136 gtk_widget_show (item);
4137 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4139 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4140 impl->browse_files_popup_menu_hidden_files_item = item;
4141 g_signal_connect (item, "toggled",
4142 G_CALLBACK (show_hidden_toggled_cb), impl);
4143 gtk_widget_show (item);
4144 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4146 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4147 impl->browse_files_popup_menu_size_column_item = item;
4148 g_signal_connect (item, "toggled",
4149 G_CALLBACK (show_size_column_toggled_cb), impl);
4150 gtk_widget_show (item);
4151 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4153 bookmarks_check_add_sensitivity (impl);
4154 check_copy_file_location_sensitivity (impl);
4157 /* Updates the popup menu for the file list, creating it if necessary */
4159 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4161 file_list_build_popup_menu (impl);
4163 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4165 /* The sensitivity of the Add to Bookmarks item is set in
4166 * bookmarks_check_add_sensitivity()
4169 /* 'Show Hidden Files' */
4170 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4171 G_CALLBACK (show_hidden_toggled_cb), impl);
4172 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4174 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4175 G_CALLBACK (show_hidden_toggled_cb), impl);
4177 /* 'Show Size Column' */
4178 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4179 G_CALLBACK (show_size_column_toggled_cb), impl);
4180 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4181 impl->show_size_column);
4182 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4183 G_CALLBACK (show_size_column_toggled_cb), impl);
4187 popup_position_func (GtkMenu *menu,
4193 GtkAllocation allocation;
4194 GtkWidget *widget = GTK_WIDGET (user_data);
4195 GdkScreen *screen = gtk_widget_get_screen (widget);
4198 GdkRectangle monitor;
4200 g_return_if_fail (gtk_widget_get_realized (widget));
4202 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
4204 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
4207 gtk_widget_get_allocation (widget, &allocation);
4208 *x += (allocation.width - req.width) / 2;
4209 *y += (allocation.height - req.height) / 2;
4211 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4212 gtk_menu_set_monitor (menu, monitor_num);
4213 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4215 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4216 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4222 file_list_popup_menu (GtkFileChooserDefault *impl,
4223 GdkEventButton *event)
4225 file_list_update_popup_menu (impl);
4227 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4228 NULL, NULL, NULL, NULL,
4229 event->button, event->time);
4232 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4234 popup_position_func, impl->browse_files_tree_view,
4235 0, GDK_CURRENT_TIME);
4236 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4242 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4244 list_popup_menu_cb (GtkWidget *widget,
4245 GtkFileChooserDefault *impl)
4247 file_list_popup_menu (impl, NULL);
4251 /* Callback used when a button is pressed on the file list. We trap button 3 to
4252 * bring up a popup menu.
4255 list_button_press_event_cb (GtkWidget *widget,
4256 GdkEventButton *event,
4257 GtkFileChooserDefault *impl)
4259 static gboolean in_press = FALSE;
4264 if (event->button != 3)
4268 gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4271 file_list_popup_menu (impl, event);
4276 OperationMode operation_mode;
4277 gint general_column;
4281 /* Sets the sort column IDs for the file list; needs to be done whenever we
4282 * change the model on the treeview.
4285 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4287 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4288 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4289 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4293 file_list_query_tooltip_cb (GtkWidget *widget,
4296 gboolean keyboard_tip,
4297 GtkTooltip *tooltip,
4300 GtkFileChooserDefault *impl = user_data;
4301 GtkTreeModel *model;
4307 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4311 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4314 &model, &path, &iter))
4317 gtk_tree_model_get (model, &iter,
4318 MODEL_COL_FILE, &file,
4323 gtk_tree_path_free (path);
4327 filename = g_file_get_path (file);
4328 gtk_tooltip_set_text (tooltip, filename);
4329 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4334 g_object_unref (file);
4335 gtk_tree_path_free (path);
4341 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4345 gtk_cell_renderer_get_padding (renderer, &xpad, &ypad);
4346 gtk_cell_renderer_set_fixed_size (renderer,
4347 xpad * 2 + impl->icon_size,
4348 ypad * 2 + impl->icon_size);
4351 /* Creates the widgets for the file list */
4353 create_file_list (GtkFileChooserDefault *impl)
4356 GtkTreeSelection *selection;
4357 GtkTreeViewColumn *column;
4358 GtkCellRenderer *renderer;
4360 /* Scrolled window */
4361 swin = gtk_scrolled_window_new (NULL, NULL);
4362 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4363 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4364 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4367 /* Tree/list view */
4369 impl->browse_files_tree_view = gtk_tree_view_new ();
4370 #ifdef PROFILE_FILE_CHOOSER
4371 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4373 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4374 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4376 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4377 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4379 gtk_drag_dest_set (impl->browse_files_tree_view,
4380 GTK_DEST_DEFAULT_ALL,
4382 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4383 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4385 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4386 G_CALLBACK (list_row_activated), impl);
4387 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4388 G_CALLBACK (browse_files_key_press_event_cb), impl);
4389 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4390 G_CALLBACK (list_popup_menu_cb), impl);
4391 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4392 G_CALLBACK (list_button_press_event_cb), impl);
4394 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4395 G_CALLBACK (file_list_drag_data_received_cb), impl);
4396 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4397 G_CALLBACK (file_list_drag_drop_cb), impl);
4398 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4399 G_CALLBACK (file_list_drag_motion_cb), impl);
4401 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4402 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4403 G_CALLBACK (file_list_query_tooltip_cb), impl);
4405 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4406 gtk_tree_selection_set_select_function (selection,
4409 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4412 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4413 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4415 g_signal_connect (selection, "changed",
4416 G_CALLBACK (list_selection_changed), impl);
4418 /* Keep the column order in sync with update_cell_renderer_attributes() */
4420 /* Filename column */
4422 impl->list_name_column = gtk_tree_view_column_new ();
4423 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4424 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4425 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4427 renderer = gtk_cell_renderer_pixbuf_new ();
4428 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4429 set_icon_cell_renderer_fixed_size (impl, renderer);
4430 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4432 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4433 g_object_set (impl->list_name_renderer,
4434 "ellipsize", PANGO_ELLIPSIZE_END,
4436 g_signal_connect (impl->list_name_renderer, "edited",
4437 G_CALLBACK (renderer_edited_cb), impl);
4438 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4439 G_CALLBACK (renderer_editing_canceled_cb), impl);
4440 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4442 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4446 column = gtk_tree_view_column_new ();
4447 gtk_tree_view_column_set_resizable (column, TRUE);
4448 gtk_tree_view_column_set_title (column, _("Size"));
4450 renderer = gtk_cell_renderer_text_new ();
4451 g_object_set (renderer,
4452 "alignment", PANGO_ALIGN_RIGHT,
4454 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4455 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4456 impl->list_size_column = column;
4458 /* Modification time column */
4460 column = gtk_tree_view_column_new ();
4461 gtk_tree_view_column_set_resizable (column, TRUE);
4462 gtk_tree_view_column_set_title (column, _("Modified"));
4464 renderer = gtk_cell_renderer_text_new ();
4465 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4466 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4467 impl->list_mtime_column = column;
4469 file_list_set_sort_column_ids (impl);
4470 update_cell_renderer_attributes (impl);
4472 gtk_widget_show_all (swin);
4477 /* Creates the widgets for the files/folders pane */
4479 file_pane_create (GtkFileChooserDefault *impl,
4480 GtkSizeGroup *size_group)
4486 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
4487 gtk_widget_show (vbox);
4489 /* Box for lists and preview */
4491 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, PREVIEW_HBOX_SPACING);
4492 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4493 gtk_widget_show (hbox);
4497 widget = create_file_list (impl);
4498 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4499 gtk_size_group_add_widget (size_group, widget);
4503 impl->preview_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4504 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4505 /* Don't show preview box initially */
4509 impl->filter_combo_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4511 widget = filter_create (impl);
4513 gtk_widget_show (widget);
4514 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4516 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4522 location_entry_create (GtkFileChooserDefault *impl)
4524 if (!impl->location_entry)
4525 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4527 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4529 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4530 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4531 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4532 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4535 /* Creates the widgets specific to Save mode */
4537 save_widgets_create (GtkFileChooserDefault *impl)
4542 if (impl->save_widgets != NULL)
4545 location_switch_to_path_bar (impl);
4547 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4549 impl->save_widgets_table = gtk_grid_new ();
4550 gtk_box_pack_start (GTK_BOX (vbox), impl->save_widgets_table, FALSE, FALSE, 0);
4551 gtk_widget_show (impl->save_widgets_table);
4552 gtk_grid_set_row_spacing (GTK_GRID (impl->save_widgets_table), 12);
4553 gtk_grid_set_column_spacing (GTK_GRID (impl->save_widgets_table), 12);
4557 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4558 gtk_widget_set_halign (widget, GTK_ALIGN_START);
4559 gtk_widget_set_valign (widget, GTK_ALIGN_CENTER);
4560 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), widget, 0, 0, 1, 1);
4561 gtk_widget_show (widget);
4563 /* Location entry */
4565 location_entry_create (impl);
4566 gtk_widget_set_hexpand (impl->location_entry, TRUE);
4567 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->location_entry, 1, 0, 1, 1);
4568 gtk_widget_show (impl->location_entry);
4569 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4572 impl->save_folder_label = gtk_label_new (NULL);
4573 gtk_widget_set_halign (impl->save_folder_label, GTK_ALIGN_START);
4574 gtk_widget_set_valign (impl->save_folder_label, GTK_ALIGN_CENTER);
4575 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->save_folder_label, 0, 1, 1, 1);
4576 gtk_widget_show (impl->save_folder_label);
4578 impl->save_widgets = vbox;
4579 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4580 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4581 gtk_widget_show (impl->save_widgets);
4584 /* Destroys the widgets specific to Save mode */
4586 save_widgets_destroy (GtkFileChooserDefault *impl)
4588 if (impl->save_widgets == NULL)
4591 gtk_widget_destroy (impl->save_widgets);
4592 impl->save_widgets = NULL;
4593 impl->save_widgets_table = NULL;
4594 impl->location_entry = NULL;
4595 impl->save_folder_label = NULL;
4598 /* Turns on the path bar widget. Can be called even if we are already in that
4602 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4604 if (impl->location_entry)
4606 gtk_widget_destroy (impl->location_entry);
4607 impl->location_entry = NULL;
4610 gtk_widget_hide (impl->location_entry_box);
4613 /* Turns on the location entry. Can be called even if we are already in that
4617 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4619 /* when in search or recent files mode, we are not showing the
4620 * location_entry_box container, so there's no point in switching
4623 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4624 impl->operation_mode == OPERATION_MODE_RECENT)
4627 if (impl->location_entry)
4629 gtk_widget_destroy (impl->location_entry);
4630 impl->location_entry = NULL;
4635 gtk_widget_show (impl->location_entry_box);
4639 location_entry_create (impl);
4640 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4641 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4643 /* Configure the entry */
4645 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4649 gtk_widget_show (impl->location_entry);
4650 gtk_widget_grab_focus (impl->location_entry);
4653 /* Sets a new location mode. set_buttons determines whether the toggle button
4654 * for the mode will also be changed.
4657 location_mode_set (GtkFileChooserDefault *impl,
4658 LocationMode new_mode,
4659 gboolean set_button)
4661 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4662 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4664 GtkWindow *toplevel;
4665 GtkWidget *current_focus;
4666 gboolean button_active;
4667 gboolean switch_to_file_list;
4671 case LOCATION_MODE_PATH_BAR:
4672 button_active = FALSE;
4674 /* The location_entry will disappear when we switch to path bar mode. So,
4675 * we'll focus the file list in that case, to avoid having a window with
4676 * no focused widget.
4678 toplevel = get_toplevel (GTK_WIDGET (impl));
4679 switch_to_file_list = FALSE;
4682 current_focus = gtk_window_get_focus (toplevel);
4683 if (!current_focus || current_focus == impl->location_entry)
4684 switch_to_file_list = TRUE;
4687 location_switch_to_path_bar (impl);
4689 if (switch_to_file_list)
4690 gtk_widget_grab_focus (impl->browse_files_tree_view);
4694 case LOCATION_MODE_FILENAME_ENTRY:
4695 button_active = TRUE;
4696 location_switch_to_filename_entry (impl);
4700 g_assert_not_reached ();
4706 g_signal_handlers_block_by_func (impl->location_button,
4707 G_CALLBACK (location_button_toggled_cb), impl);
4709 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4711 g_signal_handlers_unblock_by_func (impl->location_button,
4712 G_CALLBACK (location_button_toggled_cb), impl);
4716 impl->location_mode = new_mode;
4720 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4722 /* when in search or recent files mode, we are not showing the
4723 * location_entry_box container, so there's no point in switching
4726 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4727 impl->operation_mode == OPERATION_MODE_RECENT)
4730 /* If the file entry is not visible, show it.
4731 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
4733 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
4735 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
4737 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
4739 if (gtk_widget_has_focus (impl->location_entry))
4741 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
4745 gtk_widget_grab_focus (impl->location_entry);
4750 /* Callback used when one of the location mode buttons is toggled */
4752 location_button_toggled_cb (GtkToggleButton *toggle,
4753 GtkFileChooserDefault *impl)
4756 LocationMode new_mode;
4758 is_active = gtk_toggle_button_get_active (toggle);
4762 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4763 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4767 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4768 new_mode = LOCATION_MODE_PATH_BAR;
4771 location_mode_set (impl, new_mode, FALSE);
4774 /* Creates a toggle button for the location entry. */
4776 location_button_create (GtkFileChooserDefault *impl)
4781 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4782 gtk_widget_show (image);
4784 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4788 g_signal_connect (impl->location_button, "toggled",
4789 G_CALLBACK (location_button_toggled_cb), impl);
4791 str = _("Type a file name");
4793 gtk_widget_set_tooltip_text (impl->location_button, str);
4794 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4798 PATH_BAR_FOLDER_PATH,
4799 PATH_BAR_SELECT_A_FOLDER,
4800 PATH_BAR_ERROR_NO_FILENAME,
4801 PATH_BAR_ERROR_NO_FOLDER,
4802 PATH_BAR_RECENTLY_USED,
4806 /* Creates the info bar for informational messages or warnings, with its icon and label */
4808 info_bar_create (GtkFileChooserDefault *impl)
4810 GtkWidget *content_area;
4812 impl->browse_select_a_folder_info_bar = gtk_info_bar_new ();
4813 impl->browse_select_a_folder_icon = gtk_image_new_from_stock (GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU);
4814 impl->browse_select_a_folder_label = gtk_label_new (NULL);
4816 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar));
4818 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_icon, FALSE, FALSE, 0);
4819 gtk_box_pack_start (GTK_BOX (content_area), impl->browse_select_a_folder_label, FALSE, FALSE, 0);
4821 gtk_widget_show (impl->browse_select_a_folder_icon);
4822 gtk_widget_show (impl->browse_select_a_folder_label);
4825 /* Sets the info bar to show the appropriate informational or warning message */
4827 info_bar_set (GtkFileChooserDefault *impl, PathBarMode mode)
4831 GtkMessageType message_type;
4837 case PATH_BAR_SELECT_A_FOLDER:
4838 str = g_strconcat ("<i>", _("Please select a folder below"), "</i>", NULL);
4840 message_type = GTK_MESSAGE_OTHER;
4843 case PATH_BAR_ERROR_NO_FILENAME:
4844 str = _("Please type a file name");
4845 message_type = GTK_MESSAGE_WARNING;
4848 case PATH_BAR_ERROR_NO_FOLDER:
4849 str = _("Please select a folder below");
4850 message_type = GTK_MESSAGE_WARNING;
4854 g_assert_not_reached ();
4858 gtk_info_bar_set_message_type (GTK_INFO_BAR (impl->browse_select_a_folder_info_bar), message_type);
4859 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_select_a_folder_icon),
4860 (message_type == GTK_MESSAGE_WARNING) ? GTK_STOCK_DIALOG_WARNING : GTK_STOCK_DIRECTORY,
4861 GTK_ICON_SIZE_MENU);
4862 gtk_label_set_markup (GTK_LABEL (impl->browse_select_a_folder_label), str);
4868 /* Creates the icon and label used to show that the file chooser is in Search or Recently-used mode */
4870 special_mode_widgets_create (GtkFileChooserDefault *impl)
4872 impl->browse_special_mode_icon = gtk_image_new ();
4873 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_icon);
4874 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_icon, FALSE, FALSE, 0);
4876 impl->browse_special_mode_label = gtk_label_new (NULL);
4877 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_special_mode_label);
4878 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_special_mode_label, FALSE, FALSE, 0);
4881 /* Creates the path bar's container and eveyrthing that goes in it: location button, pathbar, info bar, and Create Folder button */
4883 path_bar_widgets_create (GtkFileChooserDefault *impl)
4885 /* Location widgets - note browse_path_bar_hbox is packed in the right place until switch_path_bar() */
4886 impl->browse_path_bar_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
4887 gtk_widget_show (impl->browse_path_bar_hbox);
4889 /* Size group that allows the path bar to be the same size between modes */
4890 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4891 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
4893 /* Location button */
4894 location_button_create (impl);
4895 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
4896 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
4899 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4900 _gtk_path_bar_set_file_system (GTK_PATH_BAR (impl->browse_path_bar), impl->file_system);
4901 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4903 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_path_bar);
4904 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4907 info_bar_create (impl);
4908 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_select_a_folder_info_bar);
4909 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_select_a_folder_info_bar, TRUE, TRUE, 0);
4911 /* Widgets for special modes (recently-used in Open mode, Search mode) */
4912 special_mode_widgets_create (impl);
4915 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4916 g_signal_connect (impl->browse_new_folder_button, "clicked",
4917 G_CALLBACK (new_folder_button_clicked), impl);
4918 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->browse_new_folder_button);
4919 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4922 /* Sets the path bar's mode to show a label, the actual folder path, or a
4923 * warning message. You may call this function with PATH_BAR_ERROR_* directly
4924 * if the pathbar is already showing the widgets you expect; otherwise, call
4925 * path_bar_update() instead to set the appropriate widgets automatically.
4928 path_bar_set_mode (GtkFileChooserDefault *impl, PathBarMode mode)
4930 gboolean path_bar_visible = FALSE;
4931 gboolean special_mode_widgets_visible = FALSE;
4932 gboolean info_bar_visible = FALSE;
4933 gboolean create_folder_visible = FALSE;
4939 case PATH_BAR_FOLDER_PATH:
4940 path_bar_visible = TRUE;
4943 case PATH_BAR_SELECT_A_FOLDER:
4944 case PATH_BAR_ERROR_NO_FILENAME:
4945 case PATH_BAR_ERROR_NO_FOLDER:
4946 info_bar_set (impl, mode);
4947 info_bar_visible = TRUE;
4950 case PATH_BAR_RECENTLY_USED:
4951 gtk_image_set_from_icon_name (GTK_IMAGE (impl->browse_special_mode_icon), "document-open-recent", GTK_ICON_SIZE_BUTTON);
4953 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
4954 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4957 special_mode_widgets_visible = TRUE;
4960 case PATH_BAR_SEARCH:
4961 gtk_image_set_from_stock (GTK_IMAGE (impl->browse_special_mode_icon), GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
4963 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
4964 gtk_label_set_markup (GTK_LABEL (impl->browse_special_mode_label), tmp);
4967 special_mode_widgets_visible = TRUE;
4971 g_assert_not_reached ();
4974 gtk_widget_set_visible (impl->browse_path_bar, path_bar_visible);
4975 gtk_widget_set_visible (impl->browse_special_mode_icon, special_mode_widgets_visible);
4976 gtk_widget_set_visible (impl->browse_special_mode_label, special_mode_widgets_visible);
4977 gtk_widget_set_visible (impl->browse_select_a_folder_info_bar, info_bar_visible);
4979 if (path_bar_visible)
4981 if (impl->create_folders
4982 && impl->action != GTK_FILE_CHOOSER_ACTION_OPEN
4983 && impl->operation_mode != OPERATION_MODE_RECENT)
4984 create_folder_visible = TRUE;
4987 gtk_widget_set_visible (impl->browse_new_folder_button, create_folder_visible);
4990 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4992 browse_widgets_create (GtkFileChooserDefault *impl)
4996 GtkSizeGroup *size_group;
4998 impl->browse_widgets_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
4999 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets_box, TRUE, TRUE, 0);
5000 gtk_widget_show (impl->browse_widgets_box);
5002 impl->browse_header_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
5003 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), impl->browse_header_box, FALSE, FALSE, 0);
5004 gtk_widget_show (impl->browse_header_box);
5006 /* Path bar, info bar, and their respective machinery - the browse_path_bar_hbox will get packed elsewhere */
5007 path_bar_widgets_create (impl);
5009 /* Box for the location label and entry */
5011 impl->location_entry_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
5012 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->location_entry_box, FALSE, FALSE, 0);
5014 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5015 gtk_widget_show (impl->location_label);
5016 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5018 /* size group is used by the scrolled windows of the panes */
5019 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5023 hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
5024 gtk_widget_show (hpaned);
5025 gtk_box_pack_start (GTK_BOX (impl->browse_widgets_box), hpaned, TRUE, TRUE, 0);
5027 widget = shortcuts_pane_create (impl, size_group);
5028 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5029 widget = file_pane_create (impl, size_group);
5030 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5031 gtk_paned_set_position (GTK_PANED (hpaned), 148);
5032 g_object_unref (size_group);
5036 gtk_file_chooser_default_constructor (GType type,
5037 guint n_construct_properties,
5038 GObjectConstructParam *construct_params)
5040 GtkFileChooserDefault *impl;
5043 profile_start ("start", NULL);
5045 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5046 n_construct_properties,
5048 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5050 g_assert (impl->file_system);
5052 gtk_widget_push_composite_child ();
5054 /* Shortcuts model */
5055 shortcuts_model_create (impl);
5057 /* The browse widgets */
5058 browse_widgets_create (impl);
5060 /* Alignment to hold extra widget */
5061 impl->extra_align = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
5062 gtk_widget_set_halign (impl->extra_align, GTK_ALIGN_START);
5063 gtk_widget_set_valign (impl->extra_align, GTK_ALIGN_CENTER);
5064 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5066 gtk_widget_pop_composite_child ();
5067 update_appearance (impl);
5069 profile_end ("end", NULL);
5074 /* Sets the extra_widget by packing it in the appropriate place */
5076 set_extra_widget (GtkFileChooserDefault *impl,
5077 GtkWidget *extra_widget)
5081 g_object_ref (extra_widget);
5082 /* FIXME: is this right ? */
5083 gtk_widget_show (extra_widget);
5086 if (impl->extra_widget)
5088 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5089 g_object_unref (impl->extra_widget);
5092 impl->extra_widget = extra_widget;
5093 if (impl->extra_widget)
5095 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5096 gtk_widget_show (impl->extra_align);
5099 gtk_widget_hide (impl->extra_align);
5103 set_local_only (GtkFileChooserDefault *impl,
5104 gboolean local_only)
5106 if (local_only != impl->local_only)
5108 impl->local_only = local_only;
5110 if (impl->location_entry)
5111 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5113 if (impl->shortcuts_model && impl->file_system)
5115 shortcuts_add_volumes (impl);
5116 shortcuts_add_bookmarks (impl);
5119 if (local_only && impl->current_folder &&
5120 !g_file_is_native (impl->current_folder))
5122 /* If we are pointing to a non-local folder, make an effort to change
5123 * back to a local folder, but it's really up to the app to not cause
5124 * such a situation, so we ignore errors.
5126 const gchar *home = g_get_home_dir ();
5132 home_file = g_file_new_for_path (home);
5134 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5136 g_object_unref (home_file);
5142 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5143 GtkFileChooserDefault *impl)
5145 shortcuts_add_volumes (impl);
5146 shortcuts_add_bookmarks (impl);
5148 bookmarks_check_add_sensitivity (impl);
5149 bookmarks_check_remove_sensitivity (impl);
5150 shortcuts_check_popup_sensitivity (impl);
5153 /* Sets the file chooser to multiple selection mode */
5155 set_select_multiple (GtkFileChooserDefault *impl,
5156 gboolean select_multiple,
5157 gboolean property_notify)
5159 GtkTreeSelection *selection;
5160 GtkSelectionMode mode;
5162 if (select_multiple == impl->select_multiple)
5165 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5167 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5168 gtk_tree_selection_set_mode (selection, mode);
5170 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5172 impl->select_multiple = select_multiple;
5173 g_object_notify (G_OBJECT (impl), "select-multiple");
5175 check_preview_change (impl);
5179 set_file_system_backend (GtkFileChooserDefault *impl)
5181 profile_start ("start for backend", "default");
5183 impl->file_system = _gtk_file_system_new ();
5185 g_signal_connect (impl->file_system, "volumes-changed",
5186 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5187 g_signal_connect (impl->file_system, "bookmarks-changed",
5188 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5190 profile_end ("end", NULL);
5194 unset_file_system_backend (GtkFileChooserDefault *impl)
5196 g_signal_handlers_disconnect_by_func (impl->file_system,
5197 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5199 g_object_unref (impl->file_system);
5201 impl->file_system = NULL;
5204 /* Saves the widgets around the pathbar so they can be reparented later
5205 * in the correct place. This function must be called paired with
5206 * restore_path_bar().
5209 save_path_bar (GtkFileChooserDefault *impl)
5213 g_object_ref (impl->browse_path_bar_hbox);
5215 parent = gtk_widget_get_parent (impl->browse_path_bar_hbox);
5217 gtk_container_remove (GTK_CONTAINER (parent), impl->browse_path_bar_hbox);
5220 /* Reparents the path bar and the "Create folder" button to the right place:
5221 * Above the file list in Open mode, or to the right of the "Save in folder:"
5222 * label in Save mode. The save_path_bar() function must be called before this
5226 restore_path_bar (GtkFileChooserDefault *impl)
5228 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5229 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5231 gtk_box_pack_start (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5232 gtk_box_reorder_child (GTK_BOX (impl->browse_header_box), impl->browse_path_bar_hbox, 0);
5234 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5235 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5237 gtk_widget_set_hexpand (impl->browse_path_bar_hbox, TRUE);
5238 gtk_grid_attach (GTK_GRID (impl->save_widgets_table), impl->browse_path_bar_hbox, 1, 1, 1, 1);
5239 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->browse_path_bar);
5242 g_assert_not_reached ();
5244 g_object_unref (impl->browse_path_bar_hbox);
5247 /* Takes the folder stored in a row in the recent_model, and puts it in the pathbar */
5249 put_recent_folder_in_pathbar (GtkFileChooserDefault *impl, GtkTreeIter *iter)
5253 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
5254 MODEL_COL_FILE, &file,
5256 _gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), file, FALSE, NULL); /* NULL-GError */
5257 g_object_unref (file);
5260 /* Sets the pathbar in the appropriate mode according to the current operation mode and action. This is the central function for
5261 * dealing with the pathbar's widgets; as long as impl->action and impl->operation_mode are set correctly, then calling this
5262 * function will update all the pathbar's widgets.
5265 path_bar_update (GtkFileChooserDefault *impl)
5269 switch (impl->operation_mode)
5271 case OPERATION_MODE_BROWSE:
5272 mode = PATH_BAR_FOLDER_PATH;
5275 case OPERATION_MODE_RECENT:
5276 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5278 GtkTreeSelection *selection;
5279 gboolean have_selected;
5282 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5284 /* Save mode means single-selection mode, so the following is valid */
5285 have_selected = gtk_tree_selection_get_selected (selection, NULL, &iter);
5289 mode = PATH_BAR_FOLDER_PATH;
5290 put_recent_folder_in_pathbar (impl, &iter);
5293 mode = PATH_BAR_SELECT_A_FOLDER;
5296 mode = PATH_BAR_RECENTLY_USED;
5300 case OPERATION_MODE_SEARCH:
5301 mode = PATH_BAR_SEARCH;
5305 g_assert_not_reached ();
5309 path_bar_set_mode (impl, mode);
5313 operation_mode_discard_search_widgets (GtkFileChooserDefault *impl)
5315 if (impl->search_hbox)
5317 gtk_widget_destroy (impl->search_hbox);
5319 impl->search_hbox = NULL;
5320 impl->search_entry = NULL;
5324 /* Stops running operations like populating the browse model, searches, and the recent-files model */
5326 operation_mode_stop (GtkFileChooserDefault *impl, OperationMode mode)
5330 case OPERATION_MODE_BROWSE:
5331 stop_loading_and_clear_list_model (impl, TRUE);
5334 case OPERATION_MODE_SEARCH:
5335 search_stop_searching (impl, FALSE);
5336 search_clear_model (impl, TRUE);
5338 operation_mode_discard_search_widgets (impl);
5341 case OPERATION_MODE_RECENT:
5342 recent_stop_loading (impl);
5343 recent_clear_model (impl, TRUE);
5347 g_assert_not_reached ();
5352 operation_mode_set_browse (GtkFileChooserDefault *impl)
5354 path_bar_update (impl);
5356 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5357 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5359 gtk_widget_show (impl->location_button);
5360 location_mode_set (impl, impl->location_mode, TRUE);
5362 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5363 gtk_widget_show (impl->location_entry_box);
5368 operation_mode_set_search (GtkFileChooserDefault *impl)
5370 g_assert (impl->search_hbox == NULL);
5371 g_assert (impl->search_entry == NULL);
5372 g_assert (impl->search_model == NULL);
5374 search_setup_widgets (impl);
5378 operation_mode_set_recent (GtkFileChooserDefault *impl)
5380 path_bar_update (impl);
5382 /* Hide the location widgets temporarily */
5383 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5384 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5386 gtk_widget_hide (impl->location_button);
5387 gtk_widget_hide (impl->location_entry_box);
5390 recent_start_loading (impl);
5393 /* Sometimes we need to frob the selection in the shortcuts list manually */
5395 shortcuts_select_item_without_activating (GtkFileChooserDefault *impl, int pos)
5397 GtkTreeSelection *selection;
5400 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
5402 g_signal_handlers_block_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5404 path = gtk_tree_path_new_from_indices (pos, -1);
5405 gtk_tree_selection_select_path (selection, path);
5406 gtk_tree_path_free (path);
5408 g_signal_handlers_unblock_by_func (selection, G_CALLBACK (shortcuts_selection_changed_cb), impl);
5412 operation_mode_set (GtkFileChooserDefault *impl, OperationMode mode)
5414 ShortcutsIndex shortcut_to_select;
5416 operation_mode_stop (impl, impl->operation_mode);
5418 impl->operation_mode = mode;
5420 switch (impl->operation_mode)
5422 case OPERATION_MODE_BROWSE:
5423 operation_mode_set_browse (impl);
5424 shortcut_to_select = SHORTCUTS_CURRENT_FOLDER;
5427 case OPERATION_MODE_SEARCH:
5428 operation_mode_set_search (impl);
5429 shortcut_to_select = SHORTCUTS_SEARCH;
5432 case OPERATION_MODE_RECENT:
5433 operation_mode_set_recent (impl);
5434 shortcut_to_select = SHORTCUTS_RECENT;
5438 g_assert_not_reached ();
5442 if (shortcut_to_select != SHORTCUTS_CURRENT_FOLDER)
5443 shortcuts_select_item_without_activating (impl, shortcuts_get_index (impl, shortcut_to_select));
5446 /* This function is basically a do_all function.
5448 * It sets the visibility on all the widgets based on the current state, and
5449 * moves the custom_widget if needed.
5452 update_appearance (GtkFileChooserDefault *impl)
5454 save_path_bar (impl);
5456 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5457 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5461 gtk_widget_hide (impl->location_button);
5462 save_widgets_create (impl);
5464 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5465 text = _("Save in _folder:");
5467 text = _("Create in _folder:");
5469 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5471 if (impl->select_multiple)
5473 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5474 "Re-setting to single selection mode.");
5475 set_select_multiple (impl, FALSE, TRUE);
5478 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5479 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5481 gtk_widget_show (impl->location_button);
5482 save_widgets_destroy (impl);
5483 location_mode_set (impl, impl->location_mode, TRUE);
5486 if (impl->location_entry)
5487 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5489 restore_path_bar (impl);
5490 path_bar_update (impl);
5492 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5493 * of files may change depending whether we are in a file or folder-only mode.
5495 gtk_widget_queue_draw (impl->browse_files_tree_view);
5497 emit_default_size_changed (impl);
5501 gtk_file_chooser_default_set_property (GObject *object,
5503 const GValue *value,
5507 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5511 case GTK_FILE_CHOOSER_PROP_ACTION:
5513 GtkFileChooserAction action = g_value_get_enum (value);
5515 if (action != impl->action)
5517 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5519 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5520 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5521 && impl->select_multiple)
5523 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5524 "this is not allowed in multiple selection mode. Resetting the file chooser "
5525 "to single selection mode.");
5526 set_select_multiple (impl, FALSE, TRUE);
5528 impl->action = action;
5529 update_cell_renderer_attributes (impl);
5530 update_appearance (impl);
5531 settings_load (impl);
5536 case GTK_FILE_CHOOSER_PROP_FILTER:
5537 set_current_filter (impl, g_value_get_object (value));
5540 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5541 set_local_only (impl, g_value_get_boolean (value));
5544 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5545 set_preview_widget (impl, g_value_get_object (value));
5548 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5549 impl->preview_widget_active = g_value_get_boolean (value);
5550 update_preview_widget_visibility (impl);
5553 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5554 impl->use_preview_label = g_value_get_boolean (value);
5555 update_preview_widget_visibility (impl);
5558 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5559 set_extra_widget (impl, g_value_get_object (value));
5562 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5564 gboolean select_multiple = g_value_get_boolean (value);
5565 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5566 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5569 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5570 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5571 "leaving the file chooser in single selection mode.");
5575 set_select_multiple (impl, select_multiple, FALSE);
5579 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5581 gboolean show_hidden = g_value_get_boolean (value);
5582 if (show_hidden != impl->show_hidden)
5584 impl->show_hidden = show_hidden;
5586 if (impl->browse_files_model)
5587 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5592 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5594 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5595 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5599 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5601 gboolean create_folders = g_value_get_boolean (value);
5602 impl->create_folders = create_folders;
5603 update_appearance (impl);
5608 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5614 gtk_file_chooser_default_get_property (GObject *object,
5619 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5623 case GTK_FILE_CHOOSER_PROP_ACTION:
5624 g_value_set_enum (value, impl->action);
5627 case GTK_FILE_CHOOSER_PROP_FILTER:
5628 g_value_set_object (value, impl->current_filter);
5631 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5632 g_value_set_boolean (value, impl->local_only);
5635 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5636 g_value_set_object (value, impl->preview_widget);
5639 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5640 g_value_set_boolean (value, impl->preview_widget_active);
5643 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5644 g_value_set_boolean (value, impl->use_preview_label);
5647 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5648 g_value_set_object (value, impl->extra_widget);
5651 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5652 g_value_set_boolean (value, impl->select_multiple);
5655 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5656 g_value_set_boolean (value, impl->show_hidden);
5659 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5660 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5663 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5664 g_value_set_boolean (value, impl->create_folders);
5668 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5673 /* This cancels everything that may be going on in the background. */
5675 cancel_all_operations (GtkFileChooserDefault *impl)
5679 pending_select_files_free (impl);
5681 /* cancel all pending operations */
5682 if (impl->pending_cancellables)
5684 for (l = impl->pending_cancellables; l; l = l->next)
5686 GCancellable *cancellable = G_CANCELLABLE (l->data);
5687 g_cancellable_cancel (cancellable);
5689 g_slist_free (impl->pending_cancellables);
5690 impl->pending_cancellables = NULL;
5693 if (impl->reload_icon_cancellables)
5695 for (l = impl->reload_icon_cancellables; l; l = l->next)
5697 GCancellable *cancellable = G_CANCELLABLE (l->data);
5698 g_cancellable_cancel (cancellable);
5700 g_slist_free (impl->reload_icon_cancellables);
5701 impl->reload_icon_cancellables = NULL;
5704 if (impl->loading_shortcuts)
5706 for (l = impl->loading_shortcuts; l; l = l->next)
5708 GCancellable *cancellable = G_CANCELLABLE (l->data);
5709 g_cancellable_cancel (cancellable);
5711 g_slist_free (impl->loading_shortcuts);
5712 impl->loading_shortcuts = NULL;
5715 if (impl->file_list_drag_data_received_cancellable)
5717 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5718 impl->file_list_drag_data_received_cancellable = NULL;
5721 if (impl->update_current_folder_cancellable)
5723 g_cancellable_cancel (impl->update_current_folder_cancellable);
5724 impl->update_current_folder_cancellable = NULL;
5727 if (impl->should_respond_get_info_cancellable)
5729 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5730 impl->should_respond_get_info_cancellable = NULL;
5733 if (impl->update_from_entry_cancellable)
5735 g_cancellable_cancel (impl->update_from_entry_cancellable);
5736 impl->update_from_entry_cancellable = NULL;
5739 if (impl->shortcuts_activate_iter_cancellable)
5741 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5742 impl->shortcuts_activate_iter_cancellable = NULL;
5745 search_stop_searching (impl, TRUE);
5746 recent_stop_loading (impl);
5749 /* Removes the settings signal handler. It's safe to call multiple times */
5751 remove_settings_signal (GtkFileChooserDefault *impl,
5754 if (impl->settings_signal_id)
5756 GtkSettings *settings;
5758 settings = gtk_settings_get_for_screen (screen);
5759 g_signal_handler_disconnect (settings,
5760 impl->settings_signal_id);
5761 impl->settings_signal_id = 0;
5766 gtk_file_chooser_default_dispose (GObject *object)
5768 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5770 cancel_all_operations (impl);
5772 if (impl->extra_widget)
5774 g_object_unref (impl->extra_widget);
5775 impl->extra_widget = NULL;
5778 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5780 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5783 /* We override show-all since we have internal widgets that
5784 * shouldn't be shown when you call show_all(), like the filter
5788 gtk_file_chooser_default_show_all (GtkWidget *widget)
5790 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5792 gtk_widget_show (widget);
5794 if (impl->extra_widget)
5795 gtk_widget_show_all (impl->extra_widget);
5798 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5799 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5802 toplevel_set_focus_cb (GtkWindow *window,
5804 GtkFileChooserDefault *impl)
5806 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5809 /* We monitor the focus widget on our toplevel to be able to know which widget
5810 * was last focused at the time our "should_respond" method gets called.
5813 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5814 GtkWidget *previous_toplevel)
5816 GtkFileChooserDefault *impl;
5817 GtkWidget *toplevel;
5819 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5820 toplevel = gtk_widget_get_toplevel (widget);
5822 if (previous_toplevel &&
5823 impl->toplevel_set_focus_id != 0)
5825 g_signal_handler_disconnect (previous_toplevel,
5826 impl->toplevel_set_focus_id);
5827 impl->toplevel_set_focus_id = 0;
5828 impl->toplevel_last_focus_widget = NULL;
5831 if (gtk_widget_is_toplevel (toplevel))
5833 g_assert (impl->toplevel_set_focus_id == 0);
5834 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5835 G_CALLBACK (toplevel_set_focus_cb), impl);
5836 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5840 /* Changes the icons wherever it is needed */
5842 change_icon_theme (GtkFileChooserDefault *impl)
5844 GtkSettings *settings;
5846 GtkCellRenderer *renderer;
5849 profile_start ("start", NULL);
5851 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5853 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5854 impl->icon_size = MAX (width, height);
5856 impl->icon_size = FALLBACK_ICON_SIZE;
5858 shortcuts_reload_icons (impl);
5859 /* the first cell in the first column is the icon column, and we have a fixed size there */
5860 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5861 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5862 renderer = GTK_CELL_RENDERER (cells->data);
5863 set_icon_cell_renderer_fixed_size (impl, renderer);
5864 g_list_free (cells);
5865 if (impl->browse_files_model)
5866 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5867 gtk_widget_queue_resize (impl->browse_files_tree_view);
5869 profile_end ("end", NULL);
5872 /* Callback used when a GtkSettings value changes */
5874 settings_notify_cb (GObject *object,
5876 GtkFileChooserDefault *impl)
5880 profile_start ("start", NULL);
5882 name = g_param_spec_get_name (pspec);
5884 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5885 strcmp (name, "gtk-icon-sizes") == 0)
5886 change_icon_theme (impl);
5888 profile_end ("end", NULL);
5891 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5895 check_icon_theme (GtkFileChooserDefault *impl)
5897 GtkSettings *settings;
5899 profile_start ("start", NULL);
5901 if (impl->settings_signal_id)
5903 profile_end ("end", NULL);
5907 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5909 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5910 impl->settings_signal_id = g_signal_connect (settings, "notify",
5911 G_CALLBACK (settings_notify_cb), impl);
5913 change_icon_theme (impl);
5916 profile_end ("end", NULL);
5920 gtk_file_chooser_default_style_updated (GtkWidget *widget)
5922 GtkFileChooserDefault *impl;
5924 profile_start ("start", NULL);
5926 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5928 profile_msg (" parent class style_udpated start", NULL);
5929 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_updated (widget);
5930 profile_msg (" parent class style_updated end", NULL);
5932 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5933 change_icon_theme (impl);
5935 emit_default_size_changed (impl);
5937 profile_end ("end", NULL);
5941 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5942 GdkScreen *previous_screen)
5944 GtkFileChooserDefault *impl;
5946 profile_start ("start", NULL);
5948 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5950 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5951 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5953 remove_settings_signal (impl, previous_screen);
5954 check_icon_theme (impl);
5956 emit_default_size_changed (impl);
5958 profile_end ("end", NULL);
5962 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5963 GtkAllocation *allocation)
5965 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5969 set_sort_column (GtkFileChooserDefault *impl)
5971 GtkTreeSortable *sortable;
5973 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5975 /* can happen when we're still populating the model */
5976 if (sortable == NULL)
5979 gtk_tree_sortable_set_sort_column_id (sortable,
5985 settings_ensure (GtkFileChooserDefault *impl)
5987 if (impl->settings != NULL)
5990 impl->settings = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
5991 "/org/gtk/settings/file-chooser/");
5992 g_settings_delay (impl->settings);
5996 settings_load (GtkFileChooserDefault *impl)
5998 LocationMode location_mode;
5999 gboolean show_hidden;
6000 gboolean show_size_column;
6002 GtkSortType sort_order;
6004 settings_ensure (impl);
6006 location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE);
6007 show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN);
6008 show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
6009 sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN);
6010 sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER);
6012 location_mode_set (impl, location_mode, TRUE);
6014 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
6016 impl->show_size_column = show_size_column;
6017 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
6019 impl->sort_column = sort_column;
6020 impl->sort_order = sort_order;
6021 /* We don't call set_sort_column() here as the models may not have been
6022 * created yet. The individual functions that create and set the models will
6023 * call set_sort_column() themselves.
6028 save_dialog_geometry (GtkFileChooserDefault *impl)
6030 GtkWindow *toplevel;
6031 int x, y, width, height;
6033 toplevel = get_toplevel (GTK_WIDGET (impl));
6035 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
6038 gtk_window_get_position (toplevel, &x, &y);
6039 gtk_window_get_size (toplevel, &width, &height);
6041 g_settings_set (impl->settings, "window-position", "(ii)", x, y);
6042 g_settings_set (impl->settings, "window-size", "(ii)", width, height);
6046 settings_save (GtkFileChooserDefault *impl)
6048 char *current_folder_uri;
6050 settings_ensure (impl);
6052 /* Current folder */
6054 if (impl->current_folder)
6055 current_folder_uri = g_file_get_uri (impl->current_folder);
6057 current_folder_uri = "";
6059 g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
6061 if (impl->current_folder)
6062 g_free (current_folder_uri);
6064 /* All the other state */
6066 g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
6067 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN,
6068 gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6069 g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
6070 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
6071 g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
6073 save_dialog_geometry (impl);
6075 /* Now apply the settings */
6076 g_settings_apply (impl->settings);
6078 g_object_unref (impl->settings);
6079 impl->settings = NULL;
6082 /* GtkWidget::realize method */
6084 gtk_file_chooser_default_realize (GtkWidget *widget)
6086 GtkFileChooserDefault *impl;
6088 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6090 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6092 emit_default_size_changed (impl);
6096 get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
6098 char *last_folder_uri;
6101 settings_ensure (impl);
6103 last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI);
6105 /* If no last folder is set, we use the user's home directory, since
6106 * this is the starting point for most documents.
6108 if (last_folder_uri[0] == '\0')
6109 file = g_file_new_for_path (g_get_home_dir ());
6111 file = g_file_new_for_uri (last_folder_uri);
6113 g_free (last_folder_uri);
6118 /* GtkWidget::map method */
6120 gtk_file_chooser_default_map (GtkWidget *widget)
6122 GtkFileChooserDefault *impl;
6124 profile_start ("start", NULL);
6126 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6128 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6130 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6132 switch (impl->reload_state)
6135 recent_shortcut_handler (impl);
6138 case RELOAD_HAS_FOLDER:
6139 /* Nothing; we are already loading or loaded, so we
6140 * don't need to reload
6145 g_assert_not_reached ();
6149 volumes_bookmarks_changed_cb (impl->file_system, impl);
6151 settings_load (impl);
6153 profile_end ("end", NULL);
6156 /* GtkWidget::unmap method */
6158 gtk_file_chooser_default_unmap (GtkWidget *widget)
6160 GtkFileChooserDefault *impl;
6162 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6164 settings_save (impl);
6166 cancel_all_operations (impl);
6167 impl->reload_state = RELOAD_EMPTY;
6169 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6173 install_list_model_filter (GtkFileChooserDefault *impl)
6175 _gtk_file_system_model_set_filter (impl->browse_files_model,
6176 impl->current_filter);
6179 #define COMPARE_DIRECTORIES \
6180 GtkFileChooserDefault *impl = user_data; \
6181 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6182 gboolean dir_a, dir_b; \
6184 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6185 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6187 if (dir_a != dir_b) \
6188 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6190 /* Sort callback for the filename column */
6192 name_sort_func (GtkTreeModel *model,
6197 COMPARE_DIRECTORIES;
6200 const char *key_a, *key_b;
6203 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6204 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6207 result = strcmp (key_a, key_b);
6219 /* Sort callback for the size column */
6221 size_sort_func (GtkTreeModel *model,
6226 COMPARE_DIRECTORIES;
6229 gint64 size_a, size_b;
6231 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6232 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6234 return size_a < size_b ? -1 : (size_a == size_b ? 0 : 1);
6238 /* Sort callback for the mtime column */
6240 mtime_sort_func (GtkTreeModel *model,
6245 COMPARE_DIRECTORIES;
6250 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6251 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6253 return ta < tb ? -1 : (ta == tb ? 0 : 1);
6257 /* Callback used when the sort column changes. We cache the sort order for use
6258 * in name_sort_func().
6261 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6262 GtkFileChooserDefault *impl)
6264 gint sort_column_id;
6265 GtkSortType sort_type;
6267 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6269 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6270 impl->sort_column = sort_column_id;
6271 impl->sort_order = sort_type;
6276 set_busy_cursor (GtkFileChooserDefault *impl,
6280 GtkWindow *toplevel;
6281 GdkDisplay *display;
6284 toplevel = get_toplevel (GTK_WIDGET (impl));
6285 widget = GTK_WIDGET (toplevel);
6286 if (!toplevel || !gtk_widget_get_realized (widget))
6289 display = gtk_widget_get_display (widget);
6292 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6296 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
6297 gdk_display_flush (display);
6300 g_object_unref (cursor);
6303 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6305 load_set_model (GtkFileChooserDefault *impl)
6307 profile_start ("start", NULL);
6309 g_assert (impl->browse_files_model != NULL);
6311 profile_msg (" gtk_tree_view_set_model start", NULL);
6312 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6313 GTK_TREE_MODEL (impl->browse_files_model));
6314 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6315 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6317 file_list_set_sort_column_ids (impl);
6318 set_sort_column (impl);
6319 profile_msg (" gtk_tree_view_set_model end", NULL);
6320 impl->list_sort_ascending = TRUE;
6322 profile_end ("end", NULL);
6325 /* Timeout callback used when the loading timer expires */
6327 load_timeout_cb (gpointer data)
6329 GtkFileChooserDefault *impl;
6331 profile_start ("start", NULL);
6333 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6334 g_assert (impl->load_state == LOAD_PRELOAD);
6335 g_assert (impl->load_timeout_id != 0);
6336 g_assert (impl->browse_files_model != NULL);
6338 impl->load_timeout_id = 0;
6339 impl->load_state = LOAD_LOADING;
6341 load_set_model (impl);
6343 profile_end ("end", NULL);
6348 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6350 load_setup_timer (GtkFileChooserDefault *impl)
6352 g_assert (impl->load_timeout_id == 0);
6353 g_assert (impl->load_state != LOAD_PRELOAD);
6355 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6356 impl->load_state = LOAD_PRELOAD;
6359 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6361 load_remove_timer (GtkFileChooserDefault *impl)
6363 if (impl->load_timeout_id != 0)
6365 g_assert (impl->load_state == LOAD_PRELOAD);
6367 g_source_remove (impl->load_timeout_id);
6368 impl->load_timeout_id = 0;
6369 impl->load_state = LOAD_EMPTY;
6372 g_assert (impl->load_state == LOAD_EMPTY ||
6373 impl->load_state == LOAD_LOADING ||
6374 impl->load_state == LOAD_FINISHED);
6377 /* Selects the first row in the file list */
6379 browse_files_select_first_row (GtkFileChooserDefault *impl)
6382 GtkTreeIter dummy_iter;
6383 GtkTreeModel *tree_model;
6385 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6390 path = gtk_tree_path_new_from_indices (0, -1);
6392 /* If the list is empty, do nothing. */
6393 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6394 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6396 gtk_tree_path_free (path);
6399 struct center_selected_row_closure {
6400 GtkFileChooserDefault *impl;
6401 gboolean already_centered;
6404 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6405 * selected row in the tree view.
6408 center_selected_row_foreach_cb (GtkTreeModel *model,
6413 struct center_selected_row_closure *closure;
6416 if (closure->already_centered)
6419 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6420 closure->already_centered = TRUE;
6423 /* Centers the selected row in the tree view */
6425 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6427 struct center_selected_row_closure closure;
6428 GtkTreeSelection *selection;
6430 closure.impl = impl;
6431 closure.already_centered = FALSE;
6433 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6434 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6438 show_and_select_files (GtkFileChooserDefault *impl,
6441 GtkTreeSelection *selection;
6442 GtkFileSystemModel *fsmodel;
6443 gboolean enabled_hidden, removed_filters;
6444 gboolean selected_a_file;
6447 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6448 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6450 enabled_hidden = impl->show_hidden;
6451 removed_filters = (impl->current_filter == NULL);
6453 selected_a_file = FALSE;
6455 for (walk = files; walk; walk = walk->next)
6457 GFile *file = walk->data;
6460 /* Is it a hidden file? */
6462 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6465 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6467 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6469 if (!enabled_hidden &&
6470 (g_file_info_get_is_hidden (info) ||
6471 g_file_info_get_is_backup (info)))
6473 g_object_set (impl, "show-hidden", TRUE, NULL);
6474 enabled_hidden = TRUE;
6478 /* Is it a filtered file? */
6480 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6481 continue; /* re-get the iter as it may change when the model refilters */
6483 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6485 /* Maybe we should have a way to ask the fsmodel if it had filtered a file */
6486 if (!removed_filters)
6488 set_current_filter (impl, NULL);
6489 removed_filters = TRUE;
6493 /* Okay, can we select the file now? */
6495 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6498 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6502 gtk_tree_selection_select_iter (selection, &iter);
6504 path = gtk_tree_model_get_path (GTK_TREE_MODEL (fsmodel), &iter);
6505 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
6507 gtk_tree_path_free (path);
6509 selected_a_file = TRUE;
6513 browse_files_center_selected_row (impl);
6515 return selected_a_file;
6518 /* Processes the pending operation when a folder is finished loading */
6520 pending_select_files_process (GtkFileChooserDefault *impl)
6522 g_assert (impl->load_state == LOAD_FINISHED);
6523 g_assert (impl->browse_files_model != NULL);
6525 if (impl->pending_select_files)
6527 show_and_select_files (impl, impl->pending_select_files);
6528 pending_select_files_free (impl);
6529 browse_files_center_selected_row (impl);
6533 /* We only select the first row if the chooser is actually mapped ---
6534 * selecting the first row is to help the user when he is interacting with
6535 * the chooser, but sometimes a chooser works not on behalf of the user,
6536 * but rather on behalf of something else like GtkFileChooserButton. In
6537 * that case, the chooser's selection should be what the caller expects,
6538 * as the user can't see that something else got selected. See bug #165264.
6540 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
6541 gtk_widget_get_mapped (GTK_WIDGET (impl)))
6542 browse_files_select_first_row (impl);
6545 g_assert (impl->pending_select_files == NULL);
6549 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6554 info = g_file_query_info (impl->current_folder,
6555 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6556 G_FILE_QUERY_INFO_NONE,
6561 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6562 g_object_unref (info);
6565 msg = g_strdup (_("Could not read the contents of the folder"));
6567 error_message (impl, msg, error->message);
6571 /* Callback used when the file system model finishes loading */
6573 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6575 GtkFileChooserDefault *impl)
6577 profile_start ("start", NULL);
6580 show_error_on_reading_current_folder (impl, error);
6582 if (impl->load_state == LOAD_PRELOAD)
6584 load_remove_timer (impl);
6585 load_set_model (impl);
6587 else if (impl->load_state == LOAD_LOADING)
6593 /* We can't g_assert_not_reached(), as something other than us may have
6594 * initiated a folder reload. See #165556.
6596 profile_end ("end", NULL);
6600 g_assert (impl->load_timeout_id == 0);
6602 impl->load_state = LOAD_FINISHED;
6604 pending_select_files_process (impl);
6605 set_busy_cursor (impl, FALSE);
6606 #ifdef PROFILE_FILE_CHOOSER
6607 access ("MARK: *** FINISHED LOADING", F_OK);
6610 profile_end ("end", NULL);
6614 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6615 gboolean remove_from_treeview)
6617 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6619 if (impl->browse_files_model)
6621 g_object_unref (impl->browse_files_model);
6622 impl->browse_files_model = NULL;
6625 if (remove_from_treeview)
6626 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6630 my_g_format_time_for_display (glong secs)
6635 time_t time_mtime, time_now;
6636 const gchar *format;
6637 gchar *locale_format = NULL;
6639 char *date_str = NULL;
6641 const char *locale, *dot = NULL;
6642 gint64 codepage = -1;
6648 #ifdef HAVE_LOCALTIME_R
6649 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6652 struct tm *ptm = localtime ((time_t *) &time_mtime);
6656 g_warning ("ptm != NULL failed");
6658 return g_strdup (_("Unknown"));
6661 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6663 #endif /* HAVE_LOCALTIME_R */
6665 g_date_set_time_t (&mtime, time_mtime);
6666 time_now = time (NULL);
6667 g_date_set_time_t (&now, time_now);
6669 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6671 /* Translators: %H means "hours" and %M means "minutes" */
6673 format = _("%H:%M");
6674 else if (days_diff == 1)
6675 format = _("Yesterday at %H:%M");
6678 if (days_diff > 1 && days_diff < 7)
6679 format = "%A"; /* Days from last week */
6681 format = "%x"; /* Any other date */
6685 /* g_locale_from_utf8() returns a string in the system
6686 * code-page, which is not always the same as that used by the C
6687 * library. For instance when running a GTK+ program with
6688 * LANG=ko on an English version of Windows, the system
6689 * code-page is 1252, but the code-page used by the C library is
6690 * 949. (It's GTK+ itself that sets the C library locale when it
6691 * notices the LANG environment variable. See gtkmain.c The
6692 * Microsoft C library doesn't look at any locale environment
6693 * variables.) We need to pass strftime() a string in the C
6694 * library's code-page. See bug #509885.
6696 locale = setlocale (LC_ALL, NULL);
6698 dot = strchr (locale, '.');
6701 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6703 /* All codepages should fit in 16 bits AFAIK */
6704 if (codepage > 0 && codepage < 65536)
6706 sprintf (charset, "CP%u", (guint) codepage);
6707 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6711 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6713 if (locale_format != NULL &&
6714 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6717 /* As above but in opposite direction... */
6718 if (codepage > 0 && codepage < 65536)
6719 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6721 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6725 if (date_str == NULL)
6726 date_str = g_strdup (_("Unknown"));
6728 g_free (locale_format);
6733 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6735 GFileAttributeType type;
6738 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6739 g_file_info_set_attribute (to, attribute, type, value);
6743 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6745 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6746 GFile *file = G_FILE (object);
6747 GFileInfo *queried, *info;
6750 queried = g_file_query_info_finish (file, res, NULL);
6751 if (queried == NULL)
6754 GDK_THREADS_ENTER ();
6756 /* now we know model is valid */
6758 /* file was deleted */
6759 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6761 GDK_THREADS_LEAVE ();
6765 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6767 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6768 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6769 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6771 _gtk_file_system_model_update_file (model, file, info, FALSE);
6773 g_object_unref (info);
6775 GDK_THREADS_LEAVE ();
6779 file_system_model_set (GtkFileSystemModel *model,
6786 GtkFileChooserDefault *impl = data;
6790 case MODEL_COL_FILE:
6791 g_value_set_object (value, file);
6793 case MODEL_COL_NAME:
6795 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6797 g_value_set_string (value, g_file_info_get_display_name (info));
6799 case MODEL_COL_NAME_COLLATED:
6801 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6803 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6805 case MODEL_COL_IS_FOLDER:
6806 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6808 case MODEL_COL_PIXBUF:
6811 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6813 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6817 GtkTreeModel *tree_model;
6818 GtkTreePath *path, *start, *end;
6821 if (impl->browse_files_tree_view == NULL ||
6822 g_file_info_has_attribute (info, "filechooser::queried"))
6825 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6826 if (tree_model != GTK_TREE_MODEL (model))
6829 if (!_gtk_file_system_model_get_iter_for_file (model,
6832 g_assert_not_reached ();
6833 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6835 path = gtk_tree_model_get_path (tree_model, &iter);
6836 if (gtk_tree_path_compare (start, path) != 1 &&
6837 gtk_tree_path_compare (path, end) != 1)
6839 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6840 g_file_query_info_async (file,
6841 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6842 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6843 G_FILE_ATTRIBUTE_STANDARD_ICON,
6844 G_FILE_QUERY_INFO_NONE,
6846 _gtk_file_system_model_get_cancellable (model),
6847 file_system_model_got_thumbnail,
6850 gtk_tree_path_free (path);
6851 gtk_tree_path_free (start);
6852 gtk_tree_path_free (end);
6857 g_value_set_object (value, NULL);
6859 case MODEL_COL_SIZE:
6860 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6862 case MODEL_COL_SIZE_TEXT:
6863 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6864 g_value_set_string (value, NULL);
6866 g_value_take_string (value, g_format_size (g_file_info_get_size (info)));
6868 case MODEL_COL_MTIME:
6869 case MODEL_COL_MTIME_TEXT:
6874 g_file_info_get_modification_time (info, &tv);
6875 if (column == MODEL_COL_MTIME)
6876 g_value_set_long (value, tv.tv_sec);
6877 else if (tv.tv_sec == 0)
6878 g_value_set_static_string (value, _("Unknown"));
6880 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6883 case MODEL_COL_ELLIPSIZE:
6884 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6887 g_assert_not_reached ();
6894 /* Gets rid of the old list model and creates a new one for the current folder */
6896 set_list_model (GtkFileChooserDefault *impl,
6899 g_assert (impl->current_folder != NULL);
6901 profile_start ("start", NULL);
6903 stop_loading_and_clear_list_model (impl, TRUE);
6905 set_busy_cursor (impl, TRUE);
6907 impl->browse_files_model =
6908 _gtk_file_system_model_new_for_directory (impl->current_folder,
6910 file_system_model_set,
6912 MODEL_COLUMN_TYPES);
6914 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6916 profile_msg (" set sort function", NULL);
6917 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6918 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6919 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6920 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6921 set_sort_column (impl);
6922 impl->list_sort_ascending = TRUE;
6923 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6924 G_CALLBACK (list_sort_column_changed_cb), impl);
6926 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6928 g_signal_connect (impl->browse_files_model, "finished-loading",
6929 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6931 install_list_model_filter (impl);
6933 profile_end ("end", NULL);
6938 struct update_chooser_entry_selected_foreach_closure {
6940 GtkTreeIter first_selected_iter;
6944 compare_utf8_filenames (const gchar *a,
6947 gchar *a_folded, *b_folded;
6950 a_folded = g_utf8_strdown (a, -1);
6951 b_folded = g_utf8_strdown (b, -1);
6953 retval = strcmp (a_folded, b_folded);
6962 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6967 struct update_chooser_entry_selected_foreach_closure *closure;
6970 closure->num_selected++;
6972 if (closure->num_selected == 1)
6973 closure->first_selected_iter = *iter;
6977 update_chooser_entry (GtkFileChooserDefault *impl)
6979 GtkTreeSelection *selection;
6980 struct update_chooser_entry_selected_foreach_closure closure;
6982 /* no need to update the file chooser's entry if there's no entry */
6983 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6984 !impl->location_entry)
6987 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6988 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6989 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6990 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6991 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6994 g_assert (impl->location_entry != NULL);
6996 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6997 closure.num_selected = 0;
6998 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
7000 if (closure.num_selected == 0)
7002 if (impl->operation_mode == OPERATION_MODE_RECENT)
7003 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), NULL);
7005 goto maybe_clear_entry;
7007 else if (closure.num_selected == 1)
7009 if (impl->operation_mode == OPERATION_MODE_BROWSE)
7012 gboolean change_entry;
7014 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
7016 /* If the cursor moved to the row of the newly created folder,
7017 * retrieving info will return NULL.
7022 g_free (impl->browse_files_last_selected_name);
7023 impl->browse_files_last_selected_name =
7024 g_strdup (g_file_info_get_display_name (info));
7026 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
7027 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7028 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7030 /* Don't change the name when clicking on a folder... */
7031 change_entry = (! _gtk_file_info_consider_as_directory (info));
7034 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7038 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7040 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7041 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7046 else if (impl->operation_mode == OPERATION_MODE_RECENT
7047 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7051 /* Set the base folder on the name entry, so it will do completion relative to the correct recent-folder */
7053 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &closure.first_selected_iter,
7054 MODEL_COL_FILE, &folder,
7056 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), folder);
7057 g_object_unref (folder);
7063 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7064 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7066 /* Multiple selection, so just clear the entry. */
7067 g_free (impl->browse_files_last_selected_name);
7068 impl->browse_files_last_selected_name = NULL;
7070 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7076 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7077 && impl->browse_files_last_selected_name)
7079 const char *entry_text;
7081 gboolean clear_entry;
7083 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7084 len = strlen (entry_text);
7087 /* The file chooser entry may have appended a "/" to its text.
7088 * So take it out, and compare the result to the old selection.
7090 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7094 tmp = g_strndup (entry_text, len - 1);
7095 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7099 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7102 clear_entry = FALSE;
7105 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7110 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7114 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7118 struct UpdateCurrentFolderData
7120 GtkFileChooserDefault *impl;
7122 gboolean keep_trail;
7123 gboolean clear_entry;
7124 GFile *original_file;
7125 GError *original_error;
7129 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7130 GtkFileSystemVolume *volume,
7131 const GError *error,
7134 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7135 struct UpdateCurrentFolderData *data = user_data;
7136 GtkFileChooserDefault *impl = data->impl;
7138 if (cancellable != impl->update_current_folder_cancellable)
7141 impl->update_current_folder_cancellable = NULL;
7142 set_busy_cursor (impl, FALSE);
7149 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7150 impl->reload_state = RELOAD_EMPTY;
7154 change_folder_and_display_error (impl, data->file, data->clear_entry);
7157 g_object_unref (data->file);
7160 g_object_unref (cancellable);
7164 update_current_folder_get_info_cb (GCancellable *cancellable,
7166 const GError *error,
7169 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7170 struct UpdateCurrentFolderData *data = user_data;
7171 GtkFileChooserDefault *impl = data->impl;
7173 if (cancellable != impl->update_current_folder_cancellable)
7176 impl->update_current_folder_cancellable = NULL;
7177 impl->reload_state = RELOAD_EMPTY;
7179 set_busy_cursor (impl, FALSE);
7188 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7190 GMountOperation *mount_operation;
7191 GtkWidget *toplevel;
7193 g_object_unref (cancellable);
7194 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7196 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7198 set_busy_cursor (impl, TRUE);
7200 impl->update_current_folder_cancellable =
7201 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7203 update_current_folder_mount_enclosing_volume_cb,
7209 if (!data->original_file)
7211 data->original_file = g_object_ref (data->file);
7212 data->original_error = g_error_copy (error);
7215 parent_file = g_file_get_parent (data->file);
7217 /* get parent path and try to change the folder to that */
7220 g_object_unref (data->file);
7221 data->file = parent_file;
7223 g_object_unref (cancellable);
7225 /* restart the update current folder operation */
7226 impl->reload_state = RELOAD_HAS_FOLDER;
7228 impl->update_current_folder_cancellable =
7229 _gtk_file_system_get_info (impl->file_system, data->file,
7231 update_current_folder_get_info_cb,
7234 set_busy_cursor (impl, TRUE);
7240 /* Error and bail out, ignoring "not found" errors since they're useless:
7241 * they only happen when a program defaults to a folder that has been (re)moved.
7243 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7244 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7246 g_error_free (data->original_error);
7248 g_object_unref (data->original_file);
7254 if (data->original_file)
7256 /* Error and bail out, ignoring "not found" errors since they're useless:
7257 * they only happen when a program defaults to a folder that has been (re)moved.
7259 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7260 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7262 g_error_free (data->original_error);
7264 g_object_unref (data->original_file);
7267 if (! _gtk_file_info_consider_as_directory (info))
7270 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7273 if (impl->current_folder != data->file)
7275 if (impl->current_folder)
7276 g_object_unref (impl->current_folder);
7278 impl->current_folder = g_object_ref (data->file);
7281 impl->reload_state = RELOAD_HAS_FOLDER;
7283 /* Update the widgets that may trigger a folder change themselves. */
7285 if (!impl->changing_folder)
7287 impl->changing_folder = TRUE;
7289 shortcuts_update_current_folder (impl);
7291 impl->changing_folder = FALSE;
7294 /* Set the folder on the save entry */
7296 if (impl->location_entry)
7298 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7299 impl->current_folder);
7301 if (data->clear_entry)
7302 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7305 /* Create a new list model. This is slightly evil; we store the result value
7306 * but perform more actions rather than returning immediately even if it
7307 * generates an error.
7309 set_list_model (impl, NULL);
7311 /* Refresh controls */
7313 shortcuts_find_current_folder (impl);
7315 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7317 check_preview_change (impl);
7318 bookmarks_check_add_sensitivity (impl);
7320 g_signal_emit_by_name (impl, "selection-changed", 0);
7323 g_object_unref (data->file);
7326 g_object_unref (cancellable);
7330 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7332 gboolean keep_trail,
7333 gboolean clear_entry,
7336 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7337 struct UpdateCurrentFolderData *data;
7339 profile_start ("start", NULL);
7341 g_object_ref (file);
7343 operation_mode_set (impl, OPERATION_MODE_BROWSE);
7345 if (impl->local_only && !g_file_is_native (file))
7347 g_set_error_literal (error,
7348 GTK_FILE_CHOOSER_ERROR,
7349 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7350 _("Cannot change to folder because it is not local"));
7352 g_object_unref (file);
7353 profile_end ("end - not local", NULL);
7357 if (impl->update_current_folder_cancellable)
7358 g_cancellable_cancel (impl->update_current_folder_cancellable);
7360 /* Test validity of path here. */
7361 data = g_new0 (struct UpdateCurrentFolderData, 1);
7363 data->file = g_object_ref (file);
7364 data->keep_trail = keep_trail;
7365 data->clear_entry = clear_entry;
7367 impl->reload_state = RELOAD_HAS_FOLDER;
7369 impl->update_current_folder_cancellable =
7370 _gtk_file_system_get_info (impl->file_system, file,
7372 update_current_folder_get_info_cb,
7375 set_busy_cursor (impl, TRUE);
7376 g_object_unref (file);
7378 profile_end ("end", NULL);
7383 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7385 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7387 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7388 impl->operation_mode == OPERATION_MODE_RECENT)
7391 if (impl->reload_state == RELOAD_EMPTY)
7393 /* We are unmapped, or we had an error while loading the last folder.
7394 * We'll return the folder used by the last invocation of the file chooser
7395 * since once we get (re)mapped, we'll load *that* folder anyway unless
7396 * the caller explicitly calls set_current_folder() on us.
7398 return get_file_for_last_folder_opened (impl);
7401 if (impl->current_folder)
7402 return g_object_ref (impl->current_folder);
7408 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7411 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7413 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7414 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7416 pending_select_files_free (impl);
7417 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7421 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7425 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7429 parent_file = g_file_get_parent (file);
7432 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7434 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7435 impl->operation_mode == OPERATION_MODE_RECENT ||
7436 impl->load_state == LOAD_EMPTY)
7442 g_assert (impl->current_folder != NULL);
7444 same_path = g_file_equal (parent_file, impl->current_folder);
7447 if (same_path && impl->load_state == LOAD_FINISHED)
7452 files.data = (gpointer) file;
7455 result = show_and_select_files (impl, &files);
7456 g_object_unref (parent_file);
7460 pending_select_files_add (impl, file);
7466 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7467 g_object_unref (parent_file);
7471 g_object_unref (parent_file);
7476 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7479 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7480 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7483 if (!impl->browse_files_model)
7486 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7491 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7496 maybe_select (GtkTreeModel *model,
7501 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7502 GtkTreeSelection *selection;
7505 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7507 gtk_tree_model_get (model, iter,
7508 MODEL_COL_IS_FOLDER, &is_folder,
7511 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7512 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7513 gtk_tree_selection_select_iter (selection, iter);
7515 gtk_tree_selection_unselect_iter (selection, iter);
7521 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7523 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7525 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7526 impl->operation_mode == OPERATION_MODE_RECENT)
7528 GtkTreeSelection *selection;
7530 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7531 gtk_tree_selection_select_all (selection);
7535 if (impl->select_multiple)
7536 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7537 maybe_select, impl);
7541 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7543 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7544 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7546 gtk_tree_selection_unselect_all (selection);
7547 pending_select_files_free (impl);
7550 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7552 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7554 * is_empty_ret - whether the file entry is totally empty
7556 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7557 * the path will be "$cwd/foobar")
7560 check_save_entry (GtkFileChooserDefault *impl,
7562 gboolean *is_well_formed_ret,
7563 gboolean *is_empty_ret,
7564 gboolean *is_file_part_empty_ret,
7565 gboolean *is_folder)
7567 GtkFileChooserEntry *chooser_entry;
7568 GFile *current_folder;
7569 const char *file_part;
7573 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7574 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7575 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7576 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7577 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7579 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7581 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7584 *is_well_formed_ret = TRUE;
7585 *is_empty_ret = TRUE;
7586 *is_file_part_empty_ret = TRUE;
7592 *is_empty_ret = FALSE;
7594 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7595 if (!current_folder)
7598 *is_well_formed_ret = FALSE;
7599 *is_file_part_empty_ret = FALSE;
7605 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7607 if (!file_part || file_part[0] == '\0')
7609 *file_ret = g_object_ref (current_folder);
7610 *is_well_formed_ret = TRUE;
7611 *is_file_part_empty_ret = TRUE;
7617 *is_file_part_empty_ret = FALSE;
7620 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7624 error_building_filename_dialog (impl, error);
7626 *is_well_formed_ret = FALSE;
7633 *is_well_formed_ret = TRUE;
7634 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7637 struct get_files_closure {
7638 GtkFileChooserDefault *impl;
7640 GFile *file_from_entry;
7644 get_files_foreach (GtkTreeModel *model,
7649 struct get_files_closure *info;
7651 GtkFileSystemModel *fs_model;
7654 fs_model = info->impl->browse_files_model;
7656 file = _gtk_file_system_model_get_file (fs_model, iter);
7658 return; /* We are on the editable row */
7660 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7661 info->result = g_slist_prepend (info->result, g_object_ref (file));
7665 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7667 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7668 struct get_files_closure info;
7669 GtkWindow *toplevel;
7670 GtkWidget *current_focus;
7671 gboolean file_list_seen;
7675 info.file_from_entry = NULL;
7677 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7678 return search_get_selected_files (impl);
7680 if (impl->operation_mode == OPERATION_MODE_RECENT)
7682 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7684 file_list_seen = TRUE;
7688 return recent_get_selected_files (impl);
7691 toplevel = get_toplevel (GTK_WIDGET (impl));
7693 current_focus = gtk_window_get_focus (toplevel);
7695 current_focus = NULL;
7697 file_list_seen = FALSE;
7698 if (current_focus == impl->browse_files_tree_view)
7700 GtkTreeSelection *selection;
7704 file_list_seen = TRUE;
7705 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7706 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7708 /* If there is no selection in the file list, we probably have this situation:
7710 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7711 * 2. He then double-clicked on a folder ("bar") in the file list
7713 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7714 * filename entry to see if that is the case.
7716 if (info.result == NULL && impl->location_entry)
7719 else if (impl->location_entry && current_focus == impl->location_entry)
7721 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7725 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7730 if (!is_well_formed)
7733 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7735 g_object_unref (info.file_from_entry);
7739 if (info.file_from_entry)
7740 info.result = g_slist_prepend (info.result, info.file_from_entry);
7741 else if (!file_list_seen)
7746 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7748 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7752 /* The focus is on a dialog's action area button or something else */
7753 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7754 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7762 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7763 * fall back to the current directory */
7764 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7765 info.result == NULL)
7767 GFile *current_folder;
7769 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7772 info.result = g_slist_prepend (info.result, current_folder);
7775 return g_slist_reverse (info.result);
7779 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7781 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7783 if (impl->preview_file)
7784 return g_object_ref (impl->preview_file);
7789 static GtkFileSystem *
7790 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7792 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7794 return impl->file_system;
7797 /* Shows or hides the filter widgets */
7799 show_filters (GtkFileChooserDefault *impl,
7803 gtk_widget_show (impl->filter_combo_hbox);
7805 gtk_widget_hide (impl->filter_combo_hbox);
7809 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7810 GtkFileFilter *filter)
7812 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7815 if (g_slist_find (impl->filters, filter))
7817 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7821 g_object_ref_sink (filter);
7822 impl->filters = g_slist_append (impl->filters, filter);
7824 name = gtk_file_filter_get_name (filter);
7826 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7828 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->filter_combo), name);
7830 if (!g_slist_find (impl->filters, impl->current_filter))
7831 set_current_filter (impl, filter);
7833 show_filters (impl, TRUE);
7837 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7838 GtkFileFilter *filter)
7840 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7841 GtkTreeModel *model;
7845 filter_index = g_slist_index (impl->filters, filter);
7847 if (filter_index < 0)
7849 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7853 impl->filters = g_slist_remove (impl->filters, filter);
7855 if (filter == impl->current_filter)
7858 set_current_filter (impl, impl->filters->data);
7860 set_current_filter (impl, NULL);
7863 /* Remove row from the combo box */
7864 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7865 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7866 g_assert_not_reached ();
7868 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7870 g_object_unref (filter);
7873 show_filters (impl, FALSE);
7877 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7879 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7881 return g_slist_copy (impl->filters);
7884 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7886 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7889 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7892 struct AddShortcutData
7894 GtkFileChooserDefault *impl;
7899 add_shortcut_get_info_cb (GCancellable *cancellable,
7901 const GError *error,
7905 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7906 struct AddShortcutData *data = user_data;
7908 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7911 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7913 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7916 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7918 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7921 g_object_unref (data->impl);
7922 g_object_unref (data->file);
7925 g_object_unref (cancellable);
7929 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7933 GCancellable *cancellable;
7934 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7935 struct AddShortcutData *data;
7939 /* Avoid adding duplicates */
7940 pos = shortcut_find_position (impl, file);
7941 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7945 uri = g_file_get_uri (file);
7946 /* translators, "Shortcut" means "Bookmark" here */
7948 GTK_FILE_CHOOSER_ERROR,
7949 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7950 _("Shortcut %s already exists"),
7957 for (l = impl->loading_shortcuts; l; l = l->next)
7959 GCancellable *c = l->data;
7962 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7963 if (f && g_file_equal (file, f))
7967 uri = g_file_get_uri (file);
7969 GTK_FILE_CHOOSER_ERROR,
7970 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7971 _("Shortcut %s already exists"),
7979 data = g_new0 (struct AddShortcutData, 1);
7980 data->impl = g_object_ref (impl);
7981 data->file = g_object_ref (file);
7983 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7985 add_shortcut_get_info_cb, data);
7990 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7991 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7997 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
8001 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8008 for (l = impl->loading_shortcuts; l; l = l->next)
8010 GCancellable *c = l->data;
8013 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
8014 if (f && g_file_equal (file, f))
8016 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
8017 g_cancellable_cancel (c);
8022 if (impl->num_shortcuts == 0)
8025 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8026 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8027 g_assert_not_reached ();
8029 for (i = 0; i < impl->num_shortcuts; i++)
8032 ShortcutType shortcut_type;
8035 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8036 SHORTCUTS_COL_DATA, &col_data,
8037 SHORTCUTS_COL_TYPE, &shortcut_type,
8039 g_assert (col_data != NULL);
8040 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8042 shortcut = col_data;
8043 if (g_file_equal (shortcut, file))
8045 shortcuts_remove_rows (impl, pos + i, 1);
8046 impl->num_shortcuts--;
8050 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8051 g_assert_not_reached ();
8056 uri = g_file_get_uri (file);
8057 /* translators, "Shortcut" means "Bookmark" here */
8059 GTK_FILE_CHOOSER_ERROR,
8060 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8061 _("Shortcut %s does not exist"),
8069 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8071 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8077 if (impl->num_shortcuts == 0)
8080 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8081 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8082 g_assert_not_reached ();
8086 for (i = 0; i < impl->num_shortcuts; i++)
8089 ShortcutType shortcut_type;
8092 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8093 SHORTCUTS_COL_DATA, &col_data,
8094 SHORTCUTS_COL_TYPE, &shortcut_type,
8096 g_assert (col_data != NULL);
8097 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8099 shortcut = col_data;
8100 list = g_slist_prepend (list, g_object_ref (shortcut));
8102 if (i != impl->num_shortcuts - 1)
8104 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8105 g_assert_not_reached ();
8109 return g_slist_reverse (list);
8112 /* Guesses a size based upon font sizes */
8114 find_good_size_from_style (GtkWidget *widget,
8118 GtkStyleContext *context;
8119 GtkStateFlags state;
8124 context = gtk_widget_get_style_context (widget);
8125 state = gtk_widget_get_state_flags (widget);
8127 screen = gtk_widget_get_screen (widget);
8130 resolution = gdk_screen_get_resolution (screen);
8131 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8135 resolution = 96.0; /* wheeee */
8137 font_size = pango_font_description_get_size (gtk_style_context_get_font (context, state));
8138 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
8140 *width = font_size * NUM_CHARS;
8141 *height = font_size * NUM_LINES;
8145 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8146 gint *default_width,
8147 gint *default_height)
8149 GtkFileChooserDefault *impl;
8151 int x, y, width, height;
8153 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8155 settings_ensure (impl);
8157 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
8158 g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
8160 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8162 *default_width = width;
8163 *default_height = height;
8167 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8169 if (impl->preview_widget_active &&
8170 impl->preview_widget &&
8171 gtk_widget_get_visible (impl->preview_widget))
8173 gtk_widget_get_preferred_size (impl->preview_box,
8175 *default_width += PREVIEW_HBOX_SPACING + req.width;
8178 if (impl->extra_widget &&
8179 gtk_widget_get_visible (impl->extra_widget))
8181 gtk_widget_get_preferred_size (impl->extra_align,
8183 *default_height += gtk_box_get_spacing (GTK_BOX (chooser_embed)) + req.height;
8187 struct switch_folder_closure {
8188 GtkFileChooserDefault *impl;
8193 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8195 switch_folder_foreach_cb (GtkTreeModel *model,
8200 struct switch_folder_closure *closure;
8204 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8205 closure->num_selected++;
8208 /* Changes to the selected folder in the list view */
8210 switch_to_selected_folder (GtkFileChooserDefault *impl)
8212 GtkTreeSelection *selection;
8213 struct switch_folder_closure closure;
8215 /* We do this with foreach() rather than get_selected() as we may be in
8216 * multiple selection mode
8219 closure.impl = impl;
8220 closure.file = NULL;
8221 closure.num_selected = 0;
8223 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8224 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8226 g_assert (closure.file && closure.num_selected == 1);
8228 change_folder_and_display_error (impl, closure.file, FALSE);
8231 /* Gets the GFileInfo for the selected row in the file list; assumes single
8235 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8236 gboolean *had_selection)
8238 GtkTreeSelection *selection;
8242 g_assert (!impl->select_multiple);
8243 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8244 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8246 *had_selection = FALSE;
8250 *had_selection = TRUE;
8252 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8256 /* Gets the display name of the selected file in the file list; assumes single
8257 * selection mode and that something is selected.
8259 static const gchar *
8260 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8263 gboolean had_selection;
8265 info = get_selected_file_info_from_file_list (impl, &had_selection);
8266 g_assert (had_selection);
8267 g_assert (info != NULL);
8269 return g_file_info_get_display_name (info);
8273 add_custom_button_to_dialog (GtkDialog *dialog,
8274 const gchar *mnemonic_label,
8275 const gchar *stock_id,
8280 button = gtk_button_new_with_mnemonic (mnemonic_label);
8281 gtk_widget_set_can_default (button, TRUE);
8282 gtk_button_set_image (GTK_BUTTON (button),
8283 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8284 gtk_widget_show (button);
8286 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8289 /* Presents an overwrite confirmation dialog; returns whether we should accept
8293 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8294 const gchar *file_part,
8295 const gchar *folder_display_name)
8297 GtkWindow *toplevel;
8301 toplevel = get_toplevel (GTK_WIDGET (impl));
8303 dialog = gtk_message_dialog_new (toplevel,
8304 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8305 GTK_MESSAGE_QUESTION,
8307 _("A file named \"%s\" already exists. Do you want to replace it?"),
8309 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8310 _("The file already exists in \"%s\". Replacing it will "
8311 "overwrite its contents."),
8312 folder_display_name);
8314 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8315 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8316 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8317 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8318 GTK_RESPONSE_ACCEPT,
8319 GTK_RESPONSE_CANCEL,
8321 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8323 if (gtk_window_has_group (toplevel))
8324 gtk_window_group_add_window (gtk_window_get_group (toplevel),
8325 GTK_WINDOW (dialog));
8327 response = gtk_dialog_run (GTK_DIALOG (dialog));
8329 gtk_widget_destroy (dialog);
8331 return (response == GTK_RESPONSE_ACCEPT);
8334 struct GetDisplayNameData
8336 GtkFileChooserDefault *impl;
8340 /* Every time we request a response explicitly, we need to save the selection to the recently-used list,
8341 * as requesting a response means, "the dialog is confirmed".
8344 request_response_and_add_to_recent_list (GtkFileChooserDefault *impl)
8346 g_signal_emit_by_name (impl, "response-requested");
8347 add_selection_to_recent_list (impl);
8351 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8353 const GError *error,
8356 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8357 gboolean should_respond = FALSE;
8358 struct GetDisplayNameData *data = user_data;
8360 if (cancellable != data->impl->should_respond_get_info_cancellable)
8363 data->impl->should_respond_get_info_cancellable = NULL;
8369 /* Huh? Did the folder disappear? Let the caller deal with it */
8370 should_respond = TRUE;
8372 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8374 set_busy_cursor (data->impl, FALSE);
8376 request_response_and_add_to_recent_list (data->impl);
8379 g_object_unref (data->impl);
8380 g_free (data->file_part);
8383 g_object_unref (cancellable);
8386 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8387 * should respond. Can get the file part from the file list or the save entry.
8390 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8391 const gchar *file_part,
8394 GtkFileChooserConfirmation conf;
8396 if (!impl->do_overwrite_confirmation)
8399 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8401 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8405 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8407 struct GetDisplayNameData *data;
8409 g_assert (file_part != NULL);
8411 data = g_new0 (struct GetDisplayNameData, 1);
8412 data->impl = g_object_ref (impl);
8413 data->file_part = g_strdup (file_part);
8415 if (impl->should_respond_get_info_cancellable)
8416 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8418 impl->should_respond_get_info_cancellable =
8419 _gtk_file_system_get_info (impl->file_system, parent_file,
8420 "standard::display-name",
8421 confirmation_confirm_get_info_cb,
8423 set_busy_cursor (data->impl, TRUE);
8427 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8430 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8434 g_assert_not_reached ();
8439 struct FileExistsData
8441 GtkFileChooserDefault *impl;
8442 gboolean file_exists_and_is_not_folder;
8448 name_entry_get_parent_info_cb (GCancellable *cancellable,
8450 const GError *error,
8453 gboolean parent_is_folder;
8454 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8455 struct FileExistsData *data = user_data;
8457 if (cancellable != data->impl->should_respond_get_info_cancellable)
8460 data->impl->should_respond_get_info_cancellable = NULL;
8462 set_busy_cursor (data->impl, FALSE);
8468 parent_is_folder = FALSE;
8470 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8472 if (parent_is_folder)
8474 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8476 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) */
8478 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8480 if (data->file_exists_and_is_not_folder)
8485 /* Dup the string because the string may be modified
8486 * depending on what clients do in the confirm-overwrite
8487 * signal and this corrupts the pointer
8489 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8490 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8494 request_response_and_add_to_recent_list (data->impl);
8497 request_response_and_add_to_recent_list (data->impl);
8499 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8500 || data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8502 GError *mkdir_error = NULL;
8504 /* In both cases (SELECT_FOLDER and CREATE_FOLDER), if you type
8505 * "/blah/nonexistent" you *will* want a folder created.
8508 set_busy_cursor (data->impl, TRUE);
8509 g_file_make_directory (data->file, NULL, &mkdir_error);
8510 set_busy_cursor (data->impl, FALSE);
8513 request_response_and_add_to_recent_list (data->impl);
8515 error_creating_folder_dialog (data->impl, data->file, mkdir_error);
8518 g_assert_not_reached ();
8524 /* The parent exists, but it's not a folder! Someone probably typed existing_file.txt/subfile.txt */
8525 error_with_file_under_nonfolder (data->impl, data->parent_file);
8531 /* The parent folder is not readable for some reason */
8533 error_copy = g_error_copy (error);
8534 error_changing_folder_dialog (data->impl, data->parent_file, error_copy);
8539 g_object_unref (data->impl);
8540 g_object_unref (data->file);
8541 g_object_unref (data->parent_file);
8544 g_object_unref (cancellable);
8548 file_exists_get_info_cb (GCancellable *cancellable,
8550 const GError *error,
8553 gboolean data_ownership_taken = FALSE;
8554 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8555 gboolean file_exists;
8557 gboolean needs_parent_check = FALSE;
8558 struct FileExistsData *data = user_data;
8560 if (cancellable != data->impl->file_exists_get_info_cancellable)
8563 data->impl->file_exists_get_info_cancellable = NULL;
8565 set_busy_cursor (data->impl, FALSE);
8570 file_exists = (info != NULL);
8571 is_folder = (file_exists && _gtk_file_info_consider_as_directory (info));
8573 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8576 change_folder_and_display_error (data->impl, data->file, TRUE);
8580 request_response_and_add_to_recent_list (data->impl); /* user typed an existing filename; we are done */
8582 needs_parent_check = TRUE; /* file doesn't exist; see if its parent exists */
8585 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8587 if (file_exists && !is_folder)
8589 /* Oops, the user typed the name of an existing path which is not
8592 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8593 g_error_copy (error));
8597 needs_parent_check = TRUE;
8600 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8604 needs_parent_check = TRUE;
8610 /* User typed a folder; we are done */
8611 request_response_and_add_to_recent_list (data->impl);
8614 error_selecting_folder_over_existing_file_dialog (data->impl, data->file);
8617 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8620 change_folder_and_display_error (data->impl, data->file, TRUE);
8622 needs_parent_check = TRUE;
8626 g_assert_not_reached();
8629 if (needs_parent_check) {
8630 /* check that everything up to the last path component exists (i.e. the parent) */
8632 data->file_exists_and_is_not_folder = file_exists && !is_folder;
8633 data_ownership_taken = TRUE;
8635 if (data->impl->should_respond_get_info_cancellable)
8636 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8638 data->impl->should_respond_get_info_cancellable =
8639 _gtk_file_system_get_info (data->impl->file_system,
8642 name_entry_get_parent_info_cb,
8644 set_busy_cursor (data->impl, TRUE);
8648 if (!data_ownership_taken)
8650 g_object_unref (data->impl);
8651 g_object_unref (data->file);
8652 g_object_unref (data->parent_file);
8656 g_object_unref (cancellable);
8660 paste_text_received (GtkClipboard *clipboard,
8662 GtkFileChooserDefault *impl)
8669 file = g_file_new_for_uri (text);
8671 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8672 location_popup_handler (impl, text);
8674 g_object_unref (file);
8677 /* Handler for the "location-popup-on-paste" keybinding signal */
8679 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8681 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8682 GDK_SELECTION_CLIPBOARD);
8683 gtk_clipboard_request_text (clipboard,
8684 (GtkClipboardTextReceivedFunc) paste_text_received,
8688 /* Implementation for GtkFileChooserEmbed::should_respond() */
8690 add_selection_to_recent_list (GtkFileChooserDefault *impl)
8695 files = gtk_file_chooser_default_get_files (GTK_FILE_CHOOSER (impl));
8697 for (l = files; l; l = l->next)
8699 GFile *file = l->data;
8702 uri = g_file_get_uri (file);
8705 gtk_recent_manager_add_item (impl->recent_manager, uri);
8710 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
8711 g_slist_free (files);
8715 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8717 GtkFileChooserDefault *impl;
8718 GtkWidget *toplevel;
8719 GtkWidget *current_focus;
8722 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8724 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8725 g_assert (GTK_IS_WINDOW (toplevel));
8729 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8731 if (current_focus == impl->browse_files_tree_view)
8733 /* The following array encodes what we do based on the impl->action and the
8734 * number of files selected.
8737 NOOP, /* Do nothing (don't respond) */
8738 RESPOND, /* Respond immediately */
8739 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8740 ALL_FILES, /* Respond only if everything selected is a file */
8741 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8742 SAVE_ENTRY, /* Go to the code for handling the save entry */
8743 NOT_REACHED /* Sanity check */
8745 static const ActionToTake what_to_do[4][3] = {
8746 /* 0 selected 1 selected many selected */
8747 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8748 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8749 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8750 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8754 gboolean all_files, all_folders;
8756 ActionToTake action;
8760 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8762 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8764 retval = search_should_respond (impl);
8768 if (impl->operation_mode == OPERATION_MODE_RECENT)
8770 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8774 retval = recent_should_respond (impl);
8779 selection_check (impl, &num_selected, &all_files, &all_folders);
8781 if (num_selected > 2)
8786 action = what_to_do [impl->action] [k];
8797 case RESPOND_OR_SWITCH:
8798 g_assert (num_selected == 1);
8802 switch_to_selected_folder (impl);
8805 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8807 retval = should_respond_after_confirm_overwrite (impl,
8808 get_display_name_from_file_list (impl),
8809 impl->current_folder);
8823 retval = all_folders;
8830 g_assert_not_reached ();
8833 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8836 gboolean is_well_formed, is_empty, is_file_part_empty;
8838 GtkFileChooserEntry *entry;
8843 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8844 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8845 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8846 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8847 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8849 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8850 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8852 if (!is_well_formed)
8855 && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8856 && impl->operation_mode == OPERATION_MODE_RECENT)
8858 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FOLDER);
8860 /* We'll #ifdef this out, as the fucking treeview selects its first row,
8861 * thus changing our assumption that no selection is present - setting
8862 * a selection causes the error message from path_bar_set_mode() to go away,
8863 * but we want the user to see that message!
8865 gtk_widget_grab_focus (impl->browse_files_tree_view);
8868 /* FIXME: else show an "invalid filename" error as the pathbar mode? */
8875 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8876 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8878 path_bar_set_mode (impl, PATH_BAR_ERROR_NO_FILENAME);
8879 gtk_widget_grab_focus (impl->location_entry);
8886 g_assert (file != NULL);
8891 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8892 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8894 change_folder_and_display_error (impl, file, TRUE);
8896 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8897 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8899 /* The folder already exists, so we do not need to create it.
8900 * Just respond to terminate the dialog.
8906 g_assert_not_reached ();
8911 struct FileExistsData *data;
8913 /* We need to check whether file exists and whether it is a folder -
8914 * the GtkFileChooserEntry *does* report is_folder==FALSE as a false
8915 * negative (it doesn't know yet if your last path component is a
8919 data = g_new0 (struct FileExistsData, 1);
8920 data->impl = g_object_ref (impl);
8921 data->file = g_object_ref (file);
8922 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8924 if (impl->file_exists_get_info_cancellable)
8925 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8927 impl->file_exists_get_info_cancellable =
8928 _gtk_file_system_get_info (impl->file_system, file,
8930 file_exists_get_info_cb,
8933 set_busy_cursor (impl, TRUE);
8936 g_error_free (error);
8939 g_object_unref (file);
8941 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8943 /* The focus is on a dialog's action area button, *and* the widget that
8944 * was focused immediately before it is the file list.
8948 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8950 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8953 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8955 /* The focus is on a dialog's action area button, *and* the widget that
8956 * was focused immediately before it is the location entry.
8961 /* The focus is on a dialog's action area button or something else */
8962 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8963 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8971 add_selection_to_recent_list (impl);
8976 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8978 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8980 GtkFileChooserDefault *impl;
8983 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8985 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8986 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8988 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8989 widget = impl->browse_files_tree_view;
8991 widget = impl->location_entry;
8993 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8994 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8995 widget = impl->location_entry;
8998 g_assert_not_reached ();
9002 g_assert (widget != NULL);
9003 gtk_widget_grab_focus (widget);
9006 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
9008 search_selected_foreach_get_file_cb (GtkTreeModel *model,
9018 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9019 *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 */
9022 /* Constructs a list of the selected paths in search mode */
9024 search_get_selected_files (GtkFileChooserDefault *impl)
9027 GtkTreeSelection *selection;
9031 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9032 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
9033 result = g_slist_reverse (result);
9038 /* Called from ::should_respond(). We return whether there are selected files
9039 * in the search list.
9042 search_should_respond (GtkFileChooserDefault *impl)
9044 GtkTreeSelection *selection;
9046 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
9048 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9049 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9052 /* Adds one hit from the search engine to the search_model */
9054 search_add_hit (GtkFileChooserDefault *impl,
9059 file = g_file_new_for_uri (uri);
9063 if (!g_file_is_native (file))
9065 g_object_unref (file);
9069 _gtk_file_system_model_add_and_query_file (impl->search_model,
9073 g_object_unref (file);
9076 /* Callback used from GtkSearchEngine when we get new hits */
9078 search_engine_hits_added_cb (GtkSearchEngine *engine,
9082 GtkFileChooserDefault *impl;
9085 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9087 for (l = hits; l; l = l->next)
9088 search_add_hit (impl, (gchar*)l->data);
9091 /* Callback used from GtkSearchEngine when the query is done running */
9093 search_engine_finished_cb (GtkSearchEngine *engine,
9096 GtkFileChooserDefault *impl;
9098 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9101 /* EB: setting the model here will avoid loads of row events,
9102 * but it'll make the search look like blocked.
9104 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9105 GTK_TREE_MODEL (impl->search_model));
9106 file_list_set_sort_column_ids (impl);
9109 /* FMQ: if search was empty, say that we got no hits */
9110 set_busy_cursor (impl, FALSE);
9113 /* Displays a generic error when we cannot create a GtkSearchEngine.
9114 * It would be better if _gtk_search_engine_new() gave us a GError
9115 * with a better message, but it doesn't do that right now.
9118 search_error_could_not_create_client (GtkFileChooserDefault *impl)
9120 error_message (impl,
9121 _("Could not start the search process"),
9122 _("The program was not able to create a connection to the indexer "
9123 "daemon. Please make sure it is running."));
9127 search_engine_error_cb (GtkSearchEngine *engine,
9128 const gchar *message,
9131 GtkFileChooserDefault *impl;
9133 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9135 search_stop_searching (impl, TRUE);
9136 error_message (impl, _("Could not send the search request"), message);
9138 set_busy_cursor (impl, FALSE);
9141 /* Frees the data in the search_model */
9143 search_clear_model (GtkFileChooserDefault *impl,
9144 gboolean remove_from_treeview)
9146 if (!impl->search_model)
9149 g_object_unref (impl->search_model);
9150 impl->search_model = NULL;
9152 if (remove_from_treeview)
9153 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9156 /* Stops any ongoing searches; does not touch the search_model */
9158 search_stop_searching (GtkFileChooserDefault *impl,
9159 gboolean remove_query)
9161 if (remove_query && impl->search_query)
9163 g_object_unref (impl->search_query);
9164 impl->search_query = NULL;
9167 if (impl->search_engine)
9169 _gtk_search_engine_stop (impl->search_engine);
9171 g_object_unref (impl->search_engine);
9172 impl->search_engine = NULL;
9176 /* Creates the search_model and puts it in the tree view */
9178 search_setup_model (GtkFileChooserDefault *impl)
9180 g_assert (impl->search_model == NULL);
9182 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9184 MODEL_COLUMN_TYPES);
9186 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9190 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9194 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9198 set_sort_column (impl);
9200 /* EB: setting the model here will make the hits list update feel
9201 * more "alive" than setting the model at the end of the search
9204 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9205 GTK_TREE_MODEL (impl->search_model));
9206 file_list_set_sort_column_ids (impl);
9209 /* Creates a new query with the specified text and launches it */
9211 search_start_query (GtkFileChooserDefault *impl,
9212 const gchar *query_text)
9214 search_stop_searching (impl, FALSE);
9215 search_clear_model (impl, TRUE);
9216 search_setup_model (impl);
9217 set_busy_cursor (impl, TRUE);
9219 if (impl->search_engine == NULL)
9220 impl->search_engine = _gtk_search_engine_new ();
9222 if (!impl->search_engine)
9224 set_busy_cursor (impl, FALSE);
9225 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9229 if (!impl->search_query)
9231 impl->search_query = _gtk_query_new ();
9232 _gtk_query_set_text (impl->search_query, query_text);
9235 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9237 g_signal_connect (impl->search_engine, "hits-added",
9238 G_CALLBACK (search_engine_hits_added_cb), impl);
9239 g_signal_connect (impl->search_engine, "finished",
9240 G_CALLBACK (search_engine_finished_cb), impl);
9241 g_signal_connect (impl->search_engine, "error",
9242 G_CALLBACK (search_engine_error_cb), impl);
9244 _gtk_search_engine_start (impl->search_engine);
9247 /* Callback used when the user presses Enter while typing on the search
9248 * entry; starts the query
9251 search_entry_activate_cb (GtkEntry *entry,
9254 GtkFileChooserDefault *impl;
9257 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9259 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9260 if (strlen (text) == 0)
9263 /* reset any existing query object */
9264 if (impl->search_query)
9266 g_object_unref (impl->search_query);
9267 impl->search_query = NULL;
9270 search_start_query (impl, text);
9274 focus_entry_idle_cb (GtkFileChooserDefault *impl)
9276 GDK_THREADS_ENTER ();
9278 g_source_destroy (impl->focus_entry_idle);
9279 impl->focus_entry_idle = NULL;
9281 if (impl->search_entry)
9282 gtk_widget_grab_focus (impl->search_entry);
9284 GDK_THREADS_LEAVE ();
9290 focus_search_entry_in_idle (GtkFileChooserDefault *impl)
9292 /* bgo#634558 - When the user clicks on the Search entry in the shortcuts
9293 * pane, we get a selection-changed signal and we set up the search widgets.
9294 * However, gtk_tree_view_button_press() focuses the treeview *after* making
9295 * the change to the selection. So, we need to re-focus the search entry
9296 * after the treeview has finished doing its work; we'll do that in an idle
9300 if (!impl->focus_entry_idle)
9301 impl->focus_entry_idle = add_idle_while_impl_is_alive (impl, G_CALLBACK (focus_entry_idle_cb));
9304 /* Hides the path bar and creates the search entry */
9306 search_setup_widgets (GtkFileChooserDefault *impl)
9308 impl->search_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
9310 path_bar_update (impl);
9312 impl->search_entry = gtk_entry_new ();
9313 g_signal_connect (impl->search_entry, "activate",
9314 G_CALLBACK (search_entry_activate_cb),
9316 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9318 /* if there already is a query, restart it */
9319 if (impl->search_query)
9321 gchar *query = _gtk_query_get_text (impl->search_query);
9325 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9326 search_start_query (impl, query);
9332 g_object_unref (impl->search_query);
9333 impl->search_query = NULL;
9337 /* Box for search widgets */
9338 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9339 gtk_widget_show_all (impl->search_hbox);
9340 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), impl->search_hbox);
9342 /* Hide the location widgets temporarily */
9344 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9345 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9347 gtk_widget_hide (impl->location_button);
9348 gtk_widget_hide (impl->location_entry_box);
9351 focus_search_entry_in_idle (impl);
9353 /* FMQ: hide the filter combo? */
9357 * Recent files support
9360 /* Frees the data in the recent_model */
9362 recent_clear_model (GtkFileChooserDefault *impl,
9363 gboolean remove_from_treeview)
9365 if (!impl->recent_model)
9368 if (remove_from_treeview)
9369 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9371 g_object_unref (impl->recent_model);
9372 impl->recent_model = NULL;
9375 /* Stops any ongoing loading of the recent files list; does
9376 * not touch the recent_model
9379 recent_stop_loading (GtkFileChooserDefault *impl)
9381 if (impl->load_recent_id)
9383 g_source_remove (impl->load_recent_id);
9384 impl->load_recent_id = 0;
9389 recent_setup_model (GtkFileChooserDefault *impl)
9391 g_assert (impl->recent_model == NULL);
9393 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9395 MODEL_COLUMN_TYPES);
9397 _gtk_file_system_model_set_filter (impl->recent_model,
9398 impl->current_filter);
9399 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9403 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9407 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9411 set_sort_column (impl);
9416 GtkFileChooserDefault *impl;
9421 recent_idle_cleanup (gpointer data)
9423 RecentLoadData *load_data = data;
9424 GtkFileChooserDefault *impl = load_data->impl;
9426 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9427 GTK_TREE_MODEL (impl->recent_model));
9428 file_list_set_sort_column_ids (impl);
9429 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model), MODEL_COL_MTIME, GTK_SORT_DESCENDING);
9431 set_busy_cursor (impl, FALSE);
9433 impl->load_recent_id = 0;
9439 get_recent_files_limit (GtkWidget *widget)
9441 GtkSettings *settings;
9444 if (gtk_widget_has_screen (widget))
9445 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9447 settings = gtk_settings_get_default ();
9449 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9454 /* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
9456 populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
9462 limit = get_recent_files_limit (GTK_WIDGET (impl));
9466 for (l = items; l; l = l->next)
9468 GtkRecentInfo *info = l->data;
9471 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9472 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9475 g_object_unref (file);
9478 if (limit != -1 && n >= limit)
9484 populate_model_with_folders (GtkFileChooserDefault *impl, GList *items)
9489 folders = _gtk_file_chooser_extract_recent_folders (items);
9491 for (l = folders; l; l = l->next)
9493 GFile *folder = l->data;
9495 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9500 g_list_foreach (folders, (GFunc) g_object_unref, NULL);
9501 g_list_free (folders);
9505 recent_idle_load (gpointer data)
9507 RecentLoadData *load_data = data;
9508 GtkFileChooserDefault *impl = load_data->impl;
9510 if (!impl->recent_manager)
9513 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9514 if (!load_data->items)
9517 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
9518 populate_model_with_recent_items (impl, load_data->items);
9520 populate_model_with_folders (impl, load_data->items);
9522 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9523 g_list_free (load_data->items);
9524 load_data->items = NULL;
9530 recent_start_loading (GtkFileChooserDefault *impl)
9532 RecentLoadData *load_data;
9534 recent_stop_loading (impl);
9535 recent_clear_model (impl, TRUE);
9536 recent_setup_model (impl);
9537 set_busy_cursor (impl, TRUE);
9539 g_assert (impl->load_recent_id == 0);
9541 load_data = g_new (RecentLoadData, 1);
9542 load_data->impl = impl;
9543 load_data->items = NULL;
9545 /* begin lazy loading the recent files into the model */
9546 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9549 recent_idle_cleanup);
9553 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9563 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9564 *list = g_slist_prepend (*list, file);
9567 /* Constructs a list of the selected paths in recent files mode */
9569 recent_get_selected_files (GtkFileChooserDefault *impl)
9572 GtkTreeSelection *selection;
9576 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9577 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9578 result = g_slist_reverse (result);
9583 /* Called from ::should_respond(). We return whether there are selected
9584 * files in the recent files list.
9587 recent_should_respond (GtkFileChooserDefault *impl)
9589 GtkTreeSelection *selection;
9591 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9593 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9594 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9598 set_current_filter (GtkFileChooserDefault *impl,
9599 GtkFileFilter *filter)
9601 if (impl->current_filter != filter)
9605 /* NULL filters are allowed to reset to non-filtered status
9607 filter_index = g_slist_index (impl->filters, filter);
9608 if (impl->filters && filter && filter_index < 0)
9611 if (impl->current_filter)
9612 g_object_unref (impl->current_filter);
9613 impl->current_filter = filter;
9614 if (impl->current_filter)
9616 g_object_ref_sink (impl->current_filter);
9620 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9623 if (impl->browse_files_model)
9624 install_list_model_filter (impl);
9626 if (impl->search_model)
9627 _gtk_file_system_model_set_filter (impl->search_model, filter);
9629 if (impl->recent_model)
9630 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9632 g_object_notify (G_OBJECT (impl), "filter");
9637 filter_combo_changed (GtkComboBox *combo_box,
9638 GtkFileChooserDefault *impl)
9640 gint new_index = gtk_combo_box_get_active (combo_box);
9641 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9643 set_current_filter (impl, new_filter);
9647 check_preview_change (GtkFileChooserDefault *impl)
9649 GtkTreePath *cursor_path;
9651 char *new_display_name;
9652 GtkTreeModel *model;
9654 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9655 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9660 gtk_tree_model_get_iter (model, &iter, cursor_path);
9661 gtk_tree_model_get (model, &iter,
9662 MODEL_COL_FILE, &new_file,
9663 MODEL_COL_NAME, &new_display_name,
9666 gtk_tree_path_free (cursor_path);
9671 new_display_name = NULL;
9674 if (new_file != impl->preview_file &&
9675 !(new_file && impl->preview_file &&
9676 g_file_equal (new_file, impl->preview_file)))
9678 if (impl->preview_file)
9680 g_object_unref (impl->preview_file);
9681 g_free (impl->preview_display_name);
9686 impl->preview_file = new_file;
9687 impl->preview_display_name = new_display_name;
9691 impl->preview_file = NULL;
9692 impl->preview_display_name = NULL;
9693 g_free (new_display_name);
9696 if (impl->use_preview_label && impl->preview_label)
9697 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9699 g_signal_emit_by_name (impl, "update-preview");
9704 g_object_unref (new_file);
9706 g_free (new_display_name);
9711 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9712 GtkFileSystemVolume *volume,
9713 const GError *error,
9717 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9718 GtkFileChooserDefault *impl = data;
9720 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9723 impl->shortcuts_activate_iter_cancellable = NULL;
9725 set_busy_cursor (impl, FALSE);
9732 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9736 name = _gtk_file_system_volume_get_display_name (volume);
9737 msg = g_strdup_printf (_("Could not mount %s"), name);
9739 error_message (impl, msg, error->message);
9748 file = _gtk_file_system_volume_get_root (volume);
9751 change_folder_and_display_error (impl, file, FALSE);
9752 g_object_unref (file);
9756 g_object_unref (impl);
9757 g_object_unref (cancellable);
9761 /* Activates a volume by mounting it if necessary and then switching to its
9765 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9766 GtkFileSystemVolume *volume)
9770 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9772 /* We ref the file chooser since volume_mount() may run a main loop, and the
9773 * user could close the file chooser window in the meantime.
9775 g_object_ref (impl);
9777 if (!_gtk_file_system_volume_is_mounted (volume))
9779 GMountOperation *mount_op;
9781 set_busy_cursor (impl, TRUE);
9783 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9784 impl->shortcuts_activate_iter_cancellable =
9785 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9786 shortcuts_activate_volume_mount_cb,
9787 g_object_ref (impl));
9788 g_object_unref (mount_op);
9792 file = _gtk_file_system_volume_get_root (volume);
9795 change_folder_and_display_error (impl, file, FALSE);
9796 g_object_unref (file);
9800 g_object_unref (impl);
9803 /* Opens the folder or volume at the specified iter in the shortcuts model */
9804 struct ShortcutsActivateData
9806 GtkFileChooserDefault *impl;
9811 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9813 const GError *error,
9816 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9817 struct ShortcutsActivateData *data = user_data;
9819 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9822 data->impl->shortcuts_activate_iter_cancellable = NULL;
9827 if (!error && _gtk_file_info_consider_as_directory (info))
9828 change_folder_and_display_error (data->impl, data->file, FALSE);
9830 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9835 g_object_unref (data->impl);
9836 g_object_unref (data->file);
9839 g_object_unref (cancellable);
9843 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9844 GtkFileSystemVolume *volume,
9845 const GError *error,
9848 struct ShortcutsActivateData *data = user_data;
9852 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9854 g_object_unref (data->impl);
9855 g_object_unref (data->file);
9861 data->impl->shortcuts_activate_iter_cancellable =
9862 _gtk_file_system_get_info (data->impl->file_system, data->file,
9864 shortcuts_activate_get_info_cb, data);
9867 _gtk_file_system_volume_unref (volume);
9871 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9875 ShortcutType shortcut_type;
9877 /* In the Save modes, we want to preserve what the uesr typed in the filename
9878 * entry, so that he may choose another folder without erasing his typed name.
9880 if (impl->location_entry
9881 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9882 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9883 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9885 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9886 SHORTCUTS_COL_DATA, &col_data,
9887 SHORTCUTS_COL_TYPE, &shortcut_type,
9890 if (impl->shortcuts_activate_iter_cancellable)
9892 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9893 impl->shortcuts_activate_iter_cancellable = NULL;
9896 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9898 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9900 GtkFileSystemVolume *volume;
9904 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9906 shortcuts_activate_volume (impl, volume);
9908 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9910 struct ShortcutsActivateData *data;
9911 GtkFileSystemVolume *volume;
9913 operation_mode_set (impl, OPERATION_MODE_BROWSE);
9915 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9917 data = g_new0 (struct ShortcutsActivateData, 1);
9918 data->impl = g_object_ref (impl);
9919 data->file = g_object_ref (col_data);
9921 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9923 GMountOperation *mount_operation;
9924 GtkWidget *toplevel;
9926 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9928 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9930 impl->shortcuts_activate_iter_cancellable =
9931 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9933 shortcuts_activate_mount_enclosing_volume,
9938 impl->shortcuts_activate_iter_cancellable =
9939 _gtk_file_system_get_info (impl->file_system, data->file,
9941 shortcuts_activate_get_info_cb, data);
9944 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9946 operation_mode_set (impl, OPERATION_MODE_SEARCH);
9948 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9950 operation_mode_set (impl, OPERATION_MODE_RECENT);
9954 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9956 shortcuts_key_press_event_cb (GtkWidget *widget,
9958 GtkFileChooserDefault *impl)
9962 modifiers = gtk_accelerator_get_default_mod_mask ();
9964 if (key_is_left_or_right (event))
9966 gtk_widget_grab_focus (impl->browse_files_tree_view);
9970 if ((event->keyval == GDK_KEY_BackSpace
9971 || event->keyval == GDK_KEY_Delete
9972 || event->keyval == GDK_KEY_KP_Delete)
9973 && (event->state & modifiers) == 0)
9975 remove_selected_bookmarks (impl);
9979 if ((event->keyval == GDK_KEY_F2)
9980 && (event->state & modifiers) == 0)
9982 rename_selected_bookmark (impl);
9990 shortcuts_select_func (GtkTreeSelection *selection,
9991 GtkTreeModel *model,
9993 gboolean path_currently_selected,
9996 GtkFileChooserDefault *impl = data;
9997 GtkTreeIter filter_iter;
9998 ShortcutType shortcut_type;
10000 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10001 g_assert_not_reached ();
10003 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10005 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10009 list_select_func (GtkTreeSelection *selection,
10010 GtkTreeModel *model,
10012 gboolean path_currently_selected,
10015 GtkFileChooserDefault *impl = data;
10017 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10018 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10021 gboolean is_folder;
10023 if (!gtk_tree_model_get_iter (model, &iter, path))
10025 gtk_tree_model_get (model, &iter,
10026 MODEL_COL_IS_FOLDER, &is_folder,
10036 list_selection_changed (GtkTreeSelection *selection,
10037 GtkFileChooserDefault *impl)
10039 /* See if we are in the new folder editable row for Save mode */
10040 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10041 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10044 gboolean had_selection;
10046 info = get_selected_file_info_from_file_list (impl, &had_selection);
10047 if (!had_selection)
10048 goto out; /* normal processing */
10051 return; /* We are on the editable row for New Folder */
10056 if (impl->location_entry)
10057 update_chooser_entry (impl);
10059 path_bar_update (impl);
10061 check_preview_change (impl);
10062 bookmarks_check_add_sensitivity (impl);
10063 check_copy_file_location_sensitivity (impl);
10065 g_signal_emit_by_name (impl, "selection-changed", 0);
10068 /* Callback used when a row in the file list is activated */
10070 list_row_activated (GtkTreeView *tree_view,
10072 GtkTreeViewColumn *column,
10073 GtkFileChooserDefault *impl)
10077 GtkTreeModel *model;
10078 gboolean is_folder;
10080 model = gtk_tree_view_get_model (tree_view);
10082 if (!gtk_tree_model_get_iter (model, &iter, path))
10085 gtk_tree_model_get (model, &iter,
10086 MODEL_COL_FILE, &file,
10087 MODEL_COL_IS_FOLDER, &is_folder,
10090 if (is_folder && file)
10092 change_folder_and_display_error (impl, file, FALSE);
10093 g_object_unref (file);
10097 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10098 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10099 g_signal_emit_by_name (impl, "file-activated");
10104 g_object_unref (file);
10108 path_bar_clicked (GtkPathBar *path_bar,
10111 gboolean child_is_hidden,
10112 GtkFileChooserDefault *impl)
10115 pending_select_files_add (impl, child_file);
10117 if (!change_folder_and_display_error (impl, file, FALSE))
10120 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10121 * show hidden files so that ".baz" appears in the file list, as it will still
10122 * be shown in the path bar: "/foo/[bar]/.baz"
10124 if (child_is_hidden)
10125 g_object_set (impl, "show-hidden", TRUE, NULL);
10129 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10131 GtkTreeViewColumn *column;
10132 GtkCellRenderer *renderer;
10133 GList *walk, *list;
10134 gboolean always_sensitive;
10136 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10137 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10139 /* Keep the following column numbers in sync with create_file_list() */
10142 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10143 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10144 for (walk = list; walk; walk = walk->next)
10146 renderer = walk->data;
10147 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10149 gtk_tree_view_column_set_attributes (column, renderer,
10150 "pixbuf", MODEL_COL_PIXBUF,
10155 gtk_tree_view_column_set_attributes (column, renderer,
10156 "text", MODEL_COL_NAME,
10157 "ellipsize", MODEL_COL_ELLIPSIZE,
10160 if (always_sensitive)
10161 g_object_set (renderer, "sensitive", TRUE, NULL);
10163 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10165 g_list_free (list);
10168 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10169 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10170 renderer = list->data;
10171 gtk_tree_view_column_set_attributes (column, renderer,
10172 "text", MODEL_COL_SIZE_TEXT,
10174 if (always_sensitive)
10175 g_object_set (renderer, "sensitive", TRUE, NULL);
10177 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10178 g_list_free (list);
10181 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10182 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10183 renderer = list->data;
10184 gtk_tree_view_column_set_attributes (column, renderer,
10185 "text", MODEL_COL_MTIME_TEXT,
10187 if (always_sensitive)
10188 g_object_set (renderer, "sensitive", TRUE, NULL);
10190 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10191 g_list_free (list);
10195 _gtk_file_chooser_default_new (void)
10197 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10201 location_set_user_text (GtkFileChooserDefault *impl,
10204 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10205 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10209 location_popup_handler (GtkFileChooserDefault *impl,
10212 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10214 GtkWidget *widget_to_focus;
10216 operation_mode_set (impl, OPERATION_MODE_BROWSE);
10218 if (impl->current_folder)
10219 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10221 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10222 widget_to_focus = impl->browse_files_tree_view;
10224 widget_to_focus = impl->location_entry;
10226 gtk_widget_grab_focus (widget_to_focus);
10230 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10231 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10236 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
10237 location_set_user_text (impl, path);
10239 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10240 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10242 gtk_widget_grab_focus (impl->location_entry);
10244 location_set_user_text (impl, path);
10247 g_assert_not_reached ();
10250 /* Handler for the "up-folder" keybinding signal */
10252 up_folder_handler (GtkFileChooserDefault *impl)
10254 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10257 /* Handler for the "down-folder" keybinding signal */
10259 down_folder_handler (GtkFileChooserDefault *impl)
10261 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10264 /* Switches to the shortcut in the specified index */
10266 switch_to_shortcut (GtkFileChooserDefault *impl,
10271 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10272 g_assert_not_reached ();
10274 shortcuts_activate_iter (impl, &iter);
10277 /* Handler for the "home-folder" keybinding signal */
10279 home_folder_handler (GtkFileChooserDefault *impl)
10281 if (impl->has_home)
10282 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10285 /* Handler for the "desktop-folder" keybinding signal */
10287 desktop_folder_handler (GtkFileChooserDefault *impl)
10289 if (impl->has_desktop)
10290 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10293 /* Handler for the "search-shortcut" keybinding signal */
10295 search_shortcut_handler (GtkFileChooserDefault *impl)
10297 if (impl->has_search)
10299 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10301 /* we want the entry widget to grab the focus the first
10302 * time, not the browse_files_tree_view widget.
10304 if (impl->search_entry)
10305 gtk_widget_grab_focus (impl->search_entry);
10309 /* Handler for the "recent-shortcut" keybinding signal */
10311 recent_shortcut_handler (GtkFileChooserDefault *impl)
10313 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10317 quick_bookmark_handler (GtkFileChooserDefault *impl,
10318 gint bookmark_index)
10323 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10326 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10328 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10329 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10332 gtk_tree_path_free (path);
10334 switch_to_shortcut (impl, bookmark_pos);
10338 show_hidden_handler (GtkFileChooserDefault *impl)
10340 g_object_set (impl,
10341 "show-hidden", !impl->show_hidden,
10346 /* Drag and drop interfaces */
10349 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10354 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10356 model->impl = NULL;
10359 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10361 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10364 ShortcutsPaneModelFilter *model;
10368 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10370 pos = *gtk_tree_path_get_indices (path);
10371 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10373 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10376 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts
10380 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10382 GtkSelectionData *selection_data)
10389 /* Fill the GtkTreeDragSourceIface vtable */
10391 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10393 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10394 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10398 /* Fill the GtkTreeDragDestIface vtable */
10400 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10402 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10403 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10407 static GtkTreeModel *
10408 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10409 GtkTreeModel *child_model,
10412 ShortcutsPaneModelFilter *model;
10414 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10415 "child-model", child_model,
10416 "virtual-root", root,
10419 model->impl = impl;
10421 return GTK_TREE_MODEL (model);