unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Subject: [PATCH 8/8] show: Further general simplifications of the JSON formatter
Date: Tue, 14 Feb 2012 12:33:43 -0500	[thread overview]
Message-ID: <1329240823-7856-9-git-send-email-amdragon@mit.edu> (raw)
In-Reply-To: <1329240823-7856-1-git-send-email-amdragon@mit.edu>

---
 notmuch-show.c |   61 ++++++++++++++++++++-----------------------------------
 1 files changed, 22 insertions(+), 39 deletions(-)

diff --git a/notmuch-show.c b/notmuch-show.c
index 07276c7..6a171a4 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -678,9 +678,10 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
     GMimeObject *meta = node->envelope_part ?
 	GMIME_OBJECT (node->envelope_part) : node->part;
     GMimeContentType *content_type = g_mime_object_get_content_type (meta);
-    GMimeStream *stream_memory = g_mime_stream_mem_new ();
     const char *cid = g_mime_object_get_content_id (meta);
-    GByteArray *part_content;
+    const char *filename = GMIME_IS_PART (node->part) ?
+	g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
+    const char *terminator = "";
     int i;
 
     if (!first)
@@ -688,15 +689,9 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
 
     printf ("{\"id\": %d", node->part_num);
 
-    if (node->decrypt_attempted) {
-	printf (", \"encstatus\": [{\"status\": ");
-	if (node->decrypt_success) {
-	    printf ("\"good\"");
-	} else {
-	    printf ("\"bad\"");
-	}
-	printf ("}]");
-    }
+    if (node->decrypt_attempted)
+	printf (", \"encstatus\": [{\"status\": \"%s\"}]",
+		node->decrypt_success ? "good" : "bad");
 
     if (node->verify_attempted) {
 	printf (", \"sigstatus\": ");
@@ -706,16 +701,13 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
     printf (", \"content-type\": %s",
 	    json_quote_str (local, g_mime_content_type_to_string (content_type)));
 
-    if (cid != NULL)
-	    printf(", \"content-id\": %s", json_quote_str (local, cid));
+    if (cid)
+	printf (", \"content-id\": %s", json_quote_str (local, cid));
+
+    if (filename)
+	printf (", \"filename\": %s", json_quote_str (local, filename));
 
     if (GMIME_IS_PART (node->part)) {
-	const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
-	if (filename)
-	    printf (", \"filename\": %s", json_quote_str (local, filename));
-    }
-
-    if (g_mime_content_type_is_type (content_type, "text", "*")) {
 	/* For non-HTML text parts, we include the content in the
 	 * JSON. Since JSON must be Unicode, we handle charset
 	 * decoding here and do not report a charset to the caller.
@@ -730,42 +722,33 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
 
 	    if (content_charset != NULL)
 		printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
-	} else {
+	} else if (g_mime_content_type_is_type (content_type, "text", "*")) {
+	    GMimeStream *stream_memory = g_mime_stream_mem_new ();
+	    GByteArray *part_content;
 	    show_text_part_content (node->part, stream_memory);
 	    part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
 
 	    printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
+	    g_object_unref (stream_memory);
 	}
-    } else if (g_mime_content_type_is_type (content_type, "multipart", "*")) {
+    } else if (GMIME_IS_MULTIPART (node->part)) {
 	printf (", \"content\": [");
-    } else if (g_mime_content_type_is_type (content_type, "message", "rfc822")) {
+	terminator = "]";
+    } else if (GMIME_IS_MESSAGE (node->part)) {
 	printf (", \"content\": [{");
-    }
-
-    if (stream_memory)
-	g_object_unref (stream_memory);
-
-    if (GMIME_IS_MESSAGE (node->part)) {
 	printf ("\"headers\": ");
 	format_headers_json (local, GMIME_MESSAGE (node->part));
 
 	printf (", \"body\": [");
+	terminator = "]}]";
     }
 
+    talloc_free (local);
+
     for (i = 0; i < node->nchildren; i++)
 	format_part_json (ctx, mime_node_child (node, i), i == 0);
 
-    if (GMIME_IS_MESSAGE (node->part))
-	printf ("]");
-
-    if (g_mime_content_type_is_type (content_type, "multipart", "*"))
-	printf ("]");
-    else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))
-	printf ("}]");
-
-    printf ("}");
-
-    talloc_free (local);
+    printf ("%s}", terminator);
 }
 
 static void
-- 
1.7.7.3

  parent reply	other threads:[~2012-02-14 17:34 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-14 17:33 [PATCH 0/8] Rewrite JSON show format Austin Clements
2012-02-14 17:33 ` [PATCH 1/8] Document the JSON schemata used by show and search Austin Clements
2012-02-14 17:33 ` [PATCH 2/8] show: Convert JSON format to the new self-recursive style Austin Clements
2012-02-14 17:33 ` [PATCH 3/8] show: Use consistent header ordering in the JSON format Austin Clements
2012-02-14 17:33 ` [PATCH 4/8] show: Unify JSON header output for messages and message parts Austin Clements
2012-02-14 17:33 ` [PATCH 5/8] show: Simplify talloc use in format_headers_json Austin Clements
2012-02-14 17:33 ` [PATCH 6/8] show: Make JSON helper functions print complete objects Austin Clements
2012-02-14 17:33 ` [PATCH 7/8] show: Make format_part_sigstatus_json's API consistent between GMIME 2.4 and 2.6 Austin Clements
2012-02-14 17:33 ` Austin Clements [this message]
2012-02-15  5:45 ` [PATCH 0/8] Rewrite JSON show format Adam Wolfe Gordon
2012-02-15 15:00 ` Tomi Ollila
2012-02-15 15:20   ` Adam Wolfe Gordon
2012-02-15 18:15   ` Austin Clements
2012-02-20  0:26 ` [PATCH v2 " Austin Clements
2012-02-20  0:26   ` [PATCH v2 1/8] Document the JSON schemata used by show and search Austin Clements
2012-02-28  2:47     ` David Bremner
2012-02-20  0:26   ` [PATCH v2 2/8] show: Convert JSON format to the new self-recursive style Austin Clements
2012-03-01 12:35     ` David Bremner
2012-02-20  0:26   ` [PATCH v2 3/8] show: Use consistent header ordering in the JSON format Austin Clements
2012-02-20  0:26   ` [PATCH v2 4/8] show: Unify JSON header output for messages and message parts Austin Clements
2012-02-20  0:26   ` [PATCH v2 5/8] show: Simplify talloc use in format_headers_json Austin Clements
2012-02-20  0:26   ` [PATCH v2 6/8] show: Make JSON helper functions print complete objects Austin Clements
2012-02-20  0:26   ` [PATCH v2 7/8] show: Make format_part_sigstatus_json's API consistent between GMIME 2.4 and 2.6 Austin Clements
2012-02-20  0:26   ` [PATCH v2 8/8] show: Further general simplifications of the JSON formatter Austin Clements
2012-02-20 10:17   ` [PATCH v2 0/8] Rewrite JSON show format Tomi Ollila

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1329240823-7856-9-git-send-email-amdragon@mit.edu \
    --to=amdragon@mit.edu \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).