sync with 0.8.5
[claws.git] / src / procmsg.c
index 2515c29178f6ca2e2a4269c20627d141863ce3a2..8e0fad864c973fa3689943b23a09abb98bfc6557 100644 (file)
@@ -202,19 +202,19 @@ GSList *procmsg_read_cache(FolderItem *item, gboolean scan_file)
        }
        cache_file = folder_item_get_cache_file(item);
        if ((fp = fopen(cache_file, "rb")) == NULL) {
-               debug_print(_("\tNo cache file\n"));
+               debug_print("\tNo cache file\n");
                g_free(cache_file);
                return NULL;
        }
        setvbuf(fp, file_buf, _IOFBF, sizeof(file_buf));
        g_free(cache_file);
 
-       debug_print(_("\tReading summary cache...\n"));
+       debug_print("\tReading summary cache...\n");
 
        /* compare cache version */
        if (fread(&ver, sizeof(ver), 1, fp) != 1 ||
            CACHE_VERSION != ver) {
-               debug_print(_("Cache version is different. Discarding it.\n"));
+               debug_print("Cache version is different. Discarding it.\n");
                fclose(fp);
                return NULL;
        }
@@ -262,7 +262,7 @@ GSList *procmsg_read_cache(FolderItem *item, gboolean scan_file)
        }
 
        fclose(fp);
-       debug_print(_("done.\n"));
+       debug_print("done.\n");
 
        return mlist;
 }
@@ -284,7 +284,7 @@ void procmsg_set_flags(GSList *mlist, FolderItem *item)
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->folder != NULL);
 
-       debug_print(_("\tMarking the messages...\n"));
+       debug_print("\tMarking the messages...\n");
 
        markdir = folder_item_get_path(item);
        if (!is_dir_exist(markdir))
@@ -326,9 +326,9 @@ void procmsg_set_flags(GSList *mlist, FolderItem *item)
 
        item->last_num = lastnum;
 
-       debug_print(_("done.\n"));
+       debug_print("done.\n");
        if (newmsg)
-               debug_print(_("\t%d new message(s)\n"), newmsg);
+               debug_print("\t%d new message(s)\n", newmsg);
 
        hash_free_value_mem(mark_table);
        g_hash_table_destroy(mark_table);
@@ -470,10 +470,14 @@ 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;
     
-               if(!MSG_IS_REALLY_DELETED(*flags)) {
+               if (!MSG_IS_REALLY_DELETED(*flags)) {
                        g_hash_table_insert(mark_table, GUINT_TO_POINTER(num), flags);
                } else {
                        g_hash_table_remove(mark_table, GUINT_TO_POINTER(num));
@@ -493,10 +497,10 @@ FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
        markfile = g_strconcat(folder, G_DIR_SEPARATOR_S, MARK_FILE, NULL);
 
        if ((fp = fopen(markfile, "rb")) == NULL)
-               debug_print(_("Mark file not found.\n"));
+               debug_print("Mark file not found.\n");
        else if (fread(&ver, sizeof(ver), 1, fp) != 1 || MARK_VERSION != ver) {
-               debug_print(_("Mark version is different (%d != %d). "
-                             "Discarding it.\n"), ver, MARK_VERSION);
+               debug_print("Mark version is different (%d != %d). "
+                             "Discarding it.\n", ver, MARK_VERSION);
                fclose(fp);
                fp = NULL;
        }
@@ -527,6 +531,15 @@ FILE *procmsg_open_mark_file(const gchar *folder, gboolean append)
        return fp;
 }
 
+static gboolean procmsg_ignore_node(GNode *node, gpointer data)
+{
+       MsgInfo *msginfo = (MsgInfo *)node->data;
+       
+       procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
+
+       return FALSE;
+}
+
 /* return the reversed thread tree */
 GNode *procmsg_get_thread_tree(GSList *mlist)
 {
@@ -551,7 +564,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
                        if (parent == NULL) {
                                parent = root;
                        } else {
-                               if(MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->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);
                                }
                        }
@@ -592,8 +605,8 @@ 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) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
-                               procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
+                       if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+                               g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
                        }
                }
                node = next;
@@ -623,14 +636,14 @@ 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) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
-                                       procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
+                               if (MSG_IS_IGNORE_THREAD(((MsgInfo *)parent->data)->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+                                       g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, procmsg_ignore_node, NULL);
                                }
                        }
                }                                       
                node = next;
        }               
-               
+
        g_hash_table_destroy(subject_table);
        g_hash_table_destroy(msgid_table);
 
@@ -761,7 +774,7 @@ FILE *procmsg_open_message(MsgInfo *msginfo)
 
        g_free(file);
 
-       if (MSG_IS_QUEUED(msginfo->flags)) {
+       if (MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags)) {
                gchar buf[BUFFSIZE];
 
                while (fgets(buf, sizeof(buf), fp) != NULL)
@@ -857,7 +870,7 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
        list = folder_item_get_msg_list(queue);
 
 
-       for(elem = list; elem != NULL; elem = elem->next) {
+       for (elem = list; elem != NULL; elem = elem->next) {
                gchar *file;
                MsgInfo *msginfo;
                
@@ -898,7 +911,7 @@ gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
        FILE *fp;
        MsgInfo *msginfo;
 
-       debug_print(_("saving sent message...\n"));
+       debug_print("saving sent message...\n");
 
        if (!outbox)
                outbox = folder_get_default_outbox();
@@ -927,26 +940,27 @@ 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);
-       if(msginfo != NULL) {
-           procmsg_msginfo_unset_flags(msginfo, ~0, ~0);
+       if (msginfo != NULL) {
+           procmsg_msginfo_unset_flags(msginfo, ~0, 0);
            procmsg_msginfo_free(msginfo);
        }
 
-       if(is_queued) {
-               unlink(file);
-       }
-
        return 0;
 }
 
@@ -1071,12 +1085,43 @@ MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
        return newmsginfo;
 }
 
+MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
+{
+       MsgInfo *full_msginfo;
+       gchar *file;
+
+       if (msginfo == NULL) return NULL;
+
+       file = procmsg_get_message_file(msginfo);
+       if (!file) {
+               g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
+               return NULL;
+       }
+
+       full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE, FALSE);
+       g_free(file);
+       if (!full_msginfo) return NULL;
+
+       full_msginfo->msgnum = msginfo->msgnum;
+       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
+
+       /* return full_msginfo;*/
+       return procmsg_msginfo_new_ref(msginfo);
+}
+
 void procmsg_msginfo_free(MsgInfo *msginfo)
 {
        if (msginfo == NULL) return;
 
        msginfo->refcnt--;
-       if(msginfo->refcnt > 0)
+       if (msginfo->refcnt > 0)
                return;
 
        g_free(msginfo->fromspace);
@@ -1105,33 +1150,33 @@ guint procmsg_msginfo_memusage(MsgInfo *msginfo)
        guint memusage = 0;
        
        memusage += sizeof(MsgInfo);
-       if(msginfo->fromname)
+       if (msginfo->fromname)
                memusage += strlen(msginfo->fromname);
-       if(msginfo->date)
+       if (msginfo->date)
                memusage += strlen(msginfo->date);
-       if(msginfo->from)
+       if (msginfo->from)
                memusage += strlen(msginfo->from);
-       if(msginfo->to)
+       if (msginfo->to)
                memusage += strlen(msginfo->to);
-       if(msginfo->cc)
+       if (msginfo->cc)
                memusage += strlen(msginfo->cc);
-       if(msginfo->newsgroups)
+       if (msginfo->newsgroups)
                memusage += strlen(msginfo->newsgroups);
-       if(msginfo->subject)
+       if (msginfo->subject)
                memusage += strlen(msginfo->subject);
-       if(msginfo->msgid)
+       if (msginfo->msgid)
                memusage += strlen(msginfo->msgid);
-       if(msginfo->inreplyto)
+       if (msginfo->inreplyto)
                memusage += strlen(msginfo->inreplyto);
-       if(msginfo->xface)
+       if (msginfo->xface)
                memusage += strlen(msginfo->xface);
-       if(msginfo->dispositionnotificationto)
+       if (msginfo->dispositionnotificationto)
                memusage += strlen(msginfo->dispositionnotificationto);
-       if(msginfo->returnreceiptto)
+       if (msginfo->returnreceiptto)
                memusage += strlen(msginfo->returnreceiptto);
-       if(msginfo->references)
+       if (msginfo->references)
                memusage += strlen(msginfo->references);
-       if(msginfo->fromspace)
+       if (msginfo->fromspace)
                memusage += strlen(msginfo->fromspace);
 
        return memusage;
@@ -1227,10 +1272,9 @@ gint procmsg_send_message_queue(const gchar *file)
        }
        filepos = ftell(fp);
 
-       fseek(fp, filepos, SEEK_SET);
        if (to_list) {
-               debug_print(_("Sending message by mail\n"));
-               if(!from) {
+               debug_print("Sending message by mail\n");
+               if (!from) {
                        g_warning(_("Queued message header is broken.\n"));
                        mailval = -1;
                } else if (mailac && mailac->use_mail_command &&
@@ -1278,7 +1322,8 @@ gint procmsg_send_message_queue(const gchar *file)
                }
        }
 
-       if(newsgroup_list && (newsval == 0)) {
+       fseek(fp, filepos, SEEK_SET);
+       if (newsgroup_list && (newsval == 0)) {
                Folder *folder;
                gchar *tmp = NULL;
                FILE *tmpfp;
@@ -1305,8 +1350,8 @@ gint procmsg_send_message_queue(const gchar *file)
                        }
                        fclose(tmpfp);
 
-                       if(newsval == 0) {
-                               debug_print(_("Sending message by news\n"));
+                       if (newsval == 0) {
+                               debug_print("Sending message by news\n");
 
                                folder = FOLDER(newsac->folder);
 
@@ -1333,35 +1378,35 @@ gint procmsg_send_message_queue(const gchar *file)
        if (mailval == 0 && newsval == 0 && savecopyfolder) {
                FolderItem *outbox;
 
-               debug_print(_("saving sent message...\n"));
+               debug_print("saving sent message...\n");
 
                outbox = folder_find_item_from_identifier(savecopyfolder);
-               if(!outbox)
+               if (!outbox)
                        outbox = folder_get_default_outbox();
 
                procmsg_save_to_outbox(outbox, file, TRUE);
        }
 
-       if(replymessageid != NULL) {
+       if (replymessageid != NULL) {
                gchar **tokens;
                FolderItem *item;
                
                tokens = g_strsplit(replymessageid, "\x7f", 0);
                item = folder_find_item_from_identifier(tokens[0]);
-               if(item != NULL) {
+               if (item != NULL) {
                        MsgInfo *msginfo;
                        
                        msginfo = folder_item_fetch_msginfo(item, atoi(tokens[1]));
-                       if((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
+                       if ((msginfo != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) {
                                procmsg_msginfo_free(msginfo);
                                msginfo = NULL;
                        }
                        
-                       if(msginfo == NULL) {
+                       if (msginfo == NULL) {
                                msginfo = folder_item_fetch_msginfo_by_id(item, tokens[2]);
                        }
                        
-                       if(msginfo != NULL) {
+                       if (msginfo != NULL) {
                                procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
                                procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
 
@@ -1390,29 +1435,29 @@ void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmp
        gboolean changed = FALSE;
        FolderItem *item = msginfo->folder;
 
-       debug_print(_("Setting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
+       debug_print("Setting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
 
        /* if new flag is set */
-       if((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
+       if ((perm_flags & MSG_NEW) && !MSG_IS_NEW(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->new++;
                changed = TRUE;
        }
 
        /* if unread flag is set */
-       if((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
+       if ((perm_flags & MSG_UNREAD) && !MSG_IS_UNREAD(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->unread++;
                changed = TRUE;
        }
 
        /* if ignore thread flag is set */
-       if((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
-               if(MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
+       if ((perm_flags & MSG_IGNORE_THREAD) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+               if (MSG_IS_NEW(msginfo->flags) || (perm_flags & MSG_NEW)) {
                        item->new--;
                        changed = TRUE;
                }
-               if(MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
+               if (MSG_IS_UNREAD(msginfo->flags) || (perm_flags & MSG_UNREAD)) {
                        item->unread--;
                        changed = TRUE;
                }
@@ -1424,7 +1469,7 @@ void procmsg_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgTmp
        msginfo->flags.perm_flags |= perm_flags;
        msginfo->flags.tmp_flags |= tmp_flags;
 
-       if(changed) {
+       if (changed) {
                folderview_update_item(item, FALSE);
        }
        CHANGE_FLAGS(msginfo);
@@ -1436,29 +1481,29 @@ void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgT
        gboolean changed = FALSE;
        FolderItem *item = msginfo->folder;
        
-       debug_print(_("Unsetting flags for message %d in folder %s\n"), msginfo->msgnum, item->path);
+       debug_print("Unsetting flags for message %d in folder %s\n", msginfo->msgnum, item->path);
 
        /* if new flag is unset */
-       if((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
+       if ((perm_flags & MSG_NEW) && MSG_IS_NEW(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->new--;
                changed = TRUE;
        }
 
        /* if unread flag is unset */
-       if((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
+       if ((perm_flags & MSG_UNREAD) && MSG_IS_UNREAD(msginfo->flags) &&
           !MSG_IS_IGNORE_THREAD(msginfo->flags)) {
                item->unread--;
                changed = TRUE;
        }
 
        /* if ignore thread flag is unset */
-       if((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
-               if(MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
+       if ((perm_flags & MSG_IGNORE_THREAD) && MSG_IS_IGNORE_THREAD(msginfo->flags)) {
+               if (MSG_IS_NEW(msginfo->flags) && !(perm_flags & MSG_NEW)) {
                        item->new++;
                        changed = TRUE;
                }
-               if(MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
+               if (MSG_IS_UNREAD(msginfo->flags) && !(perm_flags & MSG_UNREAD)) {
                        item->unread++;
                        changed = TRUE;
                }
@@ -1470,9 +1515,8 @@ void procmsg_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags perm_flags, MsgT
        msginfo->flags.perm_flags &= ~perm_flags;
        msginfo->flags.tmp_flags &= ~tmp_flags;
 
-       if(changed) {
+       if (changed) 
                folderview_update_item(item, FALSE);
-       }
        CHANGE_FLAGS(msginfo);
        procmsg_msginfo_write_flags(msginfo);
 }