]> granicus.if.org Git - neomutt/commitdiff
neomutt: move AllAccounts
authorRichard Russon <rich@flatcap.org>
Mon, 1 Jul 2019 16:28:40 +0000 (17:28 +0100)
committerRichard Russon <rich@flatcap.org>
Sat, 13 Jul 2019 23:25:45 +0000 (00:25 +0100)
account.c
account.h
imap/imap.c
imap/util.c
init.c
main.c
mx.c
neomutt.c
neomutt.h

index 96a013efb7795dca3085b725c58bdf2b7b732d47..b145d65f14589c2fcfe2964830ef68962a793824 100644 (file)
--- 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 <rich@flatcap.org>
  *
  */
 
 /**
- * @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);
 
index 2f21b2d436e249994e69d764ceffa6e65de76e65..e19a22fc8c34b9ce5361e2bdac8272d3b5b135cb 100644 (file)
--- 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 <me@mutt.org>
 #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);
index 1751e517130854d06f697be31615b45ce4a73f19..efa2fb4bf892a8137555eaae94bc96d27dd5d00f 100644 (file)
@@ -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;
index 6b4e18d17078dbed98de009a8b8c7a4f203783af..45ffb40497a90ea2846d81eb5474f0d3276c548d 100644 (file)
@@ -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 495b65b12faebc2e5491e1aa8235bf623ad09fae..2096266268fd598da4a08672667b632d1b6003f7 100644 (file)
--- 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 b0bbee72704b0a32dc70d45ad2a765ba77e48c4d..41832984b5cf68a8ebf8738435788d910d8e3fe5 100644 (file)
--- 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 0a28639c3e55c5701dd037b9be69b589ebea7943..d35ad094fe0c0f6e2bd37362f0a6e20c0297e779 100644 (file)
--- 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)
index f116b83c2c3b56c27d2821127e4d0655c35a8701..52f402365f3600d090956471383d8f92099ec059 100644 (file)
--- 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;
+}
index bb2d20479a4c83cd4ab6c5854691ea0a6ab3a18d..6d5e6abeb4fabe7cc10c4f41a5ecc5e216042120 100644 (file)
--- a/neomutt.h
+++ b/neomutt.h
@@ -23,6 +23,9 @@
 #ifndef MUTT_NEOMUTT_H
 #define MUTT_NEOMUTT_H
 
+#include <stdbool.h>
+#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);