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

   nautilus-merged-directory.c: Subclass of NautilusDirectory to implement the
   virtual merged 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-merged-directory.h"

#include "nautilus-directory-private.h"
#include "nautilus-file.h"
#include "nautilus-glib-extensions.h"
#include "nautilus-gtk-macros.h"
#include <gtk/gtksignal.h>

struct NautilusMergedDirectoryDetails {
	GList *directories;
	GHashTable *callbacks;
	GHashTable *monitors;
};

typedef struct {
	/* Basic configuration. */
	NautilusMergedDirectory *merged;
	NautilusDirectoryCallback callback;
	gpointer callback_data;

	GList *wait_for_attributes;

	GList *non_ready_directories;
	GList *merged_file_list;
} MergedCallback;

typedef struct {
	NautilusMergedDirectory *merged;

	GList *monitor_attributes;
	gboolean force_reload;
} MergedMonitor;

static void     nautilus_merged_directory_initialize       (gpointer                 object,
							    gpointer                 klass);
static void     nautilus_merged_directory_initialize_class (gpointer                 klass);
static void     remove_all_real_directories                (NautilusMergedDirectory *merged);
static guint    merged_callback_hash                       (gconstpointer            merged_callback);
static gboolean merged_callback_equal                      (gconstpointer            merged_callback,
							    gconstpointer            merged_callback_2);

NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusMergedDirectory,
				   nautilus_merged_directory,
				   NAUTILUS_TYPE_DIRECTORY)

static void
nautilus_merged_directory_initialize (gpointer object, gpointer klass)
{
	NautilusMergedDirectory *merged;

	merged = NAUTILUS_MERGED_DIRECTORY (object);

	merged->details = g_new0 (NautilusMergedDirectoryDetails, 1);
	merged->details->callbacks = g_hash_table_new
		(merged_callback_hash, merged_callback_equal);
	merged->details->monitors = g_hash_table_new
		(g_direct_hash, g_direct_equal);
}

static void
merged_destroy (GtkObject *object)
{
	NautilusMergedDirectory *merged;

	merged = NAUTILUS_MERGED_DIRECTORY (object);

	remove_all_real_directories (merged);

	if (g_hash_table_size (merged->details->callbacks) != 0) {
		g_warning ("call_when_ready still pending when merged virtual directory is destroyed");
	}
	if (g_hash_table_size (merged->details->monitors) != 0) {
		g_warning ("file monitor still active when merged virtual directory is destroyed");
	}

	g_hash_table_destroy (merged->details->callbacks);
	g_hash_table_destroy (merged->details->monitors);
	g_free (merged->details);

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

static guint
merged_callback_hash (gconstpointer merged_callback_as_pointer)
{
	const MergedCallback *merged_callback;

	merged_callback = merged_callback_as_pointer;
	return GPOINTER_TO_UINT (merged_callback->callback)
		^ GPOINTER_TO_UINT (merged_callback->callback_data);
}

static gboolean
merged_callback_equal (gconstpointer merged_callback_as_pointer,
		       gconstpointer merged_callback_as_pointer_2)
{
	const MergedCallback *merged_callback, *merged_callback_2;

	merged_callback = merged_callback_as_pointer;
	merged_callback_2 = merged_callback_as_pointer_2;

	return merged_callback->callback == merged_callback_2->callback
		&& merged_callback->callback_data == merged_callback_2->callback_data;
}

/* Return true if any directory in the list does. */
static gboolean
merged_contains_file (NautilusDirectory *directory,
		      NautilusFile *file)
{
	NautilusMergedDirectory *merged;
	GList *p;

	merged = NAUTILUS_MERGED_DIRECTORY (directory);

	for (p = merged->details->directories; p != NULL; p = p->next) {
		if (nautilus_directory_contains_file (p->data, file)) {
			return TRUE;
		}
	}
	return FALSE;
}

static void
merged_callback_destroy (MergedCallback *merged_callback)
{
	g_assert (merged_callback != NULL);
	g_assert (NAUTILUS_IS_MERGED_DIRECTORY (merged_callback->merged));

	nautilus_g_list_free_deep (merged_callback->wait_for_attributes);
	g_list_free (merged_callback->non_ready_directories);
	nautilus_file_list_free (merged_callback->merged_file_list);
	g_free (merged_callback);
}

static void
merged_callback_check_done (MergedCallback *merged_callback)
{
	/* Check if we are ready. */
	if (merged_callback->non_ready_directories != NULL) {
		return;
	}

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

	/* We are ready, so do the real callback. */
	(* merged_callback->callback) (NAUTILUS_DIRECTORY (merged_callback->merged),
				       merged_callback->merged_file_list,
				       merged_callback->callback_data);

	/* And we are done. */
	merged_callback_destroy (merged_callback);
}

static void
merged_callback_remove_directory (MergedCallback *merged_callback,
				  NautilusDirectory *directory)
{
	merged_callback->non_ready_directories = g_list_remove
		(merged_callback->non_ready_directories,
		 directory);

	/* Check if we are ready. */
	merged_callback_check_done (merged_callback);
}

static void
directory_ready_callback (NautilusDirectory *directory,
			  GList *files,
			  gpointer callback_data)
{
	MergedCallback *merged_callback;

	g_assert (NAUTILUS_IS_DIRECTORY (directory));
	g_assert (callback_data != NULL);

	merged_callback = callback_data;
	g_assert (g_list_find (merged_callback->non_ready_directories, directory) != NULL);

	/* Update based on this call. */
	merged_callback->merged_file_list = g_list_concat
		(merged_callback->merged_file_list,
		 nautilus_file_list_copy (files));

	/* Check if we are ready. */
	merged_callback_remove_directory (merged_callback,
					  directory);
}

static void
merged_callback_connect_directory (MergedCallback *merged_callback,
				   NautilusDirectory *real_merged)
{
	nautilus_directory_call_when_ready
		(real_merged,
		 merged_callback->wait_for_attributes,
		 directory_ready_callback, merged_callback);
}

static void
merged_call_when_ready (NautilusDirectory *directory,
			GList *file_attributes,
			NautilusDirectoryCallback callback,
			gpointer callback_data)
{
	NautilusMergedDirectory *merged;
	MergedCallback search_key, *merged_callback;
	GList *p;

	merged = NAUTILUS_MERGED_DIRECTORY (directory);

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

	/* Create a merged_callback record. */
	merged_callback = g_new0 (MergedCallback, 1);
	merged_callback->merged = merged;
	merged_callback->callback = callback;
	merged_callback->callback_data = callback_data;
	merged_callback->wait_for_attributes = nautilus_g_str_list_copy (file_attributes);
	for (p = merged->details->directories; p != NULL; p = p->next) {
		merged_callback->non_ready_directories = g_list_prepend
			(merged_callback->non_ready_directories, p->data);
	}

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

	/* Handle the pathological case where there are no directories. */
	if (merged->details->directories == NULL) {
		merged_callback_check_done (merged_callback);
	}

	/* Now tell all the directories about it. */
	for (p = merged->details->directories; p != NULL; p = p->next) {
		merged_callback_connect_directory (merged_callback, p->data);
	}
}

static void
merged_cancel_callback (NautilusDirectory *directory,
			NautilusDirectoryCallback callback,
			gpointer callback_data)
{
	NautilusMergedDirectory *merged;
	MergedCallback search_key, *merged_callback;
	GList *p;

	merged = NAUTILUS_MERGED_DIRECTORY (directory);

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

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

	/* Tell all the directories to cancel the call. */
	for (p = merged_callback->non_ready_directories; p != NULL; p = p->next) {
		nautilus_directory_cancel_callback
			(p->data,
			 directory_ready_callback, merged_callback);
	}
	merged_callback_destroy (merged_callback);
}

/* Create a monitor on each of the directories in the list. */
static void
merged_file_monitor_add (NautilusDirectory *directory,
			 gconstpointer client,
			 GList *file_attributes,
			 gboolean force_reload)
{
	NautilusMergedDirectory *merged;
	MergedMonitor *monitor;
	GList *p;

	merged = NAUTILUS_MERGED_DIRECTORY (directory);

	/* Map the client to a unique value so this doesn't interfere
	 * with direct monitoring of the directory by the same client.
	 */
	monitor = g_hash_table_lookup (merged->details->monitors, client);
	if (monitor != NULL) {
		g_assert (monitor->merged == merged);
		nautilus_g_list_free_deep (monitor->monitor_attributes);
	} else {
		monitor = g_new0 (MergedMonitor, 1);
		monitor->merged = merged;
		g_hash_table_insert (merged->details->monitors,
				     (gpointer) client, monitor);
	}
	monitor->monitor_attributes = nautilus_g_str_list_copy (file_attributes);
	monitor->force_reload = force_reload;
	
	/* Call through to the real directory add calls. */
	for (p = merged->details->directories; p != NULL; p = p->next) {
		nautilus_directory_file_monitor_add
			(p->data, monitor,
			 file_attributes, force_reload);
	}
}

/* Remove the monitor from each of the directories in the list. */
static void
merged_file_monitor_remove (NautilusDirectory *directory,
			    gconstpointer client)
{
	NautilusMergedDirectory *merged;
	MergedMonitor *monitor;
	GList *p;
	
	merged = NAUTILUS_MERGED_DIRECTORY (directory);
	
	/* Map the client to the value used by the earlier add call. */
        monitor = g_hash_table_lookup (merged->details->monitors, client);
	if (monitor == NULL) {
		return;
	}
	g_hash_table_remove (merged->details->monitors, client);

	/* Call through to the real directory remove calls. */
	for (p = merged->details->directories; p != NULL; p = p->next) {
		nautilus_directory_file_monitor_remove
			(p->data, monitor);
	}

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

/* Return true only if all directories in the list do. */
static gboolean
merged_are_all_files_seen (NautilusDirectory *directory)
{
	NautilusMergedDirectory *merged;
	GList *p;

	merged = NAUTILUS_MERGED_DIRECTORY (directory);

	for (p = merged->details->directories; p != NULL; p = p->next) {
		if (!nautilus_directory_are_all_files_seen (p->data)) {
			return FALSE;
		}
	}
	return TRUE;
}

/* Return true if any directory in the list does. */
static gboolean
merged_is_not_empty (NautilusDirectory *directory)
{
	NautilusMergedDirectory *merged;
	GList *p;

	merged = NAUTILUS_MERGED_DIRECTORY (directory);

	for (p = merged->details->directories; p != NULL; p = p->next) {
		if (nautilus_directory_is_not_empty (p->data)) {
			return TRUE;
		}
	}
	return FALSE;
}

static void
nautilus_merged_directory_initialize_class (gpointer klass)
{
	GtkObjectClass *object_class;
	NautilusDirectoryClass *directory_class;

	object_class = GTK_OBJECT_CLASS (klass);
	directory_class = NAUTILUS_DIRECTORY_CLASS (klass);
	
	object_class->destroy = merged_destroy;

	directory_class->contains_file = merged_contains_file;
	directory_class->call_when_ready = merged_call_when_ready;
	directory_class->cancel_callback = merged_cancel_callback;
	directory_class->file_monitor_add = merged_file_monitor_add;
	directory_class->file_monitor_remove = merged_file_monitor_remove;
 	directory_class->are_all_files_seen = merged_are_all_files_seen;
	directory_class->is_not_empty = merged_is_not_empty;
}

static void
forward_files_added_cover (NautilusDirectory *real_directory,
			   GList *files,
			   NautilusMergedDirectory *merged)
{
	nautilus_directory_emit_files_added (NAUTILUS_DIRECTORY (merged), files);
}

static void
forward_files_changed_cover (NautilusDirectory *real_directory,
			     GList *files,
			     NautilusMergedDirectory *merged)
{
	nautilus_directory_emit_files_changed (NAUTILUS_DIRECTORY (merged), files);
}

static void
monitor_add_directory (gpointer key,
		       gpointer value,
		       gpointer callback_data)
{
	MergedMonitor *monitor;
	
	monitor = value;
	nautilus_directory_file_monitor_add
		(NAUTILUS_DIRECTORY (callback_data), monitor,
		 monitor->monitor_attributes,
		 monitor->force_reload);
}

void
nautilus_merged_directory_add_real_directory (NautilusMergedDirectory *merged,
					      NautilusDirectory *real_directory)
{
	g_return_if_fail (NAUTILUS_IS_MERGED_DIRECTORY (merged));
	g_return_if_fail (NAUTILUS_IS_DIRECTORY (real_directory));
	g_return_if_fail (!NAUTILUS_IS_MERGED_DIRECTORY (real_directory));

	/* Quietly do nothing if asked to add something that's already there. */
	if (g_list_find (merged->details->directories, real_directory) != NULL) {
		return;
	}

	/* Add to our list of directories. */
	nautilus_directory_ref (real_directory);
	merged->details->directories = g_list_prepend
		(merged->details->directories, real_directory);

	/* Connect signals. */
	gtk_signal_connect (GTK_OBJECT (real_directory),
			    "files_added",
			    forward_files_added_cover,
			    merged);
	gtk_signal_connect (GTK_OBJECT (real_directory),
			    "files_changed",
			    forward_files_changed_cover,
			    merged);
	/* FIXME bugzilla.eazel.com 2540: Hook up the done_loading signal too? */

	/* Add the directory to any extant monitors. */
	g_hash_table_foreach (merged->details->monitors,
			      monitor_add_directory,
			      real_directory);
	/* FIXME bugzilla.eazel.com 2541: Do we need to add the directory to callbacks too? */
}

static void
merged_callback_remove_directory_cover (gpointer key,
					gpointer value,
					gpointer callback_data)
{
	merged_callback_remove_directory
		(value, NAUTILUS_DIRECTORY (callback_data));
}

static void
monitor_remove_directory (gpointer key,
			  gpointer value,
			  gpointer callback_data)
{
	nautilus_directory_file_monitor_remove
		(NAUTILUS_DIRECTORY (callback_data), value);
}

void
nautilus_merged_directory_remove_real_directory (NautilusMergedDirectory *merged,
						 NautilusDirectory *real_directory)
{
	g_return_if_fail (NAUTILUS_IS_MERGED_DIRECTORY (merged));
	g_return_if_fail (NAUTILUS_IS_DIRECTORY (real_directory));

	/* Quietly do nothing if asked to remove something that's not there. */
	if (g_list_find (merged->details->directories, real_directory) == NULL) {
		return;
	}

	/* Remove this directory from callbacks and monitors. */
	nautilus_g_hash_table_safe_for_each
		(merged->details->callbacks,
		 merged_callback_remove_directory_cover,
		 real_directory);
	g_hash_table_foreach
		(merged->details->monitors,
		 monitor_remove_directory,
		 real_directory);

	/* Disconnect all the signals. */
	gtk_signal_disconnect_by_data (GTK_OBJECT (real_directory), merged);

	/* Remove from our list of directories. */
	merged->details->directories = g_list_remove
		(merged->details->directories, real_directory);
	nautilus_directory_unref (real_directory);
}

static void
remove_all_real_directories (NautilusMergedDirectory *merged)
{
	while (merged->details->directories != NULL) {
		nautilus_merged_directory_remove_real_directory
			(merged, merged->details->directories->data);
	}
}