NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
context.h File Reference

The "currently-open" mailbox. More...

#include <stdbool.h>
#include <sys/types.h>
+ Include dependency graph for context.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Context
 The "current" mailbox. More...
 
struct  EventContext
 An Event that happened to an Context. More...
 

Enumerations

enum  NotifyContext { NT_CONTEXT_ADD = 1, NT_CONTEXT_DELETE, NT_CONTEXT_CHANGE }
 Types of Context Event. More...
 

Functions

void ctx_free (struct Context **ptr)
 Free a Context. More...
 
int ctx_mailbox_observer (struct NotifyCallback *nc)
 Notification that a Mailbox has changed - Implements observer_t. More...
 
struct Contextctx_new (struct Mailbox *m)
 Create a new Context. More...
 
void ctx_update (struct Context *ctx)
 Update the Context's message counts. More...
 
bool ctx_has_limit (const struct Context *ctx)
 Is a limit active? More...
 
struct Mailboxctx_mailbox (struct Context *ctx)
 wrapper to get the mailbox in a Context, or NULL More...
 
bool message_is_tagged (struct Email *e)
 Is a message in the index tagged (and within limit) More...
 
struct Emailmutt_get_virt_email (struct Mailbox *m, int vnum)
 Get a virtual Email. More...
 
int el_add_tagged (struct EmailList *el, struct Context *ctx, struct Email *e, bool use_tagged)
 Get a list of the tagged Emails. More...
 

Detailed Description

The "currently-open" mailbox.

Authors
  • 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 context.h.

Enumeration Type Documentation

◆ NotifyContext

Types of Context Event.

Observers of NT_CONTEXT will be passed an EventContext.

Enumerator
NT_CONTEXT_ADD 

The Context has been opened.

NT_CONTEXT_DELETE 

The Context is about to be destroyed.

NT_CONTEXT_CHANGE 

The Context has changed.

Definition at line 58 of file context.h.

59 {
60  NT_CONTEXT_ADD = 1,
63 };
The Context has changed.
Definition: context.h:62
The Context has been opened.
Definition: context.h:60
The Context is about to be destroyed.
Definition: context.h:61

Function Documentation

◆ ctx_free()

void ctx_free ( struct Context **  ptr)

Free a Context.

Parameters
[out]ptrContext to free

Definition at line 49 of file context.c.

50 {
51  if (!ptr || !*ptr)
52  return;
53 
54  struct Context *ctx = *ptr;
55 
56  struct EventContext ev_c = { ctx };
57  mutt_debug(LL_NOTIFY, "NT_CONTEXT_DELETE: %p\n", ctx);
59 
60  if (ctx->mailbox)
62 
64  notify_free(&ctx->notify);
65  FREE(&ctx->pattern);
67 
68  FREE(&ctx);
69  *ptr = NULL;
70 }
The "current" mailbox.
Definition: context.h:37
int ctx_mailbox_observer(struct NotifyCallback *nc)
Notification that a Mailbox has changed - Implements observer_t.
Definition: context.c:313
Log of notifications.
Definition: logging.h:45
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
struct ThreadsContext * threads
Threads context.
Definition: context.h:42
struct Mailbox * mailbox
Definition: context.h:49
struct Notify * notify
Notifications: NotifyContext, EventContext.
Definition: context.h:50
struct PatternList * limit_pattern
Compiled limit pattern.
Definition: context.h:41
The Context is about to be destroyed.
Definition: context.h:61
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Context has changed, NotifyContext, EventContext.
Definition: notify_type.h:43
#define FREE(x)
Definition: memory.h:40
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
void mutt_thread_ctx_free(struct ThreadsContext **tctx)
Finalize a threading context.
Definition: mutt_thread.c:369
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:148
An Event that happened to an Context.
Definition: context.h:68
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:1038
char * pattern
Limit pattern string.
Definition: context.h:40
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:171
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ctx_mailbox_observer()

int ctx_mailbox_observer ( struct NotifyCallback nc)

Notification that a Mailbox has changed - Implements observer_t.

Definition at line 313 of file context.c.

314 {
315  if ((nc->event_type != NT_MAILBOX) || !nc->global_data)
316  return -1;
317 
318  struct Context *ctx = nc->global_data;
319 
320  switch (nc->event_subtype)
321  {
322  case NT_MAILBOX_DELETE:
324  ctx_cleanup(ctx);
325  break;
326  case NT_MAILBOX_INVALID:
327  ctx_update(ctx);
328  break;
329  case NT_MAILBOX_UPDATE:
330  update_tables(ctx);
331  break;
332  case NT_MAILBOX_RESORT:
333  mutt_sort_headers(ctx->mailbox, ctx->threads, true, &ctx->vsize);
334  break;
335  default:
336  return 0;
337  }
338 
339  mutt_debug(LL_DEBUG5, "mailbox done\n");
340  return 0;
341 }
The "current" mailbox.
Definition: context.h:37
Mailbox is about to be deleted.
Definition: mailbox.h:174
Update internal tables.
Definition: mailbox.h:183
void mutt_sort_headers(struct Mailbox *m, struct ThreadsContext *threads, bool init, off_t *vsize)
Sort emails by their headers.
Definition: sort.c:356
struct ThreadsContext * threads
Threads context.
Definition: context.h:42
Email list was changed.
Definition: mailbox.h:180
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:43
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
struct Mailbox * mailbox
Definition: context.h:49
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:714
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
off_t vsize
Size (in bytes) of the messages shown.
Definition: context.h:39
Email list needs resorting.
Definition: mailbox.h:181
static void ctx_cleanup(struct Context *ctx)
Release memory and initialize a Context object.
Definition: context.c:105
void ctx_update(struct Context *ctx)
Update the Context&#39;s message counts.
Definition: context.c:125
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
static void update_tables(struct Context *ctx)
Update a Context structure&#39;s internal tables.
Definition: context.c:221
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:48
Log at debug level 5.
Definition: logging.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ctx_new()

struct Context* ctx_new ( struct Mailbox m)

Create a new Context.

Parameters
mMailbox
Return values
ptrNew Context

Definition at line 77 of file context.c.

78 {
79  if (!m)
80  return NULL;
81 
82  struct Context *ctx = mutt_mem_calloc(1, sizeof(struct Context));
83 
84  ctx->notify = notify_new();
86  struct EventContext ev_c = { ctx };
87  mutt_debug(LL_NOTIFY, "NT_CONTEXT_ADD: %p\n", ctx);
89  // If the Mailbox is closed, ctx->mailbox must be set to NULL
91 
92  ctx->mailbox = m;
93  ctx->threads = mutt_thread_ctx_init(m);
94  ctx->msg_in_pager = -1;
95  ctx->collapsed = false;
96  ctx_update(ctx);
97 
98  return ctx;
99 }
The "current" mailbox.
Definition: context.h:37
int ctx_mailbox_observer(struct NotifyCallback *nc)
Notification that a Mailbox has changed - Implements observer_t.
Definition: context.c:313
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct Context * ctx
The Context this Event relates to.
Definition: context.h:70
int msg_in_pager
Message currently shown in the pager.
Definition: context.h:43
Log of notifications.
Definition: logging.h:45
struct ThreadsContext * threads
Threads context.
Definition: context.h:42
Container for Accounts, Notifications.
Definition: neomutt.h:36
struct Mailbox * mailbox
Definition: context.h:49
struct Notify * notify
Notifications: NotifyContext, EventContext.
Definition: context.h:50
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:189
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
The Context has been opened.
Definition: context.h:60
void ctx_update(struct Context *ctx)
Update the Context&#39;s message counts.
Definition: context.c:125
struct ThreadsContext * mutt_thread_ctx_init(struct Mailbox *m)
Initialize a threading context.
Definition: mutt_thread.c:356
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Context has changed, NotifyContext, EventContext.
Definition: notify_type.h:43
bool collapsed
Are all threads collapsed?
Definition: context.h:47
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:48
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:93
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:148
An Event that happened to an Context.
Definition: context.h:68
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:171
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ctx_update()

void ctx_update ( struct Context ctx)

Update the Context's message counts.

Parameters
ctxMailbox

this routine is called to update the counts in the context structure

Definition at line 125 of file context.c.

126 {
127  if (!ctx || !ctx->mailbox)
128  return;
129 
130  struct Mailbox *m = ctx->mailbox;
131 
133  mutt_hash_free(&m->id_hash);
134 
135  /* reset counters */
136  m->msg_unread = 0;
137  m->msg_flagged = 0;
138  m->msg_new = 0;
139  m->msg_deleted = 0;
140  m->msg_tagged = 0;
141  m->vcount = 0;
142  m->changed = false;
143 
145 
146  const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
147  struct Email *e = NULL;
148  for (int msgno = 0; msgno < m->msg_count; msgno++)
149  {
150  e = m->emails[msgno];
151  if (!e)
152  continue;
153 
154  if (WithCrypto)
155  {
156  /* NOTE: this _must_ be done before the check for mailcap! */
157  e->security = crypt_query(e->body);
158  }
159 
160  if (ctx_has_limit(ctx))
161  {
162  e->vnum = -1;
163  }
164  else
165  {
166  m->v2r[m->vcount] = msgno;
167  e->vnum = m->vcount++;
168  }
169  e->msgno = msgno;
170 
171  if (e->env->supersedes)
172  {
173  struct Email *e2 = NULL;
174 
175  if (!m->id_hash)
176  m->id_hash = mutt_make_id_hash(m);
177 
178  e2 = mutt_hash_find(m->id_hash, e->env->supersedes);
179  if (e2)
180  {
181  e2->superseded = true;
182  if (c_score)
183  mutt_score_message(ctx->mailbox, e2, true);
184  }
185  }
186 
187  /* add this message to the hash tables */
188  if (m->id_hash && e->env->message_id)
190  if (m->subj_hash && e->env->real_subj)
192  mutt_label_hash_add(m, e);
193 
194  if (c_score)
195  mutt_score_message(ctx->mailbox, e, false);
196 
197  if (e->changed)
198  m->changed = true;
199  if (e->flagged)
200  m->msg_flagged++;
201  if (e->deleted)
202  m->msg_deleted++;
203  if (e->tagged)
204  m->msg_tagged++;
205  if (!e->read)
206  {
207  m->msg_unread++;
208  if (!e->old)
209  m->msg_new++;
210  }
211  }
212 
213  /* rethread from scratch */
214  mutt_sort_headers(ctx->mailbox, ctx->threads, true, &ctx->vsize);
215 }
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition: hash.c:354
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:327
int msg_count
Total number of messages.
Definition: mailbox.h:91
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
#define WithCrypto
Definition: lib.h:113
The envelope/body of an email.
Definition: email.h:37
struct Body * body
List of MIME parts.
Definition: email.h:91
int msg_deleted
Number of deleted messages.
Definition: mailbox.h:96
int msg_unread
Number of unread messages.
Definition: mailbox.h:92
char * supersedes
Supersedes header.
Definition: envelope.h:70
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:449
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:93
void mutt_sort_headers(struct Mailbox *m, struct ThreadsContext *threads, bool init, off_t *vsize)
Sort emails by their headers.
Definition: sort.c:356
struct HashTable * mutt_make_id_hash(struct Mailbox *m)
Create a Hash Table for message-ids.
Definition: mutt_thread.c:1658
struct ThreadsContext * threads
Threads context.
Definition: context.h:42
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:161
bool ctx_has_limit(const struct Context *ctx)
Is a limit active?
Definition: context.c:433
char * real_subj
Offset of the real subject.
Definition: envelope.h:67
bool changed
Email has been edited.
Definition: email.h:48
void mutt_label_hash_add(struct Mailbox *m, struct Email *e)
Add a message&#39;s labels to the Hash Table.
Definition: mutt_header.c:380
Container for Accounts, Notifications.
Definition: neomutt.h:36
int vcount
The number of virtual messages.
Definition: mailbox.h:102
bool tagged
Email is tagged.
Definition: email.h:44
bool read
Email is read.
Definition: email.h:51
char * message_id
Message ID.
Definition: envelope.h:69
struct Mailbox * mailbox
Definition: context.h:49
struct HashTable * id_hash
Hash Table by msg id.
Definition: mailbox.h:127
bool old
Email is seen, but unread.
Definition: email.h:50
struct Envelope * env
Envelope information.
Definition: email.h:90
bool superseded
Got superseded?
Definition: email.h:53
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:714
struct HashTable * subj_hash
Hash Table by subject.
Definition: mailbox.h:128
off_t vsize
Size (in bytes) of the messages shown.
Definition: context.h:39
A mailbox.
Definition: mailbox.h:81
int msg_tagged
How many messages are tagged?
Definition: mailbox.h:97
int vnum
Virtual message number.
Definition: email.h:88
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib...
Definition: email.h:39
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:101
bool flagged
Marked important?
Definition: email.h:43
int msg_new
Number of new messages.
Definition: mailbox.h:95
bool deleted
Email is deleted.
Definition: email.h:45
SecurityFlags crypt_query(struct Body *b)
Check out the type of encryption used.
Definition: crypt.c:698
bool changed
Mailbox has been modified.
Definition: mailbox.h:114
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
int msgno
Number displayed to the user.
Definition: email.h:87
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ctx_has_limit()

bool ctx_has_limit ( const struct Context ctx)

Is a limit active?

Parameters
ctxContext
Return values
trueA limit is active
falseNo limit is active

Definition at line 433 of file context.c.

434 {
435  return ctx && ctx->pattern;
436 }
char * pattern
Limit pattern string.
Definition: context.h:40
+ Here is the caller graph for this function:

◆ ctx_mailbox()

struct Mailbox* ctx_mailbox ( struct Context ctx)

wrapper to get the mailbox in a Context, or NULL

Parameters
ctxContext
Return values
ptrThe mailbox in the Context
NULLContext is NULL or doesn't have a mailbox

Definition at line 444 of file context.c.

445 {
446  return ctx ? ctx->mailbox : NULL;
447 }
struct Mailbox * mailbox
Definition: context.h:49
+ Here is the caller graph for this function:

◆ message_is_tagged()

bool message_is_tagged ( struct Email e)

Is a message in the index tagged (and within limit)

Parameters
eEmail
Return values
trueThe message is both tagged and within limit

If a limit is in effect, the message must be visible within it.

Definition at line 350 of file context.c.

351 {
352  return e->visible && e->tagged;
353 }
bool tagged
Email is tagged.
Definition: email.h:44
bool visible
Is this message part of the view?
Definition: email.h:74
+ Here is the caller graph for this function:

◆ mutt_get_virt_email()

struct Email* mutt_get_virt_email ( struct Mailbox m,
int  vnum 
)

Get a virtual Email.

Parameters
mMailbox
vnumVirtual index number
Return values
ptrEmail
NULLNo Email selected, or bad index values

This safely gets the result of the following:

  • mailbox->emails[mailbox->v2r[vnum]]

Definition at line 412 of file context.c.

413 {
414  if (!m || !m->emails || !m->v2r)
415  return NULL;
416 
417  if ((vnum < 0) || (vnum >= m->vcount))
418  return NULL;
419 
420  int inum = m->v2r[vnum];
421  if ((inum < 0) || (inum >= m->msg_count))
422  return NULL;
423 
424  return m->emails[inum];
425 }
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
int msg_count
Total number of messages.
Definition: mailbox.h:91
int vcount
The number of virtual messages.
Definition: mailbox.h:102
int vnum
Virtual message number.
Definition: email.h:88
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:101
+ Here is the caller graph for this function:

◆ el_add_tagged()

int el_add_tagged ( struct EmailList *  el,
struct Context ctx,
struct Email e,
bool  use_tagged 
)

Get a list of the tagged Emails.

Parameters
elEmpty EmailList to populate
ctxCurrent Mailbox
eCurrent Email
use_taggedUse tagged Emails
Return values
numNumber of selected emails
-1Error

Definition at line 364 of file context.c.

365 {
366  int count = 0;
367 
368  if (use_tagged)
369  {
370  if (!ctx || !ctx->mailbox || !ctx->mailbox->emails)
371  return -1;
372 
373  struct Mailbox *m = ctx->mailbox;
374  for (size_t i = 0; i < m->msg_count; i++)
375  {
376  e = m->emails[i];
377  if (!e)
378  break;
379  if (!message_is_tagged(e))
380  continue;
381 
382  struct EmailNode *en = mutt_mem_calloc(1, sizeof(*en));
383  en->email = e;
384  STAILQ_INSERT_TAIL(el, en, entries);
385  count++;
386  }
387  }
388  else
389  {
390  if (!e)
391  return -1;
392 
393  struct EmailNode *en = mutt_mem_calloc(1, sizeof(*en));
394  en->email = e;
395  STAILQ_INSERT_TAIL(el, en, entries);
396  count = 1;
397  }
398 
399  return count;
400 }
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
int msg_count
Total number of messages.
Definition: mailbox.h:91
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
struct Mailbox * mailbox
Definition: context.h:49
bool message_is_tagged(struct Email *e)
Is a message in the index tagged (and within limit)
Definition: context.c:350
A mailbox.
Definition: mailbox.h:81
struct Email * email
Email in the list.
Definition: email.h:131
List of Emails.
Definition: email.h:129
+ Here is the call graph for this function:
+ Here is the caller graph for this function: