+static GdkPixbuf *
+pixbuf_from_stream (TnyStream *stream, const gchar *mime_type)
+{
+ GdkPixbufLoader *loader;
+ GdkPixbuf *pixbuf;
+
+ loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, NULL);
+
+ if (loader == NULL)
+ return NULL;
+
+ tny_stream_reset (TNY_STREAM (stream));
+ while (!tny_stream_is_eos (TNY_STREAM (stream))) {
+ unsigned char read_buffer[128];
+ gint readed;
+ readed = tny_stream_read (TNY_STREAM (stream), (char *) read_buffer, 128);
+ if (!gdk_pixbuf_loader_write (loader, read_buffer, readed, NULL))
+ break;
+ }
+
+ pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
+ g_object_ref (pixbuf);
+ gdk_pixbuf_loader_close (loader, NULL);
+ g_object_unref (loader);
+
+ if (gdk_pixbuf_get_width (pixbuf) > IMAGE_MAX_WIDTH) {
+ GdkPixbuf *new_pixbuf;
+ gint new_height;
+ new_height = (gdk_pixbuf_get_height (pixbuf) * IMAGE_MAX_WIDTH) /
+ gdk_pixbuf_get_width (pixbuf);
+ new_pixbuf = gdk_pixbuf_scale_simple (pixbuf, IMAGE_MAX_WIDTH, new_height, GDK_INTERP_BILINEAR);
+ g_object_unref (pixbuf);
+ pixbuf = new_pixbuf;
+ }
+
+ return pixbuf;
+}
+
+static void
+replace_with_images (ModestMsgEditWindow *self, GList *attachments)
+{
+ ModestMsgEditWindowPrivate *priv;
+ GList *node;
+
+ priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
+
+ for (node = attachments; node != NULL; node = g_list_next (node)) {
+ TnyMimePart *part = (TnyMimePart *) node->data;
+ const gchar *cid = tny_mime_part_get_content_id (part);
+ const gchar *mime_type = tny_mime_part_get_content_type (part);
+ if ((cid != NULL)&&(mime_type != NULL)) {
+ TnyStream *stream = tny_mime_part_get_stream (part);
+ GdkPixbuf *pixbuf = pixbuf_from_stream (stream, mime_type);
+ g_object_unref (stream);
+
+ if (pixbuf != NULL) {
+ wp_text_buffer_replace_image (WP_TEXT_BUFFER (priv->text_buffer), cid, pixbuf);
+ g_object_unref (pixbuf);
+ }
+ }
+ }
+}
+
+static void
+get_related_images (ModestMsgEditWindow *self, TnyMsg *msg)
+{
+ TnyMimePart *parent = NULL;
+ const gchar *content_type = NULL;
+ ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
+
+ content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
+
+ if (content_type && !g_strcasecmp (content_type, "multipart/related")) {
+ parent = g_object_ref (msg);
+ } else if (content_type && !g_strcasecmp (content_type, "multipart/mixed")) {
+ TnyList *parts = TNY_LIST (tny_simple_list_new ());
+ TnyIterator *iter;
+
+ tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
+ iter = tny_list_create_iterator (parts);
+ while (!tny_iterator_is_done (iter)) {
+ TnyMimePart *part;
+ part = TNY_MIME_PART (tny_iterator_get_current (iter));
+ content_type = tny_mime_part_get_content_type (part);
+ if (content_type && !g_strcasecmp (content_type, "multipart/related")) {
+ parent = part;
+ break;
+ } else {
+ g_object_unref (part);
+ }
+ tny_iterator_next (iter);
+ }
+ g_object_unref (iter);
+ g_object_unref (parts);
+ }
+
+ if (parent != NULL) {
+ TnyList *parts = TNY_LIST (tny_simple_list_new ());
+ TnyIterator *iter;
+
+ tny_mime_part_get_parts (TNY_MIME_PART (parent), parts);
+ iter = tny_list_create_iterator (parts);
+ while (!tny_iterator_is_done (iter)) {
+ TnyMimePart *part;
+ part = TNY_MIME_PART (tny_iterator_get_current (iter));
+ content_type = tny_mime_part_get_content_type (part);
+ if (content_type && g_str_has_prefix (content_type, "image/")) {
+ priv->images = g_list_prepend (priv->images, part);
+ } else {
+ g_object_unref (part);
+ }
+ tny_iterator_next (iter);
+ }
+ g_object_unref (iter);
+ g_object_unref (parts);
+ g_object_unref (parent);
+ }
+}
+
+static void
+update_last_cid (ModestMsgEditWindow *self, GList *attachments)
+{
+ GList *node;
+ ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
+
+ for (node = attachments; node != NULL; node = g_list_next (node)) {
+ TnyMimePart *part = (TnyMimePart *) node->data;
+ const gchar *cid = tny_mime_part_get_content_id (part);
+ if (cid != NULL) {
+ char *invalid = NULL;
+ gint int_cid = strtol (cid, &invalid, 10);
+ if ((invalid != NULL) && (*invalid == '\0') && (int_cid > priv->last_cid)) {
+ priv->last_cid = int_cid;
+ }
+ }
+
+ }
+}