2001-04-19 14:21:46 +02:00
|
|
|
/*
|
|
|
|
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
|
2007-01-03 06:42:43 +01:00
|
|
|
* Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
|
2001-04-19 14:21:46 +02:00
|
|
|
*
|
|
|
|
* 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
|
2007-07-11 18:38:12 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2001-04-19 14:21:46 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-11 18:38:12 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2001-04-19 14:21:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
2005-02-10 13:06:07 +01:00
|
|
|
#include <glib/gi18n.h>
|
2001-04-19 14:21:46 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
2001-05-17 15:25:31 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2001-04-19 14:21:46 +02:00
|
|
|
#include <errno.h>
|
2006-10-02 19:04:07 +02:00
|
|
|
#include <time.h>
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
#include "folder.h"
|
|
|
|
#include "mh.h"
|
|
|
|
#include "procmsg.h"
|
|
|
|
#include "procheader.h"
|
|
|
|
#include "utils.h"
|
2003-10-05 12:10:30 +02:00
|
|
|
#include "codeconv.h"
|
2006-05-26 10:32:09 +02:00
|
|
|
#include "statusbar.h"
|
2006-05-24 20:02:15 +02:00
|
|
|
#include "gtkutils.h"
|
2006-10-02 19:04:07 +02:00
|
|
|
#include "timing.h"
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2005-12-20 18:43:40 +01:00
|
|
|
/* Define possible missing constants for Windows. */
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
# ifndef S_IRGRP
|
|
|
|
# define S_IRGRP 0
|
|
|
|
# define S_IWGRP 0
|
|
|
|
# endif
|
|
|
|
# ifndef S_IROTH
|
|
|
|
# define S_IROTH 0
|
|
|
|
# define S_IWOTH 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static void mh_folder_init (Folder *folder,
|
|
|
|
const gchar *name,
|
|
|
|
const gchar *path);
|
|
|
|
|
|
|
|
static Folder *mh_folder_new (const gchar *name,
|
|
|
|
const gchar *path);
|
|
|
|
static void mh_folder_destroy (Folder *folder);
|
|
|
|
static gchar *mh_fetch_msg (Folder *folder,
|
|
|
|
FolderItem *item,
|
|
|
|
gint num);
|
|
|
|
static MsgInfo *mh_get_msginfo (Folder *folder,
|
|
|
|
FolderItem *item,
|
|
|
|
gint num);
|
|
|
|
static gint mh_add_msg (Folder *folder,
|
|
|
|
FolderItem *dest,
|
|
|
|
const gchar *file,
|
|
|
|
MsgFlags *flags);
|
|
|
|
static gint mh_add_msgs (Folder *folder,
|
|
|
|
FolderItem *dest,
|
|
|
|
GSList *file_list,
|
|
|
|
GRelation *relation);
|
|
|
|
static gint mh_copy_msg (Folder *folder,
|
|
|
|
FolderItem *dest,
|
|
|
|
MsgInfo *msginfo);
|
2005-12-30 18:36:08 +01:00
|
|
|
static gint mh_copy_msgs (Folder *folder,
|
|
|
|
FolderItem *dest,
|
|
|
|
MsgInfoList *msglist,
|
|
|
|
GRelation *relation);
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_remove_msg (Folder *folder,
|
|
|
|
FolderItem *item,
|
|
|
|
gint num);
|
2006-05-26 10:32:09 +02:00
|
|
|
static gint mh_remove_msgs (Folder *folder,
|
|
|
|
FolderItem *item,
|
|
|
|
MsgInfoList *msglist,
|
|
|
|
GRelation *relation);
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_remove_all_msg (Folder *folder,
|
|
|
|
FolderItem *item);
|
|
|
|
static gboolean mh_is_msg_changed (Folder *folder,
|
|
|
|
FolderItem *item,
|
|
|
|
MsgInfo *msginfo);
|
|
|
|
|
|
|
|
static gint mh_get_num_list (Folder *folder,
|
|
|
|
FolderItem *item,
|
|
|
|
GSList **list,
|
|
|
|
gboolean *old_uids_valid);
|
|
|
|
static gint mh_scan_tree (Folder *folder);
|
|
|
|
|
|
|
|
static gint mh_create_tree (Folder *folder);
|
|
|
|
static FolderItem *mh_create_folder (Folder *folder,
|
|
|
|
FolderItem *parent,
|
|
|
|
const gchar *name);
|
|
|
|
static gint mh_rename_folder (Folder *folder,
|
|
|
|
FolderItem *item,
|
|
|
|
const gchar *name);
|
|
|
|
static gint mh_remove_folder (Folder *folder,
|
|
|
|
FolderItem *item);
|
|
|
|
|
|
|
|
static gchar *mh_get_new_msg_filename (FolderItem *dest);
|
|
|
|
|
|
|
|
static MsgInfo *mh_parse_msg (const gchar *file,
|
|
|
|
FolderItem *item);
|
2003-08-25 16:14:47 +02:00
|
|
|
static void mh_remove_missing_folder_items (Folder *folder);
|
2003-10-05 12:10:30 +02:00
|
|
|
static gchar *mh_filename_from_utf8 (const gchar *path);
|
|
|
|
static gchar *mh_filename_to_utf8 (const gchar *path);
|
2004-01-12 22:28:31 +01:00
|
|
|
static void mh_scan_tree_recursive (FolderItem *item);
|
2003-04-02 17:33:25 +02:00
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gboolean mh_rename_folder_func (GNode *node,
|
|
|
|
gpointer data);
|
|
|
|
static gchar *mh_item_get_path (Folder *folder,
|
|
|
|
FolderItem *item);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2005-02-16 08:35:34 +01:00
|
|
|
static gboolean mh_scan_required (Folder *folder,
|
|
|
|
FolderItem *item);
|
2007-09-26 09:53:13 +02:00
|
|
|
static void mh_set_mtime (Folder *folder,
|
|
|
|
FolderItem *item);
|
2006-05-12 19:46:09 +02:00
|
|
|
static int mh_item_close (Folder *folder,
|
|
|
|
FolderItem *item);
|
2006-06-17 20:50:41 +02:00
|
|
|
#if 0
|
2006-05-12 19:46:09 +02:00
|
|
|
static gint mh_get_flags (Folder *folder, FolderItem *item,
|
|
|
|
MsgInfoList *msginfo_list, GRelation *msgflags);
|
2006-06-17 20:50:41 +02:00
|
|
|
#endif
|
2006-05-23 19:42:52 +02:00
|
|
|
static void mh_write_sequences (FolderItem *item, gboolean remove_unseen);
|
2005-02-16 08:35:34 +01:00
|
|
|
|
2004-05-10 12:22:28 +02:00
|
|
|
static FolderClass mh_class;
|
2003-03-12 16:40:51 +01:00
|
|
|
|
2003-05-13 13:54:19 +02:00
|
|
|
FolderClass *mh_get_class(void)
|
2003-03-12 23:23:54 +01:00
|
|
|
{
|
2004-05-10 12:22:28 +02:00
|
|
|
if (mh_class.idstr == NULL) {
|
|
|
|
mh_class.type = F_MH;
|
|
|
|
mh_class.idstr = "mh";
|
|
|
|
mh_class.uistr = "MH";
|
|
|
|
|
|
|
|
/* Folder functions */
|
|
|
|
mh_class.new_folder = mh_folder_new;
|
|
|
|
mh_class.destroy_folder = mh_folder_destroy;
|
|
|
|
mh_class.set_xml = folder_local_set_xml;
|
|
|
|
mh_class.get_xml = folder_local_get_xml;
|
|
|
|
mh_class.scan_tree = mh_scan_tree;
|
|
|
|
mh_class.create_tree = mh_create_tree;
|
|
|
|
|
|
|
|
/* FolderItem functions */
|
|
|
|
mh_class.item_get_path = mh_item_get_path;
|
|
|
|
mh_class.create_folder = mh_create_folder;
|
|
|
|
mh_class.rename_folder = mh_rename_folder;
|
|
|
|
mh_class.remove_folder = mh_remove_folder;
|
|
|
|
mh_class.get_num_list = mh_get_num_list;
|
2005-02-16 08:35:34 +01:00
|
|
|
mh_class.scan_required = mh_scan_required;
|
2007-09-26 09:53:13 +02:00
|
|
|
mh_class.set_mtime = mh_set_mtime;
|
2006-05-12 19:46:09 +02:00
|
|
|
mh_class.close = mh_item_close;
|
2006-05-25 20:34:08 +02:00
|
|
|
mh_class.get_flags = NULL; /*mh_get_flags */;
|
2006-05-12 19:46:09 +02:00
|
|
|
|
2004-05-10 12:22:28 +02:00
|
|
|
/* Message functions */
|
|
|
|
mh_class.get_msginfo = mh_get_msginfo;
|
|
|
|
mh_class.fetch_msg = mh_fetch_msg;
|
|
|
|
mh_class.add_msg = mh_add_msg;
|
|
|
|
mh_class.add_msgs = mh_add_msgs;
|
|
|
|
mh_class.copy_msg = mh_copy_msg;
|
2005-12-30 18:36:08 +01:00
|
|
|
mh_class.copy_msgs = mh_copy_msgs;
|
2004-05-10 12:22:28 +02:00
|
|
|
mh_class.remove_msg = mh_remove_msg;
|
2006-05-26 10:32:09 +02:00
|
|
|
mh_class.remove_msgs = mh_remove_msgs;
|
2004-05-10 12:22:28 +02:00
|
|
|
mh_class.remove_all_msg = mh_remove_all_msg;
|
|
|
|
mh_class.is_msg_changed = mh_is_msg_changed;
|
|
|
|
}
|
|
|
|
|
2003-03-12 23:23:54 +01:00
|
|
|
return &mh_class;
|
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static Folder *mh_folder_new(const gchar *name, const gchar *path)
|
2002-01-16 12:48:36 +01:00
|
|
|
{
|
|
|
|
Folder *folder;
|
|
|
|
|
|
|
|
folder = (Folder *)g_new0(MHFolder, 1);
|
2003-04-05 22:38:53 +02:00
|
|
|
folder->klass = &mh_class;
|
2002-01-16 12:48:36 +01:00
|
|
|
mh_folder_init(folder, name, path);
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
2003-04-06 10:38:54 +02:00
|
|
|
static void mh_folder_destroy(Folder *folder)
|
2002-01-16 12:48:36 +01:00
|
|
|
{
|
|
|
|
folder_local_folder_destroy(LOCAL_FOLDER(folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
|
|
|
|
{
|
2002-07-25 23:09:10 +02:00
|
|
|
folder_local_folder_init(folder, name, path);
|
|
|
|
|
2002-01-16 12:48:36 +01:00
|
|
|
}
|
|
|
|
|
2005-02-16 08:35:34 +01:00
|
|
|
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);
|
|
|
|
|
2008-09-30 17:34:25 +02:00
|
|
|
if (g_stat(path, &s) < 0) {
|
2005-02-16 08:35:34 +01:00
|
|
|
FILE_OP_ERROR(path, "stat");
|
|
|
|
g_free(path);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-04-04 23:11:28 +02:00
|
|
|
if ((s.st_mtime > item->mtime) &&
|
|
|
|
(s.st_mtime - 3600 != item->mtime)) {
|
2005-02-16 08:35:34 +01:00
|
|
|
debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
|
2007-08-13 23:36:57 +02:00
|
|
|
path?path:"(null)",
|
2005-09-14 10:11:20 +02:00
|
|
|
(long int) s.st_mtime,
|
|
|
|
(long int) item->mtime);
|
2005-02-16 08:35:34 +01:00
|
|
|
g_free(path);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_print("MH scan not required: %s (%ld <= %ld)\n",
|
2007-08-13 23:36:57 +02:00
|
|
|
path?path:"(null)",
|
2005-09-14 10:11:20 +02:00
|
|
|
(long int) s.st_mtime,
|
|
|
|
(long int) item->mtime);
|
2005-02-16 08:35:34 +01:00
|
|
|
g_free(path);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-01-20 20:54:05 +01:00
|
|
|
static void mh_get_last_num(Folder *folder, FolderItem *item)
|
2002-06-30 01:33:42 +02:00
|
|
|
{
|
|
|
|
gchar *path;
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
|
|
|
gint max = 0;
|
|
|
|
gint num;
|
|
|
|
|
|
|
|
g_return_if_fail(item != NULL);
|
|
|
|
|
2007-08-13 23:36:57 +02:00
|
|
|
debug_print("mh_get_last_num(): Scanning %s ...\n", item->path?item->path:"(null)");
|
2002-06-30 01:33:42 +02:00
|
|
|
|
|
|
|
path = folder_item_get_path(item);
|
|
|
|
g_return_if_fail(path != NULL);
|
|
|
|
if (change_dir(path) < 0) {
|
|
|
|
g_free(path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_free(path);
|
|
|
|
|
|
|
|
if ((dp = opendir(".")) == NULL) {
|
|
|
|
FILE_OP_ERROR(item->path, "opendir");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((d = readdir(dp)) != NULL) {
|
2005-01-24 10:43:34 +01:00
|
|
|
if ((num = to_number(d->d_name)) > 0 &&
|
2004-01-12 22:28:31 +01:00
|
|
|
dirent_is_regular_file(d)) {
|
2002-06-30 01:33:42 +02:00
|
|
|
if (max < num)
|
|
|
|
max = num;
|
|
|
|
}
|
2007-08-05 00:19:17 +02:00
|
|
|
if (num % 2000 == 0)
|
2007-08-03 09:15:57 +02:00
|
|
|
GTK_EVENTS_FLUSH();
|
2002-06-30 01:33:42 +02:00
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
|
2007-08-13 23:36:57 +02:00
|
|
|
debug_print("Last number in dir %s = %d\n", item->path?item->path:"(null)", max);
|
2002-06-30 01:33:42 +02:00
|
|
|
item->last_num = max;
|
|
|
|
}
|
|
|
|
|
2003-08-21 22:08:27 +02:00
|
|
|
gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
|
2002-06-30 01:33:42 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
gchar *path;
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
2002-10-18 12:02:51 +02:00
|
|
|
gint num, nummsgs = 0;
|
2002-06-30 01:33:42 +02:00
|
|
|
|
2002-10-18 12:02:51 +02:00
|
|
|
g_return_val_if_fail(item != NULL, -1);
|
2002-06-30 01:33:42 +02:00
|
|
|
|
2007-08-13 23:36:57 +02:00
|
|
|
debug_print("mh_get_num_list(): Scanning %s ...\n", item->path?item->path:"(null)");
|
2002-06-30 01:33:42 +02:00
|
|
|
|
2003-08-21 22:08:27 +02:00
|
|
|
*old_uids_valid = TRUE;
|
|
|
|
|
2002-06-30 01:33:42 +02:00
|
|
|
path = folder_item_get_path(item);
|
2002-10-18 12:02:51 +02:00
|
|
|
g_return_val_if_fail(path != NULL, -1);
|
2002-06-30 01:33:42 +02:00
|
|
|
if (change_dir(path) < 0) {
|
|
|
|
g_free(path);
|
2002-10-18 12:02:51 +02:00
|
|
|
return -1;
|
2002-06-30 01:33:42 +02:00
|
|
|
}
|
|
|
|
g_free(path);
|
|
|
|
|
|
|
|
if ((dp = opendir(".")) == NULL) {
|
|
|
|
FILE_OP_ERROR(item->path, "opendir");
|
2002-10-18 12:02:51 +02:00
|
|
|
return -1;
|
2002-06-30 01:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while ((d = readdir(dp)) != NULL) {
|
2005-01-24 10:43:34 +01:00
|
|
|
if ((num = to_number(d->d_name)) > 0) {
|
2002-10-18 12:02:51 +02:00
|
|
|
*list = g_slist_prepend(*list, GINT_TO_POINTER(num));
|
2004-01-12 22:28:31 +01:00
|
|
|
nummsgs++;
|
2002-06-30 01:33:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
|
2007-09-26 09:53:13 +02:00
|
|
|
mh_set_mtime(folder, item);
|
2002-10-18 12:02:51 +02:00
|
|
|
return nummsgs;
|
2002-06-30 01:33:42 +02:00
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
|
|
|
gchar *path;
|
|
|
|
gchar *file;
|
|
|
|
|
|
|
|
g_return_val_if_fail(item != NULL, NULL);
|
2002-06-30 01:33:42 +02:00
|
|
|
g_return_val_if_fail(num > 0, NULL);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
path = folder_item_get_path(item);
|
|
|
|
file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
|
2003-10-05 12:10:30 +02:00
|
|
|
|
2001-04-19 14:21:46 +02:00
|
|
|
if (!is_file_exist(file)) {
|
|
|
|
g_free(file);
|
2005-06-17 12:13:08 +02:00
|
|
|
g_free(path);
|
2001-04-19 14:21:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-06-17 12:13:08 +02:00
|
|
|
g_free(path);
|
2001-04-19 14:21:46 +02:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
|
2002-06-30 01:33:42 +02:00
|
|
|
{
|
|
|
|
MsgInfo *msginfo;
|
2002-11-16 20:53:47 +01:00
|
|
|
gchar *file;
|
2002-06-30 01:33:42 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail(item != NULL, NULL);
|
2006-05-16 11:38:20 +02:00
|
|
|
if (num <= 0)
|
|
|
|
return NULL;
|
2002-06-30 01:33:42 +02:00
|
|
|
|
2002-11-16 20:53:47 +01:00
|
|
|
file = mh_fetch_msg(folder, item, num);
|
|
|
|
if (!file) return NULL;
|
2002-07-23 15:32:15 +02:00
|
|
|
|
2002-11-16 20:53:47 +01:00
|
|
|
msginfo = mh_parse_msg(file, item);
|
2003-02-02 10:12:44 +01:00
|
|
|
if (msginfo)
|
|
|
|
msginfo->msgnum = num;
|
2002-06-30 01:33:42 +02:00
|
|
|
|
2002-07-23 15:25:17 +02:00
|
|
|
g_free(file);
|
|
|
|
|
2002-06-30 01:33:42 +02:00
|
|
|
return msginfo;
|
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gchar *mh_get_new_msg_filename(FolderItem *dest)
|
2002-01-07 21:25:37 +01:00
|
|
|
{
|
|
|
|
gchar *destfile;
|
|
|
|
gchar *destpath;
|
|
|
|
|
|
|
|
destpath = folder_item_get_path(dest);
|
|
|
|
g_return_val_if_fail(destpath != NULL, NULL);
|
2002-04-21 11:24:27 +02:00
|
|
|
|
2002-01-07 21:25:37 +01:00
|
|
|
if (!is_dir_exist(destpath))
|
|
|
|
make_dir_hier(destpath);
|
|
|
|
|
2002-04-21 11:24:27 +02:00
|
|
|
for (;;) {
|
2002-01-07 21:25:37 +01:00
|
|
|
destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
|
|
|
|
dest->last_num + 1);
|
2002-04-21 11:24:27 +02:00
|
|
|
if (is_file_entry_exist(destfile)) {
|
2002-01-07 21:25:37 +01:00
|
|
|
dest->last_num++;
|
|
|
|
g_free(destfile);
|
2002-04-21 11:24:27 +02:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-01-07 21:25:37 +01:00
|
|
|
g_free(destpath);
|
|
|
|
|
|
|
|
return destfile;
|
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-07-25 20:47:43 +02:00
|
|
|
gint ret;
|
2003-07-26 20:30:25 +02:00
|
|
|
GSList file_list;
|
|
|
|
MsgFileInfo fileinfo;
|
|
|
|
|
|
|
|
g_return_val_if_fail(file != NULL, -1);
|
|
|
|
|
2003-07-29 00:30:01 +02:00
|
|
|
fileinfo.msginfo = NULL;
|
2003-07-26 20:30:25 +02:00
|
|
|
fileinfo.file = (gchar *)file;
|
|
|
|
fileinfo.flags = flags;
|
|
|
|
file_list.data = &fileinfo;
|
|
|
|
file_list.next = NULL;
|
|
|
|
|
2003-07-28 17:47:24 +02:00
|
|
|
ret = mh_add_msgs(folder, dest, &file_list, NULL);
|
2003-07-25 20:47:43 +02:00
|
|
|
return ret;
|
2003-07-23 16:51:47 +02:00
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
|
2003-07-28 17:47:24 +02:00
|
|
|
GRelation *relation)
|
2003-07-23 16:51:47 +02:00
|
|
|
{
|
2001-04-19 14:21:46 +02:00
|
|
|
gchar *destfile;
|
2003-07-26 20:30:25 +02:00
|
|
|
GSList *cur;
|
|
|
|
MsgFileInfo *fileinfo;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail(dest != NULL, -1);
|
2003-07-23 16:51:47 +02:00
|
|
|
g_return_val_if_fail(file_list != NULL, -1);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
if (dest->last_num < 0) {
|
2002-06-30 01:33:42 +02:00
|
|
|
mh_get_last_num(folder, dest);
|
2001-04-19 14:21:46 +02:00
|
|
|
if (dest->last_num < 0) return -1;
|
|
|
|
}
|
|
|
|
|
2003-07-26 20:30:25 +02:00
|
|
|
for (cur = file_list; cur != NULL; cur = cur->next) {
|
|
|
|
fileinfo = (MsgFileInfo *)cur->data;
|
|
|
|
|
|
|
|
destfile = mh_get_new_msg_filename(dest);
|
|
|
|
if (destfile == NULL) return -1;
|
|
|
|
|
2005-08-19 11:30:30 +02:00
|
|
|
#ifdef G_OS_UNIX
|
2003-07-26 20:30:25 +02:00
|
|
|
if (link(fileinfo->file, destfile) < 0) {
|
2005-08-19 11:30:30 +02:00
|
|
|
#endif
|
2003-07-26 20:30:25 +02:00
|
|
|
if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
|
|
|
|
g_warning(_("can't copy message %s to %s\n"),
|
|
|
|
fileinfo->file, destfile);
|
|
|
|
g_free(destfile);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-08-19 11:30:30 +02:00
|
|
|
#ifdef G_OS_UNIX
|
2001-05-17 15:25:31 +02:00
|
|
|
}
|
2005-08-19 11:30:30 +02:00
|
|
|
#endif
|
|
|
|
|
2003-07-28 17:47:24 +02:00
|
|
|
if (relation != NULL)
|
|
|
|
g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
|
2003-07-23 16:51:47 +02:00
|
|
|
g_free(destfile);
|
|
|
|
dest->last_num++;
|
2001-05-17 15:25:31 +02:00
|
|
|
}
|
2006-05-23 19:42:52 +02:00
|
|
|
mh_write_sequences(dest, TRUE);
|
2003-07-26 20:30:25 +02:00
|
|
|
return dest->last_num;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
|
2005-12-30 18:36:08 +01:00
|
|
|
{
|
|
|
|
GSList msglist;
|
|
|
|
|
|
|
|
g_return_val_if_fail(msginfo != NULL, -1);
|
|
|
|
|
|
|
|
msglist.data = msginfo;
|
|
|
|
msglist.next = NULL;
|
|
|
|
|
|
|
|
return mh_copy_msgs(folder, dest, &msglist, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
|
|
|
|
GRelation *relation)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2005-02-16 08:35:34 +01:00
|
|
|
gboolean dest_need_scan = FALSE;
|
2007-07-24 18:22:28 +02:00
|
|
|
gboolean src_need_scan = FALSE;
|
|
|
|
FolderItem *src = NULL;
|
2001-04-19 14:21:46 +02:00
|
|
|
gchar *srcfile;
|
|
|
|
gchar *destfile;
|
2001-07-30 07:49:36 +02:00
|
|
|
gint filemode = 0;
|
2003-07-20 03:15:54 +02:00
|
|
|
FolderItemPrefs *prefs;
|
2005-12-30 18:36:08 +01:00
|
|
|
MsgInfo *msginfo = NULL;
|
|
|
|
MsgInfoList *cur = NULL;
|
2006-05-24 20:02:15 +02:00
|
|
|
gint curnum = 0, total = 0;
|
2006-05-26 10:32:09 +02:00
|
|
|
gchar *srcpath = NULL;
|
2006-05-29 11:00:26 +02:00
|
|
|
gboolean full_fetch = FALSE;
|
2007-07-24 18:22:28 +02:00
|
|
|
time_t last_dest_mtime = (time_t)0;
|
|
|
|
time_t last_src_mtime = (time_t)0;
|
2006-05-24 20:02:15 +02:00
|
|
|
|
2001-04-19 14:21:46 +02:00
|
|
|
g_return_val_if_fail(dest != NULL, -1);
|
2005-12-30 18:36:08 +01:00
|
|
|
g_return_val_if_fail(msglist != NULL, -1);
|
|
|
|
|
|
|
|
msginfo = (MsgInfo *)msglist->data;
|
|
|
|
|
2001-04-19 14:21:46 +02:00
|
|
|
g_return_val_if_fail(msginfo != NULL, -1);
|
|
|
|
|
|
|
|
if (msginfo->folder == dest) {
|
2002-12-10 19:42:29 +01:00
|
|
|
g_warning("the src folder is identical to the dest.\n");
|
2001-04-19 14:21:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-05-29 11:00:26 +02:00
|
|
|
if (msginfo->folder->folder != dest->folder)
|
|
|
|
full_fetch = TRUE;
|
2007-07-24 18:22:28 +02:00
|
|
|
|
|
|
|
if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
|
|
|
|
src = msginfo->folder;
|
|
|
|
}
|
2006-05-29 11:00:26 +02:00
|
|
|
|
2001-04-19 14:21:46 +02:00
|
|
|
if (dest->last_num < 0) {
|
2002-06-30 01:33:42 +02:00
|
|
|
mh_get_last_num(folder, dest);
|
2001-04-19 14:21:46 +02:00
|
|
|
if (dest->last_num < 0) return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-30 18:36:08 +01:00
|
|
|
prefs = dest->prefs;
|
|
|
|
|
2006-05-26 10:32:09 +02:00
|
|
|
srcpath = folder_item_get_path(msginfo->folder);
|
|
|
|
|
2006-08-22 22:37:43 +02:00
|
|
|
dest_need_scan = mh_scan_required(dest->folder, dest);
|
2007-07-24 18:22:28 +02:00
|
|
|
last_dest_mtime = dest->mtime;
|
|
|
|
|
|
|
|
if (src) {
|
|
|
|
src_need_scan = mh_scan_required(src->folder, src);
|
|
|
|
last_src_mtime = src->mtime;
|
|
|
|
}
|
2006-08-22 22:37:43 +02:00
|
|
|
|
2006-05-24 20:02:15 +02:00
|
|
|
total = g_slist_length(msglist);
|
2006-05-26 10:32:09 +02:00
|
|
|
if (total > 100) {
|
|
|
|
if (MSG_IS_MOVE(msginfo->flags))
|
|
|
|
statusbar_print_all(_("Moving messages..."));
|
|
|
|
else
|
|
|
|
statusbar_print_all(_("Copying messages..."));
|
|
|
|
}
|
2005-12-30 18:36:08 +01:00
|
|
|
for (cur = msglist; cur; cur = cur->next) {
|
|
|
|
msginfo = (MsgInfo *)cur->data;
|
2006-05-26 10:32:09 +02:00
|
|
|
if (!msginfo) {
|
|
|
|
goto err_reset_status;
|
|
|
|
}
|
2006-05-29 11:00:26 +02:00
|
|
|
if (!full_fetch) {
|
|
|
|
srcfile = g_strconcat(srcpath,
|
|
|
|
G_DIR_SEPARATOR_S,
|
|
|
|
itos(msginfo->msgnum), NULL);
|
|
|
|
} else {
|
|
|
|
srcfile = procmsg_get_message_file(msginfo);
|
|
|
|
}
|
2006-05-26 10:32:09 +02:00
|
|
|
if (!srcfile) {
|
|
|
|
goto err_reset_status;
|
|
|
|
}
|
2005-12-30 18:36:08 +01:00
|
|
|
destfile = mh_get_new_msg_filename(dest);
|
|
|
|
if (!destfile) {
|
|
|
|
g_free(srcfile);
|
2006-05-26 10:32:09 +02:00
|
|
|
goto err_reset_status;
|
2005-12-30 18:36:08 +01:00
|
|
|
}
|
|
|
|
|
2006-05-26 10:32:09 +02:00
|
|
|
if (total > 100) {
|
|
|
|
statusbar_progress_all(curnum, total, 100);
|
|
|
|
if (curnum % 100 == 0)
|
|
|
|
GTK_EVENTS_FLUSH();
|
|
|
|
curnum++;
|
|
|
|
}
|
2006-05-24 20:02:15 +02:00
|
|
|
|
2005-12-30 18:36:08 +01:00
|
|
|
debug_print("Copying message %s%c%d to %s ...\n",
|
|
|
|
msginfo->folder->path, G_DIR_SEPARATOR,
|
|
|
|
msginfo->msgnum, dest->path);
|
|
|
|
|
|
|
|
|
2006-06-12 19:08:19 +02:00
|
|
|
if (MSG_IS_MOVE(msginfo->flags)) {
|
2006-08-25 19:02:15 +02:00
|
|
|
msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
|
2006-05-26 10:32:09 +02:00
|
|
|
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;
|
|
|
|
}
|
2006-08-25 19:02:15 +02:00
|
|
|
} else {
|
|
|
|
/* say unlinking's not necessary */
|
|
|
|
msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
|
2005-12-30 18:36:08 +01:00
|
|
|
}
|
|
|
|
} else if (copy_file(srcfile, destfile, TRUE) < 0) {
|
|
|
|
FILE_OP_ERROR(srcfile, "copy");
|
|
|
|
g_free(srcfile);
|
|
|
|
g_free(destfile);
|
2006-05-26 10:32:09 +02:00
|
|
|
goto err_reset_status;
|
|
|
|
}
|
2005-12-30 18:36:08 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (relation)
|
|
|
|
g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
|
2001-04-19 14:21:46 +02:00
|
|
|
g_free(srcfile);
|
|
|
|
g_free(destfile);
|
2005-12-30 18:36:08 +01:00
|
|
|
dest->last_num++;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2006-05-26 10:32:09 +02:00
|
|
|
g_free(srcpath);
|
2006-05-23 19:42:52 +02:00
|
|
|
mh_write_sequences(dest, TRUE);
|
|
|
|
|
2007-07-24 18:22:28 +02:00
|
|
|
if (dest->mtime == last_dest_mtime && !dest_need_scan) {
|
2007-09-26 09:53:13 +02:00
|
|
|
mh_set_mtime(folder, dest);
|
2006-09-30 21:36:06 +02:00
|
|
|
}
|
2007-07-24 18:22:28 +02:00
|
|
|
|
|
|
|
if (src && src->mtime == last_src_mtime && !src_need_scan) {
|
2007-09-26 09:53:13 +02:00
|
|
|
mh_set_mtime(folder, src);
|
2007-07-24 18:22:28 +02:00
|
|
|
}
|
|
|
|
|
2006-05-26 10:32:09 +02:00
|
|
|
if (total > 100) {
|
|
|
|
statusbar_progress_all(0,0,0);
|
|
|
|
statusbar_pop_all();
|
|
|
|
}
|
2001-04-19 14:21:46 +02:00
|
|
|
return dest->last_num;
|
2006-05-26 10:32:09 +02:00
|
|
|
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;
|
|
|
|
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
2001-05-28 00:40:29 +02:00
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2005-02-16 08:35:34 +01:00
|
|
|
gboolean need_scan = FALSE;
|
2006-08-24 08:34:54 +02:00
|
|
|
time_t last_mtime = (time_t)0;
|
2001-04-19 14:21:46 +02:00
|
|
|
gchar *file;
|
|
|
|
|
|
|
|
g_return_val_if_fail(item != NULL, -1);
|
|
|
|
|
|
|
|
file = mh_fetch_msg(folder, item, num);
|
|
|
|
g_return_val_if_fail(file != NULL, -1);
|
|
|
|
|
2005-02-16 08:35:34 +01:00
|
|
|
need_scan = mh_scan_required(folder, item);
|
2006-08-24 08:34:54 +02:00
|
|
|
last_mtime = item->mtime;
|
2005-02-16 08:35:34 +01:00
|
|
|
|
2008-04-18 18:26:05 +02:00
|
|
|
if (claws_unlink(file) < 0) {
|
2001-04-19 14:21:46 +02:00
|
|
|
FILE_OP_ERROR(file, "unlink");
|
|
|
|
g_free(file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-09-30 21:36:06 +02:00
|
|
|
if (item->mtime == last_mtime && !need_scan) {
|
2007-09-26 09:53:13 +02:00
|
|
|
mh_set_mtime(folder, item);
|
2006-09-30 21:36:06 +02:00
|
|
|
}
|
2001-04-19 14:21:46 +02:00
|
|
|
g_free(file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-05-26 10:32:09 +02:00
|
|
|
static gint mh_remove_msgs(Folder *folder, FolderItem *item,
|
|
|
|
MsgInfoList *msglist, GRelation *relation)
|
|
|
|
{
|
|
|
|
gboolean need_scan = FALSE;
|
|
|
|
gchar *path, *file;
|
2006-08-24 08:34:54 +02:00
|
|
|
time_t last_mtime = (time_t)0;
|
2006-05-26 10:32:09 +02:00
|
|
|
MsgInfoList *cur;
|
2006-08-25 19:02:15 +02:00
|
|
|
gint total = 0, curnum = 0;
|
2006-05-26 10:32:09 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail(item != NULL, -1);
|
|
|
|
|
|
|
|
path = folder_item_get_path(item);
|
|
|
|
|
2006-08-22 22:37:43 +02:00
|
|
|
need_scan = mh_scan_required(folder, item);
|
2006-08-24 08:34:54 +02:00
|
|
|
last_mtime = item->mtime;
|
2006-08-22 22:37:43 +02:00
|
|
|
|
2006-08-25 19:02:15 +02:00
|
|
|
total = g_slist_length(msglist);
|
|
|
|
if (total > 100) {
|
|
|
|
statusbar_print_all(_("Deleting messages..."));
|
|
|
|
}
|
|
|
|
|
2006-05-26 10:32:09 +02:00
|
|
|
for (cur = msglist; cur; cur = cur->next) {
|
|
|
|
MsgInfo *msginfo = (MsgInfo *)cur->data;
|
|
|
|
if (msginfo == NULL)
|
|
|
|
continue;
|
2006-08-25 19:02:15 +02:00
|
|
|
if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
|
|
|
|
msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (total > 100) {
|
|
|
|
statusbar_progress_all(curnum, total, 100);
|
|
|
|
if (curnum % 100 == 0)
|
|
|
|
GTK_EVENTS_FLUSH();
|
|
|
|
curnum++;
|
|
|
|
}
|
2006-08-28 14:10:36 +02:00
|
|
|
|
2006-05-26 10:32:09 +02:00
|
|
|
file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
|
|
|
|
if (file == NULL)
|
|
|
|
continue;
|
|
|
|
|
2008-04-18 18:26:05 +02:00
|
|
|
if (claws_unlink(file) < 0) {
|
2006-05-26 10:32:09 +02:00
|
|
|
g_free(file);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(file);
|
|
|
|
}
|
|
|
|
|
2006-08-25 19:02:15 +02:00
|
|
|
if (total > 100) {
|
|
|
|
statusbar_progress_all(0,0,0);
|
|
|
|
statusbar_pop_all();
|
|
|
|
}
|
2006-09-30 21:36:06 +02:00
|
|
|
if (item->mtime == last_mtime && !need_scan) {
|
2007-09-26 09:53:13 +02:00
|
|
|
mh_set_mtime(folder, item);
|
2006-09-30 21:36:06 +02:00
|
|
|
}
|
2006-05-26 10:32:09 +02:00
|
|
|
|
|
|
|
g_free(path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
|
|
|
gchar *path;
|
2001-06-06 15:05:23 +02:00
|
|
|
gint val;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail(item != NULL, -1);
|
|
|
|
|
|
|
|
path = folder_item_get_path(item);
|
|
|
|
g_return_val_if_fail(path != NULL, -1);
|
2001-06-06 15:05:23 +02:00
|
|
|
val = remove_all_numbered_files(path);
|
2001-04-19 14:21:46 +02:00
|
|
|
g_free(path);
|
|
|
|
|
2006-05-23 19:42:52 +02:00
|
|
|
mh_write_sequences(item, TRUE);
|
2006-05-12 19:46:09 +02:00
|
|
|
|
2001-06-06 15:05:23 +02:00
|
|
|
return val;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
|
|
|
|
MsgInfo *msginfo)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
|
2005-08-19 11:30:30 +02:00
|
|
|
if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
|
2005-06-01 19:44:37 +02:00
|
|
|
msginfo->size != s.st_size || (
|
2005-04-04 23:11:28 +02:00
|
|
|
(msginfo->mtime - s.st_mtime != 0) &&
|
|
|
|
(msginfo->mtime - s.st_mtime != 3600) &&
|
2005-06-01 19:44:37 +02:00
|
|
|
(msginfo->mtime - s.st_mtime != -3600)))
|
2001-04-19 14:21:46 +02:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_scan_tree(Folder *folder)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
|
|
|
FolderItem *item;
|
|
|
|
gchar *rootpath;
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
g_return_val_if_fail(folder != NULL, -1);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-08-25 16:14:47 +02:00
|
|
|
if (!folder->node) {
|
|
|
|
item = folder_item_new(folder, folder->name, NULL);
|
|
|
|
item->folder = folder;
|
|
|
|
folder->node = item->node = g_node_new(item);
|
|
|
|
} else
|
|
|
|
item = FOLDER_ITEM(folder->node->data);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
rootpath = folder_item_get_path(item);
|
|
|
|
if (change_dir(rootpath) < 0) {
|
|
|
|
g_free(rootpath);
|
2004-01-12 22:28:31 +01:00
|
|
|
return -1;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
g_free(rootpath);
|
|
|
|
|
2002-03-19 09:37:53 +01:00
|
|
|
mh_create_tree(folder);
|
2003-08-25 16:14:47 +02:00
|
|
|
mh_remove_missing_folder_items(folder);
|
2002-07-02 21:49:11 +02:00
|
|
|
mh_scan_tree_recursive(item);
|
2004-01-12 22:28:31 +01:00
|
|
|
|
|
|
|
return 0;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MAKE_DIR_IF_NOT_EXIST(dir) \
|
|
|
|
{ \
|
|
|
|
if (!is_dir_exist(dir)) { \
|
|
|
|
if (is_file_exist(dir)) { \
|
2002-12-10 19:42:29 +01:00
|
|
|
g_warning("File `%s' already exists.\n" \
|
|
|
|
"Can't create folder.", dir); \
|
2001-04-19 14:21:46 +02:00
|
|
|
return -1; \
|
|
|
|
} \
|
2007-03-20 19:16:07 +01:00
|
|
|
if (make_dir_hier(dir) < 0) \
|
2001-04-19 14:21:46 +02:00
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_create_tree(Folder *folder)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
|
|
|
gchar *rootpath;
|
|
|
|
|
|
|
|
g_return_val_if_fail(folder != NULL, -1);
|
|
|
|
|
2005-08-19 11:30:30 +02:00
|
|
|
CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
|
2001-04-19 14:21:46 +02:00
|
|
|
rootpath = LOCAL_FOLDER(folder)->rootpath;
|
|
|
|
MAKE_DIR_IF_NOT_EXIST(rootpath);
|
|
|
|
CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
|
|
|
|
MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
|
|
|
|
MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
|
|
|
|
MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
|
|
|
|
MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
|
|
|
|
MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef MAKE_DIR_IF_NOT_EXIST
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
|
2003-07-03 22:37:07 +02:00
|
|
|
{
|
|
|
|
gchar *folder_path, *path;
|
2005-06-25 14:06:38 +02:00
|
|
|
gchar *real_path;
|
2003-07-03 22:37:07 +02:00
|
|
|
g_return_val_if_fail(folder != NULL, NULL);
|
|
|
|
g_return_val_if_fail(item != NULL, NULL);
|
|
|
|
|
|
|
|
folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
|
|
|
|
g_return_val_if_fail(folder_path != NULL, NULL);
|
|
|
|
|
2005-12-23 08:36:23 +01:00
|
|
|
/* FIXME: [W32] The code below does not correctly merge
|
|
|
|
relative filenames; there should be a function to handle
|
|
|
|
this. */
|
|
|
|
if ( !is_relative_filename (folder_path) ) {
|
2003-07-03 22:37:07 +02:00
|
|
|
if (item->path)
|
|
|
|
path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
|
|
|
|
item->path, NULL);
|
|
|
|
else
|
|
|
|
path = g_strdup(folder_path);
|
|
|
|
} else {
|
|
|
|
if (item->path)
|
|
|
|
path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
|
|
|
|
folder_path, G_DIR_SEPARATOR_S,
|
|
|
|
item->path, NULL);
|
|
|
|
else
|
|
|
|
path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
|
|
|
|
folder_path, NULL);
|
|
|
|
}
|
|
|
|
g_free(folder_path);
|
2005-06-25 14:06:38 +02:00
|
|
|
real_path = mh_filename_from_utf8(path);
|
2005-06-25 14:19:54 +02:00
|
|
|
if (!is_dir_exist(real_path) && is_dir_exist(path)) {
|
|
|
|
/* mmh, older version did put utf8 filenames instead of
|
|
|
|
* the correct encoding */
|
|
|
|
rename(path, real_path);
|
|
|
|
folder_item_scan(item);
|
|
|
|
}
|
|
|
|
|
2005-06-25 14:06:38 +02:00
|
|
|
g_free(path);
|
|
|
|
return real_path;
|
2003-07-03 22:37:07 +02:00
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
|
|
|
|
const gchar *name)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-10-05 12:10:30 +02:00
|
|
|
gchar *path, *real_name;
|
2001-04-19 14:21:46 +02:00
|
|
|
gchar *fullpath;
|
|
|
|
FolderItem *new_item;
|
2002-12-13 14:38:56 +01:00
|
|
|
gchar *mh_sequences_filename;
|
|
|
|
FILE *mh_sequences_file;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail(folder != NULL, NULL);
|
|
|
|
g_return_val_if_fail(parent != NULL, NULL);
|
|
|
|
g_return_val_if_fail(name != NULL, NULL);
|
|
|
|
|
|
|
|
path = folder_item_get_path(parent);
|
2002-11-06 23:32:44 +01:00
|
|
|
if (!is_dir_exist(path))
|
|
|
|
if (make_dir_hier(path) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
2003-10-05 12:10:30 +02:00
|
|
|
real_name = mh_filename_from_utf8(name);
|
|
|
|
fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
|
|
|
|
g_free(real_name);
|
2001-04-19 14:21:46 +02:00
|
|
|
g_free(path);
|
|
|
|
|
2002-05-28 08:42:48 +02:00
|
|
|
if (make_dir(fullpath) < 0) {
|
2001-04-19 14:21:46 +02:00
|
|
|
g_free(fullpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(fullpath);
|
|
|
|
|
|
|
|
if (parent->path)
|
|
|
|
path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
|
|
|
|
NULL);
|
|
|
|
else
|
|
|
|
path = g_strdup(name);
|
2002-07-25 23:09:10 +02:00
|
|
|
new_item = folder_item_new(folder, name, path);
|
2001-04-19 14:21:46 +02:00
|
|
|
folder_item_append(parent, new_item);
|
2002-12-13 14:38:56 +01:00
|
|
|
|
2001-04-19 14:21:46 +02:00
|
|
|
g_free(path);
|
|
|
|
|
2002-12-13 14:38:56 +01:00
|
|
|
path = folder_item_get_path(new_item);
|
|
|
|
mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
|
|
|
|
".mh_sequences", NULL);
|
2005-08-19 11:30:30 +02:00
|
|
|
if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
|
2002-12-13 14:38:56 +01:00
|
|
|
fclose(mh_sequences_file);
|
|
|
|
}
|
|
|
|
g_free(mh_sequences_filename);
|
2002-12-16 18:24:54 +01:00
|
|
|
g_free(path);
|
2002-12-13 14:38:56 +01:00
|
|
|
|
2001-04-19 14:21:46 +02:00
|
|
|
return new_item;
|
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_rename_folder(Folder *folder, FolderItem *item,
|
|
|
|
const gchar *name)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-10-05 12:10:30 +02:00
|
|
|
gchar *real_name;
|
2001-04-19 14:21:46 +02:00
|
|
|
gchar *oldpath;
|
|
|
|
gchar *dirname;
|
2003-10-05 12:10:30 +02:00
|
|
|
gchar *newpath, *utf8newpath;
|
2001-04-19 14:21:46 +02:00
|
|
|
gchar *paths[2];
|
|
|
|
|
|
|
|
g_return_val_if_fail(folder != NULL, -1);
|
|
|
|
g_return_val_if_fail(item != NULL, -1);
|
|
|
|
g_return_val_if_fail(item->path != NULL, -1);
|
|
|
|
g_return_val_if_fail(name != NULL, -1);
|
|
|
|
|
|
|
|
oldpath = folder_item_get_path(item);
|
2001-05-18 05:39:01 +02:00
|
|
|
if (!is_dir_exist(oldpath))
|
|
|
|
make_dir_hier(oldpath);
|
|
|
|
|
2004-11-12 10:21:06 +01:00
|
|
|
dirname = g_path_get_dirname(oldpath);
|
2003-10-05 12:10:30 +02:00
|
|
|
real_name = mh_filename_from_utf8(name);
|
|
|
|
newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
|
|
|
|
g_free(real_name);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2005-08-19 11:30:30 +02:00
|
|
|
if (g_rename(oldpath, newpath) < 0) {
|
2001-04-19 14:21:46 +02:00
|
|
|
FILE_OP_ERROR(oldpath, "rename");
|
|
|
|
g_free(oldpath);
|
|
|
|
g_free(newpath);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(oldpath);
|
|
|
|
g_free(newpath);
|
|
|
|
|
|
|
|
if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
|
2004-11-12 10:21:06 +01:00
|
|
|
dirname = g_path_get_dirname(item->path);
|
2003-10-05 12:10:30 +02:00
|
|
|
utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
|
|
|
|
name, NULL);
|
2001-04-19 14:21:46 +02:00
|
|
|
g_free(dirname);
|
|
|
|
} else
|
2003-10-05 12:10:30 +02:00
|
|
|
utf8newpath = g_strdup(name);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
g_free(item->name);
|
|
|
|
item->name = g_strdup(name);
|
|
|
|
|
|
|
|
paths[0] = g_strdup(item->path);
|
2003-10-05 12:10:30 +02:00
|
|
|
paths[1] = utf8newpath;
|
2003-08-25 16:14:47 +02:00
|
|
|
g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
|
2001-04-19 14:21:46 +02:00
|
|
|
mh_rename_folder_func, paths);
|
|
|
|
|
|
|
|
g_free(paths[0]);
|
|
|
|
g_free(paths[1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
static gint mh_remove_folder(Folder *folder, FolderItem *item)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
|
|
|
gchar *path;
|
|
|
|
|
|
|
|
g_return_val_if_fail(folder != NULL, -1);
|
|
|
|
g_return_val_if_fail(item != NULL, -1);
|
|
|
|
g_return_val_if_fail(item->path != NULL, -1);
|
|
|
|
|
|
|
|
path = folder_item_get_path(item);
|
|
|
|
if (remove_dir_recursive(path) < 0) {
|
|
|
|
g_warning("can't remove directory `%s'\n", path);
|
|
|
|
g_free(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(path);
|
|
|
|
folder_item_remove(item);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
|
|
|
|
{
|
|
|
|
MsgInfo *msginfo;
|
2001-07-17 22:05:22 +02:00
|
|
|
MsgFlags flags;
|
|
|
|
|
2001-04-19 14:21:46 +02:00
|
|
|
g_return_val_if_fail(item != NULL, NULL);
|
|
|
|
g_return_val_if_fail(file != NULL, NULL);
|
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
flags.perm_flags = MSG_NEW|MSG_UNREAD;
|
|
|
|
flags.tmp_flags = 0;
|
|
|
|
|
2005-08-07 15:39:27 +02:00
|
|
|
if (folder_has_parent_of_type(item, F_QUEUE)) {
|
2001-07-17 22:05:22 +02:00
|
|
|
MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
|
2005-08-07 15:39:27 +02:00
|
|
|
} else if (folder_has_parent_of_type(item, F_DRAFT)) {
|
2001-07-17 22:05:22 +02:00
|
|
|
MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2002-06-19 09:31:13 +02:00
|
|
|
msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
|
2001-04-19 14:21:46 +02:00
|
|
|
if (!msginfo) return NULL;
|
|
|
|
|
|
|
|
msginfo->msgnum = atoi(file);
|
|
|
|
msginfo->folder = item;
|
|
|
|
|
|
|
|
return msginfo;
|
|
|
|
}
|
|
|
|
|
2003-08-25 16:14:47 +02:00
|
|
|
static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
|
|
|
|
{
|
|
|
|
FolderItem *item;
|
|
|
|
gchar *path;
|
|
|
|
|
|
|
|
g_return_val_if_fail(node->data != NULL, FALSE);
|
|
|
|
|
|
|
|
if (G_NODE_IS_ROOT(node))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
item = FOLDER_ITEM(node->data);
|
|
|
|
|
|
|
|
path = folder_item_get_path(item);
|
|
|
|
if (!is_dir_exist(path)) {
|
2007-08-13 23:36:57 +02:00
|
|
|
debug_print("folder '%s' not found. removing...\n", path?path:"(null)");
|
2003-08-25 16:14:47 +02:00
|
|
|
folder_item_remove(item);
|
|
|
|
}
|
|
|
|
g_free(path);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mh_remove_missing_folder_items(Folder *folder)
|
|
|
|
{
|
|
|
|
g_return_if_fail(folder != NULL);
|
|
|
|
|
|
|
|
debug_print("searching missing folders...\n");
|
|
|
|
|
|
|
|
g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
|
|
|
|
mh_remove_missing_folder_items_func, folder);
|
|
|
|
}
|
|
|
|
|
2002-07-02 21:49:11 +02:00
|
|
|
static void mh_scan_tree_recursive(FolderItem *item)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-08-25 16:14:47 +02:00
|
|
|
Folder *folder;
|
2005-08-19 11:30:30 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
GDir *dir;
|
|
|
|
#else
|
2001-04-19 14:21:46 +02:00
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
2005-08-19 11:30:30 +02:00
|
|
|
#endif
|
|
|
|
const gchar *dir_name;
|
2001-04-19 14:21:46 +02:00
|
|
|
struct stat s;
|
2003-10-05 12:10:30 +02:00
|
|
|
gchar *real_path, *entry, *utf8entry, *utf8name;
|
2001-04-19 14:21:46 +02:00
|
|
|
gint n_msg = 0;
|
|
|
|
|
|
|
|
g_return_if_fail(item != NULL);
|
|
|
|
g_return_if_fail(item->folder != NULL);
|
|
|
|
|
2003-08-25 16:14:47 +02:00
|
|
|
folder = item->folder;
|
|
|
|
|
2003-10-05 12:10:30 +02:00
|
|
|
real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
|
2005-08-19 11:30:30 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
dir = g_dir_open(real_path, 0, NULL);
|
|
|
|
if (!dir) {
|
|
|
|
g_warning("failed to open directory: %s\n", real_path);
|
|
|
|
g_free(real_path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
2003-10-05 12:10:30 +02:00
|
|
|
dp = opendir(real_path);
|
2001-04-19 14:21:46 +02:00
|
|
|
if (!dp) {
|
2003-10-05 12:10:30 +02:00
|
|
|
FILE_OP_ERROR(real_path, "opendir");
|
2001-04-19 14:21:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2005-08-19 11:30:30 +02:00
|
|
|
#endif
|
2003-10-05 12:10:30 +02:00
|
|
|
g_free(real_path);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
debug_print("scanning %s ...\n",
|
|
|
|
item->path ? item->path
|
|
|
|
: LOCAL_FOLDER(item->folder)->rootpath);
|
2003-08-25 16:14:47 +02:00
|
|
|
if (folder->ui_func)
|
|
|
|
folder->ui_func(folder, item, folder->ui_func_data);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2005-08-19 11:30:30 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
while ((dir_name = g_dir_read_name(dir)) != NULL) {
|
|
|
|
#else
|
2001-04-19 14:21:46 +02:00
|
|
|
while ((d = readdir(dp)) != NULL) {
|
2005-08-19 11:30:30 +02:00
|
|
|
dir_name = d->d_name;
|
|
|
|
#endif
|
|
|
|
if (dir_name[0] == '.') continue;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2005-08-19 11:30:30 +02:00
|
|
|
utf8name = mh_filename_to_utf8(dir_name);
|
2001-04-19 14:21:46 +02:00
|
|
|
if (item->path)
|
2003-10-05 12:10:30 +02:00
|
|
|
utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
|
|
|
|
utf8name, NULL);
|
2001-04-19 14:21:46 +02:00
|
|
|
else
|
2003-10-05 12:10:30 +02:00
|
|
|
utf8entry = g_strdup(utf8name);
|
|
|
|
entry = mh_filename_from_utf8(utf8entry);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2004-01-12 22:28:31 +01:00
|
|
|
if (
|
2007-03-22 08:52:47 +01:00
|
|
|
#if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
|
2004-01-12 22:28:31 +01:00
|
|
|
d->d_type == DT_DIR ||
|
|
|
|
(d->d_type == DT_UNKNOWN &&
|
|
|
|
#endif
|
2005-08-19 11:30:30 +02:00
|
|
|
g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
|
2007-03-22 08:52:47 +01:00
|
|
|
#if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
|
2004-01-12 22:28:31 +01:00
|
|
|
)
|
|
|
|
#endif
|
|
|
|
) {
|
2003-08-25 16:14:47 +02:00
|
|
|
FolderItem *new_item = NULL;
|
|
|
|
GNode *node;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-08-25 16:14:47 +02:00
|
|
|
node = item->node;
|
|
|
|
for (node = node->children; node != NULL; node = node->next) {
|
|
|
|
FolderItem *cur_item = FOLDER_ITEM(node->data);
|
2006-10-03 18:40:58 +02:00
|
|
|
gchar *curpath = mh_filename_from_utf8(cur_item->path);
|
|
|
|
if (!strcmp2(curpath, entry)) {
|
2003-08-25 16:14:47 +02:00
|
|
|
new_item = cur_item;
|
2006-10-03 18:40:58 +02:00
|
|
|
g_free(curpath);
|
2003-08-25 16:14:47 +02:00
|
|
|
break;
|
|
|
|
}
|
2006-10-03 18:40:58 +02:00
|
|
|
g_free(curpath);
|
2003-08-25 16:14:47 +02:00
|
|
|
}
|
|
|
|
if (!new_item) {
|
|
|
|
debug_print("new folder '%s' found.\n", entry);
|
2003-10-05 12:10:30 +02:00
|
|
|
new_item = folder_item_new(folder, utf8name, utf8entry);
|
2003-08-25 16:14:47 +02:00
|
|
|
folder_item_append(item, new_item);
|
|
|
|
}
|
|
|
|
|
2001-04-19 14:21:46 +02:00
|
|
|
if (!item->path) {
|
2003-08-25 16:14:47 +02:00
|
|
|
if (!folder->inbox &&
|
2005-08-19 11:30:30 +02:00
|
|
|
!strcmp(dir_name, INBOX_DIR)) {
|
2001-04-19 14:21:46 +02:00
|
|
|
new_item->stype = F_INBOX;
|
2003-08-25 16:14:47 +02:00
|
|
|
folder->inbox = new_item;
|
|
|
|
} else if (!folder->outbox &&
|
2005-08-19 11:30:30 +02:00
|
|
|
!strcmp(dir_name, OUTBOX_DIR)) {
|
2001-04-19 14:21:46 +02:00
|
|
|
new_item->stype = F_OUTBOX;
|
2003-08-25 16:14:47 +02:00
|
|
|
folder->outbox = new_item;
|
|
|
|
} else if (!folder->draft &&
|
2005-08-19 11:30:30 +02:00
|
|
|
!strcmp(dir_name, DRAFT_DIR)) {
|
2001-04-19 14:21:46 +02:00
|
|
|
new_item->stype = F_DRAFT;
|
2003-08-25 16:14:47 +02:00
|
|
|
folder->draft = new_item;
|
|
|
|
} else if (!folder->queue &&
|
2005-08-19 11:30:30 +02:00
|
|
|
!strcmp(dir_name, QUEUE_DIR)) {
|
2001-04-19 14:21:46 +02:00
|
|
|
new_item->stype = F_QUEUE;
|
2003-08-25 16:14:47 +02:00
|
|
|
folder->queue = new_item;
|
|
|
|
} else if (!folder->trash &&
|
2005-08-19 11:30:30 +02:00
|
|
|
!strcmp(dir_name, TRASH_DIR)) {
|
2001-04-19 14:21:46 +02:00
|
|
|
new_item->stype = F_TRASH;
|
2003-08-25 16:14:47 +02:00
|
|
|
folder->trash = new_item;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
}
|
2003-08-25 16:14:47 +02:00
|
|
|
|
2002-07-02 21:49:11 +02:00
|
|
|
mh_scan_tree_recursive(new_item);
|
2005-08-19 11:30:30 +02:00
|
|
|
} else if (to_number(dir_name) > 0) n_msg++;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
g_free(entry);
|
2003-10-05 12:10:30 +02:00
|
|
|
g_free(utf8entry);
|
|
|
|
g_free(utf8name);
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2005-08-19 11:30:30 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
g_dir_close(dir);
|
|
|
|
#else
|
2001-04-19 14:21:46 +02:00
|
|
|
closedir(dp);
|
2005-08-19 11:30:30 +02:00
|
|
|
#endif
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2007-09-26 09:53:13 +02:00
|
|
|
mh_set_mtime(folder, item);
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean mh_rename_folder_func(GNode *node, gpointer data)
|
|
|
|
{
|
|
|
|
FolderItem *item = node->data;
|
|
|
|
gchar **paths = data;
|
|
|
|
const gchar *oldpath = paths[0];
|
|
|
|
const gchar *newpath = paths[1];
|
|
|
|
gchar *base;
|
|
|
|
gchar *new_itempath;
|
|
|
|
gint oldpathlen;
|
|
|
|
|
|
|
|
oldpathlen = strlen(oldpath);
|
|
|
|
if (strncmp(oldpath, item->path, oldpathlen) != 0) {
|
|
|
|
g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
base = item->path + oldpathlen;
|
|
|
|
while (*base == G_DIR_SEPARATOR) base++;
|
|
|
|
if (*base == '\0')
|
|
|
|
new_itempath = g_strdup(newpath);
|
|
|
|
else
|
|
|
|
new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
|
|
|
|
NULL);
|
|
|
|
g_free(item->path);
|
|
|
|
item->path = new_itempath;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-10-05 12:10:30 +02:00
|
|
|
|
|
|
|
static gchar *mh_filename_from_utf8(const gchar *path)
|
|
|
|
{
|
2005-06-25 14:06:38 +02:00
|
|
|
gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
|
2003-10-05 12:10:30 +02:00
|
|
|
|
|
|
|
if (!real_path) {
|
2007-05-05 10:53:18 +02:00
|
|
|
g_warning("mh_filename_from_utf8: failed to convert character set\n");
|
2003-10-05 12:10:30 +02:00
|
|
|
real_path = g_strdup(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return real_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *mh_filename_to_utf8(const gchar *path)
|
|
|
|
{
|
2005-06-25 14:06:38 +02:00
|
|
|
gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
|
2003-10-05 12:10:30 +02:00
|
|
|
if (!utf8path) {
|
2007-05-05 10:53:18 +02:00
|
|
|
g_warning("mh_filename_to_utf8: failed to convert character set\n");
|
2003-10-05 12:10:30 +02:00
|
|
|
utf8path = g_strdup(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return utf8path;
|
|
|
|
}
|
2006-05-12 19:46:09 +02:00
|
|
|
|
|
|
|
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(
|
2006-06-07 09:30:10 +02:00
|
|
|
get_home_dir(), G_DIR_SEPARATOR_S,
|
2006-05-12 19:46:09 +02:00
|
|
|
".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;
|
|
|
|
}
|
|
|
|
|
2006-06-17 20:50:41 +02:00
|
|
|
#if 0
|
2006-05-12 19:46:09 +02:00
|
|
|
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;
|
2006-05-19 08:27:04 +02:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2006-05-12 19:46:09 +02:00
|
|
|
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);
|
|
|
|
}
|
2006-05-17 08:44:11 +02:00
|
|
|
|
|
|
|
g_free(mh_sequences_filename);
|
|
|
|
|
2006-05-12 19:46:09 +02:00
|
|
|
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);
|
2007-08-21 11:17:38 +02:00
|
|
|
g_print("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
|
2006-05-12 19:46:09 +02:00
|
|
|
g_timer_destroy(timer);
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
2006-06-17 20:50:41 +02:00
|
|
|
#endif
|
2006-05-12 19:46:09 +02:00
|
|
|
|
2006-05-23 19:42:52 +02:00
|
|
|
static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
|
2006-05-12 19:46:09 +02:00
|
|
|
{
|
|
|
|
gchar *mh_sequences_old, *mh_sequences_new;
|
|
|
|
FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
|
|
|
|
gchar buf[BUFFSIZE];
|
|
|
|
gchar *path = NULL;
|
2007-10-04 19:36:57 +02:00
|
|
|
gboolean err = FALSE;
|
2006-10-02 19:04:07 +02:00
|
|
|
START_TIMING("");
|
|
|
|
|
2006-05-12 19:46:09 +02:00
|
|
|
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("");
|
2006-09-30 20:06:16 +02:00
|
|
|
gint seq_len = 0;
|
2006-05-12 19:46:09 +02:00
|
|
|
msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
|
|
|
|
cur = msglist;
|
|
|
|
|
2006-05-23 19:42:52 +02:00
|
|
|
/* write the unseen sequence if we don't have to scrap it */
|
|
|
|
if (!remove_unseen) do {
|
2006-05-12 19:46:09 +02:00
|
|
|
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) {
|
2006-09-30 20:06:16 +02:00
|
|
|
gchar tmp[32];
|
|
|
|
gint tmp_len = 0;
|
2006-05-12 19:46:09 +02:00
|
|
|
if (start != end)
|
2006-09-30 20:06:16 +02:00
|
|
|
snprintf(tmp, 31, " %d-%d", start, end);
|
2006-05-12 19:46:09 +02:00
|
|
|
else
|
2006-09-30 20:06:16 +02:00
|
|
|
snprintf(tmp, 31, " %d", start);
|
|
|
|
|
|
|
|
tmp_len = strlen(tmp);
|
|
|
|
sequence = g_realloc(sequence, seq_len+tmp_len+1);
|
|
|
|
strcpy(sequence+seq_len, tmp);
|
|
|
|
seq_len += tmp_len;
|
|
|
|
|
2006-05-12 19:46:09 +02:00
|
|
|
start = end = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur = cur ? cur->next:NULL;
|
|
|
|
} while (cur || (start > 0 && end > 0));
|
2006-10-03 20:16:40 +02:00
|
|
|
if (sequence && *sequence) {
|
2007-10-04 19:36:57 +02:00
|
|
|
if (fprintf(mh_sequences_new_fp, "%s%s\n",
|
|
|
|
get_unseen_seq_name(), sequence) < 0)
|
|
|
|
err = TRUE;
|
|
|
|
else
|
|
|
|
debug_print("wrote unseen sequence: '%s%s'\n",
|
2006-05-12 19:46:09 +02:00
|
|
|
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())))
|
2007-10-04 19:36:57 +02:00
|
|
|
if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
|
|
|
|
err = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2006-05-12 19:46:09 +02:00
|
|
|
}
|
|
|
|
fclose(mh_sequences_old_fp);
|
|
|
|
}
|
|
|
|
|
2006-10-01 12:45:27 +02:00
|
|
|
fflush(mh_sequences_new_fp);
|
2006-10-01 14:08:47 +02:00
|
|
|
#if 0
|
|
|
|
fsync(fileno(mh_sequences_new_fp));
|
|
|
|
#endif
|
2007-10-04 19:36:57 +02:00
|
|
|
if (fclose(mh_sequences_new_fp) == EOF)
|
|
|
|
err = TRUE;
|
2006-10-01 12:45:27 +02:00
|
|
|
|
2007-10-04 19:36:57 +02:00
|
|
|
if (!err)
|
|
|
|
g_rename(mh_sequences_new, mh_sequences_old);
|
2006-05-12 19:46:09 +02:00
|
|
|
g_free(sequence);
|
|
|
|
procmsg_msg_list_free(msglist);
|
|
|
|
}
|
|
|
|
g_free(mh_sequences_old);
|
|
|
|
g_free(mh_sequences_new);
|
|
|
|
g_free(path);
|
2006-10-02 19:04:07 +02:00
|
|
|
|
|
|
|
END_TIMING();
|
2006-05-12 19:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mh_item_close(Folder *folder, FolderItem *item)
|
|
|
|
{
|
2006-09-30 19:45:27 +02:00
|
|
|
time_t last_mtime = (time_t)0;
|
|
|
|
gboolean need_scan = mh_scan_required(item->folder, item);
|
|
|
|
last_mtime = item->mtime;
|
|
|
|
|
2006-05-23 19:42:52 +02:00
|
|
|
mh_write_sequences(item, FALSE);
|
2006-09-30 19:45:27 +02:00
|
|
|
|
2006-09-30 21:36:06 +02:00
|
|
|
if (item->mtime == last_mtime && !need_scan) {
|
2007-09-26 09:53:13 +02:00
|
|
|
mh_set_mtime(folder, item);
|
2006-09-30 21:36:06 +02:00
|
|
|
}
|
2006-09-30 19:45:27 +02:00
|
|
|
|
2006-05-12 19:46:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-01 14:08:47 +02:00
|
|
|
|
2007-09-26 10:00:54 +02:00
|
|
|
static void mh_set_mtime(Folder *folder, FolderItem *item)
|
2006-10-01 14:08:47 +02:00
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
gchar *path = folder_item_get_path(item);
|
|
|
|
|
|
|
|
g_return_if_fail(path != NULL);
|
|
|
|
|
2008-09-30 17:34:25 +02:00
|
|
|
if (g_stat(path, &s) < 0) {
|
2006-10-01 14:08:47 +02:00
|
|
|
FILE_OP_ERROR(path, "stat");
|
|
|
|
g_free(path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->mtime = s.st_mtime;
|
2007-08-13 23:36:57 +02:00
|
|
|
debug_print("MH: forced mtime of %s to %ld\n", item->name?item->name:"(null)", item->mtime);
|
2006-10-01 14:08:47 +02:00
|
|
|
g_free(path);
|
|
|
|
}
|