NeoMutt  2020-11-20
Teaching an old dog new tricks
DOXYGEN
mailbox.c
Go to the documentation of this file.
1 
31 #include "config.h"
32 #include <sys/stat.h>
33 #include "config/lib.h"
34 #include "email/lib.h"
35 #include "mailbox.h"
36 #include "neomutt.h"
37 
42 struct Mailbox *mailbox_new(void)
43 {
44  static int gen = 0;
45 
46  struct Mailbox *m = mutt_mem_calloc(1, sizeof(struct Mailbox));
47 
49  m->notify = notify_new();
50 
51  m->email_max = 25;
52  m->emails = mutt_mem_calloc(m->email_max, sizeof(struct Email *));
53  m->v2r = mutt_mem_calloc(m->email_max, sizeof(int));
54  m->gen = gen++;
55 
56  return m;
57 }
58 
63 void mailbox_free(struct Mailbox **ptr)
64 {
65  if (!ptr || !*ptr)
66  return;
67 
68  struct Mailbox *m = *ptr;
69 
71 
72  if (m->mdata && m->mdata_free)
73  m->mdata_free(&m->mdata);
74 
75  for (size_t i = 0; i < m->email_max; i++)
76  email_free(&m->emails[i]);
77 
79  cs_subset_free(&m->sub);
80  FREE(&m->name);
81  FREE(&m->realpath);
82  FREE(&m->emails);
83  FREE(&m->v2r);
84  notify_free(&m->notify);
85 
86  FREE(ptr);
87 }
88 
94 struct Mailbox *mailbox_find(const char *path)
95 {
96  if (!path)
97  return NULL;
98 
99  struct stat sb;
100  struct stat tmp_sb;
101 
102  if (stat(path, &sb) != 0)
103  return NULL;
104 
105  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
107  struct MailboxNode *np = NULL;
108  struct Mailbox *m = NULL;
109  STAILQ_FOREACH(np, &ml, entries)
110  {
111  if ((stat(mailbox_path(np->mailbox), &tmp_sb) == 0) &&
112  (sb.st_dev == tmp_sb.st_dev) && (sb.st_ino == tmp_sb.st_ino))
113  {
114  m = np->mailbox;
115  break;
116  }
117  }
119 
120  return m;
121 }
122 
131 struct Mailbox *mailbox_find_name(const char *name)
132 {
133  if (!name)
134  return NULL;
135 
136  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
138  struct MailboxNode *np = NULL;
139  struct Mailbox *m = NULL;
140  STAILQ_FOREACH(np, &ml, entries)
141  {
142  if (mutt_str_equal(np->mailbox->name, name))
143  {
144  m = np->mailbox;
145  break;
146  }
147  }
149 
150  return m;
151 }
152 
159 void mailbox_update(struct Mailbox *m)
160 {
161  struct stat sb;
162 
163  if (!m)
164  return;
165 
166  if (stat(mailbox_path(m), &sb) == 0)
167  m->size = (off_t) sb.st_size;
168  else
169  m->size = 0;
170 }
171 
177 void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
178 {
179  if (!m)
180  return;
181 
182  struct EventMailbox ev_m = { m };
183  notify_send(m->notify, NT_MAILBOX, action, &ev_m);
184 }
185 
191 void mailbox_size_add(struct Mailbox *m, const struct Email *e)
192 {
193  m->size += email_size(e);
194 }
195 
201 void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
202 {
203  m->size -= email_size(e);
204 }
205 
212 bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
213 {
214  if (!m || m->sub || !sub)
215  return false;
216 
217  m->sub = cs_subset_new(m->name, sub, m->notify);
219  return true;
220 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:203
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
off_t size
Size of the Mailbox.
Definition: mailbox.h:87
NotifyMailbox
Types of Mailbox Event.
Definition: mailbox.h:164
The envelope/body of an email.
Definition: email.h:37
void mailbox_size_add(struct Mailbox *m, const struct Email *e)
Add an email&#39;s size to the total size of a Mailbox.
Definition: mailbox.c:191
Structs that make up an email.
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:137
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:62
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:94
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:84
Match any Mailbox type.
Definition: mailbox.h:45
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
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:93
An Event that happened to a Mailbox.
Definition: mailbox.h:183
Container for Accounts, Notifications.
Definition: neomutt.h:36
Mailbox was closed.
Definition: mailbox.h:172
Convenience wrapper for the config headers.
void(* mdata_free)(void **ptr)
Free the private data attached to the Mailbox.
Definition: mailbox.h:142
char * name
A short name for the Mailbox.
Definition: mailbox.h:85
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:63
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
void * mdata
Driver specific data.
Definition: mailbox.h:136
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:49
A set of inherited config items.
Definition: subset.h:46
int email_max
Number of pointers in emails.
Definition: mailbox.h:100
struct Mailbox * mailbox_find_name(const char *name)
Find the mailbox with a given name.
Definition: mailbox.c:131
A mailbox.
Definition: mailbox.h:81
struct ConfigSubset * sub
Inherited config items.
Definition: mailbox.h:86
Container for Accounts, Notifications.
void mailbox_update(struct Mailbox *m)
Get the mailbox&#39;s current size.
Definition: mailbox.c:159
enum ConfigScope scope
Scope of Subset, e.g. SET_SCOPE_ACCOUNT.
Definition: subset.h:49
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
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
Representation of a mailbox.
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:101
struct Mailbox * mailbox_new(void)
Create a new Mailbox.
Definition: mailbox.c:42
bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
Set a Mailbox&#39;s Config Subset.
Definition: mailbox.c:212
#define FREE(x)
Definition: memory.h:40
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:42
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:321
This Config is Mailbox-specific.
Definition: subset.h:40
List of Mailboxes.
Definition: mailbox.h:152
struct Notify * notify
Notifications handler.
Definition: mailbox.h:144
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
Subtract an email&#39;s size from the total size of a Mailbox.
Definition: mailbox.c:201
struct Buffer pathbuf
Definition: mailbox.h:83
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:43
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:177
int gen
Generation number, for sorting.
Definition: mailbox.h:146
size_t email_size(const struct Email *e)
compute the size of an email
Definition: email.c:117
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:154
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