summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-trash-monitor.c
blob: 83ef5667d1dac68e83635daf633d61131b7ccbfc (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* 
   nautilus-trash-monitor.c: Nautilus trash state watcher.
 
   Copyright (C) 2000, 2001 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: Pavel Cisler <pavel@eazel.com>
*/

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

#include "nautilus-directory-notify.h"
#include "nautilus-directory.h"
#include "nautilus-file-attributes.h"
#include "nautilus-trash-directory.h"
#include <eel/eel-gtk-macros.h>
#include <eel/eel-vfs-extensions.h>
#include <gtk/gtksignal.h>
#include <libgnomevfs/gnome-vfs-find-directory.h>
#include <libgnomevfs/gnome-vfs-types.h>
#include <libgnomevfs/gnome-vfs-uri.h>

struct NautilusTrashMonitorDetails {
	NautilusDirectory *trash_directory;
	gboolean empty;
};

enum {
	TRASH_STATE_CHANGED,
	CHECK_TRASH_DIRECTORY_ADDED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];
static NautilusTrashMonitor *nautilus_trash_monitor;

static void nautilus_trash_monitor_initialize_class (NautilusTrashMonitorClass *klass);
static void nautilus_trash_monitor_initialize       (gpointer                   object,
						     gpointer                   klass);
static void destroy                                 (GtkObject                 *object);

EEL_DEFINE_CLASS_BOILERPLATE (NautilusTrashMonitor, nautilus_trash_monitor, GTK_TYPE_OBJECT)

static void
nautilus_trash_monitor_initialize_class (NautilusTrashMonitorClass *klass)
{
	GtkObjectClass *object_class;

	object_class = GTK_OBJECT_CLASS (klass);

	object_class->destroy = destroy;

	signals[TRASH_STATE_CHANGED] = gtk_signal_new
		("trash_state_changed",
		 GTK_RUN_LAST,
		 object_class->type,
		 GTK_SIGNAL_OFFSET (NautilusTrashMonitorClass, trash_state_changed),
		 gtk_marshal_NONE__BOOL,
		 GTK_TYPE_NONE, 1,
		 GTK_TYPE_BOOL);

	signals[CHECK_TRASH_DIRECTORY_ADDED] = gtk_signal_new
		("check_trash_directory_added",
		 GTK_RUN_LAST,
		 object_class->type,
		 GTK_SIGNAL_OFFSET (NautilusTrashMonitorClass, check_trash_directory_added),
		 gtk_marshal_NONE__POINTER,
		 GTK_TYPE_NONE, 1,
		 GTK_TYPE_POINTER);

	gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
}

static void
nautilus_trash_files_changed_callback (NautilusDirectory *directory, GList *files, 
				       gpointer callback_data)
{
	NautilusTrashMonitor *trash_monitor;
	gboolean old_empty_state;
	NautilusFile *file;
	
	trash_monitor = callback_data;
	g_assert (NAUTILUS_IS_TRASH_MONITOR (trash_monitor));
	g_assert (trash_monitor->details->trash_directory == directory);

	/* Something about the Trash NautilusDirectory changed, find out if 
	 * it affected the empty state.
	 */
	old_empty_state = trash_monitor->details->empty;
	trash_monitor->details->empty = !nautilus_directory_is_not_empty (directory);

	if (old_empty_state != trash_monitor->details->empty) {
		file = nautilus_file_get (EEL_TRASH_URI);
		nautilus_file_changed (file);
		nautilus_file_unref (file);

		/* trash got empty or full, notify everyone who cares */
		gtk_signal_emit (GTK_OBJECT (trash_monitor), 
				 signals[TRASH_STATE_CHANGED],
				 trash_monitor->details->empty);
	}
}

static void
nautilus_trash_monitor_initialize (gpointer object, gpointer klass)
{
	NautilusDirectory *trash_directory;
	NautilusTrashMonitor *trash_monitor;
	GList *attributes;

	trash_monitor = NAUTILUS_TRASH_MONITOR (object);

	/* set up a NautilusDirectory for the Trash directory to monitor */

	trash_directory = nautilus_directory_get (EEL_TRASH_URI);

	trash_monitor->details = g_new0 (NautilusTrashMonitorDetails, 1);
	trash_monitor->details->trash_directory = trash_directory;
	trash_monitor->details->empty = TRUE;

	attributes = g_list_prepend (NULL, NAUTILUS_FILE_ATTRIBUTE_METADATA);

	/* Make sure we get notified about changes */
	nautilus_directory_file_monitor_add
		(trash_directory, trash_monitor, TRUE, TRUE, attributes,
		 nautilus_trash_files_changed_callback, trash_monitor);

	g_list_free (attributes);

    	gtk_signal_connect_while_alive
		(GTK_OBJECT (trash_directory),
		 "files_added",
		 nautilus_trash_files_changed_callback,
		 trash_monitor,
		 GTK_OBJECT (trash_monitor));
    	gtk_signal_connect_while_alive
		(GTK_OBJECT (trash_directory),
		 "files_changed",
		 nautilus_trash_files_changed_callback,
		 trash_monitor,
		 GTK_OBJECT (trash_monitor));
}

static void
destroy (GtkObject *object)
{
	NautilusTrashMonitor *trash_monitor;

	trash_monitor = NAUTILUS_TRASH_MONITOR (object);

	nautilus_directory_file_monitor_remove
		(trash_monitor->details->trash_directory, 
		 trash_monitor);
	nautilus_directory_unref (trash_monitor->details->trash_directory);
	g_free (trash_monitor->details);
}

static void
unref_trash_monitor (void)
{
	gtk_object_unref (GTK_OBJECT (nautilus_trash_monitor));
}

NautilusTrashMonitor *
nautilus_trash_monitor_get (void)
{
	NautilusDirectory *trash_directory;

	if (nautilus_trash_monitor == NULL) {
		/* not running yet, start it up */

		/* the trash directory object will get created by this */
		trash_directory = nautilus_directory_get (EEL_TRASH_URI);
		
		nautilus_trash_monitor = NAUTILUS_TRASH_MONITOR
			(gtk_object_new (NAUTILUS_TYPE_TRASH_MONITOR, NULL));
		gtk_object_ref (GTK_OBJECT (nautilus_trash_monitor));
		gtk_object_sink (GTK_OBJECT (nautilus_trash_monitor));
		g_atexit (unref_trash_monitor);
		
		/* make sure we get signalled when trash directories get added */
		nautilus_trash_directory_finish_initializing
			(NAUTILUS_TRASH_DIRECTORY (trash_directory));

		nautilus_directory_unref (trash_directory);
	}

	return nautilus_trash_monitor;
}

gboolean
nautilus_trash_monitor_is_empty (void)
{
	return nautilus_trash_monitor_get ()->details->empty;
}

static gboolean
add_one_volume_trash (const NautilusVolume *volume,
		      gpointer callback_data)
{
	GnomeVFSURI *volume_mount_point_uri;
	GnomeVFSURI *trash_uri;
	GList **result;
	
	result = (GList **)callback_data;

	if (nautilus_volume_monitor_should_integrate_trash (volume)) {
	
		/* Get the uri of the volume mount point as the place
		 * "near" which to look for trash on the given volume.
		 */ 
		volume_mount_point_uri = gnome_vfs_uri_new (
			nautilus_volume_monitor_get_volume_mount_uri (volume));

		g_assert (volume_mount_point_uri != NULL);
	
		/* Look for trash. It is OK to use a sync call here because
		 * the options we use (don't create, don't look for it if we
		 * already don't know where it is) do not cause any IO.
		 */
		if (gnome_vfs_find_directory (volume_mount_point_uri,
			GNOME_VFS_DIRECTORY_KIND_TRASH, &trash_uri,
			FALSE, FALSE, 0777) == GNOME_VFS_OK) {

			/* found trash, put it on the list */
			*result = g_list_prepend (*result, trash_uri);
		}
		
		gnome_vfs_uri_unref (volume_mount_point_uri);
	}

	/* don't stop iterating */
	return FALSE; 
}

GList *
nautilus_trash_monitor_get_trash_directories (void)
{
	GList *result;

	result = NULL;

	/* Collect the trash directories on all the mounted volumes. */
	nautilus_volume_monitor_each_mounted_volume
		(nautilus_volume_monitor_get (), add_one_volume_trash, &result);
	
	return result;
}

static gboolean
add_one_trash_directory_if_needed (const NautilusVolume *volume,
				   gpointer callback_data)
{
	NautilusTrashMonitor *trash_monitor;

	trash_monitor = NAUTILUS_TRASH_MONITOR (callback_data);
	gtk_signal_emit (GTK_OBJECT (trash_monitor),
			 signals[CHECK_TRASH_DIRECTORY_ADDED],
			 volume);
	
	return FALSE;
}

void 
nautilus_trash_monitor_add_new_trash_directories (void)
{
	NautilusTrashMonitor *trash_monitor;

	trash_monitor = nautilus_trash_monitor_get ();
	nautilus_volume_monitor_each_mounted_volume
		(nautilus_volume_monitor_get (), add_one_trash_directory_if_needed,
		trash_monitor);
}