added missing folder->destroy() initialisation for mbox folders
[claws.git] / src / mbox_folder.c
index e1253832e2529145ff1027ea9dcb16a49f4129db..2157bfbe4103413bc7bf0d292ccd49d0870952f2 100644 (file)
 
 #define MSGBUFSIZE     8192
 
+static void    mbox_folder_init                (Folder         *folder,
+                                                const gchar    *name,
+                                                const gchar    *path);
+
 static gboolean mbox_write_data(FILE * mbox_fp, FILE * new_fp,
                                gchar * new_filename, gint size);
 static gboolean mbox_rewrite(gchar * mbox);
@@ -42,9 +46,54 @@ static gboolean mbox_purge_deleted(gchar * mbox);
 static gchar * mbox_get_new_path(FolderItem * parent, gchar * name);
 static gchar * mbox_get_folderitem_name(gchar * name);
 
+MsgInfo *mbox_fetch_msginfo(Folder *folder, FolderItem *item, gint num);
+gint mbox_get_num_list(Folder *folder, FolderItem *item, GSList **list);
+gboolean mbox_check_msgnum_validity(Folder *folder, FolderItem *item);
+
+Folder *mbox_folder_new(const gchar *name, const gchar *path)
+{
+       Folder *folder;
+
+       folder = (Folder *)g_new0(MBOXFolder, 1);
+       mbox_folder_init(folder, name, path);
+
+       return folder;
+}
+
+void mbox_folder_destroy(MBOXFolder *folder)
+{
+       folder_local_folder_destroy(LOCAL_FOLDER(folder));
+}
+
+static void mbox_folder_init(Folder *folder, const gchar *name, const gchar *path)
+{
+       folder->type = F_MBOX;
 
+       folder_local_folder_init(folder, name, path);
 
-static gchar * mbox_folder_create_parent(const gchar * path)
+/*
+       folder->get_msg_list        = mbox_get_msg_list;
+*/
+       folder->fetch_msg           = mbox_fetch_msg;
+       folder->fetch_msginfo       = mbox_fetch_msginfo;
+       folder->add_msg             = mbox_add_msg;
+       folder->copy_msg            = mbox_copy_msg;
+       folder->remove_msg          = mbox_remove_msg;
+       folder->remove_all_msg      = mbox_remove_all_msg;
+/*
+       folder->scan                = mbox_scan_folder;
+*/
+       folder->get_num_list        = mbox_get_num_list;
+       folder->create_tree         = mbox_create_tree;
+       folder->create_folder       = mbox_create_folder;
+       folder->rename_folder       = mbox_rename_folder;
+       folder->remove_folder       = mbox_remove_folder;
+       folder->destroy             = mbox_folder_destroy;
+       folder->check_msgnum_validity
+                                   = mbox_check_msgnum_validity;
+}
+
+static void mbox_folder_create_parent(const gchar * path)
 {
        if (!is_file_exist(path)) {
                gchar * new_path;
@@ -117,7 +166,7 @@ static gboolean mbox_file_lock_file(gchar * base)
        FILE *lockfp;
 
        lockfile = g_strdup_printf("%s.%d", base, getpid());
-       if ((lockfp = fopen(lockfile, "w")) == NULL) {
+       if ((lockfp = fopen(lockfile, "wb")) == NULL) {
                FILE_OP_ERROR(lockfile, "fopen");
                g_warning(_("can't create lock file %s\n"), lockfile);
                g_warning(_("use 'flock' instead of 'file' if possible.\n"));
@@ -135,6 +184,7 @@ static gboolean mbox_file_lock_file(gchar * base)
                        g_warning(_("can't create %s\n"), lockfile);
                        unlink(lockfile);
                        g_free(lockfile);
+                       g_free(locklink);
                        return -1;
                }
                if (retry == 0)
@@ -145,7 +195,8 @@ static gboolean mbox_file_lock_file(gchar * base)
        }
        unlink(lockfile);
        g_free(lockfile);
-
+       g_free(locklink);
+       
        return TRUE;
 }
 
@@ -491,7 +542,7 @@ static mailfile * mailfile_init_from_file(FILE * f, gchar * filename)
        mf->count = msgnum;
 
        mailfile_error = MAILFILE_ERROR_NO_ERROR;
-
+       
        return mf;
 }
 
@@ -501,7 +552,7 @@ static mailfile * mailfile_init(char * filename)
        FILE * f;
        mailfile * mf;
   
-       f = fopen(filename, "r");
+       f = fopen(filename, "rb");
 
        if (f == NULL) {
                mailfile_error = MAILFILE_ERROR_FILE_NOT_FOUND;
@@ -539,7 +590,7 @@ static char * readfile(char * filename, int offset, int max_offset)
        int bread;
        FILE * handle;
 
-       handle = fopen(filename, "r");
+       handle = fopen(filename, "rb");
 
        if (handle == NULL) {
                mailfile_error = MAILFILE_ERROR_FILE_NOT_FOUND;
@@ -641,7 +692,7 @@ static int mailfile_find_deleted(mailfile f, char * filename)
 {
        FILE * handle;
 
-       handle = fopen(filename, "r");
+       handle = fopen(filename, "rb");
 
        while (elt) {
                struct _message m = elt->data;
@@ -693,7 +744,7 @@ static MsgInfo *mbox_parse_msg(FILE * fp, struct _message * msg,
                }
        }
 
-       msginfo = procheader_file_parse(fp, flags, FALSE);
+       msginfo = procheader_parse_stream(fp, flags, FALSE, FALSE);
 
        if (!msginfo) return NULL;
 
@@ -710,11 +761,6 @@ static void mbox_cache_init()
        mbox_cache_table = g_hash_table_new(g_str_hash, g_str_equal);
 }
 
-static void mbox_cache_done()
-{
-       g_hash_table_destroy(mbox_cache_table);
-}
-
 static void mbox_cache_free_mbox(mboxcache * cache)
 {
        g_hash_table_remove(mbox_cache_table, cache->filename);
@@ -759,7 +805,7 @@ static void mbox_cache_get_msginfo(gchar * filename, GList * msg_list)
 {
        FILE * fp;
 
-       fp = fopen(filename, "r");
+       fp = fopen(filename, "rb");
        if (fp == NULL)
                return;
 
@@ -794,7 +840,7 @@ static mboxcache * mbox_cache_read_mbox(gchar * filename)
 
        mbox_cache_get_msginfo(filename, mf->msg_list);
 
-       debug_print(_("read mbox - %s\n"), filename);
+       debug_print("read mbox - %s\n", filename);
 
        return cache;
 }
@@ -825,7 +871,7 @@ static mboxcache * mbox_cache_read_mbox_from_file(FILE * fp, gchar * filename)
 
        mbox_cache_get_msginfo_from_file(fp, mf->msg_list);
 
-       debug_print(_("read mbox from file - %s\n"), filename);
+       debug_print("read mbox from file - %s\n", filename);
 
        return cache;
 }
@@ -859,16 +905,6 @@ static gint mbox_cache_get_count(gchar * filename)
        return cache->mf->count;
 }
 
-static gint mbox_cache_get_mtime(gchar * filename)
-{
-       mboxcache * cache;
-
-       cache = mbox_cache_get_mbox(filename);
-       if (cache == NULL)
-               return -1;
-       return cache->mtime;
-}
-
 static GList * mbox_cache_get_msg_list(gchar * filename)
 {
        mboxcache * cache;
@@ -934,8 +970,6 @@ static void mbox_cache_synchronize(gchar * filename, gboolean sync)
        }
 
        if (scan_new) {
-               GList * l;
-
                /*              
                if (strstr(filename, "trash") == 0)
                        printf("old_cache: %p %s\n", old_cache, filename);
@@ -1145,7 +1179,7 @@ GSList *mbox_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
 
        mbox_purge_deleted(mbox_path);
 
-       fp = fopen(mbox_path, "r");
+       fp = fopen(mbox_path, "rb");
        
        if (fp == NULL) {
                g_free(mbox_path);
@@ -1219,7 +1253,7 @@ static gboolean mbox_extract_msg(FolderItem * item, gint msgnum,
        if (mbox_path == NULL)
                return FALSE;
 
-       src = fopen(mbox_path, "r");
+       src = fopen(mbox_path, "rb");
        if (src == NULL) {
                g_free(mbox_path);
                return FALSE;
@@ -1254,7 +1288,7 @@ static gboolean mbox_extract_msg(FolderItem * item, gint msgnum,
 
        fseek(src, offset, SEEK_SET);
 
-       dest = fopen(dest_filename, "w");
+       dest = fopen(dest_filename, "wb");
        if (dest == NULL) {
                mbox_unlock_file(src, mbox_path);
                fclose(src);
@@ -1313,7 +1347,6 @@ gchar *mbox_fetch_msg(Folder *folder, FolderItem *item, gint num)
        gchar *filename;
        
        g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
 
        path = folder_item_get_path(item);
        if (!is_dir_exist(path))
@@ -1348,7 +1381,7 @@ gint mbox_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
                if (dest->last_num < 0) return -1;
        }
 
-       src_fp = fopen(file, "r");
+       src_fp = fopen(file, "rb");
        if (src_fp == NULL) {
                return -1;
        }
@@ -1357,7 +1390,7 @@ gint mbox_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
        if (mbox_path == NULL)
                return -1;
 
-       dest_fp = fopen(mbox_path, "a");
+       dest_fp = fopen(mbox_path, "ab");
        if (dest_fp == NULL) {
                fclose(src_fp);
                g_free(mbox_path);
@@ -1387,7 +1420,7 @@ gint mbox_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
 
                if (stat(file, &s) < 0) {
                        mbox_unlock_file(dest_fp, mbox_path);
-                       g_warning(_("unvalid file - %s.\n"), file);
+                       g_warning(_("invalid file - %s.\n"), file);
                        fclose(dest_fp);
                        fclose(src_fp);
                        g_free(mbox_path);
@@ -1468,7 +1501,7 @@ gint mbox_remove_msg(Folder *folder, FolderItem *item, gint num)
        msg = mbox_cache_get_msg(mbox_path, num);
 
        g_free(mbox_path);
-       
+
        if (msg != NULL)
                MSG_SET_PERM_FLAGS(msg->flags, MSG_REALLY_DELETED);
 
@@ -1484,7 +1517,7 @@ gint mbox_remove_all_msg(Folder *folder, FolderItem *item)
        if (mbox_path == NULL)
                return -1;
 
-       fp = fopen(mbox_path, "w");
+       fp = fopen(mbox_path, "wb");
        if (fp == NULL) {
                g_free(mbox_path);
                return -1;
@@ -1601,9 +1634,6 @@ gint mbox_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
        Folder * src_folder;
        gchar * filename;
        gint num;
-       gchar * destdir;
-       gchar * mbox_path;
-       struct _message * msg;
        CopyFlagsInfo * flags_info;
 
        src_folder = msginfo->folder->folder;
@@ -1728,7 +1758,7 @@ void mbox_scan_folder(Folder *folder, FolderItem *item)
                item->total = total;
        }
 
-       debug_print(_("Last number in dir %s = %d\n"), mbox_path,
+       debug_print("Last number in dir %s = %d\n", mbox_path,
                    item->total);
        item->last_num = n_msg;
        g_free(mbox_path);
@@ -1900,6 +1930,7 @@ void mbox_change_flags(Folder * folder, FolderItem * item, MsgInfo * info)
        msg->flags = info->flags;
 
        cache->modification = TRUE;
+               
 }
 
 
@@ -1921,19 +1952,19 @@ static gboolean mbox_rewrite(gchar * mbox)
                return FALSE;
 
        if (!cache->modification) {
-               debug_print(_("no modification - %s\n"), mbox);
+               debug_print("no modification - %s\n", mbox);
                return FALSE;
        }
 
-       debug_print(_("save modification - %s\n"), mbox);
+       debug_print("save modification - %s\n", mbox);
 
-       mbox_fp = fopen(mbox, "r+");
+       mbox_fp = fopen(mbox, "rb+");
        mbox_lockwrite_file(mbox_fp, mbox);
 
        mbox_cache_synchronize_from_file(mbox_fp, mbox, TRUE);
 
        new = g_strconcat(mbox, ".", itos((int) mbox), NULL);
-       new_fp = fopen(new, "w");
+       new_fp = fopen(new, "wb");
 
        if (change_file_mode_rw(new_fp, new) < 0) {
                FILE_OP_ERROR(new, "chmod");
@@ -1963,6 +1994,7 @@ static gboolean mbox_rewrite(gchar * mbox)
                fclose(new_fp);
                mbox_unlock_file(mbox_fp, mbox);
                fclose(mbox_fp);
+               g_free(new);
                return -1;
        }
 
@@ -1979,7 +2011,7 @@ static gboolean mbox_rewrite(gchar * mbox)
 
        fclose(mbox_fp);
 
-       debug_print(_("%i messages written - %s\n"), count, mbox);
+       debug_print("%i messages written - %s\n", count, mbox);
 
        cache = mbox_cache_get_mbox(mbox);
 
@@ -1988,6 +2020,8 @@ static gboolean mbox_rewrite(gchar * mbox)
 
        mbox_cache_synchronize(mbox, FALSE);
 
+       g_free(new);
+       
        return result;
 }
 
@@ -2008,26 +2042,26 @@ static gboolean mbox_purge_deleted(gchar * mbox)
 
        for(l = msg_list ; l != NULL ; l = g_list_next(l)) {
                struct _message * msg = (struct _message *) l->data;
-               if (MSG_IS_INVALID(msg->flags) && MSG_IS_REALLY_DELETED(msg->flags)) {
+               if (!MSG_IS_INVALID(msg->flags) && MSG_IS_REALLY_DELETED(msg->flags)) {
                        modification = TRUE;
                        break;
                }
        }
 
        if (!modification) {
-               debug_print(_("no deleted messages - %s\n"), mbox);
+               debug_print("no deleted messages - %s\n", mbox);
                return FALSE;
        }
 
-       debug_print(_("purge deleted messages - %s\n"), mbox);
+       debug_print("purge deleted messages - %s\n", mbox);
 
-       mbox_fp = fopen(mbox, "r+");
+       mbox_fp = fopen(mbox, "rb+");
        mbox_lockwrite_file(mbox_fp, mbox);
 
        mbox_cache_synchronize_from_file(mbox_fp, mbox, TRUE);
 
        new = g_strconcat(mbox, ".", itos((int) mbox), NULL);
-       new_fp = fopen(new, "w");
+       new_fp = fopen(new, "wb");
 
        if (change_file_mode_rw(new_fp, new) < 0) {
                FILE_OP_ERROR(new, "chmod");
@@ -2059,6 +2093,7 @@ static gboolean mbox_purge_deleted(gchar * mbox)
                fclose(new_fp);
                mbox_unlock_file(mbox_fp, mbox);
                fclose(mbox_fp);
+               g_free(new);
                return -1;
        }
 
@@ -2075,10 +2110,10 @@ static gboolean mbox_purge_deleted(gchar * mbox)
 
        fclose(mbox_fp);
 
-       debug_print(_("%i messages written - %s\n"), count, mbox);
+       debug_print("%i messages written - %s\n", count, mbox);
 
        mbox_cache_synchronize(mbox, FALSE);
-
+       g_free(new);
        return result;
 }
 
@@ -2155,7 +2190,7 @@ FolderItem *mbox_create_folder(Folder *folder, FolderItem *parent,
 
        foldername = mbox_get_folderitem_name((gchar *) name);
 
-       new_item = folder_item_new(foldername, path);
+       new_item = folder_item_new(folder, foldername, path);
        folder_item_append(parent, new_item);
 
        if (!strcmp(name, "inbox")) {
@@ -2221,3 +2256,118 @@ gint mbox_remove_folder(Folder *folder, FolderItem *item)
        return 0;
 }
 
+gint mbox_get_num_list(Folder *folder, FolderItem *item, GSList **mlist)
+{
+       GList * l;
+       FILE * fp;
+       gchar * mbox_path;
+       gint nummsgs = 0;
+
+       mbox_path = mbox_folder_get_path(item);
+
+       if (mbox_path == NULL)
+               return -1;
+
+       mbox_purge_deleted(mbox_path);
+
+       fp = fopen(mbox_path, "rb");
+       
+       if (fp == NULL) {
+               g_free(mbox_path);
+               return -1;
+       }
+
+       mbox_lockread_file(fp, mbox_path);
+
+       mbox_cache_synchronize_from_file(fp, mbox_path, TRUE);
+
+       item->last_num = mbox_cache_get_count(mbox_path);
+
+       for(l = mbox_cache_get_msg_list(mbox_path) ; l != NULL ;
+           l = g_list_next(l)) {
+               struct _message * msg;
+
+               msg = (struct _message *) l->data;
+
+               if (MSG_IS_INVALID(msg->flags) || !MSG_IS_REALLY_DELETED(msg->flags)) {
+                       *mlist = g_slist_append(*mlist, GINT_TO_POINTER(msg->msgnum));
+                       nummsgs++;
+               } else {
+                       MSG_SET_PERM_FLAGS(msg->flags, MSG_REALLY_DELETED);
+               }
+       }
+
+       mbox_unlock_file(fp, mbox_path);
+
+       g_free(mbox_path);
+
+       fclose(fp);
+
+       return nummsgs;
+}
+
+MsgInfo *mbox_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
+{
+       gchar *mbox_path;
+       struct _message *msg;
+       FILE *src;
+       MsgInfo *msginfo;
+       
+       g_return_val_if_fail(folder != NULL, NULL);
+       g_return_val_if_fail(item != NULL, NULL);
+
+       mbox_path = mbox_folder_get_path(item);
+
+       g_return_val_if_fail(mbox_path != NULL, NULL);
+       
+       src = fopen(mbox_path, "rb");
+       if (src == NULL) {
+               g_free(mbox_path);
+               return NULL;
+       }
+       mbox_lockread_file(src, mbox_path);
+       mbox_cache_synchronize_from_file(src, mbox_path, TRUE);
+
+       msg = mbox_cache_get_msg(mbox_path, num);
+       if (msg == NULL) {
+               mbox_unlock_file(src, mbox_path);
+               fclose(src);
+               g_free(mbox_path);
+               return NULL;
+       }
+       
+       fseek(src, msg->header, SEEK_SET);
+       msginfo = mbox_parse_msg(src, msg, item);
+
+       mbox_unlock_file(src, mbox_path);
+       fclose(src);
+       g_free(mbox_path);
+
+       return msginfo;
+}
+
+gboolean mbox_check_msgnum_validity(Folder *folder, FolderItem *item)
+{
+       mboxcache * old_cache;
+       gboolean scan_new = TRUE;
+       struct stat s;
+       gchar *filename;
+
+       filename = mbox_folder_get_path(item);
+       
+       old_cache = mbox_cache_get_mbox(filename);
+
+       if (old_cache != NULL) {
+               if (stat(filename, &s) < 0) {
+                       FILE_OP_ERROR(filename, "stat");
+               } else if (old_cache->mtime == s.st_mtime) {
+                       debug_print("Folder is not modified.\n");
+                       scan_new = FALSE;
+               }
+       }
+
+       g_free(filename);
+       
+       return !scan_new;
+}
+