NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
selection.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <stdbool.h>
31#include <stddef.h>
32#include "mutt/lib.h"
33#include "email/lib.h"
34#include "menu/lib.h"
35#include "attach.h"
36#include "recvattach.h"
37
44static bool aa_contains_attach(struct AttachPtrArray *aa, struct AttachPtr *ap)
45{
46 if (!aa || !ap)
47 return false;
48
49 struct AttachPtr **app = NULL;
50 ARRAY_FOREACH(app, aa)
51 {
52 if (*app == ap)
53 return true;
54 }
55
56 return false;
57}
58
64static void aa_add_attach(struct AttachPtrArray *aa, struct AttachPtr *ap)
65{
66 if (!aa_contains_attach(aa, ap))
67 ARRAY_ADD(aa, ap);
68}
69
76static void aa_add_folded(struct AttachPtrArray *aa, struct AttachCtx *actx, int rindex)
77{
78 if (!aa || !actx || (rindex < 0) || (rindex >= actx->idxlen))
79 return;
80
81 struct AttachPtr *ap = actx->idx[rindex];
82 aa_add_attach(aa, ap);
83
84 if (!ap->collapsed)
85 return;
86
87 const int level = ap->level;
88 for (int i = rindex + 1; (i < actx->idxlen) && (actx->idx[i]->level > level); i++)
89 {
90 aa_add_attach(aa, actx->idx[i]);
91 }
92}
93
101static int aa_add_tagged(struct AttachPtrArray *aa, struct AttachCtx *actx)
102{
103 if (!aa || !actx)
104 return -1;
105
106 for (int i = 0; i < actx->idxlen; i++)
107 {
108 if (actx->idx[i]->body->tagged)
109 aa_add_folded(aa, actx, i);
110 }
111
112 return ARRAY_SIZE(aa);
113}
114
125int aa_add_selection(struct AttachPtrArray *aa, struct AttachCtx *actx,
126 struct Menu *menu, bool use_tagged, int count)
127{
128 if (!aa || !actx || !menu)
129 return -1;
130
131 if (use_tagged)
132 return aa_add_tagged(aa, actx);
133
134 const int index = menu_get_index(menu);
135 if ((index < 0) || (index >= actx->vcount))
136 return -1;
137
138 int n = (count > 1) ? count : 1;
139 if ((index + n) > actx->vcount)
140 n = actx->vcount - index;
141
142 for (int i = 0; i < n; i++)
143 {
144 const int rindex = actx->v2r[index + i];
145 if ((rindex < 0) || (rindex >= actx->idxlen))
146 return -1;
147
148 aa_add_folded(aa, actx, rindex);
149 }
150
151 return ARRAY_SIZE(aa);
152}
153
164int ba_add_selection(struct BodyArray *ba, struct AttachCtx *actx,
165 struct Menu *menu, bool use_tagged, int count)
166{
167 if (!ba)
168 return -1;
169
170 struct AttachPtrArray aa = ARRAY_HEAD_INITIALIZER;
171 if (aa_add_selection(&aa, actx, menu, use_tagged, count) < 0)
172 {
173 ARRAY_FREE(&aa);
174 return -1;
175 }
176
177 struct AttachPtr **app = NULL;
178 ARRAY_FOREACH(app, &aa)
179 {
180 ARRAY_ADD(ba, (*app)->body);
181 }
182
183 const int rc = ARRAY_SIZE(ba);
184 ARRAY_FREE(&aa);
185 return rc;
186}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
static int aa_add_tagged(struct AttachPtrArray *aa, struct AttachCtx *actx)
Get an array of tagged Attachments.
Definition selection.c:101
static void aa_add_folded(struct AttachPtrArray *aa, struct AttachCtx *actx, int rindex)
Add an Attachment, expanding collapsed children.
Definition selection.c:76
static bool aa_contains_attach(struct AttachPtrArray *aa, struct AttachPtr *ap)
Does a working set already include an Attachment?
Definition selection.c:44
int aa_add_selection(struct AttachPtrArray *aa, struct AttachCtx *actx, struct Menu *menu, bool use_tagged, int count)
Build a working set of Attachments for an action.
Definition selection.c:125
int ba_add_selection(struct BodyArray *ba, struct AttachCtx *actx, struct Menu *menu, bool use_tagged, int count)
Build a working set of attachment bodies for an action.
Definition selection.c:164
static void aa_add_attach(struct AttachPtrArray *aa, struct AttachPtr *ap)
Add an Attachment to a working set.
Definition selection.c:64
Handling of email attachments.
Structs that make up an email.
GUI present the user with a selectable list.
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition menu.c:155
Convenience wrapper for the library headers.
Routines for managing attachments.
A set of attachments.
Definition attach.h:65
short vcount
The number of virtual attachments.
Definition attach.h:74
struct AttachPtr ** idx
Array of attachments.
Definition attach.h:69
short idxlen
Number of attachmentes.
Definition attach.h:70
short * v2r
Mapping from virtual to real attachment.
Definition attach.h:73
An email to which things will be attached.
Definition attach.h:36
struct Body * body
Attachment.
Definition attach.h:37
bool collapsed
Group is collapsed.
Definition attach.h:45
int level
Nesting depth of attachment.
Definition attach.h:41
bool tagged
This attachment is tagged.
Definition body.h:90
Definition lib.h:86