Coverage Report

Created: 2025-04-22 06:17

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