1 /* GTK - The GIMP Toolkit
2 * gtkfilesystemmodel.c: GtkTreeModel wrapping a GtkFileSystem
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include "gtkfilechooserprivate.h"
25 #include "gtkfilesystemmodel.h"
26 #include "gtkfilesystem.h"
28 #include "gtkmarshalers.h"
29 #include "gtktreednd.h"
30 #include "gtktreemodel.h"
33 typedef struct _GtkFileSystemModelClass GtkFileSystemModelClass;
35 #define GTK_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
36 #define GTK_IS_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_MODEL))
37 #define GTK_FILE_SYSTEM_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
39 struct _GtkFileSystemModelClass
41 GObjectClass parent_class;
45 void (*finished_loading) (GtkFileSystemModel *model);
49 static void gtk_file_system_model_iface_init (GtkTreeModelIface *iface);
50 static void gtk_file_system_model_finalize (GObject *object);
51 static void gtk_file_system_model_dispose (GObject *object);
53 static void drag_source_iface_init (GtkTreeDragSourceIface *iface);
55 static GtkTreeModelFlags gtk_file_system_model_get_flags (GtkTreeModel *tree_model);
56 static gint gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model);
57 static GType gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
59 static gboolean gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
62 static GtkTreePath * gtk_file_system_model_get_path (GtkTreeModel *tree_model,
64 static void gtk_file_system_model_get_value (GtkTreeModel *tree_model,
68 static gboolean gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
70 static gboolean gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
73 static gboolean gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
75 static gint gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
77 static gboolean gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
81 static gboolean gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
84 static void gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
86 static void gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
89 static gboolean drag_source_row_draggable (GtkTreeDragSource *drag_source,
91 static gboolean drag_source_drag_data_get (GtkTreeDragSource *drag_source,
93 GtkSelectionData *selection_data);
95 static FileModelNode *file_model_node_new (GtkFileSystemModel *model,
96 const GtkFilePath *path);
97 static void file_model_node_free (FileModelNode *node);
98 static void file_model_node_ref (FileModelNode *node);
99 static void file_model_node_unref (GtkFileSystemModel *model,
100 FileModelNode *node);
102 static void file_model_node_idle_clear (FileModelNode *node);
103 static void file_model_node_idle_clear_cancel (FileModelNode *node);
104 static void file_model_node_child_unref (FileModelNode *parent);
106 static const GtkFileInfo *file_model_node_get_info (GtkFileSystemModel *model,
107 FileModelNode *node);
108 static gboolean file_model_node_is_visible (GtkFileSystemModel *model,
109 FileModelNode *node);
110 static void file_model_node_clear (GtkFileSystemModel *model,
111 FileModelNode *node);
112 static FileModelNode * file_model_node_get_children (GtkFileSystemModel *model,
113 FileModelNode *node);
115 static void deleted_callback (GtkFileFolder *folder,
116 FileModelNode *node);
117 static void files_added_callback (GtkFileFolder *folder,
119 FileModelNode *node);
120 static void files_changed_callback (GtkFileFolder *folder,
122 FileModelNode *node);
123 static void files_removed_callback (GtkFileFolder *folder,
125 FileModelNode *node);
127 static void root_deleted_callback (GtkFileFolder *folder,
128 GtkFileSystemModel *model);
129 static void root_files_added_callback (GtkFileFolder *folder,
131 GtkFileSystemModel *model);
132 static void root_files_changed_callback (GtkFileFolder *folder,
134 GtkFileSystemModel *model);
135 static void root_files_removed_callback (GtkFileFolder *folder,
137 GtkFileSystemModel *model);
145 static guint file_system_model_signals[LAST_SIGNAL] = { 0 };
149 G_DEFINE_TYPE_WITH_CODE (GtkFileSystemModel, _gtk_file_system_model, G_TYPE_OBJECT,
150 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
151 gtk_file_system_model_iface_init)
152 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
153 drag_source_iface_init))
156 _gtk_file_system_model_class_init (GtkFileSystemModelClass *class)
158 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
160 gobject_class->finalize = gtk_file_system_model_finalize;
161 gobject_class->dispose = gtk_file_system_model_dispose;
163 file_system_model_signals[FINISHED_LOADING] =
164 g_signal_new (I_("finished-loading"),
165 G_OBJECT_CLASS_TYPE (gobject_class),
167 G_STRUCT_OFFSET (GtkFileSystemModelClass, finished_loading),
169 _gtk_marshal_VOID__VOID,
174 gtk_file_system_model_iface_init (GtkTreeModelIface *iface)
176 iface->get_flags = gtk_file_system_model_get_flags;
177 iface->get_n_columns = gtk_file_system_model_get_n_columns;
178 iface->get_column_type = gtk_file_system_model_get_column_type;
179 iface->get_iter = gtk_file_system_model_get_iter;
180 iface->get_path = gtk_file_system_model_get_path;
181 iface->get_value = gtk_file_system_model_get_value;
182 iface->iter_next = gtk_file_system_model_iter_next;
183 iface->iter_children = gtk_file_system_model_iter_children;
184 iface->iter_has_child = gtk_file_system_model_iter_has_child;
185 iface->iter_n_children = gtk_file_system_model_iter_n_children;
186 iface->iter_nth_child = gtk_file_system_model_iter_nth_child;
187 iface->iter_parent = gtk_file_system_model_iter_parent;
188 iface->ref_node = gtk_file_system_model_ref_node;
189 iface->unref_node = gtk_file_system_model_unref_node;
193 _gtk_file_system_model_init (GtkFileSystemModel *model)
195 model->show_files = TRUE;
196 model->show_folders = TRUE;
197 model->show_hidden = FALSE;
201 gtk_file_system_model_finalize (GObject *object)
203 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
204 FileModelNode *children, *next;
206 if (model->root_folder)
207 g_object_unref (model->root_folder);
209 if (model->root_path)
210 gtk_file_path_free (model->root_path);
212 if (model->file_system)
213 g_object_unref (model->file_system);
215 children = model->roots;
218 next = children->next;
219 file_model_node_free (children);
223 G_OBJECT_CLASS (_gtk_file_system_model_parent_class)->finalize (object);
228 gtk_file_system_model_dispose (GObject *object)
230 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
232 if (model->pending_handles)
236 for (l = model->pending_handles; l; l = l->next)
237 gtk_file_system_cancel_operation (l->data);
238 g_slist_free (model->pending_handles);
239 model->pending_handles = NULL;
242 G_OBJECT_CLASS (_gtk_file_system_model_parent_class)->dispose (object);
246 drag_source_iface_init (GtkTreeDragSourceIface *iface)
248 iface->row_draggable = drag_source_row_draggable;
249 iface->drag_data_get = drag_source_drag_data_get;
250 iface->drag_data_delete = NULL;
254 * ******************** GtkTreeModel methods ********************
257 static GtkTreeModelFlags
258 gtk_file_system_model_get_flags (GtkTreeModel *tree_model)
260 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
261 GtkTreeModelFlags flags = GTK_TREE_MODEL_ITERS_PERSIST;
263 if (model->max_depth == 0)
264 flags |= GTK_TREE_MODEL_LIST_ONLY;
270 gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model)
272 return GTK_FILE_SYSTEM_MODEL_N_COLUMNS;
276 gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
281 case GTK_FILE_SYSTEM_MODEL_INFO:
282 return GTK_TYPE_FILE_INFO;
283 case GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME:
284 return G_TYPE_STRING;
286 g_assert_not_reached ();
292 gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
300 indices = gtk_tree_path_get_indices (path);
301 depth = gtk_tree_path_get_depth (path);
303 g_return_val_if_fail (depth > 0, FALSE);
305 if (!gtk_tree_model_iter_nth_child (tree_model, iter, NULL, indices[0]))
308 for (i = 1; i < depth; i++)
311 if (!gtk_tree_model_iter_nth_child (tree_model, iter, &parent, indices[i]))
319 gtk_file_system_model_get_path (GtkTreeModel *tree_model,
322 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
323 FileModelNode *node = iter->user_data;
325 GtkTreePath *result = gtk_tree_path_new ();
329 FileModelNode *parent = node->parent;
330 FileModelNode *children;
334 children = parent->children;
336 children = model->roots;
338 while (children != node)
340 if (children->is_visible)
342 children = children->next;
345 gtk_tree_path_prepend_index (result, n);
354 gtk_file_system_model_get_value (GtkTreeModel *tree_model,
359 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
360 FileModelNode *node = iter->user_data;
361 const GtkFileInfo *info;
365 case GTK_FILE_SYSTEM_MODEL_INFO:
366 if (model->has_editable && node == model->roots)
369 info = file_model_node_get_info (model, node);
371 g_value_init (value, GTK_TYPE_FILE_INFO);
372 g_value_set_boxed (value, info);
374 case GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME:
376 g_value_init (value, G_TYPE_STRING);
378 if (model->has_editable && node == model->roots)
379 g_value_set_string (value, "");
382 const GtkFileInfo *info = file_model_node_get_info (model, node);
384 g_value_set_string (value, gtk_file_info_get_display_name (info));
389 g_assert_not_reached ();
394 gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
397 FileModelNode *node = iter->user_data;
400 while (node && !node->is_visible)
403 iter->user_data = node;
409 gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
413 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
414 FileModelNode *children;
418 FileModelNode *parent_node = parent->user_data;
419 children = file_model_node_get_children (model, parent_node);
423 children = model->roots;
426 while (children && !children->is_visible)
427 children = children->next;
429 iter->user_data = children;
431 return children != NULL;
435 gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
438 FileModelNode *node = iter->user_data;
439 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
441 if (node->depth == model->max_depth)
445 const GtkFileInfo *info = file_model_node_get_info (model, node);
446 return gtk_file_info_get_is_folder (info);
451 gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
454 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
455 FileModelNode *children;
460 FileModelNode *node = iter->user_data;
461 children = file_model_node_get_children (model, node);
465 children = model->roots;
470 if (children->is_visible)
472 children = children->next;
479 gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
484 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
485 FileModelNode *children;
489 FileModelNode *parent_node = parent->user_data;
490 children = file_model_node_get_children (model, parent_node);
494 children = model->roots;
497 while (children && !children->is_visible)
498 children = children->next;
500 while (n && children)
503 children = children->next;
504 while (children && !children->is_visible)
505 children = children->next;
508 iter->user_data = children;
510 return children != NULL;
514 gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
518 FileModelNode *node = child->user_data;
521 iter->user_data = node;
527 gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
530 file_model_node_ref (iter->user_data);
534 gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
537 file_model_node_unref (GTK_FILE_SYSTEM_MODEL (tree_model),
542 drag_source_row_draggable (GtkTreeDragSource *drag_source,
545 GtkFileSystemModel *model;
549 model = GTK_FILE_SYSTEM_MODEL (drag_source);
551 if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
554 if (!model->has_editable)
557 node = iter.user_data;
558 return (node != model->roots);
562 drag_source_drag_data_get (GtkTreeDragSource *drag_source,
564 GtkSelectionData *selection_data)
566 GtkFileSystemModel *model;
568 const GtkFilePath *file_path;
572 model = GTK_FILE_SYSTEM_MODEL (drag_source);
574 if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
577 file_path = _gtk_file_system_model_get_path (model, &iter);
578 g_assert (file_path != NULL);
580 uri = gtk_file_system_path_to_uri (model->file_system, file_path);
581 uris = g_strconcat (uri, "\r\n", NULL);
583 gtk_selection_data_set (selection_data,
584 gdk_atom_intern_static_string ("text/uri-list"),
595 /* Callback used when the root folder finished loading */
597 root_folder_finished_loading_cb (GtkFileFolder *folder,
598 GtkFileSystemModel *model)
600 g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0);
604 got_root_folder_cb (GtkFileSystemHandle *handle,
605 GtkFileFolder *folder,
609 GSList *roots = NULL;
611 gboolean cancelled = handle->cancelled;
612 GtkFileSystemModel *model = data;
614 tmp_list = g_slist_find (model->pending_handles, handle);
618 model->pending_handles = g_slist_delete_link (model->pending_handles,
621 if (cancelled || !folder)
624 model->root_folder = folder;
626 if (gtk_file_folder_is_finished_loading (model->root_folder))
627 g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0);
629 g_signal_connect_object (model->root_folder, "finished-loading",
630 G_CALLBACK (root_folder_finished_loading_cb), model, 0);
632 gtk_file_folder_list_children (model->root_folder, &roots, NULL);
634 g_signal_connect_object (model->root_folder, "deleted",
635 G_CALLBACK (root_deleted_callback), model, 0);
636 g_signal_connect_object (model->root_folder, "files-added",
637 G_CALLBACK (root_files_added_callback), model, 0);
638 g_signal_connect_object (model->root_folder, "files-changed",
639 G_CALLBACK (root_files_changed_callback), model, 0);
640 g_signal_connect_object (model->root_folder, "files-removed",
641 G_CALLBACK (root_files_removed_callback), model, 0);
643 roots = gtk_file_paths_sort (roots);
645 for (tmp_list = roots; tmp_list; tmp_list = tmp_list->next)
647 FileModelNode *node = file_model_node_new (model, tmp_list->data);
648 gtk_file_path_free (tmp_list->data);
649 node->is_visible = file_model_node_is_visible (model, node);
650 node->next = model->roots;
654 if (node->is_visible)
659 iter.user_data = node;
660 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
661 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
662 gtk_tree_path_free (path);
665 g_slist_free (roots);
668 g_object_unref (model);
669 g_object_unref (handle);
673 * _gtk_file_system_model_new:
674 * @file_system: an object implementing #GtkFileSystem
675 * @root_path: the path of root of the file system to display
676 * @max_depth: the maximum depth from the children of @root_path
677 * or the roots of the file system to display in
678 * the file selector). A depth of 0 displays
679 * only the immediate children of @root_path,
680 * or the roots of the filesystem. -1 for no
682 * @types: a bitmask indicating the types of information
683 * that is desired about the files. This will
684 * determine what information is returned by
685 * _gtk_file_system_model_get_info().
686 * @error: location to store error, or %NULL.
688 * Creates a new #GtkFileSystemModel object. The #GtkFileSystemModel
689 * object wraps a #GtkFileSystem interface as a #GtkTreeModel.
690 * Using the @root_path and @max_depth parameters, the tree model
691 * can be restricted to a subportion of the entire file system.
693 * Return value: the newly created #GtkFileSystemModel object, or NULL if there
697 _gtk_file_system_model_new (GtkFileSystem *file_system,
698 const GtkFilePath *root_path,
700 GtkFileInfoType types,
703 GtkFileSystemModel *model;
704 GtkFileSystemHandle *handle;
706 g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
707 g_return_val_if_fail (root_path != NULL, NULL);
708 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
710 /* First, start loading the root folder */
712 types |= GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN;
714 /* Then, actually create the model and the root nodes */
716 model = g_object_new (GTK_TYPE_FILE_SYSTEM_MODEL, NULL);
717 model->file_system = g_object_ref (file_system);
719 model->max_depth = G_MAXUSHORT;
721 model->max_depth = MIN (max_depth, G_MAXUSHORT);
723 model->types = types;
724 model->root_folder = NULL;
725 model->root_path = gtk_file_path_copy (root_path);
729 handle = gtk_file_system_get_folder (file_system, root_path, types,
731 g_object_ref (model));
734 /* In this case got_root_folder_cb() will never be called, so we
735 * need to unref model twice.
737 g_object_unref (model);
738 g_object_unref (model);
741 GTK_FILE_CHOOSER_ERROR,
742 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
743 _("Could not obtain root folder"));
748 model->pending_handles = g_slist_append (model->pending_handles, handle);
754 model_refilter_recurse (GtkFileSystemModel *model,
755 FileModelNode *parent,
758 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
760 FileModelNode *nodes;
761 gboolean has_children = FALSE;
763 if (parent && !parent->loaded)
767 nodes = parent->children;
769 nodes = model->roots;
773 FileModelNode *next = nodes->next;
776 gtk_tree_path_append_index (path, i);
778 is_visible = file_model_node_is_visible (model, nodes);
780 if (!is_visible && nodes->is_visible)
782 file_model_node_clear (model, nodes);
783 gtk_tree_model_row_deleted (tree_model, path);
785 nodes->is_visible = FALSE;
787 else if (is_visible && !nodes->is_visible)
791 iter.user_data = nodes;
792 nodes->is_visible = TRUE;
793 gtk_tree_model_row_inserted (tree_model, path, &iter);
796 model_refilter_recurse (model, nodes, path);
804 gtk_tree_path_up (path);
809 if (parent && !has_children)
811 /* Fixme - need to insert dummy node here */
816 model_refilter_all (GtkFileSystemModel *model)
820 path = gtk_tree_path_new ();
821 model_refilter_recurse (model, NULL, path);
822 gtk_tree_path_free (path);
826 * _gtk_file_system_model_set_show_hidden:
827 * @model: a #GtkFileSystemModel
828 * @show_hidden: whether hidden files should be displayed
830 * Sets whether hidden files should be included in the #GtkTreeModel
834 _gtk_file_system_model_set_show_hidden (GtkFileSystemModel *model,
835 gboolean show_hidden)
837 show_hidden = show_hidden != FALSE;
839 if (show_hidden != model->show_hidden)
841 model->show_hidden = show_hidden;
842 model_refilter_all (model);
847 * _gtk_file_system_model_set_show_folders:
848 * @model: a #GtkFileSystemModel
849 * @show_folders: whether folders should be displayed
851 * Sets whether folders should be included in the #GtkTreeModel for
855 _gtk_file_system_model_set_show_folders (GtkFileSystemModel *model,
856 gboolean show_folders)
858 show_folders = show_folders != FALSE;
860 if (show_folders != model->show_folders)
862 model->show_folders = show_folders;
863 model_refilter_all (model);
868 * _gtk_file_system_model_set_show_files:
869 * @model: a #GtkFileSystemModel
870 * @show_files: whether files (as opposed to folders) should
873 * Sets whether files (as opposed to folders) should be included
874 * in the #GtkTreeModel for display.
877 _gtk_file_system_model_set_show_files (GtkFileSystemModel *model,
880 show_files = show_files != FALSE;
882 if (show_files != model->show_files)
884 model->show_files = show_files;
885 model_refilter_all (model);
890 * _gtk_file_system_model_get_info:
891 * @model: a #GtkFileSystemModel
892 * @iter: a #GtkTreeIter pointing to a row of @model
894 * Gets the #GtkFileInfo structure for a particular row
895 * of @model. The information included in this structure
896 * is determined by the @types parameter to
897 * _gtk_file_system_model_new().
899 * Return value: a #GtkFileInfo structure. This structure
900 * is owned by @model and must not be modified or freed.
901 * If you want to save the information for later use,
902 * you must make a copy, since the structure may be
903 * freed on later changes to the file system. If you have
904 * called _gtk_file_system_model_add_editable() and the @iter
905 * corresponds to the row that this function returned, the
906 * return value will be NULL.
909 _gtk_file_system_model_get_info (GtkFileSystemModel *model,
914 node = iter->user_data;
915 if (model->has_editable && node == model->roots)
918 return file_model_node_get_info (model, node);
922 * _gtk_file_system_model_get_path:
923 * @model: a #GtkFileSystemModel
924 * @iter: a #GtkTreeIter pointing to a row of @model
926 * Gets the path for a particular row in @model.
928 * Return value: the path. This string is owned by @model and
929 * or freed. If you want to save the path for later use,
930 * you must make a copy, since the string may be freed
931 * on later changes to the file system.
934 _gtk_file_system_model_get_path (GtkFileSystemModel *model,
937 FileModelNode *node = iter->user_data;
939 if (model->has_editable && node == model->roots)
943 return node->parent->path;
949 unref_node_and_parents (GtkFileSystemModel *model,
952 file_model_node_unref (model, node);
954 file_model_node_unref (model, node->parent);
957 static FileModelNode *
958 find_child_node (GtkFileSystemModel *model,
959 FileModelNode *parent_node,
960 const GtkFilePath *path)
962 FileModelNode *children;
965 children = file_model_node_get_children (model, parent_node);
967 children = model->roots;
971 if (children->is_visible &&
973 gtk_file_path_compare (children->path, path) == 0)
976 children = children->next;
983 * _gtk_file_system_model_set_filter:
984 * @mode: a #GtkFileSystemModel
985 * @filter: function to be called for each file
986 * @user_data: data to pass to @filter
988 * Sets a callback called for each file/directory to see whether
989 * it should be included in model. If this function was made
990 * public, we'd want to include a GDestroyNotify as well.
993 _gtk_file_system_model_set_filter (GtkFileSystemModel *model,
994 GtkFileSystemModelFilter filter,
997 g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
999 model->filter_func = filter;
1000 model->filter_data = user_data;
1002 model_refilter_all (model);
1008 GtkFileSystemModel *model;
1009 FileModelNode *node;
1010 FileModelNode *parent_node;
1013 GtkFileSystemModelPathFunc func;
1017 /* FIXME: maybe we have to wait on finished-loading? */
1019 ref_path_cb (GtkFileSystemHandle *handle,
1020 GtkFileFolder *folder,
1021 const GError *error,
1024 struct RefPathData *info = data;
1025 gboolean cancelled = handle->cancelled;
1027 if (!g_slist_find (info->model->pending_handles, handle))
1030 info->model->pending_handles = g_slist_remove (info->model->pending_handles, handle);
1032 /* Note that !folder means that the child node was already
1033 * found, without using get_folder.
1035 if (cancelled || error)
1039 info->cleanups = g_slist_prepend (info->cleanups, folder);
1040 else if (g_slist_length (info->paths) == 1
1041 && gtk_file_path_compare (info->node->path, info->paths->data) == 0)
1043 /* Done, now call the function */
1049 iter.user_data = info->node;
1050 path = gtk_tree_model_get_path (GTK_TREE_MODEL (info->model), &iter);
1052 (* info->func) (info->model, path, &iter, info->user_data);
1054 gtk_tree_path_free (path);
1060 info->node = find_child_node (info->model, info->parent_node, info->paths->data);
1062 file_model_node_ref (info->node);
1068 gtk_file_path_free (info->paths->data);
1069 info->paths = g_slist_remove (info->paths, info->paths->data);
1071 if (g_slist_length (info->paths) < 1)
1073 /* Done, now call the function */
1079 iter.user_data = info->node;
1080 path = gtk_tree_model_get_path (GTK_TREE_MODEL (info->model), &iter);
1082 (* info->func) (info->model, path, &iter, info->user_data);
1084 gtk_tree_path_free (path);
1091 info->parent_node = info->node;
1093 if (info->parent_node->loaded)
1095 info->node = find_child_node (info->model, info->parent_node, info->paths->data);
1096 ref_path_cb (NULL, NULL, NULL, info);
1100 GtkFileSystemHandle *handle;
1102 handle = gtk_file_system_get_folder (info->model->file_system,
1106 info->model->pending_handles =
1107 g_slist_append (info->model->pending_handles, handle);
1115 unref_node_and_parents (info->model, info->node);
1116 gtk_file_paths_free (info->paths);
1117 g_slist_foreach (info->cleanups, (GFunc)g_object_unref, NULL);
1118 g_slist_free (info->cleanups);
1119 g_object_unref (info->model);
1122 g_object_unref (handle);
1126 * _gtk_file_system_model_path_do:
1127 * @model: a #GtkFileSystemModel
1128 * @path: a path pointing to a file in the filesystem
1130 * @func: Function to call with the path and iter corresponding
1132 * @user_data: data to pass to @func
1134 * Locates @path within @model, referencing
1135 * (gtk_tree_model_ref_node()) all parent nodes,
1136 * calls @func passing in the path and iter for @path,
1137 * then unrefs all the parent nodes.
1139 * The reason for doing this operation as a callback
1140 * is so that if the operation performed with the
1141 * path and iter results in referencing the node
1142 * and/or parent nodes, we don't load all the information
1145 * This function is particularly useful for expanding
1146 * a #GtkTreeView to a particular point in the file system.
1149 _gtk_file_system_model_path_do (GtkFileSystemModel *model,
1150 const GtkFilePath *path,
1151 GtkFileSystemModelPathFunc func,
1154 GtkFilePath *parent_path;
1155 GSList *paths = NULL;
1156 FileModelNode *node;
1157 struct RefPathData *info;
1159 if (gtk_file_path_compare (path, model->root_path) == 0 ||
1160 !gtk_file_system_get_parent (model->file_system, path, &parent_path, NULL) ||
1161 parent_path == NULL)
1164 paths = g_slist_prepend (paths, gtk_file_path_copy (path));
1165 while (gtk_file_path_compare (parent_path, model->root_path) != 0)
1167 paths = g_slist_prepend (paths, parent_path);
1168 if (!gtk_file_system_get_parent (model->file_system, parent_path, &parent_path, NULL) ||
1169 parent_path == NULL)
1171 gtk_file_paths_free (paths);
1176 if (g_slist_length (paths) < 1)
1179 /* Now we have all paths, except the root path */
1180 node = find_child_node (model, NULL, paths->data);
1183 gtk_file_paths_free (paths);
1187 file_model_node_ref (node);
1189 gtk_file_path_free (paths->data);
1190 paths = g_slist_remove (paths, paths->data);
1192 if (g_slist_length (paths) < 1)
1194 /* Done, now call the function */
1200 iter.user_data = node;
1201 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
1203 (* func) (model, path, &iter, user_data);
1205 gtk_tree_path_free (path);
1206 unref_node_and_parents (model, node);
1211 info = g_new0 (struct RefPathData, 1);
1212 info->paths = paths;
1213 info->model = g_object_ref (model);
1215 info->user_data = user_data;
1218 if (info->node->loaded)
1220 info->parent_node = info->node;
1221 info->node = find_child_node (model, info->parent_node, info->paths->data);
1222 ref_path_cb (NULL, NULL, NULL, info);
1226 GtkFileSystemHandle *handle;
1228 handle = gtk_file_system_get_folder (model->file_system,
1229 paths->data, model->types,
1231 model->pending_handles = g_slist_append (model->pending_handles, handle);
1237 * _gtk_file_system_model_add_editable:
1238 * @model: a #GtkFileSystemModel
1239 * @iter: Location to return the iter corresponding to the editable row
1241 * Adds an "empty" row at the beginning of the model. This does not refer to
1242 * any file, but is a temporary placeholder for a file name that the user will
1243 * type when a corresponding cell is made editable. When your code is done
1244 * using this temporary row, call _gtk_file_system_model_remove_editable().
1247 _gtk_file_system_model_add_editable (GtkFileSystemModel *model, GtkTreeIter *iter)
1249 FileModelNode *node;
1252 g_return_if_fail (!model->has_editable);
1254 model->has_editable = TRUE;
1256 node = file_model_node_new (model, NULL);
1257 node->is_visible = TRUE;
1259 node->next = model->roots;
1260 model->roots = node;
1262 path = gtk_tree_path_new ();
1263 gtk_tree_path_append_index (path, 0);
1264 iter->user_data = node;
1266 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, iter);
1268 gtk_tree_path_free (path);
1272 * _gtk_file_system_model_remove_editable:
1273 * @model: a #GtkFileSystemModel
1275 * Removes the "empty" row at the beginning of the model that was
1276 * created with _gtk_file_system_model_add_editable(). You should call
1277 * this function when your code is finished editing this temporary row.
1280 _gtk_file_system_model_remove_editable (GtkFileSystemModel *model)
1283 FileModelNode *node;
1285 g_return_if_fail (model->has_editable);
1287 model->has_editable = FALSE;
1289 node = model->roots;
1290 model->roots = model->roots->next;
1291 file_model_node_free (node);
1293 path = gtk_tree_path_new ();
1294 gtk_tree_path_append_index (path, 0);
1296 gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
1298 gtk_tree_path_free (path);
1301 static FileModelNode *
1302 file_model_node_new (GtkFileSystemModel *model,
1303 const GtkFilePath *path)
1305 FileModelNode *node = g_new0 (FileModelNode, 1);
1307 node->model = model;
1308 node->path = path ? gtk_file_path_copy (path) : NULL;
1314 file_model_node_free (FileModelNode *node)
1316 file_model_node_clear (node->model, node);
1319 gtk_file_path_free (node->path);
1322 gtk_file_info_free (node->info);
1327 static const GtkFileInfo *
1328 file_model_node_get_info (GtkFileSystemModel *model,
1329 FileModelNode *node)
1335 node->info = gtk_file_info_new ();
1336 gtk_file_info_set_display_name (node->info, _("(Empty)"));
1338 else if (node->parent || model->root_folder)
1340 node->info = gtk_file_folder_get_info (node->parent ? node->parent->folder : model->root_folder,
1342 NULL); /* NULL-GError */
1345 g_assert_not_reached ();
1352 file_model_node_is_visible (GtkFileSystemModel *model,
1353 FileModelNode *node)
1355 if (model->show_folders != model->show_files ||
1356 !model->show_hidden ||
1359 const GtkFileInfo *info = file_model_node_get_info (model, node);
1363 /* File probably disappeared underneath us or resides in a
1364 directory where we have only partial access rights. */
1368 if (model->show_folders != model->show_files &&
1369 model->show_folders != gtk_file_info_get_is_folder (info))
1372 if (!model->show_hidden && gtk_file_info_get_is_hidden (info))
1375 if (model->filter_func &&
1376 !model->filter_func (model, node->path, info, model->filter_data))
1384 file_model_node_clear (GtkFileSystemModel *model,
1385 FileModelNode *node)
1387 FileModelNode *children;
1389 file_model_node_idle_clear_cancel (node);
1391 children = node->children;
1392 node->children = NULL;
1393 node->loaded = FALSE;
1397 FileModelNode *next = children->next;
1399 file_model_node_free (children);
1406 /* Unreffing node->folder may cause roots_changed,
1407 * so we need to be careful about ordering.
1409 GtkFileFolder *folder = node->folder;
1410 node->folder = NULL;
1412 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (deleted_callback), node);
1413 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_added_callback), node);
1414 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_changed_callback), node);
1415 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_removed_callback), node);
1417 g_object_unref (folder);
1422 file_model_node_ref (FileModelNode *node)
1425 if (node->ref_count == 1 && node->parent)
1426 node->parent->n_referenced_children++;
1430 idle_clear_callback (GtkFileSystemModel *model)
1432 while (model->idle_clears)
1434 FileModelNode *node = model->idle_clears->data;
1435 model->idle_clears = g_slist_delete_link (model->idle_clears, model->idle_clears);
1437 node->idle_clear = FALSE;
1438 file_model_node_clear (node->model, node);
1445 file_model_node_idle_clear (FileModelNode *node)
1447 if (!node->idle_clear)
1449 GtkFileSystemModel *model = node->model;
1451 node->idle_clear = TRUE;
1452 if (!model->idle_clears)
1454 model->idle_clear_source = g_idle_source_new ();
1455 g_source_set_priority (model->idle_clear_source, G_PRIORITY_HIGH);
1456 g_source_set_closure (model->idle_clear_source,
1457 g_cclosure_new_object (G_CALLBACK (idle_clear_callback),
1459 g_source_attach (model->idle_clear_source, NULL);
1462 model->idle_clears = g_slist_prepend (model->idle_clears, node);
1463 node->idle_clear = TRUE;
1468 file_model_node_idle_clear_cancel (FileModelNode *node)
1470 if (node->idle_clear)
1472 GtkFileSystemModel *model = node->model;
1474 model->idle_clears = g_slist_remove (model->idle_clears, node);
1475 if (!model->idle_clears)
1477 g_source_destroy (model->idle_clear_source);
1478 model->idle_clear_source = NULL;
1481 node->idle_clear = FALSE;
1486 file_model_node_unref (GtkFileSystemModel *model,
1487 FileModelNode *node)
1490 if (node->ref_count == 0)
1492 file_model_node_clear (model, node);
1494 file_model_node_child_unref (node->parent);
1499 file_model_node_child_unref (FileModelNode *parent)
1501 parent->n_referenced_children--;
1502 if (parent->n_referenced_children == 0)
1503 file_model_node_idle_clear (parent);
1506 struct GetChildrenData
1508 GtkFileSystemModel *model;
1509 FileModelNode *node;
1513 get_children_get_folder_cb (GtkFileSystemHandle *handle,
1514 GtkFileFolder *folder,
1515 const GError *error,
1516 gpointer callback_data)
1518 GSList *child_paths, *tmp_list;
1519 gboolean has_children = FALSE;
1520 gboolean cancelled = handle->cancelled;
1521 struct GetChildrenData *data = callback_data;
1523 tmp_list = g_slist_find (data->model->pending_handles, handle);
1528 data->model->pending_handles = g_slist_delete_link (data->model->pending_handles, tmp_list);
1530 if (cancelled || !folder)
1532 /* error, no folder, remove dummy child */
1533 if (data->node->parent && data->node->parent->has_dummy)
1535 data->node->parent->children = NULL;
1536 data->node->parent->has_dummy = FALSE;
1539 file_model_node_free (data->node);
1544 data->node->folder = folder;
1545 data->node->load_pending = FALSE;
1547 if (gtk_file_folder_list_children (folder, &child_paths, NULL)) /* NULL-GError */
1549 child_paths = gtk_file_paths_sort (child_paths);
1551 for (tmp_list = child_paths; tmp_list; tmp_list = tmp_list->next)
1553 FileModelNode *child_node = file_model_node_new (data->model, tmp_list->data);
1554 gtk_file_path_free (tmp_list->data);
1555 child_node->next = data->node->children;
1556 child_node->parent = data->node;
1557 child_node->depth = data->node->depth + 1;
1558 child_node->is_visible = file_model_node_is_visible (data->model, child_node);
1560 if (child_node->is_visible)
1565 has_children = TRUE;
1567 iter.user_data = child_node;
1568 path = gtk_tree_model_get_path (GTK_TREE_MODEL (data->model), &iter);
1569 gtk_tree_model_row_inserted (GTK_TREE_MODEL (data->model), path, &iter);
1570 gtk_tree_path_free (path);
1573 data->node->children = child_node;
1575 g_slist_free (child_paths);
1578 g_signal_connect (data->node->folder, "deleted",
1579 G_CALLBACK (deleted_callback), data->node);
1580 g_signal_connect (data->node->folder, "files-added",
1581 G_CALLBACK (files_added_callback), data->node);
1582 g_signal_connect (data->node->folder, "files-changed",
1583 G_CALLBACK (files_changed_callback), data->node);
1584 g_signal_connect (data->node->folder, "files-removed",
1585 G_CALLBACK (files_removed_callback), data->node);
1587 data->node->loaded = TRUE;
1591 /* The hard case ... we claimed this folder had children, but actually
1592 * it didn't. We have to add a dummy child, possibly to remove later.
1594 FileModelNode *child_node = file_model_node_new (data->model, NULL);
1595 child_node->is_visible = TRUE;
1596 child_node->parent = data->node;
1597 child_node->is_dummy = TRUE;
1599 data->node->children = child_node;
1600 data->node->has_dummy = TRUE;
1603 g_object_set_data (G_OBJECT (data->node->folder), I_("model-node"), data->node);
1606 g_object_unref (data->model);
1609 g_object_unref (handle);
1612 static FileModelNode *
1613 file_model_node_get_children (GtkFileSystemModel *model,
1614 FileModelNode *node)
1616 if (node->ref_count == 0)
1619 if (!node->loaded && !node->load_pending)
1621 const GtkFileInfo *info = file_model_node_get_info (model, node);
1622 gboolean has_children = FALSE;
1623 gboolean is_folder = node->depth < model->max_depth && gtk_file_info_get_is_folder (info);
1625 file_model_node_idle_clear_cancel (node);
1629 struct GetChildrenData *data;
1630 GtkFileSystemHandle *handle;
1632 data = g_new (struct GetChildrenData, 1);
1633 data->model = g_object_ref (model);
1637 gtk_file_system_get_folder (model->file_system,
1640 get_children_get_folder_cb,
1643 model->pending_handles = g_slist_append (model->pending_handles, handle);
1644 node->load_pending = TRUE;
1647 if (is_folder && !has_children)
1649 /* The hard case ... we claimed this folder had children, but actually
1650 * it didn't. We have to add a dummy child, possibly to remove later.
1652 FileModelNode *child_node = file_model_node_new (model, NULL);
1653 child_node->is_visible = TRUE;
1654 child_node->parent = node;
1655 child_node->is_dummy = TRUE;
1657 node->children = child_node;
1658 node->has_dummy = TRUE;
1662 return node->children;
1666 do_files_added (GtkFileSystemModel *model,
1667 FileModelNode *parent_node,
1670 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1671 FileModelNode *children;
1672 FileModelNode *prev = NULL;
1675 GSList *sorted_paths;
1678 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1682 iter.user_data = parent_node;
1683 path = gtk_tree_model_get_path (tree_model, &iter);
1684 children = parent_node->children;
1688 path = gtk_tree_path_new ();
1689 children = model->roots;
1692 gtk_tree_path_down (path);
1694 if (parent_node && parent_node->has_dummy)
1697 children = children->next;
1698 gtk_tree_path_next (path);
1701 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1703 const GtkFilePath *file_path = tmp_list->data;
1706 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1709 if (children->is_visible)
1710 gtk_tree_path_next (path);
1712 children = children->next;
1716 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1718 /* Shouldn't happen */
1724 new = file_model_node_new (model, file_path);
1727 new->next = children;
1730 else if (parent_node)
1731 parent_node->children = new;
1739 new->parent = parent_node;
1740 new->depth = parent_node->depth + 1;
1743 new->is_visible = file_model_node_is_visible (model, new);
1745 if (new->is_visible)
1747 iter.user_data = new;
1748 gtk_tree_path_free (path);
1749 path = gtk_tree_model_get_path (tree_model, &iter);
1750 gtk_tree_model_row_inserted (tree_model, path, &iter);
1752 if (gtk_file_system_model_iter_has_child (tree_model, &iter))
1753 gtk_tree_model_row_has_child_toggled (tree_model, path, &iter);
1755 if (parent_node && parent_node->has_dummy)
1757 FileModelNode *dummy = parent_node->children;
1758 GtkTreePath *dummy_path;
1760 parent_node->children = parent_node->children->next;
1761 parent_node->has_dummy = FALSE;
1763 dummy_path = gtk_tree_path_copy (path);
1764 gtk_tree_path_up (dummy_path);
1765 gtk_tree_path_down (dummy_path);
1767 gtk_tree_model_row_deleted (tree_model, dummy_path);
1768 gtk_tree_path_free (dummy_path);
1770 if (dummy->ref_count)
1771 file_model_node_child_unref (parent_node);
1772 file_model_node_free (dummy);
1775 gtk_tree_path_next (path);
1780 gtk_tree_path_free (path);
1781 g_slist_free (sorted_paths);
1785 do_files_changed (GtkFileSystemModel *model,
1786 FileModelNode *parent_node,
1789 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1790 FileModelNode *children;
1793 GSList *sorted_paths;
1796 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1800 iter.user_data = parent_node;
1801 path = gtk_tree_model_get_path (tree_model, &iter);
1802 children = parent_node->children;
1806 path = gtk_tree_path_new ();
1807 children = model->roots;
1810 gtk_tree_path_down (path);
1812 if (parent_node && parent_node->has_dummy)
1814 children = children->next;
1815 gtk_tree_path_next (path);
1818 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1820 const GtkFilePath *file_path = tmp_list->data;
1823 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1825 if (children->is_visible)
1826 gtk_tree_path_next (path);
1828 children = children->next;
1832 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1834 gtk_tree_model_row_changed (tree_model, path, &iter);
1838 /* Shouldn't happen */
1842 gtk_tree_path_free (path);
1843 g_slist_free (sorted_paths);
1847 do_files_removed (GtkFileSystemModel *model,
1848 FileModelNode *parent_node,
1851 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1852 FileModelNode *children;
1853 FileModelNode *prev = NULL;
1856 GSList *sorted_paths;
1858 FileModelNode *tmp_child;
1861 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1865 iter.user_data = parent_node;
1866 path = gtk_tree_model_get_path (tree_model, &iter);
1867 children = parent_node->children;
1871 path = gtk_tree_path_new ();
1872 children = model->roots;
1875 /* Count the number of currently visible children, so that
1876 * can catch when we need to insert a dummy node.
1879 for (tmp_child = children; tmp_child; tmp_child = tmp_child->next)
1881 if (tmp_child->is_visible)
1885 gtk_tree_path_down (path);
1887 if (parent_node && parent_node->has_dummy)
1890 children = children->next;
1891 gtk_tree_path_next (path);
1894 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1896 const GtkFilePath *file_path = tmp_list->data;
1899 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1902 if (children->is_visible)
1903 gtk_tree_path_next (path);
1905 children = children->next;
1909 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1911 FileModelNode *next = children->next;
1913 if (children->is_visible)
1916 if (parent_node && n_visible == 0)
1918 FileModelNode *dummy = file_model_node_new (model, NULL);
1919 dummy->is_visible = TRUE;
1920 dummy->parent = parent_node;
1921 dummy->is_dummy = TRUE;
1923 parent_node->children = dummy;
1924 parent_node->has_dummy = TRUE;
1926 iter.user_data = dummy;
1927 gtk_tree_model_row_inserted (tree_model, path, &iter);
1928 gtk_tree_path_next (path);
1935 else if (parent_node)
1936 parent_node->children = next;
1938 model->roots = next;
1940 if (parent_node && children->ref_count)
1941 file_model_node_child_unref (parent_node);
1943 if (children->is_visible)
1944 gtk_tree_model_row_deleted (tree_model, path);
1946 file_model_node_free (children);
1952 /* Shouldn't happen */
1956 gtk_tree_path_free (path);
1957 g_slist_free (sorted_paths);
1961 deleted_callback (GtkFileFolder *folder,
1962 FileModelNode *node)
1967 files_added_callback (GtkFileFolder *folder,
1969 FileModelNode *node)
1971 do_files_added (node->model, node, paths);
1975 files_changed_callback (GtkFileFolder *folder,
1977 FileModelNode *node)
1979 do_files_changed (node->model, node, paths);
1983 files_removed_callback (GtkFileFolder *folder,
1985 FileModelNode *node)
1987 do_files_removed (node->model, node, paths);
1991 root_deleted_callback (GtkFileFolder *folder,
1992 GtkFileSystemModel *model)
1997 root_files_added_callback (GtkFileFolder *folder,
1999 GtkFileSystemModel *model)
2001 do_files_added (model, NULL, paths);
2005 root_files_changed_callback (GtkFileFolder *folder,
2007 GtkFileSystemModel *model)
2009 do_files_changed (model, NULL, paths);
2013 root_files_removed_callback (GtkFileFolder *folder,
2015 GtkFileSystemModel *model)
2017 do_files_removed (model, NULL, paths);