sycn with 0.8.8cvs11
[claws.git] / src / procmsg.c
index 3298c69fca0feb414b05dfe204432f5eb6ffc1fe..11180f2b4033553b48e19fa7afe54e4a6f5903a2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -659,6 +659,8 @@ void procmsg_move_messages(GSList *mlist)
 
        if (!mlist) return;
 
+       folder_item_update_freeze();
+
        for (cur = mlist; cur != NULL; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
                if (!dest) {
@@ -673,12 +675,15 @@ void procmsg_move_messages(GSList *mlist)
                        dest = msginfo->to_folder;
                        movelist = g_slist_append(movelist, msginfo);
                }
+               procmsg_msginfo_set_to_folder(msginfo, NULL);
        }
 
        if (movelist) {
                folder_item_move_msgs_with_dest(dest, movelist);
                g_slist_free(movelist);
        }
+
+       folder_item_update_thaw();
 }
 
 void procmsg_copy_messages(GSList *mlist)
@@ -689,14 +694,7 @@ void procmsg_copy_messages(GSList *mlist)
 
        if (!mlist) return;
 
-       /* 
-       
-       Horrible: Scanning 2 times for every copy!
-
-       hash = procmsg_to_folder_hash_table_create(mlist);
-       folder_item_scan_foreach(hash);
-       g_hash_table_destroy(hash);
-       */
+       folder_item_update_freeze();
 
        for (cur = mlist; cur != NULL; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
@@ -712,12 +710,15 @@ void procmsg_copy_messages(GSList *mlist)
                        dest = msginfo->to_folder;
                        copylist = g_slist_append(copylist, msginfo);
                }
+               procmsg_msginfo_set_to_folder(msginfo, NULL);
        }
 
        if (copylist) {
                folder_item_copy_msgs_with_dest(dest, copylist);
                g_slist_free(copylist);
        }
+
+       folder_item_update_thaw();
 }
 
 gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
@@ -809,8 +810,7 @@ FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
        }
 
        if (MSG_IS_ENCRYPTED(msginfo->flags) &&
-           !msginfo->plaintext_file &&
-           !msginfo->decryption_failed) {
+           (!msginfo->plaintext_file || msginfo->decryption_failed)) {
                rfc2015_decrypt_message(msginfo, mimeinfo_, fp);
                if (msginfo->plaintext_file &&
                    !msginfo->decryption_failed) {
@@ -900,8 +900,6 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
                procmsg_msginfo_free(msginfo);
        }
 
-       folder_update_item(queue, FALSE);
-
        return ret;
 }
 
@@ -969,6 +967,7 @@ gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
            procmsg_msginfo_unset_flags(msginfo, ~0, 0);
            procmsg_msginfo_free(msginfo);
        }
+       folder_item_update(outbox, TRUE);
 
        return 0;
 }
@@ -1115,11 +1114,11 @@ MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
        full_msginfo->size = msginfo->size;
        full_msginfo->mtime = msginfo->mtime;
        full_msginfo->folder = msginfo->folder;
-       full_msginfo->to_folder = msginfo->to_folder;
 #if USE_GPGME
        full_msginfo->plaintext_file = g_strdup(msginfo->plaintext_file);
        full_msginfo->decryption_failed = msginfo->decryption_failed;
 #endif
+       procmsg_msginfo_set_to_folder(full_msginfo, msginfo->to_folder);
 
        return full_msginfo;
 }
@@ -1132,6 +1131,11 @@ void procmsg_msginfo_free(MsgInfo *msginfo)
        if (msginfo->refcnt > 0)
                return;
 
+       if (msginfo->to_folder) {
+               msginfo->to_folder->op_count--;
+               folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
+       }
+
        g_free(msginfo->fromspace);
        g_free(msginfo->references);
        g_free(msginfo->returnreceiptto);
@@ -1213,6 +1217,7 @@ enum
        Q_NEWS_ACCOUNT_ID  = 5,
        Q_SAVE_COPY_FOLDER = 6,
        Q_REPLY_MESSAGE_ID = 7,
+       Q_FWD_MESSAGE_ID   = 8
 };
 
 gint procmsg_send_message_queue(const gchar *file)
@@ -1225,6 +1230,7 @@ gint procmsg_send_message_queue(const gchar *file)
                                       {"NAID:", NULL, FALSE},
                                       {"SCF:",  NULL, FALSE},
                                       {"RMID:", NULL, FALSE},
+                                      {"FMID:", NULL, FALSE},
                                       {NULL,    NULL, FALSE}};
        FILE *fp;
        gint filepos;
@@ -1235,6 +1241,7 @@ gint procmsg_send_message_queue(const gchar *file)
        GSList *newsgroup_list = NULL;
        gchar *savecopyfolder = NULL;
        gchar *replymessageid = NULL;
+       gchar *fwdmessageid = NULL;
        gchar buf[BUFFSIZE];
        gint hnum;
        PrefsAccount *mailac = NULL, *newsac = NULL;
@@ -1276,6 +1283,9 @@ gint procmsg_send_message_queue(const gchar *file)
                case Q_REPLY_MESSAGE_ID:
                        if (!replymessageid) replymessageid = g_strdup(p);
                        break;
+               case Q_FWD_MESSAGE_ID:
+                       if (!fwdmessageid) fwdmessageid = g_strdup(p);
+                       break;
                }
        }
        filepos = ftell(fp);
@@ -1318,11 +1328,11 @@ gint procmsg_send_message_queue(const gchar *file)
                }
                if (mailval < 0) {
                        if (!local)
-                               alertpanel_error(
+                               alertpanel_error_log(
                                        _("Error occurred while sending the message to `%s'."),
                                        mailac ? mailac->smtp_server : smtpserver);
                        else
-                               alertpanel_error(
+                               alertpanel_error_log(
                                        _("Error occurred while sending the message with command `%s'."),
                                        (mailac && mailac->use_mail_command && 
                                         mailac->mail_command && (*mailac->mail_command)) ? 
@@ -1395,11 +1405,14 @@ gint procmsg_send_message_queue(const gchar *file)
                procmsg_save_to_outbox(outbox, file, TRUE);
        }
 
-       if (replymessageid != NULL) {
+       if (replymessageid != NULL || fwdmessageid != NULL) {
                gchar **tokens;
                FolderItem *item;
                
-               tokens = g_strsplit(replymessageid, "\x7f", 0);
+               if (replymessageid != NULL)
+                       tokens = g_strsplit(replymessageid, "\x7f", 0);
+               else
+                       tokens = g_strsplit(fwdmessageid, "\x7f", 0);
                item = folder_find_item_from_identifier(tokens[0]);
                if (item != NULL) {
                        MsgInfo *msginfo;
@@ -1415,9 +1428,14 @@ gint procmsg_send_message_queue(const gchar *file)
                        }
                        
                        if (msginfo != NULL) {
-                               procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
-                               procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
-
+                               if (replymessageid != NULL) {
+                                       procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
+                                       procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
+                               } 
+                               else {
+                                       procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
+                                       procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0);
+                               }
                                procmsg_msginfo_free(msginfo);
                        }
                }
@@ -1426,6 +1444,7 @@ gint procmsg_send_message_queue(const gchar *file)
 
        g_free(savecopyfolder);
        g_free(replymessageid);
+       g_free(fwdmessageid);
        
        return (newsval != 0 ? newsval : mailval);
 }
@@ -1453,20 +1472,17 @@ void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmp
        if ((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->new++;
-               item->need_update = TRUE;
        }
 
        /* if unread flag is set */
        if ((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->unread++;
-               item->need_update = TRUE;
        }
 
        if (!MSG_IS_UNREAD(msginfo->flags) &&(perm_flags & MSG_UNREAD)
        && procmsg_msg_has_marked_parent(msginfo)) {
                item->unreadmarked++;
-               item->need_update = TRUE;
        }
        
        if (!MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)) {
@@ -1478,16 +1494,13 @@ void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmp
        if ((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                if (MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
                        item->new--;
-                       item->need_update = TRUE;
                }
                if (MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
                        item->unread--;
-                       item->need_update = TRUE;
                }
                if ((perm_flags & MSG_UNREAD) || (MSG_IS_UNREAD(msginfo->flags)
                && procmsg_msg_has_marked_parent(msginfo))) {
                        item->unreadmarked--;
-                       item->need_update = TRUE;
                }
                if ((perm_flags & MSG_MARKED) || (MSG_IS_MARKED(msginfo->flags)
                && !MSG_IS_IGNORE_THREAD(msginfo->flags))) {
@@ -1504,6 +1517,7 @@ void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmp
 
        msginfo_update.msginfo = msginfo;
        hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
+       folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
 
        CHANGE_FLAGS(msginfo);
        procmsg_msginfo_write_flags(msginfo);
@@ -1524,21 +1538,18 @@ void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgT
        if ((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->new--;
-               item->need_update = TRUE;
        }
 
        /* if unread flag is unset */
        if ((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->unread--;
-               item->need_update = TRUE;
        }
        
        if (MSG_IS_UNREAD(msginfo->flags) && (perm_flags & MSG_UNREAD)
        && !MSG_IS_IGNORE_THREAD(msginfo->flags)
        && procmsg_msg_has_marked_parent(msginfo)) {
                item->unreadmarked--;
-               item->need_update = TRUE;
        }
 
        if (MSG_IS_MARKED(msginfo->flags) && (perm_flags & MSG_MARKED)
@@ -1550,16 +1561,13 @@ void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgT
        if ((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                if (MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
                        item->new++;
-                       item->need_update = TRUE;
                }
                if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
                        item->unread++;
-                       item->need_update = TRUE;
                }
                if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)
                && procmsg_msg_has_marked_parent(msginfo)) {
                        item->unreadmarked++;
-                       item->need_update = TRUE;
                }
                if (MSG_IS_MARKED(msginfo->flags) && !(perm_flags & MSG_MARKED)) {
                        procmsg_update_unread_children(msginfo, TRUE);
@@ -1575,6 +1583,7 @@ void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgT
 
        msginfo_update.msginfo = msginfo;
        hooks_invoke(MSGINFO_UPDATE_HOOKLIST, &msginfo_update);
+       folder_item_update(msginfo->folder, F_ITEM_UPDATE_MSGCNT);
 
        CHANGE_FLAGS(msginfo);
        procmsg_msginfo_write_flags(msginfo);
@@ -1703,7 +1712,7 @@ GSList *procmsg_find_children (MsgInfo *info)
        return children;
 }
 
-static void procmsg_update_unread_children (MsgInfo *info, gboolean newly_marked)
+static void procmsg_update_unread_children(MsgInfo *info, gboolean newly_marked)
 {
        GSList *children = procmsg_find_children(info);
        GSList *cur;
@@ -1714,8 +1723,27 @@ static void procmsg_update_unread_children (MsgInfo *info, gboolean newly_marked
                                info->folder->unreadmarked++;
                        else
                                info->folder->unreadmarked--;
-                       info->folder->need_update = TRUE;
+                       folder_item_update(info->folder, F_ITEM_UPDATE_MSGCNT);
                }
                procmsg_msginfo_free(tmp);
        }
 }
+
+/**
+ * Set the destination folder for a copy or move operation
+ *
+ * \param msginfo The message which's destination folder is changed
+ * \param to_folder The destination folder for the operation
+ */
+void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
+{
+       if(msginfo->to_folder != NULL) {
+               msginfo->to_folder->op_count--;
+               folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
+       }
+       msginfo->to_folder = to_folder;
+       if(to_folder != NULL) {
+               to_folder->op_count++;
+               folder_item_update(msginfo->to_folder, F_ITEM_UPDATE_MSGCNT);
+       }
+}