From 234e613bc3293628516fc38ad6e695b02a8b11a4 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Wed, 9 Aug 2000 00:07:56 +0000 Subject: New code to keep an on-disk cache of what UIDs have been seen in a folder. * camel-uid-cache.c: New code to keep an on-disk cache of what UIDs have been seen in a folder. * camel-provider.h: Add new flags CAMEL_PROVIDER_IS_SOURCE (mail can arrive in it by non-Camel means) and CAMEL_PROVIDER_IS_STORAGE (you can work with mail directly without needing to copy it local). * providers/*/camel-*-provider.c: Add flags as needed: imap and mbox are SOURCE and STORAGE. mh and nntp are just STORAGE, pop3 is just SOURCE. * camel-mime-message.c (process_header): Add another subject g_strstrip that fejj's earlier commit missed. svn path=/trunk/; revision=4616 --- camel/ChangeLog | 11 ++ camel/Makefile.am | 76 ++++++------ camel/camel-provider.h | 5 + camel/camel-uid-cache.c | 186 +++++++++++++++++++++++++++++ camel/camel-uid-cache.h | 56 +++++++++ camel/camel.h | 1 + camel/providers/imap/camel-imap-provider.c | 2 +- camel/providers/mbox/camel-mbox-provider.c | 2 +- camel/providers/mh/camel-mh-provider.c | 2 +- camel/providers/nntp/camel-nntp-provider.c | 2 +- camel/providers/pop3/camel-pop3-provider.c | 2 +- 11 files changed, 303 insertions(+), 42 deletions(-) create mode 100644 camel/camel-uid-cache.c create mode 100644 camel/camel-uid-cache.h diff --git a/camel/ChangeLog b/camel/ChangeLog index a7aa391b8d..55eae4be3b 100644 --- a/camel/ChangeLog +++ b/camel/ChangeLog @@ -1,5 +1,16 @@ 2000-08-08 Dan Winship + * camel-uid-cache.c: New code to keep an on-disk cache of what + UIDs have been seen in a folder. + + * camel-provider.h: Add new flags CAMEL_PROVIDER_IS_SOURCE (mail + can arrive in it by non-Camel means) and CAMEL_PROVIDER_IS_STORAGE + (you can work with mail directly without needing to copy it local). + + * providers/*/camel-*-provider.c: Add flags as needed: imap and + mbox are SOURCE and STORAGE. mh and nntp are just STORAGE, pop3 is + just SOURCE. + * camel-mime-message.c (process_header): Add another subject g_strstrip that fejj's earlier commit missed. diff --git a/camel/Makefile.am b/camel/Makefile.am index 20d4193caa..a265d3a027 100644 --- a/camel/Makefile.am +++ b/camel/Makefile.am @@ -15,16 +15,27 @@ INCLUDES = -I.. -I$(srcdir)/.. -I$(includedir) \ -DG_LOG_DOMAIN=\"camel\" libcamel_la_SOURCES = \ - camel.c \ + broken-date-parser.c \ + camel-address.c \ camel-data-wrapper.c \ camel-exception.c \ - camel-folder.c \ camel-folder-search.c \ camel-folder-summary.c \ + camel-folder.c \ + camel-internet-address.c \ camel-medium.c \ + camel-mime-filter-basic.c \ + camel-mime-filter-charset.c \ + camel-mime-filter-crlf.c \ + camel-mime-filter-from.c \ + camel-mime-filter-index.c \ + camel-mime-filter-save.c \ + camel-mime-filter.c \ camel-mime-message.c \ - camel-mime-part.c \ + camel-mime-parser.c \ camel-mime-part-utils.c \ + camel-mime-part.c \ + camel-mime-utils.c \ camel-movemail.c \ camel-multipart.c \ camel-object.c \ @@ -34,42 +45,44 @@ libcamel_la_SOURCES = \ camel-service.c \ camel-session.c \ camel-store.c \ - camel-stream.c \ camel-stream-buffer.c \ + camel-stream-filter.c \ camel-stream-fs.c \ camel-stream-mem.c \ + camel-stream.c \ camel-transport.c \ + camel-uid-cache.c \ camel-url.c \ + camel.c \ gmime-content-field.c \ gstring-util.c \ hash-table-utils.c \ md5-utils.c \ - string-utils.c \ - broken-date-parser.c \ - camel-mime-parser.c \ - camel-mime-utils.c \ - camel-mime-filter.c \ - camel-mime-filter-basic.c \ - camel-mime-filter-save.c \ - camel-mime-filter-charset.c \ - camel-mime-filter-index.c \ - camel-mime-filter-from.c \ - camel-mime-filter-crlf.c \ - camel-stream-filter.c \ - camel-address.c \ - camel-internet-address.c + string-utils.c libcamelinclude_HEADERS = \ - camel.h \ + broken-date-parser.h \ + camel-address.h \ camel-data-wrapper.h \ + camel-exception-list.def \ camel-exception.h \ - camel-folder.h \ camel-folder-search.h \ camel-folder-summary.h \ + camel-folder.h \ + camel-internet-address.h \ camel-medium.h \ + camel-mime-filter-basic.h \ + camel-mime-filter-charset.h \ + camel-mime-filter-crlf.h \ + camel-mime-filter-from.h \ + camel-mime-filter-index.h \ + camel-mime-filter-save.h \ + camel-mime-filter.h \ camel-mime-message.h \ - camel-mime-part.h \ + camel-mime-parser.h \ camel-mime-part-utils.h \ + camel-mime-part.h \ + camel-mime-utils.h \ camel-movemail.h \ camel-multipart.h \ camel-object.h \ @@ -79,32 +92,21 @@ libcamelinclude_HEADERS = \ camel-service.h \ camel-session.h \ camel-store.h \ - camel-stream.h \ camel-stream-buffer.h \ + camel-stream-filter.h \ camel-stream-fs.h \ camel-stream-mem.h \ + camel-stream.h \ camel-transport.h \ camel-types.h \ + camel-uid-cache.h \ camel-url.h \ + camel.h \ gmime-content-field.h \ gstring-util.h \ hash-table-utils.h \ md5-utils.h \ - string-utils.h \ - broken-date-parser.h \ - camel-exception-list.def \ - camel-mime-parser.h \ - camel-mime-utils.h \ - camel-mime-filter.h \ - camel-mime-filter-basic.h \ - camel-mime-filter-save.h \ - camel-mime-filter-charset.h \ - camel-mime-filter-index.h \ - camel-mime-filter-from.h \ - camel-mime-filter-crlf.h \ - camel-stream-filter.h \ - camel-address.h \ - camel-internet-address.h + string-utils.h libcamel_la_LDFLAGS = -version-info 0:0:0 -rpath $(libdir) diff --git a/camel/camel-provider.h b/camel/camel-provider.h index 58d63a773e..4f2a67839f 100644 --- a/camel/camel-provider.h +++ b/camel/camel-provider.h @@ -48,7 +48,12 @@ typedef enum { extern char *camel_provider_type_name[CAMEL_NUM_PROVIDER_TYPES]; +/* _IS_SOURCE means the user can get mail from there. + * _IS_STORAGE means the user can read mail from there. + */ #define CAMEL_PROVIDER_IS_REMOTE (1 << 0) +#define CAMEL_PROVIDER_IS_SOURCE (1 << 1) +#define CAMEL_PROVIDER_IS_STORAGE (1 << 2) typedef struct { /* Provider name used in CamelURLs. */ diff --git a/camel/camel-uid-cache.c b/camel/camel-uid-cache.c new file mode 100644 index 0000000000..2b5faddb91 --- /dev/null +++ b/camel/camel-uid-cache.c @@ -0,0 +1,186 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-uid-cache.c: UID caching code. */ + +/* + * Authors: + * Dan Winship + * + * Copyright 2000 Helix Code, Inc. (http://www.helixcode.com) + * + * 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 the Free Software Foundation; either version 2 of the + * License, or (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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include "config.h" +#include "camel-uid-cache.h" + +#include +#include +#include +#include +#include + +static void free_uid (gpointer key, gpointer value, gpointer data); +static void maybe_write_uid (gpointer key, gpointer value, gpointer data); + +/** + * camel_uid_cache_new: + * @filename: path to load the cache from + * + * Creates a new UID cache, initialized from @filename. If @filename + * doesn't already exist, the UID cache will be empty. Otherwise, if + * it does exist but can't be read, the function will return %NULL. + * + * Return value: a new UID cache, or %NULL + **/ +CamelUIDCache * +camel_uid_cache_new (const char *filename) +{ + CamelUIDCache *cache; + struct stat st; + char *buf, **uids; + int fd, i; + + fd = open (filename, O_RDWR | O_CREAT, 0700); + if (fd == -1) + return NULL; + + if (fstat (fd, &st) != 0) { + close (fd); + return NULL; + } + buf = g_malloc (st.st_size + 1); + + if (read (fd, buf, st.st_size) == -1) { + close (fd); + g_free (buf); + return NULL; + } + buf[st.st_size] = '\0'; + + cache = g_new (CamelUIDCache, 1); + cache->fd = fd; + cache->level = 1; + cache->uids = g_hash_table_new (g_str_hash, g_str_equal); + + uids = g_strsplit (buf, "\n", 0); + g_free (buf); + for (i = 0; uids[i]; i++) { + g_hash_table_insert (cache->uids, uids[i], + GINT_TO_POINTER (cache->level)); + } + g_free (uids); + + return cache; +} + +/** + * camel_uid_cache_save: + * @cache: a CamelUIDCache + * + * Attempts to save @cache back to disk. + * + * Return value: success or failure + **/ +gboolean +camel_uid_cache_save (CamelUIDCache *cache) +{ + if (lseek (cache->fd, 0, SEEK_SET) != 0) + return FALSE; + g_hash_table_foreach (cache->uids, maybe_write_uid, cache); + return ftruncate (cache->fd, lseek (cache->fd, 0, SEEK_CUR)) == 0; +} + +static void +maybe_write_uid (gpointer key, gpointer value, gpointer data) +{ + CamelUIDCache *cache = data; + + if (GPOINTER_TO_INT (value) == cache->level) { + write (cache->fd, key, strlen (key)); + write (cache->fd, "\n", 1); + } +} + +/** + * camel_uid_cache_destroy: + * @cache: a CamelUIDCache + * + * Destroys @cache and frees its data. + **/ +void +camel_uid_cache_destroy (CamelUIDCache *cache) +{ + g_hash_table_foreach (cache->uids, free_uid, NULL); + g_hash_table_destroy (cache->uids); + close (cache->fd); + g_free (cache); +} + +static void +free_uid (gpointer key, gpointer value, gpointer data) +{ + g_free (key); +} + +/** + * camel_uid_cache_get_new_uids: + * @cache: a CamelUIDCache + * @uids: an array of UIDs + * + * Returns an array of UIDs from @uids that are not in @cache, and + * removes UIDs from @cache that aren't in @uids. + * + * Return value: an array of new UIDs, which must be freed with + * camel_uid_cache_free_uids(). + **/ +GPtrArray * +camel_uid_cache_get_new_uids (CamelUIDCache *cache, GPtrArray *uids) +{ + GPtrArray *new_uids; + char *uid; + int i; + + new_uids = g_ptr_array_new (); + cache->level++; + + for (i = 0; i < uids->len; i++) { + uid = uids->pdata[i]; + if (g_hash_table_lookup (cache->uids, uid)) + g_hash_table_remove (cache->uids, uid); + else + g_ptr_array_add (new_uids, g_strdup (uid)); + g_hash_table_insert (cache->uids, g_strdup (uid), + GINT_TO_POINTER (cache->level)); + } + + return new_uids; +} + +/** + * camel_uid_cache_free_uids: + * @uids: an array returned from camel_uid_cache_get_new_uids() + * + * Frees the array of UIDs. + **/ +void +camel_uid_cache_free_uids (GPtrArray *uids) +{ + int i; + + for (i = 0; i < uids->len; i++) + g_free (uids->pdata[i]); + g_ptr_array_free (uids, TRUE); +} diff --git a/camel/camel-uid-cache.h b/camel/camel-uid-cache.h new file mode 100644 index 0000000000..92226a3afd --- /dev/null +++ b/camel/camel-uid-cache.h @@ -0,0 +1,56 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-uid-cache.h: UID caching code. */ + +/* + * Authors: + * Bertrand Guiheneuf + * + * Copyright 2000 Helix Code, Inc. (http://www.helixcode.com) + * + * 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 the Free Software Foundation; either version 2 of the + * License, or (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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef CAMEL_UID_CACHE_H +#define CAMEL_UID_CACHE_H 1 + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus */ + +#include +#include + +typedef struct { + int fd, level; + GHashTable *uids; +} CamelUIDCache; + +CamelUIDCache *camel_uid_cache_new (const char *filename); +gboolean camel_uid_cache_save (CamelUIDCache *cache); +void camel_uid_cache_destroy (CamelUIDCache *cache); + +GPtrArray *camel_uid_cache_get_new_uids (CamelUIDCache *cache, + GPtrArray *uids); +void camel_uid_cache_free_uids (GPtrArray *uids); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CAMEL_UID_CACHE_H */ diff --git a/camel/camel.h b/camel/camel.h index e9300f83cb..752b145b49 100644 --- a/camel/camel.h +++ b/camel/camel.h @@ -62,6 +62,7 @@ extern "C" { #include #include #include +#include #include #include #include diff --git a/camel/providers/imap/camel-imap-provider.c b/camel/providers/imap/camel-imap-provider.c index b9d8c74847..21452d5cee 100644 --- a/camel/providers/imap/camel-imap-provider.c +++ b/camel/providers/imap/camel-imap-provider.c @@ -42,7 +42,7 @@ static CamelProvider imap_provider = { "mail", - 0, + CAMEL_PROVIDER_IS_REMOTE | CAMEL_PROVIDER_IS_SOURCE | CAMEL_PROVIDER_IS_STORAGE, { 0, 0 }, diff --git a/camel/providers/mbox/camel-mbox-provider.c b/camel/providers/mbox/camel-mbox-provider.c index de206f18ef..d4048e1be3 100644 --- a/camel/providers/mbox/camel-mbox-provider.c +++ b/camel/providers/mbox/camel-mbox-provider.c @@ -38,7 +38,7 @@ static CamelProvider mbox_provider = { "mail", - 0, + CAMEL_PROVIDER_IS_SOURCE | CAMEL_PROVIDER_IS_STORAGE, { 0, 0 }, diff --git a/camel/providers/mh/camel-mh-provider.c b/camel/providers/mh/camel-mh-provider.c index 6a0aaac492..58e97eed30 100644 --- a/camel/providers/mh/camel-mh-provider.c +++ b/camel/providers/mh/camel-mh-provider.c @@ -37,7 +37,7 @@ static CamelProvider mh_provider = { "mail", - 0, + CAMEL_PROVIDER_IS_STORAGE, {0, 0}, diff --git a/camel/providers/nntp/camel-nntp-provider.c b/camel/providers/nntp/camel-nntp-provider.c index abfce31c5e..8badba0e9c 100644 --- a/camel/providers/nntp/camel-nntp-provider.c +++ b/camel/providers/nntp/camel-nntp-provider.c @@ -36,7 +36,7 @@ static CamelProvider news_provider = { "news", - CAMEL_PROVIDER_IS_REMOTE, + CAMEL_PROVIDER_IS_REMOTE | CAMEL_PROVIDER_IS_STORAGE, { 0, 0 }, diff --git a/camel/providers/pop3/camel-pop3-provider.c b/camel/providers/pop3/camel-pop3-provider.c index 4ccb06058f..52399c99ec 100644 --- a/camel/providers/pop3/camel-pop3-provider.c +++ b/camel/providers/pop3/camel-pop3-provider.c @@ -39,7 +39,7 @@ static CamelProvider pop3_provider = { "mail", - CAMEL_PROVIDER_IS_REMOTE, + CAMEL_PROVIDER_IS_REMOTE | CAMEL_PROVIDER_IS_SOURCE, { 0, 0 }, -- cgit