]> granicus.if.org Git - neomutt/commitdiff
mailbox: add notify callback
authorRichard Russon <rich@flatcap.org>
Wed, 19 Dec 2018 13:24:17 +0000 (13:24 +0000)
committerRichard Russon <rich@flatcap.org>
Sat, 5 Jan 2019 14:44:35 +0000 (14:44 +0000)
mailbox.c
mailbox.h
mx.c

index 98c27ca9728bf55b890506acecdef1bc1530acc7..80996ab5347365ac1fd18d349d54318cc6070364 100644 (file)
--- a/mailbox.c
+++ b/mailbox.c
@@ -766,3 +766,16 @@ void mutt_context_free(struct Context **ctx)
   mailbox_free(&(*ctx)->mailbox);
   FREE(ctx);
 }
+
+/**
+ * mutt_mailbox_changed - Notify listeners of a change to a Mailbox
+ * @param m      Mailbox
+ * @param action Change to Mailbox
+ */
+void mutt_mailbox_changed(struct Mailbox *m, enum MailboxNotification action)
+{
+  if (!m || !m->notify)
+    return;
+
+  m->notify(m, action);
+}
index 498d25bb754439e1c883669e18cdafc302ddbf63..6cd807a77512860d075207d490ed08e941c2ba2e 100644 (file)
--- a/mailbox.h
+++ b/mailbox.h
@@ -50,6 +50,16 @@ extern bool  MaildirCheckCur;
 #define MB_NORMAL 0
 #define MB_HIDDEN 1
 
+/**
+ * enum MailboxNotification - Notifications about changes to a Mailbox
+ */
+enum MailboxNotification
+{
+  MBN_CLOSED = 1, ///< Mailbox was closed
+  MBN_INVALID,    ///< Email list was changed
+  MBN_RESORT,     ///< Email list needs resorting
+};
+
 /**
  * enum AclRights - ACL Rights
  *
@@ -130,6 +140,9 @@ struct Mailbox
 
   void *mdata;                 /**< driver specific data */
   void (*free_mdata)(void **); /**< driver-specific data free function */
+
+  void (*notify)(struct Mailbox *m, enum MailboxNotification action); ///< Notification callback
+  void *ndata; ///< Notification callback private data
 };
 
 /**
@@ -168,5 +181,6 @@ int mutt_mailbox_check(int force);
 bool mutt_mailbox_notify(void);
 enum CommandResult mutt_parse_mailboxes(struct Buffer *path, struct Buffer *s, unsigned long data, struct Buffer *err);
 enum CommandResult mutt_parse_unmailboxes(struct Buffer *path, struct Buffer *s, unsigned long data, struct Buffer *err);
+void mutt_mailbox_changed(struct Mailbox *m, enum MailboxNotification action);
 
 #endif /* MUTT_MAILBOX_H */
diff --git a/mx.c b/mx.c
index 093405a1dfeacd9fd9e9981c149dbe410969c54a..d618c4f14350215fa3b981bd86fb4ab39d8b3b99 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -236,6 +236,35 @@ static int mx_open_mailbox_append(struct Mailbox *m, int flags)
   return rc;
 }
 
+/**
+ * mx_mailbox_changed - Act on a Mailbox change notification
+ * @param m      Mailbox
+ * @param action Event occurring
+ * @param ndata  Private notification data
+ */
+void mx_mailbox_changed(struct Mailbox *m, enum MailboxNotification action)
+{
+  if (!m || !m->ndata)
+    return;
+
+  struct Context *ctx = m->ndata;
+
+  switch (action)
+  {
+    case MBN_CLOSED:
+      mutt_clear_threads(ctx);
+      mx_cleanup_context(ctx);
+      break;
+    case MBN_INVALID:
+      mx_update_context(ctx);
+      break;
+    case MBN_RESORT:
+      mx_update_tables(ctx, false);
+      mutt_sort_headers(ctx, true);
+      break;
+  }
+}
+
 /**
  * mx_mbox_open - Open a mailbox and parse it
  * @param m     Mailbox to open
@@ -273,6 +302,9 @@ struct Context *mx_mbox_open(struct Mailbox *m, const char *path, int flags)
     /* int rc = */ mx_path_canon2(m, Folder);
   }
 
+  m->notify = mx_mailbox_changed;
+  m->ndata = ctx;
+
   if ((m->magic == MUTT_UNKNOWN) && (flags & MUTT_NEWFOLDER))
   {
     m->magic = MboxType;