+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_attachments (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
+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;
+ }
+ }
+
+ }
+}