+static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
+{
+ MsgPermFlags newflags = 0;
+
+ /* create new flags */
+ if (source != NULL) {
+ /* copy original flags */
+ newflags = source->flags.perm_flags;
+ } else {
+ newflags = dest->flags.perm_flags;
+ }
+
+ /* remove new, unread and deleted in special folders */
+ if (dest->folder->stype == F_OUTBOX ||
+ dest->folder->stype == F_QUEUE ||
+ dest->folder->stype == F_DRAFT ||
+ dest->folder->stype == F_TRASH)
+ newflags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
+
+ /* set ignore flag of ignored parent exists */
+ if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
+ newflags |= MSG_IGNORE_THREAD;
+ /* unset flags that are set but should not */
+ procmsg_msginfo_unset_flags(dest, dest->flags.perm_flags & ~newflags, ~0);
+ /* set new flags */
+ procmsg_msginfo_set_flags(dest, ~dest->flags.perm_flags & newflags, 0);
+
+ folder_item_update(dest->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
+}
+
+static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
+{
+ /* update folder stats */
+ if (MSG_IS_NEW(newmsginfo->flags))
+ item->new++;
+ if (MSG_IS_UNREAD(newmsginfo->flags))
+ item->unread++;
+ if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
+ item->unreadmarked++;
+ item->total++;
+
+ copy_msginfo_flags(flagsource, newmsginfo);
+
+ msgcache_add_msg(item->cache, newmsginfo);
+}
+
+static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
+{
+ if (!item->cache)
+ folder_item_read_cache(item);
+
+ if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+ msginfo->folder->new--;
+ if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+ msginfo->folder->unread--;
+ if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
+ msginfo->folder->unreadmarked--;
+ msginfo->folder->total--;
+
+ msgcache_remove_msg(item->cache, msginfo->msgnum);
+ folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
+}
+