1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */
3 * Copyright (C) 2000 Ximian Inc.
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
34 #include <glib/gi18n-lib.h>
35 #include <glib/gstdio.h>
37 #include "camel-file-utils.h"
38 #include "camel-mime-message.h"
39 #include "camel-stream-null.h"
41 #include "camel-local-summary.h"
45 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
47 #define CAMEL_LOCAL_SUMMARY_VERSION (1)
49 static int summary_header_load (CamelFolderSummary *, FILE *);
50 static int summary_header_save (CamelFolderSummary *, FILE *);
52 static CamelMessageInfo * message_info_new_from_header (CamelFolderSummary *, struct _camel_header_raw *);
54 static int local_summary_decode_x_evolution(CamelLocalSummary *cls, const char *xev, CamelLocalMessageInfo *mi);
55 static char *local_summary_encode_x_evolution(CamelLocalSummary *cls, const CamelLocalMessageInfo *mi);
57 static int local_summary_load(CamelLocalSummary *cls, int forceindex, CamelException *ex);
58 static int local_summary_check(CamelLocalSummary *cls, CamelFolderChangeInfo *changeinfo, CamelException *ex);
59 static int local_summary_sync(CamelLocalSummary *cls, gboolean expunge, CamelFolderChangeInfo *changeinfo, CamelException *ex);
60 static CamelMessageInfo *local_summary_add(CamelLocalSummary *cls, CamelMimeMessage *msg, const CamelMessageInfo *info, CamelFolderChangeInfo *, CamelException *ex);
62 static void camel_local_summary_class_init (CamelLocalSummaryClass *klass);
63 static void camel_local_summary_init (CamelLocalSummary *obj);
64 static void camel_local_summary_finalise (CamelObject *obj);
65 static CamelFolderSummaryClass *camel_local_summary_parent;
68 camel_local_summary_get_type(void)
70 static CamelType type = CAMEL_INVALID_TYPE;
72 if (type == CAMEL_INVALID_TYPE) {
73 type = camel_type_register(camel_folder_summary_get_type(), "CamelLocalSummary",
74 sizeof (CamelLocalSummary),
75 sizeof (CamelLocalSummaryClass),
76 (CamelObjectClassInitFunc) camel_local_summary_class_init,
78 (CamelObjectInitFunc) camel_local_summary_init,
79 (CamelObjectFinalizeFunc) camel_local_summary_finalise);
86 camel_local_summary_class_init(CamelLocalSummaryClass *klass)
88 CamelFolderSummaryClass *sklass = (CamelFolderSummaryClass *) klass;
90 camel_local_summary_parent = CAMEL_FOLDER_SUMMARY_CLASS(camel_type_get_global_classfuncs(camel_folder_summary_get_type()));
92 sklass->summary_header_load = summary_header_load;
93 sklass->summary_header_save = summary_header_save;
95 sklass->message_info_new_from_header = message_info_new_from_header;
97 klass->load = local_summary_load;
98 klass->check = local_summary_check;
99 klass->sync = local_summary_sync;
100 klass->add = local_summary_add;
102 klass->encode_x_evolution = local_summary_encode_x_evolution;
103 klass->decode_x_evolution = local_summary_decode_x_evolution;
107 camel_local_summary_init(CamelLocalSummary *obj)
109 struct _CamelFolderSummary *s = (CamelFolderSummary *)obj;
111 /* subclasses need to set the right instance data sizes */
112 s->message_info_size = sizeof(CamelLocalMessageInfo);
113 s->content_info_size = sizeof(CamelMessageContentInfo);
115 /* and a unique file version */
116 s->version += CAMEL_LOCAL_SUMMARY_VERSION;
120 camel_local_summary_finalise(CamelObject *obj)
122 CamelLocalSummary *mbs = CAMEL_LOCAL_SUMMARY(obj);
125 camel_object_unref((CamelObject *)mbs->index);
126 g_free(mbs->folder_path);
130 camel_local_summary_construct(CamelLocalSummary *new, const char *filename, const char *local_name, CamelIndex *index)
132 camel_folder_summary_set_build_content(CAMEL_FOLDER_SUMMARY(new), FALSE);
133 camel_folder_summary_set_filename(CAMEL_FOLDER_SUMMARY(new), filename);
134 new->folder_path = g_strdup(local_name);
137 camel_object_ref((CamelObject *)index);
141 local_summary_load(CamelLocalSummary *cls, int forceindex, CamelException *ex)
143 return camel_folder_summary_load((CamelFolderSummary *)cls);
146 /* load/check the summary */
148 camel_local_summary_load(CamelLocalSummary *cls, int forceindex, CamelException *ex)
151 CamelFolderSummary *s = (CamelFolderSummary *)cls;
153 d(printf("Loading summary ...\n"));
156 || g_stat(s->summary_path, &st) == -1
157 || ((CamelLocalSummaryClass *)(CAMEL_OBJECT_GET_CLASS(cls)))->load(cls, forceindex, ex) == -1) {
158 w(g_warning("Could not load summary: flags may be reset"));
159 camel_folder_summary_clear((CamelFolderSummary *)cls);
166 void camel_local_summary_check_force(CamelLocalSummary *cls)
168 cls->check_force = 1;
172 camel_local_summary_encode_x_evolution(CamelLocalSummary *cls, const CamelLocalMessageInfo *info)
174 return ((CamelLocalSummaryClass *)(CAMEL_OBJECT_GET_CLASS(cls)))->encode_x_evolution(cls, info);
178 camel_local_summary_decode_x_evolution(CamelLocalSummary *cls, const char *xev, CamelLocalMessageInfo *info)
180 return ((CamelLocalSummaryClass *)(CAMEL_OBJECT_GET_CLASS(cls)))->decode_x_evolution(cls, xev, info);
195 do_stat_ci(CamelLocalSummary *cls, struct _stat_info *info, CamelMessageContentInfo *ci)
198 info->citotal += ((CamelFolderSummary *)cls)->content_info_size /*+ 4 memchunks are 1/4 byte overhead per mi */;
200 info->citotal += strlen(ci->id) + 4;
202 info->citotal += strlen(ci->description) + 4;
204 info->citotal += strlen(ci->encoding) + 4;
206 CamelContentType *ct = ci->type;
207 struct _camel_header_param *param;
209 info->citotal += sizeof(*ct) + 4;
211 info->citotal += strlen(ct->type) + 4;
213 info->citotal += strlen(ct->subtype) + 4;
216 info->citotal += sizeof(*param) + 4;
218 info->citotal += strlen(param->name)+4;
220 info->citotal += strlen(param->value)+4;
226 do_stat_ci(cls, info, ci);
232 do_stat_mi(CamelLocalSummary *cls, struct _stat_info *info, CamelMessageInfo *mi)
235 info->mitotal += ((CamelFolderSummary *)cls)->content_info_size /*+ 4*/;
238 info->mitotal += strlen(mi->subject) + 4;
240 info->mitotal += strlen(mi->to) + 4;
242 info->mitotal += strlen(mi->from) + 4;
244 info->mitotal += strlen(mi->cc) + 4;
246 info->mitotal += strlen(mi->uid) + 4;
248 if (mi->references) {
249 info->mitotal += (mi->references->size-1) * sizeof(CamelSummaryMessageID) + sizeof(CamelSummaryReferences) + 4;
250 info->msgid += (mi->references->size) * sizeof(CamelSummaryMessageID);
251 info->msgcount += mi->references->size;
254 /* dont have any user flags yet */
257 do_stat_ci(cls, info, mi->content);
264 camel_local_summary_check(CamelLocalSummary *cls, CamelFolderChangeInfo *changeinfo, CamelException *ex)
268 ret = ((CamelLocalSummaryClass *)(CAMEL_OBJECT_GET_CLASS(cls)))->check(cls, changeinfo, ex);
273 CamelFolderSummary *s = (CamelFolderSummary *)cls;
274 struct _stat_info stats = { 0 };
276 for (i=0;i<camel_folder_summary_count(s);i++) {
277 CamelMessageInfo *info = camel_folder_summary_index(s, i);
278 do_stat_mi(cls, &stats, info);
279 camel_message_info_free(info);
282 printf("\nMemory used by summary:\n\n");
283 printf("Total of %d messages\n", camel_folder_summary_count(s));
284 printf("Total: %d bytes (ave %f)\n", stats.citotal + stats.mitotal,
285 (double)(stats.citotal+stats.mitotal)/(double)camel_folder_summary_count(s));
286 printf("Message Info: %d (ave %f)\n", stats.mitotal, (double)stats.mitotal/(double)stats.micount);
287 printf("Content Info; %d (ave %f) count %d\n", stats.citotal, (double)stats.citotal/(double)stats.cicount, stats.cicount);
288 printf("message id's: %d (ave %f) count %d\n", stats.msgid, (double)stats.msgid/(double)stats.msgcount, stats.msgcount);
295 camel_local_summary_sync(CamelLocalSummary *cls, gboolean expunge, CamelFolderChangeInfo *changeinfo, CamelException *ex)
297 return ((CamelLocalSummaryClass *)(CAMEL_OBJECT_GET_CLASS(cls)))->sync(cls, expunge, changeinfo, ex);
301 camel_local_summary_add(CamelLocalSummary *cls, CamelMimeMessage *msg, const CamelMessageInfo *info, CamelFolderChangeInfo *ci, CamelException *ex)
303 return ((CamelLocalSummaryClass *)(CAMEL_OBJECT_GET_CLASS(cls)))->add(cls, msg, info, ci, ex);
307 * camel_local_summary_write_headers:
314 * Write a bunch of headers to the file @fd. IF xevline is non NULL, then
315 * an X-Evolution header line is created at the end of all of the headers.
316 * If @status is non NULL, then a Status header line is also written.
317 * The headers written are termianted with a blank line.
319 * Return value: -1 on error, otherwise the number of bytes written.
322 camel_local_summary_write_headers(int fd, struct _camel_header_raw *header, const char *xevline, const char *status, const char *xstatus)
328 /* dum de dum, maybe the whole sync function should just use stdio for output */
333 out = fdopen(newfd, "w");
341 if (strcmp(header->name, "X-Evolution") != 0
342 && (status == NULL || strcmp(header->name, "Status") != 0)
343 && (xstatus == NULL || strcmp(header->name, "X-Status") != 0)) {
344 len = fprintf(out, "%s:%s\n", header->name, header->value);
351 header = header->next;
355 len = fprintf(out, "Status: %s\n", status);
364 len = fprintf(out, "X-Status: %s\n", xstatus);
373 len = fprintf(out, "X-Evolution: %s\n", xevline);
381 len = fprintf(out, "\n");
388 if (fclose(out) == -1)
395 local_summary_check(CamelLocalSummary *cls, CamelFolderChangeInfo *changeinfo, CamelException *ex)
397 /* FIXME: sync index here ? */
402 local_summary_sync(CamelLocalSummary *cls, gboolean expunge, CamelFolderChangeInfo *changeinfo, CamelException *ex)
406 ret = camel_folder_summary_save((CamelFolderSummary *)cls);
408 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
409 _("Could not save summary: %s: %s"),
410 cls->folder_path, g_strerror (errno));
412 g_warning ("Could not save summary for %s: %s", cls->folder_path, strerror (errno));
415 if (cls->index && camel_index_sync(cls->index) == -1)
416 g_warning ("Could not sync index for %s: %s", cls->folder_path, strerror (errno));
421 static CamelMessageInfo *
422 local_summary_add(CamelLocalSummary *cls, CamelMimeMessage *msg, const CamelMessageInfo *info, CamelFolderChangeInfo *ci, CamelException *ex)
424 CamelLocalMessageInfo *mi;
427 d(printf("Adding message to summary\n"));
429 mi = (CamelLocalMessageInfo *)camel_folder_summary_add_from_message((CamelFolderSummary *)cls, msg);
431 d(printf("Added, uid = %s\n", mi->uid));
433 const CamelTag *tag = camel_message_info_user_tags(info);
434 const CamelFlag *flag = camel_message_info_user_flags(info);
437 camel_message_info_set_user_flag((CamelMessageInfo *)mi, flag->name, TRUE);
442 camel_message_info_set_user_tag((CamelMessageInfo *)mi, tag->name, tag->value);
446 mi->info.flags |= (camel_message_info_flags(info) & 0xffff);
447 mi->info.size = camel_message_info_size(info);
450 /* we need to calculate the size ourselves */
451 if (mi->info.size == 0) {
452 CamelStreamNull *sn = (CamelStreamNull *)camel_stream_null_new();
454 camel_data_wrapper_write_to_stream((CamelDataWrapper *)msg, (CamelStream *)sn);
455 mi->info.size = sn->written;
456 camel_object_unref((CamelObject *)sn);
459 mi->info.flags &= ~(CAMEL_MESSAGE_FOLDER_NOXEV|CAMEL_MESSAGE_FOLDER_FLAGGED);
460 xev = camel_local_summary_encode_x_evolution(cls, mi);
461 camel_medium_set_header((CamelMedium *)msg, "X-Evolution", xev);
463 camel_folder_change_info_add_uid(ci, camel_message_info_uid(mi));
465 d(printf("Failed!\n"));
466 camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
467 _("Unable to add message to summary: unknown reason"));
469 return (CamelMessageInfo *)mi;
473 local_summary_encode_x_evolution(CamelLocalSummary *cls, const CamelLocalMessageInfo *mi)
475 GString *out = g_string_new("");
476 struct _camel_header_param *params = NULL;
477 GString *val = g_string_new("");
478 CamelFlag *flag = mi->info.user_flags;
479 CamelTag *tag = mi->info.user_tags;
481 const char *p, *uidstr;
484 /* FIXME: work out what to do with uid's that aren't stored here? */
485 /* FIXME: perhaps make that a mbox folder only issue?? */
486 p = uidstr = camel_message_info_uid(mi);
487 while (*p && isdigit(*p))
489 if (*p == 0 && sscanf (uidstr, "%u", &uid) == 1) {
490 g_string_printf (out, "%08x-%04x", uid, mi->info.flags & 0xffff);
492 g_string_printf (out, "%s-%04x", uidstr, mi->info.flags & 0xffff);
496 val = g_string_new ("");
500 g_string_append (val, flag->name);
502 g_string_append_c (val, ',');
505 camel_header_set_param (¶ms, "flags", val->str);
506 g_string_truncate (val, 0);
510 g_string_append (val, tag->name);
511 g_string_append_c (val, '=');
512 g_string_append (val, tag->value);
514 g_string_append_c (val, ',');
517 camel_header_set_param (¶ms, "tags", val->str);
519 g_string_free (val, TRUE);
520 camel_header_param_list_format_append (out, params);
521 camel_header_param_list_free (params);
524 g_string_free (out, FALSE);
530 local_summary_decode_x_evolution(CamelLocalSummary *cls, const char *xev, CamelLocalMessageInfo *mi)
532 struct _camel_header_param *params, *scan;
540 /* check for uid/flags */
541 header = camel_header_token_decode(xev);
542 if (header && strlen(header) == strlen("00000000-0000")
543 && sscanf(header, "%08x-%04x", &uid, &flags) == 2) {
545 sprintf(uidstr, "%u", uid);
555 /* check for additional data */
556 header = strchr(xev, ';');
558 params = camel_header_param_list_decode(header+1);
561 if (!g_ascii_strcasecmp(scan->name, "flags")) {
562 char **flagv = g_strsplit(scan->value, ",", 1000);
564 for (i=0;flagv[i];i++)
565 camel_message_info_set_user_flag((CamelMessageInfo *)mi, flagv[i], TRUE);
567 } else if (!g_ascii_strcasecmp(scan->name, "tags")) {
568 char **tagv = g_strsplit(scan->value, ",", 10000);
571 for (i=0;tagv[i];i++) {
572 val = strchr(tagv[i], '=');
575 camel_message_info_set_user_tag((CamelMessageInfo *)mi, tagv[i], val);
583 camel_header_param_list_free(params);
586 mi->info.uid = g_strdup(uidstr);
587 mi->info.flags = flags;
593 summary_header_load(CamelFolderSummary *s, FILE *in)
595 CamelLocalSummary *cls = (CamelLocalSummary *)s;
597 /* We dont actually add our own headers, but version that we don't anyway */
599 if (((CamelFolderSummaryClass *)camel_local_summary_parent)->summary_header_load(s, in) == -1)
602 /* Legacy version, version is in summary only */
603 if ((s->version & 0xfff) == 0x20c)
606 /* otherwise load the version number */
607 return camel_file_util_decode_fixed_int32(in, &cls->version);
611 summary_header_save(CamelFolderSummary *s, FILE *out)
613 /*CamelLocalSummary *cls = (CamelLocalSummary *)s;*/
615 if (((CamelFolderSummaryClass *)camel_local_summary_parent)->summary_header_save(s, out) == -1)
618 return camel_file_util_encode_fixed_int32(out, CAMEL_LOCAL_SUMMARY_VERSION);
621 static CamelMessageInfo *
622 message_info_new_from_header(CamelFolderSummary *s, struct _camel_header_raw *h)
624 CamelLocalMessageInfo *mi;
625 CamelLocalSummary *cls = (CamelLocalSummary *)s;
627 mi = (CamelLocalMessageInfo *)((CamelFolderSummaryClass *)camel_local_summary_parent)->message_info_new_from_header(s, h);
632 xev = camel_header_raw_find(&h, "X-Evolution", NULL);
633 if (xev==NULL || camel_local_summary_decode_x_evolution(cls, xev, mi) == -1) {
634 /* to indicate it has no xev header */
635 mi->info.flags |= CAMEL_MESSAGE_FOLDER_FLAGGED | CAMEL_MESSAGE_FOLDER_NOXEV;
636 g_free (mi->info.uid);
637 mi->info.uid = camel_folder_summary_next_uid_string(s);
639 /* shortcut, no need to look it up in the index library */
646 || !camel_index_has_name(cls->index, camel_message_info_uid(mi)))) {
647 d(printf("Am indexing message %s\n", camel_message_info_uid(mi)));
648 camel_folder_summary_set_index(s, cls->index);
650 d(printf("Not indexing message %s\n", camel_message_info_uid(mi)));
651 camel_folder_summary_set_index(s, NULL);
655 return (CamelMessageInfo *)mi;