nautilus-list-model.c 41.3 KB
Newer Older
Anders Carlsson's avatar
Anders Carlsson committed
1 2 3 4
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* fm-list-model.h - a GtkTreeModel for file lists. 

5 6
   Copyright (C) 2001, 2002 Anders Carlsson
   Copyright (C) 2003, Soeren Sandmann
7
   Copyright (C) 2004, Novell, Inc.
Anders Carlsson's avatar
Anders Carlsson committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

24
   Authors: Anders Carlsson <andersca@gnu.org>, Soeren Sandmann (sandmann@daimi.au.dk), Dave Camp <dave@ximian.com>
Anders Carlsson's avatar
Anders Carlsson committed
25 26 27
*/

#include <config.h>
28 29

#include "nautilus-list-model.h"
Anders Carlsson's avatar
Anders Carlsson committed
30 31

#include <string.h>
32
#include <glib.h>
33
#include <glib/gi18n.h>
34 35
#include <gtk/gtk.h>

36
#include <eel/eel-graphic-effects.h>
Dave Camp's avatar
Dave Camp committed
37
#include <libnautilus-private/nautilus-dnd.h>
Anders Carlsson's avatar
Anders Carlsson committed
38

39 40 41 42 43
enum {
	SUBDIRECTORY_UNLOADED,
	LAST_SIGNAL
};

44 45 46 47
static GQuark attribute_name_q,
	attribute_modification_date_q,
	attribute_date_modified_q;

48 49 50
/* msec delay after Loading... dummy row turns into (empty) */
#define LOADING_TO_EMPTY_DELAY 100

51 52
static guint list_model_signals[LAST_SIGNAL] = { 0 };

53 54 55 56 57
static int nautilus_list_model_file_entry_compare_func (gconstpointer a,
							gconstpointer b,
							gpointer      user_data);
static void nautilus_list_model_tree_model_init (GtkTreeModelIface *iface);
static void nautilus_list_model_sortable_init (GtkTreeSortableIface *iface);
Anders Carlsson's avatar
Anders Carlsson committed
58

59
struct NautilusListModelDetails {
60
	GSequence *files;
61 62
	GHashTable *directory_reverse_map; /* map from directory to GSequenceIter's */
	GHashTable *top_reverse_map;	   /* map from files in top dir to GSequenceIter's */
Anders Carlsson's avatar
Anders Carlsson committed
63 64 65

	int stamp;

66
	GQuark sort_attribute;
Anders Carlsson's avatar
Anders Carlsson committed
67 68 69
	GtkSortType order;

	gboolean sort_directories_first;
Dave Camp's avatar
Dave Camp committed
70 71 72 73

	GtkTreeView *drag_view;
	int drag_begin_x;
	int drag_begin_y;
74 75

	GPtrArray *columns;
76 77

	GList *highlight_files;
Anders Carlsson's avatar
Anders Carlsson committed
78 79
};

Dave Camp's avatar
Dave Camp committed
80
typedef struct {
81
	NautilusListModel *model;
Dave Camp's avatar
Dave Camp committed
82 83 84 85
	
	GList *path_list;
} DragDataGetInfo;

Alexander Larsson's avatar
Alexander Larsson committed
86 87 88 89
typedef struct FileEntry FileEntry;

struct FileEntry {
	NautilusFile *file;
90
	GHashTable *reverse_map;	/* map from files to GSequenceIter's */
91
	NautilusDirectory *subdirectory;
Alexander Larsson's avatar
Alexander Larsson committed
92 93
	FileEntry *parent;
	GSequence *files;
94
	GSequenceIter *ptr;
Alexander Larsson's avatar
Alexander Larsson committed
95 96 97
	guint loaded : 1;
};

98
G_DEFINE_TYPE_WITH_CODE (NautilusListModel, nautilus_list_model, G_TYPE_OBJECT,
99
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
100
						nautilus_list_model_tree_model_init)
101
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
102
						nautilus_list_model_sortable_init));
103

104
static const GtkTargetEntry drag_types [] = {
Dave Camp's avatar
Dave Camp committed
105 106 107 108
	{ NAUTILUS_ICON_DND_GNOME_ICON_LIST_TYPE, 0, NAUTILUS_ICON_DND_GNOME_ICON_LIST },
	{ NAUTILUS_ICON_DND_URI_LIST_TYPE, 0, NAUTILUS_ICON_DND_URI_LIST },
};

Alexander Larsson's avatar
Alexander Larsson committed
109 110 111 112
static void
file_entry_free (FileEntry *file_entry)
{
	nautilus_file_unref (file_entry->file);
113 114 115 116
	if (file_entry->reverse_map) {
		g_hash_table_destroy (file_entry->reverse_map);
		file_entry->reverse_map = NULL;
	}
117 118 119
	if (file_entry->subdirectory != NULL) {
		nautilus_directory_unref (file_entry->subdirectory);
	}
Alexander Larsson's avatar
Alexander Larsson committed
120 121 122 123 124 125
	if (file_entry->files != NULL) {
		g_sequence_free (file_entry->files);
	}
	g_free (file_entry);
}

126
static GtkTreeModelFlags
127
nautilus_list_model_get_flags (GtkTreeModel *tree_model)
Anders Carlsson's avatar
Anders Carlsson committed
128
{
Alexander Larsson's avatar
Alexander Larsson committed
129
	return GTK_TREE_MODEL_ITERS_PERSIST;
Anders Carlsson's avatar
Anders Carlsson committed
130 131 132
}

static int
133
nautilus_list_model_get_n_columns (GtkTreeModel *tree_model)
Anders Carlsson's avatar
Anders Carlsson committed
134
{
135
	return NAUTILUS_LIST_MODEL_NUM_COLUMNS + NAUTILUS_LIST_MODEL (tree_model)->details->columns->len;
Anders Carlsson's avatar
Anders Carlsson committed
136 137 138
}

static GType
139
nautilus_list_model_get_column_type (GtkTreeModel *tree_model, int index)
Anders Carlsson's avatar
Anders Carlsson committed
140 141
{
	switch (index) {
142
	case NAUTILUS_LIST_MODEL_FILE_COLUMN:
Anders Carlsson's avatar
Anders Carlsson committed
143
		return NAUTILUS_TYPE_FILE;
144
	case NAUTILUS_LIST_MODEL_SUBDIRECTORY_COLUMN:
145
		return NAUTILUS_TYPE_DIRECTORY;
146 147 148 149 150 151 152
	case NAUTILUS_LIST_MODEL_SMALLEST_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_SMALLER_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_SMALL_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_STANDARD_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_LARGE_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_LARGER_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_LARGEST_ICON_COLUMN:
Anders Carlsson's avatar
Anders Carlsson committed
153
		return GDK_TYPE_PIXBUF;
154
	case NAUTILUS_LIST_MODEL_FILE_NAME_IS_EDITABLE_COLUMN:
155
		return G_TYPE_BOOLEAN;
Anders Carlsson's avatar
Anders Carlsson committed
156
	default:
157
		if (index < NAUTILUS_LIST_MODEL_NUM_COLUMNS + NAUTILUS_LIST_MODEL (tree_model)->details->columns->len) {
158 159 160 161
			return G_TYPE_STRING;
		} else {
			return G_TYPE_INVALID;
		}
Anders Carlsson's avatar
Anders Carlsson committed
162 163 164
	}
}

165
static void
166
nautilus_list_model_ptr_to_iter (NautilusListModel *model, GSequenceIter *ptr, GtkTreeIter *iter)
167
{
168
	g_assert (!g_sequence_iter_is_end (ptr));
169 170 171 172 173 174
	if (iter != NULL) {
		iter->stamp = model->details->stamp;
		iter->user_data = ptr;
	}
}

Anders Carlsson's avatar
Anders Carlsson committed
175
static gboolean
176
nautilus_list_model_get_iter (GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreePath *path)
Anders Carlsson's avatar
Anders Carlsson committed
177
{
178
	NautilusListModel *model;
Alexander Larsson's avatar
Alexander Larsson committed
179
	GSequence *files;
180
	GSequenceIter *ptr;
Alexander Larsson's avatar
Alexander Larsson committed
181 182
	FileEntry *file_entry;
	int i, d;
Anders Carlsson's avatar
Anders Carlsson committed
183
	
184
	model = (NautilusListModel *)tree_model;
Alexander Larsson's avatar
Alexander Larsson committed
185 186 187 188 189
	ptr = NULL;
	
	files = model->details->files;
	for (d = 0; d < gtk_tree_path_get_depth (path); d++) {
		i = gtk_tree_path_get_indices (path)[d];
Anders Carlsson's avatar
Anders Carlsson committed
190

Alexander Larsson's avatar
Alexander Larsson committed
191 192 193
		if (files == NULL || i >= g_sequence_get_length (files)) {
			return FALSE;
		}
Anders Carlsson's avatar
Anders Carlsson committed
194

195 196
		ptr = g_sequence_get_iter_at_pos (files, i);
		file_entry = g_sequence_get (ptr);
Alexander Larsson's avatar
Alexander Larsson committed
197
		files = file_entry->files;
Anders Carlsson's avatar
Anders Carlsson committed
198 199
	}

200
	nautilus_list_model_ptr_to_iter (model, ptr, iter);
201
	
Anders Carlsson's avatar
Anders Carlsson committed
202 203 204 205
	return TRUE;
}

static GtkTreePath *
206
nautilus_list_model_get_path (GtkTreeModel *tree_model, GtkTreeIter *iter)
Anders Carlsson's avatar
Anders Carlsson committed
207 208
{
	GtkTreePath *path;
209
	NautilusListModel *model;
210
	GSequenceIter *ptr;
Alexander Larsson's avatar
Alexander Larsson committed
211 212
	FileEntry *file_entry;

Anders Carlsson's avatar
Anders Carlsson committed
213

214
	model = (NautilusListModel *)tree_model;
Anders Carlsson's avatar
Anders Carlsson committed
215 216 217
	
	g_return_val_if_fail (iter->stamp == model->details->stamp, NULL);

218
	if (g_sequence_iter_is_end (iter->user_data)) {
219
		/* FIXME is this right? */
Anders Carlsson's avatar
Anders Carlsson committed
220 221 222
		return NULL;
	}
	
223
	path = gtk_tree_path_new ();
Alexander Larsson's avatar
Alexander Larsson committed
224 225
	ptr = iter->user_data;
	while (ptr != NULL) {
226 227
		gtk_tree_path_prepend_index (path, g_sequence_iter_get_position (ptr));
		file_entry = g_sequence_get (ptr);
Alexander Larsson's avatar
Alexander Larsson committed
228 229 230 231 232 233
		if (file_entry->parent != NULL) {
			ptr = file_entry->parent->ptr;
		} else {
			ptr = NULL;
		}
	}
234

Anders Carlsson's avatar
Anders Carlsson committed
235 236 237 238
	return path;
}

static void
239
nautilus_list_model_get_value (GtkTreeModel *tree_model, GtkTreeIter *iter, int column, GValue *value)
Anders Carlsson's avatar
Anders Carlsson committed
240
{
241
	NautilusListModel *model;
Alexander Larsson's avatar
Alexander Larsson committed
242
	FileEntry *file_entry;
Anders Carlsson's avatar
Anders Carlsson committed
243 244
	NautilusFile *file;
	char *str;
245
	GdkPixbuf *icon, *rendered_icon;
246 247 248 249
	GIcon *gicon, *emblemed_icon, *emblem_icon;
	NautilusIconInfo *icon_info;
	GEmblem *emblem;
	GList *emblem_icons, *l;
250 251
	int icon_size;
	NautilusZoomLevel zoom_level;
Alexander Larsson's avatar
Alexander Larsson committed
252 253
	NautilusFileIconFlags flags;
	
254
	model = (NautilusListModel *)tree_model;
Anders Carlsson's avatar
Anders Carlsson committed
255 256

	g_return_if_fail (model->details->stamp == iter->stamp);
257
	g_return_if_fail (!g_sequence_iter_is_end (iter->user_data));
Anders Carlsson's avatar
Anders Carlsson committed
258

259
	file_entry = g_sequence_get (iter->user_data);
Alexander Larsson's avatar
Alexander Larsson committed
260
	file = file_entry->file;
Anders Carlsson's avatar
Anders Carlsson committed
261 262
	
	switch (column) {
263
	case NAUTILUS_LIST_MODEL_FILE_COLUMN:
Anders Carlsson's avatar
Anders Carlsson committed
264 265 266 267
		g_value_init (value, NAUTILUS_TYPE_FILE);

		g_value_set_object (value, file);
		break;
268
	case NAUTILUS_LIST_MODEL_SUBDIRECTORY_COLUMN:
269 270 271 272
		g_value_init (value, NAUTILUS_TYPE_DIRECTORY);

		g_value_set_object (value, file_entry->subdirectory);
		break;
273 274 275 276 277 278 279
	case NAUTILUS_LIST_MODEL_SMALLEST_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_SMALLER_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_SMALL_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_STANDARD_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_LARGE_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_LARGER_ICON_COLUMN:
	case NAUTILUS_LIST_MODEL_LARGEST_ICON_COLUMN:
Anders Carlsson's avatar
Anders Carlsson committed
280 281
		g_value_init (value, GDK_TYPE_PIXBUF);

Alexander Larsson's avatar
Alexander Larsson committed
282
		if (file != NULL) {
283
			zoom_level = nautilus_list_model_get_zoom_level_from_column_id (column);
Alexander Larsson's avatar
Alexander Larsson committed
284
			icon_size = nautilus_get_icon_size_for_zoom_level (zoom_level);
285

286
			flags = NAUTILUS_FILE_ICON_FLAGS_USE_THUMBNAILS |
287 288
				NAUTILUS_FILE_ICON_FLAGS_FORCE_THUMBNAIL_SIZE |
				NAUTILUS_FILE_ICON_FLAGS_USE_MOUNT_ICON_AS_EMBLEM;
Alexander Larsson's avatar
Alexander Larsson committed
289 290 291 292 293 294 295 296 297 298
			if (model->details->drag_view != NULL) {
				GtkTreePath *path_a, *path_b;
				
				gtk_tree_view_get_drag_dest_row (model->details->drag_view,
								 &path_a,
								 NULL);
				if (path_a != NULL) {
					path_b = gtk_tree_model_get_path (tree_model, iter);

					if (gtk_tree_path_compare (path_a, path_b) == 0) {
Alexander Larsson's avatar
Alexander Larsson committed
299
						flags |= NAUTILUS_FILE_ICON_FLAGS_FOR_DRAG_ACCEPT;
Alexander Larsson's avatar
Alexander Larsson committed
300 301 302 303
					}
						
					gtk_tree_path_free (path_a);
					gtk_tree_path_free (path_b);
304 305
				}
			}
Alexander Larsson's avatar
Alexander Larsson committed
306

307
			gicon = G_ICON (nautilus_file_get_icon_pixbuf (file, icon_size, TRUE, flags));
308
			emblem_icons = nautilus_file_get_emblem_icons (file);
309

310 311 312 313
			/* pick only the first emblem we can render for the list view */
			for (l = emblem_icons; l != NULL; l = l->next) {
				emblem_icon = l->data;
				if (nautilus_icon_theme_can_render (G_THEMED_ICON (emblem_icon))) {
314
					emblem = g_emblem_new (emblem_icon);
315
					emblemed_icon = g_emblemed_icon_new (gicon, emblem);
316

317
					g_object_unref (gicon);
318
					g_object_unref (emblem);
319
					gicon = emblemed_icon;
320

321 322
					break;
				}
323 324
			}

325
			g_list_free_full (emblem_icons, g_object_unref);
326

327 328 329 330 331
			icon_info = nautilus_icon_info_lookup (gicon, icon_size);
			icon = nautilus_icon_info_get_pixbuf_at_size (icon_info, icon_size);

			g_object_unref (icon_info);
			g_object_unref (gicon);
332 333 334

			if (model->details->highlight_files != NULL &&
			    g_list_find_custom (model->details->highlight_files,
335
			                        file, (GCompareFunc) nautilus_file_compare_location))
336
			{
337
				rendered_icon = eel_create_spotlight_pixbuf (icon);
338 339 340 341 342 343 344

				if (rendered_icon != NULL) {
					g_object_unref (icon);
					icon = rendered_icon;
				}
			}

Alexander Larsson's avatar
Alexander Larsson committed
345 346 347
			g_value_set_object (value, icon);
			g_object_unref (icon);
		}
Anders Carlsson's avatar
Anders Carlsson committed
348
		break;
349
	case NAUTILUS_LIST_MODEL_FILE_NAME_IS_EDITABLE_COLUMN:
350
		g_value_init (value, G_TYPE_BOOLEAN);
351
		
Alexander Larsson's avatar
Alexander Larsson committed
352
                g_value_set_boolean (value, file != NULL && nautilus_file_can_rename (file));
353 354
                break;
 	default:
355
 		if (column >= NAUTILUS_LIST_MODEL_NUM_COLUMNS || column < NAUTILUS_LIST_MODEL_NUM_COLUMNS + model->details->columns->len) {
356
			NautilusColumn *nautilus_column;
357
			GQuark attribute;
358
			nautilus_column = model->details->columns->pdata[column - NAUTILUS_LIST_MODEL_NUM_COLUMNS];
359 360 361
			
			g_value_init (value, G_TYPE_STRING);
			g_object_get (nautilus_column, 
362
				      "attribute_q", &attribute, 
363
				      NULL);
Alexander Larsson's avatar
Alexander Larsson committed
364
			if (file != NULL) {
365 366
				str = nautilus_file_get_string_attribute_with_default_q (file, 
											 attribute);
367
				g_value_take_string (value, str);
368
			} else if (attribute == attribute_name_q) {
369
				if (file_entry->parent->loaded) {
Alexander Larsson's avatar
Alexander Larsson committed
370 371
					g_value_set_string (value, _("(Empty)"));
				} else {
372
					g_value_set_string (value, _("Loading…"));
Alexander Larsson's avatar
Alexander Larsson committed
373 374
				}
			}
375 376 377
		} else {
			g_assert_not_reached ();
		}
Anders Carlsson's avatar
Anders Carlsson committed
378 379 380 381
	}
}

static gboolean
382
nautilus_list_model_iter_next (GtkTreeModel *tree_model, GtkTreeIter *iter)
Anders Carlsson's avatar
Anders Carlsson committed
383
{
384
	NautilusListModel *model;
Anders Carlsson's avatar
Anders Carlsson committed
385

386
	model = (NautilusListModel *)tree_model;
Anders Carlsson's avatar
Anders Carlsson committed
387 388 389

	g_return_val_if_fail (model->details->stamp == iter->stamp, FALSE);

390
	iter->user_data = g_sequence_iter_next (iter->user_data);
Anders Carlsson's avatar
Anders Carlsson committed
391

392
	return !g_sequence_iter_is_end (iter->user_data);
Anders Carlsson's avatar
Anders Carlsson committed
393 394 395
}

static gboolean
396
nautilus_list_model_iter_children (GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent)
Anders Carlsson's avatar
Anders Carlsson committed
397
{
398
	NautilusListModel *model;
Alexander Larsson's avatar
Alexander Larsson committed
399
	GSequence *files;
400
	FileEntry *file_entry;
Anders Carlsson's avatar
Anders Carlsson committed
401

402
	model = (NautilusListModel *)tree_model;
Anders Carlsson's avatar
Anders Carlsson committed
403

404 405 406 407 408
	if (parent == NULL) {
		files = model->details->files;
	} else {
		file_entry = g_sequence_get (parent->user_data);
		files = file_entry->files;
Anders Carlsson's avatar
Anders Carlsson committed
409
	}
410

411
	if (files == NULL || g_sequence_get_length (files) == 0) {
412 413
		return FALSE;
	}
414 415 416 417 418

	iter->stamp = model->details->stamp;
	iter->user_data = g_sequence_get_begin_iter (files);

	return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
419 420 421
}

static gboolean
422
nautilus_list_model_iter_has_child (GtkTreeModel *tree_model, GtkTreeIter *iter)
Anders Carlsson's avatar
Anders Carlsson committed
423
{
424 425 426 427 428 429 430 431 432
	FileEntry *file_entry;

	if (iter == NULL) {
		return !nautilus_list_model_is_empty (NAUTILUS_LIST_MODEL (tree_model));
	}

	file_entry = g_sequence_get (iter->user_data);

	return (file_entry->files != NULL && g_sequence_get_length (file_entry->files) > 0);
Anders Carlsson's avatar
Anders Carlsson committed
433 434 435
}

static int
436
nautilus_list_model_iter_n_children (GtkTreeModel *tree_model, GtkTreeIter *iter)
Anders Carlsson's avatar
Anders Carlsson committed
437
{
438
	NautilusListModel *model;
Alexander Larsson's avatar
Alexander Larsson committed
439
	GSequence *files;
440
	FileEntry *file_entry;
Anders Carlsson's avatar
Anders Carlsson committed
441

442
	model = (NautilusListModel *)tree_model;
Anders Carlsson's avatar
Anders Carlsson committed
443 444

	if (iter == NULL) {
Alexander Larsson's avatar
Alexander Larsson committed
445 446
		files = model->details->files;
	} else {
447 448
		file_entry = g_sequence_get (iter->user_data);
		files = file_entry->files;
Anders Carlsson's avatar
Anders Carlsson committed
449 450
	}

Alexander Larsson's avatar
Alexander Larsson committed
451
	return g_sequence_get_length (files);
Anders Carlsson's avatar
Anders Carlsson committed
452 453 454
}

static gboolean
455
nautilus_list_model_iter_nth_child (GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent, int n)
Anders Carlsson's avatar
Anders Carlsson committed
456
{
457
	NautilusListModel *model;
458
	GSequenceIter *child;
Alexander Larsson's avatar
Alexander Larsson committed
459
	GSequence *files;
460
	FileEntry *file_entry;
Anders Carlsson's avatar
Anders Carlsson committed
461

462
	model = (NautilusListModel *)tree_model;
463

Anders Carlsson's avatar
Anders Carlsson committed
464
	if (parent != NULL) {
465 466 467 468
		file_entry = g_sequence_get (parent->user_data);
		files = file_entry->files;
	} else {
		files = model->details->files;
Anders Carlsson's avatar
Anders Carlsson committed
469 470
	}

471
	child = g_sequence_get_iter_at_pos (files, n);
Anders Carlsson's avatar
Anders Carlsson committed
472

473
	if (g_sequence_iter_is_end (child)) {
Anders Carlsson's avatar
Anders Carlsson committed
474 475
		return FALSE;
	}
476 477 478 479 480

	iter->stamp = model->details->stamp;
	iter->user_data = child;

	return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
481 482 483
}

static gboolean
484
nautilus_list_model_iter_parent (GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *child)
Anders Carlsson's avatar
Anders Carlsson committed
485
{
486 487 488 489 490 491 492 493 494 495
	NautilusListModel *model;
	FileEntry *file_entry;

	model = (NautilusListModel *)tree_model;

	file_entry = g_sequence_get (child->user_data);

	if (file_entry->parent == NULL) {
		return FALSE;
	}
Alexander Larsson's avatar
Alexander Larsson committed
496

497 498 499 500
	iter->stamp = model->details->stamp;
	iter->user_data = file_entry->parent->ptr;

	return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
501 502
}

503
static GSequenceIter *
504
lookup_file (NautilusListModel *model, NautilusFile *file,
505 506 507
	     NautilusDirectory *directory)
{
	FileEntry *file_entry;
508
	GSequenceIter *ptr, *parent_ptr;
509 510 511 512 513 514 515 516

	parent_ptr = NULL;
	if (directory) {
		parent_ptr = g_hash_table_lookup (model->details->directory_reverse_map,
						  directory);
	}
	
	if (parent_ptr) {
517
		file_entry = g_sequence_get (parent_ptr);
518 519 520 521 522 523
		ptr = g_hash_table_lookup (file_entry->reverse_map, file);
	} else {
		ptr = g_hash_table_lookup (model->details->top_reverse_map, file);
	}

	if (ptr) {
524
		g_assert (((FileEntry *)g_sequence_get (ptr))->file == file);
525 526 527 528 529 530 531
	}
	
	return ptr;
}


struct GetIters {
532
	NautilusListModel *model;
533 534 535 536 537 538 539
	NautilusFile *file;
	GList *iters;
};

static void
dir_to_iters (struct GetIters *data,
	      GHashTable *reverse_map)
Anders Carlsson's avatar
Anders Carlsson committed
540
{
541
	GSequenceIter *ptr;
542 543 544 545 546
	
	ptr = g_hash_table_lookup (reverse_map, data->file);
	if (ptr) {
		GtkTreeIter *iter;
		iter = g_new0 (GtkTreeIter, 1);
547
		nautilus_list_model_ptr_to_iter (data->model, ptr, iter);
548 549 550 551 552 553 554 555 556 557 558 559 560
		data->iters = g_list_prepend (data->iters, iter);
	}
}

static void
file_to_iter_cb (gpointer  key,
		 gpointer  value,
		 gpointer  user_data)
{
	struct GetIters *data;
	FileEntry *dir_file_entry;

	data = user_data;
561
	dir_file_entry = g_sequence_get ((GSequenceIter *)value);
562 563 564 565
	dir_to_iters (data, dir_file_entry->reverse_map);
}

GList *
566
nautilus_list_model_get_all_iters_for_file (NautilusListModel *model, NautilusFile *file)
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
{
	struct GetIters data;

	data.file = file;
	data.model = model;
	data.iters = NULL;
	
	dir_to_iters (&data, model->details->top_reverse_map);
	g_hash_table_foreach (model->details->directory_reverse_map,
			      file_to_iter_cb, &data);

	return g_list_reverse (data.iters);
}

gboolean
582 583 584
nautilus_list_model_get_first_iter_for_file (NautilusListModel          *model,
					     NautilusFile         *file,
					     GtkTreeIter          *iter)
585 586 587 588 589 590
{
	GList *list;
	gboolean res;

	res = FALSE;
	
591
	list = nautilus_list_model_get_all_iters_for_file (model, file);
592 593 594 595
	if (list != NULL) {
		res = TRUE;
		*iter = *(GtkTreeIter *)list->data;
	}
596
	g_list_free_full (list, g_free);
597 598 599 600
	
	return res;
}

Anders Carlsson's avatar
Anders Carlsson committed
601

602
gboolean
603 604 605
nautilus_list_model_get_tree_iter_from_file (NautilusListModel *model, NautilusFile *file,
					     NautilusDirectory *directory,
					     GtkTreeIter *iter)
606
{
607
	GSequenceIter *ptr;
Anders Carlsson's avatar
Anders Carlsson committed
608

609
	ptr = lookup_file (model, file, directory);
610
	if (!ptr) {
Anders Carlsson's avatar
Anders Carlsson committed
611 612 613
		return FALSE;
	}

614
	nautilus_list_model_ptr_to_iter (model, ptr, iter);
Anders Carlsson's avatar
Anders Carlsson committed
615 616 617 618
	
	return TRUE;
}

Alexander Larsson's avatar
Alexander Larsson committed
619
static int
620 621 622
nautilus_list_model_file_entry_compare_func (gconstpointer a,
					     gconstpointer b,
					     gpointer      user_data)
Alexander Larsson's avatar
Alexander Larsson committed
623 624 625
{
	FileEntry *file_entry1;
	FileEntry *file_entry2;
626
	NautilusListModel *model;
Alexander Larsson's avatar
Alexander Larsson committed
627 628
	int result;

629
	model = (NautilusListModel *)user_data;
Alexander Larsson's avatar
Alexander Larsson committed
630 631 632 633 634

	file_entry1 = (FileEntry *)a;
	file_entry2 = (FileEntry *)b;
	
	if (file_entry1->file != NULL && file_entry2->file != NULL) {
635 636 637 638
		result = nautilus_file_compare_for_sort_by_attribute_q (file_entry1->file, file_entry2->file,
									model->details->sort_attribute,
									model->details->sort_directories_first,
									(model->details->order == GTK_SORT_DESCENDING));
Alexander Larsson's avatar
Alexander Larsson committed
639 640 641 642 643 644 645 646 647
	} else if (file_entry1->file == NULL) {
		return -1;
	} else {
		return 1;
	}

	return result;
}

648
int
649 650 651
nautilus_list_model_compare_func (NautilusListModel *model,
				  NautilusFile *file1,
				  NautilusFile *file2)
Anders Carlsson's avatar
Anders Carlsson committed
652 653
{
	int result;
654

655 656 657 658
	result = nautilus_file_compare_for_sort_by_attribute_q (file1, file2,
								model->details->sort_attribute,
								model->details->sort_directories_first,
								(model->details->order == GTK_SORT_DESCENDING));
Anders Carlsson's avatar
Anders Carlsson committed
659 660 661 662 663

	return result;
}

static void
664
nautilus_list_model_sort_file_entries (NautilusListModel *model, GSequence *files, GtkTreePath *path)
Anders Carlsson's avatar
Anders Carlsson committed
665
{
666
	GSequenceIter **old_order;
667
	GtkTreeIter iter;
668 669 670
	int *new_order;
	int length;
	int i;
Alexander Larsson's avatar
Alexander Larsson committed
671
	FileEntry *file_entry;
672
	gboolean has_iter;
673 674

	length = g_sequence_get_length (files);
Anders Carlsson's avatar
Anders Carlsson committed
675

676
	if (length <= 1) {
Anders Carlsson's avatar
Anders Carlsson committed
677 678 679
		return;
	}
	
680 681
	/* generate old order of GSequenceIter's */
	old_order = g_new (GSequenceIter *, length);
682
	for (i = 0; i < length; ++i) {
683
		GSequenceIter *ptr = g_sequence_get_iter_at_pos (files, i);
Alexander Larsson's avatar
Alexander Larsson committed
684
		
685
		file_entry = g_sequence_get (ptr);
Alexander Larsson's avatar
Alexander Larsson committed
686 687
		if (file_entry->files != NULL) {
			gtk_tree_path_append_index (path, i);
688
			nautilus_list_model_sort_file_entries (model, file_entry->files, path);
Alexander Larsson's avatar
Alexander Larsson committed
689 690
			gtk_tree_path_up (path);
		}
Anders Carlsson's avatar
Anders Carlsson committed
691

692
		old_order[i] = ptr;
Anders Carlsson's avatar
Anders Carlsson committed
693 694
	}

695
	/* sort */
696
	g_sequence_sort (files, nautilus_list_model_file_entry_compare_func, model);
697 698 699

	/* generate new order */
	new_order = g_new (int, length);
700
	/* Note: new_order[newpos] = oldpos */
701
	for (i = 0; i < length; ++i) {
702
		new_order[g_sequence_iter_get_position (old_order[i])] = i;
Anders Carlsson's avatar
Anders Carlsson committed
703 704 705
	}

	/* Let the world know about our new order */
Diego González's avatar
Diego González committed
706

707
	g_assert (new_order != NULL);
708 709 710 711 712 713 714 715 716

	has_iter = FALSE;
	if (gtk_tree_path_get_depth (path) != 0) {
		gboolean get_iter_result;
		has_iter = TRUE;
		get_iter_result = gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
		g_assert (get_iter_result);
	}

Anders Carlsson's avatar
Anders Carlsson committed
717
	gtk_tree_model_rows_reordered (GTK_TREE_MODEL (model),
718
				       path, has_iter ? &iter : NULL, new_order);
719 720

	g_free (old_order);
Anders Carlsson's avatar
Anders Carlsson committed
721 722 723
	g_free (new_order);
}

Alexander Larsson's avatar
Alexander Larsson committed
724
static void
725
nautilus_list_model_sort (NautilusListModel *model)
Alexander Larsson's avatar
Alexander Larsson committed
726 727 728 729 730
{
	GtkTreePath *path;

	path = gtk_tree_path_new ();

731
	nautilus_list_model_sort_file_entries (model, model->details->files, path);
Alexander Larsson's avatar
Alexander Larsson committed
732 733 734 735

	gtk_tree_path_free (path);
}

Anders Carlsson's avatar
Anders Carlsson committed
736
static gboolean
737 738 739
nautilus_list_model_get_sort_column_id (GtkTreeSortable *sortable,
					gint            *sort_column_id,
					GtkSortType     *order)
Anders Carlsson's avatar
Anders Carlsson committed
740
{
741
	NautilusListModel *model;
742 743
	int id;
	
744
	model = (NautilusListModel *)sortable;
745
	
746
	id = nautilus_list_model_get_sort_column_id_from_attribute 
747 748 749
		(model, model->details->sort_attribute);
	
	if (id == -1) {
Anders Carlsson's avatar
Anders Carlsson committed
750 751
		return FALSE;
	}
752
	
Anders Carlsson's avatar
Anders Carlsson committed
753
	if (sort_column_id != NULL) {
754
		*sort_column_id = id;
Anders Carlsson's avatar
Anders Carlsson committed
755 756 757 758 759 760 761 762 763 764
	}

	if (order != NULL) {
		*order = model->details->order;
	}

	return TRUE;
}

static void
765
nautilus_list_model_set_sort_column_id (GtkTreeSortable *sortable, gint sort_column_id, GtkSortType order)
Anders Carlsson's avatar
Anders Carlsson committed
766
{
767
	NautilusListModel *model;
Anders Carlsson's avatar
Anders Carlsson committed
768

769
	model = (NautilusListModel *)sortable;
Anders Carlsson's avatar
Anders Carlsson committed
770

771
	model->details->sort_attribute = nautilus_list_model_get_attribute_from_sort_column_id (model, sort_column_id);
Anders Carlsson's avatar
Anders Carlsson committed
772 773 774

	model->details->order = order;

775
	nautilus_list_model_sort (model);
Anders Carlsson's avatar
Anders Carlsson committed
776 777 778 779
	gtk_tree_sortable_sort_column_changed (sortable);
}

static gboolean
780
nautilus_list_model_has_default_sort_func (GtkTreeSortable *sortable)
Anders Carlsson's avatar
Anders Carlsson committed
781 782 783 784
{
	return FALSE;
}

785
static void
786
add_dummy_row (NautilusListModel *model, FileEntry *parent_entry)
787 788 789 790 791 792 793 794
{
	FileEntry *dummy_file_entry;
	GtkTreeIter iter;
	GtkTreePath *path;
	
	dummy_file_entry = g_new0 (FileEntry, 1);
	dummy_file_entry->parent = parent_entry;
	dummy_file_entry->ptr = g_sequence_insert_sorted (parent_entry->files, dummy_file_entry,
795
							  nautilus_list_model_file_entry_compare_func, model);
796 797 798 799 800 801 802 803
	iter.stamp = model->details->stamp;
	iter.user_data = dummy_file_entry->ptr;
	
	path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
	gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
	gtk_tree_path_free (path);
}

Alexander Larsson's avatar
Alexander Larsson committed
804
gboolean
805 806
nautilus_list_model_add_file (NautilusListModel *model, NautilusFile *file,
			      NautilusDirectory *directory)
Anders Carlsson's avatar
Anders Carlsson committed
807
{
808 809 810
	GtkTreeIter iter;
	GtkTreePath *path;
	FileEntry *file_entry;
811
	GSequenceIter *ptr, *parent_ptr;
Alexander Larsson's avatar
Alexander Larsson committed
812 813
	GSequence *files;
	gboolean replace_dummy;
814
	GHashTable *parent_hash;
815

816 817 818
	parent_ptr = g_hash_table_lookup (model->details->directory_reverse_map,
					  directory);
	if (parent_ptr) {
819
		file_entry = g_sequence_get (parent_ptr);
820 821 822 823
		ptr = g_hash_table_lookup (file_entry->reverse_map, file);
	} else {
		file_entry = NULL;
		ptr = g_hash_table_lookup (model->details->top_reverse_map, file);
Anders Carlsson's avatar
Anders Carlsson committed
824 825
	}

826 827 828 829
	if (ptr != NULL) {
		g_warning ("file already in tree (parent_ptr: %p)!!!\n", parent_ptr);
		return FALSE;
	}
Anders Carlsson's avatar
Anders Carlsson committed
830
	
Alexander Larsson's avatar
Alexander Larsson committed
831
	file_entry = g_new0 (FileEntry, 1);
832
	file_entry->file = nautilus_file_ref (file);
Alexander Larsson's avatar
Alexander Larsson committed
833
	file_entry->parent = NULL;
834 835
	file_entry->subdirectory = NULL;
	file_entry->files = NULL;
Alexander Larsson's avatar
Alexander Larsson committed
836 837
	
	files = model->details->files;
838
	parent_hash = model->details->top_reverse_map;
Alexander Larsson's avatar
Alexander Larsson committed
839 840 841
	
	replace_dummy = FALSE;

842
	if (parent_ptr != NULL) {
843
		file_entry->parent = g_sequence_get (parent_ptr);
844 845 846 847 848
		/* At this point we set loaded. Either we saw
		 * "done" and ignored it waiting for this, or we do this
		 * earlier, but then we replace the dummy row anyway,
		 * so it doesn't matter */
		file_entry->parent->loaded = 1;
849 850 851
		parent_hash = file_entry->parent->reverse_map;
		files = file_entry->parent->files;
		if (g_sequence_get_length (files) == 1) {
852 853
			GSequenceIter *dummy_ptr = g_sequence_get_iter_at_pos (files, 0);
			FileEntry *dummy_entry = g_sequence_get (dummy_ptr);
854 855 856 857 858 859
			if (dummy_entry->file == NULL) {
				/* replace the dummy loading entry */
				model->details->stamp++;
				g_sequence_remove (dummy_ptr);
				
				replace_dummy = TRUE;
Alexander Larsson's avatar
Alexander Larsson committed
860 861 862
			}
		}
	}
863

Alexander Larsson's avatar
Alexander Larsson committed
864 865
	
	file_entry->ptr = g_sequence_insert_sorted (files, file_entry,
866
					    nautilus_list_model_file_entry_compare_func, model);
867

868
	g_hash_table_insert (parent_hash, file, file_entry->ptr);
Anders Carlsson's avatar
Anders Carlsson committed
869 870
	
	iter.stamp = model->details->stamp;
Alexander Larsson's avatar
Alexander Larsson committed
871
	iter.user_data = file_entry->ptr;
Anders Carlsson's avatar
Anders Carlsson committed
872 873

	path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
Alexander Larsson's avatar
Alexander Larsson committed
874 875 876 877 878
	if (replace_dummy) {
		gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
	} else {
		gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
	}
879

Alexander Larsson's avatar
Alexander Larsson committed
880
	if (nautilus_file_is_directory (file)) {
881
		file_entry->files = g_sequence_new ((GDestroyNotify)file_entry_free);
Alexander Larsson's avatar
Alexander Larsson committed
882

883
		add_dummy_row (model, file_entry);
Alexander Larsson's avatar
Alexander Larsson committed
884 885 886 887

		gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (model),
						      path, &iter);
	}
Anders Carlsson's avatar
Anders Carlsson committed
888
	gtk_tree_path_free (path);
Alexander Larsson's avatar
Alexander Larsson committed
889 890
	
	return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
891 892 893
}

void
894 895
nautilus_list_model_file_changed (NautilusListModel *model, NautilusFile *file,
				  NautilusDirectory *directory)
Anders Carlsson's avatar
Anders Carlsson committed
896
{
897
	FileEntry *parent_file_entry;
Anders Carlsson's avatar
Anders Carlsson committed
898
	GtkTreeIter iter;
899
	GtkTreePath *path, *parent_path;
900
	GSequenceIter *ptr;
901 902 903 904
	int pos_before, pos_after, length, i, old;
	int *new_order;
	gboolean has_iter;
	GSequence *files;
Anders Carlsson's avatar
Anders Carlsson committed
905

906
	ptr = lookup_file (model, file, directory);
907 908
	if (!ptr) {
		return;
Anders Carlsson's avatar
Anders Carlsson committed
909 910
	}

911
	
912
	pos_before = g_sequence_iter_get_position (ptr);
913
		
914
	g_sequence_sort_changed (ptr, nautilus_list_model_file_entry_compare_func, model);
915

916
	pos_after = g_sequence_iter_get_position (ptr);
917 918 919 920

	if (pos_before != pos_after) {
		/* The file moved, we need to send rows_reordered */
		
921
		parent_file_entry = ((FileEntry *)g_sequence_get (ptr))->parent;
922 923 924 925 926 927 928

		if (parent_file_entry == NULL) {
			has_iter = FALSE;
			parent_path = gtk_tree_path_new ();
			files = model->details->files;
		} else {
			has_iter = TRUE;
929
			nautilus_list_model_ptr_to_iter (model, parent_file_entry->ptr, &iter);
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
			parent_path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
			files = parent_file_entry->files;
		}

		length = g_sequence_get_length (files);
		new_order = g_new (int, length);
		/* Note: new_order[newpos] = oldpos */
		for (i = 0, old = 0; i < length; ++i) {
			if (i == pos_after) {
				new_order[i] = pos_before;
			} else {
				if (old == pos_before)
					old++;
				new_order[i] = old++;
			}
		}

		gtk_tree_model_rows_reordered (GTK_TREE_MODEL (model),
					       parent_path, has_iter ? &iter : NULL, new_order);

		gtk_tree_path_free (parent_path);
		g_free (new_order);
	}
	
954
	nautilus_list_model_ptr_to_iter (model, ptr, &iter);
Anders Carlsson's avatar
Anders Carlsson committed
955 956 957 958 959 960
	path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
	gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
	gtk_tree_path_free (path);
}

gboolean
961
nautilus_list_model_is_empty (NautilusListModel *model)
Anders Carlsson's avatar
Anders Carlsson committed
962
{
963
	return (g_sequence_get_length (model->details->files) == 0);
Anders Carlsson's avatar
Anders Carlsson committed
964 965
}

966
guint
967
nautilus_list_model_get_length (NautilusListModel *model)
968 969 970 971
{
	return g_sequence_get_length (model->details->files);
}

Anders Carlsson's avatar
Anders Carlsson committed
972
static void
973
nautilus_list_model_remove (NautilusListModel *model, GtkTreeIter *iter)
Anders Carlsson's avatar
Anders Carlsson committed
974
{
975
	GSequenceIter *ptr, *child_ptr;
976
	FileEntry *file_entry, *child_file_entry, *parent_file_entry;
Anders Carlsson's avatar
Anders Carlsson committed
977
	GtkTreePath *path;
978
	GtkTreeIter parent_iter;
Anders Carlsson's avatar
Anders Carlsson committed
979

980
	ptr = iter->user_data;
981
	file_entry = g_sequence_get (ptr);
Alexander Larsson's avatar
Alexander Larsson committed
982 983 984
	
	if (file_entry->files != NULL) {
		while (g_sequence_get_length (file_entry->files) > 0) {
985 986
			child_ptr = g_sequence_get_begin_iter (file_entry->files);
			child_file_entry = g_sequence_get (child_ptr);
Alexander Larsson's avatar
Alexander Larsson committed
987
			if (child_file_entry->file != NULL) {
988
				nautilus_list_model_remove_file (model,
989 990
							   child_file_entry->file,
							   file_entry->subdirectory);
Alexander Larsson's avatar
Alexander Larsson committed
991
			} else {
992
				path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), iter);
Alexander Larsson's avatar
Alexander Larsson committed
993
				gtk_tree_path_append_index (path, 0);
994
				model->details->stamp++;
Alexander Larsson's avatar
Alexander Larsson committed
995 996
				g_sequence_remove (child_ptr);
				gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
997
				gtk_tree_path_free (path);
Alexander Larsson's avatar
Alexander Larsson committed
998
			}
999 1000 1001
			
			/* the parent iter didn't actually change */
			iter->stamp = model->details->stamp;
Alexander Larsson's avatar
Alexander Larsson committed
1002
		}
1003
			
Alexander Larsson's avatar
Alexander Larsson committed
1004 1005
	}

1006 1007 1008 1009 1010 1011
	if (file_entry->file != NULL) { /* Don't try to remove dummy row */
		if (file_entry->parent != NULL) {
			g_hash_table_remove (file_entry->parent->reverse_map, file_entry->file);
		} else {
			g_hash_table_remove (model->details->top_reverse_map, file_entry->file);
		}
1012
	}
Alexander Larsson's avatar
Alexander Larsson committed
1013

1014 1015
	parent_file_entry = file_entry->parent;
	if (parent_file_entry && g_sequence_get_length (parent_file_entry->files) == 1 &&
1016
	    file_entry->file != NULL) {
1017
		/* this is the last non-dummy child, add a dummy node */
1018 1019 1020 1021
		/* We need to do this before removing the last file to avoid
		 * collapsing the row.
		 */
		add_dummy_row (model, parent_file_entry);
1022
	}
Anders Carlsson's avatar
Anders Carlsson committed
1023

1024 1025 1026 1027
	if (file_entry->subdirectory != NULL) {
		g_signal_emit (model,
			       list_model_signals[SUBDIRECTORY_UNLOADED], 0,
			       file_entry->subdirectory);
1028 1029
		g_hash_table_remove (model->details->directory_reverse_map,
				     file_entry->subdirectory);
Alexander Larsson's avatar
Alexander Larsson committed
1030
	}
Anders Carlsson's avatar
Anders Carlsson committed
1031
	
1032
	path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), iter);
1033
	
1034
	g_sequence_remove (ptr);
1035 1036 1037
	model->details->stamp++;
	gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
	
Anders Carlsson's avatar
Anders Carlsson committed
1038
	gtk_tree_path_free (path);
1039

1040
	if (parent_file_entry && g_sequence_get_length (parent_file_entry->files) == 0) {
1041 1042 1043 1044 1045 1046 1047
		parent_iter.stamp = model->details->stamp;
		parent_iter.user_data = parent_file_entry->ptr;
		path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &parent_iter);
		gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (model),
						      path, &parent_iter);
		gtk_tree_path_free (path);
	}
Anders Carlsson's avatar
Anders Carlsson committed
1048 1049 1050
}

void
1051
nautilus_list_model_remove_file (NautilusListModel *model, NautilusFile *file,
1052
			   NautilusDirectory *directory)
Anders Carlsson's avatar
Anders Carlsson committed
1053 1054 1055
{
	GtkTreeIter iter;

1056 1057
	if (nautilus_list_model_get_tree_iter_from_file (model, file, directory, &iter)) {
		nautilus_list_model_remove (model, &iter);
Anders Carlsson's avatar
Anders Carlsson committed
1058 1059 1060
	}
}

Alexander Larsson's avatar
Alexander Larsson committed
1061
static void
1062
nautilus_list_model_clear_directory (NautilusListModel *model, GSequence *files)
Anders Carlsson's avatar
Anders Carlsson committed
1063 1064
{
	GtkTreeIter iter;
Alexander Larsson's avatar
Alexander Larsson committed
1065
	FileEntry *file_entry;
1066

Alexander Larsson's avatar
Alexander Larsson committed
1067
	while (g_sequence_get_length (files) > 0) {
1068
		iter.user_data = g_sequence_get_begin_iter (files);
Anders Carlsson's avatar
Anders Carlsson committed
1069

1070
		file_entry = g_sequence_get (iter.user_data);
Alexander Larsson's avatar
Alexander Larsson committed
1071
		if (file_entry->files != NULL) {
1072
			nautilus_list_model_clear_directory (model, file_entry->files);
Alexander Larsson's avatar
Alexander Larsson committed
1073 1074
		}
		
Anders Carlsson's avatar
Anders Carlsson committed
1075
		iter.stamp = model->details->stamp;
1076
		nautilus_list_model_remove (model, &iter);
Anders Carlsson's avatar
Anders Carlsson committed
1077 1078 1079
	}
}

Alexander Larsson's avatar
Alexander Larsson committed
1080
void
1081
nautilus_list_model_clear (NautilusListModel *model)
Alexander Larsson's avatar
Alexander Larsson committed
1082 1083 1084
{
	g_return_if_fail (model != NULL);

1085
	nautilus_list_model_clear_directory (model, model->details->files);
Alexander Larsson's avatar
Alexander Larsson committed
1086 1087
}

1088
NautilusFile *
1089
nautilus_list_model_file_for_path (NautilusListModel *model, GtkTreePath *path)
1090 1091 1092 1093 1094 1095 1096 1097 1098
{
	NautilusFile *file;
	GtkTreeIter iter;

	file = NULL;
	if (gtk_tree_model_get_iter (GTK_TREE_MODEL (model), 
				     &iter, path)) {
		gtk_tree_model_get (GTK_TREE_MODEL (model), 
				    &iter, 
1099
				    NAUTILUS_LIST_MODEL_FILE_COLUMN, &file,
1100 1101 1102 1103 1104
				    -1);
	}
	return file;
}

1105
gboolean
1106
nautilus_list_model_load_subdirectory (NautilusListModel *model, GtkTreePath *path, NautilusDirectory **directory)
1107 1108 1109
{
	GtkTreeIter iter;
	FileEntry *file_entry;
1110
	NautilusDirectory *subdirectory;
1111 1112 1113 1114 1115
	
	if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path)) {
		return FALSE;
	}

1116
	file_entry = g_sequence_get (iter.user_data);
1117 1118 1119 1120 1121
	if (file_entry->file == NULL ||
	    file_entry->subdirectory != NULL) {
		return FALSE;
	}

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	subdirectory = nautilus_directory_get_for_file (file_entry->file);

	if (g_hash_table_lookup (model->details->directory_reverse_map,
				 subdirectory) != NULL) {
		nautilus_directory_unref (subdirectory);
		g_warning ("Already in directory_reverse_map, failing\n");
		return FALSE;
	}
	
	file_entry->subdirectory = subdirectory,
	g_hash_table_insert (model->details->directory_reverse_map,
			     subdirectory, file_entry->ptr);
	file_entry->reverse_map = g_hash_table_new (g_direct_hash, g_direct_equal);

	/* Return a ref too */
	nautilus_directory_ref (subdirectory);
	*directory = subdirectory;
1139 1140 1141 1142 1143 1144
	
	return TRUE;
}

/* removes all children of the subfolder and unloads the subdirectory */
void
1145
nautilus_list_model_unload_subdirectory (NautilusListModel *model, GtkTreeIter *iter)
1146
{
1147
	GSequenceIter *child_ptr;
1148
	FileEntry *file_entry, *child_file_entry;
1149 1150
	GtkTreeIter child_iter;

1151
	file_entry = g_sequence_get (iter->user_data);
1152 1153 1154 1155 1156
	if (file_entry->file == NULL ||
	    file_entry->subdirectory == NULL) {
		return;
	}

1157 1158
	file_entry->loaded = 0;
	
1159 1160
	/* Remove all children */
	while (g_sequence_get_length (file_entry->files) > 0) {
1161 1162
		child_ptr = g_sequence_get_begin_iter (file_entry->files);
		child_file_entry = g_sequence_get (child_ptr);
1163 1164 1165 1166
		if (child_file_entry->file == NULL) {
			/* Don't delete the dummy node */
			break;
		} else {
1167 1168
			nautilus_list_model_ptr_to_iter (model, child_ptr, &child_iter);
			nautilus_list_model_remove (model, &child_iter);
1169 1170 1171 1172 1173 1174 1175 1176 1177
		}
	}

	/* Emit unload signal */
	g_signal_emit (model,
		       list_model_signals[SUBDIRECTORY_UNLOADED], 0,
		       file_entry->subdirectory);

	/* actually unload */
1178 1179
	g_hash_table_remove (model->details->directory_reverse_map,
			     file_entry->subdirectory);
1180 1181
	nautilus_directory_unref (file_entry->subdirectory);
	file_entry->subdirectory = NULL;
1182 1183 1184 1185

	g_assert (g_hash_table_size (file_entry->reverse_map) == 0);
	g_hash_table_destroy (file_entry->reverse_map);
	file_entry->reverse_map = NULL;
1186 1187 1188 1189
}



Anders Carlsson's avatar
Anders Carlsson committed
1190
void
1191
nautilus_list_model_set_should_sort_directories_first (NautilusListModel *model, gboolean sort_directories_first)
Anders Carlsson's avatar
Anders Carlsson committed
1192 1193 1194 1195 1196 1197
{
	if (model->details->sort_directories_first == sort_directories_first) {
		return;
	}

	model->details->sort_directories_first = sort_directories_first;
1198
	nautilus_list_model_sort (model);
Anders Carlsson's avatar
Anders Carlsson committed
1199 1200 1201
}

int
1202 1203
nautilus_list_model_get_sort_column_id_from_attribute (NautilusListModel *model,
						       GQuark attribute)
Anders Carlsson's avatar
Anders Carlsson committed
1204 1205 1206
{
	guint i;

1207
	if (attribute == 0) {
1208 1209 1210
		return -1;
	}