2 * Copyright (C) 2002 Ximian Inc.
4 * Authors: Michael Zucchi <notzed@ximian.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU 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 General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
31 #include "camel-imap-store-summary.h"
33 #include "camel-file-utils.h"
35 #include "string-utils.h"
36 #include "e-util/md5-utils.h"
37 #include "e-util/e-memory.h"
39 #include "camel-private.h"
40 #include "camel-utf8.h"
43 #define io(x) /* io debug */
45 #define CAMEL_IMAP_STORE_SUMMARY_VERSION_0 (0)
47 #define CAMEL_IMAP_STORE_SUMMARY_VERSION (0)
49 #define _PRIVATE(o) (((CamelImapStoreSummary *)(o))->priv)
51 static int summary_header_load(CamelStoreSummary *, FILE *);
52 static int summary_header_save(CamelStoreSummary *, FILE *);
54 /*static CamelStoreInfo * store_info_new(CamelStoreSummary *, const char *);*/
55 static CamelStoreInfo * store_info_load(CamelStoreSummary *, FILE *);
56 static int store_info_save(CamelStoreSummary *, FILE *, CamelStoreInfo *);
57 static void store_info_free(CamelStoreSummary *, CamelStoreInfo *);
59 static const char *store_info_string(CamelStoreSummary *, const CamelStoreInfo *, int);
60 static void store_info_set_string(CamelStoreSummary *, CamelStoreInfo *, int, const char *);
62 static void camel_imap_store_summary_class_init (CamelImapStoreSummaryClass *klass);
63 static void camel_imap_store_summary_init (CamelImapStoreSummary *obj);
64 static void camel_imap_store_summary_finalise (CamelObject *obj);
66 static CamelStoreSummaryClass *camel_imap_store_summary_parent;
69 camel_imap_store_summary_class_init (CamelImapStoreSummaryClass *klass)
71 CamelStoreSummaryClass *ssklass = (CamelStoreSummaryClass *)klass;
73 ssklass->summary_header_load = summary_header_load;
74 ssklass->summary_header_save = summary_header_save;
76 /*ssklass->store_info_new = store_info_new;*/
77 ssklass->store_info_load = store_info_load;
78 ssklass->store_info_save = store_info_save;
79 ssklass->store_info_free = store_info_free;
81 ssklass->store_info_string = store_info_string;
82 ssklass->store_info_set_string = store_info_set_string;
86 camel_imap_store_summary_init (CamelImapStoreSummary *s)
88 /*struct _CamelImapStoreSummaryPrivate *p;
90 p = _PRIVATE(s) = g_malloc0(sizeof(*p));*/
92 ((CamelStoreSummary *)s)->store_info_size = sizeof(CamelImapStoreInfo);
93 s->version = CAMEL_IMAP_STORE_SUMMARY_VERSION;
97 camel_imap_store_summary_finalise (CamelObject *obj)
99 /*struct _CamelImapStoreSummaryPrivate *p;*/
100 /*CamelImapStoreSummary *s = (CamelImapStoreSummary *)obj;*/
107 camel_imap_store_summary_get_type (void)
109 static CamelType type = CAMEL_INVALID_TYPE;
111 if (type == CAMEL_INVALID_TYPE) {
112 camel_imap_store_summary_parent = (CamelStoreSummaryClass *)camel_store_summary_get_type();
113 type = camel_type_register((CamelType)camel_imap_store_summary_parent, "CamelImapStoreSummary",
114 sizeof (CamelImapStoreSummary),
115 sizeof (CamelImapStoreSummaryClass),
116 (CamelObjectClassInitFunc) camel_imap_store_summary_class_init,
118 (CamelObjectInitFunc) camel_imap_store_summary_init,
119 (CamelObjectFinalizeFunc) camel_imap_store_summary_finalise);
126 * camel_imap_store_summary_new:
128 * Create a new CamelImapStoreSummary object.
130 * Return value: A new CamelImapStoreSummary widget.
132 CamelImapStoreSummary *
133 camel_imap_store_summary_new (void)
135 CamelImapStoreSummary *new = CAMEL_IMAP_STORE_SUMMARY ( camel_object_new (camel_imap_store_summary_get_type ()));
141 * camel_imap_store_summary_full_name:
145 * Retrieve a summary item by full name.
147 * A referenced to the summary item is returned, which may be
148 * ref'd or free'd as appropriate.
150 * Return value: The summary item, or NULL if the @full_name name
152 * It must be freed using camel_store_summary_info_free().
155 camel_imap_store_summary_full_name(CamelImapStoreSummary *s, const char *full_name)
158 CamelImapStoreInfo *info;
160 count = camel_store_summary_count((CamelStoreSummary *)s);
161 for (i=0;i<count;i++) {
162 info = (CamelImapStoreInfo *)camel_store_summary_index((CamelStoreSummary *)s, i);
164 if (strcmp(info->full_name, full_name) == 0)
166 camel_store_summary_info_free((CamelStoreSummary *)s, (CamelStoreInfo *)info);
174 camel_imap_store_summary_full_to_path(CamelImapStoreSummary *s, const char *full_name, char dir_sep)
180 if (dir_sep != '/') {
181 p = path = alloca(strlen(full_name)*3+1);
183 while ( (c = *f++ & 0xff) ) {
186 else if (c == '/' || c == '%')
187 p += sprintf(p, "%%%02X", c);
193 path = (char *)full_name;
195 return camel_utf7_utf8(path);
198 static guint32 hexnib(guint32 c)
200 if (c >= '0' && c <= '9')
202 else if (c>='A' && c <= 'Z')
209 camel_imap_store_summary_path_to_full(CamelImapStoreSummary *s, const char *path, char dir_sep)
211 unsigned char *full, *f;
215 char *subpath, *last = NULL;
217 CamelImapStoreNamespace *ns;
219 /* check to see if we have a subpath of path already defined */
220 subpath = alloca(strlen(path)+1);
221 strcpy(subpath, path);
223 si = camel_store_summary_path((CamelStoreSummary *)s, subpath);
225 last = strrchr(subpath, '/');
229 } while (si == NULL && last);
231 /* path is already present, use the raw version we have */
232 if (si && strlen(subpath) == strlen(path)) {
233 f = g_strdup(camel_imap_store_info_full_name(s, si));
234 camel_store_summary_info_free((CamelStoreSummary *)s, si);
238 ns = camel_imap_store_summary_namespace_find_path(s, path);
240 f = full = alloca(strlen(path)*2+1);
242 p = path + strlen(subpath);
244 p = path + strlen(ns->path);
248 while ( (c = camel_utf8_getc((const unsigned char **)&p)) ) {
256 camel_utf8_putc(&f, c);
266 camel_utf8_putc(&f, v);
270 camel_utf8_putc(&f, c);
272 /* merge old path part if required */
273 f = camel_utf8_utf7(full);
275 full = g_strdup_printf("%s%s", camel_imap_store_info_full_name(s, si), f);
277 camel_store_summary_info_free((CamelStoreSummary *)s, si);
280 full = g_strdup_printf("%s%s", ns->full_name, f);
289 camel_imap_store_summary_add_from_full(CamelImapStoreSummary *s, const char *full, char dir_sep)
291 CamelImapStoreInfo *info;
292 char *pathu8, *prefix;
295 CamelImapStoreNamespace *ns;
297 d(printf("adding full name '%s' '%c'\n", full, dir_sep));
300 full_name = alloca(len+1);
301 strcpy(full_name, full);
302 if (full_name[len-1] == dir_sep)
303 full_name[len-1] = 0;
305 info = camel_imap_store_summary_full_name(s, full_name);
307 camel_store_summary_info_free((CamelStoreSummary *)s, (CamelStoreInfo *)info);
308 d(printf(" already there\n"));
312 ns = camel_imap_store_summary_namespace_find_full(s, full_name);
314 d(printf("(found namespace for '%s' ns '%s') ", full_name, ns->path));
315 len = strlen(ns->full_name);
316 if (len >= strlen(full_name)) {
317 pathu8 = g_strdup(ns->path);
319 if (full_name[len] == ns->sep)
322 prefix = camel_imap_store_summary_full_to_path(s, full_name+len, ns->sep);
324 pathu8 = g_strdup_printf ("%s/%s", ns->path, prefix);
330 d(printf(" (pathu8 = '%s')", pathu8));
332 d(printf("(Cannot find namespace for '%s')\n", full_name));
333 pathu8 = camel_imap_store_summary_full_to_path(s, full_name, dir_sep);
336 info = (CamelImapStoreInfo *)camel_store_summary_add_from_path((CamelStoreSummary *)s, pathu8);
338 d(printf(" '%s' -> '%s'\n", pathu8, full_name));
339 camel_store_info_set_string((CamelStoreSummary *)s, (CamelStoreInfo *)info, CAMEL_IMAP_STORE_INFO_FULL_NAME, full_name);
341 d(printf(" failed\n"));
346 /* should this be const? */
347 /* TODO: deprecate/merge this function with path_to_full */
349 camel_imap_store_summary_full_from_path(CamelImapStoreSummary *s, const char *path)
351 CamelImapStoreNamespace *ns;
354 ns = camel_imap_store_summary_namespace_find_path(s, path);
356 name = camel_imap_store_summary_path_to_full(s, path, ns->sep);
358 d(printf("looking up path %s -> %s\n", path, name?name:"not found"));
363 /* TODO: this api needs some more work */
364 CamelImapStoreNamespace *camel_imap_store_summary_namespace_new(CamelImapStoreSummary *s, const char *full_name, char dir_sep)
366 CamelImapStoreNamespace *ns;
370 ns = g_malloc0(sizeof(*ns));
371 ns->full_name = g_strdup(full_name);
372 len = strlen(ns->full_name)-1;
373 if (len >= 0 && ns->full_name[len] == dir_sep)
374 ns->full_name[len] = 0;
377 o = p = ns->path = camel_imap_store_summary_full_to_path(s, ns->full_name, dir_sep);
390 void camel_imap_store_summary_namespace_set(CamelImapStoreSummary *s, CamelImapStoreNamespace *ns)
392 static void namespace_clear(CamelStoreSummary *s);
394 d(printf("Setting namesapce to '%s' '%c' -> '%s'\n", ns->full_name, ns->sep, ns->path));
395 namespace_clear((CamelStoreSummary *)s);
397 camel_store_summary_touch((CamelStoreSummary *)s);
400 CamelImapStoreNamespace *
401 camel_imap_store_summary_namespace_find_path(CamelImapStoreSummary *s, const char *path)
404 CamelImapStoreNamespace *ns;
406 /* NB: this currently only compares against 1 namespace, in future compare against others */
409 len = strlen(ns->path);
411 || (strncmp(ns->path, path, len) == 0
412 && (path[len] == '/' || path[len] == 0)))
417 /* have a default? */
421 CamelImapStoreNamespace *
422 camel_imap_store_summary_namespace_find_full(CamelImapStoreSummary *s, const char *full)
425 CamelImapStoreNamespace *ns;
427 /* NB: this currently only compares against 1 namespace, in future compare against others */
430 len = strlen(ns->full_name);
431 d(printf("find_full: comparing namespace '%s' to name '%s'\n", ns->full_name, full));
433 || (strncmp(ns->full_name, full, len) == 0
434 && (full[len] == ns->sep || full[len] == 0)))
439 /* have a default? */
444 namespace_free(CamelStoreSummary *s, CamelImapStoreNamespace *ns)
447 g_free(ns->full_name);
452 namespace_clear(CamelStoreSummary *s)
454 CamelImapStoreSummary *is = (CamelImapStoreSummary *)s;
457 namespace_free(s, is->namespace);
458 is->namespace = NULL;
461 static CamelImapStoreNamespace *
462 namespace_load(CamelStoreSummary *s, FILE *in)
464 CamelImapStoreNamespace *ns;
467 ns = g_malloc0(sizeof(*ns));
468 if (camel_file_util_decode_string(in, &ns->path) == -1
469 || camel_file_util_decode_string(in, &ns->full_name) == -1
470 || camel_file_util_decode_uint32(in, &sep) == -1) {
471 namespace_free(s, ns);
481 namespace_save(CamelStoreSummary *s, FILE *in, CamelImapStoreNamespace *ns)
483 if (camel_file_util_encode_string(in, ns->path) == -1
484 || camel_file_util_encode_string(in, ns->full_name) == -1
485 || camel_file_util_encode_uint32(in, (guint32)ns->sep) == -1)
492 summary_header_load(CamelStoreSummary *s, FILE *in)
494 CamelImapStoreSummary *is = (CamelImapStoreSummary *)s;
495 gint32 version, capabilities, count;
499 if (camel_imap_store_summary_parent->summary_header_load((CamelStoreSummary *)s, in) == -1
500 || camel_file_util_decode_fixed_int32(in, &version) == -1)
503 is->version = version;
505 if (version < CAMEL_IMAP_STORE_SUMMARY_VERSION_0) {
506 g_warning("Store summary header version too low");
510 /* note file format can be expanded to contain more namespaces, but only 1 at the moment */
511 if (camel_file_util_decode_fixed_int32(in, &capabilities) == -1
512 || camel_file_util_decode_fixed_int32(in, &count) == -1
516 is->capabilities = capabilities;
518 if ((is->namespace = namespace_load(s, in)) == NULL)
526 summary_header_save(CamelStoreSummary *s, FILE *out)
528 CamelImapStoreSummary *is = (CamelImapStoreSummary *)s;
531 count = is->namespace?1:0;
533 /* always write as latest version */
534 if (camel_imap_store_summary_parent->summary_header_save((CamelStoreSummary *)s, out) == -1
535 || camel_file_util_encode_fixed_int32(out, CAMEL_IMAP_STORE_SUMMARY_VERSION) == -1
536 || camel_file_util_encode_fixed_int32(out, is->capabilities) == -1
537 || camel_file_util_encode_fixed_int32(out, count) == -1)
540 if (is->namespace && namespace_save(s, out, is->namespace) == -1)
546 static CamelStoreInfo *
547 store_info_load(CamelStoreSummary *s, FILE *in)
549 CamelImapStoreInfo *mi;
551 mi = (CamelImapStoreInfo *)camel_imap_store_summary_parent->store_info_load(s, in);
553 if (camel_file_util_decode_string(in, &mi->full_name) == -1) {
554 camel_store_summary_info_free(s, (CamelStoreInfo *)mi);
559 return (CamelStoreInfo *)mi;
563 store_info_save(CamelStoreSummary *s, FILE *out, CamelStoreInfo *mi)
565 CamelImapStoreInfo *isi = (CamelImapStoreInfo *)mi;
567 if (camel_imap_store_summary_parent->store_info_save(s, out, mi) == -1
568 || camel_file_util_encode_string(out, isi->full_name) == -1)
575 store_info_free(CamelStoreSummary *s, CamelStoreInfo *mi)
577 CamelImapStoreInfo *isi = (CamelImapStoreInfo *)mi;
579 g_free(isi->full_name);
580 camel_imap_store_summary_parent->store_info_free(s, mi);
584 store_info_string(CamelStoreSummary *s, const CamelStoreInfo *mi, int type)
586 CamelImapStoreInfo *isi = (CamelImapStoreInfo *)mi;
590 g_assert (mi != NULL);
593 case CAMEL_IMAP_STORE_INFO_FULL_NAME:
594 return isi->full_name;
596 return camel_imap_store_summary_parent->store_info_string(s, mi, type);
601 store_info_set_string(CamelStoreSummary *s, CamelStoreInfo *mi, int type, const char *str)
603 CamelImapStoreInfo *isi = (CamelImapStoreInfo *)mi;
605 g_assert(mi != NULL);
608 case CAMEL_IMAP_STORE_INFO_FULL_NAME:
609 d(printf("Set full name %s -> %s\n", isi->full_name, str));
610 CAMEL_STORE_SUMMARY_LOCK(s, summary_lock);
611 g_free(isi->full_name);
612 isi->full_name = g_strdup(str);
613 CAMEL_STORE_SUMMARY_UNLOCK(s, summary_lock);
616 camel_imap_store_summary_parent->store_info_set_string(s, mi, type, str);