NeoMutt  2021-02-05-329-g9e03b7
Teaching an old dog new tricks
DOXYGEN
mailbox.c File Reference

Representation of a mailbox. More...

#include "config.h"
#include <assert.h>
#include <sys/stat.h>
#include "config/lib.h"
#include "email/lib.h"
#include "mailbox.h"
#include "neomutt.h"
+ Include dependency graph for mailbox.c:

Go to the source code of this file.

Functions

int mailbox_gen (void)
 Get the next generation number. More...
 
struct Mailboxmailbox_new (void)
 Create a new Mailbox. More...
 
void mailbox_free (struct Mailbox **ptr)
 Free a Mailbox. More...
 
struct Mailboxmailbox_find (const char *path)
 Find the mailbox with a given path. More...
 
struct Mailboxmailbox_find_name (const char *name)
 Find the mailbox with a given name. More...
 
void mailbox_update (struct Mailbox *m)
 Get the mailbox's current size. More...
 
void mailbox_changed (struct Mailbox *m, enum NotifyMailbox action)
 Notify observers of a change to a Mailbox. More...
 
void mailbox_size_add (struct Mailbox *m, const struct Email *e)
 Add an email's size to the total size of a Mailbox. More...
 
void mailbox_size_sub (struct Mailbox *m, const struct Email *e)
 Subtract an email's size from the total size of a Mailbox. More...
 
bool mailbox_set_subset (struct Mailbox *m, struct ConfigSubset *sub)
 Set a Mailbox's Config Subset. More...
 
void mailbox_gc_add (struct Email *e)
 Add an Email to the garbage-collection set. More...
 
void mailbox_gc_run (void)
 

Variables

struct {
   struct Email *   arr [10]
 
   size_t   idx
 
gc = { 0 }
 

Detailed Description

Representation of a mailbox.

Authors
  • Michael R. Elkins
  • Kevin J. McCarthy
  • Richard Russon

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/.

Definition in file mailbox.c.

Function Documentation

◆ mailbox_gen()

int mailbox_gen ( void  )

Get the next generation number.

Definition at line 42 of file mailbox.c.

43 {
44  static int gen = 0;
45  return gen++;
46 }
+ Here is the caller graph for this function:

◆ mailbox_new()

struct Mailbox* mailbox_new ( void  )

Create a new Mailbox.

Return values
ptrNew Mailbox

Definition at line 52 of file mailbox.c.

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 }
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
The envelope/body of an email.
Definition: email.h:37
int mailbox_gen(void)
Get the next generation number.
Definition: mailbox.c:42
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:49
int email_max
Number of pointers in emails.
Definition: mailbox.h:100
A mailbox.
Definition: mailbox.h:81
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:101
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:144
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
struct Buffer pathbuf
Definition: mailbox.h:83
int gen
Generation number, for sorting.
Definition: mailbox.h:146
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_free()

void mailbox_free ( struct Mailbox **  ptr)

Free a Mailbox.

Parameters
[out]ptrMailbox to free

Definition at line 71 of file mailbox.c.

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 }
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:44
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:62
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:84
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:93
Mailbox was closed.
Definition: mailbox.h:172
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 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
int email_max
Number of pointers in emails.
Definition: mailbox.h:100
A mailbox.
Definition: mailbox.h:81
struct ConfigSubset * sub
Inherited config items.
Definition: mailbox.h:86
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:101
void mailbox_gc_run(void)
Definition: mailbox.c:255
#define FREE(x)
Definition: memory.h:40
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:144
struct Buffer pathbuf
Definition: mailbox.h:83
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:186
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_find()

struct Mailbox* mailbox_find ( const char *  path)

Find the mailbox with a given path.

Parameters
pathPath to match
Return values
ptrMatching Mailbox

Definition at line 103 of file mailbox.c.

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 }
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:206
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:137
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
Container for Accounts, Notifications.
Definition: neomutt.h:36
A mailbox.
Definition: mailbox.h:81
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:321
List of Mailboxes.
Definition: mailbox.h:152
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:154
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_find_name()

struct Mailbox* mailbox_find_name ( const char *  name)

Find the mailbox with a given name.

Parameters
nameName to match
Return values
ptrMatching Mailbox
NULLNo matching mailbox found
Note
This searches across all Accounts

Definition at line 140 of file mailbox.c.

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 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:137
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
Container for Accounts, Notifications.
Definition: neomutt.h:36
char * name
A short name for the Mailbox.
Definition: mailbox.h:85
A mailbox.
Definition: mailbox.h:81
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:321
List of Mailboxes.
Definition: mailbox.h:152
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:154
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_update()

void mailbox_update ( struct Mailbox m)

Get the mailbox's current size.

Parameters
mMailbox to check
Note
Only applies to local Mailboxes

Definition at line 168 of file mailbox.c.

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 }
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:206
off_t size
Size of the Mailbox.
Definition: mailbox.h:87
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_changed()

void mailbox_changed ( struct Mailbox m,
enum NotifyMailbox  action 
)

Notify observers of a change to a Mailbox.

Parameters
mMailbox
actionChange to Mailbox

Definition at line 186 of file mailbox.c.

187 {
188  if (!m)
189  return;
190 
191  struct EventMailbox ev_m = { m };
192  notify_send(m->notify, NT_MAILBOX, action, &ev_m);
193 }
An Event that happened to a Mailbox.
Definition: mailbox.h:183
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:47
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:144
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:156
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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.

Parameters
mMailbox
eEmail

Definition at line 200 of file mailbox.c.

201 {
202  m->size += email_size(e);
203 }
off_t size
Size of the Mailbox.
Definition: mailbox.h:87
size_t email_size(const struct Email *e)
compute the size of an email
Definition: email.c:124
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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.

Parameters
mMailbox
eEmail

Definition at line 210 of file mailbox.c.

211 {
212  m->size -= email_size(e);
213 }
off_t size
Size of the Mailbox.
Definition: mailbox.h:87
size_t email_size(const struct Email *e)
compute the size of an email
Definition: email.c:124
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_set_subset()

bool mailbox_set_subset ( struct Mailbox m,
struct ConfigSubset sub 
)

Set a Mailbox's Config Subset.

Parameters
mMailbox
subParent Config Subset
Return values
trueSuccess

Definition at line 221 of file mailbox.c.

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 }
char * name
A short name for the Mailbox.
Definition: mailbox.h:85
struct ConfigSubset * sub
Inherited config items.
Definition: mailbox.h:86
enum ConfigScope scope
Scope of Subset, e.g. SET_SCOPE_ACCOUNT.
Definition: subset.h:49
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
This Config is Mailbox-specific.
Definition: subset.h:40
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:144
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_gc_add()

void mailbox_gc_add ( struct Email e)

Add an Email to the garbage-collection set.

Parameters
eEmail
Precondition
e != NULL

Definition at line 242 of file mailbox.c.

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 }
static struct @0 gc
#define mutt_array_size(x)
Definition: memory.h:33
void mailbox_gc_run(void)
Definition: mailbox.c:255
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_gc_run()

void mailbox_gc_run ( void  )

Definition at line 255 of file mailbox.c.

256 {
257  for (size_t i = 0; i < gc.idx; i++)
258  {
259  email_free(&gc.arr[i]);
260  }
261  gc.idx = 0;
262 }
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:44
static struct @0 gc
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ arr

struct Email* arr[10]

Definition at line 233 of file mailbox.c.

◆ idx

size_t idx

Definition at line 234 of file mailbox.c.

◆ gc

struct { ... } gc