fix news folder scan (didn't check this before last commit)
[claws.git] / src / folder.c
index baaa2adf38767e6433795206baa3cfb5dc776ecb..c417795284ef65d17888c2bc4de4c9481fb84d50 100644 (file)
@@ -279,7 +279,7 @@ void folder_item_destroy(FolderItem *item)
                }
        }
 
-       if(item->cache)
+       if (item->cache)
                folder_item_free_cache(item);
        g_free(item->name);
        g_free(item->path);
@@ -324,7 +324,8 @@ void folder_tree_destroy(Folder *folder)
        prefs_filtering_clear();
 
        g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_tree_destroy_func, NULL);
-       g_node_destroy(folder->node);
+       if (folder->node)
+               g_node_destroy(folder->node);
 
        folder->inbox = NULL;
        folder->outbox = NULL;
@@ -434,7 +435,7 @@ void folder_scan_tree(Folder *folder)
 {
        GHashTable *pptable;
        
-       if(!folder->scan_tree)
+       if (!folder->scan_tree)
                return;
        
        pptable = folder_persist_prefs_new(folder);
@@ -873,22 +874,31 @@ void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
        }
 }
 
-typedef enum {
-    IN_CACHE  = 1 << 0,
-    IN_FOLDER = 1 << 1,
-} FolderScanInfo;
+static gint folder_sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
+{
+       MsgInfo *msginfo_a = (MsgInfo *) a;
+       MsgInfo *msginfo_b = (MsgInfo *) b;
+
+       return (msginfo_a->msgnum - msginfo_b->msgnum);
+}
+
+static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
+{
+       guint gint_a = GPOINTER_TO_INT(a);
+       guint gint_b = GPOINTER_TO_INT(a);
+       
+       return (gint_a - gint_b);
+}
 
 gint folder_item_scan(FolderItem *item)
 {
        Folder *folder;
-       GSList *folder_list, *cache_list, *elem, *new_list = NULL;
-       gint i;
-       guint min = 0xffffffff, max = 0, cache_max = 0;
-       FolderScanInfo *folderscaninfo;
+       GSList *folder_list, *cache_list, *folder_list_cur, *cache_list_cur, *new_list = NULL;
        guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
-       
+       guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
+
        g_return_val_if_fail(item != NULL, -1);
-       if(item->path == NULL) return -1;
+       if (item->path == NULL) return -1;
 
        folder = item->folder;
 
@@ -898,77 +908,65 @@ gint folder_item_scan(FolderItem *item)
        debug_print("Scanning folder %s for cache changes.\n", item->path);
 
        /* Get list of messages for folder and cache */
-       if(!folder->check_msgnum_validity || 
+       if (!folder->check_msgnum_validity || 
           folder->check_msgnum_validity(folder, item)) {
-               if(!item->cache)
+               if (!item->cache)
                        folder_item_read_cache(item);
                cache_list = msgcache_get_msg_list(item->cache);
        } else {
-               if(item->cache)
+               if (item->cache)
                        msgcache_destroy(item->cache);
                item->cache = msgcache_new();
                cache_list = NULL;
        }
        folder_list = folder->get_num_list(item->folder, item);
 
-       /* Get min und max number in folder */
-       for(elem = cache_list; elem != NULL; elem = elem->next) {
-               MsgInfo *msginfo = (MsgInfo *)elem->data;
-
-               min = MIN(msginfo->msgnum, min);
-               max = MAX(msginfo->msgnum, max);
-       }
-       cache_max = max;
-       for(elem = folder_list; elem != NULL; elem = elem->next) {
-               guint num = GPOINTER_TO_INT(elem->data);
-
-               min = MIN(num, min);
-               max = MAX(num, max);
-       }
-
-       debug_print("Folder message number range from %d to %d\n", min, max);
-
-       if(max == 0) {
-               for(elem = cache_list; elem != NULL; elem = elem->next) {
-                       MsgInfo *msginfo = (MsgInfo *)elem->data;
-
-                       procmsg_msginfo_free(msginfo);
-               }
-               g_slist_free(folder_list);
-               g_slist_free(cache_list);
-
-               return 0;
-       }
+       /* Sort both lists */
+       cache_list = g_slist_sort(cache_list, folder_sort_cache_list_by_msgnum);
+       folder_list = g_slist_sort(folder_list, folder_sort_folder_list);
 
-       folderscaninfo = g_new0(FolderScanInfo, max - min + 1);
+       cache_list_cur = cache_list;
+       folder_list_cur = folder_list;
 
-       for(elem = folder_list; elem != NULL; elem = elem->next) {
-               guint num = GPOINTER_TO_INT(elem->data);
-
-               folderscaninfo[num - min] |= IN_FOLDER;
-       }
-       for(elem = cache_list; elem != NULL; elem = elem->next) {
-               MsgInfo *msginfo = (MsgInfo *)elem->data;
-
-               folderscaninfo[msginfo->msgnum - min] |= IN_CACHE;
-               procmsg_msginfo_free(msginfo);
-       }
-
-       for(i = max - min; i >= 0; i--) {
-               guint num;
+       if (cache_list_cur != NULL) {
+               GSList *cache_list_last;
+       
+               cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
+               cache_list_last = g_slist_last(cache_list);
+               cache_max_num = ((MsgInfo *)cache_list_last->data)->msgnum;
+       } else
+               cache_cur_num = G_MAXINT;
 
-               num = i + min;
-               /* Add message to cache if in folder and not in cache */
-               if( (folderscaninfo[i] & IN_FOLDER) && 
-                  !(folderscaninfo[i] & IN_CACHE)) {
+       if (folder_list_cur != NULL) {
+               GSList *folder_list_last;
+       
+               folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
+               folder_list_last = g_slist_last(folder_list);
+               folder_max_num = GPOINTER_TO_INT(folder_list_last->data);
+       } else
+               folder_cur_num = G_MAXINT;
+
+       while ((cache_cur_num != G_MAXINT) || (folder_cur_num != G_MAXINT)) {
+               printf("%d %d\n", cache_cur_num, folder_cur_num);
+               /*
+                *  Message only exists in the folder
+                *  Remember message for fetching
+                */
+               if (folder_cur_num < cache_cur_num) {
                        gboolean add = FALSE;
 
                        switch(folder->type) {
                                case F_NEWS:
-                                       if((num > cache_max) && 
-                                          ((prefs_common.max_articles == 0) ||
-                                           (max < prefs_common.max_articles) ||
-                                           (num > (max - prefs_common.max_articles)))) {
+                                       if (folder_cur_num < cache_max_num)
+                                               break;
+                                       
+                                       if (prefs_common.max_articles == 0) {
+                                               add = TRUE;
+                                       }
+
+                                       if (folder_max_num <= prefs_common.max_articles) {
+                                               add = TRUE;
+                                       } else if (folder_cur_num > (folder_max_num - prefs_common.max_articles)) {
                                                add = TRUE;
                                        }
                                        break;
@@ -977,60 +975,111 @@ gint folder_item_scan(FolderItem *item)
                                        break;
                        }
                        
-                       if(add) {
-                               new_list = g_slist_prepend(new_list, GINT_TO_POINTER(num));
-                               debug_print("Remembered message %d for fetching\n", num);
+                       if (add) {
+                               new_list = g_slist_prepend(new_list, GINT_TO_POINTER(folder_cur_num));
+                               debug_print("Remembered message %d for fetching\n", folder_cur_num);
                        }
+
+                       /* Move to next folder number */
+                       folder_list_cur = folder_list_cur->next;
+
+                       if (folder_list_cur != NULL)
+                               folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
+                       else
+                               folder_cur_num = G_MAXINT;
+
+                       continue;
                }
-               /* Remove message from cache if not in folder and in cache */
-               if(!(folderscaninfo[i] & IN_FOLDER) && 
-                   (folderscaninfo[i] & IN_CACHE)) {
-                       msgcache_remove_msg(item->cache, i + min);
-                       debug_print("Removed message %d from cache.\n", num);
+
+               /*
+                *  Message only exists in the cache
+                *  Remove the message from the cache
+                */
+               if (cache_cur_num < folder_cur_num) {
+                       msgcache_remove_msg(item->cache, cache_cur_num);
+                       debug_print("Removed message %d from cache.\n", cache_cur_num);
+
+                       /* Move to next cache number */
+                       cache_list_cur = cache_list_cur->next;
+
+                       if (cache_list_cur != NULL)
+                               cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
+                       else
+                               cache_cur_num = G_MAXINT;
+
+                       continue;
                }
-               /* Check if msginfo needs update if in cache and in folder */
-               if((folderscaninfo[i] & IN_FOLDER) && 
-                  (folderscaninfo[i] & IN_CACHE)) {
+
+               /*
+                *  Message number exists in folder and cache!
+                *  Check if the message has been modified
+                */
+               if (cache_cur_num == folder_cur_num) {
                        MsgInfo *msginfo;
 
-                       msginfo = msgcache_get_msg(item->cache, num);
-                       if(folder->is_msg_changed && folder->is_msg_changed(folder, item, msginfo)) {
+                       msginfo = msgcache_get_msg(item->cache, folder_cur_num);
+                       if (folder->is_msg_changed && folder->is_msg_changed(folder, item, msginfo)) {
                                MsgInfo *newmsginfo;
 
                                msgcache_remove_msg(item->cache, msginfo->msgnum);
 
-                               newmsginfo = folder->fetch_msginfo(folder, item, num);
-                               msgcache_add_msg(item->cache, newmsginfo);
-                               if(MSG_IS_NEW(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
-                                       newcnt++;
-                               if(MSG_IS_UNREAD(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
-                                       unreadcnt++;
-                               procmsg_msginfo_free(newmsginfo);
+                               if (NULL != (newmsginfo = folder->fetch_msginfo(folder, item, folder_cur_num))) {
+                                       msgcache_add_msg(item->cache, newmsginfo);
+                                       if (MSG_IS_NEW(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
+                                               newcnt++;
+                                       if (MSG_IS_UNREAD(newmsginfo->flags) && !MSG_IS_IGNORE_THREAD(newmsginfo->flags))
+                                               unreadcnt++;
+                                       procmsg_msginfo_free(newmsginfo);
+                               }                                       
 
-                               debug_print("Updated msginfo for message %d.\n", num);
+                               debug_print("Updated msginfo for message %d.\n", folder_cur_num);
                        } else {
-                               if(MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                        newcnt++;
-                               if(MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                        unreadcnt++;
                        }
                        totalcnt++;
                        procmsg_msginfo_free(msginfo);
+
+                       /* Move to next folder and cache number */
+                       cache_list_cur = cache_list_cur->next;
+                       folder_list_cur = folder_list_cur->next;
+
+                       if (cache_list_cur != NULL)
+                               cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
+                       else
+                               cache_cur_num = G_MAXINT;
+
+                       if (folder_list_cur != NULL)
+                               folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
+                       else
+                               folder_cur_num = G_MAXINT;
+
+                       continue;
                }
        }
 
-       if(folder->fetch_msginfos) {
+       for(cache_list_cur = cache_list; cache_list_cur != NULL; cache_list_cur = g_slist_next(cache_list_cur)) {
+               procmsg_msginfo_free((MsgInfo *) cache_list_cur->data);
+       }
+
+       g_slist_free(cache_list);
+       g_slist_free(folder_list);
+
+       if (folder->fetch_msginfos) {
+               GSList *elem;
                GSList *newmsg_list;
                MsgInfo *msginfo;
                
-               if(new_list) {
+               if (new_list) {
                        newmsg_list = folder->fetch_msginfos(folder, item, new_list);
-                       for(elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
+                       for (elem = newmsg_list; elem != NULL; elem = g_slist_next(elem)) {
                                msginfo = (MsgInfo *) elem->data;
                                msgcache_add_msg(item->cache, msginfo);
-                               if(MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                        newcnt++;
-                               if(MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                        unreadcnt++;
                                totalcnt++;
                                procmsg_msginfo_free(msginfo);
@@ -1039,17 +1088,19 @@ gint folder_item_scan(FolderItem *item)
                        folderview_update_item(item, FALSE);
                }
        } else if (folder->fetch_msginfo) {
-               for(elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
+               GSList *elem;
+       
+               for (elem = new_list; elem != NULL; elem = g_slist_next(elem)) {
                        MsgInfo *msginfo;
                        guint num;
 
                        num = GPOINTER_TO_INT(elem->data);
                        msginfo = folder->fetch_msginfo(folder, item, num);
-                       if(msginfo != NULL) {
+                       if (msginfo != NULL) {
                                msgcache_add_msg(item->cache, msginfo);
-                               if(MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                    newcnt++;
-                               if(MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
+                               if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
                                    unreadcnt++;
                                totalcnt++;
                                procmsg_msginfo_free(msginfo);
@@ -1063,10 +1114,7 @@ gint folder_item_scan(FolderItem *item)
        item->unread = unreadcnt;
        item->total = totalcnt;
        
-       g_slist_free(folder_list);
-       g_slist_free(cache_list);
        g_slist_free(new_list);
-       g_free(folderscaninfo);
 
        folderview_update_item(item, FALSE);
 
@@ -1088,7 +1136,7 @@ void folder_count_total_cache_memusage(FolderItem *item, gpointer data)
 {
        gint *memusage = (gint *)data;
 
-       if(item->cache == NULL)
+       if (item->cache == NULL)
                return;
        
        *memusage += msgcache_get_memory_usage(item->cache);
@@ -1107,16 +1155,16 @@ void folder_find_expired_caches(FolderItem *item, gpointer data)
        GSList **folder_item_list = (GSList **)data;
        gint difftime, expiretime;
        
-       if(item->cache == NULL)
+       if (item->cache == NULL)
                return;
 
-       if(item->opened > 0)
+       if (item->opened > 0)
                return;
 
        difftime = (gint) (time(NULL) - msgcache_get_last_access_time(item->cache));
        expiretime = prefs_common.cache_min_keep_time * 60;
        debug_print("Cache unused time: %d (Expire time: %d)\n", difftime, expiretime);
-       if(difftime > expiretime) {
+       if (difftime > expiretime) {
                *folder_item_list = g_slist_insert_sorted(*folder_item_list, item, folder_cache_time_compare_func);
        }
 }
@@ -1125,10 +1173,10 @@ void folder_item_free_cache(FolderItem *item)
 {
        g_return_if_fail(item != NULL);
        
-       if(item->cache == NULL)
+       if (item->cache == NULL)
                return;
        
-       if(item->opened > 0)
+       if (item->opened > 0)
                return;
 
        folder_item_write_cache(item);
@@ -1143,7 +1191,7 @@ void folder_clean_cache_memory()
        folder_func_to_all_folders(folder_count_total_cache_memusage, &memusage);       
        debug_print("Total cache memory usage: %d\n", memusage);
        
-       if(memusage > (prefs_common.cache_max_mem_usage * 1024)) {
+       if (memusage > (prefs_common.cache_max_mem_usage * 1024)) {
                GSList *folder_item_list = NULL, *listitem;
                
                debug_print("Trying to free cache memory\n");
@@ -1171,7 +1219,7 @@ void folder_item_read_cache(FolderItem *item)
        cache_file = folder_item_get_cache_file(item);
        mark_file = folder_item_get_mark_file(item);
        item->cache = msgcache_read_cache(item, cache_file);
-       if(!item->cache) {
+       if (!item->cache) {
                item->cache = msgcache_new();
                folder_item_scan(item);
        }
@@ -1198,7 +1246,7 @@ void folder_item_write_cache(FolderItem *item)
 
        cache_file = folder_item_get_cache_file(item);
        mark_file = folder_item_get_mark_file(item);
-       if(msgcache_write(cache_file, mark_file, item->cache) < 0) {
+       if (msgcache_write(cache_file, mark_file, item->cache) < 0) {
                prefs = item->prefs;
                if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
                        /* for cache file */
@@ -1221,14 +1269,14 @@ MsgInfo *folder_item_fetch_msginfo(FolderItem *item, gint num)
        g_return_val_if_fail(item != NULL, NULL);
        
        folder = item->folder;
-       if(!item->cache)
+       if (!item->cache)
                folder_item_read_cache(item);
        
-       if((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
+       if ((msginfo = msgcache_get_msg(item->cache, num)) != NULL)
                return msginfo;
        
        g_return_val_if_fail(folder->fetch_msginfo, NULL);
-       if((msginfo = folder->fetch_msginfo(folder, item, num)) != NULL) {
+       if ((msginfo = folder->fetch_msginfo(folder, item, num)) != NULL) {
                msgcache_add_msg(item->cache, msginfo);
                return msginfo;
        }
@@ -1244,10 +1292,10 @@ MsgInfo *folder_item_fetch_msginfo_by_id(FolderItem *item, const gchar *msgid)
        g_return_val_if_fail(item != NULL, NULL);
        
        folder = item->folder;
-       if(!item->cache)
+       if (!item->cache)
                folder_item_read_cache(item);
        
-       if((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
+       if ((msginfo = msgcache_get_msg_by_id(item->cache, msgid)) != NULL)
                return msginfo;
 
        return NULL;
@@ -1257,7 +1305,7 @@ GSList *folder_item_get_msg_list(FolderItem *item)
 {
        g_return_val_if_fail(item != NULL, NULL);
        
-       if(item->cache == 0)
+       if (item->cache == 0)
                folder_item_read_cache(item);
 
        g_return_val_if_fail(item->cache != NULL, NULL);
@@ -1300,10 +1348,10 @@ gint folder_item_add_msg(FolderItem *dest, const gchar *file,
         if (num > 0) {
                msginfo = folder->fetch_msginfo(folder, dest, num);
 
-               if(msginfo != NULL) {
-                       if(MSG_IS_NEW(msginfo->flags))
+               if (msginfo != NULL) {
+                       if (MSG_IS_NEW(msginfo->flags))
                                dest->new++;
-                       if(MSG_IS_UNREAD(msginfo->flags))
+                       if (MSG_IS_UNREAD(msginfo->flags))
                                dest->unread++;
                        dest->total++;
 
@@ -1341,7 +1389,7 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
 {
        Folder *folder;
        gint num;
-       Folder * src_folder;
+       Folder *src_folder;
 
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msginfo != NULL, -1);
@@ -1360,36 +1408,35 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
        if (num != -1) {
                MsgInfo *newmsginfo;
 
-               newmsginfo = folder->fetch_msginfo(folder, dest, num);
-               newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
-               if (dest->stype == F_OUTBOX ||
-                   dest->stype == F_QUEUE  ||
-                   dest->stype == F_DRAFT  ||
-                   dest->stype == F_TRASH)
-                       MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
-                                            MSG_NEW|MSG_UNREAD|MSG_DELETED);
-               msgcache_add_msg(dest->cache, newmsginfo);
-
-               /* CLAWS */
-               if(src_folder->remove_msg) {
-                       src_folder->remove_msg(src_folder,
-                                              msginfo->folder,
-                                              msginfo->msgnum);
-               }
-                msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
+               if (NULL != (newmsginfo = folder->fetch_msginfo(folder, dest, num))) {
+                       newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
+                       
+                       if (dest->stype == F_OUTBOX || dest->stype == F_QUEUE  ||
+                           dest->stype == F_DRAFT  || dest->stype == F_TRASH)
+                               MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
+                                                    MSG_NEW|MSG_UNREAD|MSG_DELETED);
+                       msgcache_add_msg(dest->cache, newmsginfo);
+
+                       /* CLAWS */
+                       if (src_folder->remove_msg)
+                               src_folder->remove_msg(src_folder, msginfo->folder,
+                                                      msginfo->msgnum);
+                       
+                       msgcache_remove_msg(msginfo->folder->cache, msginfo->msgnum);
 
-               if (MSG_IS_NEW(msginfo->flags))
-                       msginfo->folder->new--;
-               if (MSG_IS_NEW(newmsginfo->flags))
-                       dest->new++;
-               if (MSG_IS_UNREAD(msginfo->flags))
-                       msginfo->folder->unread--;
-               if (MSG_IS_UNREAD(newmsginfo->flags))
-                       dest->unread++;
-               msginfo->folder->total--;
-               dest->total++;
+                       if (MSG_IS_NEW(msginfo->flags))
+                               msginfo->folder->new--;
+                       if (MSG_IS_NEW(newmsginfo->flags))
+                               dest->new++;
+                       if (MSG_IS_UNREAD(msginfo->flags))
+                               msginfo->folder->unread--;
+                       if (MSG_IS_UNREAD(newmsginfo->flags))
+                               dest->unread++;
+                       msginfo->folder->total--;
+                       dest->total++;
 
-               procmsg_msginfo_free(newmsginfo);
+                       procmsg_msginfo_free(newmsginfo);
+               }
        }
        
        if (folder->finished_copy)
@@ -1439,7 +1486,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
         * store new message numbers in newmsgnums
         */
        item = NULL;
-       for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
+       for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
                MsgInfo * msginfo = (MsgInfo *) l->data;
 
                if (!item && msginfo->folder != NULL)
@@ -1457,7 +1504,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
         * add them to the msgcache and update folder message counts
         */
        l2 = newmsgnums;
-       for(l = msglist; l != NULL; l = g_slist_next(l)) {
+       for (l = msglist; l != NULL; l = g_slist_next(l)) {
                MsgInfo *msginfo = (MsgInfo *) l->data;
 
                num = GPOINTER_TO_INT(l2->data);
@@ -1466,7 +1513,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                        MsgInfo *newmsginfo;
 
                        newmsginfo = folder->fetch_msginfo(folder, dest, num);
-                       if(newmsginfo) {
+                       if (newmsginfo) {
                                newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
                                if (dest->stype == F_OUTBOX ||
                                    dest->stype == F_QUEUE  ||
@@ -1494,16 +1541,16 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
         * message counts
         */
        l2 = newmsgnums;
-       for(l = msglist; l != NULL; l = g_slist_next(l)) {
+       for (l = msglist; l != NULL; l = g_slist_next(l)) {
                MsgInfo *msginfo = (MsgInfo *) l->data;
 
                num = GPOINTER_TO_INT(l2->data);
                
-               if(num != -1) {
+               if (num != -1) {
                        item->folder->remove_msg(item->folder,
                                                 msginfo->folder,
                                                 msginfo->msgnum);
-                       if(!item->cache)
+                       if (!item->cache)
                                folder_item_read_cache(item);
                        msgcache_remove_msg(item->cache, msginfo->msgnum);
 
@@ -1562,23 +1609,24 @@ gint folder_item_copy_msg(FolderItem *dest, MsgInfo *msginfo)
        if (num != -1) {
                MsgInfo *newmsginfo;
 
-               newmsginfo = folder->fetch_msginfo(folder, dest, num);
-               newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
-               if (dest->stype == F_OUTBOX ||
-                   dest->stype == F_QUEUE  ||
-                   dest->stype == F_DRAFT  ||
-                   dest->stype == F_TRASH)
-                       MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
-                                            MSG_NEW|MSG_UNREAD|MSG_DELETED);
-               msgcache_add_msg(dest->cache, newmsginfo);
-
-               if (MSG_IS_NEW(newmsginfo->flags))
-                       dest->new++;
-               if (MSG_IS_UNREAD(newmsginfo->flags))
-                       dest->unread++;
-               dest->total++;
-
-               procmsg_msginfo_free(newmsginfo);
+               if (NULL != (newmsginfo = folder->fetch_msginfo(folder, dest, num))) {
+                       newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
+                       if (dest->stype == F_OUTBOX ||
+                           dest->stype == F_QUEUE  ||
+                           dest->stype == F_DRAFT  ||
+                           dest->stype == F_TRASH)
+                               MSG_UNSET_PERM_FLAGS(newmsginfo->flags,
+                                                    MSG_NEW|MSG_UNREAD|MSG_DELETED);
+                       msgcache_add_msg(dest->cache, newmsginfo);
+
+                       if (MSG_IS_NEW(newmsginfo->flags))
+                               dest->new++;
+                       if (MSG_IS_UNREAD(newmsginfo->flags))
+                               dest->unread++;
+                       dest->total++;
+
+                       procmsg_msginfo_free(newmsginfo);
+               }                       
        }
 
        if (folder->finished_copy)
@@ -1625,7 +1673,7 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
         * Copy messages to destination folder and 
         * store new message numbers in newmsgnums
         */
-       for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
+       for (l = msglist ; l != NULL ; l = g_slist_next(l)) {
                MsgInfo * msginfo = (MsgInfo *) l->data;
 
                num = folder->copy_msg(folder, dest, msginfo);
@@ -1640,7 +1688,7 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
         * add them to the msgcache and update folder message counts
         */
        l2 = newmsgnums;
-       for(l = msglist; l != NULL; l = g_slist_next(l)) {
+       for (l = msglist; l != NULL; l = g_slist_next(l)) {
                MsgInfo *msginfo = (MsgInfo *) l->data;
 
                num = GPOINTER_TO_INT(l2->data);
@@ -1649,7 +1697,7 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
                        MsgInfo *newmsginfo;
 
                        newmsginfo = folder->fetch_msginfo(folder, dest, num);
-                       if(newmsginfo) {
+                       if (newmsginfo) {
                                newmsginfo->flags.perm_flags = msginfo->flags.perm_flags;
                                if (dest->stype == F_OUTBOX ||
                                    dest->stype == F_QUEUE  ||
@@ -1692,10 +1740,10 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
        ret = folder->remove_msg(folder, item, num);
 
        msginfo = msgcache_get_msg(item->cache, num);
-       if(msginfo != NULL) {
-               if(MSG_IS_NEW(msginfo->flags))
+       if (msginfo != NULL) {
+               if (MSG_IS_NEW(msginfo->flags))
                        item->new--;
-               if(MSG_IS_UNREAD(msginfo->flags))
+               if (MSG_IS_UNREAD(msginfo->flags))
                        item->unread--;
                procmsg_msginfo_free(msginfo);
                msgcache_remove_msg(item->cache, num);
@@ -2026,7 +2074,7 @@ static gchar *folder_get_list_path(void)
 static void folder_write_list_recursive(GNode *node, gpointer data)
 {
        FILE *fp = (FILE *)data;
-       FolderItem *item = FOLDER_ITEM(node->data);
+       FolderItem *item;
        gint i, depth;
        static gchar *folder_type_str[] = {"mh", "mbox", "maildir", "imap",
                                           "news", "unknown"};
@@ -2035,7 +2083,10 @@ static void folder_write_list_recursive(GNode *node, gpointer data)
        static gchar *sort_key_str[] = {"none", "number", "size", "date",
                                        "from", "subject", "score", "label",
                                        "mark", "unread", "mime", "locked" };
+       g_return_if_fail(node != NULL);
+       g_return_if_fail(fp != NULL);
 
+       item = FOLDER_ITEM(node->data);
        g_return_if_fail(item != NULL);
 
        depth = g_node_depth(node);
@@ -2351,7 +2402,7 @@ void folder_item_apply_processing(FolderItem *item)
 
        mlist = folder_item_get_msg_list(item);
        
-       for(cur = mlist ; cur != NULL ; cur = cur->next) {
+       for (cur = mlist ; cur != NULL ; cur = cur->next) {
                MsgInfo * msginfo;
 
                msginfo = (MsgInfo *) cur->data;