From: Richard Russon Date: Mon, 1 Jul 2019 16:28:40 +0000 (+0100) Subject: neomutt: move AllAccounts X-Git-Tag: 2019-10-25~139^2~10 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fa5211d49938b0b21a2fc1e3d1fa698a02bb08ef;p=neomutt neomutt: move AllAccounts --- diff --git a/account.c b/account.c index 96a013efb..b145d65f1 100644 --- a/account.c +++ b/account.c @@ -1,7 +1,7 @@ /** * @file - * Representation of an account - * + * A group of associated Mailboxes + * @authors * Copyright (C) 2018 Richard Russon * @@ -21,16 +21,15 @@ */ /** - * @page account Representation of an account + * @page account A group of associated Mailboxes * - * Representation of an account + * A group of associated Mailboxes */ #include "config.h" #include "mutt/mutt.h" #include "account.h" - -struct AccountList AllAccounts = TAILQ_HEAD_INITIALIZER(AllAccounts); +#include "neomutt.h" /** * account_new - Create a new Account @@ -122,6 +121,8 @@ void account_free_config(struct Account *a) * account_mailbox_remove - Remove a Mailbox from an Account * @param a Account * @param m Mailbox to remove + * + * @note If m is NULL, all the mailboxes will be removed. */ bool account_mailbox_remove(struct Account *a, struct Mailbox *m) { @@ -130,19 +131,23 @@ bool account_mailbox_remove(struct Account *a, struct Mailbox *m) bool result = false; struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &a->mailboxes, entries) + struct MailboxNode *tmp = NULL; + STAILQ_FOREACH_SAFE(np, &a->mailboxes, entries, tmp) { - if (np->mailbox == m) + if (!m || (np->mailbox == m)) { STAILQ_REMOVE(&a->mailboxes, np, MailboxNode, entries); + mailbox_free(&np->mailbox); + FREE(&np); result = true; - break; + if (m) + break; } } if (STAILQ_EMPTY(&a->mailboxes)) { - account_free(&a); + neomutt_account_remove(NeoMutt, a); } return result; } @@ -157,8 +162,8 @@ void account_free(struct Account **ptr) return; struct Account *a = *ptr; - if (!TAILQ_EMPTY(&AllAccounts)) - TAILQ_REMOVE(&AllAccounts, a, entries); + account_mailbox_remove(a, NULL); + if (a->free_adata) a->free_adata(&a->adata); diff --git a/account.h b/account.h index 2f21b2d43..e19a22fc8 100644 --- a/account.h +++ b/account.h @@ -1,6 +1,6 @@ /** * @file - * Representation of a mailbox + * A group of associated Mailboxes * * @authors * Copyright (C) 1996-2000,2010,2013 Michael R. Elkins @@ -30,17 +30,19 @@ #include "mailbox.h" struct ConnAccount; +struct Notify; /** - * struct Account - XXX + * struct Account - A group of associated Mailboxes */ struct Account { - enum MailboxType magic; - struct MailboxList mailboxes; - TAILQ_ENTRY(Account) entries; - void *adata; - void (*free_adata)(void **); + enum MailboxType magic; ///< Type of Mailboxes this Account contains + struct MailboxList mailboxes; ///< List of Mailboxes + TAILQ_ENTRY(Account) entries; ///< Linked list of Accounts + struct Notify *notify; ///< Notifications handler + void *adata; ///< Private data (for Mailbox backends) + void (*free_adata)(void **); ///< Callback function to free private data char *name; ///< Name of Account const struct ConfigSet *cs; ///< Parent ConfigSet @@ -50,8 +52,6 @@ struct Account }; TAILQ_HEAD(AccountList, Account); -extern struct AccountList AllAccounts; ///< List of all Accounts - bool account_add_config(struct Account *a, const struct ConfigSet *cs, const char *name, const char *var_names[]); void account_free(struct Account **ptr); void account_free_config(struct Account *a); diff --git a/imap/imap.c b/imap/imap.c index 1751e5171..efa2fb4bf 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -56,6 +56,7 @@ #include "mutt_socket.h" #include "muttlib.h" #include "mx.h" +#include "neomutt.h" #include "pattern.h" #include "progress.h" #include "sort.h" @@ -686,7 +687,7 @@ static void imap_logout(struct ImapAccountData *adata) void imap_logout_all(void) { struct Account *np = NULL; - TAILQ_FOREACH(np, &AllAccounts, entries) + TAILQ_FOREACH(np, &NeoMutt->accounts, entries) { if (np->magic != MUTT_IMAP) continue; diff --git a/imap/util.c b/imap/util.c index 6b4e18d17..45ffb4049 100644 --- a/imap/util.c +++ b/imap/util.c @@ -56,6 +56,7 @@ #include "mailbox.h" #include "message.h" #include "mutt_account.h" +#include "neomutt.h" #include "options.h" /* These Config Variables are only used in imap/util.c */ @@ -139,7 +140,7 @@ int imap_adata_find(const char *path, struct ImapAccountData **adata, return -1; struct Account *np = NULL; - TAILQ_FOREACH(np, &AllAccounts, entries) + TAILQ_FOREACH(np, &NeoMutt->accounts, entries) { if (np->magic != MUTT_IMAP) continue; @@ -1056,7 +1057,7 @@ void imap_keepalive(void) { time_t now = time(NULL); struct Account *np = NULL; - TAILQ_FOREACH(np, &AllAccounts, entries) + TAILQ_FOREACH(np, &NeoMutt->accounts, entries) { if (np->magic != MUTT_IMAP) continue; diff --git a/init.c b/init.c index 495b65b12..209626626 100644 --- a/init.c +++ b/init.c @@ -60,6 +60,7 @@ #include "mx.h" #include "myvar.h" #include "ncrypt/ncrypt.h" +#include "neomutt.h" #include "options.h" #include "protos.h" #include "sidebar.h" @@ -1383,7 +1384,7 @@ static enum CommandResult parse_mailboxes(struct Buffer *buf, struct Buffer *s, } if (new_account) { - TAILQ_INSERT_TAIL(&AllAccounts, a, entries); + neomutt_account_add(NeoMutt, a); } struct MailboxNode *mn = mutt_mem_calloc(1, sizeof(*mn)); diff --git a/main.c b/main.c index b0bbee727..41832984b 100644 --- a/main.c +++ b/main.c @@ -1265,7 +1265,7 @@ main_exit: mutt_browser_cleanup(); mutt_free_opts(); mutt_free_keys(); - cs_free(&Config); neomutt_free(&NeoMutt); + cs_free(&Config); return rc; } diff --git a/mx.c b/mx.c index 0a28639c3..d35ad094f 100644 --- a/mx.c +++ b/mx.c @@ -61,6 +61,7 @@ #include "mutt_thread.h" #include "muttlib.h" #include "ncrypt/ncrypt.h" +#include "neomutt.h" #include "opcodes.h" #include "options.h" #include "pattern.h" @@ -297,7 +298,7 @@ struct Context *mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags) } if (new_account) { - TAILQ_INSERT_TAIL(&AllAccounts, a, entries); + neomutt_account_add(NeoMutt, a); } } @@ -1484,7 +1485,7 @@ struct Account *mx_ac_find(struct Mailbox *m) return NULL; struct Account *np = NULL; - TAILQ_FOREACH(np, &AllAccounts, entries) + TAILQ_FOREACH(np, &NeoMutt->accounts, entries) { if (np->magic != m->magic) continue; @@ -1531,7 +1532,7 @@ struct Mailbox *mx_mbox_find2(const char *path) mx_path_canon(buf, sizeof(buf), C_Folder, NULL); struct Account *np = NULL; - TAILQ_FOREACH(np, &AllAccounts, entries) + TAILQ_FOREACH(np, &NeoMutt->accounts, entries) { struct Mailbox *m = mx_mbox_find(np, buf); if (m) diff --git a/neomutt.c b/neomutt.c index f116b83c2..52f402365 100644 --- a/neomutt.c +++ b/neomutt.c @@ -29,6 +29,7 @@ #include "config.h" #include "mutt/mutt.h" #include "neomutt.h" +#include "account.h" struct NeoMutt *NeoMutt; ///< Global NeoMutt object @@ -40,6 +41,7 @@ struct NeoMutt *neomutt_new(void) { struct NeoMutt *n = mutt_mem_calloc(1, sizeof(*NeoMutt)); + TAILQ_INIT(&n->accounts); n->notify = notify_new(n, NT_NEOMUTT); return n; @@ -56,7 +58,55 @@ void neomutt_free(struct NeoMutt **ptr) struct NeoMutt *n = *ptr; + neomutt_account_remove(n, NULL); notify_free(&n->notify); FREE(ptr); } + +/** + * neomutt_account_add - Add an Account to the global list + * @param n NeoMutt + * @param a Account to add + * @retval true If Account was added + */ +bool neomutt_account_add(struct NeoMutt *n, struct Account *a) +{ + if (!n || !a) + return false; + + TAILQ_INSERT_TAIL(&n->accounts, a, entries); + notify_set_parent(a->notify, n->notify); + + return true; +} + +/** + * neomutt_account_remove - Remove an Account from the global list + * @param n NeoMutt + * @param a Account to remove + * @retval true If Account was removed + * + * @note If a is NULL, all the Accounts will be removed + */ +bool neomutt_account_remove(struct NeoMutt *n, struct Account *a) +{ + if (!n) + return false; + + bool result = false; + struct Account *np = NULL; + struct Account *tmp = NULL; + TAILQ_FOREACH_SAFE(np, &n->accounts, entries, tmp) + { + if (!a || (np == a)) + { + TAILQ_REMOVE(&n->accounts, np, entries); + account_free(&np); + result = true; + if (a) + break; + } + } + return result; +} diff --git a/neomutt.h b/neomutt.h index bb2d20479..6d5e6abeb 100644 --- a/neomutt.h +++ b/neomutt.h @@ -23,6 +23,9 @@ #ifndef MUTT_NEOMUTT_H #define MUTT_NEOMUTT_H +#include +#include "account.h" + struct Notify; /** @@ -31,6 +34,7 @@ struct Notify; struct NeoMutt { struct Notify *notify; ///< Notifications handler + struct AccountList accounts; ///< List of all Accounts }; extern struct NeoMutt *NeoMutt; @@ -45,6 +49,8 @@ enum NotifyGlobal NT_GLOBAL_TIMEOUT, ///< A timer has elapsed }; +bool neomutt_account_add(struct NeoMutt *n, struct Account *a); +bool neomutt_account_remove(struct NeoMutt *n, struct Account *a); void neomutt_free(struct NeoMutt **ptr); struct NeoMutt *neomutt_new(void);