From: Richard Russon Date: Wed, 19 Dec 2018 13:24:17 +0000 (+0000) Subject: mailbox: add notify callback X-Git-Tag: 2019-10-25~399^2~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bb645393a3e9d6fe315a549b444ed236c655f193;p=neomutt mailbox: add notify callback --- diff --git a/mailbox.c b/mailbox.c index 98c27ca97..80996ab53 100644 --- 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); +} diff --git a/mailbox.h b/mailbox.h index 498d25bb7..6cd807a77 100644 --- 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 093405a1d..d618c4f14 100644 --- 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;