]> granicus.if.org Git - neomutt/commitdiff
context: add notifications
authorRichard Russon <rich@flatcap.org>
Fri, 19 Jul 2019 13:51:46 +0000 (14:51 +0100)
committerRichard Russon <rich@flatcap.org>
Sat, 20 Jul 2019 22:13:22 +0000 (23:13 +0100)
Prepare for more Context refactoring by supporting notifications on the
Context object.

context.c
context.h
mx.c

index bd566620c654ba70ea2b810a8b3804a2a8fcec37..4385d287cb23c1d204aa65704ad4187bf808562f 100644 (file)
--- 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;
 }
 
 /**
index 0cf36f7e1d2de38e418f817e83f343325dd343e0..c4af67b6c953bfe61dcd2baf89cea5704bee44de 100644 (file)
--- 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 fd2828c955e4c49877f3884b2b9b08da1c42ca91..1917c1e13444e96aa1becd5d73457078ea807877 100644 (file)
--- 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)))