forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.c
464 lines (415 loc) · 9.8 KB
/
flags.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/**
* @file
* Manipulate the flags in an email header
*
* @authors
* Copyright (C) 1996-2000 Michael R. Elkins <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
#include "config.h"
#include <stddef.h>
#include <stdbool.h>
#include "mutt/mutt.h"
#include "mutt.h"
#include "context.h"
#include "globals.h"
#include "header.h"
#include "mutt_curses.h"
#include "mutt_menu.h"
#include "mx.h"
#include "options.h"
#include "protos.h"
#include "sort.h"
#include "thread.h"
void mutt_set_flag_update(struct Context *ctx, struct Header *h, int flag, int bf, int upd_ctx)
{
if (!ctx || !h)
return;
bool changed = h->changed;
int deleted = ctx->deleted;
int tagged = ctx->tagged;
int flagged = ctx->flagged;
int update = false;
if (ctx->readonly && flag != MUTT_TAG)
return; /* don't modify anything if we are read-only */
switch (flag)
{
case MUTT_DELETE:
if (!mutt_bit_isset(ctx->rights, MUTT_ACL_DELETE))
return;
if (bf)
{
if (!h->deleted && !ctx->readonly && (!h->flagged || !FlagSafe))
{
h->deleted = true;
update = true;
if (upd_ctx)
ctx->deleted++;
#ifdef USE_IMAP
/* deleted messages aren't treated as changed elsewhere so that the
* purge-on-sync option works correctly. This isn't applicable here */
if (ctx && ctx->magic == MUTT_IMAP)
{
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
#endif
}
}
else if (h->deleted)
{
h->deleted = false;
update = true;
if (upd_ctx)
ctx->deleted--;
#ifdef USE_IMAP
/* see my comment above */
if (ctx->magic == MUTT_IMAP)
{
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
#endif
/*
* If the user undeletes a message which is marked as
* "trash" in the maildir folder on disk, the folder has
* been changed, and is marked accordingly. However, we do
* _not_ mark the message itself changed, because trashing
* is checked in specific code in the maildir folder
* driver.
*/
if (ctx->magic == MUTT_MAILDIR && upd_ctx && h->trash)
ctx->changed = true;
}
break;
case MUTT_PURGE:
if (!mutt_bit_isset(ctx->rights, MUTT_ACL_DELETE))
return;
if (bf)
{
if (!h->purge && !ctx->readonly)
h->purge = true;
}
else if (h->purge)
h->purge = false;
break;
case MUTT_NEW:
if (!mutt_bit_isset(ctx->rights, MUTT_ACL_SEEN))
return;
if (bf)
{
if (h->read || h->old)
{
update = true;
h->old = false;
if (upd_ctx)
ctx->new ++;
if (h->read)
{
h->read = false;
if (upd_ctx)
ctx->unread++;
}
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
}
else if (!h->read)
{
update = true;
if (!h->old)
if (upd_ctx)
ctx->new --;
h->read = true;
if (upd_ctx)
ctx->unread--;
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
break;
case MUTT_OLD:
if (!mutt_bit_isset(ctx->rights, MUTT_ACL_SEEN))
return;
if (bf)
{
if (!h->old)
{
update = true;
h->old = true;
if (!h->read)
if (upd_ctx)
ctx->new --;
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
}
else if (h->old)
{
update = true;
h->old = false;
if (!h->read)
if (upd_ctx)
ctx->new ++;
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
break;
case MUTT_READ:
if (!mutt_bit_isset(ctx->rights, MUTT_ACL_SEEN))
return;
if (bf)
{
if (!h->read)
{
update = true;
h->read = true;
if (upd_ctx)
ctx->unread--;
if (!h->old)
if (upd_ctx)
ctx->new --;
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
}
else if (h->read)
{
update = true;
h->read = false;
if (upd_ctx)
ctx->unread++;
if (!h->old)
if (upd_ctx)
ctx->new ++;
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
break;
case MUTT_REPLIED:
if (!mutt_bit_isset(ctx->rights, MUTT_ACL_WRITE))
return;
if (bf)
{
if (!h->replied)
{
update = true;
h->replied = true;
if (!h->read)
{
h->read = true;
if (upd_ctx)
ctx->unread--;
if (!h->old)
if (upd_ctx)
ctx->new --;
}
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
}
else if (h->replied)
{
update = true;
h->replied = false;
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
break;
case MUTT_FLAG:
if (!mutt_bit_isset(ctx->rights, MUTT_ACL_WRITE))
return;
if (bf)
{
if (!h->flagged)
{
update = true;
h->flagged = bf;
if (upd_ctx)
ctx->flagged++;
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
}
else if (h->flagged)
{
update = true;
h->flagged = false;
if (upd_ctx)
ctx->flagged--;
h->changed = true;
if (upd_ctx)
ctx->changed = true;
}
break;
case MUTT_TAG:
if (bf)
{
if (!h->tagged)
{
update = true;
h->tagged = true;
if (upd_ctx)
ctx->tagged++;
}
}
else if (h->tagged)
{
update = true;
h->tagged = false;
if (upd_ctx)
ctx->tagged--;
}
break;
}
if (update)
{
mutt_set_header_color(ctx, h);
#ifdef USE_SIDEBAR
mutt_set_current_menu_redraw(REDRAW_SIDEBAR);
#endif
}
/* if the message status has changed, we need to invalidate the cached
* search results so that any future search will match the current status
* of this message and not what it was at the time it was last searched.
*/
if (h->searched && (changed != h->changed || deleted != ctx->deleted ||
tagged != ctx->tagged || flagged != ctx->flagged))
{
h->searched = false;
}
}
/**
* mutt_thread_set_flag - Set a flag on an entire thread
*/
void mutt_tag_set_flag(int flag, int bf)
{
for (int i = 0; i < Context->msgcount; i++)
if (message_is_tagged(Context, i))
mutt_set_flag(Context, Context->hdrs[i], flag, bf);
}
int mutt_thread_set_flag(struct Header *hdr, int flag, int bf, int subthread)
{
struct MuttThread *start = NULL, *cur = hdr->thread;
if ((Sort & SORT_MASK) != SORT_THREADS)
{
mutt_error(_("Threading is not enabled."));
return -1;
}
if (!subthread)
while (cur->parent)
cur = cur->parent;
start = cur;
if (cur->message && cur != hdr->thread)
mutt_set_flag(Context, cur->message, flag, bf);
cur = cur->child;
if (!cur)
goto done;
while (true)
{
if (cur->message && cur != hdr->thread)
mutt_set_flag(Context, cur->message, flag, bf);
if (cur->child)
cur = cur->child;
else if (cur->next)
cur = cur->next;
else
{
while (!cur->next)
{
cur = cur->parent;
if (cur == start)
goto done;
}
cur = cur->next;
}
}
done:
cur = hdr->thread;
if (cur->message)
mutt_set_flag(Context, cur->message, flag, bf);
return 0;
}
int mutt_change_flag(struct Header *h, int bf)
{
int i, flag;
struct Event event;
mutt_window_mvprintw(MuttMessageWindow, 0, 0,
"%s? (D/N/O/r/*/!): ", bf ? _("Set flag") : _("Clear flag"));
mutt_window_clrtoeol(MuttMessageWindow);
event = mutt_getch();
i = event.ch;
if (i < 0)
{
mutt_window_clearline(MuttMessageWindow, 0);
return -1;
}
mutt_window_clearline(MuttMessageWindow, 0);
switch (i)
{
case 'd':
case 'D':
if (!bf)
{
if (h)
mutt_set_flag(Context, h, MUTT_PURGE, bf);
else
mutt_tag_set_flag(MUTT_PURGE, bf);
}
flag = MUTT_DELETE;
break;
case 'N':
case 'n':
flag = MUTT_NEW;
break;
case 'o':
case 'O':
if (h)
mutt_set_flag(Context, h, MUTT_READ, !bf);
else
mutt_tag_set_flag(MUTT_READ, !bf);
flag = MUTT_OLD;
break;
case 'r':
case 'R':
flag = MUTT_REPLIED;
break;
case '*':
flag = MUTT_TAG;
break;
case '!':
flag = MUTT_FLAG;
break;
default:
BEEP();
return -1;
}
if (h)
mutt_set_flag(Context, h, flag, bf);
else
mutt_tag_set_flag(flag, bf);
return 0;
}