mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-22 02:48:08 +01:00
show: Convert non-envelope format_part_json to use sprinter
This commit is contained in:
parent
85b326f13c
commit
6da306b40a
1 changed files with 46 additions and 28 deletions
|
@ -588,7 +588,6 @@ format_part_json (const void *ctx, sprinter_t *sp, mime_node_t *node,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *local = talloc_new (ctx);
|
|
||||||
/* The disposition and content-type metadata are associated with
|
/* The disposition and content-type metadata are associated with
|
||||||
* the envelope for message parts */
|
* the envelope for message parts */
|
||||||
GMimeObject *meta = node->envelope_part ?
|
GMimeObject *meta = node->envelope_part ?
|
||||||
|
@ -597,31 +596,41 @@ format_part_json (const void *ctx, sprinter_t *sp, mime_node_t *node,
|
||||||
const char *cid = g_mime_object_get_content_id (meta);
|
const char *cid = g_mime_object_get_content_id (meta);
|
||||||
const char *filename = GMIME_IS_PART (node->part) ?
|
const char *filename = GMIME_IS_PART (node->part) ?
|
||||||
g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
|
g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
|
||||||
const char *terminator = "";
|
int nclose = 0;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (!first)
|
sp->begin_map (sp);
|
||||||
printf (", ");
|
|
||||||
|
|
||||||
printf ("{\"id\": %d", node->part_num);
|
sp->map_key (sp, "id");
|
||||||
|
sp->integer (sp, node->part_num);
|
||||||
|
|
||||||
if (node->decrypt_attempted)
|
if (node->decrypt_attempted) {
|
||||||
printf (", \"encstatus\": [{\"status\": \"%s\"}]",
|
sp->map_key (sp, "encstatus");
|
||||||
node->decrypt_success ? "good" : "bad");
|
sp->begin_list (sp);
|
||||||
|
sp->begin_map (sp);
|
||||||
|
sp->map_key (sp, "status");
|
||||||
|
sp->string (sp, node->decrypt_success ? "good" : "bad");
|
||||||
|
sp->end (sp);
|
||||||
|
sp->end (sp);
|
||||||
|
}
|
||||||
|
|
||||||
if (node->verify_attempted) {
|
if (node->verify_attempted) {
|
||||||
printf (", \"sigstatus\": ");
|
sp->map_key (sp, "sigstatus");
|
||||||
format_part_sigstatus_json (sp, node);
|
format_part_sigstatus_json (sp, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf (", \"content-type\": %s",
|
sp->map_key (sp, "content-type");
|
||||||
json_quote_str (local, g_mime_content_type_to_string (content_type)));
|
sp->string (sp, g_mime_content_type_to_string (content_type));
|
||||||
|
|
||||||
if (cid)
|
if (cid) {
|
||||||
printf (", \"content-id\": %s", json_quote_str (local, cid));
|
sp->map_key (sp, "content-id");
|
||||||
|
sp->string (sp, cid);
|
||||||
|
}
|
||||||
|
|
||||||
if (filename)
|
if (filename) {
|
||||||
printf (", \"filename\": %s", json_quote_str (local, filename));
|
sp->map_key (sp, "filename");
|
||||||
|
sp->string (sp, filename);
|
||||||
|
}
|
||||||
|
|
||||||
if (GMIME_IS_PART (node->part)) {
|
if (GMIME_IS_PART (node->part)) {
|
||||||
/* For non-HTML text parts, we include the content in the
|
/* For non-HTML text parts, we include the content in the
|
||||||
|
@ -636,35 +645,44 @@ format_part_json (const void *ctx, sprinter_t *sp, mime_node_t *node,
|
||||||
if (g_mime_content_type_is_type (content_type, "text", "html")) {
|
if (g_mime_content_type_is_type (content_type, "text", "html")) {
|
||||||
const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
|
const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
|
||||||
|
|
||||||
if (content_charset != NULL)
|
if (content_charset != NULL) {
|
||||||
printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
|
sp->map_key (sp, "content-charset");
|
||||||
|
sp->string (sp, content_charset);
|
||||||
|
}
|
||||||
} else if (g_mime_content_type_is_type (content_type, "text", "*")) {
|
} else if (g_mime_content_type_is_type (content_type, "text", "*")) {
|
||||||
GMimeStream *stream_memory = g_mime_stream_mem_new ();
|
GMimeStream *stream_memory = g_mime_stream_mem_new ();
|
||||||
GByteArray *part_content;
|
GByteArray *part_content;
|
||||||
show_text_part_content (node->part, stream_memory, 0);
|
show_text_part_content (node->part, stream_memory, 0);
|
||||||
part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
|
part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
|
||||||
|
sp->map_key (sp, "content");
|
||||||
printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
|
sp->string_len (sp, (char *) part_content->data, part_content->len);
|
||||||
g_object_unref (stream_memory);
|
g_object_unref (stream_memory);
|
||||||
}
|
}
|
||||||
} else if (GMIME_IS_MULTIPART (node->part)) {
|
} else if (GMIME_IS_MULTIPART (node->part)) {
|
||||||
printf (", \"content\": [");
|
sp->map_key (sp, "content");
|
||||||
terminator = "]";
|
sp->begin_list (sp);
|
||||||
|
nclose = 1;
|
||||||
} else if (GMIME_IS_MESSAGE (node->part)) {
|
} else if (GMIME_IS_MESSAGE (node->part)) {
|
||||||
printf (", \"content\": [{");
|
sp->map_key (sp, "content");
|
||||||
printf ("\"headers\": ");
|
sp->begin_list (sp);
|
||||||
|
sp->begin_map (sp);
|
||||||
|
|
||||||
|
sp->map_key (sp, "headers");
|
||||||
format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE);
|
format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE);
|
||||||
|
|
||||||
printf (", \"body\": [");
|
sp->map_key (sp, "body");
|
||||||
terminator = "]}]";
|
sp->begin_list (sp);
|
||||||
|
nclose = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
talloc_free (local);
|
|
||||||
|
|
||||||
for (i = 0; i < node->nchildren; i++)
|
for (i = 0; i < node->nchildren; i++)
|
||||||
format_part_json (ctx, sp, mime_node_child (node, i), i == 0, TRUE);
|
format_part_json (ctx, sp, mime_node_child (node, i), i == 0, TRUE);
|
||||||
|
|
||||||
printf ("%s}", terminator);
|
/* Close content structures */
|
||||||
|
for (i = 0; i < nclose; i++)
|
||||||
|
sp->end (sp);
|
||||||
|
/* Close part map */
|
||||||
|
sp->end (sp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static notmuch_status_t
|
static notmuch_status_t
|
||||||
|
|
Loading…
Reference in a new issue