+static void update_size_label (ModestAttachmentView *self)
+{
+ ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
+ gchar *size_str;
+ gchar *label_text;
+
+ size_str = modest_text_utils_get_display_size (priv->size);
+ label_text = g_strdup_printf (" (%s)", size_str);
+ g_free (size_str);
+ gtk_label_set_text (GTK_LABEL (priv->size_view), label_text);
+ g_free (label_text);
+}
+
+static gboolean
+idle_get_mime_part_size_cb (gpointer userdata)
+{
+ ModestAttachmentView *view = (ModestAttachmentView *) userdata;
+ gdk_threads_enter ();
+
+ if (GTK_WIDGET_VISIBLE (view)) {
+ update_size_label (view);
+ }
+
+ gdk_threads_leave ();
+
+ g_object_unref (view);
+
+ return FALSE;
+}
+
+static gpointer
+get_mime_part_size_thread (gpointer thr_user_data)
+{
+ ModestAttachmentView *view = (ModestAttachmentView *) thr_user_data;
+ ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (view);
+ gsize total = 0;
+ gssize result = 0;
+
+ result = tny_mime_part_decode_to_stream (priv->mime_part, priv->get_size_stream, NULL);
+ total = modest_count_stream_get_count(MODEST_COUNT_STREAM (priv->get_size_stream));
+ if (total == 0) {
+ modest_count_stream_reset_count(MODEST_COUNT_STREAM (priv->get_size_stream));
+ result = tny_mime_part_write_to_stream (priv->mime_part, priv->get_size_stream, NULL);
+ total = modest_count_stream_get_count(MODEST_COUNT_STREAM (priv->get_size_stream));
+ }
+
+ /* 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));
+ }
+ g_object_unref (view);
+
+ return NULL;
+}
+
+void
+modest_attachment_view_set_detect_size (ModestAttachmentView *self, gboolean detect_size)
+{
+ ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
+
+ priv->detect_size = detect_size;
+
+}
+
+void
+modest_attachment_view_set_size (ModestAttachmentView *self, guint64 size)
+{
+ ModestAttachmentViewPrivate *priv;
+
+ g_return_if_fail (MODEST_IS_ATTACHMENT_VIEW (self));
+ priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
+
+ if (!priv->detect_size) {
+ priv->size = size;
+ update_size_label (self);
+ } else {
+ g_assert ("Shouldn't set the size of the attachment view if detect size is enabled");
+ }
+}
+
+guint64
+modest_attachment_view_get_size (ModestAttachmentView *self)
+{
+ ModestAttachmentViewPrivate *priv;
+
+ g_return_val_if_fail (MODEST_IS_ATTACHMENT_VIEW (self), 0);
+ priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);