--- camel-folder.c.111	2007-05-19 15:03:27.000000000 +0100
+++ camel-folder.c	2007-05-19 15:14:01.000000000 +0100
@@ -1751,12 +1751,21 @@ static CamelSessionThreadOps filter_ops 
 	filter_free,
 };
 
+struct _CamelFolderChangeInfoPrivate {
+	GHashTable *uid_stored;	/* what we have stored, which array they're in */
+	GHashTable *uid_source;	/* used to create unique lists */
+        GPtrArray  *uid_filter; /* uids to be filtered */
+	struct _EMemPool *uid_pool;	/* pool used to store copies of uid strings */
+};
+
+
 /* Event hooks that block emission when frozen */
 static gboolean
 folder_changed (CamelObject *obj, gpointer event_data)
 {
 	CamelFolder *folder = (CamelFolder *)obj;
 	CamelFolderChangeInfo *changed = event_data;
+        struct _CamelFolderChangeInfoPrivate *p = changed->priv;
 	CamelSession *session = ((CamelService *)folder->parent_store)->session;
 	CamelFilterDriver *driver = NULL;
 	GPtrArray *junk = NULL;
@@ -1765,9 +1774,10 @@ folder_changed (CamelObject *obj, gpoint
 	int i;
 
 	d(printf ("folder_changed(%p:'%s', %p), frozen=%d\n", obj, folder->full_name, event_data, folder->priv->frozen));
-	d(printf(" added %d removed %d changed %d recent %d\n",
+	d(printf(" added %d removed %d changed %d recent %d filter %d\n",
 		 changed->uid_added->len, changed->uid_removed->len,
-		 changed->uid_changed->len, changed->uid_recent->len));
+	         changed->uid_changed->len, changed->uid_recent->len,
+	         p->uid_filter->len));
 
 	if (changed == NULL) {
 		w(g_warning ("Class %s is passing NULL to folder_changed event",
@@ -1806,15 +1816,17 @@ folder_changed (CamelObject *obj, gpoint
 	}
 
 	if ((folder->folder_flags & (CAMEL_FOLDER_FILTER_RECENT|CAMEL_FOLDER_FILTER_JUNK))
-	    && changed->uid_recent->len > 0)
+	    && p->uid_filter->len > 0)
 		driver = camel_session_get_filter_driver(session,
 							 (folder->folder_flags & CAMEL_FOLDER_FILTER_RECENT) 
 							 ? "incoming":"junktest", NULL);
 		
 	if (driver) {
 		recents = g_ptr_array_new();
-		for (i=0;i<changed->uid_recent->len;i++)
-			g_ptr_array_add(recents, g_strdup(changed->uid_recent->pdata[i]));
+		for (i=0;i<p->uid_filter->len;i++) {
+		    g_ptr_array_add(recents, g_strdup(p->uid_filter->pdata[i]));
+		}
+		g_ptr_array_set_size(p->uid_filter, 0);
 	}
 
 	if (driver || junk || notjunk) {
@@ -1830,9 +1842,15 @@ folder_changed (CamelObject *obj, gpoint
 		msg->folder = folder;
 		camel_object_ref(folder);
 		camel_folder_freeze(folder);
+	       /* Copy changes back to changed_frozen list to retain
+	        * them while we are filtering */
+	        CAMEL_FOLDER_LOCK(folder, change_lock);
+		camel_folder_change_info_cat(folder->priv->changed_frozen, changed);
+	        CAMEL_FOLDER_UNLOCK(folder, change_lock);
 		msg->driver = driver;
 		camel_exception_init(&msg->ex);
 		camel_session_thread_queue(session, &msg->msg, 0);
+	        return FALSE;
 	}
 
 	return TRUE;
@@ -1891,12 +1909,6 @@ camel_folder_free_deep (CamelFolder *fol
 	g_ptr_array_free (array, TRUE);
 }
 
-struct _CamelFolderChangeInfoPrivate {
-	GHashTable *uid_stored;	/* what we have stored, which array they're in */
-	GHashTable *uid_source;	/* used to create unique lists */
-	struct _EMemPool *uid_pool;	/* pool used to store copies of uid strings */
-};
-
 
 /**
  * camel_folder_change_info_new:
@@ -1921,6 +1933,7 @@ camel_folder_change_info_new(void)
 	info->priv = g_malloc0(sizeof(*info->priv));
 	info->priv->uid_stored = g_hash_table_new(g_str_hash, g_str_equal);
 	info->priv->uid_source = NULL;
+	info->priv->uid_filter = g_ptr_array_new();
 	info->priv->uid_pool = e_mempool_new(512, 256, E_MEMPOOL_ALIGN_BYTE);
 
 	return info;
@@ -2078,6 +2091,38 @@ camel_folder_change_info_build_diff(Came
 }
 
 static void
+change_info_recent_uid(CamelFolderChangeInfo *info, const char *uid)
+{
+	struct _CamelFolderChangeInfoPrivate *p;
+	GPtrArray *olduids;
+	char *olduid;
+	
+	p = info->priv;
+
+	/* always add to recent, but dont let anyone else know */	
+	if (!g_hash_table_lookup_extended(p->uid_stored, uid, (void **)&olduid, (void **)&olduids)) {
+		olduid = e_mempool_strdup(p->uid_pool, uid);
+	}
+	g_ptr_array_add(info->uid_recent, olduid);
+}
+
+static void
+change_info_filter_uid(CamelFolderChangeInfo *info, const char *uid)
+{
+	struct _CamelFolderChangeInfoPrivate *p;
+	GPtrArray *olduids;
+	char *olduid;
+	
+	p = info->priv;
+
+	/* always add to filter, but dont let anyone else know */	
+	if (!g_hash_table_lookup_extended(p->uid_stored, uid, (void **)&olduid, (void **)&olduids)) {
+		olduid = e_mempool_strdup(p->uid_pool, uid);
+	}
+	g_ptr_array_add(p->uid_filter, olduid);
+}
+
+static void
 change_info_cat(CamelFolderChangeInfo *info, GPtrArray *source, void (*add)(CamelFolderChangeInfo *info, const char *uid))
 {
 	int i;
@@ -2104,7 +2149,8 @@ camel_folder_change_info_cat(CamelFolder
 	change_info_cat(info, source->uid_added, camel_folder_change_info_add_uid);
 	change_info_cat(info, source->uid_removed, camel_folder_change_info_remove_uid);
 	change_info_cat(info, source->uid_changed, camel_folder_change_info_change_uid);
-	change_info_cat(info, source->uid_recent, camel_folder_change_info_recent_uid);
+	change_info_cat(info, source->uid_recent, change_info_recent_uid);
+	change_info_cat(info, source->priv->uid_filter, change_info_filter_uid);
 }
 
 /**
@@ -2211,23 +2257,16 @@ camel_folder_change_info_change_uid(Came
  * @uid: a uid
  *
  * Add a recent uid to the changedinfo.
+ * This will also add the uid to the uid_filter array for potential
+ * filtering
  **/
 void
 camel_folder_change_info_recent_uid(CamelFolderChangeInfo *info, const char *uid)
 {
-	struct _CamelFolderChangeInfoPrivate *p;
-	GPtrArray *olduids;
-	char *olduid;
-	
 	g_assert(info != NULL);
 	
-	p = info->priv;
-
-	/* always add to recent, but dont let anyone else know */	
-	if (!g_hash_table_lookup_extended(p->uid_stored, uid, (void **)&olduid, (void **)&olduids)) {
-		olduid = e_mempool_strdup(p->uid_pool, uid);
-	}
-	g_ptr_array_add(info->uid_recent, olduid);
+	change_info_recent_uid(info, uid);
+	change_info_filter_uid(info, uid);
 }
 
 /**
@@ -2274,6 +2313,7 @@ camel_folder_change_info_clear(CamelFold
 	}
 	g_hash_table_destroy(p->uid_stored);
 	p->uid_stored = g_hash_table_new(g_str_hash, g_str_equal);
+	g_ptr_array_set_size(p->uid_filter, 0);
 	e_mempool_flush(p->uid_pool, TRUE);
 }
 
@@ -2297,6 +2337,7 @@ camel_folder_change_info_free(CamelFolde
 		g_hash_table_destroy(p->uid_source);
 
 	g_hash_table_destroy(p->uid_stored);
+	g_ptr_array_free(p->uid_filter, TRUE);
 	e_mempool_destroy(p->uid_pool);
 	g_free(p);
 
