2007-06-17 [colin] 2.9.2cvs67
[claws.git] / src / procmsg.c
index ea8b3b04cc4fcd7b2f8566ae4a4f18f7fc14e1e2..04b4b76f20e9e76c9a1846144157a43939a06b91 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
  *
  * 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
@@ -49,8 +49,9 @@
 #include "inc.h"
 
 static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_session, gchar **errstr,
-                                           FolderItem *queue, gint msgnum);
-
+                                           FolderItem *queue, gint msgnum, gboolean *queued_removed);
+static void procmsg_update_unread_children     (MsgInfo        *info,
+                                        gboolean        newly_marked);
 enum
 {
        Q_SENDER           = 0,
@@ -72,67 +73,6 @@ enum
        Q_CLAWS_HDRS_OLD     = 16,
 };
 
-GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
-{
-       GHashTable *msg_table;
-
-       if (mlist == NULL) return NULL;
-
-       msg_table = g_hash_table_new(NULL, g_direct_equal);
-       procmsg_msg_hash_table_append(msg_table, mlist);
-
-       return msg_table;
-}
-
-void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist)
-{
-       GSList *cur;
-       MsgInfo *msginfo;
-
-       if (msg_table == NULL || mlist == NULL) return;
-
-       for (cur = mlist; cur != NULL; cur = cur->next) {
-               msginfo = (MsgInfo *)cur->data;
-
-               g_hash_table_insert(msg_table,
-                                   GUINT_TO_POINTER(msginfo->msgnum),
-                                   msginfo);
-       }
-}
-
-GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist)
-{
-       GHashTable *msg_table;
-       GSList *cur;
-       MsgInfo *msginfo;
-
-       if (mlist == NULL) return NULL;
-
-       msg_table = g_hash_table_new(NULL, g_direct_equal);
-
-       for (cur = mlist; cur != NULL; cur = cur->next) {
-               msginfo = (MsgInfo *)cur->data;
-               g_hash_table_insert(msg_table, msginfo->to_folder, msginfo);
-       }
-
-       return msg_table;
-}
-
-gint procmsg_get_last_num_in_msg_list(GSList *mlist)
-{
-       GSList *cur;
-       MsgInfo *msginfo;
-       gint last = 0;
-
-       for (cur = mlist; cur != NULL; cur = cur->next) {
-               msginfo = (MsgInfo *)cur->data;
-               if (msginfo && msginfo->msgnum > last)
-                       last = msginfo->msgnum;
-       }
-
-       return last;
-}
-
 void procmsg_msg_list_free(GSList *mlist)
 {
        GSList *cur;
@@ -711,7 +651,7 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
        }
 }
 
-void procmsg_empty_trash(FolderItem *trash)
+static void procmsg_empty_trash(FolderItem *trash)
 {
        GNode *node, *next;
 
@@ -725,8 +665,10 @@ void procmsg_empty_trash(FolderItem *trash)
                GSList *cur;
                for (cur = mlist ; cur != NULL ; cur = cur->next) {
                        MsgInfo * msginfo = (MsgInfo *) cur->data;
-                       if (MSG_IS_LOCKED(msginfo->flags))
+                       if (MSG_IS_LOCKED(msginfo->flags)) {
+                               procmsg_msginfo_free(msginfo);
                                continue;
+                       }
                        if (msginfo->total_size != 0 && 
                            msginfo->size != (off_t)msginfo->total_size)
                                partial_mark_for_delete(msginfo);
@@ -915,7 +857,7 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs, gchar **errstr)
        
        if (send_queue_lock) {
                /* Avoid having to translate two similar strings */
-               log_warning("%s\n", _("Already trying to send."));
+               log_warning(LOG_PROTOCOL, "%s\n", _("Already trying to send."));
                if (errstr) {
                        if (*errstr) g_free(*errstr);
                        *errstr = g_strdup_printf(_("Already trying to send."));
@@ -950,15 +892,17 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs, gchar **errstr)
                if (!MSG_IS_LOCKED(msginfo->flags)) {
                        file = folder_item_fetch_msg(queue, msginfo->msgnum);
                        if (file) {
+                               gboolean queued_removed = FALSE;
                                if (procmsg_send_message_queue_full(file, 
                                                !procmsg_is_last_for_account(queue, msginfo, elem),
-                                               errstr, queue, msginfo->msgnum) < 0) {
+                                               errstr, queue, msginfo->msgnum, &queued_removed) < 0) {
                                        g_warning("Sending queued message %d failed.\n", 
                                                  msginfo->msgnum);
                                        err++;
                                } else {
                                        sent++; 
-                                       folder_item_remove_msg(queue, msginfo->msgnum);
+                                       if (!queued_removed)
+                                               folder_item_remove_msg(queue, msginfo->msgnum);
                                }
                                g_free(file);
                        }
@@ -977,7 +921,9 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs, gchar **errstr)
                while (node != NULL) {
                        int res = 0;
                        next = node->next;
+                       send_queue_lock = FALSE;
                        res = procmsg_send_queue(FOLDER_ITEM(node->data), save_msgs, errstr);
+                       send_queue_lock = TRUE;
                        if (res < 0) 
                                err = -res;
                        else
@@ -1071,7 +1017,7 @@ gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
        return 0;
 }
 
-gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
+static gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
                            gboolean is_queued)
 {
        gint num;
@@ -1451,21 +1397,8 @@ guint procmsg_msginfo_memusage(MsgInfo *msginfo)
        return memusage;
 }
 
-gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
-{
-       const MsgInfo *msginfo1 = a;
-       const MsgInfo *msginfo2 = b;
-
-       if (!msginfo1)
-               return -1;
-       if (!msginfo2)
-               return -1;
-
-       return msginfo1->msgnum - msginfo2->msgnum;
-}
-
 static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_session, gchar **errstr,
-                                           FolderItem *queue, gint msgnum)
+                                           FolderItem *queue, gint msgnum, gboolean *queued_removed)
 {
        static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
                                       {"SSV:",  NULL, FALSE},
@@ -1754,15 +1687,25 @@ send_mail:
                        
                if (save_clear_text || tmp_enc_file == NULL) {
                        gboolean saved = FALSE;
+                       *queued_removed = FALSE;
                        if (queue && msgnum > 0) {
                                MsgInfo *queued_mail = folder_item_get_msginfo(queue, msgnum);
-                               if (folder_item_copy_msg(outbox, queued_mail) >= 0)
+                               if (folder_item_move_msg(outbox, queued_mail) >= 0) {
+                                       debug_print("moved queued mail %d to sent folder\n", msgnum);
                                        saved = TRUE;
+                                       *queued_removed = TRUE;
+                               } else if (folder_item_copy_msg(outbox, queued_mail) >= 0) {
+                                       debug_print("copied queued mail %d to sent folder\n", msgnum);
+                                       saved = TRUE;
+                               }
                                procmsg_msginfo_free(queued_mail);
                        }
-                       if (!saved)
+                       if (!saved) {
+                               debug_print("resaving clear text queued mail to sent folder\n");
                                procmsg_save_to_outbox(outbox, file, TRUE);
+                       }
                } else {
+                       debug_print("saving encrpyted queued mail to sent folder\n");
                        procmsg_save_to_outbox(outbox, tmp_enc_file, FALSE);
                }
        }
@@ -1802,8 +1745,13 @@ send_mail:
                        }
                        
                        if (msginfo != NULL) {
+                               MsgPermFlags to_unset = 0;
+
+                               if (prefs_common.mark_as_read_on_new_window)
+                                       to_unset = (MSG_NEW|MSG_UNREAD);
+
                                if (replymessageid != NULL) {
-                                       procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0);
+                                       procmsg_msginfo_unset_flags(msginfo, to_unset|MSG_FORWARDED, 0);
                                        procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0);
                                }  else {
                                        procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0);
@@ -1830,9 +1778,9 @@ send_mail:
        return (newsval != 0 ? newsval : mailval);
 }
 
-gint procmsg_send_message_queue(const gchar *file, gchar **errstr, FolderItem *queue, gint msgnum)
+gint procmsg_send_message_queue(const gchar *file, gchar **errstr, FolderItem *queue, gint msgnum, gboolean *queued_removed)
 {
-       gint result = procmsg_send_message_queue_full(file, FALSE, errstr, queue, msgnum);
+       gint result = procmsg_send_message_queue_full(file, FALSE, errstr, queue, msgnum, queued_removed);
        toolbar_main_set_sensitive(mainwindow_get_mainwindow());
        return result;
 }
@@ -2003,7 +1951,7 @@ void procmsg_msginfo_change_flags(MsgInfo *msginfo,
  *
  *\return      gboolean TRUE if perm_flags are found
  */
-gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
+static gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
                MsgPermFlags perm_flags, GHashTable *parentmsgs)
 {
        MsgInfo *tmp;
@@ -2040,7 +1988,7 @@ gboolean procmsg_msg_has_flagged_parent_real(MsgInfo *info,
 /*!
  *\brief       Callback for cleaning up hash of parentmsgs
  */
-gboolean parentmsgs_hash_remove(gpointer key,
+static gboolean parentmsgs_hash_remove(gpointer key,
                             gpointer value,
                             gpointer user_data)
 {
@@ -2075,7 +2023,7 @@ gboolean procmsg_msg_has_marked_parent(MsgInfo *info)
 }
 
 
-GSList *procmsg_find_children_func(MsgInfo *info, 
+static GSList *procmsg_find_children_func(MsgInfo *info, 
                                   GSList *children, GSList *all)
 {
        GSList *cur;
@@ -2101,7 +2049,7 @@ GSList *procmsg_find_children_func(MsgInfo *info,
        return children;
 }
 
-GSList *procmsg_find_children (MsgInfo *info)
+static GSList *procmsg_find_children (MsgInfo *info)
 {
        GSList *children;
        GSList *all, *cur;
@@ -2121,7 +2069,7 @@ GSList *procmsg_find_children (MsgInfo *info)
        return children;
 }
 
-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;
@@ -2165,21 +2113,24 @@ void procmsg_msginfo_set_to_folder(MsgInfo *msginfo, FolderItem *to_folder)
  * \return TRUE if the message was moved and MsgInfo is now invalid,
  *         FALSE otherwise
  */
-gboolean procmsg_msginfo_filter(MsgInfo *msginfo, PrefsAccount* ac_prefs)
+static gboolean procmsg_msginfo_filter(MsgInfo *msginfo, PrefsAccount* ac_prefs)
 {
        MailFilteringData mail_filtering_data;
                        
        mail_filtering_data.msginfo = msginfo;                  
        mail_filtering_data.msglist = NULL;                     
        mail_filtering_data.filtered = NULL;                    
-       mail_filtering_data.unfiltered = NULL;                  
-       if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data)) {
+       mail_filtering_data.unfiltered = NULL;
+       mail_filtering_data.account = ac_prefs; 
+
+       if (!ac_prefs || ac_prefs->filterhook_on_recv)
+               if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data))
                return TRUE;
-       }
 
        /* filter if enabled in prefs or move to inbox if not */
        if((filtering_rules != NULL) &&
-          filter_message_by_msginfo(filtering_rules, msginfo, ac_prefs)) {
+               filter_message_by_msginfo(filtering_rules, msginfo, ac_prefs,
+                               FILTERING_INCORPORATION, NULL)) {
                return TRUE;
        }
                
@@ -2217,7 +2168,9 @@ void procmsg_msglist_filter(GSList *list, PrefsAccount *ac,
        mail_filtering_data.msglist = list;                     
        mail_filtering_data.filtered = NULL;                    
        mail_filtering_data.unfiltered = NULL;  
+       mail_filtering_data.account = ac;       
                        
+       if (!ac || ac->filterhook_on_recv)
        hooks_invoke(MAIL_LISTFILTERING_HOOKLIST, &mail_filtering_data);
        
        if (mail_filtering_data.filtered == NULL &&
@@ -2345,19 +2298,31 @@ int procmsg_spam_learner_learn (MsgInfo *info, GSList *list, gboolean spam)
 }
 
 static gchar *spam_folder_item = NULL;
-void procmsg_spam_set_folder (const char *item_identifier)
+static FolderItem * (*procmsg_spam_get_folder_func)(MsgInfo *msginfo) = NULL;
+void procmsg_spam_set_folder (const char *item_identifier, FolderItem *(*spam_get_folder_func)(MsgInfo *info))
 {
        g_free(spam_folder_item);
        if (item_identifier)
                spam_folder_item = g_strdup(item_identifier);
        else
                spam_folder_item = NULL;
+       if (spam_get_folder_func != NULL)
+               procmsg_spam_get_folder_func = spam_get_folder_func;
+       else
+               procmsg_spam_get_folder_func = NULL;
 }
 
-FolderItem *procmsg_spam_get_folder (void)
+FolderItem *procmsg_spam_get_folder (MsgInfo *msginfo)
 {
-       FolderItem *item = spam_folder_item ? folder_find_item_from_identifier(spam_folder_item) : NULL;
-       return item ? item : folder_get_default_trash();
+       FolderItem *item = NULL;
+       
+       if (procmsg_spam_get_folder_func) 
+               item = procmsg_spam_get_folder_func(msginfo);
+       if (item == NULL && spam_folder_item)
+               item = folder_find_item_from_identifier(spam_folder_item);
+       if (item == NULL)
+               item = folder_get_default_trash();
+       return item;
 }
 
 static void item_has_queued_mails(FolderItem *item, gpointer data)
@@ -2375,3 +2340,19 @@ gboolean procmsg_have_queued_mails_fast (void)
        folder_func_to_all_folders(item_has_queued_mails, &result);
        return result;
 }
+
+static void item_has_trashed_mails(FolderItem *item, gpointer data)
+{
+       gboolean *result = (gboolean *)data;
+       if (*result == TRUE)
+               return;
+       if (folder_has_parent_of_type(item, F_TRASH) && item->total_msgs > 0)
+               *result = TRUE;
+}
+
+gboolean procmsg_have_trashed_mails_fast (void)
+{
+       gboolean result = FALSE;
+       folder_func_to_all_folders(item_has_trashed_mails, &result);
+       return result;
+}