2005-06-17 [colin] 1.9.11cvs80
[claws.git] / src / mh.c
index 556aa374327abacdfb086ea44ff85c7d7078ccd8..4a799b5a4e6b81e722e0db0c570ef998bb9dffca 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-2003 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2005 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
@@ -24,6 +24,7 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <dirent.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -36,7 +37,6 @@
 #  include <sys/time.h>
 #endif
 
-#include "intl.h"
 #include "folder.h"
 #include "mh.h"
 #include "procmsg.h"
@@ -107,6 +107,9 @@ static gboolean mh_rename_folder_func               (GNode          *node,
 static gchar   *mh_item_get_path               (Folder *folder, 
                                                 FolderItem *item);
 
+static gboolean mh_scan_required       (Folder         *folder,
+                                        FolderItem     *item);
+
 static FolderClass mh_class;
 
 FolderClass *mh_get_class(void)
@@ -130,6 +133,7 @@ FolderClass *mh_get_class(void)
                mh_class.rename_folder = mh_rename_folder;
                mh_class.remove_folder = mh_remove_folder;
                mh_class.get_num_list = mh_get_num_list;
+               mh_class.scan_required = mh_scan_required;
                
                /* Message functions */
                mh_class.get_msginfo = mh_get_msginfo;
@@ -167,6 +171,38 @@ static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
 
 }
 
+gboolean mh_scan_required(Folder *folder, FolderItem *item)
+{
+       gchar *path;
+       struct stat s;
+
+       path = folder_item_get_path(item);
+       g_return_val_if_fail(path != NULL, FALSE);
+
+       if (stat(path, &s) < 0) {
+               FILE_OP_ERROR(path, "stat");
+               g_free(path);
+               return FALSE;
+       }
+
+       if ((s.st_mtime > item->mtime) &&
+               (s.st_mtime - 3600 != item->mtime)) {
+               debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
+                           path,
+                           s.st_mtime,
+                           item->mtime);
+               g_free(path);
+               return TRUE;
+       }
+
+       debug_print("MH scan not required: %s (%ld <= %ld)\n",
+                   path,
+                   s.st_mtime,
+                   item->mtime);
+       g_free(path);
+       return FALSE;
+}
+
 void mh_get_last_num(Folder *folder, FolderItem *item)
 {
        gchar *path;
@@ -193,7 +229,7 @@ void mh_get_last_num(Folder *folder, FolderItem *item)
        }
 
        while ((d = readdir(dp)) != NULL) {
-               if ((num = to_number(d->d_name)) >= 0 &&
+               if ((num = to_number(d->d_name)) > 0 &&
                    dirent_is_regular_file(d)) {
                        if (max < num)
                                max = num;
@@ -215,7 +251,7 @@ gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *
 
        g_return_val_if_fail(item != NULL, -1);
 
-       debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
+       debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
 
        *old_uids_valid = TRUE;
 
@@ -233,13 +269,14 @@ gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *
        }
 
        while ((d = readdir(dp)) != NULL) {
-               if ((num = to_number(d->d_name)) >= 0) {
+               if ((num = to_number(d->d_name)) > 0) {
                        *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
                        nummsgs++;
                }
        }
        closedir(dp);
 
+       item->mtime = time(NULL);
        return nummsgs;
 }
 
@@ -256,9 +293,10 @@ static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
 
        if (!is_file_exist(file)) {
                g_free(file);
+               g_free(path);
                return NULL;
        }
-
+       g_free(path);
        return file;
 }
 
@@ -366,6 +404,7 @@ static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
 
 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 {
+       gboolean dest_need_scan = FALSE;
        gchar *srcfile;
        gchar *destfile;
        gint filemode = 0;
@@ -412,6 +451,10 @@ static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
                return -1;
        }
 
+       dest_need_scan = mh_scan_required(dest->folder, dest);
+       if (!dest_need_scan)
+               dest->mtime = time(NULL);
+
 
        if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
                if (chmod(destfile, prefs->folder_chmod) < 0)
@@ -432,6 +475,7 @@ static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
 
 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
 {
+       gboolean need_scan = FALSE;
        gchar *file;
 
        g_return_val_if_fail(item != NULL, -1);
@@ -439,12 +483,17 @@ static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
        file = mh_fetch_msg(folder, item, num);
        g_return_val_if_fail(file != NULL, -1);
 
+       need_scan = mh_scan_required(folder, item);
+
        if (unlink(file) < 0) {
                FILE_OP_ERROR(file, "unlink");
                g_free(file);
                return -1;
        }
 
+       if (!need_scan)
+               item->mtime = time(NULL);
+
        g_free(file);
        return 0;
 }
@@ -470,8 +519,10 @@ static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
        struct stat s;
 
        if (stat(itos(msginfo->msgnum), &s) < 0 ||
-           msginfo->size  != s.st_size ||
-           msginfo->mtime != s.st_mtime)
+           msginfo->size  != s.st_size || (
+               (msginfo->mtime - s.st_mtime != 0) &&
+               (msginfo->mtime - s.st_mtime != 3600) &&
+               (msginfo->mtime - s.st_mtime != -3600)))
                return TRUE;
 
        return FALSE;
@@ -639,7 +690,7 @@ static gint mh_rename_folder(Folder *folder, FolderItem *item,
        if (!is_dir_exist(oldpath))
                make_dir_hier(oldpath);
 
-       dirname = g_dirname(oldpath);
+       dirname = g_path_get_dirname(oldpath);
        real_name = mh_filename_from_utf8(name);
        newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
        g_free(real_name);
@@ -655,7 +706,7 @@ static gint mh_rename_folder(Folder *folder, FolderItem *item,
        g_free(newpath);
 
        if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
-               dirname = g_dirname(item->path);
+               dirname = g_path_get_dirname(item->path);
                utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
                                          name, NULL);
                g_free(dirname);
@@ -888,6 +939,8 @@ static void mh_scan_tree_recursive(FolderItem *item)
 
        closedir(dp);
 
+       item->mtime = time(NULL);
+
 /*
        if (item->path) {
                gint new, unread, total, min, max;
@@ -934,11 +987,10 @@ static gboolean mh_rename_folder_func(GNode *node, gpointer data)
        return FALSE;
 }
 
-#warning FIXME_GTK2 /* should we use g_filename_from_utf8()? */
 static gchar *mh_filename_from_utf8(const gchar *path)
 {
        const gchar *src_codeset = CS_UTF_8;
-       const gchar *dest_codeset = conv_get_current_charset_str();
+       const gchar *dest_codeset = conv_get_locale_charset_str();
        gchar *real_path;
 
        real_path = conv_codeset_strdup(path, src_codeset, dest_codeset);
@@ -951,10 +1003,9 @@ static gchar *mh_filename_from_utf8(const gchar *path)
        return real_path;
 }
 
-#warning FIXME_GTK2 /* should we use g_filename_to_utf8()? */
 static gchar *mh_filename_to_utf8(const gchar *path)
 {
-       const gchar *src_codeset = conv_get_current_charset_str();
+       const gchar *src_codeset = conv_get_locale_charset_str();
        const gchar *dest_codeset = CS_UTF_8;
        gchar *utf8path;