2009-01-11 [colin] 3.7.0cvs30
[claws.git] / src / folder.c
index 711d4dfde8c620abe2ebc36c6dfaae98dfc102a7..2a7c9320d047efac446787df26f10b904fac2e90 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2009 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
@@ -136,9 +136,10 @@ void folder_register_class(FolderClass *klass)
                                Folder *folder;
 
                                folder = folder_get_from_xml(node);
-                               folder_add(folder);
-                               folder_unloaded_list = g_slist_remove(folder_unloaded_list, node);
-
+                               if (folder) {
+                                       folder_add(folder);
+                                       folder_unloaded_list = g_slist_remove(folder_unloaded_list, node);
+                               }
                                cur = NULL;
                                continue;
                        }
@@ -351,6 +352,11 @@ FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path
        item->unreadmarked_msgs = 0;
        item->marked_msgs = 0;
        item->total_msgs = 0;
+       item->replied_msgs = 0;
+       item->forwarded_msgs = 0;
+       item->locked_msgs = 0;
+       item->ignored_msgs = 0;
+       item->watched_msgs = 0;
        item->order = 0;
        item->last_num = -1;
        item->cache = NULL;
@@ -392,6 +398,7 @@ void folder_item_remove(FolderItem *item)
 {
        GNode *node, *start_node;
        FolderUpdateData hookdata;
+       gchar *tags_file = NULL;
 
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->folder != NULL);
@@ -418,6 +425,10 @@ void folder_item_remove(FolderItem *item)
                msgcache_destroy(item->cache);
                item->cache = NULL;
        }
+       tags_file = folder_item_get_tags_file(item);
+       if (tags_file)
+               claws_unlink(tags_file);
+       g_free(tags_file);
 
        hookdata.folder = item->folder;
        hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_REMOVE_FOLDERITEM;
@@ -533,6 +544,16 @@ void folder_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
                        item->unreadmarked_msgs = atoi(attr->value);
                else if (!strcmp(attr->name, "marked"))
                        item->marked_msgs = atoi(attr->value);
+               else if (!strcmp(attr->name, "replied"))
+                       item->replied_msgs = atoi(attr->value);
+               else if (!strcmp(attr->name, "forwarded"))
+                       item->forwarded_msgs = atoi(attr->value);
+               else if (!strcmp(attr->name, "locked"))
+                       item->locked_msgs = atoi(attr->value);
+               else if (!strcmp(attr->name, "ignored"))
+                       item->ignored_msgs = atoi(attr->value);
+               else if (!strcmp(attr->name, "watched"))
+                       item->watched_msgs = atoi(attr->value);
                else if (!strcmp(attr->name, "order"))
                        item->order = atoi(attr->value);
                else if (!strcmp(attr->name, "total"))
@@ -648,6 +669,11 @@ XMLTag *folder_item_get_xml(Folder *folder, FolderItem *item)
        xml_tag_add_attr(tag, xml_attr_new_int("unreadmarked", item->unreadmarked_msgs));
        xml_tag_add_attr(tag, xml_attr_new_int("marked", item->marked_msgs));
        xml_tag_add_attr(tag, xml_attr_new_int("total", item->total_msgs));
+       xml_tag_add_attr(tag, xml_attr_new_int("replied", item->replied_msgs));
+       xml_tag_add_attr(tag, xml_attr_new_int("forwarded", item->forwarded_msgs));
+       xml_tag_add_attr(tag, xml_attr_new_int("locked", item->locked_msgs));
+       xml_tag_add_attr(tag, xml_attr_new_int("ignore", item->ignored_msgs));
+       xml_tag_add_attr(tag, xml_attr_new_int("watched", item->watched_msgs));
        xml_tag_add_attr(tag, xml_attr_new_int("order", item->order));
 
        if (item->account)
@@ -727,6 +753,12 @@ void folder_add(Folder *folder)
 
        g_return_if_fail(folder != NULL);
 
+       if ((FOLDER_TYPE(folder) == F_IMAP ||
+            FOLDER_TYPE(folder) == F_NEWS) &&
+           folder->account == NULL) {
+               return;
+       }
+
        for (i = 0, cur = folder_list; cur != NULL; cur = cur->next, i++) {
                cur_folder = FOLDER(cur->data);
                if (cur_folder->sort < folder->sort)
@@ -932,6 +964,8 @@ void folder_fast_scan_tree(Folder *folder)
 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
 {
        FolderItem *new_item;
+       
+       g_return_val_if_fail(parent != NULL, NULL);
 
        new_item = parent->folder->klass->create_folder(parent->folder, parent, name);
        if (new_item) {
@@ -982,6 +1016,11 @@ struct TotalMsgCount
        guint unreadmarked_msgs;
        guint marked_msgs;
        guint total_msgs;
+       guint replied_msgs;
+       guint forwarded_msgs;
+       guint locked_msgs;
+       guint ignored_msgs;
+       guint watched_msgs;
 };
 
 struct FuncToAllFoldersData
@@ -1033,6 +1072,11 @@ static void folder_count_total_msgs_func(FolderItem *item, gpointer data)
        count->unreadmarked_msgs += item->unreadmarked_msgs;
        count->marked_msgs += item->marked_msgs;
        count->total_msgs += item->total_msgs;
+       count->replied_msgs += item->replied_msgs;
+       count->forwarded_msgs += item->forwarded_msgs;
+       count->locked_msgs += item->locked_msgs;
+       count->ignored_msgs += item->ignored_msgs;
+       count->watched_msgs += item->watched_msgs;
 }
 
 struct TotalMsgStatus
@@ -1144,11 +1188,16 @@ gchar *folder_get_status(GPtrArray *folders, gboolean full)
 
 void folder_count_total_msgs(guint *new_msgs, guint *unread_msgs, 
                             guint *unreadmarked_msgs, guint *marked_msgs,
-                            guint *total_msgs)
+                            guint *total_msgs, guint *replied_msgs,
+                            guint *forwarded_msgs, guint *locked_msgs,
+                            guint *ignored_msgs, guint *watched_msgs)
 {
        struct TotalMsgCount count;
 
-       count.new_msgs = count.unread_msgs = count.unreadmarked_msgs = count.total_msgs = 0;
+       count.new_msgs = count.unread_msgs = count.unreadmarked_msgs = 0;
+       count.total_msgs = count.replied_msgs = count.forwarded_msgs = 0;
+       count.locked_msgs = count.ignored_msgs = count.watched_msgs = 0;
+       count.marked_msgs = 0;
 
        debug_print("Counting total number of messages...\n");
 
@@ -1159,6 +1208,11 @@ void folder_count_total_msgs(guint *new_msgs, guint *unread_msgs,
        *unreadmarked_msgs = count.unreadmarked_msgs;
        *marked_msgs = count.marked_msgs;
        *total_msgs = count.total_msgs;
+       *replied_msgs = count.replied_msgs;
+       *forwarded_msgs = count.forwarded_msgs;
+       *locked_msgs = count.locked_msgs;
+       *ignored_msgs = count.ignored_msgs;
+       *watched_msgs = count.watched_msgs;
 }
 
 Folder *folder_find_from_path(const gchar *path)
@@ -1227,20 +1281,53 @@ FolderItem *folder_find_item_from_path(const gchar *path)
        return d[1];
 }
 
+static gboolean folder_item_find_func_real_path(GNode *node, gpointer data)
+{
+       FolderItem *item = node->data;
+       gpointer *d = data;
+       const gchar *path = d[0];
+       gchar *tmp = folder_item_get_path(item);
+       if (path_cmp(path, tmp) != 0) {
+               g_free(tmp);
+               return FALSE;
+       }
+       g_free(tmp);
+       d[1] = item;
+
+       return TRUE;
+}
+
+FolderItem *folder_find_item_from_real_path(const gchar *path)
+{
+       Folder *folder;
+       gpointer d[2];
+       GList *list = folder_get_list();
+       
+       folder = list ? list->data:NULL;
+       
+       g_return_val_if_fail(folder != NULL, NULL);
+
+       d[0] = (gpointer)path;
+       d[1] = NULL;
+       while (d[1] == NULL && list) {
+               folder = FOLDER(list->data);
+               g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+                       folder_item_find_func_real_path, d);
+               list = list->next;
+       }
+       return d[1];
+}
+
 FolderItem *folder_find_child_item_by_name(FolderItem *item, const gchar *name)
 {
        GNode *node;
        FolderItem *child;
 
        for (node = item->node->children; node != NULL; node = node->next) {
-               gchar *base;
                child = FOLDER_ITEM(node->data);
-               base = g_path_get_basename(child->path);
-               if (strcmp2(base, name) == 0) {
-                       g_free(base);
+               if (strcmp2(child->name, name) == 0) {
                        return child;
                }
-               g_free(base);
        }
 
        return NULL;
@@ -1260,7 +1347,7 @@ FolderClass *folder_get_class_from_string(const gchar *str)
        return NULL;
 }
 
-static gchar *folder_get_identifier(Folder *folder)
+gchar *folder_get_identifier(Folder *folder)
 {
        gchar *type_str;
 
@@ -1924,6 +2011,9 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
        GSList *newmsg_list = NULL;
        guint newcnt = 0, unreadcnt = 0, totalcnt = 0;
        guint markedcnt = 0, unreadmarkedcnt = 0;
+       guint repliedcnt = 0, forwardedcnt = 0;
+       guint lockedcnt = 0, ignoredcnt = 0, watchedcnt = 0;
+
        guint cache_max_num, folder_max_num, cache_cur_num, folder_cur_num;
        gboolean update_flags = 0, old_uids_valid = FALSE;
        GHashTable *subject_table = NULL;
@@ -2025,7 +2115,8 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
                        }
 
                        /* Move to next folder number */
-                       folder_list_cur = folder_list_cur->next;
+                       if (folder_list_cur)
+                               folder_list_cur = folder_list_cur->next;
 
                        if (folder_list_cur != NULL)
                                folder_cur_num = GPOINTER_TO_INT(folder_list_cur->data);
@@ -2044,7 +2135,8 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
                        debug_print("Removed message %d from cache.\n", cache_cur_num);
 
                        /* Move to next cache number */
-                       cache_list_cur = cache_list_cur->next;
+                       if (cache_list_cur)
+                               cache_list_cur = cache_list_cur->next;
 
                        if (cache_list_cur != NULL)
                                cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
@@ -2081,8 +2173,11 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
                        }
                        
                        /* Move to next folder and cache number */
-                       cache_list_cur = cache_list_cur->next;
-                       folder_list_cur = folder_list_cur->next;
+                       if (cache_list_cur)
+                               cache_list_cur = cache_list_cur->next;
+                       
+                       if (folder_list_cur)
+                               folder_list_cur = folder_list_cur->next;
 
                        if (cache_list_cur != NULL)
                                cache_cur_num = ((MsgInfo *)cache_list_cur->data)->msgnum;
@@ -2219,6 +2314,16 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
                        unreadmarkedcnt++;
                if (MSG_IS_MARKED(msginfo->flags))
                        markedcnt++;
+               if (MSG_IS_REPLIED(msginfo->flags))
+                       repliedcnt++;
+               if (MSG_IS_FORWARDED(msginfo->flags))
+                       forwardedcnt++;
+               if (MSG_IS_LOCKED(msginfo->flags))
+                       lockedcnt++;
+               if (MSG_IS_IGNORE_THREAD(msginfo->flags))
+                       ignoredcnt++;
+               if (MSG_IS_WATCH_THREAD(msginfo->flags))
+                       watchedcnt++;
 
                totalcnt++;
 
@@ -2233,7 +2338,10 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
        
        if (item->new_msgs != newcnt || item->unread_msgs != unreadcnt
        ||  item->total_msgs != totalcnt || item->marked_msgs != markedcnt
-       ||  item->unreadmarked_msgs != unreadmarkedcnt) {
+       ||  item->unreadmarked_msgs != unreadmarkedcnt
+       ||  item->replied_msgs != repliedcnt || item->forwarded_msgs != forwardedcnt
+       ||  item->locked_msgs != lockedcnt || item->ignored_msgs != ignoredcnt
+       ||  item->watched_msgs != watchedcnt) {
                update_flags |= F_ITEM_UPDATE_CONTENT;
        }
 
@@ -2242,6 +2350,11 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
        item->total_msgs = totalcnt;
        item->unreadmarked_msgs = unreadmarkedcnt;
        item->marked_msgs = markedcnt;
+       item->replied_msgs = repliedcnt;
+       item->forwarded_msgs = forwardedcnt;
+       item->locked_msgs = lockedcnt;
+       item->ignored_msgs = ignoredcnt;
+       item->watched_msgs = watchedcnt;
 
        update_flags |= F_ITEM_UPDATE_MSGCNT;
 
@@ -2405,6 +2518,9 @@ static void folder_item_read_cache(FolderItem *item)
                        MsgInfoList *list, *cur;
                        guint newcnt = 0, unreadcnt = 0;
                        guint markedcnt = 0, unreadmarkedcnt = 0;
+                       guint repliedcnt = 0, forwardedcnt = 0;
+                       guint lockedcnt = 0, ignoredcnt = 0;
+                       guint watchedcnt = 0;
                        MsgInfo *msginfo;
 
                        item->cache = msgcache_new();
@@ -2424,12 +2540,27 @@ static void folder_item_read_cache(FolderItem *item)
                                        unreadmarkedcnt++;
                                if (MSG_IS_MARKED(msginfo->flags))
                                        markedcnt++;
+                               if (MSG_IS_REPLIED(msginfo->flags))
+                                       repliedcnt++;
+                               if (MSG_IS_FORWARDED(msginfo->flags))
+                                       forwardedcnt++;
+                               if (MSG_IS_LOCKED(msginfo->flags))
+                                       lockedcnt++;
+                               if (MSG_IS_IGNORE_THREAD(msginfo->flags))
+                                       ignoredcnt++;
+                               if (MSG_IS_WATCH_THREAD(msginfo->flags))
+                                       watchedcnt++;
                                procmsg_msginfo_unset_flags(msginfo, MSG_FULLY_CACHED, 0);
                        }
                        item->new_msgs = newcnt;
                        item->unread_msgs = unreadcnt;
                        item->unreadmarked_msgs = unreadmarkedcnt;
                        item->marked_msgs = markedcnt;
+                       item->replied_msgs = repliedcnt;
+                       item->forwarded_msgs = forwardedcnt;
+                       item->locked_msgs = lockedcnt;
+                       item->ignored_msgs = ignoredcnt;
+                       item->watched_msgs = watchedcnt;
                        procmsg_msg_list_free(list);
                } else
                        msgcache_read_mark(item->cache, mark_file);
@@ -2459,7 +2590,10 @@ void folder_item_write_cache(FolderItem *item)
                return;
 
        last_mtime = item->mtime;
-       need_scan = item->folder->klass->scan_required(item->folder, item);
+       if (item->folder->klass->scan_required)
+               need_scan = item->folder->klass->scan_required(item->folder, item);
+       else
+               need_scan = TRUE;
 
        id = folder_item_get_identifier(item);
        debug_print("Save cache for folder %s\n", id);
@@ -2787,6 +2921,16 @@ static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo
                item->unreadmarked_msgs++;
        if (MSG_IS_MARKED(newmsginfo->flags))
                item->marked_msgs++;
+       if (MSG_IS_REPLIED(newmsginfo->flags))
+               item->replied_msgs++;
+       if (MSG_IS_FORWARDED(newmsginfo->flags))
+               item->forwarded_msgs++;
+       if (MSG_IS_LOCKED(newmsginfo->flags))
+               item->locked_msgs++;
+       if (MSG_IS_IGNORE_THREAD(newmsginfo->flags))
+               item->ignored_msgs++;
+       if (MSG_IS_WATCH_THREAD(newmsginfo->flags))
+               item->watched_msgs++;
        item->total_msgs++;
 
        folder_item_update_freeze();
@@ -2815,6 +2959,16 @@ static void remove_msginfo_from_cache(FolderItem *item, MsgInfo *msginfo)
                msginfo->folder->unreadmarked_msgs--;
        if (MSG_IS_MARKED(msginfo->flags))
                item->marked_msgs--;
+       if (MSG_IS_REPLIED(msginfo->flags))
+               item->replied_msgs--;
+       if (MSG_IS_FORWARDED(msginfo->flags))
+               item->forwarded_msgs--;
+       if (MSG_IS_LOCKED(msginfo->flags))
+               item->locked_msgs--;
+       if (MSG_IS_IGNORE_THREAD(msginfo->flags))
+               item->ignored_msgs--;
+       if (MSG_IS_WATCH_THREAD(msginfo->flags))
+               item->watched_msgs--;
 
        msginfo->folder->total_msgs--;
 
@@ -2908,7 +3062,7 @@ gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
                                lastnum = num;
 
                        if (num >= 0 && remove_source) {
-                               if (g_unlink(fileinfo->file) < 0)
+                               if (claws_unlink(fileinfo->file) < 0)
                                        FILE_OP_ERROR(fileinfo->file, "unlink");
                        }
 
@@ -3241,6 +3395,10 @@ static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_sour
        statusbar_print_all(_("Updating cache for %s..."), dest->path ? dest->path : "(null)");
        total = g_slist_length(msglist);
        
+       if (FOLDER_TYPE(dest->folder) == F_IMAP && total > 1) {
+               folder_item_scan_full(dest, FALSE);
+               folderscan = TRUE;
+       }
        folder_item_set_batch(dest, TRUE);
        for (l = msglist; l != NULL; l = g_slist_next(l)) {
                MsgInfo *msginfo = (MsgInfo *) l->data;
@@ -3421,6 +3579,7 @@ gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
 {
        Folder *folder;
        gint ret = 0;
+       GSList *real_list = NULL, *cur = NULL;
 
        g_return_val_if_fail(item != NULL, -1);
        folder = item->folder;
@@ -3431,24 +3590,35 @@ gint folder_item_remove_msgs(FolderItem *item, GSList *msglist)
        if (!item->cache) folder_item_read_cache(item);
 
        folder_item_update_freeze();
+       
+       /* filter out locked mails */
+       for (cur = msglist; cur; cur = cur->next) {
+               MsgInfo *info = (MsgInfo *)cur->data;
+               if (!MSG_IS_LOCKED(info->flags))
+                       real_list = g_slist_prepend(real_list, info);
+       }
+
+       real_list = g_slist_reverse(real_list);
+
        if (item->folder->klass->remove_msgs) {
                ret = item->folder->klass->remove_msgs(item->folder,
                                                        item,
-                                                       msglist,
+                                                       real_list,
                                                        NULL);
        }
-       while (ret == 0 && msglist != NULL) {
-               MsgInfo *msginfo = (MsgInfo *)msglist->data;
+       while (ret == 0 && real_list != NULL) {
+               MsgInfo *msginfo = (MsgInfo *)real_list->data;
                if (msginfo && MSG_IS_LOCKED(msginfo->flags)) {
-                       msglist = msglist->next;
+                       real_list = real_list->next;
                        continue;
                }
                if (!item->folder->klass->remove_msgs)
                        ret = folder_item_remove_msg(item, msginfo->msgnum);
                if (ret != 0) break;
                msgcache_remove_msg(item->cache, msginfo->msgnum);
-               msglist = msglist->next;
+               real_list = real_list->next;
        }
+       g_slist_free(real_list);
        folder_item_scan_full(item, FALSE);
        folder_item_update_thaw();
        inc_unlock();
@@ -3488,6 +3658,11 @@ gint folder_item_remove_all_msg(FolderItem *item)
                item->unreadmarked_msgs = 0;
                item->marked_msgs = 0;
                item->total_msgs = 0;
+               item->replied_msgs = 0;
+               item->forwarded_msgs = 0;
+               item->locked_msgs = 0;
+               item->ignored_msgs = 0;
+               item->watched_msgs = 0;
                folder_item_update(item, F_ITEM_UPDATE_MSGCNT | F_ITEM_UPDATE_CONTENT);
        }
 
@@ -3564,7 +3739,7 @@ void folder_item_discard_cache(FolderItem *item)
        
        cache = folder_item_get_cache_file(item);
        if (is_file_exist(cache))
-               g_unlink(cache);
+               claws_unlink(cache);
        g_free(cache);
        
 }