NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
shared_data.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stdbool.h>
31 #include "mutt/lib.h"
32 #include "email/lib.h"
33 #include "core/lib.h"
34 #include "shared_data.h"
35 #include "lib.h"
36 #include "context.h"
37 #include "mutt_globals.h"
38 
43 {
44  if ((nc->event_type != NT_CONTEXT) || !nc->global_data || !nc->event_data)
45  return -1;
46 
47  struct EventContext *ev_c = nc->event_data;
48  if (nc->event_subtype == NT_CONTEXT_ADD)
49  return 0;
50 
51  struct IndexSharedData *shared = nc->global_data;
52  if (ev_c->ctx != shared->ctx)
53  return 0;
54 
56  shared->ctx = NULL;
57 
58  // Relay the message
59  mutt_debug(LL_NOTIFY, "NT_INDEX_CONTEXT\n");
60  notify_send(shared->notify, NT_INDEX, NT_INDEX_CONTEXT, shared);
61  return 0;
62 }
63 
68 {
69  if ((nc->event_type != NT_ACCOUNT) || !nc->global_data || !nc->event_data)
70  return -1;
71 
72  struct EventAccount *ev_a = nc->event_data;
73  if (nc->event_subtype == NT_ACCOUNT_ADD)
74  return 0;
75 
76  struct IndexSharedData *shared = nc->global_data;
77  if (ev_a->account != shared->account)
78  return 0;
79 
81  shared->account = NULL;
82 
83  // Relay the message
84  mutt_debug(LL_NOTIFY, "NT_INDEX_ACCOUNT\n");
85  notify_send(shared->notify, NT_INDEX, NT_INDEX_ACCOUNT, shared);
86  return 0;
87 }
88 
93 {
94  if ((nc->event_type != NT_MAILBOX) || !nc->global_data || !nc->event_data)
95  return -1;
96 
97  struct EventMailbox *ev_m = nc->event_data;
98  if (nc->event_subtype == NT_MAILBOX_ADD)
99  return 0;
100 
101  struct IndexSharedData *shared = nc->global_data;
102  if (ev_m->mailbox != shared->mailbox)
103  return 0;
104 
105  if (nc->event_subtype == NT_MAILBOX_DELETE)
106  shared->mailbox = NULL;
107 
108  // Relay the message
109  mutt_debug(LL_NOTIFY, "NT_INDEX_MAILBOX\n");
110  notify_send(shared->notify, NT_INDEX, NT_INDEX_MAILBOX, shared);
111  return 0;
112 }
113 
118 {
119  if ((nc->event_type != NT_EMAIL) || !nc->global_data || !nc->event_data)
120  return -1;
121 
122  struct EventEmail *ev_e = nc->event_data;
123  if (nc->event_subtype == NT_EMAIL_ADD)
124  return 0;
125 
126  struct IndexSharedData *shared = nc->global_data;
127  bool match = false;
128  for (int i = 0; i < ev_e->num_emails; i++)
129  {
130  if (ev_e->emails[i] == shared->email)
131  {
132  match = true;
133  break;
134  }
135  }
136 
137  if (!match)
138  return 0;
139 
140  if (nc->event_subtype == NT_EMAIL_DELETE)
141  shared->email = NULL;
142 
143  // Relay the message
144  mutt_debug(LL_NOTIFY, "NT_INDEX_EMAIL: %p\n", shared->email);
145  notify_send(shared->notify, NT_INDEX, NT_INDEX_EMAIL, shared);
146  return 0;
147 }
148 
155 {
156  if (!shared)
157  return;
158 
159  NotifyIndex subtype = NT_INDEX_NO_FLAGS;
160 
161  if (shared->ctx != ctx)
162  {
163  if (shared->ctx)
165 
166  shared->ctx = ctx;
167  subtype |= NT_INDEX_CONTEXT;
168 
169  if (ctx)
171 
172  Context = ctx;
173  }
174 
175  struct Mailbox *m = ctx_mailbox(ctx);
176  if (shared->mailbox != m)
177  {
178  if (shared->mailbox)
180 
181  shared->mailbox = m;
182  shared->email = NULL;
183  shared->email_seq = 0;
184  subtype |= NT_INDEX_MAILBOX | NT_INDEX_EMAIL;
185 
186  if (m)
188  }
189 
190  struct Account *a = m ? m->account : NULL;
191  if (shared->account != a)
192  {
193  if (shared->account)
195 
196  shared->account = a;
197  subtype |= NT_INDEX_ACCOUNT;
198 
199  if (a)
201  }
202 
203  struct ConfigSubset *sub = NeoMutt->sub;
204 #if 0
205  if (m)
206  sub = m->sub;
207  else if (a)
208  sub = a->sub;
209 #endif
210  if (shared->sub != sub)
211  {
212  shared->sub = sub;
213  subtype |= NT_INDEX_SUBSET;
214  }
215 
216  if (subtype != NT_INDEX_NO_FLAGS)
217  {
218  mutt_debug(LL_NOTIFY, "NT_INDEX: %p\n", shared);
219  notify_send(shared->notify, NT_INDEX, subtype, shared);
220  }
221 }
222 
228 void index_shared_data_set_email(struct IndexSharedData *shared, struct Email *e)
229 {
230  if (!shared)
231  return;
232 
233  size_t seq = e ? e->sequence : 0;
234  if ((shared->email != e) || (shared->email_seq != seq))
235  {
236  if (shared->email)
238 
239  shared->email = e;
240  shared->email_seq = seq;
241 
242  if (e)
244 
245  mutt_debug(LL_NOTIFY, "NT_INDEX_EMAIL: %p\n", shared->email);
246  notify_send(shared->notify, NT_INDEX, NT_INDEX_EMAIL, shared);
247  }
248 }
249 
258  const struct Email *e)
259 {
260  if (!shared)
261  return false;
262 
263  return shared->email_seq == e->sequence;
264 }
265 
273 void index_shared_data_free(struct MuttWindow *win, void **ptr)
274 {
275  if (!ptr || !*ptr)
276  return;
277 
278  struct IndexSharedData *shared = *ptr;
279 
280  mutt_debug(LL_NOTIFY, "NT_INDEX_DELETE: %p\n", shared);
281  notify_send(shared->notify, NT_INDEX, NT_INDEX_DELETE, shared);
282  notify_free(&shared->notify);
283 
284  if (shared->account)
286  if (shared->ctx)
288  if (shared->mailbox)
290  if (shared->email)
292 
293  FREE(ptr);
294 }
295 
301 {
302  struct IndexSharedData *shared = mutt_mem_calloc(1, sizeof(struct IndexSharedData));
303 
304  shared->notify = notify_new();
305  shared->sub = NeoMutt->sub;
306 
307  mutt_debug(LL_NOTIFY, "NT_INDEX_ADD: %p\n", shared);
308  notify_send(shared->notify, NT_INDEX, NT_INDEX_ADD, shared);
309 
310  return shared;
311 }
The "current" mailbox.
Definition: context.h:37
Mailbox is about to be deleted.
Definition: mailbox.h:174
#define NT_INDEX_SUBSET
Config Subset has changed.
Definition: lib.h:61
An Event that happened to an Email.
Definition: email.h:155
#define NT_INDEX_CONTEXT
Context has changed.
Definition: lib.h:63
size_t email_seq
Sequence number of the current email.
Definition: shared_data.h:43
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct Context * ctx
The Context this Event relates to.
Definition: context.h:70
The envelope/body of an email.
Definition: email.h:37
void index_shared_data_set_email(struct IndexSharedData *shared, struct Email *e)
Set the current Email for the Index and friends.
Definition: shared_data.c:228
Data passed to a notification function.
Definition: observer.h:39
static int index_shared_mailbox_observer(struct NotifyCallback *nc)
Notification that a Mailbox has changed - Implements observer_t.
Definition: shared_data.c:92
struct Mailbox * ctx_mailbox(struct Context *ctx)
wrapper to get the mailbox in a Context, or NULL
Definition: context.c:444
Log of notifications.
Definition: logging.h:45
Structs that make up an email.
struct Email * email
Currently selected Email.
Definition: shared_data.h:42
struct Mailbox * mailbox
The Mailbox this Event relates to.
Definition: mailbox.h:192
The "currently-open" mailbox.
#define NT_INDEX_EMAIL
Email has changed.
Definition: lib.h:65
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
struct ConfigSubset * sub
Config set to use.
Definition: shared_data.h:38
static int index_shared_email_observer(struct NotifyCallback *nc)
Notification that an Email has changed - Implements observer_t.
Definition: shared_data.c:117
struct Notify * notify
Notifications: NotifyAccount, EventAccount.
Definition: account.h:42
void index_shared_data_set_context(struct IndexSharedData *shared, struct Context *ctx)
Set the Context for the Index and friends.
Definition: shared_data.c:154
uint8_t NotifyIndex
Flags, e.g. NT_INDEX_ACCOUNT.
Definition: lib.h:55
A group of associated Mailboxes.
Definition: account.h:36
#define NT_INDEX_ACCOUNT
Account has changed.
Definition: lib.h:62
Data shared between Index, Pager and Sidebar.
Definition: shared_data.h:36
void index_shared_data_free(struct MuttWindow *win, void **ptr)
Free Index Data.
Definition: shared_data.c:273
#define NT_INDEX_MAILBOX
Mailbox has changed.
Definition: lib.h:64
An Event that happened to a Mailbox.
Definition: mailbox.h:190
struct Email ** emails
Definition: email.h:158
Container for Accounts, Notifications.
Definition: neomutt.h:36
An Event that happened to an Account.
Definition: account.h:78
struct Notify * notify
Notifications: NotifyIndex, IndexSharedData.
Definition: shared_data.h:44
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
Email is about to be deleted.
Definition: email.h:147
int num_emails
Definition: email.h:157
Index data has changed, NotifyIndex, IndexSharedData.
Definition: notify_type.h:47
Mailbox has been added.
Definition: mailbox.h:173
Convenience wrapper for the core headers.
struct Notify * notify
Notifications: NotifyContext, EventContext.
Definition: context.h:50
Email has been added.
Definition: email.h:146
#define NT_INDEX_DELETE
Index Shared Data is about to be freed.
Definition: lib.h:60
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
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
struct Context * ctx
Current Mailbox view.
Definition: shared_data.h:39
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
A set of inherited config items.
Definition: subset.h:46
The Context has been opened.
Definition: context.h:60
A mailbox.
Definition: mailbox.h:81
struct ConfigSubset * sub
Inherited config items.
Definition: mailbox.h:86
#define NT_INDEX_ADD
New Index Shared Data has been created.
Definition: lib.h:59
static int index_shared_context_observer(struct NotifyCallback *nc)
Notification that the Context has changed - Implements observer_t.
Definition: shared_data.c:42
The Context is about to be destroyed.
Definition: context.h:61
Account has changed, NotifyAccount, EventAccount.
Definition: notify_type.h:34
size_t sequence
Sequence number assigned on creation.
Definition: email.h:99
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
static int index_shared_account_observer(struct NotifyCallback *nc)
Notification that an Account has changed - Implements observer_t.
Definition: shared_data.c:67
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition: email.h:123
bool index_shared_data_is_cur_email(const struct IndexSharedData *shared, const struct Email *e)
Check whether an email is the currently selected Email.
Definition: shared_data.c:257
void * event_data
Data from notify_send()
Definition: observer.h:44
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.h:44
#define NT_INDEX_NO_FLAGS
No flags are set.
Definition: lib.h:58
Context has changed, NotifyContext, EventContext.
Definition: notify_type.h:43
struct Account * account
Account that owns this Mailbox.
Definition: mailbox.h:131
#define FREE(x)
Definition: memory.h:40
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:48
struct ConfigSubset * sub
Inherited config items.
Definition: account.h:40
struct Account * account
Current Account.
Definition: shared_data.h:40
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
Account has been added.
Definition: account.h:69
struct IndexSharedData * index_shared_data_new(void)
Create new Index Data.
Definition: shared_data.c:300
Hundreds of global variables to back the user variables.
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:148
An Event that happened to an Context.
Definition: context.h:68
struct Mailbox * mailbox
Current Mailbox.
Definition: shared_data.h:41
Account is about to be deleted.
Definition: account.h:70
Convenience wrapper for the library headers.
struct Account * account
The Account this Event relates to.
Definition: account.h:80
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:171
Data shared between Index, Pager and Sidebar.