NeoMutt  2023-05-17-33-gce4425
Teaching an old dog new tricks
DOXYGEN
mview.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <string.h>
31#include "mutt/lib.h"
32#include "config/lib.h"
33#include "email/lib.h"
34#include "core/lib.h"
35#include "mview.h"
36#include "imap/lib.h"
37#include "ncrypt/lib.h"
38#include "pattern/lib.h"
39#include "mutt_header.h"
40#include "mutt_thread.h"
41#include "mx.h"
42#include "score.h"
43#include "sort.h"
44
49void mview_free(struct MailboxView **ptr)
50{
51 if (!ptr || !*ptr)
52 return;
53
54 struct MailboxView *mv = *ptr;
55
56 struct EventMview ev_m = { mv };
57 mutt_debug(LL_NOTIFY, "NT_MVIEW_DELETE: %p\n", mv);
59
60 if (mv->mailbox)
62
65 FREE(&mv->pattern);
67
68 FREE(&mv);
69 *ptr = NULL;
70}
71
78struct MailboxView *mview_new(struct Mailbox *m, struct Notify *parent)
79{
80 if (!m)
81 return NULL;
82
83 struct MailboxView *mv = mutt_mem_calloc(1, sizeof(struct MailboxView));
84
85 mv->notify = notify_new();
86 notify_set_parent(mv->notify, parent);
87 struct EventMview ev_m = { mv };
88 mutt_debug(LL_NOTIFY, "NT_MVIEW_ADD: %p\n", mv);
90 // If the Mailbox is closed, mv->mailbox must be set to NULL
92
93 mv->mailbox = m;
95 mv->msg_in_pager = -1;
96 mv->collapsed = false;
98
99 return mv;
100}
101
106static void ctx_cleanup(struct MailboxView *mv)
107{
108 FREE(&mv->pattern);
110 if (mv->mailbox)
112
113 struct Notify *notify = mv->notify;
114 struct Mailbox *m = mv->mailbox;
115 memset(mv, 0, sizeof(struct MailboxView));
116 mv->notify = notify;
117 mv->mailbox = m;
118}
119
126void mview_update(struct MailboxView *mv)
127{
128 if (!mv || !mv->mailbox)
129 return;
130
131 struct Mailbox *m = mv->mailbox;
132
135
136 /* reset counters */
137 m->msg_unread = 0;
138 m->msg_flagged = 0;
139 m->msg_new = 0;
140 m->msg_deleted = 0;
141 m->msg_tagged = 0;
142 m->vcount = 0;
143 m->changed = false;
144
146
147 const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
148 struct Email *e = NULL;
149 for (int msgno = 0; msgno < m->msg_count; msgno++)
150 {
151 e = m->emails[msgno];
152 if (!e)
153 continue;
154
155 if (WithCrypto)
156 {
157 /* NOTE: this _must_ be done before the check for mailcap! */
158 e->security = crypt_query(e->body);
159 }
160
161 if (mview_has_limit(mv))
162 {
163 e->vnum = -1;
164 }
165 else
166 {
167 m->v2r[m->vcount] = msgno;
168 e->vnum = m->vcount++;
169 }
170 e->msgno = msgno;
171
172 if (e->env->supersedes)
173 {
174 struct Email *e2 = NULL;
175
176 if (!m->id_hash)
178
179 e2 = mutt_hash_find(m->id_hash, e->env->supersedes);
180 if (e2)
181 {
182 e2->superseded = true;
183 if (c_score)
184 mutt_score_message(mv->mailbox, e2, true);
185 }
186 }
187
188 /* add this message to the hash tables */
189 if (m->id_hash && e->env->message_id)
191 if (m->subj_hash && e->env->real_subj)
194
195 if (c_score)
196 mutt_score_message(mv->mailbox, e, false);
197
198 if (e->changed)
199 m->changed = true;
200 if (e->flagged)
201 m->msg_flagged++;
202 if (e->deleted)
203 m->msg_deleted++;
204 if (e->tagged)
205 m->msg_tagged++;
206 if (!e->read)
207 {
208 m->msg_unread++;
209 if (!e->old)
210 m->msg_new++;
211 }
212 }
213
214 /* rethread from scratch */
215 mutt_sort_headers(mv, true);
216}
217
222static void update_tables(struct MailboxView *mv)
223{
224 if (!mv || !mv->mailbox)
225 return;
226
227 struct Mailbox *m = mv->mailbox;
228
229 int i, j, padding;
230
231 /* update memory to reflect the new state of the mailbox */
232 m->vcount = 0;
233 mv->vsize = 0;
234 m->msg_tagged = 0;
235 m->msg_deleted = 0;
236 m->msg_new = 0;
237 m->msg_unread = 0;
238 m->changed = false;
239 m->msg_flagged = 0;
240 padding = mx_msg_padding_size(m);
241 const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash");
242 for (i = 0, j = 0; i < m->msg_count; i++)
243 {
244 if (!m->emails[i])
245 break;
246 if (!m->emails[i]->quasi_deleted &&
247 (!m->emails[i]->deleted || ((m->type == MUTT_MAILDIR) && c_maildir_trash)))
248 {
249 if (i != j)
250 {
251 m->emails[j] = m->emails[i];
252 m->emails[i] = NULL;
253 }
254 m->emails[j]->msgno = j;
255 if (m->emails[j]->vnum != -1)
256 {
257 m->v2r[m->vcount] = j;
258 m->emails[j]->vnum = m->vcount++;
259 struct Body *b = m->emails[j]->body;
260 mv->vsize += b->length + b->offset - b->hdr_offset + padding;
261 }
262
263 m->emails[j]->changed = false;
264 m->emails[j]->env->changed = false;
265
266 if ((m->type == MUTT_MAILDIR) && c_maildir_trash)
267 {
268 if (m->emails[j]->deleted)
269 m->msg_deleted++;
270 }
271
272 if (m->emails[j]->tagged)
273 m->msg_tagged++;
274 if (m->emails[j]->flagged)
275 m->msg_flagged++;
276 if (!m->emails[j]->read)
277 {
278 m->msg_unread++;
279 if (!m->emails[j]->old)
280 m->msg_new++;
281 }
282
283 j++;
284 }
285 else
286 {
287 if ((m->type == MUTT_NOTMUCH) || (m->type == MUTT_MH) ||
288 (m->type == MUTT_MAILDIR) || (m->type == MUTT_IMAP))
289 {
290 mailbox_size_sub(m, m->emails[i]);
291 }
292 /* remove message from the hash tables */
293 if (m->subj_hash && m->emails[i]->env->real_subj)
295 if (m->id_hash && m->emails[i]->env->message_id)
298
299#ifdef USE_IMAP
300 if (m->type == MUTT_IMAP)
302#endif
303
304 mailbox_gc_add(m->emails[i]);
305 m->emails[i] = NULL;
306 }
307 }
308 m->msg_count = j;
309}
310
315{
316 if (nc->event_type != NT_MAILBOX)
317 return 0;
318 if (!nc->global_data)
319 return -1;
320
321 struct MailboxView *mv = nc->global_data;
322
323 switch (nc->event_subtype)
324 {
327 ctx_cleanup(mv);
328 break;
330 mview_update(mv);
331 break;
333 update_tables(mv);
334 break;
336 mutt_sort_headers(mv, true);
337 break;
338 default:
339 return 0;
340 }
341
342 mutt_debug(LL_DEBUG5, "mailbox done\n");
343 return 0;
344}
345
354{
355 return e->visible && e->tagged;
356}
357
367int ea_add_tagged(struct EmailArray *ea, struct MailboxView *mv, struct Email *e, bool use_tagged)
368{
369 if (use_tagged)
370 {
371 if (!mv || !mv->mailbox || !mv->mailbox->emails)
372 return -1;
373
374 struct Mailbox *m = mv->mailbox;
375 for (int i = 0; i < m->msg_count; i++)
376 {
377 e = m->emails[i];
378 if (!e)
379 break;
380 if (!message_is_tagged(e))
381 continue;
382
383 ARRAY_ADD(ea, e);
384 }
385 }
386 else
387 {
388 if (!e)
389 return -1;
390
391 ARRAY_ADD(ea, e);
392 }
393
394 return ARRAY_SIZE(ea);
395}
396
407struct Email *mutt_get_virt_email(struct Mailbox *m, int vnum)
408{
409 if (!m || !m->emails || !m->v2r)
410 return NULL;
411
412 if ((vnum < 0) || (vnum >= m->vcount))
413 return NULL;
414
415 int inum = m->v2r[vnum];
416 if ((inum < 0) || (inum >= m->msg_count))
417 return NULL;
418
419 return m->emails[inum];
420}
421
428bool mview_has_limit(const struct MailboxView *mv)
429{
430 return mv && mv->pattern;
431}
432
440{
441 return mv ? mv->mailbox : NULL;
442}
443
450static struct MuttThread *top_of_thread(struct Email *e)
451{
452 if (!e)
453 return NULL;
454
455 struct MuttThread *t = e->thread;
456
457 while (t && t->parent)
458 t = t->parent;
459
460 return t;
461}
462
470bool mutt_limit_current_thread(struct MailboxView *mv, struct Email *e)
471{
472 if (!mv || !mv->mailbox || !e)
473 return false;
474
475 struct Mailbox *m = mv->mailbox;
476
477 struct MuttThread *me = top_of_thread(e);
478 if (!me)
479 return false;
480
481 m->vcount = 0;
482 mv->vsize = 0;
483 mv->collapsed = false;
484
485 for (int i = 0; i < m->msg_count; i++)
486 {
487 e = m->emails[i];
488 if (!e)
489 break;
490
491 e->vnum = -1;
492 e->visible = false;
493 e->collapsed = false;
494 e->num_hidden = 0;
495
496 if (top_of_thread(e) == me)
497 {
498 struct Body *body = e->body;
499
500 e->vnum = m->vcount;
501 e->visible = true;
502 m->v2r[m->vcount] = i;
503 m->vcount++;
504 mv->vsize += (body->length + body->offset - body->hdr_offset);
505 }
506 }
507 return true;
508}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:155
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:86
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:777
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
SecurityFlags crypt_query(struct Body *b)
Check out the type of encryption used.
Definition: crypt.c:677
Structs that make up an email.
#define mutt_debug(LEVEL,...)
Definition: logging2.h:84
int mview_mailbox_observer(struct NotifyCallback *nc)
Notification that a Mailbox has changed - Implements observer_t -.
Definition: mview.c:314
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:335
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:427
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:362
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
IMAP network mailbox.
void imap_notify_delete_email(struct Mailbox *m, struct Email *e)
Inform IMAP that an Email has been deleted.
Definition: imap.c:559
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:44
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:45
void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
Subtract an email's size from the total size of a Mailbox.
Definition: mailbox.c:248
void mailbox_gc_add(struct Email *e)
Add an Email to the garbage-collection set.
Definition: mailbox.c:287
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition: mailbox.h:176
@ NT_MAILBOX_DELETE
Mailbox is about to be deleted.
Definition: mailbox.h:169
@ NT_MAILBOX_INVALID
Email list was changed.
Definition: mailbox.h:175
@ NT_MAILBOX_UPDATE
Update internal tables.
Definition: mailbox.h:177
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:51
@ MUTT_MH
'MH' Mailbox type
Definition: mailbox.h:47
@ MUTT_IMAP
'IMAP' Mailbox type
Definition: mailbox.h:50
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition: mailbox.h:48
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition: notify.c:228
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
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
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:93
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
void mutt_label_hash_remove(struct Mailbox *m, struct Email *e)
Remove a message's labels from the Hash Table.
Definition: mutt_header.c:442
void mutt_label_hash_add(struct Mailbox *m, struct Email *e)
Add a message's labels to the Hash Table.
Definition: mutt_header.c:429
Representation of the email's header.
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:712
struct ThreadsContext * mutt_thread_ctx_init(struct MailboxView *mv)
Initialize a threading context.
Definition: mutt_thread.c:354
void mutt_thread_ctx_free(struct ThreadsContext **ptr)
Finalize a threading context.
Definition: mutt_thread.c:365
struct HashTable * mutt_make_id_hash(struct Mailbox *m)
Create a Hash Table for message-ids.
Definition: mutt_thread.c:1696
Create/manipulate threading in emails.
struct Email * mutt_get_virt_email(struct Mailbox *m, int vnum)
Get a virtual Email.
Definition: mview.c:407
static void ctx_cleanup(struct MailboxView *mv)
Release memory and initialize a MailboxView.
Definition: mview.c:106
struct Mailbox * mview_mailbox(struct MailboxView *mv)
Wrapper to get the mailbox in a MailboxView, or NULL.
Definition: mview.c:439
static void update_tables(struct MailboxView *mv)
Update a MailboxView's internal tables.
Definition: mview.c:222
bool mutt_limit_current_thread(struct MailboxView *mv, struct Email *e)
Limit the email view to the current thread.
Definition: mview.c:470
void mview_free(struct MailboxView **ptr)
Free a MailboxView.
Definition: mview.c:49
static struct MuttThread * top_of_thread(struct Email *e)
Find the first email in the current thread.
Definition: mview.c:450
int ea_add_tagged(struct EmailArray *ea, struct MailboxView *mv, struct Email *e, bool use_tagged)
Get an array of the tagged Emails.
Definition: mview.c:367
bool message_is_tagged(struct Email *e)
Is a message in the index tagged (and within limit)
Definition: mview.c:353
void mview_update(struct MailboxView *mv)
Update the MailboxView's message counts.
Definition: mview.c:126
bool mview_has_limit(const struct MailboxView *mv)
Is a limit active?
Definition: mview.c:428
struct MailboxView * mview_new(struct Mailbox *m, struct Notify *parent)
Create a new MailboxView.
Definition: mview.c:78
View of a Mailbox.
@ NT_MVIEW_DELETE
The Mview is about to be destroyed.
Definition: mview.h:62
@ NT_MVIEW_ADD
The Mview has been opened.
Definition: mview.h:61
int mx_msg_padding_size(struct Mailbox *m)
Bytes of padding between messages - Wrapper for MxOps::msg_padding_size()
Definition: mx.c:1566
API for mailboxes.
API for encryption/signing of emails.
#define WithCrypto
Definition: lib.h:116
@ NT_MAILBOX
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:49
@ NT_MVIEW
MailboxView has changed, NotifyMview, EventMview.
Definition: notify_type.h:50
Match patterns to emails.
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:161
Routines for adding user scores to emails.
void mutt_sort_headers(struct MailboxView *mv, bool init)
Sort emails by their headers.
Definition: sort.c:358
Assorted sorting methods.
The body of an email.
Definition: body.h:36
LOFF_T offset
offset where the actual data begins
Definition: body.h:52
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
long hdr_offset
Offset in stream where the headers begin.
Definition: body.h:80
The envelope/body of an email.
Definition: email.h:37
bool read
Email is read.
Definition: email.h:48
bool visible
Is this message part of the view?
Definition: email.h:120
struct Envelope * env
Envelope information.
Definition: email.h:66
bool collapsed
Is this message part of a collapsed thread?
Definition: email.h:119
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition: email.h:41
struct Body * body
List of MIME parts.
Definition: email.h:67
bool old
Email is seen, but unread.
Definition: email.h:47
size_t num_hidden
Number of hidden messages in this view (only valid when collapsed is set)
Definition: email.h:122
bool changed
Email has been edited.
Definition: email.h:75
bool flagged
Marked important?
Definition: email.h:45
int vnum
Virtual message number.
Definition: email.h:113
int msgno
Number displayed to the user.
Definition: email.h:110
bool deleted
Email is deleted.
Definition: email.h:76
bool quasi_deleted
Deleted from neomutt, but not modified on disk.
Definition: email.h:102
bool tagged
Email is tagged.
Definition: email.h:106
bool superseded
Got superseded?
Definition: email.h:50
struct MuttThread * thread
Thread of Emails.
Definition: email.h:118
char * supersedes
Supersedes header.
Definition: envelope.h:74
unsigned char changed
Changed fields, e.g. MUTT_ENV_CHANGED_SUBJECT.
Definition: envelope.h:92
char * message_id
Message ID.
Definition: envelope.h:73
char * real_subj
Offset of the real subject.
Definition: envelope.h:71
An Event that happened to an MailboxView.
Definition: mview.h:70
struct MailboxView * mv
The MailboxView this Event relates to.
Definition: mview.h:71
View of a Mailbox.
Definition: mview.h:39
bool collapsed
Are all threads collapsed?
Definition: mview.h:48
off_t vsize
Size (in bytes) of the messages shown.
Definition: mview.h:40
struct Notify * notify
Notifications: NotifyMview, EventMview.
Definition: mview.h:51
struct PatternList * limit_pattern
Compiled limit pattern.
Definition: mview.h:42
struct ThreadsContext * threads
Threads context.
Definition: mview.h:43
int msg_in_pager
Message currently shown in the pager.
Definition: mview.h:44
struct Mailbox * mailbox
Current Mailbox.
Definition: mview.h:50
char * pattern
Limit pattern string.
Definition: mview.h:41
A mailbox.
Definition: mailbox.h:79
int vcount
The number of virtual messages.
Definition: mailbox.h:99
bool changed
Mailbox has been modified.
Definition: mailbox.h:110
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:98
int msg_new
Number of new messages.
Definition: mailbox.h:92
int msg_count
Total number of messages.
Definition: mailbox.h:88
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
struct HashTable * subj_hash
Hash Table: "subject" -> Email.
Definition: mailbox.h:124
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:143
struct HashTable * id_hash
Hash Table: "message-id" -> Email.
Definition: mailbox.h:123
int msg_deleted
Number of deleted messages.
Definition: mailbox.h:93
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:90
int msg_tagged
How many messages are tagged?
Definition: mailbox.h:94
int msg_unread
Number of unread messages.
Definition: mailbox.h:89
An Email conversation.
Definition: thread.h:34
struct MuttThread * parent
Parent of this Thread.
Definition: thread.h:44
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Data passed to a notification function.
Definition: observer.h:34
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:36
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:37
void * global_data
Data from notify_observer_add()
Definition: observer.h:39
Notification API.
Definition: notify.c:51