sync with 0.8.1cvs21
[claws.git] / src / procmsg.c
index 0ae7ab6..461c0c9 100644 (file)
@@ -39,6 +39,7 @@
 #endif
 #include "alertpanel.h"
 #include "news.h"
+#include "imap.h"
 
 typedef struct _FlagInfo       FlagInfo;
 
@@ -48,15 +49,8 @@ struct _FlagInfo
        MsgFlags flags;
 };
 
-static void mark_sum_func                      (gpointer        key,
-                                                gpointer        value,
-                                                gpointer        data);
-
 static GHashTable *procmsg_read_mark_file      (const gchar    *folder);
-static gint procmsg_cmp_msgnum                 (gconstpointer   a,
-                                                gconstpointer   b);
-static gint procmsg_cmp_flag_msgnum            (gconstpointer   a,
-                                                gconstpointer   b);
+void   procmsg_msginfo_write_flags             (MsgInfo        *msginfo);
 
 
 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
@@ -340,14 +334,12 @@ void procmsg_set_flags(GSList *mlist, FolderItem *item)
        g_hash_table_destroy(mark_table);
 }
 
-gint procmsg_get_last_num_in_cache(GSList *mlist)
+gint procmsg_get_last_num_in_msg_list(GSList *mlist)
 {
        GSList *cur;
        MsgInfo *msginfo;
        gint last = 0;
 
-       if (mlist == NULL) return 0;
-
        for (cur = mlist; cur != NULL; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
                if (msginfo && msginfo->msgnum > last)
@@ -462,49 +454,6 @@ struct MarkSum {
        gint first;
 };
 
-static void mark_sum_func(gpointer key, gpointer value, gpointer data)
-{
-       MsgFlags *flags = value;
-       gint num = GPOINTER_TO_INT(key);
-       struct MarkSum *marksum = data;
-
-       if (marksum->first <= num) {
-               if (MSG_IS_NEW(*flags) && !MSG_IS_IGNORE_THREAD(*flags)) (*marksum->new)++;
-               if (MSG_IS_UNREAD(*flags) && !MSG_IS_IGNORE_THREAD(*flags)) (*marksum->unread)++;
-               if (num > *marksum->max) *marksum->max = num;
-               if (num < *marksum->min || *marksum->min == 0) *marksum->min = num;
-               (*marksum->total)++;
-       }
-
-       g_free(flags);
-}
-
-void procmsg_get_mark_sum(const gchar *folder,
-                         gint *new, gint *unread, gint *total,
-                         gint *min, gint *max,
-                         gint first)
-{
-       GHashTable *mark_table;
-       struct MarkSum marksum;
-
-       *new = *unread = *total = *min = *max = 0;
-       marksum.new    = new;
-       marksum.unread = unread;
-       marksum.total  = total;
-       marksum.min    = min;
-       marksum.max    = max;
-       marksum.first  = first;
-
-       mark_table = procmsg_read_mark_file(folder);
-
-       if (mark_table) {
-               g_hash_table_foreach(mark_table, mark_sum_func, &marksum);
-               g_hash_table_destroy(mark_table);
-       }
-       debug_print("mark->new = %d, mark->unread = %d, mark->total = %d\n",
-                   *(marksum.new), *(marksum.unread), *(marksum.total));
-}
-
 static GHashTable *procmsg_read_mark_file(const gchar *folder)
 {
        FILE *fp;
@@ -521,6 +470,10 @@ static GHashTable *procmsg_read_mark_file(const gchar *folder)
        while (fread(&num, sizeof(num), 1, fp) == 1) {
                if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) break;
 
+               flags = g_hash_table_lookup(mark_table, GUINT_TO_POINTER(num));
+               if (flags != NULL)
+                       g_free(flags);
+
                flags = g_new0(MsgFlags, 1);
                flags->perm_flags = perm_flags;
     
@@ -587,6 +540,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
        MsgInfo *msginfo;
        const gchar *msgid;
        const gchar *subject;
+       GNode *found_subject;
 
        root = g_node_new(NULL);
        msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
@@ -601,7 +555,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                        if (parent == NULL) {
                                parent = root;
                        } else {
-                               if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags)) {
+                               if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                                        procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
                                }
                        }
@@ -614,10 +568,20 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                        g_hash_table_insert(msgid_table, (gchar *)msgid, node);
 
                subject = msginfo->subject;
-               if (subject_table_lookup(subject_table,
-                                        (gchar *) subject) == NULL)
-                       subject_table_insert(subject_table, (gchar *)subject,
+               found_subject = subject_table_lookup(subject_table,
+                                                    (gchar *) subject);
+               if (found_subject == NULL)
+                       subject_table_insert(subject_table, (gchar *) subject,
                                             node);
+               else {
+                       /* replace if msg in table is older than current one 
+                        * can add here more stuff.  */
+                       if ( ((MsgInfo*)(found_subject->data))->date_t >
+                            ((MsgInfo*)(node->data))->date_t )  {
+                               subject_table_remove(subject_table, (gchar *) subject);
+                               subject_table_insert(subject_table, (gchar *) subject, node);
+                       }       
+               }
        }
 
        /* complete the unfinished threads */
@@ -632,7 +596,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                        g_node_insert_before
                                (parent, parent->children, node);
                        /* CLAWS: ignore thread */
-                       if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags)) {
+                       if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                                procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
                        }
                }
@@ -663,7 +627,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                                g_node_unlink(node);
                                g_node_append(parent, node);
                                /* CLAWS: ignore thread */
-                               if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags)) {
+                               if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                                        procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
                                }
                        }
@@ -682,7 +646,6 @@ void procmsg_move_messages(GSList *mlist)
        GSList *cur, *movelist = NULL;
        MsgInfo *msginfo;
        FolderItem *dest = NULL;
-       GHashTable *hash;
 
        if (!mlist) return;
 
@@ -713,7 +676,6 @@ void procmsg_copy_messages(GSList *mlist)
        GSList *cur, *copylist = NULL;
        MsgInfo *msginfo;
        FolderItem *dest = NULL;
-       GHashTable *hash;
 
        if (!mlist) return;
 
@@ -888,7 +850,6 @@ void procmsg_empty_trash(void)
 
 gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
 {
-       gint i;
        gint ret = 0;
        GSList *list, *elem;
 
@@ -939,10 +900,9 @@ gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
 {
        gint num;
        FILE *fp;
-       MsgFlags flag = {0, 0};
        MsgInfo *msginfo;
 
-       debug_print(_("saving sent message...\n"));
+       debug_print("saving sent message...\n");
 
        if (!outbox)
                outbox = folder_get_default_outbox();
@@ -971,19 +931,26 @@ gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
                        fputs(buf, outfp);
                fclose(outfp);
                fclose(fp);
-               Xstrdup_a(file, tmp, return -1);
-       }
 
-       if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
-               g_warning(_("can't save message\n"));
-               if(is_queued) {
-                       unlink(file);
+               folder_item_scan(outbox);
+               if ((num = folder_item_add_msg(outbox, tmp, TRUE)) < 0) {
+                       g_warning("can't save message\n");
+                       unlink(tmp);
+                       return -1;
+               }
+       } else {
+               folder_item_scan(outbox);
+               if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
+                       g_warning("can't save message\n");
+                       return -1;
                }
                return -1;
        }
        msginfo = folder_item_fetch_msginfo(outbox, num);
-       procmsg_msginfo_unset_flags(msginfo, ~0, ~0);
-       procmsg_msginfo_free(msginfo);
+       if(msginfo != NULL) {
+           procmsg_msginfo_unset_flags(msginfo, ~0, ~0);
+           procmsg_msginfo_free(msginfo);
+       }
 
        if(is_queued) {
                unlink(file);
@@ -1179,17 +1146,6 @@ guint procmsg_msginfo_memusage(MsgInfo *msginfo)
        return memusage;
 }
 
-static gint procmsg_cmp_msgnum(gconstpointer a, gconstpointer b)
-{
-       const MsgInfo *msginfo = a;
-       const guint msgnum = GPOINTER_TO_UINT(b);
-
-       if (!msginfo)
-               return -1;
-
-       return msginfo->msgnum - msgnum;
-}
-
 gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
 {
        const MsgInfo *msginfo1 = a;
@@ -1203,17 +1159,6 @@ gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
        return msginfo1->msgnum - msginfo2->msgnum;
 }
 
-static gint procmsg_cmp_flag_msgnum(gconstpointer a, gconstpointer b)
-{
-       const FlagInfo *finfo = a;
-       const guint msgnum = GPOINTER_TO_UINT(b);
-
-       if (!finfo)
-               return -1;
-
-       return finfo->msgnum - msgnum;
-}
-
 enum
 {
        Q_SENDER           = 0,
@@ -1222,7 +1167,8 @@ enum
        Q_NEWSGROUPS       = 3,
        Q_MAIL_ACCOUNT_ID  = 4,
        Q_NEWS_ACCOUNT_ID  = 5,
-       Q_SAVE_COPY_FOLDER = 6
+       Q_SAVE_COPY_FOLDER = 6,
+       Q_REPLY_MESSAGE_ID = 7,
 };
 
 gint procmsg_send_message_queue(const gchar *file)
@@ -1234,6 +1180,7 @@ gint procmsg_send_message_queue(const gchar *file)
                                       {"MAID:", NULL, FALSE},
                                       {"NAID:", NULL, FALSE},
                                       {"SCF:",  NULL, FALSE},
+                                      {"RMID:", NULL, FALSE},
                                       {NULL,    NULL, FALSE}};
        FILE *fp;
        gint filepos;
@@ -1243,6 +1190,7 @@ gint procmsg_send_message_queue(const gchar *file)
        GSList *to_list = NULL;
        GSList *newsgroup_list = NULL;
        gchar *savecopyfolder = NULL;
+       gchar *replymessageid = NULL;
        gchar buf[BUFFSIZE];
        gint hnum;
        PrefsAccount *mailac = NULL, *newsac = NULL;
@@ -1281,6 +1229,9 @@ gint procmsg_send_message_queue(const gchar *file)
                case Q_SAVE_COPY_FOLDER:
                        if (!savecopyfolder) savecopyfolder = g_strdup(p);
                        break;
+               case Q_REPLY_MESSAGE_ID:
+                       if (!replymessageid) replymessageid = g_strdup(p);
+                       break;
                }
        }
        filepos = ftell(fp);
@@ -1400,6 +1351,38 @@ gint procmsg_send_message_queue(const gchar *file)
                procmsg_save_to_outbox(outbox, file, TRUE);
        }
 
+       if(replymessageid != NULL) {
+               gchar **tokens;
+               FolderItem *item;
+               
+               tokens = g_strsplit(replymessageid, "\x7f", 0);
+               item = folder_find_item_from_identifier(tokens[0]);
+               if(item != NULL) {
+                       MsgInfo *msginfo;
+                       
+                       msginfo = folder_item_fetch_msginfo(item, atoi(tokens[1]));
+                       if((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
+                               procmsg_msginfo_free(msginfo);
+                               msginfo = NULL;
+                       }
+                       
+                       if(msginfo == NULL) {
+                               msginfo = folder_item_fetch_msginfo_by_id(item, tokens[2]);
+                       }
+                       
+                       if(msginfo != NULL) {
+                               procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
+                               procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
+
+                               procmsg_msginfo_free(msginfo);
+                       }
+               }
+               g_strfreev(tokens);
+       }
+
+       g_free(savecopyfolder);
+       g_free(replymessageid);
+       
        return (newsval != 0 ? newsval : mailval);
 }