summaryrefslogtreecommitdiff
path: root/camel/camel-vtrash-folder.c
blob: 642e637466df9afee3b1c580e86142816c7909e5 (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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *	     Michael Zucchi <notzed@ximian.com>
 *
 *  Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * 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 Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <glib.h>
#include <glib/gi18n-lib.h>

#include "camel-exception.h"
#include "camel-mime-message.h"
#include "camel-private.h"
#include "camel-store.h"
#include "camel-vee-store.h"
#include "camel-vtrash-folder.h"
#include "camel-string-utils.h"

/* Returns the class for a CamelFolder */
#define CF_CLASS(so) ((CamelFolderClass *)((CamelObject *)(so))->klass)

static struct {
	const char *full_name;
	const char *name;
	const char *expr;
	guint32 bit;
	guint32 flags;
	const char *error_copy;
	const char *db_col;
} vdata[] = {
	{ CAMEL_VTRASH_NAME, N_("Trash"), "(match-all (system-flag \"Deleted\"))", CAMEL_MESSAGE_DELETED, CAMEL_FOLDER_IS_TRASH,
	  N_("Cannot copy messages to the Trash folder"), "deleted" },
	{ CAMEL_VJUNK_NAME, N_("Junk"), "(match-all (system-flag \"Junk\"))", CAMEL_MESSAGE_JUNK, CAMEL_FOLDER_IS_JUNK,
	  N_("Cannot copy messages to the Junk folder"), "junk" },
};

static CamelVeeFolderClass *camel_vtrash_folder_parent;

static void camel_vtrash_folder_class_init (CamelVTrashFolderClass *klass);

static void
camel_vtrash_folder_init (CamelVTrashFolder *vtrash)
{
	/*CamelFolder *folder = CAMEL_FOLDER (vtrash);*/
}

CamelType
camel_vtrash_folder_get_type (void)
{
	static CamelType type = CAMEL_INVALID_TYPE;
	
	if (type == CAMEL_INVALID_TYPE) {
		type = camel_type_register (camel_vee_folder_get_type (),
					    "CamelVTrashFolder",
					    sizeof (CamelVTrashFolder),
					    sizeof (CamelVTrashFolderClass),
					    (CamelObjectClassInitFunc) camel_vtrash_folder_class_init,
					    NULL,
					    (CamelObjectInitFunc) camel_vtrash_folder_init,
					    NULL);
	}
	
	return type;
}

/**
 * camel_vtrash_folder_new:
 * @parent_store: the parent #CamelVeeStore object
 * @type: type of vfolder, #CAMEL_VTRASH_FOLDER_TRASH or
 * #CAMEL_VTRASH_FOLDER_JUNK currently.
 *
 * Create a new CamelVTrashFolder object.
 *
 * Returns: a new #CamelVTrashFolder object
 **/
CamelFolder *
camel_vtrash_folder_new (CamelStore *parent_store, camel_vtrash_folder_t type)
{
	CamelVTrashFolder *vtrash;
	
	g_assert(type < CAMEL_VTRASH_FOLDER_LAST);

	vtrash = (CamelVTrashFolder *)camel_object_new(camel_vtrash_folder_get_type());
	camel_vee_folder_construct(CAMEL_VEE_FOLDER (vtrash), parent_store, vdata[type].full_name, _(vdata[type].name),
				   CAMEL_STORE_FOLDER_PRIVATE|CAMEL_STORE_FOLDER_CREATE|CAMEL_STORE_VEE_FOLDER_AUTO|CAMEL_STORE_VEE_FOLDER_SPECIAL);

	((CamelFolder *)vtrash)->folder_flags |= vdata[type].flags;
	camel_vee_folder_set_expression((CamelVeeFolder *)vtrash, vdata[type].expr);
	vtrash->bit = vdata[type].bit;
	vtrash->type = type;

	return (CamelFolder *)vtrash;
}

/* This entire code will be useless, since we sync the counts always. */
static int
vtrash_getv(CamelObject *object, CamelException *ex, CamelArgGetV *args)
{
	CamelFolder *folder = (CamelFolder *)object;
	int i;
	guint32 tag;
	int unread = -1, deleted = 0, junked = 0, visible = 0, count = -1;

	for (i=0;i<args->argc;i++) {
		CamelArgGet *arg = &args->argv[i];

		tag = arg->tag;

		/* NB: this is a copy of camel-folder.c with the unread count logic altered.
		   makes sure its still atomically calculated */
		switch (tag & CAMEL_ARG_TAG) {
		case CAMEL_FOLDER_ARG_UNREAD:
		case CAMEL_FOLDER_ARG_DELETED:
		case CAMEL_FOLDER_ARG_JUNKED:
		case CAMEL_FOLDER_ARG_VISIBLE:
			/* This is so we can get the values atomically, and also so we can calculate them only once */
			if (unread == -1) {
				int j;
				CamelMessageInfoBase *info;
				CamelVeeMessageInfo *vinfo;

				unread = 0;
				count = camel_folder_summary_count(folder->summary);
				for (j=0; j<count; j++) {
					if ((info = (CamelMessageInfoBase *) camel_folder_summary_index(folder->summary, j))) {
						guint32 flags = 0;

						vinfo = (CamelVeeMessageInfo *) info;
						//flags = vinfo->old_flags ? vinfo->old_flags : camel_message_info_flags(info);

						if ((flags & (CAMEL_MESSAGE_SEEN)) == 0)
							unread++;
						if (flags & CAMEL_MESSAGE_DELETED)
							deleted++;
						if (flags & CAMEL_MESSAGE_JUNK)
							junked++;
						if ((flags & (CAMEL_MESSAGE_DELETED|CAMEL_MESSAGE_JUNK)) == 0)
							visible++;
						camel_message_info_free(info);
					}
				}
			}

			switch (tag & CAMEL_ARG_TAG) {
			case CAMEL_FOLDER_ARG_UNREAD:
				count = unread;
				break;
			case CAMEL_FOLDER_ARG_DELETED:
				count = deleted;
				break;
			case CAMEL_FOLDER_ARG_JUNKED:
				count = junked;
				break;
			case CAMEL_FOLDER_ARG_VISIBLE:
				count = visible;
				break;
			}

			*arg->ca_int = count;
			break;
		default:
			continue;
		}

		arg->tag = (tag & CAMEL_ARG_TYPE) | CAMEL_ARG_IGNORE;
	}

	return ((CamelObjectClass *)camel_vtrash_folder_parent)->getv(object, ex, args);
}

static void
vtrash_append_message (CamelFolder *folder, CamelMimeMessage *message,
		       const CamelMessageInfo *info, char **appended_uid,
		       CamelException *ex)
{
	camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, 
			     _(vdata[((CamelVTrashFolder *)folder)->type].error_copy));
}
#if 0
struct _transfer_data {
	CamelFolder *folder;
	CamelFolder *dest;
	GPtrArray *uids;
	gboolean delete;
};

static void
transfer_messages(CamelFolder *folder, struct _transfer_data *md, CamelException *ex)
{
	int i;

	if (!camel_exception_is_set (ex))
		camel_folder_transfer_messages_to(md->folder, md->uids, md->dest, NULL, md->delete, ex);

	for (i=0;i<md->uids->len;i++)
		g_free(md->uids->pdata[i]);
	g_ptr_array_free(md->uids, TRUE);
	camel_object_unref((CamelObject *)md->folder);
	g_free(md);
}

static void
vtrash_transfer_messages_to (CamelFolder *source, GPtrArray *uids,
			     CamelFolder *dest, GPtrArray **transferred_uids,
			     gboolean delete_originals, CamelException *ex)
{
	CamelVeeMessageInfo *mi;
	int i;
	GHashTable *batch = NULL;
	const char *tuid;
	struct _transfer_data *md;
	guint32 sbit = ((CamelVTrashFolder *)source)->bit;

	/* This is a special case of transfer_messages_to: Either the
	 * source or the destination is a vtrash folder (but not both
	 * since a store should never have more than one).
	 */

	if (transferred_uids)
		*transferred_uids = NULL;

	if (CAMEL_IS_VTRASH_FOLDER (dest)) {
		/* Copy to trash is meaningless. */
		if (!delete_originals) {
			camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, 
					     _(vdata[((CamelVTrashFolder *)dest)->type].error_copy));
			return;
		}

		/* Move to trash is the same as setting the message flag */
		for (i = 0; i < uids->len; i++)
			camel_folder_set_message_flags(source, uids->pdata[i], ((CamelVTrashFolder *)dest)->bit, ~0);
		return;
	}

	/* Moving/Copying from the trash to the original folder = undelete.
	 * Moving/Copying from the trash to a different folder = move/copy.
	 *
	 * Need to check this uid by uid, but we batch up the copies.
	 */

	for (i = 0; i < uids->len; i++) {
		mi = (CamelVeeMessageInfo *)camel_folder_get_message_info (source, uids->pdata[i]);
		if (mi == NULL) {
			g_warning ("Cannot find uid %s in source folder during transfer", (char *) uids->pdata[i]);
			continue;
		}
		
		if (dest == mi->summary->folder) {
			/* Just unset the flag on the original message */
			camel_folder_set_message_flags (source, uids->pdata[i], sbit, 0);
		} else {
			if (batch == NULL)
				batch = g_hash_table_new(NULL, NULL);
			md = g_hash_table_lookup(batch, mi->summary->folder);
			if (md == NULL) {
				md = g_malloc0(sizeof(*md));
				md->folder = mi->summary->folder;
				camel_object_ref((CamelObject *)md->folder);
				md->uids = g_ptr_array_new();
				md->dest = dest;
				g_hash_table_insert(batch, mi->summary->folder, md);
			}

			tuid = uids->pdata[i];
			if (strlen(tuid)>8)
				tuid += 8;
			g_ptr_array_add(md->uids, g_strdup(tuid));
		}
		camel_folder_free_message_info (source, (CamelMessageInfo *)mi);
	}

	if (batch) {
		g_hash_table_foreach(batch, (GHFunc)transfer_messages, ex);
		g_hash_table_destroy(batch);
	}
}

#endif 
#if 0
/* FIXME[disk-summary] rewrite the same way as camel-vee-summary.c */
static GPtrArray *
vtrash_search_by_expression(CamelFolder *folder, const char *expression, CamelException *ex)
{
	GList *node;
	GPtrArray *matches, *result = g_ptr_array_new(), *uids = g_ptr_array_new();
	struct _CamelVeeFolderPrivate *p = ((CamelVeeFolder *)folder)->priv;
	GPtrArray *infos = camel_folder_get_summary(folder);

	/* we optimise the search by only searching for messages which we have anyway */
	CAMEL_VEE_FOLDER_LOCK(folder, subfolder_lock);
	node = p->folders;
	while (node) {
		CamelFolder *f = node->data;
		int i;
		char hash[8];

		camel_vee_folder_hash_folder(f, hash);

		for (i=0;i<infos->len;i++) {
			CamelVeeMessageInfo  *vmi = (CamelVeeMessageInfo *) camel_folder_summary_uid (folder->summary, infos->pdata[i]);
			if (!vmi)
				continue;
			//if (camel_message_info_flags(mi) & ((CamelVTrashFolder *)folder)->bit)
			if (vmi->summary == f->summary) /* Belongs to this folder */
				g_ptr_array_add(uids, (void *)camel_pstring_strdup(infos->pdata[i]+8));
			camel_message_info_free (vmi);
		}

                /* FIXME[disk-summary] search in the DB of the folder, for
		 * the expression, with the vtrash bit (junk/trash) */
		if (uids->len > 0
		    && (matches = camel_folder_search_by_uids(f, expression, uids, NULL))) {
			for (i = 0; i < matches->len; i++) {
				char *uid = matches->pdata[i], *vuid;

				vuid = g_malloc(strlen(uid)+9);
				memcpy(vuid, hash, 8);
				strcpy(vuid+8, uid);
				g_ptr_array_add(result, (gpointer) camel_pstring_strdup(vuid));
				g_free (vuid);
			}
			camel_folder_search_free(f, matches);
		}
		g_ptr_array_set_size(uids, 0);

		node = g_list_next(node);
	}
	camel_folder_free_summary (folder, infos);
	CAMEL_VEE_FOLDER_UNLOCK(folder, subfolder_lock);

	g_ptr_array_foreach (uids, (GFunc) camel_pstring_free, NULL);
	g_ptr_array_free(uids, TRUE);

	return result;
}
#endif
static GPtrArray *
vtrash_search_by_uids(CamelFolder *folder, const char *expression, GPtrArray *uids, CamelException *ex)
{
	GList *node;
	GPtrArray *matches, *result = g_ptr_array_new(), *folder_uids = g_ptr_array_new();
	struct _CamelVeeFolderPrivate *p = ((CamelVeeFolder *)folder)->priv;
	
	CAMEL_VEE_FOLDER_LOCK(folder, subfolder_lock);

	node = p->folders;
	while (node) {
		CamelFolder *f = node->data;
		int i;
		char hash[8];
		
		camel_vee_folder_hash_folder(f, hash);

		/* map the vfolder uid's to the source folder uid's first */
		/* FIXME[disk-summary] check this. is it uids od folder_uids */
		//g_ptr_array_set_size(uids, 0);
		g_ptr_array_set_size (folder_uids, 0);
		for (i=0;i<uids->len;i++) {
			char *uid = uids->pdata[i];
			
			//if (strlen(uid) >= 8 && strncmp(uid, hash, 8) == 0) {
			if (strncmp(uid, hash, 8) == 0) {				
				//CamelMessageInfo *mi;
				/* FIXME[disk-summary] is it really reqd, if
				 * so uncomment it */
				//mi = camel_folder_get_message_info(f, uid+8);
				//if (mi) {
				//	if(camel_message_info_flags(mi) & ((CamelVTrashFolder *)folder)->bit)
						g_ptr_array_add(folder_uids, uid+8);
				//	camel_folder_free_message_info(f, mi);
				//}
			}
		}

		if (folder_uids->len > 0
		    && (matches = camel_folder_search_by_uids(f, expression, folder_uids, ex))) {
			for (i = 0; i < matches->len; i++) {
				char *uid = matches->pdata[i], *vuid;
				
				vuid = g_malloc(strlen(uid)+9);
				memcpy(vuid, hash, 8);
				strcpy(vuid+8, uid);
				g_ptr_array_add(result, (gpointer) camel_pstring_strdup(vuid));
				g_free (vuid);
			}
			camel_folder_search_free(f, matches);
		}
		node = g_list_next(node);
	}

	CAMEL_VEE_FOLDER_UNLOCK(folder, subfolder_lock);

	g_ptr_array_free(folder_uids, TRUE);

	return result;
}

static void
vtrash_uid_removed(CamelVTrashFolder *vf, const char *uid, char hash[8])
{
	char *vuid;

	vuid = g_alloca(strlen(uid)+9);
	memcpy(vuid, hash, 8);
	strcpy(vuid+8, uid);
	camel_folder_change_info_remove_uid(((CamelVeeFolder *)vf)->changes, vuid);
	camel_folder_summary_remove_uid(((CamelFolder *)vf)->summary, vuid);
}

static void
vtrash_uid_added(CamelVTrashFolder *vf, const char *uid, CamelFolderSummary *ssummary, char hash[8])
{
	char *vuid;
	CamelVeeMessageInfo *vinfo;

	vuid = g_alloca(strlen(uid)+9);
	memcpy(vuid, hash, 8);
	strcpy(vuid+8, uid);
	vinfo = (CamelVeeMessageInfo *)camel_folder_summary_uid(((CamelFolder *)vf)->summary, vuid);
	if (vinfo == NULL) {
		CamelMessageInfo *tinfo;
		tinfo = (CamelMessageInfo *) camel_vee_summary_add((CamelVeeSummary *)((CamelFolder *)vf)->summary, ssummary, uid, hash);
		if (tinfo) {
			camel_folder_change_info_add_uid(((CamelVeeFolder *)vf)->changes, vuid);
			camel_message_info_free (tinfo);
		}
	}
}

static void
vtrash_folder_changed(CamelVeeFolder *vf, CamelFolder *sub, CamelFolderChangeInfo *changes)
{
	CamelMessageInfo *info;
	char hash[8];
	CamelFolderChangeInfo *vf_changes = NULL;
	int i;

	camel_vee_folder_hash_folder(sub, hash);

	CAMEL_VEE_FOLDER_LOCK(vf, summary_lock);

	/* remove any removed that we also have */
	for (i=0;i<changes->uid_removed->len;i++)
		vtrash_uid_removed((CamelVTrashFolder *)vf, (const char *)changes->uid_removed->pdata[i], hash);

	/* check any changed still deleted/junked */
	for (i=0;i<changes->uid_changed->len;i++) {
		const char *uid = changes->uid_changed->pdata[i];

		info = camel_folder_get_message_info(sub, uid);
		if (info == NULL)
			continue;

		if ((camel_message_info_flags(info) & ((CamelVTrashFolder *)vf)->bit) == 0)
			vtrash_uid_removed((CamelVTrashFolder *)vf, uid, hash);
		else
			vtrash_uid_added((CamelVTrashFolder *)vf, uid, sub->summary, hash);

		camel_message_info_free(info);
	}

	/* add any new ones which are already matching */
	for (i=0;i<changes->uid_added->len;i++) {
		const char *uid = changes->uid_added->pdata[i];

		info = camel_folder_get_message_info(sub, uid);
		if (info == NULL)
			continue;

		if ((camel_message_info_flags(info) & ((CamelVTrashFolder *)vf)->bit) != 0)
			vtrash_uid_added((CamelVTrashFolder *)vf, uid, sub->summary, hash);

		camel_message_info_free(info);
	}

	if (camel_folder_change_info_changed(((CamelVeeFolder *)vf)->changes)) {
		vf_changes = ((CamelVeeFolder *)vf)->changes;
		((CamelVeeFolder *)vf)->changes = camel_folder_change_info_new();
	}

	CAMEL_VEE_FOLDER_UNLOCK(vf, summary_lock);
	
	if (vf_changes) {
		camel_object_trigger_event(vf, "folder_changed", vf_changes);
		camel_folder_change_info_free(vf_changes);
	}
}

static void
vtrash_add_folder(CamelVeeFolder *vf, CamelFolder *sub)
{
	GPtrArray *infos=NULL;
	int i;
	char hash[8], *shash;
	CamelFolderChangeInfo *vf_changes = NULL;

	camel_vee_folder_hash_folder(sub, hash);
	shash = g_strdup_printf("%c%c%c%c%c%c%c%c", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7]);
	if (!g_hash_table_lookup (vf->hashes, shash))
		g_hash_table_insert (vf->hashes, g_strdup(shash), sub->summary);
	
	CAMEL_VEE_FOLDER_LOCK(vf, summary_lock);

	if (((CamelVTrashFolder *)vf)->bit == CAMEL_MESSAGE_DELETED) {
		infos = camel_db_get_folder_deleted_uids (sub->parent_store->cdb, sub->full_name, NULL);
		if (infos) {
			((CamelFolder *)vf)->summary->saved_count += infos->len;
			((CamelFolder *)vf)->summary->deleted_count += infos->len;
		}
	}
	else if (((CamelVTrashFolder *)vf)->bit == CAMEL_MESSAGE_JUNK)
		infos = camel_db_get_folder_junk_uids (sub->parent_store->cdb, sub->full_name, NULL);

	if (!infos) {
		CAMEL_VEE_FOLDER_UNLOCK(vf, summary_lock);
		g_free (shash);
		return;
	}
	
	for (i=0;i<infos->len;i++) {
		char *uid = infos->pdata[i];
		vtrash_uid_added((CamelVTrashFolder *)vf, uid, sub->summary, hash);
	}
	
	g_ptr_array_foreach (infos, (GFunc) camel_pstring_free, NULL);
	g_ptr_array_free (infos, TRUE);

	if (camel_folder_change_info_changed(vf->changes)) {
		vf_changes = vf->changes;
		vf->changes = camel_folder_change_info_new();
	}

	CAMEL_VEE_FOLDER_UNLOCK(vf, summary_lock);

	if (vf_changes) {
		camel_object_trigger_event(vf, "folder_changed", vf_changes);
		camel_folder_change_info_free(vf_changes);
	}

	g_free(shash);
}
#if 0
static void
vtrash_remove_folder(CamelVeeFolder *vf, CamelFolder *sub)
{
	GPtrArray *infos;
	int i;
	char hash[8], *shash;
	CamelFolderChangeInfo *vf_changes = NULL;
	CamelFolderSummary *ssummary = sub->summary;
	int start, last;



	CAMEL_VEE_FOLDER_LOCK(vf, summary_lock);

	start = -1;
	last = -1;
	infos = camel_folder_get_summary ((CamelFolder *) vf);
	for (i=0;i<infos->len;i++) {

		CamelVeeMessageInfo *mi = (CamelVeeMessageInfo *) camel_folder_summary_uid (((CamelFolder *)vf)->summary, infos->pdata[i]);
		if (mi == NULL)
			continue;
		if (mi->summary == NULL) {
			camel_message_info_free (mi);
			continue;
		}

		if (mi->summary == ssummary) {
			const char *uid = camel_message_info_uid(mi);

			camel_folder_change_info_remove_uid(vf->changes, uid);

			if (last == -1) {
				last = start = i;
			} else if (last+1 == i) {
				last = i;
			} else {
				camel_folder_summary_remove_range(((CamelFolder *)vf)->summary, start, last);
				i -= (last-start)+1;
				start = last = i;
			}
		}
		camel_message_info_free (mi);
	}
	camel_folder_free_summary(sub, infos);

	if (last != -1)
		camel_folder_summary_remove_range(((CamelFolder *)vf)->summary, start, last);

	if (camel_folder_change_info_changed(vf->changes)) {
		vf_changes = vf->changes;
		vf->changes = camel_folder_change_info_new();
	}

	CAMEL_VEE_FOLDER_UNLOCK(vf, summary_lock);

	camel_vee_folder_hash_folder(sub, hash);
	shash = g_strdup_printf("%c%c%c%c%c%c%c%c", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7]);
	if (g_hash_table_lookup (vf->hashes, shash))
		g_hash_table_remove (vf->hashes, shash);
	g_free(shash);	
	if (vf_changes) {
		camel_object_trigger_event(vf, "folder_changed", vf_changes);
		camel_folder_change_info_free(vf_changes);
	}
}
#endif 
static int
vtrash_rebuild_folder(CamelVeeFolder *vf, CamelFolder *source, CamelException *ex)
{
	/* we should always be in sync */
	return 0;
}

static void
camel_vtrash_folder_class_init (CamelVTrashFolderClass *klass)
{
	CamelFolderClass *folder_class = (CamelFolderClass *) klass;
	
	camel_vtrash_folder_parent = CAMEL_VEE_FOLDER_CLASS(camel_vee_folder_get_type());

	/* Not required from here on. We don't count */
	((CamelObjectClass *)klass)->getv = vtrash_getv; 
	
	folder_class->append_message = vtrash_append_message;
	//folder_class->transfer_messages_to = vtrash_transfer_messages_to;
	/* Not required, lets use the base class search function */
	/* folder_class->search_by_expression = vtrash_search_by_expression; */
	/* folder_class->search_by_uids = vtrash_search_by_uids; */

	//((CamelVeeFolderClass *)klass)->add_folder = vtrash_add_folder;
	//((CamelVeeFolderClass *)klass)->remove_folder = vtrash_remove_folder;
	//((CamelVeeFolderClass *)klass)->rebuild_folder = vtrash_rebuild_folder;

	//((CamelVeeFolderClass *)klass)->folder_changed = vtrash_folder_changed;
}