NeoMutt  2021-02-05-89-gabe350
Teaching an old dog new tricks
DOXYGEN
mailbox.c
Go to the documentation of this file.
1 
31 #include "config.h"
32 #include <assert.h>
33 #include <sys/stat.h>
34 #include "config/lib.h"
35 #include "email/lib.h"
36 #include "mailbox.h"
37 #include "neomutt.h"
38 
42 int mailbox_gen(void)
43 {
44  static int gen = 0;
45  return gen++;
46 }
47 
52 struct Mailbox *mailbox_new(void)
53 {
54  struct Mailbox *m = mutt_mem_calloc(1, sizeof(struct Mailbox));
55 
57  m->notify = notify_new();
58 
59  m->email_max = 25;
60  m->emails = mutt_mem_calloc(m->email_max, sizeof(struct Email *));
61  m->v2r = mutt_mem_calloc(m->email_max, sizeof(int));
62  m->gen = mailbox_gen();
63 
64  return m;
65 }
66 
71 void mailbox_free(struct Mailbox **ptr)
72 {
73  if (!ptr || !*ptr)
74  return;
75 
76  struct Mailbox *m = *ptr;
77 
79 
80  if (m->mdata && m->mdata_free)
81  m->mdata_free(&m->mdata);
82 
83  for (size_t i = 0; i < m->email_max; i++)
84  email_free(&m->emails[i]);
85 
87  cs_subset_free(&m->sub);
88  FREE(&m->name);
89  FREE(&m->realpath);
90  FREE(&m->emails);
91  FREE(&m->v2r);
92  notify_free(&m->notify);
94 
95  FREE(ptr);
96 }
97 
103 struct Mailbox *mailbox_find(const char *path)
104 {
105  if (!path)
106  return NULL;
107 
108  struct stat sb;
109  struct stat tmp_sb;
110 
111  if (stat(path, &sb) != 0)
112  return NULL;
113 
114  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
116  struct MailboxNode *np = NULL;
117  struct Mailbox *m = NULL;
118  STAILQ_FOREACH(np, &ml, entries)
119  {
120  if ((stat(mailbox_path(np->mailbox), &tmp_sb) == 0) &&
121  (sb.st_dev == tmp_sb.st_dev) && (sb.st_ino == tmp_sb.st_ino))
122  {
123  m = np->mailbox;
124  break;
125  }
126  }
128 
129  return m;
130 }
131 
140 struct Mailbox *mailbox_find_name(const char *name)
141 {
142  if (!name)
143  return NULL;
144 
145  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
147  struct MailboxNode *np = NULL;
148  struct Mailbox *m = NULL;
149  STAILQ_FOREACH(np, &ml, entries)
150  {
151  if (mutt_str_equal(np->mailbox->name, name))
152  {
153  m = np->mailbox;
154  break;
155  }
156  }
158 
159  return m;
160 }
161 
168 void mailbox_update(struct Mailbox *m)
169 {
170  struct stat sb;
171 
172  if (!m)
173  return;
174 
175  if (stat(mailbox_path(m), &sb) == 0)
176  m->size = (off_t) sb.st_size;
177  else
178  m->size = 0;
179 }
180 
186 void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
187 {
188  if (!m)
189  return;
190 
191  struct EventMailbox ev_m = { m };
192  notify_send(m->notify, NT_MAILBOX, action, &ev_m);
193 }
194 
200 void mailbox_size_add(struct Mailbox *m, const struct Email *e)
201 {
202  m->size += email_size(e);
203 }
204 
210 void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
211 {
212  m->size -= email_size(e);
213 }
214 
221 bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
222 {
223  if (!m || m->sub || !sub)
224  return false;
225 
226  m->sub = cs_subset_new(m->name, sub, m->notify);
228  return true;
229 }
230 
231 static struct
232 {
233  struct Email *arr[10];
234  size_t idx;
235 } gc = { 0 };
236 
242 void mailbox_gc_add(struct Email *e)
243 {
244  assert(e);
245  if (gc.idx == mutt_array_size(gc.arr))
246  {
247  mailbox_gc_run();
248  }
249  gc.arr[gc.idx++] = e;
250 }
251 
252 /*
253  * mailbox_gc_run - Run the garbage-collection
254  */
255 void mailbox_gc_run(void)
256 {
257  for (size_t i = 0; i < gc.idx; i++)
258  {
259  email_free(&gc.arr[i]);
260  }
261  gc.idx = 0;
262 }
Mailbox::mdata_free
void(* mdata_free)(void **ptr)
Free the private data attached to the Mailbox.
Definition: mailbox.h:142
SET_SCOPE_MAILBOX
@ SET_SCOPE_MAILBOX
This Config is Mailbox-specific.
Definition: subset.h:40
MailboxNode
List of Mailboxes.
Definition: mailbox.h:152
mutt_mem_calloc
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
Mailbox
A mailbox.
Definition: mailbox.h:81
Mailbox::v2r
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:101
Mailbox::emails
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
mailbox_size_add
void mailbox_size_add(struct Mailbox *m, const struct Email *e)
Add an email's size to the total size of a Mailbox.
Definition: mailbox.c:200
email_size
size_t email_size(const struct Email *e)
compute the size of an email
Definition: email.c:117
mailbox_gen
int mailbox_gen(void)
Get the next generation number.
Definition: mailbox.c:42
mutt_buffer_dealloc
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
Mailbox::notify
struct Notify * notify
Notifications handler.
Definition: mailbox.h:144
arr
struct Email * arr[10]
Definition: mailbox.c:233
mutt_buffer_init
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
neomutt_mailboxlist_get_all
size_t neomutt_mailboxlist_get_all(struct MailboxList *head, struct NeoMutt *n, enum MailboxType type)
Get a List of all Mailboxes.
Definition: neomutt.c:160
NotifyMailbox
NotifyMailbox
Types of Mailbox Event.
Definition: mailbox.h:164
Mailbox::size
off_t size
Size of the Mailbox.
Definition: mailbox.h:87
FREE
#define FREE(x)
Definition: memory.h:40
MailboxNode::mailbox
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:154
mailbox.h
mailbox_free
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:71
mutt_array_size
#define mutt_array_size(x)
Definition: memory.h:33
STAILQ_FOREACH
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
notify_send
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:152
mutt_str_equal
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
notify_new
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:49
EventMailbox
An Event that happened to a Mailbox.
Definition: mailbox.h:183
lib.h
cs_subset_free
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:93
NT_MAILBOX
@ NT_MAILBOX
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:42
Mailbox::mdata
void * mdata
Driver specific data.
Definition: mailbox.h:136
Mailbox::gen
int gen
Generation number, for sorting.
Definition: mailbox.h:146
Mailbox::name
char * name
A short name for the Mailbox.
Definition: mailbox.h:85
MUTT_MAILBOX_ANY
@ MUTT_MAILBOX_ANY
Match any Mailbox type.
Definition: mailbox.h:45
neomutt.h
Mailbox::email_max
int email_max
Number of pointers in emails.
Definition: mailbox.h:100
mailbox_update
void mailbox_update(struct Mailbox *m)
Get the mailbox's current size.
Definition: mailbox.c:168
mailbox_new
struct Mailbox * mailbox_new(void)
Create a new Mailbox.
Definition: mailbox.c:52
Mailbox::realpath
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:84
cs_subset_new
struct ConfigSubset * cs_subset_new(const char *name, struct ConfigSubset *sub_parent, struct Notify *not_parent)
Create a new Config Subset.
Definition: subset.c:133
lib.h
STAILQ_HEAD_INITIALIZER
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:321
mailbox_find_name
struct Mailbox * mailbox_find_name(const char *name)
Find the mailbox with a given name.
Definition: mailbox.c:140
gc
static struct @0 gc
NeoMutt
Container for Accounts, Notifications.
Definition: neomutt.h:36
notify_free
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:62
mailbox_changed
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:186
ConfigSubset::scope
enum ConfigScope scope
Scope of Subset, e.g. SET_SCOPE_ACCOUNT.
Definition: subset.h:49
mailbox_size_sub
void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
Subtract an email's size from the total size of a Mailbox.
Definition: mailbox.c:210
mailbox_gc_add
void mailbox_gc_add(struct Email *e)
Add an Email to the garbage-collection set.
Definition: mailbox.c:242
email_free
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:43
mailbox_path
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:206
ConfigSubset
A set of inherited config items.
Definition: subset.h:46
Mailbox::sub
struct ConfigSubset * sub
Inherited config items.
Definition: mailbox.h:86
Email
The envelope/body of an email.
Definition: email.h:37
mailbox_gc_run
void mailbox_gc_run(void)
Definition: mailbox.c:255
NT_MAILBOX_CLOSED
@ NT_MAILBOX_CLOSED
Mailbox was closed.
Definition: mailbox.h:172
mailbox_set_subset
bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
Set a Mailbox's Config Subset.
Definition: mailbox.c:221
neomutt_mailboxlist_clear
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:137
Mailbox::pathbuf
struct Buffer pathbuf
Definition: mailbox.h:83
mailbox_find
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:103
idx
size_t idx
Definition: mailbox.c:234