Rocco Rutte:
authorpdmef <pdmef@e385b8ad-14ed-0310-8656-cc95a2468c6d>
Tue, 26 Jul 2005 19:58:56 +0000 (19:58 +0000)
committerpdmef <pdmef@e385b8ad-14ed-0310-8656-cc95a2468c6d>
Tue, 26 Jul 2005 19:58:56 +0000 (19:58 +0000)
- modularize mx_commit_message()

git-svn-id: svn://svn.berlios.de/mutt-ng/trunk@350 e385b8ad-14ed-0310-8656-cc95a2468c6d

imap/imap.c
imap/mx_imap.c
mbox.c
mh.c
mh.h
mx.c
mx.h

index fdd468e..099be62 100644 (file)
@@ -1079,7 +1079,7 @@ void imap_close_mailbox (CONTEXT * ctx)
     return;
 
   if (ctx == idata->ctx) {
-    if (idata->state = IMAP_SELECTED) {
+    if (idata->state == IMAP_SELECTED) {
       /* mx_close_mailbox won't sync if there are no deleted messages
       * and the mailbox is unchanged, so we may have to close here */
       if (idata->status != IMAP_FATAL && !ctx->deleted && 
index 2675dff..26ee476 100644 (file)
@@ -50,6 +50,14 @@ static int _imap_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
   return (imap_check_mailbox (ctx, index_hint, 0));
 }
 
+static int imap_commit_message (MESSAGE* msg, CONTEXT* ctx) {
+  int r = 0;
+
+  if ((r = safe_fclose (&msg->fp)) == 0)
+    r = imap_append_message (ctx, msg);
+  return (r);
+}
+
 mx_t* imap_reg_mx (void) {
   mx_t* fmt = safe_calloc (1, sizeof (mx_t));
 
@@ -63,5 +71,6 @@ mx_t* imap_reg_mx (void) {
   fmt->mx_fastclose_mailbox = imap_close_mailbox;
   fmt->mx_sync_mailbox = imap_sync_mailbox;
   fmt->mx_check_mailbox = _imap_check_mailbox;
+  fmt->mx_commit_message = imap_commit_message;
   return (fmt);
 }
diff --git a/mbox.c b/mbox.c
index 3cdd76e..4f426a4 100644 (file)
--- a/mbox.c
+++ b/mbox.c
@@ -1232,6 +1232,25 @@ int mbox_is_magic (const char* path, struct stat* st) {
   return (magic);
 }
 
+static int commit_message (MESSAGE* msg, CONTEXT* ctx, int mbox) {
+  if ((mbox && fputc ('\n', msg->fp) == EOF) ||
+      (!mbox && fputs (MMDF_SEP, msg->fp) == EOF))
+    return (-1);
+  if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
+    mutt_perror (_("Can't write message"));
+    return (-1);
+  }
+  return (0);
+}
+
+static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
+  return (commit_message (msg, ctx, 1));
+}
+
+static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) {
+  return (commit_message (msg, ctx, 0));
+}
+
 static mx_t* reg_mx (void) {
   mx_t* fmt = safe_calloc (1, sizeof (mx_t));
   fmt->local = 1;
@@ -1248,10 +1267,12 @@ static mx_t* reg_mx (void) {
 mx_t* mbox_reg_mx (void) {
   mx_t* fmt = reg_mx ();
   fmt->type = M_MBOX;
+  fmt->mx_commit_message = mbox_commit_message;
   return (fmt);
 }
 mx_t* mmdf_reg_mx (void) {
   mx_t* fmt = reg_mx ();
   fmt->type = M_MMDF;
+  fmt->mx_commit_message = mmdf_commit_message;
   return (fmt);
 }
diff --git a/mh.c b/mh.c
index 66216dc..4ddb155 100644 (file)
--- a/mh.c
+++ b/mh.c
@@ -1093,7 +1093,7 @@ static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr
  * 
  */
 
-int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
+static int maildir_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr)
 {
   char subdir[4];
   char suffix[16];
@@ -1161,7 +1161,7 @@ int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
  */
 
 
-static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
+static int _mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr,
                                short updseq)
 {
   DIR *dirp;
@@ -1226,12 +1226,10 @@ static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
   return 0;
 }
 
-int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
-{
-  return _mh_commit_message (ctx, msg, hdr, 1);
+static int mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr) {
+  return _mh_commit_message (msg, ctx, hdr, 1);
 }
 
-
 /* Sync a message in an MH folder.
  * 
  * This code is also used for attachment deletion in maildir
@@ -1263,9 +1261,9 @@ static int mh_rewrite_message (CONTEXT * ctx, int msgno)
     strfcpy (partpath, h->path, _POSIX_PATH_MAX);
 
     if (ctx->magic == M_MAILDIR)
-      rc = maildir_commit_message (ctx, dest, h);
+      rc = maildir_commit_message (dest, ctx, h);
     else
-      rc = _mh_commit_message (ctx, dest, h, 0);
+      rc = _mh_commit_message (dest, ctx, h, 0);
 
     mx_close_message (&dest);
 
@@ -1978,6 +1976,14 @@ static mx_t* reg_mx (void) {
   return (fmt);
 }
 
+static int mh_commit (MESSAGE* msg, CONTEXT* ctx) {
+  return (mh_commit_message (msg, ctx, NULL));
+}
+
+static int maildir_commit (MESSAGE* msg, CONTEXT* ctx) {
+  return (maildir_commit_message (msg, ctx, NULL));
+}
+
 mx_t* mh_reg_mx (void) {
   mx_t* fmt = reg_mx ();
   fmt->type = M_MH;
@@ -1986,6 +1992,7 @@ mx_t* mh_reg_mx (void) {
   fmt->mx_open_mailbox = mh_read_dir;
   fmt->mx_open_new_message = mh_open_new_message;
   fmt->mx_check_mailbox = mh_check_mailbox;
+  fmt->mx_commit_message = mh_commit;
   return (fmt);
 }
 
@@ -1997,5 +2004,6 @@ mx_t* maildir_reg_mx (void) {
   fmt->mx_open_mailbox = maildir_read_dir;
   fmt->mx_open_new_message = maildir_open_new_message;
   fmt->mx_check_mailbox = maildir_check_mailbox;
+  fmt->mx_commit_message = maildir_commit;
   return (fmt);
 }
diff --git a/mh.h b/mh.h
index 78ad651..65f8247 100644 (file)
--- a/mh.h
+++ b/mh.h
 /* TODO all of these must disappear to achieve good information hiding */
 
 int mh_buffy (const char *);
-int mh_commit_message (CONTEXT *, MESSAGE *, HEADER *);
-
-int maildir_commit_message (CONTEXT *, MESSAGE *, HEADER *);
 FILE *maildir_open_find_message (const char *, const char *);
 
+/* these are the only publicly visible for usage */
+
 mx_t* maildir_reg_mx (void);
 mx_t* mh_reg_mx (void);
 
diff --git a/mx.c b/mx.c
index 183f710..2cdbe7e 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -1243,60 +1243,14 @@ MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
 
 /* commit a message to a folder */
 
-int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
-{
-  int r = 0;
-
+int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
   if (!(msg->write && ctx->append)) {
     debug_print (1, ("msg->write = %d, ctx->append = %d\n", msg->write, ctx->append));
     return -1;
   }
-
-  switch (msg->magic) {
-  case M_MMDF:
-    {
-      if (fputs (MMDF_SEP, msg->fp) == EOF)
-        r = -1;
-      break;
-    }
-
-  case M_MBOX:
-    {
-      if (fputc ('\n', msg->fp) == EOF)
-        r = -1;
-      break;
-    }
-
-#ifdef USE_IMAP
-  case M_IMAP:
-    {
-      if ((r = safe_fclose (&msg->fp)) == 0)
-        r = imap_append_message (ctx, msg);
-      break;
-    }
-#endif
-
-  case M_MAILDIR:
-    {
-      r = maildir_commit_message (ctx, msg, NULL);
-      break;
-    }
-
-  case M_MH:
-    {
-      r = mh_commit_message (ctx, msg, NULL);
-      break;
-    }
-  }
-
-  if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
-      && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
-    mutt_perror (_("Can't write message"));
-
-    r = -1;
-  }
-
-  return r;
+  if (!ctx || !MX_IDX(ctx->magic-1) || !MX_COMMAND(ctx->magic-1,mx_commit_message))
+    return (-1);
+  return (MX_COMMAND(ctx->magic-1,mx_commit_message) (msg, ctx));
 }
 
 /* close a pointer to a message */
diff --git a/mx.h b/mx.h
index a537636..4e09442 100644 (file)
--- a/mx.h
+++ b/mx.h
@@ -102,6 +102,8 @@ typedef struct {
   void (*mx_fastclose_mailbox) (CONTEXT*);
   /* write out changes */
   int (*mx_sync_mailbox) (CONTEXT*, int, int*);
+  /* commit a message to a folder */
+  int (*mx_commit_message) (MESSAGE*, CONTEXT*);
 } mx_t;
 
 /* called from main: init all folder types */