* whether attachment images are displayed inline is now in gconf
[modest] / src / modest-tny-msg-view.c
1 /* modest-tny-msg-view.c */
2
3 /* insert (c)/licensing information) */
4
5 #include "modest-tny-msg-view.h"
6 #include "modest-tny-stream-gtkhtml.h"
7 #include "modest-tny-msg-actions.h"
8
9 #include <tny-text-buffer-stream.h>
10 #include <string.h>
11 #include <regex.h>
12 #include <ctype.h>
13 #include <glib/gi18n.h>
14 #include <gtkhtml/gtkhtml.h>
15 #include <gtkhtml/gtkhtml-stream.h>
16
17 /* 'private'/'protected' functions */
18 static void     modest_tny_msg_view_class_init   (ModestTnyMsgViewClass *klass);
19 static void     modest_tny_msg_view_init         (ModestTnyMsgView *obj);
20 static void     modest_tny_msg_view_finalize     (GObject *obj);
21
22
23 static GSList*  get_url_matches (GString *txt);
24 static gboolean fill_gtkhtml_with_txt (ModestTnyMsgView *self, GtkHTML* gtkhtml, const gchar* txt, TnyMsgIface *msg);
25
26 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri,
27                                  ModestTnyMsgView *msg_view);
28 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri,
29                                   GtkHTMLStream *stream,
30                                   ModestTnyMsgView *msg_view);
31 static gchar *construct_virtual_filename(const gchar *filename, const gint position, const gchar *id, const gboolean active);
32 static gchar *construct_virtual_filename_from_mime_part(TnyMsgMimePartIface *msg, const gint position);
33 gint virtual_filename_get_pos(const gchar *filename);
34 /*
35  * we need these regexps to find URLs in plain text e-mails
36  */
37 typedef struct _UrlMatchPattern UrlMatchPattern;
38 struct _UrlMatchPattern {
39         gchar   *regex;
40         regex_t *preg;
41         gchar   *prefix;
42         
43 };
44 #define MAIL_VIEWER_URL_MATCH_PATTERNS  {\
45         { "(file|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
46           NULL, NULL },\
47         { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
48           NULL, "http://" },\
49         { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
50           NULL, "ftp://" },\
51         { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
52            NULL, NULL},                                             \
53         { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+",                    \
54           NULL, NULL},\
55         { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
56           NULL, "mailto:"}\
57         }
58
59
60 /* list my signals */
61 enum {
62         /* MY_SIGNAL_1, */
63         /* MY_SIGNAL_2, */
64         LAST_SIGNAL
65 };
66
67 typedef struct _ModestTnyMsgViewPrivate ModestTnyMsgViewPrivate;
68 struct _ModestTnyMsgViewPrivate {
69         GtkWidget *gtkhtml;
70         TnyMsgIface *msg;
71         ModestConf *conf;
72 };
73 #define MODEST_TNY_MSG_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
74                                                  MODEST_TYPE_TNY_MSG_VIEW, \
75                                                  ModestTnyMsgViewPrivate))
76 /* globals */
77 static GtkContainerClass *parent_class = NULL;
78
79 /* uncomment the following if you have defined any signals */
80 /* static guint signals[LAST_SIGNAL] = {0}; */
81
82 GType
83 modest_tny_msg_view_get_type (void)
84 {
85         static GType my_type = 0;
86         if (!my_type) {
87                 static const GTypeInfo my_info = {
88                         sizeof(ModestTnyMsgViewClass),
89                         NULL,           /* base init */
90                         NULL,           /* base finalize */
91                         (GClassInitFunc) modest_tny_msg_view_class_init,
92                         NULL,           /* class finalize */
93                         NULL,           /* class data */
94                         sizeof(ModestTnyMsgView),
95                         1,              /* n_preallocs */
96                         (GInstanceInitFunc) modest_tny_msg_view_init,
97                 };
98                 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
99                                                   "ModestTnyMsgView",
100                                                   &my_info, 0);
101         }
102         return my_type;
103 }
104
105 static void
106 modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass)
107 {
108         GObjectClass *gobject_class;
109         gobject_class = (GObjectClass*) klass;
110
111         parent_class            = g_type_class_peek_parent (klass);
112         gobject_class->finalize = modest_tny_msg_view_finalize;
113
114         g_type_class_add_private (gobject_class, sizeof(ModestTnyMsgViewPrivate));
115 }
116
117 static void
118 modest_tny_msg_view_init (ModestTnyMsgView *obj)
119 {
120         ModestTnyMsgViewPrivate *priv;
121         
122         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
123
124         priv->msg = NULL;
125         
126         priv->gtkhtml = gtk_html_new();
127         
128         priv->conf = NULL;
129
130         gtk_html_set_editable        (GTK_HTML(priv->gtkhtml), FALSE);
131         gtk_html_allow_selection     (GTK_HTML(priv->gtkhtml), TRUE);
132         gtk_html_set_caret_mode      (GTK_HTML(priv->gtkhtml), FALSE);
133         gtk_html_set_blocking        (GTK_HTML(priv->gtkhtml), FALSE);
134         gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
135
136         g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
137                           G_CALLBACK(on_link_clicked), obj);
138         
139         g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
140                           G_CALLBACK(on_url_requested), obj);
141 }
142         
143
144 static void
145 modest_tny_msg_view_finalize (GObject *obj)
146 {       
147         
148 }
149
150 GtkWidget*
151 modest_tny_msg_view_new (TnyMsgIface *msg, ModestConf *conf)
152 {
153         GObject *obj;
154         ModestTnyMsgView* self;
155         ModestTnyMsgViewPrivate *priv;
156         
157         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_MSG_VIEW, NULL));
158         self = MODEST_TNY_MSG_VIEW(obj);
159         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
160
161         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
162                                        GTK_POLICY_AUTOMATIC,
163                                        GTK_POLICY_AUTOMATIC);
164
165         if (priv->gtkhtml) 
166                 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);  
167         
168         if (msg)
169                 modest_tny_msg_view_set_message (self, msg);
170         
171         priv->conf = conf;
172
173         return GTK_WIDGET(self);
174 }
175
176
177
178 static gboolean
179 on_link_clicked (GtkWidget *widget, const gchar *uri,
180                                  ModestTnyMsgView *msg_view)
181 {
182         ModestTnyMsgViewPrivate *priv;
183         
184         
185         if (g_str_has_prefix(uri, "attachment:")) {
186                 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(msg_view);
187                 /* toggle ...SHOW_ATTACHMENTS_INLINE */
188                 modest_conf_set_bool(priv->conf,
189                                                          MODEST_CONF_MSG_VIEW_SHOW_ATTACHMENTS_INLINE,
190                                                          !modest_conf_get_bool(priv->conf,
191                                                                                                    MODEST_CONF_MSG_VIEW_SHOW_ATTACHMENTS_INLINE,
192                                                                                                    NULL),
193                                                          NULL);
194
195                 modest_tny_msg_view_set_message(msg_view, priv->msg);
196                 return TRUE;
197         }
198         g_message ("link clicked: %s", uri); /* FIXME */
199         return FALSE;
200         
201 }
202
203
204
205 static TnyMsgMimePartIface *
206 find_cid_image (TnyMsgIface *msg, const gchar *cid)
207 {
208         TnyMsgMimePartIface *part = NULL;
209         GList *parts;
210
211         g_return_val_if_fail (msg, NULL);
212         g_return_val_if_fail (cid, NULL);
213         
214         parts  = (GList*) tny_msg_iface_get_parts (msg);
215         while (parts && !part) {
216                 const gchar *part_cid;
217                 part = TNY_MSG_MIME_PART_IFACE(parts->data);
218                 part_cid = tny_msg_mime_part_iface_get_content_id (part);
219                 printf("CMP:%s:%s\n", cid, part_cid);
220                 if (part_cid && strcmp (cid, part_cid) == 0)
221                         return part; /* we found it! */
222                 
223                 part = NULL;
224                 parts = parts->next;
225         }
226         
227         return part;
228 }
229
230
231 static TnyMsgMimePartIface *
232 find_attachment_by_filename (TnyMsgIface *msg, const gchar *fn)
233 {
234         TnyMsgMimePartIface *part = NULL;
235         GList *parts;
236         gchar *dummy;
237         gint pos;
238
239         g_return_val_if_fail (msg, NULL);
240         g_return_val_if_fail (fn, NULL);
241         
242         parts  = (GList*) tny_msg_iface_get_parts (msg);
243         pos = virtual_filename_get_pos(fn);
244         
245         g_return_val_if_fail(((pos >= 0) && (pos < g_list_length(parts))), NULL);
246         
247         part = g_list_nth_data(parts, pos);
248         
249         dummy = construct_virtual_filename_from_mime_part(part, pos);
250         if (strcmp(dummy, fn) == 0)
251                 return part;
252         else
253                 return NULL;
254 }
255
256
257 static gboolean
258 on_url_requested (GtkWidget *widget, const gchar *uri,
259                   GtkHTMLStream *stream,
260                   ModestTnyMsgView *msg_view)
261 {
262         
263         ModestTnyMsgViewPrivate *priv;
264         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (msg_view);
265
266         g_message ("url requested: %s", uri);
267         
268         if (g_str_has_prefix (uri, "cid:")) {
269                 /* +4 ==> skip "cid:" */
270                 
271                 TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
272                 if (!part) {
273                         g_message ("%s not found", uri + 4);
274                         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
275                 } else {
276                         TnyStreamIface *tny_stream =
277                                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
278                         tny_msg_mime_part_iface_decode_to_stream (part,tny_stream);
279                         gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
280                 }
281         } else if (g_str_has_prefix (uri, "Attachment:")) {
282                 TnyMsgMimePartIface *part = find_attachment_by_filename (priv->msg, uri);
283                 if (!part) {
284                         g_message ("%s not found", uri);
285                         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
286                 } else {
287                         TnyStreamIface *tny_stream =
288                                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
289                         tny_msg_mime_part_iface_decode_to_stream (part,tny_stream);
290                         gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
291                 }
292         }
293         return TRUE;
294 }
295
296
297
298
299 typedef struct  {
300         guint offset;
301         guint len;
302         const gchar* prefix;
303 } url_match_t;
304
305
306
307 static gchar *
308 construct_virtual_filename(const gchar *filename, const gint position, const gchar *id, const gboolean active)
309 {
310         GString *s;
311         g_return_val_if_fail((position >= 0), "AttachmentInvalid");
312
313         s = g_string_new("");
314         if (active)
315                 g_string_append(s, "Attachment:");
316         else
317                 g_string_append(s, "attachment:");
318         g_string_append_printf(s, "%d:", position);
319         if (id)
320                 g_string_append(s, id);
321         g_string_append_c(s, ':');
322         if (filename)
323                 g_string_append(s, filename);
324         g_string_append_c(s, ':');
325         return g_string_free(s, FALSE);
326 }
327
328
329 static gchar *
330 construct_virtual_filename_from_mime_part(TnyMsgMimePartIface *msg, const gint position)
331 {
332         const gchar *id, *filename;
333         const gboolean active = TRUE;
334         
335         filename = tny_msg_mime_part_iface_get_filename(
336                                                                         TNY_MSG_MIME_PART_IFACE(msg));
337         if (!filename)
338                 filename = "[unknown]";
339         id = tny_msg_mime_part_iface_get_content_id(
340                                                                         TNY_MSG_MIME_PART_IFACE(msg));
341         
342         return construct_virtual_filename(filename, position, id, active);
343 }
344
345 const gchar *
346 get_next_token(const gchar *s, gint *len)
347 {
348         gchar *i1, *i2;
349         i1 = (char *) s;
350         i2 = (char *) s;
351         
352         while (i2[0]) {
353                 if (i2[0] == ':')
354                         break;
355                 i2++;
356         }
357         if (!i2[0])
358                 return NULL;
359         *len = i2 - i1;
360         return ++i2;
361 }
362
363 /* maybe I should use libregexp */
364 gint
365 virtual_filename_get_pos(const gchar *filename)
366 {
367         const gchar *i1, *i2;
368         gint len, pos;
369         GString *dummy;
370         
371         i1 = filename;
372         i2 = filename;
373         
374         /* check prefix */
375         i2 = get_next_token(i2, &len);
376         if (strncmp(i1, "Attachment", len) != 0)
377                 return -1;
378         i1 = i2;
379                 
380         /* get position */
381         i2 = get_next_token(i2, &len);
382         if (i2 == NULL)
383                 return -1;
384         dummy = g_string_new_len(i1, len);
385         pos = atoi(dummy->str);
386         g_string_free(dummy, FALSE);
387         return pos;
388 }       
389
390
391 static gchar *
392 attachments_as_html(ModestTnyMsgView *self, TnyMsgIface *msg)
393 {
394         ModestTnyMsgViewPrivate *priv;
395         gboolean attachments_found = FALSE;
396         GString *appendix;
397         const GList *attachment_list, *attachment;
398         const gchar *content_type, *filename, *id;
399         gchar *virtual_filename;
400         gboolean show_attachments_inline;
401         
402         if (!msg)
403                 return g_malloc0(1);
404         
405         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
406         
407         /* CLEANUP: starting a new HTML may be unsupported */
408         appendix = g_string_new("<HTML><BODY>\n<hr><h5>Attachments:</h5>\n");
409         
410         attachment_list = tny_msg_iface_get_parts(msg);
411         attachment = attachment_list;
412         while (attachment) {
413                 filename = "";
414                 content_type = tny_msg_mime_part_iface_get_content_type(
415                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data));
416                 g_return_val_if_fail(content_type, NULL);
417                 if (      tny_msg_mime_part_iface_content_type_is(
418                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data),
419                                                                                 "image/jpeg")
420                            || tny_msg_mime_part_iface_content_type_is(
421                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data),
422                                                                                 "image/gif")) {
423                         filename = tny_msg_mime_part_iface_get_filename(
424                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data));
425                         if (!filename)
426                                 filename = "[unknown]";
427                         else
428                                 attachments_found = TRUE;
429                         id = tny_msg_mime_part_iface_get_content_id(
430                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data));
431                         show_attachments_inline = modest_conf_get_bool(priv->conf, MODEST_CONF_MSG_VIEW_SHOW_ATTACHMENTS_INLINE, NULL);
432                         virtual_filename = construct_virtual_filename(filename,
433                                          g_list_position((GList *)attachment_list, (GList *) attachment),
434                                          id, show_attachments_inline);
435                         printf("VF:%s\n", virtual_filename);
436                         if (show_attachments_inline) {
437                                 g_string_append_printf(appendix, "<IMG src=\"%s\">\n<BR><A href=\"attachment:%s\">%s</A>\n", virtual_filename, filename, filename);
438                         } else {
439                                 g_string_append_printf(appendix, "<A href=\"attachment:%s\">%s</A>: %s<BR>\n", filename, filename, content_type);
440                         }
441                 }
442                 attachment = attachment->next;
443         }
444         g_string_append(appendix, "</BODY></HTML>");
445         if (!attachments_found)
446                 g_string_assign(appendix, "");
447         return g_string_free(appendix, FALSE);
448 }
449
450 static void
451 hyperlinkify_plain_text (GString *txt)
452 {
453         GSList *cursor;
454         GSList *match_list = get_url_matches (txt);
455
456         /* we will work backwards, so the offsets stay valid */
457         for (cursor = match_list; cursor; cursor = cursor->next) {
458
459                 url_match_t *match = (url_match_t*) cursor->data;
460                 gchar *url  = g_strndup (txt->str + match->offset, match->len);
461                 gchar *repl = NULL; /* replacement  */
462
463                 /* the prefix is NULL: use the one that is already there */
464                 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
465                                         match->prefix ? match->prefix : "", url, url);
466
467                 /* replace the old thing with our hyperlink
468                  * replacement thing */
469                 g_string_erase  (txt, match->offset, match->len);
470                 g_string_insert (txt, match->offset, repl);
471                 
472                 g_free (url);
473                 g_free (repl);
474
475                 g_free (cursor->data);  
476         }
477         
478         g_slist_free (match_list);
479 }
480
481
482
483 static gchar *
484 convert_to_html (const gchar *data)
485 {
486         int              i;
487         gboolean         first_space = TRUE;
488         GString         *html;      
489         gsize           len;
490
491         if (!data)
492                 return NULL;
493
494         len = strlen (data);
495         html = g_string_sized_new (len + 100);  /* just a  guess... */
496         
497         g_string_append_printf (html,
498                                 "<html>"
499                                 "<head>"
500                                 "<meta http-equiv=\"content-type\""
501                                 " content=\"text/html; charset=utf8\">"
502                                 "</head>"
503                                 "<body><tt>");
504         
505         /* replace with special html chars where needed*/
506         for (i = 0; i != len; ++i)  {
507                 char    kar = data[i]; 
508                 switch (kar) {
509                         
510                 case 0:  break; /* ignore embedded \0s */       
511                 case '<' : g_string_append   (html, "&lt;"); break;
512                 case '>' : g_string_append   (html, "&gt;"); break;
513                 case '&' : g_string_append   (html, "&quot;"); break;
514                 case '\n': g_string_append   (html, "<br>\n"); break;
515                 default:
516                         if (kar == ' ') {
517                                 g_string_append (html, first_space ? " " : "&nbsp;");
518                                 first_space = FALSE;
519                         } else  if (kar == '\t')
520                                 g_string_append (html, "&nbsp; &nbsp;&nbsp;");
521                         else {
522                                 int charnum = 0;
523                                 first_space = TRUE;
524                                 /* optimization trick: accumulate 'normal' chars, then copy */
525                                 do {
526                                         kar = data [++charnum + i];
527                                         
528                                 } while ((i + charnum < len) &&
529                                          (kar > '>' || (kar != '<' && kar != '>'
530                                                         && kar != '&' && kar !=  ' '
531                                                         && kar != '\n' && kar != '\t')));
532                                 g_string_append_len (html, &data[i], charnum);
533                                 i += (charnum  - 1);
534                         }
535                 }
536         }
537         
538         g_string_append (html, "</tt></body></html>");
539         hyperlinkify_plain_text (html);
540
541         return g_string_free (html, FALSE);
542 }
543
544
545
546
547 static gint 
548 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
549 {
550         return match2->offset - match1->offset;
551 }
552
553
554
555 /*
556  * check if the match is inside an existing match... */
557 static void
558 chk_partial_match (const url_match_t *match, int* offset)
559 {
560         if (*offset >= match->offset && *offset < match->offset + match->len)
561                 *offset = -1;
562 }
563
564 static GSList*
565 get_url_matches (GString *txt)
566 {
567         regmatch_t rm;
568         int rv, i, offset = 0;
569         GSList *match_list = NULL;
570
571         static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
572         const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
573
574         /* initalize the regexps */
575         for (i = 0; i != pattern_num; ++i) {
576                 patterns[i].preg = g_new0 (regex_t,1);
577                 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
578                                   REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
579         }
580         /* find all the matches */
581         for (i = 0; i != pattern_num; ++i) {
582                 offset     = 0; 
583                 while (1) {
584                         int test_offset;
585                         if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
586                                 g_assert (rv == REG_NOMATCH); /* this should not happen */
587                                 break; /* try next regexp */ 
588                         }
589                         if (rm.rm_so == -1)
590                                 break;
591
592                         /* FIXME: optimize this */
593                         /* to avoid partial matches on something that was already found... */
594                         /* check_partial_match will put -1 in the data ptr if that is the case */
595                         test_offset = offset + rm.rm_so;
596                         g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
597                         
598                         /* make a list of our matches (<offset, len, prefix> tupels)*/
599                         if (test_offset != -1) {
600                                 url_match_t *match = g_new (url_match_t,1);
601                                 match->offset = offset + rm.rm_so;
602                                 match->len    = rm.rm_eo - rm.rm_so;
603                                 match->prefix = patterns[i].prefix;
604                                 match_list = g_slist_prepend (match_list, match);
605                         }
606                         offset += rm.rm_eo;
607                 }
608         }
609
610         for (i = 0; i != pattern_num; ++i) {
611                 regfree (patterns[i].preg);
612                 g_free  (patterns[i].preg);
613         } /* don't free patterns itself -- it's static */
614         
615         /* now sort the list, so the matches are in reverse order of occurence.
616          * that way, we can do the replacements starting from the end, so we don't need
617          * to recalculate the offsets
618          */
619         match_list = g_slist_sort (match_list,
620                                    (GCompareFunc)cmp_offsets_reverse); 
621         return match_list;      
622 }
623
624 static gboolean
625 fill_gtkhtml_with_txt (ModestTnyMsgView *self, GtkHTML* gtkhtml, const gchar* txt, TnyMsgIface *msg)
626 {
627         GString *html;
628         gchar *html_attachments;
629         
630         g_return_val_if_fail (gtkhtml, FALSE);
631         g_return_val_if_fail (txt, FALSE);
632
633         html = g_string_new(convert_to_html (txt));
634         html_attachments = attachments_as_html(self, msg);
635         g_string_append(html, html_attachments);
636
637         gtk_html_load_from_string (gtkhtml, html->str, html->len);
638         g_string_free (html, TRUE);
639         g_free(html_attachments);
640
641         return TRUE;
642 }
643
644
645
646 static gboolean
647 set_html_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body, TnyMsgIface *msg)
648 {
649         gchar *html_attachments;
650         TnyStreamIface *gtkhtml_stream; 
651         ModestTnyMsgViewPrivate *priv;
652         
653         g_return_val_if_fail (self, FALSE);
654         g_return_val_if_fail (tny_body, FALSE);
655         
656         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
657
658         gtkhtml_stream =
659                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
660                                  (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
661         
662         tny_stream_iface_reset (gtkhtml_stream);
663         tny_msg_mime_part_iface_decode_to_stream (tny_body, gtkhtml_stream);
664         html_attachments = attachments_as_html(self, msg);
665         /* is this clean? */
666         gtkhtml_write(gtkhtml_stream, html_attachments, strlen(html_attachments));
667         tny_stream_iface_reset (gtkhtml_stream);
668
669         g_object_unref (G_OBJECT(gtkhtml_stream));
670         g_free (html_attachments);
671         
672         return TRUE;
673 }
674
675
676 /* this is a hack --> we use the tny_text_buffer_stream to
677  * get the message text, then write to gtkhtml 'by hand' */
678 static gboolean
679 set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body, TnyMsgIface *msg)
680 {
681         GtkTextBuffer *buf;
682         GtkTextIter begin, end;
683         TnyStreamIface* txt_stream;
684         gchar *txt;
685         ModestTnyMsgViewPrivate *priv;
686                 
687         g_return_val_if_fail (self, FALSE);
688         g_return_val_if_fail (tny_body, FALSE);
689
690         priv           = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
691         
692         buf            = gtk_text_buffer_new (NULL);
693         txt_stream     = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
694                 
695         tny_stream_iface_reset (txt_stream);
696         tny_msg_mime_part_iface_decode_to_stream (tny_body, txt_stream);
697         tny_stream_iface_reset (txt_stream);            
698         
699         gtk_text_buffer_get_bounds (buf, &begin, &end);
700         txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
701         
702         fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml), txt, msg);
703
704         g_object_unref (G_OBJECT(txt_stream));
705         g_object_unref (G_OBJECT(buf));
706
707         g_free (txt);
708         return TRUE;
709 }
710
711 gchar *
712 modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self)
713 {
714         ModestTnyMsgViewPrivate *priv;
715         gchar *sel;
716         GtkWidget *html;
717         int len;
718         GtkClipboard *clip;
719
720         g_return_val_if_fail (self, NULL);
721         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
722         html = priv->gtkhtml;
723         
724         /* I'm sure there is a better way to check for selected text */
725         sel = gtk_html_get_selection_html(GTK_HTML(html), &len);
726         if (!sel)
727                 return NULL;
728         
729         g_free(sel);
730         
731         clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
732         return gtk_clipboard_wait_for_text(clip);
733 }
734
735 void
736 modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg)
737 {
738         TnyMsgMimePartIface *body;
739         ModestTnyMsgViewPrivate *priv;
740
741         g_return_if_fail (self);
742         
743         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
744
745         priv->msg = msg;
746         
747         fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml), "", msg);
748
749         if (!msg) 
750                 return;
751         
752         body = modest_tny_msg_actions_find_body_part (msg, "text/html");
753         if (body) {
754                 set_html_message (self, body, msg);
755                 return;
756         }
757         
758         body = modest_tny_msg_actions_find_body_part (msg, "text/plain");
759         if (body) {
760                 set_text_message (self, body, msg);
761                 return;
762         }
763
764         /* hmmmmm */
765         fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml),
766                                 _("Unsupported message type"), msg);
767 }