Coverage Report

Created: 2023-09-25 07:17

/src/neomutt/postpone/functions.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * @file
3
 * Postponed Emails Functions
4
 *
5
 * @authors
6
 * Copyright (C) 2022 Richard Russon <rich@flatcap.org>
7
 *
8
 * @copyright
9
 * This program is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License as published by the Free Software
11
 * Foundation, either version 2 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * This program is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU General Public License along with
20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
/**
24
 * @page postpone_functions Postponed Emails Functions
25
 *
26
 * Postponed Emails Functions
27
 */
28
29
#include "config.h"
30
#ifdef _MAKEDOC
31
#include "docs/makedoc_defs.h"
32
#else
33
#include <stddef.h>
34
#include "mutt/lib.h"
35
#include "config/lib.h"
36
#include "core/lib.h"
37
#include "gui/lib.h"
38
#include "mutt.h"
39
#include "key/lib.h"
40
#include "menu/lib.h"
41
#include "pattern/lib.h"
42
#include "functions.h"
43
#include "mview.h"
44
#include "protos.h"
45
#endif
46
47
// clang-format off
48
/**
49
 * OpPostponed - Functions for the Postpone Menu
50
 */
51
const struct MenuFuncOp OpPostponed[] = { /* map: postpone */
52
  { "exit",                          OP_EXIT },
53
  { "delete-entry",                  OP_DELETE },
54
  { "undelete-entry",                OP_UNDELETE },
55
  { NULL, 0 },
56
};
57
58
/**
59
 * PostponedDefaultBindings - Key bindings for the Postpone Menu
60
 */
61
const struct MenuOpSeq PostponedDefaultBindings[] = { /* map: postpone */
62
  { OP_DELETE,                             "d" },
63
  { OP_EXIT,                               "q" },
64
  { OP_UNDELETE,                           "u" },
65
  { 0, NULL },
66
};
67
// clang-format on
68
69
/**
70
 * op_delete - Delete the current entry - Implements ::postpone_function_t - @ingroup postpone_function_api
71
 */
72
static int op_delete(struct PostponeData *pd, int op)
73
{
74
  struct Menu *menu = pd->menu;
75
  struct MailboxView *mv = pd->mailbox_view;
76
  struct Mailbox *m = mv->mailbox;
77
78
  const int index = menu_get_index(menu);
79
  /* should deleted draft messages be saved in the trash folder? */
80
  mutt_set_flag(m, m->emails[index], MUTT_DELETE, (op == OP_DELETE), true);
81
  PostCount = m->msg_count - m->msg_deleted;
82
  const bool c_resolve = cs_subset_bool(NeoMutt->sub, "resolve");
83
  if (c_resolve && (index < (menu->max - 1)))
84
  {
85
    menu_set_index(menu, index + 1);
86
    if (index >= (menu->top + menu->page_len))
87
    {
88
      menu->top = index;
89
      menu_queue_redraw(menu, MENU_REDRAW_INDEX);
90
    }
91
  }
92
  else
93
  {
94
    menu_queue_redraw(menu, MENU_REDRAW_CURRENT);
95
  }
96
97
  return FR_SUCCESS;
98
}
99
100
/**
101
 * op_exit - Exit this menu - Implements ::postpone_function_t - @ingroup postpone_function_api
102
 */
103
static int op_exit(struct PostponeData *pd, int op)
104
{
105
  pd->done = true;
106
  return FR_SUCCESS;
107
}
108
109
/**
110
 * op_generic_select_entry - Select the current entry - Implements ::postpone_function_t - @ingroup postpone_function_api
111
 */
112
static int op_generic_select_entry(struct PostponeData *pd, int op)
113
{
114
  int index = menu_get_index(pd->menu);
115
  struct MailboxView *mv = pd->mailbox_view;
116
  struct Mailbox *m = mv->mailbox;
117
  pd->email = m->emails[index];
118
  pd->done = true;
119
  return FR_SUCCESS;
120
}
121
122
/**
123
 * op_search - Search for a regular expression - Implements ::postpone_function_t - @ingroup postpone_function_api
124
 */
125
static int op_search(struct PostponeData *pd, int op)
126
{
127
  SearchFlags flags = SEARCH_NO_FLAGS;
128
  switch (op)
129
  {
130
    case OP_SEARCH:
131
      flags |= SEARCH_PROMPT;
132
      pd->search_state->reverse = false;
133
      break;
134
    case OP_SEARCH_REVERSE:
135
      flags |= SEARCH_PROMPT;
136
      pd->search_state->reverse = true;
137
      break;
138
    case OP_SEARCH_NEXT:
139
      break;
140
    case OP_SEARCH_OPPOSITE:
141
      flags |= SEARCH_OPPOSITE;
142
      break;
143
  }
144
145
  int index = menu_get_index(pd->menu);
146
  struct MailboxView *mv = pd->mailbox_view;
147
  index = mutt_search_command(mv, pd->menu, index, pd->search_state, flags);
148
  if (index != -1)
149
    menu_set_index(pd->menu, index);
150
151
  return FR_SUCCESS;
152
}
153
154
// -----------------------------------------------------------------------------
155
156
/**
157
 * PostponeFunctions - All the NeoMutt functions that the Postpone supports
158
 */
159
static const struct PostponeFunction PostponeFunctions[] = {
160
  // clang-format off
161
  { OP_DELETE,                 op_delete },
162
  { OP_EXIT,                   op_exit },
163
  { OP_GENERIC_SELECT_ENTRY,   op_generic_select_entry },
164
  { OP_SEARCH,                 op_search },
165
  { OP_SEARCH_NEXT,            op_search },
166
  { OP_SEARCH_OPPOSITE,        op_search },
167
  { OP_SEARCH_REVERSE,         op_search },
168
  { OP_UNDELETE,               op_delete },
169
  { 0, NULL },
170
  // clang-format on
171
};
172
173
/**
174
 * postpone_function_dispatcher - Perform a Postpone function - Implements ::function_dispatcher_t - @ingroup dispatcher_api
175
 */
176
int postpone_function_dispatcher(struct MuttWindow *win, int op)
177
0
{
178
0
  if (!win || !win->wdata)
179
0
    return FR_UNKNOWN;
180
181
0
  struct MuttWindow *dlg = dialog_find(win);
182
0
  if (!dlg)
183
0
    return FR_ERROR;
184
185
0
  struct PostponeData *pd = dlg->wdata;
186
187
0
  int rc = FR_UNKNOWN;
188
0
  for (size_t i = 0; PostponeFunctions[i].op != OP_NULL; i++)
189
0
  {
190
0
    const struct PostponeFunction *fn = &PostponeFunctions[i];
191
0
    if (fn->op == op)
192
0
    {
193
0
      rc = fn->function(pd, op);
194
0
      break;
195
0
    }
196
0
  }
197
198
0
  if (rc == FR_UNKNOWN) // Not our function
199
0
    return rc;
200
201
0
  const char *result = dispatcher_get_retval_name(rc);
202
0
  mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
203
204
0
  return rc;
205
0
}
206
207
/**
208
 * postponed_get_mailbox_view - Extract the Mailbox from the Postponed Dialog
209
 * @param dlg Postponed Dialog
210
 * @retval ptr Mailbox view
211
 */
212
struct MailboxView *postponed_get_mailbox_view(struct MuttWindow *dlg)
213
0
{
214
0
  if (!dlg)
215
0
    return NULL;
216
217
0
  struct PostponeData *pd = dlg->wdata;
218
0
  if (!pd)
219
0
    return NULL;
220
221
0
  return pd->mailbox_view;
222
0
}