sync with sylpheed 0.7.0cvs27
[claws.git] / src / mh.c
index deac165096d46a0b5f3a60e7aaf8510bc29df418..eeb7fc6d9fad5201eb2ff4cda8505ec3d57e4c25 100644 (file)
--- a/src/mh.c
+++ b/src/mh.c
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2002 Hiroyuki Yamamoto
  *
  * 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
 #include "procheader.h"
 #include "utils.h"
 
+static void    mh_folder_init                  (Folder         *folder,
+                                                const gchar    *name,
+                                                const gchar    *path);
+
 static GSList  *mh_get_uncached_msgs           (GHashTable     *msg_table,
                                                 FolderItem     *item);
 static MsgInfo *mh_parse_msg                   (const gchar    *file,
                                                 FolderItem     *item);
-static void    mh_scan_tree_recursive          (FolderItem     *item);
+static void    mh_scan_tree_recursive          (FolderItem     *item, 
+                                                GHashTable *pptable);
 
 static gboolean mh_rename_folder_func          (GNode          *node,
                                                 gpointer        data);
 
 
+Folder *mh_folder_new(const gchar *name, const gchar *path)
+{
+       Folder *folder;
+
+       folder = (Folder *)g_new0(MHFolder, 1);
+       mh_folder_init(folder, name, path);
+
+       return folder;
+}
+
+void mh_folder_destroy(MHFolder *folder)
+{
+       folder_local_folder_destroy(LOCAL_FOLDER(folder));
+}
+
+static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
+{
+       folder_local_folder_init(folder, name, path);
+
+       folder->type = F_MH;
+
+       folder->get_msg_list        = mh_get_msg_list;
+       folder->fetch_msg           = mh_fetch_msg;
+       folder->add_msg             = mh_add_msg;
+       folder->move_msg            = mh_move_msg;
+       folder->move_msgs_with_dest = mh_move_msgs_with_dest;
+       folder->copy_msg            = mh_copy_msg;
+       folder->copy_msgs_with_dest = mh_copy_msgs_with_dest;
+       folder->remove_msg          = mh_remove_msg;
+       folder->remove_all_msg      = mh_remove_all_msg;
+       folder->is_msg_changed      = mh_is_msg_changed;
+       folder->scan                = mh_scan_folder;
+       folder->scan_tree           = mh_scan_tree;
+       folder->create_tree         = mh_create_tree;
+       folder->create_folder       = mh_create_folder;
+       folder->rename_folder       = mh_rename_folder;
+       folder->remove_folder       = mh_remove_folder;
+}
+
 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
 {
        GSList *mlist;
@@ -72,11 +116,14 @@ GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
        if (stat(path, &s) < 0) {
                FILE_OP_ERROR(path, "stat");
        } else {
-               if (item->mtime == s.st_mtime) {
+               time_t mtime;
+
+               mtime = MAX(s.st_mtime, s.st_ctime);
+               if (item->mtime == mtime) {
                        debug_print("Folder is not modified.\n");
                        scan_new = FALSE;
                } else
-                       item->mtime = s.st_mtime;
+                       item->mtime = mtime;
        }
        g_free(path);
 
@@ -130,10 +177,35 @@ gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
        return file;
 }
 
+gchar *mh_get_newmsg_filename(FolderItem *dest)
+{
+       gchar *destfile;
+       gchar *destpath;
+       gboolean found = FALSE;
+
+       destpath = folder_item_get_path(dest);
+       g_return_val_if_fail(destpath != NULL, NULL);
+       if (!is_dir_exist(destpath))
+               make_dir_hier(destpath);
+
+       do {
+               destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
+                                          dest->last_num + 1);
+               if(is_file_exist(destfile)) {
+                       dest->last_num++;
+                       g_free(destfile);
+               } else {
+                       found = TRUE;
+               }
+       } while(!found);
+       g_free(destpath);
+
+       return destfile;
+}
+
 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
                gboolean remove_source)
 {
-       gchar *destpath;
        gchar *destfile;
 
        g_return_val_if_fail(dest != NULL, -1);
@@ -144,24 +216,13 @@ gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
                if (dest->last_num < 0) return -1;
        }
 
-       destpath = folder_item_get_path(dest);
-       g_return_val_if_fail(destpath != NULL, -1);
-       if (!is_dir_exist(destpath))
-               make_dir_hier(destpath);
-
-       destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
-                                  dest->last_num + 1);
+       destfile = mh_get_newmsg_filename(dest);
+       if(!destfile) return -1;
 
        if (link(file, destfile) < 0) {
-               if (EXDEV == errno) {
-                       if (copy_file(file, destfile) < 0) {
-                               g_warning(_("can't copy message %s to %s\n"),
-                                         file, destfile);
-                               g_free(destfile);
-                               return -1;
-                       }
-               } else {
-                       FILE_OP_ERROR(file, "link");
+               if (copy_file(file, destfile) < 0) {
+                       g_warning(_("can't copy message %s to %s\n"),
+                                 file, destfile);
                        g_free(destfile);
                        return -1;
                }
@@ -183,6 +244,8 @@ gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
        gchar *srcfile;
        gchar *destfile;
        FILE *fp;
+       gint filemode = 0;
+       PrefsFolderItem *prefs;
 
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msginfo != NULL, -1);
@@ -197,11 +260,9 @@ gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
                if (dest->last_num < 0) return -1;
        }
 
-       destdir = folder_item_get_path(dest);
-
-       if (!is_dir_exist(destdir))
-               make_dir_hier(destdir);
+       prefs = dest->prefs;
 
+       destdir = folder_item_get_path(dest);
        if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
                g_warning(_("Can't open mark file.\n"));
 
@@ -209,33 +270,27 @@ gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
                    msginfo->folder->path, G_DIR_SEPARATOR,
                    msginfo->msgnum, dest->path);
        srcfile = procmsg_get_message_file_path(msginfo);
-       destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
-                                  dest->last_num + 1);
+
+       destfile = mh_get_newmsg_filename(dest);
+       if(!destfile) return -1;
+
        g_free(destdir);
 
-       if (is_file_exist(destfile)) {
-               g_warning(_("%s already exists."), destfile);
+       if (move_file(srcfile, destfile) < 0) {
                g_free(srcfile);
                g_free(destfile);
                if (fp) fclose(fp);
                return -1;
        }
 
-       if (rename(srcfile, destfile) < 0) {
-               if (EXDEV == errno) {
-                       if (copy_file(srcfile, destfile) < 0) {
-                               g_free(srcfile);
-                               g_free(destfile);
-                               return -1;
-                       }
-                       unlink(srcfile);
-               } else {
-                       FILE_OP_ERROR(srcfile, "rename");
-                       g_free(srcfile);
-                       g_free(destfile);
-                       if (fp) fclose(fp);
-                       return -1;
-               }
+       if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
+               if (chmod(destfile, prefs->folder_chmod) < 0)
+                       FILE_OP_ERROR(destfile, "chmod");
+
+               /* for mark file */
+               filemode = prefs->folder_chmod;
+               if (filemode & S_IRGRP) filemode |= S_IWGRP;
+               if (filemode & S_IROTH) filemode |= S_IWOTH;
        }
 
        g_free(srcfile);
@@ -251,10 +306,25 @@ gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
                    dest->stype == F_QUEUE  ||
                    dest->stype == F_DRAFT  ||
                    dest->stype == F_TRASH)
-                       MSG_UNSET_FLAGS(newmsginfo.flags,
-                                       MSG_NEW|MSG_UNREAD|MSG_DELETED);
+                       MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
+                                            MSG_NEW|MSG_UNREAD|MSG_DELETED);
 
                procmsg_write_flags(&newmsginfo, fp);
+
+               if (filemode) {
+#if HAVE_FCHMOD
+                       fchmod(fileno(fp), filemode);
+#else
+                       gchar *markfile;
+
+                       markfile = folder_item_get_mark_file(dest);
+                       if (markfile) {
+                               chmod(markfile, filemode);
+                               g_free(markfile);
+                       }
+#endif
+               }
+
                fclose(fp);
        }
 
@@ -269,6 +339,7 @@ gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
        FILE *fp;
        GSList *cur;
        MsgInfo *msginfo;
+       PrefsFolderItem *prefs;
 
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msglist != NULL, -1);
@@ -278,10 +349,9 @@ gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
                if (dest->last_num < 0) return -1;
        }
 
-       destdir = folder_item_get_path(dest);
-       if (!is_dir_exist(destdir))
-               make_dir_hier(destdir);
+       prefs = dest->prefs;
 
+       destdir = folder_item_get_path(dest);
        if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
                g_warning(_("Can't open mark file.\n"));
 
@@ -297,32 +367,15 @@ gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
                            msginfo->msgnum, dest->path);
 
                srcfile = procmsg_get_message_file_path(msginfo);
-               destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
-                                          dest->last_num + 1);
+               destfile = mh_get_newmsg_filename(dest);
+               if(!destfile) return -1;
 
-               if (is_file_exist(destfile)) {
-                       g_warning(_("%s already exists."), destfile);
+               if (move_file(srcfile, destfile) < 0) {
                        g_free(srcfile);
                        g_free(destfile);
                        break;
                }
 
-               if (rename(srcfile, destfile) < 0) {
-                       if (EXDEV == errno) {
-                               if (copy_file(srcfile, destfile) < 0) {
-                                       g_free(srcfile);
-                                       g_free(destfile);
-                                       break;
-                               }
-                               unlink(srcfile);
-                       } else {
-                               FILE_OP_ERROR(srcfile, "rename");
-                               g_free(srcfile);
-                               g_free(destfile);
-                               break;
-                       }
-               }
-
                g_free(srcfile);
                g_free(destfile);
                dest->last_num++;
@@ -336,8 +389,9 @@ gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
                            dest->stype == F_QUEUE  ||
                            dest->stype == F_DRAFT  ||
                            dest->stype == F_TRASH)
-                               MSG_UNSET_FLAGS(newmsginfo.flags,
-                                               MSG_NEW|MSG_UNREAD|MSG_DELETED);
+                               MSG_UNSET_PERM_FLAGS
+                                       (newmsginfo.flags,
+                                        MSG_NEW|MSG_UNREAD|MSG_DELETED);
 
                        procmsg_write_flags(&newmsginfo, fp);
                }
@@ -355,6 +409,8 @@ gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
        gchar *srcfile;
        gchar *destfile;
        FILE *fp;
+       gint filemode = 0;
+       PrefsFolderItem *prefs;
 
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msginfo != NULL, -1);
@@ -369,6 +425,8 @@ gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
                if (dest->last_num < 0) return -1;
        }
 
+       prefs = dest->prefs;
+
        destdir = folder_item_get_path(dest);
        if (!is_dir_exist(destdir))
                make_dir_hier(destdir);
@@ -380,17 +438,16 @@ gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
                    msginfo->folder->path, G_DIR_SEPARATOR,
                    msginfo->msgnum, dest->path);
        srcfile = procmsg_get_message_file_path(msginfo);
-       destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
-                                  dest->last_num + 1);
-       g_free(destdir);
-
-       if (is_file_exist(destfile)) {
-               g_warning(_("%s already exists."), destfile);
+       destfile = mh_get_newmsg_filename(dest);
+       if(!destfile) {
                g_free(srcfile);
-               g_free(destfile);
                if (fp) fclose(fp);
                return -1;
        }
+       
+       g_free(destdir);
+
+       dest->op_count--;
 
        if (copy_file(srcfile, destfile) < 0) {
                FILE_OP_ERROR(srcfile, "copy");
@@ -400,6 +457,16 @@ gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
                return -1;
        }
 
+       if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
+               if (chmod(destfile, prefs->folder_chmod) < 0)
+                       FILE_OP_ERROR(destfile, "chmod");
+
+               /* for mark file */
+               filemode = prefs->folder_chmod;
+               if (filemode & S_IRGRP) filemode |= S_IWGRP;
+               if (filemode & S_IROTH) filemode |= S_IWOTH;
+       }
+
        g_free(srcfile);
        g_free(destfile);
        dest->last_num++;
@@ -413,15 +480,75 @@ gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
                    dest->stype == F_QUEUE  ||
                    dest->stype == F_DRAFT  ||
                    dest->stype == F_TRASH)
-                       MSG_UNSET_FLAGS(newmsginfo.flags,
-                                       MSG_NEW|MSG_UNREAD|MSG_DELETED);
+                       MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
+                                            MSG_NEW|MSG_UNREAD|MSG_DELETED);
                procmsg_write_flags(&newmsginfo, fp);
+
+               if (filemode) {
+#if HAVE_FCHMOD
+                       fchmod(fileno(fp), filemode);
+#else
+                       gchar *markfile;
+
+                       markfile = folder_item_get_mark_file(dest);
+                       if (markfile) {
+                               chmod(markfile, filemode);
+                               g_free(markfile);
+                       }
+#endif
+               }
+
                fclose(fp);
        }
 
        return dest->last_num;
 }
 
+/*
+gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
+{
+       Folder * src_folder;
+       gchar * filename;
+       gint num;
+       gchar * destdir;
+       FILE * fp;
+
+       src_folder = msginfo->folder->folder;
+       
+       g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
+       
+       filename = src_folder->fetch_msg(src_folder,
+                                        msginfo->folder,
+                                        msginfo->msgnum);
+       if (filename == NULL)
+               return -1;
+
+       num = folder->add_msg(folder, dest, filename, FALSE);
+
+       destdir = folder_item_get_path(dest);
+       if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
+               g_warning(_("Can't open mark file.\n"));
+
+       if (fp) {
+               MsgInfo newmsginfo;
+
+               newmsginfo.msgnum = dest->last_num;
+               newmsginfo.flags = msginfo->flags;
+               if (dest->stype == F_OUTBOX ||
+                   dest->stype == F_QUEUE  ||
+                   dest->stype == F_DRAFT  ||
+                   dest->stype == F_TRASH)
+                       MSG_UNSET_FLAGS(newmsginfo.flags,
+                                       MSG_NEW|MSG_UNREAD|MSG_DELETED);
+
+               procmsg_write_flags(&newmsginfo, fp);
+               fclose(fp);
+       }
+       
+       return num;
+}
+*/
+
 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
 {
        gchar *destdir;
@@ -458,13 +585,9 @@ gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
                            msginfo->msgnum, dest->path);
 
                srcfile = procmsg_get_message_file_path(msginfo);
-               destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
-                                          dest->last_num + 1);
-
-               if (is_file_exist(destfile)) {
-                       g_warning(_("%s already exists."), destfile);
+               destfile = mh_get_newmsg_filename(dest);
+               if(!destfile) {
                        g_free(srcfile);
-                       g_free(destfile);
                        break;
                }
 
@@ -488,8 +611,9 @@ gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
                            dest->stype == F_QUEUE  ||
                            dest->stype == F_DRAFT  ||
                            dest->stype == F_TRASH)
-                               MSG_UNSET_FLAGS(newmsginfo.flags,
-                                               MSG_NEW|MSG_UNREAD|MSG_DELETED);
+                               MSG_UNSET_PERM_FLAGS
+                                       (newmsginfo.flags,
+                                        MSG_NEW|MSG_UNREAD|MSG_DELETED);
                        procmsg_write_flags(&newmsginfo, fp);
                }
        }
@@ -522,32 +646,16 @@ gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
 {
        gchar *path;
-       DIR *dp;
-       struct dirent *d;
+       gint val;
 
        g_return_val_if_fail(item != NULL, -1);
 
        path = folder_item_get_path(item);
        g_return_val_if_fail(path != NULL, -1);
-       if (change_dir(path) < 0) {
-               g_free(path);
-               return -1;
-       }
+       val = remove_all_numbered_files(path);
        g_free(path);
 
-       if ((dp = opendir(".")) == NULL) {
-               FILE_OP_ERROR(item->path, "opendir");
-               return -1;
-       }
-
-       while ((d = readdir(dp)) != NULL) {
-               if (to_number(d->d_name) < 0) continue;
-               if (unlink(d->d_name) < 0)
-                       FILE_OP_ERROR(d->d_name, "unlink");
-       }
-
-       closedir(dp);
-       return 0;
+       return val;
 }
 
 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
@@ -574,6 +682,8 @@ void mh_scan_folder(Folder *folder, FolderItem *item)
 
        g_return_if_fail(item != NULL);
 
+       debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
+
        path = folder_item_get_path(item);
        g_return_if_fail(path != NULL);
        if (change_dir(path) < 0) {
@@ -625,9 +735,12 @@ void mh_scan_tree(Folder *folder)
 {
        FolderItem *item;
        gchar *rootpath;
+       GHashTable *pptable;
 
        g_return_if_fail(folder != NULL);
 
+       pptable = folder_persist_prefs_new(folder);
+
        folder_tree_destroy(folder);
        item = folder_item_new(folder->name, NULL);
        item->folder = folder;
@@ -640,7 +753,9 @@ void mh_scan_tree(Folder *folder)
        }
        g_free(rootpath);
 
-       mh_scan_tree_recursive(item);
+       mh_scan_tree_recursive(item, pptable);
+       
+       folder_persist_prefs_free(pptable);
 }
 
 #define MAKE_DIR_IF_NOT_EXIST(dir) \
@@ -896,18 +1011,21 @@ static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
 {
        struct stat s;
        MsgInfo *msginfo;
-       MsgFlags flags = MSG_NEW|MSG_UNREAD;
+       MsgFlags flags;
+
+       flags.perm_flags = MSG_NEW|MSG_UNREAD;
+       flags.tmp_flags = 0;
 
        g_return_val_if_fail(item != NULL, NULL);
        g_return_val_if_fail(file != NULL, NULL);
 
        if (item->stype == F_QUEUE) {
-               MSG_SET_FLAGS(flags, MSG_QUEUED);
+               MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
        } else if (item->stype == F_DRAFT) {
-               MSG_SET_FLAGS(flags, MSG_DRAFT);
+               MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
        }
 
-       msginfo = procheader_parse(file, flags, FALSE);
+       msginfo = procheader_parse(file, flags, FALSE, FALSE);
        if (!msginfo) return NULL;
 
        msginfo->msgnum = atoi(file);
@@ -949,7 +1067,7 @@ static gboolean mh_is_maildir(const gchar *path)
               mh_is_maildir_one(path, "tmp");
 }
 
-static void mh_scan_tree_recursive(FolderItem *item)
+static void mh_scan_tree_recursive(FolderItem *item, GHashTable *pptable)
 {
        DIR *dp;
        struct dirent *d;
@@ -1016,7 +1134,8 @@ static void mh_scan_tree_recursive(FolderItem *item)
                                        item->folder->trash = new_item;
                                }
                        }
-                       mh_scan_tree_recursive(new_item);
+                       folder_item_restore_persist_prefs(new_item, pptable);
+                       mh_scan_tree_recursive(new_item, pptable);
                } else if (to_number(d->d_name) != -1) n_msg++;
 
                g_free(entry);