changeset: 6649:6356f1068fe2
user:      Damien Riegel <damien.rie...@gmail.com>
date:      Thu May 12 12:41:01 2016 -0700
link:      http://dev.mutt.org/hg/mutt/rev/6356f1068fe2

rename mbox_close_mailbox to mx_close_mailbox_append and move it

mbox_close_mailbox was used as the counterpart of
mx_open_mailbox_append. To make things clearer, rename it
mx_close_mailbox_append.

As it is only used in mx.c, move it there and make it static.

changeset: 6650:ae203b2c56a6
user:      Damien Riegel <damien.rie...@gmail.com>
date:      Thu May 12 12:41:25 2016 -0700
link:      http://dev.mutt.org/hg/mutt/rev/ae203b2c56a6

Start decoupling mailbox operations.

Introduce a dedicated structure for mailbox operations: struct mx_ops.
Move the open and close operations into that structure.

Assign this structure to the context in mx_open_mailbox.  This is
currently based on the "magic" for the mailbox type, but may be
refactored in the future.

Add a stub mbox_close_mailbox function.  This function does nothing,
the main purpose is to introduce a mx_ops structure for mbox, so its
usage is similar to mh/imap/pop.  We reuse the name that was made
available by the previous commmit.  Note that the actual closing of
the descriptor is done in mx.c.

To be more consistent with other mailboxes, introduce functions
mh_open_mailbox and maildir_open_mailbox, and create a dedicated
structure for mmdf.

diffs (403 lines):

diff -r 391492f27fef -r ae203b2c56a6 imap/imap.c
--- a/imap/imap.c       Tue May 10 09:42:27 2016 -0700
+++ b/imap/imap.c       Thu May 12 12:41:25 2016 -0700
@@ -554,7 +554,7 @@
   return s;
 }
 
-int imap_open_mailbox (CONTEXT* ctx)
+static int imap_open_mailbox (CONTEXT* ctx)
 {
   IMAP_DATA *idata;
   IMAP_STATUS* status;
@@ -578,7 +578,6 @@
 
   /* once again the context is new */
   ctx->data = idata;
-  ctx->mx_close = imap_close_mailbox;
 
   /* Clean up path and replace the one in the ctx */
   imap_fix_path (idata, mx.mbox, buf, sizeof (buf));
@@ -2038,3 +2037,8 @@
 
   return -1;
 }
+
+struct mx_ops mx_imap_ops = {
+  .open = imap_open_mailbox,
+  .close = imap_close_mailbox,
+};
diff -r 391492f27fef -r ae203b2c56a6 imap/imap.h
--- a/imap/imap.h       Tue May 10 09:42:27 2016 -0700
+++ b/imap/imap.h       Thu May 12 12:41:25 2016 -0700
@@ -35,7 +35,6 @@
 int imap_access (const char*, int);
 int imap_check_mailbox (CONTEXT *ctx, int *index_hint, int force);
 int imap_delete_mailbox (CONTEXT* idata, IMAP_MBOX mx);
-int imap_open_mailbox (CONTEXT *ctx);
 int imap_open_mailbox_append (CONTEXT *ctx);
 int imap_sync_mailbox (CONTEXT *ctx, int expunge, int *index_hint);
 int imap_close_mailbox (CONTEXT *ctx);
@@ -48,6 +47,8 @@
 void imap_allow_reopen (CONTEXT *ctx);
 void imap_disallow_reopen (CONTEXT *ctx);
 
+extern struct mx_ops mx_imap_ops;
+
 /* browse.c */
 int imap_browse (char* path, struct browser_state* state);
 int imap_mailbox_state (const char* path, struct mailbox_state* state);
diff -r 391492f27fef -r ae203b2c56a6 mbox.c
--- a/mbox.c    Tue May 10 09:42:27 2016 -0700
+++ b/mbox.c    Thu May 12 12:41:25 2016 -0700
@@ -412,7 +412,7 @@
 #undef PREV
 
 /* open a mbox or mmdf style mailbox */
-int mbox_open_mailbox (CONTEXT *ctx)
+static int mbox_open_mailbox (CONTEXT *ctx)
 {
   int rc;
 
@@ -440,6 +440,11 @@
   return (rc);
 }
 
+static int mbox_close_mailbox (CONTEXT *ctx)
+{
+  return 0;
+}
+
 /* return 1 if address lists are strictly identical */
 static int strict_addrcmp (const ADDRESS *a, const ADDRESS *b)
 {
@@ -1069,15 +1074,6 @@
   return rc;
 }
 
-/* close a mailbox opened in write-mode */
-int mbox_close_mailbox (CONTEXT *ctx)
-{
-  mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
-  mutt_unblock_signals ();
-  mx_fastclose_mailbox (ctx);
-  return 0;
-}
-
 int mutt_reopen_mailbox (CONTEXT *ctx, int *index_hint)
 {
   int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
@@ -1266,3 +1262,13 @@
 
   return ((st.st_size == 0));
 }
+
+struct mx_ops mx_mbox_ops = {
+  .open = mbox_open_mailbox,
+  .close = mbox_close_mailbox,
+};
+
+struct mx_ops mx_mmdf_ops = {
+  .open = mbox_open_mailbox,
+  .close = mbox_close_mailbox,
+};
diff -r 391492f27fef -r ae203b2c56a6 mh.c
--- a/mh.c      Tue May 10 09:42:27 2016 -0700
+++ b/mh.c      Thu May 12 12:41:25 2016 -0700
@@ -1168,7 +1168,7 @@
  *     subdir [IN]     NULL for MH mailboxes, otherwise the subdir of the
  *                     maildir mailbox to read from
  */
-int mh_read_dir (CONTEXT * ctx, const char *subdir)
+static int mh_read_dir (CONTEXT * ctx, const char *subdir)
 {
   struct maildir *md;
   struct mh_sequences mhs;
@@ -1188,7 +1188,6 @@
   if (!ctx->data)
   {
     ctx->data = safe_calloc(sizeof (struct mh_data), 1);
-    ctx->mx_close = mh_close_mailbox;
   }
   data = mh_data (ctx);
 
@@ -1224,7 +1223,7 @@
 }
 
 /* read a maildir style mailbox */
-int maildir_read_dir (CONTEXT * ctx)
+static int maildir_read_dir (CONTEXT * ctx)
 {
   /* maildir looks sort of like MH, except that there are two subdirectories
    * of the main folder path from which to read messages
@@ -1235,6 +1234,16 @@
   return 0;
 }
 
+static int maildir_open_mailbox (CONTEXT *ctx)
+{
+  return maildir_read_dir (ctx);
+}
+
+static int mh_open_mailbox (CONTEXT *ctx)
+{
+  return mh_read_dir (ctx, NULL);
+}
+
 /*
  * Open a new (temporary) message in an MH folder.
  */
@@ -2348,3 +2357,13 @@
 
   return 0;
 }
+
+struct mx_ops mx_maildir_ops = {
+  .open = maildir_open_mailbox,
+  .close = mh_close_mailbox,
+};
+
+struct mx_ops mx_mh_ops = {
+  .open = mh_open_mailbox,
+  .close = mh_close_mailbox,
+};
diff -r 391492f27fef -r ae203b2c56a6 mutt.h
--- a/mutt.h    Tue May 10 09:42:27 2016 -0700
+++ b/mutt.h    Thu May 12 12:41:25 2016 -0700
@@ -868,6 +868,14 @@
   RIGHTSMAX
 };
 
+struct _context;
+
+struct mx_ops
+{
+  int (*open)(struct _context *);
+  int (*close)(struct _context *);
+};
+
 typedef struct _context
 {
   char *path;
@@ -909,7 +917,7 @@
 
   /* driver hooks */
   void *data;                  /* driver specific data */
-  int (*mx_close)(struct _context *);
+  struct mx_ops *mx_ops;
 } CONTEXT;
 
 typedef struct
diff -r 391492f27fef -r ae203b2c56a6 mx.c
--- a/mx.c      Tue May 10 09:42:27 2016 -0700
+++ b/mx.c      Thu May 12 12:41:25 2016 -0700
@@ -57,6 +57,30 @@
 #include <ctype.h>
 #include <utime.h>
 
+static struct mx_ops* mx_get_ops (int magic)
+{
+  switch (magic)
+  {
+#ifdef USE_IMAP
+    case MUTT_IMAP:
+      return &mx_imap_ops;
+#endif
+    case MUTT_MAILDIR:
+      return &mx_maildir_ops;
+    case MUTT_MBOX:
+      return &mx_mbox_ops;
+    case MUTT_MH:
+      return &mx_mh_ops;
+    case MUTT_MMDF:
+      return &mx_mmdf_ops;
+#ifdef USE_POP
+    case MUTT_POP:
+      return &mx_pop_ops;
+#endif
+    default:
+      return NULL;
+  }
+}
 
 #define mutt_is_spool(s)  (mutt_strcmp (Spoolfile, s) == 0)
 
@@ -572,6 +596,15 @@
   return 0;
 }
 
+/* close a mailbox opened in write-mode */
+static int mx_close_mailbox_append (CONTEXT *ctx)
+{
+  mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
+  mutt_unblock_signals ();
+  mx_fastclose_mailbox (ctx);
+  return 0;
+}
+
 /*
  * open a mailbox and parse it
  *
@@ -616,15 +649,15 @@
   }
 
   ctx->magic = mx_get_magic (path);
-  
-  if(ctx->magic == 0)
-    mutt_error (_("%s is not a mailbox."), path);
+  ctx->mx_ops = mx_get_ops (ctx->magic);
 
-  if(ctx->magic == -1)
-    mutt_perror(path);
-  
-  if(ctx->magic <= 0)
+  if (ctx->magic <= 0 || !ctx->mx_ops)
   {
+    if (ctx->magic == 0 || !ctx->mx_ops)
+      mutt_error (_("%s is not a mailbox."), path);
+    else if (ctx->magic == -1)
+      mutt_perror(path);
+
     mx_fastclose_mailbox (ctx);
     if (!pctx)
       FREE (&ctx);
@@ -641,37 +674,7 @@
   if (!ctx->quiet)
     mutt_message (_("Reading %s..."), ctx->path);
 
-  switch (ctx->magic)
-  {
-    case MUTT_MH:
-      rc = mh_read_dir (ctx, NULL);
-      break;
-
-    case MUTT_MAILDIR:
-      rc = maildir_read_dir (ctx);
-      break;
-
-    case MUTT_MMDF:
-    case MUTT_MBOX:
-      rc = mbox_open_mailbox (ctx);
-      break;
-
-#ifdef USE_IMAP
-    case MUTT_IMAP:
-      rc = imap_open_mailbox (ctx);
-      break;
-#endif /* USE_IMAP */
-
-#ifdef USE_POP
-    case MUTT_POP:
-      rc = pop_open_mailbox (ctx);
-      break;
-#endif /* USE_POP */
-
-    default:
-      rc = -1;
-      break;
-  }
+  rc = ctx->mx_ops->open(ctx);
 
   if (rc == 0)
   {
@@ -709,8 +712,8 @@
    * XXX: really belongs in mx_close_mailbox, but this is a nice hook point */
   mutt_buffy_setnotified(ctx->path);
 
-  if (ctx->mx_close)
-    ctx->mx_close (ctx);
+  if (ctx->mx_ops)
+    ctx->mx_ops->close (ctx);
 
   if (ctx->subj_hash)
     hash_destroy (&ctx->subj_hash, NULL);
@@ -801,7 +804,7 @@
   {
     /* mailbox was opened in write-mode */
     if (ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF)
-      mbox_close_mailbox (ctx);
+      mx_close_mailbox_append (ctx);
     else
       mx_fastclose_mailbox (ctx);
     return 0;
diff -r 391492f27fef -r ae203b2c56a6 mx.h
--- a/mx.h      Tue May 10 09:42:27 2016 -0700
+++ b/mx.h      Thu May 12 12:41:25 2016 -0700
@@ -44,9 +44,7 @@
 #define MAXLOCKATTEMPT 5
 
 int mbox_sync_mailbox (CONTEXT *, int *);
-int mbox_open_mailbox (CONTEXT *);
 int mbox_check_mailbox (CONTEXT *, int *);
-int mbox_close_mailbox (CONTEXT *);
 int mbox_lock_mailbox (CONTEXT *, int, int);
 int mbox_parse_mailbox (CONTEXT *);
 int mmdf_parse_mailbox (CONTEXT *);
@@ -54,12 +52,10 @@
 int mbox_check_empty (const char *);
 void mbox_reset_atime (CONTEXT *, struct stat *);
 
-int mh_read_dir (CONTEXT *, const char *);
 int mh_sync_mailbox (CONTEXT *, int *);
 int mh_check_mailbox (CONTEXT *, int *);
 int mh_check_empty (const char *);
 
-int maildir_read_dir (CONTEXT *);
 int maildir_check_mailbox (CONTEXT *, int *);
 int maildir_check_empty (const char *);
 
@@ -82,5 +78,9 @@
 int mx_lock_file (const char *, int, int, int, int);
 int mx_unlock_file (const char *path, int fd, int dot);
 
+extern struct mx_ops mx_maildir_ops;
+extern struct mx_ops mx_mbox_ops;
+extern struct mx_ops mx_mh_ops;
+extern struct mx_ops mx_mmdf_ops;
 
 #endif
diff -r 391492f27fef -r ae203b2c56a6 pop.c
--- a/pop.c     Tue May 10 09:42:27 2016 -0700
+++ b/pop.c     Thu May 12 12:41:25 2016 -0700
@@ -397,7 +397,7 @@
 }
 
 /* open POP mailbox - fetch only headers */
-int pop_open_mailbox (CONTEXT *ctx)
+static int pop_open_mailbox (CONTEXT *ctx)
 {
   int ret;
   char buf[LONG_STRING];
@@ -426,7 +426,6 @@
   pop_data = safe_calloc (1, sizeof (POP_DATA));
   pop_data->conn = conn;
   ctx->data = pop_data;
-  ctx->mx_close = pop_close_mailbox;
 
   if (pop_open_connection (pop_data) < 0)
     return -1;
@@ -928,3 +927,8 @@
   mutt_socket_close (conn);
   FREE (&pop_data);
 }
+
+struct mx_ops mx_pop_ops = {
+  .open = pop_open_mailbox,
+  .close = pop_close_mailbox,
+};
diff -r 391492f27fef -r ae203b2c56a6 pop.h
--- a/pop.h     Tue May 10 09:42:27 2016 -0700
+++ b/pop.h     Thu May 12 12:41:25 2016 -0700
@@ -106,10 +106,11 @@
 
 /* pop.c */
 int pop_check_mailbox (CONTEXT *, int *);
-int pop_open_mailbox (CONTEXT *);
 int pop_sync_mailbox (CONTEXT *, int *);
 int pop_fetch_message (MESSAGE *, CONTEXT *, int);
 int pop_close_mailbox (CONTEXT *);
 void pop_fetch_mail (void);
 
+extern struct mx_ops mx_pop_ops;
+
 #endif

Reply via email to