From f4bb7d7748f3c407858e9c844d365411c586d861 Mon Sep 17 00:00:00 2001 From: Matthew Barnes Date: Tue, 25 Feb 2014 10:23:11 -0500 Subject: EMailFormatter: Use GOutputStream instead of CamelStream. --- mail/e-mail-request.c | 46 ++++++++++++++++++++++++---------------------- mail/em-utils.c | 25 +++++++++++++++---------- 2 files changed, 39 insertions(+), 32 deletions(-) (limited to 'mail') diff --git a/mail/e-mail-request.c b/mail/e-mail-request.c index eb1a32b02c..8c1bbc19bb 100644 --- a/mail/e-mail-request.c +++ b/mail/e-mail-request.c @@ -65,12 +65,11 @@ handle_mail_request (GSimpleAsyncResult *simple, GCancellable *cancellable) { EMailRequest *request = E_MAIL_REQUEST (object); - GInputStream *stream; EMailFormatter *formatter; EMailPartList *part_list; CamelObjectBag *registry; - CamelStream *output_stream; - GByteArray *byte_array; + GInputStream *input_stream; + GOutputStream *output_stream; const gchar *val; const gchar *default_charset, *charset; @@ -122,12 +121,7 @@ handle_mail_request (GSimpleAsyncResult *simple, if (charset != NULL && *charset != '\0') e_mail_formatter_set_charset (formatter, charset); - byte_array = g_byte_array_new (); - output_stream = camel_stream_mem_new (); - - /* We retain ownership of the byte array. */ - camel_stream_mem_set_byte_array ( - CAMEL_STREAM_MEM (output_stream), byte_array); + output_stream = g_memory_output_stream_new_resizable (); val = g_hash_table_lookup (request->priv->uri_query, "part_id"); if (val != NULL) { @@ -162,7 +156,7 @@ handle_mail_request (GSimpleAsyncResult *simple, dw = camel_medium_get_content (CAMEL_MEDIUM (mime_part)); g_return_if_fail (dw); - camel_data_wrapper_decode_to_stream_sync ( + camel_data_wrapper_decode_to_output_stream_sync ( dw, output_stream, cancellable, NULL); g_object_unref (mime_part); @@ -185,31 +179,39 @@ handle_mail_request (GSimpleAsyncResult *simple, } no_part: - g_clear_object (&output_stream); g_clear_object (&context.part_list); - if (byte_array->data == NULL) { + g_output_stream_close (output_stream, NULL, NULL); + + if (request->priv->bytes != NULL) + g_bytes_unref (request->priv->bytes); + + request->priv->bytes = g_memory_output_stream_steal_as_bytes ( + G_MEMORY_OUTPUT_STREAM (output_stream)); + + if (g_bytes_get_size (request->priv->bytes) == 0) { gchar *data; + g_bytes_unref (request->priv->bytes); + data = g_strdup_printf ( "

%s

", _("The message has no text content.")); - g_byte_array_append ( - byte_array, (guint8 *) data, strlen (data)); - g_free (data); - } - if (request->priv->bytes != NULL) - g_bytes_unref (request->priv->bytes); - request->priv->bytes = g_byte_array_free_to_bytes (byte_array); + /* Takes ownership of the string. */ + request->priv->bytes = g_bytes_new_take ( + data, strlen (data) + 1); + } - stream = g_memory_input_stream_new_from_bytes (request->priv->bytes); + input_stream = + g_memory_input_stream_new_from_bytes (request->priv->bytes); g_simple_async_result_set_op_res_gpointer ( - simple, g_object_ref (stream), + simple, g_object_ref (input_stream), (GDestroyNotify) g_object_unref); - g_object_unref (stream); + g_object_unref (input_stream); + g_object_unref (output_stream); g_object_unref (part_list); g_object_unref (formatter); diff --git a/mail/em-utils.c b/mail/em-utils.c index 5a938ab41b..2291e0869b 100644 --- a/mail/em-utils.c +++ b/mail/em-utils.c @@ -1196,23 +1196,21 @@ em_utils_message_to_html (CamelSession *session, { EMailFormatter *formatter; EMailParser *parser = NULL; - CamelStream *mem; - GByteArray *buf; + GOutputStream *stream; EShell *shell; GtkWindow *window; EMailPart *hidden_text_html_part = NULL; EMailPartValidityFlags is_validity_found = 0; GQueue queue = G_QUEUE_INIT; GList *head, *link; + gchar *data; shell = e_shell_get_default (); window = e_shell_get_active_window (shell); g_return_val_if_fail (CAMEL_IS_SESSION (session), NULL); - buf = g_byte_array_new (); - mem = camel_stream_mem_new (); - camel_stream_mem_set_byte_array (CAMEL_STREAM_MEM (mem), buf); + stream = g_memory_output_stream_new_resizable (); formatter = e_mail_formatter_quote_new (credits, flags); e_mail_formatter_update_style (formatter, @@ -1268,7 +1266,7 @@ em_utils_message_to_html (CamelSession *session, *validity_found = is_validity_found; e_mail_formatter_format_sync ( - formatter, parts_list, mem, 0, + formatter, parts_list, stream, 0, E_MAIL_FORMATTER_MODE_PRINTING, NULL); g_object_unref (formatter); @@ -1280,12 +1278,19 @@ em_utils_message_to_html (CamelSession *session, g_object_unref (parser); if (append != NULL && *append != '\0') - camel_stream_write_string (mem, append, NULL, NULL); + g_output_stream_write_all ( + stream, append, strlen (append), NULL, NULL, NULL); - camel_stream_write (mem, "", 1, NULL, NULL); - g_object_unref (mem); + g_output_stream_write (stream, "", 1, NULL, NULL); - return (gchar *) g_byte_array_free (buf, FALSE); + g_output_stream_close (stream, NULL, NULL); + + data = g_memory_output_stream_steal_data ( + G_MEMORY_OUTPUT_STREAM (stream)); + + g_object_unref (stream); + + return data; } /* ********************************************************************** */ -- cgit v1.2.3