nautilus-trash-file.c 18.5 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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-trash-file.c: Subclass of NautilusFile to help implement the
   virtual trash directory.
 
   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-trash-file.h"

29 30
#include "nautilus-directory-notify.h"
#include "nautilus-file-private.h"
31
#include "nautilus-file-utilities.h"
32
#include "nautilus-glib-extensions.h"
33
#include "nautilus-gtk-macros.h"
34 35
#include "nautilus-trash-directory.h"
#include <gtk/gtksignal.h>
36 37
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
38 39

struct NautilusTrashFileDetails {
40 41
	NautilusTrashDirectory *trash_directory;

42 43
	guint add_directory_connection_id;
	guint remove_directory_connection_id;
44 45 46 47 48

	GList *files;

	GHashTable *callbacks;
	GHashTable *monitors;
49 50
};

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
typedef struct {
	NautilusTrashFile *trash;
	NautilusFileCallback callback;
	gpointer callback_data;

	GList *attributes;

	GList *non_ready_files;
} TrashCallback;

typedef struct {
	NautilusTrashFile *trash;

	GList *attributes;
} TrashMonitor;

67 68 69 70 71 72 73 74
static void nautilus_trash_file_initialize       (gpointer   object,
						  gpointer   klass);
static void nautilus_trash_file_initialize_class (gpointer   klass);

NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusTrashFile,
				   nautilus_trash_file,
				   NAUTILUS_TYPE_FILE)

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static guint
trash_callback_hash (gconstpointer trash_callback_as_pointer)
{
	const TrashCallback *trash_callback;

	trash_callback = trash_callback_as_pointer;
	return GPOINTER_TO_UINT (trash_callback->callback)
		^ GPOINTER_TO_UINT (trash_callback->callback_data);
}

static gboolean
trash_callback_equal (gconstpointer trash_callback_as_pointer,
		      gconstpointer trash_callback_as_pointer_2)
{
	const TrashCallback *trash_callback, *trash_callback_2;

	trash_callback = trash_callback_as_pointer;
	trash_callback_2 = trash_callback_as_pointer_2;

	return trash_callback->callback == trash_callback_2->callback
		&& trash_callback->callback_data == trash_callback_2->callback_data;
}

static void
trash_callback_destroy (TrashCallback *trash_callback)
{
	g_assert (trash_callback != NULL);
	g_assert (NAUTILUS_IS_TRASH_FILE (trash_callback->trash));

104
	nautilus_file_unref (NAUTILUS_FILE (trash_callback->trash));
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 207 208 209 210 211 212 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	nautilus_g_list_free_deep (trash_callback->attributes);
	g_list_free (trash_callback->non_ready_files);
	g_free (trash_callback);
}

static void
trash_callback_check_done (TrashCallback *trash_callback)
{
	/* Check if we are ready. */
	if (trash_callback->non_ready_files != NULL) {
		return;
	}

	/* Remove from the hash table before sending it. */
	g_hash_table_remove (trash_callback->trash->details->callbacks,
			     trash_callback);

	/* We are ready, so do the real callback. */
	(* trash_callback->callback) (NAUTILUS_FILE (trash_callback->trash),
				      trash_callback->callback_data);

	/* And we are done. */
	trash_callback_destroy (trash_callback);
}

static void
trash_callback_remove_file (TrashCallback *trash_callback,
			    NautilusFile *file)
{
	trash_callback->non_ready_files = g_list_remove
		(trash_callback->non_ready_files, file);
	trash_callback_check_done (trash_callback);
}

static void
ready_callback (NautilusFile *file,
		gpointer callback_data)
{
	TrashCallback *trash_callback;

	g_assert (NAUTILUS_IS_FILE (file));
	g_assert (callback_data != NULL);

	trash_callback = callback_data;
	g_assert (g_list_find (trash_callback->non_ready_files, file) != NULL);

	trash_callback_remove_file (trash_callback, file);
}

static void
real_file_changed_callback (NautilusFile *real_file,
			    gpointer callback_data)
{
	NautilusTrashFile *trash_file;
	
	trash_file = NAUTILUS_TRASH_FILE (callback_data);
	nautilus_file_changed (NAUTILUS_FILE (trash_file));
}

static void
monitor_add_file (gpointer key,
		  gpointer value,
		  gpointer callback_data)
{
	TrashMonitor *monitor;
	
	monitor = value;
	nautilus_file_monitor_add
		(NAUTILUS_FILE (callback_data),
		 monitor,
		 monitor->attributes);
}

static void
add_real_file (NautilusTrashFile *trash,
	       NautilusFile *real_file)
{
	g_return_if_fail (NAUTILUS_IS_TRASH_FILE (trash));
	g_return_if_fail (NAUTILUS_IS_FILE (real_file));
	g_return_if_fail (!NAUTILUS_IS_TRASH_FILE (real_file));
	g_return_if_fail (g_list_find (trash->details->files, real_file) == NULL);

	nautilus_file_ref (real_file);
	trash->details->files = g_list_prepend
		(trash->details->files, real_file);
	
	gtk_signal_connect (GTK_OBJECT (real_file),
			    "changed",
			    real_file_changed_callback,
			    trash);

	/* Add the file to any extant monitors. */
	g_hash_table_foreach (trash->details->monitors,
			      monitor_add_file,
			      real_file);
}

static void
trash_callback_remove_file_cover (gpointer key,
				       gpointer value,
				       gpointer callback_data)
{
	trash_callback_remove_file
		(value, NAUTILUS_FILE (callback_data));
}

static void
monitor_remove_file (gpointer key,
		     gpointer value,
		     gpointer callback_data)
{
	nautilus_file_monitor_remove
		(NAUTILUS_FILE (callback_data), value);
}

static void
remove_real_file (NautilusTrashFile *trash,
		  NautilusFile *real_file)
{
	g_return_if_fail (NAUTILUS_IS_TRASH_FILE (trash));
	g_return_if_fail (NAUTILUS_IS_FILE (real_file));
	g_return_if_fail (g_list_find (trash->details->files, real_file) != NULL);

	nautilus_g_hash_table_safe_for_each
		(trash->details->callbacks,
		 trash_callback_remove_file_cover,
		 real_file);
	g_hash_table_foreach
		(trash->details->monitors,
		 monitor_remove_file,
		 real_file);

	gtk_signal_disconnect_by_func (GTK_OBJECT (real_file),
				       real_file_changed_callback,
				       trash);

	trash->details->files = g_list_remove
		(trash->details->files, real_file);
	nautilus_file_unref (real_file);
}

static void
add_real_file_given_directory (NautilusTrashFile *trash_file,
			       NautilusDirectory *real_directory)
{
	NautilusFile *real_file;

	real_file = nautilus_directory_get_corresponding_file (real_directory);
	add_real_file (trash_file, real_file);
	nautilus_file_unref (real_file);
}

257 258 259 260 261 262 263 264 265
static void
add_directory_callback (NautilusTrashDirectory *trash_directory,
			NautilusDirectory *real_directory,
			NautilusTrashFile *trash_file)
{
	g_assert (NAUTILUS_IS_TRASH_DIRECTORY (trash_directory));
	g_assert (NAUTILUS_IS_DIRECTORY (real_directory));
	g_assert (!NAUTILUS_IS_MERGED_DIRECTORY (real_directory));
	g_assert (NAUTILUS_IS_TRASH_FILE (trash_file));
266 267 268 269 270
	g_assert (trash_file->details->trash_directory == trash_directory);

	add_real_file_given_directory (trash_file, real_directory);

	nautilus_file_changed (NAUTILUS_FILE (trash_file));
271 272 273 274 275 276 277
}

static void
remove_directory_callback (NautilusTrashDirectory *trash_directory,
			   NautilusDirectory *real_directory,
			   NautilusTrashFile *trash_file)
{
278 279
	NautilusFile *real_file;

280 281 282 283
	g_assert (NAUTILUS_IS_TRASH_DIRECTORY (trash_directory));
	g_assert (NAUTILUS_IS_DIRECTORY (real_directory));
	g_assert (!NAUTILUS_IS_MERGED_DIRECTORY (real_directory));
	g_assert (NAUTILUS_IS_TRASH_FILE (trash_file));
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	g_assert (trash_file->details->trash_directory == trash_directory);

	real_file = nautilus_directory_get_corresponding_file (real_directory);
	remove_real_file (trash_file, real_file);
	nautilus_file_unref (real_file);

	nautilus_file_changed (NAUTILUS_FILE (trash_file));
}

static void
trash_file_call_when_ready (NautilusFile *file,
			    GList *file_attributes,
			    NautilusFileCallback callback,
			    gpointer callback_data)

{
	NautilusTrashFile *trash;
	TrashCallback search_key, *trash_callback;
	GList *node;

	trash = NAUTILUS_TRASH_FILE (file);

	/* Check to be sure we aren't overwriting. */
	search_key.callback = callback;
	search_key.callback_data = callback_data;
	if (g_hash_table_lookup (trash->details->callbacks, &search_key) != NULL) {
		g_warning ("tried to add a new callback while an old one was pending");
		return;
	}

	/* Create a trash_callback record. */
	trash_callback = g_new0 (TrashCallback, 1);
316
	nautilus_file_ref (file);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 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 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 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	trash_callback->trash = trash;
	trash_callback->callback = callback;
	trash_callback->callback_data = callback_data;
	trash_callback->attributes = nautilus_g_str_list_copy (file_attributes);
	for (node = trash->details->files; node != NULL; node = node->next) {
		trash_callback->non_ready_files = g_list_prepend
			(trash_callback->non_ready_files, node->data);
	}

	/* Put it in the hash table. */
	g_hash_table_insert (trash->details->callbacks,
			     trash_callback, trash_callback);

	/* Handle the pathological case where there are no files. */
	if (trash->details->files == NULL) {
		trash_callback_check_done (trash_callback);
	}

	/* Now connect to each file's call_when_ready. */
	for (node = trash->details->files; node != NULL; node = node->next) {
		nautilus_file_call_when_ready
			(node->data,
			 trash_callback->attributes,
			 ready_callback, trash_callback);
	}
}

static void
trash_file_cancel_call_when_ready (NautilusFile *file,
				   NautilusFileCallback callback,
				   gpointer callback_data)
{
	NautilusTrashFile *trash;
	TrashCallback search_key, *trash_callback;
	GList *node;

	trash = NAUTILUS_TRASH_FILE (file);

	/* Find the entry in the table. */
	search_key.callback = callback;
	search_key.callback_data = callback_data;
	trash_callback = g_hash_table_lookup (trash->details->callbacks, &search_key);
	if (trash_callback == NULL) {
		return;
	}

	/* Remove from the hash table before working with it. */
	g_hash_table_remove (trash_callback->trash->details->callbacks, trash_callback);

	/* Tell all the directories to cancel the call. */
	for (node = trash_callback->non_ready_files; node != NULL; node = node->next) {
		nautilus_file_cancel_call_when_ready
			(node->data,
			 ready_callback, trash_callback);
	}
	trash_callback_destroy (trash_callback);
}

static gboolean
trash_file_check_if_ready (NautilusFile *file,
			   GList *file_attributes)
{
	NautilusTrashFile *trash;
	GList *node;

	trash = NAUTILUS_TRASH_FILE (file);

	for (node = trash->details->files; node != NULL; node = node->next) {
		if (!nautilus_file_check_if_ready (node->data,
						   file_attributes)) {
			return FALSE;
		}
	}

	return TRUE;
}

static void
trash_file_monitor_add (NautilusFile *file,
			gconstpointer client,
			GList *attributes)
{
	NautilusTrashFile *trash;
	TrashMonitor *monitor;
	GList *node;

	trash = NAUTILUS_TRASH_FILE (file);

	/* Map the client to a unique value so this doesn't interfere
	 * with direct monitoring of the file by the same client.
	 */
	monitor = g_hash_table_lookup (trash->details->monitors, client);
	if (monitor != NULL) {
		g_assert (monitor->trash == trash);
		nautilus_g_list_free_deep (monitor->attributes);
	} else {
		monitor = g_new0 (TrashMonitor, 1);
		monitor->trash = trash;
		g_hash_table_insert (trash->details->monitors,
				     (gpointer) client, monitor);
	}
	monitor->attributes = nautilus_g_str_list_copy (attributes);

	for (node = trash->details->files; node != NULL; node = node->next) {
		nautilus_file_monitor_add (node->data, monitor, attributes);
	}
}   
			   
static void
trash_file_monitor_remove (NautilusFile *file,
			   gconstpointer client)
{
	NautilusTrashFile *trash;
	TrashMonitor *monitor;
	GList *node;
	
	trash = NAUTILUS_TRASH_FILE (file);
	
	/* Map the client to the value used by the earlier add call. */
        monitor = g_hash_table_lookup (trash->details->monitors, client);
	if (monitor == NULL) {
		return;
	}
	g_hash_table_remove (trash->details->monitors, client);

	/* Call through to the real file remove calls. */
	for (node = trash->details->files; node != NULL; node = node->next) {
		nautilus_file_monitor_remove (node->data, monitor);
	}

	nautilus_g_list_free_deep (monitor->attributes);
	g_free (monitor);
}

static GnomeVFSFileType
trash_file_get_file_type (NautilusFile *file)
{
	return GNOME_VFS_FILE_TYPE_DIRECTORY;
}			      

static gboolean
trash_file_get_item_count (NautilusFile *file, 
			   guint *count,
			   gboolean *count_unreadable)
{
	NautilusTrashFile *trash;
	GList *node;
	guint one_count, one_unreadable;
	gboolean got_count;
	
	trash = NAUTILUS_TRASH_FILE (file);
	
	got_count = TRUE;
	if (count != NULL) {
		*count = 0;
	}
	if (count_unreadable != NULL) {
		*count_unreadable = FALSE;
	}

	for (node = trash->details->files; node != NULL; node = node->next) {
		if (!nautilus_file_get_directory_item_count (node->data,
							     &one_count,
							     &one_unreadable)) {
			got_count = FALSE;
		}

		if (count != NULL) {
			*count += one_count;
		}
		if (count_unreadable != NULL && one_unreadable) {
			*count_unreadable = TRUE;
		}
	}

	return got_count;
}

static NautilusRequestStatus
trash_file_get_deep_counts (NautilusFile *file,
			    guint *directory_count,
			    guint *file_count,
			    guint *unreadable_directory_count,
			    GnomeVFSFileSize *total_size)
{
	NautilusTrashFile *trash;
	GList *node;
	NautilusRequestStatus status, one_status;
	guint one_directory_count, one_file_count, one_unreadable_directory_count;
	GnomeVFSFileSize one_total_size;

	trash = NAUTILUS_TRASH_FILE (file);
	
	status = NAUTILUS_REQUEST_DONE;
	if (directory_count != NULL) {
		*directory_count = 0;
	}
	if (file_count != NULL) {
		*file_count = 0;
	}
	if (unreadable_directory_count != NULL) {
		*unreadable_directory_count = 0;
	}
	if (total_size != NULL) {
		*total_size = 0;
	}

	for (node = trash->details->files; node != NULL; node = node->next) {
		one_status = nautilus_file_get_deep_counts
			(node->data,
			 &one_directory_count,
			 &one_file_count,
			 &one_unreadable_directory_count,
			 &one_total_size);
		
		if (one_status < status) {
			status = one_status;
		}
		if (directory_count != NULL) {
			*directory_count += one_directory_count;
		}
		if (file_count != NULL) {
			*file_count += one_file_count;
		}
		if (unreadable_directory_count != NULL) {
			*unreadable_directory_count += one_unreadable_directory_count;
		}
		if (total_size != NULL) {
			*total_size += one_total_size;
		}
	}

	return status;
}

static gboolean
trash_file_get_date (NautilusFile *file,
		     NautilusDateType date_type,
		     time_t *date)
{
	NautilusTrashFile *trash;
	GList *node;
	gboolean got_at_least_one;
	gboolean got_all;
	time_t one_date;

	trash = NAUTILUS_TRASH_FILE (file);

	got_at_least_one = FALSE;
	got_all = TRUE;
	
	for (node = trash->details->files; node != NULL; node = node->next) {
		if (nautilus_file_get_date (node->data,
					    date_type,
					    &one_date)) {
			if (!got_at_least_one) {
				got_at_least_one = TRUE;
				if (date != NULL) {
					*date = one_date;
				}
			} else {
				if (date != NULL && one_date > *date) {
					*date = one_date;
				}
			}
		} else {
			got_all = FALSE;
		}
	}

	return got_at_least_one && got_all;
}

590 591 592 593 594 595
static char *
trash_file_get_where_string (NautilusFile *file)
{
	return g_strdup (_("on the desktop"));
}

596 597 598 599 600 601 602

static void
remove_all_real_files (NautilusTrashFile *trash)
{
	while (trash->details->files != NULL) {
		remove_real_file (trash, trash->details->files->data);
	}
603 604
}

605 606 607
static void
nautilus_trash_file_initialize (gpointer object, gpointer klass)
{
608 609
	NautilusTrashFile *trash_file;
	NautilusTrashDirectory *trash_directory;
610
	GList *real_directories, *node;
611 612 613 614

	trash_file = NAUTILUS_TRASH_FILE (object);

	trash_directory = NAUTILUS_TRASH_DIRECTORY (nautilus_directory_get (NAUTILUS_TRASH_URI));
615

616
	trash_file->details = g_new0 (NautilusTrashFileDetails, 1);
617 618 619 620 621
	trash_file->details->trash_directory = trash_directory;

	trash_file->details->callbacks = g_hash_table_new
		(trash_callback_hash, trash_callback_equal);
	trash_file->details->monitors = g_hash_table_new (NULL, NULL);
622

623 624 625 626 627 628 629 630 631 632
	trash_file->details->add_directory_connection_id = gtk_signal_connect
		(GTK_OBJECT (trash_directory),
		 "add_real_directory",
		 add_directory_callback,
		 trash_file);
	trash_file->details->remove_directory_connection_id = gtk_signal_connect
		(GTK_OBJECT (trash_directory),
		 "remove_real_directory",
		 remove_directory_callback,
		 trash_file);
633 634 635 636 637 638 639

	real_directories = nautilus_merged_directory_get_real_directories
		(NAUTILUS_MERGED_DIRECTORY (trash_directory));
	for (node = real_directories; node != NULL; node = node->next) {
		add_real_file_given_directory (trash_file, node->data);
	}
	g_list_free (real_directories);
640 641 642 643 644
}

static void
trash_destroy (GtkObject *object)
{
645 646 647 648
	NautilusTrashFile *trash_file;
	NautilusTrashDirectory *trash_directory;

	trash_file = NAUTILUS_TRASH_FILE (object);
649 650 651 652 653 654 655 656 657 658
	trash_directory = trash_file->details->trash_directory;

	remove_all_real_files (trash_file);

	if (g_hash_table_size (trash_file->details->callbacks) != 0) {
		g_warning ("call_when_ready still pending when trash virtual file is destroyed");
	}
	if (g_hash_table_size (trash_file->details->monitors) != 0) {
		g_warning ("file monitor still active when trash virtual file is destroyed");
	}
659 660 661 662 663

	gtk_signal_disconnect (GTK_OBJECT (trash_directory),
			       trash_file->details->add_directory_connection_id);
	gtk_signal_disconnect (GTK_OBJECT (trash_directory),
			       trash_file->details->remove_directory_connection_id);
664 665 666 667

	g_hash_table_destroy (trash_file->details->callbacks);
	g_hash_table_destroy (trash_file->details->monitors);

668 669
	g_free (trash_file->details);

670 671
	nautilus_directory_unref (NAUTILUS_DIRECTORY (trash_directory));

672
	NAUTILUS_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
673 674 675 676 677 678 679 680 681 682 683 684 685
}

static void
nautilus_trash_file_initialize_class (gpointer klass)
{
	GtkObjectClass *object_class;
	NautilusFileClass *file_class;

	object_class = GTK_OBJECT_CLASS (klass);
	file_class = NAUTILUS_FILE_CLASS (klass);
	
	object_class->destroy = trash_destroy;

686 687 688 689 690 691 692 693 694
	file_class->monitor_add = trash_file_monitor_add;
	file_class->monitor_remove = trash_file_monitor_remove;
	file_class->call_when_ready = trash_file_call_when_ready;
	file_class->cancel_call_when_ready = trash_file_cancel_call_when_ready;
	file_class->check_if_ready = trash_file_check_if_ready;
	file_class->get_file_type = trash_file_get_file_type;
	file_class->get_item_count = trash_file_get_item_count;
	file_class->get_deep_counts = trash_file_get_deep_counts;
	file_class->get_date = trash_file_get_date;
695
	file_class->get_where_string = trash_file_get_where_string;
696
}