2 * Copyright (C) 2000 Ximian Inc.
4 * Authors: Not Zed <notzed@lostzed.mmc.com.au>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
33 #include <sys/types.h>
35 #include <glib/gi18n-lib.h>
37 #include "camel-mime-message.h"
38 #include "camel-private.h"
40 #include "camel-mh-summary.h"
42 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
44 #define CAMEL_MH_SUMMARY_VERSION (0x2000)
46 static int mh_summary_check(CamelLocalSummary *cls, CamelFolderChangeInfo *changeinfo, CamelException *ex);
47 static int mh_summary_sync(CamelLocalSummary *cls, gboolean expunge, CamelFolderChangeInfo *changeinfo, CamelException *ex);
48 /*static int mh_summary_add(CamelLocalSummary *cls, CamelMimeMessage *msg, CamelMessageInfo *info, CamelFolderChangeInfo *, CamelException *ex);*/
50 static char *mh_summary_next_uid_string(CamelFolderSummary *s);
52 static void camel_mh_summary_class_init (CamelMhSummaryClass *class);
53 static void camel_mh_summary_init (CamelMhSummary *gspaper);
54 static void camel_mh_summary_finalise (CamelObject *obj);
56 #define _PRIVATE(x) (((CamelMhSummary *)(x))->priv)
58 struct _CamelMhSummaryPrivate {
62 static CamelLocalSummaryClass *parent_class;
65 camel_mh_summary_get_type (void)
67 static CamelType type = CAMEL_INVALID_TYPE;
69 if (type == CAMEL_INVALID_TYPE) {
70 type = camel_type_register(camel_local_summary_get_type (), "CamelMhSummary",
71 sizeof(CamelMhSummary),
72 sizeof(CamelMhSummaryClass),
73 (CamelObjectClassInitFunc)camel_mh_summary_class_init,
75 (CamelObjectInitFunc)camel_mh_summary_init,
76 (CamelObjectFinalizeFunc)camel_mh_summary_finalise);
83 camel_mh_summary_class_init (CamelMhSummaryClass *class)
85 CamelFolderSummaryClass *sklass = (CamelFolderSummaryClass *) class;
86 CamelLocalSummaryClass *lklass = (CamelLocalSummaryClass *)class;
88 parent_class = (CamelLocalSummaryClass *)camel_type_get_global_classfuncs(camel_local_summary_get_type ());
90 /* override methods */
91 sklass->next_uid_string = mh_summary_next_uid_string;
93 lklass->check = mh_summary_check;
94 lklass->sync = mh_summary_sync;
95 /*lklass->add = mh_summary_add;*/
99 camel_mh_summary_init (CamelMhSummary *o)
101 struct _CamelFolderSummary *s = (CamelFolderSummary *) o;
103 o->priv = g_malloc0(sizeof(*o->priv));
104 /* set unique file version */
105 s->version += CAMEL_MH_SUMMARY_VERSION;
109 camel_mh_summary_finalise(CamelObject *obj)
111 CamelMhSummary *o = (CamelMhSummary *)obj;
117 * camel_mh_summary_new:
119 * Create a new CamelMhSummary object.
121 * Return value: A new #CamelMhSummary object.
123 CamelMhSummary *camel_mh_summary_new(struct _CamelFolder *folder, const char *filename, const char *mhdir, CamelIndex *index)
125 CamelMhSummary *o = (CamelMhSummary *)camel_object_new(camel_mh_summary_get_type ());
127 ((CamelFolderSummary *)o)->folder = folder;
129 camel_local_summary_construct((CamelLocalSummary *)o, filename, mhdir, index);
133 static char *mh_summary_next_uid_string(CamelFolderSummary *s)
135 CamelMhSummary *mhs = (CamelMhSummary *)s;
136 CamelLocalSummary *cls = (CamelLocalSummary *)s;
142 /* if we are working to add an existing file, then use current_uid */
143 if (mhs->priv->current_uid) {
144 uidstr = g_strdup(mhs->priv->current_uid);
145 /* tell the summary of this, so we always append numbers to the end */
146 camel_folder_summary_set_uid(s, strtoul(uidstr, NULL, 10)+1);
148 /* else scan for one - and create it too, to make sure */
152 uid = camel_folder_summary_next_uid(s);
153 name = g_strdup_printf("%s/%u", cls->folder_path, uid);
154 /* O_EXCL isn't guaranteed, sigh. Oh well, bad luck, mh has problems anyway */
155 fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0600);
157 } while (fd == -1 && errno == EEXIST);
162 uidstr = g_strdup_printf("%u", uid);
168 static int camel_mh_summary_add(CamelLocalSummary *cls, const char *name, int forceindex)
170 CamelMhSummary *mhs = (CamelMhSummary *)cls;
171 char *filename = g_strdup_printf("%s/%s", cls->folder_path, name);
175 d(printf("summarising: %s\n", name));
177 fd = open(filename, O_RDONLY);
179 g_warning ("Cannot summarise/index: %s: %s", filename, strerror (errno));
183 mp = camel_mime_parser_new();
184 camel_mime_parser_scan_from(mp, FALSE);
185 camel_mime_parser_init_with_fd(mp, fd);
186 if (cls->index && (forceindex || !camel_index_has_name(cls->index, name))) {
187 d(printf("forcing indexing of message content\n"));
188 camel_folder_summary_set_index((CamelFolderSummary *)mhs, cls->index);
190 camel_folder_summary_set_index((CamelFolderSummary *)mhs, NULL);
192 mhs->priv->current_uid = (char *)name;
193 camel_folder_summary_add_from_parser((CamelFolderSummary *)mhs, mp);
194 camel_object_unref((CamelObject *)mp);
195 mhs->priv->current_uid = NULL;
196 camel_folder_summary_set_index((CamelFolderSummary *)mhs, NULL);
202 remove_summary(char *key, CamelMessageInfo *info, CamelLocalSummary *cls)
204 d(printf("removing message %s from summary\n", key));
206 camel_index_delete_name(cls->index, camel_message_info_uid(info));
207 camel_folder_summary_remove((CamelFolderSummary *)cls, info);
208 camel_message_info_free(info);
212 sort_uid_cmp(const void *ap, const void *bp)
214 const CamelMessageInfo
215 *a = *((CamelMessageInfo **)ap),
216 *b = *((CamelMessageInfo **)bp);
218 *auid = camel_message_info_uid(a),
219 *buid = camel_message_info_uid(b);
220 int aval = atoi(auid), bval = atoi(buid);
222 return (aval < bval) ? -1 : (aval > bval) ? 1 : 0;
226 mh_summary_check(CamelLocalSummary *cls, CamelFolderChangeInfo *changeinfo, CamelException *ex)
231 CamelMessageInfo *info;
232 CamelFolderSummary *s = (CamelFolderSummary *)cls;
237 /* FIXME: Handle changeinfo */
239 d(printf("checking summary ...\n"));
241 /* scan the directory, check for mail files not in the index, or index entries that
243 dir = opendir(cls->folder_path);
245 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
246 _("Cannot open MH directory path: %s: %s"),
247 cls->folder_path, g_strerror (errno));
251 /* keeps track of all uid's that have not been processed */
252 left = g_hash_table_new(g_str_hash, g_str_equal);
253 count = camel_folder_summary_count((CamelFolderSummary *)cls);
254 forceindex = count == 0;
255 for (i=0;i<count;i++) {
256 info = camel_folder_summary_index((CamelFolderSummary *)cls, i);
258 g_hash_table_insert(left, (char *)camel_message_info_uid(info), info);
262 while ( (d = readdir(dir)) ) {
263 /* FIXME: also run stat to check for regular file */
265 while ( (c = *p++) ) {
270 info = camel_folder_summary_uid((CamelFolderSummary *)cls, d->d_name);
271 if (info == NULL || (cls->index && (!camel_index_has_name(cls->index, d->d_name)))) {
272 /* need to add this file to the summary */
274 g_hash_table_remove(left, camel_message_info_uid(info));
275 camel_folder_summary_remove((CamelFolderSummary *)cls, info);
276 camel_message_info_free(info);
278 camel_mh_summary_add(cls, d->d_name, forceindex);
280 const char *uid = camel_message_info_uid(info);
281 CamelMessageInfo *old = g_hash_table_lookup(left, uid);
284 camel_message_info_free(old);
285 g_hash_table_remove(left, uid);
287 camel_message_info_free(info);
292 g_hash_table_foreach(left, (GHFunc)remove_summary, cls);
293 g_hash_table_destroy(left);
295 /* sort the summary based on message number (uid), since the directory order is not useful */
296 CAMEL_SUMMARY_LOCK(s, summary_lock);
297 qsort(s->messages->pdata, s->messages->len, sizeof(CamelMessageInfo *), sort_uid_cmp);
298 CAMEL_SUMMARY_UNLOCK(s, summary_lock);
303 /* sync the summary file with the ondisk files */
305 mh_summary_sync(CamelLocalSummary *cls, gboolean expunge, CamelFolderChangeInfo *changes, CamelException *ex)
308 CamelLocalMessageInfo *info;
312 d(printf("summary_sync(expunge=%s)\n", expunge?"true":"false"));
314 /* we could probably get away without this ... but why not use it, esp if we're going to
315 be doing any significant io already */
316 if (camel_local_summary_check(cls, changes, ex) == -1)
319 /* FIXME: need to update/honour .mh_sequences or whatever it is */
321 count = camel_folder_summary_count((CamelFolderSummary *)cls);
322 for (i=count-1;i>=0;i--) {
323 info = (CamelLocalMessageInfo *)camel_folder_summary_index((CamelFolderSummary *)cls, i);
325 if (expunge && (info->info.flags & CAMEL_MESSAGE_DELETED)) {
326 uid = camel_message_info_uid(info);
327 name = g_strdup_printf("%s/%s", cls->folder_path, uid);
328 d(printf("deleting %s\n", name));
329 if (unlink(name) == 0 || errno==ENOENT) {
331 /* FIXME: put this in folder_summary::remove()? */
333 camel_index_delete_name(cls->index, (char *)uid);
335 camel_folder_change_info_remove_uid(changes, uid);
336 camel_folder_summary_remove((CamelFolderSummary *)cls, (CamelMessageInfo *)info);
339 } else if (info->info.flags & (CAMEL_MESSAGE_FOLDER_NOXEV|CAMEL_MESSAGE_FOLDER_FLAGGED)) {
340 info->info.flags &= 0xffff;
342 camel_message_info_free(info);
345 return ((CamelLocalSummaryClass *)parent_class)->sync(cls, expunge, changes, ex);