summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-trash-directory.c
blob: 3361f4f35dfe846d30a6679e29267cde0acb3a43 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
316
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-trash-directory.c: Subclass of NautilusDirectory to 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@bentspoon.com>
*/

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

#include "nautilus-directory-private.h"
#include "nautilus-trash-monitor.h"
#include <eel/eel-glib-extensions.h>
#include <eel/eel-stock-dialogs.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-macros.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomevfs/gnome-vfs-volume-monitor.h>

struct NautilusTrashDirectoryDetails {
	GHashTable *volumes;
};

typedef struct {
	NautilusTrashDirectory *trash;
	GnomeVFSVolume *volume;

	GnomeVFSAsyncHandle *handle;
	NautilusDirectory *real_directory;
} TrashVolume;

static void add_volume (NautilusTrashDirectory *trash,
			GnomeVFSVolume         *volume);

GNOME_CLASS_BOILERPLATE (NautilusTrashDirectory, nautilus_trash_directory,
			 NautilusMergedDirectory, NAUTILUS_TYPE_MERGED_DIRECTORY)

#define TRASH_SEARCH_TIMED_WAIT_DELAY 20000

static int pending_find_directory_count = 0;

static void
find_directory_start (void)
{
	if (pending_find_directory_count == 0) {
		eel_timed_wait_start_with_duration (TRASH_SEARCH_TIMED_WAIT_DELAY,
						    NULL,
						    add_volume,
						    _("Searching Disks"),
						    _("Nautilus is searching your disks for trash folders."),
						    NULL);
	}

	++pending_find_directory_count;
}

static void
find_directory_end (void)
{
	--pending_find_directory_count;

	if (pending_find_directory_count == 0) {
		eel_timed_wait_stop (NULL, add_volume);
	}
}

static void
find_directory_callback (GnomeVFSAsyncHandle *handle,
			 GList *results,
			 gpointer callback_data)
{
	TrashVolume *trash_volume;
	GnomeVFSFindDirectoryResult *result;
	char *uri;
	NautilusDirectory *directory;

	trash_volume = callback_data;

	g_assert (eel_g_list_exactly_one_item (results));
	g_assert (trash_volume != NULL);
	g_assert (NAUTILUS_IS_TRASH_DIRECTORY (trash_volume->trash));
	g_assert (trash_volume->real_directory == NULL);
	g_assert (trash_volume->handle == handle);

	find_directory_end ();
	
	/* We are done with the async. I/O. */
	trash_volume->handle = NULL;

	/* If we can't find the trash, ignore it silently. */
	result = results->data;
	if (result->result != GNOME_VFS_OK) {
		return;
	}

	/* If we can't make a directory object, ignore it silently. */
	uri = gnome_vfs_uri_to_string (result->uri, 
				       GNOME_VFS_URI_HIDE_NONE);
	directory = nautilus_directory_get (uri);
	g_free (uri);
	if (directory == NULL) {
		return;
	}

	/* Add the directory object. */
	trash_volume->real_directory = directory;
	nautilus_merged_directory_add_real_directory
		(NAUTILUS_MERGED_DIRECTORY (trash_volume->trash),
		 directory);
}

static gboolean
get_trash_volume (NautilusTrashDirectory *trash,
		  GnomeVFSVolume *volume,
		  TrashVolume **trash_volume,
		  GnomeVFSURI **volume_mount_uri)
{
	char *uri_str;

	/* Quick out if we already know about this volume. */
	*trash_volume = g_hash_table_lookup (trash->details->volumes,
					     volume);
					    
	if (*trash_volume != NULL && (*trash_volume)->real_directory != NULL) {
		return FALSE;
	}

	if (!gnome_vfs_volume_handles_trash (volume)) {
		return FALSE;
	}
	
	uri_str = gnome_vfs_volume_get_activation_uri (volume);
	*volume_mount_uri = gnome_vfs_uri_new (uri_str);
	g_free (uri_str);

	if (*trash_volume == NULL) {
		/* Make the structure used to track the trash for this volume. */
		*trash_volume = g_new0 (TrashVolume, 1);
		(*trash_volume)->trash = trash;
		(*trash_volume)->volume = gnome_vfs_volume_ref (volume);
		g_hash_table_insert (trash->details->volumes, volume, *trash_volume);
	}
	
	return TRUE;
}

static void
add_volume (NautilusTrashDirectory *trash,
	    GnomeVFSVolume *volume)
{
	TrashVolume *trash_volume;
	GnomeVFSURI *volume_mount_uri;
	GList vfs_uri_as_list;

	if (!get_trash_volume (trash, volume, &trash_volume, &volume_mount_uri)) {
		return;
	}

	if (trash_volume->handle) {
		/* Already searching for trash */
		gnome_vfs_uri_unref (volume_mount_uri);
		return;
	}

	/* Find the real trash directory for this one. */
	vfs_uri_as_list.data = volume_mount_uri;
	vfs_uri_as_list.next = NULL;
	vfs_uri_as_list.prev = NULL;

	/* Search for Trash directories but don't create new ones. */
	find_directory_start ();
	gnome_vfs_async_find_directory
		(&trash_volume->handle, &vfs_uri_as_list, 
		 GNOME_VFS_DIRECTORY_KIND_TRASH, FALSE, TRUE, 0777,
		 GNOME_VFS_PRIORITY_DEFAULT,
		 find_directory_callback, trash_volume);

	gnome_vfs_uri_unref (volume_mount_uri);
}

static void
check_trash_created (NautilusTrashDirectory *trash,
		     GnomeVFSVolume *volume)
{
	GnomeVFSResult result;
	TrashVolume *trash_volume;
	GnomeVFSURI *volume_mount_uri;
	GnomeVFSURI *trash_uri;
	char *uri;

	if (!get_trash_volume (trash, volume, &trash_volume, &volume_mount_uri)) {
		return;
	}

	/* Do a synch trash lookup -- if the trash directory was just created, it's location will
	 * be known and returned immediately without any blocking.
	 */
	result = gnome_vfs_find_directory (volume_mount_uri, GNOME_VFS_DIRECTORY_KIND_TRASH,
		&trash_uri, FALSE, FALSE, 077);
	
	gnome_vfs_uri_unref (volume_mount_uri);
	if (result != GNOME_VFS_OK) {
		return;
	}

	uri = gnome_vfs_uri_to_string (trash_uri, 
				       GNOME_VFS_URI_HIDE_NONE);
	trash_volume->real_directory = nautilus_directory_get (uri);
	g_free (uri);
	gnome_vfs_uri_unref (trash_uri);
	if (trash_volume->real_directory == NULL) {
		return;
	}
	
	/* Add the directory object. */
	nautilus_merged_directory_add_real_directory
		(NAUTILUS_MERGED_DIRECTORY (trash_volume->trash),
		 trash_volume->real_directory);
}

static void
remove_trash_volume (TrashVolume *trash_volume, gboolean finalizing)
{
	g_hash_table_remove (trash_volume->trash->details->volumes,
			     trash_volume->volume);
	
	if (trash_volume->handle != NULL) {
		gnome_vfs_async_cancel (trash_volume->handle);
		find_directory_end ();
	}
	if (trash_volume->real_directory != NULL) {
		if (! finalizing) {
			nautilus_merged_directory_remove_real_directory
				(NAUTILUS_MERGED_DIRECTORY (trash_volume->trash),
				 trash_volume->real_directory);
		}
		nautilus_directory_unref (trash_volume->real_directory);
	}
	gnome_vfs_volume_unref (trash_volume->volume);
	g_free (trash_volume);
}

static void
remove_volume (NautilusTrashDirectory *trash,
	       GnomeVFSVolume *volume)
{
	TrashVolume *trash_volume;

	/* Quick out if don't already know about this volume. */
	trash_volume = g_hash_table_lookup (trash->details->volumes, volume);
	if (trash_volume != NULL) {
		remove_trash_volume (trash_volume, FALSE);
	}
}

static void
check_trash_directory_added_callback (GnomeVFSVolumeMonitor *monitor,
				      GnomeVFSVolume *volume,
				      NautilusTrashDirectory *trash)
{
	check_trash_created (trash, volume);
}

static void
volume_unmount_started_callback (GnomeVFSVolumeMonitor *monitor,
				 GnomeVFSVolume *volume,
				 NautilusTrashDirectory *trash)
{
	remove_volume (trash, volume);
}

static void
volume_mounted_callback (GnomeVFSVolumeMonitor *monitor,
			 GnomeVFSVolume *volume,
			 NautilusTrashDirectory *trash)
{
	add_volume (trash, volume);
}

static void
nautilus_trash_directory_instance_init (NautilusTrashDirectory *trash)
{
	GnomeVFSVolumeMonitor *volume_monitor;

	trash->details = g_new0 (NautilusTrashDirectoryDetails, 1);
	trash->details->volumes = g_hash_table_new (NULL, NULL);

	volume_monitor = gnome_vfs_get_volume_monitor ();

	g_signal_connect_object (volume_monitor, "volume_mounted",
				 G_CALLBACK (volume_mounted_callback), trash, 0);
	g_signal_connect_object (volume_monitor, "volume_pre_unmount",
				 G_CALLBACK (volume_unmount_started_callback), trash, 0);
}

/* Finish initializing a new NautilusTrashDirectory. We have to do the
 * remaining initialization here rather than in nautilus_trash_directory_init
 * because of a cyclic dependency between the NautilusTrashDirectory and
 * NautilusTrashMonitor instances.
 */
void
nautilus_trash_directory_finish_initializing (NautilusTrashDirectory *trash)
{
	GnomeVFSVolumeMonitor *volume_monitor;
	GList *volumes, *l;
	
	volume_monitor = gnome_vfs_get_volume_monitor ();

	g_signal_connect_object (nautilus_trash_monitor_get (), "check_trash_directory_added",
				 G_CALLBACK (check_trash_directory_added_callback), trash, 0);

	volumes = gnome_vfs_volume_monitor_get_mounted_volumes (volume_monitor);
	for (l = volumes; l != NULL; l = l->next) {
		add_volume (trash, l->data);
		gnome_vfs_volume_unref (l->data);
	}
	g_list_free (volumes);
}

static void
remove_trash_volume_finalizing_cover (gpointer key, gpointer value, gpointer callback_data)
{
	TrashVolume *trash_volume;

	g_assert (key != NULL);
	g_assert (value != NULL);
	g_assert (callback_data == NULL);

	trash_volume = value;

	g_assert (NAUTILUS_IS_TRASH_DIRECTORY (trash_volume->trash));
	g_assert (trash_volume->volume == key);

	remove_trash_volume (trash_volume, TRUE);
}

static void
trash_finalize (GObject *object)
{
	NautilusTrashDirectory *trash;

	trash = NAUTILUS_TRASH_DIRECTORY (object);

	eel_g_hash_table_safe_for_each (trash->details->volumes,
					remove_trash_volume_finalizing_cover, NULL);
	g_hash_table_destroy (trash->details->volumes);
	g_free (trash->details);

	G_OBJECT_CLASS (parent_class)->finalize (object);
}

static char *
trash_get_name_for_self_as_new_file (NautilusDirectory *directory)
{
	g_assert (NAUTILUS_IS_TRASH_DIRECTORY (directory));
	return g_strdup (_("Trash"));
}

static void
nautilus_trash_directory_class_init (NautilusTrashDirectoryClass *class)
{
	G_OBJECT_CLASS (class)->finalize = trash_finalize;
	NAUTILUS_DIRECTORY_CLASS (class)->get_name_for_self_as_new_file = trash_get_name_for_self_as_new_file;
}