1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gdk/gdkkeysyms.h"
25 #include "gtkalignment.h"
26 #include "gtkbindings.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkclipboard.h"
32 #include "gtkcombobox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserprivate.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserdialog.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechoosersettings.h"
41 #include "gtkfilechooserutils.h"
42 #include "gtkfilechooser.h"
43 #include "gtkfilesystem.h"
44 #include "gtkfilesystemmodel.h"
47 #include "gtkhpaned.h"
48 #include "gtkiconfactory.h"
49 #include "gtkicontheme.h"
51 #include "gtkimagemenuitem.h"
53 #include "gtkmarshalers.h"
54 #include "gtkmessagedialog.h"
55 #include "gtkmountoperation.h"
56 #include "gtkpathbar.h"
57 #include "gtkprivate.h"
58 #include "gtkradiobutton.h"
59 #include "gtkrecentfilter.h"
60 #include "gtkrecentmanager.h"
61 #include "gtkscrolledwindow.h"
62 #include "gtkseparatormenuitem.h"
63 #include "gtksizegroup.h"
66 #include "gtktooltip.h"
67 #include "gtktreednd.h"
68 #include "gtktreeprivate.h"
69 #include "gtktreeselection.h"
79 #include <sys/types.h>
90 #undef PROFILE_FILE_CHOOSER
91 #ifdef PROFILE_FILE_CHOOSER
98 #define PROFILE_INDENT 4
99 static int profile_indent;
102 profile_add_indent (int indent)
104 profile_indent += indent;
105 if (profile_indent < 0)
106 g_error ("You screwed up your indentation");
110 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
115 profile_add_indent (indent);
117 if (profile_indent == 0)
118 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
120 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
126 profile_add_indent (indent);
129 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
130 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
131 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
133 #define profile_start(x, y)
134 #define profile_end(x, y)
135 #define profile_msg(x, y)
140 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
142 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
143 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
144 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
146 #define MAX_LOADING_TIME 500
148 #define DEFAULT_NEW_FOLDER_NAME _("Type name of new folder")
150 struct _GtkFileChooserDefaultClass
152 GtkVBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
164 LOCATION_TOGGLE_POPUP,
172 static guint signals[LAST_SIGNAL] = { 0 };
174 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
176 SHORTCUTS_COL_PIXBUF,
180 SHORTCUTS_COL_REMOVABLE,
181 SHORTCUTS_COL_PIXBUF_VISIBLE,
182 SHORTCUTS_COL_CANCELLABLE,
183 SHORTCUTS_COL_NUM_COLUMNS
188 SHORTCUT_TYPE_VOLUME,
189 SHORTCUT_TYPE_SEPARATOR,
190 SHORTCUT_TYPE_SEARCH,
194 #define MODEL_ATTRIBUTES "standard::name,standard::type,standard::display-name," \
195 "standard::is-hidden,standard::is-backup,standard::size," \
196 "standard::content-type,time::modified"
198 /* the first 3 must be these due to settings caching sort column */
203 MODEL_COL_NAME_COLLATED,
207 MODEL_COL_MTIME_TEXT,
209 MODEL_COL_NUM_COLUMNS
212 /* This list of types is passed to _gtk_file_system_model_new*() */
213 #define MODEL_COLUMN_TYPES \
214 MODEL_COL_NUM_COLUMNS, \
215 G_TYPE_STRING, /* MODEL_COL_NAME */ \
216 G_TYPE_INT64, /* MODEL_COL_SIZE */ \
217 G_TYPE_LONG, /* MODEL_COL_MTIME */ \
218 G_TYPE_FILE, /* MODEL_COL_FILE */ \
219 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
220 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
221 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
222 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
223 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
224 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
226 /* Identifiers for target types */
232 search_is_possible (GtkFileChooserDefault *impl)
234 if (impl->search_engine == NULL)
235 impl->search_engine = _gtk_search_engine_new ();
237 return impl->search_engine != NULL;
240 /* Interesting places in the shortcuts bar */
244 SHORTCUTS_RECENT_SEPARATOR,
249 SHORTCUTS_BOOKMARKS_SEPARATOR,
251 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
252 SHORTCUTS_CURRENT_FOLDER
255 /* Icon size for if we can't get it from the theme */
256 #define FALLBACK_ICON_SIZE 16
258 #define PREVIEW_HBOX_SPACING 12
262 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
263 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
265 static GObject* gtk_file_chooser_default_constructor (GType type,
266 guint n_construct_properties,
267 GObjectConstructParam *construct_params);
268 static void gtk_file_chooser_default_finalize (GObject *object);
269 static void gtk_file_chooser_default_set_property (GObject *object,
273 static void gtk_file_chooser_default_get_property (GObject *object,
277 static void gtk_file_chooser_default_dispose (GObject *object);
278 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
279 static void gtk_file_chooser_default_realize (GtkWidget *widget);
280 static void gtk_file_chooser_default_map (GtkWidget *widget);
281 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
282 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
283 GtkWidget *previous_toplevel);
284 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
285 GtkStyle *previous_style);
286 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
287 GdkScreen *previous_screen);
288 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
289 GtkAllocation *allocation);
291 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
294 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
297 gboolean clear_entry,
299 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
300 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
302 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
305 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
307 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
308 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
309 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
310 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
311 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
312 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
313 GtkFileFilter *filter);
314 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
315 GtkFileFilter *filter);
316 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
317 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
320 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
323 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
325 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
327 gint *default_height);
328 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
329 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
331 static void location_popup_handler (GtkFileChooserDefault *impl,
333 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
334 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
335 static void up_folder_handler (GtkFileChooserDefault *impl);
336 static void down_folder_handler (GtkFileChooserDefault *impl);
337 static void home_folder_handler (GtkFileChooserDefault *impl);
338 static void desktop_folder_handler (GtkFileChooserDefault *impl);
339 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
340 gint bookmark_index);
341 static void show_hidden_handler (GtkFileChooserDefault *impl);
342 static void search_shortcut_handler (GtkFileChooserDefault *impl);
343 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
344 static void update_appearance (GtkFileChooserDefault *impl);
346 static void set_current_filter (GtkFileChooserDefault *impl,
347 GtkFileFilter *filter);
348 static void check_preview_change (GtkFileChooserDefault *impl);
350 static void filter_combo_changed (GtkComboBox *combo_box,
351 GtkFileChooserDefault *impl);
353 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
355 GtkFileChooserDefault *impl);
357 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
360 gboolean path_currently_selected,
362 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
364 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
366 static int shortcuts_get_index (GtkFileChooserDefault *impl,
367 ShortcutsIndex where);
368 static int shortcut_find_position (GtkFileChooserDefault *impl,
371 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
373 static gboolean list_select_func (GtkTreeSelection *selection,
376 gboolean path_currently_selected,
379 static void list_selection_changed (GtkTreeSelection *tree_selection,
380 GtkFileChooserDefault *impl);
381 static void list_row_activated (GtkTreeView *tree_view,
383 GtkTreeViewColumn *column,
384 GtkFileChooserDefault *impl);
386 static void path_bar_clicked (GtkPathBar *path_bar,
389 gboolean child_is_hidden,
390 GtkFileChooserDefault *impl);
392 static void add_bookmark_button_clicked_cb (GtkButton *button,
393 GtkFileChooserDefault *impl);
394 static void remove_bookmark_button_clicked_cb (GtkButton *button,
395 GtkFileChooserDefault *impl);
396 static void save_folder_combo_changed_cb (GtkComboBox *combo,
397 GtkFileChooserDefault *impl);
399 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
401 static void load_remove_timer (GtkFileChooserDefault *impl);
402 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
404 static void location_button_toggled_cb (GtkToggleButton *toggle,
405 GtkFileChooserDefault *impl);
406 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
408 static void stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
409 gboolean remove_from_treeview);
410 static void search_stop_searching (GtkFileChooserDefault *impl,
411 gboolean remove_query);
412 static void search_clear_model (GtkFileChooserDefault *impl,
413 gboolean remove_from_treeview);
414 static gboolean search_should_respond (GtkFileChooserDefault *impl);
415 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
416 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
417 static void search_entry_activate_cb (GtkEntry *entry,
419 static void settings_load (GtkFileChooserDefault *impl);
421 static void recent_stop_loading (GtkFileChooserDefault *impl);
422 static void recent_clear_model (GtkFileChooserDefault *impl,
423 gboolean remove_from_treeview);
424 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
425 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
426 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
427 static void set_file_system_backend (GtkFileChooserDefault *impl);
428 static void unset_file_system_backend (GtkFileChooserDefault *impl);
434 /* Drag and drop interface declarations */
437 GtkTreeModelFilter parent;
439 GtkFileChooserDefault *impl;
440 } ShortcutsPaneModelFilter;
443 GtkTreeModelFilterClass parent_class;
444 } ShortcutsPaneModelFilterClass;
446 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
447 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
449 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
451 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
452 _shortcuts_pane_model_filter,
453 GTK_TYPE_TREE_MODEL_FILTER,
454 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
455 shortcuts_pane_model_filter_drag_source_iface_init))
457 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
458 GtkTreeModel *child_model,
463 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
464 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
465 gtk_file_chooser_default_iface_init)
466 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
467 gtk_file_chooser_embed_default_iface_init));
470 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
472 static const guint quick_bookmark_keyvals[10] = {
473 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
475 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
476 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
477 GtkBindingSet *binding_set;
480 gobject_class->finalize = gtk_file_chooser_default_finalize;
481 gobject_class->constructor = gtk_file_chooser_default_constructor;
482 gobject_class->set_property = gtk_file_chooser_default_set_property;
483 gobject_class->get_property = gtk_file_chooser_default_get_property;
484 gobject_class->dispose = gtk_file_chooser_default_dispose;
486 widget_class->show_all = gtk_file_chooser_default_show_all;
487 widget_class->realize = gtk_file_chooser_default_realize;
488 widget_class->map = gtk_file_chooser_default_map;
489 widget_class->unmap = gtk_file_chooser_default_unmap;
490 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
491 widget_class->style_set = gtk_file_chooser_default_style_set;
492 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
493 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
495 signals[LOCATION_POPUP] =
496 g_signal_new_class_handler (I_("location-popup"),
497 G_OBJECT_CLASS_TYPE (class),
498 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
499 G_CALLBACK (location_popup_handler),
501 _gtk_marshal_VOID__STRING,
502 G_TYPE_NONE, 1, G_TYPE_STRING);
504 signals[LOCATION_POPUP_ON_PASTE] =
505 g_signal_new_class_handler (I_("location-popup-on-paste"),
506 G_OBJECT_CLASS_TYPE (class),
507 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
508 G_CALLBACK (location_popup_on_paste_handler),
510 _gtk_marshal_VOID__VOID,
513 signals[LOCATION_TOGGLE_POPUP] =
514 g_signal_new_class_handler (I_("location-toggle-popup"),
515 G_OBJECT_CLASS_TYPE (class),
516 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
517 G_CALLBACK (location_toggle_popup_handler),
519 _gtk_marshal_VOID__VOID,
523 g_signal_new_class_handler (I_("up-folder"),
524 G_OBJECT_CLASS_TYPE (class),
525 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
526 G_CALLBACK (up_folder_handler),
528 _gtk_marshal_VOID__VOID,
531 signals[DOWN_FOLDER] =
532 g_signal_new_class_handler (I_("down-folder"),
533 G_OBJECT_CLASS_TYPE (class),
534 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
535 G_CALLBACK (down_folder_handler),
537 _gtk_marshal_VOID__VOID,
540 signals[HOME_FOLDER] =
541 g_signal_new_class_handler (I_("home-folder"),
542 G_OBJECT_CLASS_TYPE (class),
543 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
544 G_CALLBACK (home_folder_handler),
546 _gtk_marshal_VOID__VOID,
549 signals[DESKTOP_FOLDER] =
550 g_signal_new_class_handler (I_("desktop-folder"),
551 G_OBJECT_CLASS_TYPE (class),
552 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
553 G_CALLBACK (desktop_folder_handler),
555 _gtk_marshal_VOID__VOID,
558 signals[QUICK_BOOKMARK] =
559 g_signal_new_class_handler (I_("quick-bookmark"),
560 G_OBJECT_CLASS_TYPE (class),
561 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
562 G_CALLBACK (quick_bookmark_handler),
564 _gtk_marshal_VOID__INT,
565 G_TYPE_NONE, 1, G_TYPE_INT);
567 signals[SHOW_HIDDEN] =
568 g_signal_new_class_handler (I_("show-hidden"),
569 G_OBJECT_CLASS_TYPE (class),
570 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
571 G_CALLBACK (show_hidden_handler),
573 _gtk_marshal_VOID__VOID,
576 signals[SEARCH_SHORTCUT] =
577 g_signal_new_class_handler (I_("search-shortcut"),
578 G_OBJECT_CLASS_TYPE (class),
579 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
580 G_CALLBACK (search_shortcut_handler),
582 _gtk_marshal_VOID__VOID,
585 signals[RECENT_SHORTCUT] =
586 g_signal_new_class_handler (I_("recent-shortcut"),
587 G_OBJECT_CLASS_TYPE (class),
588 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
589 G_CALLBACK (recent_shortcut_handler),
591 _gtk_marshal_VOID__VOID,
594 binding_set = gtk_binding_set_by_class (class);
596 gtk_binding_entry_add_signal (binding_set,
597 GDK_l, GDK_CONTROL_MASK,
598 "location-toggle-popup",
601 gtk_binding_entry_add_signal (binding_set,
604 1, G_TYPE_STRING, "/");
605 gtk_binding_entry_add_signal (binding_set,
608 1, G_TYPE_STRING, "/");
611 gtk_binding_entry_add_signal (binding_set,
614 1, G_TYPE_STRING, "~");
617 gtk_binding_entry_add_signal (binding_set,
618 GDK_v, GDK_CONTROL_MASK,
619 "location-popup-on-paste",
621 gtk_binding_entry_add_signal (binding_set,
622 GDK_Up, GDK_MOD1_MASK,
625 gtk_binding_entry_add_signal (binding_set,
629 gtk_binding_entry_add_signal (binding_set,
630 GDK_KP_Up, GDK_MOD1_MASK,
634 gtk_binding_entry_add_signal (binding_set,
635 GDK_Down, GDK_MOD1_MASK,
638 gtk_binding_entry_add_signal (binding_set,
639 GDK_KP_Down, GDK_MOD1_MASK,
643 gtk_binding_entry_add_signal (binding_set,
644 GDK_Home, GDK_MOD1_MASK,
647 gtk_binding_entry_add_signal (binding_set,
648 GDK_KP_Home, GDK_MOD1_MASK,
651 gtk_binding_entry_add_signal (binding_set,
652 GDK_d, GDK_MOD1_MASK,
655 gtk_binding_entry_add_signal (binding_set,
656 GDK_h, GDK_CONTROL_MASK,
659 gtk_binding_entry_add_signal (binding_set,
660 GDK_s, GDK_MOD1_MASK,
663 gtk_binding_entry_add_signal (binding_set,
664 GDK_r, GDK_MOD1_MASK,
668 for (i = 0; i < 10; i++)
669 gtk_binding_entry_add_signal (binding_set,
670 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
674 _gtk_file_chooser_install_properties (gobject_class);
678 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
680 iface->select_file = gtk_file_chooser_default_select_file;
681 iface->unselect_file = gtk_file_chooser_default_unselect_file;
682 iface->select_all = gtk_file_chooser_default_select_all;
683 iface->unselect_all = gtk_file_chooser_default_unselect_all;
684 iface->get_files = gtk_file_chooser_default_get_files;
685 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
686 iface->get_file_system = gtk_file_chooser_default_get_file_system;
687 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
688 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
689 iface->set_current_name = gtk_file_chooser_default_set_current_name;
690 iface->add_filter = gtk_file_chooser_default_add_filter;
691 iface->remove_filter = gtk_file_chooser_default_remove_filter;
692 iface->list_filters = gtk_file_chooser_default_list_filters;
693 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
694 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
695 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
699 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
701 iface->get_default_size = gtk_file_chooser_default_get_default_size;
702 iface->should_respond = gtk_file_chooser_default_should_respond;
703 iface->initial_focus = gtk_file_chooser_default_initial_focus;
707 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
709 profile_start ("start", NULL);
710 #ifdef PROFILE_FILE_CHOOSER
711 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
713 impl->local_only = TRUE;
714 impl->preview_widget_active = TRUE;
715 impl->use_preview_label = TRUE;
716 impl->select_multiple = FALSE;
717 impl->show_hidden = FALSE;
718 impl->show_size_column = TRUE;
719 impl->icon_size = FALLBACK_ICON_SIZE;
720 impl->load_state = LOAD_EMPTY;
721 impl->reload_state = RELOAD_EMPTY;
722 impl->pending_select_files = NULL;
723 impl->location_mode = LOCATION_MODE_PATH_BAR;
724 impl->operation_mode = OPERATION_MODE_BROWSE;
725 impl->sort_column = MODEL_COL_NAME;
726 impl->sort_order = GTK_SORT_ASCENDING;
727 impl->recent_manager = gtk_recent_manager_get_default ();
728 impl->create_folders = TRUE;
730 gtk_box_set_spacing (GTK_BOX (impl), 12);
732 set_file_system_backend (impl);
734 profile_end ("end", NULL);
737 /* Frees the data columns for the specified iter in the shortcuts model*/
739 shortcuts_free_row_data (GtkFileChooserDefault *impl,
743 ShortcutType shortcut_type;
744 GCancellable *cancellable;
746 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
747 SHORTCUTS_COL_DATA, &col_data,
748 SHORTCUTS_COL_TYPE, &shortcut_type,
749 SHORTCUTS_COL_CANCELLABLE, &cancellable,
753 g_cancellable_cancel (cancellable);
755 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
756 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
760 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
762 GtkFileSystemVolume *volume;
765 _gtk_file_system_volume_free (volume);
771 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
774 g_object_unref (file);
778 /* Frees all the data columns in the shortcuts model */
780 shortcuts_free (GtkFileChooserDefault *impl)
784 if (!impl->shortcuts_model)
787 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
790 shortcuts_free_row_data (impl, &iter);
792 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
794 g_object_unref (impl->shortcuts_model);
795 impl->shortcuts_model = NULL;
799 pending_select_files_free (GtkFileChooserDefault *impl)
801 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
802 g_slist_free (impl->pending_select_files);
803 impl->pending_select_files = NULL;
807 pending_select_files_add (GtkFileChooserDefault *impl,
810 impl->pending_select_files =
811 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
814 /* Used from gtk_tree_selection_selected_foreach() */
816 store_selection_foreach (GtkTreeModel *model,
821 GtkFileChooserDefault *impl;
824 impl = GTK_FILE_CHOOSER_DEFAULT (data);
826 file = _gtk_file_system_model_get_file (GTK_FILE_SYSTEM_MODEL (model), iter);
827 pending_select_files_add (impl, file);
830 /* Stores the current selection in the list of paths to select; this is used to
831 * preserve the selection when reloading the current folder.
834 pending_select_files_store_selection (GtkFileChooserDefault *impl)
836 GtkTreeSelection *selection;
838 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
839 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
843 gtk_file_chooser_default_finalize (GObject *object)
845 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
848 unset_file_system_backend (impl);
850 if (impl->shortcuts_pane_filter_model)
851 g_object_unref (impl->shortcuts_pane_filter_model);
853 if (impl->shortcuts_combo_filter_model)
854 g_object_unref (impl->shortcuts_combo_filter_model);
856 shortcuts_free (impl);
858 g_free (impl->browse_files_last_selected_name);
860 for (l = impl->filters; l; l = l->next)
862 GtkFileFilter *filter;
864 filter = GTK_FILE_FILTER (l->data);
865 g_object_unref (filter);
867 g_slist_free (impl->filters);
869 if (impl->current_filter)
870 g_object_unref (impl->current_filter);
872 if (impl->current_volume_file)
873 g_object_unref (impl->current_volume_file);
875 if (impl->current_folder)
876 g_object_unref (impl->current_folder);
878 if (impl->preview_file)
879 g_object_unref (impl->preview_file);
881 if (impl->browse_path_bar_size_group)
882 g_object_unref (impl->browse_path_bar_size_group);
884 /* Free all the Models we have */
885 stop_loading_and_clear_list_model (impl, FALSE);
886 search_clear_model (impl, FALSE);
887 recent_clear_model (impl, FALSE);
889 /* stopping the load above should have cleared this */
890 g_assert (impl->load_timeout_id == 0);
892 g_free (impl->preview_display_name);
894 g_free (impl->edited_new_text);
896 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
899 /* Shows an error dialog set as transient for the specified window */
901 error_message_with_parent (GtkWindow *parent,
907 dialog = gtk_message_dialog_new (parent,
908 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
913 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
916 if (parent && parent->group)
917 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
919 gtk_dialog_run (GTK_DIALOG (dialog));
920 gtk_widget_destroy (dialog);
923 /* Returns a toplevel GtkWindow, or NULL if none */
925 get_toplevel (GtkWidget *widget)
929 toplevel = gtk_widget_get_toplevel (widget);
930 if (!gtk_widget_is_toplevel (toplevel))
933 return GTK_WINDOW (toplevel);
936 /* Shows an error dialog for the file chooser */
938 error_message (GtkFileChooserDefault *impl,
942 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
945 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
947 error_dialog (GtkFileChooserDefault *impl,
958 uri = g_file_get_uri (file);
959 text = g_strdup_printf (msg, uri);
960 error_message (impl, text, error->message);
963 g_error_free (error);
967 /* Displays an error message about not being able to get information for a file.
968 * Frees the GError as well.
971 error_getting_info_dialog (GtkFileChooserDefault *impl,
976 _("Could not retrieve information about the file"),
980 /* Shows an error dialog about not being able to add a bookmark */
982 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
987 _("Could not add a bookmark"),
991 /* Shows an error dialog about not being able to remove a bookmark */
993 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
998 _("Could not remove bookmark"),
1002 /* Shows an error dialog about not being able to create a folder */
1004 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1009 _("The folder could not be created"),
1013 /* Shows an error about not being able to create a folder because a file with
1014 * the same name is already there.
1017 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1022 _("The folder could not be created, as a file with the same "
1023 "name already exists. Try using a different name for the "
1024 "folder, or rename the file first."),
1028 /* Shows an error dialog about not being able to create a filename */
1030 error_building_filename_dialog (GtkFileChooserDefault *impl,
1033 error_dialog (impl, _("Invalid file name"),
1037 /* Shows an error dialog when we cannot switch to a folder */
1039 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1043 error_dialog (impl, _("The folder contents could not be displayed"),
1047 /* Changes folders, displaying an error dialog if this fails */
1049 change_folder_and_display_error (GtkFileChooserDefault *impl,
1051 gboolean clear_entry)
1056 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1058 /* We copy the path because of this case:
1060 * list_row_activated()
1061 * fetches path from model; path belongs to the model (*)
1062 * calls change_folder_and_display_error()
1063 * calls gtk_file_chooser_set_current_folder_file()
1064 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1068 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1071 error_changing_folder_dialog (impl, file, error);
1077 emit_default_size_changed (GtkFileChooserDefault *impl)
1079 profile_msg (" emit default-size-changed start", NULL);
1080 g_signal_emit_by_name (impl, "default-size-changed");
1081 profile_msg (" emit default-size-changed end", NULL);
1085 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1087 if (impl->use_preview_label)
1089 if (!impl->preview_label)
1091 impl->preview_label = gtk_label_new (impl->preview_display_name);
1092 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1093 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1094 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1095 gtk_widget_show (impl->preview_label);
1100 if (impl->preview_label)
1102 gtk_widget_destroy (impl->preview_label);
1103 impl->preview_label = NULL;
1107 if (impl->preview_widget_active && impl->preview_widget)
1108 gtk_widget_show (impl->preview_box);
1110 gtk_widget_hide (impl->preview_box);
1112 if (!GTK_WIDGET_MAPPED (impl))
1113 emit_default_size_changed (impl);
1117 set_preview_widget (GtkFileChooserDefault *impl,
1118 GtkWidget *preview_widget)
1120 if (preview_widget == impl->preview_widget)
1123 if (impl->preview_widget)
1124 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1125 impl->preview_widget);
1127 impl->preview_widget = preview_widget;
1128 if (impl->preview_widget)
1130 gtk_widget_show (impl->preview_widget);
1131 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1132 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1133 impl->preview_widget,
1134 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1137 update_preview_widget_visibility (impl);
1140 /* Renders a "Search" icon at an appropriate size for a tree view */
1142 render_search_icon (GtkFileChooserDefault *impl)
1144 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1148 render_recent_icon (GtkFileChooserDefault *impl)
1150 GtkIconTheme *theme;
1153 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1154 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1156 theme = gtk_icon_theme_get_default ();
1158 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1164 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1170 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1171 struct ReloadIconsData
1173 GtkFileChooserDefault *impl;
1174 GtkTreeRowReference *row_ref;
1178 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1180 const GError *error,
1186 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1187 struct ReloadIconsData *data = user_data;
1189 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1192 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1194 if (cancelled || error)
1197 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1199 path = gtk_tree_row_reference_get_path (data->row_ref);
1200 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1201 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1202 SHORTCUTS_COL_PIXBUF, pixbuf,
1204 gtk_tree_path_free (path);
1207 g_object_unref (pixbuf);
1210 gtk_tree_row_reference_free (data->row_ref);
1211 g_object_unref (data->impl);
1214 g_object_unref (cancellable);
1218 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1223 profile_start ("start", NULL);
1225 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1228 for (l = impl->reload_icon_cancellables; l; l = l->next)
1230 GCancellable *cancellable = G_CANCELLABLE (l->data);
1231 g_cancellable_cancel (cancellable);
1233 g_slist_free (impl->reload_icon_cancellables);
1234 impl->reload_icon_cancellables = NULL;
1239 ShortcutType shortcut_type;
1240 gboolean pixbuf_visible;
1243 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1244 SHORTCUTS_COL_DATA, &data,
1245 SHORTCUTS_COL_TYPE, &shortcut_type,
1246 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1252 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1254 GtkFileSystemVolume *volume;
1257 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1258 impl->icon_size, NULL);
1260 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1262 if (g_file_is_native (G_FILE (data)))
1265 struct ReloadIconsData *info;
1266 GtkTreePath *tree_path;
1267 GCancellable *cancellable;
1271 info = g_new0 (struct ReloadIconsData, 1);
1272 info->impl = g_object_ref (impl);
1273 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1274 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1275 gtk_tree_path_free (tree_path);
1277 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1279 shortcuts_reload_icons_get_info_cb,
1281 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1285 GtkIconTheme *icon_theme;
1287 /* Don't call get_info for remote paths to avoid latency and
1289 * If we switch to a better bookmarks file format (XBEL), we
1290 * should use mime info to get a better icon.
1292 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1293 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1294 impl->icon_size, 0, NULL);
1297 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1299 pixbuf = render_search_icon (impl);
1301 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1303 pixbuf = render_recent_icon (impl);
1306 gtk_list_store_set (impl->shortcuts_model, &iter,
1307 SHORTCUTS_COL_PIXBUF, pixbuf,
1311 g_object_unref (pixbuf);
1315 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1319 profile_end ("end", NULL);
1323 shortcuts_find_folder (GtkFileChooserDefault *impl,
1326 GtkTreeSelection *selection;
1330 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1332 g_assert (folder != NULL);
1333 pos = shortcut_find_position (impl, folder);
1336 gtk_tree_selection_unselect_all (selection);
1340 path = gtk_tree_path_new_from_indices (pos, -1);
1341 gtk_tree_selection_select_path (selection, path);
1342 gtk_tree_path_free (path);
1345 /* If a shortcut corresponds to the current folder, selects it */
1347 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1349 shortcuts_find_folder (impl, impl->current_folder);
1352 /* Removes the specified number of rows from the shortcuts list */
1354 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1360 path = gtk_tree_path_new_from_indices (start_row, -1);
1362 for (; n_rows; n_rows--)
1366 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1367 g_assert_not_reached ();
1369 shortcuts_free_row_data (impl, &iter);
1370 gtk_list_store_remove (impl->shortcuts_model, &iter);
1373 gtk_tree_path_free (path);
1377 shortcuts_update_count (GtkFileChooserDefault *impl,
1378 ShortcutsIndex type,
1383 case SHORTCUTS_HOME:
1385 impl->has_home = FALSE;
1387 impl->has_home = TRUE;
1390 case SHORTCUTS_DESKTOP:
1392 impl->has_desktop = FALSE;
1394 impl->has_desktop = TRUE;
1397 case SHORTCUTS_VOLUMES:
1398 impl->num_volumes += value;
1401 case SHORTCUTS_SHORTCUTS:
1402 impl->num_shortcuts += value;
1405 case SHORTCUTS_BOOKMARKS:
1406 impl->num_bookmarks += value;
1409 case SHORTCUTS_CURRENT_FOLDER:
1411 impl->shortcuts_current_folder_active = FALSE;
1413 impl->shortcuts_current_folder_active = TRUE;
1422 struct ShortcutsInsertRequest
1424 GtkFileChooserDefault *impl;
1428 GtkTreeRowReference *row_ref;
1429 ShortcutsIndex type;
1435 get_file_info_finished (GCancellable *cancellable,
1437 const GError *error,
1441 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1445 GCancellable *model_cancellable;
1446 struct ShortcutsInsertRequest *request = data;
1448 path = gtk_tree_row_reference_get_path (request->row_ref);
1450 /* Handle doesn't exist anymore in the model */
1453 pos = gtk_tree_path_get_indices (path)[0];
1454 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1456 gtk_tree_path_free (path);
1458 /* validate cancellable, else goto out */
1459 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1460 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1462 if (cancellable != model_cancellable)
1465 /* set the cancellable to NULL in the model (we unref later on) */
1466 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1467 SHORTCUTS_COL_CANCELLABLE, NULL,
1475 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1476 shortcuts_update_count (request->impl, request->type, -1);
1478 if (request->type == SHORTCUTS_HOME)
1482 home = g_file_new_for_path (g_get_home_dir ());
1483 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1484 g_object_unref (home);
1486 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1488 /* Remove the current folder separator */
1489 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1490 shortcuts_remove_rows (request->impl, separator_pos, 1);
1496 if (!request->label_copy)
1497 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1498 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1499 request->impl->icon_size);
1501 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1502 SHORTCUTS_COL_PIXBUF, pixbuf,
1503 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1504 SHORTCUTS_COL_NAME, request->label_copy,
1505 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1506 SHORTCUTS_COL_REMOVABLE, request->removable,
1509 if (request->impl->shortcuts_pane_filter_model)
1510 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1512 if (request->impl->shortcuts_combo_filter_model)
1513 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1515 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1516 request->impl->save_folder_combo != NULL)
1518 /* The current folder is updated via _activate_iter(), don't
1519 * have save_folder_combo_changed_cb() call _activate_iter()
1522 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1523 G_CALLBACK (save_folder_combo_changed_cb),
1526 if (request->impl->has_search)
1529 if (request->impl->has_recent)
1532 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1533 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1534 G_CALLBACK (save_folder_combo_changed_cb),
1539 g_object_unref (pixbuf);
1542 g_object_unref (request->impl);
1543 g_object_unref (request->file);
1544 gtk_tree_row_reference_free (request->row_ref);
1545 g_free (request->label_copy);
1548 g_object_unref (cancellable);
1551 /* FIXME: GtkFileSystem needs a function to split a remote path
1552 * into hostname and path components, or maybe just have a
1553 * gtk_file_system_path_get_display_name().
1555 * This function is also used in gtkfilechooserbutton.c
1558 _gtk_file_chooser_label_for_file (GFile *file)
1560 const gchar *path, *start, *end, *p;
1561 gchar *uri, *host, *label;
1563 uri = g_file_get_uri (file);
1565 start = strstr (uri, "://");
1569 path = strchr (start, '/');
1574 end = uri + strlen (uri);
1578 /* strip username */
1579 p = strchr (start, '@');
1583 p = strchr (start, ':');
1587 host = g_strndup (start, end - start);
1589 /* Translators: the first string is a path and the second string
1590 * is a hostname. Nautilus and the panel contain the same string
1593 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1599 label = g_strdup (uri);
1607 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1608 * inserts a volume. A position of -1 indicates the end of the tree.
1611 shortcuts_insert_file (GtkFileChooserDefault *impl,
1613 ShortcutType shortcut_type,
1614 GtkFileSystemVolume *volume,
1618 ShortcutsIndex type)
1621 GdkPixbuf *pixbuf = NULL;
1622 gpointer data = NULL;
1624 GtkIconTheme *icon_theme;
1626 profile_start ("start shortcut", NULL);
1628 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1631 label_copy = _gtk_file_system_volume_get_display_name (volume);
1632 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1633 impl->icon_size, NULL);
1635 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1637 if (g_file_is_native (file))
1639 struct ShortcutsInsertRequest *request;
1640 GCancellable *cancellable;
1643 request = g_new0 (struct ShortcutsInsertRequest, 1);
1644 request->impl = g_object_ref (impl);
1645 request->file = g_object_ref (file);
1646 request->name_only = TRUE;
1647 request->removable = removable;
1649 request->type = type;
1651 request->label_copy = g_strdup (label);
1654 gtk_list_store_append (impl->shortcuts_model, &iter);
1656 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1658 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1659 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1660 gtk_tree_path_free (p);
1662 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1663 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1664 get_file_info_finished, request);
1666 gtk_list_store_set (impl->shortcuts_model, &iter,
1667 SHORTCUTS_COL_DATA, g_object_ref (file),
1668 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1669 SHORTCUTS_COL_CANCELLABLE, cancellable,
1672 shortcuts_update_count (impl, type, 1);
1678 /* Don't call get_info for remote paths to avoid latency and
1681 data = g_object_ref (file);
1683 label_copy = g_strdup (label);
1685 label_copy = _gtk_file_chooser_label_for_file (file);
1687 /* If we switch to a better bookmarks file format (XBEL), we
1688 * should use mime info to get a better icon.
1690 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1691 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1692 impl->icon_size, 0, NULL);
1697 g_assert_not_reached ();
1703 gtk_list_store_append (impl->shortcuts_model, &iter);
1705 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1707 shortcuts_update_count (impl, type, 1);
1709 gtk_list_store_set (impl->shortcuts_model, &iter,
1710 SHORTCUTS_COL_PIXBUF, pixbuf,
1711 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1712 SHORTCUTS_COL_NAME, label_copy,
1713 SHORTCUTS_COL_DATA, data,
1714 SHORTCUTS_COL_TYPE, shortcut_type,
1715 SHORTCUTS_COL_REMOVABLE, removable,
1716 SHORTCUTS_COL_CANCELLABLE, NULL,
1719 if (impl->shortcuts_pane_filter_model)
1720 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1722 if (impl->shortcuts_combo_filter_model)
1723 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1725 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1727 /* The current folder is updated via _activate_iter(), don't
1728 * have save_folder_combo_changed_cb() call _activate_iter()
1731 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1733 if (impl->has_search)
1736 if (impl->has_recent)
1739 g_signal_handlers_block_by_func (impl->save_folder_combo,
1740 G_CALLBACK (save_folder_combo_changed_cb),
1743 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1744 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1745 G_CALLBACK (save_folder_combo_changed_cb),
1749 g_free (label_copy);
1752 g_object_unref (pixbuf);
1754 profile_end ("end", NULL);
1758 shortcuts_append_search (GtkFileChooserDefault *impl)
1763 pixbuf = render_search_icon (impl);
1765 gtk_list_store_append (impl->shortcuts_model, &iter);
1766 gtk_list_store_set (impl->shortcuts_model, &iter,
1767 SHORTCUTS_COL_PIXBUF, pixbuf,
1768 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1769 SHORTCUTS_COL_NAME, _("Search"),
1770 SHORTCUTS_COL_DATA, NULL,
1771 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1772 SHORTCUTS_COL_REMOVABLE, FALSE,
1776 g_object_unref (pixbuf);
1778 impl->has_search = TRUE;
1782 shortcuts_append_recent (GtkFileChooserDefault *impl)
1787 pixbuf = render_recent_icon (impl);
1789 gtk_list_store_append (impl->shortcuts_model, &iter);
1790 gtk_list_store_set (impl->shortcuts_model, &iter,
1791 SHORTCUTS_COL_PIXBUF, pixbuf,
1792 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1793 SHORTCUTS_COL_NAME, _("Recently Used"),
1794 SHORTCUTS_COL_DATA, NULL,
1795 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1796 SHORTCUTS_COL_REMOVABLE, FALSE,
1800 g_object_unref (pixbuf);
1802 impl->has_recent = TRUE;
1805 /* Appends an item for the user's home directory to the shortcuts model */
1807 shortcuts_append_home (GtkFileChooserDefault *impl)
1809 const char *home_path;
1812 profile_start ("start", NULL);
1814 home_path = g_get_home_dir ();
1815 if (home_path == NULL)
1817 profile_end ("end - no home directory!?", NULL);
1821 home = g_file_new_for_path (home_path);
1822 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1823 impl->has_home = TRUE;
1825 g_object_unref (home);
1827 profile_end ("end", NULL);
1830 /* Appends the ~/Desktop directory to the shortcuts model */
1832 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1837 profile_start ("start", NULL);
1839 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1840 /* "To disable a directory, point it to the homedir."
1841 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1843 if (!g_strcmp0 (name, g_get_home_dir ()))
1845 profile_end ("end", NULL);
1849 file = g_file_new_for_path (name);
1850 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1851 impl->has_desktop = TRUE;
1853 /* We do not actually pop up an error dialog if there is no desktop directory
1854 * because some people may really not want to have one.
1857 g_object_unref (file);
1859 profile_end ("end", NULL);
1862 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1864 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1871 profile_start ("start", NULL);
1873 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1876 for (; bookmarks; bookmarks = bookmarks->next)
1880 file = bookmarks->data;
1882 if (impl->local_only && !g_file_is_native (file))
1885 if (shortcut_find_position (impl, file) != -1)
1888 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1890 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1896 profile_end ("end", NULL);
1898 return num_inserted;
1901 /* Returns the index for the corresponding item in the shortcuts bar */
1903 shortcuts_get_index (GtkFileChooserDefault *impl,
1904 ShortcutsIndex where)
1910 if (where == SHORTCUTS_SEARCH)
1913 n += impl->has_search ? 1 : 0;
1915 if (where == SHORTCUTS_RECENT)
1918 n += impl->has_recent ? 1 : 0;
1920 if (where == SHORTCUTS_RECENT_SEPARATOR)
1923 n += impl->has_recent ? 1 : 0;
1925 if (where == SHORTCUTS_HOME)
1928 n += impl->has_home ? 1 : 0;
1930 if (where == SHORTCUTS_DESKTOP)
1933 n += impl->has_desktop ? 1 : 0;
1935 if (where == SHORTCUTS_VOLUMES)
1938 n += impl->num_volumes;
1940 if (where == SHORTCUTS_SHORTCUTS)
1943 n += impl->num_shortcuts;
1945 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1948 /* If there are no bookmarks there won't be a separator */
1949 n += (impl->num_bookmarks > 0) ? 1 : 0;
1951 if (where == SHORTCUTS_BOOKMARKS)
1954 n += impl->num_bookmarks;
1956 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1961 if (where == SHORTCUTS_CURRENT_FOLDER)
1964 g_assert_not_reached ();
1971 /* Adds all the file system volumes to the shortcuts model */
1973 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1978 gboolean old_changing_folders;
1980 profile_start ("start", NULL);
1982 old_changing_folders = impl->changing_folder;
1983 impl->changing_folder = TRUE;
1985 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1986 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1987 impl->num_volumes = 0;
1989 list = _gtk_file_system_list_volumes (impl->file_system);
1993 for (l = list; l; l = l->next)
1995 GtkFileSystemVolume *volume;
1999 if (impl->local_only)
2001 if (_gtk_file_system_volume_is_mounted (volume))
2004 gboolean base_is_native = TRUE;
2006 base_file = _gtk_file_system_volume_get_root (volume);
2007 if (base_file != NULL)
2009 base_is_native = g_file_is_native (base_file);
2010 g_object_unref (base_file);
2013 if (!base_is_native)
2018 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2022 impl->num_volumes = n;
2023 g_slist_free (list);
2025 if (impl->shortcuts_pane_filter_model)
2026 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2028 if (impl->shortcuts_combo_filter_model)
2029 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2031 impl->changing_folder = old_changing_folders;
2033 profile_end ("end", NULL);
2036 /* Inserts a separator node in the shortcuts list */
2038 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2039 ShortcutsIndex where)
2043 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2044 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2045 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2047 gtk_list_store_insert (impl->shortcuts_model, &iter,
2048 shortcuts_get_index (impl, where));
2049 gtk_list_store_set (impl->shortcuts_model, &iter,
2050 SHORTCUTS_COL_PIXBUF, NULL,
2051 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2052 SHORTCUTS_COL_NAME, NULL,
2053 SHORTCUTS_COL_DATA, NULL,
2054 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2058 /* Updates the list of bookmarks */
2060 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2063 gboolean old_changing_folders;
2065 GFile *list_selected = NULL;
2066 GFile *combo_selected = NULL;
2067 ShortcutType shortcut_type;
2070 profile_start ("start", NULL);
2072 old_changing_folders = impl->changing_folder;
2073 impl->changing_folder = TRUE;
2075 if (shortcuts_get_selected (impl, &iter))
2077 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2079 SHORTCUTS_COL_DATA, &col_data,
2080 SHORTCUTS_COL_TYPE, &shortcut_type,
2083 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2084 list_selected = g_object_ref (col_data);
2087 if (impl->save_folder_combo &&
2088 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2091 GtkTreeIter child_iter;
2093 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2096 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2098 SHORTCUTS_COL_DATA, &col_data,
2099 SHORTCUTS_COL_TYPE, &shortcut_type,
2102 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2103 combo_selected = g_object_ref (col_data);
2106 if (impl->num_bookmarks > 0)
2107 shortcuts_remove_rows (impl,
2108 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2109 impl->num_bookmarks + 1);
2111 impl->num_bookmarks = 0;
2112 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2114 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2115 shortcuts_append_bookmarks (impl, bookmarks);
2116 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2117 g_slist_free (bookmarks);
2119 if (impl->num_bookmarks == 0)
2120 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2122 if (impl->shortcuts_pane_filter_model)
2123 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2125 if (impl->shortcuts_combo_filter_model)
2126 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2130 shortcuts_find_folder (impl, list_selected);
2131 g_object_unref (list_selected);
2138 pos = shortcut_find_position (impl, combo_selected);
2141 if (impl->has_search)
2144 if (impl->has_recent)
2147 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2150 g_object_unref (combo_selected);
2153 impl->changing_folder = old_changing_folders;
2155 profile_end ("end", NULL);
2158 /* Appends a separator and a row to the shortcuts list for the current folder */
2160 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2165 g_assert (!impl->shortcuts_current_folder_active);
2169 g_assert (impl->current_folder != NULL);
2171 pos = shortcut_find_position (impl, impl->current_folder);
2174 GtkFileSystemVolume *volume;
2179 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2183 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2185 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2187 base_file = _gtk_file_system_volume_get_root (volume);
2191 if (base_file && g_file_equal (base_file, impl->current_folder))
2192 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2194 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2197 g_object_unref (base_file);
2199 else if (impl->save_folder_combo != NULL)
2201 if (impl->has_search)
2204 if (impl->has_recent)
2205 pos -= 2; /* + separator */
2207 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2211 /* Updates the current folder row in the shortcuts model */
2213 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2217 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2219 if (impl->shortcuts_current_folder_active)
2221 shortcuts_remove_rows (impl, pos, 2);
2222 impl->shortcuts_current_folder_active = FALSE;
2225 shortcuts_add_current_folder (impl);
2228 /* Filter function used for the shortcuts filter model */
2230 shortcuts_pane_filter_cb (GtkTreeModel *model,
2234 GtkFileChooserDefault *impl;
2238 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2240 path = gtk_tree_model_get_path (model, iter);
2244 pos = *gtk_tree_path_get_indices (path);
2245 gtk_tree_path_free (path);
2247 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2250 /* Creates the list model for shortcuts */
2252 shortcuts_model_create (GtkFileChooserDefault *impl)
2254 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2255 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2256 GDK_TYPE_PIXBUF, /* pixbuf */
2257 G_TYPE_STRING, /* name */
2258 G_TYPE_POINTER, /* path or volume */
2259 G_TYPE_INT, /* ShortcutType */
2260 G_TYPE_BOOLEAN, /* removable */
2261 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2262 G_TYPE_POINTER); /* GCancellable */
2264 if (search_is_possible (impl))
2266 shortcuts_append_search (impl);
2269 if (impl->recent_manager)
2271 shortcuts_append_recent (impl);
2272 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2275 if (impl->file_system)
2277 shortcuts_append_home (impl);
2278 shortcuts_append_desktop (impl);
2279 shortcuts_add_volumes (impl);
2282 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2283 GTK_TREE_MODEL (impl->shortcuts_model),
2286 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2287 shortcuts_pane_filter_cb,
2292 /* Callback used when the "New Folder" button is clicked */
2294 new_folder_button_clicked (GtkButton *button,
2295 GtkFileChooserDefault *impl)
2300 if (!impl->browse_files_model)
2301 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2303 /* Prevent button from being clicked twice */
2304 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2306 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2308 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2309 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2310 path, impl->list_name_column,
2313 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2314 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2316 impl->list_name_column,
2319 gtk_tree_path_free (path);
2322 /* Idle handler for creating a new folder after editing its name cell, or for
2323 * canceling the editing.
2326 edited_idle_cb (GtkFileChooserDefault *impl)
2328 GDK_THREADS_ENTER ();
2330 g_source_destroy (impl->edited_idle);
2331 impl->edited_idle = NULL;
2333 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2334 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2336 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2338 if (impl->edited_new_text /* not cancelled? */
2339 && (strlen (impl->edited_new_text) != 0)
2340 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2342 GError *error = NULL;
2345 file = g_file_get_child_for_display_name (impl->current_folder,
2346 impl->edited_new_text,
2350 GError *error = NULL;
2352 if (g_file_make_directory (file, NULL, &error))
2353 change_folder_and_display_error (impl, file, FALSE);
2355 error_creating_folder_dialog (impl, file, error);
2357 g_object_unref (file);
2360 error_creating_folder_dialog (impl, file, error);
2362 g_free (impl->edited_new_text);
2363 impl->edited_new_text = NULL;
2366 GDK_THREADS_LEAVE ();
2372 queue_edited_idle (GtkFileChooserDefault *impl,
2373 const gchar *new_text)
2375 /* We create the folder in an idle handler so that we don't modify the tree
2379 if (!impl->edited_idle)
2381 impl->edited_idle = g_idle_source_new ();
2382 g_source_set_closure (impl->edited_idle,
2383 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2385 g_source_attach (impl->edited_idle, NULL);
2388 g_free (impl->edited_new_text);
2389 impl->edited_new_text = g_strdup (new_text);
2392 /* Callback used from the text cell renderer when the new folder is named */
2394 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2396 const gchar *new_text,
2397 GtkFileChooserDefault *impl)
2399 /* work around bug #154921 */
2400 g_object_set (cell_renderer_text,
2401 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2402 queue_edited_idle (impl, new_text);
2405 /* Callback used from the text cell renderer when the new folder edition gets
2409 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2410 GtkFileChooserDefault *impl)
2412 /* work around bug #154921 */
2413 g_object_set (cell_renderer_text,
2414 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2415 queue_edited_idle (impl, NULL);
2418 /* Creates the widgets for the filter combo box */
2420 filter_create (GtkFileChooserDefault *impl)
2422 impl->filter_combo = gtk_combo_box_new_text ();
2423 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2425 g_signal_connect (impl->filter_combo, "changed",
2426 G_CALLBACK (filter_combo_changed), impl);
2428 gtk_widget_set_tooltip_text (impl->filter_combo,
2429 _("Select which types of files are shown"));
2431 return impl->filter_combo;
2435 button_new (GtkFileChooserDefault *impl,
2437 const char *stock_id,
2445 button = gtk_button_new_with_mnemonic (text);
2446 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2447 gtk_button_set_image (GTK_BUTTON (button), image);
2449 gtk_widget_set_sensitive (button, sensitive);
2450 g_signal_connect (button, "clicked", callback, impl);
2453 gtk_widget_show (button);
2458 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2460 shortcut_find_position (GtkFileChooserDefault *impl,
2465 int current_folder_separator_idx;
2467 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2470 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2473 /* FIXME: is this still needed? */
2474 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2478 for (i = 0; i < current_folder_separator_idx; i++)
2481 ShortcutType shortcut_type;
2483 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2484 SHORTCUTS_COL_DATA, &col_data,
2485 SHORTCUTS_COL_TYPE, &shortcut_type,
2490 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2492 GtkFileSystemVolume *volume;
2497 base_file = _gtk_file_system_volume_get_root (volume);
2499 exists = base_file && g_file_equal (file, base_file);
2502 g_object_unref (base_file);
2507 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2511 model_file = col_data;
2513 if (model_file && g_file_equal (model_file, file))
2518 if (i < current_folder_separator_idx - 1)
2520 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2521 g_assert_not_reached ();
2528 /* Tries to add a bookmark from a path name */
2530 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2536 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2538 if (shortcut_find_position (impl, file) != -1)
2542 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2544 error_adding_bookmark_dialog (impl, file, error);
2552 add_bookmark_foreach_cb (GtkTreeModel *model,
2557 GtkFileChooserDefault *impl;
2560 impl = (GtkFileChooserDefault *) data;
2562 gtk_tree_model_get (model, iter,
2563 MODEL_COL_FILE, &file,
2566 shortcuts_add_bookmark_from_file (impl, file, -1);
2568 g_object_unref (file);
2571 /* Adds a bookmark from the currently selected item in the file list */
2573 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2575 GtkTreeSelection *selection;
2577 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2579 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2580 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2582 gtk_tree_selection_selected_foreach (selection,
2583 add_bookmark_foreach_cb,
2587 /* Callback used when the "Add bookmark" button is clicked */
2589 add_bookmark_button_clicked_cb (GtkButton *button,
2590 GtkFileChooserDefault *impl)
2592 bookmarks_add_selected_folder (impl);
2595 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2596 * returns FALSE if no shortcut is selected.
2599 shortcuts_get_selected (GtkFileChooserDefault *impl,
2602 GtkTreeSelection *selection;
2603 GtkTreeIter parent_iter;
2605 if (!impl->browse_shortcuts_tree_view)
2608 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2610 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2613 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2619 /* Removes the selected bookmarks */
2621 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2629 if (!shortcuts_get_selected (impl, &iter))
2632 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2633 SHORTCUTS_COL_DATA, &col_data,
2634 SHORTCUTS_COL_REMOVABLE, &removable,
2640 g_assert (col_data != NULL);
2645 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2646 error_removing_bookmark_dialog (impl, file, error);
2649 /* Callback used when the "Remove bookmark" button is clicked */
2651 remove_bookmark_button_clicked_cb (GtkButton *button,
2652 GtkFileChooserDefault *impl)
2654 remove_selected_bookmarks (impl);
2657 struct selection_check_closure {
2658 GtkFileChooserDefault *impl;
2661 gboolean all_folders;
2664 /* Used from gtk_tree_selection_selected_foreach() */
2666 selection_check_foreach_cb (GtkTreeModel *model,
2671 struct selection_check_closure *closure;
2675 gtk_tree_model_get (model, iter,
2676 MODEL_COL_FILE, &file,
2677 MODEL_COL_IS_FOLDER, &is_folder,
2683 g_object_unref (file);
2686 closure->num_selected++;
2688 closure->all_folders = closure->all_folders && is_folder;
2689 closure->all_files = closure->all_files && !is_folder;
2692 /* Checks whether the selected items in the file list are all files or all folders */
2694 selection_check (GtkFileChooserDefault *impl,
2696 gboolean *all_files,
2697 gboolean *all_folders)
2699 struct selection_check_closure closure;
2700 GtkTreeSelection *selection;
2702 closure.impl = impl;
2703 closure.num_selected = 0;
2704 closure.all_files = TRUE;
2705 closure.all_folders = TRUE;
2707 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2708 gtk_tree_selection_selected_foreach (selection,
2709 selection_check_foreach_cb,
2712 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2715 *num_selected = closure.num_selected;
2718 *all_files = closure.all_files;
2721 *all_folders = closure.all_folders;
2724 struct get_selected_file_closure {
2725 GtkFileChooserDefault *impl;
2730 get_selected_file_foreach_cb (GtkTreeModel *model,
2735 struct get_selected_file_closure *closure = data;
2739 /* Just in case this function gets run more than once with a multiple selection; we only care about one file */
2740 g_object_unref (closure->file);
2741 closure->file = NULL;
2744 gtk_tree_model_get (model, iter,
2745 MODEL_COL_FILE, &closure->file, /* this will give us a reffed file */
2749 /* Returns a selected path from the file list */
2751 get_selected_file (GtkFileChooserDefault *impl)
2753 struct get_selected_file_closure closure;
2754 GtkTreeSelection *selection;
2756 closure.impl = impl;
2757 closure.file = NULL;
2759 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2760 gtk_tree_selection_selected_foreach (selection,
2761 get_selected_file_foreach_cb,
2764 return closure.file;
2768 GtkFileChooserDefault *impl;
2770 } UpdateTooltipData;
2773 update_tooltip (GtkTreeModel *model,
2778 UpdateTooltipData *udata = data;
2780 if (udata->tip == NULL)
2782 gchar *display_name;
2784 gtk_tree_model_get (model, iter,
2785 MODEL_COL_NAME, &display_name,
2788 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2790 g_free (display_name);
2795 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2796 * if there are no selected items *and* the current folder is not in the
2797 * bookmarks list. De-sensitize the button otherwise.
2800 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2803 gboolean all_folders;
2807 selection_check (impl, &num_selected, NULL, &all_folders);
2809 if (num_selected == 0)
2810 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2811 else if (num_selected == 1)
2815 file = get_selected_file (impl);
2816 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2818 g_object_unref (file);
2821 active = all_folders;
2823 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2825 if (impl->browse_files_popup_menu_add_shortcut_item)
2826 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2827 (num_selected == 0) ? FALSE : active);
2831 if (num_selected == 0)
2832 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2833 else if (num_selected > 1)
2834 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2837 GtkTreeSelection *selection;
2838 UpdateTooltipData data;
2840 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2843 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2847 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2852 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2853 * bookmark row is selected in the shortcuts tree.
2856 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2859 gboolean removable = FALSE;
2863 if (shortcuts_get_selected (impl, &iter))
2865 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2866 SHORTCUTS_COL_REMOVABLE, &removable,
2867 SHORTCUTS_COL_NAME, &name,
2869 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2872 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2874 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2876 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2880 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2881 _("Remove the selected bookmark"));
2886 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2889 gboolean removable = FALSE;
2891 if (impl->browse_shortcuts_popup_menu == NULL)
2894 if (shortcuts_get_selected (impl, &iter))
2895 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2896 SHORTCUTS_COL_REMOVABLE, &removable,
2899 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2900 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2903 /* GtkWidget::drag-begin handler for the shortcuts list. */
2905 shortcuts_drag_begin_cb (GtkWidget *widget,
2906 GdkDragContext *context,
2907 GtkFileChooserDefault *impl)
2910 impl->shortcuts_drag_context = g_object_ref (context);
2915 /* Removes the idle handler for outside drags */
2917 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2919 if (!impl->shortcuts_drag_outside_idle)
2922 g_source_destroy (impl->shortcuts_drag_outside_idle);
2923 impl->shortcuts_drag_outside_idle = NULL;
2927 /* GtkWidget::drag-end handler for the shortcuts list. */
2929 shortcuts_drag_end_cb (GtkWidget *widget,
2930 GdkDragContext *context,
2931 GtkFileChooserDefault *impl)
2934 g_object_unref (impl->shortcuts_drag_context);
2936 shortcuts_cancel_drag_outside_idle (impl);
2938 if (!impl->shortcuts_drag_outside)
2941 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2943 impl->shortcuts_drag_outside = FALSE;
2947 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2949 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2950 GdkDragContext *context,
2951 GtkFileChooserDefault *impl)
2953 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2957 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2961 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2964 GtkTreeView *tree_view;
2967 GdkPixmap *row_pixmap;
2972 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2974 /* Find the selected path and get its drag pixmap */
2976 if (!shortcuts_get_selected (impl, &iter))
2977 g_assert_not_reached ();
2979 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2981 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2982 gtk_tree_path_free (path);
2991 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2997 GdkPixmap *composite;
2998 int row_pixmap_width, row_pixmap_height;
2999 int pixbuf_width, pixbuf_height;
3000 int composite_width, composite_height;
3001 int pixbuf_x, pixbuf_y;
3002 GdkGC *gc, *mask_gc;
3004 GdkBitmap *pixbuf_mask;
3006 /* Create pixmap and mask for composite image */
3008 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3009 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3010 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3012 composite_width = MAX (row_pixmap_width, pixbuf_width);
3013 composite_height = MAX (row_pixmap_height, pixbuf_height);
3015 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3017 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3020 pixbuf_x = composite_width - pixbuf_width;
3022 pixbuf_y = (composite_height - pixbuf_height) / 2;
3024 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3025 gc = gdk_gc_new (composite);
3027 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3028 mask_gc = gdk_gc_new (mask);
3030 gdk_gc_set_foreground (mask_gc, &color);
3031 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3034 color.green = 0xffff;
3035 color.blue = 0xffff;
3036 gdk_gc_set_rgb_fg_color (gc, &color);
3037 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3039 /* Composite the row pixmap and the pixbuf */
3041 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3043 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3044 NULL, &pixbuf_mask, 128);
3045 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3048 pixbuf_width, pixbuf_height);
3049 g_object_unref (pixbuf_mask);
3051 gdk_draw_drawable (composite, gc, row_pixmap,
3054 row_pixmap_width, row_pixmap_height);
3056 gdk_gc_set_foreground (mask_gc, &color);
3057 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3059 gdk_draw_pixbuf (composite, gc, pixbuf,
3062 pixbuf_width, pixbuf_height,
3066 g_object_unref (pixbuf);
3067 g_object_unref (row_pixmap);
3069 row_pixmap = composite;
3073 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3075 gtk_tree_view_get_path_at_pos (tree_view,
3076 tree_view->priv->press_start_x,
3077 tree_view->priv->press_start_y,
3083 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3084 gdk_drawable_get_colormap (row_pixmap),
3087 tree_view->priv->press_start_x + 1,
3088 row_pixmap_y + cell_y + 1);
3090 g_object_unref (row_pixmap);
3092 g_object_unref (mask);
3095 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3096 * handler so that we can tell apart the drag_leave event that comes right
3097 * before a drag_drop, from a normal drag_leave. We don't want to set the
3098 * cursor nor the flag in the latter case.
3101 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3103 GDK_THREADS_ENTER ();
3105 shortcuts_drag_set_delete_cursor (impl, TRUE);
3106 impl->shortcuts_drag_outside = TRUE;
3108 shortcuts_cancel_drag_outside_idle (impl);
3110 GDK_THREADS_LEAVE ();
3116 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3120 shortcuts_drag_leave_cb (GtkWidget *widget,
3121 GdkDragContext *context,
3123 GtkFileChooserDefault *impl)
3126 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3128 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3129 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3130 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3132 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3136 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3138 GTK_TREE_VIEW_DROP_BEFORE);
3140 g_signal_stop_emission_by_name (widget, "drag-leave");
3143 /* Computes the appropriate row and position for dropping */
3145 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3149 GtkTreeViewDropPosition *pos)
3151 GtkTreeView *tree_view;
3152 GtkTreeViewColumn *column;
3156 int bookmarks_index;
3158 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3160 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3162 if (!gtk_tree_view_get_path_at_pos (tree_view,
3164 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3170 row = bookmarks_index + impl->num_bookmarks - 1;
3171 *path = gtk_tree_path_new_from_indices (row, -1);
3172 *pos = GTK_TREE_VIEW_DROP_AFTER;
3176 row = *gtk_tree_path_get_indices (*path);
3177 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3178 gtk_tree_path_free (*path);
3180 if (row < bookmarks_index)
3182 row = bookmarks_index;
3183 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3185 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3187 row = bookmarks_index + impl->num_bookmarks - 1;
3188 *pos = GTK_TREE_VIEW_DROP_AFTER;
3192 if (cell_y < cell.height / 2)
3193 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3195 *pos = GTK_TREE_VIEW_DROP_AFTER;
3198 *path = gtk_tree_path_new_from_indices (row, -1);
3201 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3202 * implement the destination side of DnD by hand, due to limitations in
3203 * GtkTreeView's DnD API.
3206 shortcuts_drag_motion_cb (GtkWidget *widget,
3207 GdkDragContext *context,
3211 GtkFileChooserDefault *impl)
3214 GtkTreeViewDropPosition pos;
3215 GdkDragAction action;
3218 if (gtk_drag_get_source_widget (context) == widget)
3220 shortcuts_cancel_drag_outside_idle (impl);
3222 if (impl->shortcuts_drag_outside)
3224 shortcuts_drag_set_delete_cursor (impl, FALSE);
3225 impl->shortcuts_drag_outside = FALSE;
3230 if (context->suggested_action == GDK_ACTION_COPY ||
3231 (context->actions & GDK_ACTION_COPY) != 0)
3232 action = GDK_ACTION_COPY;
3233 else if (context->suggested_action == GDK_ACTION_MOVE ||
3234 (context->actions & GDK_ACTION_MOVE) != 0)
3235 action = GDK_ACTION_MOVE;
3242 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3243 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3244 gtk_tree_path_free (path);
3248 g_signal_stop_emission_by_name (widget, "drag-motion");
3252 gdk_drag_status (context, action, time_);
3259 /* GtkWidget::drag-drop handler for the shortcuts list. */
3261 shortcuts_drag_drop_cb (GtkWidget *widget,
3262 GdkDragContext *context,
3266 GtkFileChooserDefault *impl)
3269 shortcuts_cancel_drag_outside_idle (impl);
3272 g_signal_stop_emission_by_name (widget, "drag-drop");
3276 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3278 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3279 GtkSelectionData *selection_data,
3285 uris = gtk_selection_data_get_uris (selection_data);
3289 for (i = 0; uris[i]; i++)
3295 file = g_file_new_for_uri (uri);
3297 if (shortcuts_add_bookmark_from_file (impl, file, position))
3300 g_object_unref (file);
3306 /* Reorders the selected bookmark to the specified position */
3308 shortcuts_reorder (GtkFileChooserDefault *impl,
3313 ShortcutType shortcut_type;
3316 int bookmarks_index;
3321 /* Get the selected path */
3323 if (!shortcuts_get_selected (impl, &iter))
3324 g_assert_not_reached ();
3326 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3327 old_position = *gtk_tree_path_get_indices (path);
3328 gtk_tree_path_free (path);
3330 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3331 old_position -= bookmarks_index;
3332 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3334 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3335 SHORTCUTS_COL_NAME, &name,
3336 SHORTCUTS_COL_DATA, &col_data,
3337 SHORTCUTS_COL_TYPE, &shortcut_type,
3339 g_assert (col_data != NULL);
3340 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3343 g_object_ref (file); /* removal below will free file, so we need a new ref */
3345 /* Remove the path from the old position and insert it in the new one */
3347 if (new_position > old_position)
3350 if (old_position == new_position)
3354 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3356 shortcuts_add_bookmark_from_file (impl, file, new_position);
3357 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3360 error_adding_bookmark_dialog (impl, file, error);
3364 g_object_unref (file);
3367 /* Callback used when we get the drag data for the bookmarks list. We add the
3368 * received URIs as bookmarks if they are folders.
3371 shortcuts_drag_data_received_cb (GtkWidget *widget,
3372 GdkDragContext *context,
3375 GtkSelectionData *selection_data,
3380 GtkFileChooserDefault *impl;
3381 GtkTreePath *tree_path;
3382 GtkTreeViewDropPosition tree_pos;
3384 int bookmarks_index;
3386 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3388 /* Compute position */
3390 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3392 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3393 position = *gtk_tree_path_get_indices (tree_path);
3394 gtk_tree_path_free (tree_path);
3396 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3399 g_assert (position >= bookmarks_index);
3400 position -= bookmarks_index;
3402 if (gtk_targets_include_uri (&selection_data->target, 1))
3403 shortcuts_drop_uris (impl, selection_data, position);
3404 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3405 shortcuts_reorder (impl, position);
3407 g_signal_stop_emission_by_name (widget, "drag-data-received");
3410 /* Callback used to display a tooltip in the shortcuts tree */
3412 shortcuts_query_tooltip_cb (GtkWidget *widget,
3415 gboolean keyboard_mode,
3416 GtkTooltip *tooltip,
3417 GtkFileChooserDefault *impl)
3419 GtkTreeModel *model;
3422 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3430 ShortcutType shortcut_type;
3432 gtk_tree_model_get (model, &iter,
3433 SHORTCUTS_COL_DATA, &col_data,
3434 SHORTCUTS_COL_TYPE, &shortcut_type,
3437 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3439 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3443 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3448 file = G_FILE (col_data);
3449 parse_name = g_file_get_parse_name (file);
3451 gtk_tooltip_set_text (tooltip, parse_name);
3453 g_free (parse_name);
3457 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3461 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3471 /* Callback used when the selection in the shortcuts tree changes */
3473 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3474 GtkFileChooserDefault *impl)
3477 GtkTreeIter child_iter;
3479 bookmarks_check_remove_sensitivity (impl);
3480 shortcuts_check_popup_sensitivity (impl);
3482 if (impl->changing_folder)
3485 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3487 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3490 shortcuts_activate_iter (impl, &child_iter);
3495 shortcuts_row_separator_func (GtkTreeModel *model,
3499 ShortcutType shortcut_type;
3501 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3503 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3507 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3509 GtkFileChooserDefault *impl)
3513 /* don't screw up focus switching with Tab */
3514 if (event->keyval == GDK_Tab
3515 || event->keyval == GDK_KP_Tab
3516 || event->keyval == GDK_ISO_Left_Tab
3517 || event->length < 1)
3520 if (impl->location_entry)
3521 entry = impl->location_entry;
3522 else if (impl->search_entry)
3523 entry = impl->search_entry;
3529 gtk_widget_grab_focus (entry);
3530 return gtk_widget_event (entry, (GdkEvent *) event);
3536 /* Callback used when the file list's popup menu is detached */
3538 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3541 GtkFileChooserDefault *impl;
3543 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3544 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3546 impl->browse_shortcuts_popup_menu = NULL;
3547 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3548 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3552 remove_shortcut_cb (GtkMenuItem *item,
3553 GtkFileChooserDefault *impl)
3555 remove_selected_bookmarks (impl);
3558 /* Rename the selected bookmark */
3560 rename_selected_bookmark (GtkFileChooserDefault *impl)
3564 GtkTreeViewColumn *column;
3565 GtkCellRenderer *cell;
3568 if (shortcuts_get_selected (impl, &iter))
3570 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3571 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3572 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3573 cell = g_list_nth_data (renderers, 1);
3574 g_list_free (renderers);
3575 g_object_set (cell, "editable", TRUE, NULL);
3576 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3577 path, column, cell, TRUE);
3578 gtk_tree_path_free (path);
3583 rename_shortcut_cb (GtkMenuItem *item,
3584 GtkFileChooserDefault *impl)
3586 rename_selected_bookmark (impl);
3589 /* Constructs the popup menu for the file list if needed */
3591 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3595 if (impl->browse_shortcuts_popup_menu)
3598 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3599 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3600 impl->browse_shortcuts_tree_view,
3601 shortcuts_popup_menu_detach_cb);
3603 item = gtk_image_menu_item_new_with_label (_("Remove"));
3604 impl->browse_shortcuts_popup_menu_remove_item = item;
3605 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3606 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3607 g_signal_connect (item, "activate",
3608 G_CALLBACK (remove_shortcut_cb), impl);
3609 gtk_widget_show (item);
3610 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3612 item = gtk_menu_item_new_with_label (_("Rename..."));
3613 impl->browse_shortcuts_popup_menu_rename_item = item;
3614 g_signal_connect (item, "activate",
3615 G_CALLBACK (rename_shortcut_cb), impl);
3616 gtk_widget_show (item);
3617 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3621 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3623 shortcuts_build_popup_menu (impl);
3624 shortcuts_check_popup_sensitivity (impl);
3628 popup_position_func (GtkMenu *menu,
3632 gpointer user_data);
3635 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3636 GdkEventButton *event)
3638 shortcuts_update_popup_menu (impl);
3640 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3641 NULL, NULL, NULL, NULL,
3642 event->button, event->time);
3645 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3647 popup_position_func, impl->browse_shortcuts_tree_view,
3648 0, GDK_CURRENT_TIME);
3649 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3654 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3656 shortcuts_popup_menu_cb (GtkWidget *widget,
3657 GtkFileChooserDefault *impl)
3659 shortcuts_popup_menu (impl, NULL);
3663 /* Callback used when a button is pressed on the shortcuts list.
3664 * We trap button 3 to bring up a popup menu.
3667 shortcuts_button_press_event_cb (GtkWidget *widget,
3668 GdkEventButton *event,
3669 GtkFileChooserDefault *impl)
3671 static gboolean in_press = FALSE;
3677 if (event->button != 3)
3681 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3687 shortcuts_popup_menu (impl, event);
3692 shortcuts_edited (GtkCellRenderer *cell,
3695 GtkFileChooserDefault *impl)
3701 g_object_set (cell, "editable", FALSE, NULL);
3703 path = gtk_tree_path_new_from_string (path_string);
3704 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3705 g_assert_not_reached ();
3707 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3708 SHORTCUTS_COL_DATA, &shortcut,
3710 gtk_tree_path_free (path);
3712 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3716 shortcuts_editing_canceled (GtkCellRenderer *cell,
3717 GtkFileChooserDefault *impl)
3719 g_object_set (cell, "editable", FALSE, NULL);
3722 /* Creates the widgets for the shortcuts and bookmarks tree */
3724 shortcuts_list_create (GtkFileChooserDefault *impl)
3727 GtkTreeSelection *selection;
3728 GtkTreeViewColumn *column;
3729 GtkCellRenderer *renderer;
3731 /* Target types for dragging a row to/from the shortcuts list */
3732 const GtkTargetEntry tree_model_row_targets[] = {
3733 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3736 /* Scrolled window */
3738 swin = gtk_scrolled_window_new (NULL, NULL);
3739 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3740 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3741 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3743 gtk_widget_show (swin);
3747 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3748 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3749 #ifdef PROFILE_FILE_CHOOSER
3750 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3753 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3755 * 1. user brings up a SAVE dialog
3756 * 2. user clicks on a shortcut in the shortcuts pane
3757 * 3. user starts typing a filename
3759 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3760 * support interactive search. However, we'd rather focus the location entry
3761 * so that the user can type *there*.
3763 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3764 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3765 * if the user starts typing while the focus is in the shortcuts pane.
3767 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3768 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3770 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3771 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3772 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3773 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3774 /* Accessible object name for the file chooser's shortcuts pane */
3775 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3777 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3779 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3781 tree_model_row_targets,
3782 G_N_ELEMENTS (tree_model_row_targets),
3785 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3786 GTK_DEST_DEFAULT_ALL,
3787 tree_model_row_targets,
3788 G_N_ELEMENTS (tree_model_row_targets),
3789 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3790 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3792 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3793 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3794 gtk_tree_selection_set_select_function (selection,
3795 shortcuts_select_func,
3798 g_signal_connect (selection, "changed",
3799 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3801 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3802 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3804 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3805 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3806 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3807 G_CALLBACK (shortcuts_drag_end_cb), impl);
3808 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3809 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3811 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3812 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3813 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3814 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3815 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3816 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3817 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3818 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3820 /* Support tooltips */
3821 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3822 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3823 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3825 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3826 gtk_widget_show (impl->browse_shortcuts_tree_view);
3830 column = gtk_tree_view_column_new ();
3831 /* Column header for the file chooser's shortcuts pane */
3832 gtk_tree_view_column_set_title (column, _("_Places"));
3834 renderer = gtk_cell_renderer_pixbuf_new ();
3835 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3836 gtk_tree_view_column_set_attributes (column, renderer,
3837 "pixbuf", SHORTCUTS_COL_PIXBUF,
3838 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3841 renderer = gtk_cell_renderer_text_new ();
3842 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
3843 g_signal_connect (renderer, "edited",
3844 G_CALLBACK (shortcuts_edited), impl);
3845 g_signal_connect (renderer, "editing-canceled",
3846 G_CALLBACK (shortcuts_editing_canceled), impl);
3847 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3848 gtk_tree_view_column_set_attributes (column, renderer,
3849 "text", SHORTCUTS_COL_NAME,
3852 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3853 shortcuts_row_separator_func,
3856 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3861 /* Creates the widgets for the shortcuts/bookmarks pane */
3863 shortcuts_pane_create (GtkFileChooserDefault *impl,
3864 GtkSizeGroup *size_group)
3870 vbox = gtk_vbox_new (FALSE, 6);
3871 gtk_widget_show (vbox);
3873 /* Shortcuts tree */
3875 widget = shortcuts_list_create (impl);
3876 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3878 /* Box for buttons */
3880 hbox = gtk_hbox_new (TRUE, 6);
3881 gtk_size_group_add_widget (size_group, hbox);
3882 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3883 gtk_widget_show (hbox);
3885 /* Add bookmark button */
3887 impl->browse_shortcuts_add_button = button_new (impl,
3892 G_CALLBACK (add_bookmark_button_clicked_cb));
3893 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3894 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3895 _("Add the selected folder to the Bookmarks"));
3897 /* Remove bookmark button */
3899 impl->browse_shortcuts_remove_button = button_new (impl,
3904 G_CALLBACK (remove_bookmark_button_clicked_cb));
3905 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3906 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3907 _("Remove the selected bookmark"));
3913 key_is_left_or_right (GdkEventKey *event)
3917 modifiers = gtk_accelerator_get_default_mod_mask ();
3919 return ((event->keyval == GDK_Right
3920 || event->keyval == GDK_KP_Right
3921 || event->keyval == GDK_Left
3922 || event->keyval == GDK_KP_Left)
3923 && (event->state & modifiers) == 0);
3926 /* Handles key press events on the file list, so that we can trap Enter to
3927 * activate the default button on our own. Also, checks to see if '/' has been
3928 * pressed. See comment by tree_view_keybinding_cb() for more details.
3931 browse_files_key_press_event_cb (GtkWidget *widget,
3935 GtkFileChooserDefault *impl;
3938 impl = (GtkFileChooserDefault *) data;
3940 modifiers = gtk_accelerator_get_default_mod_mask ();
3942 if ((event->keyval == GDK_slash
3943 || event->keyval == GDK_KP_Divide
3945 || event->keyval == GDK_asciitilde
3947 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3949 location_popup_handler (impl, event->string);
3953 if (key_is_left_or_right (event))
3955 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3959 if ((event->keyval == GDK_Return
3960 || event->keyval == GDK_ISO_Enter
3961 || event->keyval == GDK_KP_Enter
3962 || event->keyval == GDK_space
3963 || event->keyval == GDK_KP_Space)
3964 && ((event->state & modifiers) == 0)
3965 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3966 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3970 window = get_toplevel (widget);
3972 && widget != window->default_widget
3973 && !(widget == window->focus_widget &&
3974 (!window->default_widget || !gtk_widget_get_sensitive (window->default_widget))))
3976 gtk_window_activate_default (window);
3984 /* Callback used when the file list's popup menu is detached */
3986 popup_menu_detach_cb (GtkWidget *attach_widget,
3989 GtkFileChooserDefault *impl;
3991 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3992 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3994 impl->browse_files_popup_menu = NULL;
3995 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3996 impl->browse_files_popup_menu_hidden_files_item = NULL;
3999 /* Callback used when the "Add to Bookmarks" menu item is activated */
4001 add_to_shortcuts_cb (GtkMenuItem *item,
4002 GtkFileChooserDefault *impl)
4004 bookmarks_add_selected_folder (impl);
4007 /* Callback used when the "Show Hidden Files" menu item is toggled */
4009 show_hidden_toggled_cb (GtkCheckMenuItem *item,
4010 GtkFileChooserDefault *impl)
4013 "show-hidden", gtk_check_menu_item_get_active (item),
4017 /* Callback used when the "Show Size Column" menu item is toggled */
4019 show_size_column_toggled_cb (GtkCheckMenuItem *item,
4020 GtkFileChooserDefault *impl)
4022 impl->show_size_column = gtk_check_menu_item_get_active (item);
4024 gtk_tree_view_column_set_visible (impl->list_size_column,
4025 impl->show_size_column);
4028 /* Shows an error dialog about not being able to select a dragged file */
4030 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4035 _("Could not select file"),
4040 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4045 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4047 for (i = 1; uris[i]; i++)
4050 GError *error = NULL;
4053 file = g_file_new_for_uri (uri);
4055 gtk_file_chooser_default_select_file (chooser, file, &error);
4057 error_selecting_dragged_file_dialog (impl, file, error);
4059 g_object_unref (file);
4063 struct FileListDragData
4065 GtkFileChooserDefault *impl;
4071 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4073 const GError *error,
4076 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4077 struct FileListDragData *data = user_data;
4078 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4080 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4083 data->impl->file_list_drag_data_received_cancellable = NULL;
4085 if (cancelled || error)
4088 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4089 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4090 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4091 change_folder_and_display_error (data->impl, data->file, FALSE);
4094 GError *error = NULL;
4096 gtk_file_chooser_default_unselect_all (chooser);
4097 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4099 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4101 browse_files_center_selected_row (data->impl);
4104 if (data->impl->select_multiple)
4105 file_list_drag_data_select_uris (data->impl, data->uris);
4108 g_object_unref (data->impl);
4109 g_strfreev (data->uris);
4110 g_object_unref (data->file);
4113 g_object_unref (cancellable);
4117 file_list_drag_data_received_cb (GtkWidget *widget,
4118 GdkDragContext *context,
4121 GtkSelectionData *selection_data,
4126 GtkFileChooserDefault *impl;
4127 GtkFileChooser *chooser;
4132 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4133 chooser = GTK_FILE_CHOOSER (data);
4135 /* Allow only drags from other widgets; see bug #533891. */
4136 if (gtk_drag_get_source_widget (context) == widget)
4138 g_signal_stop_emission_by_name (widget, "drag-data-received");
4142 /* Parse the text/uri-list string, navigate to the first one */
4143 uris = gtk_selection_data_get_uris (selection_data);
4144 if (uris && uris[0])
4146 struct FileListDragData *data;
4149 file = g_file_new_for_uri (uri);
4151 data = g_new0 (struct FileListDragData, 1);
4152 data->impl = g_object_ref (impl);
4156 if (impl->file_list_drag_data_received_cancellable)
4157 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4159 impl->file_list_drag_data_received_cancellable =
4160 _gtk_file_system_get_info (impl->file_system, file,
4162 file_list_drag_data_received_get_info_cb,
4166 g_signal_stop_emission_by_name (widget, "drag-data-received");
4169 /* Don't do anything with the drag_drop signal */
4171 file_list_drag_drop_cb (GtkWidget *widget,
4172 GdkDragContext *context,
4176 GtkFileChooserDefault *impl)
4178 g_signal_stop_emission_by_name (widget, "drag-drop");
4182 /* Disable the normal tree drag motion handler, it makes it look like you're
4183 dropping the dragged item onto a tree item */
4185 file_list_drag_motion_cb (GtkWidget *widget,
4186 GdkDragContext *context,
4190 GtkFileChooserDefault *impl)
4192 g_signal_stop_emission_by_name (widget, "drag-motion");
4196 /* Constructs the popup menu for the file list if needed */
4198 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4202 if (impl->browse_files_popup_menu)
4205 impl->browse_files_popup_menu = gtk_menu_new ();
4206 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4207 impl->browse_files_tree_view,
4208 popup_menu_detach_cb);
4210 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4211 impl->browse_files_popup_menu_add_shortcut_item = item;
4212 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4213 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4214 g_signal_connect (item, "activate",
4215 G_CALLBACK (add_to_shortcuts_cb), impl);
4216 gtk_widget_show (item);
4217 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4219 item = gtk_separator_menu_item_new ();
4220 gtk_widget_show (item);
4221 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4223 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4224 impl->browse_files_popup_menu_hidden_files_item = item;
4225 g_signal_connect (item, "toggled",
4226 G_CALLBACK (show_hidden_toggled_cb), impl);
4227 gtk_widget_show (item);
4228 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4230 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4231 impl->browse_files_popup_menu_size_column_item = item;
4232 g_signal_connect (item, "toggled",
4233 G_CALLBACK (show_size_column_toggled_cb), impl);
4234 gtk_widget_show (item);
4235 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4237 bookmarks_check_add_sensitivity (impl);
4240 /* Updates the popup menu for the file list, creating it if necessary */
4242 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4244 file_list_build_popup_menu (impl);
4246 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4248 /* The sensitivity of the Add to Bookmarks item is set in
4249 * bookmarks_check_add_sensitivity()
4252 /* 'Show Hidden Files' */
4253 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4254 G_CALLBACK (show_hidden_toggled_cb), impl);
4255 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4257 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4258 G_CALLBACK (show_hidden_toggled_cb), impl);
4260 /* 'Show Size Column' */
4261 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4262 G_CALLBACK (show_size_column_toggled_cb), impl);
4263 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4264 impl->show_size_column);
4265 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4266 G_CALLBACK (show_size_column_toggled_cb), impl);
4270 popup_position_func (GtkMenu *menu,
4276 GtkWidget *widget = GTK_WIDGET (user_data);
4277 GdkScreen *screen = gtk_widget_get_screen (widget);
4280 GdkRectangle monitor;
4282 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4284 gdk_window_get_origin (widget->window, x, y);
4286 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4288 *x += (widget->allocation.width - req.width) / 2;
4289 *y += (widget->allocation.height - req.height) / 2;
4291 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4292 gtk_menu_set_monitor (menu, monitor_num);
4293 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4295 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4296 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4302 file_list_popup_menu (GtkFileChooserDefault *impl,
4303 GdkEventButton *event)
4305 file_list_update_popup_menu (impl);
4307 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4308 NULL, NULL, NULL, NULL,
4309 event->button, event->time);
4312 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4314 popup_position_func, impl->browse_files_tree_view,
4315 0, GDK_CURRENT_TIME);
4316 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4322 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4324 list_popup_menu_cb (GtkWidget *widget,
4325 GtkFileChooserDefault *impl)
4327 file_list_popup_menu (impl, NULL);
4331 /* Callback used when a button is pressed on the file list. We trap button 3 to
4332 * bring up a popup menu.
4335 list_button_press_event_cb (GtkWidget *widget,
4336 GdkEventButton *event,
4337 GtkFileChooserDefault *impl)
4339 static gboolean in_press = FALSE;
4345 if (event->button != 3)
4349 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4352 file_list_popup_menu (impl, event);
4357 OperationMode operation_mode;
4358 gint general_column;
4362 /* Sets the sort column IDs for the file list based on the operation mode */
4364 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4366 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4367 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4368 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4372 file_list_query_tooltip_cb (GtkWidget *widget,
4375 gboolean keyboard_tip,
4376 GtkTooltip *tooltip,
4379 GtkFileChooserDefault *impl = user_data;
4380 GtkTreeModel *model;
4386 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4390 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4393 &model, &path, &iter))
4396 gtk_tree_model_get (model, &iter,
4397 MODEL_COL_FILE, &file,
4402 gtk_tree_path_free (path);
4406 filename = g_file_get_path (file);
4407 gtk_tooltip_set_text (tooltip, filename);
4408 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4413 g_object_unref (file);
4414 gtk_tree_path_free (path);
4420 set_icon_cell_renderer_fixed_size (GtkFileChooserDefault *impl, GtkCellRenderer *renderer)
4422 gtk_cell_renderer_set_fixed_size (renderer,
4423 renderer->xpad * 2 + impl->icon_size,
4424 renderer->ypad * 2 + impl->icon_size);
4427 /* Creates the widgets for the file list */
4429 create_file_list (GtkFileChooserDefault *impl)
4432 GtkTreeSelection *selection;
4433 GtkTreeViewColumn *column;
4434 GtkCellRenderer *renderer;
4436 /* Scrolled window */
4437 swin = gtk_scrolled_window_new (NULL, NULL);
4438 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4439 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4440 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4443 /* Tree/list view */
4445 impl->browse_files_tree_view = gtk_tree_view_new ();
4446 #ifdef PROFILE_FILE_CHOOSER
4447 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4449 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4450 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4452 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4453 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4455 gtk_drag_dest_set (impl->browse_files_tree_view,
4456 GTK_DEST_DEFAULT_ALL,
4458 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4459 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4461 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4462 G_CALLBACK (list_row_activated), impl);
4463 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4464 G_CALLBACK (browse_files_key_press_event_cb), impl);
4465 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4466 G_CALLBACK (list_popup_menu_cb), impl);
4467 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4468 G_CALLBACK (list_button_press_event_cb), impl);
4470 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4471 G_CALLBACK (file_list_drag_data_received_cb), impl);
4472 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4473 G_CALLBACK (file_list_drag_drop_cb), impl);
4474 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4475 G_CALLBACK (file_list_drag_motion_cb), impl);
4477 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4478 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4479 G_CALLBACK (file_list_query_tooltip_cb), impl);
4481 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4482 gtk_tree_selection_set_select_function (selection,
4485 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4488 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4489 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4491 g_signal_connect (selection, "changed",
4492 G_CALLBACK (list_selection_changed), impl);
4494 /* Keep the column order in sync with update_cell_renderer_attributes() */
4496 /* Filename column */
4498 impl->list_name_column = gtk_tree_view_column_new ();
4499 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4500 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4501 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4503 renderer = gtk_cell_renderer_pixbuf_new ();
4504 /* We set a fixed size so that we get an empty slot even if no icons are loaded yet */
4505 set_icon_cell_renderer_fixed_size (impl, renderer);
4506 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4508 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4509 g_object_set (impl->list_name_renderer,
4510 "ellipsize", PANGO_ELLIPSIZE_END,
4512 g_signal_connect (impl->list_name_renderer, "edited",
4513 G_CALLBACK (renderer_edited_cb), impl);
4514 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4515 G_CALLBACK (renderer_editing_canceled_cb), impl);
4516 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4518 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4522 column = gtk_tree_view_column_new ();
4523 gtk_tree_view_column_set_resizable (column, TRUE);
4524 gtk_tree_view_column_set_title (column, _("Size"));
4526 renderer = gtk_cell_renderer_text_new ();
4527 g_object_set (renderer,
4528 "alignment", PANGO_ALIGN_RIGHT,
4530 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4531 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4532 impl->list_size_column = column;
4534 /* Modification time column */
4536 column = gtk_tree_view_column_new ();
4537 gtk_tree_view_column_set_resizable (column, TRUE);
4538 gtk_tree_view_column_set_title (column, _("Modified"));
4540 renderer = gtk_cell_renderer_text_new ();
4541 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4542 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4543 impl->list_mtime_column = column;
4545 file_list_set_sort_column_ids (impl);
4546 update_cell_renderer_attributes (impl);
4548 gtk_widget_show_all (swin);
4554 create_path_bar (GtkFileChooserDefault *impl)
4556 GtkWidget *path_bar;
4558 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4559 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4564 /* Creates the widgets for the files/folders pane */
4566 file_pane_create (GtkFileChooserDefault *impl,
4567 GtkSizeGroup *size_group)
4573 vbox = gtk_vbox_new (FALSE, 6);
4574 gtk_widget_show (vbox);
4576 /* Box for lists and preview */
4578 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4579 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4580 gtk_widget_show (hbox);
4584 widget = create_file_list (impl);
4585 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4589 impl->preview_box = gtk_vbox_new (FALSE, 12);
4590 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4591 /* Don't show preview box initially */
4595 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4597 widget = filter_create (impl);
4599 gtk_widget_show (widget);
4600 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4602 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4603 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4608 /* Callback used when the "Browse for more folders" expander is toggled */
4610 expander_changed_cb (GtkExpander *expander,
4612 GtkFileChooserDefault *impl)
4614 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4615 update_appearance (impl);
4618 /* Callback used when the selection changes in the save folder combo box */
4620 save_folder_combo_changed_cb (GtkComboBox *combo,
4621 GtkFileChooserDefault *impl)
4625 if (impl->changing_folder)
4628 if (gtk_combo_box_get_active_iter (combo, &iter))
4630 GtkTreeIter child_iter;
4632 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4635 shortcuts_activate_iter (impl, &child_iter);
4640 save_folder_update_tooltip (GtkComboBox *combo,
4641 GtkFileChooserDefault *impl)
4648 if (gtk_combo_box_get_active_iter (combo, &iter))
4650 GtkTreeIter child_iter;
4652 ShortcutType shortcut_type;
4654 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4657 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4658 SHORTCUTS_COL_DATA, &col_data,
4659 SHORTCUTS_COL_TYPE, &shortcut_type,
4662 if (shortcut_type == SHORTCUT_TYPE_FILE)
4663 tooltip = g_file_get_parse_name (G_FILE (col_data));
4666 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4667 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4668 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4672 /* Filter function used to filter out the Search item and its separator.
4673 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4676 shortcuts_combo_filter_func (GtkTreeModel *model,
4680 GtkFileChooserDefault *impl;
4681 GtkTreePath *tree_path;
4686 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4688 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4690 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4691 g_assert (tree_path != NULL);
4693 indices = gtk_tree_path_get_indices (tree_path);
4697 if (impl->has_search)
4699 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4700 if (idx == indices[0])
4704 if (impl->has_recent)
4706 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4707 if (idx == indices[0])
4711 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4712 if (idx == indices[0])
4717 gtk_tree_path_free (tree_path);
4722 /* Creates the combo box with the save folders */
4724 save_folder_combo_create (GtkFileChooserDefault *impl)
4727 GtkCellRenderer *cell;
4729 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4730 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4731 shortcuts_combo_filter_func,
4735 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4736 "model", impl->shortcuts_combo_filter_model,
4737 "focus-on-click", FALSE,
4739 gtk_widget_show (combo);
4741 cell = gtk_cell_renderer_pixbuf_new ();
4742 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4743 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4744 "pixbuf", SHORTCUTS_COL_PIXBUF,
4745 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4746 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4749 cell = gtk_cell_renderer_text_new ();
4750 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4751 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4752 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4753 "text", SHORTCUTS_COL_NAME,
4754 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4757 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4758 shortcuts_row_separator_func,
4761 g_signal_connect (combo, "changed",
4762 G_CALLBACK (save_folder_combo_changed_cb), impl);
4763 g_signal_connect (combo, "changed",
4764 G_CALLBACK (save_folder_update_tooltip), impl);
4769 /* Creates the widgets specific to Save mode */
4771 save_widgets_create (GtkFileChooserDefault *impl)
4776 GtkWidget *alignment;
4778 if (impl->save_widgets != NULL)
4781 location_switch_to_path_bar (impl);
4783 vbox = gtk_vbox_new (FALSE, 12);
4785 table = gtk_table_new (2, 2, FALSE);
4786 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4787 gtk_widget_show (table);
4788 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4789 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4793 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4794 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4795 gtk_table_attach (GTK_TABLE (table), widget,
4799 gtk_widget_show (widget);
4801 /* Location entry */
4803 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4804 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4806 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4807 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4808 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4809 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4811 GTK_EXPAND | GTK_FILL, 0,
4813 gtk_widget_show (impl->location_entry);
4814 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4817 impl->save_folder_label = gtk_label_new (NULL);
4818 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4819 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4823 gtk_widget_show (impl->save_folder_label);
4825 impl->save_folder_combo = save_folder_combo_create (impl);
4826 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4828 GTK_EXPAND | GTK_FILL, GTK_FILL,
4830 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4833 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4834 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4836 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4837 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4838 g_signal_connect (impl->save_expander, "notify::expanded",
4839 G_CALLBACK (expander_changed_cb),
4841 gtk_widget_show_all (alignment);
4843 impl->save_widgets = vbox;
4844 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4845 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4846 gtk_widget_show (impl->save_widgets);
4849 /* Destroys the widgets specific to Save mode */
4851 save_widgets_destroy (GtkFileChooserDefault *impl)
4853 if (impl->save_widgets == NULL)
4856 gtk_widget_destroy (impl->save_widgets);
4857 impl->save_widgets = NULL;
4858 impl->location_entry = NULL;
4859 impl->save_folder_label = NULL;
4860 impl->save_folder_combo = NULL;
4861 impl->save_expander = NULL;
4864 /* Turns on the path bar widget. Can be called even if we are already in that
4868 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4870 if (impl->location_entry)
4872 gtk_widget_destroy (impl->location_entry);
4873 impl->location_entry = NULL;
4876 gtk_widget_hide (impl->location_entry_box);
4879 /* Sets the full path of the current folder as the text in the location entry. */
4881 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4883 gchar *text, *filename;
4885 if (!impl->current_folder)
4888 filename = g_file_get_path (impl->current_folder);
4892 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4896 text = g_file_get_uri (impl->current_folder);
4900 gboolean need_slash;
4903 len = strlen (text);
4904 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4910 slash_text = g_new (char, len + 2);
4911 strcpy (slash_text, text);
4912 slash_text[len] = G_DIR_SEPARATOR;
4913 slash_text[len + 1] = 0;
4919 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4926 /* Turns on the location entry. Can be called even if we are already in that
4930 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4932 /* when in search or recent files mode, we are not showing the
4933 * location_entry_box container, so there's no point in switching
4936 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4937 impl->operation_mode == OPERATION_MODE_RECENT)
4940 if (impl->location_entry)
4941 gtk_widget_destroy (impl->location_entry);
4945 gtk_widget_show (impl->location_entry_box);
4949 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4950 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4952 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4953 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4955 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4956 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4958 /* Configure the entry */
4960 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4964 gtk_widget_show (impl->location_entry);
4965 gtk_widget_grab_focus (impl->location_entry);
4968 /* Sets a new location mode. set_buttons determines whether the toggle button
4969 * for the mode will also be changed.
4972 location_mode_set (GtkFileChooserDefault *impl,
4973 LocationMode new_mode,
4974 gboolean set_button)
4976 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4977 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4979 GtkWindow *toplevel;
4980 GtkWidget *current_focus;
4981 gboolean button_active;
4982 gboolean switch_to_file_list;
4986 case LOCATION_MODE_PATH_BAR:
4987 button_active = FALSE;
4989 /* The location_entry will disappear when we switch to path bar mode. So,
4990 * we'll focus the file list in that case, to avoid having a window with
4991 * no focused widget.
4993 toplevel = get_toplevel (GTK_WIDGET (impl));
4994 switch_to_file_list = FALSE;
4997 current_focus = gtk_window_get_focus (toplevel);
4998 if (!current_focus || current_focus == impl->location_entry)
4999 switch_to_file_list = TRUE;
5002 location_switch_to_path_bar (impl);
5004 if (switch_to_file_list)
5005 gtk_widget_grab_focus (impl->browse_files_tree_view);
5009 case LOCATION_MODE_FILENAME_ENTRY:
5010 button_active = TRUE;
5011 location_switch_to_filename_entry (impl);
5015 g_assert_not_reached ();
5021 g_signal_handlers_block_by_func (impl->location_button,
5022 G_CALLBACK (location_button_toggled_cb), impl);
5024 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5026 g_signal_handlers_unblock_by_func (impl->location_button,
5027 G_CALLBACK (location_button_toggled_cb), impl);
5031 impl->location_mode = new_mode;
5035 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5037 /* when in search or recent files mode, we are not showing the
5038 * location_entry_box container, so there's no point in switching
5041 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5042 impl->operation_mode == OPERATION_MODE_RECENT)
5045 /* If the file entry is not visible, show it.
5046 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5048 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5050 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5052 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5054 if (gtk_widget_has_focus (impl->location_entry))
5056 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5060 gtk_widget_grab_focus (impl->location_entry);
5065 /* Callback used when one of the location mode buttons is toggled */
5067 location_button_toggled_cb (GtkToggleButton *toggle,
5068 GtkFileChooserDefault *impl)
5071 LocationMode new_mode;
5073 is_active = gtk_toggle_button_get_active (toggle);
5077 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5078 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5082 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5083 new_mode = LOCATION_MODE_PATH_BAR;
5086 location_mode_set (impl, new_mode, FALSE);
5089 /* Creates a toggle button for the location entry. */
5091 location_button_create (GtkFileChooserDefault *impl)
5096 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5097 gtk_widget_show (image);
5099 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5103 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5105 g_signal_connect (impl->location_button, "toggled",
5106 G_CALLBACK (location_button_toggled_cb), impl);
5108 str = _("Type a file name");
5110 gtk_widget_set_tooltip_text (impl->location_button, str);
5111 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5114 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5116 browse_widgets_create (GtkFileChooserDefault *impl)
5121 GtkSizeGroup *size_group;
5123 /* size group is used by the [+][-] buttons and the filter combo */
5124 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5125 vbox = gtk_vbox_new (FALSE, 12);
5127 /* Location widgets */
5128 impl->browse_path_bar_hbox = gtk_hbox_new (FALSE, 12);
5129 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5130 gtk_widget_show (impl->browse_path_bar_hbox);
5132 /* Size group that allows the path bar to be the same size between modes */
5133 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5134 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5136 /* Location button */
5138 location_button_create (impl);
5139 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5143 impl->browse_path_bar = create_path_bar (impl);
5144 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5145 gtk_widget_show_all (impl->browse_path_bar);
5146 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5149 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5150 g_signal_connect (impl->browse_new_folder_button, "clicked",
5151 G_CALLBACK (new_folder_button_clicked), impl);
5152 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5154 /* Box for the location label and entry */
5156 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5157 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5159 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5160 gtk_widget_show (impl->location_label);
5161 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5164 hpaned = gtk_hpaned_new ();
5165 gtk_widget_show (hpaned);
5166 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5168 widget = shortcuts_pane_create (impl, size_group);
5169 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5170 widget = file_pane_create (impl, size_group);
5171 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5173 g_object_unref (size_group);
5179 gtk_file_chooser_default_constructor (GType type,
5180 guint n_construct_properties,
5181 GObjectConstructParam *construct_params)
5183 GtkFileChooserDefault *impl;
5186 profile_start ("start", NULL);
5188 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5189 n_construct_properties,
5191 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5193 g_assert (impl->file_system);
5195 gtk_widget_push_composite_child ();
5197 /* Shortcuts model */
5198 shortcuts_model_create (impl);
5200 /* The browse widgets */
5201 impl->browse_widgets = browse_widgets_create (impl);
5202 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5204 /* Alignment to hold extra widget */
5205 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5206 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5208 gtk_widget_pop_composite_child ();
5209 update_appearance (impl);
5211 profile_end ("end", NULL);
5216 /* Sets the extra_widget by packing it in the appropriate place */
5218 set_extra_widget (GtkFileChooserDefault *impl,
5219 GtkWidget *extra_widget)
5223 g_object_ref (extra_widget);
5224 /* FIXME: is this right ? */
5225 gtk_widget_show (extra_widget);
5228 if (impl->extra_widget)
5230 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5231 g_object_unref (impl->extra_widget);
5234 impl->extra_widget = extra_widget;
5235 if (impl->extra_widget)
5237 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5238 gtk_widget_show (impl->extra_align);
5241 gtk_widget_hide (impl->extra_align);
5245 set_local_only (GtkFileChooserDefault *impl,
5246 gboolean local_only)
5248 if (local_only != impl->local_only)
5250 impl->local_only = local_only;
5252 if (impl->location_entry)
5253 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5255 if (impl->shortcuts_model && impl->file_system)
5257 shortcuts_add_volumes (impl);
5258 shortcuts_add_bookmarks (impl);
5261 if (local_only && impl->current_folder &&
5262 !g_file_is_native (impl->current_folder))
5264 /* If we are pointing to a non-local folder, make an effort to change
5265 * back to a local folder, but it's really up to the app to not cause
5266 * such a situation, so we ignore errors.
5268 const gchar *home = g_get_home_dir ();
5274 home_file = g_file_new_for_path (home);
5276 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5278 g_object_unref (home_file);
5284 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5285 GtkFileChooserDefault *impl)
5287 shortcuts_add_volumes (impl);
5288 shortcuts_add_bookmarks (impl);
5290 bookmarks_check_add_sensitivity (impl);
5291 bookmarks_check_remove_sensitivity (impl);
5292 shortcuts_check_popup_sensitivity (impl);
5295 /* Sets the file chooser to multiple selection mode */
5297 set_select_multiple (GtkFileChooserDefault *impl,
5298 gboolean select_multiple,
5299 gboolean property_notify)
5301 GtkTreeSelection *selection;
5302 GtkSelectionMode mode;
5304 if (select_multiple == impl->select_multiple)
5307 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5309 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5310 gtk_tree_selection_set_mode (selection, mode);
5312 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5314 impl->select_multiple = select_multiple;
5315 g_object_notify (G_OBJECT (impl), "select-multiple");
5317 check_preview_change (impl);
5321 set_file_system_backend (GtkFileChooserDefault *impl)
5323 profile_start ("start for backend", "default");
5325 impl->file_system = _gtk_file_system_new ();
5327 g_signal_connect (impl->file_system, "volumes-changed",
5328 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5329 g_signal_connect (impl->file_system, "bookmarks-changed",
5330 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5332 profile_end ("end", NULL);
5336 unset_file_system_backend (GtkFileChooserDefault *impl)
5338 g_signal_handlers_disconnect_by_func (impl->file_system,
5339 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5341 g_object_unref (impl->file_system);
5343 impl->file_system = NULL;
5346 /* This function is basically a do_all function.
5348 * It sets the visibility on all the widgets based on the current state, and
5349 * moves the custom_widget if needed.
5352 update_appearance (GtkFileChooserDefault *impl)
5354 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5355 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5359 gtk_widget_hide (impl->location_button);
5360 save_widgets_create (impl);
5362 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5363 text = _("Save in _folder:");
5365 text = _("Create in _folder:");
5367 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5369 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5371 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5372 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5373 gtk_widget_set_has_tooltip (impl->save_folder_combo, FALSE);
5374 gtk_widget_show (impl->browse_widgets);
5378 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5379 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5380 gtk_widget_set_has_tooltip (impl->save_folder_combo, TRUE);
5381 gtk_widget_hide (impl->browse_widgets);
5384 if (impl->select_multiple)
5386 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5387 "Re-setting to single selection mode.");
5388 set_select_multiple (impl, FALSE, TRUE);
5391 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5392 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5394 gtk_widget_show (impl->location_button);
5395 save_widgets_destroy (impl);
5396 gtk_widget_show (impl->browse_widgets);
5397 location_mode_set (impl, impl->location_mode, TRUE);
5400 if (impl->location_entry)
5401 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5403 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5404 gtk_widget_hide (impl->browse_new_folder_button);
5406 gtk_widget_show (impl->browse_new_folder_button);
5408 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5409 * of files may change depending whether we are in a file or folder-only mode.
5411 gtk_widget_queue_draw (impl->browse_files_tree_view);
5413 emit_default_size_changed (impl);
5417 gtk_file_chooser_default_set_property (GObject *object,
5419 const GValue *value,
5423 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5427 case GTK_FILE_CHOOSER_PROP_ACTION:
5429 GtkFileChooserAction action = g_value_get_enum (value);
5431 if (action != impl->action)
5433 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5435 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5436 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5437 && impl->select_multiple)
5439 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5440 "this is not allowed in multiple selection mode. Resetting the file chooser "
5441 "to single selection mode.");
5442 set_select_multiple (impl, FALSE, TRUE);
5444 impl->action = action;
5445 update_cell_renderer_attributes (impl);
5446 update_appearance (impl);
5447 settings_load (impl);
5452 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5453 /* Ignore property */
5456 case GTK_FILE_CHOOSER_PROP_FILTER:
5457 set_current_filter (impl, g_value_get_object (value));
5460 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5461 set_local_only (impl, g_value_get_boolean (value));
5464 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5465 set_preview_widget (impl, g_value_get_object (value));
5468 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5469 impl->preview_widget_active = g_value_get_boolean (value);
5470 update_preview_widget_visibility (impl);
5473 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5474 impl->use_preview_label = g_value_get_boolean (value);
5475 update_preview_widget_visibility (impl);
5478 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5479 set_extra_widget (impl, g_value_get_object (value));
5482 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5484 gboolean select_multiple = g_value_get_boolean (value);
5485 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5486 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5489 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5490 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5491 "leaving the file chooser in single selection mode.");
5495 set_select_multiple (impl, select_multiple, FALSE);
5499 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5501 gboolean show_hidden = g_value_get_boolean (value);
5502 if (show_hidden != impl->show_hidden)
5504 impl->show_hidden = show_hidden;
5506 if (impl->browse_files_model)
5507 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5512 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5514 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5515 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5519 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5521 gboolean create_folders = g_value_get_boolean (value);
5522 impl->create_folders = create_folders;
5523 update_appearance (impl);
5528 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5534 gtk_file_chooser_default_get_property (GObject *object,
5539 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5543 case GTK_FILE_CHOOSER_PROP_ACTION:
5544 g_value_set_enum (value, impl->action);
5547 case GTK_FILE_CHOOSER_PROP_FILTER:
5548 g_value_set_object (value, impl->current_filter);
5551 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5552 g_value_set_boolean (value, impl->local_only);
5555 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5556 g_value_set_object (value, impl->preview_widget);
5559 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5560 g_value_set_boolean (value, impl->preview_widget_active);
5563 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5564 g_value_set_boolean (value, impl->use_preview_label);
5567 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5568 g_value_set_object (value, impl->extra_widget);
5571 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5572 g_value_set_boolean (value, impl->select_multiple);
5575 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5576 g_value_set_boolean (value, impl->show_hidden);
5579 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5580 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5583 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5584 g_value_set_boolean (value, impl->create_folders);
5588 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5593 /* Removes the settings signal handler. It's safe to call multiple times */
5595 remove_settings_signal (GtkFileChooserDefault *impl,
5598 if (impl->settings_signal_id)
5600 GtkSettings *settings;
5602 settings = gtk_settings_get_for_screen (screen);
5603 g_signal_handler_disconnect (settings,
5604 impl->settings_signal_id);
5605 impl->settings_signal_id = 0;
5610 gtk_file_chooser_default_dispose (GObject *object)
5613 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5615 if (impl->extra_widget)
5617 g_object_unref (impl->extra_widget);
5618 impl->extra_widget = NULL;
5621 pending_select_files_free (impl);
5623 /* cancel all pending operations */
5624 if (impl->pending_cancellables)
5626 for (l = impl->pending_cancellables; l; l = l->next)
5628 GCancellable *cancellable = G_CANCELLABLE (l->data);
5629 g_cancellable_cancel (cancellable);
5631 g_slist_free (impl->pending_cancellables);
5632 impl->pending_cancellables = NULL;
5635 if (impl->reload_icon_cancellables)
5637 for (l = impl->reload_icon_cancellables; l; l = l->next)
5639 GCancellable *cancellable = G_CANCELLABLE (l->data);
5640 g_cancellable_cancel (cancellable);
5642 g_slist_free (impl->reload_icon_cancellables);
5643 impl->reload_icon_cancellables = NULL;
5646 if (impl->loading_shortcuts)
5648 for (l = impl->loading_shortcuts; l; l = l->next)
5650 GCancellable *cancellable = G_CANCELLABLE (l->data);
5651 g_cancellable_cancel (cancellable);
5653 g_slist_free (impl->loading_shortcuts);
5654 impl->loading_shortcuts = NULL;
5657 if (impl->file_list_drag_data_received_cancellable)
5659 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5660 impl->file_list_drag_data_received_cancellable = NULL;
5663 if (impl->update_current_folder_cancellable)
5665 g_cancellable_cancel (impl->update_current_folder_cancellable);
5666 impl->update_current_folder_cancellable = NULL;
5669 if (impl->should_respond_get_info_cancellable)
5671 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5672 impl->should_respond_get_info_cancellable = NULL;
5675 if (impl->update_from_entry_cancellable)
5677 g_cancellable_cancel (impl->update_from_entry_cancellable);
5678 impl->update_from_entry_cancellable = NULL;
5681 if (impl->shortcuts_activate_iter_cancellable)
5683 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5684 impl->shortcuts_activate_iter_cancellable = NULL;
5687 search_stop_searching (impl, TRUE);
5688 recent_stop_loading (impl);
5690 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5692 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5695 /* We override show-all since we have internal widgets that
5696 * shouldn't be shown when you call show_all(), like the filter
5700 gtk_file_chooser_default_show_all (GtkWidget *widget)
5702 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5704 gtk_widget_show (widget);
5706 if (impl->extra_widget)
5707 gtk_widget_show_all (impl->extra_widget);
5710 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5711 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5714 toplevel_set_focus_cb (GtkWindow *window,
5716 GtkFileChooserDefault *impl)
5718 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5721 /* We monitor the focus widget on our toplevel to be able to know which widget
5722 * was last focused at the time our "should_respond" method gets called.
5725 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5726 GtkWidget *previous_toplevel)
5728 GtkFileChooserDefault *impl;
5729 GtkWidget *toplevel;
5731 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5733 if (previous_toplevel)
5735 g_assert (impl->toplevel_set_focus_id != 0);
5736 g_signal_handler_disconnect (previous_toplevel,
5737 impl->toplevel_set_focus_id);
5738 impl->toplevel_set_focus_id = 0;
5739 impl->toplevel_last_focus_widget = NULL;
5742 g_assert (impl->toplevel_set_focus_id == 0);
5744 toplevel = gtk_widget_get_toplevel (widget);
5745 if (GTK_IS_WINDOW (toplevel))
5747 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5748 G_CALLBACK (toplevel_set_focus_cb), impl);
5749 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5753 /* Changes the icons wherever it is needed */
5755 change_icon_theme (GtkFileChooserDefault *impl)
5757 GtkSettings *settings;
5759 GtkCellRenderer *renderer;
5762 profile_start ("start", NULL);
5764 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5766 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5767 impl->icon_size = MAX (width, height);
5769 impl->icon_size = FALLBACK_ICON_SIZE;
5771 shortcuts_reload_icons (impl);
5772 /* the first cell in the first column is the icon column, and we have a fixed size there */
5773 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5774 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5775 renderer = GTK_CELL_RENDERER (cells->data);
5776 set_icon_cell_renderer_fixed_size (impl, renderer);
5777 g_list_free (cells);
5778 if (impl->browse_files_model)
5779 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5780 gtk_widget_queue_resize (impl->browse_files_tree_view);
5782 profile_end ("end", NULL);
5785 /* Callback used when a GtkSettings value changes */
5787 settings_notify_cb (GObject *object,
5789 GtkFileChooserDefault *impl)
5793 profile_start ("start", NULL);
5795 name = g_param_spec_get_name (pspec);
5797 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5798 strcmp (name, "gtk-icon-sizes") == 0)
5799 change_icon_theme (impl);
5801 profile_end ("end", NULL);
5804 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5808 check_icon_theme (GtkFileChooserDefault *impl)
5810 GtkSettings *settings;
5812 profile_start ("start", NULL);
5814 if (impl->settings_signal_id)
5816 profile_end ("end", NULL);
5820 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5822 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5823 impl->settings_signal_id = g_signal_connect (settings, "notify",
5824 G_CALLBACK (settings_notify_cb), impl);
5826 change_icon_theme (impl);
5829 profile_end ("end", NULL);
5833 gtk_file_chooser_default_style_set (GtkWidget *widget,
5834 GtkStyle *previous_style)
5836 GtkFileChooserDefault *impl;
5838 profile_start ("start", NULL);
5840 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5842 profile_msg (" parent class style_set start", NULL);
5843 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5844 profile_msg (" parent class style_set end", NULL);
5846 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5847 change_icon_theme (impl);
5849 emit_default_size_changed (impl);
5851 profile_end ("end", NULL);
5855 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5856 GdkScreen *previous_screen)
5858 GtkFileChooserDefault *impl;
5860 profile_start ("start", NULL);
5862 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5864 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5865 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5867 remove_settings_signal (impl, previous_screen);
5868 check_icon_theme (impl);
5870 emit_default_size_changed (impl);
5872 profile_end ("end", NULL);
5876 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5877 GtkAllocation *allocation)
5879 GtkFileChooserDefault *impl;
5881 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5883 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5887 set_sort_column (GtkFileChooserDefault *impl)
5889 GtkTreeSortable *sortable;
5891 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5892 /* can happen when we're still populating the model */
5893 if (sortable == NULL)
5896 gtk_tree_sortable_set_sort_column_id (sortable,
5902 settings_load (GtkFileChooserDefault *impl)
5904 GtkFileChooserSettings *settings;
5905 LocationMode location_mode;
5906 gboolean show_hidden;
5907 gboolean expand_folders;
5908 gboolean show_size_column;
5910 GtkSortType sort_order;
5912 settings = _gtk_file_chooser_settings_new ();
5914 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5915 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5916 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5917 show_size_column = _gtk_file_chooser_settings_get_show_size_column (settings);
5918 sort_column = _gtk_file_chooser_settings_get_sort_column (settings);
5919 sort_order = _gtk_file_chooser_settings_get_sort_order (settings);
5921 g_object_unref (settings);
5923 location_mode_set (impl, location_mode, TRUE);
5925 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5927 impl->expand_folders = expand_folders;
5928 if (impl->save_expander)
5929 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5931 impl->show_size_column = show_size_column;
5932 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5934 impl->sort_column = sort_column;
5935 impl->sort_order = sort_order;
5936 /* We don't call set_sort_column() here as the models may not have been
5937 * created yet. The individual functions that create and set the models will
5938 * call set_sort_column() themselves.
5943 save_dialog_geometry (GtkFileChooserDefault *impl, GtkFileChooserSettings *settings)
5945 GtkWindow *toplevel;
5946 int x, y, width, height;
5948 /* We don't save the geometry in non-expanded "save" mode, so that the "little
5949 * dialog" won't make future Open dialogs too small.
5951 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5952 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
5953 || impl->expand_folders))
5956 toplevel = get_toplevel (GTK_WIDGET (impl));
5958 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5961 gtk_window_get_position (toplevel, &x, &y);
5962 gtk_window_get_size (toplevel, &width, &height);
5964 _gtk_file_chooser_settings_set_geometry (settings, x, y, width, height);
5968 settings_save (GtkFileChooserDefault *impl)
5970 GtkFileChooserSettings *settings;
5972 settings = _gtk_file_chooser_settings_new ();
5974 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5975 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5976 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
5977 _gtk_file_chooser_settings_set_show_size_column (settings, impl->show_size_column);
5978 _gtk_file_chooser_settings_set_sort_column (settings, impl->sort_column);
5979 _gtk_file_chooser_settings_set_sort_order (settings, impl->sort_order);
5981 save_dialog_geometry (impl, settings);
5984 _gtk_file_chooser_settings_save (settings, NULL);
5986 g_object_unref (settings);
5989 /* GtkWidget::realize method */
5991 gtk_file_chooser_default_realize (GtkWidget *widget)
5993 GtkFileChooserDefault *impl;
5995 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5997 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
5999 emit_default_size_changed (impl);
6002 /* GtkWidget::map method */
6004 gtk_file_chooser_default_map (GtkWidget *widget)
6006 GtkFileChooserDefault *impl;
6007 char *current_working_dir;
6009 profile_start ("start", NULL);
6011 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6013 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6015 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6017 switch (impl->reload_state)
6020 /* The user didn't explicitly give us a folder to
6021 * display, so we'll use the cwd
6023 current_working_dir = g_get_current_dir ();
6024 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6025 current_working_dir);
6026 g_free (current_working_dir);
6029 case RELOAD_HAS_FOLDER:
6030 /* Nothing; we are already loading or loaded, so we
6031 * don't need to reload
6035 case RELOAD_WAS_UNMAPPED:
6036 /* Just reload the current folder; else continue
6039 if (impl->current_folder)
6041 pending_select_files_store_selection (impl);
6042 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6047 g_assert_not_reached ();
6051 volumes_bookmarks_changed_cb (impl->file_system, impl);
6053 settings_load (impl);
6055 profile_end ("end", NULL);
6058 /* GtkWidget::unmap method */
6060 gtk_file_chooser_default_unmap (GtkWidget *widget)
6062 GtkFileChooserDefault *impl;
6064 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6066 settings_save (impl);
6068 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6070 impl->reload_state = RELOAD_WAS_UNMAPPED;
6074 install_list_model_filter (GtkFileChooserDefault *impl)
6076 _gtk_file_system_model_set_filter (impl->browse_files_model,
6077 impl->current_filter);
6080 #define COMPARE_DIRECTORIES \
6081 GtkFileChooserDefault *impl = user_data; \
6082 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6083 gboolean dir_a, dir_b; \
6085 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6086 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6088 if (dir_a != dir_b) \
6089 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6091 /* Sort callback for the filename column */
6093 name_sort_func (GtkTreeModel *model,
6098 COMPARE_DIRECTORIES;
6101 const char *key_a, *key_b;
6104 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6105 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6108 result = strcmp (key_a, key_b);
6120 /* Sort callback for the size column */
6122 size_sort_func (GtkTreeModel *model,
6127 COMPARE_DIRECTORIES;
6130 gint64 size_a, size_b;
6132 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6133 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6135 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6139 /* Sort callback for the mtime column */
6141 mtime_sort_func (GtkTreeModel *model,
6146 COMPARE_DIRECTORIES;
6151 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6152 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6154 return ta > tb ? -1 : (ta == tb ? 0 : 1);
6158 /* Callback used when the sort column changes. We cache the sort order for use
6159 * in name_sort_func().
6162 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6163 GtkFileChooserDefault *impl)
6165 gint sort_column_id;
6166 GtkSortType sort_type;
6168 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6170 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6171 impl->sort_column = sort_column_id;
6172 impl->sort_order = sort_type;
6177 set_busy_cursor (GtkFileChooserDefault *impl,
6180 GtkWindow *toplevel;
6181 GdkDisplay *display;
6184 toplevel = get_toplevel (GTK_WIDGET (impl));
6185 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6188 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6191 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6195 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6196 gdk_display_flush (display);
6199 gdk_cursor_unref (cursor);
6202 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6204 load_set_model (GtkFileChooserDefault *impl)
6206 profile_start ("start", NULL);
6208 g_assert (impl->browse_files_model != NULL);
6210 profile_msg (" gtk_tree_view_set_model start", NULL);
6211 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6212 GTK_TREE_MODEL (impl->browse_files_model));
6213 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6214 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6216 set_sort_column (impl);
6217 profile_msg (" gtk_tree_view_set_model end", NULL);
6218 impl->list_sort_ascending = TRUE;
6220 profile_end ("end", NULL);
6223 /* Timeout callback used when the loading timer expires */
6225 load_timeout_cb (gpointer data)
6227 GtkFileChooserDefault *impl;
6229 profile_start ("start", NULL);
6231 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6232 g_assert (impl->load_state == LOAD_PRELOAD);
6233 g_assert (impl->load_timeout_id != 0);
6234 g_assert (impl->browse_files_model != NULL);
6236 impl->load_timeout_id = 0;
6237 impl->load_state = LOAD_LOADING;
6239 load_set_model (impl);
6241 profile_end ("end", NULL);
6246 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6248 load_setup_timer (GtkFileChooserDefault *impl)
6250 g_assert (impl->load_timeout_id == 0);
6251 g_assert (impl->load_state != LOAD_PRELOAD);
6253 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6254 impl->load_state = LOAD_PRELOAD;
6257 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6259 load_remove_timer (GtkFileChooserDefault *impl)
6261 if (impl->load_timeout_id != 0)
6263 g_assert (impl->load_state == LOAD_PRELOAD);
6265 g_source_remove (impl->load_timeout_id);
6266 impl->load_timeout_id = 0;
6267 impl->load_state = LOAD_EMPTY;
6270 g_assert (impl->load_state == LOAD_EMPTY ||
6271 impl->load_state == LOAD_LOADING ||
6272 impl->load_state == LOAD_FINISHED);
6275 /* Selects the first row in the file list */
6277 browse_files_select_first_row (GtkFileChooserDefault *impl)
6280 GtkTreeIter dummy_iter;
6281 GtkTreeModel *tree_model;
6283 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6288 path = gtk_tree_path_new_from_indices (0, -1);
6290 /* If the list is empty, do nothing. */
6291 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6292 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6294 gtk_tree_path_free (path);
6297 struct center_selected_row_closure {
6298 GtkFileChooserDefault *impl;
6299 gboolean already_centered;
6302 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6303 * selected row in the tree view.
6306 center_selected_row_foreach_cb (GtkTreeModel *model,
6311 struct center_selected_row_closure *closure;
6314 if (closure->already_centered)
6317 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6318 closure->already_centered = TRUE;
6321 /* Centers the selected row in the tree view */
6323 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6325 struct center_selected_row_closure closure;
6326 GtkTreeSelection *selection;
6328 closure.impl = impl;
6329 closure.already_centered = FALSE;
6331 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6332 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6336 show_and_select_files (GtkFileChooserDefault *impl,
6339 GtkTreeSelection *selection;
6340 GtkFileSystemModel *fsmodel;
6341 gboolean can_have_hidden, can_have_filtered, selected_a_file;
6344 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6345 fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
6346 can_have_hidden = !impl->show_hidden;
6347 can_have_filtered = impl->current_filter != NULL;
6348 selected_a_file = FALSE;
6350 for (walk = files; walk && (can_have_hidden || can_have_filtered); walk = walk->next)
6352 GFile *file = walk->data;
6355 if (!_gtk_file_system_model_get_iter_for_file (fsmodel, &iter, file))
6358 if (!_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6360 GFileInfo *info = _gtk_file_system_model_get_info (fsmodel, &iter);
6362 if (can_have_hidden &&
6363 (g_file_info_get_is_hidden (info) ||
6364 g_file_info_get_is_backup (info)))
6366 g_object_set (impl, "show-hidden", TRUE, NULL);
6367 can_have_hidden = FALSE;
6370 if (can_have_filtered)
6372 set_current_filter (impl, NULL);
6373 can_have_filtered = FALSE;
6377 if (_gtk_file_system_model_iter_is_visible (fsmodel, &iter))
6379 gtk_tree_selection_select_iter (selection, &iter);
6380 selected_a_file = TRUE;
6384 browse_files_center_selected_row (impl);
6386 return selected_a_file;
6389 /* Processes the pending operation when a folder is finished loading */
6391 pending_select_files_process (GtkFileChooserDefault *impl)
6393 g_assert (impl->load_state == LOAD_FINISHED);
6394 g_assert (impl->browse_files_model != NULL);
6396 if (impl->pending_select_files)
6398 show_and_select_files (impl, impl->pending_select_files);
6399 pending_select_files_free (impl);
6400 browse_files_center_selected_row (impl);
6404 /* We only select the first row if the chooser is actually mapped ---
6405 * selecting the first row is to help the user when he is interacting with
6406 * the chooser, but sometimes a chooser works not on behalf of the user,
6407 * but rather on behalf of something else like GtkFileChooserButton. In
6408 * that case, the chooser's selection should be what the caller expects,
6409 * as the user can't see that something else got selected. See bug #165264.
6411 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6412 browse_files_select_first_row (impl);
6415 g_assert (impl->pending_select_files == NULL);
6419 show_error_on_reading_current_folder (GtkFileChooserDefault *impl, GError *error)
6424 info = g_file_query_info (impl->current_folder,
6425 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
6426 G_FILE_QUERY_INFO_NONE,
6431 msg = g_strdup_printf (_("Could not read the contents of %s"), g_file_info_get_display_name (info));
6432 g_object_unref (info);
6435 msg = g_strdup (_("Could not read the contents of the folder"));
6437 error_message (impl, msg, error->message);
6441 /* Callback used when the file system model finishes loading */
6443 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6445 GtkFileChooserDefault *impl)
6447 profile_start ("start", NULL);
6450 show_error_on_reading_current_folder (impl, error);
6452 if (impl->load_state == LOAD_PRELOAD)
6454 load_remove_timer (impl);
6455 load_set_model (impl);
6457 else if (impl->load_state == LOAD_LOADING)
6463 /* We can't g_assert_not_reached(), as something other than us may have
6464 * initiated a folder reload. See #165556.
6466 profile_end ("end", NULL);
6470 g_assert (impl->load_timeout_id == 0);
6472 impl->load_state = LOAD_FINISHED;
6474 pending_select_files_process (impl);
6475 set_busy_cursor (impl, FALSE);
6476 #ifdef PROFILE_FILE_CHOOSER
6477 access ("MARK: *** FINISHED LOADING", F_OK);
6480 profile_end ("end", NULL);
6484 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
6485 gboolean remove_from_treeview)
6487 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6489 if (impl->browse_files_model)
6491 g_object_unref (impl->browse_files_model);
6492 impl->browse_files_model = NULL;
6495 if (remove_from_treeview)
6496 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6500 my_g_format_time_for_display (glong secs)
6505 time_t time_mtime, time_now;
6506 const gchar *format;
6507 gchar *locale_format = NULL;
6509 char *date_str = NULL;
6511 const char *locale, *dot = NULL;
6512 gint64 codepage = -1;
6518 #ifdef HAVE_LOCALTIME_R
6519 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6522 struct tm *ptm = localtime ((time_t *) &time_mtime);
6526 g_warning ("ptm != NULL failed");
6528 return g_strdup (_("Unknown"));
6531 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6533 #endif /* HAVE_LOCALTIME_R */
6535 g_date_set_time_t (&mtime, time_mtime);
6536 time_now = time (NULL);
6537 g_date_set_time_t (&now, time_now);
6539 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6541 /* Translators: %H means "hours" and %M means "minutes" */
6543 format = _("%H:%M");
6544 else if (days_diff == 1)
6545 format = _("Yesterday at %H:%M");
6548 if (days_diff > 1 && days_diff < 7)
6549 format = "%A"; /* Days from last week */
6551 format = "%x"; /* Any other date */
6555 /* g_locale_from_utf8() returns a string in the system
6556 * code-page, which is not always the same as that used by the C
6557 * library. For instance when running a GTK+ program with
6558 * LANG=ko on an English version of Windows, the system
6559 * code-page is 1252, but the code-page used by the C library is
6560 * 949. (It's GTK+ itself that sets the C library locale when it
6561 * notices the LANG environment variable. See gtkmain.c The
6562 * Microsoft C library doesn't look at any locale environment
6563 * variables.) We need to pass strftime() a string in the C
6564 * library's code-page. See bug #509885.
6566 locale = setlocale (LC_ALL, NULL);
6568 dot = strchr (locale, '.');
6571 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6573 /* All codepages should fit in 16 bits AFAIK */
6574 if (codepage > 0 && codepage < 65536)
6576 sprintf (charset, "CP%u", (guint) codepage);
6577 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6581 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6583 if (locale_format != NULL &&
6584 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6587 /* As above but in opposite direction... */
6588 if (codepage > 0 && codepage < 65536)
6589 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6591 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6595 if (date_str == NULL)
6596 date_str = g_strdup (_("Unknown"));
6598 g_free (locale_format);
6603 copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
6605 GFileAttributeType type;
6608 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL))
6609 g_file_info_set_attribute (to, attribute, type, value);
6613 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6615 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6616 GFile *file = G_FILE (object);
6617 GFileInfo *queried, *info;
6620 queried = g_file_query_info_finish (file, res, NULL);
6621 if (queried == NULL)
6624 /* now we know model is valid */
6626 /* file was deleted */
6627 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6630 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6632 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6633 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6634 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6636 _gtk_file_system_model_update_file (model, file, info, FALSE);
6638 g_object_unref (info);
6642 file_system_model_set (GtkFileSystemModel *model,
6649 GtkFileChooserDefault *impl = data;
6653 case MODEL_COL_FILE:
6654 g_value_set_object (value, file);
6656 case MODEL_COL_NAME:
6658 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6660 g_value_set_string (value, g_file_info_get_display_name (info));
6662 case MODEL_COL_NAME_COLLATED:
6664 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6666 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6668 case MODEL_COL_IS_FOLDER:
6669 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6671 case MODEL_COL_PIXBUF:
6674 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6676 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6680 GtkTreeModel *tree_model;
6681 GtkTreePath *path, *start, *end;
6684 if (impl->browse_files_tree_view == NULL ||
6685 g_file_info_has_attribute (info, "filechooser::queried"))
6688 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6689 if (tree_model != GTK_TREE_MODEL (model))
6692 if (!_gtk_file_system_model_get_iter_for_file (model,
6695 g_assert_not_reached ();
6696 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6698 path = gtk_tree_model_get_path (tree_model, &iter);
6699 if (gtk_tree_path_compare (start, path) != 1 &&
6700 gtk_tree_path_compare (path, end) != 1)
6702 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6703 g_file_query_info_async (file,
6704 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6705 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6706 G_FILE_ATTRIBUTE_STANDARD_ICON,
6707 G_FILE_QUERY_INFO_NONE,
6709 _gtk_file_system_model_get_cancellable (model),
6710 file_system_model_got_thumbnail,
6713 gtk_tree_path_free (path);
6714 gtk_tree_path_free (start);
6715 gtk_tree_path_free (end);
6720 g_value_set_object (value, NULL);
6722 case MODEL_COL_SIZE:
6723 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6725 case MODEL_COL_SIZE_TEXT:
6726 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6727 g_value_set_string (value, NULL);
6729 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6731 case MODEL_COL_MTIME:
6732 case MODEL_COL_MTIME_TEXT:
6737 g_file_info_get_modification_time (info, &tv);
6738 if (column == MODEL_COL_MTIME)
6739 g_value_set_long (value, tv.tv_sec);
6740 else if (tv.tv_sec == 0)
6741 g_value_set_static_string (value, _("Unknown"));
6743 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6746 case MODEL_COL_ELLIPSIZE:
6747 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6750 g_assert_not_reached ();
6757 /* Gets rid of the old list model and creates a new one for the current folder */
6759 set_list_model (GtkFileChooserDefault *impl,
6762 g_assert (impl->current_folder != NULL);
6764 profile_start ("start", NULL);
6766 stop_loading_and_clear_list_model (impl, TRUE);
6768 set_busy_cursor (impl, TRUE);
6770 impl->browse_files_model =
6771 _gtk_file_system_model_new_for_directory (impl->current_folder,
6773 file_system_model_set,
6775 MODEL_COLUMN_TYPES);
6777 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6779 profile_msg (" set sort function", NULL);
6780 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6781 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6782 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6783 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6784 set_sort_column (impl);
6785 impl->list_sort_ascending = TRUE;
6786 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6787 G_CALLBACK (list_sort_column_changed_cb), impl);
6789 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6791 g_signal_connect (impl->browse_files_model, "finished-loading",
6792 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6794 install_list_model_filter (impl);
6796 profile_end ("end", NULL);
6801 struct update_chooser_entry_selected_foreach_closure {
6803 GtkTreeIter first_selected_iter;
6807 compare_utf8_filenames (const gchar *a,
6810 gchar *a_folded, *b_folded;
6813 a_folded = g_utf8_strdown (a, -1);
6814 b_folded = g_utf8_strdown (b, -1);
6816 retval = strcmp (a_folded, b_folded);
6825 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6830 struct update_chooser_entry_selected_foreach_closure *closure;
6833 closure->num_selected++;
6835 if (closure->num_selected == 1)
6836 closure->first_selected_iter = *iter;
6840 update_chooser_entry (GtkFileChooserDefault *impl)
6842 GtkTreeSelection *selection;
6843 struct update_chooser_entry_selected_foreach_closure closure;
6844 const char *file_part;
6846 /* no need to update the file chooser's entry if there's no entry */
6847 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6848 impl->operation_mode == OPERATION_MODE_RECENT ||
6849 !impl->location_entry)
6852 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6853 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6854 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6855 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6856 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6859 g_assert (impl->location_entry != NULL);
6861 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6862 closure.num_selected = 0;
6863 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6867 if (closure.num_selected == 0)
6869 goto maybe_clear_entry;
6871 else if (closure.num_selected == 1)
6873 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6876 gboolean change_entry;
6878 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6880 /* If the cursor moved to the row of the newly created folder,
6881 * retrieving info will return NULL.
6886 g_free (impl->browse_files_last_selected_name);
6887 impl->browse_files_last_selected_name =
6888 g_strdup (g_file_info_get_display_name (info));
6890 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6891 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6892 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6894 /* We don't want the name to change when clicking on a folder... */
6895 change_entry = (! _gtk_file_info_consider_as_directory (info));
6898 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
6902 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6904 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6905 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
6913 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6914 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6916 /* Multiple selection, so just clear the entry. */
6918 g_free (impl->browse_files_last_selected_name);
6919 impl->browse_files_last_selected_name = NULL;
6921 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6927 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6928 && impl->browse_files_last_selected_name)
6930 const char *entry_text;
6932 gboolean clear_entry;
6934 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6935 len = strlen (entry_text);
6938 /* The file chooser entry may have appended a "/" to its text. So
6939 * take it out, and compare the result to the old selection.
6941 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6945 tmp = g_strndup (entry_text, len - 1);
6946 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6950 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6953 clear_entry = FALSE;
6956 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6961 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6965 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
6969 struct UpdateCurrentFolderData
6971 GtkFileChooserDefault *impl;
6973 gboolean keep_trail;
6974 gboolean clear_entry;
6975 GFile *original_file;
6976 GError *original_error;
6980 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
6981 GtkFileSystemVolume *volume,
6982 const GError *error,
6985 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6986 struct UpdateCurrentFolderData *data = user_data;
6987 GtkFileChooserDefault *impl = data->impl;
6989 if (cancellable != impl->update_current_folder_cancellable)
6992 impl->update_current_folder_cancellable = NULL;
6993 set_busy_cursor (impl, FALSE);
7000 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7001 impl->reload_state = RELOAD_EMPTY;
7005 change_folder_and_display_error (impl, data->file, data->clear_entry);
7008 g_object_unref (data->file);
7011 g_object_unref (cancellable);
7015 update_current_folder_get_info_cb (GCancellable *cancellable,
7017 const GError *error,
7020 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7021 struct UpdateCurrentFolderData *data = user_data;
7022 GtkFileChooserDefault *impl = data->impl;
7024 if (cancellable != impl->update_current_folder_cancellable)
7027 impl->update_current_folder_cancellable = NULL;
7028 impl->reload_state = RELOAD_EMPTY;
7030 set_busy_cursor (impl, FALSE);
7039 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7041 GMountOperation *mount_operation;
7042 GtkWidget *toplevel;
7044 g_object_unref (cancellable);
7045 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7047 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7049 set_busy_cursor (impl, TRUE);
7051 impl->update_current_folder_cancellable =
7052 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7054 update_current_folder_mount_enclosing_volume_cb,
7060 if (!data->original_file)
7062 data->original_file = g_object_ref (data->file);
7063 data->original_error = g_error_copy (error);
7066 parent_file = g_file_get_parent (data->file);
7068 /* get parent path and try to change the folder to that */
7071 g_object_unref (data->file);
7072 data->file = parent_file;
7074 g_object_unref (cancellable);
7076 /* restart the update current folder operation */
7077 impl->reload_state = RELOAD_HAS_FOLDER;
7079 impl->update_current_folder_cancellable =
7080 _gtk_file_system_get_info (impl->file_system, data->file,
7082 update_current_folder_get_info_cb,
7085 set_busy_cursor (impl, TRUE);
7091 /* Error and bail out, ignoring "not found" errors since they're useless:
7092 * they only happen when a program defaults to a folder that has been (re)moved.
7094 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7095 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7097 g_error_free (data->original_error);
7099 g_object_unref (data->original_file);
7105 if (data->original_file)
7107 /* Error and bail out, ignoring "not found" errors since they're useless:
7108 * they only happen when a program defaults to a folder that has been (re)moved.
7110 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7111 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7113 g_error_free (data->original_error);
7115 g_object_unref (data->original_file);
7118 if (! _gtk_file_info_consider_as_directory (info))
7121 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7124 if (impl->current_folder != data->file)
7126 if (impl->current_folder)
7127 g_object_unref (impl->current_folder);
7129 impl->current_folder = g_object_ref (data->file);
7132 impl->reload_state = RELOAD_HAS_FOLDER;
7134 /* Update the widgets that may trigger a folder change themselves. */
7136 if (!impl->changing_folder)
7138 impl->changing_folder = TRUE;
7140 shortcuts_update_current_folder (impl);
7142 impl->changing_folder = FALSE;
7145 /* Set the folder on the save entry */
7147 if (impl->location_entry)
7149 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7150 impl->current_folder);
7152 if (data->clear_entry)
7153 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7156 /* Create a new list model. This is slightly evil; we store the result value
7157 * but perform more actions rather than returning immediately even if it
7158 * generates an error.
7160 set_list_model (impl, NULL);
7162 /* Refresh controls */
7164 shortcuts_find_current_folder (impl);
7166 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7168 check_preview_change (impl);
7169 bookmarks_check_add_sensitivity (impl);
7171 g_signal_emit_by_name (impl, "selection-changed", 0);
7174 g_object_unref (data->file);
7177 g_object_unref (cancellable);
7181 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7183 gboolean keep_trail,
7184 gboolean clear_entry,
7187 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7188 struct UpdateCurrentFolderData *data;
7190 profile_start ("start", NULL);
7192 g_object_ref (file);
7194 switch (impl->operation_mode)
7196 case OPERATION_MODE_SEARCH:
7197 search_switch_to_browse_mode (impl);
7199 case OPERATION_MODE_RECENT:
7200 recent_switch_to_browse_mode (impl);
7202 case OPERATION_MODE_BROWSE:
7206 if (impl->local_only && !g_file_is_native (file))
7208 g_set_error_literal (error,
7209 GTK_FILE_CHOOSER_ERROR,
7210 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7211 _("Cannot change to folder because it is not local"));
7213 g_object_unref (file);
7214 profile_end ("end - not local", NULL);
7218 if (impl->update_current_folder_cancellable)
7219 g_cancellable_cancel (impl->update_current_folder_cancellable);
7221 /* Test validity of path here. */
7222 data = g_new0 (struct UpdateCurrentFolderData, 1);
7224 data->file = g_object_ref (file);
7225 data->keep_trail = keep_trail;
7226 data->clear_entry = clear_entry;
7228 impl->reload_state = RELOAD_HAS_FOLDER;
7230 impl->update_current_folder_cancellable =
7231 _gtk_file_system_get_info (impl->file_system, file,
7233 update_current_folder_get_info_cb,
7236 set_busy_cursor (impl, TRUE);
7237 g_object_unref (file);
7239 profile_end ("end", NULL);
7244 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7246 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7248 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7249 impl->operation_mode == OPERATION_MODE_RECENT)
7252 if (impl->reload_state == RELOAD_EMPTY)
7254 char *current_working_dir;
7257 /* We are unmapped, or we had an error while loading the last folder. We'll return
7258 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7259 * explicitly calls set_current_folder() on us.
7261 current_working_dir = g_get_current_dir ();
7262 file = g_file_new_for_path (current_working_dir);
7263 g_free (current_working_dir);
7267 if (impl->current_folder)
7268 return g_object_ref (impl->current_folder);
7274 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7277 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7279 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7280 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7282 pending_select_files_free (impl);
7283 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7287 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7291 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7295 parent_file = g_file_get_parent (file);
7298 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7300 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7301 impl->operation_mode == OPERATION_MODE_RECENT ||
7302 impl->load_state == LOAD_EMPTY)
7308 g_assert (impl->current_folder != NULL);
7310 same_path = g_file_equal (parent_file, impl->current_folder);
7313 if (same_path && impl->load_state == LOAD_FINISHED)
7318 files.data = (gpointer) file;
7321 result = show_and_select_files (impl, &files);
7322 g_object_unref (parent_file);
7326 pending_select_files_add (impl, file);
7332 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7333 g_object_unref (parent_file);
7337 g_object_unref (parent_file);
7342 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7345 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7346 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7349 if (!impl->browse_files_model)
7352 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7357 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7362 maybe_select (GtkTreeModel *model,
7367 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7368 GtkTreeSelection *selection;
7371 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7373 gtk_tree_model_get (model, iter,
7374 MODEL_COL_IS_FOLDER, &is_folder,
7377 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7378 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7379 gtk_tree_selection_select_iter (selection, iter);
7381 gtk_tree_selection_unselect_iter (selection, iter);
7387 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7389 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7391 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7392 impl->operation_mode == OPERATION_MODE_RECENT)
7394 GtkTreeSelection *selection;
7396 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7397 gtk_tree_selection_select_all (selection);
7401 if (impl->select_multiple)
7402 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7403 maybe_select, impl);
7407 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7409 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7410 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7412 gtk_tree_selection_unselect_all (selection);
7413 pending_select_files_free (impl);
7416 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7418 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7420 * is_empty_ret - whether the file entry is totally empty
7422 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7423 * the path will be "$cwd/foobar")
7426 check_save_entry (GtkFileChooserDefault *impl,
7428 gboolean *is_well_formed_ret,
7429 gboolean *is_empty_ret,
7430 gboolean *is_file_part_empty_ret,
7431 gboolean *is_folder)
7433 GtkFileChooserEntry *chooser_entry;
7434 GFile *current_folder;
7435 const char *file_part;
7439 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7440 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7441 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7442 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7443 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7445 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7447 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7450 *is_well_formed_ret = TRUE;
7451 *is_empty_ret = TRUE;
7452 *is_file_part_empty_ret = TRUE;
7458 *is_empty_ret = FALSE;
7460 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7461 if (!current_folder)
7464 *is_well_formed_ret = FALSE;
7465 *is_file_part_empty_ret = FALSE;
7471 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7473 if (!file_part || file_part[0] == '\0')
7475 *file_ret = g_object_ref (current_folder);
7476 *is_well_formed_ret = TRUE;
7477 *is_file_part_empty_ret = TRUE;
7483 *is_file_part_empty_ret = FALSE;
7486 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7490 error_building_filename_dialog (impl, error);
7492 *is_well_formed_ret = FALSE;
7499 *is_well_formed_ret = TRUE;
7500 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7503 struct get_files_closure {
7504 GtkFileChooserDefault *impl;
7506 GFile *file_from_entry;
7510 get_files_foreach (GtkTreeModel *model,
7515 struct get_files_closure *info;
7517 GtkFileSystemModel *fs_model;
7520 fs_model = info->impl->browse_files_model;
7522 file = _gtk_file_system_model_get_file (fs_model, iter);
7524 return; /* We are on the editable row */
7526 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7527 info->result = g_slist_prepend (info->result, g_object_ref (file));
7531 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7533 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7534 struct get_files_closure info;
7535 GtkWindow *toplevel;
7536 GtkWidget *current_focus;
7537 gboolean file_list_seen;
7539 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7540 return search_get_selected_files (impl);
7542 if (impl->operation_mode == OPERATION_MODE_RECENT)
7543 return recent_get_selected_files (impl);
7547 info.file_from_entry = NULL;
7549 toplevel = get_toplevel (GTK_WIDGET (impl));
7551 current_focus = gtk_window_get_focus (toplevel);
7553 current_focus = NULL;
7555 file_list_seen = FALSE;
7556 if (current_focus == impl->browse_files_tree_view)
7558 GtkTreeSelection *selection;
7562 file_list_seen = TRUE;
7563 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7564 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7566 /* If there is no selection in the file list, we probably have this situation:
7568 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7569 * 2. He then double-clicked on a folder ("bar") in the file list
7571 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7572 * filename entry to see if that is the case.
7574 if (info.result == NULL && impl->location_entry)
7577 else if (impl->location_entry && current_focus == impl->location_entry)
7579 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7583 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7588 if (!is_well_formed)
7591 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7593 g_object_unref (info.file_from_entry);
7597 if (info.file_from_entry)
7598 info.result = g_slist_prepend (info.result, info.file_from_entry);
7599 else if (!file_list_seen)
7604 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7606 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7610 /* The focus is on a dialog's action area button or something else */
7611 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7612 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7620 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7621 * fall back to the current directory */
7622 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7623 info.result == NULL)
7625 GFile *current_folder;
7627 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7630 info.result = g_slist_prepend (info.result, current_folder);
7633 return g_slist_reverse (info.result);
7637 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7639 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7641 if (impl->preview_file)
7642 return g_object_ref (impl->preview_file);
7647 static GtkFileSystem *
7648 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7650 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7652 return impl->file_system;
7655 /* Shows or hides the filter widgets */
7657 show_filters (GtkFileChooserDefault *impl,
7661 gtk_widget_show (impl->filter_combo_hbox);
7663 gtk_widget_hide (impl->filter_combo_hbox);
7667 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7668 GtkFileFilter *filter)
7670 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7673 if (g_slist_find (impl->filters, filter))
7675 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7679 g_object_ref_sink (filter);
7680 impl->filters = g_slist_append (impl->filters, filter);
7682 name = gtk_file_filter_get_name (filter);
7684 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7686 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7688 if (!g_slist_find (impl->filters, impl->current_filter))
7689 set_current_filter (impl, filter);
7691 show_filters (impl, TRUE);
7695 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7696 GtkFileFilter *filter)
7698 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7699 GtkTreeModel *model;
7703 filter_index = g_slist_index (impl->filters, filter);
7705 if (filter_index < 0)
7707 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7711 impl->filters = g_slist_remove (impl->filters, filter);
7713 if (filter == impl->current_filter)
7716 set_current_filter (impl, impl->filters->data);
7718 set_current_filter (impl, NULL);
7721 /* Remove row from the combo box */
7722 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7723 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7724 g_assert_not_reached ();
7726 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7728 g_object_unref (filter);
7731 show_filters (impl, FALSE);
7735 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7737 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7739 return g_slist_copy (impl->filters);
7742 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7744 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7747 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7750 struct AddShortcutData
7752 GtkFileChooserDefault *impl;
7757 add_shortcut_get_info_cb (GCancellable *cancellable,
7759 const GError *error,
7763 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7764 struct AddShortcutData *data = user_data;
7766 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7769 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7771 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7774 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7776 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7779 g_object_unref (data->impl);
7780 g_object_unref (data->file);
7783 g_object_unref (cancellable);
7787 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7791 GCancellable *cancellable;
7792 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7793 struct AddShortcutData *data;
7797 /* Avoid adding duplicates */
7798 pos = shortcut_find_position (impl, file);
7799 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7803 uri = g_file_get_uri (file);
7804 /* translators, "Shortcut" means "Bookmark" here */
7806 GTK_FILE_CHOOSER_ERROR,
7807 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7808 _("Shortcut %s already exists"),
7815 for (l = impl->loading_shortcuts; l; l = l->next)
7817 GCancellable *c = l->data;
7820 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7821 if (f && g_file_equal (file, f))
7825 uri = g_file_get_uri (file);
7827 GTK_FILE_CHOOSER_ERROR,
7828 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7829 _("Shortcut %s already exists"),
7837 data = g_new0 (struct AddShortcutData, 1);
7838 data->impl = g_object_ref (impl);
7839 data->file = g_object_ref (file);
7841 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7843 add_shortcut_get_info_cb, data);
7848 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7849 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7855 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7859 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7866 for (l = impl->loading_shortcuts; l; l = l->next)
7868 GCancellable *c = l->data;
7871 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7872 if (f && g_file_equal (file, f))
7874 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7875 g_cancellable_cancel (c);
7880 if (impl->num_shortcuts == 0)
7883 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7884 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7885 g_assert_not_reached ();
7887 for (i = 0; i < impl->num_shortcuts; i++)
7890 ShortcutType shortcut_type;
7893 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7894 SHORTCUTS_COL_DATA, &col_data,
7895 SHORTCUTS_COL_TYPE, &shortcut_type,
7897 g_assert (col_data != NULL);
7898 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7900 shortcut = col_data;
7901 if (g_file_equal (shortcut, file))
7903 shortcuts_remove_rows (impl, pos + i, 1);
7904 impl->num_shortcuts--;
7908 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7909 g_assert_not_reached ();
7914 uri = g_file_get_uri (file);
7915 /* translators, "Shortcut" means "Bookmark" here */
7917 GTK_FILE_CHOOSER_ERROR,
7918 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7919 _("Shortcut %s does not exist"),
7927 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7929 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7935 if (impl->num_shortcuts == 0)
7938 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7939 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7940 g_assert_not_reached ();
7944 for (i = 0; i < impl->num_shortcuts; i++)
7947 ShortcutType shortcut_type;
7950 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7951 SHORTCUTS_COL_DATA, &col_data,
7952 SHORTCUTS_COL_TYPE, &shortcut_type,
7954 g_assert (col_data != NULL);
7955 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
7957 shortcut = col_data;
7958 list = g_slist_prepend (list, g_object_ref (shortcut));
7960 if (i != impl->num_shortcuts - 1)
7962 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7963 g_assert_not_reached ();
7967 return g_slist_reverse (list);
7970 /* Guesses a size based upon font sizes */
7972 find_good_size_from_style (GtkWidget *widget,
7976 GtkFileChooserDefault *impl;
7981 g_assert (widget->style != NULL);
7982 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7984 screen = gtk_widget_get_screen (widget);
7987 resolution = gdk_screen_get_resolution (screen);
7988 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7992 resolution = 96.0; /* wheeee */
7994 font_size = pango_font_description_get_size (widget->style->font_desc);
7995 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7997 *width = font_size * NUM_CHARS;
7998 *height = font_size * NUM_LINES;
8002 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8003 gint *default_width,
8004 gint *default_height)
8006 GtkFileChooserDefault *impl;
8009 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8011 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8012 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8013 || impl->expand_folders)
8015 GtkFileChooserSettings *settings;
8016 int x, y, width, height;
8018 settings = _gtk_file_chooser_settings_new ();
8019 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
8020 g_object_unref (settings);
8022 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8024 *default_width = width;
8025 *default_height = height;
8029 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8031 if (impl->preview_widget_active &&
8032 impl->preview_widget &&
8033 GTK_WIDGET_VISIBLE (impl->preview_widget))
8035 gtk_widget_size_request (impl->preview_box, &req);
8036 *default_width += PREVIEW_HBOX_SPACING + req.width;
8039 if (impl->extra_widget &&
8040 GTK_WIDGET_VISIBLE (impl->extra_widget))
8042 gtk_widget_size_request (impl->extra_align, &req);
8043 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
8048 gtk_widget_size_request (GTK_WIDGET (impl), &req);
8049 *default_width = req.width;
8050 *default_height = req.height;
8054 struct switch_folder_closure {
8055 GtkFileChooserDefault *impl;
8060 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8062 switch_folder_foreach_cb (GtkTreeModel *model,
8067 struct switch_folder_closure *closure;
8071 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8072 closure->num_selected++;
8075 /* Changes to the selected folder in the list view */
8077 switch_to_selected_folder (GtkFileChooserDefault *impl)
8079 GtkTreeSelection *selection;
8080 struct switch_folder_closure closure;
8082 /* We do this with foreach() rather than get_selected() as we may be in
8083 * multiple selection mode
8086 closure.impl = impl;
8087 closure.file = NULL;
8088 closure.num_selected = 0;
8090 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8091 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8093 g_assert (closure.file && closure.num_selected == 1);
8095 change_folder_and_display_error (impl, closure.file, FALSE);
8098 /* Gets the GFileInfo for the selected row in the file list; assumes single
8102 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8103 gboolean *had_selection)
8105 GtkTreeSelection *selection;
8109 g_assert (!impl->select_multiple);
8110 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8111 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8113 *had_selection = FALSE;
8117 *had_selection = TRUE;
8119 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8123 /* Gets the display name of the selected file in the file list; assumes single
8124 * selection mode and that something is selected.
8126 static const gchar *
8127 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8130 gboolean had_selection;
8132 info = get_selected_file_info_from_file_list (impl, &had_selection);
8133 g_assert (had_selection);
8134 g_assert (info != NULL);
8136 return g_file_info_get_display_name (info);
8140 add_custom_button_to_dialog (GtkDialog *dialog,
8141 const gchar *mnemonic_label,
8142 const gchar *stock_id,
8147 button = gtk_button_new_with_mnemonic (mnemonic_label);
8148 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8149 gtk_button_set_image (GTK_BUTTON (button),
8150 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8151 gtk_widget_show (button);
8153 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8156 /* Presents an overwrite confirmation dialog; returns whether we should accept
8160 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8161 const gchar *file_part,
8162 const gchar *folder_display_name)
8164 GtkWindow *toplevel;
8168 toplevel = get_toplevel (GTK_WIDGET (impl));
8170 dialog = gtk_message_dialog_new (toplevel,
8171 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8172 GTK_MESSAGE_QUESTION,
8174 _("A file named \"%s\" already exists. Do you want to replace it?"),
8176 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8177 _("The file already exists in \"%s\". Replacing it will "
8178 "overwrite its contents."),
8179 folder_display_name);
8181 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8182 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8183 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8184 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8185 GTK_RESPONSE_ACCEPT,
8186 GTK_RESPONSE_CANCEL,
8188 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8190 if (toplevel->group)
8191 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8193 response = gtk_dialog_run (GTK_DIALOG (dialog));
8195 gtk_widget_destroy (dialog);
8197 return (response == GTK_RESPONSE_ACCEPT);
8200 struct GetDisplayNameData
8202 GtkFileChooserDefault *impl;
8207 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8209 const GError *error,
8212 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8213 gboolean should_respond = FALSE;
8214 struct GetDisplayNameData *data = user_data;
8216 if (cancellable != data->impl->should_respond_get_info_cancellable)
8219 data->impl->should_respond_get_info_cancellable = NULL;
8225 /* Huh? Did the folder disappear? Let the caller deal with it */
8226 should_respond = TRUE;
8228 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8230 set_busy_cursor (data->impl, FALSE);
8232 g_signal_emit_by_name (data->impl, "response-requested");
8235 g_object_unref (data->impl);
8236 g_free (data->file_part);
8239 g_object_unref (cancellable);
8242 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8243 * should respond. Can get the file part from the file list or the save entry.
8246 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8247 const gchar *file_part,
8250 GtkFileChooserConfirmation conf;
8252 if (!impl->do_overwrite_confirmation)
8255 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8257 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8261 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8263 struct GetDisplayNameData *data;
8265 g_assert (file_part != NULL);
8267 data = g_new0 (struct GetDisplayNameData, 1);
8268 data->impl = g_object_ref (impl);
8269 data->file_part = g_strdup (file_part);
8271 if (impl->should_respond_get_info_cancellable)
8272 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8274 impl->should_respond_get_info_cancellable =
8275 _gtk_file_system_get_info (impl->file_system, parent_file,
8276 "standard::display-name",
8277 confirmation_confirm_get_info_cb,
8279 set_busy_cursor (data->impl, TRUE);
8283 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8286 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8290 g_assert_not_reached ();
8295 struct FileExistsData
8297 GtkFileChooserDefault *impl;
8298 gboolean file_exists_and_is_not_folder;
8304 save_entry_get_info_cb (GCancellable *cancellable,
8306 const GError *error,
8309 gboolean parent_is_folder;
8310 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8311 struct FileExistsData *data = user_data;
8313 if (cancellable != data->impl->should_respond_get_info_cancellable)
8316 data->impl->should_respond_get_info_cancellable = NULL;
8318 set_busy_cursor (data->impl, FALSE);
8324 parent_is_folder = FALSE;
8326 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8328 if (parent_is_folder)
8330 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8332 if (data->file_exists_and_is_not_folder)
8337 /* Dup the string because the string may be modified
8338 * depending on what clients do in the confirm-overwrite
8339 * signal and this corrupts the pointer
8341 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8342 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8346 g_signal_emit_by_name (data->impl, "response-requested");
8349 g_signal_emit_by_name (data->impl, "response-requested");
8351 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8353 GError *error = NULL;
8355 set_busy_cursor (data->impl, TRUE);
8356 g_file_make_directory (data->file, NULL, &error);
8357 set_busy_cursor (data->impl, FALSE);
8360 g_signal_emit_by_name (data->impl, "response-requested");
8362 error_creating_folder_dialog (data->impl, data->file, error);
8367 /* This will display an error, which is what we want */
8368 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8372 g_object_unref (data->impl);
8373 g_object_unref (data->file);
8374 g_object_unref (data->parent_file);
8377 g_object_unref (cancellable);
8381 file_exists_get_info_cb (GCancellable *cancellable,
8383 const GError *error,
8386 gboolean data_ownership_taken = FALSE;
8387 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8388 gboolean file_exists_and_is_not_folder;
8389 struct FileExistsData *data = user_data;
8391 if (cancellable != data->impl->file_exists_get_info_cancellable)
8394 data->impl->file_exists_get_info_cancellable = NULL;
8396 set_busy_cursor (data->impl, FALSE);
8401 file_exists_and_is_not_folder = info && (! _gtk_file_info_consider_as_directory (info));
8403 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8404 /* user typed a filename; we are done */
8405 g_signal_emit_by_name (data->impl, "response-requested");
8406 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8407 && file_exists_and_is_not_folder)
8409 /* Oops, the user typed the name of an existing path which is not
8412 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8413 g_error_copy (error));
8417 /* check that everything up to the last component exists */
8419 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8420 data_ownership_taken = TRUE;
8422 if (data->impl->should_respond_get_info_cancellable)
8423 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8425 data->impl->should_respond_get_info_cancellable =
8426 _gtk_file_system_get_info (data->impl->file_system,
8429 save_entry_get_info_cb,
8431 set_busy_cursor (data->impl, TRUE);
8435 if (!data_ownership_taken)
8437 g_object_unref (data->impl);
8438 g_object_unref (data->file);
8439 g_object_unref (data->parent_file);
8443 g_object_unref (cancellable);
8447 paste_text_received (GtkClipboard *clipboard,
8449 GtkFileChooserDefault *impl)
8456 file = g_file_new_for_uri (text);
8458 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8459 location_popup_handler (impl, text);
8461 g_object_unref (file);
8464 /* Handler for the "location-popup-on-paste" keybinding signal */
8466 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8468 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8469 GDK_SELECTION_CLIPBOARD);
8470 gtk_clipboard_request_text (clipboard,
8471 (GtkClipboardTextReceivedFunc) paste_text_received,
8476 /* Implementation for GtkFileChooserEmbed::should_respond() */
8478 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8480 GtkFileChooserDefault *impl;
8481 GtkWidget *toplevel;
8482 GtkWidget *current_focus;
8484 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8486 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8487 g_assert (GTK_IS_WINDOW (toplevel));
8489 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8491 if (current_focus == impl->browse_files_tree_view)
8493 /* The following array encodes what we do based on the impl->action and the
8494 * number of files selected.
8497 NOOP, /* Do nothing (don't respond) */
8498 RESPOND, /* Respond immediately */
8499 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8500 ALL_FILES, /* Respond only if everything selected is a file */
8501 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8502 SAVE_ENTRY, /* Go to the code for handling the save entry */
8503 NOT_REACHED /* Sanity check */
8505 static const ActionToTake what_to_do[4][3] = {
8506 /* 0 selected 1 selected many selected */
8507 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8508 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8509 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8510 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8514 gboolean all_files, all_folders;
8516 ActionToTake action;
8520 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8522 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8523 return search_should_respond (impl);
8525 if (impl->operation_mode == OPERATION_MODE_RECENT)
8526 return recent_should_respond (impl);
8528 selection_check (impl, &num_selected, &all_files, &all_folders);
8530 if (num_selected > 2)
8535 action = what_to_do [impl->action] [k];
8545 case RESPOND_OR_SWITCH:
8546 g_assert (num_selected == 1);
8550 switch_to_selected_folder (impl);
8553 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8554 return should_respond_after_confirm_overwrite (impl,
8555 get_display_name_from_file_list (impl),
8556 impl->current_folder);
8570 g_assert_not_reached ();
8573 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8576 gboolean is_well_formed, is_empty, is_file_part_empty;
8579 GtkFileChooserEntry *entry;
8584 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8585 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8586 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8587 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8588 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8590 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8591 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8593 if (is_empty || !is_well_formed)
8596 g_assert (file != NULL);
8601 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8602 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8604 change_folder_and_display_error (impl, file, TRUE);
8607 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8608 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8610 /* The folder already exists, so we do not need to create it.
8611 * Just respond to terminate the dialog.
8617 g_assert_not_reached ();
8623 struct FileExistsData *data;
8625 /* We need to check whether file exists and is not a folder */
8627 data = g_new0 (struct FileExistsData, 1);
8628 data->impl = g_object_ref (impl);
8629 data->file = g_object_ref (file);
8630 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8632 if (impl->file_exists_get_info_cancellable)
8633 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8635 impl->file_exists_get_info_cancellable =
8636 _gtk_file_system_get_info (impl->file_system, file,
8638 file_exists_get_info_cb,
8641 set_busy_cursor (impl, TRUE);
8645 g_error_free (error);
8648 g_object_unref (file);
8651 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8653 /* The focus is on a dialog's action area button, *and* the widget that
8654 * was focused immediately before it is the file list.
8658 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8660 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8663 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8665 /* The focus is on a dialog's action area button, *and* the widget that
8666 * was focused immediately before it is the location entry.
8671 /* The focus is on a dialog's action area button or something else */
8672 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8673 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8678 g_assert_not_reached ();
8682 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8684 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8686 GtkFileChooserDefault *impl;
8689 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8691 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8692 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8694 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8695 widget = impl->browse_files_tree_view;
8697 widget = impl->location_entry;
8699 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8700 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8701 widget = impl->location_entry;
8704 g_assert_not_reached ();
8708 g_assert (widget != NULL);
8709 gtk_widget_grab_focus (widget);
8712 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8714 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8724 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8725 *list = g_slist_prepend (*list, g_object_ref (file));
8728 /* Constructs a list of the selected paths in search mode */
8730 search_get_selected_files (GtkFileChooserDefault *impl)
8733 GtkTreeSelection *selection;
8737 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8738 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8739 result = g_slist_reverse (result);
8744 /* Called from ::should_respond(). We return whether there are selected files
8745 * in the search list.
8748 search_should_respond (GtkFileChooserDefault *impl)
8750 GtkTreeSelection *selection;
8752 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8754 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8755 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8758 /* Adds one hit from the search engine to the search_model */
8760 search_add_hit (GtkFileChooserDefault *impl,
8765 file = g_file_new_for_uri (uri);
8769 if (!g_file_is_native (file))
8771 g_object_unref (file);
8775 _gtk_file_system_model_add_and_query_file (impl->search_model,
8779 g_object_unref (file);
8782 /* Callback used from GtkSearchEngine when we get new hits */
8784 search_engine_hits_added_cb (GtkSearchEngine *engine,
8788 GtkFileChooserDefault *impl;
8791 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8793 for (l = hits; l; l = l->next)
8794 search_add_hit (impl, (gchar*)l->data);
8797 /* Callback used from GtkSearchEngine when the query is done running */
8799 search_engine_finished_cb (GtkSearchEngine *engine,
8802 GtkFileChooserDefault *impl;
8804 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8807 /* EB: setting the model here will avoid loads of row events,
8808 * but it'll make the search look like blocked.
8810 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8811 GTK_TREE_MODEL (impl->search_model));
8814 /* FMQ: if search was empty, say that we got no hits */
8815 set_busy_cursor (impl, FALSE);
8818 /* Displays a generic error when we cannot create a GtkSearchEngine.
8819 * It would be better if _gtk_search_engine_new() gave us a GError
8820 * with a better message, but it doesn't do that right now.
8823 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8825 error_message (impl,
8826 _("Could not start the search process"),
8827 _("The program was not able to create a connection to the indexer "
8828 "daemon. Please make sure it is running."));
8832 search_engine_error_cb (GtkSearchEngine *engine,
8833 const gchar *message,
8836 GtkFileChooserDefault *impl;
8838 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8840 search_stop_searching (impl, TRUE);
8841 error_message (impl, _("Could not send the search request"), message);
8843 set_busy_cursor (impl, FALSE);
8846 /* Frees the data in the search_model */
8848 search_clear_model (GtkFileChooserDefault *impl,
8849 gboolean remove_from_treeview)
8851 if (!impl->search_model)
8854 g_object_unref (impl->search_model);
8855 impl->search_model = NULL;
8857 if (remove_from_treeview)
8858 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8861 /* Stops any ongoing searches; does not touch the search_model */
8863 search_stop_searching (GtkFileChooserDefault *impl,
8864 gboolean remove_query)
8866 if (remove_query && impl->search_query)
8868 g_object_unref (impl->search_query);
8869 impl->search_query = NULL;
8872 if (impl->search_engine)
8874 _gtk_search_engine_stop (impl->search_engine);
8876 g_object_unref (impl->search_engine);
8877 impl->search_engine = NULL;
8881 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8883 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8885 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8887 search_stop_searching (impl, FALSE);
8888 search_clear_model (impl, TRUE);
8890 gtk_widget_destroy (impl->search_hbox);
8891 impl->search_hbox = NULL;
8892 impl->search_entry = NULL;
8894 gtk_widget_show (impl->browse_path_bar);
8895 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
8896 gtk_widget_hide (impl->browse_new_folder_button);
8898 gtk_widget_show (impl->browse_new_folder_button);
8901 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8902 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8904 gtk_widget_show (impl->location_button);
8906 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8907 gtk_widget_show (impl->location_entry_box);
8910 impl->operation_mode = OPERATION_MODE_BROWSE;
8912 file_list_set_sort_column_ids (impl);
8915 /* Creates the search_model and puts it in the tree view */
8917 search_setup_model (GtkFileChooserDefault *impl)
8919 g_assert (impl->search_model == NULL);
8921 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
8923 MODEL_COLUMN_TYPES);
8925 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8929 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8933 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8937 set_sort_column (impl);
8939 /* EB: setting the model here will make the hits list update feel
8940 * more "alive" than setting the model at the end of the search
8943 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8944 GTK_TREE_MODEL (impl->search_model));
8947 /* Creates a new query with the specified text and launches it */
8949 search_start_query (GtkFileChooserDefault *impl,
8950 const gchar *query_text)
8952 search_stop_searching (impl, FALSE);
8953 search_clear_model (impl, TRUE);
8954 search_setup_model (impl);
8955 set_busy_cursor (impl, TRUE);
8957 if (impl->search_engine == NULL)
8958 impl->search_engine = _gtk_search_engine_new ();
8960 if (!impl->search_engine)
8962 set_busy_cursor (impl, FALSE);
8963 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
8967 if (!impl->search_query)
8969 impl->search_query = _gtk_query_new ();
8970 _gtk_query_set_text (impl->search_query, query_text);
8973 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
8975 g_signal_connect (impl->search_engine, "hits-added",
8976 G_CALLBACK (search_engine_hits_added_cb), impl);
8977 g_signal_connect (impl->search_engine, "finished",
8978 G_CALLBACK (search_engine_finished_cb), impl);
8979 g_signal_connect (impl->search_engine, "error",
8980 G_CALLBACK (search_engine_error_cb), impl);
8982 _gtk_search_engine_start (impl->search_engine);
8985 /* Callback used when the user presses Enter while typing on the search
8986 * entry; starts the query
8989 search_entry_activate_cb (GtkEntry *entry,
8992 GtkFileChooserDefault *impl;
8995 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8997 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
8998 if (strlen (text) == 0)
9001 /* reset any existing query object */
9002 if (impl->search_query)
9004 g_object_unref (impl->search_query);
9005 impl->search_query = NULL;
9008 search_start_query (impl, text);
9011 /* Hides the path bar and creates the search entry */
9013 search_setup_widgets (GtkFileChooserDefault *impl)
9019 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9023 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9024 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), image);
9025 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9029 label = gtk_label_new (NULL);
9030 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9031 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9032 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9037 impl->search_entry = gtk_entry_new ();
9038 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9039 g_signal_connect (impl->search_entry, "activate",
9040 G_CALLBACK (search_entry_activate_cb),
9042 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9044 /* if there already is a query, restart it */
9045 if (impl->search_query)
9047 gchar *query = _gtk_query_get_text (impl->search_query);
9051 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9052 search_start_query (impl, query);
9058 g_object_unref (impl->search_query);
9059 impl->search_query = NULL;
9063 gtk_widget_hide (impl->browse_path_bar);
9064 gtk_widget_hide (impl->browse_new_folder_button);
9066 /* Box for search widgets */
9067 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9068 gtk_widget_show_all (impl->search_hbox);
9070 /* Hide the location widgets temporarily */
9072 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9073 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9075 gtk_widget_hide (impl->location_button);
9076 gtk_widget_hide (impl->location_entry_box);
9079 gtk_widget_grab_focus (impl->search_entry);
9081 /* FMQ: hide the filter combo? */
9084 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9086 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9090 case OPERATION_MODE_BROWSE:
9091 stop_loading_and_clear_list_model (impl, TRUE);
9094 case OPERATION_MODE_SEARCH:
9095 search_stop_searching (impl, FALSE);
9096 search_clear_model (impl, TRUE);
9098 gtk_widget_destroy (impl->search_hbox);
9099 impl->search_hbox = NULL;
9100 impl->search_entry = NULL;
9103 case OPERATION_MODE_RECENT:
9104 recent_stop_loading (impl);
9105 recent_clear_model (impl, TRUE);
9107 gtk_widget_destroy (impl->recent_hbox);
9108 impl->recent_hbox = NULL;
9113 /* Main entry point to the searching functions; this gets called when the user
9114 * activates the Search shortcut.
9117 search_activate (GtkFileChooserDefault *impl)
9119 OperationMode previous_mode;
9121 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9123 gtk_widget_grab_focus (impl->search_entry);
9127 previous_mode = impl->operation_mode;
9128 impl->operation_mode = OPERATION_MODE_SEARCH;
9130 stop_operation (impl, previous_mode);
9132 g_assert (impl->search_hbox == NULL);
9133 g_assert (impl->search_entry == NULL);
9134 g_assert (impl->search_model == NULL);
9136 search_setup_widgets (impl);
9137 file_list_set_sort_column_ids (impl);
9141 * Recent files support
9144 /* Frees the data in the recent_model */
9146 recent_clear_model (GtkFileChooserDefault *impl,
9147 gboolean remove_from_treeview)
9149 GtkTreeModel *model;
9151 if (!impl->recent_model)
9154 model = GTK_TREE_MODEL (impl->recent_model);
9156 if (remove_from_treeview)
9157 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9159 g_object_unref (impl->recent_model);
9160 impl->recent_model = NULL;
9163 /* Stops any ongoing loading of the recent files list; does
9164 * not touch the recent_model
9167 recent_stop_loading (GtkFileChooserDefault *impl)
9169 if (impl->load_recent_id)
9171 g_source_remove (impl->load_recent_id);
9172 impl->load_recent_id = 0;
9176 /* Stops any pending load, clears the file list, and switches
9177 * back to OPERATION_MODE_BROWSE
9180 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9182 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9184 recent_stop_loading (impl);
9185 recent_clear_model (impl, TRUE);
9187 gtk_widget_destroy (impl->recent_hbox);
9188 impl->recent_hbox = NULL;
9190 gtk_widget_show (impl->browse_path_bar);
9191 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9192 gtk_widget_hide (impl->browse_new_folder_button);
9194 gtk_widget_show (impl->browse_new_folder_button);
9196 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9197 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9199 gtk_widget_show (impl->location_button);
9201 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9202 gtk_widget_show (impl->location_entry_box);
9205 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9207 impl->operation_mode = OPERATION_MODE_BROWSE;
9209 file_list_set_sort_column_ids (impl);
9213 recent_setup_model (GtkFileChooserDefault *impl)
9215 g_assert (impl->recent_model == NULL);
9217 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9219 MODEL_COLUMN_TYPES);
9221 _gtk_file_system_model_set_filter (impl->recent_model,
9222 impl->current_filter);
9223 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9227 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9231 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9235 set_sort_column (impl);
9240 GtkFileChooserDefault *impl;
9243 gint n_loaded_items;
9244 guint needs_sorting : 1;
9248 recent_idle_cleanup (gpointer data)
9250 RecentLoadData *load_data = data;
9251 GtkFileChooserDefault *impl = load_data->impl;
9253 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9254 GTK_TREE_MODEL (impl->recent_model));
9256 set_busy_cursor (impl, FALSE);
9258 impl->load_recent_id = 0;
9260 if (load_data->items)
9262 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9263 g_list_free (load_data->items);
9270 recent_sort_mru (gconstpointer a,
9273 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9274 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9276 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9280 get_recent_files_limit (GtkWidget *widget)
9282 GtkSettings *settings;
9285 if (gtk_widget_has_screen (widget))
9286 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9288 settings = gtk_settings_get_default ();
9290 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9296 recent_idle_load (gpointer data)
9298 RecentLoadData *load_data = data;
9299 GtkFileChooserDefault *impl = load_data->impl;
9303 if (!impl->recent_manager)
9306 /* first iteration: load all the items */
9307 if (!load_data->items)
9309 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9310 if (!load_data->items)
9313 load_data->needs_sorting = TRUE;
9318 /* second iteration: preliminary MRU sorting and clamping */
9319 if (load_data->needs_sorting)
9323 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9324 load_data->n_items = g_list_length (load_data->items);
9326 limit = get_recent_files_limit (GTK_WIDGET (impl));
9328 if (limit != -1 && (load_data->n_items > limit))
9332 clamp = g_list_nth (load_data->items, limit - 1);
9333 if (G_LIKELY (clamp))
9338 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9341 load_data->n_items = limit;
9345 load_data->n_loaded_items = 0;
9346 load_data->needs_sorting = FALSE;
9351 /* finished loading items */
9352 for (walk = load_data->items; walk; walk = walk->next)
9354 GtkRecentInfo *info = walk->data;
9355 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9357 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9360 gtk_recent_info_unref (walk->data);
9361 g_object_unref (file);
9364 g_list_free (load_data->items);
9365 load_data->items = NULL;
9371 recent_start_loading (GtkFileChooserDefault *impl)
9373 RecentLoadData *load_data;
9375 recent_stop_loading (impl);
9376 recent_clear_model (impl, TRUE);
9377 recent_setup_model (impl);
9378 set_busy_cursor (impl, TRUE);
9380 g_assert (impl->load_recent_id == 0);
9382 load_data = g_new (RecentLoadData, 1);
9383 load_data->impl = impl;
9384 load_data->items = NULL;
9385 load_data->n_items = 0;
9386 load_data->n_loaded_items = 0;
9387 load_data->needs_sorting = TRUE;
9389 /* begin lazy loading the recent files into the model */
9390 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9393 recent_idle_cleanup);
9397 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9407 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9408 *list = g_slist_prepend (*list, file);
9411 /* Constructs a list of the selected paths in recent files mode */
9413 recent_get_selected_files (GtkFileChooserDefault *impl)
9416 GtkTreeSelection *selection;
9420 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9421 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9422 result = g_slist_reverse (result);
9427 /* Called from ::should_respond(). We return whether there are selected
9428 * files in the recent files list.
9431 recent_should_respond (GtkFileChooserDefault *impl)
9433 GtkTreeSelection *selection;
9435 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9437 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9438 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9441 /* Hide the location widgets temporarily */
9443 recent_hide_entry (GtkFileChooserDefault *impl)
9449 impl->recent_hbox = gtk_hbox_new (FALSE, 12);
9452 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
9453 gtk_size_group_add_widget (impl->browse_path_bar_size_group, image);
9454 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
9457 label = gtk_label_new (NULL);
9458 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
9459 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9460 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
9463 gtk_widget_hide (impl->browse_path_bar);
9464 gtk_widget_hide (impl->browse_new_folder_button);
9466 /* Box for recent widgets */
9467 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
9468 gtk_widget_show_all (impl->recent_hbox);
9470 /* Hide the location widgets temporarily */
9471 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9472 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9474 gtk_widget_hide (impl->location_button);
9475 gtk_widget_hide (impl->location_entry_box);
9479 /* Main entry point to the recent files functions; this gets called when
9480 * the user activates the Recently Used shortcut.
9483 recent_activate (GtkFileChooserDefault *impl)
9485 OperationMode previous_mode;
9487 if (impl->operation_mode == OPERATION_MODE_RECENT)
9490 previous_mode = impl->operation_mode;
9491 impl->operation_mode = OPERATION_MODE_RECENT;
9493 stop_operation (impl, previous_mode);
9495 recent_hide_entry (impl);
9497 file_list_set_sort_column_ids (impl);
9498 recent_start_loading (impl);
9502 set_current_filter (GtkFileChooserDefault *impl,
9503 GtkFileFilter *filter)
9505 if (impl->current_filter != filter)
9509 /* NULL filters are allowed to reset to non-filtered status
9511 filter_index = g_slist_index (impl->filters, filter);
9512 if (impl->filters && filter && filter_index < 0)
9515 if (impl->current_filter)
9516 g_object_unref (impl->current_filter);
9517 impl->current_filter = filter;
9518 if (impl->current_filter)
9520 g_object_ref_sink (impl->current_filter);
9524 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9527 if (impl->browse_files_model)
9528 install_list_model_filter (impl);
9530 if (impl->search_model)
9531 _gtk_file_system_model_set_filter (impl->search_model, filter);
9533 if (impl->recent_model)
9534 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9536 g_object_notify (G_OBJECT (impl), "filter");
9541 filter_combo_changed (GtkComboBox *combo_box,
9542 GtkFileChooserDefault *impl)
9544 gint new_index = gtk_combo_box_get_active (combo_box);
9545 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9547 set_current_filter (impl, new_filter);
9551 check_preview_change (GtkFileChooserDefault *impl)
9553 GtkTreePath *cursor_path;
9555 char *new_display_name;
9556 GtkTreeModel *model;
9558 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9559 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9564 gtk_tree_model_get_iter (model, &iter, cursor_path);
9565 gtk_tree_model_get (model, &iter,
9566 MODEL_COL_FILE, &new_file,
9567 MODEL_COL_NAME, &new_display_name,
9570 gtk_tree_path_free (cursor_path);
9575 new_display_name = NULL;
9578 if (new_file != impl->preview_file &&
9579 !(new_file && impl->preview_file &&
9580 g_file_equal (new_file, impl->preview_file)))
9582 if (impl->preview_file)
9584 g_object_unref (impl->preview_file);
9585 g_free (impl->preview_display_name);
9590 impl->preview_file = new_file;
9591 impl->preview_display_name = new_display_name;
9595 impl->preview_file = NULL;
9596 impl->preview_display_name = NULL;
9597 g_free (new_display_name);
9600 if (impl->use_preview_label && impl->preview_label)
9601 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9603 g_signal_emit_by_name (impl, "update-preview");
9608 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9609 GtkFileSystemVolume *volume,
9610 const GError *error,
9614 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9615 GtkFileChooserDefault *impl = data;
9617 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9620 impl->shortcuts_activate_iter_cancellable = NULL;
9622 set_busy_cursor (impl, FALSE);
9629 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9633 name = _gtk_file_system_volume_get_display_name (volume);
9634 msg = g_strdup_printf (_("Could not mount %s"), name);
9636 error_message (impl, msg, error->message);
9645 file = _gtk_file_system_volume_get_root (volume);
9648 change_folder_and_display_error (impl, file, FALSE);
9649 g_object_unref (file);
9653 g_object_unref (impl);
9654 g_object_unref (cancellable);
9658 /* Activates a volume by mounting it if necessary and then switching to its
9662 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9663 GtkFileSystemVolume *volume)
9667 switch (impl->operation_mode)
9669 case OPERATION_MODE_BROWSE:
9671 case OPERATION_MODE_SEARCH:
9672 search_switch_to_browse_mode (impl);
9674 case OPERATION_MODE_RECENT:
9675 recent_switch_to_browse_mode (impl);
9679 /* We ref the file chooser since volume_mount() may run a main loop, and the
9680 * user could close the file chooser window in the meantime.
9682 g_object_ref (impl);
9684 if (!_gtk_file_system_volume_is_mounted (volume))
9686 GMountOperation *mount_op;
9688 set_busy_cursor (impl, TRUE);
9690 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9691 impl->shortcuts_activate_iter_cancellable =
9692 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9693 shortcuts_activate_volume_mount_cb,
9694 g_object_ref (impl));
9695 g_object_unref (mount_op);
9699 file = _gtk_file_system_volume_get_root (volume);
9702 change_folder_and_display_error (impl, file, FALSE);
9703 g_object_unref (file);
9707 g_object_unref (impl);
9710 /* Opens the folder or volume at the specified iter in the shortcuts model */
9711 struct ShortcutsActivateData
9713 GtkFileChooserDefault *impl;
9718 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9720 const GError *error,
9723 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9724 struct ShortcutsActivateData *data = user_data;
9726 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9729 data->impl->shortcuts_activate_iter_cancellable = NULL;
9734 if (!error && _gtk_file_info_consider_as_directory (info))
9735 change_folder_and_display_error (data->impl, data->file, FALSE);
9737 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9742 g_object_unref (data->impl);
9743 g_object_unref (data->file);
9746 g_object_unref (cancellable);
9750 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9751 GtkFileSystemVolume *volume,
9752 const GError *error,
9755 struct ShortcutsActivateData *data = user_data;
9759 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9761 g_object_unref (data->impl);
9762 g_object_unref (data->file);
9768 data->impl->shortcuts_activate_iter_cancellable =
9769 _gtk_file_system_get_info (data->impl->file_system, data->file,
9771 shortcuts_activate_get_info_cb, data);
9775 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9779 ShortcutType shortcut_type;
9781 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9782 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9783 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9784 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9786 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9787 SHORTCUTS_COL_DATA, &col_data,
9788 SHORTCUTS_COL_TYPE, &shortcut_type,
9791 if (impl->shortcuts_activate_iter_cancellable)
9793 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9794 impl->shortcuts_activate_iter_cancellable = NULL;
9797 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9799 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9801 GtkFileSystemVolume *volume;
9805 shortcuts_activate_volume (impl, volume);
9807 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9809 struct ShortcutsActivateData *data;
9810 GtkFileSystemVolume *volume;
9812 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9814 data = g_new0 (struct ShortcutsActivateData, 1);
9815 data->impl = g_object_ref (impl);
9816 data->file = g_object_ref (col_data);
9818 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9820 GMountOperation *mount_operation;
9821 GtkWidget *toplevel;
9823 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9825 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9827 impl->shortcuts_activate_iter_cancellable =
9828 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9830 shortcuts_activate_mount_enclosing_volume,
9835 impl->shortcuts_activate_iter_cancellable =
9836 _gtk_file_system_get_info (impl->file_system, data->file,
9838 shortcuts_activate_get_info_cb, data);
9841 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9843 search_activate (impl);
9845 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9847 recent_activate (impl);
9851 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9853 shortcuts_key_press_event_cb (GtkWidget *widget,
9855 GtkFileChooserDefault *impl)
9859 modifiers = gtk_accelerator_get_default_mod_mask ();
9861 if (key_is_left_or_right (event))
9863 gtk_widget_grab_focus (impl->browse_files_tree_view);
9867 if ((event->keyval == GDK_BackSpace
9868 || event->keyval == GDK_Delete
9869 || event->keyval == GDK_KP_Delete)
9870 && (event->state & modifiers) == 0)
9872 remove_selected_bookmarks (impl);
9876 if ((event->keyval == GDK_F2)
9877 && (event->state & modifiers) == 0)
9879 rename_selected_bookmark (impl);
9887 shortcuts_select_func (GtkTreeSelection *selection,
9888 GtkTreeModel *model,
9890 gboolean path_currently_selected,
9893 GtkFileChooserDefault *impl = data;
9894 GtkTreeIter filter_iter;
9895 ShortcutType shortcut_type;
9897 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9898 g_assert_not_reached ();
9900 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9902 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9906 list_select_func (GtkTreeSelection *selection,
9907 GtkTreeModel *model,
9909 gboolean path_currently_selected,
9912 GtkFileChooserDefault *impl = data;
9914 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
9915 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9920 if (!gtk_tree_model_get_iter (model, &iter, path))
9922 gtk_tree_model_get (model, &iter,
9923 MODEL_COL_IS_FOLDER, &is_folder,
9933 list_selection_changed (GtkTreeSelection *selection,
9934 GtkFileChooserDefault *impl)
9936 /* See if we are in the new folder editable row for Save mode */
9937 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
9938 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9941 gboolean had_selection;
9943 info = get_selected_file_info_from_file_list (impl, &had_selection);
9945 goto out; /* normal processing */
9948 return; /* We are on the editable row for New Folder */
9953 if (impl->location_entry)
9954 update_chooser_entry (impl);
9956 check_preview_change (impl);
9957 bookmarks_check_add_sensitivity (impl);
9959 g_signal_emit_by_name (impl, "selection-changed", 0);
9962 /* Callback used when a row in the file list is activated */
9964 list_row_activated (GtkTreeView *tree_view,
9966 GtkTreeViewColumn *column,
9967 GtkFileChooserDefault *impl)
9971 GtkTreeModel *model;
9974 model = gtk_tree_view_get_model (tree_view);
9976 if (!gtk_tree_model_get_iter (model, &iter, path))
9979 gtk_tree_model_get (model, &iter,
9980 MODEL_COL_FILE, &file,
9981 MODEL_COL_IS_FOLDER, &is_folder,
9984 if (is_folder && file)
9986 change_folder_and_display_error (impl, file, FALSE);
9990 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9991 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9992 g_signal_emit_by_name (impl, "file-activated");
9995 g_object_unref (file);
9999 path_bar_clicked (GtkPathBar *path_bar,
10002 gboolean child_is_hidden,
10003 GtkFileChooserDefault *impl)
10006 pending_select_files_add (impl, child_file);
10008 if (!change_folder_and_display_error (impl, file, FALSE))
10011 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10012 * show hidden files so that ".baz" appears in the file list, as it will still
10013 * be shown in the path bar: "/foo/[bar]/.baz"
10015 if (child_is_hidden)
10016 g_object_set (impl, "show-hidden", TRUE, NULL);
10020 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10022 GtkTreeViewColumn *column;
10023 GtkCellRenderer *renderer;
10024 GList *walk, *list;
10025 gboolean always_sensitive;
10027 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10028 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10030 /* Keep the following column numbers in sync with create_file_list() */
10033 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10034 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10035 for (walk = list; walk; walk = walk->next)
10037 renderer = walk->data;
10038 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10040 gtk_tree_view_column_set_attributes (column, renderer,
10041 "pixbuf", MODEL_COL_PIXBUF,
10046 gtk_tree_view_column_set_attributes (column, renderer,
10047 "text", MODEL_COL_NAME,
10048 "ellipsize", MODEL_COL_ELLIPSIZE,
10051 if (always_sensitive)
10052 g_object_set (renderer, "sensitive", TRUE, NULL);
10054 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10056 g_list_free (list);
10059 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10060 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10061 renderer = list->data;
10062 gtk_tree_view_column_set_attributes (column, renderer,
10063 "text", MODEL_COL_SIZE_TEXT,
10065 if (always_sensitive)
10066 g_object_set (renderer, "sensitive", TRUE, NULL);
10068 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10069 g_list_free (list);
10072 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10073 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10074 renderer = list->data;
10075 gtk_tree_view_column_set_attributes (column, renderer,
10076 "text", MODEL_COL_MTIME_TEXT,
10078 if (always_sensitive)
10079 g_object_set (renderer, "sensitive", TRUE, NULL);
10081 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10082 g_list_free (list);
10086 _gtk_file_chooser_default_new (void)
10088 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10092 location_set_user_text (GtkFileChooserDefault *impl,
10095 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10096 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10100 location_popup_handler (GtkFileChooserDefault *impl,
10103 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10105 GtkWidget *widget_to_focus;
10107 /* This will give us the location widgets back */
10108 switch (impl->operation_mode)
10110 case OPERATION_MODE_SEARCH:
10111 search_switch_to_browse_mode (impl);
10113 case OPERATION_MODE_RECENT:
10114 recent_switch_to_browse_mode (impl);
10116 case OPERATION_MODE_BROWSE:
10117 g_assert_not_reached ();
10121 if (impl->current_folder)
10122 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10124 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10125 widget_to_focus = impl->browse_files_tree_view;
10127 widget_to_focus = impl->location_entry;
10129 gtk_widget_grab_focus (widget_to_focus);
10133 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10134 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10136 LocationMode new_mode;
10140 /* since the user typed something, we unconditionally want to turn on the entry */
10141 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10143 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10144 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10145 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10146 new_mode = LOCATION_MODE_PATH_BAR;
10149 g_assert_not_reached ();
10153 location_mode_set (impl, new_mode, TRUE);
10154 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10157 location_set_user_text (impl, path);
10160 location_entry_set_initial_text (impl);
10161 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10165 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10166 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10168 gtk_widget_grab_focus (impl->location_entry);
10170 location_set_user_text (impl, path);
10173 g_assert_not_reached ();
10176 /* Handler for the "up-folder" keybinding signal */
10178 up_folder_handler (GtkFileChooserDefault *impl)
10180 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10183 /* Handler for the "down-folder" keybinding signal */
10185 down_folder_handler (GtkFileChooserDefault *impl)
10187 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10190 /* Switches to the shortcut in the specified index */
10192 switch_to_shortcut (GtkFileChooserDefault *impl,
10197 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10198 g_assert_not_reached ();
10200 shortcuts_activate_iter (impl, &iter);
10203 /* Handler for the "home-folder" keybinding signal */
10205 home_folder_handler (GtkFileChooserDefault *impl)
10207 if (impl->has_home)
10208 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10211 /* Handler for the "desktop-folder" keybinding signal */
10213 desktop_folder_handler (GtkFileChooserDefault *impl)
10215 if (impl->has_desktop)
10216 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10219 /* Handler for the "search-shortcut" keybinding signal */
10221 search_shortcut_handler (GtkFileChooserDefault *impl)
10223 if (impl->has_search)
10225 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10227 /* we want the entry widget to grab the focus the first
10228 * time, not the browse_files_tree_view widget.
10230 if (impl->search_entry)
10231 gtk_widget_grab_focus (impl->search_entry);
10235 /* Handler for the "recent-shortcut" keybinding signal */
10237 recent_shortcut_handler (GtkFileChooserDefault *impl)
10239 if (impl->has_recent)
10240 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10244 quick_bookmark_handler (GtkFileChooserDefault *impl,
10245 gint bookmark_index)
10250 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10253 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10255 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10256 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10259 gtk_tree_path_free (path);
10261 switch_to_shortcut (impl, bookmark_pos);
10265 show_hidden_handler (GtkFileChooserDefault *impl)
10267 g_object_set (impl,
10268 "show-hidden", !impl->show_hidden,
10273 /* Drag and drop interfaces */
10276 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10281 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10283 model->impl = NULL;
10286 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10288 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10291 ShortcutsPaneModelFilter *model;
10295 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10297 pos = *gtk_tree_path_get_indices (path);
10298 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10300 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10303 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
10305 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10307 GtkSelectionData *selection_data)
10309 ShortcutsPaneModelFilter *model;
10311 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10318 /* Fill the GtkTreeDragSourceIface vtable */
10320 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10322 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10323 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10327 /* Fill the GtkTreeDragDestIface vtable */
10329 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10331 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10332 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10336 static GtkTreeModel *
10337 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10338 GtkTreeModel *child_model,
10341 ShortcutsPaneModelFilter *model;
10343 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10344 "child-model", child_model,
10345 "virtual-root", root,
10348 model->impl = impl;
10350 return GTK_TREE_MODEL (model);