NeoMutt  2020-11-20
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_OPEN = 1, NT_CONTEXT_CLOSE }
 Types of Context Event. More...
 

Functions

void ctx_free (struct Context **ptr)
 Free a Context. More...
 
int ctx_mailbox_observer (struct NotifyCallback *nc)
 Watch for changes affecting the Context - 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...
 
bool message_is_tagged (struct Context *ctx, 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_OPEN 

The Context has been opened.

NT_CONTEXT_CLOSE 

The Context is about to be destroyed.

Definition at line 59 of file context.h.

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

Function Documentation

◆ ctx_free()

void ctx_free ( struct Context **  ptr)

Free a Context.

Parameters
[out]ptrContext to free

Definition at line 50 of file context.c.

51 {
52  if (!ptr || !*ptr)
53  return;
54 
55  struct Context *ctx = *ptr;
56 
57  struct EventContext ev_ctx = { ctx };
59 
60  if (ctx->mailbox)
62 
64  notify_free(&ctx->notify);
65  FREE(&ctx->pattern);
67 
68  FREE(ptr);
69 }
The "current" mailbox.
Definition: context.h:38
int ctx_mailbox_observer(struct NotifyCallback *nc)
Watch for changes affecting the Context - Implements observer_t.
Definition: context.c:297
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:62
struct ThreadsContext * threads
Threads context.
Definition: context.h:43
struct Mailbox * mailbox
Definition: context.h:50
struct Notify * notify
Notifications handler.
Definition: context.h:51
struct PatternList * limit_pattern
Compiled limit pattern.
Definition: context.h:42
The Context is about to be destroyed.
Definition: context.h:62
Context has changed, NotifyContext, EventContext.
Definition: notify_type.h:38
#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:207
void mutt_thread_ctx_free(struct ThreadsContext **tctx)
Finalize a threading context.
Definition: mutt_thread.c:301
struct Notify * notify
Notifications handler.
Definition: mailbox.h:144
An Event that happened to an Context.
Definition: context.h:68
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:1004
char * pattern
Limit pattern string.
Definition: context.h:41
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
+ 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)

Watch for changes affecting the Context - Implements observer_t.

Definition at line 297 of file context.c.

298 {
299  if (!nc->global_data)
300  return -1;
301  if (nc->event_type != NT_MAILBOX)
302  return 0;
303 
304  struct Context *ctx = nc->global_data;
305 
306  switch (nc->event_subtype)
307  {
308  case NT_MAILBOX_CLOSED:
310  ctx_cleanup(ctx);
311  break;
312  case NT_MAILBOX_INVALID:
313  ctx_update(ctx);
314  break;
315  case NT_MAILBOX_UPDATE:
316  update_tables(ctx);
317  break;
318  case NT_MAILBOX_RESORT:
319  mutt_sort_headers(ctx->mailbox, ctx->threads, true, &ctx->vsize);
320  break;
321  }
322 
323  return 0;
324 }
The "current" mailbox.
Definition: context.h:38
Update internal tables.
Definition: mailbox.h:176
void mutt_sort_headers(struct Mailbox *m, struct ThreadsContext *threads, bool init, off_t *vsize)
Sort emails by their headers.
Definition: sort.c:366
struct ThreadsContext * threads
Threads context.
Definition: context.h:43
Email list was changed.
Definition: mailbox.h:173
Mailbox was closed.
Definition: mailbox.h:172
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:50
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:637
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:40
Email list needs resorting.
Definition: mailbox.h:174
static void ctx_cleanup(struct Context *ctx)
Release memory and initialize a Context object.
Definition: context.c:92
void ctx_update(struct Context *ctx)
Update the Context&#39;s message counts.
Definition: context.c:112
static void update_tables(struct Context *ctx)
Update a Context structure&#39;s internal tables.
Definition: context.c:207
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:42
+ 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 76 of file context.c.

77 {
78  struct Context *ctx = mutt_mem_calloc(1, sizeof(struct Context));
79 
80  ctx->notify = notify_new();
82  ctx->mailbox = m;
83  ctx->threads = mutt_thread_ctx_init(m);
84 
85  return ctx;
86 }
The "current" mailbox.
Definition: context.h:38
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct ThreadsContext * threads
Threads context.
Definition: context.h:43
Container for Accounts, Notifications.
Definition: neomutt.h:36
struct Mailbox * mailbox
Definition: context.h:50
struct Notify * notify
Notifications handler.
Definition: context.h:51
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:49
struct ThreadsContext * mutt_thread_ctx_init(struct Mailbox *m)
Initialize a threading context.
Definition: mutt_thread.c:288
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:82
+ 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 112 of file context.c.

113 {
114  if (!ctx || !ctx->mailbox)
115  return;
116 
117  struct Mailbox *m = ctx->mailbox;
118 
120  mutt_hash_free(&m->id_hash);
121 
122  /* reset counters */
123  m->msg_unread = 0;
124  m->msg_flagged = 0;
125  m->msg_new = 0;
126  m->msg_deleted = 0;
127  m->msg_tagged = 0;
128  m->vcount = 0;
129  m->changed = false;
130 
132 
133  struct Email *e = NULL;
134  for (int msgno = 0; msgno < m->msg_count; msgno++)
135  {
136  e = m->emails[msgno];
137  if (!e)
138  continue;
139 
140  if (WithCrypto)
141  {
142  /* NOTE: this _must_ be done before the check for mailcap! */
143  e->security = crypt_query(e->body);
144  }
145 
146  if (ctx_has_limit(ctx))
147  {
148  e->vnum = -1;
149  }
150  else
151  {
152  m->v2r[m->vcount] = msgno;
153  e->vnum = m->vcount++;
154  }
155  e->msgno = msgno;
156 
157  if (e->env->supersedes)
158  {
159  struct Email *e2 = NULL;
160 
161  if (!m->id_hash)
162  m->id_hash = mutt_make_id_hash(m);
163 
164  e2 = mutt_hash_find(m->id_hash, e->env->supersedes);
165  if (e2)
166  {
167  e2->superseded = true;
168  if (C_Score)
169  mutt_score_message(ctx->mailbox, e2, true);
170  }
171  }
172 
173  /* add this message to the hash tables */
174  if (m->id_hash && e->env->message_id)
176  if (m->subj_hash && e->env->real_subj)
178  mutt_label_hash_add(m, e);
179 
180  if (C_Score)
181  mutt_score_message(ctx->mailbox, e, false);
182 
183  if (e->changed)
184  m->changed = true;
185  if (e->flagged)
186  m->msg_flagged++;
187  if (e->deleted)
188  m->msg_deleted++;
189  if (e->tagged)
190  m->msg_tagged++;
191  if (!e->read)
192  {
193  m->msg_unread++;
194  if (!e->old)
195  m->msg_new++;
196  }
197  }
198 
199  /* rethread from scratch */
200  mutt_sort_headers(ctx->mailbox, ctx->threads, true, &ctx->vsize);
201 }
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
#define WithCrypto
Definition: lib.h:123
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:447
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:366
struct HashTable * mutt_make_id_hash(struct Mailbox *m)
Create a Hash Table for message-ids.
Definition: mutt_thread.c:1521
struct ThreadsContext * threads
Threads context.
Definition: context.h:43
WHERE bool C_Score
Config: Use message scoring.
Definition: mutt_globals.h:159
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:173
bool ctx_has_limit(const struct Context *ctx)
Is a limit active?
Definition: context.c:417
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:377
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:50
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:637
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:40
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
bool changed
Mailbox has been modified.
Definition: mailbox.h:114
SecurityFlags crypt_query(struct Body *m)
Check out the type of encryption used.
Definition: crypt.c:685
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 417 of file context.c.

418 {
419  return ctx && ctx->pattern;
420 }
char * pattern
Limit pattern string.
Definition: context.h:41
+ Here is the caller graph for this function:

◆ message_is_tagged()

bool message_is_tagged ( struct Context ctx,
struct Email e 
)

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

Parameters
ctxOpen mailbox
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 334 of file context.c.

335 {
336  return e->visible && e->tagged;
337 }
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 396 of file context.c.

397 {
398  if (!m || !m->emails || !m->v2r)
399  return NULL;
400 
401  if ((vnum < 0) || (vnum >= m->vcount))
402  return NULL;
403 
404  int inum = m->v2r[vnum];
405  if ((inum < 0) || (inum >= m->msg_count))
406  return NULL;
407 
408  return m->emails[inum];
409 }
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 348 of file context.c.

349 {
350  int count = 0;
351 
352  if (use_tagged)
353  {
354  if (!ctx || !ctx->mailbox || !ctx->mailbox->emails)
355  return -1;
356 
357  struct Mailbox *m = ctx->mailbox;
358  for (size_t i = 0; i < m->msg_count; i++)
359  {
360  e = m->emails[i];
361  if (!e)
362  break;
363  if (!message_is_tagged(ctx, e))
364  continue;
365 
366  struct EmailNode *en = mutt_mem_calloc(1, sizeof(*en));
367  en->email = e;
368  STAILQ_INSERT_TAIL(el, en, entries);
369  count++;
370  }
371  }
372  else
373  {
374  if (!e)
375  return -1;
376 
377  struct EmailNode *en = mutt_mem_calloc(1, sizeof(*en));
378  en->email = e;
379  STAILQ_INSERT_TAIL(el, en, entries);
380  count = 1;
381  }
382 
383  return count;
384 }
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
bool message_is_tagged(struct Context *ctx, struct Email *e)
Is a message in the index tagged (and within limit)
Definition: context.c:334
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:386
struct Mailbox * mailbox
Definition: context.h:50
A mailbox.
Definition: mailbox.h:81
struct Email * email
Email in the list.
Definition: email.h:127
List of Emails.
Definition: email.h:125
+ Here is the call graph for this function:
+ Here is the caller graph for this function: