summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-file-changes-queue.c
blob: cc26fbb70163ad5ed71ab7933d31c4228582df44 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-directory.h: Nautilus 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: Pavel Cisler <pavel@eazel.com>
*/

#include <config.h>

#include "nautilus-directory-notify.h"
#include "nautilus-file-changes-queue.h"
#include "nautilus-glib-extensions.h"

#ifdef G_THREADS_ENABLED
#define MUTEX_LOCK(a)	if ((a) != NULL) g_mutex_lock (a)
#define MUTEX_UNLOCK(a)	if ((a) != NULL) g_mutex_unlock (a)
#else
#define MUTEX_LOCK(a)
#define MUTEX_UNLOCK(a)
#endif

typedef enum {
	CHANGE_FILE_INITIAL,
	CHANGE_FILE_ADDED,
	CHANGE_FILE_REMOVED,
	CHANGE_FILE_MOVED,
	CHANGE_METADATA_COPY,
	CHANGE_METADATA_MOVE,
	CHANGE_METADATA_REMOVE,
	CHANGE_POSITION_SETTING
} NautilusFileChangeKind;

typedef struct {
	NautilusFileChangeKind kind;
	char *from_uri;
	char *to_uri;
	GdkPoint point;
} NautilusFileChange;

typedef struct {
	GList *head;
	GList *tail;
#ifdef G_THREADS_ENABLED
	GMutex *mutex;
#endif
} NautilusFileChangesQueue;

static NautilusFileChangesQueue *
nautilus_file_changes_queue_new (void)
{
	NautilusFileChangesQueue *result;

	result = g_new0 (NautilusFileChangesQueue, 1);
	
#ifdef G_THREADS_ENABLED
	result->mutex = g_mutex_new ();
#endif
	return result;
}

static NautilusFileChangesQueue *
nautilus_file_changes_queue_get (void)
{
	static NautilusFileChangesQueue *file_changes_queue;

	if (file_changes_queue == NULL) {
		file_changes_queue = nautilus_file_changes_queue_new ();
	}

	return file_changes_queue;
}

#if 0 /* no public free call yet */

static void
nautilus_file_change_free (NautilusFileChange *change)
{
	g_free (change->from_uri);
	g_free (change->to_uri);
}

void
nautilus_file_changes_queue_free (NautilusFileChangesQueue *queue)
{
	GList *p;
	if (queue == NULL) {
		return;
	}
	
#ifdef G_THREADS_ENABLED
	/* if lock on a defunct mutex were defined (returning a failure)
	 * we would lock here 
	 */
#endif

	for (p = queue->head; p != NULL; p = p->next) {
		nautilus_file_change_free (p->data);
	}
	g_list_free (queue->head);

#ifdef G_THREADS_ENABLED
	g_mutex_free (queue->mutex);
#endif
	g_free (queue);
}

#endif /* no public free call yet */

static void
nautilus_file_changes_queue_add_common (NautilusFileChangesQueue *queue, 
	NautilusFileChange *new_item)
{
	/* enqueue the new queue item while locking down the list */
	MUTEX_LOCK (queue->mutex);

	queue->head = g_list_prepend (queue->head, new_item);
	if (queue->tail == NULL)
		queue->tail = queue->head;

	MUTEX_UNLOCK (queue->mutex);
}

void
nautilus_file_changes_queue_file_added (const char *uri)
{
	NautilusFileChange *new_item;
	NautilusFileChangesQueue *queue;

	queue = nautilus_file_changes_queue_get();

	new_item = g_new0 (NautilusFileChange, 1);
	new_item->kind = CHANGE_FILE_ADDED;
	new_item->from_uri = g_strdup (uri);
	nautilus_file_changes_queue_add_common (queue, new_item);
}

void
nautilus_file_changes_queue_file_removed (const char *uri)
{
	NautilusFileChange *new_item;
	NautilusFileChangesQueue *queue;

	queue = nautilus_file_changes_queue_get();

	new_item = g_new0 (NautilusFileChange, 1);
	new_item->kind = CHANGE_FILE_REMOVED;
	new_item->from_uri = g_strdup (uri);
	nautilus_file_changes_queue_add_common (queue, new_item);
}

void
nautilus_file_changes_queue_file_moved (const char *from, const char *to)
{
	NautilusFileChange *new_item;
	NautilusFileChangesQueue *queue;

	queue = nautilus_file_changes_queue_get ();

	new_item = g_new (NautilusFileChange, 1);
	new_item->kind = CHANGE_FILE_MOVED;
	new_item->from_uri = g_strdup (from);
	new_item->to_uri = g_strdup (to);
	nautilus_file_changes_queue_add_common (queue, new_item);
}

void
nautilus_file_changes_queue_schedule_metadata_copy (const char *from_uri,
	const char *to_uri)
{
	NautilusFileChange *new_item;
	NautilusFileChangesQueue *queue;

	queue = nautilus_file_changes_queue_get ();

	new_item = g_new (NautilusFileChange, 1);
	new_item->kind = CHANGE_METADATA_COPY;
	new_item->from_uri = g_strdup (from_uri);
	new_item->to_uri = g_strdup (to_uri);
	nautilus_file_changes_queue_add_common (queue, new_item);
}

void
nautilus_file_changes_queue_schedule_metadata_move (const char *from_uri,
	const char *to_uri)
{
	NautilusFileChange *new_item;
	NautilusFileChangesQueue *queue;

	queue = nautilus_file_changes_queue_get ();

	new_item = g_new (NautilusFileChange, 1);
	new_item->kind = CHANGE_METADATA_MOVE;
	new_item->from_uri = g_strdup (from_uri);
	new_item->to_uri = g_strdup (to_uri);
	nautilus_file_changes_queue_add_common (queue, new_item);
}

void
nautilus_file_changes_queue_schedule_metadata_remove (const char *uri)
{
	NautilusFileChange *new_item;
	NautilusFileChangesQueue *queue;

	queue = nautilus_file_changes_queue_get ();

	new_item = g_new (NautilusFileChange, 1);
	new_item->kind = CHANGE_METADATA_REMOVE;
	new_item->from_uri = g_strdup (uri);
	nautilus_file_changes_queue_add_common (queue, new_item);
}

void
nautilus_file_changes_queue_schedule_position_setting (const char *uri, 
	GdkPoint point)
{
	NautilusFileChange *new_item;
	NautilusFileChangesQueue *queue;

	queue = nautilus_file_changes_queue_get ();

	new_item = g_new (NautilusFileChange, 1);
	new_item->kind = CHANGE_POSITION_SETTING;
	new_item->from_uri = g_strdup (uri);
	new_item->point = point;
	nautilus_file_changes_queue_add_common (queue, new_item);
}

static NautilusFileChange *
nautilus_file_changes_queue_get_change (NautilusFileChangesQueue *queue)
{
	GList *new_tail;
	NautilusFileChange *result;

	g_assert (queue != NULL);
	
	/* dequeue the tail item while locking down the list */
	MUTEX_LOCK (queue->mutex);

	if (queue->tail == NULL) {
		result = NULL;
	} else {
		new_tail = queue->tail->prev;
		result = queue->tail->data;
		queue->head = g_list_remove_link (queue->head,
						  queue->tail);
		g_list_free_1 (queue->tail);
		queue->tail = new_tail;
	}

	MUTEX_UNLOCK (queue->mutex);

	return result;
}

enum {
	CONSUME_CHANGES_MAX_CHUNK = 10
};

static void
pairs_list_free (GList *pairs)
{
	GList *p;
	URIPair *pair;

	/* deep delete the list of pairs */

	for (p = pairs; p != NULL; p = p->next) {
		/* delete the strings in each pair */
		pair = p->data;
		g_free (pair->from_uri);
		g_free (pair->to_uri);
	}

	/* delete the list and the now empty pair structs */
	nautilus_g_list_free_deep (pairs);
}

static void
position_setting_list_free (GList *list)
{
	GList *p;
	NautilusFileChangesQueuePositionSetting *item;

	for (p = list; p != NULL; p = p->next) {
		item = p->data;
		g_free (item->uri);
	}
	/* delete the list and the now empty structs */
	nautilus_g_list_free_deep (list);
}

/* go through changes in the change queue, send ones with the same kind
 * in a list to the different nautilus_directory_notify calls
 */ 
void
nautilus_file_changes_consume_changes (gboolean consume_all)
{
	NautilusFileChange *change;
	GList *additions, *deletions, *moves;
	GList *metadata_copy_requests, *metadata_move_requests, *metadata_remove_requests;
	GList *position_setting_requests;
	URIPair *pair;
	NautilusFileChangesQueuePositionSetting *position_setting;
	int kind;
	int chunk_count;
	NautilusFileChangesQueue *queue;
	

	additions = NULL;
	deletions = NULL;
	moves = NULL;
	metadata_copy_requests = NULL;
	metadata_move_requests = NULL;
	metadata_remove_requests = NULL;
	position_setting_requests = NULL;
	kind = CHANGE_FILE_INITIAL;

	queue = nautilus_file_changes_queue_get();
		
	/* Consume changes from the queue, stuffing them into one of three lists,
	 * keep doing it while the changes are of the same kind, then send them off.
	 * This is to ensure that the changes get sent off in the same order that they 
	 * arrived.
	 */
	for (chunk_count = 0; ; chunk_count++) {
		change = nautilus_file_changes_queue_get_change (queue);

		if (change == NULL
			/* no changes left */
			|| change->kind != kind
			/* all the changes we have are different that the new one */
			|| (!consume_all && chunk_count >= CONSUME_CHANGES_MAX_CHUNK)) {
			/* we have reached the chunk maximum */

			/* Send changes we collected off. 
			 * At one time we may only have one of the three lists
			 * contain changes.
			 */

			g_assert ((deletions != NULL) + (moves != NULL) 
				+ (additions != NULL) + (metadata_copy_requests != NULL)
				+ (metadata_move_requests != NULL) 
				+ (metadata_remove_requests != NULL) 
				+ (position_setting_requests != NULL) <= 1);
				
			if (deletions != NULL) {
				nautilus_directory_notify_files_removed (deletions);
				nautilus_g_list_free_deep (deletions);
				deletions = NULL;
			}
			if (moves != NULL) {
				nautilus_directory_notify_files_moved (moves);
				pairs_list_free (moves);
				moves = NULL;
			}
			if (additions != NULL) {
				nautilus_directory_notify_files_added (additions);
				nautilus_g_list_free_deep (additions);
				additions = NULL;
			}
			if (metadata_copy_requests != NULL) {
				nautilus_directory_schedule_metadata_copy (metadata_copy_requests);
				pairs_list_free (metadata_copy_requests);
				metadata_copy_requests = NULL;
			}
			if (metadata_move_requests != NULL) {
				nautilus_directory_schedule_metadata_move (metadata_move_requests);
				pairs_list_free (metadata_move_requests);
				metadata_move_requests = NULL;
			}
			if (metadata_remove_requests != NULL) {
				nautilus_directory_schedule_metadata_remove (metadata_remove_requests);
				nautilus_g_list_free_deep (metadata_remove_requests);
				metadata_remove_requests = NULL;
			}
			if (position_setting_requests != NULL) {
				nautilus_directory_schedule_position_setting (position_setting_requests);
				position_setting_list_free (position_setting_requests);
				position_setting_requests = NULL;
			}
		}

		if (change == NULL) {
			/* we are done */
			return;
		}
		
		kind = change->kind;

		/* add the new change to the list */
		switch (kind) {
		case CHANGE_FILE_ADDED:
			additions = g_list_append (additions, change->from_uri);
			break;

		case CHANGE_FILE_REMOVED:
			deletions = g_list_append (deletions, change->from_uri);
			break;

		case CHANGE_FILE_MOVED:
			pair = g_new (URIPair, 1);
			pair->from_uri = change->from_uri;
			pair->to_uri = change->to_uri;
			moves = g_list_append (moves, pair);
			break;

		case CHANGE_METADATA_COPY:
			pair = g_new (URIPair, 1);
			pair->from_uri = change->from_uri;
			pair->to_uri = change->to_uri;
			metadata_copy_requests = g_list_append (metadata_copy_requests, pair);
			break;

		case CHANGE_METADATA_MOVE:
			pair = g_new (URIPair, 1);
			pair->from_uri = change->from_uri;
			pair->to_uri = change->to_uri;
			metadata_move_requests = g_list_append (metadata_move_requests, pair);
			break;

		case CHANGE_METADATA_REMOVE:
			metadata_remove_requests = g_list_append (metadata_remove_requests, 
				change->from_uri);
			break;

		case CHANGE_POSITION_SETTING:
			position_setting = g_new (NautilusFileChangesQueuePositionSetting, 1);
			position_setting->uri = change->from_uri;
			position_setting->point = change->point;
			position_setting_requests = g_list_append (position_setting_requests,
				position_setting);
			break;

		default:
			g_assert_not_reached ();
			break;
		}
		change->from_uri = NULL;
		change->to_uri = NULL;
	}	

}