0.8.11claws66
[claws.git] / src / folder.c
index 68c744c749bf38d86dfa7fb89005c642a2211985..6de75e00f9bc2ad11f6da9c8d90d2fd253bd8937 100644 (file)
@@ -675,28 +675,23 @@ gchar *folder_item_get_name(FolderItem *item)
 
        switch (item->stype) {
        case F_INBOX:
-               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
-                               !strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
+               name = g_strdup(!strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
                                item->name);
                break;
        case F_OUTBOX:
-               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
-                               !strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
+               name = g_strdup(!strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
                                item->name);
                break;
        case F_QUEUE:
-               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
-                               !strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
+               name = g_strdup(!strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
                                item->name);
                break;
        case F_TRASH:
-               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
-                               !strcmp2(item->name, TRASH_DIR) ? _("Trash") :
+               name = g_strdup(!strcmp2(item->name, TRASH_DIR) ? _("Trash") :
                                item->name);
                break;
        case F_DRAFT:
-               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
-                               !strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
+               name = g_strdup(!strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
                                item->name);
                break;
        default:
@@ -1229,6 +1224,12 @@ gint folder_item_scan(FolderItem *item)
                        procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
                        procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
                }
+               if ((item->stype == F_OUTBOX ||
+                    item->stype == F_QUEUE  ||
+                    item->stype == F_DRAFT  ||
+                    item->stype == F_TRASH) &&
+                   (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
+                       procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
                if (MSG_IS_NEW(msginfo->flags))
                        newcnt++;
                if (MSG_IS_UNREAD(msginfo->flags))
@@ -1493,6 +1494,80 @@ static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
        return msgnum;
 }
 
+static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
+{
+       MsgPermFlags perm_flags = 0;
+       MsgTmpFlags tmp_flags = 0;
+
+       /* create new flags */
+       if (source != NULL) {
+               /* copy original flags */
+               perm_flags = source->flags.perm_flags;
+               tmp_flags = source->flags.tmp_flags;
+       } else {
+               perm_flags = dest->flags.perm_flags;
+               tmp_flags = dest->flags.tmp_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)
+               perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
+
+       /* set ignore flag of ignored parent exists */
+       if (procmsg_msg_has_flagged_parent(dest, MSG_IGNORE_THREAD))
+               perm_flags |= MSG_IGNORE_THREAD;
+
+       /* Unset tmp flags that should not be copied */
+       tmp_flags &= ~(MSG_MOVE | MSG_COPY);
+
+       /* unset flags that are set but should not */
+       procmsg_msginfo_unset_flags(dest,
+                                   dest->flags.perm_flags & ~perm_flags,
+                                   dest->flags.tmp_flags  & ~tmp_flags);
+       /* set new flags */
+       procmsg_msginfo_set_flags(dest,
+                                 ~dest->flags.perm_flags & perm_flags,
+                                 ~dest->flags.tmp_flags  & tmp_flags);
+
+       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);
+}
+
 gint folder_item_add_msg(FolderItem *dest, const gchar *file,
                         gboolean remove_source)
 {
@@ -1516,20 +1591,8 @@ gint folder_item_add_msg(FolderItem *dest, const gchar *file,
                msginfo = folder->class->get_msginfo(folder, dest, num);
 
                if (msginfo != NULL) {
-                       if (MSG_IS_NEW(msginfo->flags))
-                               dest->new++;
-                       if (MSG_IS_UNREAD(msginfo->flags))
-                               dest->unread++;
-                       if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
-                               dest->unreadmarked++;
-                       if (procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD))
-                               procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
-                       dest->total++;
-
-                       msgcache_add_msg(dest->cache, msginfo);
+                       add_msginfo_to_cache(dest, msginfo, NULL);
                        procmsg_msginfo_free(msginfo);
-
-                       folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
                }
 
                 dest->last_num = num;
@@ -1568,11 +1631,9 @@ gint folder_item_move_msg(FolderItem *dest, MsgInfo *msginfo)
 FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest) 
 {
        GSList *mlist;
-       GSList *cur;
        FolderItem *new_item;
        FolderItem *next_item;
        GNode *srcnode;
-       int cnt = 0;
        gchar *old_id, *new_id;
 
        mlist = folder_item_get_msg_list(src);
@@ -1589,18 +1650,9 @@ FolderItem *folder_item_move_recursive (FolderItem *src, FolderItem *dest)
                new_item->folder = dest->folder;
 
        /* move messages */
-       for (cur = mlist ; cur != NULL ; cur = cur->next) {
-               MsgInfo * msginfo;
-               cnt++;
-               if (cnt%500)
-                       log_message(_("Moving %s to %s (%d%%)...\n"), src->name, 
-                                       new_item->path,
-                                       100*cnt/g_slist_length(mlist));
-               msginfo = (MsgInfo *) cur->data;
-               folder_item_move_msg(new_item, msginfo);
-
-               procmsg_msginfo_free(msginfo);
-       }
+       log_message(_("Moving %s to %s...\n"), 
+                       src->name, new_item->path);
+       folder_item_move_msgs_with_dest(new_item, mlist);
        
        /*copy prefs*/
        prefs_folder_item_copy_prefs(src, new_item);
@@ -1738,6 +1790,8 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
 }
 */
 
+
+
 gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
 {
        Folder *folder;
@@ -1805,35 +1859,14 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                        if (num == 0)
                                continue;
 
-                       if (!folderscan) {
+                       if (!folderscan && 
+                           ((newmsginfo = folder->class->get_msginfo(folder, dest, num)) != NULL)) {
                                newmsginfo = folder->class->get_msginfo(folder, dest, num);
-                               if (newmsginfo) {
-                                       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++;
-                                       if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
-                                               dest->unreadmarked++;
-                                       if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
-                                               procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
-                                       dest->total++;
-                                       folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
-
-                                       procmsg_msginfo_free(newmsginfo);
-                               }
-                       } else {
-                               newmsginfo = msgcache_get_msg(dest->cache, num);
-                               procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
-                               procmsg_msginfo_set_flags(newmsginfo, msginfo->flags.perm_flags, 0);
+                               add_msginfo_to_cache(dest, newmsginfo, msginfo);
+                               procmsg_msginfo_free(newmsginfo);
+                       } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
+                               copy_msginfo_flags(msginfo, newmsginfo);
+                               procmsg_msginfo_free(newmsginfo);
                        }
                }
        }
@@ -1854,18 +1887,7 @@ gint folder_item_move_msgs_with_dest(FolderItem *dest, GSList *msglist)
                        item->folder->class->remove_msg(item->folder,
                                                        msginfo->folder,
                                                        msginfo->msgnum);
-                       if (!item->cache)
-                               folder_item_read_cache(item);
-                       msgcache_remove_msg(item->cache, msginfo->msgnum);
-
-                       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--;                       
-                       folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
+                       remove_msginfo_from_cache(item, msginfo);
                }
        }
 
@@ -1989,35 +2011,14 @@ gint folder_item_copy_msgs_with_dest(FolderItem *dest, GSList *msglist)
                        if (num == 0)
                                continue;
 
-                       if (!folderscan) {
+                       if (!folderscan && 
+                           ((newmsginfo = folder->class->get_msginfo(folder, dest, num)) != NULL)) {
                                newmsginfo = folder->class->get_msginfo(folder, dest, num);
-                               if (newmsginfo) {
-                                       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++;
-                                       if (MSG_IS_UNREAD(newmsginfo->flags) && procmsg_msg_has_marked_parent(newmsginfo))
-                                               dest->unreadmarked++;
-                                       if (procmsg_msg_has_flagged_parent(newmsginfo, MSG_IGNORE_THREAD))
-                                               procmsg_msginfo_set_flags(newmsginfo, MSG_IGNORE_THREAD, 0);
-                                       dest->total++;
-                                       folder_item_update(dest, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
-
-                                       procmsg_msginfo_free(newmsginfo);
-                               }
-                       } else {
-                               newmsginfo = msgcache_get_msg(dest->cache, num);
-                               procmsg_msginfo_unset_flags(newmsginfo, ~0, ~0);
-                               procmsg_msginfo_set_flags(newmsginfo, msginfo->flags.perm_flags, 0);
+                               add_msginfo_to_cache(dest, newmsginfo, msginfo);
+                               procmsg_msginfo_free(newmsginfo);
+                       } else if ((newmsginfo = msgcache_get_msg(dest->cache, num)) != NULL) {
+                               copy_msginfo_flags(msginfo, newmsginfo);
+                               procmsg_msginfo_free(newmsginfo);
                        }
                }
        }
@@ -2044,14 +2045,8 @@ gint folder_item_remove_msg(FolderItem *item, gint num)
 
        msginfo = msgcache_get_msg(item->cache, num);
        if (msginfo != NULL) {
-               if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
-                       item->new--;
-               if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
-                       item->unread--;
-               if (MSG_IS_UNREAD(msginfo->flags) && procmsg_msg_has_marked_parent(msginfo))
-                       item->unreadmarked--;
+               remove_msginfo_from_cache(item, msginfo);
                procmsg_msginfo_free(msginfo);
-               msgcache_remove_msg(item->cache, num);
        }
        item->total--;
        folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
@@ -2590,44 +2585,51 @@ static gchar * folder_item_get_tree_identifier(FolderItem * item)
 */
 
 /* CLAWS: temporary local folder for filtering */
-static Folder *processing_folder;
+#define TEMP_FOLDER "TEMP_FOLDER"
+#define PROCESSING_FOLDER_ITEM "processing"    
+
 static FolderItem *processing_folder_item;
 
 static void folder_create_processing_folder(void)
 {
-#define PROCESSING_FOLDER ".processing"        
-       Folder     *tmpparent;
+       Folder *processing_folder;
        gchar      *tmpname;
 
-       tmpparent = folder_get_default_folder();
-       g_assert(tmpparent);
-       debug_print("tmpparentroot %s\n", LOCAL_FOLDER(tmpparent)->rootpath);
-       if (LOCAL_FOLDER(tmpparent)->rootpath[0] == '/')
-               tmpname = g_strconcat(LOCAL_FOLDER(tmpparent)->rootpath,
-                                     G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
+       if ((processing_folder = folder_find_from_name(TEMP_FOLDER, mh_get_class())) == NULL) {
+               gchar *tmppath;
+
+               tmppath =
+                   g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+                               "tempfolder", NULL);
+               processing_folder =
+                   folder_new(mh_get_class(), TEMP_FOLDER, tmppath);
+               g_free(tmppath);
+       }
+       g_assert(processing_folder != NULL);
+
+       debug_print("tmpparentroot %s\n", LOCAL_FOLDER(processing_folder)->rootpath);
+       if (LOCAL_FOLDER(processing_folder)->rootpath[0] == '/')
+               tmpname = g_strconcat(LOCAL_FOLDER(processing_folder)->rootpath,
+                                     G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
                                      NULL);
        else
                tmpname = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
-                                     LOCAL_FOLDER(tmpparent)->rootpath,
-                                     G_DIR_SEPARATOR_S, PROCESSING_FOLDER,
+                                     LOCAL_FOLDER(processing_folder)->rootpath,
+                                     G_DIR_SEPARATOR_S, PROCESSING_FOLDER_ITEM,
                                      NULL);
 
-       processing_folder = folder_new(mh_get_class(), "PROCESSING", LOCAL_FOLDER(tmpparent)->rootpath);
-       g_assert(processing_folder);
-
        if (!is_dir_exist(tmpname)) {
                debug_print("*TMP* creating %s\n", tmpname);
                processing_folder_item = processing_folder->class->create_folder(processing_folder,
                                                                                 processing_folder->node->data,
-                                                                                PROCESSING_FOLDER);
-               g_assert(processing_folder_item);                                                                         
-       }
-       else {
+                                                                                PROCESSING_FOLDER_ITEM);
+       } else {
                debug_print("*TMP* already created\n");
                processing_folder_item = folder_item_new(processing_folder, ".processing", ".processing");
                g_assert(processing_folder_item);
                folder_item_append(processing_folder->node->data, processing_folder_item);
        }
+       g_assert(processing_folder_item != NULL);
        g_free(tmpname);
 }