X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fwidgets%2Fmodest-attachment-view.c;h=7c7cae449b1c4c422466b0dd4d8cf2fc82e739ed;hp=2fedbf3094c285e6395e577ccc1e409e2e968dbf;hb=7b18df5711ee7782411329e8a47d1d573d898136;hpb=f2f85fb554572fa18695ab7b60c6143347c92d92 diff --git a/src/widgets/modest-attachment-view.c b/src/widgets/modest-attachment-view.c index 2fedbf3..7c7cae4 100644 --- a/src/widgets/modest-attachment-view.c +++ b/src/widgets/modest-attachment-view.c @@ -38,16 +38,18 @@ #include #include #include -#include #include #include #include +#include + +#define GET_SIZE_BUFFER_SIZE 128 static GObjectClass *parent_class = NULL; -typedef struct _ModestAttachmentViewPriv ModestAttachmentViewPriv; +typedef struct _ModestAttachmentViewPrivate ModestAttachmentViewPrivate; -struct _ModestAttachmentViewPriv +struct _ModestAttachmentViewPrivate { TnyMimePart *mime_part; @@ -55,7 +57,7 @@ struct _ModestAttachmentViewPriv GtkWidget *filename_view; GtkWidget *size_view; - guint get_size_idle_id; + gboolean detect_size; TnyStream *get_size_stream; guint64 size; @@ -67,7 +69,7 @@ struct _ModestAttachmentViewPriv #define UNKNOWN_FILE_ICON "qgn_list_gene_unknown_file" #define MODEST_ATTACHMENT_VIEW_GET_PRIVATE(o) \ - (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENT_VIEW, ModestAttachmentViewPriv)) + (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ATTACHMENT_VIEW, ModestAttachmentViewPrivate)) /* TnyMimePartView functions */ static TnyMimePart *modest_attachment_view_get_part (TnyMimePartView *self); @@ -90,31 +92,99 @@ static void tny_mime_part_view_init (gpointer g, gpointer iface_data); static void update_filename_request (ModestAttachmentView *self); -static void get_mime_part_size_cb (ModestMailOperation *mail_op, - gssize size, - gpointer userdata); - - -static void -get_mime_part_size_cb (ModestMailOperation *mail_op, - gssize size, - gpointer userdata) +static void update_size_label (ModestAttachmentView *self) { - ModestAttachmentView *att_view = MODEST_ATTACHMENT_VIEW (userdata); - ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (att_view); + ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self); gchar *size_str; gchar *label_text; - if (GTK_WIDGET_VISIBLE (att_view)) { - size_str = modest_text_utils_get_display_size (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); + 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); + + return priv->size; +} static TnyMimePart * modest_attachment_view_get_part (TnyMimePartView *self) @@ -125,7 +195,7 @@ modest_attachment_view_get_part (TnyMimePartView *self) static TnyMimePart * modest_attachment_view_get_part_default (TnyMimePartView *self) { - ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self); + ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self); if (priv->mime_part) return TNY_MIME_PART (g_object_ref (priv->mime_part)); @@ -136,7 +206,7 @@ modest_attachment_view_get_part_default (TnyMimePartView *self) static void update_filename_request (ModestAttachmentView *self) { - ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self); + ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self); /* gint width, height; */ pango_layout_set_text (PANGO_LAYOUT (priv->layout_full_filename), @@ -156,7 +226,7 @@ modest_attachment_view_set_part (TnyMimePartView *self, TnyMimePart *mime_part) static void modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mime_part) { - ModestAttachmentViewPriv *priv = NULL; + ModestAttachmentViewPrivate *priv = NULL; gchar *filename = NULL; gchar *file_icon_name = NULL; gboolean show_size = FALSE; @@ -169,7 +239,7 @@ modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mim g_object_unref (priv->mime_part); } - priv->mime_part = mime_part; + priv->mime_part = g_object_ref (mime_part); priv->size = 0; priv->is_purged = tny_mime_part_is_purged (mime_part); @@ -177,8 +247,10 @@ modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mim if (TNY_IS_MSG (mime_part)) { 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) + filename = g_strdup (tny_mime_part_get_filename (mime_part)); + if (!filename) + filename = tny_header_dup_subject (header); + 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); @@ -220,13 +292,11 @@ modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mim gtk_label_set_text (GTK_LABEL (priv->size_view), ""); - if (show_size) { - ModestMailOperation *mail_op = - modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_INFO, G_OBJECT (self)); - modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op); - modest_mail_operation_get_mime_part_size (mail_op, mime_part, get_mime_part_size_cb, - self, NULL); - g_object_unref (mail_op); + if (show_size && priv->detect_size) { + g_object_ref (self); + if (!priv->get_size_stream) + priv->get_size_stream = modest_count_stream_new (); + g_thread_create (get_mime_part_size_thread, self, FALSE, NULL); } gtk_widget_queue_draw (GTK_WIDGET (self)); @@ -242,22 +312,15 @@ modest_attachment_view_clear (TnyMimePartView *self) static void modest_attachment_view_clear_default (TnyMimePartView *self) { - ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self); + ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self); if (priv->mime_part != NULL) { g_object_unref (priv->mime_part); priv->mime_part = NULL; } - if (priv->get_size_idle_id != 0) { - g_source_remove (priv->get_size_idle_id); - priv->get_size_idle_id = 0; - } - - if (priv->get_size_stream != NULL) { - g_object_unref (priv->get_size_stream); - priv->get_size_stream = NULL; - } + if (priv->get_size_stream) + modest_count_stream_reset_count(MODEST_COUNT_STREAM (priv->get_size_stream)); priv->size = 0; @@ -281,11 +344,13 @@ modest_attachment_view_clear_default (TnyMimePartView *self) * Return value: a new #ModestAttachmentView instance implemented for Gtk+ **/ GtkWidget* -modest_attachment_view_new (TnyMimePart *mime_part) +modest_attachment_view_new (TnyMimePart *mime_part, gboolean detect_size) { ModestAttachmentView *self = g_object_new (MODEST_TYPE_ATTACHMENT_VIEW, NULL); + modest_attachment_view_set_detect_size (self, detect_size); + modest_attachment_view_set_part (TNY_MIME_PART_VIEW (self), mime_part); return GTK_WIDGET (self); @@ -294,7 +359,7 @@ modest_attachment_view_new (TnyMimePart *mime_part) static void modest_attachment_view_instance_init (GTypeInstance *instance, gpointer g_class) { - ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (instance); + ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (instance); PangoContext *context; GtkWidget *box = NULL; @@ -311,9 +376,9 @@ modest_attachment_view_instance_init (GTypeInstance *instance, gpointer g_class) gtk_misc_set_alignment (GTK_MISC (priv->size_view), 0.0, 0.5); gtk_misc_set_alignment (GTK_MISC (priv->filename_view), 0.0, 0.5); - priv->get_size_idle_id = 0; priv->get_size_stream = NULL; priv->size = 0; + priv->detect_size = TRUE; box = gtk_hbox_new (FALSE, 0); gtk_box_pack_start (GTK_BOX (box), priv->icon, FALSE, FALSE, 0); @@ -341,12 +406,7 @@ modest_attachment_view_instance_init (GTypeInstance *instance, gpointer g_class) static void modest_attachment_view_finalize (GObject *object) { - ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (object); - - if (priv->get_size_idle_id) { - g_source_remove (priv->get_size_idle_id); - priv->get_size_idle_id = 0; - } + ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (object); if (priv->get_size_stream != NULL) { g_object_unref (priv->get_size_stream); @@ -366,7 +426,7 @@ modest_attachment_view_finalize (GObject *object) static void size_allocate (GtkWidget *widget, GtkAllocation *allocation) { - ModestAttachmentViewPriv *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (widget); + ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (widget); gint width, width_diff; GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation); @@ -406,7 +466,7 @@ modest_attachment_view_class_init (ModestAttachmentViewClass *klass) widget_class->size_allocate = size_allocate; - g_type_class_add_private (object_class, sizeof (ModestAttachmentViewPriv)); + g_type_class_add_private (object_class, sizeof (ModestAttachmentViewPrivate)); return; } @@ -416,9 +476,9 @@ tny_mime_part_view_init (gpointer g, gpointer iface_data) { TnyMimePartViewIface *klass = (TnyMimePartViewIface *)g; - klass->get_part_func = modest_attachment_view_get_part; - klass->set_part_func = modest_attachment_view_set_part; - klass->clear_func = modest_attachment_view_clear; + klass->get_part = modest_attachment_view_get_part; + klass->set_part = modest_attachment_view_set_part; + klass->clear = modest_attachment_view_clear; return; }