]> granicus.if.org Git - neomutt/commitdiff
notify: use data to match specific callbacks
authorRichard Russon <rich@flatcap.org>
Sat, 20 Jul 2019 12:48:19 +0000 (13:48 +0100)
committerRichard Russon <rich@flatcap.org>
Sat, 20 Jul 2019 22:14:08 +0000 (23:14 +0100)
When removing a notification callback, check both the function and data
pointers.

This means we can reuse a single callback function with multiple data
objects.

context.c
mutt/notify.c
mutt/notify.h

index 4385d287cb23c1d204aa65704ad4187bf808562f..40738ef9f3591b464f50ccc2f138e03a2e13cf84 100644 (file)
--- a/context.c
+++ b/context.c
@@ -56,7 +56,7 @@ void ctx_free(struct Context **ptr)
   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_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer, IP ctx);
 
   notify_free(&ctx->notify);
 
@@ -86,7 +86,7 @@ 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);
+    notify_observer_remove(ctx->mailbox->notify, ctx_mailbox_observer, IP ctx);
 
   struct Notify *notify = ctx->notify;
   memset(ctx, 0, sizeof(struct Context));
index 0c7a2950e75b0f03cdf8aecc0a6cc4b4ba102f21..cf2424e168bfe3d070533b8eeb3118abd08e9a67 100644 (file)
@@ -74,7 +74,7 @@ void notify_free(struct Notify **ptr)
   struct Notify *notify = *ptr;
   // NOTIFY observers
 
-  notify_observer_remove(notify, NULL);
+  notify_observer_remove(notify, NULL, 0);
 
   FREE(ptr);
 }
@@ -186,11 +186,12 @@ bool notify_observer_add(struct Notify *notify, enum NotifyType type,
  * notify_observer_remove - Remove an observer from an object
  * @param notify   Notification handler
  * @param callback Function to call on a matching event, see ::observer_t
+ * @param data     Private data to match specific callback
  * @retval true If successful
  *
  * If callback is NULL, all the observers will be removed.
  */
-bool notify_observer_remove(struct Notify *notify, observer_t callback)
+bool notify_observer_remove(struct Notify *notify, observer_t callback, intptr_t data)
 {
   if (!notify)
     return false;
@@ -200,7 +201,7 @@ bool notify_observer_remove(struct Notify *notify, observer_t callback)
   struct ObserverNode *tmp = NULL;
   STAILQ_FOREACH_SAFE(np, &notify->observers, entries, tmp)
   {
-    if (!callback || (np->observer->callback == callback))
+    if (!callback || ((np->observer->callback == callback) && (np->observer->data == data)))
     {
       STAILQ_REMOVE(&notify->observers, np, ObserverNode, entries);
       FREE(&np->observer);
index f57f3a6ea1c9953df42f978481ccbf1be51cc25c..a045527e11078b9a7a891344876fe9a201cf3ca8 100644 (file)
@@ -37,6 +37,6 @@ void notify_set_parent(struct Notify *notify, struct Notify *parent);
 bool notify_send(struct Notify *notify, int type, int subtype, intptr_t data);
 
 bool notify_observer_add(struct Notify *notify, enum NotifyType type, int subtype, observer_t callback, intptr_t data);
-bool notify_observer_remove(struct Notify *notify, observer_t callback);
+bool notify_observer_remove(struct Notify *notify, observer_t callback, intptr_t data);
 
 #endif /* MUTT_LIB_NOTIFY_H */