diff options
author | Not Zed <NotZed@Ximian.com> | 2002-06-06 17:53:36 +0800 |
---|---|---|
committer | Michael Zucci <zucchi@src.gnome.org> | 2002-06-06 17:53:36 +0800 |
commit | 1f993cacd1ac269ea4e7a77c57edbce732b9e8b5 (patch) | |
tree | b5e1562300b44d813a50320e80eff96e63559a41 /camel | |
parent | a58c048d65965fd3b543f06dd1c4e2928c60ed68 (diff) | |
download | gsoc2013-evolution-1f993cacd1ac269ea4e7a77c57edbce732b9e8b5.tar.gz gsoc2013-evolution-1f993cacd1ac269ea4e7a77c57edbce732b9e8b5.tar.zst gsoc2013-evolution-1f993cacd1ac269ea4e7a77c57edbce732b9e8b5.zip |
Remove the CONF_DEFAULT_PATH entry, as this is handled by the url config
2002-06-04 Not Zed <NotZed@Ximian.com>
* providers/local/camel-local-provider.c: Remove the
CONF_DEFAULT_PATH entry, as this is handled by the url config
stuff. This made it basically impossible to configure any local
folder from the gui.
* providers/local/camel-local-folder.c (local_refresh_info): Force
a refresh. Should work for all local folders to force them to
refresh.
* providers/local/camel-maildir-folder.c (maildir_refresh_info):
Removed, moved into camel-local-folder.
* providers/local/camel-mh-summary.c (mh_summary_next_uid_string):
Once we assign or get a uid, tell the summary of this, so the next
uid we get will be higher than any previously.
* camel-object.c (camel_object_ref, camel_object_unref): I got
sick of casting, these now take void * like they should, and
perform their own run-time type checking.
* providers/local/camel-mh-store.c (recursive_scan): Changed to
use stat, not lstat (*shrug* maybe someone wants to softlink their
maildir tree?).
(inode_hash):
(inode_equal):
(inode_free): Copied from camel-maildir store (should put into
camel-local-store or utils?).
(recursive_scan): Changed to check for re-visiting inodes. Also,
it builds the tree itself, rather than using folder_info_build.
(add_folder): Changed to folder_info_new.
(recursive_scan): Properly honour the recursive flag. Also,
lookup unread count from folder.
(folder_info_new): Init unread message count to -1, since we dont
know yet.
(folder_info_new): Take the name as an argument, and perform the
merging here.
(folders_update): Util func to add/remove folders from .folders
file. I'm assuming its sorted.
(get_folder): Add the folder to .folders if we created a new one,
and if it exists.
(delete_folder): Remove from .folders, etc.
(folders_scan): If we have a .folders file, read and use that
instead.
(recursive_scan): Handle scanning from a particular directory
properly.
(rename_folder): Implement so we can track any changes to the
.folders file if its turned on.
** Applied patch below from Greg Hudson.
2002-05-10 Greg Hudson <ghudson@mit.edu>
* camel-mh-store.c (get_inbox, get_folder_info, recursive_scan,
add_folder): Implement support for MH stores.
* camel-mh-summary.c (mh_summary_check, sort_uid_cmp): Sort MH
messages by message number (uid), like we sort maildir messages by
date.
* camel-local-provider.c (mh_provider): Turn on source and store
flags.
svn path=/trunk/; revision=17130
Diffstat (limited to 'camel')
-rw-r--r-- | camel/ChangeLog | 64 | ||||
-rw-r--r-- | camel/camel-object.c | 15 | ||||
-rw-r--r-- | camel/camel-object.h | 4 | ||||
-rw-r--r-- | camel/camel-stream-buffer.c | 1 | ||||
-rw-r--r-- | camel/providers/local/camel-local-folder.c | 18 | ||||
-rw-r--r-- | camel/providers/local/camel-local-provider.c | 6 | ||||
-rw-r--r-- | camel/providers/local/camel-maildir-folder.c | 16 | ||||
-rw-r--r-- | camel/providers/local/camel-mh-store.c | 401 | ||||
-rw-r--r-- | camel/providers/local/camel-mh-store.h | 7 | ||||
-rw-r--r-- | camel/providers/local/camel-mh-summary.c | 54 |
10 files changed, 548 insertions, 38 deletions
diff --git a/camel/ChangeLog b/camel/ChangeLog index dded90b650..e4cc73ac72 100644 --- a/camel/ChangeLog +++ b/camel/ChangeLog @@ -1,3 +1,67 @@ +2002-06-04 Not Zed <NotZed@Ximian.com> + + * providers/local/camel-local-provider.c: Remove the + CONF_DEFAULT_PATH entry, as this is handled by the url config + stuff. This made it basically impossible to configure any local + folder from the gui. + + * providers/local/camel-local-folder.c (local_refresh_info): Force + a refresh. Should work for all local folders to force them to + refresh. + + * providers/local/camel-maildir-folder.c (maildir_refresh_info): + Removed, moved into camel-local-folder. + + * providers/local/camel-mh-summary.c (mh_summary_next_uid_string): + Once we assign or get a uid, tell the summary of this, so the next + uid we get will be higher than any previously. + + * camel-object.c (camel_object_ref, camel_object_unref): I got + sick of casting, these now take void * like they should, and + perform their own run-time type checking. + + * providers/local/camel-mh-store.c (recursive_scan): Changed to + use stat, not lstat (*shrug* maybe someone wants to softlink their + maildir tree?). + (inode_hash): + (inode_equal): + (inode_free): Copied from camel-maildir store (should put into + camel-local-store or utils?). + (recursive_scan): Changed to check for re-visiting inodes. Also, + it builds the tree itself, rather than using folder_info_build. + (add_folder): Changed to folder_info_new. + (recursive_scan): Properly honour the recursive flag. Also, + lookup unread count from folder. + (folder_info_new): Init unread message count to -1, since we dont + know yet. + (folder_info_new): Take the name as an argument, and perform the + merging here. + (folders_update): Util func to add/remove folders from .folders + file. I'm assuming its sorted. + (get_folder): Add the folder to .folders if we created a new one, + and if it exists. + (delete_folder): Remove from .folders, etc. + (folders_scan): If we have a .folders file, read and use that + instead. + (recursive_scan): Handle scanning from a particular directory + properly. + (rename_folder): Implement so we can track any changes to the + .folders file if its turned on. + + ** Applied patch below from Greg Hudson. + +2002-05-10 Greg Hudson <ghudson@mit.edu> + + * camel-mh-store.c (get_inbox, get_folder_info, recursive_scan, + add_folder): Implement support for MH stores. + + * camel-mh-summary.c (mh_summary_check, sort_uid_cmp): Sort MH + messages by message number (uid), like we sort maildir messages by + date. + + * camel-local-provider.c (mh_provider): Turn on source and store + flags. + 2002-06-03 Not Zed <NotZed@Ximian.com> * camel-vee-folder.c (camel_vee_folder_add_folder): diff --git a/camel/camel-object.c b/camel/camel-object.c index eebcbf35d2..5e7eac7504 100644 --- a/camel/camel-object.c +++ b/camel/camel-object.c @@ -409,18 +409,27 @@ camel_object_new(CamelType type) } void -camel_object_ref(CamelObject *o) +camel_object_ref(void *vo) { + register CamelObject *o = vo; + + g_return_if_fail(CAMEL_IS_OBJECT(o)); + CLASS_LOCK(o->klass); o->ref_count++; CLASS_UNLOCK(o->klass); } void -camel_object_unref(CamelObject *o) +camel_object_unref(void *vo) { - register CamelObjectClass *klass = o->klass, *k; + register CamelObject *o = vo; + register CamelObjectClass *klass, *k; + + g_return_if_fail(CAMEL_IS_OBJECT(o)); + klass = o->klass; + CLASS_LOCK(klass); o->ref_count--; if (o->ref_count > 0 diff --git a/camel/camel-object.h b/camel/camel-object.h index fddc778dd3..d6b3a64575 100644 --- a/camel/camel-object.h +++ b/camel/camel-object.h @@ -192,8 +192,8 @@ CamelType camel_object_get_type (void); CamelObject *camel_object_new (CamelType type); CamelObject *camel_object_new_name (const char *name); -void camel_object_ref (CamelObject *obj); -void camel_object_unref (CamelObject *obj); +void camel_object_ref(void *); +void camel_object_unref(void *); #ifdef CAMEL_DEBUG #define camel_object_ref(o) (printf("%s (%s:%d):ref (%p)\n", __FUNCTION__, __FILE__, __LINE__, o), camel_object_ref(o)) diff --git a/camel/camel-stream-buffer.c b/camel/camel-stream-buffer.c index fe3458a93f..e8b0526873 100644 --- a/camel/camel-stream-buffer.c +++ b/camel/camel-stream-buffer.c @@ -170,6 +170,7 @@ CamelStream * camel_stream_buffer_new (CamelStream *stream, CamelStreamBufferMode mode) { CamelStreamBuffer *sbf; + sbf = CAMEL_STREAM_BUFFER (camel_object_new (camel_stream_buffer_get_type ())); CAMEL_STREAM_BUFFER_CLASS (CAMEL_OBJECT_GET_CLASS(sbf))->init (sbf, stream, mode); diff --git a/camel/providers/local/camel-local-folder.c b/camel/providers/local/camel-local-folder.c index c12e9d5da7..e79e6b8f5d 100644 --- a/camel/providers/local/camel-local-folder.c +++ b/camel/providers/local/camel-local-folder.c @@ -70,6 +70,8 @@ static int local_getv(CamelObject *object, CamelException *ex, CamelArgGetV *arg static int local_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex); static void local_unlock(CamelLocalFolder *lf); +static void local_refresh_info(CamelFolder *folder, CamelException *ex); + static void local_sync(CamelFolder *folder, gboolean expunge, CamelException *ex); static void local_expunge(CamelFolder *folder, CamelException *ex); @@ -94,6 +96,7 @@ camel_local_folder_class_init(CamelLocalFolderClass * camel_local_folder_class) /* virtual method overload */ oklass->getv = local_getv; + camel_folder_class->refresh_info = local_refresh_info; camel_folder_class->sync = local_sync; camel_folder_class->expunge = local_expunge; @@ -371,6 +374,21 @@ local_unlock(CamelLocalFolder *lf) /* nothing */ } +/* for auto-check to work */ +static void +local_refresh_info(CamelFolder *folder, CamelException *ex) +{ + CamelLocalFolder *lf = (CamelLocalFolder *)folder; + + if (camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) + return; + + if (camel_folder_change_info_changed(lf->changes)) { + camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes); + camel_folder_change_info_clear(lf->changes); + } +} + static void local_sync(CamelFolder *folder, gboolean expunge, CamelException *ex) { diff --git a/camel/providers/local/camel-local-provider.c b/camel/providers/local/camel-local-provider.c index bae68e6aa7..7bf9decfc7 100644 --- a/camel/providers/local/camel-local-provider.c +++ b/camel/providers/local/camel-local-provider.c @@ -40,6 +40,8 @@ static CamelProviderConfEntry mh_conf_entries[] = { CAMEL_PROVIDER_CONF_DEFAULT_PATH, + { CAMEL_PROVIDER_CONF_CHECKBOX, "dotfolders", NULL, + N_("Use the `.folders' folder summary file (exmh)"), "0" }, { CAMEL_PROVIDER_CONF_END } }; @@ -48,7 +50,7 @@ static CamelProvider mh_provider = { N_("MH-format mail directories"), N_("For storing local mail in MH-like mail directories."), "mail", - CAMEL_PROVIDER_IS_LOCAL, + CAMEL_PROVIDER_IS_SOURCE | CAMEL_PROVIDER_IS_STORAGE | CAMEL_PROVIDER_IS_LOCAL, CAMEL_URL_NEED_PATH | CAMEL_URL_PATH_IS_ABSOLUTE, mh_conf_entries, /* ... */ @@ -118,7 +120,7 @@ static CamelProvider spoold_provider = { "mail", CAMEL_PROVIDER_IS_SOURCE | CAMEL_PROVIDER_IS_STORAGE | CAMEL_PROVIDER_IS_LOCAL, CAMEL_URL_NEED_PATH | CAMEL_URL_PATH_IS_ABSOLUTE, - spoold_conf_entries, + NULL, /* ... */ }; diff --git a/camel/providers/local/camel-maildir-folder.c b/camel/providers/local/camel-maildir-folder.c index 3b1ba7ef86..05fdd70e10 100644 --- a/camel/providers/local/camel-maildir-folder.c +++ b/camel/providers/local/camel-maildir-folder.c @@ -54,7 +54,6 @@ static CamelLocalSummary *maildir_create_summary(const char *path, const char *f static void maildir_append_message(CamelFolder * folder, CamelMimeMessage * message, const CamelMessageInfo *info, char **appended_uid, CamelException * ex); static CamelMimeMessage *maildir_get_message(CamelFolder * folder, const gchar * uid, CamelException * ex); -static void maildir_refresh_info(CamelFolder *folder, CamelException *ex); static void maildir_finalize(CamelObject * object); @@ -68,7 +67,6 @@ static void camel_maildir_folder_class_init(CamelObjectClass * camel_maildir_fol /* virtual method definition */ /* virtual method overload */ - camel_folder_class->refresh_info = maildir_refresh_info; camel_folder_class->append_message = maildir_append_message; camel_folder_class->get_message = maildir_get_message; @@ -128,20 +126,6 @@ static CamelLocalSummary *maildir_create_summary(const char *path, const char *f } static void -maildir_refresh_info(CamelFolder *folder, CamelException *ex) -{ - CamelLocalFolder *lf = (CamelLocalFolder *)folder; - - if (camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) - return; - - if (camel_folder_change_info_changed(lf->changes)) { - camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes); - camel_folder_change_info_clear(lf->changes); - } -} - -static void maildir_append_message (CamelFolder *folder, CamelMimeMessage *message, const CamelMessageInfo *info, char **appended_uid, CamelException *ex) { CamelMaildirFolder *maildir_folder = (CamelMaildirFolder *)folder; diff --git a/camel/providers/local/camel-mh-store.c b/camel/providers/local/camel-mh-store.c index 8b8c2e5f0e..f653fdb408 100644 --- a/camel/providers/local/camel-mh-store.c +++ b/camel/providers/local/camel-mh-store.c @@ -27,32 +27,48 @@ #include <errno.h> #include <string.h> #include <unistd.h> +#include <dirent.h> #include "camel-mh-store.h" #include "camel-mh-folder.h" #include "camel-exception.h" #include "camel-url.h" +#include "camel-private.h" + +#include <camel/camel-stream-fs.h> +#include <camel/camel-stream-buffer.h> static CamelLocalStoreClass *parent_class = NULL; +#define d(x) + /* Returns the class for a CamelMhStore */ #define CMHS_CLASS(so) CAMEL_MH_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so)) #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so)) #define CMHF_CLASS(so) CAMEL_MH_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so)) +static void construct (CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex); static CamelFolder *get_folder(CamelStore * store, const char *folder_name, guint32 flags, CamelException * ex); +static CamelFolder *get_inbox (CamelStore *store, CamelException *ex); static void delete_folder(CamelStore * store, const char *folder_name, CamelException * ex); +static void rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex); +static CamelFolderInfo * get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex); static void camel_mh_store_class_init(CamelObjectClass * camel_mh_store_class) { CamelStoreClass *camel_store_class = CAMEL_STORE_CLASS(camel_mh_store_class); - /*CamelServiceClass *camel_service_class = CAMEL_SERVICE_CLASS(camel_mh_store_class);*/ + CamelServiceClass *camel_service_class = CAMEL_SERVICE_CLASS(camel_mh_store_class); parent_class = (CamelLocalStoreClass *)camel_type_get_global_classfuncs(camel_local_store_get_type()); /* virtual method overload, use defaults for most */ + camel_service_class->construct = construct; + camel_store_class->get_folder = get_folder; + camel_store_class->get_inbox = get_inbox; camel_store_class->delete_folder = delete_folder; + camel_store_class->rename_folder = rename_folder; + camel_store_class->get_folder_info = get_folder_info; } CamelType camel_mh_store_get_type(void) @@ -72,6 +88,103 @@ CamelType camel_mh_store_get_type(void) return camel_mh_store_type; } +static void +construct (CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex) +{ + CamelMhStore *mh_store = (CamelMhStore *)service; + + CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex); + if (camel_exception_is_set (ex)) + return; + + if (camel_url_get_param(url, "dotfolders")) + mh_store->flags |= CAMEL_MH_DOTFOLDERS; +} + +enum { + UPDATE_NONE, + UPDATE_ADD, + UPDATE_REMOVE, +}; + +/* update the .folders file if it exists, or create it if it doesn't */ +static void +folders_update(const char *root, const char *folder, int mode) +{ + char *tmp, *tmpnew, *line = NULL; + CamelStream *stream, *in = NULL, *out = NULL; + + tmpnew = alloca(strlen(root)+16); + sprintf(tmpnew, "%s.folders~", root); + + out = camel_stream_fs_new_with_name(tmpnew, O_WRONLY|O_CREAT|O_TRUNC, 0666); + if (out == NULL) + goto fail; + + tmp = alloca(strlen(root)+16); + sprintf(tmp, "%s.folders", root); + stream = camel_stream_fs_new_with_name(tmp, O_RDONLY, 0); + if (stream) { + in = camel_stream_buffer_new(stream, CAMEL_STREAM_BUFFER_READ); + camel_object_unref(stream); + } + if (in == NULL || stream == NULL) { + if (mode == UPDATE_ADD && camel_stream_printf(out, "%s\n", folder) == -1) + goto fail; + goto done; + } + + while ((line = camel_stream_buffer_read_line((CamelStreamBuffer *)in))) { + int copy = TRUE; + + switch (mode) { + case UPDATE_REMOVE: + if (strcmp(line, folder) == 0) + copy = FALSE; + break; + case UPDATE_ADD: { + int cmp = strcmp(line, folder); + + if (cmp > 0) { + /* found insertion point */ + if (camel_stream_printf(out, "%s\n", folder) == -1) + goto fail; + mode = UPDATE_NONE; + } else if (tmp == 0) { + /* already there */ + mode = UPDATE_NONE; + } + break; } + case UPDATE_NONE: + break; + } + + if (copy && camel_stream_printf(out, "%s\n", line) == -1) + goto fail; + + g_free(line); + line = NULL; + } + + /* add to end? */ + if (mode == UPDATE_ADD && camel_stream_printf(out, "%s\n", folder) == -1) + goto fail; + + if (camel_stream_close(out) == -1) + goto fail; + +done: + /* should we care if this fails? I suppose so ... */ + rename(tmpnew, tmp); +fail: + unlink(tmpnew); /* remove it if its there */ + g_free(line); + if (in) + camel_object_unref(in); + if (out) + camel_object_unref(out); +} + static CamelFolder *get_folder(CamelStore * store, const char *folder_name, guint32 flags, CamelException * ex) { char *name; @@ -97,6 +210,7 @@ static CamelFolder *get_folder(CamelStore * store, const char *folder_name, guin g_free (name); return NULL; } + if (mkdir(name, 0700) != 0) { camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, _("Could not create folder `%s':\n%s"), @@ -104,6 +218,12 @@ static CamelFolder *get_folder(CamelStore * store, const char *folder_name, guin g_free (name); return NULL; } + + /* add to .folders if we are supposed to */ + /* FIXME: throw exception on error */ + if (((CamelMhStore *)store)->flags & CAMEL_MH_DOTFOLDERS) + folders_update(((CamelLocalStore *)store)->toplevel_dir, folder_name, UPDATE_ADD); + } else if (!S_ISDIR(st.st_mode)) { camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER, _("`%s' is not a directory."), name); @@ -115,6 +235,12 @@ static CamelFolder *get_folder(CamelStore * store, const char *folder_name, guin return camel_mh_folder_new(store, folder_name, flags, ex); } +static CamelFolder * +get_inbox (CamelStore *store, CamelException *ex) +{ + return get_folder (store, "inbox", 0, ex); +} + static void delete_folder(CamelStore * store, const char *folder_name, CamelException * ex) { char *name; @@ -130,6 +256,279 @@ static void delete_folder(CamelStore * store, const char *folder_name, CamelExce } g_free(name); + /* remove from .folders if we are supposed to */ + if (((CamelMhStore *)store)->flags & CAMEL_MH_DOTFOLDERS) + folders_update(((CamelLocalStore *)store)->toplevel_dir, folder_name, UPDATE_REMOVE); + /* and remove metadata */ ((CamelStoreClass *)parent_class)->delete_folder(store, folder_name, ex); } + +static void +rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex) +{ + CamelException e; + + camel_exception_init(&e); + ((CamelStoreClass *)parent_class)->rename_folder(store, old, new, &e); + if (camel_exception_is_set(&e)) { + camel_exception_xfer(ex, &e); + return; + } + camel_exception_clear(&e); + + if (((CamelMhStore *)store)->flags & CAMEL_MH_DOTFOLDERS) { + /* yeah this is messy, but so is mh! */ + folders_update(((CamelLocalStore *)store)->toplevel_dir, new, UPDATE_ADD); + folders_update(((CamelLocalStore *)store)->toplevel_dir, old, UPDATE_REMOVE); + } +} + +static CamelFolderInfo *folder_info_new(CamelStore *store, const char *root, const char *path) +{ + CamelFolderInfo *fi; + char *base; + CamelFolder *folder; + + base = strrchr(path, '/'); + + /* Build the folder info structure. */ + fi = g_malloc0(sizeof(*fi)); + fi->url = g_strdup_printf("mh:%s#%s", root, path); + fi->full_name = g_strdup(path); + fi->name = g_strdup(base?base+1:path); + fi->unread_message_count = 0; + + /* check unread count if open */ + CAMEL_STORE_LOCK(store, cache_lock); + folder = g_hash_table_lookup(store->folders, path); + if (folder) { + if ((((CamelMhStore *)store)->flags & CAMEL_STORE_FOLDER_INFO_FAST) == 0) + camel_folder_refresh_info(folder, NULL); + fi->unread_message_count = camel_folder_get_unread_message_count(folder); + } + CAMEL_STORE_UNLOCK(store, cache_lock); + + /* We could: if we have no folder, and FAST isn't specified, perform a full + scan of all messages for their status flags. But its probably not worth + it as we need to read the top of every file, i.e. very very slow */ + + camel_folder_info_build_path(fi, '/'); + + d(printf("New folderinfo:\n '%s'\n '%s'\n '%s'\n", fi->full_name, fi->url, fi->path)); + + return fi; +} + +/* used to find out where we've visited already */ +struct _inode { + dev_t dnode; + ino_t inode; +}; + +/* Scan path, under root, for directories to add folders for. Both + * root and path should have a trailing "/" if they aren't empty. */ +static void recursive_scan(CamelStore *store, CamelFolderInfo **fip, CamelFolderInfo *parent, GHashTable *visited, const char *root, const char *path) +{ + char *fullpath, *tmp; + DIR *dp; + struct dirent *d; + struct stat st; + CamelFolderInfo *fi; + struct _inode in, *inew; + + /* Open the specified directory. */ + if (path[0]) { + fullpath = alloca(strlen(root)+strlen(path)+2); + sprintf(fullpath, "%s/%s", root, path); + } else + fullpath = (char *)root; + + if (stat(fullpath, &st) == -1 || !S_ISDIR(st.st_mode)) + return; + + in.dnode = st.st_dev; + in.inode = st.st_ino; + + /* see if we've visited already */ + if (g_hash_table_lookup(visited, &in) != NULL) + return; + + inew = g_malloc(sizeof(*inew)); + *inew = in; + g_hash_table_insert(visited, inew, inew); + + /* link in ... */ + fi = folder_info_new(store, root, path); + fi->parent = parent; + fi->sibling = *fip; + *fip = fi; + + if ((( ((CamelMhStore *)store)->flags & CAMEL_STORE_FOLDER_INFO_RECURSIVE) || parent == NULL)) { + /* now check content for possible other directories */ + dp = opendir(fullpath); + if (dp == NULL) + return; + + /* Look for subdirectories to add and scan. */ + while ((d = readdir(dp)) != NULL) { + /* Skip current and parent directory. */ + if (strcmp(d->d_name, ".") == 0 + || strcmp(d->d_name, "..") == 0) + continue; + + /* skip fully-numerical entries (i.e. mh messages) */ + strtoul(d->d_name, &tmp, 10); + if (*tmp == 0) + continue; + + /* otherwise, treat at potential node, and recurse, a bit more expensive than needed, but tough! */ + if (path[0]) { + tmp = g_strdup_printf("%s/%s", path, d->d_name); + recursive_scan(store, &fi->child, fi, visited, root, tmp); + g_free(tmp); + } else { + recursive_scan(store, &fi->child, fi, visited, root, d->d_name); + } + } + + closedir(dp); + } +} + +/* scan a .folders file */ +static void +folders_scan(CamelStore *store, const char *root, const char *top, CamelFolderInfo **fip) +{ + CamelFolderInfo *fi; + char line[512], *path, *tmp; + CamelStream *stream, *in; + struct stat st; + GPtrArray *folders; + GHashTable *visited; + int len; + + tmp = alloca(strlen(root)+16); + sprintf(tmp, "%s/.folders", root); + stream = camel_stream_fs_new_with_name(tmp, 0, O_RDONLY); + if (stream == NULL) + return; + + in = camel_stream_buffer_new(stream, CAMEL_STREAM_BUFFER_READ); + camel_object_unref(stream); + if (in == NULL) + return; + + visited = g_hash_table_new(g_str_hash, g_str_equal); + folders = g_ptr_array_new(); + + while ( (len = camel_stream_buffer_gets((CamelStreamBuffer *)in, line, sizeof(line))) > 0) { + /* ignore blank lines */ + if (len <= 1) + continue; + /* check for invalidly long lines, we abort evreything and fallback */ + if (line[len-1] != '\n') { + int i; + + for (i=0;i<folders->len;i++) + camel_folder_info_free(folders->pdata[i]); + g_ptr_array_set_size(folders, 0); + break; + } + line[len-1] = 0; + + /* check for \r ? */ + + if (top && top[0]) { + int toplen = strlen(top); + + /* check is subdir */ + if (strncmp(top, line, len) != 0) + continue; + + /* check is not sub-subdir if not recursive */ + if (( ((CamelMhStore *)store)->flags & CAMEL_STORE_FOLDER_INFO_RECURSIVE) == 0 + && (tmp = strrchr(line, '/')) + && tmp > line+toplen) + continue; + } + + if (g_hash_table_lookup(visited, line) != NULL) + continue; + + tmp = g_strdup(line); + g_hash_table_insert(visited, tmp, tmp); + + path = g_strdup_printf("%s/%s", root, line); + if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) { + fi = folder_info_new(store, root, line); + g_ptr_array_add(folders, fi); + } + g_free(path); + } + + if (folders->len) + *fip = camel_folder_info_build(folders, NULL, '/', TRUE); + g_ptr_array_free(folders, TRUE); + + g_hash_table_foreach(visited, (GHFunc)g_free, NULL); + g_hash_table_destroy(visited); + + camel_object_unref(in); +} + +/* FIXME: move to camel-local, this is shared with maildir code */ +static guint inode_hash(const void *d) +{ + const struct _inode *v = d; + + return v->inode ^ v->dnode; +} + +static gboolean inode_equal(const void *a, const void *b) +{ + const struct _inode *v1 = a, *v2 = b; + + return v1->inode == v2->inode && v1->dnode == v2->dnode; +} + +static void inode_free(void *k, void *v, void *d) +{ + g_free(k); +} + +static CamelFolderInfo * +get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex) +{ + CamelFolderInfo *fi = NULL; + char *root; + + root = ((CamelService *)store)->url->path; + + /* use .folders if we are supposed to */ + if (((CamelMhStore *)store)->flags & CAMEL_MH_DOTFOLDERS) { + folders_scan(store, root, top, &fi); + } else { + GHashTable *visited = g_hash_table_new(inode_hash, inode_equal); + + if (top == NULL) + top = ""; + + recursive_scan(store, &fi, NULL, visited, root, top); + + /* if we actually scanned from root, we have a "" root node we dont want */ + if (fi != NULL && top[0] == 0) { + CamelFolderInfo *rfi; + + rfi = fi; + fi = rfi->child; + rfi->child = NULL; + camel_folder_info_free(rfi); + } + + g_hash_table_foreach(visited, inode_free, NULL); + g_hash_table_destroy(visited); + } + + return fi; +} diff --git a/camel/providers/local/camel-mh-store.h b/camel/providers/local/camel-mh-store.h index b69bddb7c2..96522cb01f 100644 --- a/camel/providers/local/camel-mh-store.h +++ b/camel/providers/local/camel-mh-store.h @@ -34,9 +34,14 @@ extern "C" { #define CAMEL_MH_STORE_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), CAMEL_MH_STORE_TYPE, CamelMhStoreClass)) #define CAMEL_IS_MH_STORE(o) (CAMEL_CHECK_TYPE((o), CAMEL_MH_STORE_TYPE)) +enum { + CAMEL_MH_DOTFOLDERS = (1<<0), /* update/use .folders file */ +}; + typedef struct { CamelLocalStore parent_object; - + + guint32 flags; } CamelMhStore; typedef struct { diff --git a/camel/providers/local/camel-mh-summary.c b/camel/providers/local/camel-mh-summary.c index 530baa3caf..9eeeb25718 100644 --- a/camel/providers/local/camel-mh-summary.c +++ b/camel/providers/local/camel-mh-summary.c @@ -39,6 +39,8 @@ #include "camel-mh-summary.h" #include <camel/camel-mime-message.h> +#include "camel-private.h" + #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/ #define CAMEL_MH_SUMMARY_VERSION (0x2000) @@ -135,24 +137,30 @@ static char *mh_summary_next_uid_string(CamelFolderSummary *s) int fd = -1; guint32 uid; char *name; + char *uidstr; /* if we are working to add an existing file, then use current_uid */ - if (mhs->priv->current_uid) - return g_strdup(mhs->priv->current_uid); + if (mhs->priv->current_uid) { + uidstr = g_strdup(mhs->priv->current_uid); + /* tell the summary of this, so we always append numbers to the end */ + camel_folder_summary_set_uid(s, strtoul(uidstr, NULL, 10)+1); + } else { + /* else scan for one - and create it too, to make sure */ + do { + close(fd); + uid = camel_folder_summary_next_uid(s); + name = g_strdup_printf("%s/%u", cls->folder_path, uid); + /* O_EXCL isn't guaranteed, sigh. Oh well, bad luck, mh has problems anyway */ + fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0600); + g_free(name); + } while (fd == -1 && errno == EEXIST); - /* else scan for one - and create it too, to make sure */ - do { close(fd); - uid = camel_folder_summary_next_uid(s); - name = g_strdup_printf("%s/%u", cls->folder_path, uid); - /* O_EXCL isn't guaranteed, sigh. Oh well, bad luck, mh has problems anyway */ - fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0600); - g_free(name); - } while (fd == -1 && errno == EEXIST); - close(fd); + uidstr = g_strdup_printf("%u", uid); + } - return g_strdup_printf("%u", uid); + return uidstr; } static int camel_mh_summary_add(CamelLocalSummary *cls, const char *name, int forceindex) @@ -199,12 +207,27 @@ remove_summary(char *key, CamelMessageInfo *info, CamelLocalSummary *cls) } static int +sort_uid_cmp(const void *ap, const void *bp) +{ + const CamelMessageInfo + *a = *((CamelMessageInfo **)ap), + *b = *((CamelMessageInfo **)bp); + const char + *auid = camel_message_info_uid(a), + *buid = camel_message_info_uid(b); + int aval = atoi(auid), bval = atoi(buid); + + return (aval < bval) ? -1 : (aval > bval) ? 1 : 0; +} + +static int mh_summary_check(CamelLocalSummary *cls, CamelFolderChangeInfo *changeinfo, CamelException *ex) { DIR *dir; struct dirent *d; char *p, c; CamelMessageInfo *info; + CamelFolderSummary *s = (CamelFolderSummary *)cls; GHashTable *left; int i, count; int forceindex; @@ -258,13 +281,18 @@ mh_summary_check(CamelLocalSummary *cls, CamelFolderChangeInfo *changeinfo, Came g_hash_table_remove(left, uid); } camel_folder_summary_info_free((CamelFolderSummary *)cls, info); - } + } } } closedir(dir); g_hash_table_foreach(left, (GHFunc)remove_summary, cls); g_hash_table_destroy(left); + /* sort the summary based on message number (uid), since the directory order is not useful */ + CAMEL_SUMMARY_LOCK(s, summary_lock); + qsort(s->messages->pdata, s->messages->len, sizeof(CamelMessageInfo *), sort_uid_cmp); + CAMEL_SUMMARY_UNLOCK(s, summary_lock); + return 0; } |