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 "gtkfilesystemmodel.h"
25 #include "gtkfilesystem.h"
27 #include "gtktreednd.h"
28 #include "gtktreemodel.h"
30 typedef struct _GtkFileSystemModelClass GtkFileSystemModelClass;
31 typedef struct _FileModelNode FileModelNode;
33 #define GTK_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
34 #define GTK_IS_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_MODEL))
35 #define GTK_FILE_SYSTEM_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
37 struct _GtkFileSystemModelClass
39 GObjectClass parent_class;
42 struct _GtkFileSystemModel
44 GObject parent_instance;
46 GtkFileSystem *file_system;
47 GtkFileInfoType types;
49 GtkFileFolder *root_folder;
50 GtkFilePath *root_path;
52 GtkFileSystemModelFilter filter_func;
56 GSource *idle_clear_source;
60 guint show_hidden : 1;
61 guint show_folders : 1;
63 guint folders_only : 1;
64 guint has_editable : 1;
73 GtkFileFolder *folder;
75 FileModelNode *children;
76 FileModelNode *parent;
77 GtkFileSystemModel *model;
80 guint n_referenced_children;
91 static void gtk_file_system_model_class_init (GtkFileSystemModelClass *class);
92 static void gtk_file_system_model_iface_init (GtkTreeModelIface *iface);
93 static void gtk_file_system_model_init (GtkFileSystemModel *model);
94 static void gtk_file_system_model_finalize (GObject *object);
96 static void drag_source_iface_init (GtkTreeDragSourceIface *iface);
98 static GtkTreeModelFlags gtk_file_system_model_get_flags (GtkTreeModel *tree_model);
99 static gint gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model);
100 static GType gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
102 static gboolean gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
105 static GtkTreePath * gtk_file_system_model_get_path (GtkTreeModel *tree_model,
107 static void gtk_file_system_model_get_value (GtkTreeModel *tree_model,
111 static gboolean gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
113 static gboolean gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
115 GtkTreeIter *parent);
116 static gboolean gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
118 static gint gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
120 static gboolean gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
124 static gboolean gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
127 static void gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
129 static void gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
132 static gboolean drag_source_row_draggable (GtkTreeDragSource *drag_source,
134 static gboolean drag_source_drag_data_get (GtkTreeDragSource *drag_source,
136 GtkSelectionData *selection_data);
138 static FileModelNode *file_model_node_new (GtkFileSystemModel *model,
139 const GtkFilePath *path);
140 static void file_model_node_free (FileModelNode *node);
141 static void file_model_node_ref (FileModelNode *node);
142 static void file_model_node_unref (GtkFileSystemModel *model,
143 FileModelNode *node);
145 static void file_model_node_idle_clear (FileModelNode *node);
146 static void file_model_node_idle_clear_cancel (FileModelNode *node);
147 static void file_model_node_child_unref (FileModelNode *parent);
149 static const GtkFileInfo *file_model_node_get_info (GtkFileSystemModel *model,
150 FileModelNode *node);
151 static gboolean file_model_node_is_visible (GtkFileSystemModel *model,
152 FileModelNode *node);
153 static void file_model_node_clear (GtkFileSystemModel *model,
154 FileModelNode *node);
155 static FileModelNode * file_model_node_get_children (GtkFileSystemModel *model,
156 FileModelNode *node);
159 static void roots_changed_callback (GtkFileSystem *file_system,
160 GtkFileSystemModel *model);
163 static void deleted_callback (GtkFileFolder *folder,
164 FileModelNode *node);
165 static void files_added_callback (GtkFileFolder *folder,
167 FileModelNode *node);
168 static void files_changed_callback (GtkFileFolder *folder,
170 FileModelNode *node);
171 static void files_removed_callback (GtkFileFolder *folder,
173 FileModelNode *node);
175 static void root_deleted_callback (GtkFileFolder *folder,
176 GtkFileSystemModel *model);
177 static void root_files_added_callback (GtkFileFolder *folder,
179 GtkFileSystemModel *model);
180 static void root_files_changed_callback (GtkFileFolder *folder,
182 GtkFileSystemModel *model);
183 static void root_files_removed_callback (GtkFileFolder *folder,
185 GtkFileSystemModel *model);
187 static GObjectClass *parent_class = NULL;
190 _gtk_file_system_model_get_type (void)
192 static GType file_system_model_type = 0;
194 if (!file_system_model_type)
196 static const GTypeInfo file_system_model_info =
198 sizeof (GtkFileSystemModelClass),
199 NULL, /* base_init */
200 NULL, /* base_finalize */
201 (GClassInitFunc) gtk_file_system_model_class_init,
202 NULL, /* class_finalize */
203 NULL, /* class_data */
204 sizeof (GtkFileSystemModel),
206 (GInstanceInitFunc) gtk_file_system_model_init,
209 static const GInterfaceInfo file_system_info =
211 (GInterfaceInitFunc) gtk_file_system_model_iface_init, /* interface_init */
212 NULL, /* interface_finalize */
213 NULL /* interface_data */
216 static const GInterfaceInfo drag_source_info =
218 (GInterfaceInitFunc) drag_source_iface_init, /* interface_init */
219 NULL, /* interface_finalize */
220 NULL /* interface_data */
223 file_system_model_type = g_type_register_static (G_TYPE_OBJECT,
224 "GtkFileSystemModel",
225 &file_system_model_info, 0);
226 g_type_add_interface_static (file_system_model_type,
229 g_type_add_interface_static (file_system_model_type,
230 GTK_TYPE_TREE_DRAG_SOURCE,
234 return file_system_model_type;
238 gtk_file_system_model_class_init (GtkFileSystemModelClass *class)
240 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
242 parent_class = g_type_class_peek_parent (class);
244 gobject_class->finalize = gtk_file_system_model_finalize;
248 gtk_file_system_model_iface_init (GtkTreeModelIface *iface)
250 iface->get_flags = gtk_file_system_model_get_flags;
251 iface->get_n_columns = gtk_file_system_model_get_n_columns;
252 iface->get_column_type = gtk_file_system_model_get_column_type;
253 iface->get_iter = gtk_file_system_model_get_iter;
254 iface->get_path = gtk_file_system_model_get_path;
255 iface->get_value = gtk_file_system_model_get_value;
256 iface->iter_next = gtk_file_system_model_iter_next;
257 iface->iter_children = gtk_file_system_model_iter_children;
258 iface->iter_has_child = gtk_file_system_model_iter_has_child;
259 iface->iter_n_children = gtk_file_system_model_iter_n_children;
260 iface->iter_nth_child = gtk_file_system_model_iter_nth_child;
261 iface->iter_parent = gtk_file_system_model_iter_parent;
262 iface->ref_node = gtk_file_system_model_ref_node;
263 iface->unref_node = gtk_file_system_model_unref_node;
267 gtk_file_system_model_init (GtkFileSystemModel *model)
269 model->show_files = TRUE;
270 model->show_folders = TRUE;
271 model->show_hidden = FALSE;
275 gtk_file_system_model_finalize (GObject *object)
277 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
278 FileModelNode *children, *next;
280 if (model->root_folder)
281 g_object_unref (model->root_folder);
283 if (model->root_path)
284 gtk_file_path_free (model->root_path);
286 if (model->file_system)
287 g_object_unref (model->file_system);
289 children = model->roots;
292 next = children->next;
293 file_model_node_free (children);
297 G_OBJECT_CLASS (parent_class)->finalize (object);
301 drag_source_iface_init (GtkTreeDragSourceIface *iface)
303 iface->row_draggable = drag_source_row_draggable;
304 iface->drag_data_get = drag_source_drag_data_get;
305 iface->drag_data_delete = NULL;
309 * ******************** GtkTreeModel methods ********************
312 static GtkTreeModelFlags
313 gtk_file_system_model_get_flags (GtkTreeModel *tree_model)
315 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
316 GtkTreeModelFlags flags = GTK_TREE_MODEL_ITERS_PERSIST;
318 if (model->max_depth == 1)
319 flags |= GTK_TREE_MODEL_LIST_ONLY;
325 gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model)
327 return GTK_FILE_SYSTEM_MODEL_N_COLUMNS;
331 gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
336 case GTK_FILE_SYSTEM_MODEL_INFO:
337 return GTK_TYPE_FILE_INFO;
338 case GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME:
339 return G_TYPE_STRING;
341 g_assert_not_reached ();
347 gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
355 indices = gtk_tree_path_get_indices (path);
356 depth = gtk_tree_path_get_depth (path);
358 g_return_val_if_fail (depth > 0, FALSE);
360 if (!gtk_tree_model_iter_nth_child (tree_model, iter, NULL, indices[0]))
363 for (i = 1; i < depth; i++)
366 if (!gtk_tree_model_iter_nth_child (tree_model, iter, &parent, indices[i]))
374 gtk_file_system_model_get_path (GtkTreeModel *tree_model,
377 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
378 FileModelNode *node = iter->user_data;
380 GtkTreePath *result = gtk_tree_path_new ();
384 FileModelNode *parent = node->parent;
385 FileModelNode *children;
389 children = parent->children;
391 children = model->roots;
393 while (children != node)
395 if (children->is_visible)
397 children = children->next;
400 gtk_tree_path_prepend_index (result, n);
409 gtk_file_system_model_get_value (GtkTreeModel *tree_model,
414 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
415 FileModelNode *node = iter->user_data;
416 const GtkFileInfo *info;
420 case GTK_FILE_SYSTEM_MODEL_INFO:
421 if (model->has_editable && node == model->roots)
424 info = file_model_node_get_info (model, node);
426 g_value_init (value, GTK_TYPE_FILE_INFO);
427 g_value_set_boxed (value, info);
429 case GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME:
431 g_value_init (value, G_TYPE_STRING);
433 if (model->has_editable && node == model->roots)
434 g_value_set_string (value, "");
437 const GtkFileInfo *info = file_model_node_get_info (model, node);
439 g_value_set_string (value, gtk_file_info_get_display_name (info));
444 g_assert_not_reached ();
449 gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
452 FileModelNode *node = iter->user_data;
455 while (node && !node->is_visible)
458 iter->user_data = node;
464 gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
468 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
469 FileModelNode *children;
473 FileModelNode *parent_node = parent->user_data;
474 children = file_model_node_get_children (model, parent_node);
478 children = model->roots;
481 while (children && !children->is_visible)
482 children = children->next;
484 iter->user_data = children;
486 return children != NULL;
490 gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
493 FileModelNode *node = iter->user_data;
494 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
496 if (node->depth == model->max_depth)
500 const GtkFileInfo *info = file_model_node_get_info (model, node);
501 return gtk_file_info_get_is_folder (info);
506 gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
509 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
510 FileModelNode *children;
515 FileModelNode *node = iter->user_data;
516 children = file_model_node_get_children (model, node);
520 children = model->roots;
525 if (children->is_visible)
527 children = children->next;
534 gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
539 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
540 FileModelNode *children;
544 FileModelNode *parent_node = parent->user_data;
545 children = file_model_node_get_children (model, parent_node);
549 children = model->roots;
552 while (children && !children->is_visible)
553 children = children->next;
555 while (n && children)
558 children = children->next;
559 while (children && !children->is_visible)
560 children = children->next;
563 iter->user_data = children;
565 return children != NULL;
569 gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
573 FileModelNode *node = child->user_data;
576 iter->user_data = node;
582 gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
585 file_model_node_ref (iter->user_data);
589 gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
592 file_model_node_unref (GTK_FILE_SYSTEM_MODEL (tree_model),
597 drag_source_row_draggable (GtkTreeDragSource *drag_source,
600 GtkFileSystemModel *model;
604 model = GTK_FILE_SYSTEM_MODEL (drag_source);
606 if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
609 if (!model->has_editable)
612 node = iter.user_data;
613 return (node != model->roots);
617 drag_source_drag_data_get (GtkTreeDragSource *drag_source,
619 GtkSelectionData *selection_data)
621 GtkFileSystemModel *model;
623 const GtkFilePath *file_path;
627 model = GTK_FILE_SYSTEM_MODEL (drag_source);
629 if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
632 file_path = _gtk_file_system_model_get_path (model, &iter);
633 g_assert (file_path != NULL);
635 uri = gtk_file_system_path_to_uri (model->file_system, file_path);
636 uris = g_strconcat (uri, "\r\n", NULL);
638 gtk_selection_data_set (selection_data,
639 gdk_atom_intern ("text/uri-list", FALSE),
651 * _gtk_file_system_model_new:
652 * @file_system: an object implementing #GtkFileSystem
653 * @root_path: the path of root of the file system to display
654 * @max_depth: the maximum depth from the children of @root_path
655 * or the roots of the file system to display in
656 * the file selector). A depth of 0 displays
657 * only the immediate children of @root_path,
658 * or the roots of the filesystem. -1 for no
660 * @types: a bitmask indicating the types of information
661 * that is desired about the files. This will
662 * determine what information is returned by
663 * _gtk_file_system_model_get_info().
665 * Creates a new #GtkFileSystemModel object. The #GtkFileSystemModel
666 * object wraps a #GtkFileSystem interface as a #GtkTreeModel.
667 * Using the @root_path and @max_depth parameters, the tree model
668 * can be restricted to a subportion of the entire file system.
670 * Return value: the newly created #GtkFileSystemModel object.
673 _gtk_file_system_model_new (GtkFileSystem *file_system,
674 const GtkFilePath *root_path,
676 GtkFileInfoType types)
678 GtkFileSystemModel *model;
679 GSList *roots = NULL;
682 g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
683 g_return_val_if_fail (root_path != NULL, NULL);
685 model = g_object_new (GTK_TYPE_FILE_SYSTEM_MODEL, NULL);
686 model->file_system = g_object_ref (file_system);
688 model->max_depth = G_MAXUSHORT;
690 model->max_depth = MIN (max_depth, G_MAXUSHORT);
691 model->types = types | GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN;
697 model->root_path = gtk_file_path_copy (root_path);
698 model->root_folder = gtk_file_system_get_folder (file_system, root_path,
700 NULL); /* NULL-GError */
702 if (model->root_folder &&
703 gtk_file_folder_list_children (model->root_folder,
705 NULL)) /* NULL-GError */
709 g_signal_connect_object (model->root_folder, "deleted",
710 G_CALLBACK (root_deleted_callback), model, 0);
711 g_signal_connect_object (model->root_folder, "files-added",
712 G_CALLBACK (root_files_added_callback), model, 0);
713 g_signal_connect_object (model->root_folder, "files-changed",
714 G_CALLBACK (root_files_changed_callback), model, 0);
715 g_signal_connect_object (model->root_folder, "files-removed",
716 G_CALLBACK (root_files_removed_callback), model, 0);
722 roots = gtk_file_system_list_roots (file_system);
723 g_signal_connect_object (file_system, "roots-changed",
724 G_CALLBACK (roots_changed_callback), model, 0);
728 roots = gtk_file_paths_sort (roots);
730 for (tmp_list = roots; tmp_list; tmp_list = tmp_list->next)
732 FileModelNode *node = file_model_node_new (model, tmp_list->data);
733 gtk_file_path_free (tmp_list->data);
734 node->is_visible = file_model_node_is_visible (model, node);
735 node->next = model->roots;
739 g_slist_free (roots);
741 model->roots = (FileModelNode *)g_slist_reverse ((GSList *)model->roots);
747 model_refilter_recurse (GtkFileSystemModel *model,
748 FileModelNode *parent,
751 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
753 FileModelNode *nodes;
754 gboolean has_children = FALSE;
756 if (parent && !parent->loaded)
760 nodes = parent->children;
762 nodes = model->roots;
766 FileModelNode *next = nodes->next;
769 gtk_tree_path_append_index (path, i);
771 is_visible = file_model_node_is_visible (model, nodes);
773 if (!is_visible && nodes->is_visible)
775 file_model_node_clear (model, nodes);
776 gtk_tree_model_row_deleted (tree_model, path);
778 nodes->is_visible = FALSE;
780 else if (is_visible && !nodes->is_visible)
784 iter.user_data = nodes;
785 gtk_tree_model_row_inserted (tree_model, path, &iter);
787 nodes->is_visible = TRUE;
790 model_refilter_recurse (model, nodes, path);
798 gtk_tree_path_up (path);
803 if (parent && !has_children)
805 /* Fixme - need to insert dummy node here */
810 model_refilter_all (GtkFileSystemModel *model)
814 path = gtk_tree_path_new ();
815 model_refilter_recurse (model, NULL, path);
816 gtk_tree_path_free (path);
820 * _gtk_file_system_model_set_show_hidden:
821 * @model: a #GtkFileSystemModel
822 * @show_hidden: whether hidden files should be displayed
824 * Sets whether hidden files should be included in the #GtkTreeModel
828 _gtk_file_system_model_set_show_hidden (GtkFileSystemModel *model,
829 gboolean show_hidden)
831 show_hidden = show_hidden != FALSE;
833 if (show_hidden != model->show_hidden)
835 model->show_hidden = show_hidden;
836 model_refilter_all (model);
841 * _gtk_file_system_model_set_show_folders:
842 * @model: a #GtkFileSystemModel
843 * @show_folders: whether folders should be displayed
845 * Sets whether folders should be included in the #GtkTreeModel for
849 _gtk_file_system_model_set_show_folders (GtkFileSystemModel *model,
850 gboolean show_folders)
852 show_folders = show_folders != FALSE;
854 if (show_folders != model->show_folders)
856 model->show_folders = show_folders;
857 model_refilter_all (model);
862 * _gtk_file_system_model_set_show_files:
863 * @model: a #GtkFileSystemModel
864 * @show_files: whether files (as opposed to folders) should
867 * Sets whether files (as opposed to folders) should be included
868 * in the #GtkTreeModel for display.
871 _gtk_file_system_model_set_show_files (GtkFileSystemModel *model,
874 show_files = show_files != FALSE;
876 if (show_files != model->show_files)
878 model->show_files = show_files;
879 model_refilter_all (model);
884 * _gtk_file_system_model_get_info:
885 * @model: a #GtkFileSystemModel
886 * @iter: a #GtkTreeIter pointing to a row of @model
888 * Gets the #GtkFileInfo structure for a particular row
889 * of @model. The information included in this structure
890 * is determined by the @types parameter to
891 * _gtk_file_system_model_new().
893 * Return value: a #GtkFileInfo structure. This structure
894 * is owned by @model and must not be modified or freed.
895 * If you want to save the information for later use,
896 * you must make a copy, since the structure may be
897 * freed on later changes to the file system. If you have
898 * called _gtk_file_system_model_add_editable() and the @iter
899 * corresponds to the row that this function returned, the
900 * return value will be NULL.
903 _gtk_file_system_model_get_info (GtkFileSystemModel *model,
908 node = iter->user_data;
909 if (model->has_editable && node == model->roots)
912 return file_model_node_get_info (model, node);
916 * _gtk_file_system_model_get_path:
917 * @model: a #GtkFileSystemModel
918 * @iter: a #GtkTreeIter pointing to a row of @model
920 * Gets the path for a particular row in @model.
922 * Return value: the path. This string is owned by @model and
923 * or freed. If you want to save the path for later use,
924 * you must make a copy, since the string may be freed
925 * on later changes to the file system.
928 _gtk_file_system_model_get_path (GtkFileSystemModel *model,
931 FileModelNode *node = iter->user_data;
933 if (model->has_editable && node == model->roots)
937 return node->parent->path;
943 unref_node_and_parents (GtkFileSystemModel *model,
946 file_model_node_unref (model, node);
948 file_model_node_unref (model, node->parent);
951 static FileModelNode *
952 find_child_node (GtkFileSystemModel *model,
953 FileModelNode *parent_node,
954 const GtkFilePath *path)
956 FileModelNode *children;
959 children = file_model_node_get_children (model, parent_node);
961 children = model->roots;
965 if (children->is_visible &&
967 gtk_file_path_compare (children->path, path) == 0)
970 children = children->next;
977 static FileModelNode *
978 find_and_ref_path (GtkFileSystemModel *model,
979 const GtkFilePath *path,
982 GtkFilePath *parent_path;
983 FileModelNode *parent_node;
984 FileModelNode *child_node;
985 GtkFileFolder *folder;
987 if (gtk_file_path_compare (path, model->root_path) == 0
988 || !gtk_file_system_get_parent (model->file_system, path, &parent_path, NULL))
993 parent_node = find_and_ref_path (model, parent_path, cleanups);
994 gtk_file_path_free (parent_path);
999 child_node = find_child_node (model, parent_node, path);
1002 file_model_node_ref (child_node);
1006 folder = gtk_file_system_get_folder (model->file_system,
1009 NULL); /* NULL-GError */
1012 *cleanups = g_slist_prepend (*cleanups, folder);
1014 child_node = find_child_node (model, parent_node, path);
1017 file_model_node_ref (child_node);
1023 unref_node_and_parents (model, parent_node);
1029 * _gtk_file_system_model_set_filter:
1030 * @mode: a #GtkFileSystemModel
1031 * @filter: function to be called for each file
1032 * @user_data: data to pass to @filter
1034 * Sets a callback called for each file/directory to see whether
1035 * it should be included in model. If this function was made
1036 * public, we'd want to include a GDestroyNotify as well.
1039 _gtk_file_system_model_set_filter (GtkFileSystemModel *model,
1040 GtkFileSystemModelFilter filter,
1043 g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1045 model->filter_func = filter;
1046 model->filter_data = user_data;
1048 model_refilter_all (model);
1052 * _gtk_file_system_model_path_do:
1053 * @model: a #GtkFileSystemModel
1054 * @path: a path pointing to a file in the filesystem
1056 * @func: Function to call with the path and iter corresponding
1058 * @user_data: data to pass to @func
1060 * Locates @path within @model, referencing
1061 * (gtk_tree_model_ref_node ()) all parent nodes,
1062 * calls @func passing in the path and iter for @path,
1063 * then unrefs all the parent nodes.
1065 * The reason for doing this operation as a callback
1066 * is so that if the operation performed with the the
1067 * path and iter results in referencing the the node
1068 * and/or parent nodes, we don't load all the information
1071 * This function is particularly useful for expanding
1072 * a #GtkTreeView to a particular point in the file system.
1074 * Return value: %TRUE if the path was successfully
1075 * found in @model and @func was called.
1078 _gtk_file_system_model_path_do (GtkFileSystemModel *model,
1079 const GtkFilePath *path,
1080 GtkFileSystemModelPathFunc func,
1083 GSList *cleanups = NULL;
1084 FileModelNode *node = find_and_ref_path (model, path, &cleanups);
1091 iter.user_data = node;
1092 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
1094 (*func) (model, path, &iter, user_data);
1096 gtk_tree_path_free (path);
1097 unref_node_and_parents (model, node);
1100 g_slist_foreach (cleanups, (GFunc)g_object_unref, NULL);
1101 g_slist_free (cleanups);
1103 return node != NULL;
1107 * _gtk_file_system_model_add_editable:
1108 * @model: a #GtkFileSystemModel
1109 * @iter: Location to return the iter corresponding to the editable row
1111 * Adds an "empty" row at the beginning of the model. This does not refer to
1112 * any file, but is a temporary placeholder for a file name that the user will
1113 * type when a corresponding cell is made editable. When your code is done
1114 * using this temporary row, call _gtk_file_system_model_remove_editable().
1117 _gtk_file_system_model_add_editable (GtkFileSystemModel *model, GtkTreeIter *iter)
1119 FileModelNode *node;
1122 g_return_if_fail (!model->has_editable);
1124 model->has_editable = TRUE;
1126 node = file_model_node_new (model, NULL);
1127 node->is_visible = TRUE;
1129 node->next = model->roots;
1130 model->roots = node;
1132 file_model_node_ref (node);
1134 path = gtk_tree_path_new ();
1135 gtk_tree_path_append_index (path, 0);
1136 iter->user_data = node;
1138 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, iter);
1140 gtk_tree_path_free (path);
1144 * _gtk_file_system_model_remove_editable:
1145 * @model: a #GtkFileSystemModel
1147 * Removes the "empty" row at the beginning of the model that was
1148 * created with _gtk_file_system_model_add_editable(). You should call
1149 * this function when your code is finished editing this temporary row.
1152 _gtk_file_system_model_remove_editable (GtkFileSystemModel *model)
1156 g_return_if_fail (model->has_editable);
1158 model->has_editable = FALSE;
1159 file_model_node_unref (model, model->roots);
1161 model->roots = model->roots->next;
1163 path = gtk_tree_path_new ();
1164 gtk_tree_path_append_index (path, 0);
1166 gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
1168 gtk_tree_path_free (path);
1171 static FileModelNode *
1172 file_model_node_new (GtkFileSystemModel *model,
1173 const GtkFilePath *path)
1175 FileModelNode *node = g_new0 (FileModelNode, 1);
1177 node->model = model;
1178 node->path = path ? gtk_file_path_copy (path) : NULL;
1184 file_model_node_free (FileModelNode *node)
1186 file_model_node_clear (node->model, node);
1189 gtk_file_path_free (node->path);
1192 gtk_file_info_free (node->info);
1197 static const GtkFileInfo *
1198 file_model_node_get_info (GtkFileSystemModel *model,
1199 FileModelNode *node)
1205 node->info = gtk_file_info_new ();
1206 gtk_file_info_set_display_name (node->info, _("(Empty)"));
1208 else if (node->parent || model->root_folder)
1210 node->info = gtk_file_folder_get_info (node->parent ? node->parent->folder : model->root_folder,
1212 NULL); /* NULL-GError */
1217 node->info = gtk_file_system_get_root_info (model->file_system,
1220 NULL); /* NULL-GError */
1229 file_model_node_is_visible (GtkFileSystemModel *model,
1230 FileModelNode *node)
1232 if (model->show_folders != model->show_files ||
1233 !model->show_hidden ||
1236 const GtkFileInfo *info = file_model_node_get_info (model, node);
1240 /* File probably disappeared underneath us or resides in a
1241 directory where we have only partial access rights. */
1245 if (model->show_folders != model->show_files &&
1246 model->show_folders != gtk_file_info_get_is_folder (info))
1249 if (!model->show_hidden && gtk_file_info_get_is_hidden (info))
1252 if (model->filter_func &&
1253 !model->filter_func (model, node->path, info, model->filter_data))
1261 file_model_node_clear (GtkFileSystemModel *model,
1262 FileModelNode *node)
1264 FileModelNode *children;
1266 file_model_node_idle_clear_cancel (node);
1268 children = node->children;
1269 node->children = NULL;
1270 node->loaded = FALSE;
1274 FileModelNode *next = children->next;
1276 file_model_node_free (children);
1283 /* Unreffing node->folder may cause roots_changed,
1284 * so we need to be careful about ordering.
1286 GtkFileFolder *folder = node->folder;
1287 node->folder = NULL;
1289 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (deleted_callback), node);
1290 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_added_callback), node);
1291 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_changed_callback), node);
1292 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_removed_callback), node);
1294 g_object_unref (folder);
1299 file_model_node_ref (FileModelNode *node)
1302 if (node->ref_count == 1 && node->parent)
1303 node->parent->n_referenced_children++;
1307 idle_clear_callback (GtkFileSystemModel *model)
1309 while (model->idle_clears)
1311 FileModelNode *node = model->idle_clears->data;
1312 model->idle_clears = g_slist_delete_link (model->idle_clears, model->idle_clears);
1314 node->idle_clear = FALSE;
1315 file_model_node_clear (node->model, node);
1322 file_model_node_idle_clear (FileModelNode *node)
1324 if (!node->idle_clear)
1326 GtkFileSystemModel *model = node->model;
1328 node->idle_clear = TRUE;
1329 if (!model->idle_clears)
1331 model->idle_clear_source = g_idle_source_new ();
1332 g_source_set_priority (model->idle_clear_source, G_PRIORITY_HIGH);
1333 g_source_set_closure (model->idle_clear_source,
1334 g_cclosure_new_object (G_CALLBACK (idle_clear_callback),
1336 g_source_attach (model->idle_clear_source, NULL);
1339 model->idle_clears = g_slist_prepend (model->idle_clears, node);
1340 node->idle_clear = TRUE;
1345 file_model_node_idle_clear_cancel (FileModelNode *node)
1347 if (node->idle_clear)
1349 GtkFileSystemModel *model = node->model;
1351 model->idle_clears = g_slist_remove (model->idle_clears, node);
1352 if (!model->idle_clears)
1354 g_source_destroy (model->idle_clear_source);
1355 model->idle_clear_source = NULL;
1358 node->idle_clear = FALSE;
1363 file_model_node_unref (GtkFileSystemModel *model,
1364 FileModelNode *node)
1367 if (node->ref_count == 0)
1369 file_model_node_clear (model, node);
1371 file_model_node_child_unref (node->parent);
1376 file_model_node_child_unref (FileModelNode *parent)
1378 parent->n_referenced_children--;
1379 if (parent->n_referenced_children == 0)
1380 file_model_node_idle_clear (parent);
1383 static FileModelNode *
1384 file_model_node_get_children (GtkFileSystemModel *model,
1385 FileModelNode *node)
1387 if (node->ref_count == 0)
1392 const GtkFileInfo *info = file_model_node_get_info (model, node);
1393 gboolean has_children = FALSE;
1394 gboolean is_folder = node->depth < model->max_depth && gtk_file_info_get_is_folder (info);
1396 file_model_node_idle_clear_cancel (node);
1399 node->folder = gtk_file_system_get_folder (model->file_system,
1402 NULL); /* NULL-GError */
1406 GSList *child_paths, *tmp_list;
1408 if (gtk_file_folder_list_children (node->folder, &child_paths, NULL)) /* NULL-GError */
1410 child_paths = gtk_file_paths_sort (child_paths);
1412 for (tmp_list = child_paths; tmp_list; tmp_list = tmp_list->next)
1414 FileModelNode *child_node = file_model_node_new (model, tmp_list->data);
1415 gtk_file_path_free (tmp_list->data);
1416 child_node->next = node->children;
1417 child_node->parent = node;
1418 child_node->depth = node->depth + 1;
1419 child_node->is_visible = file_model_node_is_visible (model, child_node);
1420 if (child_node->is_visible)
1421 has_children = TRUE;
1422 node->children = child_node;
1424 g_slist_free (child_paths);
1427 node->children = (FileModelNode *)g_slist_reverse ((GSList *)node->children);
1429 g_signal_connect (node->folder, "deleted",
1430 G_CALLBACK (deleted_callback), node);
1431 g_signal_connect (node->folder, "files-added",
1432 G_CALLBACK (files_added_callback), node);
1433 g_signal_connect (node->folder, "files-changed",
1434 G_CALLBACK (files_changed_callback), node);
1435 g_signal_connect (node->folder, "files-removed",
1436 G_CALLBACK (files_removed_callback), node);
1438 g_object_set_data (G_OBJECT (node->folder), "model-node", node);
1441 if (is_folder && !has_children)
1443 /* The hard case ... we claimed this folder had children, but actually
1444 * it didn't. We have to add a dummy child, possibly to remove later.
1446 FileModelNode *child_node = file_model_node_new (model, NULL);
1447 child_node->is_visible = TRUE;
1448 child_node->parent = node;
1449 child_node->is_dummy = TRUE;
1451 node->children = child_node;
1452 node->has_dummy = TRUE;
1455 node->loaded = TRUE;
1458 return node->children;
1462 do_files_added (GtkFileSystemModel *model,
1463 FileModelNode *parent_node,
1466 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1467 FileModelNode *children;
1468 FileModelNode *prev = NULL;
1471 GSList *sorted_paths;
1474 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1478 iter.user_data = parent_node;
1479 path = gtk_tree_model_get_path (tree_model, &iter);
1480 children = parent_node->children;
1484 path = gtk_tree_path_new ();
1485 children = model->roots;
1488 gtk_tree_path_down (path);
1490 if (parent_node && parent_node->has_dummy)
1493 children = children->next;
1494 gtk_tree_path_next (path);
1497 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1499 const GtkFilePath *file_path = tmp_list->data;
1502 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1505 if (children->is_visible)
1506 gtk_tree_path_next (path);
1508 children = children->next;
1512 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1514 /* Shouldn't happen */
1520 new = file_model_node_new (model, file_path);
1523 new->next = children;
1526 else if (parent_node)
1527 parent_node->children = new;
1535 new->parent = parent_node;
1536 new->depth = parent_node->depth + 1;
1539 new->is_visible = file_model_node_is_visible (model, new);
1541 if (new->is_visible)
1543 iter.user_data = new;
1544 path = gtk_tree_model_get_path (tree_model, &iter);
1545 gtk_tree_model_row_inserted (tree_model, path, &iter);
1547 if (gtk_file_system_model_iter_has_child (tree_model, &iter))
1548 gtk_tree_model_row_has_child_toggled (tree_model, path, &iter);
1550 if (parent_node && parent_node->has_dummy)
1552 FileModelNode *dummy = parent_node->children;
1553 GtkTreePath *dummy_path;
1555 parent_node->children = parent_node->children->next;
1556 parent_node->has_dummy = FALSE;
1558 dummy_path = gtk_tree_path_copy (path);
1559 gtk_tree_path_up (dummy_path);
1560 gtk_tree_path_down (dummy_path);
1562 gtk_tree_model_row_deleted (tree_model, dummy_path);
1563 gtk_tree_path_free (dummy_path);
1565 if (dummy->ref_count)
1566 file_model_node_child_unref (parent_node);
1567 file_model_node_free (dummy);
1570 gtk_tree_path_next (path);
1575 gtk_tree_path_free (path);
1576 g_slist_free (sorted_paths);
1580 do_files_changed (GtkFileSystemModel *model,
1581 FileModelNode *parent_node,
1584 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1585 FileModelNode *children;
1586 FileModelNode *prev = NULL;
1589 GSList *sorted_paths;
1592 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1596 iter.user_data = parent_node;
1597 path = gtk_tree_model_get_path (tree_model, &iter);
1598 children = parent_node->children;
1602 path = gtk_tree_path_new ();
1603 children = model->roots;
1606 gtk_tree_path_down (path);
1608 if (parent_node && parent_node->has_dummy)
1611 children = children->next;
1612 gtk_tree_path_next (path);
1615 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1617 const GtkFilePath *file_path = tmp_list->data;
1620 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1623 if (children->is_visible)
1624 gtk_tree_path_next (path);
1626 children = children->next;
1630 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1632 gtk_tree_model_row_changed (tree_model, path, &iter);
1636 /* Shouldn't happen */
1640 gtk_tree_path_free (path);
1641 g_slist_free (sorted_paths);
1645 do_files_removed (GtkFileSystemModel *model,
1646 FileModelNode *parent_node,
1649 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1650 FileModelNode *children;
1651 FileModelNode *prev = NULL;
1654 GSList *sorted_paths;
1656 FileModelNode *tmp_child;
1659 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1663 iter.user_data = parent_node;
1664 path = gtk_tree_model_get_path (tree_model, &iter);
1665 children = parent_node->children;
1669 path = gtk_tree_path_new ();
1670 children = model->roots;
1673 /* Count the number of currently visible children, so that
1674 * can catch when we need to insert a dummy node.
1677 for (tmp_child = children; tmp_child; tmp_child = tmp_child->next)
1679 if (tmp_child->is_visible)
1683 gtk_tree_path_down (path);
1685 if (parent_node && parent_node->has_dummy)
1688 children = children->next;
1689 gtk_tree_path_next (path);
1692 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1694 const GtkFilePath *file_path = tmp_list->data;
1697 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1700 if (children->is_visible)
1701 gtk_tree_path_next (path);
1703 children = children->next;
1707 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1709 FileModelNode *next = children->next;
1711 if (children->is_visible)
1716 FileModelNode *dummy = file_model_node_new (model, NULL);
1717 dummy->is_visible = TRUE;
1718 dummy->parent = parent_node;
1719 dummy->is_dummy = TRUE;
1721 parent_node->children = dummy;
1722 parent_node->has_dummy = TRUE;
1724 iter.user_data = dummy;
1725 gtk_tree_model_row_inserted (tree_model, path, &iter);
1726 gtk_tree_path_next (path);
1733 else if (parent_node)
1734 parent_node->children = next;
1736 model->roots = next;
1738 if (parent_node && children->ref_count)
1739 file_model_node_child_unref (parent_node);
1741 if (children->is_visible)
1742 gtk_tree_model_row_deleted (tree_model, path);
1744 file_model_node_free (children);
1750 /* Shouldn't happen */
1754 gtk_tree_path_free (path);
1755 g_slist_free (sorted_paths);
1760 roots_changed_callback (GtkFileSystem *file_system,
1761 GtkFileSystemModel *model)
1763 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1766 FileModelNode *children;
1767 FileModelNode *prev = NULL;
1770 new_roots = gtk_file_system_list_roots (file_system);
1771 new_roots = gtk_file_paths_sort (new_roots);
1773 children = model->roots;
1774 tmp_list = new_roots;
1775 path = gtk_tree_path_new ();
1776 gtk_tree_path_down (path);
1778 while (children || tmp_list)
1780 FileModelNode *next = NULL;
1783 if (tmp_list && children)
1784 cmp = gtk_file_path_compare (children->path, tmp_list->data);
1792 next = children->next;
1795 prev->next = children->next;
1797 model->roots = children->next;
1799 if (children->is_visible)
1800 gtk_tree_model_row_deleted (tree_model, path);
1802 file_model_node_free (children);
1808 next = children->next;
1810 if (children->is_visible)
1811 gtk_tree_path_next (path);
1816 FileModelNode *node = file_model_node_new (model, tmp_list->data);
1817 node->is_visible = file_model_node_is_visible (model, node);
1818 node->next = children;
1824 model->roots = node;
1826 if (node->is_visible)
1828 iter.user_data = node;
1829 gtk_tree_model_row_inserted (tree_model, path, &iter);
1831 if (gtk_file_system_model_iter_has_child (tree_model, &iter))
1832 gtk_tree_model_row_has_child_toggled (tree_model, path, &iter);
1834 gtk_tree_path_next (path);
1847 gtk_file_path_free (tmp_list->data);
1848 tmp_list = tmp_list->next;
1852 g_slist_free (new_roots);
1853 gtk_tree_path_free (path);
1858 deleted_callback (GtkFileFolder *folder,
1859 FileModelNode *node)
1864 files_added_callback (GtkFileFolder *folder,
1866 FileModelNode *node)
1868 do_files_added (node->model, node, paths);
1872 files_changed_callback (GtkFileFolder *folder,
1874 FileModelNode *node)
1876 do_files_changed (node->model, node, paths);
1880 files_removed_callback (GtkFileFolder *folder,
1882 FileModelNode *node)
1884 do_files_removed (node->model, node, paths);
1888 root_deleted_callback (GtkFileFolder *folder,
1889 GtkFileSystemModel *model)
1894 root_files_added_callback (GtkFileFolder *folder,
1896 GtkFileSystemModel *model)
1898 do_files_added (model, NULL, paths);
1902 root_files_changed_callback (GtkFileFolder *folder,
1904 GtkFileSystemModel *model)
1906 do_files_changed (model, NULL, paths);
1910 root_files_removed_callback (GtkFileFolder *folder,
1912 GtkFileSystemModel *model)
1914 do_files_removed (model, NULL, paths);