2006-06-17 [colin] 2.3.0cvs20
[claws.git] / src / mh.c
index 93d49f342f1556ec58a723505cc945fbb9c6fdaf..76a603c159dc68556fd3be11785fdccfb385af57 100644 (file)
--- a/src/mh.c
+++ b/src/mh.c
@@ -43,6 +43,8 @@
 #include "procheader.h"
 #include "utils.h"
 #include "codeconv.h"
+#include "statusbar.h"
+#include "gtkutils.h"
 
 /* Define possible missing constants for Windows. */
 #ifdef G_OS_WIN32
@@ -88,6 +90,10 @@ static gint  mh_copy_msgs            (Folder         *folder,
 static gint     mh_remove_msg          (Folder         *folder,
                                         FolderItem     *item,
                                         gint            num);
+static gint    mh_remove_msgs          (Folder         *folder, 
+                                        FolderItem     *item, 
+                                        MsgInfoList    *msglist, 
+                                        GRelation      *relation);
 static gint     mh_remove_all_msg      (Folder         *folder,
                                         FolderItem     *item);
 static gboolean mh_is_msg_changed      (Folder         *folder,
@@ -126,6 +132,13 @@ static gchar   *mh_item_get_path           (Folder *folder,
 
 static gboolean mh_scan_required       (Folder         *folder,
                                         FolderItem     *item);
+static int mh_item_close               (Folder         *folder,
+                                        FolderItem     *item);
+#if 0
+static gint mh_get_flags               (Folder *folder, FolderItem *item,
+                                        MsgInfoList *msginfo_list, GRelation *msgflags);
+#endif
+static void mh_write_sequences         (FolderItem     *item, gboolean remove_unseen);
 
 static FolderClass mh_class;
 
@@ -151,7 +164,9 @@ FolderClass *mh_get_class(void)
                mh_class.remove_folder = mh_remove_folder;
                mh_class.get_num_list = mh_get_num_list;
                mh_class.scan_required = mh_scan_required;
-               
+               mh_class.close = mh_item_close;
+               mh_class.get_flags = NULL; /*mh_get_flags */;
+
                /* Message functions */
                mh_class.get_msginfo = mh_get_msginfo;
                mh_class.fetch_msg = mh_fetch_msg;
@@ -160,6 +175,7 @@ FolderClass *mh_get_class(void)
                mh_class.copy_msg = mh_copy_msg;
                mh_class.copy_msgs = mh_copy_msgs;
                mh_class.remove_msg = mh_remove_msg;
+               mh_class.remove_msgs = mh_remove_msgs;
                mh_class.remove_all_msg = mh_remove_all_msg;
                mh_class.is_msg_changed = mh_is_msg_changed;
        }
@@ -324,7 +340,8 @@ static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
        gchar *file;
 
        g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(num > 0, NULL);
+       if (num <= 0)
+               return NULL;
 
        file = mh_fetch_msg(folder, item, num);
        if (!file) return NULL;
@@ -421,7 +438,7 @@ static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                g_free(destfile);
                dest->last_num++;
        }
-
+       mh_write_sequences(dest, TRUE);
        return dest->last_num;
 }
 
@@ -446,8 +463,11 @@ static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
        gint filemode = 0;
        FolderItemPrefs *prefs;
        MsgInfo *msginfo = NULL;
-       gboolean remove_special_headers = FALSE;
        MsgInfoList *cur = NULL;
+       gint curnum = 0, total = 0;
+       gchar *srcpath = NULL;
+       gboolean full_fetch = FALSE;
+
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msglist != NULL, -1);
        
@@ -460,35 +480,51 @@ static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
                return -1;
        }
 
+       if (msginfo->folder->folder != dest->folder)
+               full_fetch = TRUE;
+
        if (dest->last_num < 0) {
                mh_get_last_num(folder, dest);
                if (dest->last_num < 0) return -1;
        }
 
-       if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
-       && !folder_has_parent_of_type(dest, F_QUEUE)
-       && !folder_has_parent_of_type(dest, F_DRAFT)) {
-               /* as every msginfo comes from the same folder, it is supposed they
-                * will either match the preceding condition either all or none.
-                */
-               remove_special_headers = TRUE;
-       } else if (!(MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
-       && (folder_has_parent_of_type(dest, F_QUEUE)
-        || folder_has_parent_of_type(dest, F_DRAFT))) {
-               return -1;
-       } 
-
        prefs = dest->prefs;
 
+       srcpath = folder_item_get_path(msginfo->folder);
+
+       total = g_slist_length(msglist);
+       if (total > 100) {
+               if (MSG_IS_MOVE(msginfo->flags))
+                       statusbar_print_all(_("Moving messages..."));
+               else
+                       statusbar_print_all(_("Copying messages..."));
+       }
        for (cur = msglist; cur; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
-               if (!msginfo)
-                       continue;
-               srcfile = procmsg_get_message_file(msginfo);
+               if (!msginfo) {
+                       goto err_reset_status;
+               }
+               if (!full_fetch) {
+                       srcfile = g_strconcat(srcpath, 
+                               G_DIR_SEPARATOR_S, 
+                               itos(msginfo->msgnum), NULL);
+               } else {
+                       srcfile = procmsg_get_message_file(msginfo);
+               }
+               if (!srcfile) {
+                       goto err_reset_status;
+               }
                destfile = mh_get_new_msg_filename(dest);
                if (!destfile) {
                        g_free(srcfile);
-                       continue;
+                       goto err_reset_status;
+               }
+
+               if (total > 100) {
+                       statusbar_progress_all(curnum, total, 100);
+                       if (curnum % 100 == 0)
+                               GTK_EVENTS_FLUSH();
+                       curnum++;
                }
 
                debug_print("Copying message %s%c%d to %s ...\n",
@@ -496,18 +532,22 @@ static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
                            msginfo->msgnum, dest->path);
 
 
-               if (remove_special_headers) {
-                       if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
-                               g_free(srcfile);
-                               g_free(destfile);
-                               continue;
+               if (MSG_IS_MOVE(msginfo->flags)) {
+                       if (move_file(srcfile, destfile, TRUE) < 0) {
+                               FILE_OP_ERROR(srcfile, "move");
+                               if (copy_file(srcfile, destfile, TRUE) < 0) {
+                                       FILE_OP_ERROR(srcfile, "copy");
+                                       g_free(srcfile);
+                                       g_free(destfile);
+                                       goto err_reset_status;
+                               }
                        }
                } else if (copy_file(srcfile, destfile, TRUE) < 0) {
                        FILE_OP_ERROR(srcfile, "copy");
                        g_free(srcfile);
                        g_free(destfile);
-                       continue;
-               }
+                       goto err_reset_status;
+               } 
                if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
                        if (chmod(destfile, prefs->folder_chmod) < 0)
                                FILE_OP_ERROR(destfile, "chmod");
@@ -524,11 +564,27 @@ static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
                dest->last_num++;
        }
 
+       g_free(srcpath);
+       mh_write_sequences(dest, TRUE);
+
        dest_need_scan = mh_scan_required(dest->folder, dest);
        if (!dest_need_scan)
                dest->mtime = time(NULL);
-
+       
+       if (total > 100) {
+               statusbar_progress_all(0,0,0);
+               statusbar_pop_all();
+       }
        return dest->last_num;
+err_reset_status:
+       g_free(srcpath);
+       mh_write_sequences(dest, TRUE);
+       if (total > 100) {
+               statusbar_progress_all(0,0,0);
+               statusbar_pop_all();
+       }
+       return -1;
+
 }
 
 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
@@ -556,6 +612,42 @@ static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
        return 0;
 }
 
+static gint mh_remove_msgs(Folder *folder, FolderItem *item, 
+                   MsgInfoList *msglist, GRelation *relation)
+{
+       gboolean need_scan = FALSE;
+       gchar *path, *file;
+       MsgInfoList *cur;
+
+       g_return_val_if_fail(item != NULL, -1);
+
+       path = folder_item_get_path(item);
+       
+       for (cur = msglist; cur; cur = cur->next) {
+               MsgInfo *msginfo = (MsgInfo *)cur->data;
+               if (msginfo == NULL)
+                       continue;
+               file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
+               if (file == NULL)
+                       continue;
+               
+               if (g_unlink(file) < 0) {
+                       g_free(file);
+                       continue;
+               }
+               
+               g_free(file);
+       }
+
+       need_scan = mh_scan_required(folder, item);
+
+       if (!need_scan)
+               item->mtime = time(NULL);
+
+       g_free(path);
+       return 0;
+}
+
 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
 {
        gchar *path;
@@ -568,6 +660,8 @@ static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
        val = remove_all_numbered_files(path);
        g_free(path);
 
+       mh_write_sequences(item, TRUE);
+
        return val;
 }
 
@@ -997,22 +1091,6 @@ static void mh_scan_tree_recursive(FolderItem *item)
 #endif
 
        item->mtime = time(NULL);
-
-/*
-       if (item->path) {
-               gint new, unread, total, min, max;
-
-               procmsg_get_mark_sum(item->path, &new, &unread, &total,
-                                    &min, &max, 0);
-               if (n_msg > total) {
-                       new += n_msg - total;
-                       unread += n_msg - total;
-               }
-               item->new = new;
-               item->unread = unread;
-               item->total = n_msg;
-       }
-*/
 }
 
 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
@@ -1066,3 +1144,226 @@ static gchar *mh_filename_to_utf8(const gchar *path)
 
        return utf8path;
 }
+
+static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
+{
+       MsgInfo *msginfo_a = (MsgInfo *) a;
+       MsgInfo *msginfo_b = (MsgInfo *) b;
+
+       return (msginfo_a->msgnum - msginfo_b->msgnum);
+}
+
+static gchar *get_unseen_seq_name(void)
+{
+       static gchar *seq_name = NULL;
+       if (!seq_name) {
+               gchar buf[BUFFSIZE];
+               gchar *tmp;
+               gchar *profile_path = g_strconcat(
+                       get_home_dir(), G_DIR_SEPARATOR_S,
+                       ".mh_profile", NULL);
+               FILE *fp = g_fopen(profile_path, "r");
+               if (fp) {
+                       while (fgets(buf, sizeof(buf), fp) != NULL) {
+                               if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
+                                       gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
+                                       while (*seq_tmp == ' ')
+                                               seq_tmp++;
+                                       seq_name = g_strdup(seq_tmp);
+                                       seq_name = strretchomp(seq_name);
+                                       break;
+                               }
+                       }
+                       fclose(fp);
+               }
+               if (!seq_name)
+                       seq_name = g_strdup("unseen");
+               tmp = g_strdup_printf("%s:", seq_name);
+               g_free(seq_name);
+               seq_name = tmp;
+       }
+       return seq_name;        
+}
+
+#if 0
+static gint mh_get_flags(Folder *folder, FolderItem *item,
+                           MsgInfoList *msginfo_list, GRelation *msgflags)
+{
+       gchar *mh_sequences_filename;
+       FILE *mh_sequences_file;
+       gchar buf[BUFFSIZE];
+       gchar *unseen_list = NULL;
+       gchar *path;
+       MsgInfoList *mcur = NULL;
+/*
+       GTimer *timer = g_timer_new();
+       g_timer_start(timer);
+*/
+       if (!item)
+               return 0;
+
+       /* don't update from .mh_sequences if the item's opened: mails may have
+        * been marked read/unread and it's not yet written in the file. */     
+       if (item->opened)
+               return 0;
+
+       path = folder_item_get_path(item);
+
+       mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
+                                           ".mh_sequences", NULL);
+       g_free(path);
+       if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
+               while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
+                       if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
+                               unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
+                               break;
+                       }
+               }
+               fclose(mh_sequences_file);
+       }
+       
+       g_free(mh_sequences_filename);
+       
+       if (unseen_list) {
+               gchar *cur = NULL;
+               gchar *token = NULL, *next = NULL, *boundary = NULL;
+               gint num = 0;
+               GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
+
+               cur = unseen_list = strretchomp(unseen_list);
+               debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
+next_token:
+               while (*cur && *cur == ' ')
+                       cur++;
+               
+               if ((next = strchr(cur, ' ')) != NULL) {
+                       token = cur;
+                       cur = next+1;
+                       *next = '\0';
+               } else {
+                       token = cur;
+                       cur = NULL;
+               }
+               
+               if ((boundary = strchr(token, '-')) != NULL) {
+                       gchar *start, *end;
+                       int i;
+                       start = token;
+                       end = boundary+1;
+                       *boundary='\0';
+                       for (i = atoi(start); i <= atoi(end); i++) {
+                               g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
+                       }
+               } else if ((num = atoi(token)) > 0) {
+                       g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
+               }
+               
+               if (cur)
+                       goto next_token;
+               for (mcur = msginfo_list; mcur; mcur = mcur->next) {
+                       MsgInfo *msginfo = (MsgInfo *)mcur->data;
+                       MsgPermFlags flags = msginfo->flags.perm_flags;
+                       if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
+                               flags |= MSG_UNREAD;
+                       } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
+                               flags &= ~(MSG_UNREAD);
+                       }
+                       if (flags != msginfo->flags.perm_flags)
+                               g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
+               }
+               g_hash_table_destroy(unseen_table);
+               g_free(unseen_list);
+       }
+/*
+       g_timer_stop(timer);
+       printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
+       g_timer_destroy(timer);
+*/
+       return 0;
+}
+#endif
+
+static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
+{
+       gchar *mh_sequences_old, *mh_sequences_new;
+       FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
+       gchar buf[BUFFSIZE];
+       gchar *path = NULL;
+/*
+       GTimer *timer = g_timer_new();
+       g_timer_start(timer);
+*/
+       if (!item)
+               return;
+       
+       path = folder_item_get_path(item);
+
+       mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
+                                           ".mh_sequences", NULL);
+       mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
+                                           ".mh_sequences.new", NULL);
+       if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
+               GSList *msglist = folder_item_get_msg_list(item);
+               GSList *cur;
+               MsgInfo *info = NULL;
+               gint start = -1, end = -1;
+               gchar *sequence = g_strdup("");
+               msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
+               cur = msglist;
+               
+               /* write the unseen sequence if we don't have to scrap it */
+               if (!remove_unseen) do {
+                       info = (MsgInfo *)(cur ? cur->data:NULL);
+                       if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
+                               if (start < 0)
+                                       start = end = info->msgnum;
+                               else
+                                       end = info->msgnum;
+                       } else {
+                               if (start > 0 && end > 0) {
+                                       gchar *tmp = sequence;
+                                       if (start != end)
+                                               sequence = g_strdup_printf("%s %d-%d ", tmp, start, end);
+                                       else
+                                               sequence = g_strdup_printf("%s %d ", tmp, start);
+                                       g_free(tmp);
+                                       start = end = -1;
+                               }
+                       }
+                       cur = cur ? cur->next:NULL;
+               } while (cur || (start > 0 && end > 0));
+               if (sequence && strlen(sequence)) {
+                       fprintf(mh_sequences_new_fp, "%s%s\n", 
+                                       get_unseen_seq_name(), sequence);
+                       debug_print("wrote unseen sequence: '%s%s'\n", 
+                                       get_unseen_seq_name(), sequence);
+               }
+               /* rewrite the rest of the file */
+               if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
+                       while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
+                               if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
+                                       fprintf(mh_sequences_new_fp, "%s", buf);
+                       }
+                       fclose(mh_sequences_old_fp);
+               }
+               
+               fclose(mh_sequences_new_fp);
+               g_rename(mh_sequences_new, mh_sequences_old);
+               g_free(sequence);
+               procmsg_msg_list_free(msglist);
+       }
+       g_free(mh_sequences_old);
+       g_free(mh_sequences_new);
+       g_free(path);
+/*
+       g_timer_stop(timer);
+       printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
+       g_timer_destroy(timer);
+*/
+}
+
+static int mh_item_close(Folder *folder, FolderItem *item)
+{
+       mh_write_sequences(item, FALSE);
+       return 0;
+}