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 "gtkmarshalers.h"
28 #include "gtktreednd.h"
29 #include "gtktreemodel.h"
31 typedef struct _GtkFileSystemModelClass GtkFileSystemModelClass;
32 typedef struct _FileModelNode FileModelNode;
34 #define GTK_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
35 #define GTK_IS_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_MODEL))
36 #define GTK_FILE_SYSTEM_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
38 struct _GtkFileSystemModelClass
40 GObjectClass parent_class;
44 void (*finished_loading) (GtkFileSystemModel *model);
47 struct _GtkFileSystemModel
49 GObject parent_instance;
51 GtkFileSystem *file_system;
52 GtkFileInfoType types;
54 GtkFileFolder *root_folder;
55 GtkFilePath *root_path;
57 GtkFileSystemModelFilter filter_func;
61 GSource *idle_clear_source;
62 GSource *idle_finished_loading_source;
66 guint show_hidden : 1;
67 guint show_folders : 1;
69 guint folders_only : 1;
70 guint has_editable : 1;
79 GtkFileFolder *folder;
81 FileModelNode *children;
82 FileModelNode *parent;
83 GtkFileSystemModel *model;
86 guint n_referenced_children;
97 static void gtk_file_system_model_class_init (GtkFileSystemModelClass *class);
98 static void gtk_file_system_model_iface_init (GtkTreeModelIface *iface);
99 static void gtk_file_system_model_init (GtkFileSystemModel *model);
100 static void gtk_file_system_model_finalize (GObject *object);
102 static void drag_source_iface_init (GtkTreeDragSourceIface *iface);
104 static GtkTreeModelFlags gtk_file_system_model_get_flags (GtkTreeModel *tree_model);
105 static gint gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model);
106 static GType gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
108 static gboolean gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
111 static GtkTreePath * gtk_file_system_model_get_path (GtkTreeModel *tree_model,
113 static void gtk_file_system_model_get_value (GtkTreeModel *tree_model,
117 static gboolean gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
119 static gboolean gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
121 GtkTreeIter *parent);
122 static gboolean gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
124 static gint gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
126 static gboolean gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
130 static gboolean gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
133 static void gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
135 static void gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
138 static gboolean drag_source_row_draggable (GtkTreeDragSource *drag_source,
140 static gboolean drag_source_drag_data_get (GtkTreeDragSource *drag_source,
142 GtkSelectionData *selection_data);
144 static FileModelNode *file_model_node_new (GtkFileSystemModel *model,
145 const GtkFilePath *path);
146 static void file_model_node_free (FileModelNode *node);
147 static void file_model_node_ref (FileModelNode *node);
148 static void file_model_node_unref (GtkFileSystemModel *model,
149 FileModelNode *node);
151 static void file_model_node_idle_clear (FileModelNode *node);
152 static void file_model_node_idle_clear_cancel (FileModelNode *node);
153 static void file_model_node_child_unref (FileModelNode *parent);
155 static const GtkFileInfo *file_model_node_get_info (GtkFileSystemModel *model,
156 FileModelNode *node);
157 static gboolean file_model_node_is_visible (GtkFileSystemModel *model,
158 FileModelNode *node);
159 static void file_model_node_clear (GtkFileSystemModel *model,
160 FileModelNode *node);
161 static FileModelNode * file_model_node_get_children (GtkFileSystemModel *model,
162 FileModelNode *node);
165 static void roots_changed_callback (GtkFileSystem *file_system,
166 GtkFileSystemModel *model);
169 static void deleted_callback (GtkFileFolder *folder,
170 FileModelNode *node);
171 static void files_added_callback (GtkFileFolder *folder,
173 FileModelNode *node);
174 static void files_changed_callback (GtkFileFolder *folder,
176 FileModelNode *node);
177 static void files_removed_callback (GtkFileFolder *folder,
179 FileModelNode *node);
181 static void root_deleted_callback (GtkFileFolder *folder,
182 GtkFileSystemModel *model);
183 static void root_files_added_callback (GtkFileFolder *folder,
185 GtkFileSystemModel *model);
186 static void root_files_changed_callback (GtkFileFolder *folder,
188 GtkFileSystemModel *model);
189 static void root_files_removed_callback (GtkFileFolder *folder,
191 GtkFileSystemModel *model);
193 static GObjectClass *parent_class = NULL;
201 static guint file_system_model_signals[LAST_SIGNAL] = { 0 };
206 _gtk_file_system_model_get_type (void)
208 static GType file_system_model_type = 0;
210 if (!file_system_model_type)
212 static const GTypeInfo file_system_model_info =
214 sizeof (GtkFileSystemModelClass),
215 NULL, /* base_init */
216 NULL, /* base_finalize */
217 (GClassInitFunc) gtk_file_system_model_class_init,
218 NULL, /* class_finalize */
219 NULL, /* class_data */
220 sizeof (GtkFileSystemModel),
222 (GInstanceInitFunc) gtk_file_system_model_init,
225 static const GInterfaceInfo file_system_info =
227 (GInterfaceInitFunc) gtk_file_system_model_iface_init, /* interface_init */
228 NULL, /* interface_finalize */
229 NULL /* interface_data */
232 static const GInterfaceInfo drag_source_info =
234 (GInterfaceInitFunc) drag_source_iface_init, /* interface_init */
235 NULL, /* interface_finalize */
236 NULL /* interface_data */
239 file_system_model_type = g_type_register_static (G_TYPE_OBJECT,
240 "GtkFileSystemModel",
241 &file_system_model_info, 0);
242 g_type_add_interface_static (file_system_model_type,
245 g_type_add_interface_static (file_system_model_type,
246 GTK_TYPE_TREE_DRAG_SOURCE,
250 return file_system_model_type;
254 gtk_file_system_model_class_init (GtkFileSystemModelClass *class)
256 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
258 parent_class = g_type_class_peek_parent (class);
260 gobject_class->finalize = gtk_file_system_model_finalize;
262 file_system_model_signals[FINISHED_LOADING] =
263 g_signal_new ("finished-loading",
264 G_OBJECT_CLASS_TYPE (gobject_class),
266 G_STRUCT_OFFSET (GtkFileSystemModelClass, finished_loading),
268 _gtk_marshal_VOID__VOID,
273 gtk_file_system_model_iface_init (GtkTreeModelIface *iface)
275 iface->get_flags = gtk_file_system_model_get_flags;
276 iface->get_n_columns = gtk_file_system_model_get_n_columns;
277 iface->get_column_type = gtk_file_system_model_get_column_type;
278 iface->get_iter = gtk_file_system_model_get_iter;
279 iface->get_path = gtk_file_system_model_get_path;
280 iface->get_value = gtk_file_system_model_get_value;
281 iface->iter_next = gtk_file_system_model_iter_next;
282 iface->iter_children = gtk_file_system_model_iter_children;
283 iface->iter_has_child = gtk_file_system_model_iter_has_child;
284 iface->iter_n_children = gtk_file_system_model_iter_n_children;
285 iface->iter_nth_child = gtk_file_system_model_iter_nth_child;
286 iface->iter_parent = gtk_file_system_model_iter_parent;
287 iface->ref_node = gtk_file_system_model_ref_node;
288 iface->unref_node = gtk_file_system_model_unref_node;
292 gtk_file_system_model_init (GtkFileSystemModel *model)
294 model->show_files = TRUE;
295 model->show_folders = TRUE;
296 model->show_hidden = FALSE;
300 gtk_file_system_model_finalize (GObject *object)
302 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
303 FileModelNode *children, *next;
305 if (model->root_folder)
306 g_object_unref (model->root_folder);
308 if (model->root_path)
309 gtk_file_path_free (model->root_path);
311 if (model->file_system)
312 g_object_unref (model->file_system);
314 if (model->idle_finished_loading_source)
315 g_source_destroy (model->idle_finished_loading_source);
317 children = model->roots;
320 next = children->next;
321 file_model_node_free (children);
325 G_OBJECT_CLASS (parent_class)->finalize (object);
329 drag_source_iface_init (GtkTreeDragSourceIface *iface)
331 iface->row_draggable = drag_source_row_draggable;
332 iface->drag_data_get = drag_source_drag_data_get;
333 iface->drag_data_delete = NULL;
337 * ******************** GtkTreeModel methods ********************
340 static GtkTreeModelFlags
341 gtk_file_system_model_get_flags (GtkTreeModel *tree_model)
343 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
344 GtkTreeModelFlags flags = GTK_TREE_MODEL_ITERS_PERSIST;
346 if (model->max_depth == 0)
347 flags |= GTK_TREE_MODEL_LIST_ONLY;
353 gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model)
355 return GTK_FILE_SYSTEM_MODEL_N_COLUMNS;
359 gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
364 case GTK_FILE_SYSTEM_MODEL_INFO:
365 return GTK_TYPE_FILE_INFO;
366 case GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME:
367 return G_TYPE_STRING;
369 g_assert_not_reached ();
375 gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
383 indices = gtk_tree_path_get_indices (path);
384 depth = gtk_tree_path_get_depth (path);
386 g_return_val_if_fail (depth > 0, FALSE);
388 if (!gtk_tree_model_iter_nth_child (tree_model, iter, NULL, indices[0]))
391 for (i = 1; i < depth; i++)
394 if (!gtk_tree_model_iter_nth_child (tree_model, iter, &parent, indices[i]))
402 gtk_file_system_model_get_path (GtkTreeModel *tree_model,
405 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
406 FileModelNode *node = iter->user_data;
408 GtkTreePath *result = gtk_tree_path_new ();
412 FileModelNode *parent = node->parent;
413 FileModelNode *children;
417 children = parent->children;
419 children = model->roots;
421 while (children != node)
423 if (children->is_visible)
425 children = children->next;
428 gtk_tree_path_prepend_index (result, n);
437 gtk_file_system_model_get_value (GtkTreeModel *tree_model,
442 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
443 FileModelNode *node = iter->user_data;
444 const GtkFileInfo *info;
448 case GTK_FILE_SYSTEM_MODEL_INFO:
449 if (model->has_editable && node == model->roots)
452 info = file_model_node_get_info (model, node);
454 g_value_init (value, GTK_TYPE_FILE_INFO);
455 g_value_set_boxed (value, info);
457 case GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME:
459 g_value_init (value, G_TYPE_STRING);
461 if (model->has_editable && node == model->roots)
462 g_value_set_string (value, "");
465 const GtkFileInfo *info = file_model_node_get_info (model, node);
467 g_value_set_string (value, gtk_file_info_get_display_name (info));
472 g_assert_not_reached ();
477 gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
480 FileModelNode *node = iter->user_data;
483 while (node && !node->is_visible)
486 iter->user_data = node;
492 gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
496 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
497 FileModelNode *children;
501 FileModelNode *parent_node = parent->user_data;
502 children = file_model_node_get_children (model, parent_node);
506 children = model->roots;
509 while (children && !children->is_visible)
510 children = children->next;
512 iter->user_data = children;
514 return children != NULL;
518 gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
521 FileModelNode *node = iter->user_data;
522 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
524 if (node->depth == model->max_depth)
528 const GtkFileInfo *info = file_model_node_get_info (model, node);
529 return gtk_file_info_get_is_folder (info);
534 gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
537 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
538 FileModelNode *children;
543 FileModelNode *node = iter->user_data;
544 children = file_model_node_get_children (model, node);
548 children = model->roots;
553 if (children->is_visible)
555 children = children->next;
562 gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
567 GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
568 FileModelNode *children;
572 FileModelNode *parent_node = parent->user_data;
573 children = file_model_node_get_children (model, parent_node);
577 children = model->roots;
580 while (children && !children->is_visible)
581 children = children->next;
583 while (n && children)
586 children = children->next;
587 while (children && !children->is_visible)
588 children = children->next;
591 iter->user_data = children;
593 return children != NULL;
597 gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
601 FileModelNode *node = child->user_data;
604 iter->user_data = node;
610 gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
613 file_model_node_ref (iter->user_data);
617 gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
620 file_model_node_unref (GTK_FILE_SYSTEM_MODEL (tree_model),
625 drag_source_row_draggable (GtkTreeDragSource *drag_source,
628 GtkFileSystemModel *model;
632 model = GTK_FILE_SYSTEM_MODEL (drag_source);
634 if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
637 if (!model->has_editable)
640 node = iter.user_data;
641 return (node != model->roots);
645 drag_source_drag_data_get (GtkTreeDragSource *drag_source,
647 GtkSelectionData *selection_data)
649 GtkFileSystemModel *model;
651 const GtkFilePath *file_path;
655 model = GTK_FILE_SYSTEM_MODEL (drag_source);
657 if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
660 file_path = _gtk_file_system_model_get_path (model, &iter);
661 g_assert (file_path != NULL);
663 uri = gtk_file_system_path_to_uri (model->file_system, file_path);
664 uris = g_strconcat (uri, "\r\n", NULL);
666 gtk_selection_data_set (selection_data,
667 gdk_atom_intern ("text/uri-list", FALSE),
678 /* Callback used when the root folder finished loading */
680 root_folder_finished_loading_cb (GtkFileFolder *folder,
681 GtkFileSystemModel *model)
683 g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0);
686 /* Emits the "finished-loading" signal as an idle handler; see the comment in
687 * _gtk_file_system_model_new()
690 idle_finished_loading_cb (GtkFileSystemModel *model)
692 g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0);
694 g_source_destroy (model->idle_finished_loading_source);
695 model->idle_finished_loading_source = NULL;
700 /* Queues an idle handler to emit the "finished-loading" signal */
702 queue_finished_loading (GtkFileSystemModel *model)
704 model->idle_finished_loading_source = g_idle_source_new ();
705 g_source_set_closure (model->idle_finished_loading_source,
706 g_cclosure_new_object (G_CALLBACK (idle_finished_loading_cb),
708 g_source_attach (model->idle_finished_loading_source, NULL);
712 * _gtk_file_system_model_new:
713 * @file_system: an object implementing #GtkFileSystem
714 * @root_path: the path of root of the file system to display
715 * @max_depth: the maximum depth from the children of @root_path
716 * or the roots of the file system to display in
717 * the file selector). A depth of 0 displays
718 * only the immediate children of @root_path,
719 * or the roots of the filesystem. -1 for no
721 * @types: a bitmask indicating the types of information
722 * that is desired about the files. This will
723 * determine what information is returned by
724 * _gtk_file_system_model_get_info().
726 * Creates a new #GtkFileSystemModel object. The #GtkFileSystemModel
727 * object wraps a #GtkFileSystem interface as a #GtkTreeModel.
728 * Using the @root_path and @max_depth parameters, the tree model
729 * can be restricted to a subportion of the entire file system.
731 * Return value: the newly created #GtkFileSystemModel object.
734 _gtk_file_system_model_new (GtkFileSystem *file_system,
735 const GtkFilePath *root_path,
737 GtkFileInfoType types)
739 GtkFileSystemModel *model;
740 GSList *roots = NULL;
743 g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
744 g_return_val_if_fail (root_path != NULL, NULL);
746 model = g_object_new (GTK_TYPE_FILE_SYSTEM_MODEL, NULL);
747 model->file_system = g_object_ref (file_system);
749 model->max_depth = G_MAXUSHORT;
751 model->max_depth = MIN (max_depth, G_MAXUSHORT);
752 model->types = types | GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN;
758 model->root_path = gtk_file_path_copy (root_path);
759 model->root_folder = gtk_file_system_get_folder (file_system, root_path,
761 NULL); /* NULL-GError */
763 if (model->root_folder)
765 if (gtk_file_folder_list_children (model->root_folder,
767 NULL)) /* NULL-GError */
770 if (gtk_file_folder_is_finished_loading (model->root_folder))
771 queue_finished_loading (model); /* done in an idle because we are being created */
773 g_signal_connect (model->root_folder, "finished-loading",
774 G_CALLBACK (root_folder_finished_loading_cb), model);
776 g_signal_connect_object (model->root_folder, "deleted",
777 G_CALLBACK (root_deleted_callback), model, 0);
778 g_signal_connect_object (model->root_folder, "files-added",
779 G_CALLBACK (root_files_added_callback), model, 0);
780 g_signal_connect_object (model->root_folder, "files-changed",
781 G_CALLBACK (root_files_changed_callback), model, 0);
782 g_signal_connect_object (model->root_folder, "files-removed",
783 G_CALLBACK (root_files_removed_callback), model, 0);
789 roots = gtk_file_system_list_roots (file_system);
790 g_signal_connect_object (file_system, "roots-changed",
791 G_CALLBACK (roots_changed_callback), model, 0);
795 roots = gtk_file_paths_sort (roots);
797 for (tmp_list = roots; tmp_list; tmp_list = tmp_list->next)
799 FileModelNode *node = file_model_node_new (model, tmp_list->data);
800 gtk_file_path_free (tmp_list->data);
801 node->is_visible = file_model_node_is_visible (model, node);
802 node->next = model->roots;
806 g_slist_free (roots);
808 model->roots = (FileModelNode *)g_slist_reverse ((GSList *)model->roots);
814 model_refilter_recurse (GtkFileSystemModel *model,
815 FileModelNode *parent,
818 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
820 FileModelNode *nodes;
821 gboolean has_children = FALSE;
823 if (parent && !parent->loaded)
827 nodes = parent->children;
829 nodes = model->roots;
833 FileModelNode *next = nodes->next;
836 gtk_tree_path_append_index (path, i);
838 is_visible = file_model_node_is_visible (model, nodes);
840 if (!is_visible && nodes->is_visible)
842 file_model_node_clear (model, nodes);
843 gtk_tree_model_row_deleted (tree_model, path);
845 nodes->is_visible = FALSE;
847 else if (is_visible && !nodes->is_visible)
851 iter.user_data = nodes;
852 nodes->is_visible = TRUE;
853 gtk_tree_model_row_inserted (tree_model, path, &iter);
856 model_refilter_recurse (model, nodes, path);
864 gtk_tree_path_up (path);
869 if (parent && !has_children)
871 /* Fixme - need to insert dummy node here */
876 model_refilter_all (GtkFileSystemModel *model)
880 path = gtk_tree_path_new ();
881 model_refilter_recurse (model, NULL, path);
882 gtk_tree_path_free (path);
886 * _gtk_file_system_model_set_show_hidden:
887 * @model: a #GtkFileSystemModel
888 * @show_hidden: whether hidden files should be displayed
890 * Sets whether hidden files should be included in the #GtkTreeModel
894 _gtk_file_system_model_set_show_hidden (GtkFileSystemModel *model,
895 gboolean show_hidden)
897 show_hidden = show_hidden != FALSE;
899 if (show_hidden != model->show_hidden)
901 model->show_hidden = show_hidden;
902 model_refilter_all (model);
907 * _gtk_file_system_model_set_show_folders:
908 * @model: a #GtkFileSystemModel
909 * @show_folders: whether folders should be displayed
911 * Sets whether folders should be included in the #GtkTreeModel for
915 _gtk_file_system_model_set_show_folders (GtkFileSystemModel *model,
916 gboolean show_folders)
918 show_folders = show_folders != FALSE;
920 if (show_folders != model->show_folders)
922 model->show_folders = show_folders;
923 model_refilter_all (model);
928 * _gtk_file_system_model_set_show_files:
929 * @model: a #GtkFileSystemModel
930 * @show_files: whether files (as opposed to folders) should
933 * Sets whether files (as opposed to folders) should be included
934 * in the #GtkTreeModel for display.
937 _gtk_file_system_model_set_show_files (GtkFileSystemModel *model,
940 show_files = show_files != FALSE;
942 if (show_files != model->show_files)
944 model->show_files = show_files;
945 model_refilter_all (model);
950 * _gtk_file_system_model_get_info:
951 * @model: a #GtkFileSystemModel
952 * @iter: a #GtkTreeIter pointing to a row of @model
954 * Gets the #GtkFileInfo structure for a particular row
955 * of @model. The information included in this structure
956 * is determined by the @types parameter to
957 * _gtk_file_system_model_new().
959 * Return value: a #GtkFileInfo structure. This structure
960 * is owned by @model and must not be modified or freed.
961 * If you want to save the information for later use,
962 * you must make a copy, since the structure may be
963 * freed on later changes to the file system. If you have
964 * called _gtk_file_system_model_add_editable() and the @iter
965 * corresponds to the row that this function returned, the
966 * return value will be NULL.
969 _gtk_file_system_model_get_info (GtkFileSystemModel *model,
974 node = iter->user_data;
975 if (model->has_editable && node == model->roots)
978 return file_model_node_get_info (model, node);
982 * _gtk_file_system_model_get_path:
983 * @model: a #GtkFileSystemModel
984 * @iter: a #GtkTreeIter pointing to a row of @model
986 * Gets the path for a particular row in @model.
988 * Return value: the path. This string is owned by @model and
989 * or freed. If you want to save the path for later use,
990 * you must make a copy, since the string may be freed
991 * on later changes to the file system.
994 _gtk_file_system_model_get_path (GtkFileSystemModel *model,
997 FileModelNode *node = iter->user_data;
999 if (model->has_editable && node == model->roots)
1003 return node->parent->path;
1009 unref_node_and_parents (GtkFileSystemModel *model,
1010 FileModelNode *node)
1012 file_model_node_unref (model, node);
1014 file_model_node_unref (model, node->parent);
1017 static FileModelNode *
1018 find_child_node (GtkFileSystemModel *model,
1019 FileModelNode *parent_node,
1020 const GtkFilePath *path)
1022 FileModelNode *children;
1025 children = file_model_node_get_children (model, parent_node);
1027 children = model->roots;
1031 if (children->is_visible &&
1033 gtk_file_path_compare (children->path, path) == 0)
1036 children = children->next;
1043 static FileModelNode *
1044 find_and_ref_path (GtkFileSystemModel *model,
1045 const GtkFilePath *path,
1048 GtkFilePath *parent_path;
1049 FileModelNode *parent_node;
1050 FileModelNode *child_node;
1051 GtkFileFolder *folder;
1053 if (gtk_file_path_compare (path, model->root_path) == 0
1054 || !gtk_file_system_get_parent (model->file_system, path, &parent_path, NULL))
1059 parent_node = find_and_ref_path (model, parent_path, cleanups);
1060 gtk_file_path_free (parent_path);
1065 child_node = find_child_node (model, parent_node, path);
1068 file_model_node_ref (child_node);
1072 folder = gtk_file_system_get_folder (model->file_system,
1075 NULL); /* NULL-GError */
1078 *cleanups = g_slist_prepend (*cleanups, folder);
1080 child_node = find_child_node (model, parent_node, path);
1083 file_model_node_ref (child_node);
1089 unref_node_and_parents (model, parent_node);
1095 * _gtk_file_system_model_set_filter:
1096 * @mode: a #GtkFileSystemModel
1097 * @filter: function to be called for each file
1098 * @user_data: data to pass to @filter
1100 * Sets a callback called for each file/directory to see whether
1101 * it should be included in model. If this function was made
1102 * public, we'd want to include a GDestroyNotify as well.
1105 _gtk_file_system_model_set_filter (GtkFileSystemModel *model,
1106 GtkFileSystemModelFilter filter,
1109 g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1111 model->filter_func = filter;
1112 model->filter_data = user_data;
1114 model_refilter_all (model);
1118 * _gtk_file_system_model_path_do:
1119 * @model: a #GtkFileSystemModel
1120 * @path: a path pointing to a file in the filesystem
1122 * @func: Function to call with the path and iter corresponding
1124 * @user_data: data to pass to @func
1126 * Locates @path within @model, referencing
1127 * (gtk_tree_model_ref_node ()) all parent nodes,
1128 * calls @func passing in the path and iter for @path,
1129 * then unrefs all the parent nodes.
1131 * The reason for doing this operation as a callback
1132 * is so that if the operation performed with the the
1133 * path and iter results in referencing the the node
1134 * and/or parent nodes, we don't load all the information
1137 * This function is particularly useful for expanding
1138 * a #GtkTreeView to a particular point in the file system.
1140 * Return value: %TRUE if the path was successfully
1141 * found in @model and @func was called.
1144 _gtk_file_system_model_path_do (GtkFileSystemModel *model,
1145 const GtkFilePath *path,
1146 GtkFileSystemModelPathFunc func,
1149 GSList *cleanups = NULL;
1150 FileModelNode *node = find_and_ref_path (model, path, &cleanups);
1157 iter.user_data = node;
1158 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
1160 (*func) (model, path, &iter, user_data);
1162 gtk_tree_path_free (path);
1163 unref_node_and_parents (model, node);
1166 g_slist_foreach (cleanups, (GFunc)g_object_unref, NULL);
1167 g_slist_free (cleanups);
1169 return node != NULL;
1173 * _gtk_file_system_model_add_editable:
1174 * @model: a #GtkFileSystemModel
1175 * @iter: Location to return the iter corresponding to the editable row
1177 * Adds an "empty" row at the beginning of the model. This does not refer to
1178 * any file, but is a temporary placeholder for a file name that the user will
1179 * type when a corresponding cell is made editable. When your code is done
1180 * using this temporary row, call _gtk_file_system_model_remove_editable().
1183 _gtk_file_system_model_add_editable (GtkFileSystemModel *model, GtkTreeIter *iter)
1185 FileModelNode *node;
1188 g_return_if_fail (!model->has_editable);
1190 model->has_editable = TRUE;
1192 node = file_model_node_new (model, NULL);
1193 node->is_visible = TRUE;
1195 node->next = model->roots;
1196 model->roots = node;
1198 file_model_node_ref (node);
1200 path = gtk_tree_path_new ();
1201 gtk_tree_path_append_index (path, 0);
1202 iter->user_data = node;
1204 gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, iter);
1206 gtk_tree_path_free (path);
1210 * _gtk_file_system_model_remove_editable:
1211 * @model: a #GtkFileSystemModel
1213 * Removes the "empty" row at the beginning of the model that was
1214 * created with _gtk_file_system_model_add_editable(). You should call
1215 * this function when your code is finished editing this temporary row.
1218 _gtk_file_system_model_remove_editable (GtkFileSystemModel *model)
1222 g_return_if_fail (model->has_editable);
1224 model->has_editable = FALSE;
1225 file_model_node_unref (model, model->roots);
1227 model->roots = model->roots->next;
1229 path = gtk_tree_path_new ();
1230 gtk_tree_path_append_index (path, 0);
1232 gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
1234 gtk_tree_path_free (path);
1237 static FileModelNode *
1238 file_model_node_new (GtkFileSystemModel *model,
1239 const GtkFilePath *path)
1241 FileModelNode *node = g_new0 (FileModelNode, 1);
1243 node->model = model;
1244 node->path = path ? gtk_file_path_copy (path) : NULL;
1250 file_model_node_free (FileModelNode *node)
1252 file_model_node_clear (node->model, node);
1255 gtk_file_path_free (node->path);
1258 gtk_file_info_free (node->info);
1263 static const GtkFileInfo *
1264 file_model_node_get_info (GtkFileSystemModel *model,
1265 FileModelNode *node)
1271 node->info = gtk_file_info_new ();
1272 gtk_file_info_set_display_name (node->info, _("(Empty)"));
1274 else if (node->parent || model->root_folder)
1276 node->info = gtk_file_folder_get_info (node->parent ? node->parent->folder : model->root_folder,
1278 NULL); /* NULL-GError */
1283 node->info = gtk_file_system_get_root_info (model->file_system,
1286 NULL); /* NULL-GError */
1295 file_model_node_is_visible (GtkFileSystemModel *model,
1296 FileModelNode *node)
1298 if (model->show_folders != model->show_files ||
1299 !model->show_hidden ||
1302 const GtkFileInfo *info = file_model_node_get_info (model, node);
1306 /* File probably disappeared underneath us or resides in a
1307 directory where we have only partial access rights. */
1311 if (model->show_folders != model->show_files &&
1312 model->show_folders != gtk_file_info_get_is_folder (info))
1315 if (!model->show_hidden && gtk_file_info_get_is_hidden (info))
1318 if (model->filter_func &&
1319 !model->filter_func (model, node->path, info, model->filter_data))
1327 file_model_node_clear (GtkFileSystemModel *model,
1328 FileModelNode *node)
1330 FileModelNode *children;
1332 file_model_node_idle_clear_cancel (node);
1334 children = node->children;
1335 node->children = NULL;
1336 node->loaded = FALSE;
1340 FileModelNode *next = children->next;
1342 file_model_node_free (children);
1349 /* Unreffing node->folder may cause roots_changed,
1350 * so we need to be careful about ordering.
1352 GtkFileFolder *folder = node->folder;
1353 node->folder = NULL;
1355 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (deleted_callback), node);
1356 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_added_callback), node);
1357 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_changed_callback), node);
1358 g_signal_handlers_disconnect_by_func (folder, G_CALLBACK (files_removed_callback), node);
1360 g_object_unref (folder);
1365 file_model_node_ref (FileModelNode *node)
1368 if (node->ref_count == 1 && node->parent)
1369 node->parent->n_referenced_children++;
1373 idle_clear_callback (GtkFileSystemModel *model)
1375 while (model->idle_clears)
1377 FileModelNode *node = model->idle_clears->data;
1378 model->idle_clears = g_slist_delete_link (model->idle_clears, model->idle_clears);
1380 node->idle_clear = FALSE;
1381 file_model_node_clear (node->model, node);
1388 file_model_node_idle_clear (FileModelNode *node)
1390 if (!node->idle_clear)
1392 GtkFileSystemModel *model = node->model;
1394 node->idle_clear = TRUE;
1395 if (!model->idle_clears)
1397 model->idle_clear_source = g_idle_source_new ();
1398 g_source_set_priority (model->idle_clear_source, G_PRIORITY_HIGH);
1399 g_source_set_closure (model->idle_clear_source,
1400 g_cclosure_new_object (G_CALLBACK (idle_clear_callback),
1402 g_source_attach (model->idle_clear_source, NULL);
1405 model->idle_clears = g_slist_prepend (model->idle_clears, node);
1406 node->idle_clear = TRUE;
1411 file_model_node_idle_clear_cancel (FileModelNode *node)
1413 if (node->idle_clear)
1415 GtkFileSystemModel *model = node->model;
1417 model->idle_clears = g_slist_remove (model->idle_clears, node);
1418 if (!model->idle_clears)
1420 g_source_destroy (model->idle_clear_source);
1421 model->idle_clear_source = NULL;
1424 node->idle_clear = FALSE;
1429 file_model_node_unref (GtkFileSystemModel *model,
1430 FileModelNode *node)
1433 if (node->ref_count == 0)
1435 file_model_node_clear (model, node);
1437 file_model_node_child_unref (node->parent);
1442 file_model_node_child_unref (FileModelNode *parent)
1444 parent->n_referenced_children--;
1445 if (parent->n_referenced_children == 0)
1446 file_model_node_idle_clear (parent);
1449 static FileModelNode *
1450 file_model_node_get_children (GtkFileSystemModel *model,
1451 FileModelNode *node)
1453 if (node->ref_count == 0)
1458 const GtkFileInfo *info = file_model_node_get_info (model, node);
1459 gboolean has_children = FALSE;
1460 gboolean is_folder = node->depth < model->max_depth && gtk_file_info_get_is_folder (info);
1462 file_model_node_idle_clear_cancel (node);
1465 node->folder = gtk_file_system_get_folder (model->file_system,
1468 NULL); /* NULL-GError */
1472 GSList *child_paths, *tmp_list;
1474 if (gtk_file_folder_list_children (node->folder, &child_paths, NULL)) /* NULL-GError */
1476 child_paths = gtk_file_paths_sort (child_paths);
1478 for (tmp_list = child_paths; tmp_list; tmp_list = tmp_list->next)
1480 FileModelNode *child_node = file_model_node_new (model, tmp_list->data);
1481 gtk_file_path_free (tmp_list->data);
1482 child_node->next = node->children;
1483 child_node->parent = node;
1484 child_node->depth = node->depth + 1;
1485 child_node->is_visible = file_model_node_is_visible (model, child_node);
1486 if (child_node->is_visible)
1487 has_children = TRUE;
1488 node->children = child_node;
1490 g_slist_free (child_paths);
1493 node->children = (FileModelNode *)g_slist_reverse ((GSList *)node->children);
1495 g_signal_connect (node->folder, "deleted",
1496 G_CALLBACK (deleted_callback), node);
1497 g_signal_connect (node->folder, "files-added",
1498 G_CALLBACK (files_added_callback), node);
1499 g_signal_connect (node->folder, "files-changed",
1500 G_CALLBACK (files_changed_callback), node);
1501 g_signal_connect (node->folder, "files-removed",
1502 G_CALLBACK (files_removed_callback), node);
1504 g_object_set_data (G_OBJECT (node->folder), "model-node", node);
1507 if (is_folder && !has_children)
1509 /* The hard case ... we claimed this folder had children, but actually
1510 * it didn't. We have to add a dummy child, possibly to remove later.
1512 FileModelNode *child_node = file_model_node_new (model, NULL);
1513 child_node->is_visible = TRUE;
1514 child_node->parent = node;
1515 child_node->is_dummy = TRUE;
1517 node->children = child_node;
1518 node->has_dummy = TRUE;
1521 node->loaded = TRUE;
1524 return node->children;
1528 do_files_added (GtkFileSystemModel *model,
1529 FileModelNode *parent_node,
1532 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1533 FileModelNode *children;
1534 FileModelNode *prev = NULL;
1537 GSList *sorted_paths;
1540 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1544 iter.user_data = parent_node;
1545 path = gtk_tree_model_get_path (tree_model, &iter);
1546 children = parent_node->children;
1550 path = gtk_tree_path_new ();
1551 children = model->roots;
1554 gtk_tree_path_down (path);
1556 if (parent_node && parent_node->has_dummy)
1559 children = children->next;
1560 gtk_tree_path_next (path);
1563 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1565 const GtkFilePath *file_path = tmp_list->data;
1568 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1571 if (children->is_visible)
1572 gtk_tree_path_next (path);
1574 children = children->next;
1578 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1580 /* Shouldn't happen */
1586 new = file_model_node_new (model, file_path);
1589 new->next = children;
1592 else if (parent_node)
1593 parent_node->children = new;
1601 new->parent = parent_node;
1602 new->depth = parent_node->depth + 1;
1605 new->is_visible = file_model_node_is_visible (model, new);
1607 if (new->is_visible)
1609 iter.user_data = new;
1610 path = gtk_tree_model_get_path (tree_model, &iter);
1611 gtk_tree_model_row_inserted (tree_model, path, &iter);
1613 if (gtk_file_system_model_iter_has_child (tree_model, &iter))
1614 gtk_tree_model_row_has_child_toggled (tree_model, path, &iter);
1616 if (parent_node && parent_node->has_dummy)
1618 FileModelNode *dummy = parent_node->children;
1619 GtkTreePath *dummy_path;
1621 parent_node->children = parent_node->children->next;
1622 parent_node->has_dummy = FALSE;
1624 dummy_path = gtk_tree_path_copy (path);
1625 gtk_tree_path_up (dummy_path);
1626 gtk_tree_path_down (dummy_path);
1628 gtk_tree_model_row_deleted (tree_model, dummy_path);
1629 gtk_tree_path_free (dummy_path);
1631 if (dummy->ref_count)
1632 file_model_node_child_unref (parent_node);
1633 file_model_node_free (dummy);
1636 gtk_tree_path_next (path);
1641 gtk_tree_path_free (path);
1642 g_slist_free (sorted_paths);
1646 do_files_changed (GtkFileSystemModel *model,
1647 FileModelNode *parent_node,
1650 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1651 FileModelNode *children;
1652 FileModelNode *prev = NULL;
1655 GSList *sorted_paths;
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 gtk_tree_path_down (path);
1674 if (parent_node && parent_node->has_dummy)
1677 children = children->next;
1678 gtk_tree_path_next (path);
1681 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1683 const GtkFilePath *file_path = tmp_list->data;
1686 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1689 if (children->is_visible)
1690 gtk_tree_path_next (path);
1692 children = children->next;
1696 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1698 gtk_tree_model_row_changed (tree_model, path, &iter);
1702 /* Shouldn't happen */
1706 gtk_tree_path_free (path);
1707 g_slist_free (sorted_paths);
1711 do_files_removed (GtkFileSystemModel *model,
1712 FileModelNode *parent_node,
1715 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1716 FileModelNode *children;
1717 FileModelNode *prev = NULL;
1720 GSList *sorted_paths;
1722 FileModelNode *tmp_child;
1725 sorted_paths = gtk_file_paths_sort (g_slist_copy (paths));
1729 iter.user_data = parent_node;
1730 path = gtk_tree_model_get_path (tree_model, &iter);
1731 children = parent_node->children;
1735 path = gtk_tree_path_new ();
1736 children = model->roots;
1739 /* Count the number of currently visible children, so that
1740 * can catch when we need to insert a dummy node.
1743 for (tmp_child = children; tmp_child; tmp_child = tmp_child->next)
1745 if (tmp_child->is_visible)
1749 gtk_tree_path_down (path);
1751 if (parent_node && parent_node->has_dummy)
1754 children = children->next;
1755 gtk_tree_path_next (path);
1758 for (tmp_list = sorted_paths; tmp_list; tmp_list = tmp_list->next)
1760 const GtkFilePath *file_path = tmp_list->data;
1763 (!children->path || gtk_file_path_compare (children->path, file_path) < 0))
1766 if (children->is_visible)
1767 gtk_tree_path_next (path);
1769 children = children->next;
1773 children->path && gtk_file_path_compare (children->path, file_path) == 0)
1775 FileModelNode *next = children->next;
1777 if (children->is_visible)
1782 FileModelNode *dummy = file_model_node_new (model, NULL);
1783 dummy->is_visible = TRUE;
1784 dummy->parent = parent_node;
1785 dummy->is_dummy = TRUE;
1787 parent_node->children = dummy;
1788 parent_node->has_dummy = TRUE;
1790 iter.user_data = dummy;
1791 gtk_tree_model_row_inserted (tree_model, path, &iter);
1792 gtk_tree_path_next (path);
1799 else if (parent_node)
1800 parent_node->children = next;
1802 model->roots = next;
1804 if (parent_node && children->ref_count)
1805 file_model_node_child_unref (parent_node);
1807 if (children->is_visible)
1808 gtk_tree_model_row_deleted (tree_model, path);
1810 file_model_node_free (children);
1816 /* Shouldn't happen */
1820 gtk_tree_path_free (path);
1821 g_slist_free (sorted_paths);
1826 roots_changed_callback (GtkFileSystem *file_system,
1827 GtkFileSystemModel *model)
1829 GtkTreeModel *tree_model = GTK_TREE_MODEL (model);
1832 FileModelNode *children;
1833 FileModelNode *prev = NULL;
1836 new_roots = gtk_file_system_list_roots (file_system);
1837 new_roots = gtk_file_paths_sort (new_roots);
1839 children = model->roots;
1840 tmp_list = new_roots;
1841 path = gtk_tree_path_new ();
1842 gtk_tree_path_down (path);
1844 while (children || tmp_list)
1846 FileModelNode *next = NULL;
1849 if (tmp_list && children)
1850 cmp = gtk_file_path_compare (children->path, tmp_list->data);
1858 next = children->next;
1861 prev->next = children->next;
1863 model->roots = children->next;
1865 if (children->is_visible)
1866 gtk_tree_model_row_deleted (tree_model, path);
1868 file_model_node_free (children);
1874 next = children->next;
1876 if (children->is_visible)
1877 gtk_tree_path_next (path);
1882 FileModelNode *node = file_model_node_new (model, tmp_list->data);
1883 node->is_visible = file_model_node_is_visible (model, node);
1884 node->next = children;
1890 model->roots = node;
1892 if (node->is_visible)
1894 iter.user_data = node;
1895 gtk_tree_model_row_inserted (tree_model, path, &iter);
1897 if (gtk_file_system_model_iter_has_child (tree_model, &iter))
1898 gtk_tree_model_row_has_child_toggled (tree_model, path, &iter);
1900 gtk_tree_path_next (path);
1913 gtk_file_path_free (tmp_list->data);
1914 tmp_list = tmp_list->next;
1918 g_slist_free (new_roots);
1919 gtk_tree_path_free (path);
1924 deleted_callback (GtkFileFolder *folder,
1925 FileModelNode *node)
1930 files_added_callback (GtkFileFolder *folder,
1932 FileModelNode *node)
1934 do_files_added (node->model, node, paths);
1938 files_changed_callback (GtkFileFolder *folder,
1940 FileModelNode *node)
1942 do_files_changed (node->model, node, paths);
1946 files_removed_callback (GtkFileFolder *folder,
1948 FileModelNode *node)
1950 do_files_removed (node->model, node, paths);
1954 root_deleted_callback (GtkFileFolder *folder,
1955 GtkFileSystemModel *model)
1960 root_files_added_callback (GtkFileFolder *folder,
1962 GtkFileSystemModel *model)
1964 do_files_added (model, NULL, paths);
1968 root_files_changed_callback (GtkFileFolder *folder,
1970 GtkFileSystemModel *model)
1972 do_files_changed (model, NULL, paths);
1976 root_files_removed_callback (GtkFileFolder *folder,
1978 GtkFileSystemModel *model)
1980 do_files_removed (model, NULL, paths);