From da5ba42988b265a915c8ca40748b6bd6a415e304 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Wed, 10 Jul 2019 17:09:21 +0100 Subject: [PATCH] notify: drop old Context notifications --- context.c | 33 +++++++++++++++++++++------------ context.h | 5 +++-- mailbox.c | 6 ++++-- mailbox.h | 2 -- mutt/notify.c | 4 +++- mutt/notify_type.h | 1 + mx.c | 6 ++---- 7 files changed, 34 insertions(+), 23 deletions(-) diff --git a/context.c b/context.c index baf24024f..65aa7ad09 100644 --- a/context.c +++ b/context.c @@ -44,12 +44,16 @@ * ctx_free - Free a Context * @param[out] ctx Context to free */ -void ctx_free(struct Context **ctx) +void ctx_free(struct Context **ptr) { - if (!ctx || !*ctx) + if (!ptr || !*ptr) return; - FREE(ctx); + struct Context *ctx = *ptr; + if (ctx->mailbox) + notify_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer); + + FREE(ptr); } /** @@ -60,6 +64,8 @@ void ctx_cleanup(struct Context *ctx) { FREE(&ctx->pattern); mutt_pattern_free(&ctx->limit_pattern); + if (ctx->mailbox) + notify_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer); memset(ctx, 0, sizeof(struct Context)); } @@ -250,18 +256,19 @@ void ctx_update_tables(struct Context *ctx, bool committing) } /** - * ctx_mailbox_changed - Act on a Mailbox change notification - * @param m Mailbox - * @param action Event occurring + * ctx_mailbox_observer - Watch for changes affecting the Context - Implements ::observer_t */ -void ctx_mailbox_changed(struct Mailbox *m, enum MailboxNotification action) +int ctx_mailbox_observer(struct NotifyCallback *nc) { - if (!m || !m->ndata) - return; - - struct Context *ctx = m->ndata; + if (!nc) + return -1; + if ((nc->obj_type != NT_MAILBOX) || (nc->event_type != NT_MAILBOX)) + return 0; + struct Context *ctx = (struct Context *) nc->data; + if (!ctx) + return -1; - switch (action) + switch (nc->event_subtype) { case MBN_CLOSED: mutt_clear_threads(ctx); @@ -281,6 +288,8 @@ void ctx_mailbox_changed(struct Mailbox *m, enum MailboxNotification action) ctx->last_tag = NULL; break; } + + return 0; } /** diff --git a/context.h b/context.h index 6be1bec25..ffe6eb53c 100644 --- a/context.h +++ b/context.h @@ -31,6 +31,7 @@ #include "pattern.h" struct EmailList; +struct NotifyCallback; /** * struct Context - The "current" mailbox @@ -52,8 +53,8 @@ struct Context struct Mailbox *mailbox; }; -void ctx_free(struct Context **ctx); -void ctx_mailbox_changed(struct Mailbox *m, enum MailboxNotification action); +void ctx_free(struct Context **ptr); +int ctx_mailbox_observer(struct NotifyCallback *nc); void ctx_update(struct Context *ctx); void ctx_update_tables(struct Context *ctx, bool committing); diff --git a/mailbox.c b/mailbox.c index 1254e2eff..d0505ee05 100644 --- a/mailbox.c +++ b/mailbox.c @@ -46,6 +46,7 @@ struct Mailbox *mailbox_new(void) struct Mailbox *m = mutt_mem_calloc(1, sizeof(struct Mailbox)); m->pathbuf = mutt_buffer_new(); + m->notify = notify_new(m, NT_MAILBOX); return m; } @@ -61,6 +62,7 @@ void mailbox_free(struct Mailbox **ptr) struct Mailbox *m = *ptr; mutt_mailbox_changed(m, MBN_CLOSED); + notify_free(&m->notify); mutt_buffer_free(&m->pathbuf); FREE(&m->desc); @@ -208,10 +210,10 @@ void mutt_mailbox_update(struct Mailbox *m) */ void mutt_mailbox_changed(struct Mailbox *m, enum MailboxNotification action) { - if (!m || !m->notify2) + if (!m) return; - m->notify2(m, action); + notify_send(m->notify, NT_MAILBOX, action, 0); } /** diff --git a/mailbox.h b/mailbox.h index 234f47653..684acdc37 100644 --- a/mailbox.h +++ b/mailbox.h @@ -147,8 +147,6 @@ struct Mailbox void (*free_mdata)(void **); /**< driver-specific data free function */ struct Notify *notify; ///< Notifications handler - void (*notify2)(struct Mailbox *m, enum MailboxNotification action); ///< Notification callback - void *ndata; ///< Notification callback private data }; /** diff --git a/mutt/notify.c b/mutt/notify.c index 4f369a1e7..0c7a2950e 100644 --- a/mutt/notify.c +++ b/mutt/notify.c @@ -116,7 +116,9 @@ static bool send(struct Notify *source, struct Notify *current, int type, // mutt_debug(LL_NOTIFY, "send: %d, %ld\n", type, data); struct ObserverNode *np = NULL; - STAILQ_FOREACH(np, ¤t->observers, entries) + struct ObserverNode *tmp = NULL; + // We use the `_SAFE` version in case an event causes an observer to be deleted + STAILQ_FOREACH_SAFE(np, ¤t->observers, entries, tmp) { struct Observer *o = np->observer; diff --git a/mutt/notify_type.h b/mutt/notify_type.h index 982a12b76..846ab8192 100644 --- a/mutt/notify_type.h +++ b/mutt/notify_type.h @@ -35,6 +35,7 @@ enum NotifyType NT_MAILBOX, ///< Mailbox has changed NT_EMAIL, ///< Email has changed NT_WINDOW, ///< Window has changed + NT_CONTEXT, ///< Context has changed }; #endif /* MUTT_LIB_NOTIFY_TYPE_H */ diff --git a/mx.c b/mx.c index 4e85a21e2..d853c7f8c 100644 --- a/mx.c +++ b/mx.c @@ -270,6 +270,7 @@ struct Context *mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags) struct Context *ctx = mutt_mem_calloc(1, sizeof(*ctx)); ctx->mailbox = m; + notify_observer_add(m->notify, NT_MAILBOX, 0, ctx_mailbox_observer, IP ctx); if ((m->magic == MUTT_UNKNOWN) && (flags & (MUTT_NEWFOLDER | MUTT_APPEND))) { @@ -385,8 +386,6 @@ struct Context *mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags) } OptForceRefresh = false; - m->notify2 = ctx_mailbox_changed; - m->ndata = ctx; return ctx; } @@ -413,7 +412,6 @@ void mx_fastclose_mailbox(struct Mailbox *m) m->mx_ops->mbox_close(m); mutt_mailbox_changed(m, MBN_CLOSED); - m->notify2 = NULL; mutt_hash_free(&m->subj_hash); mutt_hash_free(&m->id_hash); @@ -741,7 +739,7 @@ int mx_mbox_close(struct Context **ptr) if ((m->magic == MUTT_MBOX) || (m->magic == MUTT_MMDF)) mbox_reset_atime(m, NULL); mx_fastclose_mailbox(m); - FREE(ptr); + ctx_free(ptr); return 0; } -- 2.40.0