forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.c
141 lines (123 loc) · 3.37 KB
/
account.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @file
* A group of associated Mailboxes
*
* @authors
* Copyright (C) 2018-2019 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page core_account A group of associated Mailboxes
*
* A group of associated Mailboxes
*/
#include "config.h"
#include <stddef.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/neomutt.h"
#include "account.h"
#include "mailbox.h"
/**
* account_new - Create a new Account
* @param name Name for the Account
* @param sub Parent Config Subset
* @retval ptr New Account
*/
struct Account *account_new(const char *name, struct ConfigSubset *sub)
{
if (!sub)
return NULL;
struct Account *a = mutt_mem_calloc(1, sizeof(struct Account));
STAILQ_INIT(&a->mailboxes);
a->notify = notify_new();
a->name = mutt_str_dup(name);
a->sub = cs_subset_new(name, sub, a->notify);
a->sub->cs = sub->cs;
a->sub->scope = SET_SCOPE_ACCOUNT;
return a;
}
/**
* account_mailbox_add - Add a Mailbox to an Account
* @param a Account
* @param m Mailbox to add
* @retval true If Mailbox was added
*/
bool account_mailbox_add(struct Account *a, struct Mailbox *m)
{
if (!a || !m)
return false;
if (a->type == MUTT_UNKNOWN)
a->type = m->type;
m->account = a;
struct MailboxNode *np = mutt_mem_calloc(1, sizeof(*np));
np->mailbox = m;
STAILQ_INSERT_TAIL(&a->mailboxes, np, entries);
mailbox_set_subset(m, a->sub);
notify_set_parent(m->notify, a->notify);
struct EventMailbox ev_m = { m };
notify_send(a->notify, NT_MAILBOX, NT_MAILBOX_ADD, &ev_m);
return true;
}
/**
* 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 and FREE'd. Otherwise,
* the specified mailbox is removed from the Account but not FREE'd.
*/
bool account_mailbox_remove(struct Account *a, struct Mailbox *m)
{
if (!a)
return false;
bool result = false;
struct MailboxNode *np = NULL;
struct MailboxNode *tmp = NULL;
STAILQ_FOREACH_SAFE(np, &a->mailboxes, entries, tmp)
{
if (!m || (np->mailbox == m))
{
struct EventMailbox ev_m = { m };
notify_send(a->notify, NT_MAILBOX, NT_MAILBOX_REMOVE, &ev_m);
STAILQ_REMOVE(&a->mailboxes, np, MailboxNode, entries);
if (!m)
mailbox_free(&np->mailbox);
FREE(&np);
result = true;
if (m)
break;
}
}
return result;
}
/**
* account_free - Free an Account
* @param[out] ptr Account to free
*/
void account_free(struct Account **ptr)
{
if (!ptr || !*ptr)
return;
struct Account *a = *ptr;
if (a->adata_free)
a->adata_free(&a->adata);
account_mailbox_remove(a, NULL);
cs_subset_free(&a->sub);
FREE(&a->name);
notify_free(&a->notify);
FREE(ptr);
}