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