NeoMutt  2024-12-12-19-ge4b57e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mview.c File Reference

View of a Mailbox. More...

#include "config.h"
#include <string.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "mview.h"
#include "imap/lib.h"
#include "ncrypt/lib.h"
#include "pattern/lib.h"
#include "mutt_header.h"
#include "mutt_thread.h"
#include "mx.h"
#include "score.h"
+ Include dependency graph for mview.c:

Go to the source code of this file.

Functions

void mview_free (struct MailboxView **ptr)
 Free a MailboxView.
 
struct MailboxViewmview_new (struct Mailbox *m, struct Notify *parent)
 Create a new MailboxView.
 
static void mview_clean (struct MailboxView *mv)
 Release memory and initialize a MailboxView.
 
void mview_update (struct MailboxView *mv)
 Update the MailboxView's message counts.
 
static void update_tables (struct MailboxView *mv)
 Update a MailboxView's internal tables.
 
int mview_mailbox_observer (struct NotifyCallback *nc)
 Notification that a Mailbox has changed - Implements observer_t -.
 
bool message_is_tagged (struct Email *e)
 Is a message in the index tagged (and within limit)
 
int ea_add_tagged (struct EmailArray *ea, struct MailboxView *mv, struct Email *e, bool use_tagged)
 Get an array of the tagged Emails.
 
struct Emailmutt_get_virt_email (struct Mailbox *m, int vnum)
 Get a virtual Email.
 
bool mview_has_limit (const struct MailboxView *mv)
 Is a limit active?
 
struct Mailboxmview_mailbox (struct MailboxView *mv)
 Wrapper to get the mailbox in a MailboxView, or NULL.
 
static struct MuttThreadtop_of_thread (struct Email *e)
 Find the first email in the current thread.
 
bool mutt_limit_current_thread (struct MailboxView *mv, struct Email *e)
 Limit the email view to the current thread.
 

Detailed Description

View of a Mailbox.

Authors
  • Richard Russon
  • Pietro Cerutti

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 mview.c.

Function Documentation

◆ mview_free()

void mview_free ( struct MailboxView **  ptr)

Free a MailboxView.

Parameters
[out]ptrMailboxView to free

Definition at line 49 of file mview.c.

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", (void *) mv);
59
60 if (mv->mailbox)
61 {
63
64 // Disconnect the Emails before freeing the Threads
65 for (int i = 0; i < mv->mailbox->msg_count; i++)
66 {
67 struct Email *e = mv->mailbox->emails[i];
68 if (!e)
69 continue;
70 e->thread = NULL;
71 e->threaded = false;
72 }
73 }
74
76 notify_free(&mv->notify);
77 FREE(&mv->pattern);
79
80 *ptr = NULL;
81 FREE(&mv);
82}
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:778
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
int mview_mailbox_observer(struct NotifyCallback *nc)
Notification that a Mailbox has changed - Implements observer_t -.
Definition: mview.c:324
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:48
#define FREE(x)
Definition: memory.h:55
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition: notify.c:230
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:173
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:75
void mutt_thread_ctx_free(struct ThreadsContext **ptr)
Finalize a threading context.
Definition: mutt_thread.c:367
@ NT_MVIEW_DELETE
The Mview is about to be destroyed.
Definition: mview.h:63
@ NT_MVIEW
MailboxView has changed, NotifyMview, EventMview.
Definition: notify_type.h:50
The envelope/body of an email.
Definition: email.h:39
bool threaded
Used for threading.
Definition: email.h:108
struct MuttThread * thread
Thread of Emails.
Definition: email.h:119
An Event that happened to an MailboxView.
Definition: mview.h:71
struct MailboxView * mv
The MailboxView this Event relates to.
Definition: mview.h:72
View of a Mailbox.
Definition: mview.h:40
struct Notify * notify
Notifications: NotifyMview, EventMview.
Definition: mview.h:52
struct PatternList * limit_pattern
Compiled limit pattern.
Definition: mview.h:43
struct ThreadsContext * threads
Threads context.
Definition: mview.h:44
struct Mailbox * mailbox
Current Mailbox.
Definition: mview.h:51
char * pattern
Limit pattern string.
Definition: mview.h:42
int msg_count
Total number of messages.
Definition: mailbox.h:88
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:145
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mview_new()

struct MailboxView * mview_new ( struct Mailbox m,
struct Notify parent 
)

Create a new MailboxView.

Parameters
mMailbox
parentNotification parent
Return values
ptrNew MailboxView

Definition at line 90 of file mview.c.

91{
92 if (!m)
93 return NULL;
94
95 struct MailboxView *mv = MUTT_MEM_CALLOC(1, struct MailboxView);
96
97 mv->notify = notify_new();
98 notify_set_parent(mv->notify, parent);
99 struct EventMview ev_m = { mv };
100 mutt_debug(LL_NOTIFY, "NT_MVIEW_ADD: %p\n", (void *) mv);
102 // If the Mailbox is closed, mv->mailbox must be set to NULL
104
105 mv->mailbox = m;
107 mv->msg_in_pager = -1;
108 mv->collapsed = false;
110
111 return mv;
112}
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
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:191
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:62
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:95
struct ThreadsContext * mutt_thread_ctx_init(struct MailboxView *mv)
Initialize a threading context.
Definition: mutt_thread.c:356
void mview_update(struct MailboxView *mv)
Update the MailboxView's message counts.
Definition: mview.c:138
@ NT_MVIEW_ADD
The Mview has been opened.
Definition: mview.h:62
@ NT_MAILBOX
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:49
bool collapsed
Are all threads collapsed?
Definition: mview.h:49
int msg_in_pager
Message currently shown in the pager.
Definition: mview.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mview_clean()

static void mview_clean ( struct MailboxView mv)
static

Release memory and initialize a MailboxView.

Parameters
mvMailboxView to cleanup

Definition at line 118 of file mview.c.

119{
120 FREE(&mv->pattern);
122 if (mv->mailbox)
124
125 struct Notify *notify = mv->notify;
126 struct Mailbox *m = mv->mailbox;
127 memset(mv, 0, sizeof(struct MailboxView));
128 mv->notify = notify;
129 mv->mailbox = m;
130}
A mailbox.
Definition: mailbox.h:79
Notification API.
Definition: notify.c:53
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mview_update()

void mview_update ( struct MailboxView mv)

Update the MailboxView's message counts.

Parameters
mvMailbox View

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

Definition at line 138 of file mview.c.

139{
140 if (!mv || !mv->mailbox)
141 return;
142
143 struct Mailbox *m = mv->mailbox;
144
147
148 /* reset counters */
149 m->msg_unread = 0;
150 m->msg_flagged = 0;
151 m->msg_new = 0;
152 m->msg_deleted = 0;
153 m->msg_tagged = 0;
154 m->vcount = 0;
155 m->changed = false;
156
158
159 const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
160 struct Email *e = NULL;
161 for (int msgno = 0; msgno < m->msg_count; msgno++)
162 {
163 e = m->emails[msgno];
164 if (!e)
165 continue;
166
167 if (WithCrypto)
168 {
169 /* NOTE: this _must_ be done before the check for mailcap! */
170 e->security = crypt_query(e->body);
171 }
172
173 if (mview_has_limit(mv))
174 {
175 e->vnum = -1;
176 }
177 else
178 {
179 m->v2r[m->vcount] = msgno;
180 e->vnum = m->vcount++;
181 }
182 e->msgno = msgno;
183
184 if (e->env->supersedes)
185 {
186 struct Email *e2 = NULL;
187
188 if (!m->id_hash)
190
191 e2 = mutt_hash_find(m->id_hash, e->env->supersedes);
192 if (e2)
193 {
194 e2->superseded = true;
195 if (c_score)
196 mutt_score_message(mv->mailbox, e2, true);
197 }
198 }
199
200 /* add this message to the hash tables */
201 if (m->id_hash && e->env->message_id)
203 if (m->subj_hash && e->env->real_subj)
206
207 if (c_score)
208 mutt_score_message(mv->mailbox, e, false);
209
210 if (e->changed)
211 m->changed = true;
212 if (e->flagged)
213 m->msg_flagged++;
214 if (e->deleted)
215 m->msg_deleted++;
216 if (e->tagged)
217 m->msg_tagged++;
218 if (!e->read)
219 {
220 m->msg_unread++;
221 if (!e->old)
222 m->msg_new++;
223 }
224 }
225
226 /* rethread from scratch */
227 mutt_sort_headers(mv, true);
228}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
SecurityFlags crypt_query(struct Body *b)
Check out the type of encryption used.
Definition: crypt.c:687
void mutt_sort_headers(struct MailboxView *mv, bool init)
Sort emails by their headers.
Definition: sort.c:355
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_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
void mutt_label_hash_add(struct Mailbox *m, struct Email *e)
Add a message's labels to the Hash Table.
Definition: mutt_header.c:417
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:719
struct HashTable * mutt_make_id_hash(struct Mailbox *m)
Create a Hash Table for message-ids.
Definition: mutt_thread.c:1703
bool mview_has_limit(const struct MailboxView *mv)
Is a limit active?
Definition: mview.c:438
#define WithCrypto
Definition: lib.h:122
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:165
bool read
Email is read.
Definition: email.h:50
struct Envelope * env
Envelope information.
Definition: email.h:68
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition: email.h:43
struct Body * body
List of MIME parts.
Definition: email.h:69
bool old
Email is seen, but unread.
Definition: email.h:49
bool changed
Email has been edited.
Definition: email.h:77
bool flagged
Marked important?
Definition: email.h:47
int vnum
Virtual message number.
Definition: email.h:114
int msgno
Number displayed to the user.
Definition: email.h:111
bool deleted
Email is deleted.
Definition: email.h:78
bool tagged
Email is tagged.
Definition: email.h:107
bool superseded
Got superseded?
Definition: email.h:52
char * supersedes
Supersedes header.
Definition: envelope.h:74
char * message_id
Message ID.
Definition: envelope.h:73
char *const real_subj
Offset of the real subject.
Definition: envelope.h:71
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
struct HashTable * subj_hash
Hash Table: "subject" -> Email.
Definition: mailbox.h:124
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
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ update_tables()

static void update_tables ( struct MailboxView mv)
static

Update a MailboxView's internal tables.

Parameters
mvMailbox

Definition at line 234 of file mview.c.

235{
236 if (!mv || !mv->mailbox)
237 return;
238
239 struct Mailbox *m = mv->mailbox;
240
241 int i, j, padding;
242
243 /* update memory to reflect the new state of the mailbox */
244 m->vcount = 0;
245 mv->vsize = 0;
246 m->msg_tagged = 0;
247 m->msg_deleted = 0;
248 m->msg_new = 0;
249 m->msg_unread = 0;
250 m->changed = false;
251 m->msg_flagged = 0;
252 padding = mx_msg_padding_size(m);
253 const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash");
254 for (i = 0, j = 0; i < m->msg_count; i++)
255 {
256 if (!m->emails[i])
257 break;
258 if (!m->emails[i]->quasi_deleted &&
259 (!m->emails[i]->deleted || ((m->type == MUTT_MAILDIR) && c_maildir_trash)))
260 {
261 if (i != j)
262 {
263 m->emails[j] = m->emails[i];
264 m->emails[i] = NULL;
265 }
266 m->emails[j]->msgno = j;
267 if (m->emails[j]->vnum != -1)
268 {
269 m->v2r[m->vcount] = j;
270 m->emails[j]->vnum = m->vcount++;
271 struct Body *b = m->emails[j]->body;
272 mv->vsize += b->length + b->offset - b->hdr_offset + padding;
273 }
274
275 m->emails[j]->changed = false;
276 m->emails[j]->env->changed = false;
277
278 if ((m->type == MUTT_MAILDIR) && c_maildir_trash)
279 {
280 if (m->emails[j]->deleted)
281 m->msg_deleted++;
282 }
283
284 if (m->emails[j]->tagged)
285 m->msg_tagged++;
286 if (m->emails[j]->flagged)
287 m->msg_flagged++;
288 if (!m->emails[j]->read)
289 {
290 m->msg_unread++;
291 if (!m->emails[j]->old)
292 m->msg_new++;
293 }
294
295 j++;
296 }
297 else
298 {
299 if ((m->type == MUTT_NOTMUCH) || (m->type == MUTT_MH) ||
300 (m->type == MUTT_MAILDIR) || (m->type == MUTT_IMAP))
301 {
302 mailbox_size_sub(m, m->emails[i]);
303 }
304 /* remove message from the hash tables */
305 if (m->subj_hash && m->emails[i]->env->real_subj)
307 if (m->id_hash && m->emails[i]->env->message_id)
310
311 if (m->type == MUTT_IMAP)
313
314 mailbox_gc_add(m->emails[i]);
315 m->emails[i] = NULL;
316 }
317 }
318 m->msg_count = j;
319}
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:259
void mailbox_gc_add(struct Email *e)
Add an Email to the garbage-collection set.
Definition: mailbox.c:298
@ 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_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:427
void imap_notify_delete_email(struct Mailbox *m, struct Email *e)
Inform IMAP that an Email has been deleted.
Definition: imap.c:647
void mutt_label_hash_remove(struct Mailbox *m, struct Email *e)
Remove a message's labels from the Hash Table.
Definition: mutt_header.c:430
int mx_msg_padding_size(struct Mailbox *m)
Bytes of padding between messages - Wrapper for MxOps::msg_padding_size()
Definition: mx.c:1505
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:81
bool quasi_deleted
Deleted from neomutt, but not modified on disk.
Definition: email.h:103
unsigned char changed
Changed fields, e.g. MUTT_ENV_CHANGED_SUBJECT.
Definition: envelope.h:90
off_t vsize
Size (in bytes) of the messages shown.
Definition: mview.h:41
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
+ Here is the call graph for this function:
+ 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 363 of file mview.c.

364{
365 return e->visible && e->tagged;
366}
bool visible
Is this message part of the view?
Definition: email.h:121
+ Here is the caller graph for this function:

◆ ea_add_tagged()

int ea_add_tagged ( struct EmailArray *  ea,
struct MailboxView mv,
struct Email e,
bool  use_tagged 
)

Get an array of the tagged Emails.

Parameters
eaEmpty EmailArray to populate
mvCurrent Mailbox
eCurrent Email
use_taggedUse tagged Emails
Return values
numNumber of selected emails
-1Error

Definition at line 377 of file mview.c.

378{
379 if (use_tagged)
380 {
381 if (!mv || !mv->mailbox || !mv->mailbox->emails)
382 return -1;
383
384 struct Mailbox *m = mv->mailbox;
385 for (int i = 0; i < m->msg_count; i++)
386 {
387 e = m->emails[i];
388 if (!e)
389 break;
390 if (!message_is_tagged(e))
391 continue;
392
393 ARRAY_ADD(ea, e);
394 }
395 }
396 else
397 {
398 if (!e)
399 return -1;
400
401 ARRAY_ADD(ea, e);
402 }
403
404 return ARRAY_SIZE(ea);
405}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
bool message_is_tagged(struct Email *e)
Is a message in the index tagged (and within limit)
Definition: mview.c:363
+ Here is the call graph for this function:
+ 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 417 of file mview.c.

418{
419 if (!m || !m->emails || !m->v2r)
420 return NULL;
421
422 if ((vnum < 0) || (vnum >= m->vcount))
423 return NULL;
424
425 int inum = m->v2r[vnum];
426 if ((inum < 0) || (inum >= m->msg_count))
427 return NULL;
428
429 return m->emails[inum];
430}
+ Here is the caller graph for this function:

◆ mview_has_limit()

bool mview_has_limit ( const struct MailboxView mv)

Is a limit active?

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

Definition at line 438 of file mview.c.

439{
440 return mv && mv->pattern;
441}
+ Here is the caller graph for this function:

◆ mview_mailbox()

struct Mailbox * mview_mailbox ( struct MailboxView mv)

Wrapper to get the mailbox in a MailboxView, or NULL.

Parameters
mvMailboxView
Return values
ptrThe mailbox in the MailboxView
NULLMailboxView is NULL or doesn't have a mailbox

Definition at line 449 of file mview.c.

450{
451 return mv ? mv->mailbox : NULL;
452}
+ Here is the caller graph for this function:

◆ top_of_thread()

static struct MuttThread * top_of_thread ( struct Email e)
static

Find the first email in the current thread.

Parameters
eCurrent Email
Return values
ptrSuccess, email found
NULLError

Definition at line 460 of file mview.c.

461{
462 if (!e)
463 return NULL;
464
465 struct MuttThread *t = e->thread;
466
467 while (t && t->parent)
468 t = t->parent;
469
470 return t;
471}
An Email conversation.
Definition: thread.h:34
struct MuttThread * parent
Parent of this Thread.
Definition: thread.h:44
+ Here is the caller graph for this function:

◆ mutt_limit_current_thread()

bool mutt_limit_current_thread ( struct MailboxView mv,
struct Email e 
)

Limit the email view to the current thread.

Parameters
mvMailbox View
eEmail
Return values
trueSuccess
falseFailure

Definition at line 480 of file mview.c.

481{
482 if (!mv || !mv->mailbox || !e)
483 return false;
484
485 struct Mailbox *m = mv->mailbox;
486
487 struct MuttThread *me = top_of_thread(e);
488 if (!me)
489 return false;
490
491 m->vcount = 0;
492 mv->vsize = 0;
493 mv->collapsed = false;
494
495 for (int i = 0; i < m->msg_count; i++)
496 {
497 e = m->emails[i];
498 if (!e)
499 break;
500
501 e->vnum = -1;
502 e->visible = false;
503 e->collapsed = false;
504 e->num_hidden = 0;
505
506 if (top_of_thread(e) == me)
507 {
508 struct Body *body = e->body;
509
510 e->vnum = m->vcount;
511 e->visible = true;
512 m->v2r[m->vcount] = i;
513 m->vcount++;
514 mv->vsize += (body->length + body->offset - body->hdr_offset);
515 }
516 }
517 return true;
518}
static struct MuttThread * top_of_thread(struct Email *e)
Find the first email in the current thread.
Definition: mview.c:460
bool collapsed
Is this message part of a collapsed thread?
Definition: email.h:120
size_t num_hidden
Number of hidden messages in this view (only valid when collapsed is set)
Definition: email.h:123
+ Here is the call graph for this function:
+ Here is the caller graph for this function: