From 0c188701c1dfca1740bab509e6bcc314c475d5a7 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Wed, 3 Jul 2019 22:37:52 +0100 Subject: [PATCH] refactor users of AllMailboxes --- browser.c | 13 +++++++++---- imap/browse.c | 12 +++++++----- imap/imap.c | 4 +++- init.c | 14 +++++--------- mailbox.c | 22 ++++++++++++++++------ mailbox.h | 3 ++- main.c | 2 +- mutt_mailbox.c | 19 +++++++++++++------ neomutt.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ neomutt.h | 3 +++ sidebar.c | 12 ++++++++---- 11 files changed, 118 insertions(+), 37 deletions(-) diff --git a/browser.c b/browser.c index be8b84315..23c0dc80b 100644 --- a/browser.c +++ b/browser.c @@ -60,6 +60,7 @@ #include "mutt_window.h" #include "muttlib.h" #include "mx.h" +#include "neomutt.h" #include "opcodes.h" #include "options.h" #include "sendlib.h" @@ -783,12 +784,14 @@ static int examine_directory(struct Menu *menu, struct BrowserState *state, else if (!S_ISREG(s.st_mode)) continue; + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { if (mutt_str_strcmp(mutt_b2s(buf), mutt_b2s(np->mailbox->pathbuf)) != 0) break; } + neomutt_mailboxlist_clear(&ml); if (np && Context && Context->mailbox && (mutt_str_strcmp(np->mailbox->realpath, Context->mailbox->realpath) == 0)) @@ -842,14 +845,15 @@ static int examine_mailboxes(struct Menu *menu, struct BrowserState *state) { init_state(state, menu); - if (STAILQ_EMPTY(&AllMailboxes)) + if (TAILQ_EMPTY(&NeoMutt->accounts)) return -1; mailbox = mutt_buffer_pool_get(); md = mutt_buffer_pool_get(); mutt_mailbox_check(Context ? Context->mailbox : NULL, 0); + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { if (Context && (mutt_str_strcmp(np->mailbox->realpath, Context->mailbox->realpath) == 0)) { @@ -899,6 +903,7 @@ static int examine_mailboxes(struct Menu *menu, struct BrowserState *state) add_folder(menu, state, mutt_b2s(mailbox), np->mailbox->desc, &s, np->mailbox, NULL); } + neomutt_mailboxlist_clear(&ml); } browser_sort(state); @@ -1618,7 +1623,7 @@ void mutt_buffer_select_file(struct Buffer *file, SelectFileFlags flags, int nentry = menu->current; // TODO(sileht): It could be better to select INBOX instead. But I - // don't want to manipulate Context/AllMailboxes/mailbox->account here for now. + // don't want to manipulate Context/Mailboxes/mailbox->account here for now. // Let's just protect neomutt against crash for now. #1417 if (mutt_str_strcmp(mutt_b2s(Context->mailbox->pathbuf), state.entry[nentry].name) == 0) diff --git a/imap/browse.c b/imap/browse.c index 4de11b8df..eef72a68e 100644 --- a/imap/browse.c +++ b/imap/browse.c @@ -46,6 +46,7 @@ #include "mailbox.h" #include "mutt_logging.h" #include "muttlib.h" +#include "neomutt.h" /** * add_folder - Format and add an IMAP folder to the browser @@ -115,12 +116,14 @@ static void add_folder(char delim, char *folder, bool noselect, bool noinferiors (state->entry)[state->entrylen].selectable = !noselect; (state->entry)[state->entrylen].inferiors = !noinferiors; + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { if (mutt_str_strcmp(tmp, mutt_b2s(np->mailbox->pathbuf)) == 0) break; } + neomutt_mailboxlist_clear(&ml); if (np) { @@ -207,18 +210,17 @@ int imap_browse(const char *path, struct BrowserState *state) C_ImapCheckSubscribed = false; // Pick first mailbox connected to the same server + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_IMAP); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { - if (np->mailbox->magic != MUTT_IMAP) - continue; - adata = imap_adata_get(np->mailbox); // Pick first mailbox connected on the same server if (imap_account_match(&adata->conn_account, &conn_account)) break; adata = NULL; } + neomutt_mailboxlist_clear(&ml); if (!adata) goto fail; diff --git a/imap/imap.c b/imap/imap.c index efa2fb4bf..c5edfe084 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -518,8 +518,9 @@ static int complete_hosts(char *buf, size_t buflen) size_t matchlen; matchlen = mutt_str_strlen(buf); + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { if (!mutt_str_startswith(mutt_b2s(np->mailbox->pathbuf), buf, CASE_MATCH)) continue; @@ -532,6 +533,7 @@ static int complete_hosts(char *buf, size_t buflen) else longest_common_prefix(buf, mutt_b2s(np->mailbox->pathbuf), matchlen, buflen); } + neomutt_mailboxlist_clear(&ml); #if 0 TAILQ_FOREACH(conn, mutt_socket_head(), entries) diff --git a/init.c b/init.c index 209626626..3a3bd7bc1 100644 --- a/init.c +++ b/init.c @@ -3194,15 +3194,11 @@ int mutt_init(bool skip_sys_rc, struct ListHead *commands) if (C_VirtualSpoolfile) { /* Find the first virtual folder and open it */ - struct MailboxNode *mp = NULL; - STAILQ_FOREACH(mp, &AllMailboxes, entries) - { - if (mp->mailbox->magic == MUTT_NOTMUCH) - { - cs_str_string_set(Config, "spoolfile", mutt_b2s(mp->mailbox->pathbuf), NULL); - break; - } - } + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_NOTMUCH); + struct MailboxNode *mp = STAILQ_FIRST(&ml); + if (mp) + cs_str_string_set(Config, "spoolfile", mutt_b2s(mp->mailbox->pathbuf), NULL); + neomutt_mailboxlist_clear(&ml); } #endif diff --git a/mailbox.c b/mailbox.c index f51f68151..166b6e782 100644 --- a/mailbox.c +++ b/mailbox.c @@ -142,17 +142,21 @@ struct Mailbox *mutt_mailbox_find(const char *path) if (stat(path, &sb) != 0) return NULL; + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + struct Mailbox *m = NULL; + STAILQ_FOREACH(np, &ml, entries) { if ((stat(mutt_b2s(np->mailbox->pathbuf), &tmp_sb) == 0) && (sb.st_dev == tmp_sb.st_dev) && (sb.st_ino == tmp_sb.st_ino)) { - return np->mailbox; + m = np->mailbox; + break; } } + neomutt_mailboxlist_clear(&ml); - return NULL; + return m; } /** @@ -166,14 +170,20 @@ struct Mailbox *mutt_mailbox_find_desc(const char *desc) if (!desc) return NULL; + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + struct Mailbox *m = NULL; + STAILQ_FOREACH(np, &ml, entries) { if (np->mailbox->desc && (mutt_str_strcmp(np->mailbox->desc, desc) == 0)) - return np->mailbox; + { + m = np->mailbox; + break; + } } + neomutt_mailboxlist_clear(&ml); - return NULL; + return m; } /** diff --git a/mailbox.h b/mailbox.h index d7c14f50b..ff544fbfe 100644 --- a/mailbox.h +++ b/mailbox.h @@ -42,7 +42,8 @@ struct stat; */ enum MailboxType { - MUTT_MAILBOX_ERROR = -1, ///< Error occurred examining mailbox + MUTT_MAILBOX_ANY = -2, ///< Match any Mailbox type + MUTT_MAILBOX_ERROR = -1, ///< Error occurred examining Mailbox MUTT_UNKNOWN = 0, ///< Mailbox wasn't recognised MUTT_MBOX, ///< 'mbox' Mailbox type MUTT_MMDF, ///< 'mmdf' Mailbox type diff --git a/main.c b/main.c index 41832984b..5bbbddc31 100644 --- a/main.c +++ b/main.c @@ -1155,7 +1155,7 @@ int main(int argc, char *argv[], char *envp[]) } else #endif - if (STAILQ_EMPTY(&AllMailboxes)) + if (TAILQ_EMPTY(&NeoMutt->accounts)) { mutt_error(_("No incoming mailboxes defined")); goto main_curses; // TEST39: neomutt -n -F /dev/null -y diff --git a/mutt_mailbox.c b/mutt_mailbox.c index e17b28fe1..f11e25e68 100644 --- a/mutt_mailbox.c +++ b/mutt_mailbox.c @@ -11,6 +11,7 @@ #include "mutt_window.h" #include "muttlib.h" #include "mx.h" +#include "neomutt.h" #include "protos.h" static time_t MailboxTime = 0; /**< last time we started checking for mail */ @@ -111,7 +112,7 @@ static void mailbox_check(struct Mailbox *m_cur, struct Mailbox *m_check, } /** - * mutt_mailbox_check - Check all AllMailboxes for new mail + * mutt_mailbox_check - Check all all Mailboxes for new mail * @param m_cur Current Mailbox * @param force Force flags, see below * @retval num Number of mailboxes with new mail @@ -120,7 +121,7 @@ static void mailbox_check(struct Mailbox *m_cur, struct Mailbox *m_check, * - MUTT_MAILBOX_CHECK_FORCE ignore MailboxTime and check for new mail * - MUTT_MAILBOX_CHECK_FORCE_STATS ignore MailboxTime and calculate statistics * - * Check all AllMailboxes for new mail and total/new/flagged messages + * Check all all Mailboxes for new mail and total/new/flagged messages */ int mutt_mailbox_check(struct Mailbox *m_cur, int force) { @@ -137,7 +138,7 @@ int mutt_mailbox_check(struct Mailbox *m_cur, int force) #endif /* fastest return if there are no mailboxes */ - if (STAILQ_EMPTY(&AllMailboxes)) + if (TAILQ_EMPTY(&NeoMutt->accounts)) return 0; t = time(NULL); @@ -166,13 +167,15 @@ int mutt_mailbox_check(struct Mailbox *m_cur, int force) contex_sb.st_ino = 0; } + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { mailbox_check(m_cur, np->mailbox, &contex_sb, check_stats || (!np->mailbox->first_check_stats_done && C_MailCheckStats)); np->mailbox->first_check_stats_done = true; } + neomutt_mailboxlist_clear(&ml); return MailboxCount; } @@ -207,8 +210,9 @@ bool mutt_mailbox_list(void) mailboxlist[0] = '\0'; pos += strlen(strncat(mailboxlist, _("New mail in "), sizeof(mailboxlist) - 1 - pos)); + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { /* Is there new mail in this mailbox? */ if (!np->mailbox->has_new || (have_unnotified && np->mailbox->notified)) @@ -236,6 +240,7 @@ bool mutt_mailbox_list(void) pos += strlen(strncat(mailboxlist + pos, mutt_b2s(path), sizeof(mailboxlist) - 1 - pos)); first = 0; } + neomutt_mailboxlist_clear(&ml); if (!first && np) { @@ -292,8 +297,9 @@ void mutt_mailbox_next_buffer(struct Mailbox *m_cur, struct Buffer *s) bool found = false; for (int pass = 0; pass < 2; pass++) { + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { if (np->mailbox->magic == MUTT_NOTMUCH) /* only match real mailboxes */ continue; @@ -307,6 +313,7 @@ void mutt_mailbox_next_buffer(struct Mailbox *m_cur, struct Buffer *s) if (mutt_str_strcmp(mutt_b2s(s), mutt_b2s(np->mailbox->pathbuf)) == 0) found = true; } + neomutt_mailboxlist_clear(&ml); } mutt_mailbox_check(m_cur, MUTT_MAILBOX_CHECK_FORCE); /* mailbox was wrong - resync things */ diff --git a/neomutt.c b/neomutt.c index 28fcc2b88..6b8cf33f1 100644 --- a/neomutt.c +++ b/neomutt.c @@ -114,3 +114,54 @@ bool neomutt_account_remove(struct NeoMutt *n, struct Account *a) } return result; } + +/** + * neomutt_mailboxlist_clear - Free a Mailbox List + * @param ml Mailbox List to free + */ +void neomutt_mailboxlist_clear(struct MailboxList *ml) +{ + if (!ml) + return; + + struct MailboxNode *mn = NULL; + struct MailboxNode *tmp = NULL; + STAILQ_FOREACH_SAFE(mn, ml, entries, tmp) + { + STAILQ_REMOVE(ml, mn, MailboxNode, entries); + FREE(&mn); + } +} + +/** + * neomutt_mailboxlist_get_all - Get a List of all Mailboxes + * @param n NeoMutt + * @param type Type of Account to match, see #MailboxType + * @retval obj List of Mailboxes + * + * @note If type is #MUTT_MAILBOX_ANY then all Mailbox types will be matched + */ +struct MailboxList neomutt_mailboxlist_get_all(struct NeoMutt *n, enum MailboxType magic) +{ + struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml); + if (!n) + return ml; + + struct Account *a = NULL; + struct MailboxNode *mn = NULL; + + TAILQ_FOREACH(a, &n->accounts, entries) + { + if ((magic > MUTT_UNKNOWN) && (a->magic != magic)) + continue; + + STAILQ_FOREACH(mn, &a->mailboxes, entries) + { + struct MailboxNode *mn2 = mutt_mem_calloc(1, sizeof(*mn2)); + mn2->mailbox = mn->mailbox; + STAILQ_INSERT_TAIL(&ml, mn2, entries); + } + } + + return ml; +} diff --git a/neomutt.h b/neomutt.h index 6d5e6abeb..941e25d62 100644 --- a/neomutt.h +++ b/neomutt.h @@ -54,4 +54,7 @@ bool neomutt_account_remove(struct NeoMutt *n, struct Account *a); void neomutt_free(struct NeoMutt **ptr); struct NeoMutt *neomutt_new(void); +void neomutt_mailboxlist_clear(struct MailboxList *ml); +struct MailboxList neomutt_mailboxlist_get_all(struct NeoMutt *n, enum MailboxType magic); + #endif /* MUTT_NEOMUTT_H */ diff --git a/sidebar.c b/sidebar.c index 042c9636b..f7fe92c20 100644 --- a/sidebar.c +++ b/sidebar.c @@ -47,6 +47,7 @@ #include "mutt_window.h" #include "muttlib.h" #include "mx.h" +#include "neomutt.h" #include "opcodes.h" #include "sort.h" @@ -409,8 +410,9 @@ static void unsort_entries(void) { int i = 0; + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { if (i >= EntryCount) break; @@ -429,6 +431,7 @@ static void unsort_entries(void) i++; } } + neomutt_mailboxlist_clear(&ml); } /** @@ -804,8 +807,7 @@ static void fill_empty_space(int first_row, int num_rows, int div_width, int num * * Display a list of mailboxes in a panel on the left. What's displayed will * depend on our index markers: TopMailbox, OpnMailbox, HilMailbox, BotMailbox. - * On the first run they'll be NULL, so we display the top of NeoMutt's list - * (AllMailboxes). + * On the first run they'll be NULL, so we display the top of NeoMutt's list. * * * TopMailbox - first visible mailbox * * BotMailbox - last visible mailbox @@ -983,11 +985,13 @@ void mutt_sb_draw(void) if (!Entries) { + struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY); struct MailboxNode *np = NULL; - STAILQ_FOREACH(np, &AllMailboxes, entries) + STAILQ_FOREACH(np, &ml, entries) { mutt_sb_notify_mailbox(np->mailbox, true); } + neomutt_mailboxlist_clear(&ml); } if (!prepare_sidebar(num_rows)) -- 2.40.0