NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_autocrypt.c
Go to the documentation of this file.
1
69#include "config.h"
70#include <stdbool.h>
71#include <stdio.h>
72#include "private.h"
73#include "mutt/lib.h"
74#include "address/lib.h"
75#include "config/lib.h"
76#include "core/lib.h"
77#include "gui/lib.h"
78#include "lib.h"
79#include "expando/lib.h"
80#include "key/lib.h"
81#include "menu/lib.h"
82#include "autocrypt_data.h"
83#include "expando.h"
84#include "functions.h"
85#include "mutt_logging.h"
86
88static const struct Mapping AutocryptHelp[] = {
89 // clang-format off
90 { N_("Exit"), OP_EXIT },
91 /* L10N: Autocrypt Account Menu Help line:
92 create new account */
93 { N_("Create"), OP_AUTOCRYPT_CREATE_ACCT },
94 /* L10N: Autocrypt Account Menu Help line:
95 delete account */
96 { N_("Delete"), OP_AUTOCRYPT_DELETE_ACCT },
97 /* L10N: Autocrypt Account Menu Help line:
98 toggle an account active/inactive
99 The words here are abbreviated to keep the help line compact.
100 It currently has the content:
101 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
102 { N_("Tgl Active"), OP_AUTOCRYPT_TOGGLE_ACTIVE },
103 /* L10N: Autocrypt Account Menu Help line:
104 toggle "prefer-encrypt" on an account
105 The words here are abbreviated to keep the help line compact.
106 It currently has the content:
107 q:Exit c:Create D:Delete a:Tgl Active p:Prf Encr ?:Help */
108 { N_("Prf Encr"), OP_AUTOCRYPT_TOGGLE_PREFER },
109 { N_("Help"), OP_HELP },
110 { NULL, 0 }
111 // clang-format on
112};
113
119static int autocrypt_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
120{
121 struct AutocryptData *ad = menu->mdata;
122 struct AccountEntry **pentry = ARRAY_GET(&ad->entries, line);
123 if (!pentry)
124 return 0;
125
126 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
127 if (c_arrow_cursor)
128 {
129 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
130 if (max_cols > 0)
131 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
132 }
133
134 const struct Expando *c_autocrypt_acct_format = cs_subset_expando(NeoMutt->sub, "autocrypt_acct_format");
135 return expando_filter(c_autocrypt_acct_format, AutocryptRenderCallbacks,
136 *pentry, MUTT_FORMAT_ARROWCURSOR, max_cols, buf);
137}
138
144bool populate_menu(struct Menu *menu)
145{
146 struct AutocryptData *ad = menu->mdata;
147
148 // Clear out any existing data
150 menu->max = 0;
151
152 struct AutocryptAccountArray accounts = ARRAY_HEAD_INITIALIZER;
153
154 if (mutt_autocrypt_db_account_get_all(&accounts) < 0)
155 return false;
156
157 menu->max = ARRAY_SIZE(&accounts);
158
159 struct AutocryptAccount **pac = NULL;
160 ARRAY_FOREACH(pac, &accounts)
161 {
162 struct AccountEntry *entry = MUTT_MEM_CALLOC(1, struct AccountEntry);
163
164 entry->num = ARRAY_FOREACH_IDX + 1;
165 /* note: we are transferring the account pointer to the entries
166 * array, and freeing the accounts array below. the account
167 * will be freed in autocrypt_menu_free(). */
168 entry->account = *pac;
169
170 entry->addr = mutt_addr_new();
171 entry->addr->mailbox = buf_new((*pac)->email_addr);
172 mutt_addr_to_local(entry->addr);
173 ARRAY_ADD(&ad->entries, entry);
174 }
175 ARRAY_FREE(&accounts);
176
178 return true;
179}
180
187{
188 if (nc->event_type != NT_CONFIG)
189 return 0;
190 if (!nc->global_data || !nc->event_data)
191 return -1;
192
193 struct EventConfig *ev_c = nc->event_data;
194
195 if (!mutt_str_equal(ev_c->name, "autocrypt_acct_format"))
196 return 0;
197
198 struct Menu *menu = nc->global_data;
200 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
201
202 return 0;
203}
204
213{
214 if (nc->event_type != NT_WINDOW)
215 return 0;
216 if (!nc->global_data || !nc->event_data)
217 return -1;
219 return 0;
220
221 struct MuttWindow *win_menu = nc->global_data;
222 struct EventWindow *ev_w = nc->event_data;
223 if (ev_w->win != win_menu)
224 return 0;
225
226 struct Menu *menu = win_menu->wdata;
227
230
231 mutt_debug(LL_DEBUG5, "window delete done\n");
232 return 0;
233}
234
241{
242 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
243 if (!c_autocrypt)
244 return;
245
246 if (mutt_autocrypt_init(false))
247 return;
248
251
252 struct Menu *menu = sdw.menu;
253
254 struct AutocryptData *ad = autocrypt_data_new();
255 ad->menu = menu;
256
258 menu->mdata = ad;
260
262
263 // L10N: Autocrypt Account Management Menu title
264 sbar_set_title(sdw.sbar, _("Autocrypt Accounts"));
265
266 // NT_COLOR is handled by the SimpleDialog
269
270 struct MuttWindow *old_focus = window_set_focus(menu->win);
271 // ---------------------------------------------------------------------------
272 // Event Loop
273 int op = OP_NULL;
274 do
275 {
276 menu_tagging_dispatcher(menu->win, op);
277 window_redraw(NULL);
278
280 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
281 if (op < 0)
282 continue;
283 if (op == OP_NULL)
284 {
286 continue;
287 }
289
290 int rc = autocrypt_function_dispatcher(sdw.dlg, op);
291
292 if (rc == FR_UNKNOWN)
293 rc = menu_function_dispatcher(menu->win, op);
294 if (rc == FR_UNKNOWN)
295 rc = global_function_dispatcher(NULL, op);
296 } while (!ad->done);
297 // ---------------------------------------------------------------------------
298
299 window_set_focus(old_focus);
301}
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:401
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1340
Email Address Handling.
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:109
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition: array.h:58
int mutt_autocrypt_db_account_get_all(struct AutocryptAccountArray *aaa)
Get all accounts from an Autocrypt database.
Definition: db.c:461
const struct ExpandoRenderCallback AutocryptRenderCallbacks[]
Callbacks for Autocrypt Expandos.
Definition: expando.c:121
int mutt_autocrypt_init(bool can_create)
Initialise Autocrypt.
Definition: autocrypt.c:99
struct AutocryptData * autocrypt_data_new(void)
Create new Autocrypt Data.
void account_entry_array_clear(struct AccountEntryArray *entries)
Clear an AccountEntry array.
Private Autocrypt Data.
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:304
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:291
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
Definition: config_type.c:357
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition: curs_lib.c:443
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
static const struct Mapping AutocryptHelp[]
Help Bar for the Autocrypt Account selection dialog.
Definition: dlg_autocrypt.c:88
bool populate_menu(struct Menu *menu)
Add the Autocrypt data to a Menu.
int expando_filter(const struct Expando *exp, const struct ExpandoRenderCallback *erc, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando and run the result through a filter.
Definition: filter.c:138
Parse Expando string.
int km_dokey(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:464
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: get.c:294
int menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:230
int autocrypt_function_dispatcher(struct MuttWindow *win, int op)
Perform a Autocrypt function - Implements function_dispatcher_t -.
Definition: functions.c:214
int global_function_dispatcher(struct MuttWindow *win, int op)
Perform a Global function - Implements function_dispatcher_t -.
Definition: global.c:172
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:318
void dlg_autocrypt(void)
Display the Autocrypt account Menu -.
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static int autocrypt_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Format an Autocrypt Account for the Menu - Implements Menu::make_entry() -.
void autocrypt_data_free(struct Menu *menu, void **ptr)
Free Autocrypt Data - Implements Menu::mdata_free() -.
static int autocrypt_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
static int autocrypt_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Convenience wrapper for the gui headers.
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:168
struct SimpleDialogWindows simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:132
Manage keymappings.
#define GETCH_NO_FLAGS
No flags are set.
Definition: lib.h:51
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
GUI present the user with a selectable list.
#define MENU_REDRAW_FULL
Redraw everything.
Definition: lib.h:59
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:184
Convenience wrapper for the library headers.
#define N_(a)
Definition: message.h:32
#define _(a)
Definition: message.h:28
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_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:191
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
NeoMutt Logging.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:634
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
Definition: mutt_window.c:684
@ WT_DLG_AUTOCRYPT
Autocrypt Dialog, dlg_autocrypt()
Definition: mutt_window.h:80
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:229
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:57
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:48
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: render.h:37
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:227
Sidebar Expando definitions.
Sidebar functions.
GUI display the mailboxes in a side panel.
Key value store.
An entry in the Autocrypt account Menu.
Definition: private.h:46
struct Address * addr
Email address associated with the account.
Definition: private.h:49
struct AutocryptAccount * account
Account details.
Definition: private.h:48
int num
Number in the index.
Definition: private.h:47
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
Autocrypt account.
Definition: lib.h:110
Data to pass to the Autocrypt Functions.
bool done
Should we close the Dialog?
struct Menu * menu
Autocrypt Menu.
struct AccountEntryArray entries
Account Entries.
String manipulation buffer.
Definition: buffer.h:36
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition: subset.h:52
A config-change event.
Definition: subset.h:71
const char * name
Name of config item that changed.
Definition: subset.h:73
An Event that happened to a Window.
Definition: mutt_window.h:239
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:240
Parsed Expando trees.
Definition: expando.h:41
Mapping between user-readable string and a constant.
Definition: mapping.h:33
Definition: lib.h:79
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:86
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:161
int(* make_entry)(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Definition: lib.h:106
struct ConfigSubset * sub
Inherited config items.
Definition: lib.h:87
void * mdata
Private data.
Definition: lib.h:147
int max
Number of entries in the menu.
Definition: lib.h:81
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
Data passed to a notification function.
Definition: observer.h:34
void * event_data
Data from notify_send()
Definition: observer.h:38
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
Tuple for the results of simple_dialog_new()
Definition: simple.h:35
struct MuttWindow * sbar
Simple Bar.
Definition: simple.h:37
struct Menu * menu
Menu.
Definition: simple.h:38
struct MuttWindow * dlg
Main Dialog Window.
Definition: simple.h:36
@ MENU_AUTOCRYPT
Autocrypt Account menu.
Definition: type.h:40