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.
23 #include "gdk/gdkkeysyms.h"
24 #include "gtkalignment.h"
25 #include "gtkbindings.h"
26 #include "gtkbutton.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 "gtkeventbox.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserprivate.h"
37 #include "gtkfilechooserdefault.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechoosersettings.h"
41 #include "gtkfilechooserutils.h"
42 #include "gtkfilechooser.h"
43 #include "gtkfilesystemmodel.h"
46 #include "gtkhpaned.h"
47 #include "gtkiconfactory.h"
48 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
53 #include "gtkmarshalers.h"
54 #include "gtkmenuitem.h"
55 #include "gtkmessagedialog.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 "gtktreednd.h"
67 #include "gtktreeprivate.h"
68 #include "gtktreeselection.h"
69 #include "gtktypebuiltins.h"
72 #if defined (G_OS_UNIX)
73 #include "gtkfilesystemunix.h"
74 #elif defined (G_OS_WIN32)
75 #include "gtkfilesystemwin32.h"
84 #include <sys/types.h>
95 #undef PROFILE_FILE_CHOOSER
96 #ifdef PROFILE_FILE_CHOOSER
103 #define PROFILE_INDENT 4
104 static int profile_indent;
107 profile_add_indent (int indent)
109 profile_indent += indent;
110 if (profile_indent < 0)
111 g_error ("You screwed up your indentation");
115 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
120 profile_add_indent (indent);
122 if (profile_indent == 0)
123 str = g_strdup_printf ("MARK: %s %s %s", func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
125 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
131 profile_add_indent (indent);
134 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
135 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
136 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
138 #define profile_start(x, y)
139 #define profile_end(x, y)
140 #define profile_msg(x, y)
145 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
147 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
148 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
149 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
151 #define MAX_LOADING_TIME 500
153 struct _GtkFileChooserDefaultClass
155 GtkVBoxClass parent_class;
161 LOCATION_POPUP_ON_PASTE,
167 LOCATION_TOGGLE_POPUP,
175 static guint signals[LAST_SIGNAL] = { 0 };
177 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
179 SHORTCUTS_COL_PIXBUF,
183 SHORTCUTS_COL_REMOVABLE,
184 SHORTCUTS_COL_PIXBUF_VISIBLE,
185 SHORTCUTS_COL_HANDLE,
186 SHORTCUTS_COL_NUM_COLUMNS
191 SHORTCUT_TYPE_VOLUME,
192 SHORTCUT_TYPE_SEPARATOR,
193 SHORTCUT_TYPE_SEARCH,
197 /* Column numbers for the file list */
202 FILE_LIST_COL_NUM_COLUMNS
205 /* Column numbers for the search model.
206 * Keep this in sync with search_setup_model()
209 SEARCH_MODEL_COL_PATH,
210 SEARCH_MODEL_COL_DISPLAY_NAME,
211 SEARCH_MODEL_COL_COLLATION_KEY,
212 SEARCH_MODEL_COL_STAT,
213 SEARCH_MODEL_COL_HANDLE,
214 SEARCH_MODEL_COL_PIXBUF,
215 SEARCH_MODEL_COL_MIME_TYPE,
216 SEARCH_MODEL_COL_IS_FOLDER,
217 SEARCH_MODEL_COL_NUM_COLUMNS
221 RECENT_MODEL_COL_PATH,
222 RECENT_MODEL_COL_DISPLAY_NAME,
223 RECENT_MODEL_COL_INFO,
224 RECENT_MODEL_COL_IS_FOLDER,
225 RECENT_MODEL_COL_HANDLE,
226 RECENT_MODEL_COL_NUM_COLUMNS
229 /* Identifiers for target types */
235 /* Target types for dragging from the shortcuts list */
236 static const GtkTargetEntry shortcuts_source_targets[] = {
237 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
240 static const int num_shortcuts_source_targets = G_N_ELEMENTS (shortcuts_source_targets);
242 /* Target types for dropping into the shortcuts list */
243 static const GtkTargetEntry shortcuts_dest_targets[] = {
244 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
245 { "text/uri-list", 0, TEXT_URI_LIST }
248 static const int num_shortcuts_dest_targets = G_N_ELEMENTS (shortcuts_dest_targets);
250 /* Target types for DnD from the file list */
251 static const GtkTargetEntry file_list_source_targets[] = {
252 { "text/uri-list", 0, TEXT_URI_LIST }
255 static const int num_file_list_source_targets = G_N_ELEMENTS (file_list_source_targets);
257 /* Target types for dropping into the file list */
258 static const GtkTargetEntry file_list_dest_targets[] = {
259 { "text/uri-list", 0, TEXT_URI_LIST }
262 static const int num_file_list_dest_targets = G_N_ELEMENTS (file_list_dest_targets);
265 search_is_possible (GtkFileChooserDefault *impl)
267 if (impl->search_engine == NULL)
268 impl->search_engine = _gtk_search_engine_new ();
270 return impl->search_engine != NULL;
273 /* Interesting places in the shortcuts bar */
276 SHORTCUTS_SEARCH_SEPARATOR,
278 SHORTCUTS_RECENT_SEPARATOR,
283 SHORTCUTS_BOOKMARKS_SEPARATOR,
285 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
286 SHORTCUTS_CURRENT_FOLDER
289 /* Icon size for if we can't get it from the theme */
290 #define FALLBACK_ICON_SIZE 16
292 #define PREVIEW_HBOX_SPACING 12
296 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
297 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
299 static GObject* gtk_file_chooser_default_constructor (GType type,
300 guint n_construct_properties,
301 GObjectConstructParam *construct_params);
302 static void gtk_file_chooser_default_finalize (GObject *object);
303 static void gtk_file_chooser_default_set_property (GObject *object,
307 static void gtk_file_chooser_default_get_property (GObject *object,
311 static void gtk_file_chooser_default_dispose (GObject *object);
312 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
313 static void gtk_file_chooser_default_map (GtkWidget *widget);
314 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
315 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
316 GtkWidget *previous_toplevel);
317 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
318 GtkStyle *previous_style);
319 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
320 GdkScreen *previous_screen);
322 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
323 const GtkFilePath *path,
325 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
326 const GtkFilePath *path,
328 gboolean clear_entry,
330 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
331 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
333 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
334 const GtkFilePath *path,
336 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
337 const GtkFilePath *path);
338 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
339 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
340 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
341 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
342 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
343 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
344 GtkFileFilter *filter);
345 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
346 GtkFileFilter *filter);
347 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
348 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
349 const GtkFilePath *path,
351 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
352 const GtkFilePath *path,
354 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
356 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
358 gint *default_height);
359 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
360 gboolean *resize_horizontally,
361 gboolean *resize_vertically);
362 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
363 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
365 static void location_popup_handler (GtkFileChooserDefault *impl,
367 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
368 static void location_toggle_popup_handler (GtkFileChooserDefault *impl);
369 static void up_folder_handler (GtkFileChooserDefault *impl);
370 static void down_folder_handler (GtkFileChooserDefault *impl);
371 static void home_folder_handler (GtkFileChooserDefault *impl);
372 static void desktop_folder_handler (GtkFileChooserDefault *impl);
373 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
374 gint bookmark_index);
375 static void show_hidden_handler (GtkFileChooserDefault *impl);
376 static void search_shortcut_handler (GtkFileChooserDefault *impl);
377 static void recent_shortcut_handler (GtkFileChooserDefault *impl);
378 static void update_appearance (GtkFileChooserDefault *impl);
380 static void set_current_filter (GtkFileChooserDefault *impl,
381 GtkFileFilter *filter);
382 static void check_preview_change (GtkFileChooserDefault *impl);
384 static void filter_combo_changed (GtkComboBox *combo_box,
385 GtkFileChooserDefault *impl);
386 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
388 GtkTreeViewColumn *column,
389 GtkFileChooserDefault *impl);
391 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
393 GtkFileChooserDefault *impl);
395 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
398 gboolean path_currently_selected,
400 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
402 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
404 static int shortcuts_get_index (GtkFileChooserDefault *impl,
405 ShortcutsIndex where);
406 static int shortcut_find_position (GtkFileChooserDefault *impl,
407 const GtkFilePath *path);
409 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
411 static gboolean list_select_func (GtkTreeSelection *selection,
414 gboolean path_currently_selected,
417 static void list_selection_changed (GtkTreeSelection *tree_selection,
418 GtkFileChooserDefault *impl);
419 static void list_row_activated (GtkTreeView *tree_view,
421 GtkTreeViewColumn *column,
422 GtkFileChooserDefault *impl);
424 static void select_func (GtkFileSystemModel *model,
429 static void path_bar_clicked (GtkPathBar *path_bar,
430 GtkFilePath *file_path,
431 GtkFilePath *child_path,
432 gboolean child_is_hidden,
433 GtkFileChooserDefault *impl);
435 static void add_bookmark_button_clicked_cb (GtkButton *button,
436 GtkFileChooserDefault *impl);
437 static void remove_bookmark_button_clicked_cb (GtkButton *button,
438 GtkFileChooserDefault *impl);
439 static void save_folder_combo_changed_cb (GtkComboBox *combo,
440 GtkFileChooserDefault *impl);
442 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
443 GtkCellRenderer *cell,
444 GtkTreeModel *tree_model,
447 static void list_name_data_func (GtkTreeViewColumn *tree_column,
448 GtkCellRenderer *cell,
449 GtkTreeModel *tree_model,
453 static void list_size_data_func (GtkTreeViewColumn *tree_column,
454 GtkCellRenderer *cell,
455 GtkTreeModel *tree_model,
459 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
460 GtkCellRenderer *cell,
461 GtkTreeModel *tree_model,
465 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
468 static void load_remove_timer (GtkFileChooserDefault *impl);
469 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
471 static void location_button_toggled_cb (GtkToggleButton *toggle,
472 GtkFileChooserDefault *impl);
473 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
475 static void search_stop_searching (GtkFileChooserDefault *impl,
476 gboolean remove_query);
477 static void search_clear_model (GtkFileChooserDefault *impl,
478 gboolean remove_from_treeview);
479 static gboolean search_should_respond (GtkFileChooserDefault *impl);
480 static void search_switch_to_browse_mode (GtkFileChooserDefault *impl);
481 static GSList *search_get_selected_paths (GtkFileChooserDefault *impl);
482 static void search_entry_activate_cb (GtkEntry *entry,
485 static void recent_manager_update (GtkFileChooserDefault *impl);
486 static void recent_stop_loading (GtkFileChooserDefault *impl);
487 static void recent_clear_model (GtkFileChooserDefault *impl,
488 gboolean remove_from_treeview);
489 static gboolean recent_should_respond (GtkFileChooserDefault *impl);
490 static void recent_switch_to_browse_mode (GtkFileChooserDefault *impl);
491 static GSList * recent_get_selected_paths (GtkFileChooserDefault *impl);
495 /* Drag and drop interface declarations */
498 GtkTreeModelFilter parent;
500 GtkFileChooserDefault *impl;
501 } ShortcutsPaneModelFilter;
504 GtkTreeModelFilterClass parent_class;
505 } ShortcutsPaneModelFilterClass;
507 #define SHORTCUTS_PANE_MODEL_FILTER_TYPE (_shortcuts_pane_model_filter_get_type ())
508 #define SHORTCUTS_PANE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_PANE_MODEL_FILTER_TYPE, ShortcutsPaneModelFilter))
510 static void shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
512 G_DEFINE_TYPE_WITH_CODE (ShortcutsPaneModelFilter,
513 _shortcuts_pane_model_filter,
514 GTK_TYPE_TREE_MODEL_FILTER,
515 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
516 shortcuts_pane_model_filter_drag_source_iface_init))
518 static GtkTreeModel *shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
519 GtkTreeModel *child_model,
524 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
525 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
526 gtk_file_chooser_default_iface_init)
527 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
528 gtk_file_chooser_embed_default_iface_init));
531 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
533 static const guint quick_bookmark_keyvals[10] = {
534 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
536 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
537 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
538 GtkBindingSet *binding_set;
541 gobject_class->finalize = gtk_file_chooser_default_finalize;
542 gobject_class->constructor = gtk_file_chooser_default_constructor;
543 gobject_class->set_property = gtk_file_chooser_default_set_property;
544 gobject_class->get_property = gtk_file_chooser_default_get_property;
545 gobject_class->dispose = gtk_file_chooser_default_dispose;
547 widget_class->show_all = gtk_file_chooser_default_show_all;
548 widget_class->map = gtk_file_chooser_default_map;
549 widget_class->unmap = gtk_file_chooser_default_unmap;
550 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
551 widget_class->style_set = gtk_file_chooser_default_style_set;
552 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
554 signals[LOCATION_POPUP] =
555 _gtk_binding_signal_new (I_("location-popup"),
556 G_OBJECT_CLASS_TYPE (class),
557 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
558 G_CALLBACK (location_popup_handler),
560 _gtk_marshal_VOID__STRING,
561 G_TYPE_NONE, 1, G_TYPE_STRING);
562 signals[LOCATION_POPUP_ON_PASTE] =
563 _gtk_binding_signal_new ("location-popup-on-paste",
564 G_OBJECT_CLASS_TYPE (class),
565 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
566 G_CALLBACK (location_popup_on_paste_handler),
568 _gtk_marshal_VOID__VOID,
570 signals[LOCATION_TOGGLE_POPUP] =
571 _gtk_binding_signal_new (I_("location-toggle-popup"),
572 G_OBJECT_CLASS_TYPE (class),
573 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
574 G_CALLBACK (location_toggle_popup_handler),
576 _gtk_marshal_VOID__VOID,
579 _gtk_binding_signal_new (I_("up-folder"),
580 G_OBJECT_CLASS_TYPE (class),
581 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
582 G_CALLBACK (up_folder_handler),
584 _gtk_marshal_VOID__VOID,
586 signals[DOWN_FOLDER] =
587 _gtk_binding_signal_new (I_("down-folder"),
588 G_OBJECT_CLASS_TYPE (class),
589 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
590 G_CALLBACK (down_folder_handler),
592 _gtk_marshal_VOID__VOID,
594 signals[HOME_FOLDER] =
595 _gtk_binding_signal_new (I_("home-folder"),
596 G_OBJECT_CLASS_TYPE (class),
597 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
598 G_CALLBACK (home_folder_handler),
600 _gtk_marshal_VOID__VOID,
602 signals[DESKTOP_FOLDER] =
603 _gtk_binding_signal_new (I_("desktop-folder"),
604 G_OBJECT_CLASS_TYPE (class),
605 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
606 G_CALLBACK (desktop_folder_handler),
608 _gtk_marshal_VOID__VOID,
610 signals[QUICK_BOOKMARK] =
611 _gtk_binding_signal_new (I_("quick-bookmark"),
612 G_OBJECT_CLASS_TYPE (class),
613 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
614 G_CALLBACK (quick_bookmark_handler),
616 _gtk_marshal_VOID__INT,
617 G_TYPE_NONE, 1, G_TYPE_INT);
618 signals[SHOW_HIDDEN] =
619 _gtk_binding_signal_new ("show-hidden",
620 G_OBJECT_CLASS_TYPE (class),
621 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
622 G_CALLBACK (show_hidden_handler),
624 _gtk_marshal_VOID__VOID,
626 signals[SEARCH_SHORTCUT] =
627 _gtk_binding_signal_new ("search-shortcut",
628 G_OBJECT_CLASS_TYPE (class),
629 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
630 G_CALLBACK (search_shortcut_handler),
632 _gtk_marshal_VOID__VOID,
634 signals[RECENT_SHORTCUT] =
635 _gtk_binding_signal_new ("recent-shortcut",
636 G_OBJECT_CLASS_TYPE (class),
637 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
638 G_CALLBACK (recent_shortcut_handler),
640 _gtk_marshal_VOID__VOID,
643 binding_set = gtk_binding_set_by_class (class);
645 gtk_binding_entry_add_signal (binding_set,
646 GDK_l, GDK_CONTROL_MASK,
647 "location-toggle-popup",
650 gtk_binding_entry_add_signal (binding_set,
653 1, G_TYPE_STRING, "/");
654 gtk_binding_entry_add_signal (binding_set,
657 1, G_TYPE_STRING, "/");
660 gtk_binding_entry_add_signal (binding_set,
663 1, G_TYPE_STRING, "~");
666 gtk_binding_entry_add_signal (binding_set,
667 GDK_v, GDK_CONTROL_MASK,
668 "location-popup-on-paste",
670 gtk_binding_entry_add_signal (binding_set,
671 GDK_Up, GDK_MOD1_MASK,
674 gtk_binding_entry_add_signal (binding_set,
678 gtk_binding_entry_add_signal (binding_set,
679 GDK_KP_Up, GDK_MOD1_MASK,
683 gtk_binding_entry_add_signal (binding_set,
684 GDK_Down, GDK_MOD1_MASK,
687 gtk_binding_entry_add_signal (binding_set,
688 GDK_KP_Down, GDK_MOD1_MASK,
692 gtk_binding_entry_add_signal (binding_set,
693 GDK_Home, GDK_MOD1_MASK,
696 gtk_binding_entry_add_signal (binding_set,
697 GDK_KP_Home, GDK_MOD1_MASK,
700 gtk_binding_entry_add_signal (binding_set,
701 GDK_d, GDK_MOD1_MASK,
704 gtk_binding_entry_add_signal (binding_set,
705 GDK_h, GDK_CONTROL_MASK,
708 gtk_binding_entry_add_signal (binding_set,
709 GDK_s, GDK_MOD1_MASK,
712 gtk_binding_entry_add_signal (binding_set,
713 GDK_r, GDK_MOD1_MASK,
717 for (i = 0; i < 10; i++)
718 gtk_binding_entry_add_signal (binding_set,
719 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
723 _gtk_file_chooser_install_properties (gobject_class);
727 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
729 iface->select_path = gtk_file_chooser_default_select_path;
730 iface->unselect_path = gtk_file_chooser_default_unselect_path;
731 iface->select_all = gtk_file_chooser_default_select_all;
732 iface->unselect_all = gtk_file_chooser_default_unselect_all;
733 iface->get_paths = gtk_file_chooser_default_get_paths;
734 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
735 iface->get_file_system = gtk_file_chooser_default_get_file_system;
736 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
737 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
738 iface->set_current_name = gtk_file_chooser_default_set_current_name;
739 iface->add_filter = gtk_file_chooser_default_add_filter;
740 iface->remove_filter = gtk_file_chooser_default_remove_filter;
741 iface->list_filters = gtk_file_chooser_default_list_filters;
742 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
743 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
744 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
748 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
750 iface->get_default_size = gtk_file_chooser_default_get_default_size;
751 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
752 iface->should_respond = gtk_file_chooser_default_should_respond;
753 iface->initial_focus = gtk_file_chooser_default_initial_focus;
757 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
759 profile_start ("start", NULL);
760 #ifdef PROFILE_FILE_CHOOSER
761 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
763 impl->local_only = TRUE;
764 impl->preview_widget_active = TRUE;
765 impl->use_preview_label = TRUE;
766 impl->select_multiple = FALSE;
767 impl->show_hidden = FALSE;
768 impl->icon_size = FALLBACK_ICON_SIZE;
769 impl->load_state = LOAD_EMPTY;
770 impl->reload_state = RELOAD_EMPTY;
771 impl->pending_select_paths = NULL;
772 impl->location_mode = LOCATION_MODE_PATH_BAR;
773 impl->operation_mode = OPERATION_MODE_BROWSE;
775 gtk_box_set_spacing (GTK_BOX (impl), 12);
777 impl->tooltips = gtk_tooltips_new ();
778 g_object_ref_sink (impl->tooltips);
780 profile_end ("end", NULL);
783 /* Frees the data columns for the specified iter in the shortcuts model*/
785 shortcuts_free_row_data (GtkFileChooserDefault *impl,
789 ShortcutType shortcut_type;
790 GtkFileSystemHandle *handle;
792 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
793 SHORTCUTS_COL_DATA, &col_data,
794 SHORTCUTS_COL_TYPE, &shortcut_type,
795 SHORTCUTS_COL_HANDLE, &handle,
799 gtk_file_system_cancel_operation (handle);
801 if (!(shortcut_type == SHORTCUT_TYPE_PATH ||
802 shortcut_type == SHORTCUT_TYPE_VOLUME) ||
806 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
808 GtkFileSystemVolume *volume;
811 gtk_file_system_volume_free (impl->file_system, volume);
817 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
820 gtk_file_path_free (path);
824 /* Frees all the data columns in the shortcuts model */
826 shortcuts_free (GtkFileChooserDefault *impl)
830 if (!impl->shortcuts_model)
833 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
836 shortcuts_free_row_data (impl, &iter);
838 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
840 g_object_unref (impl->shortcuts_model);
841 impl->shortcuts_model = NULL;
845 pending_select_paths_free (GtkFileChooserDefault *impl)
849 for (l = impl->pending_select_paths; l; l = l->next)
854 gtk_file_path_free (path);
857 g_slist_free (impl->pending_select_paths);
858 impl->pending_select_paths = NULL;
862 pending_select_paths_add (GtkFileChooserDefault *impl,
863 const GtkFilePath *path)
865 impl->pending_select_paths =
866 g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
869 /* Used from gtk_tree_selection_selected_foreach() */
871 store_selection_foreach (GtkTreeModel *model,
876 GtkFileChooserDefault *impl;
877 GtkTreeIter child_iter;
878 const GtkFilePath *file_path;
880 impl = GTK_FILE_CHOOSER_DEFAULT (data);
882 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
884 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
885 pending_select_paths_add (impl, file_path);
888 /* Stores the current selection in the list of paths to select; this is used to
889 * preserve the selection when reloading the current folder.
892 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
894 GtkTreeSelection *selection;
896 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
897 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
901 gtk_file_chooser_default_finalize (GObject *object)
903 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
906 if (impl->shortcuts_pane_filter_model)
907 g_object_unref (impl->shortcuts_pane_filter_model);
909 if (impl->shortcuts_combo_filter_model)
910 g_object_unref (impl->shortcuts_combo_filter_model);
912 shortcuts_free (impl);
914 g_object_unref (impl->file_system);
916 g_free (impl->browse_files_last_selected_name);
918 for (l = impl->filters; l; l = l->next)
920 GtkFileFilter *filter;
922 filter = GTK_FILE_FILTER (l->data);
923 g_object_unref (filter);
925 g_slist_free (impl->filters);
927 if (impl->current_filter)
928 g_object_unref (impl->current_filter);
930 if (impl->current_volume_path)
931 gtk_file_path_free (impl->current_volume_path);
933 if (impl->current_folder)
934 gtk_file_path_free (impl->current_folder);
936 if (impl->preview_path)
937 gtk_file_path_free (impl->preview_path);
939 load_remove_timer (impl);
941 /* Free all the Models we have */
942 if (impl->browse_files_model)
943 g_object_unref (impl->browse_files_model);
945 if (impl->sort_model)
946 g_object_unref (impl->sort_model);
948 search_clear_model (impl, FALSE);
949 recent_clear_model (impl, FALSE);
951 g_free (impl->preview_display_name);
953 g_free (impl->edited_new_text);
955 g_object_unref (impl->tooltips);
957 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
960 /* Shows an error dialog set as transient for the specified window */
962 error_message_with_parent (GtkWindow *parent,
968 dialog = gtk_message_dialog_new (parent,
969 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
974 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
978 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
980 gtk_dialog_run (GTK_DIALOG (dialog));
981 gtk_widget_destroy (dialog);
984 /* Returns a toplevel GtkWindow, or NULL if none */
986 get_toplevel (GtkWidget *widget)
990 toplevel = gtk_widget_get_toplevel (widget);
991 if (!GTK_WIDGET_TOPLEVEL (toplevel))
994 return GTK_WINDOW (toplevel);
997 /* Shows an error dialog for the file chooser */
999 error_message (GtkFileChooserDefault *impl,
1003 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
1006 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
1008 error_dialog (GtkFileChooserDefault *impl,
1010 const GtkFilePath *path,
1013 if (G_LIKELY (error))
1019 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1020 text = g_strdup_printf (msg, uri);
1021 error_message (impl, text, error->message);
1024 g_error_free (error);
1028 /* Displays an error message about not being able to get information for a file.
1029 * Frees the GError as well.
1032 error_getting_info_dialog (GtkFileChooserDefault *impl,
1033 const GtkFilePath *path,
1037 _("Could not retrieve information about the file"),
1041 /* Shows an error dialog about not being able to add a bookmark */
1043 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1044 const GtkFilePath *path,
1048 _("Could not add a bookmark"),
1052 /* Shows an error dialog about not being able to remove a bookmark */
1054 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1055 const GtkFilePath *path,
1059 _("Could not remove bookmark"),
1063 /* Shows an error dialog about not being able to create a folder */
1065 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1066 const GtkFilePath *path,
1070 _("The folder could not be created"),
1074 /* Shows an error about not being able to create a folder because a file with
1075 * the same name is already there.
1078 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1079 const GtkFilePath *path,
1083 _("The folder could not be created, as a file with the same "
1084 "name already exists. Try using a different name for the "
1085 "folder, or rename the file first."),
1089 /* Shows an error dialog about not being able to create a filename */
1091 error_building_filename_dialog (GtkFileChooserDefault *impl,
1092 const GtkFilePath *folder_part,
1093 const char *file_part,
1096 error_dialog (impl, _("Invalid file name"),
1100 /* Shows an error dialog when we cannot switch to a folder */
1102 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1103 const GtkFilePath *path,
1106 error_dialog (impl, _("The folder contents could not be displayed"),
1110 /* Changes folders, displaying an error dialog if this fails */
1112 change_folder_and_display_error (GtkFileChooserDefault *impl,
1113 const GtkFilePath *path,
1114 gboolean clear_entry)
1118 GtkFilePath *path_copy;
1120 g_return_val_if_fail (path != NULL, FALSE);
1122 profile_start ("start", (char *) path);
1124 /* We copy the path because of this case:
1126 * list_row_activated()
1127 * fetches path from model; path belongs to the model (*)
1128 * calls change_folder_and_display_error()
1129 * calls _gtk_file_chooser_set_current_folder_path()
1130 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1133 path_copy = gtk_file_path_copy (path);
1136 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), path_copy, TRUE, clear_entry, &error);
1139 error_changing_folder_dialog (impl, path_copy, error);
1141 gtk_file_path_free (path_copy);
1143 profile_end ("end", (char *) path);
1149 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1151 if (impl->use_preview_label)
1153 if (!impl->preview_label)
1155 impl->preview_label = gtk_label_new (impl->preview_display_name);
1156 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1157 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1158 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1159 gtk_widget_show (impl->preview_label);
1164 if (impl->preview_label)
1166 gtk_widget_destroy (impl->preview_label);
1167 impl->preview_label = NULL;
1171 if (impl->preview_widget_active && impl->preview_widget)
1172 gtk_widget_show (impl->preview_box);
1174 gtk_widget_hide (impl->preview_box);
1176 g_signal_emit_by_name (impl, "default-size-changed");
1180 set_preview_widget (GtkFileChooserDefault *impl,
1181 GtkWidget *preview_widget)
1183 if (preview_widget == impl->preview_widget)
1186 if (impl->preview_widget)
1187 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1188 impl->preview_widget);
1190 impl->preview_widget = preview_widget;
1191 if (impl->preview_widget)
1193 gtk_widget_show (impl->preview_widget);
1194 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1195 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1196 impl->preview_widget,
1197 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1200 update_preview_widget_visibility (impl);
1203 /* Renders a "Search" icon at an appropriate size for a tree view */
1205 render_search_icon (GtkFileChooserDefault *impl)
1207 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FIND, GTK_ICON_SIZE_MENU, NULL);
1211 render_recent_icon (GtkFileChooserDefault *impl)
1213 return gtk_widget_render_icon (GTK_WIDGET (impl), GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
1217 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1218 struct ReloadIconsData
1220 GtkFileChooserDefault *impl;
1221 GtkTreeRowReference *row_ref;
1225 shortcuts_reload_icons_get_info_cb (GtkFileSystemHandle *handle,
1226 const GtkFileInfo *info,
1227 const GError *error,
1233 gboolean cancelled = handle->cancelled;
1234 struct ReloadIconsData *data = user_data;
1236 if (!g_slist_find (data->impl->reload_icon_handles, handle))
1239 data->impl->reload_icon_handles = g_slist_remove (data->impl->reload_icon_handles, handle);
1241 if (cancelled || error)
1244 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->impl),
1245 data->impl->icon_size, NULL);
1247 path = gtk_tree_row_reference_get_path (data->row_ref);
1248 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1249 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1250 SHORTCUTS_COL_PIXBUF, pixbuf,
1252 gtk_tree_path_free (path);
1255 g_object_unref (pixbuf);
1258 gtk_tree_row_reference_free (data->row_ref);
1259 g_object_unref (data->impl);
1262 g_object_unref (handle);
1266 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1271 profile_start ("start", NULL);
1273 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1276 for (l = impl->reload_icon_handles; l; l = l->next)
1278 GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1279 gtk_file_system_cancel_operation (handle);
1281 g_slist_free (impl->reload_icon_handles);
1282 impl->reload_icon_handles = NULL;
1287 ShortcutType shortcut_type;
1288 gboolean pixbuf_visible;
1291 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1292 SHORTCUTS_COL_DATA, &data,
1293 SHORTCUTS_COL_TYPE, &shortcut_type,
1294 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1300 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1302 GtkFileSystemVolume *volume;
1305 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1306 impl->icon_size, NULL);
1308 gtk_list_store_set (impl->shortcuts_model, &iter,
1309 SHORTCUTS_COL_PIXBUF, pixbuf,
1313 g_object_unref (pixbuf);
1315 else if (shortcut_type == SHORTCUT_TYPE_PATH)
1317 if (gtk_file_system_path_is_local (impl->file_system, (GtkFilePath *)data))
1319 const GtkFilePath *path;
1320 struct ReloadIconsData *info;
1321 GtkTreePath *tree_path;
1322 GtkFileSystemHandle *handle;
1326 info = g_new0 (struct ReloadIconsData, 1);
1327 info->impl = g_object_ref (impl);
1328 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1329 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1330 gtk_tree_path_free (tree_path);
1332 handle = gtk_file_system_get_info (impl->file_system, path,
1334 shortcuts_reload_icons_get_info_cb,
1336 impl->reload_icon_handles = g_slist_append (impl->reload_icon_handles, handle);
1340 GtkIconTheme *icon_theme;
1342 /* Don't call get_info for remote paths to avoid latency and
1344 * If we switch to a better bookmarks file format (XBEL), we
1345 * should use mime info to get a better icon.
1347 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1348 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory",
1349 impl->icon_size, 0, NULL);
1351 gtk_list_store_set (impl->shortcuts_model, &iter,
1352 SHORTCUTS_COL_PIXBUF, pixbuf,
1356 g_object_unref (pixbuf);
1359 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
1361 pixbuf = render_search_icon (impl);
1363 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
1365 pixbuf = render_recent_icon (impl);
1369 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1373 profile_end ("end", NULL);
1377 shortcuts_find_folder (GtkFileChooserDefault *impl,
1378 GtkFilePath *folder)
1380 GtkTreeSelection *selection;
1384 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1386 g_assert (folder != NULL);
1387 pos = shortcut_find_position (impl, folder);
1390 gtk_tree_selection_unselect_all (selection);
1394 path = gtk_tree_path_new_from_indices (pos, -1);
1395 gtk_tree_selection_select_path (selection, path);
1396 gtk_tree_path_free (path);
1399 /* If a shortcut corresponds to the current folder, selects it */
1401 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1403 shortcuts_find_folder (impl, impl->current_folder);
1406 /* Removes the specified number of rows from the shortcuts list */
1408 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1414 path = gtk_tree_path_new_from_indices (start_row, -1);
1416 for (; n_rows; n_rows--)
1420 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1421 g_assert_not_reached ();
1423 shortcuts_free_row_data (impl, &iter);
1424 gtk_list_store_remove (impl->shortcuts_model, &iter);
1427 gtk_tree_path_free (path);
1431 shortcuts_update_count (GtkFileChooserDefault *impl,
1432 ShortcutsIndex type,
1437 case SHORTCUTS_HOME:
1439 impl->has_home = FALSE;
1441 impl->has_home = TRUE;
1444 case SHORTCUTS_DESKTOP:
1446 impl->has_desktop = FALSE;
1448 impl->has_desktop = TRUE;
1451 case SHORTCUTS_VOLUMES:
1452 impl->num_volumes += value;
1455 case SHORTCUTS_SHORTCUTS:
1456 impl->num_shortcuts += value;
1459 case SHORTCUTS_BOOKMARKS:
1460 impl->num_bookmarks += value;
1463 case SHORTCUTS_CURRENT_FOLDER:
1465 impl->shortcuts_current_folder_active = FALSE;
1467 impl->shortcuts_current_folder_active = TRUE;
1476 struct ShortcutsInsertRequest
1478 GtkFileChooserDefault *impl;
1479 GtkFilePath *parent_path;
1483 GtkTreeRowReference *row_ref;
1484 ShortcutsIndex type;
1490 get_file_info_finished (GtkFileSystemHandle *handle,
1491 const GtkFileInfo *info,
1492 const GError *error,
1496 gboolean cancelled = handle->cancelled;
1500 GtkFileSystemHandle *model_handle;
1501 struct ShortcutsInsertRequest *request = data;
1503 path = gtk_tree_row_reference_get_path (request->row_ref);
1505 /* Handle doesn't exist anymore in the model */
1508 pos = gtk_tree_path_get_indices (path)[0];
1509 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1511 gtk_tree_path_free (path);
1513 /* validate handle, else goto out */
1514 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1515 SHORTCUTS_COL_HANDLE, &model_handle,
1517 if (handle != model_handle)
1520 /* set the handle to NULL in the model (we unref later on) */
1521 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1522 SHORTCUTS_COL_HANDLE, NULL,
1530 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1531 shortcuts_update_count (request->impl, request->type, -1);
1533 if (request->type == SHORTCUTS_HOME)
1535 const char *home = g_get_home_dir ();
1536 GtkFilePath *home_path;
1538 home_path = gtk_file_system_filename_to_path (request->impl->file_system, home);
1539 error_getting_info_dialog (request->impl, home_path, g_error_copy (error));
1540 gtk_file_path_free (home_path);
1542 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1544 /* Remove the current folder separator */
1545 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1546 shortcuts_remove_rows (request->impl, separator_pos, 1);
1552 if (!request->label_copy)
1553 request->label_copy = g_strdup (gtk_file_info_get_display_name (info));
1554 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1555 request->impl->icon_size, NULL);
1557 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1558 SHORTCUTS_COL_PIXBUF, pixbuf,
1559 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1560 SHORTCUTS_COL_NAME, request->label_copy,
1561 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_PATH,
1562 SHORTCUTS_COL_REMOVABLE, request->removable,
1565 if (request->impl->shortcuts_pane_filter_model)
1566 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_pane_filter_model));
1568 if (request->impl->shortcuts_combo_filter_model)
1569 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_combo_filter_model));
1571 if (request->type == SHORTCUTS_CURRENT_FOLDER
1572 && request->impl->save_folder_combo != NULL)
1574 /* The current folder is updated via _activate_iter(), don't
1575 * have save_folder_combo_changed_cb() call _activate_iter()
1578 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1579 G_CALLBACK (save_folder_combo_changed_cb),
1581 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), request->impl->has_search ? pos - 2 : pos);
1582 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1583 G_CALLBACK (save_folder_combo_changed_cb),
1588 g_object_unref (pixbuf);
1591 g_object_unref (request->impl);
1592 gtk_file_path_free (request->parent_path);
1593 gtk_file_path_free (request->path);
1594 gtk_tree_row_reference_free (request->row_ref);
1595 g_free (request->label_copy);
1598 g_object_unref (handle);
1601 /* FIXME: GtkFileSystem needs a function to split a remote path
1602 * into hostname and path components, or maybe just have a
1603 * gtk_file_system_path_get_display_name().
1605 * This function is also used in gtkfilechooserbutton.c
1608 _gtk_file_chooser_label_for_uri (const gchar *uri)
1610 const gchar *path, *start, *end, *p;
1611 gchar *host, *label;
1613 start = strstr (uri, "://");
1615 path = strchr (start, '/');
1621 end = uri + strlen (uri);
1625 /* strip username */
1626 p = strchr (start, '@');
1632 p = strchr (start, ':');
1636 host = g_strndup (start, end - start);
1638 /* Translators: the first string is a path and the second string
1639 * is a hostname. Nautilus and the panel contain the same string
1642 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1649 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1650 * inserts a volume. A position of -1 indicates the end of the tree.
1653 shortcuts_insert_path (GtkFileChooserDefault *impl,
1655 ShortcutType shortcut_type,
1656 GtkFileSystemVolume *volume,
1657 const GtkFilePath *path,
1660 ShortcutsIndex type)
1663 GdkPixbuf *pixbuf = NULL;
1664 gpointer data = NULL;
1666 GtkIconTheme *icon_theme;
1668 profile_start ("start", (shortcut_type == SHORTCUT_TYPE_VOLUME) ? "volume"
1669 : ((shortcut_type == SHORTCUT_TYPE_PATH) ? (char *) path : NULL));
1671 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
1674 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1675 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1676 impl->icon_size, NULL);
1678 else if (shortcut_type == SHORTCUT_TYPE_PATH)
1680 if (gtk_file_system_path_is_local (impl->file_system, path))
1682 struct ShortcutsInsertRequest *request;
1683 GtkFileSystemHandle *handle;
1686 request = g_new0 (struct ShortcutsInsertRequest, 1);
1687 request->impl = g_object_ref (impl);
1688 request->path = gtk_file_path_copy (path);
1689 request->name_only = TRUE;
1690 request->removable = removable;
1692 request->type = type;
1694 request->label_copy = g_strdup (label);
1697 gtk_list_store_append (impl->shortcuts_model, &iter);
1699 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1701 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1702 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1703 gtk_tree_path_free (p);
1705 handle = gtk_file_system_get_info (request->impl->file_system, request->path,
1706 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN | GTK_FILE_INFO_ICON,
1707 get_file_info_finished, request);
1709 gtk_list_store_set (impl->shortcuts_model, &iter,
1710 SHORTCUTS_COL_DATA, gtk_file_path_copy (path),
1711 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_PATH,
1712 SHORTCUTS_COL_HANDLE, handle,
1715 shortcuts_update_count (impl, type, 1);
1721 /* Don't call get_info for remote paths to avoid latency and
1724 data = gtk_file_path_copy (path);
1726 label_copy = g_strdup (label);
1731 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1733 label_copy = _gtk_file_chooser_label_for_uri (uri);
1738 /* If we switch to a better bookmarks file format (XBEL), we
1739 * should use mime info to get a better icon.
1741 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1742 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory",
1743 impl->icon_size, 0, NULL);
1748 g_assert_not_reached ();
1754 gtk_list_store_append (impl->shortcuts_model, &iter);
1756 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1758 shortcuts_update_count (impl, type, 1);
1760 gtk_list_store_set (impl->shortcuts_model, &iter,
1761 SHORTCUTS_COL_PIXBUF, pixbuf,
1762 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1763 SHORTCUTS_COL_NAME, label_copy,
1764 SHORTCUTS_COL_DATA, data,
1765 SHORTCUTS_COL_TYPE, shortcut_type,
1766 SHORTCUTS_COL_REMOVABLE, removable,
1767 SHORTCUTS_COL_HANDLE, NULL,
1770 if (impl->shortcuts_pane_filter_model)
1771 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
1773 if (impl->shortcuts_combo_filter_model)
1774 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
1776 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1778 /* The current folder is updated via _activate_iter(), don't
1779 * have save_folder_combo_changed_cb() call _activate_iter()
1782 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1783 g_signal_handlers_block_by_func (impl->save_folder_combo,
1784 G_CALLBACK (save_folder_combo_changed_cb),
1786 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
1787 impl->has_search ? combo_pos - 2 : combo_pos);
1788 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1789 G_CALLBACK (save_folder_combo_changed_cb),
1793 g_free (label_copy);
1796 g_object_unref (pixbuf);
1798 profile_end ("end", NULL);
1802 shortcuts_append_search (GtkFileChooserDefault *impl)
1807 pixbuf = render_search_icon (impl);
1809 gtk_list_store_append (impl->shortcuts_model, &iter);
1810 gtk_list_store_set (impl->shortcuts_model, &iter,
1811 SHORTCUTS_COL_PIXBUF, pixbuf,
1812 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1813 SHORTCUTS_COL_NAME, _("Search"),
1814 SHORTCUTS_COL_DATA, NULL,
1815 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEARCH,
1816 SHORTCUTS_COL_REMOVABLE, FALSE,
1820 g_object_unref (pixbuf);
1822 impl->has_search = TRUE;
1826 shortcuts_append_recent (GtkFileChooserDefault *impl)
1831 pixbuf = render_recent_icon (impl);
1833 gtk_list_store_append (impl->shortcuts_model, &iter);
1834 gtk_list_store_set (impl->shortcuts_model, &iter,
1835 SHORTCUTS_COL_PIXBUF, pixbuf,
1836 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1837 SHORTCUTS_COL_NAME, _("Recently Used"),
1838 SHORTCUTS_COL_DATA, NULL,
1839 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_RECENT,
1840 SHORTCUTS_COL_REMOVABLE, FALSE,
1844 g_object_unref (pixbuf);
1846 impl->has_recent = TRUE;
1849 /* Appends an item for the user's home directory to the shortcuts model */
1851 shortcuts_append_home (GtkFileChooserDefault *impl)
1854 GtkFilePath *home_path;
1856 profile_start ("start", NULL);
1858 home = g_get_home_dir ();
1861 profile_end ("end - no home directory!?", NULL);
1865 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1867 shortcuts_insert_path (impl, -1, SHORTCUT_TYPE_PATH, NULL, home_path, NULL, FALSE, SHORTCUTS_HOME);
1868 impl->has_home = TRUE;
1870 gtk_file_path_free (home_path);
1872 profile_end ("end", NULL);
1875 /* Appends the ~/Desktop directory to the shortcuts model */
1877 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1883 profile_start ("start", NULL);
1886 name = _gtk_file_system_win32_get_desktop ();
1888 home = g_get_home_dir ();
1891 profile_end ("end - no home directory!?", NULL);
1895 name = g_build_filename (home, "Desktop", NULL);
1898 path = gtk_file_system_filename_to_path (impl->file_system, name);
1901 shortcuts_insert_path (impl, -1, SHORTCUT_TYPE_PATH, NULL, path, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1902 impl->has_desktop = TRUE;
1904 /* We do not actually pop up an error dialog if there is no desktop directory
1905 * because some people may really not want to have one.
1908 gtk_file_path_free (path);
1910 profile_end ("end", NULL);
1913 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1915 shortcuts_append_paths (GtkFileChooserDefault *impl,
1922 profile_start ("start", NULL);
1924 /* As there is no separator now, we want to start there.
1926 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1929 for (; paths; paths = paths->next)
1935 if (impl->local_only &&
1936 !gtk_file_system_path_is_local (impl->file_system, path))
1939 label = gtk_file_system_get_bookmark_label (impl->file_system, path);
1941 /* NULL GError, but we don't really want to show error boxes here */
1942 shortcuts_insert_path (impl, start_row + num_inserted, SHORTCUT_TYPE_PATH, NULL, path, label, TRUE, SHORTCUTS_BOOKMARKS);
1948 profile_end ("end", NULL);
1950 return num_inserted;
1953 /* Returns the index for the corresponding item in the shortcuts bar */
1955 shortcuts_get_index (GtkFileChooserDefault *impl,
1956 ShortcutsIndex where)
1962 if (where == SHORTCUTS_SEARCH)
1965 n += impl->has_search ? 1 : 0;
1967 if (where == SHORTCUTS_SEARCH_SEPARATOR)
1970 n += impl->has_search ? 1 : 0;
1972 if (where == SHORTCUTS_RECENT)
1975 n += impl->has_recent ? 1 : 0;
1977 if (where == SHORTCUTS_RECENT_SEPARATOR)
1980 n += impl->has_recent ? 1 : 0;
1982 if (where == SHORTCUTS_HOME)
1985 n += impl->has_home ? 1 : 0;
1987 if (where == SHORTCUTS_DESKTOP)
1990 n += impl->has_desktop ? 1 : 0;
1992 if (where == SHORTCUTS_VOLUMES)
1995 n += impl->num_volumes;
1997 if (where == SHORTCUTS_SHORTCUTS)
2000 n += impl->num_shortcuts;
2002 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
2005 /* If there are no bookmarks there won't be a separator */
2006 n += (impl->num_bookmarks > 0) ? 1 : 0;
2008 if (where == SHORTCUTS_BOOKMARKS)
2011 n += impl->num_bookmarks;
2013 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
2018 if (where == SHORTCUTS_CURRENT_FOLDER)
2021 g_assert_not_reached ();
2028 /* Adds all the file system volumes to the shortcuts model */
2030 shortcuts_add_volumes (GtkFileChooserDefault *impl)
2035 gboolean old_changing_folders;
2037 profile_start ("start", NULL);
2040 old_changing_folders = impl->changing_folder;
2041 impl->changing_folder = TRUE;
2043 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
2044 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
2045 impl->num_volumes = 0;
2047 list = gtk_file_system_list_volumes (impl->file_system);
2051 for (l = list; l; l = l->next)
2053 GtkFileSystemVolume *volume;
2057 if (impl->local_only)
2059 if (gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
2061 GtkFilePath *base_path;
2063 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2064 if (base_path != NULL)
2066 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
2067 gtk_file_path_free (base_path);
2071 gtk_file_system_volume_free (impl->file_system, volume);
2078 shortcuts_insert_path (impl, start_row + n, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
2082 impl->num_volumes = n;
2083 g_slist_free (list);
2085 if (impl->shortcuts_pane_filter_model)
2086 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2088 if (impl->shortcuts_combo_filter_model)
2089 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2091 impl->changing_folder = old_changing_folders;
2093 profile_end ("end", NULL);
2096 /* Inserts a separator node in the shortcuts list */
2098 shortcuts_insert_separator (GtkFileChooserDefault *impl,
2099 ShortcutsIndex where)
2103 g_assert (where == SHORTCUTS_SEARCH_SEPARATOR ||
2104 where == SHORTCUTS_RECENT_SEPARATOR ||
2105 where == SHORTCUTS_BOOKMARKS_SEPARATOR ||
2106 where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2108 gtk_list_store_insert (impl->shortcuts_model, &iter,
2109 shortcuts_get_index (impl, where));
2110 gtk_list_store_set (impl->shortcuts_model, &iter,
2111 SHORTCUTS_COL_PIXBUF, NULL,
2112 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
2113 SHORTCUTS_COL_NAME, NULL,
2114 SHORTCUTS_COL_DATA, NULL,
2115 SHORTCUTS_COL_TYPE, SHORTCUT_TYPE_SEPARATOR,
2119 /* Updates the list of bookmarks */
2121 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
2124 gboolean old_changing_folders;
2126 GtkFilePath *list_selected = NULL;
2127 GtkFilePath *combo_selected = NULL;
2128 ShortcutType shortcut_type;
2131 profile_start ("start", NULL);
2133 old_changing_folders = impl->changing_folder;
2134 impl->changing_folder = TRUE;
2136 if (shortcuts_get_selected (impl, &iter))
2138 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2140 SHORTCUTS_COL_DATA, &col_data,
2141 SHORTCUTS_COL_TYPE, &shortcut_type,
2144 if (col_data && shortcut_type == SHORTCUT_TYPE_PATH)
2145 list_selected = gtk_file_path_copy (col_data);
2148 if (impl->save_folder_combo &&
2149 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
2152 GtkTreeIter child_iter;
2154 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
2157 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
2159 SHORTCUTS_COL_DATA, &col_data,
2160 SHORTCUTS_COL_TYPE, &shortcut_type,
2163 if (col_data && shortcut_type == SHORTCUT_TYPE_PATH)
2164 combo_selected = gtk_file_path_copy (col_data);
2167 if (impl->num_bookmarks > 0)
2168 shortcuts_remove_rows (impl,
2169 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
2170 impl->num_bookmarks + 1);
2172 impl->num_bookmarks = 0;
2174 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
2175 shortcuts_append_paths (impl, bookmarks);
2176 gtk_file_paths_free (bookmarks);
2178 if (impl->num_bookmarks > 0)
2179 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
2181 if (impl->shortcuts_pane_filter_model)
2182 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model));
2184 if (impl->shortcuts_combo_filter_model)
2185 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model));
2189 shortcuts_find_folder (impl, list_selected);
2190 gtk_file_path_free (list_selected);
2197 pos = shortcut_find_position (impl, combo_selected);
2199 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
2200 impl->has_search ? pos - 2 : pos);
2201 gtk_file_path_free (combo_selected);
2204 impl->changing_folder = old_changing_folders;
2206 profile_end ("end", NULL);
2209 /* Appends a separator and a row to the shortcuts list for the current folder */
2211 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
2216 g_assert (!impl->shortcuts_current_folder_active);
2220 g_assert (impl->current_folder != NULL);
2222 pos = shortcut_find_position (impl, impl->current_folder);
2225 GtkFileSystemVolume *volume;
2226 GtkFilePath *base_path;
2230 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2234 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
2236 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
2238 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2243 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
2245 shortcuts_insert_path (impl, pos, SHORTCUT_TYPE_VOLUME, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2249 shortcuts_insert_path (impl, pos, SHORTCUT_TYPE_PATH, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
2251 gtk_file_system_volume_free (impl->file_system, volume);
2255 gtk_file_path_free (base_path);
2257 else if (impl->save_folder_combo != NULL)
2258 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
2259 impl->has_search ? pos - 2 : pos);
2262 /* Updates the current folder row in the shortcuts model */
2264 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2268 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2270 if (impl->shortcuts_current_folder_active)
2272 shortcuts_remove_rows (impl, pos, 2);
2273 impl->shortcuts_current_folder_active = FALSE;
2276 shortcuts_add_current_folder (impl);
2279 /* Filter function used for the shortcuts filter model */
2281 shortcuts_pane_filter_cb (GtkTreeModel *model,
2285 GtkFileChooserDefault *impl;
2289 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2291 path = gtk_tree_model_get_path (model, iter);
2295 pos = *gtk_tree_path_get_indices (path);
2296 gtk_tree_path_free (path);
2298 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2301 /* Creates the list model for shortcuts */
2303 shortcuts_model_create (GtkFileChooserDefault *impl)
2305 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2306 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2307 GDK_TYPE_PIXBUF, /* pixbuf */
2308 G_TYPE_STRING, /* name */
2309 G_TYPE_POINTER, /* path or volume */
2310 G_TYPE_INT, /* ShortcutType */
2311 G_TYPE_BOOLEAN, /* removable */
2312 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2313 G_TYPE_POINTER); /* GtkFileSystemHandle */
2315 if (search_is_possible (impl))
2317 shortcuts_append_search (impl);
2318 shortcuts_insert_separator (impl, SHORTCUTS_SEARCH_SEPARATOR);
2321 if (impl->recent_manager)
2323 shortcuts_append_recent (impl);
2324 shortcuts_insert_separator (impl, SHORTCUTS_RECENT_SEPARATOR);
2327 if (impl->file_system)
2329 shortcuts_append_home (impl);
2330 shortcuts_append_desktop (impl);
2331 shortcuts_add_volumes (impl);
2334 impl->shortcuts_pane_filter_model = shortcuts_pane_model_filter_new (impl,
2335 GTK_TREE_MODEL (impl->shortcuts_model),
2338 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2339 shortcuts_pane_filter_cb,
2344 /* Callback used when the "New Folder" button is clicked */
2346 new_folder_button_clicked (GtkButton *button,
2347 GtkFileChooserDefault *impl)
2352 if (!impl->browse_files_model)
2353 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2355 /* Prevent button from being clicked twice */
2356 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2358 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2360 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2361 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2362 path, impl->list_name_column,
2365 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2366 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2368 impl->list_name_column,
2371 gtk_tree_path_free (path);
2375 edited_idle_create_folder_cb (GtkFileSystemHandle *handle,
2376 const GtkFilePath *path,
2377 const GError *error,
2380 gboolean cancelled = handle->cancelled;
2381 GtkFileChooserDefault *impl = data;
2383 if (!g_slist_find (impl->pending_handles, handle))
2386 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
2392 change_folder_and_display_error (impl, path, FALSE);
2394 error_creating_folder_dialog (impl, path, g_error_copy (error));
2397 g_object_unref (impl);
2398 g_object_unref (handle);
2401 /* Idle handler for creating a new folder after editing its name cell, or for
2402 * canceling the editing.
2405 edited_idle_cb (GtkFileChooserDefault *impl)
2407 GDK_THREADS_ENTER ();
2409 g_source_destroy (impl->edited_idle);
2410 impl->edited_idle = NULL;
2412 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2413 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2415 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2417 if (impl->edited_new_text) /* not cancelled? */
2420 GtkFilePath *file_path;
2423 file_path = gtk_file_system_make_path (impl->file_system,
2424 impl->current_folder,
2425 impl->edited_new_text,
2429 GtkFileSystemHandle *handle;
2431 handle = gtk_file_system_create_folder (impl->file_system, file_path,
2432 edited_idle_create_folder_cb,
2433 g_object_ref (impl));
2434 impl->pending_handles = g_slist_append (impl->pending_handles, handle);
2436 gtk_file_path_free (file_path);
2439 error_creating_folder_dialog (impl, file_path, error);
2441 g_free (impl->edited_new_text);
2442 impl->edited_new_text = NULL;
2445 GDK_THREADS_LEAVE ();
2451 queue_edited_idle (GtkFileChooserDefault *impl,
2452 const gchar *new_text)
2454 /* We create the folder in an idle handler so that we don't modify the tree
2458 if (!impl->edited_idle)
2460 impl->edited_idle = g_idle_source_new ();
2461 g_source_set_closure (impl->edited_idle,
2462 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2464 g_source_attach (impl->edited_idle, NULL);
2467 g_free (impl->edited_new_text);
2468 impl->edited_new_text = g_strdup (new_text);
2471 /* Callback used from the text cell renderer when the new folder is named */
2473 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2475 const gchar *new_text,
2476 GtkFileChooserDefault *impl)
2478 /* work around bug #154921 */
2479 g_object_set (cell_renderer_text,
2480 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2481 queue_edited_idle (impl, new_text);
2484 /* Callback used from the text cell renderer when the new folder edition gets
2488 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2489 GtkFileChooserDefault *impl)
2491 /* work around bug #154921 */
2492 g_object_set (cell_renderer_text,
2493 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2494 queue_edited_idle (impl, NULL);
2497 /* Creates the widgets for the filter combo box */
2499 filter_create (GtkFileChooserDefault *impl)
2501 impl->filter_combo = gtk_combo_box_new_text ();
2502 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2504 g_signal_connect (impl->filter_combo, "changed",
2505 G_CALLBACK (filter_combo_changed), impl);
2507 return impl->filter_combo;
2511 button_new (GtkFileChooserDefault *impl,
2513 const char *stock_id,
2521 button = gtk_button_new_with_mnemonic (text);
2522 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2523 gtk_button_set_image (GTK_BUTTON (button), image);
2525 gtk_widget_set_sensitive (button, sensitive);
2526 g_signal_connect (button, "clicked", callback, impl);
2529 gtk_widget_show (button);
2534 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2536 shortcut_find_position (GtkFileChooserDefault *impl,
2537 const GtkFilePath *path)
2541 int current_folder_separator_idx;
2543 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2546 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2549 /* FIXME: is this still needed? */
2550 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2554 for (i = 0; i < current_folder_separator_idx; i++)
2557 ShortcutType shortcut_type;
2559 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2560 SHORTCUTS_COL_DATA, &col_data,
2561 SHORTCUTS_COL_TYPE, &shortcut_type,
2566 if (shortcut_type == SHORTCUT_TYPE_VOLUME)
2568 GtkFileSystemVolume *volume;
2569 GtkFilePath *base_path;
2573 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2575 exists = base_path && strcmp (gtk_file_path_get_string (path),
2576 gtk_file_path_get_string (base_path)) == 0;
2582 else if (shortcut_type == SHORTCUT_TYPE_PATH)
2584 GtkFilePath *model_path;
2586 model_path = col_data;
2588 if (model_path && gtk_file_path_compare (model_path, path) == 0)
2593 if (i < current_folder_separator_idx - 1)
2595 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2596 g_assert_not_reached ();
2603 /* Tries to add a bookmark from a path name */
2605 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
2606 const GtkFilePath *path,
2611 g_return_val_if_fail (path != NULL, FALSE);
2613 if (shortcut_find_position (impl, path) != -1)
2617 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
2619 error_adding_bookmark_dialog (impl, path, error);
2627 add_bookmark_foreach_cb (GtkTreeModel *model,
2632 GtkFileChooserDefault *impl;
2633 GtkFileSystemModel *fs_model;
2634 GtkTreeIter child_iter;
2635 const GtkFilePath *file_path;
2637 impl = (GtkFileChooserDefault *) data;
2639 fs_model = impl->browse_files_model;
2640 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2642 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
2643 shortcuts_add_bookmark_from_path (impl, file_path, -1);
2646 /* Adds a bookmark from the currently selected item in the file list */
2648 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2650 GtkTreeSelection *selection;
2652 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2654 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2655 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2657 gtk_tree_selection_selected_foreach (selection,
2658 add_bookmark_foreach_cb,
2662 /* Callback used when the "Add bookmark" button is clicked */
2664 add_bookmark_button_clicked_cb (GtkButton *button,
2665 GtkFileChooserDefault *impl)
2667 bookmarks_add_selected_folder (impl);
2670 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2671 * returns FALSE if no shortcut is selected.
2674 shortcuts_get_selected (GtkFileChooserDefault *impl,
2677 GtkTreeSelection *selection;
2678 GtkTreeIter parent_iter;
2680 if (!impl->browse_shortcuts_tree_view)
2683 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2685 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2688 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
2694 /* Removes the selected bookmarks */
2696 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2704 if (!shortcuts_get_selected (impl, &iter))
2707 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2708 SHORTCUTS_COL_DATA, &col_data,
2709 SHORTCUTS_COL_REMOVABLE, &removable,
2715 g_assert (col_data != NULL);
2720 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2721 error_removing_bookmark_dialog (impl, path, error);
2724 /* Callback used when the "Remove bookmark" button is clicked */
2726 remove_bookmark_button_clicked_cb (GtkButton *button,
2727 GtkFileChooserDefault *impl)
2729 remove_selected_bookmarks (impl);
2732 struct selection_check_closure {
2733 GtkFileChooserDefault *impl;
2736 gboolean all_folders;
2739 /* Used from gtk_tree_selection_selected_foreach() */
2741 selection_check_foreach_cb (GtkTreeModel *model,
2746 struct selection_check_closure *closure;
2747 GtkTreeIter child_iter;
2748 const GtkFileInfo *info;
2752 closure->num_selected++;
2754 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2756 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2757 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2759 closure->all_folders = closure->all_folders && is_folder;
2760 closure->all_files = closure->all_files && !is_folder;
2763 /* Checks whether the selected items in the file list are all files or all folders */
2765 selection_check (GtkFileChooserDefault *impl,
2767 gboolean *all_files,
2768 gboolean *all_folders)
2770 struct selection_check_closure closure;
2771 GtkTreeSelection *selection;
2773 closure.impl = impl;
2774 closure.num_selected = 0;
2775 closure.all_files = TRUE;
2776 closure.all_folders = TRUE;
2778 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2779 gtk_tree_selection_selected_foreach (selection,
2780 selection_check_foreach_cb,
2783 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2786 *num_selected = closure.num_selected;
2789 *all_files = closure.all_files;
2792 *all_folders = closure.all_folders;
2795 struct get_selected_path_closure {
2796 GtkFileChooserDefault *impl;
2797 const GtkFilePath *path;
2801 get_selected_path_foreach_cb (GtkTreeModel *model,
2806 struct get_selected_path_closure *closure;
2807 GtkTreeIter child_iter;
2811 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2812 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2815 /* Returns a selected path from the file list */
2816 static const GtkFilePath *
2817 get_selected_path (GtkFileChooserDefault *impl)
2819 struct get_selected_path_closure closure;
2820 GtkTreeSelection *selection;
2822 closure.impl = impl;
2823 closure.path = NULL;
2825 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2826 gtk_tree_selection_selected_foreach (selection,
2827 get_selected_path_foreach_cb,
2830 return closure.path;
2834 GtkFileChooserDefault *impl;
2836 } UpdateTooltipData;
2839 update_tooltip (GtkTreeModel *model,
2844 UpdateTooltipData *udata = data;
2845 GtkTreeIter child_iter;
2846 const GtkFileInfo *info;
2848 if (udata->tip == NULL)
2850 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2854 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2855 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2856 gtk_file_info_get_display_name (info));
2861 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2862 * if there are no selected items *and* the current folder is not in the
2863 * bookmarks list. De-sensitize the button otherwise.
2866 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2869 gboolean all_folders;
2873 /* FIXME - Find a way to enable bookmarking items returned by
2874 * a search query or inside the recently used files list
2876 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
2877 impl->operation_mode == OPERATION_MODE_RECENT)
2879 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, FALSE);
2881 if (impl->browse_files_popup_menu_add_shortcut_item)
2882 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item, FALSE);
2887 selection_check (impl, &num_selected, NULL, &all_folders);
2889 if (num_selected == 0)
2890 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2891 else if (num_selected == 1)
2893 const GtkFilePath *path;
2895 path = get_selected_path (impl);
2896 active = all_folders && (shortcut_find_position (impl, path) == -1);
2899 active = all_folders;
2901 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2903 if (impl->browse_files_popup_menu_add_shortcut_item)
2904 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2905 (num_selected == 0) ? FALSE : active);
2909 if (num_selected == 0)
2910 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2911 else if (num_selected > 1)
2912 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2915 GtkTreeSelection *selection;
2916 UpdateTooltipData data;
2918 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2921 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2925 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2930 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2931 * bookmark row is selected in the shortcuts tree.
2934 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2937 gboolean removable = FALSE;
2940 if (shortcuts_get_selected (impl, &iter))
2941 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2942 SHORTCUTS_COL_REMOVABLE, &removable,
2943 SHORTCUTS_COL_NAME, &name,
2946 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2952 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2953 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2962 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2965 gboolean removable = FALSE;
2967 if (impl->browse_shortcuts_popup_menu == NULL)
2970 if (shortcuts_get_selected (impl, &iter))
2971 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2972 SHORTCUTS_COL_REMOVABLE, &removable,
2975 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2976 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2979 /* GtkWidget::drag-begin handler for the shortcuts list. */
2981 shortcuts_drag_begin_cb (GtkWidget *widget,
2982 GdkDragContext *context,
2983 GtkFileChooserDefault *impl)
2986 impl->shortcuts_drag_context = g_object_ref (context);
2991 /* Removes the idle handler for outside drags */
2993 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2995 if (!impl->shortcuts_drag_outside_idle)
2998 g_source_destroy (impl->shortcuts_drag_outside_idle);
2999 impl->shortcuts_drag_outside_idle = NULL;
3003 /* GtkWidget::drag-end handler for the shortcuts list. */
3005 shortcuts_drag_end_cb (GtkWidget *widget,
3006 GdkDragContext *context,
3007 GtkFileChooserDefault *impl)
3010 g_object_unref (impl->shortcuts_drag_context);
3012 shortcuts_cancel_drag_outside_idle (impl);
3014 if (!impl->shortcuts_drag_outside)
3017 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
3019 impl->shortcuts_drag_outside = FALSE;
3023 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
3025 shortcuts_drag_data_delete_cb (GtkWidget *widget,
3026 GdkDragContext *context,
3027 GtkFileChooserDefault *impl)
3029 g_signal_stop_emission_by_name (widget, "drag_data_delete");
3033 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
3037 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
3040 GtkTreeView *tree_view;
3043 GdkPixmap *row_pixmap;
3048 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3050 /* Find the selected path and get its drag pixmap */
3052 if (!shortcuts_get_selected (impl, &iter))
3053 g_assert_not_reached ();
3055 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3057 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
3058 gtk_tree_path_free (path);
3067 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
3073 GdkPixmap *composite;
3074 int row_pixmap_width, row_pixmap_height;
3075 int pixbuf_width, pixbuf_height;
3076 int composite_width, composite_height;
3077 int pixbuf_x, pixbuf_y;
3078 GdkGC *gc, *mask_gc;
3080 GdkBitmap *pixbuf_mask;
3082 /* Create pixmap and mask for composite image */
3084 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
3085 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
3086 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
3088 composite_width = MAX (row_pixmap_width, pixbuf_width);
3089 composite_height = MAX (row_pixmap_height, pixbuf_height);
3091 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
3093 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
3096 pixbuf_x = composite_width - pixbuf_width;
3098 pixbuf_y = (composite_height - pixbuf_height) / 2;
3100 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
3101 gc = gdk_gc_new (composite);
3103 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
3104 mask_gc = gdk_gc_new (mask);
3106 gdk_gc_set_foreground (mask_gc, &color);
3107 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
3110 color.green = 0xffff;
3111 color.blue = 0xffff;
3112 gdk_gc_set_rgb_fg_color (gc, &color);
3113 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
3115 /* Composite the row pixmap and the pixbuf */
3117 gdk_pixbuf_render_pixmap_and_mask_for_colormap
3119 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
3120 NULL, &pixbuf_mask, 128);
3121 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
3124 pixbuf_width, pixbuf_height);
3125 g_object_unref (pixbuf_mask);
3127 gdk_draw_drawable (composite, gc, row_pixmap,
3130 row_pixmap_width, row_pixmap_height);
3132 gdk_gc_set_foreground (mask_gc, &color);
3133 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
3135 gdk_draw_pixbuf (composite, gc, pixbuf,
3138 pixbuf_width, pixbuf_height,
3142 g_object_unref (pixbuf);
3143 g_object_unref (row_pixmap);
3145 row_pixmap = composite;
3149 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
3151 gtk_tree_view_get_path_at_pos (tree_view,
3152 tree_view->priv->press_start_x,
3153 tree_view->priv->press_start_y,
3159 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
3160 gdk_drawable_get_colormap (row_pixmap),
3163 tree_view->priv->press_start_x + 1,
3164 row_pixmap_y + cell_y + 1);
3166 g_object_unref (row_pixmap);
3168 g_object_unref (mask);
3171 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
3172 * handler so that we can tell apart the drag_leave event that comes right
3173 * before a drag_drop, from a normal drag_leave. We don't want to set the
3174 * cursor nor the flag in the latter case.
3177 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
3179 GDK_THREADS_ENTER ();
3181 shortcuts_drag_set_delete_cursor (impl, TRUE);
3182 impl->shortcuts_drag_outside = TRUE;
3184 shortcuts_cancel_drag_outside_idle (impl);
3186 GDK_THREADS_LEAVE ();
3192 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
3196 shortcuts_drag_leave_cb (GtkWidget *widget,
3197 GdkDragContext *context,
3199 GtkFileChooserDefault *impl)
3202 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
3204 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
3205 g_source_set_closure (impl->shortcuts_drag_outside_idle,
3206 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
3208 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
3212 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3214 GTK_TREE_VIEW_DROP_BEFORE);
3216 g_signal_stop_emission_by_name (widget, "drag_leave");
3219 /* Computes the appropriate row and position for dropping */
3221 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
3225 GtkTreeViewDropPosition *pos)
3227 GtkTreeView *tree_view;
3228 GtkTreeViewColumn *column;
3232 int bookmarks_index;
3234 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
3236 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3238 if (!gtk_tree_view_get_path_at_pos (tree_view,
3240 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
3246 row = bookmarks_index + impl->num_bookmarks - 1;
3247 *path = gtk_tree_path_new_from_indices (row, -1);
3248 *pos = GTK_TREE_VIEW_DROP_AFTER;
3252 row = *gtk_tree_path_get_indices (*path);
3253 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
3254 gtk_tree_path_free (*path);
3256 if (row < bookmarks_index)
3258 row = bookmarks_index;
3259 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3261 else if (row > bookmarks_index + impl->num_bookmarks - 1)
3263 row = bookmarks_index + impl->num_bookmarks - 1;
3264 *pos = GTK_TREE_VIEW_DROP_AFTER;
3268 if (cell_y < cell.height / 2)
3269 *pos = GTK_TREE_VIEW_DROP_BEFORE;
3271 *pos = GTK_TREE_VIEW_DROP_AFTER;
3274 *path = gtk_tree_path_new_from_indices (row, -1);
3277 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
3278 * implement the destination side of DnD by hand, due to limitations in
3279 * GtkTreeView's DnD API.
3282 shortcuts_drag_motion_cb (GtkWidget *widget,
3283 GdkDragContext *context,
3287 GtkFileChooserDefault *impl)
3290 GtkTreeViewDropPosition pos;
3291 GdkDragAction action;
3294 if (gtk_drag_get_source_widget (context) == widget)
3296 shortcuts_cancel_drag_outside_idle (impl);
3298 if (impl->shortcuts_drag_outside)
3300 shortcuts_drag_set_delete_cursor (impl, FALSE);
3301 impl->shortcuts_drag_outside = FALSE;
3306 if (context->suggested_action == GDK_ACTION_COPY ||
3307 (context->actions & GDK_ACTION_COPY) != 0)
3308 action = GDK_ACTION_COPY;
3309 else if (context->suggested_action == GDK_ACTION_MOVE ||
3310 (context->actions & GDK_ACTION_MOVE) != 0)
3311 action = GDK_ACTION_MOVE;
3318 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3319 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3320 gtk_tree_path_free (path);
3324 g_signal_stop_emission_by_name (widget, "drag_motion");
3328 gdk_drag_status (context, action, time_);
3335 /* GtkWidget::drag-drop handler for the shortcuts list. */
3337 shortcuts_drag_drop_cb (GtkWidget *widget,
3338 GdkDragContext *context,
3342 GtkFileChooserDefault *impl)
3345 shortcuts_cancel_drag_outside_idle (impl);
3348 g_signal_stop_emission_by_name (widget, "drag_drop");
3352 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3354 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3361 uris = g_uri_list_extract_uris (data);
3363 for (i = 0; uris[i]; i++)
3369 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3373 if (shortcuts_add_bookmark_from_path (impl, path, position))
3376 gtk_file_path_free (path);
3382 g_set_error (&error,
3383 GTK_FILE_CHOOSER_ERROR,
3384 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3385 _("Could not add a bookmark for '%s' "
3386 "because it is an invalid path name."),
3388 error_adding_bookmark_dialog (impl, path, error);
3395 /* Reorders the selected bookmark to the specified position */
3397 shortcuts_reorder (GtkFileChooserDefault *impl,
3402 ShortcutType shortcut_type;
3405 int bookmarks_index;
3406 const GtkFilePath *file_path;
3407 GtkFilePath *file_path_copy;
3411 /* Get the selected path */
3413 if (!shortcuts_get_selected (impl, &iter))
3414 g_assert_not_reached ();
3416 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3417 old_position = *gtk_tree_path_get_indices (path);
3418 gtk_tree_path_free (path);
3420 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3421 old_position -= bookmarks_index;
3422 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3424 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3425 SHORTCUTS_COL_NAME, &name,
3426 SHORTCUTS_COL_DATA, &col_data,
3427 SHORTCUTS_COL_TYPE, &shortcut_type,
3429 g_assert (col_data != NULL);
3430 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
3432 file_path = col_data;
3433 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
3435 /* Remove the path from the old position and insert it in the new one */
3437 if (new_position > old_position)
3440 if (old_position == new_position)
3444 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
3446 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
3447 gtk_file_system_set_bookmark_label (impl->file_system, file_path_copy, name);
3450 error_adding_bookmark_dialog (impl, file_path_copy, error);
3454 gtk_file_path_free (file_path_copy);
3457 /* Callback used when we get the drag data for the bookmarks list. We add the
3458 * received URIs as bookmarks if they are folders.
3461 shortcuts_drag_data_received_cb (GtkWidget *widget,
3462 GdkDragContext *context,
3465 GtkSelectionData *selection_data,
3470 GtkFileChooserDefault *impl;
3471 GtkTreePath *tree_path;
3472 GtkTreeViewDropPosition tree_pos;
3474 int bookmarks_index;
3476 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3478 /* Compute position */
3480 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3482 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3483 position = *gtk_tree_path_get_indices (tree_path);
3484 gtk_tree_path_free (tree_path);
3486 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3489 g_assert (position >= bookmarks_index);
3490 position -= bookmarks_index;
3492 if (selection_data->target == gdk_atom_intern_static_string ("text/uri-list"))
3493 shortcuts_drop_uris (impl, (const char *) selection_data->data, position);
3494 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3495 shortcuts_reorder (impl, position);
3497 g_signal_stop_emission_by_name (widget, "drag_data_received");
3500 /* Callback used when the selection in the shortcuts tree changes */
3502 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3503 GtkFileChooserDefault *impl)
3505 bookmarks_check_remove_sensitivity (impl);
3506 shortcuts_check_popup_sensitivity (impl);
3510 shortcuts_row_separator_func (GtkTreeModel *model,
3514 ShortcutType shortcut_type;
3516 gtk_tree_model_get (model, iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
3518 return shortcut_type == SHORTCUT_TYPE_SEPARATOR;
3521 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3522 * keypresses before the TreeView gets them.
3525 tree_view_keybinding_cb (GtkWidget *tree_view,
3527 GtkFileChooserDefault *impl)
3529 if ((event->keyval == GDK_slash
3530 || event->keyval == GDK_KP_Divide
3532 || event->keyval == GDK_asciitilde
3534 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3536 location_popup_handler (impl, event->string);
3543 /* Callback used when the file list's popup menu is detached */
3545 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3548 GtkFileChooserDefault *impl;
3550 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3551 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3553 impl->browse_shortcuts_popup_menu = NULL;
3554 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3555 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3559 remove_shortcut_cb (GtkMenuItem *item,
3560 GtkFileChooserDefault *impl)
3562 remove_selected_bookmarks (impl);
3565 /* Rename the selected bookmark */
3567 rename_selected_bookmark (GtkFileChooserDefault *impl)
3571 GtkTreeViewColumn *column;
3572 GtkCellRenderer *cell;
3575 if (shortcuts_get_selected (impl, &iter))
3577 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3578 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3579 renderers = gtk_tree_view_column_get_cell_renderers (column);
3580 cell = g_list_nth_data (renderers, 1);
3581 g_list_free (renderers);
3582 g_object_set (cell, "editable", TRUE, NULL);
3583 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3584 path, column, cell, TRUE);
3585 gtk_tree_path_free (path);
3590 rename_shortcut_cb (GtkMenuItem *item,
3591 GtkFileChooserDefault *impl)
3593 rename_selected_bookmark (impl);
3596 /* Constructs the popup menu for the file list if needed */
3598 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3602 if (impl->browse_shortcuts_popup_menu)
3605 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3606 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3607 impl->browse_shortcuts_tree_view,
3608 shortcuts_popup_menu_detach_cb);
3610 item = gtk_image_menu_item_new_with_label (_("Remove"));
3611 impl->browse_shortcuts_popup_menu_remove_item = item;
3612 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3613 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3614 g_signal_connect (item, "activate",
3615 G_CALLBACK (remove_shortcut_cb), impl);
3616 gtk_widget_show (item);
3617 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3619 item = gtk_menu_item_new_with_label (_("Rename..."));
3620 impl->browse_shortcuts_popup_menu_rename_item = item;
3621 g_signal_connect (item, "activate",
3622 G_CALLBACK (rename_shortcut_cb), impl);
3623 gtk_widget_show (item);
3624 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3626 shortcuts_check_popup_sensitivity (impl);
3630 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3632 shortcuts_build_popup_menu (impl);
3636 popup_position_func (GtkMenu *menu,
3640 gpointer user_data);
3643 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3644 GdkEventButton *event)
3646 shortcuts_update_popup_menu (impl);
3648 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3649 NULL, NULL, NULL, NULL,
3650 event->button, event->time);
3653 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3655 popup_position_func, impl->browse_shortcuts_tree_view,
3656 0, GDK_CURRENT_TIME);
3657 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3662 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3664 shortcuts_popup_menu_cb (GtkWidget *widget,
3665 GtkFileChooserDefault *impl)
3667 shortcuts_popup_menu (impl, NULL);
3671 /* Callback used when a button is pressed on the shortcuts list.
3672 * We trap button 3 to bring up a popup menu.
3675 shortcuts_button_press_event_cb (GtkWidget *widget,
3676 GdkEventButton *event,
3677 GtkFileChooserDefault *impl)
3679 static gboolean in_press = FALSE;
3685 if (event->button != 3)
3689 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3695 shortcuts_popup_menu (impl, event);
3700 shortcuts_edited (GtkCellRenderer *cell,
3703 GtkFileChooserDefault *impl)
3707 GtkFilePath *shortcut;
3709 g_object_set (cell, "editable", FALSE, NULL);
3711 path = gtk_tree_path_new_from_string (path_string);
3712 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3713 g_assert_not_reached ();
3715 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3716 SHORTCUTS_COL_DATA, &shortcut,
3718 gtk_tree_path_free (path);
3720 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3724 shortcuts_editing_canceled (GtkCellRenderer *cell,
3725 GtkFileChooserDefault *impl)
3727 g_object_set (cell, "editable", FALSE, NULL);
3730 /* Creates the widgets for the shortcuts and bookmarks tree */
3732 shortcuts_list_create (GtkFileChooserDefault *impl)
3735 GtkTreeSelection *selection;
3736 GtkTreeViewColumn *column;
3737 GtkCellRenderer *renderer;
3739 /* Scrolled window */
3741 swin = gtk_scrolled_window_new (NULL, NULL);
3742 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3743 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3744 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3746 gtk_widget_show (swin);
3750 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3751 #ifdef PROFILE_FILE_CHOOSER
3752 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3754 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3755 G_CALLBACK (tree_view_keybinding_cb), impl);
3756 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3757 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3758 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3759 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3760 /* Accessible object name for the file chooser's shortcuts pane */
3761 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3763 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_pane_filter_model);
3765 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3767 shortcuts_source_targets,
3768 num_shortcuts_source_targets,
3771 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3772 GTK_DEST_DEFAULT_ALL,
3773 shortcuts_dest_targets,
3774 num_shortcuts_dest_targets,
3775 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3777 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3778 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
3779 gtk_tree_selection_set_select_function (selection,
3780 shortcuts_select_func,
3783 g_signal_connect (selection, "changed",
3784 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3786 g_signal_connect (impl->browse_shortcuts_tree_view, "row_activated",
3787 G_CALLBACK (shortcuts_row_activated_cb), impl);
3789 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3790 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3792 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3793 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3794 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3795 G_CALLBACK (shortcuts_drag_end_cb), impl);
3796 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3797 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3799 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3800 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3801 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3802 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3803 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3804 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3805 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3806 G_CALLBACK (shortcuts_drag_data_received_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_signal_connect (renderer, "edited",
3826 G_CALLBACK (shortcuts_edited), impl);
3827 g_signal_connect (renderer, "editing-canceled",
3828 G_CALLBACK (shortcuts_editing_canceled), impl);
3829 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3830 gtk_tree_view_column_set_attributes (column, renderer,
3831 "text", SHORTCUTS_COL_NAME,
3834 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3835 shortcuts_row_separator_func,
3838 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3843 /* Creates the widgets for the shortcuts/bookmarks pane */
3845 shortcuts_pane_create (GtkFileChooserDefault *impl,
3846 GtkSizeGroup *size_group)
3852 vbox = gtk_vbox_new (FALSE, 6);
3853 gtk_widget_show (vbox);
3855 /* Shortcuts tree */
3857 widget = shortcuts_list_create (impl);
3858 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3860 /* Box for buttons */
3862 hbox = gtk_hbox_new (TRUE, 6);
3863 gtk_size_group_add_widget (size_group, hbox);
3864 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3865 gtk_widget_show (hbox);
3867 /* Add bookmark button */
3869 impl->browse_shortcuts_add_button = button_new (impl,
3874 G_CALLBACK (add_bookmark_button_clicked_cb));
3875 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3876 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
3877 _("Add the selected folder to the Bookmarks"), NULL);
3879 /* Remove bookmark button */
3881 impl->browse_shortcuts_remove_button = button_new (impl,
3886 G_CALLBACK (remove_bookmark_button_clicked_cb));
3887 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3888 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
3889 _("Remove the selected bookmark"), NULL);
3894 /* Handles key press events on the file list, so that we can trap Enter to
3895 * activate the default button on our own. Also, checks to see if '/' has been
3896 * pressed. See comment by tree_view_keybinding_cb() for more details.
3899 trap_activate_cb (GtkWidget *widget,
3903 GtkFileChooserDefault *impl;
3906 impl = (GtkFileChooserDefault *) data;
3908 modifiers = gtk_accelerator_get_default_mod_mask ();
3910 if ((event->keyval == GDK_slash
3911 || event->keyval == GDK_KP_Divide
3913 || event->keyval == GDK_asciitilde
3915 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3917 location_popup_handler (impl, event->string);
3921 if ((event->keyval == GDK_Return
3922 || event->keyval == GDK_ISO_Enter
3923 || event->keyval == GDK_KP_Enter
3924 || event->keyval == GDK_space)
3925 && ((event->state & modifiers) == 0)
3926 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3927 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3931 window = get_toplevel (widget);
3933 && widget != window->default_widget
3934 && !(widget == window->focus_widget &&
3935 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3937 gtk_window_activate_default (window);
3945 /* Callback used when the file list's popup menu is detached */
3947 popup_menu_detach_cb (GtkWidget *attach_widget,
3950 GtkFileChooserDefault *impl;
3952 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3953 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3955 impl->browse_files_popup_menu = NULL;
3956 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3957 impl->browse_files_popup_menu_hidden_files_item = NULL;
3960 /* Callback used when the "Add to Bookmarks" menu item is activated */
3962 add_to_shortcuts_cb (GtkMenuItem *item,
3963 GtkFileChooserDefault *impl)
3965 bookmarks_add_selected_folder (impl);
3968 /* Callback used when the "Show Hidden Files" menu item is toggled */
3970 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3971 GtkFileChooserDefault *impl)
3974 "show-hidden", gtk_check_menu_item_get_active (item),
3978 /* Shows an error dialog about not being able to select a dragged file */
3980 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3981 const GtkFilePath *path,
3985 _("Could not select file"),
3990 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3995 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3997 for (i = 1; uris[i]; i++)
4002 path = gtk_file_system_uri_to_path (impl->file_system, uri);
4006 GError *error = NULL;
4008 gtk_file_chooser_default_select_path (chooser, path, &error);
4010 error_selecting_dragged_file_dialog (impl, path, error);
4012 gtk_file_path_free (path);
4017 struct FileListDragData
4019 GtkFileChooserDefault *impl;
4025 file_list_drag_data_received_get_info_cb (GtkFileSystemHandle *handle,
4026 const GtkFileInfo *info,
4027 const GError *error,
4030 gboolean cancelled = handle->cancelled;
4031 struct FileListDragData *data = user_data;
4032 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
4034 if (handle != data->impl->file_list_drag_data_received_handle)
4037 data->impl->file_list_drag_data_received_handle = NULL;
4039 if (cancelled || error)
4042 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4043 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
4044 data->uris[1] == 0 && !error &&
4045 gtk_file_info_get_is_folder (info))
4046 change_folder_and_display_error (data->impl, data->path, FALSE);
4049 GError *error = NULL;
4051 gtk_file_chooser_default_unselect_all (chooser);
4052 gtk_file_chooser_default_select_path (chooser, data->path, &error);
4054 error_selecting_dragged_file_dialog (data->impl, data->path, error);
4056 browse_files_center_selected_row (data->impl);
4059 if (data->impl->select_multiple)
4060 file_list_drag_data_select_uris (data->impl, data->uris);
4063 g_object_unref (data->impl);
4064 g_strfreev (data->uris);
4065 gtk_file_path_free (data->path);
4068 g_object_unref (handle);
4072 file_list_drag_data_received_cb (GtkWidget *widget,
4073 GdkDragContext *context,
4076 GtkSelectionData *selection_data,
4081 GtkFileChooserDefault *impl;
4082 GtkFileChooser *chooser;
4086 GError *error = NULL;
4088 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4089 chooser = GTK_FILE_CHOOSER (data);
4091 /* Parse the text/uri-list string, navigate to the first one */
4092 uris = g_uri_list_extract_uris ((const char *) selection_data->data);
4096 path = gtk_file_system_uri_to_path (impl->file_system, uri);
4100 struct FileListDragData *data;
4102 data = g_new0 (struct FileListDragData, 1);
4103 data->impl = g_object_ref (impl);
4107 if (impl->file_list_drag_data_received_handle)
4108 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
4110 impl->file_list_drag_data_received_handle =
4111 gtk_file_system_get_info (impl->file_system, path,
4112 GTK_FILE_INFO_IS_FOLDER,
4113 file_list_drag_data_received_get_info_cb,
4119 g_set_error (&error,
4120 GTK_FILE_CHOOSER_ERROR,
4121 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
4122 _("Could not select file '%s' "
4123 "because it is an invalid path name."),
4125 error_selecting_dragged_file_dialog (impl, NULL, error);
4128 if (impl->select_multiple)
4129 file_list_drag_data_select_uris (impl, uris);
4135 g_signal_stop_emission_by_name (widget, "drag_data_received");
4138 /* Don't do anything with the drag_drop signal */
4140 file_list_drag_drop_cb (GtkWidget *widget,
4141 GdkDragContext *context,
4145 GtkFileChooserDefault *impl)
4147 g_signal_stop_emission_by_name (widget, "drag_drop");
4151 /* Disable the normal tree drag motion handler, it makes it look like you're
4152 dropping the dragged item onto a tree item */
4154 file_list_drag_motion_cb (GtkWidget *widget,
4155 GdkDragContext *context,
4159 GtkFileChooserDefault *impl)
4161 g_signal_stop_emission_by_name (widget, "drag_motion");
4165 /* Constructs the popup menu for the file list if needed */
4167 file_list_build_popup_menu (GtkFileChooserDefault *impl)
4171 if (impl->browse_files_popup_menu)
4174 impl->browse_files_popup_menu = gtk_menu_new ();
4175 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
4176 impl->browse_files_tree_view,
4177 popup_menu_detach_cb);
4179 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
4180 impl->browse_files_popup_menu_add_shortcut_item = item;
4181 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
4182 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
4183 gtk_widget_set_sensitive (item, FALSE);
4184 g_signal_connect (item, "activate",
4185 G_CALLBACK (add_to_shortcuts_cb), impl);
4186 gtk_widget_show (item);
4187 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4189 item = gtk_separator_menu_item_new ();
4190 gtk_widget_show (item);
4191 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4193 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
4194 impl->browse_files_popup_menu_hidden_files_item = item;
4195 g_signal_connect (item, "toggled",
4196 G_CALLBACK (show_hidden_toggled_cb), impl);
4197 gtk_widget_show (item);
4198 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
4201 /* Updates the popup menu for the file list, creating it if necessary */
4203 file_list_update_popup_menu (GtkFileChooserDefault *impl)
4205 file_list_build_popup_menu (impl);
4207 /* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
4209 /* The sensitivity of the Add to Bookmarks item is set in
4210 * bookmarks_check_add_sensitivity()
4213 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
4214 G_CALLBACK (show_hidden_toggled_cb), impl);
4215 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
4217 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
4218 G_CALLBACK (show_hidden_toggled_cb), impl);
4222 popup_position_func (GtkMenu *menu,
4228 GtkWidget *widget = GTK_WIDGET (user_data);
4229 GdkScreen *screen = gtk_widget_get_screen (widget);
4232 GdkRectangle monitor;
4234 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
4236 gdk_window_get_origin (widget->window, x, y);
4238 gtk_widget_size_request (GTK_WIDGET (menu), &req);
4240 *x += (widget->allocation.width - req.width) / 2;
4241 *y += (widget->allocation.height - req.height) / 2;
4243 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4244 gtk_menu_set_monitor (menu, monitor_num);
4245 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4247 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4248 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4254 file_list_popup_menu (GtkFileChooserDefault *impl,
4255 GdkEventButton *event)
4257 file_list_update_popup_menu (impl);
4259 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4260 NULL, NULL, NULL, NULL,
4261 event->button, event->time);
4264 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
4266 popup_position_func, impl->browse_files_tree_view,
4267 0, GDK_CURRENT_TIME);
4268 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
4274 /* Callback used for the GtkWidget::popup-menu signal of the file list */
4276 list_popup_menu_cb (GtkWidget *widget,
4277 GtkFileChooserDefault *impl)
4279 file_list_popup_menu (impl, NULL);
4283 /* Callback used when a button is pressed on the file list. We trap button 3 to
4284 * bring up a popup menu.
4287 list_button_press_event_cb (GtkWidget *widget,
4288 GdkEventButton *event,
4289 GtkFileChooserDefault *impl)
4291 static gboolean in_press = FALSE;
4297 if (event->button != 3)
4301 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4304 file_list_popup_menu (impl, event);
4308 /* Sets the sort column IDs for the file list based on the operation mode */
4310 file_list_set_sort_column_ids (GtkFileChooserDefault *impl)
4312 int name_id, mtime_id;
4314 name_id = mtime_id = 0;
4316 switch (impl->operation_mode)
4318 case OPERATION_MODE_BROWSE:
4319 name_id = FILE_LIST_COL_NAME;
4320 mtime_id = FILE_LIST_COL_MTIME;
4322 case OPERATION_MODE_SEARCH:
4323 name_id = SEARCH_MODEL_COL_PATH;
4324 mtime_id = SEARCH_MODEL_COL_STAT;
4326 case OPERATION_MODE_RECENT:
4327 name_id = RECENT_MODEL_COL_PATH;
4328 mtime_id = RECENT_MODEL_COL_INFO;
4332 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, name_id);
4333 gtk_tree_view_column_set_sort_column_id (impl->list_mtime_column, mtime_id);
4337 /* Creates the widgets for the file list */
4339 create_file_list (GtkFileChooserDefault *impl)
4342 GtkTreeSelection *selection;
4343 GtkTreeViewColumn *column;
4344 GtkCellRenderer *renderer;
4346 /* Scrolled window */
4348 swin = gtk_scrolled_window_new (NULL, NULL);
4349 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4350 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
4351 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4354 /* Tree/list view */
4356 impl->browse_files_tree_view = gtk_tree_view_new ();
4357 #ifdef PROFILE_FILE_CHOOSER
4358 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4360 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4361 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4363 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4364 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4366 gtk_drag_dest_set (impl->browse_files_tree_view,
4367 GTK_DEST_DEFAULT_ALL,
4368 file_list_dest_targets,
4369 num_file_list_dest_targets,
4370 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4372 g_signal_connect (impl->browse_files_tree_view, "row_activated",
4373 G_CALLBACK (list_row_activated), impl);
4374 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4375 G_CALLBACK (trap_activate_cb), impl);
4376 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4377 G_CALLBACK (list_popup_menu_cb), impl);
4378 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4379 G_CALLBACK (list_button_press_event_cb), impl);
4381 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4382 G_CALLBACK (file_list_drag_data_received_cb), impl);
4383 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4384 G_CALLBACK (file_list_drag_drop_cb), impl);
4385 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4386 G_CALLBACK (file_list_drag_motion_cb), impl);
4388 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4389 gtk_tree_selection_set_select_function (selection,
4392 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4394 file_list_source_targets,
4395 num_file_list_source_targets,
4398 g_signal_connect (selection, "changed",
4399 G_CALLBACK (list_selection_changed), impl);
4401 /* Filename column */
4403 impl->list_name_column = gtk_tree_view_column_new ();
4404 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4405 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4406 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4407 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4409 renderer = gtk_cell_renderer_pixbuf_new ();
4410 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4411 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4412 list_icon_data_func, impl, NULL);
4414 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4415 g_object_set (impl->list_name_renderer,
4416 "ellipsize", PANGO_ELLIPSIZE_END,
4418 g_signal_connect (impl->list_name_renderer, "edited",
4419 G_CALLBACK (renderer_edited_cb), impl);
4420 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4421 G_CALLBACK (renderer_editing_canceled_cb), impl);
4422 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4423 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4424 list_name_data_func, impl, NULL);
4426 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4430 column = gtk_tree_view_column_new ();
4431 gtk_tree_view_column_set_title (column, _("Size"));
4433 renderer = gtk_cell_renderer_text_new ();
4434 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4435 gtk_tree_view_column_set_cell_data_func (column, renderer,
4436 list_size_data_func, impl, NULL);
4437 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4438 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4441 /* Modification time column */
4443 column = gtk_tree_view_column_new ();
4444 gtk_tree_view_column_set_resizable (column, TRUE);
4445 gtk_tree_view_column_set_title (column, _("Modified"));
4447 renderer = gtk_cell_renderer_text_new ();
4448 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4449 gtk_tree_view_column_set_cell_data_func (column, renderer,
4450 list_mtime_data_func, impl, NULL);
4451 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4452 impl->list_mtime_column = column;
4454 file_list_set_sort_column_ids (impl);
4456 gtk_widget_show_all (swin);
4462 create_path_bar (GtkFileChooserDefault *impl)
4464 GtkWidget *path_bar;
4466 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4467 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4473 set_filter_tooltip (GtkWidget *widget,
4476 GtkTooltips *tooltips = (GtkTooltips *)data;
4478 if (GTK_IS_BUTTON (widget))
4479 gtk_tooltips_set_tip (tooltips, widget,
4480 _("Select which types of files are shown"),
4485 realize_filter_combo (GtkWidget *combo,
4488 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
4490 gtk_container_forall (GTK_CONTAINER (combo),
4495 /* Creates the widgets for the files/folders pane */
4497 file_pane_create (GtkFileChooserDefault *impl,
4498 GtkSizeGroup *size_group)
4504 vbox = gtk_vbox_new (FALSE, 6);
4505 gtk_widget_show (vbox);
4507 /* Box for lists and preview */
4509 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4510 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4511 gtk_widget_show (hbox);
4515 widget = create_file_list (impl);
4516 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4520 impl->preview_box = gtk_vbox_new (FALSE, 12);
4521 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4522 /* Don't show preview box initially */
4526 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4528 widget = filter_create (impl);
4530 g_signal_connect (widget, "realize",
4531 G_CALLBACK (realize_filter_combo), impl);
4533 gtk_widget_show (widget);
4534 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4536 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4537 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4542 /* Callback used when the "Browse for more folders" expander is toggled */
4544 expander_changed_cb (GtkExpander *expander,
4546 GtkFileChooserDefault *impl)
4548 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4549 update_appearance (impl);
4552 /* Callback used when the selection changes in the save folder combo box */
4554 save_folder_combo_changed_cb (GtkComboBox *combo,
4555 GtkFileChooserDefault *impl)
4559 if (impl->changing_folder)
4562 if (gtk_combo_box_get_active_iter (combo, &iter))
4564 GtkTreeIter child_iter;
4566 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4569 shortcuts_activate_iter (impl, &child_iter);
4573 /* Filter function used to filter out the Search item and its separator.
4574 * Used for the "Save in folder" combo box, so that these items do not appear in it.
4577 shortcuts_combo_filter_func (GtkTreeModel *model,
4581 GtkFileChooserDefault *impl;
4582 GtkTreePath *tree_path;
4587 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4589 g_assert (model == GTK_TREE_MODEL (impl->shortcuts_model));
4591 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
4592 g_assert (tree_path != NULL);
4594 indices = gtk_tree_path_get_indices (tree_path);
4598 if (impl->has_search)
4600 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH);
4601 if (idx == indices[0])
4605 idx = shortcuts_get_index (impl, SHORTCUTS_SEARCH_SEPARATOR);
4606 if (idx == indices[0])
4611 if (impl->has_recent)
4613 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT);
4614 if (idx == indices[0])
4618 idx = shortcuts_get_index (impl, SHORTCUTS_RECENT_SEPARATOR);
4619 if (idx == indices[0])
4624 gtk_tree_path_free (tree_path);
4629 /* Creates the combo box with the save folders */
4631 save_folder_combo_create (GtkFileChooserDefault *impl)
4634 GtkCellRenderer *cell;
4636 impl->shortcuts_combo_filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (impl->shortcuts_model), NULL);
4637 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_combo_filter_model),
4638 shortcuts_combo_filter_func,
4642 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4643 "model", impl->shortcuts_combo_filter_model,
4644 "focus-on-click", FALSE,
4646 gtk_widget_show (combo);
4648 cell = gtk_cell_renderer_pixbuf_new ();
4649 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4650 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4651 "pixbuf", SHORTCUTS_COL_PIXBUF,
4652 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4653 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4656 cell = gtk_cell_renderer_text_new ();
4657 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4658 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4659 "text", SHORTCUTS_COL_NAME,
4660 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4663 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4664 shortcuts_row_separator_func,
4667 g_signal_connect (combo, "changed",
4668 G_CALLBACK (save_folder_combo_changed_cb), impl);
4673 /* Creates the widgets specific to Save mode */
4675 save_widgets_create (GtkFileChooserDefault *impl)
4680 GtkWidget *alignment;
4682 if (impl->save_widgets != NULL)
4685 location_switch_to_path_bar (impl);
4687 vbox = gtk_vbox_new (FALSE, 12);
4689 table = gtk_table_new (2, 2, FALSE);
4690 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4691 gtk_widget_show (table);
4692 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4693 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4697 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4698 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4699 gtk_table_attach (GTK_TABLE (table), widget,
4703 gtk_widget_show (widget);
4705 /* Location entry */
4707 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4708 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4710 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4711 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4712 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4714 GTK_EXPAND | GTK_FILL, 0,
4716 gtk_widget_show (impl->location_entry);
4717 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4720 impl->save_folder_label = gtk_label_new (NULL);
4721 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4722 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4726 gtk_widget_show (impl->save_folder_label);
4728 impl->save_folder_combo = save_folder_combo_create (impl);
4729 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4731 GTK_EXPAND | GTK_FILL, GTK_FILL,
4733 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4736 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4737 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4739 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4740 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4741 g_signal_connect (impl->save_expander, "notify::expanded",
4742 G_CALLBACK (expander_changed_cb),
4744 gtk_widget_show_all (alignment);
4746 impl->save_widgets = vbox;
4747 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4748 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4749 gtk_widget_show (impl->save_widgets);
4752 /* Destroys the widgets specific to Save mode */
4754 save_widgets_destroy (GtkFileChooserDefault *impl)
4756 if (impl->save_widgets == NULL)
4759 gtk_widget_destroy (impl->save_widgets);
4760 impl->save_widgets = NULL;
4761 impl->location_entry = NULL;
4762 impl->save_folder_label = NULL;
4763 impl->save_folder_combo = NULL;
4764 impl->save_expander = NULL;
4767 /* Turns on the path bar widget. Can be called even if we are already in that
4771 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4773 if (impl->location_entry)
4775 gtk_widget_destroy (impl->location_entry);
4776 impl->location_entry = NULL;
4779 gtk_widget_hide (impl->location_entry_box);
4782 /* Sets the full path of the current folder as the text in the location entry. */
4784 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4788 if (!impl->current_folder)
4791 if (gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4795 filename = gtk_file_system_path_to_filename (impl->file_system, impl->current_folder);
4798 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4805 text = gtk_file_system_path_to_uri (impl->file_system, impl->current_folder);
4809 gboolean need_slash;
4812 len = strlen (text);
4813 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4819 slash_text = g_new (char, len + 2);
4820 strcpy (slash_text, text);
4821 slash_text[len] = G_DIR_SEPARATOR;
4822 slash_text[len + 1] = 0;
4828 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4833 /* Turns on the location entry. Can be called even if we are already in that
4837 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4839 /* when in search or recent files mode, we are not showing the
4840 * location_entry_box container, so there's no point in switching
4843 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
4844 impl->operation_mode == OPERATION_MODE_RECENT)
4847 if (impl->location_entry)
4848 gtk_widget_destroy (impl->location_entry);
4852 gtk_widget_show (impl->location_entry_box);
4856 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4857 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4859 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4860 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4862 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4863 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4865 /* Configure the entry */
4867 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4871 gtk_widget_show (impl->location_entry);
4872 gtk_widget_grab_focus (impl->location_entry);
4875 /* Sets a new location mode. set_buttons determines whether the toggle button
4876 * for the mode will also be changed.
4879 location_mode_set (GtkFileChooserDefault *impl,
4880 LocationMode new_mode,
4881 gboolean set_button)
4883 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4884 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4886 GtkWindow *toplevel;
4887 GtkWidget *current_focus;
4888 gboolean button_active;
4889 gboolean switch_to_file_list;
4893 case LOCATION_MODE_PATH_BAR:
4894 button_active = FALSE;
4896 /* The location_entry will disappear when we switch to path bar mode. So,
4897 * we'll focus the file list in that case, to avoid having a window with
4898 * no focused widget.
4900 toplevel = get_toplevel (GTK_WIDGET (impl));
4901 switch_to_file_list = FALSE;
4904 current_focus = gtk_window_get_focus (toplevel);
4905 if (!current_focus || current_focus == impl->location_entry)
4906 switch_to_file_list = TRUE;
4909 location_switch_to_path_bar (impl);
4911 if (switch_to_file_list)
4912 gtk_widget_grab_focus (impl->browse_files_tree_view);
4916 case LOCATION_MODE_FILENAME_ENTRY:
4917 button_active = TRUE;
4918 location_switch_to_filename_entry (impl);
4922 g_assert_not_reached ();
4928 g_signal_handlers_block_by_func (impl->location_button,
4929 G_CALLBACK (location_button_toggled_cb), impl);
4931 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4933 g_signal_handlers_unblock_by_func (impl->location_button,
4934 G_CALLBACK (location_button_toggled_cb), impl);
4938 impl->location_mode = new_mode;
4942 toggle_location_mode (GtkFileChooserDefault *impl,
4943 gboolean set_button)
4945 LocationMode new_mode;
4948 new_mode = (impl->location_mode == LOCATION_MODE_PATH_BAR) ?
4949 LOCATION_MODE_FILENAME_ENTRY : LOCATION_MODE_PATH_BAR;
4951 location_mode_set (impl, new_mode, set_button);
4955 location_toggle_popup_handler (GtkFileChooserDefault *impl)
4957 toggle_location_mode (impl, TRUE);
4960 /* Callback used when one of the location mode buttons is toggled */
4962 location_button_toggled_cb (GtkToggleButton *toggle,
4963 GtkFileChooserDefault *impl)
4967 is_active = gtk_toggle_button_get_active (toggle);
4970 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4972 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4974 toggle_location_mode (impl, FALSE);
4977 /* Creates a toggle button for the location entry. */
4979 location_button_create (GtkFileChooserDefault *impl)
4984 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4985 gtk_widget_show (image);
4987 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4991 g_signal_connect (impl->location_button, "toggled",
4992 G_CALLBACK (location_button_toggled_cb), impl);
4994 str = _("Type a file name");
4996 gtk_tooltips_set_tip (impl->tooltips, impl->location_button, str, NULL);
4997 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
5000 /* Creates the main hpaned with the widgets shared by Open and Save mode */
5002 browse_widgets_create (GtkFileChooserDefault *impl)
5008 GtkSizeGroup *size_group;
5010 /* size group is used by the [+][-] buttons and the filter combo */
5011 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
5012 vbox = gtk_vbox_new (FALSE, 12);
5014 /* Location widgets */
5015 hbox = gtk_hbox_new (FALSE, 12);
5016 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
5017 gtk_widget_show (hbox);
5018 impl->browse_path_bar_hbox = hbox;
5020 location_button_create (impl);
5021 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
5025 impl->browse_path_bar = create_path_bar (impl);
5026 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
5027 gtk_widget_show_all (impl->browse_path_bar);
5028 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
5031 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
5032 g_signal_connect (impl->browse_new_folder_button, "clicked",
5033 G_CALLBACK (new_folder_button_clicked), impl);
5034 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
5036 /* Box for the location label and entry */
5038 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
5039 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
5041 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
5042 gtk_widget_show (impl->location_label);
5043 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
5046 hpaned = gtk_hpaned_new ();
5047 gtk_widget_show (hpaned);
5048 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
5050 widget = shortcuts_pane_create (impl, size_group);
5051 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
5052 widget = file_pane_create (impl, size_group);
5053 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
5055 g_object_unref (size_group);
5061 gtk_file_chooser_default_constructor (GType type,
5062 guint n_construct_properties,
5063 GObjectConstructParam *construct_params)
5065 GtkFileChooserDefault *impl;
5068 profile_start ("start", NULL);
5070 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
5071 n_construct_properties,
5073 impl = GTK_FILE_CHOOSER_DEFAULT (object);
5075 g_assert (impl->file_system);
5077 gtk_widget_push_composite_child ();
5079 /* Recent files manager */
5080 recent_manager_update (impl);
5082 /* Shortcuts model */
5083 shortcuts_model_create (impl);
5085 /* The browse widgets */
5086 impl->browse_widgets = browse_widgets_create (impl);
5087 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
5089 /* Alignment to hold extra widget */
5090 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
5091 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
5093 gtk_widget_pop_composite_child ();
5094 update_appearance (impl);
5096 profile_end ("end", NULL);
5101 /* Sets the extra_widget by packing it in the appropriate place */
5103 set_extra_widget (GtkFileChooserDefault *impl,
5104 GtkWidget *extra_widget)
5108 g_object_ref (extra_widget);
5109 /* FIXME: is this right ? */
5110 gtk_widget_show (extra_widget);
5113 if (impl->extra_widget)
5115 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5116 g_object_unref (impl->extra_widget);
5119 impl->extra_widget = extra_widget;
5120 if (impl->extra_widget)
5122 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
5123 gtk_widget_show (impl->extra_align);
5126 gtk_widget_hide (impl->extra_align);
5130 set_local_only (GtkFileChooserDefault *impl,
5131 gboolean local_only)
5133 if (local_only != impl->local_only)
5135 impl->local_only = local_only;
5137 if (impl->shortcuts_model && impl->file_system)
5139 shortcuts_add_volumes (impl);
5140 shortcuts_add_bookmarks (impl);
5144 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
5146 /* If we are pointing to a non-local folder, make an effort to change
5147 * back to a local folder, but it's really up to the app to not cause
5148 * such a situation, so we ignore errors.
5150 const gchar *home = g_get_home_dir ();
5151 GtkFilePath *home_path;
5156 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
5158 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
5160 gtk_file_path_free (home_path);
5166 volumes_changed_cb (GtkFileSystem *file_system,
5167 GtkFileChooserDefault *impl)
5169 shortcuts_add_volumes (impl);
5172 /* Callback used when the set of bookmarks changes in the file system */
5174 bookmarks_changed_cb (GtkFileSystem *file_system,
5175 GtkFileChooserDefault *impl)
5177 shortcuts_add_bookmarks (impl);
5179 bookmarks_check_add_sensitivity (impl);
5180 bookmarks_check_remove_sensitivity (impl);
5181 shortcuts_check_popup_sensitivity (impl);
5184 /* Sets the file chooser to multiple selection mode */
5186 set_select_multiple (GtkFileChooserDefault *impl,
5187 gboolean select_multiple,
5188 gboolean property_notify)
5190 GtkTreeSelection *selection;
5191 GtkSelectionMode mode;
5193 if (select_multiple == impl->select_multiple)
5196 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
5198 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5199 gtk_tree_selection_set_mode (selection, mode);
5201 impl->select_multiple = select_multiple;
5202 g_object_notify (G_OBJECT (impl), "select-multiple");
5204 check_preview_change (impl);
5208 set_file_system_backend (GtkFileChooserDefault *impl,
5209 const char *backend)
5211 profile_start ("start for backend", backend ? backend : "default");
5213 if (impl->file_system)
5215 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
5216 impl->volumes_changed_id = 0;
5217 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
5218 impl->bookmarks_changed_id = 0;
5219 g_object_unref (impl->file_system);
5222 impl->file_system = NULL;
5224 impl->file_system = _gtk_file_system_create (backend);
5227 GtkSettings *settings = gtk_settings_get_default ();
5228 gchar *default_backend = NULL;
5230 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
5231 if (default_backend)
5233 impl->file_system = _gtk_file_system_create (default_backend);
5234 g_free (default_backend);
5238 if (!impl->file_system)
5240 #if defined (G_OS_UNIX)
5241 impl->file_system = gtk_file_system_unix_new ();
5242 #elif defined (G_OS_WIN32)
5243 impl->file_system = gtk_file_system_win32_new ();
5245 #error "No default filesystem implementation on the platform"
5249 if (impl->file_system)
5251 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
5252 G_CALLBACK (volumes_changed_cb),
5254 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
5255 G_CALLBACK (bookmarks_changed_cb),
5259 profile_end ("end", NULL);
5262 /* This function is basically a do_all function.
5264 * It sets the visibility on all the widgets based on the current state, and
5265 * moves the custom_widget if needed.
5268 update_appearance (GtkFileChooserDefault *impl)
5270 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5271 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5275 gtk_widget_hide (impl->location_button);
5276 save_widgets_create (impl);
5278 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5279 text = _("Save in _folder:");
5281 text = _("Create in _folder:");
5283 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
5285 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5287 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
5288 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
5289 gtk_widget_show (impl->browse_widgets);
5293 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
5294 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
5295 gtk_widget_hide (impl->browse_widgets);
5298 gtk_widget_show (impl->browse_new_folder_button);
5300 if (impl->select_multiple)
5302 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
5303 "Re-setting to single selection mode.");
5304 set_select_multiple (impl, FALSE, TRUE);
5307 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5308 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5310 gtk_widget_show (impl->location_button);
5311 save_widgets_destroy (impl);
5312 gtk_widget_show (impl->browse_widgets);
5313 location_mode_set (impl, impl->location_mode, TRUE);
5316 if (impl->location_entry)
5317 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
5319 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5320 gtk_widget_hide (impl->browse_new_folder_button);
5322 gtk_widget_show (impl->browse_new_folder_button);
5324 /* This *is* needed; we need to redraw the file list because the "sensitivity"
5325 * of files may change depending whether we are in a file or folder-only mode.
5327 gtk_widget_queue_draw (impl->browse_files_tree_view);
5329 g_signal_emit_by_name (impl, "default-size-changed");
5333 gtk_file_chooser_default_set_property (GObject *object,
5335 const GValue *value,
5339 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5343 case GTK_FILE_CHOOSER_PROP_ACTION:
5345 GtkFileChooserAction action = g_value_get_enum (value);
5347 if (action != impl->action)
5349 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
5351 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5352 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5353 && impl->select_multiple)
5355 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
5356 "this is not allowed in multiple selection mode. Resetting the file chooser "
5357 "to single selection mode.");
5358 set_select_multiple (impl, FALSE, TRUE);
5360 impl->action = action;
5361 update_appearance (impl);
5366 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
5367 set_file_system_backend (impl, g_value_get_string (value));
5370 case GTK_FILE_CHOOSER_PROP_FILTER:
5371 set_current_filter (impl, g_value_get_object (value));
5374 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5375 set_local_only (impl, g_value_get_boolean (value));
5378 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5379 set_preview_widget (impl, g_value_get_object (value));
5382 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5383 impl->preview_widget_active = g_value_get_boolean (value);
5384 update_preview_widget_visibility (impl);
5387 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5388 impl->use_preview_label = g_value_get_boolean (value);
5389 update_preview_widget_visibility (impl);
5392 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5393 set_extra_widget (impl, g_value_get_object (value));
5396 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5398 gboolean select_multiple = g_value_get_boolean (value);
5399 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5400 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5403 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
5404 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5405 "leaving the file chooser in single selection mode.");
5409 set_select_multiple (impl, select_multiple, FALSE);
5413 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5415 gboolean show_hidden = g_value_get_boolean (value);
5416 if (show_hidden != impl->show_hidden)
5418 impl->show_hidden = show_hidden;
5420 if (impl->browse_files_model)
5421 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5426 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5428 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5429 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5440 gtk_file_chooser_default_get_property (GObject *object,
5445 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5449 case GTK_FILE_CHOOSER_PROP_ACTION:
5450 g_value_set_enum (value, impl->action);
5453 case GTK_FILE_CHOOSER_PROP_FILTER:
5454 g_value_set_object (value, impl->current_filter);
5457 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5458 g_value_set_boolean (value, impl->local_only);
5461 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5462 g_value_set_object (value, impl->preview_widget);
5465 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5466 g_value_set_boolean (value, impl->preview_widget_active);
5469 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5470 g_value_set_boolean (value, impl->use_preview_label);
5473 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5474 g_value_set_object (value, impl->extra_widget);
5477 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5478 g_value_set_boolean (value, impl->select_multiple);
5481 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5482 g_value_set_boolean (value, impl->show_hidden);
5485 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5486 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5490 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5495 /* Removes the settings signal handler. It's safe to call multiple times */
5497 remove_settings_signal (GtkFileChooserDefault *impl,
5500 if (impl->settings_signal_id)
5502 GtkSettings *settings;
5504 settings = gtk_settings_get_for_screen (screen);
5505 g_signal_handler_disconnect (settings,
5506 impl->settings_signal_id);
5507 impl->settings_signal_id = 0;
5512 gtk_file_chooser_default_dispose (GObject *object)
5515 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5517 if (impl->extra_widget)
5519 g_object_unref (impl->extra_widget);
5520 impl->extra_widget = NULL;
5523 if (impl->volumes_changed_id > 0)
5525 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
5526 impl->volumes_changed_id = 0;
5529 if (impl->bookmarks_changed_id > 0)
5531 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
5532 impl->bookmarks_changed_id = 0;
5535 pending_select_paths_free (impl);
5537 /* cancel all pending operations */
5538 if (impl->pending_handles)
5540 for (l = impl->pending_handles; l; l = l->next)
5542 GtkFileSystemHandle *handle =l->data;
5543 gtk_file_system_cancel_operation (handle);
5545 g_slist_free (impl->pending_handles);
5546 impl->pending_handles = NULL;
5549 if (impl->reload_icon_handles)
5551 for (l = impl->reload_icon_handles; l; l = l->next)
5553 GtkFileSystemHandle *handle =l->data;
5554 gtk_file_system_cancel_operation (handle);
5556 g_slist_free (impl->reload_icon_handles);
5557 impl->reload_icon_handles = NULL;
5560 if (impl->loading_shortcuts)
5562 for (l = impl->loading_shortcuts; l; l = l->next)
5564 GtkFileSystemHandle *handle =l->data;
5565 gtk_file_system_cancel_operation (handle);
5567 g_slist_free (impl->loading_shortcuts);
5568 impl->loading_shortcuts = NULL;
5571 if (impl->file_list_drag_data_received_handle)
5573 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
5574 impl->file_list_drag_data_received_handle = NULL;
5577 if (impl->update_current_folder_handle)
5579 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
5580 impl->update_current_folder_handle = NULL;
5583 if (impl->show_and_select_paths_handle)
5585 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5586 impl->show_and_select_paths_handle = NULL;
5589 if (impl->should_respond_get_info_handle)
5591 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
5592 impl->should_respond_get_info_handle = NULL;
5595 if (impl->update_from_entry_handle)
5597 gtk_file_system_cancel_operation (impl->update_from_entry_handle);
5598 impl->update_from_entry_handle = NULL;
5601 if (impl->shortcuts_activate_iter_handle)
5603 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
5604 impl->shortcuts_activate_iter_handle = NULL;
5607 search_stop_searching (impl, TRUE);
5608 recent_stop_loading (impl);
5610 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5612 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5615 /* We override show-all since we have internal widgets that
5616 * shouldn't be shown when you call show_all(), like the filter
5620 gtk_file_chooser_default_show_all (GtkWidget *widget)
5622 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5624 gtk_widget_show (widget);
5626 if (impl->extra_widget)
5627 gtk_widget_show_all (impl->extra_widget);
5630 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5631 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5634 toplevel_set_focus_cb (GtkWindow *window,
5636 GtkFileChooserDefault *impl)
5638 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5641 /* We monitor the focus widget on our toplevel to be able to know which widget
5642 * was last focused at the time our "should_respond" method gets called.
5645 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5646 GtkWidget *previous_toplevel)
5648 GtkFileChooserDefault *impl;
5649 GtkWidget *toplevel;
5651 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5653 if (previous_toplevel)
5655 g_assert (impl->toplevel_set_focus_id != 0);
5656 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5657 impl->toplevel_set_focus_id = 0;
5658 impl->toplevel_last_focus_widget = NULL;
5661 g_assert (impl->toplevel_set_focus_id == 0);
5663 toplevel = gtk_widget_get_toplevel (widget);
5664 if (GTK_IS_WINDOW (toplevel))
5666 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5667 G_CALLBACK (toplevel_set_focus_cb), impl);
5668 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5672 /* Changes the icons wherever it is needed */
5674 change_icon_theme (GtkFileChooserDefault *impl)
5676 GtkSettings *settings;
5679 profile_start ("start", NULL);
5681 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5683 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5684 impl->icon_size = MAX (width, height);
5686 impl->icon_size = FALLBACK_ICON_SIZE;
5688 shortcuts_reload_icons (impl);
5689 gtk_widget_queue_resize (impl->browse_files_tree_view);
5691 profile_end ("end", NULL);
5694 /* Callback used when a GtkSettings value changes */
5696 settings_notify_cb (GObject *object,
5698 GtkFileChooserDefault *impl)
5702 profile_start ("start", NULL);
5704 name = g_param_spec_get_name (pspec);
5706 if (strcmp (name, "gtk-icon-theme-name") == 0 ||
5707 strcmp (name, "gtk-icon-sizes") == 0)
5708 change_icon_theme (impl);
5710 profile_end ("end", NULL);
5713 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5717 check_icon_theme (GtkFileChooserDefault *impl)
5719 GtkSettings *settings;
5721 profile_start ("start", NULL);
5723 if (impl->settings_signal_id)
5725 profile_end ("end", NULL);
5729 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5731 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5732 impl->settings_signal_id = g_signal_connect (settings, "notify",
5733 G_CALLBACK (settings_notify_cb), impl);
5735 change_icon_theme (impl);
5738 profile_end ("end", NULL);
5742 recent_manager_update (GtkFileChooserDefault *impl)
5744 GtkRecentManager *manager;
5746 profile_start ("start", NULL);
5748 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5749 manager = gtk_recent_manager_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5751 manager = gtk_recent_manager_get_default ();
5753 if (impl->recent_manager != manager)
5754 impl->recent_manager = manager;
5756 profile_end ("end", NULL);
5760 gtk_file_chooser_default_style_set (GtkWidget *widget,
5761 GtkStyle *previous_style)
5763 GtkFileChooserDefault *impl;
5765 profile_start ("start", NULL);
5767 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5769 profile_msg (" parent class style_set start", NULL);
5770 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5771 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5772 profile_msg (" parent class style_set end", NULL);
5774 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5775 change_icon_theme (impl);
5777 profile_msg (" emit default-size-changed start", NULL);
5778 g_signal_emit_by_name (widget, "default-size-changed");
5779 profile_msg (" emit default-size-changed end", NULL);
5781 profile_end ("end", NULL);
5785 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5786 GdkScreen *previous_screen)
5788 GtkFileChooserDefault *impl;
5790 profile_start ("start", NULL);
5792 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5794 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5795 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5797 remove_settings_signal (impl, previous_screen);
5798 check_icon_theme (impl);
5799 recent_manager_update (impl);
5801 g_signal_emit_by_name (widget, "default-size-changed");
5803 profile_end ("end", NULL);
5807 get_is_file_filtered (GtkFileChooserDefault *impl,
5808 const GtkFilePath *path,
5809 GtkFileInfo *file_info)
5811 GtkFileFilterInfo filter_info;
5812 GtkFileFilterFlags needed;
5815 if (!impl->current_filter)
5818 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5820 needed = gtk_file_filter_get_needed (impl->current_filter);
5822 filter_info.display_name = gtk_file_info_get_display_name (file_info);
5823 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
5825 if (needed & GTK_FILE_FILTER_FILENAME)
5827 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
5828 if (filter_info.filename)
5829 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5832 filter_info.filename = NULL;
5834 if (needed & GTK_FILE_FILTER_URI)
5836 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
5837 if (filter_info.uri)
5838 filter_info.contains |= GTK_FILE_FILTER_URI;
5841 filter_info.uri = NULL;
5843 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5845 if (filter_info.filename)
5846 g_free ((gchar *)filter_info.filename);
5847 if (filter_info.uri)
5848 g_free ((gchar *)filter_info.uri);
5854 settings_load (GtkFileChooserDefault *impl)
5856 GtkFileChooserSettings *settings;
5857 LocationMode location_mode;
5858 gboolean show_hidden;
5859 gboolean expand_folders;
5861 settings = _gtk_file_chooser_settings_new ();
5863 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5864 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5865 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5867 g_object_unref (settings);
5869 location_mode_set (impl, location_mode, TRUE);
5870 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5871 impl->expand_folders = expand_folders;
5872 if (impl->save_expander)
5873 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5877 settings_save (GtkFileChooserDefault *impl)
5879 GtkFileChooserSettings *settings;
5881 settings = _gtk_file_chooser_settings_new ();
5883 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5884 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5885 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
5888 _gtk_file_chooser_settings_save (settings, NULL);
5890 g_object_unref (settings);
5893 /* GtkWidget::map method */
5895 gtk_file_chooser_default_map (GtkWidget *widget)
5897 GtkFileChooserDefault *impl;
5898 char *current_working_dir;
5900 profile_start ("start", NULL);
5902 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5904 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5906 if (impl->operation_mode == OPERATION_MODE_BROWSE)
5908 switch (impl->reload_state)
5911 /* The user didn't explicitly give us a folder to
5912 * display, so we'll use the cwd
5914 current_working_dir = g_get_current_dir ();
5915 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
5916 current_working_dir);
5917 g_free (current_working_dir);
5920 case RELOAD_HAS_FOLDER:
5921 /* Nothing; we are already loading or loaded, so we
5922 * don't need to reload
5926 case RELOAD_WAS_UNMAPPED:
5927 /* Just reload the current folder; else continue
5930 if (impl->current_folder)
5932 pending_select_paths_store_selection (impl);
5933 change_folder_and_display_error (impl, impl->current_folder, FALSE);
5938 g_assert_not_reached ();
5942 bookmarks_changed_cb (impl->file_system, impl);
5944 settings_load (impl);
5946 profile_end ("end", NULL);
5949 /* GtkWidget::unmap method */
5951 gtk_file_chooser_default_unmap (GtkWidget *widget)
5953 GtkFileChooserDefault *impl;
5955 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5957 settings_save (impl);
5959 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5961 impl->reload_state = RELOAD_WAS_UNMAPPED;
5965 list_model_filter_func (GtkFileSystemModel *model,
5967 const GtkFileInfo *file_info,
5970 GtkFileChooserDefault *impl = user_data;
5972 if (!impl->current_filter)
5975 if (gtk_file_info_get_is_folder (file_info))
5978 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
5982 install_list_model_filter (GtkFileChooserDefault *impl)
5984 GtkFileSystemModelFilter filter;
5987 g_assert (impl->browse_files_model != NULL);
5989 if (impl->current_filter)
5991 filter = list_model_filter_func;
6000 _gtk_file_system_model_set_filter (impl->browse_files_model,
6005 #define COMPARE_DIRECTORIES \
6006 GtkFileChooserDefault *impl = user_data; \
6007 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
6008 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
6009 gboolean dir_a, dir_b; \
6012 dir_a = gtk_file_info_get_is_folder (info_a); \
6014 return impl->list_sort_ascending ? -1 : 1; \
6017 dir_b = gtk_file_info_get_is_folder (info_b); \
6019 return impl->list_sort_ascending ? 1 : -1; \
6021 if (dir_a != dir_b) \
6022 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
6024 /* Sort callback for the filename column */
6026 name_sort_func (GtkTreeModel *model,
6031 COMPARE_DIRECTORIES;
6033 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
6036 /* Sort callback for the size column */
6038 size_sort_func (GtkTreeModel *model,
6043 COMPARE_DIRECTORIES;
6046 gint64 size_a = gtk_file_info_get_size (info_a);
6047 gint64 size_b = gtk_file_info_get_size (info_b);
6049 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
6053 /* Sort callback for the mtime column */
6055 mtime_sort_func (GtkTreeModel *model,
6060 COMPARE_DIRECTORIES;
6063 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
6064 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
6066 return ta > tb ? -1 : (ta == tb ? 0 : 1);
6070 /* Callback used when the sort column changes. We cache the sort order for use
6071 * in name_sort_func().
6074 list_sort_column_changed_cb (GtkTreeSortable *sortable,
6075 GtkFileChooserDefault *impl)
6077 GtkSortType sort_type;
6079 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
6080 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
6084 set_busy_cursor (GtkFileChooserDefault *impl,
6087 GtkWindow *toplevel;
6088 GdkDisplay *display;
6091 toplevel = get_toplevel (GTK_WIDGET (impl));
6092 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
6095 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
6098 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
6102 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
6103 gdk_display_flush (display);
6106 gdk_cursor_unref (cursor);
6109 /* Creates a sort model to wrap the file system model and sets it on the tree view */
6111 load_set_model (GtkFileChooserDefault *impl)
6113 profile_start ("start", NULL);
6115 g_assert (impl->browse_files_model != NULL);
6116 g_assert (impl->sort_model == NULL);
6118 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
6119 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
6120 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
6121 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
6122 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
6123 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
6124 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
6125 impl->list_sort_ascending = TRUE;
6126 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
6128 g_signal_connect (impl->sort_model, "sort_column_changed",
6129 G_CALLBACK (list_sort_column_changed_cb), impl);
6131 profile_msg (" gtk_tree_view_set_model start", NULL);
6132 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
6133 GTK_TREE_MODEL (impl->sort_model));
6134 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
6135 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
6136 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
6137 profile_msg (" gtk_tree_view_set_model end", NULL);
6139 profile_end ("end", NULL);
6142 /* Timeout callback used when the loading timer expires */
6144 load_timeout_cb (gpointer data)
6146 GtkFileChooserDefault *impl;
6148 profile_start ("start", NULL);
6150 impl = GTK_FILE_CHOOSER_DEFAULT (data);
6151 g_assert (impl->load_state == LOAD_PRELOAD);
6152 g_assert (impl->load_timeout_id != 0);
6153 g_assert (impl->browse_files_model != NULL);
6155 impl->load_timeout_id = 0;
6156 impl->load_state = LOAD_LOADING;
6158 load_set_model (impl);
6160 profile_end ("end", NULL);
6165 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
6167 load_setup_timer (GtkFileChooserDefault *impl)
6169 g_assert (impl->load_timeout_id == 0);
6170 g_assert (impl->load_state != LOAD_PRELOAD);
6172 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
6173 impl->load_state = LOAD_PRELOAD;
6176 /* Removes the load timeout and switches to the LOAD_FINISHED state */
6178 load_remove_timer (GtkFileChooserDefault *impl)
6180 if (impl->load_timeout_id != 0)
6182 g_assert (impl->load_state == LOAD_PRELOAD);
6184 g_source_remove (impl->load_timeout_id);
6185 impl->load_timeout_id = 0;
6186 impl->load_state = LOAD_EMPTY;
6189 g_assert (impl->load_state == LOAD_EMPTY ||
6190 impl->load_state == LOAD_LOADING ||
6191 impl->load_state == LOAD_FINISHED);
6194 /* Selects the first row in the file list */
6196 browse_files_select_first_row (GtkFileChooserDefault *impl)
6200 if (!impl->sort_model)
6203 path = gtk_tree_path_new_from_indices (0, -1);
6204 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
6205 gtk_tree_path_free (path);
6208 struct center_selected_row_closure {
6209 GtkFileChooserDefault *impl;
6210 gboolean already_centered;
6213 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
6214 * selected row in the tree view.
6217 center_selected_row_foreach_cb (GtkTreeModel *model,
6222 struct center_selected_row_closure *closure;
6225 if (closure->already_centered)
6228 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
6229 closure->already_centered = TRUE;
6232 /* Centers the selected row in the tree view */
6234 browse_files_center_selected_row (GtkFileChooserDefault *impl)
6236 struct center_selected_row_closure closure;
6237 GtkTreeSelection *selection;
6239 closure.impl = impl;
6240 closure.already_centered = FALSE;
6242 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6243 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
6246 struct ShowAndSelectPathsData
6248 GtkFileChooserDefault *impl;
6253 show_and_select_paths_finished_loading (GtkFileFolder *folder,
6256 gboolean have_hidden;
6257 gboolean have_filtered;
6259 struct ShowAndSelectPathsData *data = user_data;
6261 have_hidden = FALSE;
6262 have_filtered = FALSE;
6264 for (l = data->paths; l; l = l->next)
6266 const GtkFilePath *path;
6272 info = gtk_file_folder_get_info (folder, path, NULL);
6276 have_hidden = gtk_file_info_get_is_hidden (info);
6279 have_filtered = !gtk_file_info_get_is_folder (info) && get_is_file_filtered (data->impl, path, info);
6281 gtk_file_info_free (info);
6283 if (have_hidden && have_filtered)
6284 break; /* we now have all the information we need */
6288 g_signal_handlers_disconnect_by_func (folder,
6289 show_and_select_paths_finished_loading,
6292 g_object_unref (folder);
6295 g_object_set (data->impl, "show-hidden", TRUE, NULL);
6298 set_current_filter (data->impl, NULL);
6300 for (l = data->paths; l; l = l->next)
6302 const GtkFilePath *path;
6305 _gtk_file_system_model_path_do (data->impl->browse_files_model, path,
6306 select_func, data->impl);
6309 browse_files_center_selected_row (data->impl);
6311 g_object_unref (data->impl);
6312 gtk_file_paths_free (data->paths);
6317 show_and_select_paths_get_folder_cb (GtkFileSystemHandle *handle,
6318 GtkFileFolder *folder,
6319 const GError *error,
6322 gboolean cancelled = handle->cancelled;
6323 struct ShowAndSelectPathsData *data = user_data;
6325 if (data->impl->show_and_select_paths_handle != handle)
6328 data->impl->show_and_select_paths_handle = NULL;
6330 if (cancelled || error)
6333 g_object_unref (handle);
6335 if (gtk_file_folder_is_finished_loading (folder))
6336 show_and_select_paths_finished_loading (folder, user_data);
6338 g_signal_connect (folder, "finished-loading",
6339 G_CALLBACK (show_and_select_paths_finished_loading),
6345 g_object_unref (data->impl);
6346 gtk_file_paths_free (data->paths);
6349 g_object_unref (handle);
6353 show_and_select_paths (GtkFileChooserDefault *impl,
6354 const GtkFilePath *parent_path,
6358 struct ShowAndSelectPathsData *info;
6360 profile_start ("start", NULL);
6364 profile_end ("end", NULL);
6368 info = g_new (struct ShowAndSelectPathsData, 1);
6369 info->impl = g_object_ref (impl);
6370 info->paths = gtk_file_paths_copy (paths);
6372 if (impl->show_and_select_paths_handle)
6373 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
6375 impl->show_and_select_paths_handle =
6376 gtk_file_system_get_folder (impl->file_system, parent_path,
6377 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN,
6378 show_and_select_paths_get_folder_cb, info);
6380 profile_end ("end", NULL);
6384 /* Processes the pending operation when a folder is finished loading */
6386 pending_select_paths_process (GtkFileChooserDefault *impl)
6388 g_assert (impl->load_state == LOAD_FINISHED);
6389 g_assert (impl->browse_files_model != NULL);
6390 g_assert (impl->sort_model != NULL);
6392 if (impl->pending_select_paths)
6395 show_and_select_paths (impl, impl->current_folder, impl->pending_select_paths, NULL);
6396 pending_select_paths_free (impl);
6397 browse_files_center_selected_row (impl);
6401 /* We only select the first row if the chooser is actually mapped ---
6402 * selecting the first row is to help the user when he is interacting with
6403 * the chooser, but sometimes a chooser works not on behalf of the user,
6404 * but rather on behalf of something else like GtkFileChooserButton. In
6405 * that case, the chooser's selection should be what the caller expects,
6406 * as the user can't see that something else got selected. See bug #165264.
6408 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
6409 browse_files_select_first_row (impl);
6412 g_assert (impl->pending_select_paths == NULL);
6415 /* Callback used when the file system model finishes loading */
6417 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
6418 GtkFileChooserDefault *impl)
6420 profile_start ("start", NULL);
6422 if (impl->load_state == LOAD_PRELOAD)
6424 load_remove_timer (impl);
6425 load_set_model (impl);
6427 else if (impl->load_state == LOAD_LOADING)
6433 /* We can't g_assert_not_reached(), as something other than us may have
6434 * initiated a folder reload. See #165556.
6436 profile_end ("end", NULL);
6440 g_assert (impl->load_timeout_id == 0);
6442 impl->load_state = LOAD_FINISHED;
6444 pending_select_paths_process (impl);
6445 set_busy_cursor (impl, FALSE);
6446 #ifdef PROFILE_FILE_CHOOSER
6447 access ("MARK: *** FINISHED LOADING", F_OK);
6450 profile_end ("end", NULL);
6454 stop_loading_and_clear_list_model (GtkFileChooserDefault *impl)
6456 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6458 if (impl->browse_files_model)
6460 g_object_unref (impl->browse_files_model);
6461 impl->browse_files_model = NULL;
6464 if (impl->sort_model)
6466 g_object_unref (impl->sort_model);
6467 impl->sort_model = NULL;
6470 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6473 /* Gets rid of the old list model and creates a new one for the current folder */
6475 set_list_model (GtkFileChooserDefault *impl,
6478 g_assert (impl->current_folder != NULL);
6480 profile_start ("start", NULL);
6482 stop_loading_and_clear_list_model (impl);
6484 set_busy_cursor (impl, TRUE);
6485 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6487 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6488 impl->current_folder, 0,
6491 if (!impl->browse_files_model)
6493 set_busy_cursor (impl, FALSE);
6494 profile_end ("end", NULL);
6498 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6500 g_signal_connect (impl->browse_files_model, "finished-loading",
6501 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6503 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6505 install_list_model_filter (impl);
6507 profile_end ("end", NULL);
6512 struct update_chooser_entry_selected_foreach_closure {
6514 GtkTreeIter first_selected_iter;
6518 compare_utf8_filenames (const gchar *a,
6521 gchar *a_folded, *b_folded;
6524 a_folded = g_utf8_strdown (a, -1);
6525 b_folded = g_utf8_strdown (b, -1);
6527 retval = strcmp (a_folded, b_folded);
6536 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6541 struct update_chooser_entry_selected_foreach_closure *closure;
6544 closure->num_selected++;
6546 if (closure->num_selected == 1)
6547 closure->first_selected_iter = *iter;
6551 update_chooser_entry (GtkFileChooserDefault *impl)
6553 GtkTreeSelection *selection;
6554 struct update_chooser_entry_selected_foreach_closure closure;
6555 const char *file_part;
6557 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6558 impl->operation_mode == OPERATION_MODE_RECENT ||
6559 !impl->location_entry)
6562 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6563 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6564 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6565 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6566 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6569 g_assert (impl->location_entry != NULL);
6571 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6572 closure.num_selected = 0;
6573 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6577 if (closure.num_selected == 0)
6579 goto maybe_clear_entry;
6581 else if (closure.num_selected == 1)
6583 if (impl->operation_mode == OPERATION_MODE_BROWSE)
6585 GtkTreeIter child_iter;
6586 const GtkFileInfo *info;
6587 gboolean change_entry;
6589 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6591 &closure.first_selected_iter);
6593 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6595 /* If the cursor moved to the row of the newly created folder,
6596 * retrieving info will return NULL.
6601 g_free (impl->browse_files_last_selected_name);
6602 impl->browse_files_last_selected_name =
6603 g_strdup (gtk_file_info_get_display_name (info));
6605 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6606 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6607 change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
6609 change_entry = TRUE; /* ... unless we are in one of the folder modes */
6612 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6613 impl->browse_files_last_selected_name);
6616 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
6618 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model),
6619 &closure.first_selected_iter,
6620 SEARCH_MODEL_COL_DISPLAY_NAME, &file_part,
6623 else if (impl->operation_mode == OPERATION_MODE_RECENT)
6625 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model),
6626 &closure.first_selected_iter,
6627 RECENT_MODEL_COL_DISPLAY_NAME, &file_part,
6633 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6634 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6636 /* Multiple selection, so just clear the entry. */
6638 g_free (impl->browse_files_last_selected_name);
6639 impl->browse_files_last_selected_name = NULL;
6641 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6647 if (impl->browse_files_last_selected_name)
6649 const char *entry_text;
6651 gboolean clear_entry;
6653 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6654 len = strlen (entry_text);
6657 /* The file chooser entry may have appended a "/" to its text. So
6658 * take it out, and compare the result to the old selection.
6660 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6664 tmp = g_strndup (entry_text, len - 1);
6665 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6669 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6672 clear_entry = FALSE;
6675 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6680 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6681 const GtkFilePath *path,
6684 return gtk_file_chooser_default_update_current_folder (chooser, path, FALSE, FALSE, error);
6688 struct UpdateCurrentFolderData
6690 GtkFileChooserDefault *impl;
6692 gboolean keep_trail;
6693 gboolean clear_entry;
6694 GtkFilePath *original_path;
6695 GError *original_error;
6699 update_current_folder_get_info_cb (GtkFileSystemHandle *handle,
6700 const GtkFileInfo *info,
6701 const GError *error,
6704 gboolean cancelled = handle->cancelled;
6705 struct UpdateCurrentFolderData *data = user_data;
6706 GtkFileChooserDefault *impl = data->impl;
6708 if (handle != impl->update_current_folder_handle)
6711 impl->update_current_folder_handle = NULL;
6712 impl->reload_state = RELOAD_EMPTY;
6714 set_busy_cursor (impl, FALSE);
6721 GtkFilePath *parent_path;
6723 if (!data->original_path)
6725 data->original_path = gtk_file_path_copy (data->path);
6726 data->original_error = g_error_copy (error);
6729 /* get parent path and try to change the folder to that */
6730 if (gtk_file_system_get_parent (impl->file_system, data->path, &parent_path, NULL) &&
6731 parent_path != NULL)
6733 gtk_file_path_free (data->path);
6734 data->path = parent_path;
6736 g_object_unref (handle);
6738 /* restart the update current folder operation */
6739 impl->reload_state = RELOAD_HAS_FOLDER;
6741 impl->update_current_folder_handle =
6742 gtk_file_system_get_info (impl->file_system, data->path,
6743 GTK_FILE_INFO_IS_FOLDER,
6744 update_current_folder_get_info_cb,
6747 set_busy_cursor (impl, TRUE);
6753 /* error and bail out */
6754 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6756 gtk_file_path_free (data->original_path);
6762 if (data->original_path)
6764 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6766 gtk_file_path_free (data->original_path);
6769 if (!gtk_file_info_get_is_folder (info))
6772 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), data->path, data->keep_trail, NULL))
6775 if (impl->current_folder != data->path)
6777 if (impl->current_folder)
6778 gtk_file_path_free (impl->current_folder);
6780 impl->current_folder = gtk_file_path_copy (data->path);
6782 impl->reload_state = RELOAD_HAS_FOLDER;
6785 /* Update the widgets that may trigger a folder change themselves. */
6787 if (!impl->changing_folder)
6789 impl->changing_folder = TRUE;
6791 shortcuts_update_current_folder (impl);
6793 impl->changing_folder = FALSE;
6796 /* Set the folder on the save entry */
6798 if (impl->location_entry)
6800 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6801 impl->current_folder);
6803 if (data->clear_entry)
6804 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6807 /* Create a new list model. This is slightly evil; we store the result value
6808 * but perform more actions rather than returning immediately even if it
6809 * generates an error.
6811 set_list_model (impl, NULL);
6813 /* Refresh controls */
6815 shortcuts_find_current_folder (impl);
6817 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6819 check_preview_change (impl);
6820 bookmarks_check_add_sensitivity (impl);
6822 g_signal_emit_by_name (impl, "selection-changed", 0);
6825 gtk_file_path_free (data->path);
6828 g_object_unref (handle);
6832 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6833 const GtkFilePath *path,
6834 gboolean keep_trail,
6835 gboolean clear_entry,
6838 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6839 struct UpdateCurrentFolderData *data;
6841 profile_start ("start", (char *) path);
6843 switch (impl->operation_mode)
6845 case OPERATION_MODE_SEARCH:
6846 search_switch_to_browse_mode (impl);
6848 case OPERATION_MODE_RECENT:
6849 recent_switch_to_browse_mode (impl);
6851 case OPERATION_MODE_BROWSE:
6855 g_assert (path != NULL);
6857 if (impl->local_only &&
6858 !gtk_file_system_path_is_local (impl->file_system, path))
6861 GTK_FILE_CHOOSER_ERROR,
6862 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6863 _("Cannot change to folder because it is not local"));
6865 profile_end ("end - not local", (char *) path);
6869 if (impl->update_current_folder_handle)
6870 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
6872 /* Test validity of path here. */
6873 data = g_new0 (struct UpdateCurrentFolderData, 1);
6875 data->path = gtk_file_path_copy (path);
6876 data->keep_trail = keep_trail;
6877 data->clear_entry = clear_entry;
6879 impl->reload_state = RELOAD_HAS_FOLDER;
6881 impl->update_current_folder_handle =
6882 gtk_file_system_get_info (impl->file_system, path, GTK_FILE_INFO_IS_FOLDER,
6883 update_current_folder_get_info_cb,
6886 set_busy_cursor (impl, TRUE);
6888 profile_end ("end", NULL);
6892 static GtkFilePath *
6893 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
6895 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6897 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6898 impl->operation_mode == OPERATION_MODE_RECENT)
6901 if (impl->reload_state == RELOAD_EMPTY)
6903 char *current_working_dir;
6906 /* We are unmapped, or we had an error while loading the last folder. We'll return
6907 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
6908 * explicitly calls set_current_folder() on us.
6910 current_working_dir = g_get_current_dir ();
6911 path = gtk_file_system_filename_to_path (impl->file_system, current_working_dir);
6912 g_free (current_working_dir);
6916 return gtk_file_path_copy (impl->current_folder);
6920 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
6923 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6925 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
6926 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6928 pending_select_paths_free (impl);
6929 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
6933 select_func (GtkFileSystemModel *model,
6938 GtkFileChooserDefault *impl = user_data;
6939 GtkTreeSelection *selection;
6940 GtkTreeIter sorted_iter;
6942 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6944 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
6945 gtk_tree_selection_select_iter (selection, &sorted_iter);
6949 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
6950 const GtkFilePath *path,
6953 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6954 GtkFilePath *parent_path;
6957 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
6961 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
6963 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
6964 impl->operation_mode == OPERATION_MODE_RECENT ||
6965 impl->load_state == LOAD_EMPTY)
6971 g_assert (impl->current_folder != NULL);
6973 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
6976 if (same_path && impl->load_state == LOAD_FINISHED)
6981 paths.data = (gpointer) path;
6984 result = show_and_select_paths (impl, parent_path, &paths, error);
6985 gtk_file_path_free (parent_path);
6989 pending_select_paths_add (impl, path);
6995 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
6996 gtk_file_path_free (parent_path);
7000 gtk_file_path_free (parent_path);
7005 unselect_func (GtkFileSystemModel *model,
7010 GtkFileChooserDefault *impl = user_data;
7011 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
7012 GtkTreePath *sorted_path;
7014 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
7016 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
7018 gtk_tree_path_free (sorted_path);
7022 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
7023 const GtkFilePath *path)
7025 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7027 if (!impl->browse_files_model)
7030 _gtk_file_system_model_path_do (impl->browse_files_model, path,
7031 unselect_func, impl);
7035 maybe_select (GtkTreeModel *model,
7040 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
7041 GtkTreeSelection *selection;
7042 const GtkFileInfo *info;
7045 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7047 info = get_list_file_info (impl, iter);
7048 is_folder = gtk_file_info_get_is_folder (info);
7050 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
7051 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
7052 gtk_tree_selection_select_iter (selection, iter);
7054 gtk_tree_selection_unselect_iter (selection, iter);
7060 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
7062 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7064 if (impl->operation_mode == OPERATION_MODE_SEARCH ||
7065 impl->operation_mode == OPERATION_MODE_RECENT)
7067 GtkTreeSelection *selection;
7069 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7070 gtk_tree_selection_select_all (selection);
7074 if (impl->select_multiple)
7075 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
7076 maybe_select, impl);
7080 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
7082 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7083 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7085 gtk_tree_selection_unselect_all (selection);
7086 pending_select_paths_free (impl);
7089 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
7091 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
7093 * is_empty_ret - whether the file entry is totally empty
7095 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
7096 * the path will be "$cwd/foobar")
7099 check_save_entry (GtkFileChooserDefault *impl,
7100 GtkFilePath **path_ret,
7101 gboolean *is_well_formed_ret,
7102 gboolean *is_empty_ret,
7103 gboolean *is_file_part_empty_ret,
7104 gboolean *is_folder)
7106 GtkFileChooserEntry *chooser_entry;
7107 const GtkFilePath *current_folder;
7108 const char *file_part;
7112 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7113 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7114 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7115 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7116 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7118 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7120 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
7123 *is_well_formed_ret = TRUE;
7124 *is_empty_ret = TRUE;
7125 *is_file_part_empty_ret = TRUE;
7131 *is_empty_ret = FALSE;
7133 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7134 if (!current_folder)
7137 *is_well_formed_ret = FALSE;
7138 *is_file_part_empty_ret = FALSE;
7144 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7146 if (!file_part || file_part[0] == '\0')
7148 *path_ret = gtk_file_path_copy (current_folder);
7149 *is_well_formed_ret = TRUE;
7150 *is_file_part_empty_ret = TRUE;
7156 *is_file_part_empty_ret = FALSE;
7159 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
7163 error_building_filename_dialog (impl, current_folder, file_part, error);
7165 *is_well_formed_ret = FALSE;
7172 *is_well_formed_ret = TRUE;
7173 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, path);
7176 struct get_paths_closure {
7177 GtkFileChooserDefault *impl;
7179 GtkFilePath *path_from_entry;
7183 get_paths_foreach (GtkTreeModel *model,
7188 struct get_paths_closure *info;
7189 const GtkFilePath *file_path;
7190 GtkFileSystemModel *fs_model;
7191 GtkTreeIter sel_iter;
7194 fs_model = info->impl->browse_files_model;
7195 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
7197 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
7199 return; /* We are on the editable row */
7201 if (!info->path_from_entry
7202 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
7203 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
7207 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
7209 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7210 struct get_paths_closure info;
7211 GtkWindow *toplevel;
7212 GtkWidget *current_focus;
7213 gboolean file_list_seen;
7215 if (impl->operation_mode == OPERATION_MODE_SEARCH)
7216 return search_get_selected_paths (impl);
7218 if (impl->operation_mode == OPERATION_MODE_RECENT)
7219 return recent_get_selected_paths (impl);
7223 info.path_from_entry = NULL;
7225 toplevel = get_toplevel (GTK_WIDGET (impl));
7227 current_focus = gtk_window_get_focus (toplevel);
7229 current_focus = NULL;
7231 file_list_seen = FALSE;
7232 if (current_focus == impl->browse_files_tree_view)
7234 GtkTreeSelection *selection;
7238 file_list_seen = TRUE;
7239 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7240 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
7242 /* If there is no selection in the file list, we probably have this situation:
7244 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
7245 * 2. He then double-clicked on a folder ("bar") in the file list
7247 * So we want the selection to be "bar/foo.txt". Jump to the case for the
7248 * filename entry to see if that is the case.
7250 if (info.result == NULL && impl->location_entry)
7253 else if (impl->location_entry && current_focus == impl->location_entry)
7255 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
7259 check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7264 if (!is_well_formed)
7267 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7269 gtk_file_path_free (info.path_from_entry);
7273 if (info.path_from_entry)
7274 info.result = g_slist_prepend (info.result, info.path_from_entry);
7275 else if (!file_list_seen)
7280 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7282 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7286 /* The focus is on a dialog's action area button or something else */
7287 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7288 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7296 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
7297 * fall back to the current directory */
7298 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
7299 info.result == NULL)
7301 info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_path (chooser));
7304 return g_slist_reverse (info.result);
7307 static GtkFilePath *
7308 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
7310 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7312 if (impl->preview_path)
7313 return gtk_file_path_copy (impl->preview_path);
7318 static GtkFileSystem *
7319 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
7321 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7323 return impl->file_system;
7326 /* Shows or hides the filter widgets */
7328 show_filters (GtkFileChooserDefault *impl,
7332 gtk_widget_show (impl->filter_combo_hbox);
7334 gtk_widget_hide (impl->filter_combo_hbox);
7338 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
7339 GtkFileFilter *filter)
7341 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7344 if (g_slist_find (impl->filters, filter))
7346 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
7350 g_object_ref_sink (filter);
7351 impl->filters = g_slist_append (impl->filters, filter);
7353 name = gtk_file_filter_get_name (filter);
7355 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
7357 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
7359 if (!g_slist_find (impl->filters, impl->current_filter))
7360 set_current_filter (impl, filter);
7362 show_filters (impl, TRUE);
7366 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
7367 GtkFileFilter *filter)
7369 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7370 GtkTreeModel *model;
7374 filter_index = g_slist_index (impl->filters, filter);
7376 if (filter_index < 0)
7378 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
7382 impl->filters = g_slist_remove (impl->filters, filter);
7384 if (filter == impl->current_filter)
7387 set_current_filter (impl, impl->filters->data);
7389 set_current_filter (impl, NULL);
7392 /* Remove row from the combo box */
7393 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
7394 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
7395 g_assert_not_reached ();
7397 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
7399 g_object_unref (filter);
7402 show_filters (impl, FALSE);
7406 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
7408 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7410 return g_slist_copy (impl->filters);
7413 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
7415 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
7418 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
7421 struct AddShortcutData
7423 GtkFileChooserDefault *impl;
7428 add_shortcut_get_info_cb (GtkFileSystemHandle *handle,
7429 const GtkFileInfo *info,
7430 const GError *error,
7434 gboolean cancelled = handle->cancelled;
7435 struct AddShortcutData *data = user_data;
7437 if (!g_slist_find (data->impl->loading_shortcuts, handle))
7440 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, handle);
7442 if (cancelled || error || !gtk_file_info_get_is_folder (info))
7445 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
7447 shortcuts_insert_path (data->impl, pos, SHORTCUT_TYPE_PATH, NULL, data->path, NULL, FALSE, SHORTCUTS_SHORTCUTS);
7450 g_object_unref (data->impl);
7451 gtk_file_path_free (data->path);
7454 g_object_unref (handle);
7458 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
7459 const GtkFilePath *path,
7462 GtkFileSystemHandle *handle;
7463 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7464 struct AddShortcutData *data;
7468 /* Avoid adding duplicates */
7469 pos = shortcut_find_position (impl, path);
7470 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
7474 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7475 /* translators, "Shortcut" means "Bookmark" here */
7477 GTK_FILE_CHOOSER_ERROR,
7478 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7479 _("Shortcut %s already exists"),
7486 for (l = impl->loading_shortcuts; l; l = l->next)
7488 GtkFileSystemHandle *h = l->data;
7491 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7492 if (p && !gtk_file_path_compare (path, p))
7496 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7498 GTK_FILE_CHOOSER_ERROR,
7499 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
7500 _("Shortcut %s already exists"),
7508 data = g_new0 (struct AddShortcutData, 1);
7509 data->impl = g_object_ref (impl);
7510 data->path = gtk_file_path_copy (path);
7512 handle = gtk_file_system_get_info (impl->file_system, path,
7513 GTK_FILE_INFO_IS_FOLDER,
7514 add_shortcut_get_info_cb, data);
7519 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, handle);
7520 g_object_set_data (G_OBJECT (handle), "add-shortcut-path-key", data->path);
7526 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7527 const GtkFilePath *path,
7530 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7537 for (l = impl->loading_shortcuts; l; l = l->next)
7539 GtkFileSystemHandle *h = l->data;
7542 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7543 if (p && !gtk_file_path_compare (path, p))
7545 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, h);
7546 gtk_file_system_cancel_operation (h);
7551 if (impl->num_shortcuts == 0)
7554 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7555 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7556 g_assert_not_reached ();
7558 for (i = 0; i < impl->num_shortcuts; i++)
7561 ShortcutType shortcut_type;
7562 GtkFilePath *shortcut;
7564 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7565 SHORTCUTS_COL_DATA, &col_data,
7566 SHORTCUTS_COL_TYPE, &shortcut_type,
7568 g_assert (col_data != NULL);
7569 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
7571 shortcut = col_data;
7572 if (gtk_file_path_compare (shortcut, path) == 0)
7574 shortcuts_remove_rows (impl, pos + i, 1);
7575 impl->num_shortcuts--;
7579 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7580 g_assert_not_reached ();
7585 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7586 /* translators, "Shortcut" means "Bookmark" here */
7588 GTK_FILE_CHOOSER_ERROR,
7589 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7590 _("Shortcut %s does not exist"),
7598 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7600 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7606 if (impl->num_shortcuts == 0)
7609 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7610 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7611 g_assert_not_reached ();
7615 for (i = 0; i < impl->num_shortcuts; i++)
7618 ShortcutType shortcut_type;
7619 GtkFilePath *shortcut;
7621 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7622 SHORTCUTS_COL_DATA, &col_data,
7623 SHORTCUTS_COL_TYPE, &shortcut_type,
7625 g_assert (col_data != NULL);
7626 g_assert (shortcut_type == SHORTCUT_TYPE_PATH);
7628 shortcut = col_data;
7629 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
7631 if (i != impl->num_shortcuts - 1)
7633 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7634 g_assert_not_reached ();
7638 return g_slist_reverse (list);
7641 /* Guesses a size based upon font sizes */
7643 find_good_size_from_style (GtkWidget *widget,
7647 GtkFileChooserDefault *impl;
7648 gint default_width, default_height;
7654 g_assert (widget->style != NULL);
7655 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7657 screen = gtk_widget_get_screen (widget);
7660 resolution = gdk_screen_get_resolution (screen);
7661 if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */
7665 resolution = 96.0; /* wheeee */
7667 font_size = pango_font_description_get_size (widget->style->font_desc);
7668 font_size = PANGO_PIXELS (font_size) * resolution / 72.0;
7670 default_width = font_size * NUM_CHARS;
7671 default_height = font_size * NUM_LINES;
7673 if (impl->preview_widget_active && impl->preview_widget)
7675 gtk_widget_size_request (impl->preview_box, &req);
7676 default_width += PREVIEW_HBOX_SPACING + req.width;
7679 if (impl->extra_widget)
7681 gtk_widget_size_request (impl->extra_align, &req);
7682 default_height += GTK_BOX (widget)->spacing + req.height;
7685 gtk_widget_size_request (widget, &req);
7686 default_width = MAX (default_width, req.width);
7687 default_height = MAX (default_height, req.height);
7689 *width = default_width;
7690 *height = default_height;
7694 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7695 gint *default_width,
7696 gint *default_height)
7698 GtkFileChooserDefault *impl;
7700 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7701 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7705 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
7706 gboolean *resize_horizontally,
7707 gboolean *resize_vertically)
7709 GtkFileChooserDefault *impl;
7711 g_return_if_fail (resize_horizontally != NULL);
7712 g_return_if_fail (resize_vertically != NULL);
7714 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7716 *resize_horizontally = TRUE;
7717 *resize_vertically = TRUE;
7719 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7720 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7722 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
7724 *resize_horizontally = FALSE;
7725 *resize_vertically = FALSE;
7730 struct switch_folder_closure {
7731 GtkFileChooserDefault *impl;
7732 const GtkFilePath *path;
7736 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7738 switch_folder_foreach_cb (GtkTreeModel *model,
7743 struct switch_folder_closure *closure;
7744 GtkTreeIter child_iter;
7748 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7750 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
7751 closure->num_selected++;
7754 /* Changes to the selected folder in the list view */
7756 switch_to_selected_folder (GtkFileChooserDefault *impl)
7758 GtkTreeSelection *selection;
7759 struct switch_folder_closure closure;
7761 /* We do this with foreach() rather than get_selected() as we may be in
7762 * multiple selection mode
7765 closure.impl = impl;
7766 closure.path = NULL;
7767 closure.num_selected = 0;
7769 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7770 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7772 g_assert (closure.path && closure.num_selected == 1);
7774 change_folder_and_display_error (impl, closure.path, FALSE);
7777 /* Gets the GtkFileInfo for the selected row in the file list; assumes single
7780 static const GtkFileInfo *
7781 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7782 gboolean *had_selection)
7784 GtkTreeSelection *selection;
7785 GtkTreeIter iter, child_iter;
7786 const GtkFileInfo *info;
7788 g_assert (!impl->select_multiple);
7789 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7790 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7792 *had_selection = FALSE;
7796 *had_selection = TRUE;
7798 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7802 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7806 /* Gets the display name of the selected file in the file list; assumes single
7807 * selection mode and that something is selected.
7809 static const gchar *
7810 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7812 const GtkFileInfo *info;
7813 gboolean had_selection;
7815 info = get_selected_file_info_from_file_list (impl, &had_selection);
7816 g_assert (had_selection);
7817 g_assert (info != NULL);
7819 return gtk_file_info_get_display_name (info);
7823 add_custom_button_to_dialog (GtkDialog *dialog,
7824 const gchar *mnemonic_label,
7825 const gchar *stock_id,
7830 button = gtk_button_new_with_mnemonic (mnemonic_label);
7831 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7832 gtk_button_set_image (GTK_BUTTON (button),
7833 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7834 gtk_widget_show (button);
7836 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7839 /* Presents an overwrite confirmation dialog; returns whether we should accept
7843 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7844 const gchar *file_part,
7845 const gchar *folder_display_name)
7847 GtkWindow *toplevel;
7851 toplevel = get_toplevel (GTK_WIDGET (impl));
7853 dialog = gtk_message_dialog_new (toplevel,
7854 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7855 GTK_MESSAGE_QUESTION,
7857 _("A file named \"%s\" already exists. Do you want to replace it?"),
7859 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7860 _("The file already exists in \"%s\". Replacing it will "
7861 "overwrite its contents."),
7862 folder_display_name);
7864 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7865 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"), GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7866 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7868 if (toplevel->group)
7869 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7871 response = gtk_dialog_run (GTK_DIALOG (dialog));
7873 gtk_widget_destroy (dialog);
7875 return (response == GTK_RESPONSE_ACCEPT);
7878 struct GetDisplayNameData
7880 GtkFileChooserDefault *impl;
7885 confirmation_confirm_get_info_cb (GtkFileSystemHandle *handle,
7886 const GtkFileInfo *info,
7887 const GError *error,
7890 gboolean cancelled = handle->cancelled;
7891 gboolean should_respond = FALSE;
7892 struct GetDisplayNameData *data = user_data;
7894 if (handle != data->impl->should_respond_get_info_handle)
7897 data->impl->should_respond_get_info_handle = NULL;
7903 /* Huh? Did the folder disappear? Let the caller deal with it */
7904 should_respond = TRUE;
7906 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, gtk_file_info_get_display_name (info));
7908 set_busy_cursor (data->impl, FALSE);
7910 g_signal_emit_by_name (data->impl, "response-requested");
7913 g_object_unref (data->impl);
7914 g_free (data->file_part);
7917 g_object_unref (handle);
7920 /* Does overwrite confirmation if appropriate, and returns whether the dialog
7921 * should respond. Can get the file part from the file list or the save entry.
7924 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
7925 const gchar *file_part,
7926 const GtkFilePath *parent_path)
7928 GtkFileChooserConfirmation conf;
7930 if (!impl->do_overwrite_confirmation)
7933 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
7935 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
7939 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
7941 struct GetDisplayNameData *data;
7943 g_assert (file_part != NULL);
7945 data = g_new0 (struct GetDisplayNameData, 1);
7946 data->impl = g_object_ref (impl);
7947 data->file_part = g_strdup (file_part);
7949 if (impl->should_respond_get_info_handle)
7950 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
7952 impl->should_respond_get_info_handle =
7953 gtk_file_system_get_info (impl->file_system, parent_path,
7954 GTK_FILE_INFO_DISPLAY_NAME,
7955 confirmation_confirm_get_info_cb,
7957 set_busy_cursor (data->impl, TRUE);
7961 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
7964 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
7968 g_assert_not_reached ();
7973 /* Gives the focus to the browse tree view only if it is visible */
7975 focus_browse_tree_view_if_possible (GtkFileChooserDefault *impl)
7979 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7980 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7981 && !gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
7987 gtk_widget_grab_focus (impl->browse_files_tree_view);
7991 action_create_folder_cb (GtkFileSystemHandle *handle,
7992 const GtkFilePath *path,
7993 const GError *error,
7996 gboolean cancelled = handle->cancelled;
7997 GtkFileChooserDefault *impl = user_data;
7999 if (!g_slist_find (impl->pending_handles, handle))
8002 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
8004 set_busy_cursor (impl, FALSE);
8010 error_creating_folder_dialog (impl, path, g_error_copy (error));
8012 g_signal_emit_by_name (impl, "response-requested");
8015 g_object_unref (impl);
8016 g_object_unref (handle);
8019 struct FileExistsData
8021 GtkFileChooserDefault *impl;
8022 gboolean file_exists_and_is_not_folder;
8023 GtkFilePath *parent_path;
8028 save_entry_get_info_cb (GtkFileSystemHandle *handle,
8029 const GtkFileInfo *info,
8030 const GError *error,
8033 gboolean parent_is_folder;
8034 gboolean cancelled = handle->cancelled;
8035 struct FileExistsData *data = user_data;
8037 if (handle != data->impl->should_respond_get_info_handle)
8040 data->impl->should_respond_get_info_handle = NULL;
8042 set_busy_cursor (data->impl, FALSE);
8048 parent_is_folder = FALSE;
8050 parent_is_folder = gtk_file_info_get_is_folder (info);
8052 if (parent_is_folder)
8054 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8056 if (data->file_exists_and_is_not_folder)
8059 const char *file_part;
8061 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
8062 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_path);
8065 g_signal_emit_by_name (data->impl, "response-requested");
8068 g_signal_emit_by_name (data->impl, "response-requested");
8070 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
8072 GtkFileSystemHandle *handle;
8074 g_object_ref (data->impl);
8075 handle = gtk_file_system_create_folder (data->impl->file_system,
8077 action_create_folder_cb,
8079 data->impl->pending_handles = g_slist_append (data->impl->pending_handles, handle);
8080 set_busy_cursor (data->impl, TRUE);
8085 /* This will display an error, which is what we want */
8086 change_folder_and_display_error (data->impl, data->parent_path, FALSE);
8090 g_object_unref (data->impl);
8091 gtk_file_path_free (data->path);
8092 gtk_file_path_free (data->parent_path);
8095 g_object_unref (handle);
8099 file_exists_get_info_cb (GtkFileSystemHandle *handle,
8100 const GtkFileInfo *info,
8101 const GError *error,
8104 gboolean data_ownership_taken = FALSE;
8105 gboolean cancelled = handle->cancelled;
8106 gboolean file_exists_and_is_not_folder;
8107 struct FileExistsData *data = user_data;
8109 if (handle != data->impl->file_exists_get_info_handle)
8112 data->impl->file_exists_get_info_handle = NULL;
8114 set_busy_cursor (data->impl, FALSE);
8119 file_exists_and_is_not_folder = info && !gtk_file_info_get_is_folder (info);
8121 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
8122 /* user typed a filename; we are done */
8123 g_signal_emit_by_name (data->impl, "response-requested");
8124 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8125 && file_exists_and_is_not_folder)
8127 /* Oops, the user typed the name of an existing path which is not
8130 error_creating_folder_over_existing_file_dialog (data->impl, data->path,
8131 g_error_copy (error));
8135 /* check that everything up to the last component exists */
8137 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
8138 data_ownership_taken = TRUE;
8140 if (data->impl->should_respond_get_info_handle)
8141 gtk_file_system_cancel_operation (data->impl->should_respond_get_info_handle);
8143 data->impl->should_respond_get_info_handle =
8144 gtk_file_system_get_info (data->impl->file_system,
8146 GTK_FILE_INFO_IS_FOLDER,
8147 save_entry_get_info_cb,
8149 set_busy_cursor (data->impl, TRUE);
8153 if (!data_ownership_taken)
8155 g_object_unref (data->impl);
8156 gtk_file_path_free (data->path);
8157 gtk_file_path_free (data->parent_path);
8161 g_object_unref (handle);
8165 paste_text_received (GtkClipboard *clipboard,
8167 GtkFileChooserDefault *impl)
8174 path = gtk_file_system_uri_to_path (impl->file_system, text);
8177 if (!g_path_is_absolute (text))
8179 location_popup_handler (impl, text);
8183 path = gtk_file_system_filename_to_path (impl->file_system, text);
8186 location_popup_handler (impl, text);
8191 if (!gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (impl), path, NULL))
8192 location_popup_handler (impl, text);
8194 gtk_file_path_free (path);
8197 /* Handler for the "location-popup-on-paste" keybinding signal */
8199 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
8201 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
8202 GDK_SELECTION_CLIPBOARD);
8203 gtk_clipboard_request_text (clipboard,
8204 (GtkClipboardTextReceivedFunc) paste_text_received,
8209 /* Implementation for GtkFileChooserEmbed::should_respond() */
8211 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
8213 GtkFileChooserDefault *impl;
8214 GtkWidget *toplevel;
8215 GtkWidget *current_focus;
8217 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8219 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
8220 g_assert (GTK_IS_WINDOW (toplevel));
8222 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
8224 if (current_focus == impl->browse_files_tree_view)
8226 /* The following array encodes what we do based on the impl->action and the
8227 * number of files selected.
8230 NOOP, /* Do nothing (don't respond) */
8231 RESPOND, /* Respond immediately */
8232 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
8233 ALL_FILES, /* Respond only if everything selected is a file */
8234 ALL_FOLDERS, /* Respond only if everything selected is a folder */
8235 SAVE_ENTRY, /* Go to the code for handling the save entry */
8236 NOT_REACHED /* Sanity check */
8238 static const ActionToTake what_to_do[4][3] = {
8239 /* 0 selected 1 selected many selected */
8240 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
8241 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
8242 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
8243 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
8247 gboolean all_files, all_folders;
8249 ActionToTake action;
8253 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
8255 if (impl->operation_mode == OPERATION_MODE_SEARCH)
8256 return search_should_respond (impl);
8258 if (impl->operation_mode == OPERATION_MODE_RECENT)
8259 return recent_should_respond (impl);
8261 selection_check (impl, &num_selected, &all_files, &all_folders);
8263 if (num_selected > 2)
8268 action = what_to_do [impl->action] [k];
8278 case RESPOND_OR_SWITCH:
8279 g_assert (num_selected == 1);
8283 switch_to_selected_folder (impl);
8286 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8287 return should_respond_after_confirm_overwrite (impl,
8288 get_display_name_from_file_list (impl),
8289 impl->current_folder);
8303 g_assert_not_reached ();
8306 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
8309 gboolean is_well_formed, is_empty, is_file_part_empty;
8312 GtkFileChooserEntry *entry;
8317 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8318 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
8319 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8320 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8321 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
8323 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
8324 check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
8326 if (is_empty || !is_well_formed)
8329 g_assert (path != NULL);
8334 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8335 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8337 change_folder_and_display_error (impl, path, TRUE);
8340 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8341 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8343 /* The folder already exists, so we do not need to create it.
8344 * Just respond to terminate the dialog.
8350 g_assert_not_reached ();
8356 struct FileExistsData *data;
8358 /* We need to check whether path exists and is not a folder */
8360 data = g_new0 (struct FileExistsData, 1);
8361 data->impl = g_object_ref (impl);
8362 data->path = gtk_file_path_copy (path);
8363 data->parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
8365 if (impl->file_exists_get_info_handle)
8366 gtk_file_system_cancel_operation (impl->file_exists_get_info_handle);
8368 impl->file_exists_get_info_handle =
8369 gtk_file_system_get_info (impl->file_system, path,
8370 GTK_FILE_INFO_IS_FOLDER,
8371 file_exists_get_info_cb,
8374 set_busy_cursor (impl, TRUE);
8378 g_error_free (error);
8381 gtk_file_path_free (path);
8384 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
8386 /* The focus is on a dialog's action area button, *and* the widget that
8387 * was focused immediately before it is the shortcuts list. Switch to the
8388 * selected shortcut and tell the caller not to respond.
8392 if (shortcuts_get_selected (impl, &iter))
8394 shortcuts_activate_iter (impl, &iter);
8396 focus_browse_tree_view_if_possible (impl);
8403 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
8405 /* The focus is on a dialog's action area button, *and* the widget that
8406 * was focused immediately before it is the file list.
8410 else if (impl->operation_mode == OPERATION_MODE_SEARCH && impl->toplevel_last_focus_widget == impl->search_entry)
8412 search_entry_activate_cb (GTK_ENTRY (impl->search_entry), impl);
8415 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
8417 /* The focus is on a dialog's action area button, *and* the widget that
8418 * was focused immediately before it is the location entry.
8423 /* The focus is on a dialog's action area button or something else */
8424 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8425 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8430 g_assert_not_reached ();
8434 /* Implementation for GtkFileChooserEmbed::initial_focus() */
8436 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
8438 GtkFileChooserDefault *impl;
8441 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
8443 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8444 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8446 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8447 widget = impl->browse_files_tree_view;
8449 widget = impl->location_entry;
8451 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
8452 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8453 widget = impl->location_entry;
8456 g_assert_not_reached ();
8460 g_assert (widget != NULL);
8461 gtk_widget_grab_focus (widget);
8464 /* Callback used from gtk_tree_selection_selected_foreach(); gets the selected GtkFilePaths */
8466 search_selected_foreach_get_path_cb (GtkTreeModel *model,
8472 const GtkFilePath *file_path;
8473 GtkFilePath *file_path_copy;
8477 gtk_tree_model_get (model, iter, SEARCH_MODEL_COL_PATH, &file_path, -1);
8478 file_path_copy = gtk_file_path_copy (file_path);
8479 *list = g_slist_prepend (*list, file_path_copy);
8482 /* Constructs a list of the selected paths in search mode */
8484 search_get_selected_paths (GtkFileChooserDefault *impl)
8487 GtkTreeSelection *selection;
8491 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8492 gtk_tree_selection_selected_foreach (selection, search_selected_foreach_get_path_cb, &result);
8493 result = g_slist_reverse (result);
8498 /* Called from ::should_respond(). We return whether there are selected files
8499 * in the search list.
8502 search_should_respond (GtkFileChooserDefault *impl)
8504 GtkTreeSelection *selection;
8506 g_assert (impl->operation_mode == OPERATION_MODE_SEARCH);
8508 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
8509 return (gtk_tree_selection_count_selected_rows (selection) != 0);
8512 struct SearchHitInsertRequest
8514 GtkFileChooserDefault *impl;
8516 GtkTreeRowReference *row_ref;
8520 search_hit_get_info_cb (GtkFileSystemHandle *handle,
8521 const GtkFileInfo *info,
8522 const GError *error,
8525 gboolean cancelled = handle->cancelled;
8526 GdkPixbuf *pixbuf = NULL;
8529 GtkFileSystemHandle *model_handle;
8530 gboolean is_folder = FALSE;
8532 struct SearchHitInsertRequest *request = data;
8534 path = gtk_tree_row_reference_get_path (request->row_ref);
8538 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->search_model),
8540 gtk_tree_path_free (path);
8542 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->search_model), &iter,
8543 SEARCH_MODEL_COL_HANDLE, &model_handle,
8545 if (handle != model_handle)
8548 /* set the handle to NULL in the model */
8549 gtk_list_store_set (request->impl->search_model, &iter,
8550 SEARCH_MODEL_COL_HANDLE, NULL,
8558 gtk_list_store_remove (request->impl->search_model, &iter);
8562 mime_type = g_strdup (gtk_file_info_get_mime_type (info));
8563 is_folder = gtk_file_info_get_is_folder (info);
8564 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
8565 request->impl->icon_size, NULL);
8567 gtk_list_store_set (request->impl->search_model, &iter,
8568 SEARCH_MODEL_COL_PIXBUF, pixbuf,
8569 SEARCH_MODEL_COL_MIME_TYPE, mime_type,
8570 SEARCH_MODEL_COL_IS_FOLDER, is_folder,
8574 g_object_unref (pixbuf);
8577 g_object_unref (request->impl);
8578 gtk_file_path_free (request->path);
8579 gtk_tree_row_reference_free (request->row_ref);
8582 g_object_unref (handle);
8585 /* Adds one hit from the search engine to the search_model */
8587 search_add_hit (GtkFileChooserDefault *impl,
8593 char *collation_key;
8594 struct stat statbuf;
8595 struct stat *statbuf_copy;
8598 GtkFileSystemHandle *handle;
8599 struct SearchHitInsertRequest *request;
8601 path = gtk_file_system_uri_to_path (impl->file_system, uri);
8605 filename = gtk_file_system_path_to_filename (impl->file_system, path);
8608 gtk_file_path_free (path);
8612 if (stat (filename, &statbuf) != 0)
8614 gtk_file_path_free (path);
8619 statbuf_copy = g_new (struct stat, 1);
8620 *statbuf_copy = statbuf;
8622 display_name = g_filename_display_name (filename);
8623 collation_key = g_utf8_collate_key_for_filename (display_name, -1);
8625 request = g_new0 (struct SearchHitInsertRequest, 1);
8626 request->impl = g_object_ref (impl);
8627 request->path = gtk_file_path_copy (path);
8629 gtk_list_store_append (impl->search_model, &iter);
8630 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->search_model), &iter);
8632 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->search_model), p);
8633 gtk_tree_path_free (p);
8635 handle = gtk_file_system_get_info (impl->file_system, path,
8636 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_ICON | GTK_FILE_INFO_MIME_TYPE,
8637 search_hit_get_info_cb,
8640 gtk_list_store_set (impl->search_model, &iter,
8641 SEARCH_MODEL_COL_PATH, path,
8642 SEARCH_MODEL_COL_DISPLAY_NAME, display_name,
8643 SEARCH_MODEL_COL_COLLATION_KEY, collation_key,
8644 SEARCH_MODEL_COL_STAT, statbuf_copy,
8645 SEARCH_MODEL_COL_HANDLE, handle,
8649 /* Callback used from GtkSearchEngine when we get new hits */
8651 search_engine_hits_added_cb (GtkSearchEngine *engine,
8655 GtkFileChooserDefault *impl;
8658 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8660 for (l = hits; l; l = l->next)
8661 search_add_hit (impl, (gchar*)l->data);
8664 /* Callback used from GtkSearchEngine when the query is done running */
8666 search_engine_finished_cb (GtkSearchEngine *engine,
8669 GtkFileChooserDefault *impl;
8671 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8673 /* FMQ: if search was empty, say that we got no hits */
8675 set_busy_cursor (impl, FALSE);
8678 /* Displays a generic error when we cannot create a GtkSearchEngine.
8679 * It would be better if _gtk_search_engine_new() gave us a GError
8680 * with a better message, but it doesn't do that right now.
8683 search_error_could_not_create_client (GtkFileChooserDefault *impl)
8685 error_message (impl,
8686 _("Could not start the search process"),
8687 _("The program was not able to create a connection to the indexer "
8688 "daemon. Please make sure it is running."));
8692 search_engine_error_cb (GtkSearchEngine *engine,
8693 const gchar *message,
8696 GtkFileChooserDefault *impl;
8698 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8700 search_stop_searching (impl, TRUE);
8701 error_message (impl, _("Could not send the search request"), message);
8703 set_busy_cursor (impl, FALSE);
8706 /* Frees the data in the search_model */
8708 search_clear_model (GtkFileChooserDefault *impl,
8709 gboolean remove_from_treeview)
8711 GtkTreeModel *model;
8714 if (!impl->search_model)
8717 model = GTK_TREE_MODEL (impl->search_model);
8719 if (gtk_tree_model_get_iter_first (model, &iter))
8723 gchar *display_name;
8724 gchar *collation_key;
8725 struct stat *statbuf;
8726 GtkFileSystemHandle *handle;
8728 gtk_tree_model_get (model, &iter,
8729 SEARCH_MODEL_COL_PATH, &path,
8730 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
8731 SEARCH_MODEL_COL_COLLATION_KEY, &collation_key,
8732 SEARCH_MODEL_COL_STAT, &statbuf,
8733 SEARCH_MODEL_COL_HANDLE, &handle,
8737 gtk_file_system_cancel_operation (handle);
8739 gtk_file_path_free (path);
8740 g_free (display_name);
8741 g_free (collation_key);
8744 while (gtk_tree_model_iter_next (model, &iter));
8746 g_object_unref (impl->search_model);
8747 impl->search_model = NULL;
8749 if (remove_from_treeview)
8750 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
8753 /* Stops any ongoing searches; does not touch the search_model */
8755 search_stop_searching (GtkFileChooserDefault *impl,
8756 gboolean remove_query)
8758 if (remove_query && impl->search_query)
8760 g_object_unref (impl->search_query);
8761 impl->search_query = NULL;
8764 if (impl->search_engine)
8766 g_object_unref (impl->search_engine);
8767 impl->search_engine = NULL;
8771 /* Stops any pending searches, clears the file list, and switches back to OPERATION_MODE_BROWSE */
8773 search_switch_to_browse_mode (GtkFileChooserDefault *impl)
8775 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
8777 search_stop_searching (impl, FALSE);
8778 search_clear_model (impl, TRUE);
8780 gtk_widget_destroy (impl->search_hbox);
8781 impl->search_hbox = NULL;
8782 impl->search_entry = NULL;
8784 gtk_widget_show (impl->browse_path_bar);
8785 gtk_widget_show (impl->browse_new_folder_button);
8787 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8788 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8790 gtk_widget_show (impl->location_button);
8792 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8793 gtk_widget_show (impl->location_entry_box);
8796 impl->operation_mode = OPERATION_MODE_BROWSE;
8798 file_list_set_sort_column_ids (impl);
8801 /* Sort callback from the path column */
8803 search_column_path_sort_func (GtkTreeModel *model,
8808 const char *collation_key_a, *collation_key_b;
8810 gtk_tree_model_get (model, a, SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_a, -1);
8811 gtk_tree_model_get (model, b, SEARCH_MODEL_COL_COLLATION_KEY, &collation_key_b, -1);
8813 return strcmp (collation_key_a, collation_key_b);
8816 /* Sort callback from the modification time column */
8818 search_column_mtime_sort_func (GtkTreeModel *model,
8823 const struct stat *statbuf_a, *statbuf_b;
8825 /* Note that although we store a whole struct stat in the model, we only
8826 * compare the mtime here. If we add another column relative to a struct stat
8827 * (e.g. a file size column), we'll want another sort callback similar to this
8831 gtk_tree_model_get (model, a, SEARCH_MODEL_COL_STAT, &statbuf_a, -1);
8832 gtk_tree_model_get (model, b, SEARCH_MODEL_COL_STAT, &statbuf_b, -1);
8834 if (statbuf_a->st_mtime < statbuf_b->st_mtime)
8836 else if (statbuf_a->st_mtime > statbuf_b->st_mtime)
8842 /* Creates the search_model and puts it in the tree view */
8844 search_setup_model (GtkFileChooserDefault *impl)
8846 g_assert (impl->search_model == NULL);
8848 /* We store these columns in the search model:
8850 * SEARCH_MODEL_COL_PATH - a GtkFilePath for the hit's URI, stored as a
8851 * pointer not as a GTK_TYPE_FILE_PATH
8852 * SEARCH_MODEL_COL_DISPLAY_NAME - a string with the display name, stored
8853 * as a pointer not as a G_TYPE_STRING
8854 * SEARCH_MODEL_COL_COLLATION_KEY - collation key for the filename, stored
8855 * as a pointer not as a G_TYPE_STRING
8856 * SEARCH_MODEL_COL_STAT - pointer to a struct stat
8857 * SEARCH_MODEL_COL_HANDLE - handle used when getting the hit's info
8858 * SEARCH_MODEL_COL_PIXBUF - GdkPixbuf for the hit's icon
8859 * SEARCH_MODEL_COL_MIME_TYPE - a string with the hit's MIME type
8860 * SEARCH_MODEL_COL_IS_FOLDER - a boolean flag for folders
8862 * Keep this in sync with the enumeration defined near the beginning
8865 impl->search_model = gtk_list_store_new (SEARCH_MODEL_COL_NUM_COLUMNS,
8874 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8875 SEARCH_MODEL_COL_PATH,
8876 search_column_path_sort_func,
8879 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->search_model),
8880 SEARCH_MODEL_COL_STAT,
8881 search_column_mtime_sort_func,
8885 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->search_model),
8886 SEARCH_MODEL_COL_STAT,
8887 GTK_SORT_DESCENDING);
8889 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
8890 GTK_TREE_MODEL (impl->search_model));
8893 /* Creates a new query with the specified text and launches it */
8895 search_start_query (GtkFileChooserDefault *impl,
8896 const gchar *query_text)
8898 search_stop_searching (impl, FALSE);
8899 search_clear_model (impl, TRUE);
8900 search_setup_model (impl);
8901 set_busy_cursor (impl, TRUE);
8903 if (impl->search_engine == NULL)
8904 impl->search_engine = _gtk_search_engine_new ();
8906 if (!impl->search_engine)
8908 set_busy_cursor (impl, FALSE);
8909 search_error_could_not_create_client (impl); /* lame; we don't get an error code or anything */
8913 if (!impl->search_query)
8915 impl->search_query = _gtk_query_new ();
8916 _gtk_query_set_text (impl->search_query, query_text);
8919 _gtk_search_engine_set_query (impl->search_engine, impl->search_query);
8921 g_signal_connect (impl->search_engine, "hits-added",
8922 G_CALLBACK (search_engine_hits_added_cb), impl);
8923 g_signal_connect (impl->search_engine, "finished",
8924 G_CALLBACK (search_engine_finished_cb), impl);
8925 g_signal_connect (impl->search_engine, "error",
8926 G_CALLBACK (search_engine_error_cb), impl);
8928 _gtk_search_engine_start (impl->search_engine);
8931 /* Callback used when the user presses Enter while typing on the search
8932 * entry; starts the query
8935 search_entry_activate_cb (GtkEntry *entry,
8938 GtkFileChooserDefault *impl;
8941 impl = GTK_FILE_CHOOSER_DEFAULT (data);
8943 text = gtk_entry_get_text (GTK_ENTRY (impl->search_entry));
8944 if (strlen (text) == 0)
8947 /* reset any existing query object */
8948 if (impl->search_query)
8950 g_object_unref (impl->search_query);
8951 impl->search_query = NULL;
8954 search_start_query (impl, text);
8957 /* Hides the path bar and creates the search entry */
8959 search_setup_widgets (GtkFileChooserDefault *impl)
8964 impl->search_hbox = gtk_hbox_new (FALSE, 12);
8968 label = gtk_label_new (NULL);
8969 text = g_strconcat ("<b>", _("Search:"), "</b>", NULL);
8970 gtk_label_set_markup (GTK_LABEL (label), text);
8972 gtk_box_pack_start (GTK_BOX (impl->search_hbox), label, FALSE, FALSE, 0);
8976 impl->search_entry = gtk_entry_new ();
8977 g_signal_connect (impl->search_entry, "activate",
8978 G_CALLBACK (search_entry_activate_cb),
8980 gtk_box_pack_start (GTK_BOX (impl->search_hbox), impl->search_entry, TRUE, TRUE, 0);
8982 /* if there already is a query, restart it */
8983 if (impl->search_query)
8985 gchar *query = _gtk_query_get_text (impl->search_query);
8989 gtk_entry_set_text (GTK_ENTRY (impl->search_entry), query);
8990 search_start_query (impl, query);
8996 g_object_unref (impl->search_query);
8997 impl->search_query = NULL;
9001 gtk_widget_hide (impl->browse_path_bar);
9002 gtk_widget_hide (impl->browse_new_folder_button);
9004 /* Box for search widgets */
9005 gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->search_hbox, TRUE, TRUE, 0);
9006 gtk_widget_show_all (impl->search_hbox);
9008 /* Hide the location widgets temporarily */
9010 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9011 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9013 gtk_widget_hide (impl->location_button);
9014 gtk_widget_hide (impl->location_entry_box);
9017 gtk_widget_grab_focus (impl->search_entry);
9019 /* FMQ: hide the filter combo? */
9022 /* Main entry point to the searching functions; this gets called when the user
9023 * activates the Search shortcut.
9026 search_activate (GtkFileChooserDefault *impl)
9028 OperationMode previous_mode;
9030 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9032 gtk_widget_grab_focus (impl->search_entry);
9036 previous_mode = impl->operation_mode;
9037 impl->operation_mode = OPERATION_MODE_SEARCH;
9039 switch (previous_mode)
9041 case OPERATION_MODE_RECENT:
9042 recent_stop_loading (impl);
9043 recent_clear_model (impl, TRUE);
9046 case OPERATION_MODE_BROWSE:
9047 stop_loading_and_clear_list_model (impl);
9050 case OPERATION_MODE_SEARCH:
9051 g_assert_not_reached ();
9055 g_assert (impl->search_hbox == NULL);
9056 g_assert (impl->search_entry == NULL);
9057 g_assert (impl->search_model == NULL);
9059 search_setup_widgets (impl);
9060 file_list_set_sort_column_ids (impl);
9064 * Recent files support
9067 /* Frees the data in the recent_model */
9069 recent_clear_model (GtkFileChooserDefault *impl,
9070 gboolean remove_from_treeview)
9072 GtkTreeModel *model;
9075 if (!impl->recent_model)
9078 model = GTK_TREE_MODEL (impl->recent_model);
9080 if (gtk_tree_model_get_iter_first (model, &iter))
9084 GtkFilePath *file_path;
9085 GtkFileSystemHandle *handle;
9086 GtkRecentInfo *recent_info;
9087 gchar *display_name;
9089 gtk_tree_model_get (model, &iter,
9090 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
9091 RECENT_MODEL_COL_PATH, &file_path,
9092 RECENT_MODEL_COL_HANDLE, &handle,
9093 RECENT_MODEL_COL_INFO, &recent_info,
9097 gtk_file_system_cancel_operation (handle);
9099 gtk_file_path_free (file_path);
9100 gtk_recent_info_unref (recent_info);
9101 g_free (display_name);
9103 while (gtk_tree_model_iter_next (model, &iter));
9106 g_object_unref (impl->recent_model);
9107 impl->recent_model = NULL;
9109 if (remove_from_treeview)
9110 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
9113 /* Stops any ongoing loading of the recent files list; does
9114 * not touch the recent_model
9117 recent_stop_loading (GtkFileChooserDefault *impl)
9119 if (impl->load_recent_id)
9121 g_source_remove (impl->load_recent_id);
9122 impl->load_recent_id = 0;
9126 /* Stops any pending load, clears the file list, and switches
9127 * back to OPERATION_MODE_BROWSE
9130 recent_switch_to_browse_mode (GtkFileChooserDefault *impl)
9132 g_assert (impl->operation_mode != OPERATION_MODE_BROWSE);
9134 recent_stop_loading (impl);
9135 recent_clear_model (impl, TRUE);
9137 gtk_widget_show (impl->browse_path_bar);
9138 gtk_widget_show (impl->browse_new_folder_button);
9140 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9141 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9143 gtk_widget_show (impl->location_button);
9145 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
9146 gtk_widget_show (impl->location_entry_box);
9149 impl->operation_mode = OPERATION_MODE_BROWSE;
9151 file_list_set_sort_column_ids (impl);
9154 /* Sort callback from the modification time column */
9156 recent_column_mtime_sort_func (GtkTreeModel *model,
9161 GtkRecentInfo *info_a, *info_b;
9163 gtk_tree_model_get (model, a, RECENT_MODEL_COL_INFO, &info_a, -1);
9164 gtk_tree_model_get (model, b, RECENT_MODEL_COL_INFO, &info_b, -1);
9166 if (gtk_recent_info_get_modified (info_a) < gtk_recent_info_get_modified (info_b))
9168 else if (gtk_recent_info_get_modified (info_a) > gtk_recent_info_get_modified (info_b))
9176 recent_setup_model (GtkFileChooserDefault *impl)
9178 g_assert (impl->recent_model == NULL);
9180 /* We store these columns in the search model:
9182 * RECENT_MODEL_COL_PATH - a pointer to GtkFilePath for the hit's URI,
9183 * stored as a pointer and not as a GTK_TYPE_FILE_PATH;
9184 * RECENT_MODEL_COL_DISPLAY_NAME - a string with the display name,
9185 * stored as a pointer and not as a G_TYPE_STRING;
9186 * RECENT_MODEL_COL_INFO - GtkRecentInfo, stored as a pointer and not
9187 * as a GTK_TYPE_RECENT_INFO;
9188 * RECENT_MODEL_COL_IS_FOLDER - boolean flag;
9189 * RECENT_MODEL_COL_HANDLE - GtkFileSystemHandle, stored as a pointer
9190 * and not as a GTK_TYPE_FILE_SYSTEM_HANDLE;
9192 * Keep this in sync with the enumeration defined near the beginning of
9195 impl->recent_model = gtk_list_store_new (RECENT_MODEL_COL_NUM_COLUMNS,
9202 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->recent_model),
9203 RECENT_MODEL_COL_INFO,
9204 recent_column_mtime_sort_func,
9208 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->recent_model),
9209 RECENT_MODEL_COL_INFO,
9210 GTK_SORT_DESCENDING);
9212 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
9213 GTK_TREE_MODEL (impl->recent_model));
9218 GtkFileChooserDefault *impl;
9221 gint n_loaded_items;
9225 recent_idle_cleanup (gpointer data)
9227 RecentLoadData *load_data = data;
9229 set_busy_cursor (load_data->impl, FALSE);
9231 load_data->impl->load_recent_id = 0;
9233 if (load_data->items)
9235 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9236 g_list_free (load_data->items);
9242 struct RecentItemInsertRequest
9244 GtkFileChooserDefault *impl;
9246 GtkTreeRowReference *row_ref;
9250 recent_item_get_info_cb (GtkFileSystemHandle *handle,
9251 const GtkFileInfo *info,
9252 const GError *error,
9255 gboolean cancelled = handle->cancelled;
9258 GtkFileSystemHandle *model_handle;
9259 gboolean is_folder = FALSE;
9260 struct RecentItemInsertRequest *request = data;
9262 path = gtk_tree_row_reference_get_path (request->row_ref);
9266 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->recent_model),
9268 gtk_tree_path_free (path);
9270 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->recent_model), &iter,
9271 RECENT_MODEL_COL_HANDLE, &model_handle,
9273 if (handle != model_handle)
9276 gtk_list_store_set (request->impl->recent_model, &iter,
9277 RECENT_MODEL_COL_HANDLE, NULL,
9285 gtk_list_store_remove (request->impl->recent_model, &iter);
9289 is_folder = gtk_file_info_get_is_folder (info);
9291 gtk_list_store_set (request->impl->recent_model, &iter,
9292 RECENT_MODEL_COL_IS_FOLDER, is_folder,
9296 g_object_unref (request->impl);
9297 gtk_file_path_free (request->path);
9298 gtk_tree_row_reference_free (request->row_ref);
9301 g_object_unref (handle);
9305 recent_idle_load (gpointer data)
9307 RecentLoadData *load_data = data;
9308 GtkFileChooserDefault *impl = load_data->impl;
9311 GtkRecentInfo *info;
9312 const gchar *uri, *display_name;
9314 GtkFileSystemHandle *handle;
9315 struct RecentItemInsertRequest *request;
9317 if (!impl->recent_manager)
9320 if (!load_data->items)
9322 load_data->items = gtk_recent_manager_get_items (impl->recent_manager);
9323 load_data->n_items = g_list_length (load_data->items);
9324 load_data->n_loaded_items = 0;
9329 info = g_list_nth_data (load_data->items, load_data->n_loaded_items);
9330 g_assert (info != NULL);
9332 uri = gtk_recent_info_get_uri (info);
9333 display_name = gtk_recent_info_get_display_name (info);
9334 path = gtk_file_system_uri_to_path (impl->file_system, uri);
9338 gtk_list_store_append (impl->recent_model, &iter);
9339 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->recent_model), &iter);
9341 request = g_new0 (struct RecentItemInsertRequest, 1);
9342 request->impl = g_object_ref (impl);
9343 request->path = gtk_file_path_copy (path);
9344 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->recent_model), p);
9345 gtk_tree_path_free (p);
9347 handle = gtk_file_system_get_info (impl->file_system, path,
9348 GTK_FILE_INFO_IS_FOLDER,
9349 recent_item_get_info_cb,
9352 gtk_list_store_set (impl->recent_model, &iter,
9353 RECENT_MODEL_COL_PATH, path,
9354 RECENT_MODEL_COL_DISPLAY_NAME, g_strdup (display_name),
9355 RECENT_MODEL_COL_INFO, gtk_recent_info_ref (info),
9356 RECENT_MODEL_COL_HANDLE, handle,
9361 load_data->n_loaded_items += 1;
9363 /* finished loading items */
9364 if (load_data->n_loaded_items == load_data->n_items)
9366 g_list_foreach (load_data->items, (GFunc) gtk_recent_info_unref, NULL);
9367 g_list_free (load_data->items);
9368 load_data->items = NULL;
9377 recent_start_loading (GtkFileChooserDefault *impl)
9379 RecentLoadData *load_data;
9381 recent_stop_loading (impl);
9382 recent_clear_model (impl, TRUE);
9383 recent_setup_model (impl);
9384 set_busy_cursor (impl, TRUE);
9386 if (!impl->recent_manager)
9387 recent_manager_update (impl);
9389 g_assert (impl->load_recent_id == 0);
9391 load_data = g_new (RecentLoadData, 1);
9392 load_data->impl = impl;
9393 load_data->items = NULL;
9394 load_data->n_items = 0;
9395 load_data->n_loaded_items = 0;
9397 /* begin lazy loading the recent files into the model */
9398 impl->load_recent_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
9401 recent_idle_cleanup);
9405 recent_selected_foreach_get_path_cb (GtkTreeModel *model,
9411 const GtkFilePath *file_path;
9412 GtkFilePath *file_path_copy;
9416 gtk_tree_model_get (model, iter, RECENT_MODEL_COL_PATH, &file_path, -1);
9417 file_path_copy = gtk_file_path_copy (file_path);
9418 *list = g_slist_prepend (*list, file_path_copy);
9421 /* Constructs a list of the selected paths in recent files mode */
9423 recent_get_selected_paths (GtkFileChooserDefault *impl)
9426 GtkTreeSelection *selection;
9430 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9431 gtk_tree_selection_selected_foreach (selection, recent_selected_foreach_get_path_cb, &result);
9432 result = g_slist_reverse (result);
9437 /* Called from ::should_respond(). We return whether there are selected
9438 * files in the recent files list.
9441 recent_should_respond (GtkFileChooserDefault *impl)
9443 GtkTreeSelection *selection;
9445 g_assert (impl->operation_mode == OPERATION_MODE_RECENT);
9447 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
9448 return (gtk_tree_selection_count_selected_rows (selection) != 0);
9451 /* Hide the location widgets temporarily */
9453 recent_hide_entry (GtkFileChooserDefault *impl)
9455 gtk_widget_hide (impl->browse_path_bar);
9456 gtk_widget_hide (impl->browse_new_folder_button);
9458 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
9459 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
9461 gtk_widget_hide (impl->location_button);
9462 gtk_widget_hide (impl->location_entry_box);
9465 /* EB: hide the filter combo? */
9468 /* Main entry point to the recent files functions; this gets called when
9469 * the user activates the Recently Used shortcut.
9472 recent_activate (GtkFileChooserDefault *impl)
9474 OperationMode previous_mode;
9476 if (impl->operation_mode == OPERATION_MODE_RECENT)
9479 previous_mode = impl->operation_mode;
9480 impl->operation_mode = OPERATION_MODE_RECENT;
9482 switch (previous_mode)
9484 case OPERATION_MODE_SEARCH:
9485 search_stop_searching (impl, FALSE);
9486 search_clear_model (impl, TRUE);
9488 gtk_widget_destroy (impl->search_hbox);
9489 impl->search_hbox = NULL;
9490 impl->search_entry = NULL;
9493 case OPERATION_MODE_BROWSE:
9494 stop_loading_and_clear_list_model (impl);
9497 case OPERATION_MODE_RECENT:
9498 g_assert_not_reached ();
9502 recent_hide_entry (impl);
9503 file_list_set_sort_column_ids (impl);
9504 recent_start_loading (impl);
9509 set_current_filter (GtkFileChooserDefault *impl,
9510 GtkFileFilter *filter)
9512 if (impl->current_filter != filter)
9516 /* NULL filters are allowed to reset to non-filtered status
9518 filter_index = g_slist_index (impl->filters, filter);
9519 if (impl->filters && filter && filter_index < 0)
9522 if (impl->current_filter)
9523 g_object_unref (impl->current_filter);
9524 impl->current_filter = filter;
9525 if (impl->current_filter)
9527 g_object_ref_sink (impl->current_filter);
9531 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
9534 if (impl->browse_files_model)
9535 install_list_model_filter (impl);
9537 g_object_notify (G_OBJECT (impl), "filter");
9542 filter_combo_changed (GtkComboBox *combo_box,
9543 GtkFileChooserDefault *impl)
9545 gint new_index = gtk_combo_box_get_active (combo_box);
9546 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
9548 set_current_filter (impl, new_filter);
9552 check_preview_change (GtkFileChooserDefault *impl)
9554 GtkTreePath *cursor_path;
9555 const GtkFilePath *new_path;
9556 const char *new_display_name;
9558 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
9560 new_display_name = NULL;
9563 if (impl->operation_mode == OPERATION_MODE_BROWSE)
9565 if (impl->sort_model)
9568 GtkTreeIter child_iter;
9569 const GtkFileInfo *new_info;
9571 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
9572 gtk_tree_path_free (cursor_path);
9574 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
9576 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
9577 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
9579 new_display_name = gtk_file_info_get_display_name (new_info);
9582 else if (impl->operation_mode == OPERATION_MODE_SEARCH)
9586 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model), &iter, cursor_path);
9587 gtk_tree_path_free (cursor_path);
9589 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &iter,
9590 SEARCH_MODEL_COL_PATH, &new_path,
9591 SEARCH_MODEL_COL_DISPLAY_NAME, &new_display_name,
9594 else if (impl->operation_mode == OPERATION_MODE_RECENT)
9598 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model), &iter, cursor_path);
9599 gtk_tree_path_free (cursor_path);
9601 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &iter,
9602 RECENT_MODEL_COL_PATH, &new_path,
9603 RECENT_MODEL_COL_DISPLAY_NAME, &new_display_name,
9608 if (new_path != impl->preview_path &&
9609 !(new_path && impl->preview_path &&
9610 gtk_file_path_compare (new_path, impl->preview_path) == 0))
9612 if (impl->preview_path)
9614 gtk_file_path_free (impl->preview_path);
9615 g_free (impl->preview_display_name);
9620 impl->preview_path = gtk_file_path_copy (new_path);
9621 impl->preview_display_name = g_strdup (new_display_name);
9625 impl->preview_path = NULL;
9626 impl->preview_display_name = NULL;
9629 if (impl->use_preview_label && impl->preview_label)
9630 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
9632 g_signal_emit_by_name (impl, "update-preview");
9637 shortcuts_activate_volume_mount_cb (GtkFileSystemHandle *handle,
9638 GtkFileSystemVolume *volume,
9639 const GError *error,
9643 gboolean cancelled = handle->cancelled;
9644 GtkFileChooserDefault *impl = data;
9646 if (handle != impl->shortcuts_activate_iter_handle)
9649 impl->shortcuts_activate_iter_handle = NULL;
9651 set_busy_cursor (impl, FALSE);
9660 msg = g_strdup_printf (_("Could not mount %s"),
9661 gtk_file_system_volume_get_display_name (impl->file_system, volume));
9662 error_message (impl, msg, error->message);
9668 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
9671 change_folder_and_display_error (impl, path, FALSE);
9672 focus_browse_tree_view_if_possible (impl);
9674 gtk_file_path_free (path);
9678 g_object_unref (impl);
9679 g_object_unref (handle);
9683 /* Activates a volume by mounting it if necessary and then switching to its
9687 shortcuts_activate_volume (GtkFileChooserDefault *impl,
9688 GtkFileSystemVolume *volume)
9692 switch (impl->operation_mode)
9694 case OPERATION_MODE_BROWSE:
9696 case OPERATION_MODE_SEARCH:
9697 search_switch_to_browse_mode (impl);
9699 case OPERATION_MODE_RECENT:
9700 recent_switch_to_browse_mode (impl);
9704 /* We ref the file chooser since volume_mount() may run a main loop, and the
9705 * user could close the file chooser window in the meantime.
9707 g_object_ref (impl);
9709 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
9711 set_busy_cursor (impl, TRUE);
9713 impl->shortcuts_activate_iter_handle =
9714 gtk_file_system_volume_mount (impl->file_system, volume,
9715 shortcuts_activate_volume_mount_cb,
9716 g_object_ref (impl));
9720 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
9723 change_folder_and_display_error (impl, path, FALSE);
9724 gtk_file_path_free (path);
9728 g_object_unref (impl);
9731 /* Opens the folder or volume at the specified iter in the shortcuts model */
9732 struct ShortcutsActivateData
9734 GtkFileChooserDefault *impl;
9739 shortcuts_activate_get_info_cb (GtkFileSystemHandle *handle,
9740 const GtkFileInfo *info,
9741 const GError *error,
9744 gboolean cancelled = handle->cancelled;
9745 struct ShortcutsActivateData *data = user_data;
9747 if (handle != data->impl->shortcuts_activate_iter_handle)
9750 data->impl->shortcuts_activate_iter_handle = NULL;
9755 if (!error && gtk_file_info_get_is_folder (info))
9757 change_folder_and_display_error (data->impl, data->path, FALSE);
9758 focus_browse_tree_view_if_possible (data->impl);
9761 gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (data->impl),
9766 g_object_unref (data->impl);
9767 gtk_file_path_free (data->path);
9770 g_object_unref (handle);
9774 shortcuts_activate_iter (GtkFileChooserDefault *impl,
9778 ShortcutType shortcut_type;
9780 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY && impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
9781 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
9783 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
9784 SHORTCUTS_COL_DATA, &col_data,
9785 SHORTCUTS_COL_TYPE, &shortcut_type,
9788 if (impl->shortcuts_activate_iter_handle)
9790 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
9791 impl->shortcuts_activate_iter_handle = NULL;
9794 if (shortcut_type == SHORTCUT_TYPE_SEPARATOR)
9796 else if (shortcut_type == SHORTCUT_TYPE_VOLUME)
9798 GtkFileSystemVolume *volume;
9802 shortcuts_activate_volume (impl, volume);
9804 else if (shortcut_type == SHORTCUT_TYPE_PATH)
9806 struct ShortcutsActivateData *data;
9808 data = g_new0 (struct ShortcutsActivateData, 1);
9809 data->impl = g_object_ref (impl);
9810 data->path = gtk_file_path_copy (col_data);
9812 impl->shortcuts_activate_iter_handle =
9813 gtk_file_system_get_info (impl->file_system, data->path,
9814 GTK_FILE_INFO_IS_FOLDER,
9815 shortcuts_activate_get_info_cb, data);
9817 else if (shortcut_type == SHORTCUT_TYPE_SEARCH)
9819 search_activate (impl);
9821 else if (shortcut_type == SHORTCUT_TYPE_RECENT)
9823 recent_activate (impl);
9827 /* Callback used when a row in the shortcuts list is activated */
9829 shortcuts_row_activated_cb (GtkTreeView *tree_view,
9831 GtkTreeViewColumn *column,
9832 GtkFileChooserDefault *impl)
9835 GtkTreeIter child_iter;
9837 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &iter, path))
9840 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_pane_filter_model),
9843 shortcuts_activate_iter (impl, &child_iter);
9846 /* Handler for GtkWidget::key-press-event on the shortcuts list */
9848 shortcuts_key_press_event_cb (GtkWidget *widget,
9850 GtkFileChooserDefault *impl)
9854 modifiers = gtk_accelerator_get_default_mod_mask ();
9856 if ((event->keyval == GDK_BackSpace
9857 || event->keyval == GDK_Delete
9858 || event->keyval == GDK_KP_Delete)
9859 && (event->state & modifiers) == 0)
9861 remove_selected_bookmarks (impl);
9865 if ((event->keyval == GDK_F2)
9866 && (event->state & modifiers) == 0)
9868 rename_selected_bookmark (impl);
9876 shortcuts_select_func (GtkTreeSelection *selection,
9877 GtkTreeModel *model,
9879 gboolean path_currently_selected,
9882 GtkFileChooserDefault *impl = data;
9883 GtkTreeIter filter_iter;
9884 ShortcutType shortcut_type;
9886 if (!gtk_tree_model_get_iter (impl->shortcuts_pane_filter_model, &filter_iter, path))
9887 g_assert_not_reached ();
9889 gtk_tree_model_get (impl->shortcuts_pane_filter_model, &filter_iter, SHORTCUTS_COL_TYPE, &shortcut_type, -1);
9891 return shortcut_type != SHORTCUT_TYPE_SEPARATOR;
9895 list_select_func (GtkTreeSelection *selection,
9896 GtkTreeModel *model,
9898 gboolean path_currently_selected,
9901 GtkFileChooserDefault *impl = data;
9903 if (impl->operation_mode == OPERATION_MODE_SEARCH)
9906 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
9907 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
9909 GtkTreeIter iter, child_iter;
9910 const GtkFileInfo *info;
9912 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
9915 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
9917 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
9919 if (info && !gtk_file_info_get_is_folder (info))
9927 list_selection_changed (GtkTreeSelection *selection,
9928 GtkFileChooserDefault *impl)
9930 /* See if we are in the new folder editable row for Save mode */
9931 if (impl->operation_mode == OPERATION_MODE_BROWSE &&
9932 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
9934 const GtkFileInfo *info;
9935 gboolean had_selection;
9937 info = get_selected_file_info_from_file_list (impl, &had_selection);
9939 goto out; /* normal processing */
9942 return; /* We are on the editable row for New Folder */
9947 /* TODO - Change the following functions to make them accept MODE_SEARCH */
9948 if (impl->location_entry)
9949 update_chooser_entry (impl);
9951 check_preview_change (impl);
9952 bookmarks_check_add_sensitivity (impl);
9954 g_signal_emit_by_name (impl, "selection-changed", 0);
9957 /* Callback used when a row in the file list is activated */
9959 list_row_activated (GtkTreeView *tree_view,
9961 GtkTreeViewColumn *column,
9962 GtkFileChooserDefault *impl)
9966 switch (impl->operation_mode)
9968 case OPERATION_MODE_SEARCH:
9970 GtkFilePath *file_path;
9973 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->search_model), &iter, path))
9976 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), &iter,
9977 SEARCH_MODEL_COL_PATH, &file_path,
9978 SEARCH_MODEL_COL_IS_FOLDER, &is_folder,
9983 change_folder_and_display_error (impl, file_path, FALSE);
9984 search_switch_to_browse_mode (impl);
9988 g_signal_emit_by_name (impl, "file-activated");
9992 case OPERATION_MODE_RECENT:
9994 GtkFilePath *file_path;
9997 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->recent_model), &iter, path))
10000 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), &iter,
10001 RECENT_MODEL_COL_PATH, &file_path,
10002 RECENT_MODEL_COL_IS_FOLDER, &is_folder,
10007 change_folder_and_display_error (impl, file_path, FALSE);
10008 recent_switch_to_browse_mode (impl);
10012 g_signal_emit_by_name (impl, "file-activated");
10016 case OPERATION_MODE_BROWSE:
10018 GtkTreeIter child_iter;
10019 const GtkFileInfo *info;
10021 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
10024 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10025 &child_iter, &iter);
10026 info = _gtk_file_system_model_get_info (impl->browse_files_model,
10029 if (gtk_file_info_get_is_folder (info))
10031 const GtkFilePath *file_path;
10033 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
10034 change_folder_and_display_error (impl, file_path, FALSE);
10038 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10039 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
10040 g_signal_emit_by_name (impl, "file-activated");
10047 path_bar_clicked (GtkPathBar *path_bar,
10048 GtkFilePath *file_path,
10049 GtkFilePath *child_path,
10050 gboolean child_is_hidden,
10051 GtkFileChooserDefault *impl)
10054 pending_select_paths_add (impl, child_path);
10056 if (!change_folder_and_display_error (impl, file_path, FALSE))
10059 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
10060 * show hidden files so that ".baz" appears in the file list, as it will still
10061 * be shown in the path bar: "/foo/[bar]/.baz"
10063 if (child_is_hidden)
10064 g_object_set (impl, "show-hidden", TRUE, NULL);
10067 static const GtkFileInfo *
10068 get_list_file_info (GtkFileChooserDefault *impl,
10071 GtkTreeIter child_iter;
10073 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10077 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
10081 list_icon_data_func (GtkTreeViewColumn *tree_column,
10082 GtkCellRenderer *cell,
10083 GtkTreeModel *tree_model,
10087 GtkFileChooserDefault *impl = data;
10088 GtkTreeIter child_iter;
10089 GdkPixbuf *pixbuf = NULL;
10090 gboolean sensitive = TRUE;
10092 profile_start ("start", NULL);
10094 switch (impl->operation_mode)
10096 case OPERATION_MODE_SEARCH:
10097 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), iter,
10098 SEARCH_MODEL_COL_PIXBUF, &pixbuf,
10103 case OPERATION_MODE_RECENT:
10105 GtkRecentInfo *info;
10107 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
10108 RECENT_MODEL_COL_INFO, &info,
10111 pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
10113 if (impl->local_only && !gtk_recent_info_is_local (info))
10118 case OPERATION_MODE_BROWSE:
10120 const GtkFileInfo *info;
10121 const GtkFilePath *path;
10123 info = get_list_file_info (impl, iter);
10125 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
10128 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
10133 /* FIXME: NULL GError */
10134 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (impl),
10135 impl->icon_size, NULL);
10140 /* We are on the editable row */
10145 (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10146 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
10147 sensitive = gtk_file_info_get_is_folder (info);
10152 g_object_set (cell,
10154 "sensitive", sensitive,
10158 g_object_unref (pixbuf);
10160 profile_end ("end", NULL);
10164 list_name_data_func (GtkTreeViewColumn *tree_column,
10165 GtkCellRenderer *cell,
10166 GtkTreeModel *tree_model,
10170 GtkFileChooserDefault *impl = data;
10171 const GtkFileInfo *info;
10172 gboolean sensitive;
10174 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10176 char *display_name;
10178 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), iter,
10179 SEARCH_MODEL_COL_DISPLAY_NAME, &display_name,
10181 g_object_set (cell,
10182 "text", display_name,
10184 "ellipsize", PANGO_ELLIPSIZE_START,
10189 if (impl->operation_mode == OPERATION_MODE_RECENT)
10191 char *display_name;
10193 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
10194 RECENT_MODEL_COL_DISPLAY_NAME, &display_name,
10196 g_object_set (cell,
10197 "text", display_name,
10199 "ellipsize", PANGO_ELLIPSIZE_END,
10204 info = get_list_file_info (impl, iter);
10209 g_object_set (cell,
10210 "text", _("Type name of new folder"),
10212 "ellipsize", PANGO_ELLIPSIZE_NONE,
10219 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10220 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10222 sensitive = gtk_file_info_get_is_folder (info);
10225 g_object_set (cell,
10226 "text", gtk_file_info_get_display_name (info),
10227 "sensitive", sensitive,
10228 "ellipsize", PANGO_ELLIPSIZE_END,
10234 list_size_data_func (GtkTreeViewColumn *tree_column,
10235 GtkCellRenderer *cell,
10236 GtkTreeModel *tree_model,
10240 GtkFileChooserDefault *impl = data;
10241 const GtkFileInfo *info = get_list_file_info (impl, iter);
10244 gboolean sensitive = TRUE;
10246 if (!info || gtk_file_info_get_is_folder (info))
10248 g_object_set (cell,
10250 "sensitive", sensitive,
10255 size = gtk_file_info_get_size (info);
10257 if (size < (gint64)1024)
10258 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
10259 else if (size < (gint64)1024*1024)
10260 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
10261 else if (size < (gint64)1024*1024*1024)
10262 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
10264 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
10266 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
10267 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10268 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10271 g_object_set (cell,
10273 "sensitive", sensitive,
10274 "alignment", PANGO_ALIGN_RIGHT,
10281 /* Tree column data callback for the file list; fetches the mtime of a file */
10283 list_mtime_data_func (GtkTreeViewColumn *tree_column,
10284 GtkCellRenderer *cell,
10285 GtkTreeModel *tree_model,
10289 GtkFileChooserDefault *impl;
10290 GtkFileTime time_mtime;
10294 gboolean sensitive = TRUE;
10298 if (impl->operation_mode == OPERATION_MODE_SEARCH)
10300 struct stat *statbuf;
10302 gtk_tree_model_get (GTK_TREE_MODEL (impl->search_model), iter,
10303 SEARCH_MODEL_COL_STAT, &statbuf,
10305 time_mtime = statbuf->st_mtime;
10307 else if (impl->operation_mode == OPERATION_MODE_RECENT)
10309 GtkRecentInfo *info;
10311 gtk_tree_model_get (GTK_TREE_MODEL (impl->recent_model), iter,
10312 RECENT_MODEL_COL_INFO, &info,
10314 time_mtime = (GtkFileTime) gtk_recent_info_get_modified (info);
10316 if (impl->local_only && !gtk_recent_info_is_local (info))
10321 const GtkFileInfo *info;
10323 info = get_list_file_info (impl, iter);
10326 g_object_set (cell,
10333 time_mtime = gtk_file_info_get_modification_time (info);
10335 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
10336 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10337 sensitive = gtk_file_info_get_is_folder (info);
10340 if (time_mtime == 0)
10341 strcpy (buf, _("Unknown"));
10345 g_date_set_time_t (&mtime, time_mtime);
10346 time_now = time (NULL);
10347 g_date_set_time_t (&now, time_now);
10349 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
10351 if (days_diff == 0)
10352 strcpy (buf, _("Today"));
10353 else if (days_diff == 1)
10354 strcpy (buf, _("Yesterday"));
10359 if (days_diff > 1 && days_diff < 7)
10360 format = "%A"; /* Days from last week */
10362 format = "%x"; /* Any other date */
10364 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
10365 strcpy (buf, _("Unknown"));
10369 g_object_set (cell,
10371 "sensitive", sensitive,
10376 _gtk_file_chooser_default_new (const char *file_system)
10378 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
10379 "file-system-backend", file_system,
10384 location_set_user_text (GtkFileChooserDefault *impl,
10387 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
10388 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
10392 location_popup_handler (GtkFileChooserDefault *impl,
10395 if (impl->operation_mode != OPERATION_MODE_BROWSE)
10397 GtkWidget *widget_to_focus;
10399 /* This will give us the location widgets back */
10400 switch (impl->operation_mode)
10402 case OPERATION_MODE_SEARCH:
10403 search_switch_to_browse_mode (impl);
10405 case OPERATION_MODE_RECENT:
10406 recent_switch_to_browse_mode (impl);
10408 case OPERATION_MODE_BROWSE:
10409 g_assert_not_reached ();
10413 if (impl->current_folder)
10414 change_folder_and_display_error (impl, impl->current_folder, FALSE);
10416 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10417 widget_to_focus = impl->browse_files_tree_view;
10419 widget_to_focus = impl->location_entry;
10421 gtk_widget_grab_focus (widget_to_focus);
10425 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
10426 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
10428 LocationMode new_mode;
10432 /* since the user typed something, we unconditionally want to turn on the entry */
10433 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10435 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
10436 new_mode = LOCATION_MODE_FILENAME_ENTRY;
10437 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
10438 new_mode = LOCATION_MODE_PATH_BAR;
10441 g_assert_not_reached ();
10445 location_mode_set (impl, new_mode, TRUE);
10446 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
10449 location_set_user_text (impl, path);
10452 location_entry_set_initial_text (impl);
10453 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
10457 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
10458 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
10460 gtk_widget_grab_focus (impl->location_entry);
10462 location_set_user_text (impl, path);
10465 g_assert_not_reached ();
10468 /* Handler for the "up-folder" keybinding signal */
10470 up_folder_handler (GtkFileChooserDefault *impl)
10472 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
10475 /* Handler for the "down-folder" keybinding signal */
10477 down_folder_handler (GtkFileChooserDefault *impl)
10479 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
10482 /* Switches to the shortcut in the specified index */
10484 switch_to_shortcut (GtkFileChooserDefault *impl,
10489 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
10490 g_assert_not_reached ();
10492 shortcuts_activate_iter (impl, &iter);
10493 focus_browse_tree_view_if_possible (impl);
10496 /* Handler for the "home-folder" keybinding signal */
10498 home_folder_handler (GtkFileChooserDefault *impl)
10500 if (impl->has_home)
10501 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
10504 /* Handler for the "desktop-folder" keybinding signal */
10506 desktop_folder_handler (GtkFileChooserDefault *impl)
10508 if (impl->has_desktop)
10509 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
10512 /* Handler for the "search-shortcut" keybinding signal */
10514 search_shortcut_handler (GtkFileChooserDefault *impl)
10516 if (impl->has_search)
10517 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_SEARCH));
10520 /* Handler for the "recent-shortcut" keybinding signal */
10522 recent_shortcut_handler (GtkFileChooserDefault *impl)
10524 if (impl->has_recent)
10525 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_RECENT));
10529 quick_bookmark_handler (GtkFileChooserDefault *impl,
10530 gint bookmark_index)
10535 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
10538 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
10540 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
10541 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
10544 gtk_tree_path_free (path);
10546 switch_to_shortcut (impl, bookmark_pos);
10550 show_hidden_handler (GtkFileChooserDefault *impl)
10552 g_object_set (impl,
10553 "show-hidden", !impl->show_hidden,
10558 /* Drag and drop interfaces */
10561 _shortcuts_pane_model_filter_class_init (ShortcutsPaneModelFilterClass *class)
10566 _shortcuts_pane_model_filter_init (ShortcutsPaneModelFilter *model)
10568 model->impl = NULL;
10571 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
10573 shortcuts_pane_model_filter_row_draggable (GtkTreeDragSource *drag_source,
10576 ShortcutsPaneModelFilter *model;
10580 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10582 pos = *gtk_tree_path_get_indices (path);
10583 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
10585 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
10588 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
10590 shortcuts_pane_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
10592 GtkSelectionData *selection_data)
10594 ShortcutsPaneModelFilter *model;
10596 model = SHORTCUTS_PANE_MODEL_FILTER (drag_source);
10603 /* Fill the GtkTreeDragSourceIface vtable */
10605 shortcuts_pane_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
10607 iface->row_draggable = shortcuts_pane_model_filter_row_draggable;
10608 iface->drag_data_get = shortcuts_pane_model_filter_drag_data_get;
10612 /* Fill the GtkTreeDragDestIface vtable */
10614 shortcuts_pane_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
10616 iface->drag_data_received = shortcuts_pane_model_filter_drag_data_received;
10617 iface->row_drop_possible = shortcuts_pane_model_filter_row_drop_possible;
10621 static GtkTreeModel *
10622 shortcuts_pane_model_filter_new (GtkFileChooserDefault *impl,
10623 GtkTreeModel *child_model,
10626 ShortcutsPaneModelFilter *model;
10628 model = g_object_new (SHORTCUTS_PANE_MODEL_FILTER_TYPE,
10629 "child-model", child_model,
10630 "virtual-root", root,
10633 model->impl = impl;
10635 return GTK_TREE_MODEL (model);