NeoMutt  2022-04-29-215-gc12b98
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 "mview.h"
37
42{
43 if (nc->event_type != NT_MVIEW)
44 return 0;
45 if (!nc->global_data || !nc->event_data)
46 return -1;
47 if (nc->event_subtype == NT_MVIEW_ADD)
48 return 0;
49
50 struct EventMview *ev_m = nc->event_data;
51 struct IndexSharedData *shared = nc->global_data;
52 if (ev_m->mv != shared->mailboxview)
53 return 0;
54
56 shared->mailboxview = NULL;
57
58 mutt_debug(LL_NOTIFY, "relay NT_MVIEW to shared data observers\n");
59 notify_send(shared->notify, nc->event_type, nc->event_subtype, shared);
60 return 0;
61}
62
67{
68 if (nc->event_type != NT_ACCOUNT)
69 return 0;
70 if (!nc->global_data || !nc->event_data)
71 return -1;
73 return 0;
74
75 struct EventAccount *ev_a = nc->event_data;
76 struct IndexSharedData *shared = nc->global_data;
77 if (ev_a->account != shared->account)
78 return 0;
79
81 shared->account = NULL;
82
83 mutt_debug(LL_NOTIFY, "relay NT_ACCOUNT to shared data observers\n");
84 notify_send(shared->notify, nc->event_type, nc->event_subtype, shared);
85 return 0;
86}
87
92{
93 if (nc->event_type != NT_MAILBOX)
94 return 0;
95 if (!nc->global_data || !nc->event_data)
96 return -1;
98 return 0;
99
100 struct EventMailbox *ev_m = nc->event_data;
101 struct IndexSharedData *shared = nc->global_data;
102 if (ev_m->mailbox != shared->mailbox)
103 return 0;
104
106 shared->mailbox = NULL;
107
108 mutt_debug(LL_NOTIFY, "relay NT_MAILBOX to shared data observers\n");
109 notify_send(shared->notify, nc->event_type, nc->event_subtype, ev_m);
110 return 0;
111}
112
117{
118 if (nc->event_type != NT_EMAIL)
119 return 0;
120 if (!nc->global_data || !nc->event_data)
121 return -1;
122 if (nc->event_subtype == NT_EMAIL_ADD)
123 return 0;
124
125 struct EventEmail *ev_e = nc->event_data;
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
141 {
142 shared->email = NULL;
143 mutt_debug(LL_NOTIFY, "NT_INDEX_EMAIL: %p\n", shared->email);
144 notify_send(shared->notify, NT_INDEX, NT_INDEX_EMAIL, shared);
145 }
146
147 mutt_debug(LL_NOTIFY, "relay NT_EMAIL %p to shared data observers\n", shared->email);
148 notify_send(shared->notify, nc->event_type, nc->event_subtype, shared);
149 return 0;
150}
151
158{
159 if (!shared)
160 return;
161
163
164 if (shared->mailboxview != mv)
165 {
166 if (shared->mailboxview)
169
170 shared->mailboxview = mv;
171 subtype |= NT_INDEX_MVIEW;
172
173 if (mv)
175 }
176
177 struct Mailbox *m = mview_mailbox(mv);
178 if (shared->mailbox != m)
179 {
180 if (shared->mailbox)
182
183 shared->mailbox = m;
184 shared->email = NULL;
185 shared->email_seq = 0;
186 subtype |= NT_INDEX_MAILBOX | NT_INDEX_EMAIL;
187
188 if (m)
190 }
191
192 struct Account *a = m ? m->account : NULL;
193 if (shared->account != a)
194 {
195 if (shared->account)
197
198 shared->account = a;
199 subtype |= NT_INDEX_ACCOUNT;
200
201 if (a)
203 }
204
205 struct ConfigSubset *sub = NeoMutt->sub;
206#if 0
207 if (m)
208 sub = m->sub;
209 else if (a)
210 sub = a->sub;
211#endif
212 if (shared->sub != sub)
213 {
214 shared->sub = sub;
215 subtype |= NT_INDEX_SUBSET;
216 }
217
218 if (subtype != NT_INDEX_NO_FLAGS)
219 {
220 mutt_debug(LL_NOTIFY, "NT_INDEX: %p\n", shared);
221 notify_send(shared->notify, NT_INDEX, subtype, shared);
222 }
223}
224
230void index_shared_data_set_email(struct IndexSharedData *shared, struct Email *e)
231{
232 if (!shared)
233 return;
234
235 size_t seq = e ? e->sequence : 0;
236 if ((shared->email != e) || (shared->email_seq != seq))
237 {
238 if (shared->email)
240
241 shared->email = e;
242 shared->email_seq = seq;
243
244 if (e)
246
247 mutt_debug(LL_NOTIFY, "NT_INDEX_EMAIL: %p\n", shared->email);
248 notify_send(shared->notify, NT_INDEX, NT_INDEX_EMAIL, shared);
249 }
250}
251
260 const struct Email *e)
261{
262 if (!shared)
263 return false;
264
265 return shared->email_seq == e->sequence;
266}
267
273void 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->mailboxview)
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}
@ NT_ACCOUNT_ADD
Account has been added.
Definition: account.h:69
@ NT_ACCOUNT_DELETE
Account is about to be deleted.
Definition: account.h:70
Convenience wrapper for the core headers.
Structs that make up an email.
@ NT_EMAIL_ADD
Email has been added.
Definition: email.h:147
@ NT_EMAIL_DELETE
Email is about to be deleted.
Definition: email.h:148
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
static int index_shared_account_observer(struct NotifyCallback *nc)
Notification that an Account has changed - Implements observer_t -.
Definition: shared_data.c:66
static int index_shared_email_observer(struct NotifyCallback *nc)
Notification that an Email has changed - Implements observer_t -.
Definition: shared_data.c:116
static int index_shared_context_observer(struct NotifyCallback *nc)
Notification that the MailboxView has changed - Implements observer_t -.
Definition: shared_data.c:41
static int index_shared_mailbox_observer(struct NotifyCallback *nc)
Notification that a Mailbox has changed - Implements observer_t -.
Definition: shared_data.c:91
void index_shared_data_free(struct MuttWindow *win, void **ptr)
Free Shared Index Data - Implements MuttWindow::wdata_free() -.
Definition: shared_data.c:273
#define NT_INDEX_DELETE
Index Shared Data is about to be freed.
Definition: lib.h:61
#define NT_INDEX_MAILBOX
Mailbox has changed.
Definition: lib.h:65
uint8_t NotifyIndex
Flags, e.g. NT_INDEX_ACCOUNT.
Definition: lib.h:58
#define NT_INDEX_SUBSET
Config Subset has changed.
Definition: lib.h:62
#define NT_INDEX_MVIEW
MailboxView has changed.
Definition: lib.h:64
#define NT_INDEX_ADD
New Index Shared Data has been created.
Definition: lib.h:60
#define NT_INDEX_ACCOUNT
Account has changed.
Definition: lib.h:63
#define NT_INDEX_EMAIL
Email has changed.
Definition: lib.h:66
#define NT_INDEX_NO_FLAGS
No flags are set.
Definition: lib.h:59
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:230
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:259
struct IndexSharedData * index_shared_data_new(void)
Create new Index Data.
Definition: shared_data.c:300
void index_shared_data_set_context(struct IndexSharedData *shared, struct MailboxView *mv)
Set the MailboxView for the Index and friends.
Definition: shared_data.c:157
Data shared between Index, Pager and Sidebar.
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
@ NT_MAILBOX_DELETE
Mailbox is about to be deleted.
Definition: mailbox.h:170
@ NT_MAILBOX_ADD
Mailbox has been added.
Definition: mailbox.h:169
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition: notify.c:228
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
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
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
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
struct Mailbox * mview_mailbox(struct MailboxView *mv)
Wrapper to get the mailbox in a MailboxView, or NULL.
Definition: mview.c:446
The "currently-open" mailbox.
@ NT_MVIEW_DELETE
The Mview is about to be destroyed.
Definition: mview.h:61
@ NT_MVIEW_ADD
The Mview has been opened.
Definition: mview.h:60
@ NT_EMAIL
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.h:44
@ NT_MAILBOX
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:49
@ NT_ACCOUNT
Account has changed, NotifyAccount, EventAccount.
Definition: notify_type.h:36
@ NT_INDEX
Index data has changed, NotifyIndex, IndexSharedData.
Definition: notify_type.h:48
@ NT_MVIEW
MailboxView has changed, NotifyMview, EventMview.
Definition: notify_type.h:50
Key value store.
A group of associated Mailboxes.
Definition: account.h:37
struct Notify * notify
Notifications: NotifyAccount, EventAccount.
Definition: account.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: account.h:40
A set of inherited config items.
Definition: subset.h:47
The envelope/body of an email.
Definition: email.h:37
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition: email.h:71
size_t sequence
Sequence number assigned on creation.
Definition: email.h:65
An Event that happened to an Account.
Definition: account.h:79
struct Account * account
The Account this Event relates to.
Definition: account.h:80
An Event that happened to an Email.
Definition: email.h:160
int num_emails
Number of Emails the event applies to.
Definition: email.h:161
struct Email ** emails
Emails affected by the event.
Definition: email.h:162
An Event that happened to a Mailbox.
Definition: mailbox.h:186
struct Mailbox * mailbox
The Mailbox this Event relates to.
Definition: mailbox.h:187
An Event that happened to an MailboxView.
Definition: mview.h:69
struct MailboxView * mv
The MailboxView this Event relates to.
Definition: mview.h:70
Data shared between Index, Pager and Sidebar.
Definition: shared_data.h:37
size_t email_seq
Sequence number of the current email.
Definition: shared_data.h:43
struct MailboxView * mailboxview
Current Mailbox view.
Definition: shared_data.h:39
struct Account * account
Current Account.
Definition: shared_data.h:40
struct Email * email
Currently selected Email.
Definition: shared_data.h:42
struct Mailbox * mailbox
Current Mailbox.
Definition: shared_data.h:41
struct ConfigSubset * sub
Config set to use.
Definition: shared_data.h:38
struct Notify * notify
Notifications: NotifyIndex, IndexSharedData.
Definition: shared_data.h:44
The "current" mailbox.
Definition: mview.h:38
struct Notify * notify
Notifications: NotifyMview, EventMview.
Definition: mview.h:50
A mailbox.
Definition: mailbox.h:79
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:144
struct Account * account
Account that owns this Mailbox.
Definition: mailbox.h:127
struct ConfigSubset * sub
Inherited config items.
Definition: mailbox.h:83
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
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