NeoMutt  2023-03-22-27-g3cb248
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 "core/lib.h"
34#include "account.h"
35#include "mailbox.h"
36
43struct Account *account_new(const char *name, struct ConfigSubset *sub)
44{
45 if (!sub)
46 return NULL;
47
48 struct Account *a = mutt_mem_calloc(1, sizeof(struct Account));
49
51 a->notify = notify_new();
53 a->sub = cs_subset_new(name, sub, a->notify);
54 a->sub->cs = sub->cs;
56
57 return a;
58}
59
66bool account_mailbox_add(struct Account *a, struct Mailbox *m)
67{
68 if (!a || !m)
69 return false;
70
71 if (a->type == MUTT_UNKNOWN)
72 a->type = m->type;
73
74 m->account = a;
75 struct MailboxNode *np = mutt_mem_calloc(1, sizeof(*np));
76 np->mailbox = m;
77 STAILQ_INSERT_TAIL(&a->mailboxes, np, entries);
80
81 mutt_debug(LL_NOTIFY, "NT_MAILBOX_ADD: %s %p\n", mailbox_get_type_name(m->type), m);
82 struct EventMailbox ev_m = { m };
84 return true;
85}
86
96bool account_mailbox_remove(struct Account *a, struct Mailbox *m)
97{
98 if (!a || STAILQ_EMPTY(&a->mailboxes))
99 return false;
100
101 if (!m)
102 {
103 mutt_debug(LL_NOTIFY, "NT_MAILBOX_DELETE_ALL\n");
104 struct EventMailbox ev_m = { NULL };
106 }
107
108 bool result = false;
109 struct MailboxNode *np = NULL;
110 struct MailboxNode *tmp = NULL;
111 STAILQ_FOREACH_SAFE(np, &a->mailboxes, entries, tmp)
112 {
113 if (m && (np->mailbox != m))
114 continue;
115
116 STAILQ_REMOVE(&a->mailboxes, np, MailboxNode, entries);
117 if (m)
118 {
119 m->account = NULL;
121 }
122 else
123 {
124 // we make it invisible here to force the deletion of the mailbox
125 np->mailbox->visible = false;
126 mailbox_free(&np->mailbox);
127 }
128 FREE(&np);
129 result = true;
130 if (m)
131 break;
132 }
133
134 return result;
135}
136
141void account_free(struct Account **ptr)
142{
143 if (!ptr || !*ptr)
144 return;
145
146 struct Account *a = *ptr;
147
148 mutt_debug(LL_NOTIFY, "NT_ACCOUNT_DELETE: %s %p\n", mailbox_get_type_name(a->type), a);
149 struct EventAccount ev_a = { a };
151
152 account_mailbox_remove(a, NULL);
153
154 if (a->adata && a->adata_free)
155 a->adata_free(&a->adata);
156
157 cs_subset_free(&a->sub);
158 FREE(&a->name);
159 notify_free(&a->notify);
160
161 FREE(ptr);
162}
bool account_mailbox_remove(struct Account *a, struct Mailbox *m)
Remove a Mailbox from an Account.
Definition: account.c:96
struct Account * account_new(const char *name, struct ConfigSubset *sub)
Create a new Account.
Definition: account.c:43
bool account_mailbox_add(struct Account *a, struct Mailbox *m)
Add a Mailbox to an Account.
Definition: account.c:66
void account_free(struct Account **ptr)
Free an Account.
Definition: account.c:141
A group of associated Mailboxes.
@ NT_ACCOUNT_DELETE
Account is about to be deleted.
Definition: account.h:69
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
Set a Mailbox's Config Subset.
Definition: mailbox.c:258
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:87
const char * mailbox_get_type_name(enum MailboxType type)
Get the type of a Mailbox.
Definition: mailbox.c:311
Representation of a mailbox.
@ NT_MAILBOX_DELETE_ALL
All Mailboxes are about to be deleted.
Definition: mailbox.h:170
@ NT_MAILBOX_ADD
Mailbox has been added.
Definition: mailbox.h:168
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition: mailbox.h:44
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
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
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:93
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
@ NT_MAILBOX
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:49
@ NT_ACCOUNT
Account has changed, NotifyAccount, EventAccount.
Definition: notify_type.h:36
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_INIT(head)
Definition: queue.h:372
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
#define STAILQ_EMPTY(head)
Definition: queue.h:348
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
A group of associated Mailboxes.
Definition: account.h:37
enum MailboxType type
Type of Mailboxes this Account contains.
Definition: account.h:38
char * name
Name of Account.
Definition: account.h:39
struct Notify * notify
Notifications: NotifyAccount, EventAccount.
Definition: account.h:42
void(* adata_free)(void **ptr)
Free the private data attached to the Account.
Definition: account.h:52
struct ConfigSubset * sub
Inherited config items.
Definition: account.h:40
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
struct MailboxList mailboxes
List of Mailboxes.
Definition: account.h:41
A set of inherited config items.
Definition: subset.h:47
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
enum ConfigScope scope
Scope of Subset, e.g. SET_SCOPE_ACCOUNT.
Definition: subset.h:49
An Event that happened to an Account.
Definition: account.h:78
An Event that happened to a Mailbox.
Definition: mailbox.h:185
List of Mailboxes.
Definition: mailbox.h:152
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:153
A mailbox.
Definition: mailbox.h:79
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:143
struct Account * account
Account that owns this Mailbox.
Definition: mailbox.h:127
bool visible
True if a result of "mailboxes".
Definition: mailbox.h:130
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
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
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:104
@ SET_SCOPE_ACCOUNT
This Config is Account-specific.
Definition: subset.h:39