Partially fixes NB#77528
[modest] / src / widgets / modest-attachment-view.c
index 7960fb0..0d171bf 100644 (file)
@@ -42,6 +42,7 @@
 #include <modest-mail-operation.h>
 #include <modest-mail-operation-queue.h>
 #include <modest-runtime.h>
+#include <modest-count-stream.h>
 
 #define GET_SIZE_BUFFER_SIZE 128
 
@@ -122,28 +123,27 @@ get_mime_part_size_thread (gpointer thr_user_data)
 {
        ModestAttachmentView *view =  (ModestAttachmentView *) thr_user_data;
        ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (view);
-       gchar read_buffer[GET_SIZE_BUFFER_SIZE];
        TnyStream *stream;
-       gssize readed_size;
-       gssize total = 0;
-
-       stream = tny_camel_mem_stream_new ();
-       tny_mime_part_decode_to_stream (priv->mime_part, stream);
-       tny_stream_reset (stream);
-       if (tny_stream_is_eos (stream)) {
-               tny_stream_close (stream);
-               stream = tny_mime_part_get_stream (priv->mime_part);
+       gsize total = 0;
+       gssize result = 0;
+
+       stream = modest_count_stream_new();
+       result = tny_mime_part_decode_to_stream (priv->mime_part, stream, NULL);
+       total = modest_count_stream_get_count(MODEST_COUNT_STREAM (stream));
+       if (total == 0) {
+               modest_count_stream_reset_count(MODEST_COUNT_STREAM (stream));
+               result = tny_mime_part_write_to_stream (priv->mime_part, stream, NULL);
+               total = modest_count_stream_get_count(MODEST_COUNT_STREAM (stream));
        }
        
-       while (!tny_stream_is_eos (stream)) {
-               readed_size = tny_stream_read (stream, read_buffer, GET_SIZE_BUFFER_SIZE);
-               total += readed_size;
+       /* if there was an error, don't set the size (this is pretty uncommon) */
+       if (result < 0) {
+               g_warning ("%s: error while writing mime part to stream\n", __FUNCTION__);
+       } else {
+               priv->size = (guint64)total;
+               g_idle_add (idle_get_mime_part_size_cb, g_object_ref (view));
        }
 
-       priv->size = total;
-
-       g_idle_add (idle_get_mime_part_size_cb, g_object_ref (view));
-
        g_object_unref (stream);
        g_object_unref (view);
 
@@ -212,7 +212,7 @@ modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mim
                TnyHeader *header = tny_msg_get_header (TNY_MSG (mime_part));
                if (TNY_IS_HEADER (header)) {
                        filename = g_strdup (tny_header_get_subject (header));
-                       if (filename == NULL)
+                       if (filename == NULL || filename[0] == '\0')
                                filename = g_strdup (_("mail_va_no_subject"));
                        if (priv->is_purged)
                                file_icon_name = modest_platform_get_file_icon_name (NULL, NULL, NULL);