+/* Write a MIME text part out to the given stream.
+ *
+ * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
+ * each output line.
+ *
+ * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
+ * UTF-8) will be performed, so it is inappropriate to call this
+ * function with a non-text part. Doing so will trigger an internal
+ * error.
+ */
+void
+show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
+ notmuch_show_text_part_flags flags)
+{
+ GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
+ GMimeStream *stream_filter = NULL;
+ GMimeFilter *crlf_filter = NULL;
+ GMimeFilter *windows_filter = NULL;
+ GMimeDataWrapper *wrapper;
+ const char *charset;
+
+ if (! g_mime_content_type_is_type (content_type, "text", "*"))
+ INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
+ g_mime_content_type_to_string (content_type));
+
+ if (stream_out == NULL)
+ return;
+
+ charset = g_mime_object_get_content_type_parameter (part, "charset");
+ charset = charset ? g_mime_charset_canon_name (charset) : NULL;
+ wrapper = g_mime_part_get_content_object (GMIME_PART (part));
+ if (wrapper && charset && !g_ascii_strncasecmp (charset, "iso-8859-", 9)) {
+ GMimeStream *null_stream = NULL;
+ GMimeStream *null_stream_filter = NULL;
+
+ /* Check for mislabeled Windows encoding */
+ null_stream = g_mime_stream_null_new ();
+ null_stream_filter = g_mime_stream_filter_new (null_stream);
+ windows_filter = g_mime_filter_windows_new (charset);
+ g_mime_stream_filter_add(GMIME_STREAM_FILTER (null_stream_filter),
+ windows_filter);
+ g_mime_data_wrapper_write_to_stream (wrapper, null_stream_filter);
+ charset = g_mime_filter_windows_real_charset(
+ (GMimeFilterWindows *) windows_filter);
+
+ if (null_stream_filter)
+ g_object_unref (null_stream_filter);
+ if (null_stream)
+ g_object_unref (null_stream);
+ /* Keep a reference to windows_filter in order to prevent the
+ * charset string from deallocation. */
+ }
+
+ stream_filter = g_mime_stream_filter_new (stream_out);
+ crlf_filter = g_mime_filter_crlf_new (false, false);
+ g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
+ crlf_filter);
+ g_object_unref (crlf_filter);
+
+ if (charset) {
+ GMimeFilter *charset_filter;
+ charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
+ /* This result can be NULL for things like "unknown-8bit".
+ * Don't set a NULL filter as that makes GMime print
+ * annoying assertion-failure messages on stderr. */
+ if (charset_filter) {
+ g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
+ charset_filter);
+ g_object_unref (charset_filter);
+ }
+
+ }
+
+ if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
+ GMimeFilter *reply_filter;
+ reply_filter = g_mime_filter_reply_new (true);
+ if (reply_filter) {
+ g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
+ reply_filter);
+ g_object_unref (reply_filter);
+ }
+ }
+
+ if (wrapper && stream_filter)
+ g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
+ if (stream_filter)
+ g_object_unref(stream_filter);
+ if (windows_filter)
+ g_object_unref (windows_filter);