Coverage Report

Created: 2023-06-07 06:15

/src/neomutt/core/account.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * @file
3
 * A group of associated Mailboxes
4
 *
5
 * @authors
6
 * Copyright (C) 2018-2019 Richard Russon <rich@flatcap.org>
7
 *
8
 * @copyright
9
 * This program is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License as published by the Free Software
11
 * Foundation, either version 2 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * This program is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU General Public License along with
20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
/**
24
 * @page core_account Account object
25
 *
26
 * A group of associated Mailboxes
27
 */
28
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
37
/**
38
 * account_new - Create a new Account
39
 * @param name Name for the Account
40
 * @param sub  Parent Config Subset
41
 * @retval ptr New Account
42
 */
43
struct Account *account_new(const char *name, struct ConfigSubset *sub)
44
0
{
45
0
  if (!sub)
46
0
    return NULL;
47
48
0
  struct Account *a = mutt_mem_calloc(1, sizeof(struct Account));
49
50
0
  STAILQ_INIT(&a->mailboxes);
51
0
  a->notify = notify_new();
52
0
  a->name = mutt_str_dup(name);
53
0
  a->sub = cs_subset_new(name, sub, a->notify);
54
0
  a->sub->cs = sub->cs;
55
0
  a->sub->scope = SET_SCOPE_ACCOUNT;
56
57
0
  return a;
58
0
}
59
60
/**
61
 * account_mailbox_add - Add a Mailbox to an Account
62
 * @param a Account
63
 * @param m Mailbox to add
64
 * @retval true Mailbox was added
65
 */
66
bool account_mailbox_add(struct Account *a, struct Mailbox *m)
67
0
{
68
0
  if (!a || !m)
69
0
    return false;
70
71
0
  if (a->type == MUTT_UNKNOWN)
72
0
    a->type = m->type;
73
74
0
  m->account = a;
75
0
  struct MailboxNode *np = mutt_mem_calloc(1, sizeof(*np));
76
0
  np->mailbox = m;
77
0
  STAILQ_INSERT_TAIL(&a->mailboxes, np, entries);
78
0
  mailbox_set_subset(m, a->sub);
79
0
  notify_set_parent(m->notify, a->notify);
80
81
0
  mutt_debug(LL_NOTIFY, "NT_MAILBOX_ADD: %s %p\n", mailbox_get_type_name(m->type), m);
82
0
  struct EventMailbox ev_m = { m };
83
0
  notify_send(a->notify, NT_MAILBOX, NT_MAILBOX_ADD, &ev_m);
84
0
  return true;
85
0
}
86
87
/**
88
 * account_mailbox_remove - Remove a Mailbox from an Account
89
 * @param a Account
90
 * @param m Mailbox to remove
91
 * @retval true On success
92
 *
93
 * @note If m is NULL, all the mailboxes will be removed and FREE'd. Otherwise,
94
 * the specified mailbox is removed from the Account but not FREE'd.
95
 */
96
bool account_mailbox_remove(struct Account *a, struct Mailbox *m)
97
0
{
98
0
  if (!a || STAILQ_EMPTY(&a->mailboxes))
99
0
    return false;
100
101
0
  if (!m)
102
0
  {
103
0
    mutt_debug(LL_NOTIFY, "NT_MAILBOX_DELETE_ALL\n");
104
0
    struct EventMailbox ev_m = { NULL };
105
0
    notify_send(a->notify, NT_MAILBOX, NT_MAILBOX_DELETE_ALL, &ev_m);
106
0
  }
107
108
0
  bool result = false;
109
0
  struct MailboxNode *np = NULL;
110
0
  struct MailboxNode *tmp = NULL;
111
0
  STAILQ_FOREACH_SAFE(np, &a->mailboxes, entries, tmp)
112
0
  {
113
0
    if (m && (np->mailbox != m))
114
0
      continue;
115
116
0
    STAILQ_REMOVE(&a->mailboxes, np, MailboxNode, entries);
117
0
    if (m)
118
0
    {
119
0
      m->account = NULL;
120
0
      notify_set_parent(m->notify, NeoMutt->notify);
121
0
    }
122
0
    else
123
0
    {
124
      // we make it invisible here to force the deletion of the mailbox
125
0
      np->mailbox->visible = false;
126
0
      mailbox_free(&np->mailbox);
127
0
    }
128
0
    FREE(&np);
129
0
    result = true;
130
0
    if (m)
131
0
      break;
132
0
  }
133
134
0
  return result;
135
0
}
136
137
/**
138
 * account_free - Free an Account
139
 * @param[out] ptr Account to free
140
 */
141
void account_free(struct Account **ptr)
142
0
{
143
0
  if (!ptr || !*ptr)
144
0
    return;
145
146
0
  struct Account *a = *ptr;
147
148
0
  mutt_debug(LL_NOTIFY, "NT_ACCOUNT_DELETE: %s %p\n", mailbox_get_type_name(a->type), a);
149
0
  struct EventAccount ev_a = { a };
150
0
  notify_send(a->notify, NT_ACCOUNT, NT_ACCOUNT_DELETE, &ev_a);
151
152
0
  account_mailbox_remove(a, NULL);
153
154
0
  if (a->adata && a->adata_free)
155
0
    a->adata_free(&a->adata);
156
157
0
  cs_subset_free(&a->sub);
158
0
  FREE(&a->name);
159
0
  notify_free(&a->notify);
160
161
0
  FREE(ptr);
162
0
}