From 42850a93b800db919b74405c6b0056c55cb9d96c Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Mon, 20 Feb 2012 19:26:30 +1900 Subject: [PATCH] [PATCH v2 8/8] show: Further general simplifications of the JSON formatter --- c2/b4dd7623876e9db58fcc4684a14685621e603e | 194 ++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 c2/b4dd7623876e9db58fcc4684a14685621e603e diff --git a/c2/b4dd7623876e9db58fcc4684a14685621e603e b/c2/b4dd7623876e9db58fcc4684a14685621e603e new file mode 100644 index 000000000..5a956bf5e --- /dev/null +++ b/c2/b4dd7623876e9db58fcc4684a14685621e603e @@ -0,0 +1,194 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id DA46A431FC2 + for ; Sun, 19 Feb 2012 16:26:42 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id 4qvs8pMRrU7I for ; + Sun, 19 Feb 2012 16:26:41 -0800 (PST) +Received: from dmz-mailsec-scanner-5.mit.edu (DMZ-MAILSEC-SCANNER-5.MIT.EDU + [18.7.68.34]) + by olra.theworths.org (Postfix) with ESMTP id 94D7E431FC7 + for ; Sun, 19 Feb 2012 16:26:36 -0800 (PST) +X-AuditID: 12074422-b7fd66d0000008f9-c3-4f41933c7c3b +Received: from mailhub-auth-2.mit.edu ( [18.7.62.36]) + by dmz-mailsec-scanner-5.mit.edu (Symantec Messaging Gateway) with SMTP + id 60.06.02297.C33914F4; Sun, 19 Feb 2012 19:26:36 -0500 (EST) +Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) + by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id q1K0QZLf028406; + Sun, 19 Feb 2012 19:26:35 -0500 +Received: from drake.mit.edu + (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com + [209.6.116.242]) (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q1K0QYXY000306 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Sun, 19 Feb 2012 19:26:35 -0500 (EST) +Received: from amthrax by drake.mit.edu with local (Exim 4.77) + (envelope-from ) + id 1RzH5O-0001yX-5i; Sun, 19 Feb 2012 19:26:34 -0500 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH v2 8/8] show: Further general simplifications of the JSON + formatter +Date: Sun, 19 Feb 2012 19:26:30 -0500 +Message-Id: <1329697590-7404-9-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.7.7.3 +In-Reply-To: <1329697590-7404-1-git-send-email-amdragon@mit.edu> +References: <1329240823-7856-1-git-send-email-amdragon@mit.edu> + <1329697590-7404-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFtrDIsWRmVeSWpSXmKPExsUixG6nomsz2dHfYN98UYvrN2cyW7xZOY/V + gcnj8NeFLB7PVt1iDmCK4rJJSc3JLEst0rdL4Mp4236TpWCuYsX/iRtZGhiPSHUxcnJICJhI + XLm0lQ3CFpO4cG89kM3FISSwj1Hi2dn/jBDOBkaJlueHWCGc+0wSG3/sYYJw5jNKHH96ggWk + n01AQ2Lb/uWMILaIgLTEzruzWUFsZgEricNXN4HZwgLBEk8PTASzWQRUJY7tXg60j4ODV8Be + 4t37YogzFCTOrT7HDhLmFHCQWL3DFSQsJFAqceTJFqYJjPwLGBlWMcqm5Fbp5iZm5hSnJusW + Jyfm5aUW6Zrq5WaW6KWmlG5iBAeSi9IOxp8HlQ4xCnAwKvHwSpY5+AuxJpYVV+YeYpTkYFIS + 5T01wdFfiC8pP6UyI7E4I76oNCe1+BCjBAezkgjvAnugHG9KYmVValE+TEqag0VJnFdd652f + kEB6YklqdmpqQWoRTFaGg0NJgnfXJKBGwaLU9NSKtMycEoQ0EwcnyHAeoOHXQGp4iwsSc4sz + 0yHypxgVpcR594MkBEASGaV5cL2wSH/FKA70ijBvGkgVDzBJwHW/AhrMBDSYV8gOZHBJIkJK + qoHRW+fcuubpuxXlqz7y1FkcqUto3LF3W8uD/LsmUwyOF2dYRBRUTri8UereisZM49tNfS/+ + b9nx7OH3Yz9jl9rPrjx/77fopoCrorcu7NfIvZzv9UzpM1P29/MbOxf1R/49eHHVi20HpUwe + vZgTtzHoq2jzLrao8rkMfvuUTBjEVsgb/1FpLtGeq8RSnJFoqMVcVJwIALZRibLPAgAA +Cc: tomi.ollila@iki.fi +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Mon, 20 Feb 2012 00:26:43 -0000 + +--- + 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 + -- 2.26.2