NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
account.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stddef.h>
31 #include "mutt/lib.h"
32 #include "config/lib.h"
33 #include "account.h"
34 #include "mailbox.h"
35 
42 struct Account *account_new(const char *name, struct ConfigSubset *sub)
43 {
44  if (!sub)
45  return NULL;
46 
47  struct Account *a = mutt_mem_calloc(1, sizeof(struct Account));
48 
50  a->notify = notify_new();
51  a->name = mutt_str_dup(name);
52  a->sub = cs_subset_new(name, sub, a->notify);
53  a->sub->cs = sub->cs;
55 
56  return a;
57 }
58 
65 bool account_mailbox_add(struct Account *a, struct Mailbox *m)
66 {
67  if (!a || !m)
68  return false;
69 
70  if (a->type == MUTT_UNKNOWN)
71  a->type = m->type;
72 
73  m->account = a;
74  struct MailboxNode *np = mutt_mem_calloc(1, sizeof(*np));
75  np->mailbox = m;
76  STAILQ_INSERT_TAIL(&a->mailboxes, np, entries);
77  mailbox_set_subset(m, a->sub);
79 
80  mutt_debug(LL_NOTIFY, "NT_MAILBOX_ADD: %s %p\n", mailbox_get_type_name(m->type), m);
81  struct EventMailbox ev_m = { m };
83  return true;
84 }
85 
95 bool account_mailbox_remove(struct Account *a, struct Mailbox *m)
96 {
97  if (!a || STAILQ_EMPTY(&a->mailboxes))
98  return false;
99 
100  if (!m)
101  {
102  mutt_debug(LL_NOTIFY, "NT_MAILBOX_DELETE_ALL\n");
103  struct EventMailbox ev_m = { NULL };
105  }
106 
107  bool result = false;
108  struct MailboxNode *np = NULL;
109  struct MailboxNode *tmp = NULL;
110  STAILQ_FOREACH_SAFE(np, &a->mailboxes, entries, tmp)
111  {
112  if (m && (np->mailbox != m))
113  continue;
114 
115  STAILQ_REMOVE(&a->mailboxes, np, MailboxNode, entries);
116  if (!m)
117  mailbox_free(&np->mailbox);
118  FREE(&np);
119  result = true;
120  if (m)
121  break;
122  }
123 
124  return result;
125 }
126 
131 void account_free(struct Account **ptr)
132 {
133  if (!ptr || !*ptr)
134  return;
135 
136  struct Account *a = *ptr;
137 
138  mutt_debug(LL_NOTIFY, "NT_ACCOUNT_DELETE: %s %p\n", mailbox_get_type_name(a->type), a);
139  struct EventAccount ev_a = { a };
141 
142  account_mailbox_remove(a, NULL);
143 
144  if (a->adata && a->adata_free)
145  a->adata_free(&a->adata);
146 
147  cs_subset_free(&a->sub);
148  FREE(&a->name);
149  notify_free(&a->notify);
150 
151  FREE(ptr);
152 }
enum MailboxType type
Mailbox type.
Definition: mailbox.h:105
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
char * name
Name of Account.
Definition: account.h:39
Log of notifications.
Definition: logging.h:45
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
struct Notify * notify
Notifications: NotifyAccount, EventAccount.
Definition: account.h:42
struct MailboxList mailboxes
List of Mailboxes.
Definition: account.h:41
A group of associated Mailboxes.
Definition: account.h:36
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
Mailbox wasn&#39;t recognised.
Definition: mailbox.h:47
const char * mailbox_get_type_name(enum MailboxType type)
Get the type of a Mailbox.
Definition: mailbox.c:292
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:104
enum MailboxType type
Type of Mailboxes this Account contains.
Definition: account.h:38
An Event that happened to a Mailbox.
Definition: mailbox.h:190
An Event that happened to an Account.
Definition: account.h:78
Convenience wrapper for the config headers.
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
Mailbox has been added.
Definition: mailbox.h:173
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:87
#define STAILQ_INIT(head)
Definition: queue.h:372
void(* adata_free)(void **ptr)
Free the private data attached to the Account.
Definition: account.h:53
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
void account_free(struct Account **ptr)
Free an Account.
Definition: account.c:131
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
A set of inherited config items.
Definition: subset.h:46
A mailbox.
Definition: mailbox.h:81
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
Account has changed, NotifyAccount, EventAccount.
Definition: notify_type.h:34
This Config is Account-specific.
Definition: subset.h:39
A group of associated Mailboxes.
enum ConfigScope scope
Scope of Subset, e.g. SET_SCOPE_ACCOUNT.
Definition: subset.h:49
struct Account * account_new(const char *name, struct ConfigSubset *sub)
Create a new Account.
Definition: account.c:42
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
All Mailboxes are about to be deleted.
Definition: mailbox.h:175
struct ConfigSubset * cs_subset_new(const char *name, struct ConfigSubset *sub_parent, struct Notify *not_parent)
Create a new Config Subset.
Definition: subset.c:144
Representation of a mailbox.
struct Account * account
Account that owns this Mailbox.
Definition: mailbox.h:131
bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
Set a Mailbox&#39;s Config Subset.
Definition: mailbox.c:244
bool account_mailbox_add(struct Account *a, struct Mailbox *m)
Add a Mailbox to an Account.
Definition: account.c:65
#define FREE(x)
Definition: memory.h:40
bool account_mailbox_remove(struct Account *a, struct Mailbox *m)
Remove a Mailbox from an Account.
Definition: account.c:95
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:48
struct ConfigSubset * sub
Inherited config items.
Definition: account.h:40
#define STAILQ_EMPTY(head)
Definition: queue.h:348
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:93
List of Mailboxes.
Definition: mailbox.h:156
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:148
Account is about to be deleted.
Definition: account.h:70
Convenience wrapper for the library headers.
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:158
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:171