From b5bae38ef5905c48f68df3b10f5afc30fd83d8e4 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Fri, 19 Jul 2019 14:51:46 +0100 Subject: [PATCH] context: add notifications Prepare for more Context refactoring by supporting notifications on the Context object. --- context.c | 23 +++++++++++++++++++++++ context.h | 19 +++++++++++++++++++ mx.c | 7 ++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/context.c b/context.c index bd566620c..4385d287c 100644 --- a/context.c +++ b/context.c @@ -51,12 +51,32 @@ void ctx_free(struct Context **ptr) return; struct Context *ctx = *ptr; + + struct EventContext ev_ctx = { ctx }; + notify_send(ctx->notify, NT_CONTEXT, NT_CONTEXT_CLOSE, IP & ev_ctx); + if (ctx->mailbox) notify_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer); + notify_free(&ctx->notify); + FREE(ptr); } +/** + * ctx_new - Create a new Context + * @retval ptr New Context + */ +struct Context *ctx_new(void) +{ + struct Context *ctx = mutt_mem_calloc(1, sizeof(struct Context)); + + ctx->notify = notify_new(ctx, NT_CONTEXT); + notify_set_parent(ctx->notify, NeoMutt->notify); + + return ctx; +} + /** * ctx_cleanup - Release memory and initialize a Context object * @param ctx Context to cleanup @@ -67,7 +87,10 @@ void ctx_cleanup(struct Context *ctx) mutt_pattern_free(&ctx->limit_pattern); if (ctx->mailbox) notify_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer); + + struct Notify *notify = ctx->notify; memset(ctx, 0, sizeof(struct Context)); + ctx->notify = notify; } /** diff --git a/context.h b/context.h index 0cf36f7e1..c4af67b6c 100644 --- a/context.h +++ b/context.h @@ -48,10 +48,29 @@ struct Context bool collapsed : 1; ///< Are all threads collapsed? struct Mailbox *mailbox; + struct Notify *notify; ///< Notifications handler +}; + +/** + * struct EventContext - An Event that happened to an Context + */ +struct EventContext +{ + struct Context *context; ///< The Context this Event relates to +}; + +/** + * enum NotifyContext - Types of Context Event + */ +enum NotifyContext +{ + NT_CONTEXT_OPEN = 1, ///< The Context has been opened + NT_CONTEXT_CLOSE, ///< The Context is about to be destroyed }; void ctx_free (struct Context **ptr); int ctx_mailbox_observer(struct NotifyCallback *nc); +struct Context *ctx_new (void); void ctx_update (struct Context *ctx); void ctx_update_tables (struct Context *ctx, bool committing); diff --git a/mx.c b/mx.c index fd2828c95..1917c1e13 100644 --- a/mx.c +++ b/mx.c @@ -256,8 +256,13 @@ struct Context *mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags) if (!m) return NULL; - struct Context *ctx = mutt_mem_calloc(1, sizeof(*ctx)); + struct Context *ctx = ctx_new(); ctx->mailbox = m; + + struct EventContext ev_ctx = { ctx }; + notify_send(ctx->notify, NT_CONTEXT, NT_CONTEXT_OPEN, IP & ev_ctx); + + // If the Mailbox is closed, Context->mailbox must be set to NULL notify_observer_add(m->notify, NT_MAILBOX, 0, ctx_mailbox_observer, IP ctx); if ((m->magic == MUTT_UNKNOWN) && (flags & (MUTT_NEWFOLDER | MUTT_APPEND))) -- 2.50.0