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.
23 #include "gtkfilesystemmodel.h"
24 #include "gtkfilesystem.h"
26 #include "gtktreemodel.h"
28 typedef struct _GtkFileSystemModelClass GtkFileSystemModelClass;
29 typedef struct _FileModelNode FileModelNode;
31 #define GTK_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
32 #define GTK_IS_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_MODEL))
33 #define GTK_FILE_SYSTEM_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
35 struct _GtkFileSystemModelClass
37 GObjectClass parent_class;
40 struct _GtkFileSystemModel
42 GObject parent_instance;
44 GtkFileSystem *file_system;
45 GtkFileInfoType types;
47 GtkFileFolder *root_folder;
49 GtkFileSystemModelFilter filter_func;
53 GSource *idle_clear_source;
57 guint show_hidden : 1;
58 guint show_folders : 1;
60 guint folders_only : 1;
69 GtkFileFolder *folder;
71 FileModelNode *children;
72 FileModelNode *parent;
73 GtkFileSystemModel *model;
76 guint n_referenced_children;
87 static void gtk_file_system_model_class_init (GtkFileSystemModelClass *class);
88 static void gtk_file_system_model_iface_init (GtkTreeModelIface *iface);
89 static void gtk_file_system_model_init (GtkFileSystemModel *model);
90 static void gtk_file_system_model_finalize (GObject *object);
92 static GtkTreeModelFlags gtk_file_system_model_get_flags (GtkTreeModel *tree_model);
93 static gint gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model);
94 static GType gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
96 static gboolean gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
99 static GtkTreePath * gtk_file_system_model_get_path (GtkTreeModel *tree_model,
101 static void gtk_file_system_model_get_value (GtkTreeModel *tree_model,
105 static gboolean gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
107 static gboolean gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
109 GtkTreeIter *parent);
110 static gboolean gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
112 static gint gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
114 static gboolean gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
118 static gboolean gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
121 static void gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
123 static void gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
126 static FileModelNode *file_model_node_new (GtkFileSystemModel *model,
127 const GtkFilePath *path);
128 static void file_model_node_free (FileModelNode *node);
129 static void file_model_node_ref (FileModelNode *node);
130 static void file_model_node_unref (GtkFileSystemModel *model,
131 FileModelNode *node);
133 static void file_model_node_idle_clear (FileModelNode *node);
134 static void file_model_node_idle_clear_cancel (FileModelNode *node);
135 static void file_model_node_child_unref (FileModelNode *parent);
137 static const GtkFileInfo *file_model_node_get_info (GtkFileSystemModel *model,
138 FileModelNode *node);
139 static gboolean file_model_node_is_visible (GtkFileSystemModel *model,
140 FileModelNode *node);
141 static void file_model_node_clear (GtkFileSystemModel *model,
142 FileModelNode *node);
143 static FileModelNode * file_model_node_get_children (GtkFileSystemModel *model,
144 FileModelNode *node);
146 static void roots_changed_callback (GtkFileSystem *file_system,
147 GtkFileSystemModel *model);
149 static void deleted_callback (GtkFileFolder *folder,
150 FileModelNode *node);
151 static void files_added_callback (GtkFileFolder *folder,
153 FileModelNode *node);
154 static void files_changed_callback (GtkFileFolder *folder,
156 FileModelNode *node);
157 static void files_removed_callback (GtkFileFolder *folder,
159 FileModelNode *node);
161 static void root_deleted_callback (GtkFileFolder *folder,
162 GtkFileSystemModel *model);
163 static void root_files_added_callback (GtkFileFolder *folder,
165 GtkFileSystemModel *model);
166 static void root_files_changed_callback (GtkFileFolder *folder,
168 GtkFileSystemModel *model);
169 static void root_files_removed_callback (GtkFileFolder *folder,
171 GtkFileSystemModel *model);
174 _gtk_file_system_model_get_type (void)
176 static GType file_system_model_type = 0;
178 if (!file_system_model_type)
180 static const GTypeInfo file_system_model_info =
182 sizeof (GtkFileSystemModelClass),
183 NULL, /* base_init */
184 NULL, /* base_finalize */
185 (GClassInitFunc) gtk_file_system_model_class_init,
186 NULL, /* class_finalize */
187 NULL, /* class_data */
188 sizeof (GtkFileSystemModel),
190 (GInstanceInitFunc) gtk_file_system_model_init,
193 static const GInterfaceInfo file_system_info =
195 (GInterfaceInitFunc) gtk_file_system_model_iface_init, /* interface_init */
196 NULL, /* interface_finalize */
197 NULL /* interface_data */
200 file_system_model_type = g_type_register_static (G_TYPE_OBJECT,
201 "GtkFileSystemModel",
202 &file_system_model_info, 0);
203 g_type_add_interface_static (file_system_model_type,
208 return file_system_model_type;
212 gtk_file_system_model_class_init (GtkFileSystemModelClass *class)
214 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
216 gobject_class->finalize = gtk_file_system_model_finalize;
220 gtk_file_system_model_iface_init (GtkTreeModelIface *iface)
222 iface->get_flags = gtk_file_system_model_get_flags;
223 iface->get_n_columns = gtk_file_system_model_get_n_columns;
224 iface->get_column_type = gtk_file_system_model_get_column_type;
225 iface->get_iter = gtk_file_system_model_get_iter;
226 iface->get_path = gtk_file_system_model_get_path;
227 iface->get_value = gtk_file_system_model_get_value;
228 iface->iter_next = gtk_file_system_model_iter_next;
229 iface->iter_children = gtk_file_system_model_iter_children;
230 iface->iter_has_child = gtk_file_system_model_iter_has_child;
231 iface->iter_n_children = gtk_file_system_model_iter_n_children;
232 iface->iter_nth_child = gtk_file_system_model_iter_nth_child;
233 iface->iter_parent = gtk_file_system_model_iter_parent;
234 iface->ref_node = gtk_file_system_model_ref_node;
235 iface->unref_node = gtk_file_system_model_unref_node;
239 gtk_file_system_model_init (GtkFileSystemModel *model)
241 model->show_files = TRUE;
242 model->show_folders = TRUE;
243 model->show_hidden = FALSE;
247 gtk_file_system_model_finalize (GObject *object)
249 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
250 FileModelNode *children;
252 if (model->root_folder)
253 g_object_unref (model->root_folder);
255 children = model->roots;
258 file_model_node_free (children);
259 children = children->next;
264 * ******************** GtkTreeModel methods ********************
267 static GtkTreeModelFlags
268 gtk_file_system_model_get_flags (GtkTreeModel *tree_model)
270 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
271 GtkTreeModelFlags flags = GTK_TREE_MODEL_ITERS_PERSIST;
273 if (model->max_depth == 1)
274 flags |= GTK_TREE_MODEL_LIST_ONLY;
280 gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model)
282 return GTK_FILE_SYSTEM_MODEL_N_COLUMNS;
286 gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
291 case GTK_FILE_SYSTEM_MODEL_INFO:
292 return GTK_TYPE_FILE_INFO;
293 case GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME:
294 return G_TYPE_STRING;
296 g_assert_not_reached ();
302 gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
310 indices = gtk_tree_path_get_indices (path);
311 depth = gtk_tree_path_get_depth (path);
313 g_return_val_if_fail (depth > 0, FALSE);
315 if (!gtk_tree_model_iter_nth_child (tree_model, iter, NULL, indices[0]))
318 for (i = 1; i < depth; i++)
321 if (!gtk_tree_model_iter_nth_child (tree_model, iter, &parent, indices[i]))
329 gtk_file_system_model_get_path (GtkTreeModel *tree_model,
332 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
333 FileModelNode *node = iter->user_data;
335 GtkTreePath *result = gtk_tree_path_new ();
339 FileModelNode *parent = node->parent;
340 FileModelNode *children;
344 children = parent->children;
346 children = model->roots;
348 while (children != node)
350 if (children->is_visible)
352 children = children->next;
355 gtk_tree_path_prepend_index (result, n);
364 gtk_file_system_model_get_value (GtkTreeModel *tree_model,
369 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
370 FileModelNode *node = iter->user_data;
374 case GTK_FILE_SYSTEM_MODEL_INFO:
375 g_value_init (value, GTK_TYPE_FILE_INFO);
376 g_value_set_boxed (value, file_model_node_get_info (model, node));
378 case GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME:
380 const GtkFileInfo *info = file_model_node_get_info (model, node);
381 g_value_init (value, G_TYPE_STRING);
382 g_value_set_string (value, gtk_file_info_get_display_name (info));
386 g_assert_not_reached ();
391 gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
394 FileModelNode *node = iter->user_data;
397 while (node && !node->is_visible)
400 iter->user_data = node;
406 gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
410 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
411 FileModelNode *children;
415 FileModelNode *parent_node = parent->user_data;
416 children = file_model_node_get_children (model, parent_node);
420 children = model->roots;
423 while (children && !children->is_visible)
424 children = children->next;
426 iter->user_data = children;
428 return children != NULL;
432 gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
435 FileModelNode *node = iter->user_data;
436 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
438 if (node->depth == model->max_depth)
442 const GtkFileInfo *info = file_model_node_get_info (model, node);
443 return gtk_file_info_get_is_folder (info);
448 gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
451 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
452 FileModelNode *children;
457 FileModelNode *node = iter->user_data;
458 children = file_model_node_get_children (model, node);
462 children = model->roots;
467 if (children->is_visible)
469 children = children->next;
476 gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
481 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
482 FileModelNode *children;
486 FileModelNode *parent_node = parent->user_data;
487 children = file_model_node_get_children (model, parent_node);
491 children = model->roots;
494 while (children && !children->is_visible)
495 children = children->next;
497 while (n && children)
500 children = children->next;
501 while (children && !children->is_visible)
502 children = children->next;
505 iter->user_data = children;
507 return children != NULL;
511 gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
515 FileModelNode *node = child->user_data;
518 iter->user_data = node;
524 gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
527 file_model_node_ref (iter->user_data);
531 gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
534 file_model_node_unref (GTK_FILE_SYSTEM_MODEL (tree_model),
539 * _gtk_file_system_model_new:
540 * @file_system: an object implementing #GtkFileSystem
541 * @root_path: the path of root of the file system to display,
542 * or %NULL to display starting from the
543 * root or roots of the fielsystem.
544 * @max_depth: the maximum depth from the children of @root_path
545 * or the roots of the file system to display in
546 * the file selector). A depth of 0 displays
547 * only the immediate children of @root_path,
548 * or the roots of the filesystem. -1 for no
550 * @types: a bitmask indicating the types of information
551 * that is desired about the files. This will
552 * determine what information is returned by
553 * _gtk_file_system_model_get_info().
555 * Creates a new #GtkFileSystemModel object. The #GtkFileSystemModel
556 * object wraps a #GtkFileSystem interface as a #GtkTreeModel.
557 * Using the @root_path and @max_depth parameters, the tree model
558 * can be restricted to a subportion of the entire file system.
560 * Return value: the newly created #GtkFileSystemModel object.
563 _gtk_file_system_model_new (GtkFileSystem *file_system,
564 const GtkFilePath *root_path,
566 GtkFileInfoType types)
568 GtkFileSystemModel *model;
569 GSList *roots = NULL;
572 g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
574 model = g_object_new (GTK_TYPE_FILE_SYSTEM_MODEL, NULL);
575 model->file_system = g_object_ref (file_system);
577 model->max_depth = G_MAXUSHORT;
579 model->max_depth = MIN (max_depth, G_MAXUSHORT);
580 model->types = types | GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN;
586 model->root_folder = gtk_file_system_get_folder (file_system, root_path,
588 NULL); /* NULL-GError */
590 if (model->root_folder &&
591 gtk_file_folder_list_children (model->root_folder,
593 NULL)) /* NULL-GError */
597 g_signal_connect_object (model->root_folder, "deleted",
598 G_CALLBACK (root_deleted_callback), model, 0);
599 g_signal_connect_object (model->root_folder, "files-added",
600 G_CALLBACK (root_files_added_callback), model, 0);
601 g_signal_connect_object (model->root_folder, "files-changed",
602 G_CALLBACK (root_files_changed_callback), model, 0);
603 g_signal_connect_object (model->root_folder, "files-removed",
604 G_CALLBACK (root_files_removed_callback), model, 0);
609 roots = gtk_file_system_list_roots (file_system);
610 g_signal_connect_object (file_system, "roots-changed",
611 G_CALLBACK (roots_changed_callback), model, 0);
614 roots = gtk_file_paths_sort (roots);
616 for (tmp_list = roots; tmp_list; tmp_list = tmp_list->next)
618 FileModelNode *node = file_model_node_new (model, tmp_list->data);
619 gtk_file_path_free (tmp_list->data);
620 node->is_visible = file_model_node_is_visible (model, node);
621 node->next = model->roots;
625 g_slist_free (roots);
627 model->roots = (FileModelNode *)g_slist_reverse ((GSList *)model->roots);
633 model_refilter_recurse (GtkFileSystemModel *model,
634 FileModelNode *parent,
637 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
639 FileModelNode *nodes;
640 gboolean has_children = FALSE;
642 if (parent && !parent->loaded)
646 nodes = parent->children;
648 nodes = model->roots;
652 FileModelNode *next = nodes->next;
655 gtk_tree_path_append_index (path, i);
657 is_visible = file_model_node_is_visible (model, nodes);
659 if (!is_visible && nodes->is_visible)
661 file_model_node_clear (model, nodes);
662 gtk_tree_model_row_deleted (tree_model, path);
664 nodes->is_visible = FALSE;
666 else if (is_visible && !nodes->is_visible)
670 iter.user_data = nodes;
671 gtk_tree_model_row_inserted (tree_model, path, &iter);
673 nodes->is_visible = TRUE;
676 model_refilter_recurse (model, nodes, path);
684 gtk_tree_path_up (path);
689 if (parent && !has_children)
691 /* Fixme - need to insert dummy node here */
696 model_refilter_all (GtkFileSystemModel *model)
700 path = gtk_tree_path_new ();
701 model_refilter_recurse (model, NULL, path);
702 gtk_tree_path_free (path);
706 * _gtk_file_system_model_set_show_hidden:
707 * @model: a #GtkFileSystemModel
708 * @show_hidden: whether hidden files should be displayed
710 * Sets whether hidden files should be included in the #GtkTreeModel
714 _gtk_file_system_model_set_show_hidden (GtkFileSystemModel *model,
715 gboolean show_hidden)
717 show_hidden = show_hidden != FALSE;
719 if (show_hidden != model->show_hidden)
721 model->show_hidden = show_hidden;
722 model_refilter_all (model);
727 * _gtk_file_system_model_set_show_folders:
728 * @model: a #GtkFileSystemModel
729 * @show_folders: whether folders should be displayed
731 * Sets whether folders should be included in the #GtkTreeModel for
735 _gtk_file_system_model_set_show_folders (GtkFileSystemModel *model,
736 gboolean show_folders)
738 show_folders = show_folders != FALSE;
740 if (show_folders != model->show_folders)
742 model->show_folders = show_folders;
743 model_refilter_all (model);
748 * _gtk_file_system_model_set_show_files:
749 * @model: a #GtkFileSystemModel
750 * @show_files: whether files (as opposed to folders) should
753 * Sets whether files (as opposed to folders) should be included
754 * in the #GtkTreeModel for display.
757 _gtk_file_system_model_set_show_files (GtkFileSystemModel *model,
760 show_files = show_files != FALSE;
762 if (show_files != model->show_files)
764 model->show_files = show_files;
765 model_refilter_all (model);
770 * _gtk_file_system_model_get_info:
771 * @model: a #GtkFileSystemModel
772 * @iter: a #GtkTreeIter pointing to a row of @model
774 * Gets the #GtkFileInfo structure for a particular row
775 * of @model. The information included in this structure
776 * is determined by the @types parameter to
777 * _gtk_file_system_model_new().
779 * Return value: a #GtkFileInfo structure. This structure
780 * is owned by @model and must not be modified or freed.
781 * If you want to save the information for later use,
782 * you must make a copy, since the structure may be
783 * freed on later changes to the file system.
786 _gtk_file_system_model_get_info (GtkFileSystemModel *model,
789 return file_model_node_get_info (model, iter->user_data);
793 * _gtk_file_system_model_get_path:
794 * @model: a #GtkFileSystemModel
795 * @iter: a #GtkTreeIter pointing to a row of @model
797 * Gets the path for a particular row in @model.
799 * Return value: the path. This string is owned by @model and
800 * or freed. If you want to save the path for later use,
801 * you must make a copy, since the string may be freed
802 * on later changes to the file system.
805 _gtk_file_system_model_get_path (GtkFileSystemModel *model,
808 FileModelNode *node = iter->user_data;
811 return node->parent->path;
817 unref_node_and_parents (GtkFileSystemModel *model,
820 file_model_node_unref (model, node);
822 file_model_node_unref (model, node->parent);
825 static FileModelNode *
826 find_child_node (GtkFileSystemModel *model,
827 FileModelNode *parent_node,
828 const GtkFilePath *path)
830 FileModelNode *children;
833 children = file_model_node_get_children (model, parent_node);
835 children = model->roots;
839 if (children->is_visible &&
841 gtk_file_path_compare (children->path, path) == 0)
844 children = children->next;
851 static FileModelNode *
852 find_and_ref_path (GtkFileSystemModel *model,
853 const GtkFilePath *path,
856 GtkFilePath *parent_path;
857 FileModelNode *parent_node;
858 FileModelNode *child_node;
859 GtkFileFolder *folder;
861 if (!gtk_file_system_get_parent (model->file_system, path, &parent_path, NULL))
866 parent_node = find_and_ref_path (model, parent_path, cleanups);
867 gtk_file_path_free (parent_path);
875 child_node = find_child_node (model, parent_node, path);
878 file_model_node_ref (child_node);
882 folder = gtk_file_system_get_folder (model->file_system,
885 NULL); /* NULL-GError */
888 *cleanups = g_slist_prepend (*cleanups, folder);
890 child_node = find_child_node (model, parent_node, path);
893 file_model_node_ref (child_node);
899 unref_node_and_parents (model, parent_node);
905 * _gtk_file_system_model_set_filter:
906 * @mode: a #GtkFileSystemModel
907 * @filter: function to be called for each file
908 * @user_data: data to pass to @filter
910 * Sets a callback called for each file/directory to see whether
911 * it should be included in model. If this function was made
912 * public, we'd want to include a GDestroyNotify as well.
915 _gtk_file_system_model_set_filter (GtkFileSystemModel *model,
916 GtkFileSystemModelFilter filter,
919 g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
921 model->filter_func = filter;
922 model->filter_data = user_data;
924 model_refilter_all (model);
928 * _gtk_file_system_model_path_do:
929 * @model: a #GtkFileSystemModel
930 * @path: a path pointing to a file in the filesystem
932 * @func: Function to call with the path and iter corresponding
934 * @user_data: data to pass to @func
936 * Locates @path within @model, referencing
937 * (gtk_tree_model_ref_node ()) all parent nodes,
938 * calls @func passing in the path and iter for @path,
939 * then unrefs all the parent nodes.
941 * The reason for doing this operation as a callback
942 * is so that if the operation performed with the the
943 * path and iter results in referencing the the node
944 * and/or parent nodes, we don't load all the information
947 * This function is particularly useful for expanding
948 * a #GtkTreeView to a particular point in the file system.
950 * Return value: %TRUE if the path was successfully
951 * found in @model and @func was called.
954 _gtk_file_system_model_path_do (GtkFileSystemModel *model,
955 const GtkFilePath *path,
956 GtkFileSystemModelPathFunc func,
959 GSList *cleanups = NULL;
960 FileModelNode *node = find_and_ref_path (model, path, &cleanups);
967 iter.user_data = node;
968 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
970 (*func) (model, path, &iter, user_data);
972 gtk_tree_path_free (path);
973 unref_node_and_parents (model, node);
976 g_slist_foreach (cleanups, (GFunc)g_object_unref, NULL);
977 g_slist_free (cleanups);
982 static FileModelNode *
983 file_model_node_new (GtkFileSystemModel *model,
984 const GtkFilePath *path)
986 FileModelNode *node = g_new0 (FileModelNode, 1);
989 node->path = path ? gtk_file_path_copy (path) : NULL;
995 file_model_node_free (FileModelNode *node)
997 file_model_node_clear (node->model, node);
1000 gtk_file_path_free (node->path);
1003 gtk_file_info_free (node->info);
1008 static const GtkFileInfo *
1009 file_model_node_get_info (GtkFileSystemModel *model,
1010 FileModelNode *node)
1016 node->info = gtk_file_info_new ();
1017 gtk_file_info_set_display_name (node->info, _("(Empty)"));
1019 else if (node->parent || model->root_folder)
1021 node->info = gtk_file_folder_get_info (node->parent ? node->parent->folder : model->root_folder,
1023 NULL); /* NULL-GError */
1027 node->info = gtk_file_system_get_root_info (model->file_system,
1030 NULL); /* NULL-GError */
1038 file_model_node_is_visible (GtkFileSystemModel *model,
1039 FileModelNode *node)
1041 if (model->show_hidden && model->show_folders && model->show_files)
1045 const GtkFileInfo *info = file_model_node_get_info (model, node);
1046 gboolean is_folder = gtk_file_info_get_is_folder (info);
1048 if (!model->show_folders && is_folder)
1050 if (!model->show_files && !is_folder)
1052 if (!model->show_hidden && gtk_file_info_get_is_hidden (info))
1054 if (model->filter_func && !model->filter_func (model, node->path, info, model->filter_data))
1062 file_model_node_clear (GtkFileSystemModel *model,
1063 FileModelNode *node)
1065 FileModelNode *children;
1067 file_model_node_idle_clear_cancel (node);
1069 children = node->children;
1070 node->children = NULL;
1071 node->loaded = FALSE;
1075 FileModelNode *next = children->next;
1077 file_model_node_free (children);
1084 /* Unreffing node->folder may cause roots_changed,
1085 * so we need to be careful about ordering.
1087 GtkFileFolder *folder = node->folder;
1088 node->folder = NULL;
1090 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (deleted_callback), node);
1091 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_added_callback), node);
1092 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_changed_callback), node);
1093 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_removed_callback), node);
1095 g_object_unref (folder);
1100 file_model_node_ref (FileModelNode *node)
1103 if (node->ref_count == 1 && node->parent)
1104 node->parent->n_referenced_children++;
1108 idle_clear_callback (GtkFileSystemModel *model)
1110 while (model->idle_clears)
1112 FileModelNode *node = model->idle_clears->data;
1113 model->idle_clears = g_slist_delete_link (model->idle_clears, model->idle_clears);
1115 node->idle_clear = FALSE;
1116 file_model_node_clear (node->model, node);
1123 file_model_node_idle_clear (FileModelNode *node)
1125 if (!node->idle_clear)
1127 GtkFileSystemModel *model = node->model;
1129 node->idle_clear = TRUE;
1130 if (!model->idle_clears)
1132 model->idle_clear_source = g_idle_source_new ();
1133 g_source_set_priority (model->idle_clear_source, G_PRIORITY_HIGH);
1134 g_source_set_closure (model->idle_clear_source,
1135 g_cclosure_new_object (G_CALLBACK (idle_clear_callback),
1137 g_source_attach (model->idle_clear_source, NULL);
1140 model->idle_clears = g_slist_prepend (model->idle_clears, node);
1141 node->idle_clear = TRUE;
1146 file_model_node_idle_clear_cancel (FileModelNode *node)
1148 if (node->idle_clear)
1150 GtkFileSystemModel *model = node->model;
1152 model->idle_clears = g_slist_remove (model->idle_clears, node);
1153 if (!model->idle_clears)
1155 g_source_destroy (model->idle_clear_source);
1156 model->idle_clear_source = NULL;
1159 node->idle_clear = FALSE;
1164 file_model_node_unref (GtkFileSystemModel *model,
1165 FileModelNode *node)
1168 if (node->ref_count == 0)
1170 file_model_node_clear (model, node);
1172 file_model_node_child_unref (node->parent);
1177 file_model_node_child_unref (FileModelNode *parent)
1179 parent->n_referenced_children--;
1180 if (parent->n_referenced_children == 0)
1181 file_model_node_idle_clear (parent);
1184 static FileModelNode *
1185 file_model_node_get_children (GtkFileSystemModel *model,
1186 FileModelNode *node)
1188 if (node->ref_count == 0)
1193 const GtkFileInfo *info = file_model_node_get_info (model, node);
1194 gboolean has_children = FALSE;
1195 gboolean is_folder = node->depth < model->max_depth && gtk_file_info_get_is_folder (info);
1197 file_model_node_idle_clear_cancel (node);
1200 node->folder = gtk_file_system_get_folder (model->file_system,
1203 NULL); /* NULL-GError */
1207 GSList *child_paths, *tmp_list;
1209 if (gtk_file_folder_list_children (node->folder, &child_paths, NULL)) /* NULL-GError */
1211 child_paths = gtk_file_paths_sort (child_paths);
1213 for (tmp_list = child_paths; tmp_list; tmp_list = tmp_list->next)
1215 FileModelNode *child_node = file_model_node_new (model, tmp_list->data);
1216 gtk_file_path_free (tmp_list->data);
1217 child_node->next = node->children;
1218 child_node->parent = node;
1219 child_node->depth = node->depth + 1;
1220 child_node->is_visible = file_model_node_is_visible (model, child_node);
1221 if (child_node->is_visible)
1222 has_children = TRUE;
1223 node->children = child_node;
1225 g_slist_free (child_paths);
1228 node->children = (FileModelNode *)g_slist_reverse ((GSList *)node->children);
1230 g_signal_connect (node->folder, "deleted",
1231 G_CALLBACK (deleted_callback), node);
1232 g_signal_connect (node->folder, "files-added",
1233 G_CALLBACK (files_added_callback), node);
1234 g_signal_connect (node->folder, "files-changed",
1235 G_CALLBACK (files_changed_callback), node);
1236 g_signal_connect (node->folder, "files-removed",
1237 G_CALLBACK (files_removed_callback), node);
1239 g_object_set_data (G_OBJECT (node->folder), "model-node", node);
1242 if (is_folder && !has_children)
1244 /* The hard case ... we claimed this folder had children, but actually
1245 * it didn't. We have to add a dummy child, possibly to remove later.
1247 FileModelNode *child_node = file_model_node_new (model, NULL);
1248 child_node->is_visible = TRUE;
1249 child_node->parent = node;
1250 child_node->is_dummy = TRUE;
1252 node->children = child_node;
1253 node->has_dummy = TRUE;
1256 node->loaded = TRUE;
1259 return node->children;
1263 do_files_added (GtkFileSystemModel *model,
1264 FileModelNode *parent_node,
1267 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1268 FileModelNode *children;
1269 FileModelNode *prev = NULL;
1272 GSList *sorted_paths;
1275 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1279 iter.user_data = parent_node;
1280 path = gtk_tree_model_get_path (tree_model, &iter);
1281 children = parent_node->children;
1285 path = gtk_tree_path_new ();
1286 children = model->roots;
1289 gtk_tree_path_down (path);
1291 if (parent_node && parent_node->has_dummy)
1294 children = children->next;
1295 gtk_tree_path_next (path);
1298 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1300 const GtkFilePath *file_path = tmp_list->data;
1303 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1306 if (children->is_visible)
1307 gtk_tree_path_next (path);
1309 children = children->next;
1313 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1315 /* Shouldn't happen */
1321 new = file_model_node_new (model, file_path);
1324 new->next = children;
1327 else if (parent_node)
1328 parent_node->children = new;
1336 new->parent = parent_node;
1337 new->depth = parent_node->depth + 1;
1340 new->is_visible = file_model_node_is_visible (model, new);
1342 if (new->is_visible)
1344 iter.user_data = new;
1345 path = gtk_tree_model_get_path (tree_model, &iter);
1346 gtk_tree_model_row_inserted (tree_model, path, &iter);
1348 if (gtk_file_system_model_iter_has_child (tree_model, &iter))
1349 gtk_tree_model_row_has_child_toggled (tree_model, path, &iter);
1351 if (parent_node && parent_node->has_dummy)
1353 FileModelNode *dummy = parent_node->children;
1354 GtkTreePath *dummy_path;
1356 parent_node->children = parent_node->children->next;
1357 parent_node->has_dummy = FALSE;
1359 dummy_path = gtk_tree_path_copy (path);
1360 gtk_tree_path_up (dummy_path);
1361 gtk_tree_path_down (dummy_path);
1363 gtk_tree_model_row_deleted (tree_model, dummy_path);
1364 gtk_tree_path_free (dummy_path);
1366 if (dummy->ref_count)
1367 file_model_node_child_unref (parent_node);
1368 file_model_node_free (dummy);
1371 gtk_tree_path_next (path);
1376 gtk_tree_path_free (path);
1377 g_slist_free (sorted_paths);
1381 do_files_changed (GtkFileSystemModel *model,
1382 FileModelNode *parent_node,
1385 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1386 FileModelNode *children;
1387 FileModelNode *prev = NULL;
1390 GSList *sorted_paths;
1393 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1397 iter.user_data = parent_node;
1398 path = gtk_tree_model_get_path (tree_model, &iter);
1399 children = parent_node->children;
1403 path = gtk_tree_path_new ();
1404 children = model->roots;
1407 gtk_tree_path_down (path);
1409 if (parent_node && parent_node->has_dummy)
1412 children = children->next;
1413 gtk_tree_path_next (path);
1416 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1418 const GtkFilePath *file_path = tmp_list->data;
1421 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1424 if (children->is_visible)
1425 gtk_tree_path_next (path);
1427 children = children->next;
1431 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1433 gtk_tree_model_row_changed (tree_model, path, &iter);
1437 /* Shouldn't happen */
1441 gtk_tree_path_free (path);
1442 g_slist_free (sorted_paths);
1446 do_files_removed (GtkFileSystemModel *model,
1447 FileModelNode *parent_node,
1450 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1451 FileModelNode *children;
1452 FileModelNode *prev = NULL;
1455 GSList *sorted_paths;
1457 FileModelNode *tmp_child;
1460 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1464 iter.user_data = parent_node;
1465 path = gtk_tree_model_get_path (tree_model, &iter);
1466 children = parent_node->children;
1470 path = gtk_tree_path_new ();
1471 children = model->roots;
1474 /* Count the number of currently visible children, so that
1475 * can catch when we need to insert a dummy node.
1478 for (tmp_child = children; tmp_child; tmp_child = tmp_child->next)
1480 if (tmp_child->is_visible)
1484 gtk_tree_path_down (path);
1486 if (parent_node && parent_node->has_dummy)
1489 children = children->next;
1490 gtk_tree_path_next (path);
1493 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1495 const GtkFilePath *file_path = tmp_list->data;
1498 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1501 if (children->is_visible)
1502 gtk_tree_path_next (path);
1504 children = children->next;
1508 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1510 FileModelNode *next = children->next;
1512 if (children->is_visible)
1517 FileModelNode *dummy = file_model_node_new (model, NULL);
1518 dummy->is_visible = TRUE;
1519 dummy->parent = parent_node;
1520 dummy->is_dummy = TRUE;
1522 parent_node->children = dummy;
1523 parent_node->has_dummy = TRUE;
1525 iter.user_data = dummy;
1526 gtk_tree_model_row_inserted (tree_model, path, &iter);
1527 gtk_tree_path_next (path);
1534 else if (parent_node)
1535 parent_node->children = next;
1537 model->roots = next;
1539 if (parent_node && children->ref_count)
1540 file_model_node_child_unref (parent_node);
1542 if (children->is_visible)
1543 gtk_tree_model_row_deleted (tree_model, path);
1545 file_model_node_free (children);
1551 /* Shouldn't happen */
1555 gtk_tree_path_free (path);
1556 g_slist_free (sorted_paths);
1560 roots_changed_callback (GtkFileSystem *file_system,
1561 GtkFileSystemModel *model)
1563 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1566 FileModelNode *children;
1567 FileModelNode *prev = NULL;
1570 new_roots = gtk_file_system_list_roots (file_system);
1571 new_roots = gtk_file_paths_sort (new_roots);
1573 children = model->roots;
1574 tmp_list = new_roots;
1575 path = gtk_tree_path_new ();
1576 gtk_tree_path_down (path);
1578 while (children || tmp_list)
1580 FileModelNode *next = NULL;
1583 if (tmp_list && children)
1584 cmp = gtk_file_path_compare (children->path, tmp_list->data);
1592 next = children->next;
1595 prev->next = children->next;
1597 model->roots = children->next;
1599 if (children->is_visible)
1600 gtk_tree_model_row_deleted (tree_model, path);
1602 file_model_node_free (children);
1608 next = children->next;
1610 if (children->is_visible)
1611 gtk_tree_path_next (path);
1616 FileModelNode *node = file_model_node_new (model, tmp_list->data);
1617 node->is_visible = file_model_node_is_visible (model, node);
1618 node->next = children;
1624 model->roots = node;
1626 if (node->is_visible)
1628 iter.user_data = node;
1629 gtk_tree_model_row_inserted (tree_model, path, &iter);
1631 if (gtk_file_system_model_iter_has_child (tree_model, &iter))
1632 gtk_tree_model_row_has_child_toggled (tree_model, path, &iter);
1634 gtk_tree_path_next (path);
1647 gtk_file_path_free (tmp_list->data);
1648 tmp_list = tmp_list->next;
1652 g_slist_free (new_roots);
1653 gtk_tree_path_free (path);
1657 deleted_callback (GtkFileFolder *folder,
1658 FileModelNode *node)
1663 files_added_callback (GtkFileFolder *folder,
1665 FileModelNode *node)
1667 do_files_added (node->model, node, paths);
1671 files_changed_callback (GtkFileFolder *folder,
1673 FileModelNode *node)
1675 do_files_changed (node->model, node, paths);
1679 files_removed_callback (GtkFileFolder *folder,
1681 FileModelNode *node)
1683 do_files_removed (node->model, node, paths);
1687 root_deleted_callback (GtkFileFolder *folder,
1688 GtkFileSystemModel *model)
1693 root_files_added_callback (GtkFileFolder *folder,
1695 GtkFileSystemModel *model)
1697 do_files_added (model, NULL, paths);
1701 root_files_changed_callback (GtkFileFolder *folder,
1703 GtkFileSystemModel *model)
1705 do_files_changed (model, NULL, paths);
1709 root_files_removed_callback (GtkFileFolder *folder,
1711 GtkFileSystemModel *model)
1713 do_files_removed (model, NULL, paths);