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 /* Identifiers for target types */
218 search_is_possible (GtkFileChooserDefault *impl)
220 if (impl->search_engine == NULL)
221 impl->search_engine = _gtk_search_engine_new ();
223 return impl->search_engine != NULL;
226 /* Interesting places in the shortcuts bar */
230 SHORTCUTS_RECENT_SEPARATOR,
235 SHORTCUTS_BOOKMARKS_SEPARATOR,
237 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
238 SHORTCUTS_CURRENT_FOLDER
241 /* Icon size for if we can't get it from the theme */
242 #define FALLBACK_ICON_SIZE 16
244 #define PREVIEW_HBOX_SPACING 12
248 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
249 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
251 static GObject* gtk_file_chooser_default_constructor (GType type,
252 guint n_construct_properties,
253 GObjectConstructParam *construct_params);
254 static void gtk_file_chooser_default_finalize (GObject *object);
255 static void gtk_file_chooser_default_set_property (GObject *object,
259 static void gtk_file_chooser_default_get_property (GObject *object,
263 static void gtk_file_chooser_default_dispose (GObject *object);
264 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
265 static void gtk_file_chooser_default_realize (GtkWidget *widget);
266 static void gtk_file_chooser_default_map (GtkWidget *widget);
267 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
268 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
269 GtkWidget *previous_toplevel);
270 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
271 GtkStyle *previous_style);
272 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
273 GdkScreen *previous_screen);
274 static void gtk_file_chooser_default_size_allocate (GtkWidget *widget,
275 GtkAllocation *allocation);
277 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
280 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
283 gboolean clear_entry,
285 static GFile * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
286 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
288 static gboolean gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
291 static void gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
293 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
294 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
295 static GSList * gtk_file_chooser_default_get_files (GtkFileChooser *chooser);
296 static GFile * gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser);
297 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
298 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
299 GtkFileFilter *filter);
300 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
301 GtkFileFilter *filter);
302 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
303 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
306 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
309 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
311 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
313 gint *default_height);
314 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
315 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
317 static void location_popup_handler (GtkFileChooserDefault *impl,
319 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
320 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
321 static void up_folder_handler (GtkFileChooserDefault *impl);
322 static void down_folder_handler (GtkFileChooserDefault *impl);
323 static void home_folder_handler (GtkFileChooserDefault *impl);
324 static void desktop_folder_handler (GtkFileChooserDefault *impl);
325 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
326 gint bookmark_index);
327 static void show_hidden_handler (GtkFileChooserDefault *impl);
328 static void search_shortcut_handler (GtkFileChooserDefault *impl);
329 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
330 static void update_appearance (GtkFileChooserDefault *impl);
332 static void set_current_filter (GtkFileChooserDefault *impl,
333 GtkFileFilter *filter);
334 static void check_preview_change (GtkFileChooserDefault *impl);
336 static void filter_combo_changed (GtkComboBox *combo_box,
337 GtkFileChooserDefault *impl);
339 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
341 GtkFileChooserDefault *impl);
343 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
346 gboolean path_currently_selected,
348 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
350 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
352 static int shortcuts_get_index (GtkFileChooserDefault *impl,
353 ShortcutsIndex where);
354 static int shortcut_find_position (GtkFileChooserDefault *impl,
357 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
359 static gboolean list_select_func (GtkTreeSelection *selection,
362 gboolean path_currently_selected,
365 static void list_selection_changed (GtkTreeSelection *tree_selection,
366 GtkFileChooserDefault *impl);
367 static void list_row_activated (GtkTreeView *tree_view,
369 GtkTreeViewColumn *column,
370 GtkFileChooserDefault *impl);
372 static void select_func (GtkFileSystemModel *model,
377 static void path_bar_clicked (GtkPathBar *path_bar,
380 gboolean child_is_hidden,
381 GtkFileChooserDefault *impl);
383 static void add_bookmark_button_clicked_cb (GtkButton *button,
384 GtkFileChooserDefault *impl);
385 static void remove_bookmark_button_clicked_cb (GtkButton *button,
386 GtkFileChooserDefault *impl);
387 static void save_folder_combo_changed_cb (GtkComboBox *combo,
388 GtkFileChooserDefault *impl);
390 static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
392 static void load_remove_timer (GtkFileChooserDefault *impl);
393 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
395 static void location_button_toggled_cb (GtkToggleButton *toggle,
396 GtkFileChooserDefault *impl);
397 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
399 static void search_stop_searching (GtkFileChooserDefault *impl,
400 gboolean remove_query);
401 static void search_clear_model (GtkFileChooserDefault *impl,
402 gboolean remove_from_treeview);
403 static gboolean search_should_respond (GtkFileChooserDefault *impl);
404 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
405 static GSList *search_get_selected_files (GtkFileChooserDefault *impl);
406 static void search_entry_activate_cb (GtkEntry *entry,
408 static void settings_load (GtkFileChooserDefault *impl);
410 static void recent_stop_loading (GtkFileChooserDefault *impl);
411 static void recent_clear_model (GtkFileChooserDefault *impl,
412 gboolean remove_from_treeview);
413 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
414 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
415 static GSList * recent_get_selected_files (GtkFileChooserDefault *impl);
416 static void set_file_system_backend (GtkFileChooserDefault *impl);
417 static void unset_file_system_backend (GtkFileChooserDefault *impl);
423 /* Drag and drop interface declarations */
426 GtkTreeModelFilter parent;
428 GtkFileChooserDefault *impl;
429 } ShortcutsPaneModelFilter;
432 GtkTreeModelFilterClass parent_class;
433 } ShortcutsPaneModelFilterClass;
435 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
436 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
438 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
440 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
441 _shortcuts_pane_model_filter,
442 GTK_TYPE_TREE_MODEL_FILTER,
443 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
444 shortcuts_pane_model_filter_drag_source_iface_init))
446 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
447 GtkTreeModel *child_model,
452 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
453 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
454 gtk_file_chooser_default_iface_init)
455 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
456 gtk_file_chooser_embed_default_iface_init));
459 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
461 static const guint quick_bookmark_keyvals[10] = {
462 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
464 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
465 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
466 GtkBindingSet *binding_set;
469 gobject_class->finalize = gtk_file_chooser_default_finalize;
470 gobject_class->constructor = gtk_file_chooser_default_constructor;
471 gobject_class->set_property = gtk_file_chooser_default_set_property;
472 gobject_class->get_property = gtk_file_chooser_default_get_property;
473 gobject_class->dispose = gtk_file_chooser_default_dispose;
475 widget_class->show_all = gtk_file_chooser_default_show_all;
476 widget_class->realize = gtk_file_chooser_default_realize;
477 widget_class->map = gtk_file_chooser_default_map;
478 widget_class->unmap = gtk_file_chooser_default_unmap;
479 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
480 widget_class->style_set = gtk_file_chooser_default_style_set;
481 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
482 widget_class->size_allocate = gtk_file_chooser_default_size_allocate;
484 signals[LOCATION_POPUP] =
485 g_signal_new_class_handler (I_("location-popup"),
486 G_OBJECT_CLASS_TYPE (class),
487 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
488 G_CALLBACK (location_popup_handler),
490 _gtk_marshal_VOID__STRING,
491 G_TYPE_NONE, 1, G_TYPE_STRING);
493 signals[LOCATION_POPUP_ON_PASTE] =
494 g_signal_new_class_handler (I_("location-popup-on-paste"),
495 G_OBJECT_CLASS_TYPE (class),
496 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
497 G_CALLBACK (location_popup_on_paste_handler),
499 _gtk_marshal_VOID__VOID,
502 signals[LOCATION_TOGGLE_POPUP] =
503 g_signal_new_class_handler (I_("location-toggle-popup"),
504 G_OBJECT_CLASS_TYPE (class),
505 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
506 G_CALLBACK (location_toggle_popup_handler),
508 _gtk_marshal_VOID__VOID,
512 g_signal_new_class_handler (I_("up-folder"),
513 G_OBJECT_CLASS_TYPE (class),
514 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
515 G_CALLBACK (up_folder_handler),
517 _gtk_marshal_VOID__VOID,
520 signals[DOWN_FOLDER] =
521 g_signal_new_class_handler (I_("down-folder"),
522 G_OBJECT_CLASS_TYPE (class),
523 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
524 G_CALLBACK (down_folder_handler),
526 _gtk_marshal_VOID__VOID,
529 signals[HOME_FOLDER] =
530 g_signal_new_class_handler (I_("home-folder"),
531 G_OBJECT_CLASS_TYPE (class),
532 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
533 G_CALLBACK (home_folder_handler),
535 _gtk_marshal_VOID__VOID,
538 signals[DESKTOP_FOLDER] =
539 g_signal_new_class_handler (I_("desktop-folder"),
540 G_OBJECT_CLASS_TYPE (class),
541 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
542 G_CALLBACK (desktop_folder_handler),
544 _gtk_marshal_VOID__VOID,
547 signals[QUICK_BOOKMARK] =
548 g_signal_new_class_handler (I_("quick-bookmark"),
549 G_OBJECT_CLASS_TYPE (class),
550 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
551 G_CALLBACK (quick_bookmark_handler),
553 _gtk_marshal_VOID__INT,
554 G_TYPE_NONE, 1, G_TYPE_INT);
556 signals[SHOW_HIDDEN] =
557 g_signal_new_class_handler (I_("show-hidden"),
558 G_OBJECT_CLASS_TYPE (class),
559 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
560 G_CALLBACK (show_hidden_handler),
562 _gtk_marshal_VOID__VOID,
565 signals[SEARCH_SHORTCUT] =
566 g_signal_new_class_handler (I_("search-shortcut"),
567 G_OBJECT_CLASS_TYPE (class),
568 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
569 G_CALLBACK (search_shortcut_handler),
571 _gtk_marshal_VOID__VOID,
574 signals[RECENT_SHORTCUT] =
575 g_signal_new_class_handler (I_("recent-shortcut"),
576 G_OBJECT_CLASS_TYPE (class),
577 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
578 G_CALLBACK (recent_shortcut_handler),
580 _gtk_marshal_VOID__VOID,
583 binding_set = gtk_binding_set_by_class (class);
585 gtk_binding_entry_add_signal (binding_set,
586 GDK_l, GDK_CONTROL_MASK,
587 "location-toggle-popup",
590 gtk_binding_entry_add_signal (binding_set,
593 1, G_TYPE_STRING, "/");
594 gtk_binding_entry_add_signal (binding_set,
597 1, G_TYPE_STRING, "/");
600 gtk_binding_entry_add_signal (binding_set,
603 1, G_TYPE_STRING, "~");
606 gtk_binding_entry_add_signal (binding_set,
607 GDK_v, GDK_CONTROL_MASK,
608 "location-popup-on-paste",
610 gtk_binding_entry_add_signal (binding_set,
611 GDK_Up, GDK_MOD1_MASK,
614 gtk_binding_entry_add_signal (binding_set,
618 gtk_binding_entry_add_signal (binding_set,
619 GDK_KP_Up, GDK_MOD1_MASK,
623 gtk_binding_entry_add_signal (binding_set,
624 GDK_Down, GDK_MOD1_MASK,
627 gtk_binding_entry_add_signal (binding_set,
628 GDK_KP_Down, GDK_MOD1_MASK,
632 gtk_binding_entry_add_signal (binding_set,
633 GDK_Home, GDK_MOD1_MASK,
636 gtk_binding_entry_add_signal (binding_set,
637 GDK_KP_Home, GDK_MOD1_MASK,
640 gtk_binding_entry_add_signal (binding_set,
641 GDK_d, GDK_MOD1_MASK,
644 gtk_binding_entry_add_signal (binding_set,
645 GDK_h, GDK_CONTROL_MASK,
648 gtk_binding_entry_add_signal (binding_set,
649 GDK_s, GDK_MOD1_MASK,
652 gtk_binding_entry_add_signal (binding_set,
653 GDK_r, GDK_MOD1_MASK,
657 for (i = 0; i < 10; i++)
658 gtk_binding_entry_add_signal (binding_set,
659 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
663 _gtk_file_chooser_install_properties (gobject_class);
667 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
669 iface->select_file = gtk_file_chooser_default_select_file;
670 iface->unselect_file = gtk_file_chooser_default_unselect_file;
671 iface->select_all = gtk_file_chooser_default_select_all;
672 iface->unselect_all = gtk_file_chooser_default_unselect_all;
673 iface->get_files = gtk_file_chooser_default_get_files;
674 iface->get_preview_file = gtk_file_chooser_default_get_preview_file;
675 iface->get_file_system = gtk_file_chooser_default_get_file_system;
676 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
677 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
678 iface->set_current_name = gtk_file_chooser_default_set_current_name;
679 iface->add_filter = gtk_file_chooser_default_add_filter;
680 iface->remove_filter = gtk_file_chooser_default_remove_filter;
681 iface->list_filters = gtk_file_chooser_default_list_filters;
682 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
683 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
684 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
688 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
690 iface->get_default_size = gtk_file_chooser_default_get_default_size;
691 iface->should_respond = gtk_file_chooser_default_should_respond;
692 iface->initial_focus = gtk_file_chooser_default_initial_focus;
696 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
698 profile_start ("start", NULL);
699 #ifdef PROFILE_FILE_CHOOSER
700 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
702 impl->local_only = TRUE;
703 impl->preview_widget_active = TRUE;
704 impl->use_preview_label = TRUE;
705 impl->select_multiple = FALSE;
706 impl->show_hidden = FALSE;
707 impl->show_size_column = TRUE;
708 impl->icon_size = FALLBACK_ICON_SIZE;
709 impl->load_state = LOAD_EMPTY;
710 impl->reload_state = RELOAD_EMPTY;
711 impl->pending_select_files = NULL;
712 impl->location_mode = LOCATION_MODE_PATH_BAR;
713 impl->operation_mode = OPERATION_MODE_BROWSE;
714 impl->sort_column = MODEL_COL_NAME;
715 impl->sort_order = GTK_SORT_ASCENDING;
716 impl->recent_manager = gtk_recent_manager_get_default ();
717 impl->create_folders = TRUE;
719 gtk_box_set_spacing (GTK_BOX (impl), 12);
721 set_file_system_backend (impl);
723 profile_end ("end", NULL);
726 /* Frees the data columns for the specified iter in the shortcuts model*/
728 shortcuts_free_row_data (GtkFileChooserDefault *impl,
732 ShortcutType shortcut_type;
733 GCancellable *cancellable;
735 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
736 SHORTCUTS_COL_DATA, &col_data,
737 SHORTCUTS_COL_TYPE, &shortcut_type,
738 SHORTCUTS_COL_CANCELLABLE, &cancellable,
742 g_cancellable_cancel (cancellable);
744 if (!(shortcut_type == SHORTCUT_TYPE_FILE ||
745 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
749 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
751 GtkFileSystemVolume *volume;
754 _gtk_file_system_volume_free (volume);
760 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
763 g_object_unref (file);
767 /* Frees all the data columns in the shortcuts model */
769 shortcuts_free (GtkFileChooserDefault *impl)
773 if (!impl->shortcuts_model)
776 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
779 shortcuts_free_row_data (impl, &iter);
781 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
783 g_object_unref (impl->shortcuts_model);
784 impl->shortcuts_model = NULL;
788 pending_select_files_free (GtkFileChooserDefault *impl)
790 g_slist_foreach (impl->pending_select_files, (GFunc) g_object_unref, NULL);
791 g_slist_free (impl->pending_select_files);
792 impl->pending_select_files = NULL;
796 pending_select_files_add (GtkFileChooserDefault *impl,
799 impl->pending_select_files =
800 g_slist_prepend (impl->pending_select_files, g_object_ref (file));
803 /* Used from gtk_tree_selection_selected_foreach() */
805 store_selection_foreach (GtkTreeModel *model,
810 GtkFileChooserDefault *impl;
813 impl = GTK_FILE_CHOOSER_DEFAULT (data);
815 file = _gtk_file_system_model_get_file (GTK_FILE_SYSTEM_MODEL (model), iter);
816 pending_select_files_add (impl, file);
819 /* Stores the current selection in the list of paths to select; this is used to
820 * preserve the selection when reloading the current folder.
823 pending_select_files_store_selection (GtkFileChooserDefault *impl)
825 GtkTreeSelection *selection;
827 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
828 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
832 gtk_file_chooser_default_finalize (GObject *object)
834 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
837 unset_file_system_backend (impl);
839 if (impl->shortcuts_pane_filter_model)
840 g_object_unref (impl->shortcuts_pane_filter_model);
842 if (impl->shortcuts_combo_filter_model)
843 g_object_unref (impl->shortcuts_combo_filter_model);
845 shortcuts_free (impl);
847 g_free (impl->browse_files_last_selected_name);
849 for (l = impl->filters; l; l = l->next)
851 GtkFileFilter *filter;
853 filter = GTK_FILE_FILTER (l->data);
854 g_object_unref (filter);
856 g_slist_free (impl->filters);
858 if (impl->current_filter)
859 g_object_unref (impl->current_filter);
861 if (impl->current_volume_file)
862 g_object_unref (impl->current_volume_file);
864 if (impl->current_folder)
865 g_object_unref (impl->current_folder);
867 if (impl->preview_file)
868 g_object_unref (impl->preview_file);
870 if (impl->browse_path_bar_size_group)
871 g_object_unref (impl->browse_path_bar_size_group);
873 load_remove_timer (impl);
875 /* Free all the Models we have */
876 if (impl->browse_files_model)
877 g_object_unref (impl->browse_files_model);
879 search_clear_model (impl, FALSE);
880 recent_clear_model (impl, FALSE);
882 g_free (impl->preview_display_name);
884 g_free (impl->edited_new_text);
886 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
889 /* Shows an error dialog set as transient for the specified window */
891 error_message_with_parent (GtkWindow *parent,
897 dialog = gtk_message_dialog_new (parent,
898 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
903 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
906 if (parent && parent->group)
907 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
909 gtk_dialog_run (GTK_DIALOG (dialog));
910 gtk_widget_destroy (dialog);
913 /* Returns a toplevel GtkWindow, or NULL if none */
915 get_toplevel (GtkWidget *widget)
919 toplevel = gtk_widget_get_toplevel (widget);
920 if (!GTK_WIDGET_TOPLEVEL (toplevel))
923 return GTK_WINDOW (toplevel);
926 /* Shows an error dialog for the file chooser */
928 error_message (GtkFileChooserDefault *impl,
932 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
935 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
937 error_dialog (GtkFileChooserDefault *impl,
948 uri = g_file_get_uri (file);
949 text = g_strdup_printf (msg, uri);
950 error_message (impl, text, error->message);
953 g_error_free (error);
957 /* Displays an error message about not being able to get information for a file.
958 * Frees the GError as well.
961 error_getting_info_dialog (GtkFileChooserDefault *impl,
966 _("Could not retrieve information about the file"),
970 /* Shows an error dialog about not being able to add a bookmark */
972 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
977 _("Could not add a bookmark"),
981 /* Shows an error dialog about not being able to remove a bookmark */
983 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
988 _("Could not remove bookmark"),
992 /* Shows an error dialog about not being able to create a folder */
994 error_creating_folder_dialog (GtkFileChooserDefault *impl,
999 _("The folder could not be created"),
1003 /* Shows an error about not being able to create a folder because a file with
1004 * the same name is already there.
1007 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1012 _("The folder could not be created, as a file with the same "
1013 "name already exists. Try using a different name for the "
1014 "folder, or rename the file first."),
1018 /* Shows an error dialog about not being able to create a filename */
1020 error_building_filename_dialog (GtkFileChooserDefault *impl,
1023 error_dialog (impl, _("Invalid file name"),
1027 /* Shows an error dialog when we cannot switch to a folder */
1029 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1033 error_dialog (impl, _("The folder contents could not be displayed"),
1037 /* Changes folders, displaying an error dialog if this fails */
1039 change_folder_and_display_error (GtkFileChooserDefault *impl,
1041 gboolean clear_entry)
1046 g_return_val_if_fail (G_IS_FILE (file), FALSE);
1048 /* We copy the path because of this case:
1050 * list_row_activated()
1051 * fetches path from model; path belongs to the model (*)
1052 * calls change_folder_and_display_error()
1053 * calls gtk_file_chooser_set_current_folder_file()
1054 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1058 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), file, TRUE, clear_entry, &error);
1061 error_changing_folder_dialog (impl, file, error);
1067 emit_default_size_changed (GtkFileChooserDefault *impl)
1069 profile_msg (" emit default-size-changed start", NULL);
1070 g_signal_emit_by_name (impl, "default-size-changed");
1071 profile_msg (" emit default-size-changed end", NULL);
1075 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1077 if (impl->use_preview_label)
1079 if (!impl->preview_label)
1081 impl->preview_label = gtk_label_new (impl->preview_display_name);
1082 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1083 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1084 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1085 gtk_widget_show (impl->preview_label);
1090 if (impl->preview_label)
1092 gtk_widget_destroy (impl->preview_label);
1093 impl->preview_label = NULL;
1097 if (impl->preview_widget_active && impl->preview_widget)
1098 gtk_widget_show (impl->preview_box);
1100 gtk_widget_hide (impl->preview_box);
1102 if (!GTK_WIDGET_MAPPED (impl))
1103 emit_default_size_changed (impl);
1107 set_preview_widget (GtkFileChooserDefault *impl,
1108 GtkWidget *preview_widget)
1110 if (preview_widget == impl->preview_widget)
1113 if (impl->preview_widget)
1114 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1115 impl->preview_widget);
1117 impl->preview_widget = preview_widget;
1118 if (impl->preview_widget)
1120 gtk_widget_show (impl->preview_widget);
1121 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1122 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1123 impl->preview_widget,
1124 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1127 update_preview_widget_visibility (impl);
1130 /* Renders a "Search" icon at an appropriate size for a tree view */
1132 render_search_icon (GtkFileChooserDefault *impl)
1134 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1138 render_recent_icon (GtkFileChooserDefault *impl)
1140 GtkIconTheme *theme;
1143 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
1144 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1146 theme = gtk_icon_theme_get_default ();
1148 retval = gtk_icon_theme_load_icon (theme, "document-open-recent",
1154 retval = gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1160 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1161 struct ReloadIconsData
1163 GtkFileChooserDefault *impl;
1164 GtkTreeRowReference *row_ref;
1168 shortcuts_reload_icons_get_info_cb (GCancellable *cancellable,
1170 const GError *error,
1176 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1177 struct ReloadIconsData *data = user_data;
1179 if (!g_slist_find (data->impl->reload_icon_cancellables, cancellable))
1182 data->impl->reload_icon_cancellables = g_slist_remove (data->impl->reload_icon_cancellables, cancellable);
1184 if (cancelled || error)
1187 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->impl), data->impl->icon_size);
1189 path = gtk_tree_row_reference_get_path (data->row_ref);
1190 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1191 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1192 SHORTCUTS_COL_PIXBUF, pixbuf,
1194 gtk_tree_path_free (path);
1197 g_object_unref (pixbuf);
1200 gtk_tree_row_reference_free (data->row_ref);
1201 g_object_unref (data->impl);
1204 g_object_unref (cancellable);
1208 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1213 profile_start ("start", NULL);
1215 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1218 for (l = impl->reload_icon_cancellables; l; l = l->next)
1220 GCancellable *cancellable = G_CANCELLABLE (l->data);
1221 g_cancellable_cancel (cancellable);
1223 g_slist_free (impl->reload_icon_cancellables);
1224 impl->reload_icon_cancellables = NULL;
1229 ShortcutType shortcut_type;
1230 gboolean pixbuf_visible;
1233 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1234 SHORTCUTS_COL_DATA, &data,
1235 SHORTCUTS_COL_TYPE, &shortcut_type,
1236 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1242 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1244 GtkFileSystemVolume *volume;
1247 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1248 impl->icon_size, NULL);
1250 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1252 if (g_file_is_native (G_FILE (data)))
1255 struct ReloadIconsData *info;
1256 GtkTreePath *tree_path;
1257 GCancellable *cancellable;
1261 info = g_new0 (struct ReloadIconsData, 1);
1262 info->impl = g_object_ref (impl);
1263 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1264 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1265 gtk_tree_path_free (tree_path);
1267 cancellable = _gtk_file_system_get_info (impl->file_system, file,
1269 shortcuts_reload_icons_get_info_cb,
1271 impl->reload_icon_cancellables = g_slist_append (impl->reload_icon_cancellables, cancellable);
1275 GtkIconTheme *icon_theme;
1277 /* Don't call get_info for remote paths to avoid latency and
1279 * If we switch to a better bookmarks file format (XBEL), we
1280 * should use mime info to get a better icon.
1282 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1283 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1284 impl->icon_size, 0, NULL);
1287 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1289 pixbuf = render_search_icon (impl);
1291 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1293 pixbuf = render_recent_icon (impl);
1296 gtk_list_store_set (impl->shortcuts_model, &iter,
1297 SHORTCUTS_COL_PIXBUF, pixbuf,
1301 g_object_unref (pixbuf);
1305 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1309 profile_end ("end", NULL);
1313 shortcuts_find_folder (GtkFileChooserDefault *impl,
1316 GtkTreeSelection *selection;
1320 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1322 g_assert (folder != NULL);
1323 pos = shortcut_find_position (impl, folder);
1326 gtk_tree_selection_unselect_all (selection);
1330 path = gtk_tree_path_new_from_indices (pos, -1);
1331 gtk_tree_selection_select_path (selection, path);
1332 gtk_tree_path_free (path);
1335 /* If a shortcut corresponds to the current folder, selects it */
1337 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1339 shortcuts_find_folder (impl, impl->current_folder);
1342 /* Removes the specified number of rows from the shortcuts list */
1344 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1350 path = gtk_tree_path_new_from_indices (start_row, -1);
1352 for (; n_rows; n_rows--)
1356 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1357 g_assert_not_reached ();
1359 shortcuts_free_row_data (impl, &iter);
1360 gtk_list_store_remove (impl->shortcuts_model, &iter);
1363 gtk_tree_path_free (path);
1367 shortcuts_update_count (GtkFileChooserDefault *impl,
1368 ShortcutsIndex type,
1373 case SHORTCUTS_HOME:
1375 impl->has_home = FALSE;
1377 impl->has_home = TRUE;
1380 case SHORTCUTS_DESKTOP:
1382 impl->has_desktop = FALSE;
1384 impl->has_desktop = TRUE;
1387 case SHORTCUTS_VOLUMES:
1388 impl->num_volumes += value;
1391 case SHORTCUTS_SHORTCUTS:
1392 impl->num_shortcuts += value;
1395 case SHORTCUTS_BOOKMARKS:
1396 impl->num_bookmarks += value;
1399 case SHORTCUTS_CURRENT_FOLDER:
1401 impl->shortcuts_current_folder_active = FALSE;
1403 impl->shortcuts_current_folder_active = TRUE;
1412 struct ShortcutsInsertRequest
1414 GtkFileChooserDefault *impl;
1418 GtkTreeRowReference *row_ref;
1419 ShortcutsIndex type;
1425 get_file_info_finished (GCancellable *cancellable,
1427 const GError *error,
1431 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1435 GCancellable *model_cancellable;
1436 struct ShortcutsInsertRequest *request = data;
1438 path = gtk_tree_row_reference_get_path (request->row_ref);
1440 /* Handle doesn't exist anymore in the model */
1443 pos = gtk_tree_path_get_indices (path)[0];
1444 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1446 gtk_tree_path_free (path);
1448 /* validate cancellable, else goto out */
1449 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1450 SHORTCUTS_COL_CANCELLABLE, &model_cancellable,
1452 if (cancellable != model_cancellable)
1455 /* set the cancellable to NULL in the model (we unref later on) */
1456 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1457 SHORTCUTS_COL_CANCELLABLE, NULL,
1465 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1466 shortcuts_update_count (request->impl, request->type, -1);
1468 if (request->type == SHORTCUTS_HOME)
1472 home = g_file_new_for_path (g_get_home_dir ());
1473 error_getting_info_dialog (request->impl, home, g_error_copy (error));
1474 g_object_unref (home);
1476 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1478 /* Remove the current folder separator */
1479 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1480 shortcuts_remove_rows (request->impl, separator_pos, 1);
1486 if (!request->label_copy)
1487 request->label_copy = g_strdup (g_file_info_get_display_name (info));
1488 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1489 request->impl->icon_size);
1491 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1492 SHORTCUTS_COL_PIXBUF, pixbuf,
1493 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1494 SHORTCUTS_COL_NAME, request->label_copy,
1495 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1496 SHORTCUTS_COL_REMOVABLE, request->removable,
1499 if (request->impl->shortcuts_pane_filter_model)
1500 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1502 if (request->impl->shortcuts_combo_filter_model)
1503 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1505 if (request->type == SHORTCUTS_CURRENT_FOLDER &&
1506 request->impl->save_folder_combo != NULL)
1508 /* The current folder is updated via _activate_iter(), don't
1509 * have save_folder_combo_changed_cb() call _activate_iter()
1512 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1513 G_CALLBACK (save_folder_combo_changed_cb),
1516 if (request->impl->has_search)
1519 if (request->impl->has_recent)
1522 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1523 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1524 G_CALLBACK (save_folder_combo_changed_cb),
1529 g_object_unref (pixbuf);
1532 g_object_unref (request->impl);
1533 g_object_unref (request->file);
1534 gtk_tree_row_reference_free (request->row_ref);
1535 g_free (request->label_copy);
1538 g_object_unref (cancellable);
1541 /* FIXME: GtkFileSystem needs a function to split a remote path
1542 * into hostname and path components, or maybe just have a
1543 * gtk_file_system_path_get_display_name().
1545 * This function is also used in gtkfilechooserbutton.c
1548 _gtk_file_chooser_label_for_file (GFile *file)
1550 const gchar *path, *start, *end, *p;
1551 gchar *uri, *host, *label;
1553 uri = g_file_get_uri (file);
1555 start = strstr (uri, "://");
1559 path = strchr (start, '/');
1564 end = uri + strlen (uri);
1568 /* strip username */
1569 p = strchr (start, '@');
1573 p = strchr (start, ':');
1577 host = g_strndup (start, end - start);
1579 /* Translators: the first string is a path and the second string
1580 * is a hostname. Nautilus and the panel contain the same string
1583 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1589 label = g_strdup (uri);
1597 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1598 * inserts a volume. A position of -1 indicates the end of the tree.
1601 shortcuts_insert_file (GtkFileChooserDefault *impl,
1603 ShortcutType shortcut_type,
1604 GtkFileSystemVolume *volume,
1608 ShortcutsIndex type)
1611 GdkPixbuf *pixbuf = NULL;
1612 gpointer data = NULL;
1614 GtkIconTheme *icon_theme;
1616 profile_start ("start shortcut", NULL);
1618 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1621 label_copy = _gtk_file_system_volume_get_display_name (volume);
1622 pixbuf = _gtk_file_system_volume_render_icon (volume, GTK_WIDGET (impl),
1623 impl->icon_size, NULL);
1625 else if (shortcut_type == SHORTCUT_TYPE_FILE)
1627 if (g_file_is_native (file))
1629 struct ShortcutsInsertRequest *request;
1630 GCancellable *cancellable;
1633 request = g_new0 (struct ShortcutsInsertRequest, 1);
1634 request->impl = g_object_ref (impl);
1635 request->file = g_object_ref (file);
1636 request->name_only = TRUE;
1637 request->removable = removable;
1639 request->type = type;
1641 request->label_copy = g_strdup (label);
1644 gtk_list_store_append (impl->shortcuts_model, &iter);
1646 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1648 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1649 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1650 gtk_tree_path_free (p);
1652 cancellable = _gtk_file_system_get_info (request->impl->file_system, request->file,
1653 "standard::is-hidden,standard::is-backup,standard::display-name,standard::icon",
1654 get_file_info_finished, request);
1656 gtk_list_store_set (impl->shortcuts_model, &iter,
1657 SHORTCUTS_COL_DATA, g_object_ref (file),
1658 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_FILE,
1659 SHORTCUTS_COL_CANCELLABLE, cancellable,
1662 shortcuts_update_count (impl, type, 1);
1668 /* Don't call get_info for remote paths to avoid latency and
1671 data = g_object_ref (file);
1673 label_copy = g_strdup (label);
1675 label_copy = _gtk_file_chooser_label_for_file (file);
1677 /* If we switch to a better bookmarks file format (XBEL), we
1678 * should use mime info to get a better icon.
1680 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1681 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1682 impl->icon_size, 0, NULL);
1687 g_assert_not_reached ();
1693 gtk_list_store_append (impl->shortcuts_model, &iter);
1695 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1697 shortcuts_update_count (impl, type, 1);
1699 gtk_list_store_set (impl->shortcuts_model, &iter,
1700 SHORTCUTS_COL_PIXBUF, pixbuf,
1701 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1702 SHORTCUTS_COL_NAME, label_copy,
1703 SHORTCUTS_COL_DATA, data,
1704 SHORTCUTS_COL_TYPE, shortcut_type,
1705 SHORTCUTS_COL_REMOVABLE, removable,
1706 SHORTCUTS_COL_CANCELLABLE, NULL,
1709 if (impl->shortcuts_pane_filter_model)
1710 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1712 if (impl->shortcuts_combo_filter_model)
1713 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1715 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1717 /* The current folder is updated via _activate_iter(), don't
1718 * have save_folder_combo_changed_cb() call _activate_iter()
1721 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1723 if (impl->has_search)
1726 if (impl->has_recent)
1729 g_signal_handlers_block_by_func (impl->save_folder_combo,
1730 G_CALLBACK (save_folder_combo_changed_cb),
1733 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1734 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1735 G_CALLBACK (save_folder_combo_changed_cb),
1739 g_free (label_copy);
1742 g_object_unref (pixbuf);
1744 profile_end ("end", NULL);
1748 shortcuts_append_search (GtkFileChooserDefault *impl)
1753 pixbuf = render_search_icon (impl);
1755 gtk_list_store_append (impl->shortcuts_model, &iter);
1756 gtk_list_store_set (impl->shortcuts_model, &iter,
1757 SHORTCUTS_COL_PIXBUF, pixbuf,
1758 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1759 SHORTCUTS_COL_NAME, _("Search"),
1760 SHORTCUTS_COL_DATA, NULL,
1761 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1762 SHORTCUTS_COL_REMOVABLE, FALSE,
1766 g_object_unref (pixbuf);
1768 impl->has_search = TRUE;
1772 shortcuts_append_recent (GtkFileChooserDefault *impl)
1777 pixbuf = render_recent_icon (impl);
1779 gtk_list_store_append (impl->shortcuts_model, &iter);
1780 gtk_list_store_set (impl->shortcuts_model, &iter,
1781 SHORTCUTS_COL_PIXBUF, pixbuf,
1782 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1783 SHORTCUTS_COL_NAME, _("Recently Used"),
1784 SHORTCUTS_COL_DATA, NULL,
1785 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1786 SHORTCUTS_COL_REMOVABLE, FALSE,
1790 g_object_unref (pixbuf);
1792 impl->has_recent = TRUE;
1795 /* Appends an item for the user's home directory to the shortcuts model */
1797 shortcuts_append_home (GtkFileChooserDefault *impl)
1799 const char *home_path;
1802 profile_start ("start", NULL);
1804 home_path = g_get_home_dir ();
1805 if (home_path == NULL)
1807 profile_end ("end - no home directory!?", NULL);
1811 home = g_file_new_for_path (home_path);
1812 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, home, NULL, FALSE, SHORTCUTS_HOME);
1813 impl->has_home = TRUE;
1815 g_object_unref (home);
1817 profile_end ("end", NULL);
1820 /* Appends the ~/Desktop directory to the shortcuts model */
1822 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1827 profile_start ("start", NULL);
1829 name = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1830 /* "To disable a directory, point it to the homedir."
1831 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1833 if (!g_strcmp0 (name, g_get_home_dir ()))
1835 profile_end ("end", NULL);
1839 file = g_file_new_for_path (name);
1840 shortcuts_insert_file (impl, -1, SHORTCUT_TYPE_FILE, NULL, file, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1841 impl->has_desktop = TRUE;
1843 /* We do not actually pop up an error dialog if there is no desktop directory
1844 * because some people may really not want to have one.
1847 g_object_unref (file);
1849 profile_end ("end", NULL);
1852 /* Appends a list of GFile to the shortcuts model; returns how many were inserted */
1854 shortcuts_append_bookmarks (GtkFileChooserDefault *impl,
1861 profile_start ("start", NULL);
1863 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR) + 1;
1866 for (; bookmarks; bookmarks = bookmarks->next)
1870 file = bookmarks->data;
1872 if (impl->local_only && !g_file_is_native (file))
1875 if (shortcut_find_position (impl, file) != -1)
1878 label = _gtk_file_system_get_bookmark_label (impl->file_system, file);
1880 shortcuts_insert_file (impl, start_row + num_inserted, SHORTCUT_TYPE_FILE, NULL, file, label, TRUE, SHORTCUTS_BOOKMARKS);
1886 profile_end ("end", NULL);
1888 return num_inserted;
1891 /* Returns the index for the corresponding item in the shortcuts bar */
1893 shortcuts_get_index (GtkFileChooserDefault *impl,
1894 ShortcutsIndex where)
1900 if (where == SHORTCUTS_SEARCH)
1903 n += impl->has_search ? 1 : 0;
1905 if (where == SHORTCUTS_RECENT)
1908 n += impl->has_recent ? 1 : 0;
1910 if (where == SHORTCUTS_RECENT_SEPARATOR)
1913 n += impl->has_recent ? 1 : 0;
1915 if (where == SHORTCUTS_HOME)
1918 n += impl->has_home ? 1 : 0;
1920 if (where == SHORTCUTS_DESKTOP)
1923 n += impl->has_desktop ? 1 : 0;
1925 if (where == SHORTCUTS_VOLUMES)
1928 n += impl->num_volumes;
1930 if (where == SHORTCUTS_SHORTCUTS)
1933 n += impl->num_shortcuts;
1935 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1938 /* If there are no bookmarks there won't be a separator */
1939 n += (impl->num_bookmarks > 0) ? 1 : 0;
1941 if (where == SHORTCUTS_BOOKMARKS)
1944 n += impl->num_bookmarks;
1946 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1951 if (where == SHORTCUTS_CURRENT_FOLDER)
1954 g_assert_not_reached ();
1961 /* Adds all the file system volumes to the shortcuts model */
1963 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1968 gboolean old_changing_folders;
1970 profile_start ("start", NULL);
1972 old_changing_folders = impl->changing_folder;
1973 impl->changing_folder = TRUE;
1975 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1976 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1977 impl->num_volumes = 0;
1979 list = _gtk_file_system_list_volumes (impl->file_system);
1983 for (l = list; l; l = l->next)
1985 GtkFileSystemVolume *volume;
1989 if (impl->local_only)
1991 if (_gtk_file_system_volume_is_mounted (volume))
1994 gboolean base_is_native = TRUE;
1996 base_file = _gtk_file_system_volume_get_root (volume);
1997 if (base_file != NULL)
1999 base_is_native = g_file_is_native (base_file);
2000 g_object_unref (base_file);
2003 if (!base_is_native)
2008 shortcuts_insert_file (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2012 impl->num_volumes = n;
2013 g_slist_free (list);
2015 if (impl->shortcuts_pane_filter_model)
2016 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2018 if (impl->shortcuts_combo_filter_model)
2019 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2021 impl->changing_folder = old_changing_folders;
2023 profile_end ("end", NULL);
2026 /* Inserts a separator node in the shortcuts list */
2028 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2029 ShortcutsIndex where)
2033 g_assert (where == SHORTCUTS_RECENT_SEPARATOR ||
2034 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2035 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2037 gtk_list_store_insert (impl->shortcuts_model, &iter,
2038 shortcuts_get_index (impl, where));
2039 gtk_list_store_set (impl->shortcuts_model, &iter,
2040 SHORTCUTS_COL_PIXBUF, NULL,
2041 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2042 SHORTCUTS_COL_NAME, NULL,
2043 SHORTCUTS_COL_DATA, NULL,
2044 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2048 /* Updates the list of bookmarks */
2050 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2053 gboolean old_changing_folders;
2055 GFile *list_selected = NULL;
2056 GFile *combo_selected = NULL;
2057 ShortcutType shortcut_type;
2060 profile_start ("start", NULL);
2062 old_changing_folders = impl->changing_folder;
2063 impl->changing_folder = TRUE;
2065 if (shortcuts_get_selected (impl, &iter))
2067 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2069 SHORTCUTS_COL_DATA, &col_data,
2070 SHORTCUTS_COL_TYPE, &shortcut_type,
2073 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2074 list_selected = g_object_ref (col_data);
2077 if (impl->save_folder_combo &&
2078 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2081 GtkTreeIter child_iter;
2083 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2086 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2088 SHORTCUTS_COL_DATA, &col_data,
2089 SHORTCUTS_COL_TYPE, &shortcut_type,
2092 if (col_data && shortcut_type == SHORTCUT_TYPE_FILE)
2093 combo_selected = g_object_ref (col_data);
2096 if (impl->num_bookmarks > 0)
2097 shortcuts_remove_rows (impl,
2098 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2099 impl->num_bookmarks + 1);
2101 impl->num_bookmarks = 0;
2102 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2104 bookmarks = _gtk_file_system_list_bookmarks (impl->file_system);
2105 shortcuts_append_bookmarks (impl, bookmarks);
2106 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2107 g_slist_free (bookmarks);
2109 if (impl->num_bookmarks == 0)
2110 shortcuts_remove_rows (impl, shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR), 1);
2112 if (impl->shortcuts_pane_filter_model)
2113 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2115 if (impl->shortcuts_combo_filter_model)
2116 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2120 shortcuts_find_folder (impl, list_selected);
2121 g_object_unref (list_selected);
2128 pos = shortcut_find_position (impl, combo_selected);
2131 if (impl->has_search)
2134 if (impl->has_recent)
2137 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2140 g_object_unref (combo_selected);
2143 impl->changing_folder = old_changing_folders;
2145 profile_end ("end", NULL);
2148 /* Appends a separator and a row to the shortcuts list for the current folder */
2150 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2155 g_assert (!impl->shortcuts_current_folder_active);
2159 g_assert (impl->current_folder != NULL);
2161 pos = shortcut_find_position (impl, impl->current_folder);
2164 GtkFileSystemVolume *volume;
2169 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2173 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2175 volume = _gtk_file_system_get_volume_for_file (impl->file_system, impl->current_folder);
2177 base_file = _gtk_file_system_volume_get_root (volume);
2181 if (base_file && g_file_equal (base_file, impl->current_folder))
2182 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2184 shortcuts_insert_file (impl, pos, SHORTCUT_TYPE_FILE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2187 g_object_unref (base_file);
2189 else if (impl->save_folder_combo != NULL)
2191 if (impl->has_search)
2194 if (impl->has_recent)
2195 pos -= 2; /* + separator */
2197 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2201 /* Updates the current folder row in the shortcuts model */
2203 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2207 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2209 if (impl->shortcuts_current_folder_active)
2211 shortcuts_remove_rows (impl, pos, 2);
2212 impl->shortcuts_current_folder_active = FALSE;
2215 shortcuts_add_current_folder (impl);
2218 /* Filter function used for the shortcuts filter model */
2220 shortcuts_pane_filter_cb (GtkTreeModel *model,
2224 GtkFileChooserDefault *impl;
2228 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2230 path = gtk_tree_model_get_path (model, iter);
2234 pos = *gtk_tree_path_get_indices (path);
2235 gtk_tree_path_free (path);
2237 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2240 /* Creates the list model for shortcuts */
2242 shortcuts_model_create (GtkFileChooserDefault *impl)
2244 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2245 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2246 GDK_TYPE_PIXBUF, /* pixbuf */
2247 G_TYPE_STRING, /* name */
2248 G_TYPE_POINTER, /* path or volume */
2249 G_TYPE_INT, /* ShortcutType */
2250 G_TYPE_BOOLEAN, /* removable */
2251 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2252 G_TYPE_POINTER); /* GCancellable */
2254 if (search_is_possible (impl))
2256 shortcuts_append_search (impl);
2259 if (impl->recent_manager)
2261 shortcuts_append_recent (impl);
2262 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2265 if (impl->file_system)
2267 shortcuts_append_home (impl);
2268 shortcuts_append_desktop (impl);
2269 shortcuts_add_volumes (impl);
2272 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2273 GTK_TREE_MODEL (impl->shortcuts_model),
2276 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2277 shortcuts_pane_filter_cb,
2282 /* Callback used when the "New Folder" button is clicked */
2284 new_folder_button_clicked (GtkButton *button,
2285 GtkFileChooserDefault *impl)
2290 if (!impl->browse_files_model)
2291 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2293 /* Prevent button from being clicked twice */
2294 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2296 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2298 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2299 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2300 path, impl->list_name_column,
2303 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2304 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2306 impl->list_name_column,
2309 gtk_tree_path_free (path);
2312 /* Idle handler for creating a new folder after editing its name cell, or for
2313 * canceling the editing.
2316 edited_idle_cb (GtkFileChooserDefault *impl)
2318 GDK_THREADS_ENTER ();
2320 g_source_destroy (impl->edited_idle);
2321 impl->edited_idle = NULL;
2323 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2324 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2326 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2328 if (impl->edited_new_text /* not cancelled? */
2329 && (strlen (impl->edited_new_text) != 0)
2330 && (strcmp (impl->edited_new_text, DEFAULT_NEW_FOLDER_NAME) != 0)) /* Don't create folder if name is empty or has not been edited */
2332 GError *error = NULL;
2335 file = g_file_get_child_for_display_name (impl->current_folder,
2336 impl->edited_new_text,
2340 GError *error = NULL;
2342 if (g_file_make_directory (file, NULL, &error))
2343 change_folder_and_display_error (impl, file, FALSE);
2345 error_creating_folder_dialog (impl, file, error);
2347 g_object_unref (file);
2350 error_creating_folder_dialog (impl, file, error);
2352 g_free (impl->edited_new_text);
2353 impl->edited_new_text = NULL;
2356 GDK_THREADS_LEAVE ();
2362 queue_edited_idle (GtkFileChooserDefault *impl,
2363 const gchar *new_text)
2365 /* We create the folder in an idle handler so that we don't modify the tree
2369 if (!impl->edited_idle)
2371 impl->edited_idle = g_idle_source_new ();
2372 g_source_set_closure (impl->edited_idle,
2373 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2375 g_source_attach (impl->edited_idle, NULL);
2378 g_free (impl->edited_new_text);
2379 impl->edited_new_text = g_strdup (new_text);
2382 /* Callback used from the text cell renderer when the new folder is named */
2384 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2386 const gchar *new_text,
2387 GtkFileChooserDefault *impl)
2389 /* work around bug #154921 */
2390 g_object_set (cell_renderer_text,
2391 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2392 queue_edited_idle (impl, new_text);
2395 /* Callback used from the text cell renderer when the new folder edition gets
2399 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2400 GtkFileChooserDefault *impl)
2402 /* work around bug #154921 */
2403 g_object_set (cell_renderer_text,
2404 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2405 queue_edited_idle (impl, NULL);
2408 /* Creates the widgets for the filter combo box */
2410 filter_create (GtkFileChooserDefault *impl)
2412 impl->filter_combo = gtk_combo_box_new_text ();
2413 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2415 g_signal_connect (impl->filter_combo, "changed",
2416 G_CALLBACK (filter_combo_changed), impl);
2418 gtk_widget_set_tooltip_text (impl->filter_combo,
2419 _("Select which types of files are shown"));
2421 return impl->filter_combo;
2425 button_new (GtkFileChooserDefault *impl,
2427 const char *stock_id,
2435 button = gtk_button_new_with_mnemonic (text);
2436 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2437 gtk_button_set_image (GTK_BUTTON (button), image);
2439 gtk_widget_set_sensitive (button, sensitive);
2440 g_signal_connect (button, "clicked", callback, impl);
2443 gtk_widget_show (button);
2448 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2450 shortcut_find_position (GtkFileChooserDefault *impl,
2455 int current_folder_separator_idx;
2457 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2460 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2463 /* FIXME: is this still needed? */
2464 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2468 for (i = 0; i < current_folder_separator_idx; i++)
2471 ShortcutType shortcut_type;
2473 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2474 SHORTCUTS_COL_DATA, &col_data,
2475 SHORTCUTS_COL_TYPE, &shortcut_type,
2480 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2482 GtkFileSystemVolume *volume;
2487 base_file = _gtk_file_system_volume_get_root (volume);
2489 exists = base_file && g_file_equal (file, base_file);
2492 g_object_unref (base_file);
2497 else if (shortcut_type == SHORTCUT_TYPE_FILE)
2501 model_file = col_data;
2503 if (model_file && g_file_equal (model_file, file))
2508 if (i < current_folder_separator_idx - 1)
2510 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2511 g_assert_not_reached ();
2518 /* Tries to add a bookmark from a path name */
2520 shortcuts_add_bookmark_from_file (GtkFileChooserDefault *impl,
2526 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2528 if (shortcut_find_position (impl, file) != -1)
2532 if (!_gtk_file_system_insert_bookmark (impl->file_system, file, pos, &error))
2534 error_adding_bookmark_dialog (impl, file, error);
2542 add_bookmark_foreach_cb (GtkTreeModel *model,
2547 GtkFileChooserDefault *impl;
2550 impl = (GtkFileChooserDefault *) data;
2552 gtk_tree_model_get (model, iter,
2553 MODEL_COL_FILE, &file,
2556 shortcuts_add_bookmark_from_file (impl, file, -1);
2558 g_object_unref (file);
2561 /* Adds a bookmark from the currently selected item in the file list */
2563 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2565 GtkTreeSelection *selection;
2567 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2569 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2570 shortcuts_add_bookmark_from_file (impl, impl->current_folder, -1);
2572 gtk_tree_selection_selected_foreach (selection,
2573 add_bookmark_foreach_cb,
2577 /* Callback used when the "Add bookmark" button is clicked */
2579 add_bookmark_button_clicked_cb (GtkButton *button,
2580 GtkFileChooserDefault *impl)
2582 bookmarks_add_selected_folder (impl);
2585 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2586 * returns FALSE if no shortcut is selected.
2589 shortcuts_get_selected (GtkFileChooserDefault *impl,
2592 GtkTreeSelection *selection;
2593 GtkTreeIter parent_iter;
2595 if (!impl->browse_shortcuts_tree_view)
2598 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2600 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2603 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2609 /* Removes the selected bookmarks */
2611 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2619 if (!shortcuts_get_selected (impl, &iter))
2622 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2623 SHORTCUTS_COL_DATA, &col_data,
2624 SHORTCUTS_COL_REMOVABLE, &removable,
2630 g_assert (col_data != NULL);
2635 if (!_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
2636 error_removing_bookmark_dialog (impl, file, error);
2639 /* Callback used when the "Remove bookmark" button is clicked */
2641 remove_bookmark_button_clicked_cb (GtkButton *button,
2642 GtkFileChooserDefault *impl)
2644 remove_selected_bookmarks (impl);
2647 struct selection_check_closure {
2648 GtkFileChooserDefault *impl;
2651 gboolean all_folders;
2654 /* Used from gtk_tree_selection_selected_foreach() */
2656 selection_check_foreach_cb (GtkTreeModel *model,
2661 struct selection_check_closure *closure;
2665 gtk_tree_model_get (model, iter,
2666 MODEL_COL_FILE, &file,
2667 MODEL_COL_IS_FOLDER, &is_folder,
2673 g_object_unref (file);
2676 closure->num_selected++;
2678 closure->all_folders = closure->all_folders && is_folder;
2679 closure->all_files = closure->all_files && !is_folder;
2682 /* Checks whether the selected items in the file list are all files or all folders */
2684 selection_check (GtkFileChooserDefault *impl,
2686 gboolean *all_files,
2687 gboolean *all_folders)
2689 struct selection_check_closure closure;
2690 GtkTreeSelection *selection;
2692 closure.impl = impl;
2693 closure.num_selected = 0;
2694 closure.all_files = TRUE;
2695 closure.all_folders = TRUE;
2697 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2698 gtk_tree_selection_selected_foreach (selection,
2699 selection_check_foreach_cb,
2702 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2705 *num_selected = closure.num_selected;
2708 *all_files = closure.all_files;
2711 *all_folders = closure.all_folders;
2714 struct get_selected_file_closure {
2715 GtkFileChooserDefault *impl;
2720 get_selected_file_foreach_cb (GtkTreeModel *model,
2725 struct get_selected_file_closure *closure = data;
2727 gtk_tree_model_get (model, iter,
2728 MODEL_COL_FILE, &closure->file,
2731 g_object_unref (closure->file);
2734 /* Returns a selected path from the file list */
2736 get_selected_file (GtkFileChooserDefault *impl)
2738 struct get_selected_file_closure closure;
2739 GtkTreeSelection *selection;
2741 closure.impl = impl;
2742 closure.file = NULL;
2744 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2745 gtk_tree_selection_selected_foreach (selection,
2746 get_selected_file_foreach_cb,
2749 return closure.file;
2753 GtkFileChooserDefault *impl;
2755 } UpdateTooltipData;
2758 update_tooltip (GtkTreeModel *model,
2763 UpdateTooltipData *udata = data;
2765 if (udata->tip == NULL)
2767 gchar *display_name;
2769 gtk_tree_model_get (model, iter,
2770 MODEL_COL_NAME, &display_name,
2773 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2775 g_free (display_name);
2780 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2781 * if there are no selected items *and* the current folder is not in the
2782 * bookmarks list. De-sensitize the button otherwise.
2785 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2788 gboolean all_folders;
2792 selection_check (impl, &num_selected, NULL, &all_folders);
2794 if (num_selected == 0)
2795 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2796 else if (num_selected == 1)
2800 file = get_selected_file (impl);
2801 active = file && all_folders && (shortcut_find_position (impl, file) == -1);
2804 active = all_folders;
2806 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2808 if (impl->browse_files_popup_menu_add_shortcut_item)
2809 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2810 (num_selected == 0) ? FALSE : active);
2814 if (num_selected == 0)
2815 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2816 else if (num_selected > 1)
2817 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2820 GtkTreeSelection *selection;
2821 UpdateTooltipData data;
2823 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2826 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2830 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button, tip);
2835 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2836 * bookmark row is selected in the shortcuts tree.
2839 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2842 gboolean removable = FALSE;
2846 if (shortcuts_get_selected (impl, &iter))
2848 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2849 SHORTCUTS_COL_REMOVABLE, &removable,
2850 SHORTCUTS_COL_NAME, &name,
2852 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2855 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2857 tip = g_strdup_printf (_("Bookmark '%s' cannot be removed"), name);
2859 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button, tip);
2863 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
2864 _("Remove the selected bookmark"));
2869 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2872 gboolean removable = FALSE;
2874 if (impl->browse_shortcuts_popup_menu == NULL)
2877 if (shortcuts_get_selected (impl, &iter))
2878 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2879 SHORTCUTS_COL_REMOVABLE, &removable,
2882 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2883 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2886 /* GtkWidget::drag-begin handler for the shortcuts list. */
2888 shortcuts_drag_begin_cb (GtkWidget *widget,
2889 GdkDragContext *context,
2890 GtkFileChooserDefault *impl)
2893 impl->shortcuts_drag_context = g_object_ref (context);
2898 /* Removes the idle handler for outside drags */
2900 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2902 if (!impl->shortcuts_drag_outside_idle)
2905 g_source_destroy (impl->shortcuts_drag_outside_idle);
2906 impl->shortcuts_drag_outside_idle = NULL;
2910 /* GtkWidget::drag-end handler for the shortcuts list. */
2912 shortcuts_drag_end_cb (GtkWidget *widget,
2913 GdkDragContext *context,
2914 GtkFileChooserDefault *impl)
2917 g_object_unref (impl->shortcuts_drag_context);
2919 shortcuts_cancel_drag_outside_idle (impl);
2921 if (!impl->shortcuts_drag_outside)
2924 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2926 impl->shortcuts_drag_outside = FALSE;
2930 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2932 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2933 GdkDragContext *context,
2934 GtkFileChooserDefault *impl)
2936 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2940 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2944 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2947 GtkTreeView *tree_view;
2950 GdkPixmap *row_pixmap;
2955 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2957 /* Find the selected path and get its drag pixmap */
2959 if (!shortcuts_get_selected (impl, &iter))
2960 g_assert_not_reached ();
2962 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2964 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2965 gtk_tree_path_free (path);
2974 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2980 GdkPixmap *composite;
2981 int row_pixmap_width, row_pixmap_height;
2982 int pixbuf_width, pixbuf_height;
2983 int composite_width, composite_height;
2984 int pixbuf_x, pixbuf_y;
2985 GdkGC *gc, *mask_gc;
2987 GdkBitmap *pixbuf_mask;
2989 /* Create pixmap and mask for composite image */
2991 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2992 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2993 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2995 composite_width = MAX (row_pixmap_width, pixbuf_width);
2996 composite_height = MAX (row_pixmap_height, pixbuf_height);
2998 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3000 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3003 pixbuf_x = composite_width - pixbuf_width;
3005 pixbuf_y = (composite_height - pixbuf_height) / 2;
3007 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3008 gc = gdk_gc_new (composite);
3010 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3011 mask_gc = gdk_gc_new (mask);
3013 gdk_gc_set_foreground (mask_gc, &color);
3014 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3017 color.green = 0xffff;
3018 color.blue = 0xffff;
3019 gdk_gc_set_rgb_fg_color (gc, &color);
3020 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3022 /* Composite the row pixmap and the pixbuf */
3024 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3026 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3027 NULL, &pixbuf_mask, 128);
3028 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3031 pixbuf_width, pixbuf_height);
3032 g_object_unref (pixbuf_mask);
3034 gdk_draw_drawable (composite, gc, row_pixmap,
3037 row_pixmap_width, row_pixmap_height);
3039 gdk_gc_set_foreground (mask_gc, &color);
3040 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3042 gdk_draw_pixbuf (composite, gc, pixbuf,
3045 pixbuf_width, pixbuf_height,
3049 g_object_unref (pixbuf);
3050 g_object_unref (row_pixmap);
3052 row_pixmap = composite;
3056 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3058 gtk_tree_view_get_path_at_pos (tree_view,
3059 tree_view->priv->press_start_x,
3060 tree_view->priv->press_start_y,
3066 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3067 gdk_drawable_get_colormap (row_pixmap),
3070 tree_view->priv->press_start_x + 1,
3071 row_pixmap_y + cell_y + 1);
3073 g_object_unref (row_pixmap);
3075 g_object_unref (mask);
3078 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3079 * handler so that we can tell apart the drag_leave event that comes right
3080 * before a drag_drop, from a normal drag_leave. We don't want to set the
3081 * cursor nor the flag in the latter case.
3084 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3086 GDK_THREADS_ENTER ();
3088 shortcuts_drag_set_delete_cursor (impl, TRUE);
3089 impl->shortcuts_drag_outside = TRUE;
3091 shortcuts_cancel_drag_outside_idle (impl);
3093 GDK_THREADS_LEAVE ();
3099 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3103 shortcuts_drag_leave_cb (GtkWidget *widget,
3104 GdkDragContext *context,
3106 GtkFileChooserDefault *impl)
3109 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3111 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3112 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3113 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3115 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3119 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3121 GTK_TREE_VIEW_DROP_BEFORE);
3123 g_signal_stop_emission_by_name (widget, "drag-leave");
3126 /* Computes the appropriate row and position for dropping */
3128 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3132 GtkTreeViewDropPosition *pos)
3134 GtkTreeView *tree_view;
3135 GtkTreeViewColumn *column;
3139 int bookmarks_index;
3141 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3143 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3145 if (!gtk_tree_view_get_path_at_pos (tree_view,
3147 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3153 row = bookmarks_index + impl->num_bookmarks - 1;
3154 *path = gtk_tree_path_new_from_indices (row, -1);
3155 *pos = GTK_TREE_VIEW_DROP_AFTER;
3159 row = *gtk_tree_path_get_indices (*path);
3160 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3161 gtk_tree_path_free (*path);
3163 if (row < bookmarks_index)
3165 row = bookmarks_index;
3166 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3168 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3170 row = bookmarks_index + impl->num_bookmarks - 1;
3171 *pos = GTK_TREE_VIEW_DROP_AFTER;
3175 if (cell_y < cell.height / 2)
3176 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3178 *pos = GTK_TREE_VIEW_DROP_AFTER;
3181 *path = gtk_tree_path_new_from_indices (row, -1);
3184 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3185 * implement the destination side of DnD by hand, due to limitations in
3186 * GtkTreeView's DnD API.
3189 shortcuts_drag_motion_cb (GtkWidget *widget,
3190 GdkDragContext *context,
3194 GtkFileChooserDefault *impl)
3197 GtkTreeViewDropPosition pos;
3198 GdkDragAction action;
3201 if (gtk_drag_get_source_widget (context) == widget)
3203 shortcuts_cancel_drag_outside_idle (impl);
3205 if (impl->shortcuts_drag_outside)
3207 shortcuts_drag_set_delete_cursor (impl, FALSE);
3208 impl->shortcuts_drag_outside = FALSE;
3213 if (context->suggested_action == GDK_ACTION_COPY ||
3214 (context->actions & GDK_ACTION_COPY) != 0)
3215 action = GDK_ACTION_COPY;
3216 else if (context->suggested_action == GDK_ACTION_MOVE ||
3217 (context->actions & GDK_ACTION_MOVE) != 0)
3218 action = GDK_ACTION_MOVE;
3225 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3226 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3227 gtk_tree_path_free (path);
3231 g_signal_stop_emission_by_name (widget, "drag-motion");
3235 gdk_drag_status (context, action, time_);
3242 /* GtkWidget::drag-drop handler for the shortcuts list. */
3244 shortcuts_drag_drop_cb (GtkWidget *widget,
3245 GdkDragContext *context,
3249 GtkFileChooserDefault *impl)
3252 shortcuts_cancel_drag_outside_idle (impl);
3255 g_signal_stop_emission_by_name (widget, "drag-drop");
3259 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3261 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3262 GtkSelectionData *selection_data,
3268 uris = gtk_selection_data_get_uris (selection_data);
3272 for (i = 0; uris[i]; i++)
3278 file = g_file_new_for_uri (uri);
3280 if (shortcuts_add_bookmark_from_file (impl, file, position))
3283 g_object_unref (file);
3289 /* Reorders the selected bookmark to the specified position */
3291 shortcuts_reorder (GtkFileChooserDefault *impl,
3296 ShortcutType shortcut_type;
3299 int bookmarks_index;
3304 /* Get the selected path */
3306 if (!shortcuts_get_selected (impl, &iter))
3307 g_assert_not_reached ();
3309 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3310 old_position = *gtk_tree_path_get_indices (path);
3311 gtk_tree_path_free (path);
3313 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3314 old_position -= bookmarks_index;
3315 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3317 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3318 SHORTCUTS_COL_NAME, &name,
3319 SHORTCUTS_COL_DATA, &col_data,
3320 SHORTCUTS_COL_TYPE, &shortcut_type,
3322 g_assert (col_data != NULL);
3323 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
3326 g_object_ref (file); /* removal below will free file, so we need a new ref */
3328 /* Remove the path from the old position and insert it in the new one */
3330 if (new_position > old_position)
3333 if (old_position == new_position)
3337 if (_gtk_file_system_remove_bookmark (impl->file_system, file, &error))
3339 shortcuts_add_bookmark_from_file (impl, file, new_position);
3340 _gtk_file_system_set_bookmark_label (impl->file_system, file, name);
3343 error_adding_bookmark_dialog (impl, file, error);
3347 g_object_unref (file);
3350 /* Callback used when we get the drag data for the bookmarks list. We add the
3351 * received URIs as bookmarks if they are folders.
3354 shortcuts_drag_data_received_cb (GtkWidget *widget,
3355 GdkDragContext *context,
3358 GtkSelectionData *selection_data,
3363 GtkFileChooserDefault *impl;
3364 GtkTreePath *tree_path;
3365 GtkTreeViewDropPosition tree_pos;
3367 int bookmarks_index;
3369 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3371 /* Compute position */
3373 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3375 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3376 position = *gtk_tree_path_get_indices (tree_path);
3377 gtk_tree_path_free (tree_path);
3379 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3382 g_assert (position >= bookmarks_index);
3383 position -= bookmarks_index;
3385 if (gtk_targets_include_uri (&selection_data->target, 1))
3386 shortcuts_drop_uris (impl, selection_data, position);
3387 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3388 shortcuts_reorder (impl, position);
3390 g_signal_stop_emission_by_name (widget, "drag-data-received");
3393 /* Callback used to display a tooltip in the shortcuts tree */
3395 shortcuts_query_tooltip_cb (GtkWidget *widget,
3398 gboolean keyboard_mode,
3399 GtkTooltip *tooltip,
3400 GtkFileChooserDefault *impl)
3402 GtkTreeModel *model;
3405 if (gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (widget),
3413 ShortcutType shortcut_type;
3415 gtk_tree_model_get (model, &iter,
3416 SHORTCUTS_COL_DATA, &col_data,
3417 SHORTCUTS_COL_TYPE, &shortcut_type,
3420 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
3422 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
3426 else if (shortcut_type == SHORTCUT_TYPE_FILE)
3431 file = G_FILE (col_data);
3432 parse_name = g_file_get_parse_name (file);
3434 gtk_tooltip_set_text (tooltip, parse_name);
3436 g_free (parse_name);
3440 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
3444 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
3454 /* Callback used when the selection in the shortcuts tree changes */
3456 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3457 GtkFileChooserDefault *impl)
3460 GtkTreeIter child_iter;
3462 bookmarks_check_remove_sensitivity (impl);
3463 shortcuts_check_popup_sensitivity (impl);
3465 if (impl->changing_folder)
3468 if (gtk_tree_selection_get_selected(selection, NULL, &iter))
3470 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
3473 shortcuts_activate_iter (impl, &child_iter);
3478 shortcuts_row_separator_func (GtkTreeModel *model,
3482 ShortcutType shortcut_type;
3484 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3486 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3490 shortcuts_key_press_event_after_cb (GtkWidget *tree_view,
3492 GtkFileChooserDefault *impl)
3496 /* don't screw up focus switching with Tab */
3497 if (event->keyval == GDK_Tab
3498 || event->keyval == GDK_KP_Tab
3499 || event->keyval == GDK_ISO_Left_Tab
3500 || event->length < 1)
3503 if (impl->location_entry)
3504 entry = impl->location_entry;
3505 else if (impl->search_entry)
3506 entry = impl->search_entry;
3512 gtk_widget_grab_focus (entry);
3513 return gtk_widget_event (entry, (GdkEvent *) event);
3519 /* Callback used when the file list's popup menu is detached */
3521 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3524 GtkFileChooserDefault *impl;
3526 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3527 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3529 impl->browse_shortcuts_popup_menu = NULL;
3530 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3531 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3535 remove_shortcut_cb (GtkMenuItem *item,
3536 GtkFileChooserDefault *impl)
3538 remove_selected_bookmarks (impl);
3541 /* Rename the selected bookmark */
3543 rename_selected_bookmark (GtkFileChooserDefault *impl)
3547 GtkTreeViewColumn *column;
3548 GtkCellRenderer *cell;
3551 if (shortcuts_get_selected (impl, &iter))
3553 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3554 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3555 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
3556 cell = g_list_nth_data (renderers, 1);
3557 g_list_free (renderers);
3558 g_object_set (cell, "editable", TRUE, NULL);
3559 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3560 path, column, cell, TRUE);
3561 gtk_tree_path_free (path);
3566 rename_shortcut_cb (GtkMenuItem *item,
3567 GtkFileChooserDefault *impl)
3569 rename_selected_bookmark (impl);
3572 /* Constructs the popup menu for the file list if needed */
3574 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3578 if (impl->browse_shortcuts_popup_menu)
3581 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3582 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3583 impl->browse_shortcuts_tree_view,
3584 shortcuts_popup_menu_detach_cb);
3586 item = gtk_image_menu_item_new_with_label (_("Remove"));
3587 impl->browse_shortcuts_popup_menu_remove_item = item;
3588 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3589 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3590 g_signal_connect (item, "activate",
3591 G_CALLBACK (remove_shortcut_cb), impl);
3592 gtk_widget_show (item);
3593 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3595 item = gtk_menu_item_new_with_label (_("Rename..."));
3596 impl->browse_shortcuts_popup_menu_rename_item = item;
3597 g_signal_connect (item, "activate",
3598 G_CALLBACK (rename_shortcut_cb), impl);
3599 gtk_widget_show (item);
3600 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3604 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3606 shortcuts_build_popup_menu (impl);
3607 shortcuts_check_popup_sensitivity (impl);
3611 popup_position_func (GtkMenu *menu,
3615 gpointer user_data);
3618 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3619 GdkEventButton *event)
3621 shortcuts_update_popup_menu (impl);
3623 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3624 NULL, NULL, NULL, NULL,
3625 event->button, event->time);
3628 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3630 popup_position_func, impl->browse_shortcuts_tree_view,
3631 0, GDK_CURRENT_TIME);
3632 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3637 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3639 shortcuts_popup_menu_cb (GtkWidget *widget,
3640 GtkFileChooserDefault *impl)
3642 shortcuts_popup_menu (impl, NULL);
3646 /* Callback used when a button is pressed on the shortcuts list.
3647 * We trap button 3 to bring up a popup menu.
3650 shortcuts_button_press_event_cb (GtkWidget *widget,
3651 GdkEventButton *event,
3652 GtkFileChooserDefault *impl)
3654 static gboolean in_press = FALSE;
3660 if (event->button != 3)
3664 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3670 shortcuts_popup_menu (impl, event);
3675 shortcuts_edited (GtkCellRenderer *cell,
3678 GtkFileChooserDefault *impl)
3684 g_object_set (cell, "editable", FALSE, NULL);
3686 path = gtk_tree_path_new_from_string (path_string);
3687 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3688 g_assert_not_reached ();
3690 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3691 SHORTCUTS_COL_DATA, &shortcut,
3693 gtk_tree_path_free (path);
3695 _gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3699 shortcuts_editing_canceled (GtkCellRenderer *cell,
3700 GtkFileChooserDefault *impl)
3702 g_object_set (cell, "editable", FALSE, NULL);
3705 /* Creates the widgets for the shortcuts and bookmarks tree */
3707 shortcuts_list_create (GtkFileChooserDefault *impl)
3710 GtkTreeSelection *selection;
3711 GtkTreeViewColumn *column;
3712 GtkCellRenderer *renderer;
3714 /* Target types for dragging a row to/from the shortcuts list */
3715 const GtkTargetEntry tree_model_row_targets[] = {
3716 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
3719 /* Scrolled window */
3721 swin = gtk_scrolled_window_new (NULL, NULL);
3722 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3723 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3724 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3726 gtk_widget_show (swin);
3730 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3731 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3732 #ifdef PROFILE_FILE_CHOOSER
3733 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3736 /* Connect "after" to key-press-event on the shortcuts pane. We want this action to be possible:
3738 * 1. user brings up a SAVE dialog
3739 * 2. user clicks on a shortcut in the shortcuts pane
3740 * 3. user starts typing a filename
3742 * Normally, the user's typing would be ignored, as the shortcuts treeview doesn't
3743 * support interactive search. However, we'd rather focus the location entry
3744 * so that the user can type *there*.
3746 * To preserve keyboard navigation in the shortcuts pane, we don't focus the
3747 * filename entry if one clicks on a shortcut; rather, we focus the entry only
3748 * if the user starts typing while the focus is in the shortcuts pane.
3750 g_signal_connect_after (impl->browse_shortcuts_tree_view, "key-press-event",
3751 G_CALLBACK (shortcuts_key_press_event_after_cb), impl);
3753 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3754 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3755 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3756 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3757 /* Accessible object name for the file chooser's shortcuts pane */
3758 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3760 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3762 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3764 tree_model_row_targets,
3765 G_N_ELEMENTS (tree_model_row_targets),
3768 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3769 GTK_DEST_DEFAULT_ALL,
3770 tree_model_row_targets,
3771 G_N_ELEMENTS (tree_model_row_targets),
3772 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3773 gtk_drag_dest_add_uri_targets (impl->browse_shortcuts_tree_view);
3775 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3776 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
3777 gtk_tree_selection_set_select_function (selection,
3778 shortcuts_select_func,
3781 g_signal_connect (selection, "changed",
3782 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3784 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3785 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3787 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3788 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3789 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3790 G_CALLBACK (shortcuts_drag_end_cb), impl);
3791 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3792 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3794 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3795 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3796 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3797 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3798 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3799 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3800 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3801 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3803 /* Support tooltips */
3804 gtk_widget_set_has_tooltip (impl->browse_shortcuts_tree_view, TRUE);
3805 g_signal_connect (impl->browse_shortcuts_tree_view, "query-tooltip",
3806 G_CALLBACK (shortcuts_query_tooltip_cb), impl);
3808 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3809 gtk_widget_show (impl->browse_shortcuts_tree_view);
3813 column = gtk_tree_view_column_new ();
3814 /* Column header for the file chooser's shortcuts pane */
3815 gtk_tree_view_column_set_title (column, _("_Places"));
3817 renderer = gtk_cell_renderer_pixbuf_new ();
3818 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3819 gtk_tree_view_column_set_attributes (column, renderer,
3820 "pixbuf", SHORTCUTS_COL_PIXBUF,
3821 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3824 renderer = gtk_cell_renderer_text_new ();
3825 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
3826 g_signal_connect (renderer, "edited",
3827 G_CALLBACK (shortcuts_edited), impl);
3828 g_signal_connect (renderer, "editing-canceled",
3829 G_CALLBACK (shortcuts_editing_canceled), impl);
3830 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3831 gtk_tree_view_column_set_attributes (column, renderer,
3832 "text", SHORTCUTS_COL_NAME,
3835 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3836 shortcuts_row_separator_func,
3839 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3844 /* Creates the widgets for the shortcuts/bookmarks pane */
3846 shortcuts_pane_create (GtkFileChooserDefault *impl,
3847 GtkSizeGroup *size_group)
3853 vbox = gtk_vbox_new (FALSE, 6);
3854 gtk_widget_show (vbox);
3856 /* Shortcuts tree */
3858 widget = shortcuts_list_create (impl);
3859 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3861 /* Box for buttons */
3863 hbox = gtk_hbox_new (TRUE, 6);
3864 gtk_size_group_add_widget (size_group, hbox);
3865 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3866 gtk_widget_show (hbox);
3868 /* Add bookmark button */
3870 impl->browse_shortcuts_add_button = button_new (impl,
3875 G_CALLBACK (add_bookmark_button_clicked_cb));
3876 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3877 gtk_widget_set_tooltip_text (impl->browse_shortcuts_add_button,
3878 _("Add the selected folder to the Bookmarks"));
3880 /* Remove bookmark button */
3882 impl->browse_shortcuts_remove_button = button_new (impl,
3887 G_CALLBACK (remove_bookmark_button_clicked_cb));
3888 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3889 gtk_widget_set_tooltip_text (impl->browse_shortcuts_remove_button,
3890 _("Remove the selected bookmark"));
3896 key_is_left_or_right (GdkEventKey *event)
3900 modifiers = gtk_accelerator_get_default_mod_mask ();
3902 return ((event->keyval == GDK_Right
3903 || event->keyval == GDK_KP_Right
3904 || event->keyval == GDK_Left
3905 || event->keyval == GDK_KP_Left)
3906 && (event->state & modifiers) == 0);
3909 /* Handles key press events on the file list, so that we can trap Enter to
3910 * activate the default button on our own. Also, checks to see if '/' has been
3911 * pressed. See comment by tree_view_keybinding_cb() for more details.
3914 browse_files_key_press_event_cb (GtkWidget *widget,
3918 GtkFileChooserDefault *impl;
3921 impl = (GtkFileChooserDefault *) data;
3923 modifiers = gtk_accelerator_get_default_mod_mask ();
3925 if ((event->keyval == GDK_slash
3926 || event->keyval == GDK_KP_Divide
3928 || event->keyval == GDK_asciitilde
3930 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3932 location_popup_handler (impl, event->string);
3936 if (key_is_left_or_right (event))
3938 gtk_widget_grab_focus (impl->browse_shortcuts_tree_view);
3942 if ((event->keyval == GDK_Return
3943 || event->keyval == GDK_ISO_Enter
3944 || event->keyval == GDK_KP_Enter
3945 || event->keyval == GDK_space
3946 || event->keyval == GDK_KP_Space)
3947 && ((event->state & modifiers) == 0)
3948 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3949 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3953 window = get_toplevel (widget);
3955 && widget != window->default_widget
3956 && !(widget == window->focus_widget &&
3957 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3959 gtk_window_activate_default (window);
3967 /* Callback used when the file list's popup menu is detached */
3969 popup_menu_detach_cb (GtkWidget *attach_widget,
3972 GtkFileChooserDefault *impl;
3974 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3975 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3977 impl->browse_files_popup_menu = NULL;
3978 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3979 impl->browse_files_popup_menu_hidden_files_item = NULL;
3982 /* Callback used when the "Add to Bookmarks" menu item is activated */
3984 add_to_shortcuts_cb (GtkMenuItem *item,
3985 GtkFileChooserDefault *impl)
3987 bookmarks_add_selected_folder (impl);
3990 /* Callback used when the "Show Hidden Files" menu item is toggled */
3992 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3993 GtkFileChooserDefault *impl)
3996 "show-hidden", gtk_check_menu_item_get_active (item),
4000 /* Callback used when the "Show Size Column" menu item is toggled */
4002 show_size_column_toggled_cb (GtkCheckMenuItem *item,
4003 GtkFileChooserDefault *impl)
4005 impl->show_size_column = gtk_check_menu_item_get_active (item);
4007 gtk_tree_view_column_set_visible (impl->list_size_column,
4008 impl->show_size_column);
4011 /* Shows an error dialog about not being able to select a dragged file */
4013 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
4018 _("Could not select file"),
4023 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
4028 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
4030 for (i = 1; uris[i]; i++)
4033 GError *error = NULL;
4036 file = g_file_new_for_uri (uri);
4038 gtk_file_chooser_default_select_file (chooser, file, &error);
4040 error_selecting_dragged_file_dialog (impl, file, error);
4042 g_object_unref (file);
4046 struct FileListDragData
4048 GtkFileChooserDefault *impl;
4054 file_list_drag_data_received_get_info_cb (GCancellable *cancellable,
4056 const GError *error,
4059 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
4060 struct FileListDragData *data = user_data;
4061 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4063 if (cancellable != data->impl->file_list_drag_data_received_cancellable)
4066 data->impl->file_list_drag_data_received_cancellable = NULL;
4068 if (cancelled || error)
4071 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4072 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4073 data->uris[1] == 0 && !error && _gtk_file_info_consider_as_directory (info))
4074 change_folder_and_display_error (data->impl, data->file, FALSE);
4077 GError *error = NULL;
4079 gtk_file_chooser_default_unselect_all (chooser);
4080 gtk_file_chooser_default_select_file (chooser, data->file, &error);
4082 error_selecting_dragged_file_dialog (data->impl, data->file, error);
4084 browse_files_center_selected_row (data->impl);
4087 if (data->impl->select_multiple)
4088 file_list_drag_data_select_uris (data->impl, data->uris);
4091 g_object_unref (data->impl);
4092 g_strfreev (data->uris);
4093 g_object_unref (data->file);
4096 g_object_unref (cancellable);
4100 file_list_drag_data_received_cb (GtkWidget *widget,
4101 GdkDragContext *context,
4104 GtkSelectionData *selection_data,
4109 GtkFileChooserDefault *impl;
4110 GtkFileChooser *chooser;
4115 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4116 chooser = GTK_FILE_CHOOSER (data);
4118 /* Allow only drags from other widgets; see bug #533891. */
4119 if (gtk_drag_get_source_widget (context) == widget)
4121 g_signal_stop_emission_by_name (widget, "drag-data-received");
4125 /* Parse the text/uri-list string, navigate to the first one */
4126 uris = gtk_selection_data_get_uris (selection_data);
4127 if (uris && uris[0])
4129 struct FileListDragData *data;
4132 file = g_file_new_for_uri (uri);
4134 data = g_new0 (struct FileListDragData, 1);
4135 data->impl = g_object_ref (impl);
4139 if (impl->file_list_drag_data_received_cancellable)
4140 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
4142 impl->file_list_drag_data_received_cancellable =
4143 _gtk_file_system_get_info (impl->file_system, file,
4145 file_list_drag_data_received_get_info_cb,
4149 g_signal_stop_emission_by_name (widget, "drag-data-received");
4152 /* Don't do anything with the drag_drop signal */
4154 file_list_drag_drop_cb (GtkWidget *widget,
4155 GdkDragContext *context,
4159 GtkFileChooserDefault *impl)
4161 g_signal_stop_emission_by_name (widget, "drag-drop");
4165 /* Disable the normal tree drag motion handler, it makes it look like you're
4166 dropping the dragged item onto a tree item */
4168 file_list_drag_motion_cb (GtkWidget *widget,
4169 GdkDragContext *context,
4173 GtkFileChooserDefault *impl)
4175 g_signal_stop_emission_by_name (widget, "drag-motion");
4179 /* Constructs the popup menu for the file list if needed */
4181 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4185 if (impl->browse_files_popup_menu)
4188 impl->browse_files_popup_menu = gtk_menu_new ();
4189 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4190 impl->browse_files_tree_view,
4191 popup_menu_detach_cb);
4193 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4194 impl->browse_files_popup_menu_add_shortcut_item = item;
4195 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4196 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4197 g_signal_connect (item, "activate",
4198 G_CALLBACK (add_to_shortcuts_cb), impl);
4199 gtk_widget_show (item);
4200 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4202 item = gtk_separator_menu_item_new ();
4203 gtk_widget_show (item);
4204 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4206 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4207 impl->browse_files_popup_menu_hidden_files_item = item;
4208 g_signal_connect (item, "toggled",
4209 G_CALLBACK (show_hidden_toggled_cb), impl);
4210 gtk_widget_show (item);
4211 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4213 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Size Column"));
4214 impl->browse_files_popup_menu_size_column_item = item;
4215 g_signal_connect (item, "toggled",
4216 G_CALLBACK (show_size_column_toggled_cb), impl);
4217 gtk_widget_show (item);
4218 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4220 bookmarks_check_add_sensitivity (impl);
4223 /* Updates the popup menu for the file list, creating it if necessary */
4225 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4227 file_list_build_popup_menu (impl);
4229 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4231 /* The sensitivity of the Add to Bookmarks item is set in
4232 * bookmarks_check_add_sensitivity()
4235 /* 'Show Hidden Files' */
4236 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4237 G_CALLBACK (show_hidden_toggled_cb), impl);
4238 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4240 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4241 G_CALLBACK (show_hidden_toggled_cb), impl);
4243 /* 'Show Size Column' */
4244 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_size_column_item,
4245 G_CALLBACK (show_size_column_toggled_cb), impl);
4246 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_size_column_item),
4247 impl->show_size_column);
4248 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_size_column_item,
4249 G_CALLBACK (show_size_column_toggled_cb), impl);
4253 popup_position_func (GtkMenu *menu,
4259 GtkWidget *widget = GTK_WIDGET (user_data);
4260 GdkScreen *screen = gtk_widget_get_screen (widget);
4263 GdkRectangle monitor;
4265 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4267 gdk_window_get_origin (widget->window, x, y);
4269 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4271 *x += (widget->allocation.width - req.width) / 2;
4272 *y += (widget->allocation.height - req.height) / 2;
4274 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4275 gtk_menu_set_monitor (menu, monitor_num);
4276 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4278 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4279 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4285 file_list_popup_menu (GtkFileChooserDefault *impl,
4286 GdkEventButton *event)
4288 file_list_update_popup_menu (impl);
4290 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4291 NULL, NULL, NULL, NULL,
4292 event->button, event->time);
4295 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4297 popup_position_func, impl->browse_files_tree_view,
4298 0, GDK_CURRENT_TIME);
4299 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4305 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4307 list_popup_menu_cb (GtkWidget *widget,
4308 GtkFileChooserDefault *impl)
4310 file_list_popup_menu (impl, NULL);
4314 /* Callback used when a button is pressed on the file list. We trap button 3 to
4315 * bring up a popup menu.
4318 list_button_press_event_cb (GtkWidget *widget,
4319 GdkEventButton *event,
4320 GtkFileChooserDefault *impl)
4322 static gboolean in_press = FALSE;
4328 if (event->button != 3)
4332 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4335 file_list_popup_menu (impl, event);
4340 OperationMode operation_mode;
4341 gint general_column;
4345 /* Sets the sort column IDs for the file list based on the operation mode */
4347 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4349 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, MODEL_COL_NAME);
4350 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, MODEL_COL_MTIME);
4351 gtk_tree_view_column_set_sort_column_id (impl->list_size_column, MODEL_COL_SIZE);
4355 file_list_query_tooltip_cb (GtkWidget *widget,
4358 gboolean keyboard_tip,
4359 GtkTooltip *tooltip,
4362 GtkFileChooserDefault *impl = user_data;
4363 GtkTreeModel *model;
4369 if (impl->operation_mode == OPERATION_MODE_BROWSE)
4373 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (impl->browse_files_tree_view),
4376 &model, &path, &iter))
4379 gtk_tree_model_get (model, &iter,
4380 MODEL_COL_FILE, &file,
4385 gtk_tree_path_free (path);
4389 filename = g_file_get_path (file);
4390 gtk_tooltip_set_text (tooltip, filename);
4391 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (impl->browse_files_tree_view),
4396 g_object_unref (file);
4397 gtk_tree_path_free (path);
4402 /* Creates the widgets for the file list */
4404 create_file_list (GtkFileChooserDefault *impl)
4407 GtkTreeSelection *selection;
4408 GtkTreeViewColumn *column;
4409 GtkCellRenderer *renderer;
4411 /* Scrolled window */
4412 swin = gtk_scrolled_window_new (NULL, NULL);
4413 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4414 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4415 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4418 /* Tree/list view */
4420 impl->browse_files_tree_view = gtk_tree_view_new ();
4421 #ifdef PROFILE_FILE_CHOOSER
4422 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4424 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4425 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4427 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4428 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4430 gtk_drag_dest_set (impl->browse_files_tree_view,
4431 GTK_DEST_DEFAULT_ALL,
4433 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4434 gtk_drag_dest_add_uri_targets (impl->browse_files_tree_view);
4436 g_signal_connect (impl->browse_files_tree_view, "row-activated",
4437 G_CALLBACK (list_row_activated), impl);
4438 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
4439 G_CALLBACK (browse_files_key_press_event_cb), impl);
4440 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
4441 G_CALLBACK (list_popup_menu_cb), impl);
4442 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
4443 G_CALLBACK (list_button_press_event_cb), impl);
4445 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
4446 G_CALLBACK (file_list_drag_data_received_cb), impl);
4447 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
4448 G_CALLBACK (file_list_drag_drop_cb), impl);
4449 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
4450 G_CALLBACK (file_list_drag_motion_cb), impl);
4452 g_object_set (impl->browse_files_tree_view, "has-tooltip", TRUE, NULL);
4453 g_signal_connect (impl->browse_files_tree_view, "query-tooltip",
4454 G_CALLBACK (file_list_query_tooltip_cb), impl);
4456 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4457 gtk_tree_selection_set_select_function (selection,
4460 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4463 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4464 gtk_drag_source_add_uri_targets (impl->browse_files_tree_view);
4466 g_signal_connect (selection, "changed",
4467 G_CALLBACK (list_selection_changed), impl);
4469 /* Filename column */
4471 impl->list_name_column = gtk_tree_view_column_new ();
4472 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4473 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4474 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4476 renderer = gtk_cell_renderer_pixbuf_new ();
4477 /* We set a fixed size so that we get an emoty row even if no icons are loaded yet */
4478 gtk_cell_renderer_set_fixed_size (renderer,
4479 renderer->xpad * 2 + impl->icon_size,
4480 renderer->ypad * 2 + impl->icon_size);
4481 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4483 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4484 g_object_set (impl->list_name_renderer,
4485 "ellipsize", PANGO_ELLIPSIZE_END,
4487 g_signal_connect (impl->list_name_renderer, "edited",
4488 G_CALLBACK (renderer_edited_cb), impl);
4489 g_signal_connect (impl->list_name_renderer, "editing-canceled",
4490 G_CALLBACK (renderer_editing_canceled_cb), impl);
4491 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4493 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4497 column = gtk_tree_view_column_new ();
4498 gtk_tree_view_column_set_resizable (column, TRUE);
4499 gtk_tree_view_column_set_title (column, _("Size"));
4501 renderer = gtk_cell_renderer_text_new ();
4502 g_object_set (renderer,
4503 "alignment", PANGO_ALIGN_RIGHT,
4505 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4506 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4507 impl->list_size_column = column;
4509 /* Modification time column */
4511 column = gtk_tree_view_column_new ();
4512 gtk_tree_view_column_set_resizable (column, TRUE);
4513 gtk_tree_view_column_set_title (column, _("Modified"));
4515 renderer = gtk_cell_renderer_text_new ();
4516 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4517 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4518 impl->list_mtime_column = column;
4520 file_list_set_sort_column_ids (impl);
4521 update_cell_renderer_attributes (impl);
4523 gtk_widget_show_all (swin);
4529 create_path_bar (GtkFileChooserDefault *impl)
4531 GtkWidget *path_bar;
4533 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4534 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4539 /* Creates the widgets for the files/folders pane */
4541 file_pane_create (GtkFileChooserDefault *impl,
4542 GtkSizeGroup *size_group)
4548 vbox = gtk_vbox_new (FALSE, 6);
4549 gtk_widget_show (vbox);
4551 /* Box for lists and preview */
4553 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4554 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4555 gtk_widget_show (hbox);
4559 widget = create_file_list (impl);
4560 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4564 impl->preview_box = gtk_vbox_new (FALSE, 12);
4565 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4566 /* Don't show preview box initially */
4570 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4572 widget = filter_create (impl);
4574 gtk_widget_show (widget);
4575 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4577 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4578 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4583 /* Callback used when the "Browse for more folders" expander is toggled */
4585 expander_changed_cb (GtkExpander *expander,
4587 GtkFileChooserDefault *impl)
4589 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4590 update_appearance (impl);
4593 /* Callback used when the selection changes in the save folder combo box */
4595 save_folder_combo_changed_cb (GtkComboBox *combo,
4596 GtkFileChooserDefault *impl)
4600 if (impl->changing_folder)
4603 if (gtk_combo_box_get_active_iter (combo, &iter))
4605 GtkTreeIter child_iter;
4607 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4610 shortcuts_activate_iter (impl, &child_iter);
4615 save_folder_update_tooltip (GtkComboBox *combo,
4616 GtkFileChooserDefault *impl)
4623 if (gtk_combo_box_get_active_iter (combo, &iter))
4625 GtkTreeIter child_iter;
4627 ShortcutType shortcut_type;
4629 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4632 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
4633 SHORTCUTS_COL_DATA, &col_data,
4634 SHORTCUTS_COL_TYPE, &shortcut_type,
4637 if (shortcut_type == SHORTCUT_TYPE_FILE)
4638 tooltip = g_file_get_parse_name (G_FILE (col_data));
4641 gtk_widget_set_tooltip_text (GTK_WIDGET (combo), tooltip);
4642 gtk_widget_set_has_tooltip (GTK_WIDGET (combo),
4643 gtk_widget_get_sensitive (GTK_WIDGET (combo)));
4647 /* Filter function used to filter out the Search item and its separator.
4648 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4651 shortcuts_combo_filter_func (GtkTreeModel *model,
4655 GtkFileChooserDefault *impl;
4656 GtkTreePath *tree_path;
4661 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4663 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4665 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4666 g_assert (tree_path != NULL);
4668 indices = gtk_tree_path_get_indices (tree_path);
4672 if (impl->has_search)
4674 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4675 if (idx == indices[0])
4679 if (impl->has_recent)
4681 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4682 if (idx == indices[0])
4686 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4687 if (idx == indices[0])
4692 gtk_tree_path_free (tree_path);
4697 /* Creates the combo box with the save folders */
4699 save_folder_combo_create (GtkFileChooserDefault *impl)
4702 GtkCellRenderer *cell;
4704 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4705 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4706 shortcuts_combo_filter_func,
4710 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4711 "model", impl->shortcuts_combo_filter_model,
4712 "focus-on-click", FALSE,
4714 gtk_widget_show (combo);
4716 cell = gtk_cell_renderer_pixbuf_new ();
4717 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4718 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4719 "pixbuf", SHORTCUTS_COL_PIXBUF,
4720 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4721 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4724 cell = gtk_cell_renderer_text_new ();
4725 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
4726 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4727 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4728 "text", SHORTCUTS_COL_NAME,
4729 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4732 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4733 shortcuts_row_separator_func,
4736 g_signal_connect (combo, "changed",
4737 G_CALLBACK (save_folder_combo_changed_cb), impl);
4738 g_signal_connect (combo, "changed",
4739 G_CALLBACK (save_folder_update_tooltip), impl);
4744 /* Creates the widgets specific to Save mode */
4746 save_widgets_create (GtkFileChooserDefault *impl)
4751 GtkWidget *alignment;
4753 if (impl->save_widgets != NULL)
4756 location_switch_to_path_bar (impl);
4758 vbox = gtk_vbox_new (FALSE, 12);
4760 table = gtk_table_new (2, 2, FALSE);
4761 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4762 gtk_widget_show (table);
4763 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4764 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4768 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4769 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4770 gtk_table_attach (GTK_TABLE (table), widget,
4774 gtk_widget_show (widget);
4776 /* Location entry */
4778 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4779 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4781 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->local_only);
4782 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4783 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4784 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4786 GTK_EXPAND | GTK_FILL, 0,
4788 gtk_widget_show (impl->location_entry);
4789 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4792 impl->save_folder_label = gtk_label_new (NULL);
4793 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4794 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4798 gtk_widget_show (impl->save_folder_label);
4800 impl->save_folder_combo = save_folder_combo_create (impl);
4801 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4803 GTK_EXPAND | GTK_FILL, GTK_FILL,
4805 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4808 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4809 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4811 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4812 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4813 g_signal_connect (impl->save_expander, "notify::expanded",
4814 G_CALLBACK (expander_changed_cb),
4816 gtk_widget_show_all (alignment);
4818 impl->save_widgets = vbox;
4819 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4820 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4821 gtk_widget_show (impl->save_widgets);
4824 /* Destroys the widgets specific to Save mode */
4826 save_widgets_destroy (GtkFileChooserDefault *impl)
4828 if (impl->save_widgets == NULL)
4831 gtk_widget_destroy (impl->save_widgets);
4832 impl->save_widgets = NULL;
4833 impl->location_entry = NULL;
4834 impl->save_folder_label = NULL;
4835 impl->save_folder_combo = NULL;
4836 impl->save_expander = NULL;
4839 /* Turns on the path bar widget. Can be called even if we are already in that
4843 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4845 if (impl->location_entry)
4847 gtk_widget_destroy (impl->location_entry);
4848 impl->location_entry = NULL;
4851 gtk_widget_hide (impl->location_entry_box);
4854 /* Sets the full path of the current folder as the text in the location entry. */
4856 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4858 gchar *text, *filename;
4860 if (!impl->current_folder)
4863 filename = g_file_get_path (impl->current_folder);
4867 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4871 text = g_file_get_uri (impl->current_folder);
4875 gboolean need_slash;
4878 len = strlen (text);
4879 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4885 slash_text = g_new (char, len + 2);
4886 strcpy (slash_text, text);
4887 slash_text[len] = G_DIR_SEPARATOR;
4888 slash_text[len + 1] = 0;
4894 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4901 /* Turns on the location entry. Can be called even if we are already in that
4905 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4907 /* when in search or recent files mode, we are not showing the
4908 * location_entry_box container, so there's no point in switching
4911 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4912 impl->operation_mode == OPERATION_MODE_RECENT)
4915 if (impl->location_entry)
4916 gtk_widget_destroy (impl->location_entry);
4920 gtk_widget_show (impl->location_entry_box);
4924 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4925 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4927 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4928 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4930 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4931 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4933 /* Configure the entry */
4935 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4939 gtk_widget_show (impl->location_entry);
4940 gtk_widget_grab_focus (impl->location_entry);
4943 /* Sets a new location mode. set_buttons determines whether the toggle button
4944 * for the mode will also be changed.
4947 location_mode_set (GtkFileChooserDefault *impl,
4948 LocationMode new_mode,
4949 gboolean set_button)
4951 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4952 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4954 GtkWindow *toplevel;
4955 GtkWidget *current_focus;
4956 gboolean button_active;
4957 gboolean switch_to_file_list;
4961 case LOCATION_MODE_PATH_BAR:
4962 button_active = FALSE;
4964 /* The location_entry will disappear when we switch to path bar mode. So,
4965 * we'll focus the file list in that case, to avoid having a window with
4966 * no focused widget.
4968 toplevel = get_toplevel (GTK_WIDGET (impl));
4969 switch_to_file_list = FALSE;
4972 current_focus = gtk_window_get_focus (toplevel);
4973 if (!current_focus || current_focus == impl->location_entry)
4974 switch_to_file_list = TRUE;
4977 location_switch_to_path_bar (impl);
4979 if (switch_to_file_list)
4980 gtk_widget_grab_focus (impl->browse_files_tree_view);
4984 case LOCATION_MODE_FILENAME_ENTRY:
4985 button_active = TRUE;
4986 location_switch_to_filename_entry (impl);
4990 g_assert_not_reached ();
4996 g_signal_handlers_block_by_func (impl->location_button,
4997 G_CALLBACK (location_button_toggled_cb), impl);
4999 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
5001 g_signal_handlers_unblock_by_func (impl->location_button,
5002 G_CALLBACK (location_button_toggled_cb), impl);
5006 impl->location_mode = new_mode;
5010 location_toggle_popup_handler (GtkFileChooserDefault *impl)
5012 /* when in search or recent files mode, we are not showing the
5013 * location_entry_box container, so there's no point in switching
5016 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
5017 impl->operation_mode == OPERATION_MODE_RECENT)
5020 /* If the file entry is not visible, show it.
5021 * If it is visible, turn it off only if it is focused. Otherwise, switch to the entry.
5023 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
5025 location_mode_set (impl, LOCATION_MODE_FILENAME_ENTRY, TRUE);
5027 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
5029 if (GTK_WIDGET_HAS_FOCUS (impl->location_entry))
5031 location_mode_set (impl, LOCATION_MODE_PATH_BAR, TRUE);
5035 gtk_widget_grab_focus (impl->location_entry);
5040 /* Callback used when one of the location mode buttons is toggled */
5042 location_button_toggled_cb (GtkToggleButton *toggle,
5043 GtkFileChooserDefault *impl)
5046 LocationMode new_mode;
5048 is_active = gtk_toggle_button_get_active (toggle);
5052 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
5053 new_mode = LOCATION_MODE_FILENAME_ENTRY;
5057 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
5058 new_mode = LOCATION_MODE_PATH_BAR;
5061 location_mode_set (impl, new_mode, FALSE);
5064 /* Creates a toggle button for the location entry. */
5066 location_button_create (GtkFileChooserDefault *impl)
5071 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
5072 gtk_widget_show (image);
5074 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
5078 gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->location_button);
5080 g_signal_connect (impl->location_button, "toggled",
5081 G_CALLBACK (location_button_toggled_cb), impl);
5083 str = _("Type a file name");
5085 gtk_widget_set_tooltip_text (impl->location_button, str);
5086 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5089 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5091 browse_widgets_create (GtkFileChooserDefault *impl)
5096 GtkSizeGroup *size_group;
5098 /* size group is used by the [+][-] buttons and the filter combo */
5099 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5100 vbox = gtk_vbox_new (FALSE, 12);
5102 /* Location widgets */
5103 impl->browse_path_bar_hbox = gtk_hbox_new (FALSE, 12);
5104 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_path_bar_hbox, FALSE, FALSE, 0);
5105 gtk_widget_show (impl->browse_path_bar_hbox);
5107 /* Size group that allows the path bar to be the same size between modes */
5108 impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5109 gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE);
5111 /* Location button */
5113 location_button_create (impl);
5114 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->location_button, FALSE, FALSE, 0);
5118 impl->browse_path_bar = create_path_bar (impl);
5119 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5120 gtk_widget_show_all (impl->browse_path_bar);
5121 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5124 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5125 g_signal_connect (impl->browse_new_folder_button, "clicked",
5126 G_CALLBACK (new_folder_button_clicked), impl);
5127 gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5129 /* Box for the location label and entry */
5131 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5132 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5134 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5135 gtk_widget_show (impl->location_label);
5136 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5139 hpaned = gtk_hpaned_new ();
5140 gtk_widget_show (hpaned);
5141 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5143 widget = shortcuts_pane_create (impl, size_group);
5144 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5145 widget = file_pane_create (impl, size_group);
5146 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5148 g_object_unref (size_group);
5154 gtk_file_chooser_default_constructor (GType type,
5155 guint n_construct_properties,
5156 GObjectConstructParam *construct_params)
5158 GtkFileChooserDefault *impl;
5161 profile_start ("start", NULL);
5163 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5164 n_construct_properties,
5166 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5168 g_assert (impl->file_system);
5170 gtk_widget_push_composite_child ();
5172 /* Shortcuts model */
5173 shortcuts_model_create (impl);
5175 /* The browse widgets */
5176 impl->browse_widgets = browse_widgets_create (impl);
5177 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5179 /* Alignment to hold extra widget */
5180 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5181 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5183 gtk_widget_pop_composite_child ();
5184 update_appearance (impl);
5186 profile_end ("end", NULL);
5191 /* Sets the extra_widget by packing it in the appropriate place */
5193 set_extra_widget (GtkFileChooserDefault *impl,
5194 GtkWidget *extra_widget)
5198 g_object_ref (extra_widget);
5199 /* FIXME: is this right ? */
5200 gtk_widget_show (extra_widget);
5203 if (impl->extra_widget)
5205 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5206 g_object_unref (impl->extra_widget);
5209 impl->extra_widget = extra_widget;
5210 if (impl->extra_widget)
5212 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5213 gtk_widget_show (impl->extra_align);
5216 gtk_widget_hide (impl->extra_align);
5220 set_local_only (GtkFileChooserDefault *impl,
5221 gboolean local_only)
5223 if (local_only != impl->local_only)
5225 impl->local_only = local_only;
5227 if (impl->location_entry)
5228 _gtk_file_chooser_entry_set_local_only (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), local_only);
5230 if (impl->shortcuts_model && impl->file_system)
5232 shortcuts_add_volumes (impl);
5233 shortcuts_add_bookmarks (impl);
5236 if (local_only && impl->current_folder &&
5237 !g_file_is_native (impl->current_folder))
5239 /* If we are pointing to a non-local folder, make an effort to change
5240 * back to a local folder, but it's really up to the app to not cause
5241 * such a situation, so we ignore errors.
5243 const gchar *home = g_get_home_dir ();
5249 home_file = g_file_new_for_path (home);
5251 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL);
5253 g_object_unref (home_file);
5259 volumes_bookmarks_changed_cb (GtkFileSystem *file_system,
5260 GtkFileChooserDefault *impl)
5262 shortcuts_add_volumes (impl);
5263 shortcuts_add_bookmarks (impl);
5265 bookmarks_check_add_sensitivity (impl);
5266 bookmarks_check_remove_sensitivity (impl);
5267 shortcuts_check_popup_sensitivity (impl);
5270 /* Sets the file chooser to multiple selection mode */
5272 set_select_multiple (GtkFileChooserDefault *impl,
5273 gboolean select_multiple,
5274 gboolean property_notify)
5276 GtkTreeSelection *selection;
5277 GtkSelectionMode mode;
5279 if (select_multiple == impl->select_multiple)
5282 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5284 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5285 gtk_tree_selection_set_mode (selection, mode);
5287 gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW (impl->browse_files_tree_view), select_multiple);
5289 impl->select_multiple = select_multiple;
5290 g_object_notify (G_OBJECT (impl), "select-multiple");
5292 check_preview_change (impl);
5296 set_file_system_backend (GtkFileChooserDefault *impl)
5298 profile_start ("start for backend", "default");
5300 impl->file_system = _gtk_file_system_new ();
5302 g_signal_connect (impl->file_system, "volumes-changed",
5303 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5304 g_signal_connect (impl->file_system, "bookmarks-changed",
5305 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5307 profile_end ("end", NULL);
5311 unset_file_system_backend (GtkFileChooserDefault *impl)
5313 g_signal_handlers_disconnect_by_func (impl->file_system,
5314 G_CALLBACK (volumes_bookmarks_changed_cb), impl);
5316 g_object_unref (impl->file_system);
5318 impl->file_system = NULL;
5321 /* This function is basically a do_all function.
5323 * It sets the visibility on all the widgets based on the current state, and
5324 * moves the custom_widget if needed.
5327 update_appearance (GtkFileChooserDefault *impl)
5329 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5330 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5334 gtk_widget_hide (impl->location_button);
5335 save_widgets_create (impl);
5337 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5338 text = _("Save in _folder:");
5340 text = _("Create in _folder:");
5342 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5344 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5346 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5347 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5348 gtk_widget_set_has_tooltip (impl->save_folder_combo, FALSE);
5349 gtk_widget_show (impl->browse_widgets);
5353 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5354 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5355 gtk_widget_set_has_tooltip (impl->save_folder_combo, TRUE);
5356 gtk_widget_hide (impl->browse_widgets);
5359 if (impl->select_multiple)
5361 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5362 "Re-setting to single selection mode.");
5363 set_select_multiple (impl, FALSE, TRUE);
5366 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5367 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5369 gtk_widget_show (impl->location_button);
5370 save_widgets_destroy (impl);
5371 gtk_widget_show (impl->browse_widgets);
5372 location_mode_set (impl, impl->location_mode, TRUE);
5375 if (impl->location_entry)
5376 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5378 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
5379 gtk_widget_hide (impl->browse_new_folder_button);
5381 gtk_widget_show (impl->browse_new_folder_button);
5383 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5384 * of files may change depending whether we are in a file or folder-only mode.
5386 gtk_widget_queue_draw (impl->browse_files_tree_view);
5388 emit_default_size_changed (impl);
5392 gtk_file_chooser_default_set_property (GObject *object,
5394 const GValue *value,
5398 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5402 case GTK_FILE_CHOOSER_PROP_ACTION:
5404 GtkFileChooserAction action = g_value_get_enum (value);
5406 if (action != impl->action)
5408 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5410 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5411 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5412 && impl->select_multiple)
5414 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5415 "this is not allowed in multiple selection mode. Resetting the file chooser "
5416 "to single selection mode.");
5417 set_select_multiple (impl, FALSE, TRUE);
5419 impl->action = action;
5420 update_cell_renderer_attributes (impl);
5421 update_appearance (impl);
5422 settings_load (impl);
5427 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5428 /* Ignore property */
5431 case GTK_FILE_CHOOSER_PROP_FILTER:
5432 set_current_filter (impl, g_value_get_object (value));
5435 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5436 set_local_only (impl, g_value_get_boolean (value));
5439 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5440 set_preview_widget (impl, g_value_get_object (value));
5443 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5444 impl->preview_widget_active = g_value_get_boolean (value);
5445 update_preview_widget_visibility (impl);
5448 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5449 impl->use_preview_label = g_value_get_boolean (value);
5450 update_preview_widget_visibility (impl);
5453 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5454 set_extra_widget (impl, g_value_get_object (value));
5457 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5459 gboolean select_multiple = g_value_get_boolean (value);
5460 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5461 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5464 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5465 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5466 "leaving the file chooser in single selection mode.");
5470 set_select_multiple (impl, select_multiple, FALSE);
5474 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5476 gboolean show_hidden = g_value_get_boolean (value);
5477 if (show_hidden != impl->show_hidden)
5479 impl->show_hidden = show_hidden;
5481 if (impl->browse_files_model)
5482 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5487 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5489 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5490 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5494 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5496 gboolean create_folders = g_value_get_boolean (value);
5497 impl->create_folders = create_folders;
5498 update_appearance (impl);
5503 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5509 gtk_file_chooser_default_get_property (GObject *object,
5514 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5518 case GTK_FILE_CHOOSER_PROP_ACTION:
5519 g_value_set_enum (value, impl->action);
5522 case GTK_FILE_CHOOSER_PROP_FILTER:
5523 g_value_set_object (value, impl->current_filter);
5526 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5527 g_value_set_boolean (value, impl->local_only);
5530 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5531 g_value_set_object (value, impl->preview_widget);
5534 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5535 g_value_set_boolean (value, impl->preview_widget_active);
5538 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5539 g_value_set_boolean (value, impl->use_preview_label);
5542 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5543 g_value_set_object (value, impl->extra_widget);
5546 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5547 g_value_set_boolean (value, impl->select_multiple);
5550 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5551 g_value_set_boolean (value, impl->show_hidden);
5554 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5555 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5558 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
5559 g_value_set_boolean (value, impl->create_folders);
5563 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5568 /* Removes the settings signal handler. It's safe to call multiple times */
5570 remove_settings_signal (GtkFileChooserDefault *impl,
5573 if (impl->settings_signal_id)
5575 GtkSettings *settings;
5577 settings = gtk_settings_get_for_screen (screen);
5578 g_signal_handler_disconnect (settings,
5579 impl->settings_signal_id);
5580 impl->settings_signal_id = 0;
5585 gtk_file_chooser_default_dispose (GObject *object)
5588 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5590 if (impl->extra_widget)
5592 g_object_unref (impl->extra_widget);
5593 impl->extra_widget = NULL;
5596 pending_select_files_free (impl);
5598 /* cancel all pending operations */
5599 if (impl->pending_cancellables)
5601 for (l = impl->pending_cancellables; l; l = l->next)
5603 GCancellable *cancellable = G_CANCELLABLE (l->data);
5604 g_cancellable_cancel (cancellable);
5606 g_slist_free (impl->pending_cancellables);
5607 impl->pending_cancellables = NULL;
5610 if (impl->reload_icon_cancellables)
5612 for (l = impl->reload_icon_cancellables; l; l = l->next)
5614 GCancellable *cancellable = G_CANCELLABLE (l->data);
5615 g_cancellable_cancel (cancellable);
5617 g_slist_free (impl->reload_icon_cancellables);
5618 impl->reload_icon_cancellables = NULL;
5621 if (impl->loading_shortcuts)
5623 for (l = impl->loading_shortcuts; l; l = l->next)
5625 GCancellable *cancellable = G_CANCELLABLE (l->data);
5626 g_cancellable_cancel (cancellable);
5628 g_slist_free (impl->loading_shortcuts);
5629 impl->loading_shortcuts = NULL;
5632 if (impl->file_list_drag_data_received_cancellable)
5634 g_cancellable_cancel (impl->file_list_drag_data_received_cancellable);
5635 impl->file_list_drag_data_received_cancellable = NULL;
5638 if (impl->update_current_folder_cancellable)
5640 g_cancellable_cancel (impl->update_current_folder_cancellable);
5641 impl->update_current_folder_cancellable = NULL;
5644 if (impl->show_and_select_files_cancellable)
5646 g_cancellable_cancel (impl->show_and_select_files_cancellable);
5647 impl->show_and_select_files_cancellable = NULL;
5650 if (impl->should_respond_get_info_cancellable)
5652 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
5653 impl->should_respond_get_info_cancellable = NULL;
5656 if (impl->update_from_entry_cancellable)
5658 g_cancellable_cancel (impl->update_from_entry_cancellable);
5659 impl->update_from_entry_cancellable = NULL;
5662 if (impl->shortcuts_activate_iter_cancellable)
5664 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
5665 impl->shortcuts_activate_iter_cancellable = NULL;
5668 search_stop_searching (impl, TRUE);
5669 recent_stop_loading (impl);
5671 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5673 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5676 /* We override show-all since we have internal widgets that
5677 * shouldn't be shown when you call show_all(), like the filter
5681 gtk_file_chooser_default_show_all (GtkWidget *widget)
5683 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5685 gtk_widget_show (widget);
5687 if (impl->extra_widget)
5688 gtk_widget_show_all (impl->extra_widget);
5691 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5692 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5695 toplevel_set_focus_cb (GtkWindow *window,
5697 GtkFileChooserDefault *impl)
5699 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5702 /* We monitor the focus widget on our toplevel to be able to know which widget
5703 * was last focused at the time our "should_respond" method gets called.
5706 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5707 GtkWidget *previous_toplevel)
5709 GtkFileChooserDefault *impl;
5710 GtkWidget *toplevel;
5712 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5714 if (previous_toplevel)
5716 g_assert (impl->toplevel_set_focus_id != 0);
5717 g_signal_handler_disconnect (previous_toplevel,
5718 impl->toplevel_set_focus_id);
5719 impl->toplevel_set_focus_id = 0;
5720 impl->toplevel_last_focus_widget = NULL;
5723 g_assert (impl->toplevel_set_focus_id == 0);
5725 toplevel = gtk_widget_get_toplevel (widget);
5726 if (GTK_IS_WINDOW (toplevel))
5728 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
5729 G_CALLBACK (toplevel_set_focus_cb), impl);
5730 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5734 /* Changes the icons wherever it is needed */
5736 change_icon_theme (GtkFileChooserDefault *impl)
5738 GtkSettings *settings;
5740 GtkCellRenderer *renderer;
5743 profile_start ("start", NULL);
5745 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5747 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5748 impl->icon_size = MAX (width, height);
5750 impl->icon_size = FALLBACK_ICON_SIZE;
5752 shortcuts_reload_icons (impl);
5753 /* the first cell in the first column is the icon column, and we have a fixed size there */
5754 cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (
5755 gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0)));
5756 renderer = GTK_CELL_RENDERER (cells->data);
5757 gtk_cell_renderer_set_fixed_size (renderer,
5758 renderer->xpad * 2 + impl->icon_size,
5759 renderer->ypad * 2 + impl->icon_size);
5760 g_list_free (cells);
5761 if (impl->browse_files_model)
5762 _gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_PIXBUF);
5763 gtk_widget_queue_resize (impl->browse_files_tree_view);
5765 profile_end ("end", NULL);
5768 /* Callback used when a GtkSettings value changes */
5770 settings_notify_cb (GObject *object,
5772 GtkFileChooserDefault *impl)
5776 profile_start ("start", NULL);
5778 name = g_param_spec_get_name (pspec);
5780 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5781 strcmp (name, "gtk-icon-sizes") == 0)
5782 change_icon_theme (impl);
5784 profile_end ("end", NULL);
5787 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5791 check_icon_theme (GtkFileChooserDefault *impl)
5793 GtkSettings *settings;
5795 profile_start ("start", NULL);
5797 if (impl->settings_signal_id)
5799 profile_end ("end", NULL);
5803 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5805 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5806 impl->settings_signal_id = g_signal_connect (settings, "notify",
5807 G_CALLBACK (settings_notify_cb), impl);
5809 change_icon_theme (impl);
5812 profile_end ("end", NULL);
5816 gtk_file_chooser_default_style_set (GtkWidget *widget,
5817 GtkStyle *previous_style)
5819 GtkFileChooserDefault *impl;
5821 profile_start ("start", NULL);
5823 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5825 profile_msg (" parent class style_set start", NULL);
5826 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5827 profile_msg (" parent class style_set end", NULL);
5829 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5830 change_icon_theme (impl);
5832 emit_default_size_changed (impl);
5834 profile_end ("end", NULL);
5838 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5839 GdkScreen *previous_screen)
5841 GtkFileChooserDefault *impl;
5843 profile_start ("start", NULL);
5845 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5847 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5848 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5850 remove_settings_signal (impl, previous_screen);
5851 check_icon_theme (impl);
5853 emit_default_size_changed (impl);
5855 profile_end ("end", NULL);
5859 gtk_file_chooser_default_size_allocate (GtkWidget *widget,
5860 GtkAllocation *allocation)
5862 GtkFileChooserDefault *impl;
5864 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5866 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->size_allocate (widget, allocation);
5870 get_is_file_filtered (GtkFileChooserDefault *impl,
5872 GFileInfo *file_info)
5874 GtkFileFilterInfo filter_info;
5875 GtkFileFilterFlags needed;
5878 if (!impl->current_filter)
5881 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5883 needed = gtk_file_filter_get_needed (impl->current_filter);
5885 filter_info.display_name = g_file_info_get_display_name (file_info);
5886 filter_info.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
5888 if (needed & GTK_FILE_FILTER_FILENAME)
5890 filter_info.filename = g_file_get_path (file);
5891 if (filter_info.filename)
5892 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5895 filter_info.filename = NULL;
5897 if (needed & GTK_FILE_FILTER_URI)
5899 filter_info.uri = g_file_get_uri (file);
5900 if (filter_info.uri)
5901 filter_info.contains |= GTK_FILE_FILTER_URI;
5904 filter_info.uri = NULL;
5906 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5908 g_free ((gchar *)filter_info.filename);
5909 g_free ((gchar *)filter_info.uri);
5910 g_free ((gchar *)filter_info.mime_type);
5916 set_sort_column (GtkFileChooserDefault *impl)
5918 GtkTreeSortable *sortable;
5920 sortable = GTK_TREE_SORTABLE (gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view)));
5921 /* can happen when we're still populating the model */
5922 if (sortable == NULL)
5925 gtk_tree_sortable_set_sort_column_id (sortable,
5931 settings_load (GtkFileChooserDefault *impl)
5933 GtkFileChooserSettings *settings;
5934 LocationMode location_mode;
5935 gboolean show_hidden;
5936 gboolean expand_folders;
5937 gboolean show_size_column;
5939 GtkSortType sort_order;
5941 settings = _gtk_file_chooser_settings_new ();
5943 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5944 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5945 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5946 show_size_column = _gtk_file_chooser_settings_get_show_size_column (settings);
5947 sort_column = _gtk_file_chooser_settings_get_sort_column (settings);
5948 sort_order = _gtk_file_chooser_settings_get_sort_order (settings);
5950 g_object_unref (settings);
5952 location_mode_set (impl, location_mode, TRUE);
5954 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5956 impl->expand_folders = expand_folders;
5957 if (impl->save_expander)
5958 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5960 impl->show_size_column = show_size_column;
5961 gtk_tree_view_column_set_visible (impl->list_size_column, show_size_column);
5963 impl->sort_column = sort_column;
5964 impl->sort_order = sort_order;
5965 /* We don't call set_sort_column() here as the models may not have been
5966 * created yet. The individual functions that create and set the models will
5967 * call set_sort_column() themselves.
5972 save_dialog_geometry (GtkFileChooserDefault *impl, GtkFileChooserSettings *settings)
5974 GtkWindow *toplevel;
5975 int x, y, width, height;
5977 /* We don't save the geometry in non-expanded "save" mode, so that the "little
5978 * dialog" won't make future Open dialogs too small.
5980 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5981 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
5982 || impl->expand_folders))
5985 toplevel = get_toplevel (GTK_WIDGET (impl));
5987 if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
5990 gtk_window_get_position (toplevel, &x, &y);
5991 gtk_window_get_size (toplevel, &width, &height);
5993 _gtk_file_chooser_settings_set_geometry (settings, x, y, width, height);
5997 settings_save (GtkFileChooserDefault *impl)
5999 GtkFileChooserSettings *settings;
6001 settings = _gtk_file_chooser_settings_new ();
6003 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
6004 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
6005 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
6006 _gtk_file_chooser_settings_set_show_size_column (settings, impl->show_size_column);
6007 _gtk_file_chooser_settings_set_sort_column (settings, impl->sort_column);
6008 _gtk_file_chooser_settings_set_sort_order (settings, impl->sort_order);
6010 save_dialog_geometry (impl, settings);
6013 _gtk_file_chooser_settings_save (settings, NULL);
6015 g_object_unref (settings);
6018 /* GtkWidget::realize method */
6020 gtk_file_chooser_default_realize (GtkWidget *widget)
6022 GtkFileChooserDefault *impl;
6024 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6026 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->realize (widget);
6028 emit_default_size_changed (impl);
6031 /* GtkWidget::map method */
6033 gtk_file_chooser_default_map (GtkWidget *widget)
6035 GtkFileChooserDefault *impl;
6036 char *current_working_dir;
6038 profile_start ("start", NULL);
6040 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6042 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
6044 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6046 switch (impl->reload_state)
6049 /* The user didn't explicitly give us a folder to
6050 * display, so we'll use the cwd
6052 current_working_dir = g_get_current_dir ();
6053 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
6054 current_working_dir);
6055 g_free (current_working_dir);
6058 case RELOAD_HAS_FOLDER:
6059 /* Nothing; we are already loading or loaded, so we
6060 * don't need to reload
6064 case RELOAD_WAS_UNMAPPED:
6065 /* Just reload the current folder; else continue
6068 if (impl->current_folder)
6070 pending_select_files_store_selection (impl);
6071 change_folder_and_display_error (impl, impl->current_folder, FALSE);
6076 g_assert_not_reached ();
6080 volumes_bookmarks_changed_cb (impl->file_system, impl);
6082 settings_load (impl);
6084 profile_end ("end", NULL);
6087 /* GtkWidget::unmap method */
6089 gtk_file_chooser_default_unmap (GtkWidget *widget)
6091 GtkFileChooserDefault *impl;
6093 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6095 settings_save (impl);
6097 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
6099 impl->reload_state = RELOAD_WAS_UNMAPPED;
6103 install_list_model_filter (GtkFileChooserDefault *impl)
6105 _gtk_file_system_model_set_filter (impl->browse_files_model,
6106 impl->current_filter);
6109 #define COMPARE_DIRECTORIES \
6110 GtkFileChooserDefault *impl = user_data; \
6111 GtkFileSystemModel *fs_model = GTK_FILE_SYSTEM_MODEL (model); \
6112 gboolean dir_a, dir_b; \
6114 dir_a = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_IS_FOLDER)); \
6115 dir_b = g_value_get_boolean (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_IS_FOLDER)); \
6117 if (dir_a != dir_b) \
6118 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6120 /* Sort callback for the filename column */
6122 name_sort_func (GtkTreeModel *model,
6127 COMPARE_DIRECTORIES;
6130 const char *key_a, *key_b;
6133 key_a = g_value_get_string (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_NAME_COLLATED));
6134 key_b = g_value_get_string (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_NAME_COLLATED));
6137 result = strcmp (key_a, key_b);
6149 /* Sort callback for the size column */
6151 size_sort_func (GtkTreeModel *model,
6156 COMPARE_DIRECTORIES;
6159 gint64 size_a, size_b;
6161 size_a = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_SIZE));
6162 size_b = g_value_get_int64 (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_SIZE));
6164 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6168 /* Sort callback for the mtime column */
6170 mtime_sort_func (GtkTreeModel *model,
6175 COMPARE_DIRECTORIES;
6180 ta = g_value_get_long (_gtk_file_system_model_get_value (fs_model, a, MODEL_COL_MTIME));
6181 tb = g_value_get_long (_gtk_file_system_model_get_value (fs_model, b, MODEL_COL_MTIME));
6183 return ta > tb ? -1 : (ta == tb ? 0 : 1);
6187 /* Callback used when the sort column changes. We cache the sort order for use
6188 * in name_sort_func().
6191 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6192 GtkFileChooserDefault *impl)
6194 gint sort_column_id;
6195 GtkSortType sort_type;
6197 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_type))
6199 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6200 impl->sort_column = sort_column_id;
6201 impl->sort_order = sort_type;
6206 set_busy_cursor (GtkFileChooserDefault *impl,
6209 GtkWindow *toplevel;
6210 GdkDisplay *display;
6213 toplevel = get_toplevel (GTK_WIDGET (impl));
6214 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6217 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6220 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6224 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6225 gdk_display_flush (display);
6228 gdk_cursor_unref (cursor);
6231 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6233 load_set_model (GtkFileChooserDefault *impl)
6235 profile_start ("start", NULL);
6237 g_assert (impl->browse_files_model != NULL);
6239 profile_msg (" gtk_tree_view_set_model start", NULL);
6240 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6241 GTK_TREE_MODEL (impl->browse_files_model));
6242 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6243 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6245 set_sort_column (impl);
6246 profile_msg (" gtk_tree_view_set_model end", NULL);
6247 impl->list_sort_ascending = TRUE;
6249 profile_end ("end", NULL);
6252 /* Timeout callback used when the loading timer expires */
6254 load_timeout_cb (gpointer data)
6256 GtkFileChooserDefault *impl;
6258 profile_start ("start", NULL);
6260 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6261 g_assert (impl->load_state == LOAD_PRELOAD);
6262 g_assert (impl->load_timeout_id != 0);
6263 g_assert (impl->browse_files_model != NULL);
6265 impl->load_timeout_id = 0;
6266 impl->load_state = LOAD_LOADING;
6268 load_set_model (impl);
6270 profile_end ("end", NULL);
6275 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6277 load_setup_timer (GtkFileChooserDefault *impl)
6279 g_assert (impl->load_timeout_id == 0);
6280 g_assert (impl->load_state != LOAD_PRELOAD);
6282 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6283 impl->load_state = LOAD_PRELOAD;
6286 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6288 load_remove_timer (GtkFileChooserDefault *impl)
6290 if (impl->load_timeout_id != 0)
6292 g_assert (impl->load_state == LOAD_PRELOAD);
6294 g_source_remove (impl->load_timeout_id);
6295 impl->load_timeout_id = 0;
6296 impl->load_state = LOAD_EMPTY;
6299 g_assert (impl->load_state == LOAD_EMPTY ||
6300 impl->load_state == LOAD_LOADING ||
6301 impl->load_state == LOAD_FINISHED);
6304 /* Selects the first row in the file list */
6306 browse_files_select_first_row (GtkFileChooserDefault *impl)
6309 GtkTreeIter dummy_iter;
6310 GtkTreeModel *tree_model;
6312 path = gtk_tree_path_new_from_indices (0, -1);
6313 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6318 /* If the list is empty, do nothing. */
6319 if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
6320 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6322 gtk_tree_path_free (path);
6325 struct center_selected_row_closure {
6326 GtkFileChooserDefault *impl;
6327 gboolean already_centered;
6330 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6331 * selected row in the tree view.
6334 center_selected_row_foreach_cb (GtkTreeModel *model,
6339 struct center_selected_row_closure *closure;
6342 if (closure->already_centered)
6345 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6346 closure->already_centered = TRUE;
6349 /* Centers the selected row in the tree view */
6351 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6353 struct center_selected_row_closure closure;
6354 GtkTreeSelection *selection;
6356 closure.impl = impl;
6357 closure.already_centered = FALSE;
6359 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6360 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6363 struct ShowAndSelectPathsData
6365 GtkFileChooserDefault *impl;
6370 show_and_select_files_finished_loading (GtkFolder *folder,
6373 gboolean have_hidden;
6374 gboolean have_filtered;
6376 struct ShowAndSelectPathsData *data = user_data;
6378 have_hidden = FALSE;
6379 have_filtered = FALSE;
6381 for (l = data->files; l; l = l->next)
6388 info = _gtk_folder_get_info (folder, file);
6392 have_hidden = g_file_info_get_is_hidden (info)
6393 || g_file_info_get_is_backup (info);
6396 have_filtered = (! _gtk_file_info_consider_as_directory (info)) &&
6397 get_is_file_filtered (data->impl, file, info);
6399 g_object_unref (info);
6401 if (have_hidden && have_filtered)
6402 break; /* we now have all the information we need */
6406 g_signal_handlers_disconnect_by_func (folder,
6407 show_and_select_files_finished_loading,
6411 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6414 set_current_filter (data->impl, NULL);
6416 for (l = data->files; l; l = l->next)
6423 if (!_gtk_file_system_model_get_iter_for_file (data->impl->browse_files_model,
6428 path = gtk_tree_model_get_path (GTK_TREE_MODEL (data->impl->browse_files_model), &iter);
6429 select_func (data->impl->browse_files_model, path, &iter, data->impl);
6430 gtk_tree_path_free (path);
6433 browse_files_center_selected_row (data->impl);
6435 g_object_unref (data->impl);
6436 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6437 g_slist_free (data->files);
6442 show_and_select_files_get_folder_cb (GCancellable *cancellable,
6444 const GError *error,
6447 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
6448 struct ShowAndSelectPathsData *data = user_data;
6450 if (data->impl->show_and_select_files_cancellable != cancellable)
6453 data->impl->show_and_select_files_cancellable = NULL;
6455 if (cancelled || error)
6458 g_object_unref (cancellable);
6460 if (_gtk_folder_is_finished_loading (folder))
6461 show_and_select_files_finished_loading (folder, user_data);
6463 g_signal_connect (folder, "finished-loading",
6464 G_CALLBACK (show_and_select_files_finished_loading),
6470 g_object_unref (data->impl);
6471 g_slist_foreach (data->files, (GFunc) g_object_unref, NULL);
6472 g_slist_free (data->files);
6475 g_object_unref (cancellable);
6479 show_and_select_files (GtkFileChooserDefault *impl,
6484 struct ShowAndSelectPathsData *info;
6486 profile_start ("start", NULL);
6490 profile_end ("end", NULL);
6494 info = g_new (struct ShowAndSelectPathsData, 1);
6495 info->impl = g_object_ref (impl);
6496 info->files = g_slist_copy (files);
6497 g_slist_foreach (info->files, (GFunc) g_object_ref, NULL);
6499 if (impl->show_and_select_files_cancellable)
6500 g_cancellable_cancel (impl->show_and_select_files_cancellable);
6502 impl->show_and_select_files_cancellable =
6503 _gtk_file_system_get_folder (impl->file_system, parent_file,
6504 "standard::is-hidden,standard::is-backup,standard::type,standard::name,standard::content-type",
6505 show_and_select_files_get_folder_cb, info);
6507 profile_end ("end", NULL);
6511 /* Processes the pending operation when a folder is finished loading */
6513 pending_select_files_process (GtkFileChooserDefault *impl)
6515 g_assert (impl->load_state == LOAD_FINISHED);
6516 g_assert (impl->browse_files_model != NULL);
6518 if (impl->pending_select_files)
6521 show_and_select_files (impl, impl->current_folder, impl->pending_select_files, NULL);
6522 pending_select_files_free (impl);
6523 browse_files_center_selected_row (impl);
6527 /* We only select the first row if the chooser is actually mapped ---
6528 * selecting the first row is to help the user when he is interacting with
6529 * the chooser, but sometimes a chooser works not on behalf of the user,
6530 * but rather on behalf of something else like GtkFileChooserButton. In
6531 * that case, the chooser's selection should be what the caller expects,
6532 * as the user can't see that something else got selected. See bug #165264.
6534 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6535 browse_files_select_first_row (impl);
6538 g_assert (impl->pending_select_files == NULL);
6541 /* Callback used when the file system model finishes loading */
6543 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6545 GtkFileChooserDefault *impl)
6547 profile_start ("start", NULL);
6549 if (impl->load_state == LOAD_PRELOAD)
6551 load_remove_timer (impl);
6552 load_set_model (impl);
6554 else if (impl->load_state == LOAD_LOADING)
6560 /* We can't g_assert_not_reached(), as something other than us may have
6561 * initiated a folder reload. See #165556.
6563 profile_end ("end", NULL);
6567 g_assert (impl->load_timeout_id == 0);
6569 impl->load_state = LOAD_FINISHED;
6571 pending_select_files_process (impl);
6572 set_busy_cursor (impl, FALSE);
6573 #ifdef PROFILE_FILE_CHOOSER
6574 access ("MARK: *** FINISHED LOADING", F_OK);
6577 profile_end ("end", NULL);
6581 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6583 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6585 if (impl->browse_files_model)
6587 g_object_unref (impl->browse_files_model);
6588 impl->browse_files_model = NULL;
6591 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6595 my_g_format_time_for_display (glong secs)
6600 time_t time_mtime, time_now;
6601 const gchar *format;
6602 gchar *locale_format = NULL;
6604 char *date_str = NULL;
6608 #ifdef HAVE_LOCALTIME_R
6609 localtime_r ((time_t *) &time_mtime, &tm_mtime);
6612 struct tm *ptm = localtime ((time_t *) &timeval.tv_sec);
6616 g_warning ("ptm != NULL failed");
6618 return g_strdup (_("Unknown"));
6621 memcpy ((void *) &tm_mtime, (void *) ptm, sizeof (struct tm));
6623 #endif /* HAVE_LOCALTIME_R */
6625 g_date_set_time_t (&mtime, time_mtime);
6626 time_now = time (NULL);
6627 g_date_set_time_t (&now, time_now);
6629 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6631 /* Translators: %H means "hours" and %M means "minutes" */
6633 format = _("%H:%M");
6634 else if (days_diff == 1)
6635 format = _("Yesterday at %H:%M");
6638 if (days_diff > 1 && days_diff < 7)
6639 format = "%A"; /* Days from last week */
6641 format = "%x"; /* Any other date */
6645 /* g_locale_from_utf8() returns a string in the system
6646 * code-page, which is not always the same as that used by the C
6647 * library. For instance when running a GTK+ program with
6648 * LANG=ko on an English version of Windows, the system
6649 * code-page is 1252, but the code-page used by the C library is
6650 * 949. (It's GTK+ itself that sets the C library locale when it
6651 * notices the LANG environment variable. See gtkmain.c The
6652 * Microsoft C library doesn't look at any locale environment
6653 * variables.) We need to pass strftime() a string in the C
6654 * library's code-page. See bug #509885.
6656 locale = setlocale (LC_ALL, NULL);
6658 dot = strchr (locale, '.');
6661 codepage = g_ascii_strtoll (dot+1, NULL, 10);
6663 /* All codepages should fit in 16 bits AFAIK */
6664 if (codepage > 0 && codepage < 65536)
6666 sprintf (charset, "CP%u", (guint) codepage);
6667 locale_format = g_convert (format, -1, charset, "UTF-8", NULL, NULL, NULL);
6671 locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
6673 if (locale_format != NULL &&
6674 strftime (buf, sizeof (buf), locale_format, &tm_mtime) != 0)
6677 /* As above but in opposite direction... */
6678 if (codepage > 0 && codepage < 65536)
6679 date_str = g_convert (buf, -1, "UTF-8", charset, NULL, NULL, NULL);
6681 date_str = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
6685 if (date_str == NULL)
6686 date_str = g_strdup (_("Unknown"));
6688 g_free (locale_format);
6692 #define copy_attribute(to, from, attribute) G_STMT_START { \
6693 GFileAttributeType type; \
6696 if (g_file_info_get_attribute_data (from, attribute, &type, &value, NULL)) \
6697 g_file_info_set_attribute (to, attribute, type, value); \
6701 file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
6703 GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
6704 GFile *file = G_FILE (object);
6705 GFileInfo *queried, *info;
6708 queried = g_file_query_info_finish (file, res, NULL);
6709 if (queried == NULL)
6712 /* now we know model is valid */
6714 /* file was deleted */
6715 if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
6718 info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
6720 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
6721 copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
6722 copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
6724 _gtk_file_system_model_update_file (model, file, info, FALSE);
6726 g_object_unref (info);
6730 file_system_model_set (GtkFileSystemModel *model,
6737 GtkFileChooserDefault *impl = data;
6741 case MODEL_COL_FILE:
6742 g_value_set_object (value, file);
6744 case MODEL_COL_NAME:
6746 g_value_set_string (value, DEFAULT_NEW_FOLDER_NAME);
6748 g_value_set_string (value, g_file_info_get_display_name (info));
6750 case MODEL_COL_NAME_COLLATED:
6752 g_value_take_string (value, g_utf8_collate_key_for_filename (DEFAULT_NEW_FOLDER_NAME, -1));
6754 g_value_take_string (value, g_utf8_collate_key_for_filename (g_file_info_get_display_name (info), -1));
6756 case MODEL_COL_IS_FOLDER:
6757 g_value_set_boolean (value, info == NULL || _gtk_file_info_consider_as_directory (info));
6759 case MODEL_COL_PIXBUF:
6762 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
6764 g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), impl->icon_size));
6768 GtkTreeModel *tree_model;
6769 GtkTreePath *path, *start, *end;
6772 if (impl->browse_files_tree_view == NULL ||
6773 g_file_info_has_attribute (info, "filechooser::queried"))
6776 tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
6777 if (tree_model != GTK_TREE_MODEL (model))
6780 if (!_gtk_file_system_model_get_iter_for_file (model,
6783 g_assert_not_reached ();
6784 if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
6786 path = gtk_tree_model_get_path (tree_model, &iter);
6787 if (gtk_tree_path_compare (start, path) != 1 &&
6788 gtk_tree_path_compare (path, end) != 1)
6790 g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
6791 g_file_query_info_async (file,
6792 G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
6793 G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
6794 G_FILE_ATTRIBUTE_STANDARD_ICON,
6797 _gtk_file_system_model_get_cancellable (model),
6798 file_system_model_got_thumbnail,
6801 gtk_tree_path_free (path);
6802 gtk_tree_path_free (start);
6803 gtk_tree_path_free (end);
6808 g_value_set_object (value, NULL);
6810 case MODEL_COL_SIZE:
6811 g_value_set_int64 (value, info ? g_file_info_get_size (info) : 0);
6813 case MODEL_COL_SIZE_TEXT:
6814 if (info == NULL || _gtk_file_info_consider_as_directory (info))
6815 g_value_set_string (value, NULL);
6817 g_value_take_string (value, g_format_size_for_display (g_file_info_get_size (info)));
6819 case MODEL_COL_MTIME:
6820 case MODEL_COL_MTIME_TEXT:
6825 g_file_info_get_modification_time (info, &tv);
6826 if (column == MODEL_COL_MTIME)
6827 g_value_set_long (value, tv.tv_sec);
6828 else if (tv.tv_sec == 0)
6829 g_value_set_static_string (value, _("Unknown"));
6831 g_value_take_string (value, my_g_format_time_for_display (tv.tv_sec));
6834 case MODEL_COL_ELLIPSIZE:
6835 g_value_set_enum (value, info ? PANGO_ELLIPSIZE_END : PANGO_ELLIPSIZE_NONE);
6838 g_assert_not_reached ();
6845 /* Gets rid of the old list model and creates a new one for the current folder */
6847 set_list_model (GtkFileChooserDefault *impl,
6850 g_assert (impl->current_folder != NULL);
6852 profile_start ("start", NULL);
6854 stop_loading_and_clear_list_model (impl);
6856 set_busy_cursor (impl, TRUE);
6857 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6859 impl->browse_files_model =
6860 _gtk_file_system_model_new_for_directory (impl->current_folder,
6862 file_system_model_set,
6864 MODEL_COL_NUM_COLUMNS,
6874 PANGO_TYPE_ELLIPSIZE_MODE);
6876 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6878 profile_msg (" set sort function", NULL);
6879 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_NAME, name_sort_func, impl, NULL);
6880 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_SIZE, size_sort_func, impl, NULL);
6881 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), MODEL_COL_MTIME, mtime_sort_func, impl, NULL);
6882 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->browse_files_model), NULL, NULL, NULL);
6883 set_sort_column (impl);
6884 impl->list_sort_ascending = TRUE;
6885 g_signal_connect (impl->browse_files_model, "sort-column-changed",
6886 G_CALLBACK (list_sort_column_changed_cb), impl);
6888 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6890 g_signal_connect (impl->browse_files_model, "finished-loading",
6891 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6893 install_list_model_filter (impl);
6895 profile_end ("end", NULL);
6900 struct update_chooser_entry_selected_foreach_closure {
6902 GtkTreeIter first_selected_iter;
6906 compare_utf8_filenames (const gchar *a,
6909 gchar *a_folded, *b_folded;
6912 a_folded = g_utf8_strdown (a, -1);
6913 b_folded = g_utf8_strdown (b, -1);
6915 retval = strcmp (a_folded, b_folded);
6924 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6929 struct update_chooser_entry_selected_foreach_closure *closure;
6932 closure->num_selected++;
6934 if (closure->num_selected == 1)
6935 closure->first_selected_iter = *iter;
6939 update_chooser_entry (GtkFileChooserDefault *impl)
6941 GtkTreeSelection *selection;
6942 struct update_chooser_entry_selected_foreach_closure closure;
6943 const char *file_part;
6945 /* no need to update the file chooser's entry if there's no entry */
6946 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6947 impl->operation_mode == OPERATION_MODE_RECENT ||
6948 !impl->location_entry)
6951 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6952 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6953 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6954 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6955 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6958 g_assert (impl->location_entry != NULL);
6960 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6961 closure.num_selected = 0;
6962 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6966 if (closure.num_selected == 0)
6968 goto maybe_clear_entry;
6970 else if (closure.num_selected == 1)
6972 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6975 gboolean change_entry;
6977 info = _gtk_file_system_model_get_info (impl->browse_files_model, &closure.first_selected_iter);
6979 /* If the cursor moved to the row of the newly created folder,
6980 * retrieving info will return NULL.
6985 g_free (impl->browse_files_last_selected_name);
6986 impl->browse_files_last_selected_name =
6987 g_strdup (g_file_info_get_display_name (info));
6989 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6990 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6991 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6993 /* We don't want the name to change when clicking on a folder... */
6994 change_entry = (! _gtk_file_info_consider_as_directory (info));
6997 change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
7001 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
7003 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7004 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (impl->location_entry));
7012 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7013 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
7015 /* Multiple selection, so just clear the entry. */
7017 g_free (impl->browse_files_last_selected_name);
7018 impl->browse_files_last_selected_name = NULL;
7020 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7026 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7027 && impl->browse_files_last_selected_name)
7029 const char *entry_text;
7031 gboolean clear_entry;
7033 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
7034 len = strlen (entry_text);
7037 /* The file chooser entry may have appended a "/" to its text. So
7038 * take it out, and compare the result to the old selection.
7040 if (entry_text[len - 1] == G_DIR_SEPARATOR)
7044 tmp = g_strndup (entry_text, len - 1);
7045 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
7049 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
7052 clear_entry = FALSE;
7055 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7060 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
7064 return gtk_file_chooser_default_update_current_folder (chooser, file, FALSE, FALSE, error);
7068 struct UpdateCurrentFolderData
7070 GtkFileChooserDefault *impl;
7072 gboolean keep_trail;
7073 gboolean clear_entry;
7074 GFile *original_file;
7075 GError *original_error;
7079 update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellable,
7080 GtkFileSystemVolume *volume,
7081 const GError *error,
7084 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7085 struct UpdateCurrentFolderData *data = user_data;
7086 GtkFileChooserDefault *impl = data->impl;
7088 if (cancellable != impl->update_current_folder_cancellable)
7091 impl->update_current_folder_cancellable = NULL;
7092 set_busy_cursor (impl, FALSE);
7099 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
7100 impl->reload_state = RELOAD_EMPTY;
7104 change_folder_and_display_error (impl, data->file, data->clear_entry);
7107 g_object_unref (data->file);
7110 g_object_unref (cancellable);
7114 update_current_folder_get_info_cb (GCancellable *cancellable,
7116 const GError *error,
7119 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7120 struct UpdateCurrentFolderData *data = user_data;
7121 GtkFileChooserDefault *impl = data->impl;
7123 if (cancellable != impl->update_current_folder_cancellable)
7126 impl->update_current_folder_cancellable = NULL;
7127 impl->reload_state = RELOAD_EMPTY;
7129 set_busy_cursor (impl, FALSE);
7138 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED))
7140 GMountOperation *mount_operation;
7141 GtkWidget *toplevel;
7143 g_object_unref (cancellable);
7144 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7146 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
7148 set_busy_cursor (impl, TRUE);
7150 impl->update_current_folder_cancellable =
7151 _gtk_file_system_mount_enclosing_volume (impl->file_system, data->file,
7153 update_current_folder_mount_enclosing_volume_cb,
7159 if (!data->original_file)
7161 data->original_file = g_object_ref (data->file);
7162 data->original_error = g_error_copy (error);
7165 parent_file = g_file_get_parent (data->file);
7167 /* get parent path and try to change the folder to that */
7170 g_object_unref (data->file);
7171 data->file = parent_file;
7173 g_object_unref (cancellable);
7175 /* restart the update current folder operation */
7176 impl->reload_state = RELOAD_HAS_FOLDER;
7178 impl->update_current_folder_cancellable =
7179 _gtk_file_system_get_info (impl->file_system, data->file,
7181 update_current_folder_get_info_cb,
7184 set_busy_cursor (impl, TRUE);
7190 /* Error and bail out, ignoring "not found" errors since they're useless:
7191 * they only happen when a program defaults to a folder that has been (re)moved.
7193 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7194 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7196 g_error_free (data->original_error);
7198 g_object_unref (data->original_file);
7204 if (data->original_file)
7206 /* Error and bail out, ignoring "not found" errors since they're useless:
7207 * they only happen when a program defaults to a folder that has been (re)moved.
7209 if (!g_error_matches (data->original_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7210 error_changing_folder_dialog (impl, data->original_file, data->original_error);
7212 g_error_free (data->original_error);
7214 g_object_unref (data->original_file);
7217 if (! _gtk_file_info_consider_as_directory (info))
7220 if (!_gtk_path_bar_set_file (GTK_PATH_BAR (impl->browse_path_bar), data->file, data->keep_trail, NULL))
7223 if (impl->current_folder != data->file)
7225 if (impl->current_folder)
7226 g_object_unref (impl->current_folder);
7228 impl->current_folder = g_object_ref (data->file);
7231 impl->reload_state = RELOAD_HAS_FOLDER;
7233 /* Update the widgets that may trigger a folder change themselves. */
7235 if (!impl->changing_folder)
7237 impl->changing_folder = TRUE;
7239 shortcuts_update_current_folder (impl);
7241 impl->changing_folder = FALSE;
7244 /* Set the folder on the save entry */
7246 if (impl->location_entry)
7248 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
7249 impl->current_folder);
7251 if (data->clear_entry)
7252 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
7255 /* Create a new list model. This is slightly evil; we store the result value
7256 * but perform more actions rather than returning immediately even if it
7257 * generates an error.
7259 set_list_model (impl, NULL);
7261 /* Refresh controls */
7263 shortcuts_find_current_folder (impl);
7265 g_signal_emit_by_name (impl, "current-folder-changed", 0);
7267 check_preview_change (impl);
7268 bookmarks_check_add_sensitivity (impl);
7270 g_signal_emit_by_name (impl, "selection-changed", 0);
7273 g_object_unref (data->file);
7276 g_object_unref (cancellable);
7280 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
7282 gboolean keep_trail,
7283 gboolean clear_entry,
7286 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7287 struct UpdateCurrentFolderData *data;
7289 profile_start ("start", NULL);
7291 g_object_ref (file);
7293 switch (impl->operation_mode)
7295 case OPERATION_MODE_SEARCH:
7296 search_switch_to_browse_mode (impl);
7298 case OPERATION_MODE_RECENT:
7299 recent_switch_to_browse_mode (impl);
7301 case OPERATION_MODE_BROWSE:
7305 if (impl->local_only && !g_file_is_native (file))
7307 g_set_error_literal (error,
7308 GTK_FILE_CHOOSER_ERROR,
7309 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
7310 _("Cannot change to folder because it is not local"));
7312 g_object_unref (file);
7313 profile_end ("end - not local", NULL);
7317 if (impl->update_current_folder_cancellable)
7318 g_cancellable_cancel (impl->update_current_folder_cancellable);
7320 /* Test validity of path here. */
7321 data = g_new0 (struct UpdateCurrentFolderData, 1);
7323 data->file = g_object_ref (file);
7324 data->keep_trail = keep_trail;
7325 data->clear_entry = clear_entry;
7327 impl->reload_state = RELOAD_HAS_FOLDER;
7329 impl->update_current_folder_cancellable =
7330 _gtk_file_system_get_info (impl->file_system, file,
7332 update_current_folder_get_info_cb,
7335 set_busy_cursor (impl, TRUE);
7336 g_object_unref (file);
7338 profile_end ("end", NULL);
7343 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
7345 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7347 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7348 impl->operation_mode == OPERATION_MODE_RECENT)
7351 if (impl->reload_state == RELOAD_EMPTY)
7353 char *current_working_dir;
7356 /* We are unmapped, or we had an error while loading the last folder. We'll return
7357 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
7358 * explicitly calls set_current_folder() on us.
7360 current_working_dir = g_get_current_dir ();
7361 file = g_file_new_for_path (current_working_dir);
7362 g_free (current_working_dir);
7366 if (impl->current_folder)
7367 return g_object_ref (impl->current_folder);
7373 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
7376 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7378 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7379 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7381 pending_select_files_free (impl);
7382 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
7386 select_func (GtkFileSystemModel *model,
7391 GtkFileChooserDefault *impl = user_data;
7392 GtkTreeSelection *selection;
7394 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7396 gtk_tree_selection_select_iter (selection, iter);
7400 gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
7404 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7408 parent_file = g_file_get_parent (file);
7411 return gtk_file_chooser_set_current_folder_file (chooser, file, error);
7413 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7414 impl->operation_mode == OPERATION_MODE_RECENT ||
7415 impl->load_state == LOAD_EMPTY)
7421 g_assert (impl->current_folder != NULL);
7423 same_path = g_file_equal (parent_file, impl->current_folder);
7426 if (same_path && impl->load_state == LOAD_FINISHED)
7431 files.data = (gpointer) file;
7434 result = show_and_select_files (impl, parent_file, &files, error);
7435 g_object_unref (parent_file);
7439 pending_select_files_add (impl, file);
7445 result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error);
7446 g_object_unref (parent_file);
7450 g_object_unref (parent_file);
7455 gtk_file_chooser_default_unselect_file (GtkFileChooser *chooser,
7458 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7459 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7462 if (!impl->browse_files_model)
7465 if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
7470 gtk_tree_selection_unselect_iter (gtk_tree_view_get_selection (tree_view),
7475 maybe_select (GtkTreeModel *model,
7480 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7481 GtkTreeSelection *selection;
7484 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7486 gtk_tree_model_get (model, iter,
7487 MODEL_COL_IS_FOLDER, &is_folder,
7490 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7491 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7492 gtk_tree_selection_select_iter (selection, iter);
7494 gtk_tree_selection_unselect_iter (selection, iter);
7500 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7502 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7504 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7505 impl->operation_mode == OPERATION_MODE_RECENT)
7507 GtkTreeSelection *selection;
7509 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7510 gtk_tree_selection_select_all (selection);
7514 if (impl->select_multiple)
7515 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->browse_files_model),
7516 maybe_select, impl);
7520 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7522 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7523 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7525 gtk_tree_selection_unselect_all (selection);
7526 pending_select_files_free (impl);
7529 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7531 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7533 * is_empty_ret - whether the file entry is totally empty
7535 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7536 * the path will be "$cwd/foobar")
7539 check_save_entry (GtkFileChooserDefault *impl,
7541 gboolean *is_well_formed_ret,
7542 gboolean *is_empty_ret,
7543 gboolean *is_file_part_empty_ret,
7544 gboolean *is_folder)
7546 GtkFileChooserEntry *chooser_entry;
7547 GFile *current_folder;
7548 const char *file_part;
7552 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7553 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7554 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7555 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7556 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7558 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7560 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7563 *is_well_formed_ret = TRUE;
7564 *is_empty_ret = TRUE;
7565 *is_file_part_empty_ret = TRUE;
7571 *is_empty_ret = FALSE;
7573 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7574 if (!current_folder)
7577 *is_well_formed_ret = FALSE;
7578 *is_file_part_empty_ret = FALSE;
7584 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7586 if (!file_part || file_part[0] == '\0')
7588 *file_ret = g_object_ref (current_folder);
7589 *is_well_formed_ret = TRUE;
7590 *is_file_part_empty_ret = TRUE;
7596 *is_file_part_empty_ret = FALSE;
7599 file = g_file_get_child_for_display_name (current_folder, file_part, &error);
7603 error_building_filename_dialog (impl, error);
7605 *is_well_formed_ret = FALSE;
7612 *is_well_formed_ret = TRUE;
7613 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, file);
7616 struct get_files_closure {
7617 GtkFileChooserDefault *impl;
7619 GFile *file_from_entry;
7623 get_files_foreach (GtkTreeModel *model,
7628 struct get_files_closure *info;
7630 GtkFileSystemModel *fs_model;
7633 fs_model = info->impl->browse_files_model;
7635 file = _gtk_file_system_model_get_file (fs_model, iter);
7637 return; /* We are on the editable row */
7639 if (!info->file_from_entry || !g_file_equal (info->file_from_entry, file))
7640 info->result = g_slist_prepend (info->result, g_object_ref (file));
7644 gtk_file_chooser_default_get_files (GtkFileChooser *chooser)
7646 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7647 struct get_files_closure info;
7648 GtkWindow *toplevel;
7649 GtkWidget *current_focus;
7650 gboolean file_list_seen;
7652 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7653 return search_get_selected_files (impl);
7655 if (impl->operation_mode == OPERATION_MODE_RECENT)
7656 return recent_get_selected_files (impl);
7660 info.file_from_entry = NULL;
7662 toplevel = get_toplevel (GTK_WIDGET (impl));
7664 current_focus = gtk_window_get_focus (toplevel);
7666 current_focus = NULL;
7668 file_list_seen = FALSE;
7669 if (current_focus == impl->browse_files_tree_view)
7671 GtkTreeSelection *selection;
7675 file_list_seen = TRUE;
7676 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7677 gtk_tree_selection_selected_foreach (selection, get_files_foreach, &info);
7679 /* If there is no selection in the file list, we probably have this situation:
7681 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7682 * 2. He then double-clicked on a folder ("bar") in the file list
7684 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7685 * filename entry to see if that is the case.
7687 if (info.result == NULL && impl->location_entry)
7690 else if (impl->location_entry && current_focus == impl->location_entry)
7692 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7696 check_save_entry (impl, &info.file_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7701 if (!is_well_formed)
7704 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7706 g_object_unref (info.file_from_entry);
7710 if (info.file_from_entry)
7711 info.result = g_slist_prepend (info.result, info.file_from_entry);
7712 else if (!file_list_seen)
7717 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7719 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7723 /* The focus is on a dialog's action area button or something else */
7724 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7725 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7733 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7734 * fall back to the current directory */
7735 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7736 info.result == NULL)
7738 GFile *current_folder;
7740 current_folder = gtk_file_chooser_get_current_folder_file (chooser);
7743 info.result = g_slist_prepend (info.result, current_folder);
7746 return g_slist_reverse (info.result);
7750 gtk_file_chooser_default_get_preview_file (GtkFileChooser *chooser)
7752 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7754 if (impl->preview_file)
7755 return g_object_ref (impl->preview_file);
7760 static GtkFileSystem *
7761 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7763 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7765 return impl->file_system;
7768 /* Shows or hides the filter widgets */
7770 show_filters (GtkFileChooserDefault *impl,
7774 gtk_widget_show (impl->filter_combo_hbox);
7776 gtk_widget_hide (impl->filter_combo_hbox);
7780 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7781 GtkFileFilter *filter)
7783 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7786 if (g_slist_find (impl->filters, filter))
7788 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7792 g_object_ref_sink (filter);
7793 impl->filters = g_slist_append (impl->filters, filter);
7795 name = gtk_file_filter_get_name (filter);
7797 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7799 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7801 if (!g_slist_find (impl->filters, impl->current_filter))
7802 set_current_filter (impl, filter);
7804 show_filters (impl, TRUE);
7808 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7809 GtkFileFilter *filter)
7811 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7812 GtkTreeModel *model;
7816 filter_index = g_slist_index (impl->filters, filter);
7818 if (filter_index < 0)
7820 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7824 impl->filters = g_slist_remove (impl->filters, filter);
7826 if (filter == impl->current_filter)
7829 set_current_filter (impl, impl->filters->data);
7831 set_current_filter (impl, NULL);
7834 /* Remove row from the combo box */
7835 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7836 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7837 g_assert_not_reached ();
7839 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7841 g_object_unref (filter);
7844 show_filters (impl, FALSE);
7848 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7850 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7852 return g_slist_copy (impl->filters);
7855 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7857 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7860 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7863 struct AddShortcutData
7865 GtkFileChooserDefault *impl;
7870 add_shortcut_get_info_cb (GCancellable *cancellable,
7872 const GError *error,
7876 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
7877 struct AddShortcutData *data = user_data;
7879 if (!g_slist_find (data->impl->loading_shortcuts, cancellable))
7882 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, cancellable);
7884 if (cancelled || error || (! _gtk_file_info_consider_as_directory (info)))
7887 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7889 shortcuts_insert_file (data->impl, pos, SHORTCUT_TYPE_FILE, NULL, data->file, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7892 g_object_unref (data->impl);
7893 g_object_unref (data->file);
7896 g_object_unref (cancellable);
7900 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7904 GCancellable *cancellable;
7905 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7906 struct AddShortcutData *data;
7910 /* Avoid adding duplicates */
7911 pos = shortcut_find_position (impl, file);
7912 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7916 uri = g_file_get_uri (file);
7917 /* translators, "Shortcut" means "Bookmark" here */
7919 GTK_FILE_CHOOSER_ERROR,
7920 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7921 _("Shortcut %s already exists"),
7928 for (l = impl->loading_shortcuts; l; l = l->next)
7930 GCancellable *c = l->data;
7933 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7934 if (f && g_file_equal (file, f))
7938 uri = g_file_get_uri (file);
7940 GTK_FILE_CHOOSER_ERROR,
7941 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7942 _("Shortcut %s already exists"),
7950 data = g_new0 (struct AddShortcutData, 1);
7951 data->impl = g_object_ref (impl);
7952 data->file = g_object_ref (file);
7954 cancellable = _gtk_file_system_get_info (impl->file_system, file,
7956 add_shortcut_get_info_cb, data);
7961 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, cancellable);
7962 g_object_set_data (G_OBJECT (cancellable), "add-shortcut-path-key", data->file);
7968 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7972 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7979 for (l = impl->loading_shortcuts; l; l = l->next)
7981 GCancellable *c = l->data;
7984 f = g_object_get_data (G_OBJECT (c), "add-shortcut-path-key");
7985 if (f && g_file_equal (file, f))
7987 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, c);
7988 g_cancellable_cancel (c);
7993 if (impl->num_shortcuts == 0)
7996 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7997 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7998 g_assert_not_reached ();
8000 for (i = 0; i < impl->num_shortcuts; i++)
8003 ShortcutType shortcut_type;
8006 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8007 SHORTCUTS_COL_DATA, &col_data,
8008 SHORTCUTS_COL_TYPE, &shortcut_type,
8010 g_assert (col_data != NULL);
8011 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8013 shortcut = col_data;
8014 if (g_file_equal (shortcut, file))
8016 shortcuts_remove_rows (impl, pos + i, 1);
8017 impl->num_shortcuts--;
8021 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8022 g_assert_not_reached ();
8027 uri = g_file_get_uri (file);
8028 /* translators, "Shortcut" means "Bookmark" here */
8030 GTK_FILE_CHOOSER_ERROR,
8031 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
8032 _("Shortcut %s does not exist"),
8040 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
8042 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
8048 if (impl->num_shortcuts == 0)
8051 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
8052 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8053 g_assert_not_reached ();
8057 for (i = 0; i < impl->num_shortcuts; i++)
8060 ShortcutType shortcut_type;
8063 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
8064 SHORTCUTS_COL_DATA, &col_data,
8065 SHORTCUTS_COL_TYPE, &shortcut_type,
8067 g_assert (col_data != NULL);
8068 g_assert (shortcut_type == SHORTCUT_TYPE_FILE);
8070 shortcut = col_data;
8071 list = g_slist_prepend (list, g_object_ref (shortcut));
8073 if (i != impl->num_shortcuts - 1)
8075 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
8076 g_assert_not_reached ();
8080 return g_slist_reverse (list);
8083 /* Guesses a size based upon font sizes */
8085 find_good_size_from_style (GtkWidget *widget,
8089 GtkFileChooserDefault *impl;
8094 g_assert (widget->style != NULL);
8095 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
8097 screen = gtk_widget_get_screen (widget);
8100 resolution = gdk_screen_get_resolution (screen);
8101 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
8105 resolution = 96.0; /* wheeee */
8107 font_size = pango_font_description_get_size (widget->style->font_desc);
8108 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
8110 *width = font_size * NUM_CHARS;
8111 *height = font_size * NUM_LINES;
8115 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
8116 gint *default_width,
8117 gint *default_height)
8119 GtkFileChooserDefault *impl;
8122 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8124 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8125 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8126 || impl->expand_folders)
8128 GtkFileChooserSettings *settings;
8129 int x, y, width, height;
8131 settings = _gtk_file_chooser_settings_new ();
8132 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
8133 g_object_unref (settings);
8135 if (x >= 0 && y >= 0 && width > 0 && height > 0)
8137 *default_width = width;
8138 *default_height = height;
8142 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
8144 if (impl->preview_widget_active &&
8145 impl->preview_widget &&
8146 GTK_WIDGET_VISIBLE (impl->preview_widget))
8148 gtk_widget_size_request (impl->preview_box, &req);
8149 *default_width += PREVIEW_HBOX_SPACING + req.width;
8152 if (impl->extra_widget &&
8153 GTK_WIDGET_VISIBLE (impl->extra_widget))
8155 gtk_widget_size_request (impl->extra_align, &req);
8156 *default_height += GTK_BOX (chooser_embed)->spacing + req.height;
8161 gtk_widget_size_request (GTK_WIDGET (impl), &req);
8162 *default_width = req.width;
8163 *default_height = req.height;
8167 struct switch_folder_closure {
8168 GtkFileChooserDefault *impl;
8173 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
8175 switch_folder_foreach_cb (GtkTreeModel *model,
8180 struct switch_folder_closure *closure;
8184 closure->file = _gtk_file_system_model_get_file (closure->impl->browse_files_model, iter);
8185 closure->num_selected++;
8188 /* Changes to the selected folder in the list view */
8190 switch_to_selected_folder (GtkFileChooserDefault *impl)
8192 GtkTreeSelection *selection;
8193 struct switch_folder_closure closure;
8195 /* We do this with foreach() rather than get_selected() as we may be in
8196 * multiple selection mode
8199 closure.impl = impl;
8200 closure.file = NULL;
8201 closure.num_selected = 0;
8203 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8204 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
8206 g_assert (closure.file && closure.num_selected == 1);
8208 change_folder_and_display_error (impl, closure.file, FALSE);
8211 /* Gets the GFileInfo for the selected row in the file list; assumes single
8215 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
8216 gboolean *had_selection)
8218 GtkTreeSelection *selection;
8222 g_assert (!impl->select_multiple);
8223 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8224 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
8226 *had_selection = FALSE;
8230 *had_selection = TRUE;
8232 info = _gtk_file_system_model_get_info (impl->browse_files_model, &iter);
8236 /* Gets the display name of the selected file in the file list; assumes single
8237 * selection mode and that something is selected.
8239 static const gchar *
8240 get_display_name_from_file_list (GtkFileChooserDefault *impl)
8243 gboolean had_selection;
8245 info = get_selected_file_info_from_file_list (impl, &had_selection);
8246 g_assert (had_selection);
8247 g_assert (info != NULL);
8249 return g_file_info_get_display_name (info);
8253 add_custom_button_to_dialog (GtkDialog *dialog,
8254 const gchar *mnemonic_label,
8255 const gchar *stock_id,
8260 button = gtk_button_new_with_mnemonic (mnemonic_label);
8261 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8262 gtk_button_set_image (GTK_BUTTON (button),
8263 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
8264 gtk_widget_show (button);
8266 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
8269 /* Presents an overwrite confirmation dialog; returns whether we should accept
8273 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
8274 const gchar *file_part,
8275 const gchar *folder_display_name)
8277 GtkWindow *toplevel;
8281 toplevel = get_toplevel (GTK_WIDGET (impl));
8283 dialog = gtk_message_dialog_new (toplevel,
8284 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
8285 GTK_MESSAGE_QUESTION,
8287 _("A file named \"%s\" already exists. Do you want to replace it?"),
8289 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
8290 _("The file already exists in \"%s\". Replacing it will "
8291 "overwrite its contents."),
8292 folder_display_name);
8294 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
8295 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"),
8296 GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
8297 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
8298 GTK_RESPONSE_ACCEPT,
8299 GTK_RESPONSE_CANCEL,
8301 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
8303 if (toplevel->group)
8304 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
8306 response = gtk_dialog_run (GTK_DIALOG (dialog));
8308 gtk_widget_destroy (dialog);
8310 return (response == GTK_RESPONSE_ACCEPT);
8313 struct GetDisplayNameData
8315 GtkFileChooserDefault *impl;
8320 confirmation_confirm_get_info_cb (GCancellable *cancellable,
8322 const GError *error,
8325 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8326 gboolean should_respond = FALSE;
8327 struct GetDisplayNameData *data = user_data;
8329 if (cancellable != data->impl->should_respond_get_info_cancellable)
8332 data->impl->should_respond_get_info_cancellable = NULL;
8338 /* Huh? Did the folder disappear? Let the caller deal with it */
8339 should_respond = TRUE;
8341 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, g_file_info_get_display_name (info));
8343 set_busy_cursor (data->impl, FALSE);
8345 g_signal_emit_by_name (data->impl, "response-requested");
8348 g_object_unref (data->impl);
8349 g_free (data->file_part);
8352 g_object_unref (cancellable);
8355 /* Does overwrite confirmation if appropriate, and returns whether the dialog
8356 * should respond. Can get the file part from the file list or the save entry.
8359 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
8360 const gchar *file_part,
8363 GtkFileChooserConfirmation conf;
8365 if (!impl->do_overwrite_confirmation)
8368 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
8370 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
8374 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
8376 struct GetDisplayNameData *data;
8378 g_assert (file_part != NULL);
8380 data = g_new0 (struct GetDisplayNameData, 1);
8381 data->impl = g_object_ref (impl);
8382 data->file_part = g_strdup (file_part);
8384 if (impl->should_respond_get_info_cancellable)
8385 g_cancellable_cancel (impl->should_respond_get_info_cancellable);
8387 impl->should_respond_get_info_cancellable =
8388 _gtk_file_system_get_info (impl->file_system, parent_file,
8389 "standard::display-name",
8390 confirmation_confirm_get_info_cb,
8392 set_busy_cursor (data->impl, TRUE);
8396 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
8399 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
8403 g_assert_not_reached ();
8408 struct FileExistsData
8410 GtkFileChooserDefault *impl;
8411 gboolean file_exists_and_is_not_folder;
8417 save_entry_get_info_cb (GCancellable *cancellable,
8419 const GError *error,
8422 gboolean parent_is_folder;
8423 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8424 struct FileExistsData *data = user_data;
8426 if (cancellable != data->impl->should_respond_get_info_cancellable)
8429 data->impl->should_respond_get_info_cancellable = NULL;
8431 set_busy_cursor (data->impl, FALSE);
8437 parent_is_folder = FALSE;
8439 parent_is_folder = _gtk_file_info_consider_as_directory (info);
8441 if (parent_is_folder)
8443 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8445 if (data->file_exists_and_is_not_folder)
8450 /* Dup the string because the string may be modified
8451 * depending on what clients do in the confirm-overwrite
8452 * signal and this corrupts the pointer
8454 file_part = g_strdup (_gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry)));
8455 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_file);
8459 g_signal_emit_by_name (data->impl, "response-requested");
8462 g_signal_emit_by_name (data->impl, "response-requested");
8464 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8466 GError *error = NULL;
8468 set_busy_cursor (data->impl, TRUE);
8469 g_file_make_directory (data->file, NULL, &error);
8470 set_busy_cursor (data->impl, FALSE);
8473 g_signal_emit_by_name (data->impl, "response-requested");
8475 error_creating_folder_dialog (data->impl, data->file, error);
8480 /* This will display an error, which is what we want */
8481 change_folder_and_display_error (data->impl, data->parent_file, FALSE);
8485 g_object_unref (data->impl);
8486 g_object_unref (data->file);
8487 g_object_unref (data->parent_file);
8490 g_object_unref (cancellable);
8494 file_exists_get_info_cb (GCancellable *cancellable,
8496 const GError *error,
8499 gboolean data_ownership_taken = FALSE;
8500 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
8501 gboolean file_exists_and_is_not_folder;
8502 struct FileExistsData *data = user_data;
8504 if (cancellable != data->impl->file_exists_get_info_cancellable)
8507 data->impl->file_exists_get_info_cancellable = NULL;
8509 set_busy_cursor (data->impl, FALSE);
8514 file_exists_and_is_not_folder = info && (! _gtk_file_info_consider_as_directory (info));
8516 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8517 /* user typed a filename; we are done */
8518 g_signal_emit_by_name (data->impl, "response-requested");
8519 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8520 && file_exists_and_is_not_folder)
8522 /* Oops, the user typed the name of an existing path which is not
8525 error_creating_folder_over_existing_file_dialog (data->impl, data->file,
8526 g_error_copy (error));
8530 /* check that everything up to the last component exists */
8532 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8533 data_ownership_taken = TRUE;
8535 if (data->impl->should_respond_get_info_cancellable)
8536 g_cancellable_cancel (data->impl->should_respond_get_info_cancellable);
8538 data->impl->should_respond_get_info_cancellable =
8539 _gtk_file_system_get_info (data->impl->file_system,
8542 save_entry_get_info_cb,
8544 set_busy_cursor (data->impl, TRUE);
8548 if (!data_ownership_taken)
8550 g_object_unref (data->impl);
8551 g_object_unref (data->file);
8552 g_object_unref (data->parent_file);
8556 g_object_unref (cancellable);
8560 paste_text_received (GtkClipboard *clipboard,
8562 GtkFileChooserDefault *impl)
8569 file = g_file_new_for_uri (text);
8571 if (!gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (impl), file, NULL))
8572 location_popup_handler (impl, text);
8574 g_object_unref (file);
8577 /* Handler for the "location-popup-on-paste" keybinding signal */
8579 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8581 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8582 GDK_SELECTION_CLIPBOARD);
8583 gtk_clipboard_request_text (clipboard,
8584 (GtkClipboardTextReceivedFunc) paste_text_received,
8589 /* Implementation for GtkFileChooserEmbed::should_respond() */
8591 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8593 GtkFileChooserDefault *impl;
8594 GtkWidget *toplevel;
8595 GtkWidget *current_focus;
8597 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8599 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8600 g_assert (GTK_IS_WINDOW (toplevel));
8602 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8604 if (current_focus == impl->browse_files_tree_view)
8606 /* The following array encodes what we do based on the impl->action and the
8607 * number of files selected.
8610 NOOP, /* Do nothing (don't respond) */
8611 RESPOND, /* Respond immediately */
8612 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8613 ALL_FILES, /* Respond only if everything selected is a file */
8614 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8615 SAVE_ENTRY, /* Go to the code for handling the save entry */
8616 NOT_REACHED /* Sanity check */
8618 static const ActionToTake what_to_do[4][3] = {
8619 /* 0 selected 1 selected many selected */
8620 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8621 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8622 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8623 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8627 gboolean all_files, all_folders;
8629 ActionToTake action;
8633 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8635 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8636 return search_should_respond (impl);
8638 if (impl->operation_mode == OPERATION_MODE_RECENT)
8639 return recent_should_respond (impl);
8641 selection_check (impl, &num_selected, &all_files, &all_folders);
8643 if (num_selected > 2)
8648 action = what_to_do [impl->action] [k];
8658 case RESPOND_OR_SWITCH:
8659 g_assert (num_selected == 1);
8663 switch_to_selected_folder (impl);
8666 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8667 return should_respond_after_confirm_overwrite (impl,
8668 get_display_name_from_file_list (impl),
8669 impl->current_folder);
8683 g_assert_not_reached ();
8686 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8689 gboolean is_well_formed, is_empty, is_file_part_empty;
8692 GtkFileChooserEntry *entry;
8697 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8698 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8699 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8700 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8701 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8703 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8704 check_save_entry (impl, &file, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8706 if (is_empty || !is_well_formed)
8709 g_assert (file != NULL);
8714 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8715 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8717 change_folder_and_display_error (impl, file, TRUE);
8720 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8721 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8723 /* The folder already exists, so we do not need to create it.
8724 * Just respond to terminate the dialog.
8730 g_assert_not_reached ();
8736 struct FileExistsData *data;
8738 /* We need to check whether file exists and is not a folder */
8740 data = g_new0 (struct FileExistsData, 1);
8741 data->impl = g_object_ref (impl);
8742 data->file = g_object_ref (file);
8743 data->parent_file = g_object_ref (_gtk_file_chooser_entry_get_current_folder (entry));
8745 if (impl->file_exists_get_info_cancellable)
8746 g_cancellable_cancel (impl->file_exists_get_info_cancellable);
8748 impl->file_exists_get_info_cancellable =
8749 _gtk_file_system_get_info (impl->file_system, file,
8751 file_exists_get_info_cb,
8754 set_busy_cursor (impl, TRUE);
8758 g_error_free (error);
8761 g_object_unref (file);
8764 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8766 /* The focus is on a dialog's action area button, *and* the widget that
8767 * was focused immediately before it is the file list.
8771 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8773 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8776 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8778 /* The focus is on a dialog's action area button, *and* the widget that
8779 * was focused immediately before it is the location entry.
8784 /* The focus is on a dialog's action area button or something else */
8785 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8786 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8791 g_assert_not_reached ();
8795 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8797 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8799 GtkFileChooserDefault *impl;
8802 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8804 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8805 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8807 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8808 widget = impl->browse_files_tree_view;
8810 widget = impl->location_entry;
8812 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8813 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8814 widget = impl->location_entry;
8817 g_assert_not_reached ();
8821 g_assert (widget != NULL);
8822 gtk_widget_grab_focus (widget);
8825 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GFiles */
8827 search_selected_foreach_get_file_cb (GtkTreeModel *model,
8837 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
8838 *list = g_slist_prepend (*list, g_object_ref (file));
8841 /* Constructs a list of the selected paths in search mode */
8843 search_get_selected_files (GtkFileChooserDefault *impl)
8846 GtkTreeSelection *selection;
8850 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8851 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_file_cb, &result);
8852 result = g_slist_reverse (result);
8857 /* Called from ::should_respond(). We return whether there are selected files
8858 * in the search list.
8861 search_should_respond (GtkFileChooserDefault *impl)
8863 GtkTreeSelection *selection;
8865 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8867 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8868 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8871 /* Adds one hit from the search engine to the search_model */
8873 search_add_hit (GtkFileChooserDefault *impl,
8878 file = g_file_new_for_uri (uri);
8882 if (!g_file_is_native (file))
8884 g_object_unref (file);
8888 _gtk_file_system_model_add_and_query_file (impl->search_model,
8892 g_object_unref (file);
8895 /* Callback used from GtkSearchEngine when we get new hits */
8897 search_engine_hits_added_cb (GtkSearchEngine *engine,
8901 GtkFileChooserDefault *impl;
8904 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8906 for (l = hits; l; l = l->next)
8907 search_add_hit (impl, (gchar*)l->data);
8910 /* Callback used from GtkSearchEngine when the query is done running */
8912 search_engine_finished_cb (GtkSearchEngine *engine,
8915 GtkFileChooserDefault *impl;
8917 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8920 /* EB: setting the model here will avoid loads of row events,
8921 * but it'll make the search look like blocked.
8923 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8924 GTK_TREE_MODEL (impl->search_model));
8927 /* FMQ: if search was empty, say that we got no hits */
8928 set_busy_cursor (impl, FALSE);
8931 /* Displays a generic error when we cannot create a GtkSearchEngine.
8932 * It would be better if _gtk_search_engine_new() gave us a GError
8933 * with a better message, but it doesn't do that right now.
8936 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8938 error_message (impl,
8939 _("Could not start the search process"),
8940 _("The program was not able to create a connection to the indexer "
8941 "daemon. Please make sure it is running."));
8945 search_engine_error_cb (GtkSearchEngine *engine,
8946 const gchar *message,
8949 GtkFileChooserDefault *impl;
8951 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8953 search_stop_searching (impl, TRUE);
8954 error_message (impl, _("Could not send the search request"), message);
8956 set_busy_cursor (impl, FALSE);
8959 /* Frees the data in the search_model */
8961 search_clear_model (GtkFileChooserDefault *impl,
8962 gboolean remove_from_treeview)
8964 if (!impl->search_model)
8967 g_object_unref (impl->search_model);
8968 impl->search_model = NULL;
8970 if (remove_from_treeview)
8971 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8974 /* Stops any ongoing searches; does not touch the search_model */
8976 search_stop_searching (GtkFileChooserDefault *impl,
8977 gboolean remove_query)
8979 if (remove_query && impl->search_query)
8981 g_object_unref (impl->search_query);
8982 impl->search_query = NULL;
8985 if (impl->search_engine)
8987 _gtk_search_engine_stop (impl->search_engine);
8989 g_object_unref (impl->search_engine);
8990 impl->search_engine = NULL;
8994 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8996 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8998 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9000 search_stop_searching (impl, FALSE);
9001 search_clear_model (impl, TRUE);
9003 gtk_widget_destroy (impl->search_hbox);
9004 impl->search_hbox = NULL;
9005 impl->search_entry = NULL;
9007 gtk_widget_show (impl->browse_path_bar);
9008 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9009 gtk_widget_hide (impl->browse_new_folder_button);
9011 gtk_widget_show (impl->browse_new_folder_button);
9014 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9015 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9017 gtk_widget_show (impl->location_button);
9019 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9020 gtk_widget_show (impl->location_entry_box);
9023 impl->operation_mode = OPERATION_MODE_BROWSE;
9025 file_list_set_sort_column_ids (impl);
9028 /* Creates the search_model and puts it in the tree view */
9030 search_setup_model (GtkFileChooserDefault *impl)
9032 g_assert (impl->search_model == NULL);
9034 impl->search_model = _gtk_file_system_model_new (file_system_model_set,
9036 MODEL_COL_NUM_COLUMNS,
9037 G_TYPE_STRING, /* MODEL_COL_NAME */
9038 G_TYPE_INT64, /* MODEL_COL_SIZE */
9039 G_TYPE_LONG, /* MODEL_COL_MTIME */
9040 G_TYPE_FILE, /* MODEL_COL_FILE */
9041 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */
9042 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */
9043 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */
9044 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */
9045 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */
9046 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
9049 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9053 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9057 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
9061 set_sort_column (impl);
9063 /* EB: setting the model here will make the hits list update feel
9064 * more "alive" than setting the model at the end of the search
9067 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9068 GTK_TREE_MODEL (impl->search_model));
9071 /* Creates a new query with the specified text and launches it */
9073 search_start_query (GtkFileChooserDefault *impl,
9074 const gchar *query_text)
9076 search_stop_searching (impl, FALSE);
9077 search_clear_model (impl, TRUE);
9078 search_setup_model (impl);
9079 set_busy_cursor (impl, TRUE);
9081 if (impl->search_engine == NULL)
9082 impl->search_engine = _gtk_search_engine_new ();
9084 if (!impl->search_engine)
9086 set_busy_cursor (impl, FALSE);
9087 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
9091 if (!impl->search_query)
9093 impl->search_query = _gtk_query_new ();
9094 _gtk_query_set_text (impl->search_query, query_text);
9097 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
9099 g_signal_connect (impl->search_engine, "hits-added",
9100 G_CALLBACK (search_engine_hits_added_cb), impl);
9101 g_signal_connect (impl->search_engine, "finished",
9102 G_CALLBACK (search_engine_finished_cb), impl);
9103 g_signal_connect (impl->search_engine, "error",
9104 G_CALLBACK (search_engine_error_cb), impl);
9106 _gtk_search_engine_start (impl->search_engine);
9109 /* Callback used when the user presses Enter while typing on the search
9110 * entry; starts the query
9113 search_entry_activate_cb (GtkEntry *entry,
9116 GtkFileChooserDefault *impl;
9119 impl = GTK_FILE_CHOOSER_DEFAULT (data);
9121 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
9122 if (strlen (text) == 0)
9125 /* reset any existing query object */
9126 if (impl->search_query)
9128 g_object_unref (impl->search_query);
9129 impl->search_query = NULL;
9132 search_start_query (impl, text);
9135 /* Hides the path bar and creates the search entry */
9137 search_setup_widgets (GtkFileChooserDefault *impl)
9143 impl->search_hbox = gtk_hbox_new (FALSE, 12);
9147 image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_BUTTON);
9148 gtk_size_group_add_widget (GTK_SIZE_GROUP (impl->browse_path_bar_size_group), image);
9149 gtk_box_pack_start (GTK_BOX (impl->search_hbox), image, FALSE, FALSE, 5);
9153 label = gtk_label_new (NULL);
9154 tmp = g_strdup_printf ("<b>%s</b>", _("Search:"));
9155 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9156 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
9161 impl->search_entry = gtk_entry_new ();
9162 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->search_entry);
9163 g_signal_connect (impl->search_entry, "activate",
9164 G_CALLBACK (search_entry_activate_cb),
9166 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
9168 /* if there already is a query, restart it */
9169 if (impl->search_query)
9171 gchar *query = _gtk_query_get_text (impl->search_query);
9175 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
9176 search_start_query (impl, query);
9182 g_object_unref (impl->search_query);
9183 impl->search_query = NULL;
9187 gtk_widget_hide (impl->browse_path_bar);
9188 gtk_widget_hide (impl->browse_new_folder_button);
9190 /* Box for search widgets */
9191 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9192 gtk_widget_show_all (impl->search_hbox);
9194 /* Hide the location widgets temporarily */
9196 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9197 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9199 gtk_widget_hide (impl->location_button);
9200 gtk_widget_hide (impl->location_entry_box);
9203 gtk_widget_grab_focus (impl->search_entry);
9205 /* FMQ: hide the filter combo? */
9208 /* Stops running operations like populating the browse model, searches, and the recent-files model */
9210 stop_operation (GtkFileChooserDefault *impl, OperationMode mode)
9214 case OPERATION_MODE_BROWSE:
9215 stop_loading_and_clear_list_model (impl);
9218 case OPERATION_MODE_SEARCH:
9219 search_stop_searching (impl, FALSE);
9220 search_clear_model (impl, TRUE);
9222 gtk_widget_destroy (impl->search_hbox);
9223 impl->search_hbox = NULL;
9224 impl->search_entry = NULL;
9227 case OPERATION_MODE_RECENT:
9228 recent_stop_loading (impl);
9229 recent_clear_model (impl, TRUE);
9231 gtk_widget_destroy (impl->recent_hbox);
9232 impl->recent_hbox = NULL;
9237 /* Main entry point to the searching functions; this gets called when the user
9238 * activates the Search shortcut.
9241 search_activate (GtkFileChooserDefault *impl)
9243 OperationMode previous_mode;
9245 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9247 gtk_widget_grab_focus (impl->search_entry);
9251 previous_mode = impl->operation_mode;
9252 impl->operation_mode = OPERATION_MODE_SEARCH;
9254 stop_operation (impl, previous_mode);
9256 g_assert (impl->search_hbox == NULL);
9257 g_assert (impl->search_entry == NULL);
9258 g_assert (impl->search_model == NULL);
9260 search_setup_widgets (impl);
9261 file_list_set_sort_column_ids (impl);
9265 * Recent files support
9268 /* Frees the data in the recent_model */
9270 recent_clear_model (GtkFileChooserDefault *impl,
9271 gboolean remove_from_treeview)
9273 GtkTreeModel *model;
9275 if (!impl->recent_model)
9278 model = GTK_TREE_MODEL (impl->recent_model);
9280 if (remove_from_treeview)
9281 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9283 g_object_unref (impl->recent_model);
9284 impl->recent_model = NULL;
9287 /* Stops any ongoing loading of the recent files list; does
9288 * not touch the recent_model
9291 recent_stop_loading (GtkFileChooserDefault *impl)
9293 if (impl->load_recent_id)
9295 g_source_remove (impl->load_recent_id);
9296 impl->load_recent_id = 0;
9300 /* Stops any pending load, clears the file list, and switches
9301 * back to OPERATION_MODE_BROWSE
9304 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9306 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9308 recent_stop_loading (impl);
9309 recent_clear_model (impl, TRUE);
9311 gtk_widget_destroy (impl->recent_hbox);
9312 impl->recent_hbox = NULL;
9314 gtk_widget_show (impl->browse_path_bar);
9315 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || !impl->create_folders)
9316 gtk_widget_hide (impl->browse_new_folder_button);
9318 gtk_widget_show (impl->browse_new_folder_button);
9320 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9321 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9323 gtk_widget_show (impl->location_button);
9325 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9326 gtk_widget_show (impl->location_entry_box);
9329 gtk_tree_view_column_set_visible (impl->list_size_column, impl->show_size_column);
9331 impl->operation_mode = OPERATION_MODE_BROWSE;
9333 file_list_set_sort_column_ids (impl);
9337 recent_setup_model (GtkFileChooserDefault *impl)
9339 g_assert (impl->recent_model == NULL);
9341 impl->recent_model = _gtk_file_system_model_new (file_system_model_set,
9343 MODEL_COL_NUM_COLUMNS,
9344 G_TYPE_STRING, /* MODEL_COL_NAME */
9345 G_TYPE_INT64, /* MODEL_COL_SIZE */
9346 G_TYPE_LONG, /* MODEL_COL_MTIME */
9347 G_TYPE_FILE, /* MODEL_COL_FILE */
9348 G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */
9349 G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */
9350 GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */
9351 G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */
9352 G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */
9353 PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
9356 _gtk_file_system_model_set_filter (impl->recent_model,
9357 impl->current_filter);
9358 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9362 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9366 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9370 set_sort_column (impl);
9375 GtkFileChooserDefault *impl;
9378 gint n_loaded_items;
9379 guint needs_sorting : 1;
9383 recent_idle_cleanup (gpointer data)
9385 RecentLoadData *load_data = data;
9386 GtkFileChooserDefault *impl = load_data->impl;
9388 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9389 GTK_TREE_MODEL (impl->recent_model));
9391 set_busy_cursor (impl, FALSE);
9393 impl->load_recent_id = 0;
9395 if (load_data->items)
9397 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9398 g_list_free (load_data->items);
9405 recent_sort_mru (gconstpointer a,
9408 GtkRecentInfo *info_a = (GtkRecentInfo *) a;
9409 GtkRecentInfo *info_b = (GtkRecentInfo *) b;
9411 return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
9415 get_recent_files_limit (GtkWidget *widget)
9417 GtkSettings *settings;
9420 if (gtk_widget_has_screen (widget))
9421 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9423 settings = gtk_settings_get_default ();
9425 g_object_get (G_OBJECT (settings), "gtk-recent-files-limit", &limit, NULL);
9431 recent_idle_load (gpointer data)
9433 RecentLoadData *load_data = data;
9434 GtkFileChooserDefault *impl = load_data->impl;
9438 if (!impl->recent_manager)
9441 /* first iteration: load all the items */
9442 if (!load_data->items)
9444 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9445 if (!load_data->items)
9448 load_data->needs_sorting = TRUE;
9453 /* second iteration: preliminary MRU sorting and clamping */
9454 if (load_data->needs_sorting)
9458 load_data->items = g_list_sort (load_data->items, recent_sort_mru);
9459 load_data->n_items = g_list_length (load_data->items);
9461 limit = get_recent_files_limit (GTK_WIDGET (impl));
9463 if (limit != -1 && (load_data->n_items > limit))
9467 clamp = g_list_nth (load_data->items, limit - 1);
9468 if (G_LIKELY (clamp))
9473 g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
9476 load_data->n_items = limit;
9480 load_data->n_loaded_items = 0;
9481 load_data->needs_sorting = FALSE;
9486 /* finished loading items */
9487 for (walk = load_data->items; walk; walk = walk->next)
9489 GtkRecentInfo *info = walk->data;
9490 file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
9492 _gtk_file_system_model_add_and_query_file (impl->recent_model,
9495 gtk_recent_info_unref (walk->data);
9496 g_object_unref (file);
9499 g_list_free (load_data->items);
9500 load_data->items = NULL;
9506 recent_start_loading (GtkFileChooserDefault *impl)
9508 RecentLoadData *load_data;
9510 recent_stop_loading (impl);
9511 recent_clear_model (impl, TRUE);
9512 recent_setup_model (impl);
9513 set_busy_cursor (impl, TRUE);
9515 g_assert (impl->load_recent_id == 0);
9517 load_data = g_new (RecentLoadData, 1);
9518 load_data->impl = impl;
9519 load_data->items = NULL;
9520 load_data->n_items = 0;
9521 load_data->n_loaded_items = 0;
9522 load_data->needs_sorting = TRUE;
9524 /* begin lazy loading the recent files into the model */
9525 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9528 recent_idle_cleanup);
9532 recent_selected_foreach_get_file_cb (GtkTreeModel *model,
9542 gtk_tree_model_get (model, iter, MODEL_COL_FILE, &file, -1);
9543 *list = g_slist_prepend (*list, file);
9546 /* Constructs a list of the selected paths in recent files mode */
9548 recent_get_selected_files (GtkFileChooserDefault *impl)
9551 GtkTreeSelection *selection;
9555 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9556 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_file_cb, &result);
9557 result = g_slist_reverse (result);
9562 /* Called from ::should_respond(). We return whether there are selected
9563 * files in the recent files list.
9566 recent_should_respond (GtkFileChooserDefault *impl)
9568 GtkTreeSelection *selection;
9570 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9572 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9573 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9576 /* Hide the location widgets temporarily */
9578 recent_hide_entry (GtkFileChooserDefault *impl)
9584 impl->recent_hbox = gtk_hbox_new (FALSE, 12);
9587 image = gtk_image_new_from_icon_name ("document-open-recent", GTK_ICON_SIZE_BUTTON);
9588 gtk_size_group_add_widget (impl->browse_path_bar_size_group, image);
9589 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), image, FALSE, FALSE, 5);
9592 label = gtk_label_new (NULL);
9593 tmp = g_strdup_printf ("<b>%s</b>", _("Recently Used"));
9594 gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), tmp);
9595 gtk_box_pack_start (GTK_BOX (impl->recent_hbox), label, FALSE, FALSE, 0);
9598 gtk_widget_hide (impl->browse_path_bar);
9599 gtk_widget_hide (impl->browse_new_folder_button);
9601 /* Box for recent widgets */
9602 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->recent_hbox, TRUE, TRUE, 0);
9603 gtk_widget_show_all (impl->recent_hbox);
9605 /* Hide the location widgets temporarily */
9606 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9607 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9609 gtk_widget_hide (impl->location_button);
9610 gtk_widget_hide (impl->location_entry_box);
9614 /* Main entry point to the recent files functions; this gets called when
9615 * the user activates the Recently Used shortcut.
9618 recent_activate (GtkFileChooserDefault *impl)
9620 OperationMode previous_mode;
9622 if (impl->operation_mode == OPERATION_MODE_RECENT)
9625 previous_mode = impl->operation_mode;
9626 impl->operation_mode = OPERATION_MODE_RECENT;
9628 stop_operation (impl, previous_mode);
9630 recent_hide_entry (impl);
9632 file_list_set_sort_column_ids (impl);
9633 recent_start_loading (impl);
9637 set_current_filter (GtkFileChooserDefault *impl,
9638 GtkFileFilter *filter)
9640 if (impl->current_filter != filter)
9644 /* NULL filters are allowed to reset to non-filtered status
9646 filter_index = g_slist_index (impl->filters, filter);
9647 if (impl->filters && filter && filter_index < 0)
9650 if (impl->current_filter)
9651 g_object_unref (impl->current_filter);
9652 impl->current_filter = filter;
9653 if (impl->current_filter)
9655 g_object_ref_sink (impl->current_filter);
9659 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9662 if (impl->browse_files_model)
9663 install_list_model_filter (impl);
9665 if (impl->search_model)
9666 _gtk_file_system_model_set_filter (impl->search_model, filter);
9668 if (impl->recent_model)
9669 _gtk_file_system_model_set_filter (impl->recent_model, filter);
9671 g_object_notify (G_OBJECT (impl), "filter");
9676 filter_combo_changed (GtkComboBox *combo_box,
9677 GtkFileChooserDefault *impl)
9679 gint new_index = gtk_combo_box_get_active (combo_box);
9680 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9682 set_current_filter (impl, new_filter);
9686 check_preview_change (GtkFileChooserDefault *impl)
9688 GtkTreePath *cursor_path;
9690 char *new_display_name;
9691 GtkTreeModel *model;
9693 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9694 model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
9699 gtk_tree_model_get_iter (model, &iter, cursor_path);
9700 gtk_tree_model_get (model, &iter,
9701 MODEL_COL_FILE, &new_file,
9702 MODEL_COL_NAME, &new_display_name,
9705 gtk_tree_path_free (cursor_path);
9710 new_display_name = NULL;
9713 if (new_file != impl->preview_file &&
9714 !(new_file && impl->preview_file &&
9715 g_file_equal (new_file, impl->preview_file)))
9717 if (impl->preview_file)
9719 g_object_unref (impl->preview_file);
9720 g_free (impl->preview_display_name);
9725 impl->preview_file = new_file;
9726 impl->preview_display_name = new_display_name;
9730 impl->preview_file = NULL;
9731 impl->preview_display_name = NULL;
9732 g_free (new_display_name);
9735 if (impl->use_preview_label && impl->preview_label)
9736 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9738 g_signal_emit_by_name (impl, "update-preview");
9743 shortcuts_activate_volume_mount_cb (GCancellable *cancellable,
9744 GtkFileSystemVolume *volume,
9745 const GError *error,
9749 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9750 GtkFileChooserDefault *impl = data;
9752 if (cancellable != impl->shortcuts_activate_iter_cancellable)
9755 impl->shortcuts_activate_iter_cancellable = NULL;
9757 set_busy_cursor (impl, FALSE);
9764 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
9768 name = _gtk_file_system_volume_get_display_name (volume);
9769 msg = g_strdup_printf (_("Could not mount %s"), name);
9771 error_message (impl, msg, error->message);
9780 file = _gtk_file_system_volume_get_root (volume);
9783 change_folder_and_display_error (impl, file, FALSE);
9784 g_object_unref (file);
9788 g_object_unref (impl);
9789 g_object_unref (cancellable);
9793 /* Activates a volume by mounting it if necessary and then switching to its
9797 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9798 GtkFileSystemVolume *volume)
9802 switch (impl->operation_mode)
9804 case OPERATION_MODE_BROWSE:
9806 case OPERATION_MODE_SEARCH:
9807 search_switch_to_browse_mode (impl);
9809 case OPERATION_MODE_RECENT:
9810 recent_switch_to_browse_mode (impl);
9814 /* We ref the file chooser since volume_mount() may run a main loop, and the
9815 * user could close the file chooser window in the meantime.
9817 g_object_ref (impl);
9819 if (!_gtk_file_system_volume_is_mounted (volume))
9821 GMountOperation *mount_op;
9823 set_busy_cursor (impl, TRUE);
9825 mount_op = gtk_mount_operation_new (get_toplevel (GTK_WIDGET (impl)));
9826 impl->shortcuts_activate_iter_cancellable =
9827 _gtk_file_system_mount_volume (impl->file_system, volume, mount_op,
9828 shortcuts_activate_volume_mount_cb,
9829 g_object_ref (impl));
9830 g_object_unref (mount_op);
9834 file = _gtk_file_system_volume_get_root (volume);
9837 change_folder_and_display_error (impl, file, FALSE);
9838 g_object_unref (file);
9842 g_object_unref (impl);
9845 /* Opens the folder or volume at the specified iter in the shortcuts model */
9846 struct ShortcutsActivateData
9848 GtkFileChooserDefault *impl;
9853 shortcuts_activate_get_info_cb (GCancellable *cancellable,
9855 const GError *error,
9858 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
9859 struct ShortcutsActivateData *data = user_data;
9861 if (cancellable != data->impl->shortcuts_activate_iter_cancellable)
9864 data->impl->shortcuts_activate_iter_cancellable = NULL;
9869 if (!error && _gtk_file_info_consider_as_directory (info))
9870 change_folder_and_display_error (data->impl, data->file, FALSE);
9872 gtk_file_chooser_default_select_file (GTK_FILE_CHOOSER (data->impl),
9877 g_object_unref (data->impl);
9878 g_object_unref (data->file);
9881 g_object_unref (cancellable);
9885 shortcuts_activate_mount_enclosing_volume (GCancellable *cancellable,
9886 GtkFileSystemVolume *volume,
9887 const GError *error,
9890 struct ShortcutsActivateData *data = user_data;
9894 error_changing_folder_dialog (data->impl, data->file, g_error_copy (error));
9896 g_object_unref (data->impl);
9897 g_object_unref (data->file);
9903 data->impl->shortcuts_activate_iter_cancellable =
9904 _gtk_file_system_get_info (data->impl->file_system, data->file,
9906 shortcuts_activate_get_info_cb, data);
9910 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9914 ShortcutType shortcut_type;
9916 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY
9917 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
9918 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
9919 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9921 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9922 SHORTCUTS_COL_DATA, &col_data,
9923 SHORTCUTS_COL_TYPE, &shortcut_type,
9926 if (impl->shortcuts_activate_iter_cancellable)
9928 g_cancellable_cancel (impl->shortcuts_activate_iter_cancellable);
9929 impl->shortcuts_activate_iter_cancellable = NULL;
9932 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9934 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9936 GtkFileSystemVolume *volume;
9940 shortcuts_activate_volume (impl, volume);
9942 else if (shortcut_type == SHORTCUT_TYPE_FILE)
9944 struct ShortcutsActivateData *data;
9945 GtkFileSystemVolume *volume;
9947 volume = _gtk_file_system_get_volume_for_file (impl->file_system, col_data);
9949 data = g_new0 (struct ShortcutsActivateData, 1);
9950 data->impl = g_object_ref (impl);
9951 data->file = g_object_ref (col_data);
9953 if (!volume || !_gtk_file_system_volume_is_mounted (volume))
9955 GMountOperation *mount_operation;
9956 GtkWidget *toplevel;
9958 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
9960 mount_operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));
9962 impl->shortcuts_activate_iter_cancellable =
9963 _gtk_file_system_mount_enclosing_volume (impl->file_system, col_data,
9965 shortcuts_activate_mount_enclosing_volume,
9970 impl->shortcuts_activate_iter_cancellable =
9971 _gtk_file_system_get_info (impl->file_system, data->file,
9973 shortcuts_activate_get_info_cb, data);
9976 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9978 search_activate (impl);
9980 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9982 recent_activate (impl);
9986 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9988 shortcuts_key_press_event_cb (GtkWidget *widget,
9990 GtkFileChooserDefault *impl)
9994 modifiers = gtk_accelerator_get_default_mod_mask ();
9996 if (key_is_left_or_right (event))
9998 gtk_widget_grab_focus (impl->browse_files_tree_view);
10002 if ((event->keyval == GDK_BackSpace
10003 || event->keyval == GDK_Delete
10004 || event->keyval == GDK_KP_Delete)
10005 && (event->state & modifiers) == 0)
10007 remove_selected_bookmarks (impl);
10011 if ((event->keyval == GDK_F2)
10012 && (event->state & modifiers) == 0)
10014 rename_selected_bookmark (impl);
10022 shortcuts_select_func (GtkTreeSelection *selection,
10023 GtkTreeModel *model,
10025 gboolean path_currently_selected,
10028 GtkFileChooserDefault *impl = data;
10029 GtkTreeIter filter_iter;
10030 ShortcutType shortcut_type;
10032 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
10033 g_assert_not_reached ();
10035 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
10037 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
10041 list_select_func (GtkTreeSelection *selection,
10042 GtkTreeModel *model,
10044 gboolean path_currently_selected,
10047 GtkFileChooserDefault *impl = data;
10049 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10050 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10053 gboolean is_folder;
10055 if (!gtk_tree_model_get_iter (model, &iter, path))
10057 gtk_tree_model_get (model, &iter,
10058 MODEL_COL_IS_FOLDER, &is_folder,
10068 list_selection_changed (GtkTreeSelection *selection,
10069 GtkFileChooserDefault *impl)
10071 /* See if we are in the new folder editable row for Save mode */
10072 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
10073 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10076 gboolean had_selection;
10078 info = get_selected_file_info_from_file_list (impl, &had_selection);
10079 if (!had_selection)
10080 goto out; /* normal processing */
10083 return; /* We are on the editable row for New Folder */
10088 if (impl->location_entry)
10089 update_chooser_entry (impl);
10091 check_preview_change (impl);
10092 bookmarks_check_add_sensitivity (impl);
10094 g_signal_emit_by_name (impl, "selection-changed", 0);
10097 /* Callback used when a row in the file list is activated */
10099 list_row_activated (GtkTreeView *tree_view,
10101 GtkTreeViewColumn *column,
10102 GtkFileChooserDefault *impl)
10106 GtkTreeModel *model;
10107 gboolean is_folder;
10109 model = gtk_tree_view_get_model (tree_view);
10111 if (!gtk_tree_model_get_iter (model, &iter, path))
10114 gtk_tree_model_get (model, &iter,
10115 MODEL_COL_FILE, &file,
10116 MODEL_COL_IS_FOLDER, &is_folder,
10119 if (is_folder && file)
10121 change_folder_and_display_error (impl, file, FALSE);
10125 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10126 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10127 g_signal_emit_by_name (impl, "file-activated");
10130 g_object_unref (file);
10134 path_bar_clicked (GtkPathBar *path_bar,
10137 gboolean child_is_hidden,
10138 GtkFileChooserDefault *impl)
10141 pending_select_files_add (impl, child_file);
10143 if (!change_folder_and_display_error (impl, file, FALSE))
10146 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10147 * show hidden files so that ".baz" appears in the file list, as it will still
10148 * be shown in the path bar: "/foo/[bar]/.baz"
10150 if (child_is_hidden)
10151 g_object_set (impl, "show-hidden", TRUE, NULL);
10155 update_cell_renderer_attributes (GtkFileChooserDefault *impl)
10157 GtkTreeViewColumn *column;
10158 GtkCellRenderer *renderer;
10159 GList *walk, *list;
10160 gboolean always_sensitive;
10162 always_sensitive = impl->action != GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
10163 impl->action != GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
10166 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 0);
10167 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10168 for (walk = list; walk; walk = walk->next)
10170 renderer = walk->data;
10171 if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
10173 gtk_tree_view_column_set_attributes (column, renderer,
10174 "pixbuf", MODEL_COL_PIXBUF,
10179 gtk_tree_view_column_set_attributes (column, renderer,
10180 "text", MODEL_COL_NAME,
10181 "ellipsize", MODEL_COL_ELLIPSIZE,
10184 if (always_sensitive)
10185 g_object_set (renderer, "sensitive", TRUE, NULL);
10187 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10189 g_list_free (list);
10192 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 1);
10193 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10194 renderer = list->data;
10195 gtk_tree_view_column_set_attributes (column, renderer,
10196 "text", MODEL_COL_SIZE_TEXT,
10198 if (always_sensitive)
10199 g_object_set (renderer, "sensitive", TRUE, NULL);
10201 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10202 g_list_free (list);
10205 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_files_tree_view), 2);
10206 list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
10207 renderer = list->data;
10208 gtk_tree_view_column_set_attributes (column, renderer,
10209 "text", MODEL_COL_MTIME_TEXT,
10211 if (always_sensitive)
10212 g_object_set (renderer, "sensitive", TRUE, NULL);
10214 gtk_tree_view_column_add_attribute (column, renderer, "sensitive", MODEL_COL_IS_FOLDER);
10215 g_list_free (list);
10219 _gtk_file_chooser_default_new (void)
10221 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT, NULL);
10225 location_set_user_text (GtkFileChooserDefault *impl,
10228 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10229 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10233 location_popup_handler (GtkFileChooserDefault *impl,
10236 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10238 GtkWidget *widget_to_focus;
10240 /* This will give us the location widgets back */
10241 switch (impl->operation_mode)
10243 case OPERATION_MODE_SEARCH:
10244 search_switch_to_browse_mode (impl);
10246 case OPERATION_MODE_RECENT:
10247 recent_switch_to_browse_mode (impl);
10249 case OPERATION_MODE_BROWSE:
10250 g_assert_not_reached ();
10254 if (impl->current_folder)
10255 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10257 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10258 widget_to_focus = impl->browse_files_tree_view;
10260 widget_to_focus = impl->location_entry;
10262 gtk_widget_grab_focus (widget_to_focus);
10266 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10267 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10269 LocationMode new_mode;
10273 /* since the user typed something, we unconditionally want to turn on the entry */
10274 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10276 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10277 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10278 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10279 new_mode = LOCATION_MODE_PATH_BAR;
10282 g_assert_not_reached ();
10286 location_mode_set (impl, new_mode, TRUE);
10287 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10290 location_set_user_text (impl, path);
10293 location_entry_set_initial_text (impl);
10294 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10298 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10299 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10301 gtk_widget_grab_focus (impl->location_entry);
10303 location_set_user_text (impl, path);
10306 g_assert_not_reached ();
10309 /* Handler for the "up-folder" keybinding signal */
10311 up_folder_handler (GtkFileChooserDefault *impl)
10313 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10316 /* Handler for the "down-folder" keybinding signal */
10318 down_folder_handler (GtkFileChooserDefault *impl)
10320 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10323 /* Switches to the shortcut in the specified index */
10325 switch_to_shortcut (GtkFileChooserDefault *impl,
10330 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10331 g_assert_not_reached ();
10333 shortcuts_activate_iter (impl, &iter);
10336 /* Handler for the "home-folder" keybinding signal */
10338 home_folder_handler (GtkFileChooserDefault *impl)
10340 if (impl->has_home)
10341 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10344 /* Handler for the "desktop-folder" keybinding signal */
10346 desktop_folder_handler (GtkFileChooserDefault *impl)
10348 if (impl->has_desktop)
10349 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10352 /* Handler for the "search-shortcut" keybinding signal */
10354 search_shortcut_handler (GtkFileChooserDefault *impl)
10356 if (impl->has_search)
10358 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10360 /* we want the entry widget to grab the focus the first
10361 * time, not the browse_files_tree_view widget.
10363 if (impl->search_entry)
10364 gtk_widget_grab_focus (impl->search_entry);
10368 /* Handler for the "recent-shortcut" keybinding signal */
10370 recent_shortcut_handler (GtkFileChooserDefault *impl)
10372 if (impl->has_recent)
10373 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10377 quick_bookmark_handler (GtkFileChooserDefault *impl,
10378 gint bookmark_index)
10383 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10386 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10388 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10389 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10392 gtk_tree_path_free (path);
10394 switch_to_shortcut (impl, bookmark_pos);
10398 show_hidden_handler (GtkFileChooserDefault *impl)
10400 g_object_set (impl,
10401 "show-hidden", !impl->show_hidden,
10406 /* Drag and drop interfaces */
10409 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10414 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10416 model->impl = NULL;
10419 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10421 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10424 ShortcutsPaneModelFilter *model;
10428 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10430 pos = *gtk_tree_path_get_indices (path);
10431 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10433 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10436 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
10438 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10440 GtkSelectionData *selection_data)
10442 ShortcutsPaneModelFilter *model;
10444 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10451 /* Fill the GtkTreeDragSourceIface vtable */
10453 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10455 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10456 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10460 /* Fill the GtkTreeDragDestIface vtable */
10462 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10464 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10465 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10469 static GtkTreeModel *
10470 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10471 GtkTreeModel *child_model,
10474 ShortcutsPaneModelFilter *model;
10476 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10477 "child-model", child_model,
10478 "virtual-root", root,
10481 model->impl = impl;
10483 return GTK_TREE_MODEL (model);