2006-02-06 [colin] 2.0.0cvs21
[claws.git] / src / imap.c
index 2fa20c8d470e3c6492a2f6906eaf3deef72f834e..f637763bcca145bee600dd315ccebe68803476e0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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
@@ -932,7 +932,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                make_dir_hier(path);
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
        g_free(path);
-
+       debug_print("trying to fetch cached %s\n", filename);
        if (is_file_exist(filename)) {
                /* see whether the local file represents the whole message
                 * or not. As the IMAP server reports size with \r chars,
@@ -1069,6 +1069,11 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                                        unlock_session();
                                        return -1;
                                }
+                       }  else if (!(MSG_IS_QUEUED(*fileinfo->flags) 
+                                     || MSG_IS_DRAFT(*fileinfo->flags))
+                                   && (folder_has_parent_of_type(dest, F_QUEUE)
+                                   || folder_has_parent_of_type(dest, F_DRAFT))) {
+                               return -1;
                        } 
                }
                if (real_file == NULL)
@@ -1093,12 +1098,33 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                        statusbar_progress_all(0,0,0);
                        statusbar_pop_all();
                        return -1;
+               } else {
+                       debug_print("appended new message as %d\n", new_uid);
+                       /* put the local file in the imapcache, so that we don't
+                        * have to fetch it back later. */
+                       if (new_uid > 0) {
+                               gchar *cache_path = folder_item_get_path(dest);
+                               if (!is_dir_exist(cache_path))
+                                       make_dir_hier(cache_path);
+                               if (is_dir_exist(cache_path)) {
+                                       gchar *cache_file = g_strconcat(
+                                               cache_path, G_DIR_SEPARATOR_S, 
+                                               itos(new_uid), NULL);
+                                       copy_file(real_file, cache_file, TRUE);
+                                       debug_print("copied to cache: %s\n", cache_file);
+                                       g_free(cache_file);
+                               }
+                               g_free(cache_path);
+                       }
                }
 
                if (relation != NULL)
                        g_relation_insert(relation, fileinfo->msginfo != NULL ? 
                                          (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
                                          GINT_TO_POINTER(dest->last_num + 1));
+               if (new_uid == 0) {
+                       new_uid = dest->last_num+1;
+               }
                if (last_uid < new_uid)
                        last_uid = new_uid;
                if (file_is_tmp)
@@ -1109,6 +1135,7 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
        statusbar_progress_all(0,0,0);
        statusbar_pop_all();
        
+       imap_cmd_expunge(session);
        unlock_session();
        
        g_free(destdir);
@@ -1239,18 +1266,28 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
        msginfo = (MsgInfo *)msglist->data;
        g_return_val_if_fail(msginfo->folder != NULL, -1);
 
+       /* if from/to are the same "type" (with or without extra headers),
+        * copy them via imap */
        if (folder == msginfo->folder->folder &&
            !folder_has_parent_of_type(msginfo->folder, F_DRAFT) &&
-           !folder_has_parent_of_type(msginfo->folder, F_QUEUE)) {
+           !folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
+           !folder_has_parent_of_type(dest, F_DRAFT) &&
+           !folder_has_parent_of_type(dest, F_QUEUE)) {
+               ret = imap_do_copy_msgs(folder, dest, msglist, relation);
+               return ret;
+       } else if (folder == msginfo->folder->folder &&
+           (folder_has_parent_of_type(msginfo->folder, F_DRAFT) ||
+            folder_has_parent_of_type(msginfo->folder, F_QUEUE)) && 
+           (folder_has_parent_of_type(dest, F_DRAFT) ||
+            folder_has_parent_of_type(dest, F_QUEUE))) {
                ret = imap_do_copy_msgs(folder, dest, msglist, relation);
                return ret;
        }
-
+       /* else reupload them */
        file_list = procmsg_get_message_file_list(msglist);
        g_return_val_if_fail(file_list != NULL, -1);
 
        ret = imap_add_msgs(folder, dest, file_list, relation);
-
        procmsg_message_file_list_free(file_list);
 
        return ret;
@@ -2321,45 +2358,6 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        
        real_path = imap_get_real_path(folder, path);
 
-#if 0
-       if (time(NULL) - item->last_update >= 5 && item->last_update != 1) {
-               /* do the full stuff */
-               item->last_update = 1; /* force update */
-               debug_print("updating everything\n");
-               r = imap_status(session, folder, path, item,
-               &item->c_messages, &item->c_uid_next,
-               &item->c_uid_validity, &item->c_unseen, block);
-               if (r != MAILIMAP_NO_ERROR) {
-                       debug_print("status err %d\n", r);
-                       return IMAP_ERROR;
-               }
-               item->last_update = time(NULL);
-               if (messages) 
-                       *messages = item->c_messages;
-               if (uid_next)
-                       *uid_next = item->c_uid_next;
-               if (uid_validity)
-                       *uid_validity = item->c_uid_validity;
-               if (unseen)
-                       *unseen = item->c_unseen;
-               return 0;
-       } else if (time(NULL) - item->last_update < 5) {
-               /* return cached stuff */
-               debug_print("using cache\n");
-               if (messages) 
-                       *messages = item->c_messages;
-               if (uid_next)
-                       *uid_next = item->c_uid_next;
-               if (uid_validity)
-                       *uid_validity = item->c_uid_validity;
-               if (unseen)
-                       *unseen = item->c_unseen;
-               return 0;
-       }
-#endif
-
-       /* if we get there, we're updating cache */
-
        if (messages) {
                mask |= 1 << 0;
        }
@@ -3108,7 +3106,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->item.path));
-       if (selected_folder) {
+       if (selected_folder && time(NULL) - item->use_cache < 2) {
                ok = imap_cmd_noop(session);
                if (ok != IMAP_SUCCESS) {
                        debug_print("disconnected!\n");
@@ -3133,6 +3131,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                        ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, item,
                                 &exists, &uid_next, &uid_val, NULL, FALSE);
                }
+               item->item.last_num = uid_next - 1;
+               
                item->use_cache = (time_t)0;
                if (ok != IMAP_SUCCESS) {
                        statusbar_pop_all();
@@ -3155,13 +3155,11 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                }
        }
 
-       if (!selected_folder)
-               item->uid_next = uid_next;
-
        /* If old uid_next matches new uid_next we can be sure no message
           was added to the folder */
-       if (( selected_folder && !session->folder_content_changed) ||
-           (!selected_folder && uid_next == item->uid_next)) {
+       debug_print("uid_next is %d and item->uid_next %d \n", 
+               uid_next, item->uid_next);
+       if (uid_next == item->uid_next) {
                nummsgs = g_slist_length(item->uid_list);
 
                /* If number of messages is still the same we
@@ -3170,6 +3168,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                   we discard our cache to start a new scan to find
                   out which numbers have been removed */
                if (exists == nummsgs) {
+                       debug_print("exists == nummsgs\n");
                        *msgnum_list = g_slist_copy(item->uid_list);
                        statusbar_pop_all();
                        unlock_session();
@@ -3219,6 +3218,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        remove_numbered_files_not_in_list(dir, *msgnum_list);
        g_free(dir);
        
+       item->uid_next = uid_next;
+       
        debug_print("get_num_list - ok - %i\n", nummsgs);
        statusbar_pop_all();
        unlock_session();
@@ -3362,7 +3363,7 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
        lock_session();
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->item.path));
-       if (selected_folder) {
+       if (selected_folder && time(NULL) - item->use_cache < 2) {
                ok = imap_cmd_noop(session);
                if (ok != IMAP_SUCCESS) {
                        debug_print("disconnected!\n");
@@ -3390,7 +3391,9 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                item->c_uid_next = uid_next;
                item->c_uid_validity = uid_val;
                item->c_unseen = unseen;
-
+               item->item.last_num = uid_next - 1;
+               debug_print("uidnext %d, item->uid_next %d, exists %d, item->item.total_msgs %d\n", 
+                       uid_next, item->uid_next, exists, item->item.total_msgs);
                if ((uid_next != item->uid_next) || (exists != item->item.total_msgs)) {
                        unlock_session();
                        return TRUE;