gtkfilesystemmodel.c 66.3 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Owen Taylor's avatar
Owen Taylor committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * gtkfilesystemmodel.c: GtkTreeModel wrapping a GtkFileSystem
 * Copyright (C) 2003, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Owen Taylor's avatar
Owen Taylor committed
17 18
 */

19
#include "config.h"
20

Owen Taylor's avatar
Owen Taylor committed
21
#include "gtkfilesystemmodel.h"
22 23 24 25

#include <stdlib.h>
#include <string.h>

26
#include "gtkfilesystem.h"
27
#include "gtkintl.h"
Federico Mena Quintero's avatar
Federico Mena Quintero committed
28
#include "gtkmarshalers.h"
29
#include "gtktreedatalist.h"
30
#include "gtktreednd.h"
31
#include "gtktreemodel.h"
Owen Taylor's avatar
Owen Taylor committed
32

33 34 35 36 37 38 39 40 41 42 43 44
/*** Structure: how GtkFileSystemModel works
 *
 * This is a custom GtkTreeModel used to hold a collection of files for GtkFileChooser.  There are two use cases:
 *
 *   1. The model populates itself from a folder, using the GIO file enumerator API.  This happens if you use
 *      _gtk_file_system_model_new_for_directory().  This is the normal usage for showing the contents of a folder.
 *
 *   2. The caller populates the model by hand, with files not necessarily in the same folder.  This happens
 *      if you use _gtk_file_system_model_new() and then _gtk_file_system_model_add_and_query_file().  This is
 *      the special kind of usage for "search" and "recent-files", where the file chooser gives the model the
 *      files to be displayed.
 *
45 46 47
 * Internal data structure
 * -----------------------
 *
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 * Each file is kept in a FileModelNode structure.  Each FileModelNode holds a GFile* and other data.  All the
 * node structures have the same size, determined at runtime, depending on the number of columns that were passed
 * to _gtk_file_system_model_new() or _gtk_file_system_model_new_for_directory() (that is, the size of a node is
 * not sizeof (FileModelNode), but rather model->node_size).  The last field in the FileModelNode structure,
 * node->values[], is an array of GValue, used to hold the data for those columns.
 *
 * The model stores an array of FileModelNode structures in model->files.  This is a GArray where each element is
 * model->node_size bytes in size (the model computes that node size when initializing itself).  There are
 * convenience macros, get_node() and node_index(), to access that array based on an array index or a pointer to
 * a node inside the array.
 *
 * The model accesses files through two of its fields:
 *
 *   model->files - GArray of FileModelNode structures.
 *
 *   model->file_lookup - hash table that maps a GFile* to an index inside the model->files array.
 *
 * The model->file_lookup hash table is populated lazily.  It is both accessed and populated with the
 * node_get_for_file() function.  The invariant is that the files in model->files[n] for n < g_hash_table_size
 * (model->file_lookup) are already added to the hash table. The hash table will get cleared when we re-sort the
 * files, as the array will be in a different order and the indexes need to be rebuilt.
 *
 * Each FileModelNode has a node->visible field, which indicates whether the node is visible in the GtkTreeView.
 * A node may be invisible if, for example, it corresponds to a hidden file and the file chooser is not showing
72 73 74 75 76 77 78 79
 * hidden files.  Also, a file filter may be explicitly set onto the model, for example, to only show files that
 * match "*.jpg".  In this case, node->filtered_out says whether the node failed the filter.  The ultimate
 * decision on whether a node is visible or not in the treeview is distilled into the node->visible field.
 * The reason for having a separate node->filtered_out field is so that the file chooser can query whether
 * a (filtered-out) folder should be made sensitive in the GUI.
 *
 * Visible rows vs. possibly-invisible nodes
 * -----------------------------------------
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 85 86 87 88 89 90 91 92 93
 *   index - An index in the model->files array.  All variables/fields that represent indexes are either called
 *   "index" or "i_*", or simply "i" for things like loop counters.
 *
 *   row - An index in the GtkTreeView, i.e. the index of a row within the outward-facing API of the
 *   GtkFileSystemModel.  However, note that our rows are 1-based, not 0-based, for the reason explained in the
 *   following paragraph.  Variables/fields that represent visible rows are called "row", or "r_*", or simply
 *   "r".
 *
 * Each FileModelNode has a node->row field which is the number of visible rows in the treeview, *before and
 * 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 96 97 98 99 100 101 102 103 104
 * turn the 0-based treepath into a 1-based row for our purposes.  If a node is not visible, it will have the
 * same row number as its closest preceding visible node.
 *
 * We try to compute the node->row fields lazily.  A node is said to be "valid" if its node->row is accurate.
 * For this, the model keeps a model->n_nodes_valid field which is the count of valid nodes starting from the
 * beginning of the model->files array.  When a node changes its information, or when a node gets deleted, that
 * node and the following ones get invalidated by simply setting model->n_nodes_valid to the array index of the
 * node.  If the model happens to need a node's row number and that node is in the model->files array after
 * model->n_nodes_valid, then the nodes get re-validated up to the sought node.  See node_validate_rows() for
 * this logic.
105
 *
106 107
 * You never access a node->row directly.  Instead, call node_get_tree_row().  That function will validate the nodes
 * up to the sought one if the node is not valid yet, and it will return a proper 0-based row.
108 109 110 111 112 113 114 115 116 117
 *
 * Sorting
 * -------
 *
 * The model implements the GtkTreeSortable interface.  To avoid re-sorting
 * every time a node gets added (which would lead to O(n^2) performance during
 * the initial population of the model), the model can freeze itself (with
 * freeze_updates()) during the intial population process.  When the model is
 * frozen, sorting will not happen.  The model will sort itself when the freeze
 * count goes back to zero, via corresponding calls to thaw_updates().
118 119
 */

120
/*** DEFINES ***/
121

122 123 124 125
/* priority used for all async callbacks in the main loop
 * This should be higher than redraw priorities so multiple callbacks
 * firing can be handled without intermediate redraws */
#define IO_PRIORITY G_PRIORITY_DEFAULT
126

127 128
/* random number that everyone else seems to use, too */
#define FILES_PER_QUERY 100
129

130 131
typedef struct _FileModelNode           FileModelNode;
typedef struct _GtkFileSystemModelClass GtkFileSystemModelClass;
132

133 134 135 136
struct _FileModelNode
{
  GFile *               file;           /* file represented by this node or NULL for editable */
  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 140
					 * this one - see the "Structure" comment above.
					 */
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 154
  GFile *               dir;            /* directory that's displayed */
  guint                 dir_thaw_source;/* GSource id for unfreezing the model */
155 156
  char *                attributes;     /* attributes the file info must contain, or NULL for all attributes */
  GFileMonitor *        dir_monitor;    /* directory that is monitored, or NULL if monitoring was not supported */
157

158 159
  GCancellable *        cancellable;    /* cancellable in use for all operations - cancelled on dispose */
  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 162
  guint                 n_nodes_valid;  /* count of valid nodes (i.e. those whose node->row is accurate) */
  GHashTable *          file_lookup;    /* mapping of GFile => array index in model->files
163 164 165 166 167
					 * This hash table doesn't always have the same number of entries as the files array;
					 * it can get cleared completely when we resort.
					 * The hash table gets re-populated in node_get_for_file() if this mismatch is
					 * detected.
					 */
168

169 170 171 172
  guint                 n_columns;      /* number of columns */
  GType *               column_types;   /* types of each column */
  GtkFileSystemModelGetValue get_func;  /* function to call to fill in values in columns */
  gpointer              get_data;       /* data to pass to get_func */
173

174
  GtkFileFilter *       filter;         /* filter to use for deciding which nodes are visible */
175

176 177 178 179 180 181 182 183
  int                   sort_column_id; /* current sorting column */
  GtkSortType           sort_order;     /* current sorting order */
  GList *               sort_list;      /* list of sorting functions */
  GtkTreeIterCompareFunc default_sort_func; /* default sort function */
  gpointer              default_sort_data; /* data to pass to default sort func */
  GDestroyNotify        default_sort_destroy; /* function to call to destroy default_sort_data */

  guint                 frozen;         /* number of times we're frozen */
184

185 186 187 188 189 190
  gboolean              filter_on_thaw :1;/* set when filtering needs to happen upon thawing */
  gboolean              sort_on_thaw :1;/* set when sorting needs to happen upon thawing */

  guint                 show_hidden :1; /* whether to show hidden files */
  guint                 show_folders :1;/* whether to show folders */
  guint                 show_files :1;  /* whether to show files */
191
  guint                 filter_folders :1;/* whether filter applies to folders */
192
};
Owen Taylor's avatar
Owen Taylor committed
193 194 195 196 197 198 199 200

#define GTK_FILE_SYSTEM_MODEL_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
#define GTK_IS_FILE_SYSTEM_MODEL_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_MODEL))
#define GTK_FILE_SYSTEM_MODEL_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))

struct _GtkFileSystemModelClass
{
  GObjectClass parent_class;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
201 202 203

  /* Signals */

204
  void (*finished_loading) (GtkFileSystemModel *model, GError *error);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
205 206
};

207 208 209
static void freeze_updates (GtkFileSystemModel *model);
static void thaw_updates (GtkFileSystemModel *model);

210 211 212
static guint node_get_for_file (GtkFileSystemModel *model,
				GFile              *file);

213 214 215
static void add_file (GtkFileSystemModel *model,
		      GFile              *file,
		      GFileInfo          *info);
216 217 218
static void remove_file (GtkFileSystemModel *model,
			 GFile              *file);

219 220 221 222 223 224 225 226 227 228 229 230 231 232
/* iter setup:
 * @user_data: the model
 * @user_data2: GUINT_TO_POINTER of array index of current entry
 *
 * All other fields are unused. Note that the array index does not corrspond
 * 1:1 with the path index as entries might not be visible.
 */
#define ITER_INDEX(iter) GPOINTER_TO_UINT((iter)->user_data2)
#define ITER_IS_VALID(model, iter) ((model) == (iter)->user_data)
#define ITER_INIT_FROM_INDEX(model, _iter, _index) G_STMT_START {\
  g_assert (_index < (model)->files->len); \
  (_iter)->user_data = (model); \
  (_iter)->user_data2 = GUINT_TO_POINTER (_index); \
}G_STMT_END
Federico Mena Quintero's avatar
Federico Mena Quintero committed
233

234
/*** FileModelNode ***/
Federico Mena Quintero's avatar
Federico Mena Quintero committed
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)
Owen Taylor's avatar
Owen Taylor committed
241

242 243
/* @up_to_index: smallest model->files array index that will be valid after this call
 * @up_to_row: smallest node->row that will be valid after this call
244
 *
245 246 247
 * If you want to validate up to an index or up to a row, specify the index or
 * the row you want and specify G_MAXUINT for the other argument.  Pass
 * G_MAXUINT for both arguments for "validate everything".
248
 */
Owen Taylor's avatar
Owen Taylor committed
249
static void
250
node_validate_rows (GtkFileSystemModel *model, guint up_to_index, guint up_to_row)
Owen Taylor's avatar
Owen Taylor committed
251
{
252
  guint i, row;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
253

254 255
  if (model->files->len == 0)
    return;
256 257 258 259 260 261

  up_to_index = MIN (up_to_index, model->files->len - 1);

  i = model->n_nodes_valid;
  if (i != 0)
    row = get_node (model, i - 1)->row;
262
  else
263 264 265
    row = 0;

  while (i <= up_to_index && row <= up_to_row)
266
    {
267
      FileModelNode *node = get_node (model, i);
268
      if (node->visible)
269 270 271
        row++;
      node->row = row;
      i++;
272
    }
273
  model->n_nodes_valid = i;
Owen Taylor's avatar
Owen Taylor committed
274 275
}

276
static guint
277
node_get_tree_row (GtkFileSystemModel *model, guint index)
Owen Taylor's avatar
Owen Taylor committed
278
{
279 280
  if (model->n_nodes_valid <= index)
    node_validate_rows (model, index, G_MAXUINT);
281

282
  return get_node (model, index)->row - 1;
Owen Taylor's avatar
Owen Taylor committed
283 284
}

285 286
static void 
node_invalidate_index (GtkFileSystemModel *model, guint id)
Owen Taylor's avatar
Owen Taylor committed
287
{
288
  model->n_nodes_valid = MIN (model->n_nodes_valid, id);
Owen Taylor's avatar
Owen Taylor committed
289 290
}

291
static GtkTreePath *
292
tree_path_new_from_node (GtkFileSystemModel *model, guint id)
Owen Taylor's avatar
Owen Taylor committed
293
{
294
  guint r = node_get_tree_row (model, id);
Owen Taylor's avatar
Owen Taylor committed
295

296
  g_assert (r < model->files->len);
297

298
  return gtk_tree_path_new_from_indices (r, -1);
299
}
Owen Taylor's avatar
Owen Taylor committed
300

301
static void
302 303 304 305 306
emit_row_inserted_for_node (GtkFileSystemModel *model, guint id)
{
  GtkTreePath *path;
  GtkTreeIter iter;

307
  path = tree_path_new_from_node (model, id);
308 309 310 311 312 313 314
  ITER_INIT_FROM_INDEX (model, &iter, id);
  gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
  gtk_tree_path_free (path);
}

static void
emit_row_changed_for_node (GtkFileSystemModel *model, guint id)
315 316 317
{
  GtkTreePath *path;
  GtkTreeIter iter;
318

319
  path = tree_path_new_from_node (model, id);
320 321 322 323 324 325
  ITER_INIT_FROM_INDEX (model, &iter, id);
  gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
  gtk_tree_path_free (path);
}

static void
326
emit_row_deleted_for_row (GtkFileSystemModel *model, guint row)
327 328 329
{
  GtkTreePath *path;

330
  path = gtk_tree_path_new_from_indices (row, -1);
331 332 333 334
  gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
  gtk_tree_path_free (path);
}

335
static void
336
node_set_visible_and_filtered_out (GtkFileSystemModel *model, guint id, gboolean visible, gboolean filtered_out)
337 338 339
{
  FileModelNode *node = get_node (model, id);

340
  /* Filteredness */
341

342 343 344 345 346 347
  if (node->filtered_out != filtered_out)
    {
      node->filtered_out = filtered_out;
      if (node->visible && visible)
        emit_row_changed_for_node (model, id);
    }
348

349 350
  /* Visibility */
  
351 352 353
  if (node->visible == visible ||
      node->frozen_add)
    return;
354

355
  if (visible)
Owen Taylor's avatar
Owen Taylor committed
356
    {
357 358
      node->visible = TRUE;
      node_invalidate_index (model, id);
359
      emit_row_inserted_for_node (model, id);
360 361 362
    }
  else
    {
363
      guint row;
364

365 366
      row = node_get_tree_row (model, id);
      g_assert (row < model->files->len);
367

368 369
      node->visible = FALSE;
      node_invalidate_index (model, id);
370
      emit_row_deleted_for_row (model, row);
Owen Taylor's avatar
Owen Taylor committed
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 379
  GtkFileFilterInfo filter_info = { 0, };
  GtkFileFilterFlags required;
380
  gboolean result;
381 382 383
  char *mime_type = NULL;
  char *filename = NULL;
  char *uri = NULL;
384

385
  if (node->info == NULL)
386
    return TRUE;
387

388
  if (model->filter == NULL)
389
    return FALSE;
390

391 392 393 394 395 396 397 398
  /* fill info */
  required = gtk_file_filter_get_needed (model->filter);

  filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME;
  filter_info.display_name = g_file_info_get_display_name (node->info);

  if (required & GTK_FILE_FILTER_MIME_TYPE)
    {
399 400 401 402 403 404 405 406 407 408
      const char *s = g_file_info_get_content_type (node->info);
      if (s)
	{
	  mime_type = g_content_type_get_mime_type (s);
	  if (mime_type)
	    {
	      filter_info.mime_type = mime_type;
	      filter_info.contains |= GTK_FILE_FILTER_MIME_TYPE;
	    }
	}
409 410 411 412 413
    }

  if (required & GTK_FILE_FILTER_FILENAME)
    {
      filename = g_file_get_path (node->file);
414
      if (filename)
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
        {
          filter_info.filename = filename;
	  filter_info.contains |= GTK_FILE_FILTER_FILENAME;
        }
    }

  if (required & GTK_FILE_FILTER_URI)
    {
      uri = g_file_get_uri (node->file);
      if (uri)
        {
          filter_info.uri = uri;
	  filter_info.contains |= GTK_FILE_FILTER_URI;
        }
    }

431
  result = !gtk_file_filter_filter (model->filter, &filter_info);
432 433 434 435 436 437

  g_free (mime_type);
  g_free (filename);
  g_free (uri);

  return result;
438 439
}

440
static gboolean
441
node_should_be_visible (GtkFileSystemModel *model, guint id, gboolean filtered_out)
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
{
  FileModelNode *node = get_node (model, id);
  gboolean result;

  if (node->info == NULL)
    return FALSE;

  if (!model->show_hidden &&
      (g_file_info_get_is_hidden (node->info) || g_file_info_get_is_backup (node->info)))
    return FALSE;

  if (_gtk_file_info_consider_as_directory (node->info))
    {
      if (!model->show_folders)
        return FALSE;

      if (!model->filter_folders)
        return TRUE;
    }
  else
    {
      if (!model->show_files)
        return FALSE;
    }

467
  result = !filtered_out;
468 469 470 471

  return result;
}

472 473 474 475 476 477 478 479 480 481 482 483
static void
node_compute_visibility_and_filters (GtkFileSystemModel *model, guint id)
{
  gboolean filtered_out;
  gboolean visible;

  filtered_out = node_should_be_filtered_out (model, id);
  visible = node_should_be_visible (model, id, filtered_out);

  node_set_visible_and_filtered_out (model, id, visible, filtered_out);
}

484
/*** GtkTreeModel ***/
Owen Taylor's avatar
Owen Taylor committed
485 486 487 488

static GtkTreeModelFlags
gtk_file_system_model_get_flags (GtkTreeModel *tree_model)
{
489 490
  /* GTK_TREE_MODEL_ITERS_PERSIST doesn't work with arrays :( */
  return GTK_TREE_MODEL_LIST_ONLY;
Owen Taylor's avatar
Owen Taylor committed
491 492 493 494 495
}

static gint
gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model)
{
496 497 498
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
  
  return model->n_columns;
Owen Taylor's avatar
Owen Taylor committed
499 500 501 502
}

static GType
gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
503
				       gint          i)
Owen Taylor's avatar
Owen Taylor committed
504
{
505 506 507 508 509
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
  
  g_return_val_if_fail (i >= 0 && (guint) i < model->n_columns, G_TYPE_NONE);

  return model->column_types[i];
Owen Taylor's avatar
Owen Taylor committed
510 511
}

512 513
static int
compare_indices (gconstpointer key, gconstpointer _node)
Owen Taylor's avatar
Owen Taylor committed
514
{
515 516
  const FileModelNode *node = _node;

517
  return GPOINTER_TO_UINT (key) - node->row;
518
}
Owen Taylor's avatar
Owen Taylor committed
519

520 521 522 523 524 525 526 527
static gboolean
gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
				      GtkTreeIter  *iter,
				      GtkTreeIter  *parent,
				      gint          n)
{
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
  char *node;
528 529
  guint id;
  guint row_to_find;
Owen Taylor's avatar
Owen Taylor committed
530

531
  g_return_val_if_fail (n >= 0, FALSE);
Owen Taylor's avatar
Owen Taylor committed
532

533
  if (parent != NULL)
Owen Taylor's avatar
Owen Taylor committed
534 535
    return FALSE;

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 539
  if (model->n_nodes_valid > 0 &&
      get_node (model, model->n_nodes_valid - 1)->row >= row_to_find)
Owen Taylor's avatar
Owen Taylor committed
540
    {
541 542 543 544 545
      /* Fast path - the nodes are valid up to the sought one.
       *
       * First, find a node with the sought row number...*/

      node = bsearch (GUINT_TO_POINTER (row_to_find), 
546
                      model->files->data,
547
                      model->n_nodes_valid,
548
                      model->node_size,
549 550 551 552
                      compare_indices);
      if (node == NULL)
        return FALSE;

553 554
      /* ... Second, back up until we find the first visible node with that row number */

555 556 557
      id = node_index (model, node);
      while (!get_node (model, id)->visible)
        id--;
558 559

      g_assert (get_node (model, id)->row == row_to_find);
560 561 562
    }
  else
    {
563 564
      /* Slow path - the nodes need to be validated up to the sought one */

565
      node_validate_rows (model, G_MAXUINT, n); /* note that this is really "n", not row_to_find - see node_validate_rows() */
566 567
      id = model->n_nodes_valid - 1;
      if (model->n_nodes_valid == 0 || get_node (model, id)->row != row_to_find)
568
        return FALSE;
Owen Taylor's avatar
Owen Taylor committed
569 570
    }

571
  ITER_INIT_FROM_INDEX (model, iter, id);
Owen Taylor's avatar
Owen Taylor committed
572 573 574
  return TRUE;
}

575 576 577 578 579 580 581
static gboolean
gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
				GtkTreeIter  *iter,
				GtkTreePath  *path)
{
  g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);

582 583 584
  if (gtk_tree_path_get_depth (path) > 1)
    return FALSE;

585 586 587
  return gtk_file_system_model_iter_nth_child (tree_model, 
                                               iter,
                                               NULL, 
588
                                               gtk_tree_path_get_indices (path)[0]);
589 590
}

Owen Taylor's avatar
Owen Taylor committed
591 592 593 594 595 596
static GtkTreePath *
gtk_file_system_model_get_path (GtkTreeModel *tree_model,
				GtkTreeIter  *iter)
{
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
      
597
  g_return_val_if_fail (ITER_IS_VALID (model, iter), NULL);
Owen Taylor's avatar
Owen Taylor committed
598

599
  return tree_path_new_from_node (model, ITER_INDEX (iter));
Owen Taylor's avatar
Owen Taylor committed
600 601 602 603 604 605 606 607 608
}

static void
gtk_file_system_model_get_value (GtkTreeModel *tree_model,
				 GtkTreeIter  *iter,
				 gint          column,
				 GValue       *value)
{
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
609
  const GValue *original;
Owen Taylor's avatar
Owen Taylor committed
610
  
611 612
  g_return_if_fail ((guint) column < model->n_columns);
  g_return_if_fail (ITER_IS_VALID (model, iter));
613

614 615 616 617 618
  original = _gtk_file_system_model_get_value (model, iter, column);
  if (original)
    {
      g_value_init (value, G_VALUE_TYPE (original));
      g_value_copy (original, value);
Owen Taylor's avatar
Owen Taylor committed
619
    }
620 621
  else
    g_value_init (value, model->column_types[column]);
Owen Taylor's avatar
Owen Taylor committed
622 623 624 625 626 627
}

static gboolean
gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
				 GtkTreeIter  *iter)
{
628 629
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
  guint i;
Owen Taylor's avatar
Owen Taylor committed
630

631 632 633 634 635
  g_return_val_if_fail (ITER_IS_VALID (model, iter), FALSE);

  for (i = ITER_INDEX (iter) + 1; i < model->files->len; i++) 
    {
      FileModelNode *node = get_node (model, i);
Owen Taylor's avatar
Owen Taylor committed
636

637 638 639 640 641 642 643 644
      if (node->visible)
        {
          ITER_INIT_FROM_INDEX (model, iter, i);
          return TRUE;
        }
    }
      
  return FALSE;
Owen Taylor's avatar
Owen Taylor committed
645 646 647 648 649 650 651
}

static gboolean
gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
				     GtkTreeIter  *iter,
				     GtkTreeIter  *parent)
{
652
  return FALSE;
Owen Taylor's avatar
Owen Taylor committed
653 654 655 656 657 658
}

static gboolean
gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
				      GtkTreeIter  *iter)
{
659
  return FALSE;
Owen Taylor's avatar
Owen Taylor committed
660 661 662 663 664 665 666 667 668
}

static gint
gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
				       GtkTreeIter  *iter)
{
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);

  if (iter)
669
    return 0;
Owen Taylor's avatar
Owen Taylor committed
670

671
  return node_get_tree_row (model, model->files->len - 1) + 1;
Owen Taylor's avatar
Owen Taylor committed
672 673 674 675 676 677 678
}

static gboolean
gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
				   GtkTreeIter  *iter,
				   GtkTreeIter  *child)
{
679
  return FALSE;
Owen Taylor's avatar
Owen Taylor committed
680 681 682 683 684 685
}

static void
gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
				GtkTreeIter  *iter)
{
686
  /* nothing to do */
Owen Taylor's avatar
Owen Taylor committed
687 688 689 690 691 692
}

static void
gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
				  GtkTreeIter  *iter)
{
693
  /* nothing to do */
Owen Taylor's avatar
Owen Taylor committed
694 695
}

696 697
static void
gtk_file_system_model_iface_init (GtkTreeModelIface *iface)
698
{
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
  iface->get_flags =       gtk_file_system_model_get_flags;
  iface->get_n_columns =   gtk_file_system_model_get_n_columns;
  iface->get_column_type = gtk_file_system_model_get_column_type;
  iface->get_iter =        gtk_file_system_model_get_iter;
  iface->get_path =        gtk_file_system_model_get_path;
  iface->get_value =       gtk_file_system_model_get_value;
  iface->iter_next =       gtk_file_system_model_iter_next;
  iface->iter_children =   gtk_file_system_model_iter_children;
  iface->iter_has_child =  gtk_file_system_model_iter_has_child;
  iface->iter_n_children = gtk_file_system_model_iter_n_children;
  iface->iter_nth_child =  gtk_file_system_model_iter_nth_child;
  iface->iter_parent =     gtk_file_system_model_iter_parent;
  iface->ref_node =        gtk_file_system_model_ref_node;
  iface->unref_node =      gtk_file_system_model_unref_node;
}
714

715
/*** GtkTreeSortable ***/
716

717 718 719 720 721 722 723
typedef struct _SortData SortData;
struct _SortData {
  GtkFileSystemModel *    model;
  GtkTreeIterCompareFunc  func;
  gpointer                data;
  int                     order;        /* -1 to invert sort order or 1 to keep it */
};
724

725 726 727 728 729
/* returns FALSE if no sort necessary */
static gboolean
sort_data_init (SortData *data, GtkFileSystemModel *model)
{
  GtkTreeDataSortHeader *header;
730

731
  if (model->files->len <= 2)
732 733
    return FALSE;

734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
  switch (model->sort_column_id)
    {
    case GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID:
      if (!model->default_sort_func)
        return FALSE;
      data->func = model->default_sort_func;
      data->data = model->default_sort_data;
      break;
    case GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID:
      return FALSE;
    default:
      header = _gtk_tree_data_list_get_header (model->sort_list, model->sort_column_id);
      if (header == NULL)
        return FALSE;
      data->func = header->func;
      data->data = header->data;
      break;
    }
752

753 754
  data->order = model->sort_order == GTK_SORT_DESCENDING ? -1 : 1;
  data->model = model;
755 756 757
  return TRUE;
}

758 759
static int
compare_array_element (gconstpointer a, gconstpointer b, gpointer user_data)
Federico Mena Quintero's avatar
Federico Mena Quintero committed
760
{
761 762 763 764 765 766
  SortData *data = user_data;
  GtkTreeIter itera, iterb;

  ITER_INIT_FROM_INDEX (data->model, &itera, node_index (data->model, a));
  ITER_INIT_FROM_INDEX (data->model, &iterb, node_index (data->model, b));
  return data->func (GTK_TREE_MODEL (data->model), &itera, &iterb, data->data) * data->order;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
767 768
}

769
static void
770
gtk_file_system_model_sort (GtkFileSystemModel *model)
Federico Mena Quintero's avatar
Federico Mena Quintero committed
771
{
772
  SortData data;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
773

774 775 776 777 778
  if (model->frozen)
    {
      model->sort_on_thaw = TRUE;
      return;
    }
779

780 781 782
  if (sort_data_init (&data, model))
    {
      GtkTreePath *path;
783 784
      guint i;
      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 794
                         compare_array_element,
                         &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 803 804 805 806
          for (i = 0; i < model->files->len; i++)
            {
              FileModelNode *node = get_node (model, i);
              if (!node->visible)
                {
807
                  node->row = r;
808 809 810
                  continue;
                }

811
              new_order[r] = node->row - 1;
812 813
              r++;
              node->row = r;
814
            }
815
          g_assert (r == n_visible_rows);
816 817 818 819 820 821 822 823 824
          path = gtk_tree_path_new ();
          gtk_tree_model_rows_reordered (GTK_TREE_MODEL (model),
                                         path,
                                         NULL,
                                         new_order);
          gtk_tree_path_free (path);
          g_free (new_order);
        }
    }
825

826
  model->sort_on_thaw = FALSE;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
827 828
}

829 830
static void
gtk_file_system_model_sort_node (GtkFileSystemModel *model, guint node)
Owen Taylor's avatar
Owen Taylor committed
831
{
832 833 834
  /* FIXME: improve */
  gtk_file_system_model_sort (model);
}
835

836 837 838 839 840 841
static gboolean
gtk_file_system_model_get_sort_column_id (GtkTreeSortable  *sortable,
                                          gint             *sort_column_id,
                                          GtkSortType      *order)
{
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
842

843 844 845 846
  if (sort_column_id)
    *sort_column_id = model->sort_column_id;
  if (order)
    *order = model->sort_order;
Owen Taylor's avatar
Owen Taylor committed
847

848 849 850
  if (model->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
      model->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
    return FALSE;
851

852
  return TRUE;
Owen Taylor's avatar
Owen Taylor committed
853 854 855
}

static void
856 857 858
gtk_file_system_model_set_sort_column_id (GtkTreeSortable  *sortable,
                                          gint              sort_column_id,
                                          GtkSortType       order)
Owen Taylor's avatar
Owen Taylor committed
859
{
860
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
Owen Taylor's avatar
Owen Taylor committed
861

862 863
  if ((model->sort_column_id == sort_column_id) &&
      (model->sort_order == order))
Owen Taylor's avatar
Owen Taylor committed
864 865
    return;

866
  if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
Owen Taylor's avatar
Owen Taylor committed
867
    {
868
      if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
Owen Taylor's avatar
Owen Taylor committed
869
	{
870
	  GtkTreeDataSortHeader *header = NULL;
Owen Taylor's avatar
Owen Taylor committed
871

872 873
	  header = _gtk_tree_data_list_get_header (model->sort_list, 
						   sort_column_id);
Owen Taylor's avatar
Owen Taylor committed
874

875 876 877
	  /* We want to make sure that we have a function */
	  g_return_if_fail (header != NULL);
	  g_return_if_fail (header->func != NULL);
Owen Taylor's avatar
Owen Taylor committed
878 879 880
	}
      else
	{
881
	  g_return_if_fail (model->default_sort_func != NULL);
Owen Taylor's avatar
Owen Taylor committed
882 883 884
	}
    }

885 886 887 888 889 890 891

  model->sort_column_id = sort_column_id;
  model->sort_order = order;

  gtk_tree_sortable_sort_column_changed (sortable);

  gtk_file_system_model_sort (model);
Owen Taylor's avatar
Owen Taylor committed
892 893
}

Owen Taylor's avatar
Owen Taylor committed
894
static void
895 896 897 898 899
gtk_file_system_model_set_sort_func (GtkTreeSortable        *sortable,
                                     gint                    sort_column_id,
                                     GtkTreeIterCompareFunc  func,
                                     gpointer                data,
                                     GDestroyNotify          destroy)
Owen Taylor's avatar
Owen Taylor committed
900
{
901 902 903 904 905
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);

  model->sort_list = _gtk_tree_data_list_set_header (model->sort_list, 
                                                     sort_column_id, 
                                                     func, data, destroy);
Owen Taylor's avatar
Owen Taylor committed
906

907 908
  if (model->sort_column_id == sort_column_id)
    gtk_file_system_model_sort (model);
Owen Taylor's avatar
Owen Taylor committed
909 910
}

911 912 913 914 915
static void
gtk_file_system_model_set_default_sort_func (GtkTreeSortable        *sortable,
                                             GtkTreeIterCompareFunc  func,
                                             gpointer                data,
                                             GDestroyNotify          destroy)
Owen Taylor's avatar
Owen Taylor committed
916
{
917
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
Owen Taylor's avatar
Owen Taylor committed
918

919
  if (model->default_sort_destroy)
Owen Taylor's avatar
Owen Taylor committed
920
    {
921 922 923 924
      GDestroyNotify d = model->default_sort_destroy;

      model->default_sort_destroy = NULL;
      d (model->default_sort_data);
Owen Taylor's avatar
Owen Taylor committed
925 926
    }

927 928 929
  model->default_sort_func = func;
  model->default_sort_data = data;
  model->default_sort_destroy = destroy;
Owen Taylor's avatar
Owen Taylor committed
930

931 932
  if (model->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
    gtk_file_system_model_sort (model);
Owen Taylor's avatar
Owen Taylor committed
933 934
}

935 936
static gboolean
gtk_file_system_model_has_default_sort_func (GtkTreeSortable *sortable)
Owen Taylor's avatar
Owen Taylor committed
937
{
938
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
Owen Taylor's avatar
Owen Taylor committed
939

940
  return (model->default_sort_func != NULL);
Owen Taylor's avatar
Owen Taylor committed
941 942
}

943 944
static void
gtk_file_system_model_sortable_init (GtkTreeSortableIface *iface)
Owen Taylor's avatar
Owen Taylor committed
945
{
946 947 948 949 950
  iface->get_sort_column_id = gtk_file_system_model_get_sort_column_id;
  iface->set_sort_column_id = gtk_file_system_model_set_sort_column_id;
  iface->set_sort_func = gtk_file_system_model_set_sort_func;
  iface->set_default_sort_func = gtk_file_system_model_set_default_sort_func;
  iface->has_default_sort_func = gtk_file_system_model_has_default_sort_func;
Owen Taylor's avatar
Owen Taylor committed
951 952
}

953 954 955 956 957
/*** GtkTreeDragSource ***/

static gboolean
drag_source_row_draggable (GtkTreeDragSource *drag_source,
			   GtkTreePath       *path)
Owen Taylor's avatar
Owen Taylor committed
958
{
959 960
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (drag_source);
  GtkTreeIter iter;
Owen Taylor's avatar
Owen Taylor committed
961

962 963
  if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
    return FALSE;
964

965
  return ITER_INDEX (&iter) != 0;
Owen Taylor's avatar
Owen Taylor committed
966 967
}

968 969 970 971
static gboolean
drag_source_drag_data_get (GtkTreeDragSource *drag_source,
			   GtkTreePath       *path,
			   GtkSelectionData  *selection_data)
Owen Taylor's avatar
Owen Taylor committed
972
{
973 974 975 976
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (drag_source);
  FileModelNode *node;
  GtkTreeIter iter;
  char *uris[2]; 
Owen Taylor's avatar
Owen Taylor committed
977

978 979
  if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
    return FALSE;
Owen Taylor's avatar
Owen Taylor committed
980

981 982 983
  node = get_node (model, ITER_INDEX (&iter));
  if (node->file == NULL)
    return FALSE;
Owen Taylor's avatar
Owen Taylor committed
984

985 986 987 988
  uris[0] = g_file_get_uri (node->file);
  uris[1] = NULL;
  gtk_selection_data_set_uris (selection_data, uris);
  g_free (uris[0]);
Owen Taylor's avatar
Owen Taylor committed
989

990
  return TRUE;
Owen Taylor's avatar
Owen Taylor committed
991
}
Owen Taylor's avatar
Owen Taylor committed
992

993 994
static void
drag_source_iface_init (GtkTreeDragSourceIface *iface)
Owen Taylor's avatar
Owen Taylor committed
995
{
996 997 998
  iface->row_draggable = drag_source_row_draggable;
  iface->drag_data_get = drag_source_drag_data_get;
  iface->drag_data_delete = NULL;
Owen Taylor's avatar
Owen Taylor committed
999 1000
}

1001
/*** GtkFileSystemModel ***/
1002

1003 1004 1005 1006
/* Signal IDs */
enum {
  FINISHED_LOADING,
  LAST_SIGNAL
1007 1008
};

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
static guint file_system_model_signals[LAST_SIGNAL] = { 0 };



G_DEFINE_TYPE_WITH_CODE (GtkFileSystemModel, _gtk_file_system_model, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
						gtk_file_system_model_iface_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
						gtk_file_system_model_sortable_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
						drag_source_iface_init))

1021
static void
1022
gtk_file_system_model_dispose (GObject *object)
1023
{
1024 1025
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);

1026 1027 1028 1029 1030 1031
  if (model->dir_thaw_source)
    {
      g_source_remove (model->dir_thaw_source);
      model->dir_thaw_source = 0;
    }

1032 1033 1034
  g_cancellable_cancel (model->cancellable);
  if (model->dir_monitor)
    g_file_monitor_cancel (model->dir_monitor);
1035

1036 1037
  G_OBJECT_CLASS (_gtk_file_system_model_parent_class)->dispose (object);
}
1038 1039


1040 1041 1042 1043 1044
static void
gtk_file_system_model_finalize (GObject *object)
{
  GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
  guint i;
1045

1046
  for (i = 0; i < model->files->len; i++)
1047
    {
1048 1049
      int v;

1050 1051 1052 1053 1054
      FileModelNode *node = get_node (model, i);
      if (node->file)
        g_object_unref (node->file);
      if (node->info)
        g_object_unref (node->info);
1055

1056 1057
      for (v = 0; v < model->n_columns; v++)
	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 1063 1064 1065 1066 1067 1068
  g_object_unref (model->cancellable);
  g_free (model->attributes);
  if (model->dir)
    g_object_unref (model->dir);
  if (model->dir_monitor)
    g_object_unref (model->dir_monitor);
  g_hash_table_destroy (model->file_lookup);
1069 1070
  if (model->filter)
    g_object_unref (model->filter);
1071

1072
  g_slice_free1 (sizeof (GType) * model->n_columns, model->column_types);
1073

1074 1075 1076
  _gtk_tree_data_list_header_free (model->sort_list);
  if (model->default_sort_destroy)
    model->default_sort_destroy (model->default_sort_data);
1077

1078 1079
  G_OBJECT_CLASS (_gtk_file_system_model_parent_class)->finalize (object);
}
1080

1081 1082 1083 1084
static void
_gtk_file_system_model_class_init (GtkFileSystemModelClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1085

1086 1087
  gobject_class->finalize = gtk_file_system_model_finalize;
  gobject_class->dispose = gtk_file_system_model_dispose;
1088

1089 1090 1091 1092 1093 1094 1095 1096 1097
  file_system_model_signals[FINISHED_LOADING] =
    g_signal_new (I_("finished-loading"),
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkFileSystemModelClass, finished_loading),
		  NULL, NULL,
		  _gtk_marshal_VOID__POINTER,
		  G_TYPE_NONE, 1, G_TYPE_POINTER);
}
1098

1099 1100 1101 1102 1103 1104
static void
_gtk_file_system_model_init (GtkFileSystemModel *model)
{
  model->show_files = TRUE;
  model->show_folders = TRUE;
  model->show_hidden = FALSE;
1105
  model->filter_folders = FALSE;
1106

1107
  model->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
1108

1109 1110 1111
  model->file_lookup = g_hash_table_new (g_file_hash, (GEqualFunc) g_file_equal);
  model->cancellable = g_cancellable_new ();
}
1112

1113
/*** API ***/
1114

1115 1116 1117 1118 1119
static void
gtk_file_system_model_closed_enumerator (GObject *object, GAsyncResult *res, gpointer data)
{
  g_file_enumerator_close_finish (G_FILE_ENUMERATOR (object), res, NULL);
}
1120

1121 1122 1123 1124
static gboolean
thaw_func (gpointer data)
{
  GtkFileSystemModel *model = data;
1125

1126
  thaw_updates (model);
1127
  model->dir_thaw_source = 0;
1128

1129
  return FALSE;
1130 1131
}

1132 1133
static void
gtk_file_system_model_got_files (GObject *object, GAsyncResult *res, gpointer data)
Owen Taylor's avatar
Owen Taylor committed
1134
{
1135 1136 1137 1138
  GFileEnumerator *enumerator = G_FILE_ENUMERATOR (object);
  GtkFileSystemModel *model = data;
  GList *walk, *files;
  GError *error = NULL;
Owen Taylor's avatar
Owen Taylor committed
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 1150 1151 1152 1153
          model->dir_thaw_source = gdk_threads_add_timeout_full (IO_PRIORITY + 1,
                                                                 50,
                                                                 thaw_func,
                                                                 model,
                                                                 NULL);
1154
          g_source_set_name_by_id (model->dir_thaw_source, "[gtk+] thaw_func");
1155
        }
Owen Taylor's avatar
Owen Taylor committed
1156

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
      for (walk = files; walk; walk = walk->next)
        {
          const char *name;
          GFileInfo *info;
          GFile *file;
          
          info = walk->data;
          name = g_file_info_get_name (info);
          if (name == NULL)
            {
              /* Shouldn't happen, but the APIs allow it */
              g_object_unref (info);
              continue;
            }
          file = g_file_get_child (model->dir, name);
1172
          add_file (model, file, info);
1173 1174 1175 1176
          g_object_unref (file);
          g_object_unref (info);
        }
      g_list_free (files);
1177

1178 1179 1180 1181 1182 1183 1184 1185
      g_file_enumerator_next_files_async (enumerator,
					  g_file_is_native (model->dir) ? 50 * FILES_PER_QUERY : FILES_PER_QUERY,
					  IO_PRIORITY,
					  model->cancellable,
					  gtk_file_system_model_got_files,
					  model);
    }
  else
1186
    {
1187
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
1188
        {
1189 1190 1191 1192 1193 1194 1195 1196 1197
          g_file_enumerator_close_async (enumerator,
                                         IO_PRIORITY,
                                         model->cancellable,
                                         gtk_file_system_model_closed_enumerator,
                                         NULL);
          if (model->dir_thaw_source != 0)
            {
              g_source_remove (model->dir_thaw_source);
              model->dir_thaw_source = 0;
1198
              thaw_updates (model);
1199
            }
1200

1201 1202
          g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0, error);
        }
1203

1204 1205
      if (error)
        g_error_free (error);
1206
    }
Owen Taylor's avatar
Owen Taylor committed
1207

1208
  gdk_threads_leave ();
Owen Taylor's avatar
Owen Taylor committed
1209 1210 1211
}

static void
1212 1213 1214
gtk_file_system_model_query_done (GObject *     object,
                                  GAsyncResult *res,
                                  gpointer      data)
Owen Taylor's avatar
Owen Taylor committed
1215
{
1216 1217 1218
  GtkFileSystemModel *model = data; /* only a valid pointer if not cancelled */
  GFile *file = G_FILE (object);
  GFileInfo *info;
1219
  guint id;
Owen Taylor's avatar
Owen Taylor committed
1220

1221 1222 1223
  info = g_file_query_info_finish (file, res, NULL);
  if (info == NULL)
    return;
Owen Taylor's avatar
Owen Taylor committed
1224

1225
  gdk_threads_enter ();
1226 1227 1228 1229 1230 1231

  _gtk_file_system_model_update_file (model, file, info);

  id = node_get_for_file (model, file);
  gtk_file_system_model_sort_node (model, id);

1232
  gdk_threads_leave ();
Owen Taylor's avatar
Owen Taylor committed
1233 1234
}

1235 1236 1237 1238 1239 1240
static void
gtk_file_system_model_monitor_change (GFileMonitor *      monitor,
                                      GFile *             file,
                                      GFile *             other_file,
                                      GFileMonitorEvent   type,
                                      GtkFileSystemModel *model)
Owen Taylor's avatar
Owen Taylor committed
1241
{
1242
  switch (type)
Owen Taylor's avatar
Owen Taylor committed
1243
    {
1244 1245 1246 1247 1248 1249
      case G_FILE_MONITOR_EVENT_CREATED:
      case G_FILE_MONITOR_EVENT_CHANGED:
      case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
        /* We can treat all of these the same way */
        g_file_query_info_async (file,
                                 model->attributes,
1250
                                 G_FILE_QUERY_INFO_NONE,
1251 1252 1253 1254 1255 1256
                                 IO_PRIORITY,
                                 model->cancellable,
                                 gtk_file_system_model_query_done,
                                 model);
        break;
      case G_FILE_MONITOR_EVENT_DELETED:
1257
	gdk_threads_enter ();
1258
        remove_file (model, file);
1259
	gdk_threads_leave ();
1260 1261 1262 1263 1264 1265 1266 1267
        break;
      case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
        /* FIXME: use freeze/thaw with this somehow? */
      case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
      case G_FILE_MONITOR_EVENT_UNMOUNTED:
      default:
        /* ignore these */
        break;
Owen Taylor's avatar
Owen Taylor committed
1268 1269 1270
    }
}

1271 1272
static void
gtk_file_system_model_got_enumerator (GObject *dir, GAsyncResult *res, gpointer data)
Owen Taylor's avatar
Owen Taylor committed
1273
{
1274 1275 1276
  GtkFileSystemModel *model = data;
  GFileEnumerator *enumerator;
  GError *error = NULL;
1277

1278
  gdk_threads_enter ();
Owen Taylor's avatar
Owen Taylor committed
1279

1280 1281
  enumerator = g_file_enumerate_children_finish (G_FILE (dir), res, &error);
  if (enumerator == NULL)
Owen Taylor's avatar
Owen Taylor committed
1282
    {
1283 1284 1285 1286 1287
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
      {
        g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0, error);
        g_error_free (error);
      }
Owen Taylor's avatar
Owen Taylor committed
1288
    }
1289
  else
1290
    {
1291 1292 1293 1294 1295 1296 1297 1298
      g_file_enumerator_next_files_async (enumerator,
                                          g_file_is_native (model->dir) ? 50 * FILES_PER_QUERY : FILES_PER_QUERY,
                                          IO_PRIORITY,
                                          model->cancellable,
                                          gtk_file_system_model_got_files,
                                          model);
      g_object_unref (enumerator);
      model->dir_monitor = g_file_monitor_directory (model->dir,
1299
                                                     G_FILE_MONITOR_NONE,
1300
                                                     model->cancellable,
1301
                                                     NULL); /* we don't mind if directory monitoring isn't supported, so the GError is NULL here */
1302 1303 1304 1305 1306
      if (model->dir_monitor)
        g_signal_connect (model->dir_monitor,
                          "changed",
                          G_CALLBACK (gtk_file_system_model_monitor_change),
                          model);
1307
    }
1308 1309

  gdk_threads_leave ();
Owen Taylor's avatar
Owen Taylor committed
1310 1311 1312
}

static void
1313 1314 1315
gtk_file_system_model_set_n_columns (GtkFileSystemModel *model,
                                     gint                n_columns,
                                     va_list             args)
Owen Taylor's avatar