nautilus-directory.c 55.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-directory.c: Mautilus directory model.
 
   Copyright (C) 1999, 2000 Eazel, Inc.
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   This program 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
   General Public License for more details.
  
   You should have received a copy of the GNU General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
   Author: Darin Adler <darin@eazel.com>
*/

#include <config.h>
#include "nautilus-directory.h"

28 29
#include <grp.h>
#include <pwd.h>
30 31
#include <stdlib.h>

32
#include <gtk/gtksignal.h>
33 34
#include <gtk/gtkmain.h>

35 36 37
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>

38 39
#include <libgnomevfs/gnome-vfs-types.h>
#include <libgnomevfs/gnome-vfs-uri.h>
40
#include <libgnomevfs/gnome-vfs-file-info.h>
41 42 43 44 45 46 47
#include <libgnomevfs/gnome-vfs-ops.h>
#include <libgnomevfs/gnome-vfs-async-ops.h>

#include <gnome-xml/parser.h>
#include <gnome-xml/tree.h>
#include <gnome-xml/xmlmemory.h>

48
#include "nautilus-alloc.h"
49
#include "nautilus-glib-extensions.h"
50 51 52 53
#include "nautilus-gtk-macros.h"
#include "nautilus-lib-self-check-functions.h"
#include "nautilus-string.h"

54 55 56 57 58 59 60 61 62 63 64 65
#define METAFILE_NAME ".nautilus-metafile.xml"
#define METAFILE_PERMISSIONS (GNOME_VFS_PERM_USER_READ | GNOME_VFS_PERM_USER_WRITE \
			      | GNOME_VFS_PERM_GROUP_READ | GNOME_VFS_PERM_GROUP_WRITE \
			      | GNOME_VFS_PERM_OTHER_READ | GNOME_VFS_PERM_OTHER_WRITE)

#define NAUTILUS_DIRECTORY_NAME ".nautilus"
#define METAFILES_DIRECTORY_NAME "metafiles"
#define METAFILES_DIRECTORY_PERMISSIONS (GNOME_VFS_PERM_USER_ALL \
					 | GNOME_VFS_PERM_GROUP_ALL \
					 | GNOME_VFS_PERM_OTHER_ALL)
#define METAFILE_SUFFIX ".xml"

66 67
#define METAFILE_XML_VERSION "1.0"

68 69
#define DIRECTORY_LOAD_ITEMS_PER_CB 1

70 71 72 73 74 75
typedef enum {
	NAUTILUS_DATE_TYPE_MODIFIED,
	NAUTILUS_DATE_TYPE_CHANGED,
	NAUTILUS_DATE_TYPE_ACCESSED
} NautilusDateType;

76 77 78 79 80 81
enum 
{
	FILES_ADDED,
	LAST_SIGNAL
};

82 83 84 85
/* FIXME: This hack needs to die eventually. See comments with function */
static int get_directory_item_count_hack (NautilusFile *file, gboolean ignore_invisible_items);


86 87
static guint nautilus_directory_signals[LAST_SIGNAL];

88 89 90 91 92 93 94 95 96 97 98
static void nautilus_directory_initialize_class (gpointer klass);
static void nautilus_directory_initialize (gpointer object, gpointer klass);
static void nautilus_directory_finalize (GtkObject *object);

static NautilusDirectory *nautilus_directory_new (const char* uri);

static void nautilus_directory_read_metafile (NautilusDirectory *directory);
static void nautilus_directory_write_metafile (NautilusDirectory *directory);
static void nautilus_directory_request_write_metafile (NautilusDirectory *directory);
static void nautilus_directory_remove_write_metafile_idle (NautilusDirectory *directory);

99
static void nautilus_file_detach (NautilusFile *file);
100
static int nautilus_file_compare_by_type (NautilusFile *file_1, NautilusFile *file_2);
101 102 103 104 105
static int  nautilus_file_compare_for_sort_internal (NautilusFile *file_1,
					 	     NautilusFile *file_2,
					 	     NautilusFileSortType sort_type,
					 	     gboolean reversed);

106 107
static char * nautilus_file_get_date_as_string (NautilusFile *file,
						NautilusDateType date_type);
108 109 110
static char *nautilus_file_get_owner_as_string (NautilusFile *file);
static char *nautilus_file_get_group_as_string (NautilusFile *file);
static char *nautilus_file_get_permissions_as_string (NautilusFile *file);
111 112
static char * nautilus_file_get_size_as_string (NautilusFile *file);
static char * nautilus_file_get_type_as_string (NautilusFile *file);
113 114 115 116 117 118 119
static void nautilus_directory_load_cb (GnomeVFSAsyncHandle *handle,
					GnomeVFSResult result,
					GnomeVFSDirectoryList *list,
					guint entries_read,
					gpointer callback_data);
static NautilusFile *nautilus_directory_new_file (NautilusDirectory *directory,
						  GnomeVFSFileInfo *info);
120

121
NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusDirectory, nautilus_directory, GTK_TYPE_OBJECT)
122 123 124 125 126 127 128

struct _NautilusDirectoryDetails
{
	char *uri_text;
	GnomeVFSURI *uri;

	GnomeVFSURI *metafile_uri;
129 130 131
	GnomeVFSURI *alternate_metafile_uri;
	gboolean use_alternate_metafile;

132
	xmlDoc *metafile_tree;
133 134 135 136 137 138 139 140 141
	guint write_metafile_idle_id;

	GnomeVFSAsyncHandle *directory_load_in_progress;
	GnomeVFSDirectoryListPosition directory_load_list_last_handled;

	GList *pending_file_info;
        guint dequeue_pending_idle_id;

	gboolean directory_loaded;
142 143 144 145 146 147

	NautilusFileList *files;
};

struct _NautilusFile
{
148 149 150 151
	guint ref_count;

	NautilusDirectory *directory;
	GnomeVFSFileInfo *info;
152 153 154 155 156 157 158 159 160 161 162 163
};

static GHashTable* directory_objects;

static void
nautilus_directory_initialize_class (gpointer klass)
{
	GtkObjectClass *object_class;

	object_class = GTK_OBJECT_CLASS (klass);
	
	object_class->finalize = nautilus_directory_finalize;
164 165 166 167 168 169 170 171 172 173

	nautilus_directory_signals[FILES_ADDED] =
		gtk_signal_new ("files_added",
				GTK_RUN_LAST,
				object_class->type,
				GTK_SIGNAL_OFFSET (NautilusDirectoryClass, files_added),
				gtk_marshal_NONE__POINTER,
				GTK_TYPE_NONE, 1, GTK_TYPE_POINTER);

	gtk_object_class_add_signals (object_class, nautilus_directory_signals, LAST_SIGNAL);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

static void
nautilus_directory_initialize (gpointer object, gpointer klass)
{
	NautilusDirectory *directory;

	directory = NAUTILUS_DIRECTORY(object);

	directory->details = g_new0 (NautilusDirectoryDetails, 1);
}

static void
nautilus_directory_finalize (GtkObject *object)
{
	NautilusDirectory *directory;

	directory = NAUTILUS_DIRECTORY (object);

193 194 195
	if (directory->details->write_metafile_idle_id != 0)
		nautilus_directory_write_metafile (directory);

196 197
	g_hash_table_remove (directory_objects, directory->details->uri_text);

198
	/* Let go of all the files. */
199
	while (directory->details->files != NULL) {
200 201 202 203
		nautilus_file_detach (directory->details->files->data);

		directory->details->files = g_list_remove_link
			(directory->details->files, directory->details->files);
204 205
	}

206
	g_free (directory->details->uri_text);
207
	if (directory->details->uri != NULL)
208
		gnome_vfs_uri_unref (directory->details->uri);
209
	if (directory->details->metafile_uri != NULL)
210
		gnome_vfs_uri_unref (directory->details->metafile_uri);
211 212
	if (directory->details->alternate_metafile_uri != NULL)
		gnome_vfs_uri_unref (directory->details->alternate_metafile_uri);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	xmlFreeDoc (directory->details->metafile_tree);

	g_free (directory->details);

	NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, finalize, (object));
}

/**
 * nautilus_directory_get:
 * @uri: URI of directory to get.
 *
 * Get a directory given a uri.
 * Creates the appropriate subclass given the uri mappings.
 * Returns a referenced object, not a floating one. Unref when finished.
 * If two windows are viewing the same uri, the directory object is shared.
 */
NautilusDirectory *
nautilus_directory_get (const char *uri)
{
	NautilusDirectory *directory;

	g_return_val_if_fail (uri != NULL, NULL);

	/* FIXME: This currently ignores the issue of two uris that are not identical but point
	   to the same data.
	*/

	/* Create the hash table first time through. */
241
	if (directory_objects == NULL)
242 243 244 245 246 247 248 249 250
		directory_objects = g_hash_table_new (g_str_hash, g_str_equal);

	/* If the object is already in the hash table, look it up. */
	directory = g_hash_table_lookup (directory_objects, uri);
	if (directory != NULL) {
		g_assert (NAUTILUS_IS_DIRECTORY (directory));
		gtk_object_ref (GTK_OBJECT (directory));
	} else {
		/* Create a new directory object instead. */
251
		directory = nautilus_directory_new (uri);
252
		if (directory == NULL)
253 254
			return NULL;

255 256 257 258 259 260 261 262 263 264 265
		g_assert (strcmp (directory->details->uri_text, uri) == 0);

		/* Put it in the hash table. */
		gtk_object_ref (GTK_OBJECT (directory));
		gtk_object_sink (GTK_OBJECT (directory));
		g_hash_table_insert (directory_objects, directory->details->uri_text, directory);
	}

	return directory;
}

266 267 268 269 270 271 272 273
char *
nautilus_directory_get_uri (NautilusDirectory *directory)
{
	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), NULL);

	return g_strdup (directory->details->uri_text);
}

274 275 276 277 278
/* This reads the metafile synchronously. This must go eventually.
   To do this asynchronously we'd need a way to read an entire file
   with async. calls; currently you can only get the file length with
   a synchronous call.
*/
279
static GnomeVFSResult
280
nautilus_directory_try_to_read_metafile (NautilusDirectory *directory, GnomeVFSURI *metafile_uri)
281 282 283 284
{
	GnomeVFSResult result;
	GnomeVFSFileInfo metafile_info;
	GnomeVFSHandle *metafile_handle;
285 286 287 288
	size_t size; /* not GnomeVFSFileSize, since it's passed to g_malloc */
	GnomeVFSFileSize actual_size;
	char *buffer;
	
289 290
	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), GNOME_VFS_ERROR_GENERIC);
	g_return_val_if_fail (directory->details->metafile_tree == NULL, GNOME_VFS_ERROR_GENERIC);
291

292
	result = gnome_vfs_get_file_info_uri (metafile_uri,
293 294 295 296
					      &metafile_info,
					      GNOME_VFS_FILE_INFO_DEFAULT,
					      NULL);

297 298
	if (result == GNOME_VFS_OK) {
		/* Check for the case where the info doesn't give the file size. */
299
		if ((metafile_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE) == 0)
300 301 302
			result = GNOME_VFS_ERROR_GENERIC;
	}

303 304
	if (result == GNOME_VFS_OK) {
		/* Check for a size that won't fit into a size_t. */
305 306 307
		size = metafile_info.size;
		if (size != metafile_info.size)
			result = GNOME_VFS_ERROR_TOOBIG;
308 309
	}

310
	metafile_handle = NULL;
311
	if (result == GNOME_VFS_OK) {
312
		result = gnome_vfs_open_uri (&metafile_handle,
313
					     metafile_uri,
314
					     GNOME_VFS_OPEN_READ);
315
	}
316

317
	if (result == GNOME_VFS_OK) {
318
		buffer = g_malloc (size);
319
		result = gnome_vfs_read (metafile_handle, buffer, size, &actual_size);
320
		directory->details->metafile_tree = xmlParseMemory (buffer, actual_size);
321
		g_free (buffer);
322
	}
323

324
	if (metafile_handle != NULL)
325 326
		gnome_vfs_close (metafile_handle);

327 328 329 330 331 332 333 334 335 336
	return result;
}

static void
nautilus_directory_read_metafile (NautilusDirectory *directory)
{
	GnomeVFSResult result;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));

337 338 339 340 341 342 343 344 345
	/* Check for the alternate metafile first.
	   If we read from it, then write to it later.
	*/
	directory->details->use_alternate_metafile = FALSE;
	result = nautilus_directory_try_to_read_metafile (directory, directory->details->alternate_metafile_uri);
	if (result == GNOME_VFS_OK)
		directory->details->use_alternate_metafile = TRUE;
	else
		result = nautilus_directory_try_to_read_metafile (directory, directory->details->metafile_uri);
346 347 348 349 350 351 352 353 354 355 356 357
}

static void
nautilus_directory_remove_write_metafile_idle (NautilusDirectory *directory)
{
	if (directory->details->write_metafile_idle_id != 0) {
		gtk_idle_remove (directory->details->write_metafile_idle_id);
		directory->details->write_metafile_idle_id = 0;
	}
}

/* This writes the metafile synchronously. This must go eventually. */
358
static GnomeVFSResult
359
nautilus_directory_try_to_write_metafile (NautilusDirectory *directory, GnomeVFSURI *metafile_uri)
360 361 362 363 364 365 366
{
	xmlChar *buffer;
	int buffer_size;
	GnomeVFSResult result;
	GnomeVFSHandle *metafile_handle;
	GnomeVFSFileSize actual_size;

367 368 369
	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), GNOME_VFS_ERROR_GENERIC);
	g_return_val_if_fail (directory->details != NULL, GNOME_VFS_ERROR_GENERIC);
	g_return_val_if_fail (directory->details->metafile_tree != NULL, GNOME_VFS_ERROR_GENERIC);
370 371 372

	metafile_handle = NULL;
	result = gnome_vfs_create_uri (&metafile_handle,
373
				       metafile_uri,
374 375
				       GNOME_VFS_OPEN_WRITE,
				       FALSE,
376
				       METAFILE_PERMISSIONS);
377

378
	buffer = NULL;
379
	if (result == GNOME_VFS_OK) {
380
		xmlDocDumpMemory (directory->details->metafile_tree, &buffer, &buffer_size);
381
		result = gnome_vfs_write (metafile_handle, buffer, buffer_size, &actual_size);
382 383
		if (buffer_size != actual_size)
			result = GNOME_VFS_ERROR_GENERIC;
384 385 386 387 388 389
	}

	if (metafile_handle != NULL)
		gnome_vfs_close (metafile_handle);

	xmlFree (buffer);
390 391 392 393 394 395 396 397 398 399 400 401

	return result;
}

static void
nautilus_directory_write_metafile (NautilusDirectory *directory)
{
	GnomeVFSResult result;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));

	/* We are about the write the metafile, so we can cancel the pending
402 403
	   request to do so.
	*/
404 405 406 407 408 409 410 411 412
	nautilus_directory_remove_write_metafile_idle (directory);

	/* Don't write anything if there's nothing to write.
	   At some point, we might want to change this to actually delete
	   the metafile in this case.
	*/
	if (directory->details->metafile_tree == NULL)
		return;

413 414 415 416 417
	/* Try the main URI, unless we have already been instructed to use the alternate URI. */
	if (directory->details->use_alternate_metafile)
		result = GNOME_VFS_ERROR_ACCESSDENIED;
	else
		result = nautilus_directory_try_to_write_metafile (directory, directory->details->metafile_uri);
418

419 420 421 422 423
	/* Try the alternate URI if the main one failed. */
	if (result != GNOME_VFS_OK)
		result = nautilus_directory_try_to_write_metafile (directory, directory->details->alternate_metafile_uri);

	/* Check for errors. Later this must be reported to the user, not spit out as a warning. */
424 425
	if (result != GNOME_VFS_OK)
		g_warning ("nautilus_directory_write_metafile failed to write metafile - we should report this to the user");
426 427 428
}

static gboolean
429
nautilus_directory_write_metafile_idle_cb (gpointer callback_data)
430
{
431 432 433 434 435 436 437
	NautilusDirectory *directory;

	directory = NAUTILUS_DIRECTORY (callback_data);

	directory->details->write_metafile_idle_id = 0;
	nautilus_directory_write_metafile (directory);

438 439 440 441 442 443 444 445 446
	return FALSE;
}

static void
nautilus_directory_request_write_metafile (NautilusDirectory *directory)
{
	/* Set up an idle task that will write the metafile. */
	if (directory->details->write_metafile_idle_id == 0)
		directory->details->write_metafile_idle_id =
447
			gtk_idle_add (nautilus_directory_write_metafile_idle_cb,
448 449
				      directory);
}
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

/* To use a directory name as a file name, we need to escape any slashes.
   This means that "/" is replaced by "%2F" and "%" is replaced by "%25".
   Later we might share the escaping code with some more generic escaping
   function, but this should do for now.
*/
static char *
nautilus_directory_escape_slashes (const char *path)
{
	char c;
	const char *in;
	guint length;
	char *result;
	char *out;

	/* Figure out how long the result needs to be. */
	in = path;
	length = 0;
	while ((c = *in++) != '\0')
		switch (c) {
		case '/':
		case '%':
			length += 3;
			break;
		default:
			length += 1;
		}

	/* Create the result string. */
	result = g_malloc (length + 1);
	in = path;
	out = result;	
	while ((c = *in++) != '\0')
		switch (c) {
		case '/':
			*out++ = '%';
			*out++ = '2';
			*out++ = 'F';
			break;
		case '%':
			*out++ = '%';
			*out++ = '2';
			*out++ = '5';
			break;
		default:
			*out++ = c;
		}
	g_assert (out == result + length);
	*out = '\0';

	return result;
}

static GnomeVFSResult
nautilus_make_directory_and_parents (GnomeVFSURI *uri, guint permissions)
{
	GnomeVFSResult result;
	GnomeVFSURI *parent_uri;

	/* Make the directory, and return right away unless there's
	   a possible problem with the parent.
	*/
	result = gnome_vfs_make_directory_for_uri (uri, permissions);
	if (result != GNOME_VFS_ERROR_NOTFOUND)
		return result;

	/* If we can't get a parent, we are done. */
	parent_uri = gnome_vfs_uri_get_parent (uri);
	if (parent_uri == NULL)
		return result;

	/* If we can get a parent, use a recursive call to create
	   the parent and its parents.
	*/
	result = nautilus_make_directory_and_parents (parent_uri, permissions);
	gnome_vfs_uri_unref (parent_uri);
	if (result != GNOME_VFS_OK)
		return result;

	/* A second try at making the directory after the parents
	   have all been created.
	*/
	result = gnome_vfs_make_directory_for_uri (uri, permissions);
	return result;
}

536
static GnomeVFSURI *
537
nautilus_directory_construct_alternate_metafile_uri (GnomeVFSURI *uri)
538 539 540 541 542 543 544 545 546 547 548 549 550 551
{
	GnomeVFSResult result;
	GnomeVFSURI *home_uri, *nautilus_directory_uri, *metafiles_directory_uri, *alternate_uri;
	char *uri_as_string, *escaped_uri, *file_name;

	/* Ensure that the metafiles directory exists. */
	home_uri = gnome_vfs_uri_new (g_get_home_dir ());
	nautilus_directory_uri = gnome_vfs_uri_append_path (home_uri, NAUTILUS_DIRECTORY_NAME);
	gnome_vfs_uri_unref (home_uri);
	metafiles_directory_uri = gnome_vfs_uri_append_path (nautilus_directory_uri, METAFILES_DIRECTORY_NAME);
	gnome_vfs_uri_unref (nautilus_directory_uri);
	result = nautilus_make_directory_and_parents (metafiles_directory_uri, METAFILES_DIRECTORY_PERMISSIONS);
	if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_FILEEXISTS) {
		gnome_vfs_uri_unref (metafiles_directory_uri);
552
		return NULL;
553 554 555
	}

	/* Construct a file name from the URI. */
556
	uri_as_string = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
557 558 559 560 561 562 563 564 565 566
	escaped_uri = nautilus_directory_escape_slashes (uri_as_string);
	g_free (uri_as_string);
	file_name = g_strconcat (escaped_uri, ".xml", NULL);
	g_free (escaped_uri);

	/* Construct a URI for something in the "metafiles" directory. */
	alternate_uri = gnome_vfs_uri_append_path (metafiles_directory_uri, file_name);
	gnome_vfs_uri_unref (metafiles_directory_uri);
	g_free (file_name);

567
	return alternate_uri;
568
}
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
      
#if NAUTILUS_DIRECTORY_ASYNC

static void
nautilus_directory_opened_metafile (GnomeVFSAsyncHandle *handle,
				    GnomeVFSResult result,
				    gpointer callback_data)
{
}

	result = gnome_vfs_async_open_uri (&metafile_handle, metafile_uri, GNOME_VFS_OPEN_READ,
					   nautilus_directory_opened_metafile, directory);
#endif

static NautilusDirectory *
nautilus_directory_new (const char* uri)
{
	NautilusDirectory *directory;
	GnomeVFSURI *vfs_uri;
	GnomeVFSURI *metafile_uri;
589
	GnomeVFSURI *alternate_metafile_uri;
590 591 592 593 594 595

	vfs_uri = gnome_vfs_uri_new (uri);
	if (vfs_uri == NULL)
		return NULL;

	metafile_uri = gnome_vfs_uri_append_path (vfs_uri, METAFILE_NAME);
596
	alternate_metafile_uri = nautilus_directory_construct_alternate_metafile_uri (vfs_uri);
597 598 599

	directory = gtk_type_new (NAUTILUS_TYPE_DIRECTORY);

600
	directory->details->uri_text = g_strdup(uri);
601 602
	directory->details->uri = vfs_uri;
	directory->details->metafile_uri = metafile_uri;
603
	directory->details->alternate_metafile_uri = alternate_metafile_uri;
604 605 606 607 608 609 610

	nautilus_directory_read_metafile (directory);

	return directory;
}

void
611 612 613
nautilus_directory_start_monitoring (NautilusDirectory *directory,
				     NautilusFileListCallback callback,
				     gpointer callback_data)
614
{
615 616
	GnomeVFSResult result;

617 618 619 620 621 622 623
	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));
	g_return_if_fail (callback != NULL);

	if (directory->details->files != NULL)
		(* callback) (directory,
			      directory->details->files,
			      callback_data);
624

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
	if (directory->details->directory_loaded)
		return;
	if (directory->details->directory_load_in_progress != NULL)
		return;
	
	directory->details->directory_load_list_last_handled
		= GNOME_VFS_DIRECTORY_LIST_POSITION_NONE;
	result = gnome_vfs_async_load_directory_uri
		(&directory->details->directory_load_in_progress, /* handle */
		 directory->details->uri,                         /* uri */
		 (GNOME_VFS_FILE_INFO_GETMIMETYPE	          /* options */
		  | GNOME_VFS_FILE_INFO_FASTMIMETYPE
		  | GNOME_VFS_FILE_INFO_FOLLOWLINKS),
		 NULL, 					          /* meta_keys */
		 NULL, 					          /* sort_rules */
		 FALSE, 				          /* reverse_order */
		 GNOME_VFS_DIRECTORY_FILTER_NONE,                 /* filter_type */
		 (GNOME_VFS_DIRECTORY_FILTER_NOSELFDIR            /* filter_options */
		  | GNOME_VFS_DIRECTORY_FILTER_NOPARENTDIR),
		 NULL,                                            /* filter_pattern */
		 DIRECTORY_LOAD_ITEMS_PER_CB,                     /* items_per_notification */
		 nautilus_directory_load_cb,                      /* callback */
		 directory);
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
}

static gboolean
dequeue_pending_idle_cb (gpointer callback_data)
{
	NautilusDirectory *directory;
	GList *pending_file_info;
	GList *p;
	NautilusFile *file;
	NautilusFileList *pending_files;

	directory = NAUTILUS_DIRECTORY (callback_data);

	directory->details->dequeue_pending_idle_id = 0;

	pending_files = NULL;

	/* Build a list of NautilusFile objects. */
	pending_file_info = directory->details->pending_file_info;
	directory->details->pending_file_info = NULL;
	for (p = pending_file_info; p != NULL; p = p->next) {
		file = nautilus_directory_new_file (directory, p->data);
		pending_files = g_list_prepend (pending_files, file);
		gnome_vfs_file_info_unref (p->data);
	}
	g_list_free (pending_file_info);

	if (pending_files == NULL)
		return FALSE;

	/* Tell the people who are monitoring about these new files. */
	gtk_signal_emit (GTK_OBJECT (directory),
			 nautilus_directory_signals[FILES_ADDED],
			 pending_files);

	/* Remember them for later. */
	directory->details->files = g_list_concat
		(directory->details->files, pending_files);

	return FALSE;
}

static void
schedule_dequeue_pending (NautilusDirectory *directory)
{
	if (directory->details->dequeue_pending_idle_id == 0)
		directory->details->dequeue_pending_idle_id
			= gtk_idle_add (dequeue_pending_idle_cb, directory);
}

static void
nautilus_directory_load_one (NautilusDirectory *directory,
			     GnomeVFSFileInfo *info)
{
	gnome_vfs_file_info_ref (info);
        directory->details->pending_file_info
		= g_list_prepend (directory->details->pending_file_info, info);
	schedule_dequeue_pending (directory);
}

static void
nautilus_directory_load_done (NautilusDirectory *directory,
			      GnomeVFSResult result)
{
	directory->details->directory_load_in_progress = NULL;
	directory->details->directory_loaded = TRUE;
	schedule_dequeue_pending (directory);
}

static GnomeVFSDirectoryListPosition
nautilus_gnome_vfs_directory_list_get_next_position (GnomeVFSDirectoryList *list,
						     GnomeVFSDirectoryListPosition position)
{
	if (position != GNOME_VFS_DIRECTORY_LIST_POSITION_NONE)
		return gnome_vfs_directory_list_position_next (position);
	if (list == NULL)
		return GNOME_VFS_DIRECTORY_LIST_POSITION_NONE;
	return gnome_vfs_directory_list_get_first_position (list);
}

static void
nautilus_directory_load_cb (GnomeVFSAsyncHandle *handle,
			    GnomeVFSResult result,
			    GnomeVFSDirectoryList *list,
			    guint entries_read,
			    gpointer callback_data)
{
	NautilusDirectory *directory;
	GnomeVFSDirectoryListPosition last_handled, p;

	directory = NAUTILUS_DIRECTORY (callback_data);

	g_assert (directory->details->directory_load_in_progress == handle);

	/* Move items from the list onto our pending queue.
	 * We can't do this in the most straightforward way, becuse the position
	 * for a gnome_vfs_directory_list does not have a way of representing one
	 * past the end. So we must keep a position to the last item we handled
	 * rather than keeping a position past the last item we handled.
	 */
	last_handled = directory->details->directory_load_list_last_handled;
        p = last_handled;
	while ((p = nautilus_gnome_vfs_directory_list_get_next_position (list, p))
	       != GNOME_VFS_DIRECTORY_LIST_POSITION_NONE) {
		nautilus_directory_load_one
			(directory, gnome_vfs_directory_list_get (list, p));
		last_handled = p;
	}
	directory->details->directory_load_list_last_handled = last_handled;

	if (result != GNOME_VFS_OK)
		nautilus_directory_load_done (directory, result);
}

void
nautilus_directory_stop_monitoring (NautilusDirectory *directory)
{
	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));

	if (directory->details->directory_load_in_progress != NULL) {
		gnome_vfs_async_cancel (directory->details->directory_load_in_progress);
		directory->details->directory_load_in_progress = NULL;
	}
}

gboolean
nautilus_directory_are_all_files_seen (NautilusDirectory *directory)
{
	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), FALSE);
	
	return directory->details->directory_loaded;
779 780
}

781 782 783 784
static char *
nautilus_directory_get_metadata_from_node (xmlNode *node,
					   const char *tag,
					   const char *default_metadata)
785 786 787 788 789 790 791
{
	xmlChar *property;
	char *result;

	g_return_val_if_fail (tag, NULL);
	g_return_val_if_fail (tag[0], NULL);

792
	property = xmlGetProp (node, tag);
793 794 795 796
	if (property == NULL)
		result = g_strdup (default_metadata);
	else
		result = g_strdup (property);
797
	xmlFree (property);
798 799 800 801

	return result;
}

802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
static xmlNode *
nautilus_directory_create_metafile_tree_root (NautilusDirectory *directory)
{
	xmlNode *root;

	if (directory->details->metafile_tree == NULL)
		directory->details->metafile_tree = xmlNewDoc (METAFILE_XML_VERSION);
	root = xmlDocGetRootElement (directory->details->metafile_tree);
	if (root == NULL) {
		root = xmlNewDocNode (directory->details->metafile_tree, NULL, "DIRECTORY", NULL);
		xmlDocSetRootElement (directory->details->metafile_tree, root);
	}

	return root;
}

char *
nautilus_directory_get_metadata (NautilusDirectory *directory,
				 const char *tag,
				 const char *default_metadata)
{
	/* It's legal to call this on a NULL directory. */
	if (directory == NULL)
825 826
		return g_strdup (default_metadata);

827 828 829 830 831 832 833 834
	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), NULL);

	/* The root itself represents the directory. */
	return nautilus_directory_get_metadata_from_node
		(xmlDocGetRootElement (directory->details->metafile_tree),
		 tag, default_metadata);
}

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
void
nautilus_directory_set_metadata (NautilusDirectory *directory,
				 const char *tag,
				 const char *default_metadata,
				 const char *metadata)
{
	char *old_metadata;
	gboolean old_metadata_matches;
	xmlNode *root;
	const char *value;
	xmlAttr *property_node;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));
	g_return_if_fail (tag);
	g_return_if_fail (tag[0]);

	/* If the data in the metafile is already correct, do nothing. */
	old_metadata = nautilus_directory_get_metadata (directory, tag, default_metadata);
	old_metadata_matches = nautilus_strcmp (old_metadata, metadata) == 0;
	g_free (old_metadata);
	if (old_metadata_matches)
		return;

	/* Data that matches the default is represented in the tree by
	   the lack of an attribute.
	*/
	if (nautilus_strcmp (default_metadata, metadata) == 0)
		value = NULL;
	else
		value = metadata;

	/* Get at the tree. */
867
	root = nautilus_directory_create_metafile_tree_root (directory);
868 869 870 871 872 873 874 875 876 877

	/* Add or remove an attribute node. */
	property_node = xmlSetProp (root, tag, value);
	if (value == NULL)
		xmlRemoveProp (property_node);

	/* Since we changed the tree, arrange for it to be written. */
	nautilus_directory_request_write_metafile (directory);
}

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
gboolean 
nautilus_directory_get_boolean_metadata (NautilusDirectory *directory,
					 const char *tag,
					 gboolean default_metadata)
{
	char *result_as_string;
	gboolean result;

	result_as_string = nautilus_directory_get_metadata (
				directory,
				tag,
				default_metadata ? "TRUE" : "FALSE");

	/* Handle oddball case of non-existent directory */
	if (result_as_string == NULL)
		return default_metadata;

	if (strcmp (result_as_string, "TRUE") == 0)
	{
		result = TRUE;
	} 
	else if (strcmp (result_as_string, "FALSE") == 0)
	{
		result = FALSE;
	}
	else
	{
		g_assert_not_reached ();
		result = default_metadata;
	}

	g_free (result_as_string);
	return result;

}

void               
nautilus_directory_set_boolean_metadata (NautilusDirectory *directory,
					 const char *tag,
					 gboolean default_metadata,
					 gboolean metadata)
{
	nautilus_directory_set_metadata (directory,
					 tag,
					 default_metadata ? "TRUE" : "FALSE",
					 metadata ? "TRUE" : "FALSE");
}

int 
nautilus_directory_get_integer_metadata (NautilusDirectory *directory,
					 const char *tag,
					 int default_metadata)
{
	char *result_as_string;
	char *default_as_string;
	int result;

	default_as_string = g_strdup_printf ("%d", default_metadata);
	result_as_string = nautilus_directory_get_metadata (
				directory,
				tag,
				default_as_string);

	/* Handle oddball case of non-existent directory */
	if (result_as_string == NULL)
	{
		result = default_metadata;
	}
	else
	{
		result = atoi (result_as_string);
		g_free (result_as_string);
	}

	g_free (default_as_string);
	return result;

}

void               
nautilus_directory_set_integer_metadata (NautilusDirectory *directory,
					 const char *tag,
					 int default_metadata,
					 int metadata)
{
	char *value_as_string;
	char *default_as_string;

	value_as_string = g_strdup_printf ("%d", metadata);
	default_as_string = g_strdup_printf ("%d", default_metadata);

	nautilus_directory_set_metadata (directory,
					 tag,
					 default_as_string,
					 value_as_string);

	g_free (value_as_string);
	g_free (default_as_string);
}


979
static char *
980 981 982 983 984 985 986 987 988 989 990 991
nautilus_directory_get_file_metadata (NautilusDirectory *directory,
				      const char *file_name,
				      const char *tag,
				      const char *default_metadata)
{
	xmlNode *root, *child;
	xmlChar *property;

	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), NULL);

	/* The root itself represents the directory. */
	root = xmlDocGetRootElement (directory->details->metafile_tree);
992 993
	if (root == NULL)
		return g_strdup (default_metadata);
994 995 996

	/* The children represent the files.
	   This linear search is temporary.
997
	   Eventually, we'll have a pointer from the NautilusFile right to
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	   the corresponding XML node, or we won't have the XML tree
	   in memory at all.
	*/
	for (child = root->childs; child != NULL; child = child->next)
		if (strcmp (child->name, "FILE") == 0) {
			property = xmlGetProp (child, "NAME");
			if (nautilus_eat_strcmp (property, file_name) == 0)
				break;
		}

	/* If we found a child, we can get the data from it. */
	return nautilus_directory_get_metadata_from_node
		(child, tag, default_metadata);
}

1013
static void
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
nautilus_directory_set_file_metadata (NautilusDirectory *directory,
				      const char *file_name,
				      const char *tag,
				      const char *default_metadata,
				      const char *metadata)
{
	char *old_metadata;
	gboolean old_metadata_matches;
	xmlNode *root, *child;
	const char *value;
	xmlChar *property;
	xmlAttr *property_node;

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));
	g_return_if_fail (tag);
	g_return_if_fail (tag[0]);

	/* If the data in the metafile is already correct, do nothing. */
	old_metadata = nautilus_directory_get_file_metadata
		(directory, file_name, tag, default_metadata);
	old_metadata_matches = nautilus_strcmp (old_metadata, metadata) == 0;
	g_free (old_metadata);
	if (old_metadata_matches)
		return;

	/* Data that matches the default is represented in the tree by
	   the lack of an attribute.
	*/
	if (nautilus_strcmp (default_metadata, metadata) == 0)
		value = NULL;
	else
		value = metadata;

	/* The root itself represents the directory. */
	root = nautilus_directory_create_metafile_tree_root (directory);
	
	/* The children represent the files.
	   This linear search is temporary.
1052
	   Eventually, we'll have a pointer from the NautilusFile right to
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	   the corresponding XML node, or we won't have the XML tree
	   in memory at all.
	*/
	for (child = root->childs; child != NULL; child = child->next)
		if (strcmp (child->name, "FILE") == 0) {
			property = xmlGetProp (child, "NAME");
			if (nautilus_eat_strcmp (property, file_name) == 0)
				break;
		}
	if (child == NULL) {
		g_assert (value != NULL);
		child = xmlNewChild (root, NULL, "FILE", NULL);
		xmlSetProp (child, "NAME", file_name);
	}

	/* Add or remove an attribute node. */
	property_node = xmlSetProp (child, tag, value);
	if (value == NULL)
		xmlRemoveProp (property_node);
	
	/* Since we changed the tree, arrange for it to be written. */
	nautilus_directory_request_write_metafile (directory);
}

1077
static NautilusFile *
1078 1079 1080 1081 1082 1083 1084 1085 1086
nautilus_directory_new_file (NautilusDirectory *directory, GnomeVFSFileInfo *info)
{
	NautilusFile *file;

	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), NULL);
	g_return_val_if_fail (info != NULL, NULL);

	gnome_vfs_file_info_ref (info);

1087
	file = g_new0 (NautilusFile, 1);
1088 1089 1090 1091 1092 1093
	file->directory = directory;
	file->info = info;

	return file;
}

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
/**
 * nautilus_file_get:
 * @uri: URI of file to get.
 *
 * Get a file given a uri.
 * Returns a referenced object. Unref when finished.
 * If two windows are viewing the same uri, the file object is shared.
 */
NautilusFile *
nautilus_file_get (const char *uri)
{
	GnomeVFSResult result;
1106
	GnomeVFSFileInfo *file_info;
1107 1108 1109 1110 1111 1112
	GnomeVFSURI *vfs_uri, *directory_vfs_uri;
	char *directory_uri;
	NautilusDirectory *directory;
	NautilusFile *file;

	/* Get info on the file. */
1113 1114
	file_info = gnome_vfs_file_info_new ();
	result = gnome_vfs_get_file_info (uri, file_info,
1115 1116 1117
					  GNOME_VFS_FILE_INFO_GETMIMETYPE
					  | GNOME_VFS_FILE_INFO_FASTMIMETYPE
		  			  | GNOME_VFS_FILE_INFO_FOLLOWLINKS, NULL);
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
	if (result != GNOME_VFS_OK)
		return NULL;

	/* Make VFS version of URI. */
	vfs_uri = gnome_vfs_uri_new (uri);
	if (vfs_uri == NULL)
		return NULL;

	/* Make VFS version of directory URI. */
	directory_vfs_uri = gnome_vfs_uri_get_parent (vfs_uri);
	gnome_vfs_uri_unref (vfs_uri);
	if (directory_vfs_uri == NULL)
		return NULL;

	/* Make text version of directory URI. */
	directory_uri = gnome_vfs_uri_to_string (directory_vfs_uri,
						 GNOME_VFS_URI_HIDE_NONE);
	gnome_vfs_uri_unref (directory_vfs_uri);

	/* Get object that represents the directory. */
	directory = nautilus_directory_get (directory_uri);
	g_free (directory_uri);
	if (directory == NULL)
		return NULL;

1143 1144 1145 1146
	file = nautilus_directory_new_file (directory, file_info);

	gnome_vfs_file_info_unref (file_info);
	nautilus_file_ref (file);
1147
	gtk_object_unref (GTK_OBJECT (directory));
1148
	
1149 1150 1151
	return file;
}

1152 1153 1154 1155 1156
void
nautilus_file_ref (NautilusFile *file)
{
	g_return_if_fail (file != NULL);

1157
	g_assert (file->ref_count < G_MAXINT);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
	g_assert (file->directory != NULL);

	/* Increment the ref count. */
	if (file->ref_count++ != 0)
		return;

	/* As soon as someone other than the directory holds a ref, 
	 * we need to hold the directory too. */
	gtk_object_ref (GTK_OBJECT (file->directory));
}

1169 1170 1171 1172
void
nautilus_file_unref (NautilusFile *file)
{
	g_return_if_fail (file != NULL);
1173 1174 1175

	g_assert (file->ref_count != 0);
	g_assert (file->directory != NULL);
1176 1177 1178 1179 1180

	/* Decrement the ref count. */
	if (--file->ref_count != 0)
		return;

1181 1182 1183 1184 1185 1186 1187 1188 1189
	/* No references left, so it's time to release our hold on the directory. */
	gtk_object_unref (GTK_OBJECT (file->directory));
}

static void
nautilus_file_detach (NautilusFile *file)
{
	g_assert (file->ref_count == 0);

1190 1191 1192 1193
	/* Destroy the file object. */
	gnome_vfs_file_info_unref (file->info);
}

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
static int
nautilus_file_compare_by_size_with_directories (NautilusFile *file_1, NautilusFile *file_2)
{
	gboolean is_directory_1;
	gboolean is_directory_2;
	int item_count_1;
	int item_count_2;

	is_directory_1 = file_1->info->type == GNOME_VFS_FILE_TYPE_DIRECTORY;
	is_directory_2 = file_2->info->type == GNOME_VFS_FILE_TYPE_DIRECTORY;

	if (is_directory_1 && !is_directory_2)
		return -1;

	if (is_directory_2 && !is_directory_1)
		return +1;

	if (!is_directory_1 && !is_directory_2)
		return 0;

	/* Both are directories, compare by item count. */
	/* FIXME: get_directory_item_count_hack is slow, and calling
	 * it for every pairwise comparison here is nasty. Need to
	 * change this to (not-yet-existent) architecture where the
	 * item count can be calculated once in a deferred way, and
	 * then stored or cached.
	 */
	item_count_1 = get_directory_item_count_hack (file_1, FALSE);
	item_count_2 = get_directory_item_count_hack (file_2, FALSE);

	if (item_count_1 < item_count_2)
		return -1;

	if (item_count_2 < item_count_1)
		return +1;

	return 0;
}

1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
static int
nautilus_file_compare_by_type (NautilusFile *file_1, NautilusFile *file_2)
{
	gboolean is_directory_1;
	gboolean is_directory_2;
	char * type_string_1;
	char * type_string_2;
	int result;

	/* Directories go first. Then, if mime types are identical,
	 * don't bother getting strings (for speed). This assumes
	 * that the string is dependent entirely on the mime type,
	 * which is true now but might not be later.
	 */
	is_directory_1 = file_1->info->type == GNOME_VFS_FILE_TYPE_DIRECTORY;
	is_directory_2 = file_2->info->type == GNOME_VFS_FILE_TYPE_DIRECTORY;
	
	if (is_directory_1 && is_directory_2)
		return 0;

	if (is_directory_1)
		return -1;

	if (is_directory_2)
		return +1;

	if (nautilus_strcmp (file_1->info->mime_type, file_2->info->mime_type) == 0)
		return 0;

	type_string_1 = nautilus_file_get_type_as_string (file_1);
	type_string_2 = nautilus_file_get_type_as_string (file_2);

	result = nautilus_strcmp (type_string_1, type_string_2);

	g_free (type_string_1);
	g_free (type_string_2);

	return result;
}

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
static int
nautilus_file_compare_for_sort_internal (NautilusFile *file_1,
					 NautilusFile *file_2,
					 NautilusFileSortType sort_type,
					 gboolean reversed)
{
	GnomeVFSDirectorySortRule *rules;

	g_return_val_if_fail (file_1 != NULL, 0);
	g_return_val_if_fail (file_2 != NULL, 0);
	g_return_val_if_fail (sort_type != NAUTILUS_FILE_SORT_NONE, 0);

#define ALLOC_RULES(n) alloca ((n) * sizeof (GnomeVFSDirectorySortRule))

	switch (sort_type) {
	case NAUTILUS_FILE_SORT_BY_NAME:
		rules = ALLOC_RULES (2);
		/* Note: This used to put directories first. I
		 * thought that was counterintuitive and removed it,
		 * but I can imagine discussing this further.
		 * John Sullivan <sullivan@eazel.com>
		 */
1295
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYNAME_IGNORECASE;
1296 1297 1298
		rules[1] = GNOME_VFS_DIRECTORY_SORT_NONE;
		break;
	case NAUTILUS_FILE_SORT_BY_SIZE:
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
		/* Compare directory sizes ourselves, then if necessary
		 * use GnomeVFS to compare file sizes.
		 */
		{
		int size_compare;

		size_compare = nautilus_file_compare_by_size_with_directories (file_1, file_2);
		if (size_compare != 0)
			return size_compare;
		}
		rules = ALLOC_RULES (3);
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYSIZE;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_BYNAME_IGNORECASE;
		rules[2] = GNOME_VFS_DIRECTORY_SORT_NONE;
1313 1314
		break;
	case NAUTILUS_FILE_SORT_BY_TYPE:
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
		/* GnomeVFS doesn't know about our special text for certain
		 * mime types, so we handle the mime-type sorting ourselves.
		 */
		{
		int type_compare;

		type_compare = nautilus_file_compare_by_type (file_1, file_2);
		if (type_compare != 0)
			return type_compare;
		}
		rules = ALLOC_RULES (2);
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYNAME_IGNORECASE;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_NONE;
1328 1329 1330 1331
		break;
	case NAUTILUS_FILE_SORT_BY_MTIME:
		rules = ALLOC_RULES (3);
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYMTIME;
1332
		rules[1] = GNOME_VFS_DIRECTORY_SORT_BYNAME_IGNORECASE;
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
		rules[2] = GNOME_VFS_DIRECTORY_SORT_NONE;
		break;
	default:
		g_assert_not_reached ();
		return 0;
	}

	if (reversed)
		return gnome_vfs_file_info_compare_for_sort_reversed (file_1->info,
								      file_2->info,
								      rules);
	else
		return gnome_vfs_file_info_compare_for_sort (file_1->info,
							     file_2->info,
							     rules);

#undef ALLOC_RULES
}

/**
 * nautilus_file_compare_for_sort:
 * @file_1: A file object
 * @file_2: Another file object
 * @sort_type: Sort criterion
 * 
 * Return value: int < 0 if @file_1 should come before file_2 in a smallest-to-largest
 * sorted list; int > 0 if @file_2 should come before file_1 in a smallest-to-largest
 * sorted list; 0 if @file_1 and @file_2 are equal for this sort criterion. Note
 * that each named sort type may actually break ties several ways, with the name
 * of the sort criterion being the primary but not only differentiator.
 **/
int
nautilus_file_compare_for_sort (NautilusFile *file_1,
				NautilusFile *file_2,
				NautilusFileSortType sort_type)
{
	return nautilus_file_compare_for_sort_internal (file_1, file_2, sort_type, FALSE);
}

/**
 * nautilus_file_compare_for_sort_reversed:
 * @file_1: A file object
 * @file_2: Another file object
 * @sort_type: Sort criterion
 * 
 * Return value: The opposite of nautilus_file_compare_for_sort: int > 0 if @file_1 
 * should come before file_2 in a smallest-to-largest sorted list; int < 0 if @file_2 
 * should come before file_1 in a smallest-to-largest sorted list; 0 if @file_1 
 * and @file_2 are equal for this sort criterion. Note that each named sort type 
 * may actually break ties several ways, with the name of the sort criterion 
 * being the primary but not only differentiator.
 **/
int
nautilus_file_compare_for_sort_reversed (NautilusFile *file_1,
					 NautilusFile *file_2,
					 NautilusFileSortType sort_type)
{
	return nautilus_file_compare_for_sort_internal (file_1, file_2, sort_type, TRUE);
}

1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
char *
nautilus_file_get_metadata (NautilusFile *file,
			    const char *tag,
			    const char *default_metadata)
{
	g_return_val_if_fail (file != NULL, NULL);

	return nautilus_directory_get_file_metadata (file->directory, file->info->name, tag, default_metadata);
}

void
nautilus_file_set_metadata (NautilusFile *file,
			    const char *tag,
			    const char *default_metadata,
			    const char *metadata)
{
	g_return_if_fail (file != NULL);

	nautilus_directory_set_file_metadata (file->directory, file->info->name, tag, default_metadata, metadata);
}

char *
nautilus_file_get_name (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, NULL);

1419 1420 1421 1422
	g_assert (file->directory == NULL || NAUTILUS_IS_DIRECTORY (file->directory));
	g_assert (file->info->name != NULL);
	g_assert (file->info->name[0] != '\0');

1423 1424 1425
	return g_strdup (file->info->name);
}

1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
char *
nautilus_file_get_uri (NautilusFile *file)
{
	GnomeVFSURI *uri;
	char *uri_text;

	g_return_val_if_fail (file != NULL, NULL);
	g_return_val_if_fail (file->directory != NULL, NULL);

	uri = gnome_vfs_uri_append_path (file->directory->details->uri, file->info->name);
	uri_text = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
	gnome_vfs_uri_unref (uri);
	return uri_text;
}

1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
/**
 * nautilus_file_get_date_as_string:
 * 
 * Get a user-displayable string representing a file modification date. 
 * The caller is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
1451
static char *
1452 1453
nautilus_file_get_date_as_string (NautilusFile *file, NautilusDateType date_type)
{	 
1454 1455 1456 1457 1458 1459
	struct tm *file_time;
	const char *format;
	GDate *today;
	GDate *file_date;
	guint32 file_date_age;

1460 1461
	g_return_val_if_fail (file != NULL, NULL);

1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
	switch (date_type)
	{
		case NAUTILUS_DATE_TYPE_CHANGED:
			file_time = localtime(&file->info->ctime);
			break;
		case NAUTILUS_DATE_TYPE_ACCESSED:
			file_time = localtime(&file->info->atime);
			break;
		case NAUTILUS_DATE_TYPE_MODIFIED:
			file_time = localtime(&file->info->mtime);
			break;
		default:
			g_assert_not_reached ();
	}
1476 1477
	file_date = nautilus_g_date_new_tm (file_time);
	
1478 1479
	today = g_date_new ();
	g_date_set_time (today, time (NULL));
1480

1481
	/* Overflow results in a large number; fine for our purposes. */
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
	file_date_age = g_date_julian (today) - g_date_julian (file_date);

	g_date_free (file_date);
	g_date_free (today);

	/* Format varies depending on how old the date is. This minimizes
	 * the length (and thus clutter & complication) of typical dates
	 * while providing sufficient detail for recent dates to make
	 * them maximally understandable at a glance. Keep all format
	 * strings separate rather than combining bits & pieces for
	 * internationalization's sake.
1493
	 */
1494 1495 1496 1497

	if (file_date_age == 0)
	{
		/* today, use special word */
1498
		format = _("today %-I:%M %p");
1499 1500 1501 1502
	}
	else if (file_date_age == 1)
	{
		/* yesterday, use special word */
1503
		format = _("yesterday %-I:%M %p");
1504 1505 1506 1507
	}
	else if (file_date_age < 7)
	{
		/* current week, include day of week */
1508
		format = _("%A %-m/%-d/%y %-I:%M %p");
1509 1510 1511
	}
	else
	{
1512
		format = _("%-m/%-d/%y %-I:%M %p");
1513 1514
	}

1515
	return nautilus_strdup_strftime (format, file_time);
1516 1517
}

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
/**
 * nautilus_file_get_size
 * 
 * Get the file size.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Size in bytes.
 * 
 **/
GnomeVFSFileSize
nautilus_file_get_size (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, 0);

	return file->info->size;
}

1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
/**
 * nautilus_file_get_permissions_as_string:
 * 
 * Get a user-displayable string representing a file's permissions. The caller
 * is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
static char *
nautilus_file_get_permissions_as_string (NautilusFile *file)
{
	GnomeVFSFilePermissions permissions;
	gboolean is_directory;
	gboolean is_link;

	permissions = file->info->permissions;
	is_directory = file->info->type == GNOME_VFS_FILE_TYPE_DIRECTORY;
	is_link = GNOME_VFS_FILE_INFO_SYMLINK(file->info);
	
	return g_strdup_printf ("%c%c%c%c%c%c%c%c%c%c",
				 is_link ? 'l' : is_directory ? 'd' : '-',
		 		 permissions & GNOME_VFS_PERM_USER_READ ? 'r' : '-',
				 permissions & GNOME_VFS_PERM_USER_WRITE ? 'w' : '-',
				 permissions & GNOME_VFS_PERM_USER_EXEC ? 'x' : '-',
				 permissions & GNOME_VFS_PERM_GROUP_READ ? 'r' : '-',
				 permissions & GNOME_VFS_PERM_GROUP_WRITE ? 'w' : '-',
				 permissions & GNOME_VFS_PERM_GROUP_EXEC ? 'x' : '-',
				 permissions & GNOME_VFS_PERM_OTHER_READ ? 'r' : '-',
				 permissions & GNOME_VFS_PERM_OTHER_WRITE ? 'w' : '-',
				 permissions & GNOME_VFS_PERM_OTHER_EXEC ? 'x' : '-');
}

/**
 * nautilus_file_get_owner_as_string:
 * 
 * Get a user-displayable string representing a file's owner. The caller
 * is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
static char *
nautilus_file_get_owner_as_string (NautilusFile *file)
{
	struct passwd *password_info;

	/* FIXME: Can we trust the uid in the file info? Might
	 * there be garbage there? What will it do for non-local files?
	 */
	/* No need to free result of getpwuid */
	password_info = getpwuid (file->info->uid);

	g_print ("pointer to password info is %p\n", password_info);

	if (password_info == NULL)
	{
		return g_strdup ("unknown owner");
	}
	
	return g_strdup (password_info->pw_name);
}

/**
 * nautilus_file_get_group_as_string:
 * 
 * Get a user-displayable string representing a file's group. The caller
 * is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
static char *
nautilus_file_get_group_as_string (NautilusFile *file)
{
	struct group *group_info;

	/* FIXME: Can we trust the gid in the file info? Might
	 * there be garbage there? What will it do for non-local files?
	 */
	/* No need to free result of getgrgid */
	group_info = getgrgid (file->info->gid);

	if (group_info == NULL)
	{
		return g_strdup ("unknown group");
	}
	
	return g_strdup (group_info->gr_name);
}


1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
/* This #include is part of the following hack, and should be removed with it */
#include <dirent.h>

static int
get_directory_item_count_hack (NautilusFile *file, gboolean ignore_invisible_items)
{
 	/* Code borrowed from Gnomad and hacked into here for now */

	char * uri;
	char * path;
    DIR* directory;
    int count;
    struct dirent * entry;

	g_assert (file->info->type == GNOME_VFS_FILE_TYPE_DIRECTORY);

	uri = nautilus_file_get_uri (file);
	if (nautilus_has_prefix (uri, "file://"))
		path = uri + 7;
	else
		path = uri;

	directory = opendir (path);

	g_free (uri);
    
    if (!directory)
        return 0;
        
    count = 0;
    
    while ((entry = readdir(directory)) != NULL)
        // Only count invisible items if requested.
        if (!ignore_invisible_items || entry->d_name[0] != '.')
            count += 1;
            
    closedir(directory);
1667 1668 1669

    /* This way of getting the count includes . and .., so we subtract those out */
    return count - 2;
1670 1671 1672

}

1673 1674 1675 1676
/**
 * nautilus_file_get_size_as_string:
 * 
 * Get a user-displayable string representing a file size. The caller
1677 1678
 * is responsible for g_free-ing this string. The string is an item
 * count for directories.
1679 1680 1681 1682 1683
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
1684
static char *
1685 1686 1687 1688 1689
nautilus_file_get_size_as_string (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, NULL);

	if (file->info->type == GNOME_VFS_FILE_TYPE_DIRECTORY)
1690
	{
1691 1692 1693 1694
		/* FIXME: Since computing the item count is slow, we
		 * want to do it in a deferred way. However, that
		 * architecture doesn't exist yet, so we're hacking
		 * it in for now.
1695
		 */
1696 1697 1698 1699
		int item_count;

		item_count = get_directory_item_count_hack (file, FALSE);
		if (item_count == 0)
1700
			return g_strdup ("0 items");
1701 1702 1703 1704
		else if (item_count == 1)
			return g_strdup ("1 item");
		else
			return g_strdup_printf ("%d items", item_count);
1705
	}
1706 1707 1708 1709

	return gnome_vfs_file_size_to_string (file->info->size);
}

1710 1711 1712 1713 1714 1715 1716 1717
/**
 * nautilus_file_get_string_attribute:
 * 
 * Get a user-displayable string from a named attribute. Use g_free to
 * free this string.
 * 
 * @file: NautilusFile representing the file in question.
 * @attribute_name: The name of the desired attribute. The currently supported
1718 1719
 * set includes "name", "type", "size", "date_modified", "date_changed",
 * "date_accessed", "owner", "group", "permissions".
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
 * 
 * Returns: Newly allocated string ready to display to the user, or NULL
 * if @attribute_name is not supported.
 * 
 **/
char *
nautilus_file_get_string_attribute (NautilusFile *file, const char *attribute_name)
{
	if (strcmp (attribute_name, "name") == 0)
		return nautilus_file_get_name (file);

	if (strcmp (attribute_name, "type") == 0)
		return nautilus_file_get_type_as_string (file);

	if (strcmp (attribute_name, "size") == 0)
		return nautilus_file_get_size_as_string (file);

	if (strcmp (attribute_name, "date_modified") == 0)
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
		return nautilus_file_get_date_as_string (file, 
							 NAUTILUS_DATE_TYPE_MODIFIED);

	if (strcmp (attribute_name, "date_changed") == 0)
		return nautilus_file_get_date_as_string (file, 
							 NAUTILUS_DATE_TYPE_CHANGED);

	if (strcmp (attribute_name, "date_accessed") == 0)
		return nautilus_file_get_date_as_string (file, 
							 NAUTILUS_DATE_TYPE_ACCESSED);
1748

1749 1750 1751 1752 1753 1754 1755 1756 1757
	if (strcmp (attribute_name, "permissions") == 0)
		return nautilus_file_get_permissions_as_string (file);

	if (strcmp (attribute_name, "owner") == 0)
		return nautilus_file_get_owner_as_string (file);

	if (strcmp (attribute_name, "group") == 0)
		return nautilus_file_get_group_as_string (file);

1758 1759 1760
	return NULL;
}

1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
/**
 * nautilus_file_get_type_as_string:
 * 
 * Get a user-displayable string representing a file type. The caller
 * is responsible for g_free-ing this string.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: Newly allocated string ready to display to the user.
 * 
 **/
1771
static char *
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
nautilus_file_get_type_as_string (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, NULL);

	if (file->info->type == GNOME_VFS_FILE_TYPE_DIRECTORY)
		/* Special-case this so it isn't "special/directory".
		 * Should this be "folder" instead?
		 */		
		return g_strdup (_("directory"));

1782 1783 1784
	if (nautilus_strlen (file->info->mime_type) == 0)
		return g_strdup (_("unknown type"));

1785 1786 1787
	return g_strdup (file->info->mime_type);
}

1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
/**
 * nautilus_file_get_type
 * 
 * Return this file's type.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: The type.
 * 
 **/
GnomeVFSFileType
nautilus_file_get_type (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return file->info->type;
}

/**
 * nautilus_file_get_mime_type
 * 
 * Return this file's mime type.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: The mime type.
 * 
 **/
const char *
nautilus_file_get_mime_type (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return file->info->mime_type;
}

/**
 * nautilus_file_is_symbolic_link
 * 
 * Check if this file is a symbolic link.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: True if the file is a symbolic link.
 * 
 **/
gboolean
nautilus_file_is_symbolic_link (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return GNOME_VFS_FILE_INFO_SYMLINK (file->info);
}

/**
 * nautilus_file_is_executable
 * 
 * Check if this file is executable at all.
 * @file: NautilusFile representing the file in question.
 * 
 * Returns: True if any of the execute bits are set.
 * 
 **/
gboolean
nautilus_file_is_executable (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, FALSE);

	return (file->info->flags & (GNOME_VFS_PERM_USER_EXEC
				     | GNOME_VFS_PERM_GROUP_EXEC
				     | GNOME_VFS_PERM_OTHER_EXEC)) != 0;
}

1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
#if !defined (NAUTILUS_OMIT_SELF_CHECK)

static int data_dummy;
static guint file_count;

static void
get_files_cb (NautilusDirectory *directory, NautilusFileList *files, gpointer data)
{
	g_assert (NAUTILUS_IS_DIRECTORY (directory));
	g_assert (files);
	g_assert (data == &data_dummy);

	file_count += g_list_length (files);
}

void
nautilus_self_check_directory (void)
{
	NautilusDirectory *directory;
1877 1878
	NautilusFile *file_1;
	NautilusFile *file_2;
1879 1880 1881

	directory = nautilus_directory_get ("file:///etc");

1882
	NAUTILUS_CHECK_INTEGER_RESULT (g_hash_table_size (directory_objects), 1);
1883 1884

	file_count = 0;
1885
	nautilus_directory_start_monitoring (directory, get_files_cb, &data_dummy);
1886

1887 1888 1889
	nautilus_directory_set_metadata (directory, "TEST", "default", "value");
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_get_metadata (directory, "TEST", "default"), "value");

1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
	nautilus_directory_set_boolean_metadata (directory, "TEST_BOOLEAN", TRUE, TRUE);
	NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_directory_get_boolean_metadata (directory, "TEST_BOOLEAN", TRUE), TRUE);
	nautilus_directory_set_boolean_metadata (directory, "TEST_BOOLEAN", TRUE, FALSE);
	NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_directory_get_boolean_metadata (directory, "TEST_BOOLEAN", TRUE), FALSE);
	NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_directory_get_boolean_metadata (NULL, "TEST_BOOLEAN", TRUE), TRUE);

	nautilus_directory_set_integer_metadata (directory, "TEST_INTEGER", 0, 17);
	NAUTILUS_CHECK_INTEGER_RESULT (nautilus_directory_get_integer_metadata (directory, "TEST_INTEGER", 0), 17);
	nautilus_directory_set_integer_metadata (directory, "TEST_INTEGER", 0, -1);
	NAUTILUS_CHECK_INTEGER_RESULT (nautilus_directory_get_integer_metadata (directory, "TEST_INTEGER", 0), -1);
	nautilus_directory_set_integer_metadata (directory, "TEST_INTEGER", 42, 42);
	NAUTILUS_CHECK_INTEGER_RESULT (nautilus_directory_get_integer_metadata (directory, "TEST_INTEGER", 42), 42);
	NAUTILUS_CHECK_INTEGER_RESULT (nautilus_directory_get_integer_metadata (NULL, "TEST_INTEGER", 42), 42);
	NAUTILUS_CHECK_INTEGER_RESULT (nautilus_directory_get_integer_metadata (directory, "NONEXISTENT_KEY", 42), 42);

1905 1906
	gtk_object_unref (GTK_OBJECT (directory));

1907
	NAUTILUS_CHECK_INTEGER_RESULT (g_hash_table_size (directory_objects), 0);
1908

1909 1910
	directory = nautilus_directory_get ("file:///etc");

1911
	NAUTILUS_CHECK_INTEGER_RESULT (g_hash_table_size (directory_objects), 1);
1912 1913 1914

	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_get_metadata (directory, "TEST", "default"), "value");

1915 1916 1917 1918 1919 1920 1921 1922 1923
	/* nautilus_directory_escape_slashes */
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_escape_slashes (""), "");
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_escape_slashes ("a"), "a");
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_escape_slashes ("/"), "%2F");
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_escape_slashes ("%"), "%25");
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_escape_slashes ("a/a"), "a%2Fa");
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_escape_slashes ("a%a"), "a%25a");
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_escape_slashes ("%25"), "%2525");
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_escape_slashes ("%2F"), "%252F");
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934

	/* sorting */
	file_1 = nautilus_file_get ("file:///etc");
	file_2 = nautilus_file_get ("file:///usr");

	NAUTILUS_CHECK_INTEGER_RESULT (file_1->ref_count, 1);
	NAUTILUS_CHECK_INTEGER_RESULT (file_2->ref_count, 1);

	NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_file_compare_for_sort (file_1, file_2, NAUTILUS_FILE_SORT_BY_NAME) < 0, TRUE);
	NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_file_compare_for_sort_reversed (file_1, file_2, NAUTILUS_FILE_SORT_BY_NAME) > 0, TRUE);
	NAUTILUS_CHECK_BOOLEAN_RESULT (nautilus_file_compare_for_sort (file_1, file_1, NAUTILUS_FILE_SORT_BY_NAME) == 0, TRUE);
1935 1936 1937
}

#endif /* !NAUTILUS_OMIT_SELF_CHECK */