Cleanup of libbalsa/send.c



Hi,

I did some cleanup of libbalsa/send.c, it contains the following cleanups:
*Moved more common code to add_mutt_body_plain
*Made new function add_mutt_body_file, moved code from 
libbalsa_message_postpone and libbalsa_create_msg
*changed position of first { of a function to a new line.

Bart

-- 
Fingerprint = CD4D 5601 287D F075 6F96  6157 99F9 E56A 4B08 6D06
Index: libbalsa/send.c
===================================================================
RCS file: /cvs/gnome/balsa/libbalsa/send.c,v
retrieving revision 1.141
diff -u -b -B -w -p -r1.141 send.c
--- libbalsa/send.c	2001/10/27 21:28:24	1.141
+++ libbalsa/send.c	2001/10/31 16:02:02
@@ -270,35 +270,56 @@ encode_descriptions (BODY *b)
 }
 
 static BODY *
-add_mutt_body_plain(const gchar * charset, gint encoding_style, 
+add_mutt_body_plain(LibBalsaMessageBody *body, gint encoding_style, 
 		    gboolean flow)
 {
-    BODY *body;
+    BODY *mutt_body;
     gchar buffer[PATH_MAX];
+    const gchar * charset=body->charset;
+	FILE *tempfp = NULL;
 
     g_return_val_if_fail(charset, NULL);
     libbalsa_lock_mutt();
-    body = mutt_new_body();
+    mutt_body = mutt_new_body();
 
-    body->type = TYPETEXT;
-    body->subtype = g_strdup("plain");
-    body->unlink = 1;
-    body->use_disp = 0;
+    mutt_body->type = TYPETEXT;
+    mutt_body->subtype = g_strdup("plain");
+    mutt_body->unlink = 1;
+    mutt_body->use_disp = 0;
 
-    body->encoding = encoding_style;
+    mutt_body->encoding = encoding_style;
 
-    mutt_set_parameter("charset", charset, &body->parameter);
+    mutt_set_parameter("charset", charset, &mutt_body->parameter);
     if (flow)
-	mutt_set_parameter("format", "flowed", &body->parameter);
+	mutt_set_parameter("format", "flowed", &mutt_body->parameter);
 
     mutt_mktemp(buffer);
-    body->filename = g_strdup(buffer);
-    mutt_update_encoding(body);
+    mutt_body->filename = g_strdup(buffer);
+    mutt_update_encoding(mutt_body);
 
     libbalsa_unlock_mutt();
 
-    return body;
+    if (body->mime_type) {
+        /* change the type and subtype within the mutt body */
+        gchar *type, *subtype;
+
+        type = g_strdup (body->mime_type);
+        if ((subtype = strchr (type, '/'))) {
+            *subtype++ = 0;
+            libbalsa_lock_mutt();
+            mutt_body->type = mutt_check_mime_type (type);
+            g_free(mutt_body->subtype);
+            mutt_body->subtype = g_strdup(subtype);
+            libbalsa_unlock_mutt();
+        }
+        g_free (type);
 }
+    tempfp = safe_fopen(mutt_body->filename, "w+");
+    fputs(body->buffer, tempfp);
+    fclose(tempfp);
+    tempfp = NULL;
+    return mutt_body;
+}
 
 static BODY *
 add_mutt_body_as_extbody(const gchar *filename, const gchar *mime_type)
@@ -335,6 +356,40 @@ add_mutt_body_as_extbody(const gchar *fi
     return body;
 }
 
+static BODY *
+add_mutt_body_file(const gchar *filename, const gchar *body_mime_type)
+{
+    BODY *newbdy;
+    gchar **mime_type;
+
+    libbalsa_lock_mutt();
+    newbdy = mutt_make_file_attach(filename);
+    libbalsa_unlock_mutt();
+    if (!newbdy)
+        return;
+
+    /* Do this here because we don't want
+     * to use libmutt's mime types */
+    mime_type = g_strsplit(body_mime_type, "/", 2);
+    /* use BASE64 encoding for non-text mime types 
+       use 8BIT for message */
+    libbalsa_lock_mutt();
+    newbdy->disposition = DISPATTACH;
+    if(!strcasecmp(mime_type[0],"message") && 
+       !strcasecmp(mime_type[1],"rfc822")) {
+        newbdy->encoding = ENC8BIT;
+        newbdy->disposition = DISPINLINE;
+    } else if(strcasecmp(mime_type[0],"text") != 0)
+        newbdy->encoding = ENCBASE64;
+    newbdy->type = mutt_check_mime_type(mime_type[0]);
+    g_free(newbdy->subtype);
+    newbdy->subtype = g_strdup(mime_type[1]);
+    libbalsa_unlock_mutt();
+    g_strfreev(mime_type);
+
+    return newbdy;
+}
+
 #if 0
 /* you never know when you will need this one... */
 static void dump_queue(const char*msg)
@@ -1001,7 +1055,8 @@ monitor_cb (const char *buf, int buflen,
 
 /* [BCS] radically different since it uses the libESMTP interface.
  */
-static guint balsa_send_message_real(SendMessageInfo* info) {
+static guint balsa_send_message_real(SendMessageInfo* info)
+{
 
 #ifdef BALSA_USE_THREADS
     SendThreadMessage *threadmsg;
@@ -1067,7 +1122,8 @@ static guint balsa_send_message_real(Sen
    libmutt calls. Also, structure info should be freed before exiting.
 */
 
-static guint balsa_send_message_real(SendMessageInfo* info) {
+static guint balsa_send_message_real(SendMessageInfo* info)
+{
     MessageQueueItem *mqi, *next_message;
     int i;
 #ifdef BALSA_USE_THREADS
@@ -1133,7 +1189,8 @@ static guint balsa_send_message_real(Sen
 
 
 static void
-message2HEADER(LibBalsaMessage * message, HEADER * hdr) {
+message2HEADER(LibBalsaMessage * message, HEADER * hdr)
+{
     gchar *tmp;
 
     libbalsa_lock_mutt();
@@ -1219,7 +1276,8 @@ libbalsa_message_postpone(LibBalsaMessag
 			  LibBalsaMailbox * draftbox,
 			  LibBalsaMessage * reply_message,
 			  gchar * fcc, gint encoding,
-			  gboolean flow) {
+			  gboolean flow)
+{
     HEADER *msg;
     BODY *last, *newbdy;
     gchar *tmp;
@@ -1240,64 +1298,20 @@ libbalsa_message_postpone(LibBalsaMessag
 	last = last->next;
 
     while (body) {
-	FILE *tempfp = NULL;
 	newbdy = NULL;
 
 	if (body->filename) {
-	    libbalsa_lock_mutt();
-	    newbdy = mutt_make_file_attach(body->filename);
-	    libbalsa_unlock_mutt();
+            newbdy = 
+                add_mutt_body_file(body->filename, 
+                                   body->mime_type ? body->mime_type :
+                                   libbalsa_lookup_mime_type(body->filename));
 	    if (!newbdy) {
 		g_warning("Cannot attach file: %s.\nPostponing without it.",
 		     body->filename);
-	    } else {
-		gchar **mime_type;
-
-		/* Do this here because we don't want
-		 * to use libmutt's mime types */
-		if (!body->mime_type)
-		    mime_type =
-			g_strsplit(libbalsa_lookup_mime_type(body->filename),
-				   "/", 2);
-		else
-		    mime_type = g_strsplit(body->mime_type, "/", 2);
-		/* use BASE64 encoding for non-text mime types 
-		   use 8BIT for message */
-		libbalsa_lock_mutt();
-		newbdy->disposition = DISPATTACH;
-		if(!strcasecmp(mime_type[0],"message") && 
-		   !strcasecmp(mime_type[1],"rfc822")) {
-		    newbdy->encoding = ENC8BIT;
-		    newbdy->disposition = DISPINLINE;
-		} else if(strcasecmp(mime_type[0],"text") != 0)
-		    newbdy->encoding = ENCBASE64;
-		newbdy->type = mutt_check_mime_type(mime_type[0]);
-		g_free(newbdy->subtype);
-		newbdy->subtype = g_strdup(mime_type[1]);
-		libbalsa_unlock_mutt();
-		g_strfreev(mime_type);
 	    }
 	} else if (body->buffer) {
-	    newbdy = add_mutt_body_plain(body->charset, encoding, flow);
-	    if (body->mime_type) {
-		/* change the type and subtype within the mutt body */
-		gchar *type, *subtype;
-		
-		type = g_strdup (body->mime_type);
-		if ((subtype = strchr (type, '/'))) {
-		    *subtype++ = 0;
-		    libbalsa_lock_mutt();
-		    newbdy->type = mutt_check_mime_type (type);
-		    libbalsa_unlock_mutt();
-		    newbdy->subtype = g_strdup(subtype);
+            newbdy = add_mutt_body_plain(body, encoding, flow);
 		}
-		g_free (type);
-	    }
-	    tempfp = safe_fopen(newbdy->filename, "w+");
-	    fputs(body->buffer, tempfp);
-	    fclose(tempfp);
-	    tempfp = NULL;
-	}
 
 	if (newbdy) {
 	    if (last)
@@ -1357,21 +1371,21 @@ libbalsa_message_postpone(LibBalsaMessag
 */ 
 static gboolean
 libbalsa_create_msg(LibBalsaMessage * message, HEADER * msg, char *tmpfile,
-		    gint encoding, gboolean flow, int queu) {
+		    gint encoding, gboolean flow, int queu)
+{
     BODY *last, *newbdy;
-    FILE *tempfp;
+    FILE *tempfp=NULL;
     HEADER *msg_tmp;
     MESSAGE *mensaje;
     LIST *in_reply_to;
     LIST *references;
     LibBalsaMessageBody *body;
     GList *list;
-    gchar **mime_type;
     gboolean res = TRUE;
 
     message2HEADER(message, msg);
 
-    /* If the message has references set, add them to he envelope */
+    /* If the message has references set, add them to the envelope */
     if (message->references != NULL) {
 	list = message->references;
         libbalsa_lock_mutt();
@@ -1408,7 +1422,6 @@ libbalsa_create_msg(LibBalsaMessage * me
 	last = last->next;
 
     while (body) {
-	FILE *tempfp = NULL;
 	newbdy = NULL;
 
 	if (body->filename) {
@@ -1418,61 +1431,18 @@ libbalsa_create_msg(LibBalsaMessage * me
 					     body->mime_type ? body->mime_type :
 					     libbalsa_lookup_mime_type(body->filename));
 	    } else {
-		libbalsa_lock_mutt();
-		newbdy = mutt_make_file_attach(body->filename);
-		libbalsa_unlock_mutt();
+		newbdy = 
+		    add_mutt_body_file(body->filename, 
+					     body->mime_type ? body->mime_type :
+					     libbalsa_lookup_mime_type(body->filename));
 		if (!newbdy) {
 		    g_warning
 			("Cannot attach file: %s.\nSending without it.",
 			 body->filename);
-		} else {
-		    
-		    /* Do this here because we don't want
-		     * to use libmutt's mime types */
-		    if (!body->mime_type)
-			mime_type =
-			    g_strsplit(libbalsa_lookup_mime_type(body->filename),
-				       "/", 2);
-		    else
-			mime_type = g_strsplit(body->mime_type, "/", 2);
-		    /* use BASE64 encoding for non-text mime types 
-		       use 8BIT for message */
-		    libbalsa_lock_mutt();
-		    newbdy->disposition = DISPATTACH;
-		    if(!strcasecmp(mime_type[0],"message") && 
-		       !strcasecmp(mime_type[1],"rfc822")) {
-			newbdy->encoding = ENC8BIT;
-			newbdy->disposition = DISPINLINE;
-		    } else if(strcasecmp(mime_type[0],"text") != 0)
-			newbdy->encoding = ENCBASE64;
-		    newbdy->type = mutt_check_mime_type(mime_type[0]);
-		    g_free(newbdy->subtype);
-		    newbdy->subtype = g_strdup(mime_type[1]);
-		    libbalsa_unlock_mutt();
-		    g_strfreev(mime_type);
 		}
 	    }
 	} else if (body->buffer) {
-	    newbdy = add_mutt_body_plain(body->charset, encoding, flow);
-	    if (body->mime_type) {
-		/* change the type and subtype within the mutt body */
-		gchar *type, *subtype;
-		
-		type = g_strdup (body->mime_type);
-		if ((subtype = strchr (type, '/'))) {
-		    *subtype++ = 0;
-		    libbalsa_lock_mutt();
-		    newbdy->type = mutt_check_mime_type (type);
-		    g_free(newbdy->subtype);
-		    newbdy->subtype = g_strdup(subtype);
-		    libbalsa_unlock_mutt();
-		}
-		g_free (type);
-	    }
-	    tempfp = safe_fopen(newbdy->filename, "w+");
-	    fputs(body->buffer, tempfp);
-	    fclose(tempfp);
-	    tempfp = NULL;
+	    newbdy = add_mutt_body_plain(body, encoding, flow);
 	} else {
 	    /* safe_free bug patch: steal it! */
             libbalsa_lock_mutt();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]