]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilesystemmodel.c
480a6f5a9d6d37d6f23776b3d0fdb478220098e2
[~andy/gtk] / gtk / gtkfilesystemmodel.c
1 /* GTK - The GIMP Toolkit
2  * gtkfilesystemmodel.c: GtkTreeModel wrapping a GtkFileSystem
3  * Copyright (C) 2003, Red Hat, Inc.
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20
21 #include "gtkfilesystemmodel.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "gtkfilesystem.h"
27 #include "gtkintl.h"
28 #include "gtkmarshalers.h"
29 #include "gtktreedatalist.h"
30 #include "gtktreednd.h"
31 #include "gtktreemodel.h"
32
33 /*** Structure: how GtkFileSystemModel works
34  *
35  * This is a custom GtkTreeModel used to hold a collection of files for GtkFileChooser.  There are two use cases:
36  *
37  *   1. The model populates itself from a folder, using the GIO file enumerator API.  This happens if you use
38  *      _gtk_file_system_model_new_for_directory().  This is the normal usage for showing the contents of a folder.
39  *
40  *   2. The caller populates the model by hand, with files not necessarily in the same folder.  This happens
41  *      if you use _gtk_file_system_model_new() and then _gtk_file_system_model_add_and_query_file().  This is
42  *      the special kind of usage for "search" and "recent-files", where the file chooser gives the model the
43  *      files to be displayed.
44  *
45  * Internal data structure
46  * -----------------------
47  *
48  * Each file is kept in a FileModelNode structure.  Each FileModelNode holds a GFile* and other data.  All the
49  * node structures have the same size, determined at runtime, depending on the number of columns that were passed
50  * to _gtk_file_system_model_new() or _gtk_file_system_model_new_for_directory() (that is, the size of a node is
51  * not sizeof (FileModelNode), but rather model->node_size).  The last field in the FileModelNode structure,
52  * node->values[], is an array of GValue, used to hold the data for those columns.
53  *
54  * The model stores an array of FileModelNode structures in model->files.  This is a GArray where each element is
55  * model->node_size bytes in size (the model computes that node size when initializing itself).  There are
56  * convenience macros, get_node() and node_index(), to access that array based on an array index or a pointer to
57  * a node inside the array.
58  *
59  * The model accesses files through two of its fields:
60  *
61  *   model->files - GArray of FileModelNode structures.
62  *
63  *   model->file_lookup - hash table that maps a GFile* to an index inside the model->files array.
64  *
65  * The model->file_lookup hash table is populated lazily.  It is both accessed and populated with the
66  * node_get_for_file() function.  The invariant is that the files in model->files[n] for n < g_hash_table_size
67  * (model->file_lookup) are already added to the hash table. The hash table will get cleared when we re-sort the
68  * files, as the array will be in a different order and the indexes need to be rebuilt.
69  *
70  * Each FileModelNode has a node->visible field, which indicates whether the node is visible in the GtkTreeView.
71  * A node may be invisible if, for example, it corresponds to a hidden file and the file chooser is not showing
72  * hidden files.  Also, a file filter may be explicitly set onto the model, for example, to only show files that
73  * match "*.jpg".  In this case, node->filtered_out says whether the node failed the filter.  The ultimate
74  * decision on whether a node is visible or not in the treeview is distilled into the node->visible field.
75  * The reason for having a separate node->filtered_out field is so that the file chooser can query whether
76  * a (filtered-out) folder should be made sensitive in the GUI.
77  *
78  * Visible rows vs. possibly-invisible nodes
79  * -----------------------------------------
80  *
81  * Since not all nodes in the model->files array may be visible, we need a way to map visible row indexes from
82  * the treeview to array indexes in our array of files.  And thus we introduce a bit of terminology:
83  *
84  *   index - An index in the model->files array.  All variables/fields that represent indexes are either called
85  *   "index" or "i_*", or simply "i" for things like loop counters.
86  *
87  *   row - An index in the GtkTreeView, i.e. the index of a row within the outward-facing API of the
88  *   GtkFileSystemModel.  However, note that our rows are 1-based, not 0-based, for the reason explained in the
89  *   following paragraph.  Variables/fields that represent visible rows are called "row", or "r_*", or simply
90  *   "r".
91  *
92  * Each FileModelNode has a node->row field which is the number of visible rows in the treeview, *before and
93  * including* that node.  This means that node->row is 1-based, instead of 0-based --- this makes some code
94  * simpler, believe it or not :)  This also means that when the calling GtkTreeView gives us a GtkTreePath, we
95  * turn the 0-based treepath into a 1-based row for our purposes.  If a node is not visible, it will have the
96  * same row number as its closest preceding visible node.
97  *
98  * We try to compute the node->row fields lazily.  A node is said to be "valid" if its node->row is accurate.
99  * For this, the model keeps a model->n_nodes_valid field which is the count of valid nodes starting from the
100  * beginning of the model->files array.  When a node changes its information, or when a node gets deleted, that
101  * node and the following ones get invalidated by simply setting model->n_nodes_valid to the array index of the
102  * node.  If the model happens to need a node's row number and that node is in the model->files array after
103  * model->n_nodes_valid, then the nodes get re-validated up to the sought node.  See node_validate_rows() for
104  * this logic.
105  *
106  * You never access a node->row directly.  Instead, call node_get_tree_row().  That function will validate the nodes
107  * up to the sought one if the node is not valid yet, and it will return a proper 0-based row.
108  *
109  * Sorting
110  * -------
111  *
112  * The model implements the GtkTreeSortable interface.  To avoid re-sorting
113  * every time a node gets added (which would lead to O(n^2) performance during
114  * the initial population of the model), the model can freeze itself (with
115  * freeze_updates()) during the intial population process.  When the model is
116  * frozen, sorting will not happen.  The model will sort itself when the freeze
117  * count goes back to zero, via corresponding calls to thaw_updates().
118  */
119
120 /*** DEFINES ***/
121
122 /* priority used for all async callbacks in the main loop
123  * This should be higher than redraw priorities so multiple callbacks
124  * firing can be handled without intermediate redraws */
125 #define IO_PRIORITY G_PRIORITY_DEFAULT
126
127 /* random number that everyone else seems to use, too */
128 #define FILES_PER_QUERY 100
129
130 typedef struct _FileModelNode           FileModelNode;
131 typedef struct _GtkFileSystemModelClass GtkFileSystemModelClass;
132
133 struct _FileModelNode
134 {
135   GFile *               file;           /* file represented by this node or NULL for editable */
136   GFileInfo *           info;           /* info for this file or NULL if unknown */
137
138   guint                 row;            /* if valid (see model->n_valid_indexes), visible nodes before and including
139                                          * this one - see the "Structure" comment above.
140                                          */
141
142   guint                 visible :1;     /* if the file is currently visible */
143   guint                 filtered_out :1;/* if the file is currently filtered out (i.e. it didn't pass the filters) */
144   guint                 frozen_add :1;  /* true if the model was frozen and the entry has not been added yet */
145
146   GValue                values[1];      /* actually n_columns values */
147 };
148
149 struct _GtkFileSystemModel
150 {
151   GObject               parent_instance;
152
153   GFile *               dir;            /* directory that's displayed */
154   guint                 dir_thaw_source;/* GSource id for unfreezing the model */
155   char *                attributes;     /* attributes the file info must contain, or NULL for all attributes */
156   GFileMonitor *        dir_monitor;    /* directory that is monitored, or NULL if monitoring was not supported */
157
158   GCancellable *        cancellable;    /* cancellable in use for all operations - cancelled on dispose */
159   GArray *              files;          /* array of FileModelNode containing all our files */
160   gsize                 node_size;      /* Size of a FileModelNode structure once its ->values field has n_columns */
161   guint                 n_nodes_valid;  /* count of valid nodes (i.e. those whose node->row is accurate) */
162   GHashTable *          file_lookup;    /* mapping of GFile => array index in model->files
163                                          * This hash table doesn't always have the same number of entries as the files array;
164                                          * it can get cleared completely when we resort.
165                                          * The hash table gets re-populated in node_get_for_file() if this mismatch is
166                                          * detected.
167                                          */
168
169   guint                 n_columns;      /* number of columns */
170   GType *               column_types;   /* types of each column */
171   GtkFileSystemModelGetValue get_func;  /* function to call to fill in values in columns */
172   gpointer              get_data;       /* data to pass to get_func */
173
174   GtkFileFilter *       filter;         /* filter to use for deciding which nodes are visible */
175
176   int                   sort_column_id; /* current sorting column */
177   GtkSortType           sort_order;     /* current sorting order */
178   GList *               sort_list;      /* list of sorting functions */
179   GtkTreeIterCompareFunc default_sort_func; /* default sort function */
180   gpointer              default_sort_data; /* data to pass to default sort func */
181   GDestroyNotify        default_sort_destroy; /* function to call to destroy default_sort_data */
182
183   guint                 frozen;         /* number of times we're frozen */
184
185   gboolean              filter_on_thaw :1;/* set when filtering needs to happen upon thawing */
186   gboolean              sort_on_thaw :1;/* set when sorting needs to happen upon thawing */
187
188   guint                 show_hidden :1; /* whether to show hidden files */
189   guint                 show_folders :1;/* whether to show folders */
190   guint                 show_files :1;  /* whether to show files */
191   guint                 filter_folders :1;/* whether filter applies to folders */
192 };
193
194 #define GTK_FILE_SYSTEM_MODEL_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
195 #define GTK_IS_FILE_SYSTEM_MODEL_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_MODEL))
196 #define GTK_FILE_SYSTEM_MODEL_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
197
198 struct _GtkFileSystemModelClass
199 {
200   GObjectClass parent_class;
201
202   /* Signals */
203
204   void (*finished_loading) (GtkFileSystemModel *model, GError *error);
205 };
206
207 static guint node_get_for_file (GtkFileSystemModel *model,
208                                 GFile              *file);
209
210 static void add_file (GtkFileSystemModel *model,
211                       GFile              *file,
212                       GFileInfo          *info);
213 static void remove_file (GtkFileSystemModel *model,
214                          GFile              *file);
215
216 /* iter setup:
217  * @user_data: the model
218  * @user_data2: GUINT_TO_POINTER of array index of current entry
219  *
220  * All other fields are unused. Note that the array index does not corrspond
221  * 1:1 with the path index as entries might not be visible.
222  */
223 #define ITER_INDEX(iter) GPOINTER_TO_UINT((iter)->user_data2)
224 #define ITER_IS_VALID(model, iter) ((model) == (iter)->user_data)
225 #define ITER_INIT_FROM_INDEX(model, _iter, _index) G_STMT_START {\
226   g_assert (_index < (model)->files->len); \
227   (_iter)->user_data = (model); \
228   (_iter)->user_data2 = GUINT_TO_POINTER (_index); \
229 }G_STMT_END
230
231 /*** FileModelNode ***/
232
233 /* Get a FileModelNode structure given an index in the model->files array of nodes */
234 #define get_node(_model, _index) ((FileModelNode *) ((_model)->files->data + (_index) * (_model)->node_size))
235
236 /* Get an index within the model->files array of nodes, given a FileModelNode* */
237 #define node_index(_model, _node) (((gchar *) (_node) - (_model)->files->data) / (_model)->node_size)
238
239 /* @up_to_index: smallest model->files array index that will be valid after this call
240  * @up_to_row: smallest node->row that will be valid after this call
241  *
242  * If you want to validate up to an index or up to a row, specify the index or
243  * the row you want and specify G_MAXUINT for the other argument.  Pass
244  * G_MAXUINT for both arguments for "validate everything".
245  */
246 static void
247 node_validate_rows (GtkFileSystemModel *model, guint up_to_index, guint up_to_row)
248 {
249   guint i, row;
250
251   if (model->files->len == 0)
252     return;
253
254   up_to_index = MIN (up_to_index, model->files->len - 1);
255
256   i = model->n_nodes_valid;
257   if (i != 0)
258     row = get_node (model, i - 1)->row;
259   else
260     row = 0;
261
262   while (i <= up_to_index && row <= up_to_row)
263     {
264       FileModelNode *node = get_node (model, i);
265       if (node->visible)
266         row++;
267       node->row = row;
268       i++;
269     }
270   model->n_nodes_valid = i;
271 }
272
273 static guint
274 node_get_tree_row (GtkFileSystemModel *model, guint index)
275 {
276   if (model->n_nodes_valid <= index)
277     node_validate_rows (model, index, G_MAXUINT);
278
279   return get_node (model, index)->row - 1;
280 }
281
282 static void 
283 node_invalidate_index (GtkFileSystemModel *model, guint id)
284 {
285   model->n_nodes_valid = MIN (model->n_nodes_valid, id);
286 }
287
288 static GtkTreePath *
289 tree_path_new_from_node (GtkFileSystemModel *model, guint id)
290 {
291   guint r = node_get_tree_row (model, id);
292
293   g_assert (r < model->files->len);
294
295   return gtk_tree_path_new_from_indices (r, -1);
296 }
297
298 static void
299 emit_row_inserted_for_node (GtkFileSystemModel *model, guint id)
300 {
301   GtkTreePath *path;
302   GtkTreeIter iter;
303
304   path = tree_path_new_from_node (model, id);
305   ITER_INIT_FROM_INDEX (model, &iter, id);
306   gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
307   gtk_tree_path_free (path);
308 }
309
310 static void
311 emit_row_changed_for_node (GtkFileSystemModel *model, guint id)
312 {
313   GtkTreePath *path;
314   GtkTreeIter iter;
315
316   path = tree_path_new_from_node (model, id);
317   ITER_INIT_FROM_INDEX (model, &iter, id);
318   gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
319   gtk_tree_path_free (path);
320 }
321
322 static void
323 emit_row_deleted_for_row (GtkFileSystemModel *model, guint row)
324 {
325   GtkTreePath *path;
326
327   path = gtk_tree_path_new_from_indices (row, -1);
328   gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
329   gtk_tree_path_free (path);
330 }
331
332 static void
333 node_set_visible_and_filtered_out (GtkFileSystemModel *model, guint id, gboolean visible, gboolean filtered_out)
334 {
335   FileModelNode *node = get_node (model, id);
336
337   /* Filteredness */
338
339   if (node->filtered_out != filtered_out)
340     {
341       node->filtered_out = filtered_out;
342       if (node->visible && visible)
343         emit_row_changed_for_node (model, id);
344     }
345
346   /* Visibility */
347   
348   if (node->visible == visible ||
349       node->frozen_add)
350     return;
351
352   if (visible)
353     {
354       node->visible = TRUE;
355       node_invalidate_index (model, id);
356       emit_row_inserted_for_node (model, id);
357     }
358   else
359     {
360       guint row;
361
362       row = node_get_tree_row (model, id);
363       g_assert (row < model->files->len);
364
365       node->visible = FALSE;
366       node_invalidate_index (model, id);
367       emit_row_deleted_for_row (model, row);
368     }
369 }
370
371 static gboolean
372 node_should_be_filtered_out (GtkFileSystemModel *model, guint id)
373 {
374   FileModelNode *node = get_node (model, id);
375   GtkFileFilterInfo filter_info = { 0, };
376   GtkFileFilterFlags required;
377   gboolean result;
378   char *mime_type = NULL;
379   char *filename = NULL;
380   char *uri = NULL;
381
382   if (node->info == NULL)
383     return TRUE;
384
385   if (model->filter == NULL)
386     return FALSE;
387
388   /* fill info */
389   required = gtk_file_filter_get_needed (model->filter);
390
391   filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME;
392   filter_info.display_name = g_file_info_get_display_name (node->info);
393
394   if (required & GTK_FILE_FILTER_MIME_TYPE)
395     {
396       const char *s = g_file_info_get_content_type (node->info);
397       if (s)
398         {
399           mime_type = g_content_type_get_mime_type (s);
400           if (mime_type)
401             {
402               filter_info.mime_type = mime_type;
403               filter_info.contains |= GTK_FILE_FILTER_MIME_TYPE;
404             }
405         }
406     }
407
408   if (required & GTK_FILE_FILTER_FILENAME)
409     {
410       filename = g_file_get_path (node->file);
411       if (filename)
412         {
413           filter_info.filename = filename;
414           filter_info.contains |= GTK_FILE_FILTER_FILENAME;
415         }
416     }
417
418   if (required & GTK_FILE_FILTER_URI)
419     {
420       uri = g_file_get_uri (node->file);
421       if (uri)
422         {
423           filter_info.uri = uri;
424           filter_info.contains |= GTK_FILE_FILTER_URI;
425         }
426     }
427
428   result = !gtk_file_filter_filter (model->filter, &filter_info);
429
430   g_free (mime_type);
431   g_free (filename);
432   g_free (uri);
433
434   return result;
435 }
436
437 static gboolean
438 node_should_be_visible (GtkFileSystemModel *model, guint id, gboolean filtered_out)
439 {
440   FileModelNode *node = get_node (model, id);
441   gboolean result;
442
443   if (node->info == NULL)
444     return FALSE;
445
446   if (!model->show_hidden &&
447       (g_file_info_get_is_hidden (node->info) || g_file_info_get_is_backup (node->info)))
448     return FALSE;
449
450   if (_gtk_file_info_consider_as_directory (node->info))
451     {
452       if (!model->show_folders)
453         return FALSE;
454
455       if (!model->filter_folders)
456         return TRUE;
457     }
458   else
459     {
460       if (!model->show_files)
461         return FALSE;
462     }
463
464   result = !filtered_out;
465
466   return result;
467 }
468
469 static void
470 node_compute_visibility_and_filters (GtkFileSystemModel *model, guint id)
471 {
472   gboolean filtered_out;
473   gboolean visible;
474
475   filtered_out = node_should_be_filtered_out (model, id);
476   visible = node_should_be_visible (model, id, filtered_out);
477
478   node_set_visible_and_filtered_out (model, id, visible, filtered_out);
479 }
480
481 /*** GtkTreeModel ***/
482
483 static GtkTreeModelFlags
484 gtk_file_system_model_get_flags (GtkTreeModel *tree_model)
485 {
486   /* GTK_TREE_MODEL_ITERS_PERSIST doesn't work with arrays :( */
487   return GTK_TREE_MODEL_LIST_ONLY;
488 }
489
490 static gint
491 gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model)
492 {
493   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
494   
495   return model->n_columns;
496 }
497
498 static GType
499 gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
500                                        gint          i)
501 {
502   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
503   
504   g_return_val_if_fail (i >= 0 && (guint) i < model->n_columns, G_TYPE_NONE);
505
506   return model->column_types[i];
507 }
508
509 static int
510 compare_indices (gconstpointer key, gconstpointer _node)
511 {
512   const FileModelNode *node = _node;
513
514   return GPOINTER_TO_UINT (key) - node->row;
515 }
516
517 static gboolean
518 gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
519                                       GtkTreeIter  *iter,
520                                       GtkTreeIter  *parent,
521                                       gint          n)
522 {
523   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
524   char *node;
525   guint id;
526   guint row_to_find;
527
528   g_return_val_if_fail (n >= 0, FALSE);
529
530   if (parent != NULL)
531     return FALSE;
532
533   row_to_find = n + 1; /* plus one as our node->row numbers are 1-based; see the "Structure" comment at the beginning */
534
535   if (model->n_nodes_valid > 0 &&
536       get_node (model, model->n_nodes_valid - 1)->row >= row_to_find)
537     {
538       /* Fast path - the nodes are valid up to the sought one.
539        *
540        * First, find a node with the sought row number...*/
541
542       node = bsearch (GUINT_TO_POINTER (row_to_find), 
543                       model->files->data,
544                       model->n_nodes_valid,
545                       model->node_size,
546                       compare_indices);
547       if (node == NULL)
548         return FALSE;
549
550       /* ... Second, back up until we find the first visible node with that row number */
551
552       id = node_index (model, node);
553       while (!get_node (model, id)->visible)
554         id--;
555
556       g_assert (get_node (model, id)->row == row_to_find);
557     }
558   else
559     {
560       /* Slow path - the nodes need to be validated up to the sought one */
561
562       node_validate_rows (model, G_MAXUINT, n); /* note that this is really "n", not row_to_find - see node_validate_rows() */
563       id = model->n_nodes_valid - 1;
564       if (model->n_nodes_valid == 0 || get_node (model, id)->row != row_to_find)
565         return FALSE;
566     }
567
568   ITER_INIT_FROM_INDEX (model, iter, id);
569   return TRUE;
570 }
571
572 static gboolean
573 gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
574                                 GtkTreeIter  *iter,
575                                 GtkTreePath  *path)
576 {
577   g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
578
579   if (gtk_tree_path_get_depth (path) > 1)
580     return FALSE;
581
582   return gtk_file_system_model_iter_nth_child (tree_model, 
583                                                iter,
584                                                NULL, 
585                                                gtk_tree_path_get_indices (path)[0]);
586 }
587
588 static GtkTreePath *
589 gtk_file_system_model_get_path (GtkTreeModel *tree_model,
590                                 GtkTreeIter  *iter)
591 {
592   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
593       
594   g_return_val_if_fail (ITER_IS_VALID (model, iter), NULL);
595
596   return tree_path_new_from_node (model, ITER_INDEX (iter));
597 }
598
599 static void
600 gtk_file_system_model_get_value (GtkTreeModel *tree_model,
601                                  GtkTreeIter  *iter,
602                                  gint          column,
603                                  GValue       *value)
604 {
605   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
606   const GValue *original;
607   
608   g_return_if_fail ((guint) column < model->n_columns);
609   g_return_if_fail (ITER_IS_VALID (model, iter));
610
611   original = _gtk_file_system_model_get_value (model, iter, column);
612   if (original)
613     {
614       g_value_init (value, G_VALUE_TYPE (original));
615       g_value_copy (original, value);
616     }
617   else
618     g_value_init (value, model->column_types[column]);
619 }
620
621 static gboolean
622 gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
623                                  GtkTreeIter  *iter)
624 {
625   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
626   guint i;
627
628   g_return_val_if_fail (ITER_IS_VALID (model, iter), FALSE);
629
630   for (i = ITER_INDEX (iter) + 1; i < model->files->len; i++) 
631     {
632       FileModelNode *node = get_node (model, i);
633
634       if (node->visible)
635         {
636           ITER_INIT_FROM_INDEX (model, iter, i);
637           return TRUE;
638         }
639     }
640       
641   return FALSE;
642 }
643
644 static gboolean
645 gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
646                                      GtkTreeIter  *iter,
647                                      GtkTreeIter  *parent)
648 {
649   return FALSE;
650 }
651
652 static gboolean
653 gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
654                                       GtkTreeIter  *iter)
655 {
656   return FALSE;
657 }
658
659 static gint
660 gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
661                                        GtkTreeIter  *iter)
662 {
663   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
664
665   if (iter)
666     return 0;
667
668   return node_get_tree_row (model, model->files->len - 1) + 1;
669 }
670
671 static gboolean
672 gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
673                                    GtkTreeIter  *iter,
674                                    GtkTreeIter  *child)
675 {
676   return FALSE;
677 }
678
679 static void
680 gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
681                                 GtkTreeIter  *iter)
682 {
683   /* nothing to do */
684 }
685
686 static void
687 gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
688                                   GtkTreeIter  *iter)
689 {
690   /* nothing to do */
691 }
692
693 static void
694 gtk_file_system_model_iface_init (GtkTreeModelIface *iface)
695 {
696   iface->get_flags =       gtk_file_system_model_get_flags;
697   iface->get_n_columns =   gtk_file_system_model_get_n_columns;
698   iface->get_column_type = gtk_file_system_model_get_column_type;
699   iface->get_iter =        gtk_file_system_model_get_iter;
700   iface->get_path =        gtk_file_system_model_get_path;
701   iface->get_value =       gtk_file_system_model_get_value;
702   iface->iter_next =       gtk_file_system_model_iter_next;
703   iface->iter_children =   gtk_file_system_model_iter_children;
704   iface->iter_has_child =  gtk_file_system_model_iter_has_child;
705   iface->iter_n_children = gtk_file_system_model_iter_n_children;
706   iface->iter_nth_child =  gtk_file_system_model_iter_nth_child;
707   iface->iter_parent =     gtk_file_system_model_iter_parent;
708   iface->ref_node =        gtk_file_system_model_ref_node;
709   iface->unref_node =      gtk_file_system_model_unref_node;
710 }
711
712 /*** GtkTreeSortable ***/
713
714 typedef struct _SortData SortData;
715 struct _SortData {
716   GtkFileSystemModel *    model;
717   GtkTreeIterCompareFunc  func;
718   gpointer                data;
719   int                     order;        /* -1 to invert sort order or 1 to keep it */
720 };
721
722 /* returns FALSE if no sort necessary */
723 static gboolean
724 sort_data_init (SortData *data, GtkFileSystemModel *model)
725 {
726   GtkTreeDataSortHeader *header;
727
728   if (model->files->len <= 2)
729     return FALSE;
730
731   switch (model->sort_column_id)
732     {
733     case GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID:
734       if (!model->default_sort_func)
735         return FALSE;
736       data->func = model->default_sort_func;
737       data->data = model->default_sort_data;
738       break;
739     case GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID:
740       return FALSE;
741     default:
742       header = _gtk_tree_data_list_get_header (model->sort_list, model->sort_column_id);
743       if (header == NULL)
744         return FALSE;
745       data->func = header->func;
746       data->data = header->data;
747       break;
748     }
749
750   data->order = model->sort_order == GTK_SORT_DESCENDING ? -1 : 1;
751   data->model = model;
752   return TRUE;
753 }
754
755 static int
756 compare_array_element (gconstpointer a, gconstpointer b, gpointer user_data)
757 {
758   SortData *data = user_data;
759   GtkTreeIter itera, iterb;
760
761   ITER_INIT_FROM_INDEX (data->model, &itera, node_index (data->model, a));
762   ITER_INIT_FROM_INDEX (data->model, &iterb, node_index (data->model, b));
763   return data->func (GTK_TREE_MODEL (data->model), &itera, &iterb, data->data) * data->order;
764 }
765
766 static void
767 gtk_file_system_model_sort (GtkFileSystemModel *model)
768 {
769   SortData data;
770
771   if (model->frozen)
772     {
773       model->sort_on_thaw = TRUE;
774       return;
775     }
776
777   if (sort_data_init (&data, model))
778     {
779       GtkTreePath *path;
780       guint i;
781       guint r, n_visible_rows;
782
783       node_validate_rows (model, G_MAXUINT, G_MAXUINT);
784       n_visible_rows = node_get_tree_row (model, model->files->len - 1) + 1;
785       model->n_nodes_valid = 0;
786       g_hash_table_remove_all (model->file_lookup);
787       g_qsort_with_data (get_node (model, 1), /* start at index 1; don't sort the editable row */
788                          model->files->len - 1,
789                          model->node_size,
790                          compare_array_element,
791                          &data);
792       g_assert (model->n_nodes_valid == 0);
793       g_assert (g_hash_table_size (model->file_lookup) == 0);
794       if (n_visible_rows)
795         {
796           int *new_order = g_new (int, n_visible_rows);
797         
798           r = 0;
799           for (i = 0; i < model->files->len; i++)
800             {
801               FileModelNode *node = get_node (model, i);
802               if (!node->visible)
803                 {
804                   node->row = r;
805                   continue;
806                 }
807
808               new_order[r] = node->row - 1;
809               r++;
810               node->row = r;
811             }
812           g_assert (r == n_visible_rows);
813           path = gtk_tree_path_new ();
814           gtk_tree_model_rows_reordered (GTK_TREE_MODEL (model),
815                                          path,
816                                          NULL,
817                                          new_order);
818           gtk_tree_path_free (path);
819           g_free (new_order);
820         }
821     }
822
823   model->sort_on_thaw = FALSE;
824 }
825
826 static void
827 gtk_file_system_model_sort_node (GtkFileSystemModel *model, guint node)
828 {
829   /* FIXME: improve */
830   gtk_file_system_model_sort (model);
831 }
832
833 static gboolean
834 gtk_file_system_model_get_sort_column_id (GtkTreeSortable  *sortable,
835                                           gint             *sort_column_id,
836                                           GtkSortType      *order)
837 {
838   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
839
840   if (sort_column_id)
841     *sort_column_id = model->sort_column_id;
842   if (order)
843     *order = model->sort_order;
844
845   if (model->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
846       model->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
847     return FALSE;
848
849   return TRUE;
850 }
851
852 static void
853 gtk_file_system_model_set_sort_column_id (GtkTreeSortable  *sortable,
854                                           gint              sort_column_id,
855                                           GtkSortType       order)
856 {
857   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
858
859   if ((model->sort_column_id == sort_column_id) &&
860       (model->sort_order == order))
861     return;
862
863   if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
864     {
865       if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
866         {
867           GtkTreeDataSortHeader *header = NULL;
868
869           header = _gtk_tree_data_list_get_header (model->sort_list, 
870                                                    sort_column_id);
871
872           /* We want to make sure that we have a function */
873           g_return_if_fail (header != NULL);
874           g_return_if_fail (header->func != NULL);
875         }
876       else
877         {
878           g_return_if_fail (model->default_sort_func != NULL);
879         }
880     }
881
882
883   model->sort_column_id = sort_column_id;
884   model->sort_order = order;
885
886   gtk_tree_sortable_sort_column_changed (sortable);
887
888   gtk_file_system_model_sort (model);
889 }
890
891 static void
892 gtk_file_system_model_set_sort_func (GtkTreeSortable        *sortable,
893                                      gint                    sort_column_id,
894                                      GtkTreeIterCompareFunc  func,
895                                      gpointer                data,
896                                      GDestroyNotify          destroy)
897 {
898   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
899
900   model->sort_list = _gtk_tree_data_list_set_header (model->sort_list, 
901                                                      sort_column_id, 
902                                                      func, data, destroy);
903
904   if (model->sort_column_id == sort_column_id)
905     gtk_file_system_model_sort (model);
906 }
907
908 static void
909 gtk_file_system_model_set_default_sort_func (GtkTreeSortable        *sortable,
910                                              GtkTreeIterCompareFunc  func,
911                                              gpointer                data,
912                                              GDestroyNotify          destroy)
913 {
914   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
915
916   if (model->default_sort_destroy)
917     {
918       GDestroyNotify d = model->default_sort_destroy;
919
920       model->default_sort_destroy = NULL;
921       d (model->default_sort_data);
922     }
923
924   model->default_sort_func = func;
925   model->default_sort_data = data;
926   model->default_sort_destroy = destroy;
927
928   if (model->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
929     gtk_file_system_model_sort (model);
930 }
931
932 static gboolean
933 gtk_file_system_model_has_default_sort_func (GtkTreeSortable *sortable)
934 {
935   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
936
937   return (model->default_sort_func != NULL);
938 }
939
940 static void
941 gtk_file_system_model_sortable_init (GtkTreeSortableIface *iface)
942 {
943   iface->get_sort_column_id = gtk_file_system_model_get_sort_column_id;
944   iface->set_sort_column_id = gtk_file_system_model_set_sort_column_id;
945   iface->set_sort_func = gtk_file_system_model_set_sort_func;
946   iface->set_default_sort_func = gtk_file_system_model_set_default_sort_func;
947   iface->has_default_sort_func = gtk_file_system_model_has_default_sort_func;
948 }
949
950 /*** GtkTreeDragSource ***/
951
952 static gboolean
953 drag_source_row_draggable (GtkTreeDragSource *drag_source,
954                            GtkTreePath       *path)
955 {
956   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (drag_source);
957   GtkTreeIter iter;
958
959   if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
960     return FALSE;
961
962   return ITER_INDEX (&iter) != 0;
963 }
964
965 static gboolean
966 drag_source_drag_data_get (GtkTreeDragSource *drag_source,
967                            GtkTreePath       *path,
968                            GtkSelectionData  *selection_data)
969 {
970   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (drag_source);
971   FileModelNode *node;
972   GtkTreeIter iter;
973   char *uris[2]; 
974
975   if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
976     return FALSE;
977
978   node = get_node (model, ITER_INDEX (&iter));
979   if (node->file == NULL)
980     return FALSE;
981
982   uris[0] = g_file_get_uri (node->file);
983   uris[1] = NULL;
984   gtk_selection_data_set_uris (selection_data, uris);
985   g_free (uris[0]);
986
987   return TRUE;
988 }
989
990 static void
991 drag_source_iface_init (GtkTreeDragSourceIface *iface)
992 {
993   iface->row_draggable = drag_source_row_draggable;
994   iface->drag_data_get = drag_source_drag_data_get;
995   iface->drag_data_delete = NULL;
996 }
997
998 /*** GtkFileSystemModel ***/
999
1000 /* Signal IDs */
1001 enum {
1002   FINISHED_LOADING,
1003   LAST_SIGNAL
1004 };
1005
1006 static guint file_system_model_signals[LAST_SIGNAL] = { 0 };
1007
1008 \f
1009
1010 G_DEFINE_TYPE_WITH_CODE (GtkFileSystemModel, _gtk_file_system_model, G_TYPE_OBJECT,
1011                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
1012                                                 gtk_file_system_model_iface_init)
1013                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
1014                                                 gtk_file_system_model_sortable_init)
1015                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
1016                                                 drag_source_iface_init))
1017
1018 static void
1019 gtk_file_system_model_dispose (GObject *object)
1020 {
1021   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
1022
1023   if (model->dir_thaw_source)
1024     {
1025       g_source_remove (model->dir_thaw_source);
1026       model->dir_thaw_source = 0;
1027     }
1028
1029   g_cancellable_cancel (model->cancellable);
1030   if (model->dir_monitor)
1031     g_file_monitor_cancel (model->dir_monitor);
1032
1033   G_OBJECT_CLASS (_gtk_file_system_model_parent_class)->dispose (object);
1034 }
1035
1036
1037 static void
1038 gtk_file_system_model_finalize (GObject *object)
1039 {
1040   GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
1041   guint i;
1042
1043   for (i = 0; i < model->files->len; i++)
1044     {
1045       int v;
1046
1047       FileModelNode *node = get_node (model, i);
1048       if (node->file)
1049         g_object_unref (node->file);
1050       if (node->info)
1051         g_object_unref (node->info);
1052
1053       for (v = 0; v < model->n_columns; v++)
1054         if (G_VALUE_TYPE (&node->values[v]) != G_TYPE_INVALID)
1055           g_value_unset (&node->values[v]);
1056     }
1057   g_array_free (model->files, TRUE);
1058
1059   g_object_unref (model->cancellable);
1060   g_free (model->attributes);
1061   if (model->dir)
1062     g_object_unref (model->dir);
1063   if (model->dir_monitor)
1064     g_object_unref (model->dir_monitor);
1065   g_hash_table_destroy (model->file_lookup);
1066   if (model->filter)
1067     g_object_unref (model->filter);
1068
1069   g_slice_free1 (sizeof (GType) * model->n_columns, model->column_types);
1070
1071   _gtk_tree_data_list_header_free (model->sort_list);
1072   if (model->default_sort_destroy)
1073     model->default_sort_destroy (model->default_sort_data);
1074
1075   G_OBJECT_CLASS (_gtk_file_system_model_parent_class)->finalize (object);
1076 }
1077
1078 static void
1079 _gtk_file_system_model_class_init (GtkFileSystemModelClass *class)
1080 {
1081   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1082
1083   gobject_class->finalize = gtk_file_system_model_finalize;
1084   gobject_class->dispose = gtk_file_system_model_dispose;
1085
1086   file_system_model_signals[FINISHED_LOADING] =
1087     g_signal_new (I_("finished-loading"),
1088                   G_OBJECT_CLASS_TYPE (gobject_class),
1089                   G_SIGNAL_RUN_LAST,
1090                   G_STRUCT_OFFSET (GtkFileSystemModelClass, finished_loading),
1091                   NULL, NULL,
1092                   _gtk_marshal_VOID__POINTER,
1093                   G_TYPE_NONE, 1, G_TYPE_POINTER);
1094 }
1095
1096 static void
1097 _gtk_file_system_model_init (GtkFileSystemModel *model)
1098 {
1099   model->show_files = TRUE;
1100   model->show_folders = TRUE;
1101   model->show_hidden = FALSE;
1102   model->filter_folders = FALSE;
1103
1104   model->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
1105
1106   model->file_lookup = g_hash_table_new (g_file_hash, (GEqualFunc) g_file_equal);
1107   model->cancellable = g_cancellable_new ();
1108 }
1109
1110 /*** API ***/
1111
1112 static void
1113 gtk_file_system_model_closed_enumerator (GObject *object, GAsyncResult *res, gpointer data)
1114 {
1115   g_file_enumerator_close_finish (G_FILE_ENUMERATOR (object), res, NULL);
1116 }
1117
1118 static gboolean
1119 thaw_func (gpointer data)
1120 {
1121   GtkFileSystemModel *model = data;
1122
1123   _gtk_file_system_model_thaw_updates (model);
1124   model->dir_thaw_source = 0;
1125
1126   return FALSE;
1127 }
1128
1129 static void
1130 gtk_file_system_model_got_files (GObject *object, GAsyncResult *res, gpointer data)
1131 {
1132   GFileEnumerator *enumerator = G_FILE_ENUMERATOR (object);
1133   GtkFileSystemModel *model = data;
1134   GList *walk, *files;
1135   GError *error = NULL;
1136
1137   gdk_threads_enter ();
1138
1139   files = g_file_enumerator_next_files_finish (enumerator, res, &error);
1140
1141   if (files)
1142     {
1143       if (model->dir_thaw_source == 0)
1144         {
1145           _gtk_file_system_model_freeze_updates (model);
1146           model->dir_thaw_source = gdk_threads_add_timeout_full (IO_PRIORITY + 1,
1147                                                                  50,
1148                                                                  thaw_func,
1149                                                                  model,
1150                                                                  NULL);
1151         }
1152
1153       for (walk = files; walk; walk = walk->next)
1154         {
1155           const char *name;
1156           GFileInfo *info;
1157           GFile *file;
1158           
1159           info = walk->data;
1160           name = g_file_info_get_name (info);
1161           if (name == NULL)
1162             {
1163               /* Shouldn't happen, but the APIs allow it */
1164               g_object_unref (info);
1165               continue;
1166             }
1167           file = g_file_get_child (model->dir, name);
1168           add_file (model, file, info);
1169           g_object_unref (file);
1170           g_object_unref (info);
1171         }
1172       g_list_free (files);
1173
1174       g_file_enumerator_next_files_async (enumerator,
1175                                           g_file_is_native (model->dir) ? 50 * FILES_PER_QUERY : FILES_PER_QUERY,
1176                                           IO_PRIORITY,
1177                                           model->cancellable,
1178                                           gtk_file_system_model_got_files,
1179                                           model);
1180     }
1181   else
1182     {
1183       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
1184         {
1185           g_file_enumerator_close_async (enumerator,
1186                                          IO_PRIORITY,
1187                                          model->cancellable,
1188                                          gtk_file_system_model_closed_enumerator,
1189                                          NULL);
1190           if (model->dir_thaw_source != 0)
1191             {
1192               g_source_remove (model->dir_thaw_source);
1193               model->dir_thaw_source = 0;
1194               _gtk_file_system_model_thaw_updates (model);
1195             }
1196
1197           g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0, error);
1198         }
1199
1200       if (error)
1201         g_error_free (error);
1202     }
1203
1204   gdk_threads_leave ();
1205 }
1206
1207 static void
1208 gtk_file_system_model_query_done (GObject *     object,
1209                                   GAsyncResult *res,
1210                                   gpointer      data)
1211 {
1212   GtkFileSystemModel *model = data; /* only a valid pointer if not cancelled */
1213   GFile *file = G_FILE (object);
1214   GFileInfo *info;
1215   guint id;
1216
1217   info = g_file_query_info_finish (file, res, NULL);
1218   if (info == NULL)
1219     return;
1220
1221   gdk_threads_enter ();
1222
1223   _gtk_file_system_model_update_file (model, file, info);
1224
1225   id = node_get_for_file (model, file);
1226   gtk_file_system_model_sort_node (model, id);
1227
1228   gdk_threads_leave ();
1229 }
1230
1231 static void
1232 gtk_file_system_model_monitor_change (GFileMonitor *      monitor,
1233                                       GFile *             file,
1234                                       GFile *             other_file,
1235                                       GFileMonitorEvent   type,
1236                                       GtkFileSystemModel *model)
1237 {
1238   switch (type)
1239     {
1240       case G_FILE_MONITOR_EVENT_CREATED:
1241       case G_FILE_MONITOR_EVENT_CHANGED:
1242       case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
1243         /* We can treat all of these the same way */
1244         g_file_query_info_async (file,
1245                                  model->attributes,
1246                                  G_FILE_QUERY_INFO_NONE,
1247                                  IO_PRIORITY,
1248                                  model->cancellable,
1249                                  gtk_file_system_model_query_done,
1250                                  model);
1251         break;
1252       case G_FILE_MONITOR_EVENT_DELETED:
1253         gdk_threads_enter ();
1254         remove_file (model, file);
1255         gdk_threads_leave ();
1256         break;
1257       case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
1258         /* FIXME: use freeze/thaw with this somehow? */
1259       case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
1260       case G_FILE_MONITOR_EVENT_UNMOUNTED:
1261       default:
1262         /* ignore these */
1263         break;
1264     }
1265 }
1266
1267 static void
1268 gtk_file_system_model_got_enumerator (GObject *dir, GAsyncResult *res, gpointer data)
1269 {
1270   GtkFileSystemModel *model = data;
1271   GFileEnumerator *enumerator;
1272   GError *error = NULL;
1273
1274   gdk_threads_enter ();
1275
1276   enumerator = g_file_enumerate_children_finish (G_FILE (dir), res, &error);
1277   if (enumerator == NULL)
1278     {
1279       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
1280       {
1281         g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0, error);
1282         g_error_free (error);
1283       }
1284     }
1285   else
1286     {
1287       g_file_enumerator_next_files_async (enumerator,
1288                                           g_file_is_native (model->dir) ? 50 * FILES_PER_QUERY : FILES_PER_QUERY,
1289                                           IO_PRIORITY,
1290                                           model->cancellable,
1291                                           gtk_file_system_model_got_files,
1292                                           model);
1293       g_object_unref (enumerator);
1294       model->dir_monitor = g_file_monitor_directory (model->dir,
1295                                                      G_FILE_MONITOR_NONE,
1296                                                      model->cancellable,
1297                                                      NULL); /* we don't mind if directory monitoring isn't supported, so the GError is NULL here */
1298       if (model->dir_monitor)
1299         g_signal_connect (model->dir_monitor,
1300                           "changed",
1301                           G_CALLBACK (gtk_file_system_model_monitor_change),
1302                           model);
1303     }
1304
1305   gdk_threads_leave ();
1306 }
1307
1308 static void
1309 gtk_file_system_model_set_n_columns (GtkFileSystemModel *model,
1310                                      gint                n_columns,
1311                                      va_list             args)
1312 {
1313   guint i;
1314
1315   g_assert (model->files == NULL);
1316   g_assert (n_columns > 0);
1317
1318   model->n_columns = n_columns;
1319   model->column_types = g_slice_alloc (sizeof (GType) * n_columns);
1320
1321   model->node_size = sizeof (FileModelNode) + sizeof (GValue) * (n_columns - 1); /* minus 1 because FileModelNode.values[] has a default size of 1 */
1322
1323   for (i = 0; i < (guint) n_columns; i++)
1324     {
1325       GType type = va_arg (args, GType);
1326       if (! _gtk_tree_data_list_check_type (type))
1327         {
1328           g_error ("%s: type %s cannot be a column type for GtkFileSystemModel\n", G_STRLOC, g_type_name (type));
1329           return; /* not reached */
1330         }
1331
1332       model->column_types[i] = type;
1333     }
1334
1335   model->sort_list = _gtk_tree_data_list_header_new (n_columns, model->column_types);
1336
1337   model->files = g_array_sized_new (FALSE, FALSE, model->node_size, FILES_PER_QUERY);
1338   /* add editable node at start */
1339   g_array_set_size (model->files, 1);
1340   memset (get_node (model, 0), 0, model->node_size);
1341 }
1342
1343 static void
1344 gtk_file_system_model_set_directory (GtkFileSystemModel *model,
1345                                      GFile *             dir,
1346                                      const gchar *       attributes)
1347 {
1348   g_assert (G_IS_FILE (dir));
1349
1350   model->dir = g_object_ref (dir);
1351   model->attributes = g_strdup (attributes);
1352
1353   g_file_enumerate_children_async (model->dir,
1354                                    attributes,
1355                                    G_FILE_QUERY_INFO_NONE,
1356                                    IO_PRIORITY,
1357                                    model->cancellable,
1358                                    gtk_file_system_model_got_enumerator,
1359                                    model);
1360
1361 }
1362
1363 static GtkFileSystemModel *
1364 _gtk_file_system_model_new_valist (GtkFileSystemModelGetValue get_func,
1365                                    gpointer            get_data,
1366                                    guint               n_columns,
1367                                    va_list             args)
1368 {
1369   GtkFileSystemModel *model;
1370
1371   model = g_object_new (GTK_TYPE_FILE_SYSTEM_MODEL, NULL);
1372   model->get_func = get_func;
1373   model->get_data = get_data;
1374
1375   gtk_file_system_model_set_n_columns (model, n_columns, args);
1376
1377   return model;
1378 }
1379
1380 /**
1381  * _gtk_file_system_model_new:
1382  * @get_func: function to call for getting a value
1383  * @get_data: user data argument passed to @get_func
1384  * @n_columns: number of columns
1385  * @...: @n_columns #GType types for the columns
1386  *
1387  * Creates a new #GtkFileSystemModel object. You need to add files
1388  * to the list using _gtk_file_system_model_add_and_query_file()
1389  * or _gtk_file_system_model_update_file().
1390  *
1391  * Return value: the newly created #GtkFileSystemModel
1392  **/
1393 GtkFileSystemModel *
1394 _gtk_file_system_model_new (GtkFileSystemModelGetValue get_func,
1395                             gpointer            get_data,
1396                             guint               n_columns,
1397                             ...)
1398 {
1399   GtkFileSystemModel *model;
1400   va_list args;
1401
1402   g_return_val_if_fail (get_func != NULL, NULL);
1403   g_return_val_if_fail (n_columns > 0, NULL);
1404
1405   va_start (args, n_columns);
1406   model = _gtk_file_system_model_new_valist (get_func, get_data, n_columns, args);
1407   va_end (args);
1408
1409   return model;
1410 }
1411
1412 /**
1413  * _gtk_file_system_model_new_for_directory:
1414  * @directory: the directory to show.
1415  * @attributes: (allow-none): attributes to immediately load or %NULL for all
1416  * @get_func: function that the model should call to query data about a file
1417  * @get_data: user data to pass to the @get_func
1418  * @n_columns: number of columns
1419  * @...: @n_columns #GType types for the columns
1420  *
1421  * Creates a new #GtkFileSystemModel object. The #GtkFileSystemModel
1422  * object wraps the given @directory as a #GtkTreeModel.
1423  * The model will query the given directory with the given @attributes
1424  * and add all files inside the directory automatically. If supported,
1425  * it will also monitor the drectory and update the model's
1426  * contents to reflect changes, if the @directory supports monitoring.
1427  * 
1428  * Return value: the newly created #GtkFileSystemModel
1429  **/
1430 GtkFileSystemModel *
1431 _gtk_file_system_model_new_for_directory (GFile *                    dir,
1432                                           const gchar *              attributes,
1433                                           GtkFileSystemModelGetValue get_func,
1434                                           gpointer                   get_data,
1435                                           guint                      n_columns,
1436                                           ...)
1437 {
1438   GtkFileSystemModel *model;
1439   va_list args;
1440
1441   g_return_val_if_fail (G_IS_FILE (dir), NULL);
1442   g_return_val_if_fail (get_func != NULL, NULL);
1443   g_return_val_if_fail (n_columns > 0, NULL);
1444
1445   va_start (args, n_columns);
1446   model = _gtk_file_system_model_new_valist (get_func, get_data, n_columns, args);
1447   va_end (args);
1448
1449   gtk_file_system_model_set_directory (model, dir, attributes);
1450
1451   return model;
1452 }
1453
1454 static void
1455 gtk_file_system_model_refilter_all (GtkFileSystemModel *model)
1456 {
1457   guint i;
1458
1459   if (model->frozen)
1460     {
1461       model->filter_on_thaw = TRUE;
1462       return;
1463     }
1464
1465   _gtk_file_system_model_freeze_updates (model);
1466
1467   /* start at index 1, don't change the editable */
1468   for (i = 1; i < model->files->len; i++)
1469     node_compute_visibility_and_filters (model, i);
1470
1471   model->filter_on_thaw = FALSE;
1472   _gtk_file_system_model_thaw_updates (model);
1473 }
1474
1475 /**
1476  * _gtk_file_system_model_set_show_hidden:
1477  * @model: a #GtkFileSystemModel
1478  * @show_hidden: whether hidden files should be displayed
1479  * 
1480  * Sets whether hidden files should be included in the #GtkTreeModel
1481  * for display.
1482  **/
1483 void
1484 _gtk_file_system_model_set_show_hidden (GtkFileSystemModel *model,
1485                                         gboolean            show_hidden)
1486 {
1487   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1488
1489   show_hidden = show_hidden != FALSE;
1490
1491   if (show_hidden != model->show_hidden)
1492     {
1493       model->show_hidden = show_hidden;
1494       gtk_file_system_model_refilter_all (model);
1495     }
1496 }
1497
1498 /**
1499  * _gtk_file_system_model_set_show_folders:
1500  * @model: a #GtkFileSystemModel
1501  * @show_folders: whether folders should be displayed
1502  * 
1503  * Sets whether folders should be included in the #GtkTreeModel for
1504  * display.
1505  **/
1506 void
1507 _gtk_file_system_model_set_show_folders (GtkFileSystemModel *model,
1508                                          gboolean            show_folders)
1509 {
1510   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1511
1512   show_folders = show_folders != FALSE;
1513
1514   if (show_folders != model->show_folders)
1515     {
1516       model->show_folders = show_folders;
1517       gtk_file_system_model_refilter_all (model);
1518     }
1519 }
1520
1521 /**
1522  * _gtk_file_system_model_set_show_files:
1523  * @model: a #GtkFileSystemModel
1524  * @show_files: whether files (as opposed to folders) should
1525  *              be displayed.
1526  * 
1527  * Sets whether files (as opposed to folders) should be included
1528  * in the #GtkTreeModel for display.
1529  **/
1530 void
1531 _gtk_file_system_model_set_show_files (GtkFileSystemModel *model,
1532                                        gboolean            show_files)
1533 {
1534   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1535
1536   show_files = show_files != FALSE;
1537
1538   if (show_files != model->show_files)
1539     {
1540       model->show_files = show_files;
1541       gtk_file_system_model_refilter_all (model);
1542     }
1543 }
1544
1545 /**
1546  * _gtk_file_system_model_set_filter_folders:
1547  * @model: a #GtkFileSystemModel
1548  * @filter_folders: whether the filter applies to folders
1549  * 
1550  * Sets whether the filter set by _gtk_file_system_model_set_filter()
1551  * applies to folders. By default, it does not and folders are always
1552  * visible.
1553  **/
1554 void
1555 _gtk_file_system_model_set_filter_folders (GtkFileSystemModel *model,
1556                                            gboolean            filter_folders)
1557 {
1558   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1559
1560   filter_folders = filter_folders != FALSE;
1561
1562   if (filter_folders != model->filter_folders)
1563     {
1564       model->filter_folders = filter_folders;
1565       gtk_file_system_model_refilter_all (model);
1566     }
1567 }
1568
1569 /**
1570  * _gtk_file_system_model_get_cancellable:
1571  * @model: the model
1572  *
1573  * Gets the cancellable used by the @model. This is the cancellable used
1574  * internally by the @model that will be cancelled when @model is 
1575  * disposed. So you can use it for operations that should be cancelled
1576  * when the model goes away.
1577  *
1578  * Returns: The cancellable used by @model
1579  **/
1580 GCancellable *
1581 _gtk_file_system_model_get_cancellable (GtkFileSystemModel *model)
1582 {
1583   g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), NULL);
1584
1585   return model->cancellable;
1586 }
1587
1588 /**
1589  * _gtk_file_system_model_iter_is_visible:
1590  * @model: the model
1591  * @iter: a valid iterator
1592  *
1593  * Checks if the iterator is visible. A visible iterator references
1594  * a row that is currently exposed using the #GtkTreeModel API. If
1595  * the iterator is invisible, it references a file that is not shown
1596  * for some reason, such as being filtered out by the current filter or
1597  * being a hidden file.
1598  *
1599  * Returns: %TRUE if the iterator is visible
1600  **/
1601 gboolean
1602 _gtk_file_system_model_iter_is_visible (GtkFileSystemModel *model,
1603                                         GtkTreeIter        *iter)
1604 {
1605   FileModelNode *node;
1606
1607   g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), FALSE);
1608   g_return_val_if_fail (iter != NULL, FALSE);
1609
1610   node = get_node (model, ITER_INDEX (iter));
1611   return node->visible;
1612 }
1613
1614 /**
1615  * _gtk_file_system_model_iter_is_filtered_out:
1616  * @model: the model
1617  * @iter: a valid iterator
1618  *
1619  * Checks if the iterator is filtered out.  This is only useful for rows
1620  * that refer to folders, as those are always visible regardless
1621  * of what the current filter says.  This function lets you see
1622  * the results of the filter.
1623  *
1624  * Returns: %TRUE if the iterator passed the current filter; %FALSE if the
1625  * filter would not have let the row pass.
1626  **/
1627 gboolean
1628 _gtk_file_system_model_iter_is_filtered_out (GtkFileSystemModel *model,
1629                                              GtkTreeIter        *iter)
1630 {
1631   FileModelNode *node;
1632
1633   g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), FALSE);
1634   g_return_val_if_fail (iter != NULL, FALSE);
1635
1636   node = get_node (model, ITER_INDEX (iter));
1637   return node->filtered_out;
1638 }
1639
1640 /**
1641  * _gtk_file_system_model_get_info:
1642  * @model: a #GtkFileSystemModel
1643  * @iter: a #GtkTreeIter pointing to a row of @model
1644  * 
1645  * Gets the #GFileInfo structure for a particular row
1646  * of @model.
1647  * 
1648  * Return value: a #GFileInfo structure. This structure
1649  *   is owned by @model and must not be modified or freed.
1650  *   If you want to keep the information for later use,
1651  *   you must take a reference, since the structure may be
1652  *   freed on later changes to the file system.  If you have
1653  *   called _gtk_file_system_model_add_editable() and the @iter
1654  *   corresponds to the row that this function returned, the
1655  *   return value will be NULL.
1656  **/
1657 GFileInfo *
1658 _gtk_file_system_model_get_info (GtkFileSystemModel *model,
1659                                  GtkTreeIter        *iter)
1660 {
1661   FileModelNode *node;
1662
1663   g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), NULL);
1664   g_return_val_if_fail (iter != NULL, NULL);
1665
1666   node = get_node (model, ITER_INDEX (iter));
1667   g_assert (node->info == NULL || G_IS_FILE_INFO (node->info));
1668   return node->info;
1669 }
1670
1671 /**
1672  * _gtk_file_system_model_get_file:
1673  * @model: a #GtkFileSystemModel
1674  * @iter: a #GtkTreeIter pointing to a row of @model
1675  * 
1676  * Gets the file for a particular row in @model. 
1677  *
1678  * Return value: the file. This object is owned by @model and
1679  *   or freed. If you want to save the path for later use,
1680  *   you must take a ref, since the object may be freed
1681  *   on later changes to the file system.
1682  **/
1683 GFile *
1684 _gtk_file_system_model_get_file (GtkFileSystemModel *model,
1685                                  GtkTreeIter        *iter)
1686 {
1687   FileModelNode *node;
1688
1689   g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), NULL);
1690
1691   node = get_node (model, ITER_INDEX (iter));
1692   return node->file;
1693 }
1694
1695 /**
1696  * _gtk_file_system_model_get_value:
1697  * @model: a #GtkFileSystemModel
1698  * @iter: a #GtkTreeIter pointing to a row of @model
1699  * @column: the column to get the value for
1700  *
1701  * Gets the value associated with the given row @iter and @column.
1702  * If no value is available yet and the default value should be used,
1703  * %NULL is returned.
1704  * This is a performance optimization for the calls 
1705  * gtk_tree_model_get() or gtk_tree_model_get_value(), which copy 
1706  * the value and spend a considerable amount of time in iterator 
1707  * lookups. Both of which are slow.
1708  *
1709  * Returns: a pointer to the actual value as stored in @model or %NULL
1710  *          if no value available yet.
1711  **/
1712 const GValue *
1713 _gtk_file_system_model_get_value (GtkFileSystemModel *model,
1714                                   GtkTreeIter *       iter,
1715                                   int                 column)
1716 {
1717   FileModelNode *node;
1718
1719   g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), NULL);
1720   g_return_val_if_fail (column >= 0 && (guint) column < model->n_columns, NULL);
1721
1722   node = get_node (model, ITER_INDEX (iter));
1723     
1724   if (!G_VALUE_TYPE (&node->values[column]))
1725     {
1726       g_value_init (&node->values[column], model->column_types[column]);
1727       if (!model->get_func (model, 
1728                             node->file, 
1729                             node->info, 
1730                             column, 
1731                             &node->values[column],
1732                             model->get_data))
1733         {
1734           g_value_unset (&node->values[column]);
1735           return NULL;
1736         }
1737     }
1738   
1739   return &node->values[column];
1740 }
1741
1742 static guint
1743 node_get_for_file (GtkFileSystemModel *model,
1744                    GFile *             file)
1745 {
1746   guint i;
1747
1748   i = GPOINTER_TO_UINT (g_hash_table_lookup (model->file_lookup, file));
1749   if (i != 0)
1750     return i;
1751
1752   /* Node 0 is the editable row and has no associated file or entry in the table, so we start counting from 1.
1753    *
1754    * The invariant here is that the files in model->files[n] for n < g_hash_table_size (model->file_lookup)
1755    * are already added to the hash table. The table can get cleared when we re-sort; this loop merely rebuilds
1756    * our (file -> index) mapping on demand.
1757    *
1758    * If we exit the loop, the next pending batch of mappings will be resolved when this function gets called again
1759    * with another file that is not yet in the mapping.
1760    */
1761   for (i = g_hash_table_size (model->file_lookup) + 1; i < model->files->len; i++)
1762     {
1763       FileModelNode *node = get_node (model, i);
1764
1765       g_hash_table_insert (model->file_lookup, node->file, GUINT_TO_POINTER (i));
1766       if (g_file_equal (node->file, file))
1767         return i;
1768     }
1769
1770   return 0;
1771 }
1772
1773 /**
1774  * _gtk_file_system_model_get_iter_for_file:
1775  * @model: the model
1776  * @iter: the iterator to be initialized
1777  * @file: the file to look up
1778  *
1779  * Initializes @iter to point to the row used for @file, if @file is part 
1780  * of the model. Note that upon successful return, @iter may point to an 
1781  * invisible row in the @model. Use 
1782  * _gtk_file_system_model_iter_is_visible() to make sure it is visible to
1783  * the tree view.
1784  *
1785  * Returns: %TRUE if file is part of the model and @iter was initialized
1786  **/
1787 gboolean
1788 _gtk_file_system_model_get_iter_for_file (GtkFileSystemModel *model,
1789                                           GtkTreeIter        *iter,
1790                                           GFile *             file)
1791 {
1792   guint i;
1793
1794   g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), FALSE);
1795   g_return_val_if_fail (iter != NULL, FALSE);
1796   g_return_val_if_fail (G_IS_FILE (file), FALSE);
1797
1798   i = node_get_for_file (model, file);
1799
1800   if (i == 0)
1801     return FALSE;
1802
1803   ITER_INIT_FROM_INDEX (model, iter, i);
1804   return TRUE;
1805 }
1806
1807 /**
1808  * add_file:
1809  * @model: the model
1810  * @file: the file to add
1811  * @info: the information to associate with the file
1812  *
1813  * Adds the given @file with its associated @info to the @model. 
1814  * If the model is frozen, the file will only show up after it is thawn.
1815  **/
1816 static void
1817 add_file (GtkFileSystemModel *model,
1818           GFile              *file,
1819           GFileInfo          *info)
1820 {
1821   FileModelNode *node;
1822   
1823   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1824   g_return_if_fail (G_IS_FILE (file));
1825   g_return_if_fail (G_IS_FILE_INFO (info));
1826
1827   node = g_slice_alloc0 (model->node_size);
1828   node->file = g_object_ref (file);
1829   if (info)
1830     node->info = g_object_ref (info);
1831   node->frozen_add = model->frozen ? TRUE : FALSE;
1832
1833   g_array_append_vals (model->files, node, 1);
1834   g_slice_free1 (model->node_size, node);
1835
1836   if (!model->frozen)
1837     node_compute_visibility_and_filters (model, model->files->len -1);
1838
1839   gtk_file_system_model_sort_node (model, model->files->len -1);
1840 }
1841
1842 /**
1843  * remove_file:
1844  * @model: the model
1845  * @file: file to remove from the model. The file must have been 
1846  *        added to the model previously
1847  *
1848  * Removes the given file from the model. If the file is not part of 
1849  * @model, this function does nothing.
1850  **/
1851 static void
1852 remove_file (GtkFileSystemModel *model,
1853              GFile              *file)
1854 {
1855   FileModelNode *node;
1856   guint id;
1857
1858   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1859   g_return_if_fail (G_IS_FILE (file));
1860
1861   id = node_get_for_file (model, file);
1862   if (id == 0)
1863     return;
1864
1865   node = get_node (model, id);
1866   node_set_visible_and_filtered_out (model, id, FALSE, FALSE);
1867
1868   g_hash_table_remove (model->file_lookup, file);
1869   g_object_unref (node->file);
1870
1871   if (node->info)
1872     g_object_unref (node->info);
1873
1874   g_array_remove_index (model->files, id);
1875   g_hash_table_remove_all (model->file_lookup);
1876   /* We don't need to resort, as removing a row doesn't change the sorting order */
1877 }
1878
1879 /**
1880  * _gtk_file_system_model_update_file:
1881  * @model: the model
1882  * @file: the file
1883  * @info: the new file info
1884  *
1885  * Tells the file system model that the file changed and that the 
1886  * new @info should be used for it now.  If the file is not part of 
1887  * @model, it will get added automatically.
1888  **/
1889 void
1890 _gtk_file_system_model_update_file (GtkFileSystemModel *model,
1891                                     GFile              *file,
1892                                     GFileInfo          *info)
1893 {
1894   FileModelNode *node;
1895   guint i, id;
1896   GFileInfo *old_info;
1897
1898   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1899   g_return_if_fail (G_IS_FILE (file));
1900   g_return_if_fail (G_IS_FILE_INFO (info));
1901
1902   id = node_get_for_file (model, file);
1903   if (id == 0)
1904     {
1905       add_file (model, file, info);
1906       id = node_get_for_file (model, file);
1907     }
1908
1909   node = get_node (model, id);
1910
1911   old_info = node->info;
1912   node->info = g_object_ref (info);
1913   if (old_info)
1914     g_object_unref (old_info);
1915
1916   for (i = 0; i < model->n_columns; i++)
1917     {
1918       if (G_VALUE_TYPE (&node->values[i]))
1919         g_value_unset (&node->values[i]);
1920     }
1921
1922   if (node->visible)
1923     emit_row_changed_for_node (model, id);
1924 }
1925
1926 /**
1927  * _gtk_file_system_model_set_filter:
1928  * @mode: a #GtkFileSystemModel
1929  * @filter: (allow-none): %NULL or filter to use
1930  * 
1931  * Sets a filter to be used for deciding if a row should be visible or not.
1932  * Whether this filter applies to directories can be toggled with
1933  * _gtk_file_system_model_set_filter_folders().
1934  **/
1935 void
1936 _gtk_file_system_model_set_filter (GtkFileSystemModel      *model,
1937                                    GtkFileFilter *          filter)
1938 {
1939   GtkFileFilter *old_filter;
1940
1941   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1942   g_return_if_fail (filter == NULL || GTK_IS_FILE_FILTER (filter));
1943   
1944   if (filter)
1945     g_object_ref (filter);
1946
1947   old_filter = model->filter;
1948   model->filter = filter;
1949
1950   if (old_filter)
1951     g_object_unref (old_filter);
1952
1953   gtk_file_system_model_refilter_all (model);
1954 }
1955
1956 /**
1957  * _gtk_file_system_model_add_editable:
1958  * @model: a #GtkFileSystemModel
1959  * @iter: Location to return the iter corresponding to the editable row
1960  * 
1961  * Adds an "empty" row at the beginning of the model.  This does not refer to
1962  * any file, but is a temporary placeholder for a file name that the user will
1963  * type when a corresponding cell is made editable.  When your code is done
1964  * using this temporary row, call _gtk_file_system_model_remove_editable().
1965  **/
1966 void
1967 _gtk_file_system_model_add_editable (GtkFileSystemModel *model, GtkTreeIter *iter)
1968 {
1969   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1970   g_return_if_fail (!get_node (model, 0)->visible);
1971
1972   node_set_visible_and_filtered_out (model, 0, TRUE, FALSE);
1973   ITER_INIT_FROM_INDEX (model, iter, 0);
1974 }
1975
1976 /**
1977  * _gtk_file_system_model_remove_editable:
1978  * @model: a #GtkFileSystemModel
1979  * 
1980  * Removes the "empty" row at the beginning of the model that was
1981  * created with _gtk_file_system_model_add_editable().  You should call
1982  * this function when your code is finished editing this temporary row.
1983  **/
1984 void
1985 _gtk_file_system_model_remove_editable (GtkFileSystemModel *model)
1986 {
1987   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
1988   g_return_if_fail (get_node (model, 0)->visible);
1989
1990   node_set_visible_and_filtered_out (model, 0, FALSE, FALSE);
1991 }
1992
1993 /**
1994  * _gtk_file_system_model_freeze_updates:
1995  * @model: a #GtkFileSystemModel
1996  *
1997  * Freezes most updates on the model, so that performing multiple 
1998  * operations on the files in the model do not cause any events.
1999  * Use _gtk_file_system_model_thaw_updates() to resume proper 
2000  * operations. It is fine to call this function multiple times as
2001  * long as freeze and thaw calls are balanced.
2002  **/
2003 void
2004 _gtk_file_system_model_freeze_updates (GtkFileSystemModel *model)
2005 {
2006   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
2007
2008   model->frozen++;
2009 }
2010
2011 /**
2012  * _gtk_file_system_model_thaw_updates:
2013  * @model: a #GtkFileSystemModel
2014  *
2015  * Undoes the effect of a previous call to
2016  * _gtk_file_system_model_freeze_updates() 
2017  **/
2018 void
2019 _gtk_file_system_model_thaw_updates (GtkFileSystemModel *model)
2020 {
2021   gboolean stuff_added;
2022
2023   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
2024   g_return_if_fail (model->frozen > 0);
2025
2026   model->frozen--;
2027   if (model->frozen > 0)
2028     return;
2029
2030   stuff_added = get_node (model, model->files->len - 1)->frozen_add;
2031
2032   if (model->filter_on_thaw)
2033     gtk_file_system_model_refilter_all (model);
2034   if (model->sort_on_thaw)
2035     gtk_file_system_model_sort (model);
2036   if (stuff_added)
2037     {
2038       guint i;
2039
2040       for (i = 0; i < model->files->len; i++)
2041         {
2042           FileModelNode *node = get_node (model, i);
2043
2044           if (!node->frozen_add)
2045             continue;
2046           node->frozen_add = FALSE;
2047           node_compute_visibility_and_filters (model, i);
2048         }
2049     }
2050 }
2051
2052 /**
2053  * _gtk_file_system_model_clear_cache:
2054  * @model: a #GtkFileSystemModel
2055  * @column: the column to clear or -1 for all columns
2056  *
2057  * Clears the cached values in the model for the given @column. Use 
2058  * this function whenever your get_value function would return different
2059  * values for a column.
2060  * The file chooser uses this for example when the icon theme changes to 
2061  * invalidate the cached pixbufs.
2062  **/
2063 void
2064 _gtk_file_system_model_clear_cache (GtkFileSystemModel *model,
2065                                     int                 column)
2066 {
2067   guint i;
2068   int start, end;
2069   gboolean changed;
2070
2071   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
2072   g_return_if_fail (column >= -1 && (guint) column < model->n_columns);
2073
2074   if (column > -1)
2075     {
2076       start = column;
2077       end = column + 1;
2078     }
2079   else
2080     {
2081       start = 0;
2082       end = model->n_columns;
2083     }
2084
2085   for (i = 0; i < model->files->len; i++)
2086     {
2087       FileModelNode *node = get_node (model, i);
2088       changed = FALSE;
2089       for (column = start; column < end; column++)
2090         {
2091           if (!G_VALUE_TYPE (&node->values[column]))
2092             continue;
2093           
2094           g_value_unset (&node->values[column]);
2095           changed = TRUE;
2096         }
2097
2098       if (changed && node->visible)
2099         emit_row_changed_for_node (model, i);
2100     }
2101
2102   /* FIXME: resort? */
2103 }
2104
2105 /**
2106  * _gtk_file_system_model_add_and_query_file:
2107  * @model: a #GtkFileSystemModel
2108  * @file: the file to add
2109  * @attributes: attributes to query before adding the file
2110  *
2111  * This is a conenience function that calls g_file_query_info_async() on 
2112  * the given file, and when successful, adds it to the model.
2113  * Upon failure, the @file is discarded.
2114  **/
2115 void
2116 _gtk_file_system_model_add_and_query_file (GtkFileSystemModel *model,
2117                                            GFile *             file,
2118                                            const char *        attributes)
2119 {
2120   g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
2121   g_return_if_fail (G_IS_FILE (file));
2122   g_return_if_fail (attributes != NULL);
2123
2124   g_file_query_info_async (file,
2125                            attributes,
2126                            G_FILE_QUERY_INFO_NONE,
2127                            IO_PRIORITY,
2128                            model->cancellable,
2129                            gtk_file_system_model_query_done,
2130                            model);
2131 }