From 7018fc58b4ebf6e2c52102c0443169f8120db261 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 2 Aug 2012 21:14:53 -0400 Subject: [PATCH] show: Convert format_headers_json to use sprinter This no longer requires a talloc context (not that it really did before since it didn't return anything), so we remove its context argument. --- notmuch-client.h | 3 ++- notmuch-reply.c | 2 +- notmuch-show.c | 68 +++++++++++++++++++++++------------------------- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/notmuch-client.h b/notmuch-client.h index 112574c7..b11caff8 100644 --- a/notmuch-client.h +++ b/notmuch-client.h @@ -183,7 +183,8 @@ format_part_json (const void *ctx, struct sprinter *sp, mime_node_t *node, notmuch_bool_t first, notmuch_bool_t output_body); void -format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply); +format_headers_json (struct sprinter *sp, GMimeMessage *message, + notmuch_bool_t reply); typedef enum { NOTMUCH_SHOW_TEXT_PART_REPLY = 1 << 0, diff --git a/notmuch-reply.c b/notmuch-reply.c index 07d4452a..fa6665f7 100644 --- a/notmuch-reply.c +++ b/notmuch-reply.c @@ -617,7 +617,7 @@ notmuch_reply_format_json(void *ctx, /* The headers of the reply message we've created */ printf ("{\"reply-headers\": "); - format_headers_json (ctx, reply, TRUE); + format_headers_json (sp, reply, TRUE); g_object_unref (G_OBJECT (reply)); reply = NULL; diff --git a/notmuch-show.c b/notmuch-show.c index b258f656..9852119f 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -199,48 +199,46 @@ _is_from_line (const char *line) } void -format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply) +format_headers_json (sprinter_t *sp, GMimeMessage *message, + notmuch_bool_t reply) { - void *local = talloc_new (ctx); InternetAddressList *recipients; const char *recipients_string; - printf ("{%s: %s", - json_quote_str (local, "Subject"), - json_quote_str (local, g_mime_message_get_subject (message))); - printf (", %s: %s", - json_quote_str (local, "From"), - json_quote_str (local, g_mime_message_get_sender (message))); + sp->begin_map (sp); + + sp->map_key (sp, "Subject"); + sp->string (sp, g_mime_message_get_subject (message)); + + sp->map_key (sp, "From"); + sp->string (sp, g_mime_message_get_sender (message)); + recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO); recipients_string = internet_address_list_to_string (recipients, 0); - if (recipients_string) - printf (", %s: %s", - json_quote_str (local, "To"), - json_quote_str (local, recipients_string)); - recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC); - recipients_string = internet_address_list_to_string (recipients, 0); - if (recipients_string) - printf (", %s: %s", - json_quote_str (local, "Cc"), - json_quote_str (local, recipients_string)); - - if (reply) { - printf (", %s: %s", - json_quote_str (local, "In-reply-to"), - json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"))); - - printf (", %s: %s", - json_quote_str (local, "References"), - json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References"))); - } else { - printf (", %s: %s", - json_quote_str (local, "Date"), - json_quote_str (local, g_mime_message_get_date_as_string (message))); + if (recipients_string) { + sp->map_key (sp, "To"); + sp->string (sp, recipients_string); } - printf ("}"); + recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC); + recipients_string = internet_address_list_to_string (recipients, 0); + if (recipients_string) { + sp->map_key (sp, "Cc"); + sp->string (sp, recipients_string); + } - talloc_free (local); + if (reply) { + sp->map_key (sp, "In-reply-to"); + sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to")); + + sp->map_key (sp, "References"); + sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References")); + } else { + sp->map_key (sp, "Date"); + sp->string (sp, g_mime_message_get_date_as_string (message)); + } + + sp->end (sp); } /* Write a MIME text part out to the given stream. @@ -575,7 +573,7 @@ format_part_json (const void *ctx, sprinter_t *sp, mime_node_t *node, format_message_json (ctx, node->envelope_file); printf ("\"headers\": "); - format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE); + format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE); if (output_body) { printf (", \"body\": ["); @@ -651,7 +649,7 @@ format_part_json (const void *ctx, sprinter_t *sp, mime_node_t *node, } else if (GMIME_IS_MESSAGE (node->part)) { printf (", \"content\": [{"); printf ("\"headers\": "); - format_headers_json (local, GMIME_MESSAGE (node->part), FALSE); + format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE); printf (", \"body\": ["); terminator = "]}]";