diff options
author | Dan Winship <danw@src.gnome.org> | 2000-09-06 04:33:05 +0800 |
---|---|---|
committer | Dan Winship <danw@src.gnome.org> | 2000-09-06 04:33:05 +0800 |
commit | 7a21e4e9044d17d340e6e3ba2fbc4b110123d9f3 (patch) | |
tree | 78c584c5517b90a9b28b5d767bff41e902e3206d /camel | |
parent | 52d74ec874d411dfb8a51d7e5cbae648c5efdd44 (diff) | |
download | gsoc2013-evolution-7a21e4e9044d17d340e6e3ba2fbc4b110123d9f3.tar.gz gsoc2013-evolution-7a21e4e9044d17d340e6e3ba2fbc4b110123d9f3.tar.zst gsoc2013-evolution-7a21e4e9044d17d340e6e3ba2fbc4b110123d9f3.zip |
Cache provider, for caching a remote store locally. This is not done yet.
svn path=/trunk/; revision=5206
Diffstat (limited to 'camel')
-rw-r--r-- | camel/providers/cache/Makefile.am | 35 | ||||
-rw-r--r-- | camel/providers/cache/camel-cache-folder.c | 689 | ||||
-rw-r--r-- | camel/providers/cache/camel-cache-folder.h | 87 | ||||
-rw-r--r-- | camel/providers/cache/camel-cache-map.c | 255 | ||||
-rw-r--r-- | camel/providers/cache/camel-cache-map.h | 64 | ||||
-rw-r--r-- | camel/providers/cache/camel-cache-provider.c | 51 | ||||
-rw-r--r-- | camel/providers/cache/camel-cache-store.c | 222 | ||||
-rw-r--r-- | camel/providers/cache/camel-cache-store.h | 67 | ||||
-rw-r--r-- | camel/providers/cache/libcamelcache.urls | 1 |
9 files changed, 1471 insertions, 0 deletions
diff --git a/camel/providers/cache/Makefile.am b/camel/providers/cache/Makefile.am new file mode 100644 index 0000000000..7f9f6a9df5 --- /dev/null +++ b/camel/providers/cache/Makefile.am @@ -0,0 +1,35 @@ +## Process this file with automake to produce Makefile.in + +SUBDIRS = + +libcamelcacheincludedir = $(includedir)/camel + +providerdir = $(pkglibdir)/camel-providers/$(VERSION) + +provider_LTLIBRARIES = libcamelcache.la +provider_DATA = libcamelcache.urls + +INCLUDES = \ + -I.. \ + -I$(srcdir)/.. \ + -I$(srcdir)/../../.. \ + -I$(includedir) \ + -I$(top_srcdir)/intl \ + $(GTK_INCLUDEDIR) \ + -I$(top_srcdir)/camel \ + -DG_LOG_DOMAIN=\"camel-cache-provider\" + +libcamelcache_la_SOURCES = \ + camel-cache-folder.c \ + camel-cache-provider.c \ + camel-cache-store.c \ + camel-cache-map.c + +libcamelcacheinclude_HEADERS = \ + camel-cache-folder.h \ + camel-cache-store.h + + +libcamelcache_la_LDFLAGS = -version-info 0:0:0 + +EXTRA_DIST = libcamelcache.urls diff --git a/camel/providers/cache/camel-cache-folder.c b/camel/providers/cache/camel-cache-folder.c new file mode 100644 index 0000000000..0d24fb58bb --- /dev/null +++ b/camel/providers/cache/camel-cache-folder.c @@ -0,0 +1,689 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-cache-folder.c : class for a cache folder */ + +/* + * Authors: + * Dan Winship <danw@helixcode.com> + * + * Copyright (C) 2000 Helix Code, Inc. (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 + */ + + +/* + * Notes on the cache provider: + * + * We require that the remote folder have persistent UIDs, and nothing + * else. We require that the local store folder have persistent UIDs + * and summary capability. + * + * If the remote folder does not have summary capability, we will need + * to sync any new messages over to the local folder when the folder + * is opened or when it changes. If the remote folder does have + * summary capability, we can be more relaxed about doing this. + * + * If the remote folder has search capability, we will use it, at + * least when the folder isn't synced. Otherwise if the local folder + * has search capability, we will use that (but it will require + * syncing the remote folder locally to use). Otherwise the cache + * folder won't have search capability. + * + * CamelCacheFolder UIDs are remote UIDs, because we need to be able + * to return a complete list of them at get_uids time, and the + * messages might not all be present in the local folder, and we can't + * predict what UIDs will be assigned to them when they are cached + * there. We keep hash tables mapping remote to local UIDs and vice + * versa, and a map file to cache this information between sessions. + * The maps must always be 100% accurate. + * + * The messages in the local folder may not be in the same order as + * the messages in the remote folder. + * + * + * Many operations on the local folder are done with a NULL + * CamelException, because having them fail only results in efficiency + * problems, not actual permanent failures. (Eg, get_message will + * try to append the message to the local folder, but doesn't check + * for failure, because it already has the message to pass back to the + * user.) + */ + +#include "camel-cache-folder.h" +#include "camel-cache-store.h" +#include <camel/camel-exception.h> +#include <camel/camel-medium.h> + +static CamelFolderClass *parent_class; +#define CF_CLASS(so) CAMEL_FOLDER_CLASS (GTK_OBJECT(so)->klass) + +static void init (CamelFolder *folder, CamelStore *parent_store, + CamelFolder *parent_folder, const gchar *name, + gchar *separator, gboolean path_begins_with_sep, + CamelException *ex); + +static void cache_sync (CamelFolder *folder, gboolean expunge, + CamelException *ex); + +static void expunge (CamelFolder *folder, CamelException *ex); + +static gint get_message_count (CamelFolder *folder); + +static void append_message (CamelFolder *folder, CamelMimeMessage *message, + guint32 flags, CamelException *ex); + +static guint32 get_message_flags (CamelFolder *folder, const char *uid); +static void set_message_flags (CamelFolder *folder, const char *uid, + guint32 flags, guint32 set); +static gboolean get_message_user_flag (CamelFolder *folder, const char *uid, + const char *name); +static void set_message_user_flag (CamelFolder *folder, const char *uid, + const char *name, gboolean value); + +static CamelMimeMessage *get_message (CamelFolder *folder, + const gchar *uid, + CamelException *ex); + +static GPtrArray *get_uids (CamelFolder *folder); +static GPtrArray *get_summary (CamelFolder *folder); +static GPtrArray *get_subfolder_names (CamelFolder *folder); +static void free_subfolder_names (CamelFolder *folder, GPtrArray *subfolders); + +static GPtrArray *search_by_expression (CamelFolder *folder, + const char *expression, + CamelException *ex); + +static const CamelMessageInfo *get_message_info (CamelFolder *folder, + const char *uid); + +static void finalize (GtkObject *object); + +static void +camel_cache_folder_class_init (CamelCacheFolderClass *camel_cache_folder_class) +{ + CamelFolderClass *camel_folder_class = + CAMEL_FOLDER_CLASS (camel_cache_folder_class); + GtkObjectClass *gtk_object_class = + GTK_OBJECT_CLASS (camel_cache_folder_class); + + parent_class = gtk_type_class (camel_folder_get_type ()); + + /* virtual method overload */ + camel_folder_class->init = init; + camel_folder_class->sync = cache_sync; + camel_folder_class->expunge = expunge; + camel_folder_class->get_message_count = get_message_count; + camel_folder_class->append_message = append_message; + camel_folder_class->get_message_flags = get_message_flags; + camel_folder_class->set_message_flags = set_message_flags; + camel_folder_class->get_message_user_flag = get_message_user_flag; + camel_folder_class->set_message_user_flag = set_message_user_flag; + camel_folder_class->get_message = get_message; + camel_folder_class->get_uids = get_uids; + camel_folder_class->free_uids = camel_folder_free_nop; + camel_folder_class->get_summary = get_summary; + camel_folder_class->free_summary = camel_folder_free_nop; + camel_folder_class->get_subfolder_names = get_subfolder_names; + camel_folder_class->free_subfolder_names = free_subfolder_names; + camel_folder_class->search_by_expression = search_by_expression; + camel_folder_class->get_message_info = get_message_info; + + gtk_object_class->finalize = finalize; +} + +GtkType +camel_cache_folder_get_type (void) +{ + static GtkType camel_cache_folder_type = 0; + + if (!camel_cache_folder_type) { + GtkTypeInfo camel_cache_folder_info = + { + "CamelCacheFolder", + sizeof (CamelCacheFolder), + sizeof (CamelCacheFolderClass), + (GtkClassInitFunc) camel_cache_folder_class_init, + (GtkObjectInitFunc) NULL, + /* reserved_1 */ NULL, + /* reserved_2 */ NULL, + (GtkClassInitFunc) NULL, + }; + + camel_cache_folder_type = gtk_type_unique (CAMEL_FOLDER_TYPE, &camel_cache_folder_info); + } + + return camel_cache_folder_type; +} + + +static void +cache_free_summary (CamelCacheFolder *cache_folder) +{ + if (cache_folder->remote_summary) { + camel_folder_free_summary (cache_folder->remote, + cache_folder->summary); + } else { + int i; + + for (i = 0; i < cache_folder->summary->len; i++) { + camel_message_info_free ( + cache_folder->summary->pdata[i]); + } + g_ptr_array_free (cache_folder->summary, TRUE); + g_hash_table_destroy (cache_folder->summary_uids); + } +} + +static void +finalize (GtkObject *object) +{ + CamelCacheFolder *cache_folder = CAMEL_CACHE_FOLDER (object); + + if (cache_folder->uids) { + camel_folder_free_uids (cache_folder->remote, + cache_folder->uids); + } + if (cache_folder->summary) + cache_free_summary (cache_folder); + + if (cache_folder->uidmap) + camel_cache_map_destroy (cache_folder->uidmap); + + gtk_object_unref (GTK_OBJECT (cache_folder->local)); + gtk_object_unref (GTK_OBJECT (cache_folder->remote)); + + g_free (cache_folder->mapfile); + + GTK_OBJECT_CLASS (parent_class)->finalize (object); +} + + +static void +update (CamelCacheFolder *cache_folder, CamelException *ex) +{ + if (cache_folder->uids) { + camel_folder_free_uids (cache_folder->remote, + cache_folder->uids); + } + cache_folder->uids = camel_folder_get_uids (cache_folder->remote); + + if (cache_folder->summary) + cache_free_summary (cache_folder); + + if (cache_folder->remote_summary) { + cache_folder->summary = + camel_folder_get_summary (cache_folder->remote); + } else { + CamelMessageInfo *mi; + GPtrArray *lsummary; + const char *ruid; + int i; + + if (!cache_folder->is_synced) { + camel_cache_folder_sync (cache_folder, ex); + if (camel_exception_is_set (ex)) + return; + } + + cache_folder->summary = g_ptr_array_new (); + cache_folder->summary_uids = g_hash_table_new (g_str_hash, + g_str_equal); + + lsummary = camel_folder_get_summary (cache_folder->local); + + /* For each local message, duplicate its info, replace + * the uid with the remote one, and add it to the + * uid->info cache. + */ + for (i = 0; i < lsummary->len; i++) { + mi = lsummary->pdata[i]; + ruid = camel_cache_map_get_remote (cache_folder->uidmap, mi->uid); + if (!ruid) { + /* Stale message. Delete it from cache. */ + camel_folder_delete_message ( + cache_folder->local, mi->uid); + continue; + } + + mi = g_new (CamelMessageInfo, 1); + camel_message_info_dup_to (lsummary->pdata[i], mi); + g_free (mi->uid); + mi->uid = g_strdup (ruid); + g_hash_table_insert (cache_folder->summary_uids, + mi->uid, mi); + } + camel_folder_free_summary (cache_folder->local, lsummary); + + /* Now build the summary array in remote UID order. */ + for (i = 0; i < cache_folder->uids->len; i++) { + mi = g_hash_table_lookup (cache_folder->summary_uids, + cache_folder->uids->pdata[i]); + g_ptr_array_add (cache_folder->summary, mi); + } + } + +} + +static void +init (CamelFolder *folder, CamelStore *parent_store, + CamelFolder *parent_folder, const gchar *name, gchar *separator, + gboolean path_begins_with_sep, CamelException *ex) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + char *path; + + CF_CLASS (folder)->init (folder, parent_store, parent_folder, + name, separator, path_begins_with_sep, ex); + if (camel_exception_is_set (ex)) + return; + + folder->permanent_flags = + camel_folder_get_permanent_flags (cache_folder->local); + folder->can_hold_folders = cache_folder->remote->can_hold_folders; + folder->can_hold_messages = cache_folder->remote->can_hold_messages; + folder->has_summary_capability = TRUE; + folder->has_search_capability = + camel_folder_has_search_capability (cache_folder->local) || + camel_folder_has_search_capability (cache_folder->remote); + + cache_folder->remote_summary = + camel_folder_has_summary_capability (cache_folder->remote); + + /* Load UIDs, summary, etc. */ + path = CAMEL_SERVICE (cache_folder->local->parent_store)->url->path; + cache_folder->mapfile = g_strdup_printf ("%s/%s.map", path, name); + cache_folder->uidmap = camel_cache_map_new (); + camel_cache_map_read (cache_folder->uidmap, cache_folder->mapfile, ex); + if (camel_exception_is_set (ex)) + return; + update (cache_folder, ex); + + return; +} + +/* If the remote folder changes, cache the new messages if necessary, + * update the summary, and propagate the signal. + */ +static void +remote_folder_changed (CamelFolder *remote_folder, int type, gpointer data) +{ + CamelCacheFolder *cache_folder = data; + + update (cache_folder, NULL); + gtk_signal_emit_by_name (GTK_OBJECT (cache_folder), "folder_changed", + type); +} + +/* If the local folder changes, it's because we just cached a message + * or expunged messages. Look for new messages and update the UID maps. + */ +static void +local_folder_changed (CamelFolder *local_folder, int type, gpointer data) +{ + CamelCacheFolder *cache_folder = data; + CamelMimeMessage *msg; + GPtrArray *new_luids; + char *luid; + const char *ruid; + int i; + + /* Get the updated list of local UIDs. For any that we didn't + * already know about, figure out the corresponding remote + * UID. + */ + new_luids = camel_folder_get_uids (local_folder); + for (i = 0; i < new_luids->len; i++) { + luid = new_luids->pdata[i]; + if (!camel_cache_map_get_remote (cache_folder->uidmap, luid)) { + msg = camel_folder_get_message (local_folder, + luid, NULL); + if (!msg) + continue; /* Hrmph. */ + ruid = camel_medium_get_header (CAMEL_MEDIUM (msg), + "X-Evolution-Remote-UID"); + if (!ruid) { + /* How'd that get here? */ + camel_folder_delete_message (local_folder, + luid); + continue; + } + + camel_cache_map_update (cache_folder->uidmap, + luid, ruid); + } + } + camel_folder_free_uids (local_folder, new_luids); + + /* FIXME: the uidmaps contain bad data now. */ +} + +/* DONE */ +static void +cache_sync (CamelFolder *folder, gboolean expunge, CamelException *ex) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + camel_folder_sync (cache_folder->remote, expunge, ex); + if (!camel_exception_is_set (ex)) { + camel_folder_sync (cache_folder->local, expunge, NULL); + camel_cache_map_write (cache_folder->uidmap, + cache_folder->mapfile, ex); + } +} + +/* DONE */ +static void +expunge (CamelFolder *folder, CamelException *ex) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + camel_folder_expunge (cache_folder->remote, ex); + if (!camel_exception_is_set (ex)) + camel_folder_expunge (cache_folder->local, NULL); +} + +/* DONE */ +static gint +get_message_count (CamelFolder *folder) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + return cache_folder->summary->len; +} + +/* DONE */ +static void +append_message (CamelFolder *folder, CamelMimeMessage *message, + guint32 flags, CamelException *ex) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + /* We'd like to cache this locally as well, but we have no + * 100% reliable way to determine the UID assigned to the + * remote message, so we can't. + */ + camel_folder_append_message (cache_folder->remote, message, flags, ex); +} + +/* DONE */ +static guint32 +get_message_flags (CamelFolder *folder, const char *uid) +{ + const CamelMessageInfo *mi; + + mi = get_message_info (folder, uid); + g_return_val_if_fail (mi != NULL, 0); + return mi->flags; +} + +/* DONE */ +static void +set_message_flags (CamelFolder *folder, const char *uid, + guint32 flags, guint32 set) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + const char *luid; + + luid = camel_cache_map_get_local (cache_folder->uidmap, uid); + if (luid) { + camel_folder_set_message_flags (cache_folder->local, luid, + flags, set); + } + camel_folder_set_message_flags (cache_folder->remote, uid, flags, set); +} + +/* DONE */ +static gboolean +get_message_user_flag (CamelFolder *folder, const char *uid, const char *name) +{ + const CamelMessageInfo *mi; + + mi = get_message_info (folder, uid); + g_return_val_if_fail (mi != NULL, 0); + return camel_flag_get ((CamelFlag **)&mi->user_flags, name); +} + +/* DONE */ +static void +set_message_user_flag (CamelFolder *folder, const char *uid, + const char *name, gboolean value) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + const char *luid; + + luid = camel_cache_map_get_local (cache_folder->uidmap, uid); + if (luid) { + camel_folder_set_message_user_flag (cache_folder->local, luid, + name, value); + } + camel_folder_set_message_user_flag (cache_folder->remote, uid, + name, value); +} + + +/* DONE */ +static CamelMimeMessage * +get_message (CamelFolder *folder, const gchar *uid, CamelException *ex) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + CamelMimeMessage *msg; + guint32 flags; + const char *luid; + + /* Check if we have it cached first. */ + luid = camel_cache_map_get_local (cache_folder->uidmap, uid); + if (luid) { + msg = camel_folder_get_message (cache_folder->local, + luid, NULL); + if (msg) + return msg; + + /* Hm... Oh well. Update the map and try for real. */ + camel_cache_map_remove (cache_folder->uidmap, NULL, uid); + } + + /* OK. It's not cached. Get the remote message. */ + msg = camel_folder_get_message (cache_folder->remote, uid, ex); + if (!msg) + return NULL; + flags = camel_folder_get_message_flags (cache_folder->remote, uid); + + /* Add a header giving the remote UID and append it to the + * local folder. (This should eventually invoke + * local_folder_changed(), which will take care of updating + * the uidmaps.) + */ + camel_medium_add_header (CAMEL_MEDIUM (msg), "X-Evolution-Remote-UID", + uid); + camel_folder_append_message (cache_folder->local, msg, flags, NULL); + + return msg; +} + +/* DONE */ +static GPtrArray * +get_uids (CamelFolder *folder) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + return cache_folder->uids; +} + +/* DONE */ +static GPtrArray * +get_summary (CamelFolder *folder) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + return cache_folder->summary; +} + +/* DONE */ +static GPtrArray * +get_subfolder_names (CamelFolder *folder) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + return camel_folder_get_subfolder_names (cache_folder->remote); +} + +/* DONE */ +static void +free_subfolder_names (CamelFolder *folder, GPtrArray *subfolders) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + camel_folder_free_subfolder_names (cache_folder->remote, subfolders); +} + +/* DONE */ +static GPtrArray * +search_by_expression (CamelFolder *folder, const char *expression, + CamelException *ex) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + /* Search on the remote folder if we're not synced. */ + if (!cache_folder->is_synced && + camel_folder_has_search_capability (cache_folder->remote)) { + return camel_folder_search_by_expression (cache_folder->remote, + expression, ex); + } else { + GPtrArray *matches; + const char *ruid; + int i; + + if (!cache_folder->is_synced) + camel_cache_folder_sync (cache_folder, ex); + if (camel_exception_is_set (ex)) + return NULL; + matches = search_by_expression (cache_folder->local, + expression, ex); + if (camel_exception_is_set (ex)) + return NULL; + + /* Convert local uids to remote. */ + for (i = 0; i < matches->len; i++) { + ruid = camel_cache_map_get_remote (cache_folder->uidmap, + matches->pdata[i]); + g_free (matches->pdata[i]); + matches->pdata[i] = g_strdup (ruid); + } + + return matches; + } +} + +/* DONE */ +static const CamelMessageInfo * +get_message_info (CamelFolder *folder, const char *uid) +{ + CamelCacheFolder *cache_folder = (CamelCacheFolder *)folder; + + if (cache_folder->remote_summary) { + return camel_folder_get_message_info (cache_folder->remote, + uid); + } else + return g_hash_table_lookup (cache_folder->summary_uids, uid); +} + + +CamelFolder * +camel_cache_folder_new (CamelStore *store, CamelFolder *parent, + CamelFolder *remote, CamelFolder *local) +{ + CamelCacheFolder *cache_folder; + CamelFolder *folder; + + cache_folder = gtk_type_new (CAMEL_CACHE_FOLDER_TYPE); + folder = (CamelFolder *)cache_folder; + + cache_folder->local = local; + gtk_object_ref (GTK_OBJECT (local)); + gtk_signal_connect (GTK_OBJECT (local), "folder_changed", + GTK_SIGNAL_FUNC (local_folder_changed), + cache_folder); + + cache_folder->remote = remote; + gtk_object_ref (GTK_OBJECT (remote)); + gtk_signal_connect (GTK_OBJECT (remote), "folder_changed", + GTK_SIGNAL_FUNC (remote_folder_changed), + cache_folder); + + /* XXX */ + + return folder; +} + +void +camel_cache_folder_sync (CamelCacheFolder *cache_folder, CamelException *ex) +{ + CamelMimeMessage *msg; + const char *ruid, *luid; + int lsize, i; + guint32 flags; + + lsize = camel_folder_get_message_count (cache_folder->local); + + camel_folder_freeze (cache_folder->local); + for (i = 0; i < cache_folder->uids->len; i++) { + ruid = cache_folder->uids->pdata[i]; + luid = camel_cache_map_get_local (cache_folder->uidmap, ruid); + + /* Don't re-copy messages we already have. */ + if (luid && + camel_folder_get_message_info (cache_folder->local, luid)) + continue; + + msg = camel_folder_get_message (cache_folder->remote, + ruid, ex); + if (camel_exception_is_set (ex)) + return; + flags = camel_folder_get_message_flags (cache_folder->remote, + ruid); + + camel_medium_add_header (CAMEL_MEDIUM (msg), + "X-Evolution-Remote-UID", ruid); + camel_folder_append_message (cache_folder->local, msg, + flags, ex); + if (camel_exception_is_set (ex)) + return; + } + camel_folder_thaw (cache_folder->local); +} + +static void +get_mappings (CamelCacheFolder *cache_folder, int first, CamelException *ex) +{ + GPtrArray *uids; + CamelMimeMessage *msg; + const char *ruid; + int i; + + uids = camel_folder_get_uids (cache_folder->local); + for (i = first; i < uids->len; i++) { + msg = camel_folder_get_message (cache_folder->local, + uids->pdata[i], ex); + if (!msg) + break; + ruid = camel_medium_get_header (CAMEL_MEDIUM (msg), + "X-Evolution-Remote-UID"); + + camel_cache_map_add (cache_folder->uidmap, + uids->pdata[i], ruid); + } + camel_folder_free_uids (cache_folder->local, uids); +} diff --git a/camel/providers/cache/camel-cache-folder.h b/camel/providers/cache/camel-cache-folder.h new file mode 100644 index 0000000000..4f30c701b2 --- /dev/null +++ b/camel/providers/cache/camel-cache-folder.h @@ -0,0 +1,87 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-cache-folder.h: Class for a cache folder */ + +/* + * Author: + * Dan Winship <danw@helixcode.com> + * + * Copyright (C) 2000 Helix Code, Inc. (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_CACHE_FOLDER_H +#define CAMEL_CACHE_FOLDER_H 1 + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus }*/ + +#include <camel/camel-folder.h> +#include "camel-cache-map.h" + +#define CAMEL_CACHE_FOLDER_TYPE (camel_cache_folder_get_type ()) +#define CAMEL_CACHE_FOLDER(obj) (GTK_CHECK_CAST((obj), CAMEL_CACHE_FOLDER_TYPE, CamelCacheFolder)) +#define CAMEL_CACHE_FOLDER_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), CAMEL_CACHE_FOLDER_TYPE, CamelCacheFolderClass)) +#define IS_CAMEL_CACHE_FOLDER(o) (GTK_CHECK_TYPE((o), CAMEL_CACHE_FOLDER_TYPE)) + + +typedef struct { + CamelFolder parent_object; + + /* Remote and local folders */ + CamelFolder *remote, *local; + + /* Remote UIDs, in order, summary, and uid->info map if + * summary is from local info. + */ + GPtrArray *uids, *summary; + GHashTable *summary_uids; + + /* UID map */ + CamelCacheMap *uidmap; + char *mapfile; + + /* Is the summary remote? Is the folder known to be synced? */ + gboolean remote_summary, is_synced; + +} CamelCacheFolder; + + + +typedef struct { + CamelFolderClass parent_class; + + /* Virtual methods */ + +} CamelCacheFolderClass; + + +CamelFolder *camel_cache_folder_new (CamelStore *store, CamelFolder *parent, + CamelFolder *remote, CamelFolder *local); + +void camel_cache_folder_sync (CamelCacheFolder *cache_folder, + CamelException *ex); + +/* Standard Gtk function */ +GtkType camel_cache_folder_get_type (void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CAMEL_CACHE_FOLDER_H */ diff --git a/camel/providers/cache/camel-cache-map.c b/camel/providers/cache/camel-cache-map.c new file mode 100644 index 0000000000..d3cece6f60 --- /dev/null +++ b/camel/providers/cache/camel-cache-map.c @@ -0,0 +1,255 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-cache-map.c : functions for a local<->remote uid map */ + +/* + * Authors: + * Dan Winship <danw@helixcode.com> + * + * Copyright (C) 2000 Helix Code, Inc. (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 "camel-cache-map.h" +#include <camel/camel-exception.h> + +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +/** + * camel_cache_map_new: + * + * Return value: a new CamelCacheMap + **/ +CamelCacheMap * +camel_cache_map_new (void) +{ + CamelCacheMap *map = g_new (CamelCacheMap, 1); + + map->l2r = g_hash_table_new (g_str_hash, g_str_equal); + map->r2l = g_hash_table_new (g_str_hash, g_str_equal); + + return map; +} + +static void +free_mapping (gpointer key, gpointer value, gpointer data) +{ + g_free (key); + g_free (data); +} + +/** + * camel_cache_map_destroy: + * @map: a CamelCacheMap + * + * Frees @map and all of the data stored in it. + **/ +void +camel_cache_map_destroy (CamelCacheMap *map) +{ + g_hash_table_foreach (map->l2r, free_mapping, NULL); + g_hash_table_destroy (map->l2r); + g_hash_table_destroy (map->r2l); + g_free (map); +} + +/** + * camel_cache_map_add: + * @map: a CamelCacheMap + * @luid: the local uid + * @ruid: the remote uid + * + * Adds a mapping between @luid and @ruid. If either already exists + * in the map, this may leak memory and result in incorrect map entries. + * Use camel_cache_map_update() in that case instead. + **/ +void +camel_cache_map_add (CamelCacheMap *map, const char *luid, const char *ruid) +{ + char *map_luid = g_strdup (luid); + char *map_ruid = g_strdup (ruid); + + g_hash_table_insert (map->l2r, map_luid, map_ruid); + g_hash_table_insert (map->r2l, map_ruid, map_luid); +} + +/** + * camel_cache_map_remove: + * @map: a CamelCacheMap + * @luid: the local uid + * @ruid: the remote uid + * + * Removes the mapping between @luid and @ruid. Either (but not both) + * of the uids can be %NULL if they are not both known. + **/ +void +camel_cache_map_remove (CamelCacheMap *map, const char *luid, const char *ruid) +{ + gpointer map_luid, map_ruid; + + if ((luid && g_hash_table_lookup_extended (map->l2r, luid, + &map_luid, &map_ruid)) || + (ruid && g_hash_table_lookup_extended (map->r2l, ruid, + &map_luid, &map_ruid))) { + g_hash_table_remove (map->l2r, map_luid); + g_hash_table_remove (map->r2l, map_ruid); + g_free (map_luid); + g_free (map_ruid); + } +} + +/** + * camel_cache_map_update: + * @map: a CamelCacheMap + * @luid: the local uid + * @ruid: the remote uid + * + * Updates the mappings to associate @luid with @ruid, clearing any + * previous mappings for both of them. + **/ +void +camel_cache_map_update (CamelCacheMap *map, const char *luid, const char *ruid) +{ + camel_cache_map_remove (map, luid, ruid); + camel_cache_map_add (map, luid, ruid); +} + +/** + * camel_cache_map_get_local + * @map: a CamelCacheMap + * @ruid: the remote uid + * + * Return value: the corresponding local uid, or %NULL + **/ +const char * +camel_cache_map_get_local (CamelCacheMap *map, const char *ruid) +{ + return g_hash_table_lookup (map->r2l, ruid); +} + +/** + * camel_cache_map_get_remote + * @map: a CamelCacheMap + * @luid: the local uid + * + * Return value: the corresponding remote uid, or %NULL + **/ +const char * +camel_cache_map_get_remote (CamelCacheMap *map, const char *luid) +{ + return g_hash_table_lookup (map->l2r, luid); +} + + + +static void +write_mapping (gpointer key, gpointer value, gpointer user_data) +{ + int fd = *(int *)user_data; + + /* FIXME: We assume the local UID has no ':'s in it. */ + write (fd, key, strlen (key)); + write (fd, ":", 1); + write (fd, value, strlen (value)); + write (fd, "\n", 1); +} + +/** + * camel_cache_map_write: + * @map: a CamelCacheMap + * @file: the filename to write the map to + * @ex: a CamelException + * + * Writes @map out to @file, setting @ex if something goes wrong. + **/ +void +camel_cache_map_write (CamelCacheMap *map, const char *file, + CamelException *ex) +{ + int fd; + char *tmpfile; + + tmpfile = g_strdup_printf ("%s~", file); + fd = open (tmpfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); + if (fd == -1) { + g_free (tmpfile); + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + "Could not create cache map file: %s", + g_strerror (errno)); + return; + } + + g_hash_table_foreach (map->l2r, write_mapping, &fd); + + if (close (fd) == -1 || + rename (tmpfile, file) == -1) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + "Could not save cache map file: %s", + g_strerror (errno)); + unlink (tmpfile); + } + g_free (tmpfile); +} + +/** + * camel_cache_map_read: + * @map: a CamelCacheMap + * @file: the filename to read the map from + * @ex: a CamelException + * + * Reads @map from @file, setting @ex if something goes wrong. @map + * should be a freshly-created CamelCacheMap. + **/ +void +camel_cache_map_read (CamelCacheMap *map, const char *file, CamelException *ex) +{ + FILE *f; + char buf[1024], *p, *q; + + /* FIXME: lazy implementation. We could make this work with + * lines longer than 1024 chars. :) + */ + + f = fopen (file, "r"); + if (!f) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + "Could not open cache map file: %s", + g_strerror (errno)); + return; + } + + while (fgets (buf, sizeof (buf), f)) { + p = strchr (buf, ':'); + if (p) + q = strchr (buf, '\n'); + if (!p || !q) { + camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, + "Bad cache file."); + return; + } + *p++ = *q = '\0'; + + /* Local uid at buf, remote at p. */ + camel_cache_map_add (map, buf, p); + } + + fclose (f); +} diff --git a/camel/providers/cache/camel-cache-map.h b/camel/providers/cache/camel-cache-map.h new file mode 100644 index 0000000000..a15c9afe1a --- /dev/null +++ b/camel/providers/cache/camel-cache-map.h @@ -0,0 +1,64 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-cache-map.h: functions for dealing with UID maps */ + +/* + * Author: + * Dan Winship <danw@helixcode.com> + * + * Copyright (C) 2000 Helix Code, Inc. (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_CACHE_MAP_H +#define CAMEL_CACHE_MAP_H 1 + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus }*/ + +#include <glib.h> +#include <camel/camel-types.h> + +typedef struct { + GHashTable *l2r, *r2l; +} CamelCacheMap; + +CamelCacheMap *camel_cache_map_new (void); +void camel_cache_map_destroy (CamelCacheMap *map); + +void camel_cache_map_add (CamelCacheMap *map, const char *luid, + const char *ruid); +void camel_cache_map_remove (CamelCacheMap *map, const char *luid, + const char *ruid); +void camel_cache_map_update (CamelCacheMap *map, const char *luid, + const char *ruid); + +const char *camel_cache_map_get_local (CamelCacheMap *map, const char *ruid); +const char *camel_cache_map_get_remote (CamelCacheMap *map, const char *luid); + +void camel_cache_map_write (CamelCacheMap *map, const char *file, + CamelException *ex); +void camel_cache_map_read (CamelCacheMap *map, const char *file, + CamelException *ex); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CAMEL_CACHE_MAP_H */ diff --git a/camel/providers/cache/camel-cache-provider.c b/camel/providers/cache/camel-cache-provider.c new file mode 100644 index 0000000000..2d17a62df4 --- /dev/null +++ b/camel/providers/cache/camel-cache-provider.c @@ -0,0 +1,51 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-cache-provider.c: cache provider registration code */ + +/* + * Authors : + * Dan Winship <danw@helixcode.com> + * + * Copyright (C) 2000 Helix Code, Inc. (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-cache-store.h" +#include "camel-provider.h" +#include "camel-session.h" + +static CamelProvider cache_provider = { + "cache", + "Cache", + + "For caching remote mail into a local store." + + "cache", + + 0, + + { 0, 0 } +}; + +void +camel_provider_module_init (CamelSession *session) +{ + cache_provider.object_types[CAMEL_PROVIDER_STORE] = + camel_cache_store_get_type(); + + camel_session_register_provider (session, &cache_provider); +} diff --git a/camel/providers/cache/camel-cache-store.c b/camel/providers/cache/camel-cache-store.c new file mode 100644 index 0000000000..ce34a0e716 --- /dev/null +++ b/camel/providers/cache/camel-cache-store.c @@ -0,0 +1,222 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-cache-store.c : class for a cache store */ + +/* + * Authors: + * Dan Winship <danw@helixcode.com> + * + * Copyright (C) 2000 Helix Code, Inc. (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 <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> + +#include "camel-cache-store.h" +#include "camel-cache-folder.h" +#include "camel-stream-buffer.h" +#include "camel-stream-fs.h" +#include "camel-session.h" +#include "camel-exception.h" +#include "camel-url.h" +#include "md5-utils.h" + +static CamelServiceClass *service_class = NULL; + +static void finalize (GtkObject *object); + +static gboolean cache_connect (CamelService *service, CamelException *ex); +static gboolean cache_disconnect (CamelService *service, CamelException *ex); + +static CamelFolder *get_folder (CamelStore *store, const char *folder_name, + gboolean create, CamelException *ex); +static CamelFolder *delete_folder (CamelStore *store, const char *folder_name, + CamelException *ex); +static char *get_folder_name (CamelStore *store, const char *folder_name, + CamelException *ex); +static char *get_root_folder_name (CamelStore *store, CamelException *ex); +static char *get_default_folder_name (CamelStore *store, CamelException *ex); + + +static void +camel_cache_store_class_init (CamelCacheStoreClass *camel_cache_store_class) +{ + GtkObjectClass *object_class = + GTK_OBJECT_CLASS (camel_cache_store_class); + CamelServiceClass *camel_service_class = + CAMEL_SERVICE_CLASS (camel_cache_store_class); + CamelStoreClass *camel_store_class = + CAMEL_STORE_CLASS (camel_cache_store_class); + + service_class = gtk_type_class (camel_service_get_type ()); + + /* virtual method overload */ + object_class->finalize = finalize; + + camel_service_class->connect = cache_connect; + camel_service_class->disconnect = cache_disconnect; + + camel_store_class->get_folder = get_folder; + camel_store_class->delete_folder = delete_folder; + camel_store_class->get_folder_name = get_folder_name; + camel_store_class->get_root_folder_name = get_root_folder_name; + camel_store_class->get_default_folder_name = get_default_folder_name; +} + + +static void +camel_cache_store_init (gpointer object, gpointer klass) +{ + CamelCacheStore *cache_store = CAMEL_CACHE_STORE (object); + CamelService *service = CAMEL_SERVICE (object); + + service->url_flags = CAMEL_SERVICE_URL_NEED_PATH; +} + + +GtkType +camel_cache_store_get_type (void) +{ + static GtkType camel_cache_store_type = 0; + + if (!camel_cache_store_type) { + GtkTypeInfo camel_cache_store_info = + { + "CamelCacheStore", + sizeof (CamelCacheStore), + sizeof (CamelCacheStoreClass), + (GtkClassInitFunc) camel_cache_store_class_init, + (GtkObjectInitFunc) camel_cache_store_init, + /* reserved_1 */ NULL, + /* reserved_2 */ NULL, + (GtkClassInitFunc) NULL, + }; + + camel_cache_store_type = gtk_type_unique (CAMEL_STORE_TYPE, &camel_cache_store_info); + } + + return camel_cache_store_type; +} + +static void +finalize (GtkObject *object) +{ + CamelCacheStore *cache_store = CAMEL_CACHE_STORE (object); + + gtk_object_unref (GTK_OBJECT (cache_store->local)); + gtk_object_unref (GTK_OBJECT (cache_store->remote)); +} + + +static gboolean +cache_connect (CamelService *service, CamelException *ex) +{ + CamelCacheStore *cache_store = CAMEL_CACHE_STORE (service); + + if (!cache_store->remote) { + cache_store->remote = + camel_session_get_store (service->session, + service->url->path, + ex); + if (camel_exception_is_set (ex)) + return FALSE; + + cache_store->local = XXX; + if (camel_exception_is_set (ex)) + return FALSE; + } + + if (!camel_service_connect (CAMEL_SERVICE (cache_store->remote), ex)) + return FALSE; + if (!camel_service_connect (CAMEL_SERVICE (cache_store->local), ex)) { + camel_service_disconnect (CAMEL_SERVICE (store->remote), NULL); + return FALSE; + } + + return service_class->connect (service, ex); +} + +static gboolean +cache_disconnect (CamelService *service, CamelException *ex) +{ + CamelCacheStore *store = CAMEL_CACHE_STORE (service); + + if (!service_class->disconnect (service, ex)) + return FALSE; + + return camel_service_disconnect (CAMEL_SERVICE (store->local), ex) && + camel_service_disconnect (CAMEL_SERVICE (store->remote), ex); +} + +static CamelFolder * +get_folder (CamelStore *store, const char *folder_name, + gboolean create, CamelException *ex) +{ + CamelCacheStore *cache_store = CAMEL_CACHE_STORE (store); + CamelFolder *rf, *lf; + + rf = camel_store_get_folder (cache_store->remote, folder_name, + create, ex); + if (!rf) + return NULL; + + lf = camel_store_get_folder (cache_store->local, folder_name, + TRUE, ex); + if (!lf) { + camel_exception_setv (ex, camel_exception_get_id (ex), + "Could not create cache folder:\n%s", + camel_exception_get_description (ex)); + return NULL; + } + + return camel_cache_folder_new (cache_store, rf, lf, ex); +} + +static char * +get_folder_name (CamelStore *store, const char *folder_name, + CamelException *ex) +{ + CamelCacheStore *cache_store = CAMEL_CACHE_STORE (store); + + return camel_store_get_folder_name (cache_store->remote, + folder_name, ex); +} + +static char * +get_root_folder_name (CamelStore *store, CamelException *ex) +{ + CamelCacheStore *cache_store = CAMEL_CACHE_STORE (store); + + return camel_store_get_root_folder_name (cache_store->remote, ex); +} + +static char * +get_default_folder_name (CamelStore *store, CamelException *ex) +{ + CamelCacheStore *cache_store = CAMEL_CACHE_STORE (store); + + return camel_store_get_default_folder_name (cache_store->remote, ex); +} diff --git a/camel/providers/cache/camel-cache-store.h b/camel/providers/cache/camel-cache-store.h new file mode 100644 index 0000000000..d85f759640 --- /dev/null +++ b/camel/providers/cache/camel-cache-store.h @@ -0,0 +1,67 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* camel-cache-store.h: class for a cache store */ + +/* + * Authors: + * Dan Winship <danw@helixcode.com> + * + * Copyright (C) 2000 Helix Code, Inc. (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_CACHE_STORE_H +#define CAMEL_CACHE_STORE_H 1 + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus }*/ + +#include <camel/camel-store.h> + +#define CAMEL_CACHE_STORE_TYPE (camel_cache_store_get_type ()) +#define CAMEL_CACHE_STORE(obj) (GTK_CHECK_CAST((obj), CAMEL_CACHE_STORE_TYPE, CamelCacheStore)) +#define CAMEL_CACHE_STORE_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), CAMEL_CACHE_STORE_TYPE, CamelCacheStoreClass)) +#define IS_CAMEL_CACHE_STORE(o) (GTK_CHECK_TYPE((o), CAMEL_CACHE_STORE_TYPE)) + +typedef struct { + CamelStore parent_object; + + CamelStore *remote, *local; + +} CamelCacheStore; + + +typedef struct { + CamelStoreClass parent_class; + +} CamelCacheStoreClass; + + +/* support functions */ +void camel_cache_store_sync (CamelCacheStore *store); + +/* Standard Gtk function */ +GtkType camel_cache_store_get_type (void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CAMEL_CACHE_STORE_H */ + + diff --git a/camel/providers/cache/libcamelcache.urls b/camel/providers/cache/libcamelcache.urls new file mode 100644 index 0000000000..06cf65390f --- /dev/null +++ b/camel/providers/cache/libcamelcache.urls @@ -0,0 +1 @@ +cache |