NeoMutt  2021-02-05-89-gabe350
Teaching an old dog new tricks
DOXYGEN
notify.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include "mutt/lib.h"
31 #include "config/lib.h"
32 #include "email/lib.h"
33 #include "core/lib.h"
34 #include "gui/lib.h"
35 #include "lib.h"
36 #include "context.h"
37 #include "mutt_globals.h"
38 
39 extern const struct Mapping Fields[];
40 extern const struct Mapping ComposeFields[];
41 
42 static const char *get_event_type(enum NotifyType type)
43 {
44  switch (type)
45  {
46  case NT_ACCOUNT:
47  return "account";
48  case NT_COLOR:
49  return "color";
50  case NT_COMMAND:
51  return "command";
52  case NT_CONFIG:
53  return "config";
54  case NT_CONTEXT:
55  return "context";
56  case NT_EMAIL:
57  return "email";
58  case NT_GLOBAL:
59  return "global";
60  case NT_MAILBOX:
61  return "mailbox";
62  case NT_WINDOW:
63  return "window";
64  default:
65  return "UNKNOWN";
66  }
67 }
68 
69 static const char *get_mailbox_type(enum MailboxType type)
70 {
71  switch (type)
72  {
73  case MUTT_COMPRESSED:
74  return "compressed";
75  case MUTT_IMAP:
76  return "imap";
77  case MUTT_MAILDIR:
78  return "maildir";
79  case MUTT_MBOX:
80  return "mbox";
81  case MUTT_MH:
82  return "mh";
83  case MUTT_MMDF:
84  return "mmdf";
85  case MUTT_NNTP:
86  return "nntp";
87  case MUTT_NOTMUCH:
88  return "notmuch";
89  case MUTT_POP:
90  return "pop";
91  default:
92  return "UNKNOWN";
93  }
94 }
95 
96 static const char *get_global_event(int id)
97 {
98  switch (id)
99  {
100  case NT_GLOBAL_SHUTDOWN:
101  return "shutdown";
102  case NT_GLOBAL_STARTUP:
103  return "startup";
104  case NT_GLOBAL_TIMEOUT:
105  return "timeout";
106  default:
107  return "UNKNOWN";
108  }
109 }
110 
111 static const char *get_config_type(int id)
112 {
113  switch (id)
114  {
115  case NT_CONFIG_SET:
116  return "set";
117  case NT_CONFIG_RESET:
118  return "reset";
120  return "initial_set";
121  default:
122  return "UNKNOWN";
123  }
124 }
125 
126 static const char *get_mailbox_event(int id)
127 {
128  switch (id)
129  {
130  case NT_MAILBOX_ADD:
131  return "add";
132  case NT_MAILBOX_REMOVE:
133  return "remove";
134  case NT_MAILBOX_CLOSED:
135  return "closed";
136  case NT_MAILBOX_INVALID:
137  return "invalid";
138  case NT_MAILBOX_RESORT:
139  return "resort";
140  case NT_MAILBOX_UPDATE:
141  return "update";
142  case NT_MAILBOX_UNTAG:
143  return "untag";
144  default:
145  return "UNKNOWN";
146  }
147 }
148 
149 static const char *get_context(int id)
150 {
151  switch (id)
152  {
153  case NT_CONTEXT_CLOSE:
154  return "close";
155  case NT_CONTEXT_OPEN:
156  return "open";
157  default:
158  return "UNKNOWN";
159  }
160 }
161 
162 static void notify_dump_account(struct NotifyCallback *nc)
163 {
164  struct EventAccount *ev_a = nc->event_data;
165  struct Account *a = ev_a->account;
166 
167  mutt_debug(LL_DEBUG1, " Account: %p (%s) %s\n", a,
168  get_mailbox_type(a->type), NONULL(a->name));
169 }
170 
171 static void notify_dump_color(struct NotifyCallback *nc)
172 {
173  struct EventColor *ev_c = nc->event_data;
174 
175  const char *color = NULL;
176  const char *scope = "";
177 
178  if (ev_c->color == MT_COLOR_MAX)
179  color = "ALL";
180 
181  if (!color)
183 
184  if (!color)
185  {
187  scope = "compose ";
188  }
189 
190  if (!color)
191  color = "UNKNOWN";
192 
193  mutt_debug(LL_DEBUG1, " Color: %s %s%s (%d)\n",
194  (nc->event_subtype == NT_COLOR_SET) ? "set" : "reset", scope,
195  color, ev_c->color);
196 }
197 
198 static void notify_dump_command(struct NotifyCallback *nc)
199 {
200  struct Command *cmd = nc->event_data;
201 
202  if (cmd->data < 4096)
203  mutt_debug(LL_DEBUG1, " Command: %s, data: %ld\n", cmd->name, cmd->data);
204  else
205  mutt_debug(LL_DEBUG1, " Command: %s, data: %p\n", cmd->name, (void *) cmd->data);
206 }
207 
208 static void notify_dump_config(struct NotifyCallback *nc)
209 {
210  struct EventConfig *ev_c = nc->event_data;
211 
212  struct Buffer value = mutt_buffer_make(128);
213  cs_he_string_get(ev_c->sub->cs, ev_c->he, &value);
214  mutt_debug(LL_DEBUG1, " Config: %s %s = %s\n", get_config_type(nc->event_subtype),
215  ev_c->name, mutt_buffer_string(&value));
216  mutt_buffer_dealloc(&value);
217 }
218 
219 static void notify_dump_context(struct NotifyCallback *nc)
220 {
221  struct EventContext *ev_c = nc->event_data;
222 
223  const char *path = "NONE";
224  if (ev_c->context && ev_c->context->mailbox)
225  path = mailbox_path(ev_c->context->mailbox);
226 
227  mutt_debug(LL_DEBUG1, " Context: %s %s\n", get_context(nc->event_subtype), path);
228 }
229 
230 static void notify_dump_email(struct NotifyCallback *nc)
231 {
232  struct EventEmail *ev_e = nc->event_data;
233 
234  mutt_debug(LL_DEBUG1, " Email: %d\n", ev_e->num_emails);
235  for (size_t i = 0; i < ev_e->num_emails; i++)
236  {
237  mutt_debug(LL_DEBUG1, " : %p\n", ev_e->emails[i]);
238  }
239 }
240 
241 static void notify_dump_global(struct NotifyCallback *nc)
242 {
243  mutt_debug(LL_DEBUG1, " Global: %s\n", get_global_event(nc->event_subtype));
244 }
245 
246 static void notify_dump_mailbox(struct NotifyCallback *nc)
247 {
248  struct EventMailbox *ev_m = nc->event_data;
249 
250  struct Mailbox *m = ev_m->mailbox;
251  const char *path = m ? mailbox_path(m) : "";
252  mutt_debug(LL_DEBUG1, " Mailbox: %s %s\n", get_mailbox_event(nc->event_subtype), path);
253 }
254 
256 {
257  struct EventWindow *ev_w = nc->event_data;
258  const struct MuttWindow *win = ev_w->win;
259  WindowNotifyFlags flags = ev_w->flags;
260 
261  struct Buffer buf = mutt_buffer_make(128);
262 
263  mutt_buffer_add_printf(&buf, "[%s] ", win_name(win));
264 
265  if (flags & WN_VISIBLE)
266  mutt_buffer_addstr(&buf, "visible ");
267  if (flags & WN_HIDDEN)
268  mutt_buffer_addstr(&buf, "hidden ");
269 
270  if (flags & WN_MOVED)
271  {
272  mutt_buffer_add_printf(&buf, "moved (C%d,R%d)->(C%d,R%d) ",
273  win->old.col_offset, win->old.row_offset,
274  win->state.col_offset, win->state.row_offset);
275  }
276 
277  if (flags & WN_TALLER)
278  mutt_buffer_add_printf(&buf, "taller [%d->%d] ", win->old.rows, win->state.rows);
279  if (flags & WN_SHORTER)
280  mutt_buffer_add_printf(&buf, "shorter [%d->%d] ", win->old.rows, win->state.rows);
281  if (flags & WN_WIDER)
282  mutt_buffer_add_printf(&buf, "wider [%d->%d] ", win->old.cols, win->state.cols);
283  if (flags & WN_NARROWER)
284  mutt_buffer_add_printf(&buf, "narrower [%d->%d] ", win->old.cols, win->state.cols);
285 
286  mutt_debug(LL_DEBUG1, " Window: %s\n", mutt_buffer_string(&buf));
287 
288  mutt_buffer_dealloc(&buf);
289 }
290 
292 {
293  struct EventWindow *ev_w = nc->event_data;
294  struct MuttWindow *win = ev_w->win;
295 
296  struct Buffer buf = mutt_buffer_make(128);
297 
298  mutt_buffer_addstr(&buf, "Focus: ");
299 
300  if (win)
301  {
302  struct MuttWindow *dlg = dialog_find(win);
303  if (dlg && (dlg != win))
304  mutt_buffer_add_printf(&buf, "%s:", win_name(dlg));
305 
306  mutt_buffer_add_printf(&buf, "%s ", win_name(win));
307 
308  mutt_buffer_add_printf(&buf, "(C%d,R%d) [%dx%d]", win->state.col_offset,
309  win->state.row_offset, win->state.cols, win->state.rows);
310  }
311  else
312  {
313  mutt_buffer_addstr(&buf, "NONE");
314  }
315 
316  mutt_debug(LL_DEBUG1, " Window: %s\n", mutt_buffer_string(&buf));
317 
318  mutt_buffer_dealloc(&buf);
319 }
320 
322 {
323  mutt_debug(LL_DEBUG1, "\033[1;31mNotification:\033[0m %s\n", get_event_type(nc->event_type));
324 
325  switch (nc->event_type)
326  {
327  case NT_ACCOUNT:
329  break;
330  case NT_COLOR:
331  notify_dump_color(nc);
332  break;
333  case NT_COMMAND:
335  break;
336  case NT_CONFIG:
337  notify_dump_config(nc);
338  break;
339  case NT_CONTEXT:
341  break;
342  case NT_EMAIL:
343  notify_dump_email(nc);
344  break;
345  case NT_GLOBAL:
346  notify_dump_global(nc);
347  break;
348  case NT_MAILBOX:
350  break;
351  case NT_WINDOW:
352  if (nc->event_subtype == NT_WINDOW_STATE)
354  else if (nc->event_subtype == NT_WINDOW_FOCUS)
356  break;
357  default:
358  mutt_debug(LL_DEBUG1, " Event Type: %d\n", nc->event_type);
359  mutt_debug(LL_DEBUG1, " Event Sub-type: %d\n", nc->event_subtype);
360  mutt_debug(LL_DEBUG1, " Event Data: %p\n", nc->event_data);
361  break;
362  }
363 
364  mutt_debug(LL_DEBUG1, " Global Data: %p\n", nc->global_data);
365 
366  return 0;
367 }
get_global_event
static const char * get_global_event(int id)
Definition: notify.c:96
NT_WINDOW_STATE
@ NT_WINDOW_STATE
Window state has changed, e.g. WN_VISIBLE.
Definition: mutt_window.h:183
get_event_type
static const char * get_event_type(enum NotifyType type)
Definition: notify.c:42
WN_NARROWER
#define WN_NARROWER
Window became narrower.
Definition: mutt_window.h:169
NotifyCallback
Data passed to a notification function.
Definition: observer.h:39
MUTT_MMDF
@ MUTT_MMDF
'mmdf' Mailbox type
Definition: mailbox.h:49
get_mailbox_event
static const char * get_mailbox_event(int id)
Definition: notify.c:126
EventWindow::flags
WindowNotifyFlags flags
Attributes of Window that changed.
Definition: mutt_window.h:194
NT_MAILBOX_REMOVE
@ NT_MAILBOX_REMOVE
A Mailbox is about to be destroyed.
Definition: mailbox.h:167
NONULL
#define NONULL(x)
Definition: string2.h:37
Mailbox
A mailbox.
Definition: mailbox.h:81
NT_CONFIG
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:37
NT_MAILBOX_INVALID
@ NT_MAILBOX_INVALID
Email list was changed.
Definition: mailbox.h:173
Buffer
String manipulation buffer.
Definition: buffer.h:33
Account::type
enum MailboxType type
Type of Mailboxes this Account contains.
Definition: account.h:38
NT_GLOBAL_STARTUP
@ NT_GLOBAL_STARTUP
NeoMutt is initialised.
Definition: neomutt.h:50
EventContext
An Event that happened to an Context.
Definition: context.h:68
notify_dump_window_focus
static void notify_dump_window_focus(struct NotifyCallback *nc)
Definition: notify.c:291
mutt_buffer_dealloc
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
notify_dump_config
static void notify_dump_config(struct NotifyCallback *nc)
Definition: notify.c:208
cs_he_string_get
int cs_he_string_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
Get a config item as a string.
Definition: set.c:636
mutt_buffer_add_printf
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:203
NT_GLOBAL
@ NT_GLOBAL
Not object-related, NotifyGlobal.
Definition: notify_type.h:40
dialog_find
struct MuttWindow * dialog_find(struct MuttWindow *win)
Find the parent Dialog of a Window.
Definition: dialog.c:46
MUTT_POP
@ MUTT_POP
'POP3' Mailbox type
Definition: mailbox.h:55
Account::name
char * name
Name of Account.
Definition: account.h:39
EventWindow::win
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:193
MuttWindow
A division of the screen.
Definition: mutt_window.h:115
EventAccount
An Event that happened to an Account.
Definition: account.h:70
notify_dump_context
static void notify_dump_context(struct NotifyCallback *nc)
Definition: notify.c:219
WindowState::row_offset
short row_offset
Absolute on-screen row.
Definition: mutt_window.h:60
NT_MAILBOX_UNTAG
@ NT_MAILBOX_UNTAG
Clear the 'last-tagged' pointer.
Definition: mailbox.h:177
mutt_globals.h
LL_DEBUG1
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
NT_GLOBAL_TIMEOUT
@ NT_GLOBAL_TIMEOUT
A timer has elapsed.
Definition: neomutt.h:52
EventEmail::emails
struct Email ** emails
Definition: email.h:149
get_mailbox_type
static const char * get_mailbox_type(enum MailboxType type)
Definition: notify.c:69
WN_TALLER
#define WN_TALLER
Window became taller.
Definition: mutt_window.h:166
WN_HIDDEN
#define WN_HIDDEN
Window became hidden.
Definition: mutt_window.h:172
NT_COLOR_SET
@ NT_COLOR_SET
A new Color has been set.
Definition: color.h:171
WN_SHORTER
#define WN_SHORTER
Window became shorter.
Definition: mutt_window.h:167
NT_WINDOW
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:43
get_config_type
static const char * get_config_type(int id)
Definition: notify.c:111
Mapping
Mapping between user-readable string and a constant.
Definition: mapping.h:31
EventMailbox
An Event that happened to a Mailbox.
Definition: mailbox.h:183
lib.h
NT_CONFIG_SET
@ NT_CONFIG_SET
Config item has been set.
Definition: subset.h:62
MUTT_COMPRESSED
@ MUTT_COMPRESSED
Compressed file Mailbox type.
Definition: mailbox.h:56
NT_MAILBOX
@ NT_MAILBOX
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:42
NT_GLOBAL_SHUTDOWN
@ NT_GLOBAL_SHUTDOWN
NeoMutt is about to close.
Definition: neomutt.h:51
Command
A user-callable command.
Definition: mutt_commands.h:45
WindowState::col_offset
short col_offset
Absolute on-screen column.
Definition: mutt_window.h:59
NotifyType
NotifyType
Notification Types.
Definition: notify_type.h:31
NT_COMMAND
@ NT_COMMAND
A Command has been executed, Command.
Definition: notify_type.h:36
MUTT_NNTP
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition: mailbox.h:52
EventEmail::num_emails
int num_emails
Definition: email.h:148
NT_MAILBOX_UPDATE
@ NT_MAILBOX_UPDATE
Update internal tables.
Definition: mailbox.h:176
Fields
const struct Mapping Fields[]
Definition: color.c:99
lib.h
Account
A group of associated Mailboxes.
Definition: account.h:36
notify_dump_color
static void notify_dump_color(struct NotifyCallback *nc)
Definition: notify.c:171
MuttWindow::old
struct WindowState old
Previous state of the Window.
Definition: mutt_window.h:121
NT_COLOR
@ NT_COLOR
Colour has changed, ColorId, EventColor.
Definition: notify_type.h:35
lib.h
win_name
const char * win_name(const struct MuttWindow *win)
Definition: common.c:34
ConfigSubset::cs
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
EventConfig::he
struct HashElem * he
Config item that changed.
Definition: subset.h:74
NotifyCallback::global_data
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
EventAccount::account
struct Account * account
The Account this Event relates to.
Definition: account.h:72
MT_COLOR_MAX
@ MT_COLOR_MAX
Definition: color.h:112
debug_notify_observer
int debug_notify_observer(struct NotifyCallback *nc)
Definition: notify.c:321
MUTT_NOTMUCH
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:54
MUTT_IMAP
@ MUTT_IMAP
'IMAP' Mailbox type
Definition: mailbox.h:53
WN_VISIBLE
#define WN_VISIBLE
Window became visible.
Definition: mutt_window.h:171
Context::mailbox
struct Mailbox * mailbox
Definition: context.h:50
NT_CONTEXT_OPEN
@ NT_CONTEXT_OPEN
The Context has been opened.
Definition: context.h:61
MUTT_MH
@ MUTT_MH
'MH' Mailbox type
Definition: mailbox.h:50
mutt_debug
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
EventColor
An Event that happened to a Colour.
Definition: color.h:159
notify_dump_command
static void notify_dump_command(struct NotifyCallback *nc)
Definition: notify.c:198
mutt_buffer_string
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
WindowState::rows
short rows
Number of rows, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:58
EventConfig
A config-change event.
Definition: subset.h:70
NotifyCallback::event_data
void * event_data
Data from notify_send()
Definition: observer.h:44
NT_WINDOW_FOCUS
@ NT_WINDOW_FOCUS
Window focus has changed.
Definition: mutt_window.h:185
Command::data
intptr_t data
Data or flags to pass to the command.
Definition: mutt_commands.h:59
MuttWindow::state
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:120
lib.h
NT_MAILBOX_ADD
@ NT_MAILBOX_ADD
A new Mailbox has been created.
Definition: mailbox.h:166
WindowNotifyFlags
uint8_t WindowNotifyFlags
Changes to a MuttWindow.
Definition: mutt_window.h:164
NT_EMAIL
@ NT_EMAIL
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.h:39
EventConfig::sub
const struct ConfigSubset * sub
Config Subset.
Definition: subset.h:72
EventColor::color
enum ColorId color
Definition: color.h:161
EventConfig::name
const char * name
Name of config item that changed.
Definition: subset.h:73
notify_dump_global
static void notify_dump_global(struct NotifyCallback *nc)
Definition: notify.c:241
NT_CONTEXT
@ NT_CONTEXT
Context has changed, NotifyContext, EventContext.
Definition: notify_type.h:38
MailboxType
MailboxType
Supported mailbox formats.
Definition: mailbox.h:43
NT_CONFIG_RESET
@ NT_CONFIG_RESET
Config item has been reset to initial, or parent, value.
Definition: subset.h:63
context.h
NT_CONTEXT_CLOSE
@ NT_CONTEXT_CLOSE
The Context is about to be destroyed.
Definition: context.h:62
Command::name
const char * name
Name of the command.
Definition: mutt_commands.h:47
mutt_buffer_addstr
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
EventContext::context
struct Context * context
The Context this Event relates to.
Definition: context.h:70
MUTT_MAILDIR
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition: mailbox.h:51
notify_dump_mailbox
static void notify_dump_mailbox(struct NotifyCallback *nc)
Definition: notify.c:246
notify_dump_account
static void notify_dump_account(struct NotifyCallback *nc)
Definition: notify.c:162
WindowState::cols
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:57
mailbox_path
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:206
notify_dump_window_state
static void notify_dump_window_state(struct NotifyCallback *nc)
Definition: notify.c:255
NotifyCallback::event_subtype
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:43
NotifyCallback::event_type
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
EventEmail
An Event that happened to an Email.
Definition: email.h:146
lib.h
NT_MAILBOX_CLOSED
@ NT_MAILBOX_CLOSED
Mailbox was closed.
Definition: mailbox.h:172
ComposeFields
const struct Mapping ComposeFields[]
Definition: color.c:146
mutt_buffer_make
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
NT_MAILBOX_RESORT
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition: mailbox.h:174
mutt_map_get_name
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
MUTT_MBOX
@ MUTT_MBOX
'mbox' Mailbox type
Definition: mailbox.h:48
NT_CONFIG_INITIAL_SET
@ NT_CONFIG_INITIAL_SET
Config item's initial value has been set.
Definition: subset.h:64
EventWindow
An Event that happened to a Window.
Definition: mutt_window.h:191
EventMailbox::mailbox
struct Mailbox * mailbox
The Mailbox this Event relates to.
Definition: mailbox.h:185
NT_ACCOUNT
@ NT_ACCOUNT
Account has changed, NotifyAccount, EventAccount.
Definition: notify_type.h:34
notify_dump_email
static void notify_dump_email(struct NotifyCallback *nc)
Definition: notify.c:230
WN_WIDER
#define WN_WIDER
Window became wider.
Definition: mutt_window.h:168
WN_MOVED
#define WN_MOVED
Window moved.
Definition: mutt_window.h:170
get_context
static const char * get_context(int id)
Definition: notify.c:149