Index: camel/providers/local/camel-local-summary.h
===================================================================
--- camel/providers/local/camel-local-summary.h	(revision 10220)
+++ camel/providers/local/camel-local-summary.h	(working copy)
@@ -70,7 +70,7 @@ struct _CamelLocalSummaryClass {
 
 	char *(*encode_x_evolution)(CamelLocalSummary *cls, const CamelLocalMessageInfo *info);
 	int (*decode_x_evolution)(CamelLocalSummary *cls, const char *xev, CamelLocalMessageInfo *info);
-	int (*need_index)();
+	int (*need_index)(void);
 };
 
 CamelType	camel_local_summary_get_type	(void);
Index: camel/providers/local/camel-maildir-summary.c
===================================================================
--- camel/providers/local/camel-maildir-summary.c	(revision 10220)
+++ camel/providers/local/camel-maildir-summary.c	(working copy)
@@ -273,6 +273,8 @@ maildir_summary_add (CamelLocalSummary *
 			d(printf("Setting filename to %s\n", camel_maildir_info_filename(mi)));
 		}
 	}
+	
+	printf ("%s: changed:%d mi:%p\n", __FUNCTION__, camel_folder_change_info_changed (changes)?1:0, mi);
 
 	return (CamelMessageInfo *)mi;
 }
@@ -294,7 +296,7 @@ static CamelMessageInfo *message_info_ne
 			mdi->info.info.uid = camel_pstring_add (camel_folder_summary_next_uid_string(s), TRUE);
 
 		/* handle 'duplicates' */
-		info = camel_folder_summary_uid(s, uid);
+		info = camel_folder_summary_peek_info (s, uid);
 		if (info) {
 			d(printf("already seen uid '%s', just summarising instead\n", uid));
 			camel_message_info_free(mi);
@@ -504,6 +506,7 @@ camel_maildir_summary_add (CamelLocalSum
 	camel_object_unref((CamelObject *)mp);
 	maildirs->priv->current_file = NULL;
 	camel_folder_summary_set_index((CamelFolderSummary *)maildirs, NULL);
+	printf ("%s: %s\n", __FUNCTION__, filename);
 	g_free(filename);
 	return 0;
 }
@@ -557,6 +560,7 @@ maildir_summary_check(CamelLocalSummary 
 		camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
 			_("Cannot open maildir directory path: %s: %s"),
 			cls->folder_path, g_strerror (errno));
+		printf ("%s: cannot open %s: %s\n", __FUNCTION__, cur, g_strerror (errno));
 		g_free(cur);
 		g_free(new);
 		camel_operation_end(NULL);
@@ -566,7 +570,13 @@ maildir_summary_check(CamelLocalSummary 
 
 	/* keeps track of all uid's that have not been processed */
 	left = g_hash_table_new(g_str_hash, g_str_equal);
-	count = camel_folder_summary_count((CamelFolderSummary *)cls);
+	count = camel_folder_summary_count (s);
+	if (count != camel_folder_summary_cache_size (s)) {
+		printf ("%s: going to reload because count:%d cachesize:%d", __FUNCTION__, count, camel_folder_summary_cache_size (s));
+		camel_folder_summary_reload_from_db (s, ex);
+		count = camel_folder_summary_count (s);
+		printf (" finally count: %d\n", count);
+	}
 	forceindex = count == 0;
 	for (i=0;i<count;i++) {
 		info = camel_folder_summary_index((CamelFolderSummary *)cls, i);
@@ -709,11 +719,12 @@ maildir_summary_check(CamelLocalSummary 
 			g_free(dest);
 		}
 		camel_operation_end(NULL);
+		closedir(dir);
 	}
-	closedir(dir);
 
 	g_free(new);
 	g_free(cur);
+	printf ("%s: %s: changes:%d\n", __FUNCTION__, cls->folder_path, changes ? (camel_folder_change_info_changed (changes) ? 1 : 0) : -1);
 
 	g_mutex_unlock (((CamelMaildirSummary *) cls)->priv->summary_lock);
 
@@ -735,8 +746,10 @@ maildir_summary_sync(CamelLocalSummary *
 
 	d(printf("summary_sync(expunge=%s)\n", expunge?"true":"false"));
 
-	if (camel_local_summary_check(cls, changes, ex) == -1)
+	if (camel_local_summary_check(cls, changes, ex) == -1) {
+		printf ("%s: camel_local_summary_check failed.\n", __FUNCTION__);
 		return -1;
+	}
 
 	camel_operation_start(NULL, _("Storing folder"));
 
@@ -810,6 +823,8 @@ maildir_summary_sync(CamelLocalSummary *
 
 	camel_operation_end(NULL);
 
+	printf ("%s: %s: changes:%d\n", __FUNCTION__, cls->folder_path, changes ? (camel_folder_change_info_changed (changes) ? 1 : 0) : -1);
+
 	return ((CamelLocalSummaryClass *)parent_class)->sync(cls, expunge, changes, ex);
 }
 
Index: camel/providers/local/camel-maildir-folder.c
===================================================================
--- camel/providers/local/camel-maildir-folder.c	(revision 10220)
+++ camel/providers/local/camel-maildir-folder.c	(working copy)
@@ -161,7 +161,6 @@ static CamelLocalSummary *maildir_create
 static void
 maildir_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
 {
-	CamelMaildirFolder *maildir_folder = (CamelMaildirFolder *)folder;
 	CamelLocalFolder *lf = (CamelLocalFolder *)folder;
 	CamelStream *output_stream;
 	CamelMessageInfo *mi;
@@ -170,10 +169,13 @@ maildir_append_message (CamelFolder *fol
 	
 	d(printf("Appending message\n"));
 
+	if (camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1)
+		goto check_changed;
+
 	/* add it to the summary/assign the uid, etc */
 	mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
 	if (camel_exception_is_set (ex))
-		return;
+		goto check_changed;
 	
 	mdi = (CamelMaildirMessageInfo *)mi;
 
@@ -197,17 +199,13 @@ maildir_append_message (CamelFolder *fol
 	g_free (dest);
 	g_free (name);
 
-	camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed",
-				    ((CamelLocalFolder *)maildir_folder)->changes);
-	camel_folder_change_info_clear (((CamelLocalFolder *)maildir_folder)->changes);
-
 	if (appended_uid)
 		*appended_uid = g_strdup(camel_message_info_uid(mi));
 
 	if (output_stream)
 		camel_object_unref (output_stream);
 
-	return;
+	goto check_changed;
 
  fail_write:
 
@@ -230,6 +228,17 @@ maildir_append_message (CamelFolder *fol
 	
 	g_free (name);
 	g_free (dest);
+
+ check_changed:
+	if (lf && camel_folder_change_info_changed (lf->changes)) {
+		printf ("%s: notify changed in %s\n", __FUNCTION__, folder->full_name);
+		camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed", lf->changes);
+		camel_folder_change_info_clear (lf->changes);
+	} else {
+		if (lf) 
+			camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex);
+		printf ("%s: no change in %s (second try: %d)\n", __FUNCTION__, folder->full_name, lf ? (camel_folder_change_info_changed (lf->changes) ? 1 : 0) : -1);
+	}
 }
 
 static CamelMimeMessage *
@@ -239,17 +248,21 @@ maildir_get_message(CamelFolder * folder
 	CamelStream *message_stream = NULL;
 	CamelMimeMessage *message = NULL;
 	CamelMessageInfo *info;
-	char *name;
+	char *name = NULL;
 	CamelMaildirMessageInfo *mdi;
 
 	d(printf("getting message: %s\n", uid));
 
+	if (camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) {
+		return NULL;
+	}
+
 	/* get the message summary info */
 	if ((info = camel_folder_summary_uid(folder->summary, uid)) == NULL) {
 		camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
 				     _("Cannot get message: %s from folder %s\n  %s"),
 				     uid, lf->folder_path, _("No such message"));
-		return NULL;
+		goto fail;
 	}
 
 	mdi = (CamelMaildirMessageInfo *)info;
@@ -263,8 +276,7 @@ maildir_get_message(CamelFolder * folder
 		camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
 				     _("Cannot get message: %s from folder %s\n  %s"),
 				     uid, lf->folder_path, g_strerror(errno));
-		g_free(name);
-		return NULL;
+		goto fail;
 	}
 
 	message = camel_mime_message_new();
@@ -272,14 +284,21 @@ maildir_get_message(CamelFolder * folder
 		camel_exception_setv(ex, (errno==EINTR)?CAMEL_EXCEPTION_USER_CANCEL:CAMEL_EXCEPTION_SYSTEM,
 				     _("Cannot get message: %s from folder %s\n  %s"),
 				     uid, lf->folder_path, _("Invalid message contents"));
-		g_free(name);
-		camel_object_unref((CamelObject *)message_stream);
 		camel_object_unref((CamelObject *)message);
-		return NULL;
-
+		message = NULL;
 	}
 	camel_object_unref((CamelObject *)message_stream);
-	g_free(name);
+
+ fail:
+	g_free (name);
+
+	if (lf && camel_folder_change_info_changed (lf->changes)) {
+		printf ("%s: notify changed %s\n", __FUNCTION__, folder->full_name);
+		camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed", lf->changes);
+		camel_folder_change_info_clear (lf->changes);
+	} else {
+		printf ("%s: no change in %s\n", __FUNCTION__, folder->full_name);
+	}
 
 	return message;
 }
Index: camel/providers/local/camel-mh-store.c
===================================================================
--- camel/providers/local/camel-mh-store.c	(revision 10220)
+++ camel/providers/local/camel-mh-store.c	(working copy)
@@ -336,7 +336,7 @@ fill_fi(CamelStore *store, CamelFolderIn
 		path = g_strdup_printf("%s/%s.ev-summary", root, fi->full_name);
 		folderpath = g_strdup_printf("%s/%s", root, fi->full_name);
 		s = (CamelFolderSummary *)camel_mh_summary_new(NULL, path, folderpath, NULL);
-		if (camel_folder_summary_header_load(s) != -1) {
+		if (camel_folder_summary_header_load_from_db (s, store, fi->full_name, NULL) != -1) {
 			fi->unread = s->unread_count;
 			fi->total = s->saved_count;
 		}
Index: camel/providers/local/camel-local-folder.c
===================================================================
--- camel/providers/local/camel-local-folder.c	(revision 10220)
+++ camel/providers/local/camel-local-folder.c	(working copy)
@@ -488,15 +488,17 @@ local_refresh_info(CamelFolder *folder, 
 
 	CAMEL_FOLDER_REC_LOCK(folder, lock);
 
-	if (camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1)
+	if (camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) {
+		CAMEL_FOLDER_REC_UNLOCK(folder, lock);
 		return;
+	}
 
+	CAMEL_FOLDER_REC_UNLOCK(folder, lock);
 	if (camel_folder_change_info_changed(lf->changes)) {
+		printf ("%s: on %s\n", __FUNCTION__, folder->full_name);
 		camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
 		camel_folder_change_info_clear(lf->changes);
 	}
-	CAMEL_FOLDER_REC_UNLOCK(folder, lock);
-	
 }
 
 static void
@@ -516,6 +518,7 @@ local_sync(CamelFolder *folder, gboolean
 	camel_local_folder_unlock(lf);
 
 	if (camel_folder_change_info_changed(lf->changes)) {
+		printf ("%s: on %s\n", __FUNCTION__, folder->full_name);
 		camel_object_trigger_event(CAMEL_OBJECT(folder), "folder_changed", lf->changes);
 		camel_folder_change_info_clear(lf->changes);
 	}
Index: camel/providers/local/camel-maildir-store.c
===================================================================
--- camel/providers/local/camel-maildir-store.c	(revision 10220)
+++ camel/providers/local/camel-maildir-store.c	(working copy)
@@ -322,7 +322,7 @@ fill_fi(CamelStore *store, CamelFolderIn
 		path = g_strdup_printf("%s/%s.ev-summary", root, fi->full_name);
 		folderpath = g_strdup_printf("%s/%s", root, fi->full_name);
 		s = (CamelFolderSummary *)camel_maildir_summary_new(NULL, path, folderpath, NULL);
-		if (camel_folder_summary_header_load(s) != -1) {
+		if (camel_folder_summary_header_load_from_db (s, store, fi->full_name, NULL) != -1) {
 			fi->unread = s->unread_count;
 			fi->total = s->saved_count;
 		}
Index: camel/providers/local/camel-mh-folder.c
===================================================================
--- camel/providers/local/camel-mh-folder.c	(revision 10220)
+++ camel/providers/local/camel-mh-folder.c	(working copy)
@@ -124,7 +124,6 @@ static CamelLocalSummary *mh_create_summ
 static void
 mh_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex)
 {
-	CamelMhFolder *mh_folder = (CamelMhFolder *)folder;
 	CamelLocalFolder *lf = (CamelLocalFolder *)folder;
 	CamelStream *output_stream;
 	CamelMessageInfo *mi;
@@ -134,10 +133,13 @@ mh_append_message (CamelFolder *folder, 
 
 	d(printf("Appending message\n"));
 
+	if (camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1)
+		goto check_changed;
+
 	/* add it to the summary/assign the uid, etc */
 	mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
 	if (camel_exception_is_set (ex))
-		return;
+		goto check_changed;
 	
 	d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
 	
@@ -156,14 +158,10 @@ mh_append_message (CamelFolder *folder, 
 
 	g_free(name);
 	
-	camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed",
-				    ((CamelLocalFolder *)mh_folder)->changes);
-	camel_folder_change_info_clear (((CamelLocalFolder *)mh_folder)->changes);
-	
 	if (appended_uid)
 		*appended_uid = g_strdup(camel_message_info_uid(mi));
 
-	return;
+	goto check_changed;
 	
  fail_write:
 	
@@ -185,6 +183,12 @@ mh_append_message (CamelFolder *folder, 
 	}
 	
 	g_free (name);
+
+ check_changed:
+	if (lf && camel_folder_change_info_changed (lf->changes)) {
+		camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed", lf->changes);
+		camel_folder_change_info_clear (lf->changes);
+	}
 }
 
 static CamelMimeMessage *mh_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex)
@@ -193,16 +197,20 @@ static CamelMimeMessage *mh_get_message(
 	CamelStream *message_stream = NULL;
 	CamelMimeMessage *message = NULL;
 	CamelMessageInfo *info;
-	char *name;
+	char *name = NULL;
 
 	d(printf("getting message: %s\n", uid));
 
+	if (camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) {
+		return NULL;
+	}
+
 	/* get the message summary info */
 	if ((info = camel_folder_summary_uid(folder->summary, uid)) == NULL) {
 		camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
 				     _("Cannot get message: %s from folder %s\n  %s"), uid, lf->folder_path,
 				     _("No such message"));
-		return NULL;
+		goto fail;
 	}
 
 	/* we only need it to check the message exists */
@@ -213,8 +221,7 @@ static CamelMimeMessage *mh_get_message(
 		camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
 				      _("Cannot get message: %s from folder %s\n  %s"), name, lf->folder_path,
 				      g_strerror (errno));
-		g_free(name);
-		return NULL;
+		goto fail;
 	}
 
 	message = camel_mime_message_new();
@@ -222,14 +229,19 @@ static CamelMimeMessage *mh_get_message(
 		camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
 				      _("Cannot get message: %s from folder %s\n  %s"), name, lf->folder_path,
 				      _("Message construction failed."));
-		g_free(name);
-		camel_object_unref((CamelObject *)message_stream);
 		camel_object_unref((CamelObject *)message);
-		return NULL;
-
+		message = NULL;
 	}
+
 	camel_object_unref((CamelObject *)message_stream);
-	g_free(name);
+
+ fail:
+	g_free (name);
+
+	if (lf && camel_folder_change_info_changed (lf->changes)) {
+		camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed", lf->changes);
+		camel_folder_change_info_clear (lf->changes);
+	}
 
 	return message;
 }
Index: camel/camel-folder.c
===================================================================
--- camel/camel-folder.c	(revision 10220)
+++ camel/camel-folder.c	(working copy)
@@ -1765,6 +1765,7 @@ thaw (CamelFolder * folder)
 	CAMEL_FOLDER_UNLOCK(folder, change_lock);
 
 	if (info) {
+		printf ("%s: on %s\n", __FUNCTION__, folder->full_name);
 		camel_object_trigger_event (folder, "folder_changed", info);
 		camel_folder_change_info_free(info);
 	}
Index: camel/camel-vee-folder.c
===================================================================
--- camel/camel-vee-folder.c	(revision 10220)
+++ camel/camel-vee-folder.c	(working copy)
@@ -177,7 +177,7 @@ void
 camel_vee_folder_add_folder(CamelVeeFolder *vf, CamelFolder *sub)
 {
 	struct _CamelVeeFolderPrivate *p = _PRIVATE(vf);
-	int i, cache;
+	int i;
 	CamelVeeFolder *folder_unmatched = vf->parent_vee_store ? vf->parent_vee_store->folder_unmatched : NULL;
 	
 	if (vf == (CamelVeeFolder *)sub) {
@@ -618,7 +618,7 @@ vee_sync(CamelFolder *folder, gboolean e
 		GSList *del = NULL;
 
 		for (i=0; i < count; i++) {
-			CamelVeeMessageInfo *mi = camel_folder_summary_index (folder->summary, i);
+			CamelVeeMessageInfo *mi = (CamelVeeMessageInfo *)camel_folder_summary_index (folder->summary, i);
 			if (mi->old_flags & CAMEL_MESSAGE_DELETED) {
 				del = g_slist_prepend (del, (gpointer) camel_pstring_strdup(((CamelMessageInfo *)mi)->uid));
 				camel_folder_summary_remove_index_fast (folder->summary, i);
Index: camel/camel-vee-summary.c
===================================================================
--- camel/camel-vee-summary.c	(revision 10220)
+++ camel/camel-vee-summary.c	(working copy)
@@ -178,7 +178,11 @@ camel_vee_summary_load_check_unread_vfol
 	static char *exp = NULL;
 	char *meta;
 	gboolean hacked_unread_folder = FALSE;
-	CamelVeeFolder *vf = (CamelVeeFolder *) ((CamelFolderSummary *)vs)->folder;
+	CamelVeeFolder *vf;
+
+	g_return_if_fail (vs != NULL);
+
+	vf = (CamelVeeFolder *) ((CamelFolderSummary *)vs)->folder;
 
 	/* HACK: Ugliest of all hacks. Its virtually not possible now
 	 * to maintain counts and the non matching uids of unread vfolder here.
@@ -216,7 +220,7 @@ vee_info_set_flags(CamelMessageInfo *mi,
 		printf("Expression for vfolder '%s' is '%s'\n", mi->summary->folder->full_name, g_strescape(vf->expression, ""));
 
 	if (vf->priv->unread_vfolder == -1)
-		camel_vee_summary_load_check_unread_vfolder (mi->summary);
+		camel_vee_summary_load_check_unread_vfolder (CAMEL_VEE_SUMMARY (mi->summary));
 
 	if (vf->priv->unread_vfolder == 1)
 		hacked_unread_folder = TRUE;
Index: camel/camel-folder-summary.c
===================================================================
--- camel/camel-folder-summary.c	(revision 10220)
+++ camel/camel-folder-summary.c	(working copy)
@@ -4660,6 +4660,7 @@ info_set_flags(CamelMessageInfo *info, g
 	if (mi->summary && mi->summary->folder && mi->uid) {
 		CamelFolderChangeInfo *changes = camel_folder_change_info_new();
 
+		printf ("%s: on %s\n", __FUNCTION__, mi->summary->folder->full_name);
 		camel_folder_change_info_change_uid(changes, camel_message_info_uid(info));
 		camel_object_trigger_event(mi->summary->folder, "folder_changed", changes);
 		camel_folder_change_info_free(changes);
@@ -4710,6 +4711,7 @@ info_set_user_flag(CamelMessageInfo *inf
 		mi->flags |= CAMEL_MESSAGE_FOLDER_FLAGGED;
 		mi->dirty = TRUE;
 		camel_folder_summary_touch(mi->summary);
+		printf ("%s: on %s\n", __FUNCTION__, mi->summary->folder->full_name);
 		camel_folder_change_info_change_uid(changes, camel_message_info_uid(info));
 		camel_object_trigger_event(mi->summary->folder, "folder_changed", changes);
 		camel_folder_change_info_free(changes);
@@ -4752,6 +4754,7 @@ info_set_user_tag(CamelMessageInfo *info
 		mi->flags |= CAMEL_MESSAGE_FOLDER_FLAGGED;
 		mi->dirty = TRUE;		
 		camel_folder_summary_touch(mi->summary);
+		printf ("%s: on %s\n", __FUNCTION__, mi->summary->folder->full_name);
 		camel_folder_change_info_change_uid(changes, camel_message_info_uid(info));
 		camel_object_trigger_event(mi->summary->folder, "folder_changed", changes);
 		camel_folder_change_info_free(changes);
@@ -4831,7 +4834,6 @@ camel_message_info_dump (CamelMessageInf
 static void
 camel_folder_summary_class_init (CamelFolderSummaryClass *klass)
 {
-	CamelObjectClass *camel_object_class = CAMEL_OBJECT_CLASS (klass);
 	camel_folder_summary_parent = camel_type_get_global_classfuncs (camel_object_get_type ());
 
 	klass->summary_header_load = summary_header_load;
Index: camel/camel-folder-summary.h
===================================================================
--- camel/camel-folder-summary.h	(revision 10220)
+++ camel/camel-folder-summary.h	(working copy)
@@ -394,7 +394,6 @@ void camel_folder_summary_remove_range(C
 void camel_folder_summary_clear(CamelFolderSummary *summary);
 void camel_folder_summary_clear_db (CamelFolderSummary *s);
 
-int camel_folder_summary_reload_from_db (CamelFolderSummary *s, CamelException *ex);
 int camel_folder_summary_cache_size (CamelFolderSummary *s);
 
 /* lookup functions */
