NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
dlgautocrypt.c File Reference

Autocrypt account menu. More...

#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "private.h"
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "lib.h"
#include "menu/lib.h"
#include "question/lib.h"
#include "format_flags.h"
#include "muttlib.h"
#include "opcodes.h"
+ Include dependency graph for dlgautocrypt.c:

Go to the source code of this file.

Data Structures

struct  AccountEntry
 An entry in the Autocrypt account Menu. More...
 

Functions

static const char * autocrypt_format_str (char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
 Format a string for the Autocrypt account list - Implements format_t -
Expando Description
%a Email address
%k Gpg keyid
%n Current entry number
%p Prefer-encrypt flag
%s Status flag (active/inactive)
More...
 
static void autocrypt_make_entry (struct Menu *menu, char *buf, size_t buflen, int num)
 Create a line for the Autocrypt account menu - Implements Menu::make_entry() -. More...
 
static void autocrypt_menu_free (struct Menu *menu, void **ptr)
 Free the Autocrypt account Menu - Implements Menu::mdata_free() -. More...
 
static bool populate_menu (struct Menu *menu)
 Add the Autocrypt data to a Menu. More...
 
static void toggle_active (struct AccountEntry *entry)
 Toggle whether an Autocrypt account is active. More...
 
static void toggle_prefer_encrypt (struct AccountEntry *entry)
 Toggle whether an Autocrypt account prefers encryption. More...
 
static int autocrypt_config_observer (struct NotifyCallback *nc)
 Notification that a Config Variable has changed - Implements observer_t. More...
 
static int autocrypt_window_observer (struct NotifyCallback *nc)
 Notification that a Window has changed - Implements observer_t. More...
 
void dlg_select_autocrypt_account (struct Mailbox *m)
 Display the Autocrypt account Menu. More...
 

Variables

static const struct Mapping AutocryptAcctHelp []
 Help Bar for the Autocrypt Account selection dialog. More...
 

Detailed Description

Autocrypt account menu.

Authors
  • Kevin J. McCarthy

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

Function Documentation

◆ populate_menu()

static bool populate_menu ( struct Menu menu)
static

Add the Autocrypt data to a Menu.

Parameters
menuMenu to populate
Return values
trueSuccess

Definition at line 223 of file dlgautocrypt.c.

224 {
225  // Clear out any existing data
226  autocrypt_menu_free(menu, &menu->mdata);
227  menu->max = 0;
228 
229  struct AutocryptAccount **accounts = NULL;
230  int num_accounts = 0;
231 
232  if (mutt_autocrypt_db_account_get_all(&accounts, &num_accounts) < 0)
233  return false;
234 
235  struct AccountEntry *entries =
236  mutt_mem_calloc(num_accounts, sizeof(struct AccountEntry));
237  menu->mdata = entries;
239  menu->max = num_accounts;
240 
241  for (int i = 0; i < num_accounts; i++)
242  {
243  entries[i].num = i + 1;
244  /* note: we are transferring the account pointer to the entries
245  * array, and freeing the accounts array below. the account
246  * will be freed in autocrypt_menu_free(). */
247  entries[i].account = accounts[i];
248 
249  entries[i].addr = mutt_addr_new();
250  entries[i].addr->mailbox = mutt_str_dup(accounts[i]->email_addr);
251  mutt_addr_to_local(entries[i].addr);
252  }
253  FREE(&accounts);
254 
256  return true;
257 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:385
char * mailbox
Mailbox and host address.
Definition: address.h:38
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1351
An entry in the Autocrypt account Menu.
Definition: dlgautocrypt.c:89
void * mdata
Private data.
Definition: lib.h:155
struct Address * addr
Definition: dlgautocrypt.c:93
int max
Number of entries in the menu.
Definition: lib.h:71
Autocrypt account.
Definition: lib.h:103
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:170
struct AutocryptAccount * account
Definition: dlgautocrypt.c:92
#define FREE(x)
Definition: memory.h:40
static void autocrypt_menu_free(struct Menu *menu, void **ptr)
Free the Autocrypt account Menu - Implements Menu::mdata_free() -.
Definition: dlgautocrypt.c:205
int mutt_autocrypt_db_account_get_all(struct AutocryptAccount ***accounts, int *num_accounts)
Get all accounts from an Autocrypt database.
Definition: db.c:456
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toggle_active()

static void toggle_active ( struct AccountEntry entry)
static

Toggle whether an Autocrypt account is active.

Parameters
entryMenu Entry for the account

Definition at line 263 of file dlgautocrypt.c.

264 {
265  entry->account->enabled = !entry->account->enabled;
266  if (mutt_autocrypt_db_account_update(entry->account) != 0)
267  {
268  entry->account->enabled = !entry->account->enabled;
269  /* L10N: This error message is displayed if a database update of an
270  account record fails for some odd reason. */
271  mutt_error(_("Error updating account record"));
272  }
273 }
#define mutt_error(...)
Definition: logging.h:88
#define _(a)
Definition: message.h:28
bool enabled
Definition: lib.h:109
int mutt_autocrypt_db_account_update(struct AutocryptAccount *acct)
Update Account info in the Autocrypt database.
Definition: db.c:375
struct AutocryptAccount * account
Definition: dlgautocrypt.c:92
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toggle_prefer_encrypt()

static void toggle_prefer_encrypt ( struct AccountEntry entry)
static

Toggle whether an Autocrypt account prefers encryption.

Parameters
entryMenu Entry for the account

Definition at line 279 of file dlgautocrypt.c.

280 {
281  entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
283  {
284  entry->account->prefer_encrypt = !entry->account->prefer_encrypt;
285  mutt_error(_("Error updating account record"));
286  }
287 }
#define mutt_error(...)
Definition: logging.h:88
#define _(a)
Definition: message.h:28
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:108
int mutt_autocrypt_db_account_update(struct AutocryptAccount *acct)
Update Account info in the Autocrypt database.
Definition: db.c:375
struct AutocryptAccount * account
Definition: dlgautocrypt.c:92
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ autocrypt_config_observer()

static int autocrypt_config_observer ( struct NotifyCallback nc)
static

Notification that a Config Variable has changed - Implements observer_t.

The Address Book Window is affected by changes to $sort_autocrypt.

Definition at line 294 of file dlgautocrypt.c.

295 {
296  if ((nc->event_type != NT_CONFIG) || !nc->global_data || !nc->event_data)
297  return -1;
298 
299  struct EventConfig *ev_c = nc->event_data;
300 
301  if (!mutt_str_equal(ev_c->name, "autocrypt_acct_format"))
302  return 0;
303 
304  struct Menu *menu = nc->global_data;
306  mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
307 
308  return 0;
309 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
Definition: lib.h:67
A config-change event.
Definition: subset.h:69
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
void * event_data
Data from notify_send()
Definition: observer.h:44
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:42
Log at debug level 5.
Definition: logging.h:44
const char * name
Name of config item that changed.
Definition: subset.h:72
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ autocrypt_window_observer()

static int autocrypt_window_observer ( struct NotifyCallback nc)
static

Notification that a Window has changed - Implements observer_t.

This function is triggered by changes to the windows.

  • Delete (this window): clean up the resources held by the Help Bar

Definition at line 318 of file dlgautocrypt.c.

319 {
320  if ((nc->event_type != NT_WINDOW) || !nc->global_data || !nc->event_data)
321  return -1;
322 
323  if (nc->event_subtype != NT_WINDOW_DELETE)
324  return 0;
325 
326  struct MuttWindow *win_menu = nc->global_data;
327  struct EventWindow *ev_w = nc->event_data;
328  if (ev_w->win != win_menu)
329  return 0;
330 
331  struct Menu *menu = win_menu->wdata;
332 
335 
336  mutt_debug(LL_DEBUG5, "window delete done\n");
337  return 0;
338 }
Definition: lib.h:67
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
An Event that happened to a Window.
Definition: mutt_window.h:215
Window is about to be deleted.
Definition: mutt_window.h:206
Container for Accounts, Notifications.
Definition: neomutt.h:36
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:43
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
static int autocrypt_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t.
Definition: dlgautocrypt.c:294
static int autocrypt_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t.
Definition: dlgautocrypt.c:318
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
void * event_data
Data from notify_send()
Definition: observer.h:44
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
Log at debug level 5.
Definition: logging.h:44
void * wdata
Private data.
Definition: mutt_window.h:145
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dlg_select_autocrypt_account()

void dlg_select_autocrypt_account ( struct Mailbox m)

Display the Autocrypt account Menu.

Parameters
mMailbox

Definition at line 344 of file dlgautocrypt.c.

345 {
346  const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
347  if (!c_autocrypt)
348  return;
349 
350  if (mutt_autocrypt_init(m, false))
351  return;
352 
353  struct MuttWindow *dlg =
355 
356  struct Menu *menu = dlg->wdata;
358 
359  populate_menu(menu);
360 
361  struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
362  // L10N: Autocrypt Account Management Menu title
363  sbar_set_title(sbar, _("Autocrypt Accounts"));
364 
365  struct MuttWindow *win_menu = menu->win;
366 
367  // NT_COLOR is handled by the SimpleDialog
370 
371  bool done = false;
372  while (!done)
373  {
374  switch (menu_loop(menu))
375  {
376  case OP_EXIT:
377  done = true;
378  break;
379 
380  case OP_AUTOCRYPT_CREATE_ACCT:
381  if (mutt_autocrypt_account_init(false) == 0)
382  populate_menu(menu);
383  break;
384 
385  case OP_AUTOCRYPT_DELETE_ACCT:
386  {
387  if (!menu->mdata)
388  break;
389 
390  const int index = menu_get_index(menu);
391  struct AccountEntry *entry = ((struct AccountEntry *) menu->mdata) + index;
392  char msg[128];
393  snprintf(msg, sizeof(msg),
394  // L10N: Confirmation message when deleting an autocrypt account
395  _("Really delete account \"%s\"?"), entry->addr->mailbox);
396  if (mutt_yesorno(msg, MUTT_NO) != MUTT_YES)
397  break;
398 
399  if (mutt_autocrypt_db_account_delete(entry->account) == 0)
400  populate_menu(menu);
401 
402  break;
403  }
404 
405  case OP_AUTOCRYPT_TOGGLE_ACTIVE:
406  {
407  if (!menu->mdata)
408  break;
409 
410  const int index = menu_get_index(menu);
411  struct AccountEntry *entry = ((struct AccountEntry *) menu->mdata) + index;
412  toggle_active(entry);
414  break;
415  }
416 
417  case OP_AUTOCRYPT_TOGGLE_PREFER:
418  {
419  if (!menu->mdata)
420  break;
421 
422  const int index = menu_get_index(menu);
423  struct AccountEntry *entry = (struct AccountEntry *) (menu->mdata) + index;
424  toggle_prefer_encrypt(entry);
426  break;
427  }
428  }
429  }
430 
431  simple_dialog_free(&dlg);
432 }
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:550
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:165
Definition: lib.h:67
static const struct Mapping AutocryptAcctHelp[]
Help Bar for the Autocrypt Account selection dialog.
Definition: dlgautocrypt.c:97
#define _(a)
Definition: message.h:28
char * mailbox
Mailbox and host address.
Definition: address.h:38
Container for Accounts, Notifications.
Definition: neomutt.h:36
static void toggle_prefer_encrypt(struct AccountEntry *entry)
Toggle whether an Autocrypt account prefers encryption.
Definition: dlgautocrypt.c:279
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition: autocrypt.c:155
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:105
int mutt_autocrypt_db_account_delete(struct AutocryptAccount *acct)
Delete an Account from the Autocrypt database.
Definition: db.c:421
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
An entry in the Autocrypt account Menu.
Definition: dlgautocrypt.c:89
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:76
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:189
Autocrypt Dialog, dlg_select_autocrypt_account()
Definition: mutt_window.h:79
int mutt_autocrypt_init(struct Mailbox *m, bool can_create)
Initialise Autocrypt.
Definition: autocrypt.c:98
static void toggle_active(struct AccountEntry *entry)
Toggle whether an Autocrypt account is active.
Definition: dlgautocrypt.c:263
void * mdata
Private data.
Definition: lib.h:155
struct Address * addr
Definition: dlgautocrypt.c:93
static int autocrypt_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t.
Definition: dlgautocrypt.c:294
static void autocrypt_make_entry(struct Menu *menu, char *buf, size_t buflen, int num)
Create a line for the Autocrypt account menu - Implements Menu::make_entry() -.
Definition: dlgautocrypt.c:191
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:221
static int autocrypt_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t.
Definition: dlgautocrypt.c:318
static bool populate_menu(struct Menu *menu)
Add the Autocrypt data to a Menu.
Definition: dlgautocrypt.c:223
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
Autocrypt Account menu.
Definition: type.h:40
enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:180
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:42
struct AutocryptAccount * account
Definition: dlgautocrypt.c:92
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:38
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:128
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
void * wdata
Private data.
Definition: mutt_window.h:145
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ AutocryptAcctHelp

const struct Mapping AutocryptAcctHelp[]
static
Initial value:
= {
{ N_("Exit"), OP_EXIT },
{ N_("Create"), OP_AUTOCRYPT_CREATE_ACCT },
{ N_("Delete"), OP_AUTOCRYPT_DELETE_ACCT },
{ N_("Tgl Active"), OP_AUTOCRYPT_TOGGLE_ACTIVE },
{ N_("Prf Encr"), OP_AUTOCRYPT_TOGGLE_PREFER },
{ N_("Help"), OP_HELP },
{ NULL, 0 }
}
#define N_(a)
Definition: message.h:32

Help Bar for the Autocrypt Account selection dialog.

Definition at line 97 of file dlgautocrypt.c.