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