nautilus-directory.c 27.8 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 28 29
/* -*- 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>
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "nautilus-directory.h"
30
#include "libnautilus.h"
31 32 33 34 35 36 37

#include <stdlib.h>

#include <gtk/gtkmain.h>

#include <libgnomevfs/gnome-vfs-types.h>
#include <libgnomevfs/gnome-vfs-uri.h>
38
#include <libgnomevfs/gnome-vfs-file-info.h>
39 40 41 42 43 44 45 46 47 48 49
#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>

#include "nautilus-gtk-macros.h"
#include "nautilus-lib-self-check-functions.h"
#include "nautilus-string.h"

50 51 52 53 54 55 56 57 58 59 60 61
#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"

62 63 64 65 66 67 68 69 70 71 72 73 74
#define METAFILE_XML_VERSION "1.0"

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);

75
NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusDirectory, nautilus_directory, GTK_TYPE_OBJECT)
76 77 78 79 80 81 82

struct _NautilusDirectoryDetails
{
	char *uri_text;
	GnomeVFSURI *uri;

	GnomeVFSURI *metafile_uri;
83 84 85
	GnomeVFSURI *alternate_metafile_uri;
	gboolean use_alternate_metafile;

86 87 88 89 90 91 92 93
	xmlDoc *metafile_tree;
	int write_metafile_idle_id;

	NautilusFileList *files;
};

struct _NautilusFile
{
94 95 96 97
	guint ref_count;

	NautilusDirectory *directory;
	GnomeVFSFileInfo *info;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
};

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;
}

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);

129 130 131
	if (directory->details->write_metafile_idle_id != 0)
		nautilus_directory_write_metafile (directory);

132 133
	g_hash_table_remove (directory_objects, directory->details->uri_text);

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	/* Unref all the files. */
	while (directory->details->files != NULL) {
		NautilusFile *file;
		
		file = directory->details->files->data;

		/* Detach the file from this directory. */
		g_assert (file->directory == directory);
		file->directory = NULL;
		
		/* Let the reference go. */
		nautilus_file_unref (file);

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

150
	g_free (directory->details->uri_text);
151
	if (directory->details->uri != NULL)
152
		gnome_vfs_uri_unref (directory->details->uri);
153
	if (directory->details->metafile_uri != NULL)
154
		gnome_vfs_uri_unref (directory->details->metafile_uri);
155 156
	if (directory->details->alternate_metafile_uri != NULL)
		gnome_vfs_uri_unref (directory->details->alternate_metafile_uri);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	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. */
	if (!directory_objects)
		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. */
		directory = NAUTILUS_DIRECTORY (nautilus_directory_new (uri));
		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;
}

207 208 209 210 211 212 213 214
char *
nautilus_directory_get_uri (NautilusDirectory *directory)
{
	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), NULL);

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

215 216 217 218 219
/* 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.
*/
220
static GnomeVFSResult
221
nautilus_directory_try_to_read_metafile (NautilusDirectory *directory, GnomeVFSURI *metafile_uri)
222 223 224 225 226 227
{
	GnomeVFSResult result;
	GnomeVFSFileInfo metafile_info;
	GnomeVFSHandle *metafile_handle;
	GnomeVFSFileSize size, actual_size;

228 229
	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);
230

231
	result = gnome_vfs_get_file_info_uri (metafile_uri,
232 233 234 235 236
					      &metafile_info,
					      GNOME_VFS_FILE_INFO_DEFAULT,
					      NULL);

	metafile_handle = NULL;
237
	if (result == GNOME_VFS_OK)
238
		result = gnome_vfs_open_uri (&metafile_handle,
239
					     metafile_uri,
240 241 242 243
					     GNOME_VFS_OPEN_READ);

	if (result == GNOME_VFS_OK) {
		size = metafile_info.size;
244
		if (size != metafile_info.size)
245 246 247 248
			result = GNOME_VFS_ERROR_TOOBIG;
	}

	if (result == GNOME_VFS_OK) {
249
		char *buffer = g_alloca(size+1);
250

251
		result = gnome_vfs_read (metafile_handle, buffer, size, &actual_size);
252
		buffer[size] = '\0';
253
		directory->details->metafile_tree = xmlParseMemory (buffer, actual_size);
254 255 256 257 258
	}

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

259 260 261 262 263 264 265 266 267 268
	return result;
}

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

	g_return_if_fail (NAUTILUS_IS_DIRECTORY (directory));

269 270 271 272 273 274 275 276 277
	/* 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);
278

279
	/* Check for errors. Later this must be reported to the user, not spit out as a warning. */
280 281
	if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_NOTFOUND)
		g_warning ("nautilus_directory_read_metafile failed to read metafile - we should report this to the user");
282 283 284 285 286 287 288 289 290 291 292 293
}

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. */
294
static GnomeVFSResult
295
nautilus_directory_try_to_write_metafile (NautilusDirectory *directory, GnomeVFSURI *metafile_uri)
296 297 298 299 300 301 302
{
	xmlChar *buffer;
	int buffer_size;
	GnomeVFSResult result;
	GnomeVFSHandle *metafile_handle;
	GnomeVFSFileSize actual_size;

303 304 305
	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);
306 307 308

	metafile_handle = NULL;
	result = gnome_vfs_create_uri (&metafile_handle,
309
				       metafile_uri,
310 311
				       GNOME_VFS_OPEN_WRITE,
				       FALSE,
312
				       METAFILE_PERMISSIONS);
313

314
	buffer = NULL;
315
	if (result == GNOME_VFS_OK) {
316
		xmlDocDumpMemory (directory->details->metafile_tree, &buffer, &buffer_size);
317
		result = gnome_vfs_write (metafile_handle, buffer, buffer_size, &actual_size);
318 319
		if (buffer_size != actual_size)
			result = GNOME_VFS_ERROR_GENERIC;
320 321 322 323 324 325
	}

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

	xmlFree (buffer);
326 327 328 329 330 331 332 333 334 335 336 337

	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
338 339
	   request to do so.
	*/
340 341 342 343 344 345 346 347 348
	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;

349 350 351 352 353
	/* 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);
354

355 356 357 358 359
	/* 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. */
360 361
	if (result != GNOME_VFS_OK)
		g_warning ("nautilus_directory_write_metafile failed to write metafile - we should report this to the user");
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
}

static gboolean
nautilus_directory_write_metafile_on_idle (gpointer data)
{
	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (data), FALSE);
	nautilus_directory_write_metafile (data);
	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 =
			gtk_idle_add (nautilus_directory_write_metafile_on_idle,
				      directory);
}
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

/* 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;
}

467 468
static GnomeVFSURI *
nautilus_directory_construct_alternate_metafile_uri (GnomeVFSURI *metafile_uri)
469 470 471 472 473 474 475 476 477 478 479 480 481 482
{
	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);
483
		return NULL;
484 485 486
	}

	/* Construct a file name from the URI. */
487
	uri_as_string = gnome_vfs_uri_to_string (metafile_uri,
488 489 490 491 492 493 494 495 496 497 498
						 GNOME_VFS_URI_HIDE_NONE);
	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);

499
	return alternate_uri;
500
}
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
      
#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;
521
	GnomeVFSURI *alternate_metafile_uri;
522 523 524 525 526 527

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

	metafile_uri = gnome_vfs_uri_append_path (vfs_uri, METAFILE_NAME);
528
	alternate_metafile_uri = nautilus_directory_construct_alternate_metafile_uri (metafile_uri);
529 530 531

	directory = gtk_type_new (NAUTILUS_TYPE_DIRECTORY);

532
	directory->details->uri_text = g_strdup(uri);
533 534
	directory->details->uri = vfs_uri;
	directory->details->metafile_uri = metafile_uri;
535
	directory->details->alternate_metafile_uri = alternate_metafile_uri;
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

	nautilus_directory_read_metafile (directory);

	return directory;
}

void
nautilus_directory_get_files (NautilusDirectory *directory,
			      NautilusFileListCallback callback,
			      gpointer callback_data)
{
	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);
}

556 557 558 559
static char *
nautilus_directory_get_metadata_from_node (xmlNode *node,
					   const char *tag,
					   const char *default_metadata)
560 561 562 563 564 565 566
{
	xmlChar *property;
	char *result;

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

567
	property = xmlGetProp (node, tag);
568 569 570 571
	if (property == NULL)
		result = g_strdup (default_metadata);
	else
		result = g_strdup (property);
572
	xmlFree (property);
573 574 575 576

	return result;
}

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
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)
		return NULL;
	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);
}

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
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. */
641
	root = nautilus_directory_create_metafile_tree_root (directory);
642 643 644 645 646 647 648 649 650 651

	/* 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);
}

652
static char *
653 654 655 656 657 658 659 660 661 662 663 664
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);
665 666
	if (root == NULL)
		return g_strdup (default_metadata);
667 668 669

	/* The children represent the files.
	   This linear search is temporary.
670
	   Eventually, we'll have a pointer from the NautilusFile right to
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
	   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);
}

686
static void
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
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.
725
	   Eventually, we'll have a pointer from the NautilusFile right to
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
	   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);
}

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 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
NautilusFile *
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);

	file = g_new (NautilusFile, 1);
	file->ref_count = 1;
	file->directory = directory;
	file->info = info;

	directory->details->files = g_list_prepend (directory->details->files, file);

	return file;
}

void
nautilus_file_unref (NautilusFile *file)
{
	g_return_if_fail (file != NULL);
	g_return_if_fail (file->ref_count != 0);

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

	/* Destroy the file object. */
	g_assert (file->directory == NULL);
	gnome_vfs_file_info_unref (file->info);
}

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);

811 812 813 814 815
	g_assert (file->ref_count != 0);
	g_assert (file->directory == NULL || NAUTILUS_IS_DIRECTORY (file->directory));
	g_assert (file->info->name != NULL);
	g_assert (file->info->name[0] != '\0');

816 817 818
	return g_strdup (file->info->name);
}

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
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;
}

834 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 867 868 869 870 871 872 873 874 875 876 877 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
GnomeVFSFileInfo *
nautilus_file_get_info (NautilusFile *file)
{
	g_return_val_if_fail (file != NULL, NULL);

	return file->info;
}

/**
 * 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.
 * 
 **/
gchar *
nautilus_file_get_date_as_string (NautilusFile *file)
{
	/* Note: There's also accessed time and changed time.
	 * Accessed time doesn't seem worth showing to the user.
	 * Changed time is only subtly different from modified time
	 * (changed time includes "metadata" changes like file permissions).
	 * We should not display both, but we might change our minds as to
	 * which one is better.
	 */

	g_return_val_if_fail (file != NULL, NULL);

	/* Note that ctime is a funky function that returns a
	 * string that you're not supposed to free.
	 */
	return g_strdup (ctime (&file->info->mtime));
}

/**
 * nautilus_file_get_size_as_string:
 * 
 * Get a user-displayable string representing a file size. 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.
 * 
 **/
gchar *
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)
		return g_strdup (_("--"));

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

/**
 * 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.
 * 
 **/
gchar *
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"));

	return g_strdup (file->info->mime_type);
}

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
#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;

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

	g_assert (g_hash_table_size (directory_objects) == 1);

	file_count = 0;
	nautilus_directory_get_files (directory, get_files_cb, &data_dummy);

943 944 945
	nautilus_directory_set_metadata (directory, "TEST", "default", "value");
	NAUTILUS_CHECK_STRING_RESULT (nautilus_directory_get_metadata (directory, "TEST", "default"), "value");

946 947 948
	gtk_object_unref (GTK_OBJECT (directory));

	g_assert (g_hash_table_size (directory_objects) == 0);
949

950 951 952 953 954 955
	directory = nautilus_directory_get ("file:///etc");

	g_assert (g_hash_table_size (directory_objects) == 1);

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

956 957 958 959 960 961 962 963 964
	/* 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");
965 966 967
}

#endif /* !NAUTILUS_OMIT_SELF_CHECK */