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 == 0)
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 nodes->is_visible = TRUE;
786 gtk_tree_model_row_inserted (tree_model, path, &iter);
789 model_refilter_recurse (model, nodes, path);
797 gtk_tree_path_up (path);
802 if (parent && !has_children)
804 /* Fixme - need to insert dummy node here */
809 model_refilter_all (GtkFileSystemModel *model)
813 path = gtk_tree_path_new ();
814 model_refilter_recurse (model, NULL, path);
815 gtk_tree_path_free (path);
819 * _gtk_file_system_model_set_show_hidden:
820 * @model: a #GtkFileSystemModel
821 * @show_hidden: whether hidden files should be displayed
823 * Sets whether hidden files should be included in the #GtkTreeModel
827 _gtk_file_system_model_set_show_hidden (GtkFileSystemModel *model,
828 gboolean show_hidden)
830 show_hidden = show_hidden != FALSE;
832 if (show_hidden != model->show_hidden)
834 model->show_hidden = show_hidden;
835 model_refilter_all (model);
840 * _gtk_file_system_model_set_show_folders:
841 * @model: a #GtkFileSystemModel
842 * @show_folders: whether folders should be displayed
844 * Sets whether folders should be included in the #GtkTreeModel for
848 _gtk_file_system_model_set_show_folders (GtkFileSystemModel *model,
849 gboolean show_folders)
851 show_folders = show_folders != FALSE;
853 if (show_folders != model->show_folders)
855 model->show_folders = show_folders;
856 model_refilter_all (model);
861 * _gtk_file_system_model_set_show_files:
862 * @model: a #GtkFileSystemModel
863 * @show_files: whether files (as opposed to folders) should
866 * Sets whether files (as opposed to folders) should be included
867 * in the #GtkTreeModel for display.
870 _gtk_file_system_model_set_show_files (GtkFileSystemModel *model,
873 show_files = show_files != FALSE;
875 if (show_files != model->show_files)
877 model->show_files = show_files;
878 model_refilter_all (model);
883 * _gtk_file_system_model_get_info:
884 * @model: a #GtkFileSystemModel
885 * @iter: a #GtkTreeIter pointing to a row of @model
887 * Gets the #GtkFileInfo structure for a particular row
888 * of @model. The information included in this structure
889 * is determined by the @types parameter to
890 * _gtk_file_system_model_new().
892 * Return value: a #GtkFileInfo structure. This structure
893 * is owned by @model and must not be modified or freed.
894 * If you want to save the information for later use,
895 * you must make a copy, since the structure may be
896 * freed on later changes to the file system. If you have
897 * called _gtk_file_system_model_add_editable() and the @iter
898 * corresponds to the row that this function returned, the
899 * return value will be NULL.
902 _gtk_file_system_model_get_info (GtkFileSystemModel *model,
907 node = iter->user_data;
908 if (model->has_editable && node == model->roots)
911 return file_model_node_get_info (model, node);
915 * _gtk_file_system_model_get_path:
916 * @model: a #GtkFileSystemModel
917 * @iter: a #GtkTreeIter pointing to a row of @model
919 * Gets the path for a particular row in @model.
921 * Return value: the path. This string is owned by @model and
922 * or freed. If you want to save the path for later use,
923 * you must make a copy, since the string may be freed
924 * on later changes to the file system.
927 _gtk_file_system_model_get_path (GtkFileSystemModel *model,
930 FileModelNode *node = iter->user_data;
932 if (model->has_editable && node == model->roots)
936 return node->parent->path;
942 unref_node_and_parents (GtkFileSystemModel *model,
945 file_model_node_unref (model, node);
947 file_model_node_unref (model, node->parent);
950 static FileModelNode *
951 find_child_node (GtkFileSystemModel *model,
952 FileModelNode *parent_node,
953 const GtkFilePath *path)
955 FileModelNode *children;
958 children = file_model_node_get_children (model, parent_node);
960 children = model->roots;
964 if (children->is_visible &&
966 gtk_file_path_compare (children->path, path) == 0)
969 children = children->next;
976 static FileModelNode *
977 find_and_ref_path (GtkFileSystemModel *model,
978 const GtkFilePath *path,
981 GtkFilePath *parent_path;
982 FileModelNode *parent_node;
983 FileModelNode *child_node;
984 GtkFileFolder *folder;
986 if (gtk_file_path_compare (path, model->root_path) == 0
987 || !gtk_file_system_get_parent (model->file_system, path, &parent_path, NULL))
992 parent_node = find_and_ref_path (model, parent_path, cleanups);
993 gtk_file_path_free (parent_path);
998 child_node = find_child_node (model, parent_node, path);
1001 file_model_node_ref (child_node);
1005 folder = gtk_file_system_get_folder (model->file_system,
1008 NULL); /* NULL-GError */
1011 *cleanups = g_slist_prepend (*cleanups, folder);
1013 child_node = find_child_node (model, parent_node, path);
1016 file_model_node_ref (child_node);
1022 unref_node_and_parents (model, parent_node);
1028 * _gtk_file_system_model_set_filter:
1029 * @mode: a #GtkFileSystemModel
1030 * @filter: function to be called for each file
1031 * @user_data: data to pass to @filter
1033 * Sets a callback called for each file/directory to see whether
1034 * it should be included in model. If this function was made
1035 * public, we'd want to include a GDestroyNotify as well.
1038 _gtk_file_system_model_set_filter (GtkFileSystemModel *model,
1039 GtkFileSystemModelFilter filter,
1042 g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1044 model->filter_func = filter;
1045 model->filter_data = user_data;
1047 model_refilter_all (model);
1051 * _gtk_file_system_model_path_do:
1052 * @model: a #GtkFileSystemModel
1053 * @path: a path pointing to a file in the filesystem
1055 * @func: Function to call with the path and iter corresponding
1057 * @user_data: data to pass to @func
1059 * Locates @path within @model, referencing
1060 * (gtk_tree_model_ref_node ()) all parent nodes,
1061 * calls @func passing in the path and iter for @path,
1062 * then unrefs all the parent nodes.
1064 * The reason for doing this operation as a callback
1065 * is so that if the operation performed with the the
1066 * path and iter results in referencing the the node
1067 * and/or parent nodes, we don't load all the information
1070 * This function is particularly useful for expanding
1071 * a #GtkTreeView to a particular point in the file system.
1073 * Return value: %TRUE if the path was successfully
1074 * found in @model and @func was called.
1077 _gtk_file_system_model_path_do (GtkFileSystemModel *model,
1078 const GtkFilePath *path,
1079 GtkFileSystemModelPathFunc func,
1082 GSList *cleanups = NULL;
1083 FileModelNode *node = find_and_ref_path (model, path, &cleanups);
1090 iter.user_data = node;
1091 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
1093 (*func) (model, path, &iter, user_data);
1095 gtk_tree_path_free (path);
1096 unref_node_and_parents (model, node);
1099 g_slist_foreach (cleanups, (GFunc)g_object_unref, NULL);
1100 g_slist_free (cleanups);
1102 return node != NULL;
1106 * _gtk_file_system_model_add_editable:
1107 * @model: a #GtkFileSystemModel
1108 * @iter: Location to return the iter corresponding to the editable row
1110 * Adds an "empty" row at the beginning of the model. This does not refer to
1111 * any file, but is a temporary placeholder for a file name that the user will
1112 * type when a corresponding cell is made editable. When your code is done
1113 * using this temporary row, call _gtk_file_system_model_remove_editable().
1116 _gtk_file_system_model_add_editable (GtkFileSystemModel *model, GtkTreeIter *iter)
1118 FileModelNode *node;
1121 g_return_if_fail (!model->has_editable);
1123 model->has_editable = TRUE;
1125 node = file_model_node_new (model, NULL);
1126 node->is_visible = TRUE;
1128 node->next = model->roots;
1129 model->roots = node;
1131 file_model_node_ref (node);
1133 path = gtk_tree_path_new ();
1134 gtk_tree_path_append_index (path, 0);
1135 iter->user_data = node;
1137 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, iter);
1139 gtk_tree_path_free (path);
1143 * _gtk_file_system_model_remove_editable:
1144 * @model: a #GtkFileSystemModel
1146 * Removes the "empty" row at the beginning of the model that was
1147 * created with _gtk_file_system_model_add_editable(). You should call
1148 * this function when your code is finished editing this temporary row.
1151 _gtk_file_system_model_remove_editable (GtkFileSystemModel *model)
1155 g_return_if_fail (model->has_editable);
1157 model->has_editable = FALSE;
1158 file_model_node_unref (model, model->roots);
1160 model->roots = model->roots->next;
1162 path = gtk_tree_path_new ();
1163 gtk_tree_path_append_index (path, 0);
1165 gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
1167 gtk_tree_path_free (path);
1170 static FileModelNode *
1171 file_model_node_new (GtkFileSystemModel *model,
1172 const GtkFilePath *path)
1174 FileModelNode *node = g_new0 (FileModelNode, 1);
1176 node->model = model;
1177 node->path = path ? gtk_file_path_copy (path) : NULL;
1183 file_model_node_free (FileModelNode *node)
1185 file_model_node_clear (node->model, node);
1188 gtk_file_path_free (node->path);
1191 gtk_file_info_free (node->info);
1196 static const GtkFileInfo *
1197 file_model_node_get_info (GtkFileSystemModel *model,
1198 FileModelNode *node)
1204 node->info = gtk_file_info_new ();
1205 gtk_file_info_set_display_name (node->info, _("(Empty)"));
1207 else if (node->parent || model->root_folder)
1209 node->info = gtk_file_folder_get_info (node->parent ? node->parent->folder : model->root_folder,
1211 NULL); /* NULL-GError */
1216 node->info = gtk_file_system_get_root_info (model->file_system,
1219 NULL); /* NULL-GError */
1228 file_model_node_is_visible (GtkFileSystemModel *model,
1229 FileModelNode *node)
1231 if (model->show_folders != model->show_files ||
1232 !model->show_hidden ||
1235 const GtkFileInfo *info = file_model_node_get_info (model, node);
1239 /* File probably disappeared underneath us or resides in a
1240 directory where we have only partial access rights. */
1244 if (model->show_folders != model->show_files &&
1245 model->show_folders != gtk_file_info_get_is_folder (info))
1248 if (!model->show_hidden && gtk_file_info_get_is_hidden (info))
1251 if (model->filter_func &&
1252 !model->filter_func (model, node->path, info, model->filter_data))
1260 file_model_node_clear (GtkFileSystemModel *model,
1261 FileModelNode *node)
1263 FileModelNode *children;
1265 file_model_node_idle_clear_cancel (node);
1267 children = node->children;
1268 node->children = NULL;
1269 node->loaded = FALSE;
1273 FileModelNode *next = children->next;
1275 file_model_node_free (children);
1282 /* Unreffing node->folder may cause roots_changed,
1283 * so we need to be careful about ordering.
1285 GtkFileFolder *folder = node->folder;
1286 node->folder = NULL;
1288 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (deleted_callback), node);
1289 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_added_callback), node);
1290 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_changed_callback), node);
1291 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_removed_callback), node);
1293 g_object_unref (folder);
1298 file_model_node_ref (FileModelNode *node)
1301 if (node->ref_count == 1 && node->parent)
1302 node->parent->n_referenced_children++;
1306 idle_clear_callback (GtkFileSystemModel *model)
1308 while (model->idle_clears)
1310 FileModelNode *node = model->idle_clears->data;
1311 model->idle_clears = g_slist_delete_link (model->idle_clears, model->idle_clears);
1313 node->idle_clear = FALSE;
1314 file_model_node_clear (node->model, node);
1321 file_model_node_idle_clear (FileModelNode *node)
1323 if (!node->idle_clear)
1325 GtkFileSystemModel *model = node->model;
1327 node->idle_clear = TRUE;
1328 if (!model->idle_clears)
1330 model->idle_clear_source = g_idle_source_new ();
1331 g_source_set_priority (model->idle_clear_source, G_PRIORITY_HIGH);
1332 g_source_set_closure (model->idle_clear_source,
1333 g_cclosure_new_object (G_CALLBACK (idle_clear_callback),
1335 g_source_attach (model->idle_clear_source, NULL);
1338 model->idle_clears = g_slist_prepend (model->idle_clears, node);
1339 node->idle_clear = TRUE;
1344 file_model_node_idle_clear_cancel (FileModelNode *node)
1346 if (node->idle_clear)
1348 GtkFileSystemModel *model = node->model;
1350 model->idle_clears = g_slist_remove (model->idle_clears, node);
1351 if (!model->idle_clears)
1353 g_source_destroy (model->idle_clear_source);
1354 model->idle_clear_source = NULL;
1357 node->idle_clear = FALSE;
1362 file_model_node_unref (GtkFileSystemModel *model,
1363 FileModelNode *node)
1366 if (node->ref_count == 0)
1368 file_model_node_clear (model, node);
1370 file_model_node_child_unref (node->parent);
1375 file_model_node_child_unref (FileModelNode *parent)
1377 parent->n_referenced_children--;
1378 if (parent->n_referenced_children == 0)
1379 file_model_node_idle_clear (parent);
1382 static FileModelNode *
1383 file_model_node_get_children (GtkFileSystemModel *model,
1384 FileModelNode *node)
1386 if (node->ref_count == 0)
1391 const GtkFileInfo *info = file_model_node_get_info (model, node);
1392 gboolean has_children = FALSE;
1393 gboolean is_folder = node->depth < model->max_depth && gtk_file_info_get_is_folder (info);
1395 file_model_node_idle_clear_cancel (node);
1398 node->folder = gtk_file_system_get_folder (model->file_system,
1401 NULL); /* NULL-GError */
1405 GSList *child_paths, *tmp_list;
1407 if (gtk_file_folder_list_children (node->folder, &child_paths, NULL)) /* NULL-GError */
1409 child_paths = gtk_file_paths_sort (child_paths);
1411 for (tmp_list = child_paths; tmp_list; tmp_list = tmp_list->next)
1413 FileModelNode *child_node = file_model_node_new (model, tmp_list->data);
1414 gtk_file_path_free (tmp_list->data);
1415 child_node->next = node->children;
1416 child_node->parent = node;
1417 child_node->depth = node->depth + 1;
1418 child_node->is_visible = file_model_node_is_visible (model, child_node);
1419 if (child_node->is_visible)
1420 has_children = TRUE;
1421 node->children = child_node;
1423 g_slist_free (child_paths);
1426 node->children = (FileModelNode *)g_slist_reverse ((GSList *)node->children);
1428 g_signal_connect (node->folder, "deleted",
1429 G_CALLBACK (deleted_callback), node);
1430 g_signal_connect (node->folder, "files-added",
1431 G_CALLBACK (files_added_callback), node);
1432 g_signal_connect (node->folder, "files-changed",
1433 G_CALLBACK (files_changed_callback), node);
1434 g_signal_connect (node->folder, "files-removed",
1435 G_CALLBACK (files_removed_callback), node);
1437 g_object_set_data (G_OBJECT (node->folder), "model-node", node);
1440 if (is_folder && !has_children)
1442 /* The hard case ... we claimed this folder had children, but actually
1443 * it didn't. We have to add a dummy child, possibly to remove later.
1445 FileModelNode *child_node = file_model_node_new (model, NULL);
1446 child_node->is_visible = TRUE;
1447 child_node->parent = node;
1448 child_node->is_dummy = TRUE;
1450 node->children = child_node;
1451 node->has_dummy = TRUE;
1454 node->loaded = TRUE;
1457 return node->children;
1461 do_files_added (GtkFileSystemModel *model,
1462 FileModelNode *parent_node,
1465 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1466 FileModelNode *children;
1467 FileModelNode *prev = NULL;
1470 GSList *sorted_paths;
1473 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1477 iter.user_data = parent_node;
1478 path = gtk_tree_model_get_path (tree_model, &iter);
1479 children = parent_node->children;
1483 path = gtk_tree_path_new ();
1484 children = model->roots;
1487 gtk_tree_path_down (path);
1489 if (parent_node && parent_node->has_dummy)
1492 children = children->next;
1493 gtk_tree_path_next (path);
1496 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1498 const GtkFilePath *file_path = tmp_list->data;
1501 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1504 if (children->is_visible)
1505 gtk_tree_path_next (path);
1507 children = children->next;
1511 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1513 /* Shouldn't happen */
1519 new = file_model_node_new (model, file_path);
1522 new->next = children;
1525 else if (parent_node)
1526 parent_node->children = new;
1534 new->parent = parent_node;
1535 new->depth = parent_node->depth + 1;
1538 new->is_visible = file_model_node_is_visible (model, new);
1540 if (new->is_visible)
1542 iter.user_data = new;
1543 path = gtk_tree_model_get_path (tree_model, &iter);
1544 gtk_tree_model_row_inserted (tree_model, path, &iter);
1546 if (gtk_file_system_model_iter_has_child (tree_model, &iter))
1547 gtk_tree_model_row_has_child_toggled (tree_model, path, &iter);
1549 if (parent_node && parent_node->has_dummy)
1551 FileModelNode *dummy = parent_node->children;
1552 GtkTreePath *dummy_path;
1554 parent_node->children = parent_node->children->next;
1555 parent_node->has_dummy = FALSE;
1557 dummy_path = gtk_tree_path_copy (path);
1558 gtk_tree_path_up (dummy_path);
1559 gtk_tree_path_down (dummy_path);
1561 gtk_tree_model_row_deleted (tree_model, dummy_path);
1562 gtk_tree_path_free (dummy_path);
1564 if (dummy->ref_count)
1565 file_model_node_child_unref (parent_node);
1566 file_model_node_free (dummy);
1569 gtk_tree_path_next (path);
1574 gtk_tree_path_free (path);
1575 g_slist_free (sorted_paths);
1579 do_files_changed (GtkFileSystemModel *model,
1580 FileModelNode *parent_node,
1583 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1584 FileModelNode *children;
1585 FileModelNode *prev = NULL;
1588 GSList *sorted_paths;
1591 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1595 iter.user_data = parent_node;
1596 path = gtk_tree_model_get_path (tree_model, &iter);
1597 children = parent_node->children;
1601 path = gtk_tree_path_new ();
1602 children = model->roots;
1605 gtk_tree_path_down (path);
1607 if (parent_node && parent_node->has_dummy)
1610 children = children->next;
1611 gtk_tree_path_next (path);
1614 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1616 const GtkFilePath *file_path = tmp_list->data;
1619 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1622 if (children->is_visible)
1623 gtk_tree_path_next (path);
1625 children = children->next;
1629 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1631 gtk_tree_model_row_changed (tree_model, path, &iter);
1635 /* Shouldn't happen */
1639 gtk_tree_path_free (path);
1640 g_slist_free (sorted_paths);
1644 do_files_removed (GtkFileSystemModel *model,
1645 FileModelNode *parent_node,
1648 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1649 FileModelNode *children;
1650 FileModelNode *prev = NULL;
1653 GSList *sorted_paths;
1655 FileModelNode *tmp_child;
1658 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1662 iter.user_data = parent_node;
1663 path = gtk_tree_model_get_path (tree_model, &iter);
1664 children = parent_node->children;
1668 path = gtk_tree_path_new ();
1669 children = model->roots;
1672 /* Count the number of currently visible children, so that
1673 * can catch when we need to insert a dummy node.
1676 for (tmp_child = children; tmp_child; tmp_child = tmp_child->next)
1678 if (tmp_child->is_visible)
1682 gtk_tree_path_down (path);
1684 if (parent_node && parent_node->has_dummy)
1687 children = children->next;
1688 gtk_tree_path_next (path);
1691 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1693 const GtkFilePath *file_path = tmp_list->data;
1696 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1699 if (children->is_visible)
1700 gtk_tree_path_next (path);
1702 children = children->next;
1706 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1708 FileModelNode *next = children->next;
1710 if (children->is_visible)
1715 FileModelNode *dummy = file_model_node_new (model, NULL);
1716 dummy->is_visible = TRUE;
1717 dummy->parent = parent_node;
1718 dummy->is_dummy = TRUE;
1720 parent_node->children = dummy;
1721 parent_node->has_dummy = TRUE;
1723 iter.user_data = dummy;
1724 gtk_tree_model_row_inserted (tree_model, path, &iter);
1725 gtk_tree_path_next (path);
1732 else if (parent_node)
1733 parent_node->children = next;
1735 model->roots = next;
1737 if (parent_node && children->ref_count)
1738 file_model_node_child_unref (parent_node);
1740 if (children->is_visible)
1741 gtk_tree_model_row_deleted (tree_model, path);
1743 file_model_node_free (children);
1749 /* Shouldn't happen */
1753 gtk_tree_path_free (path);
1754 g_slist_free (sorted_paths);
1759 roots_changed_callback (GtkFileSystem *file_system,
1760 GtkFileSystemModel *model)
1762 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1765 FileModelNode *children;
1766 FileModelNode *prev = NULL;
1769 new_roots = gtk_file_system_list_roots (file_system);
1770 new_roots = gtk_file_paths_sort (new_roots);
1772 children = model->roots;
1773 tmp_list = new_roots;
1774 path = gtk_tree_path_new ();
1775 gtk_tree_path_down (path);
1777 while (children || tmp_list)
1779 FileModelNode *next = NULL;
1782 if (tmp_list && children)
1783 cmp = gtk_file_path_compare (children->path, tmp_list->data);
1791 next = children->next;
1794 prev->next = children->next;
1796 model->roots = children->next;
1798 if (children->is_visible)
1799 gtk_tree_model_row_deleted (tree_model, path);
1801 file_model_node_free (children);
1807 next = children->next;
1809 if (children->is_visible)
1810 gtk_tree_path_next (path);
1815 FileModelNode *node = file_model_node_new (model, tmp_list->data);
1816 node->is_visible = file_model_node_is_visible (model, node);
1817 node->next = children;
1823 model->roots = node;
1825 if (node->is_visible)
1827 iter.user_data = node;
1828 gtk_tree_model_row_inserted (tree_model, path, &iter);
1830 if (gtk_file_system_model_iter_has_child (tree_model, &iter))
1831 gtk_tree_model_row_has_child_toggled (tree_model, path, &iter);
1833 gtk_tree_path_next (path);
1846 gtk_file_path_free (tmp_list->data);
1847 tmp_list = tmp_list->next;
1851 g_slist_free (new_roots);
1852 gtk_tree_path_free (path);
1857 deleted_callback (GtkFileFolder *folder,
1858 FileModelNode *node)
1863 files_added_callback (GtkFileFolder *folder,
1865 FileModelNode *node)
1867 do_files_added (node->model, node, paths);
1871 files_changed_callback (GtkFileFolder *folder,
1873 FileModelNode *node)
1875 do_files_changed (node->model, node, paths);
1879 files_removed_callback (GtkFileFolder *folder,
1881 FileModelNode *node)
1883 do_files_removed (node->model, node, paths);
1887 root_deleted_callback (GtkFileFolder *folder,
1888 GtkFileSystemModel *model)
1893 root_files_added_callback (GtkFileFolder *folder,
1895 GtkFileSystemModel *model)
1897 do_files_added (model, NULL, paths);
1901 root_files_changed_callback (GtkFileFolder *folder,
1903 GtkFileSystemModel *model)
1905 do_files_changed (model, NULL, paths);
1909 root_files_removed_callback (GtkFileFolder *folder,
1911 GtkFileSystemModel *model)
1913 do_files_removed (model, NULL, paths);