NeoMutt  2018-07-16 +2225-8687db
Teaching an old dog new tricks
DOXYGEN
mailbox.c File Reference

Representation of a mailbox. More...

#include "config.h"
#include <sys/stat.h>
#include "config/lib.h"
#include "email/lib.h"
#include "mailbox.h"
#include "neomutt.h"

Go to the source code of this file.

Functions

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

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

struct Mailbox* mailbox_new ( void  )

Create a new Mailbox.

Return values
ptrNew Mailbox

Definition at line 42 of file mailbox.c.

43 {
44  struct Mailbox *m = mutt_mem_calloc(1, sizeof(struct Mailbox));
45 
46  m->pathbuf = mutt_buffer_new();
47  m->notify = notify_new(m, NT_MAILBOX);
48 
49  return m;
50 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct Notify * notify_new(void *object, enum NotifyType type)
Create a new notifications handler.
Definition: notify.c:54
A mailbox.
Definition: mailbox.h:93
struct Buffer * mutt_buffer_new(void)
Create and initialise a Buffer.
Definition: buffer.c:45
struct Buffer * pathbuf
Definition: mailbox.h:95
Mailbox has changed.
Definition: notify_type.h:35
struct Notify * notify
Notifications handler.
Definition: mailbox.h:151
void mailbox_free ( struct Mailbox **  ptr)

Free a Mailbox.

Parameters
[out]ptrMailbox to free

Definition at line 56 of file mailbox.c.

57 {
58  if (!ptr || !*ptr)
59  return;
60 
61  struct Mailbox *m = *ptr;
63  notify_free(&m->notify);
64 
66  FREE(&m->name);
67  if (m->mdata && m->free_mdata)
68  m->free_mdata(&m->mdata);
69  FREE(&m->realpath);
70  FREE(ptr);
71 }
void mailbox_changed(struct Mailbox *m, enum MailboxNotification action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:159
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:96
Mailbox was closed.
Definition: mailbox.h:64
void(* free_mdata)(void **)
Driver-specific data free function.
Definition: mailbox.h:149
char * name
A short name for the Mailbox.
Definition: mailbox.h:97
void mutt_buffer_free(struct Buffer **p)
Release a Buffer and its contents.
Definition: buffer.c:134
void * mdata
Driver specific data.
Definition: mailbox.h:148
A mailbox.
Definition: mailbox.h:93
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:69
struct Buffer * pathbuf
Definition: mailbox.h:95
#define FREE(x)
Definition: memory.h:40
struct Notify * notify
Notifications handler.
Definition: mailbox.h:151
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 78 of file mailbox.c.

79 {
80  if (!path)
81  return NULL;
82 
83  struct stat sb;
84  struct stat tmp_sb;
85 
86  if (stat(path, &sb) != 0)
87  return NULL;
88 
89  struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY);
90  struct MailboxNode *np = NULL;
91  struct Mailbox *m = NULL;
92  STAILQ_FOREACH(np, &ml, entries)
93  {
94  if ((stat(mutt_b2s(np->mailbox->pathbuf), &tmp_sb) == 0) &&
95  (sb.st_dev == tmp_sb.st_dev) && (sb.st_ino == tmp_sb.st_ino))
96  {
97  m = np->mailbox;
98  break;
99  }
100  }
102 
103  return m;
104 }
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:134
Match any Mailbox type.
Definition: mailbox.h:45
Container for Accounts, Notifications.
Definition: neomutt.h:35
struct MailboxList neomutt_mailboxlist_get_all(struct NeoMutt *n, enum MailboxType magic)
Get a List of all Mailboxes.
Definition: neomutt.c:156
#define mutt_b2s(buf)
Definition: buffer.h:42
A mailbox.
Definition: mailbox.h:93
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct Buffer * pathbuf
Definition: mailbox.h:95
List of Mailboxes.
Definition: mailbox.h:157
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:159
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 114 of file mailbox.c.

115 {
116  if (!name)
117  return NULL;
118 
119  struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY);
120  struct MailboxNode *np = NULL;
121  struct Mailbox *m = NULL;
122  STAILQ_FOREACH(np, &ml, entries)
123  {
124  if (mutt_str_strcmp(np->mailbox->name, name) == 0)
125  {
126  m = np->mailbox;
127  break;
128  }
129  }
131 
132  return m;
133 }
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:134
Match any Mailbox type.
Definition: mailbox.h:45
Container for Accounts, Notifications.
Definition: neomutt.h:35
char * name
A short name for the Mailbox.
Definition: mailbox.h:97
const char * name
Definition: pgpmicalg.c:45
struct MailboxList neomutt_mailboxlist_get_all(struct NeoMutt *n, enum MailboxType magic)
Get a List of all Mailboxes.
Definition: neomutt.c:156
A mailbox.
Definition: mailbox.h:93
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
List of Mailboxes.
Definition: mailbox.h:157
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:159
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 141 of file mailbox.c.

142 {
143  struct stat sb;
144 
145  if (!m)
146  return;
147 
148  if (stat(mutt_b2s(m->pathbuf), &sb) == 0)
149  m->size = (off_t) sb.st_size;
150  else
151  m->size = 0;
152 }
off_t size
Size of the Mailbox.
Definition: mailbox.h:99
#define mutt_b2s(buf)
Definition: buffer.h:42
struct Buffer * pathbuf
Definition: mailbox.h:95
void mailbox_changed ( struct Mailbox m,
enum MailboxNotification  action 
)

Notify observers of a change to a Mailbox.

Parameters
mMailbox
actionChange to Mailbox

Definition at line 159 of file mailbox.c.

160 {
161  if (!m)
162  return;
163 
164  notify_send(m->notify, NT_MAILBOX, action, 0);
165 }
Mailbox has changed.
Definition: notify_type.h:35
struct Notify * notify
Notifications handler.
Definition: mailbox.h:151
bool notify_send(struct Notify *notify, int type, int subtype, intptr_t data)
Send out a notification message.
Definition: notify.c:145
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 172 of file mailbox.c.

173 {
174  m->size += mutt_email_size(e);
175 }
off_t size
Size of the Mailbox.
Definition: mailbox.h:99
size_t mutt_email_size(const struct Email *e)
compute the size of an email
Definition: email.c:109
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 182 of file mailbox.c.

183 {
184  m->size -= mutt_email_size(e);
185 }
off_t size
Size of the Mailbox.
Definition: mailbox.h:99
size_t mutt_email_size(const struct Email *e)
compute the size of an email
Definition: email.c:109