2007-04-26 [colin] 2.9.1cvs28
[claws.git] / src / imap.c
index d199dc3bda4d4df406f80a1fa90cba81200cec68..43656a1add4017b5cc35b6b0073552981b2658c0 100644 (file)
@@ -110,6 +110,7 @@ struct _IMAPSession
        guint exists;
        Folder * folder;
        gboolean busy;
+       gboolean cancelled;
 };
 
 struct _IMAPNameSpace
@@ -168,6 +169,8 @@ struct _IMAPFolderItem
 
        GHashTable *flags_set_table;
        GHashTable *flags_unset_table;
+       guint32 last_change;
+       guint32 last_sync;
 };
 
 static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
@@ -301,6 +304,7 @@ static gchar *imap_get_real_path            (IMAPSession    *session,
                                                 IMAPFolder     *folder,
                                                 const gchar    *path);
 static void imap_synchronise           (FolderItem     *item);
+static gboolean imap_is_busy           (Folder *folder);
 
 static void imap_free_capabilities     (IMAPSession    *session);
 
@@ -684,24 +688,40 @@ static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *sess
        return session;
 }
 
-#define lock_session() {\
-       if (session) { \
-               debug_print("locking session %p (%d)\n", session, session->busy); \
-               if (session->busy) \
-                       debug_print("         SESSION WAS LOCKED !!      \n"); \
-               session->busy = TRUE;\
-       } else {\
-               debug_print("can't lock null session\n"); \
-       }\
+static void lock_session(IMAPSession *session)
+{
+       if (session) {
+                MainWindow *mainwin;
+               
+               debug_print("locking session %p (%d)\n", session, session->busy);
+               if (session->busy)
+                       debug_print("         SESSION WAS LOCKED !!      \n");
+                session->busy = TRUE;
+               mainwin = mainwindow_get_mainwindow();
+               if (mainwin) {
+                       toolbar_main_set_sensitive(mainwin);
+                       main_window_set_menu_sensitive(mainwin);
+               }
+       } else {
+               debug_print("can't lock null session\n");
+       }
 }
 
-#define unlock_session() {\
-       if (session) { \
-               debug_print("unlocking session %p\n", session); \
-               session->busy = FALSE;\
-       } else {\
-               debug_print("can't unlock null session\n"); \
-       }\
+static void unlock_session(IMAPSession *session)
+{
+       if (session) {
+                MainWindow *mainwin;
+               
+               debug_print("unlocking session %p\n", session);
+               session->busy = FALSE;
+               mainwin = mainwindow_get_mainwindow();
+               if (mainwin) {
+                       toolbar_main_set_sensitive(mainwin);
+                       main_window_set_menu_sensitive(mainwin);
+                }
+       } else {
+               debug_print("can't unlock null session\n");
+       }
 }
 
 static IMAPSession *imap_session_get(Folder *folder)
@@ -714,7 +734,7 @@ static IMAPSession *imap_session_get(Folder *folder)
        g_return_val_if_fail(folder->account != NULL, NULL);
        
        if (prefs_common.work_offline && 
-           !inc_offline_should_override(
+           !inc_offline_should_override(FALSE,
                _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                return NULL;
@@ -753,7 +773,7 @@ static IMAPSession *imap_session_get(Folder *folder)
                return NULL;
        }
 
-       lock_session();
+       lock_session(session);
 
        /* I think the point of this code is to avoid sending a
         * keepalive if we've used the session recently and therefore
@@ -763,12 +783,14 @@ static IMAPSession *imap_session_get(Folder *folder)
         * A better solution than sending a NOOP every time would be
         * for every command to be prepared to retry until it is
         * successfully sent. -- mbp */
-       if (time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) {
+       if ((time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) || session->cancelled) {
                /* verify that the session is still alive */
                if (imap_cmd_noop(session) != IMAP_SUCCESS) {
                        debug_print("disconnected!\n");
                        session = imap_reconnect_if_possible(folder, session);
                }
+               
+               session->cancelled = FALSE;
        }
 
        rfolder->session = SESSION(session);
@@ -959,22 +981,22 @@ static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
        return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
 }
 
-static guint get_size_with_crs(MsgInfo *info
+static guint get_file_size_with_crs(const gchar *filename
 {
        FILE *fp = NULL;
        guint cnt = 0;
        gchar buf[4096];
        
-       if (info == NULL)
+       if (filename == NULL)
                return -1;
        
-       fp = procmsg_open_message(info);
+       fp = fopen(filename, "rb");
        if (!fp)
                return -1;
        
        while (fgets(buf, sizeof (buf), fp) != NULL) {
                cnt += strlen(buf);
-               if (!strstr(buf, "\r") && strstr(buf, "\n"))
+               if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
                        cnt++;
        }
        
@@ -1005,9 +1027,8 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                /* see whether the local file represents the whole message
                 * or not. As the IMAP server reports size with \r chars,
                 * we have to update the local file (UNIX \n only) size */
-               MsgInfo *msginfo = imap_parse_msg(filename, item);
                MsgInfo *cached = msgcache_get_msg(item->cache,uid);
-               guint have_size = get_size_with_crs(msginfo);
+               guint have_size = get_file_size_with_crs(filename);
 
                if (cached)
                        debug_print("message %d has been already %scached (%d/%d).\n", uid,
@@ -1016,17 +1037,14 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                
                if (cached && (cached->size <= have_size || !body)) {
                        procmsg_msginfo_free(cached);
-                       procmsg_msginfo_free(msginfo);
                        file_strip_crs(filename);
                        return filename;
                } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
                        debug_print("message not cached and file recent, considering file complete\n");
-                       procmsg_msginfo_free(msginfo);
                        file_strip_crs(filename);
                        return filename;
                } else {
                        procmsg_msginfo_free(cached);
-                       procmsg_msginfo_free(msginfo);
                }
        }
 
@@ -1044,7 +1062,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
        if (ok != IMAP_SUCCESS) {
                g_warning("can't select mailbox %s\n", item->path);
                g_free(filename);
-               unlock_session();
+               unlock_session(session);
                return NULL;
        }
 
@@ -1054,15 +1072,64 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
        if (ok != IMAP_SUCCESS) {
                g_warning("can't fetch message %d\n", uid);
                g_free(filename);
-               unlock_session();
+               unlock_session(session);
                return NULL;
        }
 
-       unlock_session();
+       unlock_session(session);
        file_strip_crs(filename);
        return filename;
 }
 
+static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
+{
+       gchar *path, *filename;
+       guint size = 0;
+       MsgInfo *cached = msgcache_get_msg(item->cache,uid);
+       
+       if (!cached)
+               return FALSE;
+
+       path = folder_item_get_path(item);
+       if (!is_dir_exist(path))
+               return FALSE;
+
+       filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
+       g_free(path);
+       if (is_file_exist(filename)) {
+               if (cached && cached->total_size == cached->size) {
+                       /* fast path */
+                       g_free(filename);
+                       return TRUE;
+               }
+               size = get_file_size_with_crs(filename);
+               g_free(filename);
+       }
+       if (cached && size >= cached->size) {
+               cached->total_size = cached->size;
+               procmsg_msginfo_free(cached);
+               return TRUE;
+       }
+       if (cached)
+               procmsg_msginfo_free(cached);
+       return FALSE;   
+}
+
+void imap_cache_msg(FolderItem *item, gint msgnum)
+{
+       Folder *folder = NULL;
+       
+       if (!item)
+               return;
+       folder = item->folder;
+       
+       if (!imap_is_msg_fully_cached(folder, item, msgnum)) {
+               gchar *tmp = imap_fetch_msg_full(folder, item, msgnum, TRUE, TRUE);
+               debug_print("fetched %s\n", tmp);
+               g_free(tmp);
+       }
+}
+
 static gint imap_add_msg(Folder *folder, FolderItem *dest, 
                         const gchar *file, MsgFlags *flags)
 {
@@ -1141,7 +1208,7 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                        g_warning("can't append message %s\n", real_file);
                        g_free(real_file);
                        g_free(destdir);
-                       unlock_session();
+                       unlock_session(session);
                        statusbar_progress_all(0,0,0);
                        statusbar_pop_all();
                        return -1;
@@ -1182,7 +1249,7 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
        statusbar_pop_all();
        
        imap_cmd_expunge(session);
-       unlock_session();
+       unlock_session(session);
        
        g_free(destdir);
 
@@ -1219,7 +1286,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        src = msginfo->folder;
        if (src == dest) {
                g_warning("the src folder is identical to the dest.\n");
-               unlock_session();
+               unlock_session(session);
                return -1;
        }
 
@@ -1234,7 +1301,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                        infolist = g_slist_prepend(infolist, fileinfo);
                }
                infolist = g_slist_reverse(infolist);
-               unlock_session();
+               unlock_session(session);
                res = folder_item_add_msgs(dest, infolist, FALSE);
                for (cur = infolist; cur; cur = cur->next) {
                        MsgFileInfo *info = (MsgFileInfo *)cur->data;
@@ -1248,7 +1315,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
                         NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
-               unlock_session();
+               unlock_session(session);
                return ok;
        }
 
@@ -1293,7 +1360,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                if (ok != IMAP_SUCCESS) {
                        g_relation_destroy(uid_mapping);
                        imap_lep_set_free(seq_list);
-                       unlock_session();
+                       unlock_session(session);
                        statusbar_pop_all();
                        return -1;
                }
@@ -1353,7 +1420,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
        IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
 
-       unlock_session();
+       unlock_session(session);
        if (ok == IMAP_SUCCESS)
                return last_num;
        else
@@ -1415,7 +1482,7 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
        ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
                         NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
-               unlock_session();
+               unlock_session(session);
                return ok;
        }
 
@@ -1431,17 +1498,16 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
        g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
 
        ok = imap_set_message_flags
-               (IMAP_SESSION(REMOTE_FOLDER(folder)->session),
-               numlist, IMAP_FLAG_DELETED, TRUE);
+               (session, numlist, IMAP_FLAG_DELETED, TRUE);
        if (ok != IMAP_SUCCESS) {
                log_warning(LOG_PROTOCOL, _("can't set deleted flags\n"));
-               unlock_session();
+               unlock_session(session);
                return ok;
        }
        ok = imap_cmd_expunge(session);
        if (ok != IMAP_SUCCESS) {
                log_warning(LOG_PROTOCOL, _("can't expunge\n"));
-               unlock_session();
+               unlock_session(session);
                return ok;
        }
        
@@ -1458,7 +1524,7 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
        g_slist_free(numlist);
 
        g_free(destdir);
-       unlock_session();
+       unlock_session(session);
        if (ok == IMAP_SUCCESS)
                return 0;
        else
@@ -1527,7 +1593,7 @@ gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
                int r;
                clist * lep_list;
 
-               Xstrdup_a(root_folder, folder->account->imap_dir, {unlock_session();return -1;});
+               Xstrdup_a(root_folder, folder->account->imap_dir, {unlock_session(session);return -1;});
                extract_quote(root_folder, '"');
                subst_char(root_folder,
                           imap_get_path_separator(session, IMAP_FOLDER(folder),
@@ -1548,7 +1614,7 @@ gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
                                item->folder = folder;
                                folder->node = item->node = g_node_new(item);
                        }
-                       unlock_session();
+                       unlock_session(session);
                        return -1;
                }
                mailimap_list_result_free(lep_list);
@@ -1573,7 +1639,7 @@ gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
 
        imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
        imap_create_missing_folders(folder);
-       unlock_session();
+       unlock_session(session);
 
        return 0;
 }
@@ -1975,7 +2041,7 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        else if (folder->account->imap_dir && *folder->account->imap_dir) {
                gchar *imap_dir;
 
-               Xstrdup_a(imap_dir, folder->account->imap_dir, {unlock_session();return NULL;});
+               Xstrdup_a(imap_dir, folder->account->imap_dir, {unlock_session(session);return NULL;});
                strtailchomp(imap_dir, '/');
                dirpath = g_strconcat(imap_dir, "/", name, NULL);
        } else
@@ -1990,7 +2056,7 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        strtailchomp(dirpath, '/');
        Xstrdup_a(new_name, name, {
                g_free(dirpath); 
-               unlock_session();               
+               unlock_session(session);                
                return NULL;});
 
        separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path);
@@ -2011,7 +2077,7 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
                        g_free(dirpath);
                        ptr_array_free_strings(argbuf);
                        g_ptr_array_free(argbuf, TRUE);
-                       unlock_session();
+                       unlock_session(session);
                        return NULL;
                }
                
@@ -2025,7 +2091,7 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
                                log_warning(LOG_PROTOCOL, _("can't create mailbox\n"));
                                g_free(imap_path);
                                g_free(dirpath);
-                               unlock_session();
+                               unlock_session(session);
                                return NULL;
                        }
                        r = imap_threaded_list(folder, "", imap_path, &lep_list);
@@ -2071,7 +2137,7 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        if (!is_dir_exist(dirpath))
                make_dir_hier(dirpath);
        g_free(dirpath);
-       unlock_session();
+       unlock_session(session);
 
        if (exist) {
                /* folder existed, scan it */
@@ -2111,7 +2177,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path)) != NULL) {
                g_warning(_("New folder name must not contain the namespace "
                            "path separator"));
-               unlock_session();
+               unlock_session(session);
                return -1;
        }
 
@@ -2123,7 +2189,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
                              &exists, &recent, &unseen, &uid_validity, FALSE);
        if (ok != IMAP_SUCCESS) {
                g_free(real_oldpath);
-               unlock_session();
+               unlock_session(session);
                return -1;
        }
 
@@ -2145,7 +2211,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
                g_free(real_oldpath);
                g_free(newpath);
                g_free(real_newpath);
-               unlock_session();
+               unlock_session(session);
                return -1;
        }
        g_free(item->name);
@@ -2171,7 +2237,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        g_free(newpath);
        g_free(real_oldpath);
        g_free(real_newpath);
-       unlock_session();
+       unlock_session(session);
        return 0;
 }
 
@@ -2233,7 +2299,7 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
        if (ok != IMAP_SUCCESS) {
                log_warning(LOG_PROTOCOL, _("can't delete mailbox\n"));
                g_free(path);
-               unlock_session();
+               unlock_session(session);
                return -1;
        }
 
@@ -2243,7 +2309,7 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
                g_warning("can't remove directory '%s'\n", cache_dir);
        g_free(cache_dir);
        folder_item_remove(item);
-       unlock_session();
+       unlock_session(session);
        return 0;
 }
 
@@ -2306,6 +2372,9 @@ static void *imap_get_uncached_messages_thread(void *data)
                carray * env_list;
                int count;
                
+               if (session->cancelled)
+                       break;
+               
                imapset = cur->data;
                
                r = imap_threaded_fetch_env(session->folder,
@@ -2399,7 +2468,7 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                data->cur += count;
                
                if (prefs_common.work_offline && 
-                   !inc_offline_should_override(
+                   !inc_offline_should_override(FALSE,
                        _("Claws Mail needs network access in order "
                          "to access the IMAP server."))) {
                        g_free(data);
@@ -2463,7 +2532,7 @@ gchar imap_get_path_separator_for_item(FolderItem *item)
        debug_print("getting session...");
        session = imap_session_get(FOLDER(folder));
        result = imap_get_path_separator(session, imap_folder, item->path);
-       unlock_session();
+       unlock_session(session);
        return result;
 }
 
@@ -2621,15 +2690,19 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
 
        if (messages) {
                mask |= 1 << 0;
+               *messages = 0;
        }
        if (uid_next) {
                mask |= 1 << 2;
+               *uid_next = 0;
        }
        if (uid_validity) {
                mask |= 1 << 3;
+               *uid_validity = 0;
        }
        if (unseen) {
                mask |= 1 << 4;
+               *unseen = 0;
        }
        r = imap_threaded_status(FOLDER(folder), real_path, 
                &data_status, mask);
@@ -2654,31 +2727,38 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
                info = clist_content(iter);
                switch (info->st_att) {
                case MAILIMAP_STATUS_ATT_MESSAGES:
-                       * messages = info->st_value;
-                       got_values |= 1 << 0;
+                       if (messages) {
+                               * messages = info->st_value;
+                               got_values |= 1 << 0;
+                       }
                        break;
                        
                case MAILIMAP_STATUS_ATT_UIDNEXT:
-                       * uid_next = info->st_value;
-                       got_values |= 1 << 2;
+                       if (uid_next) {
+                               * uid_next = info->st_value;
+                               got_values |= 1 << 2;
+                       }
                        break;
                        
                case MAILIMAP_STATUS_ATT_UIDVALIDITY:
-                       * uid_validity = info->st_value;
-                       got_values |= 1 << 3;
+                       if (uid_validity) {
+                               * uid_validity = info->st_value;
+                               got_values |= 1 << 3;
+                       }
                        break;
                        
                case MAILIMAP_STATUS_ATT_UNSEEN:
-                       * unseen = info->st_value;
-                       got_values |= 1 << 4;
+                       if (unseen) {
+                               * unseen = info->st_value;
+                               got_values |= 1 << 4;
+                       }
                        break;
                }
        }
        mailimap_mailbox_data_status_free(data_status);
        
        if (got_values != mask) {
-               debug_print("status: incomplete values received (%d)\n", got_values);
-               return IMAP_ERROR;
+               g_warning("status: incomplete values received (%d)\n", got_values);
        }
        return IMAP_SUCCESS;
 }
@@ -2897,7 +2977,7 @@ static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
        data->body = body;
 
        if (prefs_common.work_offline && 
-           !inc_offline_should_override(
+           !inc_offline_should_override(FALSE,
                _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                g_free(data);
@@ -2982,7 +3062,7 @@ static gint imap_cmd_expunge(IMAPSession *session)
        int r;
        
        if (prefs_common.work_offline && 
-           !inc_offline_should_override(
+           !inc_offline_should_override(FALSE,
                _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                return -1;
@@ -3327,7 +3407,7 @@ static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderIte
        data->msgnum_list = msgnum_list;
        data->session = session;
        if (prefs_common.work_offline && 
-           !inc_offline_should_override(
+           !inc_offline_should_override(FALSE,
                _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                g_free(data);
@@ -3379,7 +3459,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                        session = imap_reconnect_if_possible(folder, session);
                        if (session == NULL) {
                                statusbar_pop_all();
-                               unlock_session();
+                               unlock_session(session);
                                return -1;
                        }
                }
@@ -3404,7 +3484,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                item->use_cache = (time_t)0;
                if (ok != IMAP_SUCCESS) {
                        statusbar_pop_all();
-                       unlock_session();
+                       unlock_session(session);
                        return -1;
                }
                if(item->item.mtime == uid_val)
@@ -3440,7 +3520,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                        debug_print("exists == nummsgs\n");
                        *msgnum_list = g_slist_copy(item->uid_list);
                        statusbar_pop_all();
-                       unlock_session();
+                       unlock_session(session);
                        return nummsgs;
                } else if (exists < nummsgs) {
                        debug_print("Freeing imap uid cache");
@@ -3453,15 +3533,16 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        if (exists == 0) {
                *msgnum_list = NULL;
                statusbar_pop_all();
-               unlock_session();
+               unlock_session(session);
                return 0;
        }
 
+       item->last_change = time(NULL);
        nummsgs = get_list_of_uids(session, folder, item, &uidlist);
 
        if (nummsgs < 0) {
                statusbar_pop_all();
-               unlock_session();
+               unlock_session(session);
                return -1;
        }
 
@@ -3491,7 +3572,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        
        debug_print("get_num_list - ok - %i\n", nummsgs);
        statusbar_pop_all();
-       unlock_session();
+       unlock_session(session);
        return nummsgs;
 }
 
@@ -3542,7 +3623,7 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
        ok = imap_select(session, IMAP_FOLDER(folder), item->path,
                         NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
-               unlock_session();
+               unlock_session(session);
                return NULL;
        }
        if (!(folder_has_parent_of_type(item, F_DRAFT) || 
@@ -3597,7 +3678,7 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
 
                g_slist_free(sorted_list);
        }
-       unlock_session();
+       unlock_session(session);
        return ret;
 }
 
@@ -3652,14 +3733,14 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
 
                if (session->folder_content_changed
                ||  session->exists != item->item.total_msgs) {
-                       unlock_session();
+                       unlock_session(session);
                        return TRUE;
                }
        } else {
                ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
                                 &exists, &uid_next, &uid_val, &unseen, FALSE);
                if (ok != IMAP_SUCCESS) {
-                       unlock_session();
+                       unlock_session(session);
                        return FALSE;
                }
 
@@ -3673,11 +3754,12 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                        uid_next, item->uid_next, exists, item->item.total_msgs);
                if ((uid_next != item->uid_next) || (exists != item->item.total_msgs)
                    || unseen != item->item.unread_msgs || uid_val != item->item.mtime) {
-                       unlock_session();
+                       unlock_session(session);
+                       item->last_change = time(NULL);
                        return TRUE;
                }
        }
-       unlock_session();
+       unlock_session(session);
        return FALSE;
 }
 
@@ -3731,7 +3813,7 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
 
        if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
            NULL, NULL, NULL, NULL, FALSE)) != IMAP_SUCCESS) {
-               unlock_session();
+               unlock_session(session);
                return;
        }
        numlist.next = NULL;
@@ -3776,7 +3858,7 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
                if (flags_set) {
                        ok = imap_set_message_flags(session, &numlist, flags_set, TRUE);
                        if (ok != IMAP_SUCCESS) {
-                               unlock_session();
+                               unlock_session(session);
                                return;
                        }
                }
@@ -3784,13 +3866,13 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
                if (flags_unset) {
                        ok = imap_set_message_flags(session, &numlist, flags_unset, FALSE);
                        if (ok != IMAP_SUCCESS) {
-                               unlock_session();
+                               unlock_session(session);
                                return;
                        }
                }
        }
        msginfo->flags.perm_flags = newflags;
-       unlock_session();
+       unlock_session(session);
        return;
 }
 
@@ -3812,18 +3894,17 @@ static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
        ok = imap_select(session, IMAP_FOLDER(folder), item->path,
                         NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
-               unlock_session();
+               unlock_session(session);
                return ok;
        }
        numlist.next = NULL;
        numlist.data = GINT_TO_POINTER(uid);
        
        ok = imap_set_message_flags
-               (IMAP_SESSION(REMOTE_FOLDER(folder)->session),
-               &numlist, IMAP_FLAG_DELETED, TRUE);
+               (session, &numlist, IMAP_FLAG_DELETED, TRUE);
        if (ok != IMAP_SUCCESS) {
                log_warning(LOG_PROTOCOL, _("can't set deleted flags: %d\n"), uid);
-               unlock_session();
+               unlock_session(session);
                return ok;
        }
 
@@ -3838,7 +3919,7 @@ static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
        }
        if (ok != IMAP_SUCCESS) {
                log_warning(LOG_PROTOCOL, _("can't expunge\n"));
-               unlock_session();
+               unlock_session(session);
                return ok;
        }
 
@@ -3848,7 +3929,7 @@ static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
        if (is_dir_exist(dir))
                remove_numbered_files(dir, uid, uid);
        g_free(dir);
-       unlock_session();
+       unlock_session(session);
        return IMAP_SUCCESS;
 }
 
@@ -3928,7 +4009,7 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                        &exists_cnt, NULL, &unseen_cnt, NULL, TRUE);
                if (ok != IMAP_SUCCESS) {
                        stuff->done = TRUE;
-                       unlock_session();
+                       unlock_session(session);
                        return GINT_TO_POINTER(-1);
                }
 
@@ -4066,7 +4147,7 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
        g_string_free(cmd_buf, TRUE);
 
        stuff->done = TRUE;
-       unlock_session();
+       unlock_session(session);
        return GINT_TO_POINTER(0);
 }
 
@@ -4085,7 +4166,7 @@ static gint imap_get_flags(Folder *folder, FolderItem *item,
        GSList *tmp = NULL, *cur;
        
        if (prefs_common.work_offline && 
-           !inc_offline_should_override(
+           !inc_offline_should_override(FALSE,
                _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                g_free(data);
@@ -4139,7 +4220,7 @@ static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
                g_warning("can't select mailbox %s\n", item->path);
        }
 
-       unlock_session();
+       unlock_session(session);
        g_slist_free(data->msglist);    
        g_free(data);
        return TRUE;
@@ -4533,6 +4614,62 @@ void imap_folder_unref(Folder *folder)
                ((IMAPFolder *)folder)->refcnt--;
 }
 
+void imap_cancel_all(void)
+{
+       GList *folderlist;
+       GList *cur;
+       
+       folderlist = folder_get_list();
+       for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
+               Folder *folder = (Folder *) cur->data;
+
+               if (folder->klass == &imap_class) {
+                       if (imap_is_busy(folder)) {
+                               IMAPSession *imap_session;
+                               RemoteFolder *rfolder;
+                               
+                               fprintf(stderr, "cancelled\n");
+                               imap_threaded_cancel(folder);
+                               rfolder = (RemoteFolder *) folder;
+                               imap_session = (IMAPSession *) rfolder->session;
+                               imap_session->cancelled = 1;
+                       }
+               }
+       }
+}
+
+gboolean imap_cancel_all_enabled(void)
+{
+       GList *folderlist;
+       GList *cur;
+       
+       folderlist = folder_get_list();
+       for (cur = folderlist; cur != NULL; cur = g_list_next(cur)) {
+               Folder *folder = (Folder *) cur->data;
+
+               if (folder->klass == &imap_class) {
+                       if (imap_is_busy(folder)) {
+                               return TRUE;
+                       }
+               }
+       }
+       
+       return FALSE;
+}
+
+static gboolean imap_is_busy(Folder *folder)
+{
+       IMAPSession *imap_session;
+       RemoteFolder *rfolder;
+       
+       rfolder = (RemoteFolder *) folder;
+       imap_session = (IMAPSession *) rfolder->session;
+       if (imap_session == NULL)
+               return FALSE;
+       
+       return imap_session->busy;
+}
+
 #else /* HAVE_LIBETPAN */
 
 static FolderClass imap_class;
@@ -4620,11 +4757,31 @@ GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only
 {
        return NULL;
 }
+
+void imap_cache_msg(FolderItem *item, gint msgnum)
+{
+}
+
+void imap_cancel_all(void)
+{
+}
+
+gboolean imap_cancel_all_enabled(void)
+{
+       return FALSE;
+}
+
 #endif
 
 void imap_synchronise(FolderItem *item) 
 {
+       if (IMAP_FOLDER_ITEM(item)->last_sync == IMAP_FOLDER_ITEM(item)->last_change) {
+               debug_print("%s already synced\n", item->path?item->path:item->name);
+               return;
+       }
+       debug_print("syncing %s\n", item->path?item->path:item->name);
        imap_gtk_synchronise(item);
+       IMAP_FOLDER_ITEM(item)->last_sync = IMAP_FOLDER_ITEM(item)->last_change;
 }
 
 static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
@@ -4641,7 +4798,13 @@ static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
                if (!attr || !attr->name || !attr->value) continue;
                if (!strcmp(attr->name, "uidnext"))
                        IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
+               if (!strcmp(attr->name, "last_sync"))
+                       IMAP_FOLDER_ITEM(item)->last_sync = atoi(attr->value);
+               if (!strcmp(attr->name, "last_change"))
+                       IMAP_FOLDER_ITEM(item)->last_change = atoi(attr->value);
        }
+       if (IMAP_FOLDER_ITEM(item)->last_change == 0)
+               IMAP_FOLDER_ITEM(item)->last_change = time(NULL);
 #endif
 }
 
@@ -4654,6 +4817,10 @@ static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
 #ifdef HAVE_LIBETPAN
        xml_tag_add_attr(tag, xml_attr_new_int("uidnext", 
                        IMAP_FOLDER_ITEM(item)->uid_next));
+       xml_tag_add_attr(tag, xml_attr_new_int("last_sync", 
+                       IMAP_FOLDER_ITEM(item)->last_sync));
+       xml_tag_add_attr(tag, xml_attr_new_int("last_change", 
+                       IMAP_FOLDER_ITEM(item)->last_change));
 
 #endif
        return tag;