From: Melvin H. <mha...@us...> - 2003-08-19 09:12:48
|
Update of /cvsroot/libetpan/xetpan/src In directory sc8-pr-cvs1:/tmp/cvs-serv23348/src Modified Files: folder.c Log Message: - Removed obsolete code; code clean up. - Folder_new(): new function used by a_Folder_new() and a_Folder_new_copy() - a_Folder_new(): unique folder identifier is now signed but only the non-zero positive space is used. Negative space is a mark to delete the folder (used by the mailbox editor) - a_Folder_new_copy(): updated to use Folder_new() - a_Folder_free_copy(): new function to free a copy of a folder - a_Folder_list_copy() -> a_Folder_list_new_copy() - a_Folder_list_copy(): new function to copy a list of folders to an already existing list - a_Folder_list_free_copy(): new function to free a list of copies of folders - a_Folder_get_from_ref(): removed warning when the folder is not found, because it is now expected to not find a folder if it was removed by the user Index: folder.c =================================================================== RCS file: /cvsroot/libetpan/xetpan/src/folder.c,v retrieving revision 1.19 retrieving revision 1.20 diff -u -d -r1.19 -r1.20 --- folder.c 26 Jul 2003 11:04:34 -0000 1.19 +++ folder.c 19 Aug 2003 09:12:45 -0000 1.20 @@ -39,24 +39,19 @@ #include "mailbox.h" #include "message.h" -#if 0 -static int -Folder_list_folder_cb(cntree *atree, void *data); +#include "ui.h" -static int -Folder_list_add_folder_cb(cntree *atree, void *data); -#endif +static XePFolder * +Folder_new(XePFolderType type); -static int -Folder_storage_create(XePFolder *folder); +static void +Folder_free(XePFolder *folder); -#if 0 static int -Folder_processing_cb(cntree *atree, void *data, void **post_data); +Folder_ref_list_copy(clist *source, clist *target); static int -Folder_search_cb(cntree *atree, void *data, void **post_data); -#endif +Folder_storage_create(XePFolder *folder); static void Folder_update_msg_session(XePFolder *folder); @@ -64,6 +59,7 @@ static void Folder_append_file_cb(void *anull, void *amessage); + #define XEP_FOLDER_RECONNECT_MAX_TRIES 5 #define XEP_FOLDER_HASH_SIZE 128 @@ -78,19 +74,44 @@ a_Folder_new(XePFolderType type) { XePFolder *folder; - static unsigned long uid = 0; + static long uid = 0; A_LOGGING_TRACE; + folder = Folder_new(type); + if (folder) + { + /* TODO: remove the periodicity of folder uid, because in + * the very long term, in theory, two folders can have the + * same uid + */ + if (uid < 0) /* Use only the positive space */ + uid = 0; + folder->uid = ++uid; + + if (folder_uid_hash_table == NULL) + folder_uid_hash_table = cinthash_new(XEP_FOLDER_HASH_SIZE); + + cinthash_add(folder_uid_hash_table, folder->uid, folder); + } + return folder; +} + +static XePFolder * +Folder_new(XePFolderType type) +{ + XePFolder *folder; + folder = x_alloc(1, XePFolder); if (folder == NULL) A_LOGGING_SYSTEM_ERROR("x_alloc: %s", strerror(errno)); else { + folder->id = NULL; - folder->uid = ++uid; folder->type = type; + folder->uid = 0; folder->thread.state = XEP_MANAGER_THREAD_STATE_NONE; folder->mailbox = NULL; folder->path = NULL; @@ -102,17 +123,15 @@ folder->unseen = 0; folder->recent = 0; - if (folder_uid_hash_table == NULL) - folder_uid_hash_table = cinthash_new(XEP_FOLDER_HASH_SIZE); - - cinthash_add(folder_uid_hash_table, folder->uid, folder); - switch (type) { case XEP_FOLDER_TYPE_REAL: folder->rule_refs = a_Processing_ref_list_new(); + folder->folder_refs = NULL; + folder->condition_refs = NULL; break; case XEP_FOLDER_TYPE_VIRTUAL: + folder->rule_refs = NULL; folder->folder_refs = a_Folder_ref_list_new(); folder->condition_refs = a_Processing_ref_list_new(); break; @@ -127,88 +146,33 @@ a_Folder_new_copy(XePFolder *folder) { XePFolder *folder_copy; - clistiter *iter; - XePProcessingRef *ref; - XePProcessingRef *ref_copy; - XePFolderRef *folder_ref; - XePFolderRef *folder_ref_copy; int r = 0; - - folder_copy = a_Folder_new(folder->type); + folder_copy = Folder_new(folder->type); if (folder_copy == NULL) { return NULL; } - - folder_copy->id = X_STRDUP(folder->id); - folder_copy->path = X_STRDUP(folder->path); + + folder_copy->uid = folder->uid; + folder_copy->id = X_STRDUP(folder->id); + folder_copy->path = X_STRDUP(folder->path); switch (folder_copy->type) { case XEP_FOLDER_TYPE_REAL: - for (iter = clist_begin(folder->rule_refs); - iter && r == 0; - iter = iter->next) - { - ref = (XePProcessingRef *) iter->data; - ref_copy = a_Processing_ref_new_copy(ref); - if (ref_copy) - { - if (clist_append(folder_copy->rule_refs, ref_copy)) - { - A_LOGGING_ERROR("malloc(): %s", strerror(errno)); - a_Processing_ref_free(ref_copy); - r = -1; - } - } - else - { - r = -1; - } - } + r = a_Processing_ref_list_copy(folder->rule_refs, + folder_copy->rule_refs); + break; case XEP_FOLDER_TYPE_VIRTUAL: - for (iter = clist_begin(folder->folder_refs); - iter && r == 0; - iter = iter->next) - { - folder_ref = (XePFolderRef *) iter->data; - folder_ref_copy = a_Folder_ref_new_copy(folder_ref); - if (folder_ref_copy) - { - if (clist_append(folder_copy->folder_refs, folder_ref_copy)) - { - A_LOGGING_ERROR("malloc(): %s", strerror(errno)); - a_Folder_ref_free(folder_ref_copy); - r = -1; - } - } - else - { - r = -1; - } - } - for (iter = clist_begin(folder->condition_refs); - iter && r == 0; - iter = iter->next) - { - ref = (XePProcessingRef *) iter->data; - ref_copy = a_Processing_ref_new_copy(ref); - if (ref_copy) - { - if (clist_append(folder_copy->condition_refs, ref_copy)) - { - A_LOGGING_ERROR("malloc(): %s", strerror(errno)); - a_Processing_ref_free(ref_copy); - r = -1; - } - } - else - { - r = -1; - } - } + + r = Folder_ref_list_copy(folder->folder_refs, folder_copy->folder_refs); + + if (r == 0) + r = a_Processing_ref_list_copy(folder->condition_refs, + folder_copy->condition_refs); + break; default: break; @@ -216,7 +180,7 @@ if (r == -1) { - a_Folder_free(folder_copy); + a_Folder_free_copy(folder_copy); folder_copy = NULL; } @@ -230,6 +194,20 @@ return; cinthash_remove(folder_uid_hash_table, folder->uid); + Folder_free(folder); +} + +void +a_Folder_free_copy(XePFolder *folder) +{ + if (folder) + Folder_free(folder); +} + +static void +Folder_free(XePFolder *folder) +{ + A_LOGGING_TRACE; x_free(folder->id); @@ -251,8 +229,57 @@ x_free(folder); } + +void +a_Folder_copy(XePFolder *source, XePFolder *target) +{ + int r = 0; + + x_free(target->id); + target->id = X_STRDUP(source->id); + + x_free(target->path); + target->path = X_STRDUP(source->path); + + /* Type and UID cannot be changed by the user, so they are the + * same + */ + + /* The mailbox is certainly the same, too */ + + + switch (source->type) + { + case XEP_FOLDER_TYPE_REAL: + a_Processing_ref_list_free(target->rule_refs); + r = a_Processing_ref_list_copy(source->rule_refs, + target->rule_refs); + + break; + case XEP_FOLDER_TYPE_VIRTUAL: + + a_Folder_ref_list_free(target->folder_refs); + r = Folder_ref_list_copy(source->folder_refs, target->folder_refs); + + if (r == 0) + { + a_Processing_ref_list_free(target->condition_refs); + r = a_Processing_ref_list_copy(source->condition_refs, + target->condition_refs); + } + + break; + default: + break; + } +#if 0 + /* TODO: handle errors */ + return r; +#endif +} + XePFolder * -a_Folder_get_from_uid(unsigned long uid) +a_Folder_get_from_uid(long uid) { return (XePFolder *) cinthash_find(folder_uid_hash_table, uid); } @@ -316,7 +343,7 @@ } if (r == -1) { - a_Folder_list_free(folders_copy); + a_Folder_list_free_copy(folders_copy); folders_copy = NULL; } return folders_copy; @@ -330,12 +357,92 @@ if (!folders) return; + A_LOGGING_TRACE; + for (iter = clist_begin(folders); iter; iter = iter->next) a_Folder_free((XePFolder *) iter->data); clist_free(folders); } +void +a_Folder_list_free_copy(clist *folders) +{ + clistiter *iter; + + if (!folders) + return; + + A_LOGGING_TRACE; + + for (iter = clist_begin(folders); iter; iter = iter->next) + a_Folder_free_copy((XePFolder *) iter->data); + + clist_free(folders); +} + +void +a_Folder_list_copy(clist *source, clist *target) +{ + clistiter *iter; + clistiter *next; + int r = 0; + XePFolder *folder_copy; + XePFolder *folder; + + iter = clist_begin(source); + while (iter) + iter = clist_delete(source, iter); + + iter = clist_begin(source); + while (iter && r == 0) + { + folder_copy = (XePFolder *) iter->data; + next = iter->next; + + if (folder_copy->uid < 0) + { + /* Folder was marked for delete */ + folder = a_Folder_get_from_uid(-folder_copy->uid); + if (folder) + { + a_Folder_free(folder); + } + else + { + A_LOGGING_SYSTEM_ERROR("attempt to delete a non-existent " + "folder %s", folder_copy->id); + } + } + else + { + folder = a_Folder_get_from_uid(folder_copy->uid); + if (folder == NULL) + { + /* Steal the copy and register it. */ + folder = folder_copy; + a_Folder_register(folder); + /* Be kind enough, and replace the stolen folder + * with a copy */ + folder_copy = a_Folder_new_copy(folder); + iter->data = folder_copy; + } + else if (folder == folder_copy) + { + folder = a_Folder_new_copy(folder_copy); + iter->data = folder; + folder = folder_copy; + } + else + { + a_Folder_copy(folder_copy, folder); + } + clist_append(target, folder); + } + iter = next; + } + +} static void Folder_list_dump(XePFoldersList *folderslist) @@ -349,7 +456,6 @@ } } - XePFolderRef * a_Folder_ref_new(void) { @@ -429,6 +535,36 @@ folder_ref->folder = NULL; } +static int +Folder_ref_list_copy(clist *source, clist *target) +{ + XePFolderRef *folder_ref; + XePFolderRef *folder_ref_copy; + + clistiter *iter; + int r = 0; + + for (iter = clist_begin(source); iter && r == 0; iter = iter->next) + { + folder_ref = (XePFolderRef *) iter->data; + folder_ref_copy = a_Folder_ref_new_copy(folder_ref); + if (folder_ref_copy) + { + if (clist_append(target, folder_ref_copy)) + { + A_LOGGING_ERROR("malloc(): %s", strerror(errno)); + a_Folder_ref_free(folder_ref_copy); + r = -1; + } + } + else + { + r = -1; + } + } + return r; +} + void a_Folder_ref_relink_all(void) { @@ -468,11 +604,6 @@ folder_ref->folder = a_Folder_get_from_ref(folder_ref->ref); r = (folder_ref->folder == NULL); - if (r) - { - A_LOGGING_ERROR("Reference made to a non existent folder '%s'", - folder_ref->ref); - } return r; } |