dcdcba5e1da1f66c23c18e3007d26fd7a25a7929
[modest] / src / widgets / modest-msg-view.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <tny-text-buffer-stream.h>
31 #include <string.h>
32 #include <regex.h>
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <glib/gi18n.h>
36 #include <gtkhtml/gtkhtml.h>
37 #include <gtkhtml/gtkhtml-stream.h>
38 #include <tny-list-iface.h>
39
40 #include <modest-tny-msg-actions.h>
41 #include "modest-msg-view.h"
42 #include "modest-tny-stream-gtkhtml.h"
43
44
45 /* 'private'/'protected' functions */
46 static void     modest_msg_view_class_init   (ModestMsgViewClass *klass);
47 static void     modest_msg_view_init         (ModestMsgView *obj);
48 static void     modest_msg_view_finalize     (GObject *obj);
49
50
51 static GSList*  get_url_matches (GString *txt);
52 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri,
53                                  ModestMsgView *msg_view);
54 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri,
55                                   GtkHTMLStream *stream,
56                                   ModestMsgView *msg_view);
57 static gboolean on_link_hover (GtkWidget *widget, const gchar *uri,
58                                ModestMsgView *msg_view);
59
60 /*
61  * we need these regexps to find URLs in plain text e-mails
62  */
63 typedef struct _UrlMatchPattern UrlMatchPattern;
64 struct _UrlMatchPattern {
65         gchar   *regex;
66         regex_t *preg;
67         gchar   *prefix;
68         
69 };
70
71 #define ATT_PREFIX "att:"
72
73 #define MAIL_VIEWER_URL_MATCH_PATTERNS  {                               \
74         { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
75           NULL, NULL },\
76         { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
77           NULL, "http://" },\
78         { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
79           NULL, "ftp://" },\
80         { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
81            NULL, NULL},                                             \
82         { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+",                    \
83           NULL, NULL},\
84         { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
85           NULL, "mailto:"}\
86         }
87
88
89 /* list my signals */
90 enum {
91         LINK_CLICKED_SIGNAL,
92         LINK_HOVER_SIGNAL,
93         ATTACHMENT_CLICKED_SIGNAL,
94         LAST_SIGNAL
95 };
96
97 typedef struct _ModestMsgViewPrivate ModestMsgViewPrivate;
98 struct _ModestMsgViewPrivate {
99         GtkWidget *gtkhtml;
100         const TnyMsgIface *msg;
101 };
102 #define MODEST_MSG_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
103                                                  MODEST_TYPE_MSG_VIEW, \
104                                                  ModestMsgViewPrivate))
105 /* globals */
106 static GtkContainerClass *parent_class = NULL;
107
108 /* uncomment the following if you have defined any signals */
109 static guint signals[LAST_SIGNAL] = {0};
110
111 GType
112 modest_msg_view_get_type (void)
113 {
114         static GType my_type = 0;
115         if (!my_type) {
116                 static const GTypeInfo my_info = {
117                         sizeof(ModestMsgViewClass),
118                         NULL,           /* base init */
119                         NULL,           /* base finalize */
120                         (GClassInitFunc) modest_msg_view_class_init,
121                         NULL,           /* class finalize */
122                         NULL,           /* class data */
123                         sizeof(ModestMsgView),
124                         1,              /* n_preallocs */
125                         (GInstanceInitFunc) modest_msg_view_init,
126                 };
127                 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
128                                                   "ModestMsgView",
129                                                   &my_info, 0);
130         }
131         return my_type;
132 }
133
134 static void
135 modest_msg_view_class_init (ModestMsgViewClass *klass)
136 {
137         GObjectClass *gobject_class;
138         gobject_class = (GObjectClass*) klass;
139
140         parent_class            = g_type_class_peek_parent (klass);
141         gobject_class->finalize = modest_msg_view_finalize;
142
143         g_type_class_add_private (gobject_class, sizeof(ModestMsgViewPrivate));
144
145                 
146         signals[LINK_CLICKED_SIGNAL] =
147                 g_signal_new ("link_clicked",
148                               G_TYPE_FROM_CLASS (gobject_class),
149                               G_SIGNAL_RUN_FIRST,
150                               G_STRUCT_OFFSET(ModestMsgViewClass, link_clicked),
151                               NULL, NULL,
152                               g_cclosure_marshal_VOID__STRING,
153                               G_TYPE_NONE, 1, G_TYPE_STRING);
154         
155         signals[ATTACHMENT_CLICKED_SIGNAL] =
156                 g_signal_new ("attachment_clicked",
157                               G_TYPE_FROM_CLASS (gobject_class),
158                               G_SIGNAL_RUN_FIRST,
159                               G_STRUCT_OFFSET(ModestMsgViewClass, attachment_clicked),
160                               NULL, NULL,
161                               g_cclosure_marshal_VOID__POINTER,
162                               G_TYPE_NONE, 1, G_TYPE_INT);
163         
164         signals[LINK_HOVER_SIGNAL] =
165                 g_signal_new ("link_hover",
166                               G_TYPE_FROM_CLASS (gobject_class),
167                               G_SIGNAL_RUN_FIRST,
168                               G_STRUCT_OFFSET(ModestMsgViewClass, link_hover),
169                               NULL, NULL,
170                               g_cclosure_marshal_VOID__STRING,
171                               G_TYPE_NONE, 1, G_TYPE_STRING);
172 }
173
174 static void
175 modest_msg_view_init (ModestMsgView *obj)
176 {
177         ModestMsgViewPrivate *priv;
178         
179         priv = MODEST_MSG_VIEW_GET_PRIVATE(obj);
180
181         priv->msg                     = NULL;
182         priv->gtkhtml                 = gtk_html_new();
183         
184         gtk_html_set_editable        (GTK_HTML(priv->gtkhtml), FALSE);
185         gtk_html_allow_selection     (GTK_HTML(priv->gtkhtml), TRUE);
186         gtk_html_set_caret_mode      (GTK_HTML(priv->gtkhtml), FALSE);
187         gtk_html_set_blocking        (GTK_HTML(priv->gtkhtml), FALSE);
188         gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
189
190         g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
191                           G_CALLBACK(on_link_clicked), obj);
192         
193         g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
194                           G_CALLBACK(on_url_requested), obj);
195
196         g_signal_connect (G_OBJECT(priv->gtkhtml), "on_url",
197                           G_CALLBACK(on_link_hover), obj);
198 }
199         
200
201 static void
202 modest_msg_view_finalize (GObject *obj)
203 {       
204         G_OBJECT_CLASS(parent_class)->finalize (obj);           
205 }
206
207
208 GtkWidget*
209 modest_msg_view_new (const TnyMsgIface *msg)
210 {
211         GObject *obj;
212         ModestMsgView* self;
213         ModestMsgViewPrivate *priv;
214         
215         obj  = G_OBJECT(g_object_new(MODEST_TYPE_MSG_VIEW, NULL));
216         self = MODEST_MSG_VIEW(obj);
217         priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
218
219         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
220                                        GTK_POLICY_AUTOMATIC,
221                                        GTK_POLICY_AUTOMATIC);
222
223         if (priv->gtkhtml) 
224                 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
225         
226         if (msg)
227                 modest_msg_view_set_message (self, msg);
228         
229         return GTK_WIDGET(self);
230 }
231
232
233 static gboolean
234 on_link_clicked (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
235 {
236         int index;
237
238         g_return_val_if_fail (msg_view, FALSE);
239         
240         /* is it an attachment? */
241         if (g_str_has_prefix(uri, ATT_PREFIX)) {
242
243                 index = atoi (uri + strlen(ATT_PREFIX));
244                 
245                 if (index == 0) {
246                         /* index is 1-based, so 0 indicates an error */
247                         g_printerr ("modest: invalid attachment id: %s\n", uri);
248                         return FALSE;
249                 }
250
251                 g_signal_emit (G_OBJECT(msg_view), signals[ATTACHMENT_CLICKED_SIGNAL],
252                                0, index);
253                 return FALSE;
254         }
255
256         g_signal_emit (G_OBJECT(msg_view), signals[LINK_CLICKED_SIGNAL],
257                        0, uri);
258
259         return FALSE;
260 }
261
262
263
264 static gboolean
265 on_link_hover (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
266 {
267         if (uri && g_str_has_prefix (uri, ATT_PREFIX))
268                 return FALSE;
269
270         g_signal_emit (G_OBJECT(msg_view), signals[LINK_HOVER_SIGNAL],
271                        0, uri);
272
273         return FALSE;
274 }
275
276
277
278 static TnyMsgMimePartIface *
279 find_cid_image (const TnyMsgIface *msg, const gchar *cid)
280 {
281         TnyMsgMimePartIface *part = NULL;
282         const TnyListIface *parts;
283         TnyIteratorIface *iter;
284         
285         g_return_val_if_fail (msg, NULL);
286         g_return_val_if_fail (cid, NULL);
287         
288         parts  = tny_msg_iface_get_parts ((TnyMsgIface*)msg); // FIXME: tinymail
289         iter   = tny_list_iface_create_iterator ((TnyListIface*)parts);
290         
291         while (!tny_iterator_iface_is_done(iter)) {
292                 const gchar *part_cid;
293                 part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current(iter));
294                 part_cid = tny_msg_mime_part_iface_get_content_id (part);
295
296                 if (part_cid && strcmp (cid, part_cid) == 0)
297                         break;
298
299                 part = NULL;
300                 tny_iterator_iface_next (iter);
301         }
302
303         g_object_unref (G_OBJECT(iter));        
304         return part;
305 }
306
307
308 static gboolean
309 on_url_requested (GtkWidget *widget, const gchar *uri,
310                   GtkHTMLStream *stream,
311                   ModestMsgView *msg_view)
312 {
313         ModestMsgViewPrivate *priv;
314         priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
315         
316         if (g_str_has_prefix (uri, "cid:")) {
317                 /* +4 ==> skip "cid:" */
318                 const TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
319                 if (!part) {
320                         g_printerr ("modest: '%s' not found\n", uri + 4);
321                         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
322                 } else {
323                         TnyStreamIface *tny_stream =
324                                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
325                         // FIXME: tinymail
326                         tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)part,
327                                                                   tny_stream);
328                         gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
329                 }
330         }
331
332         return TRUE;
333 }
334
335
336 typedef struct  {
337         guint offset;
338         guint len;
339         const gchar* prefix;
340 } url_match_t;
341
342
343
344 /* render the attachments as hyperlinks in html */
345 static gchar*
346 attachments_as_html (ModestMsgView *self, const TnyMsgIface *msg)
347 {
348         ModestMsgViewPrivate *priv;
349         GString *appendix;
350         const TnyListIface *parts;
351         TnyIteratorIface *iter;
352         gchar *html;
353         int index = 0;
354         
355         if (!msg)
356                 return NULL;
357
358         priv  = MODEST_MSG_VIEW_GET_PRIVATE (self);
359         parts = tny_msg_iface_get_parts ((TnyMsgIface*)msg);
360         // FIXME: tinymail
361         iter  = tny_list_iface_create_iterator ((TnyListIface*)parts);
362
363         appendix= g_string_new ("");
364         
365         while (!tny_iterator_iface_is_done(iter)) {
366                 TnyMsgMimePartIface *part;
367
368                 ++index; /* attachment numbers are 1-based */
369                 
370                 part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current (iter));
371
372                 if (tny_msg_mime_part_iface_is_attachment (part)) {
373
374                         const gchar *filename = tny_msg_mime_part_iface_get_filename(part);
375                         if (!filename)
376                                 filename = _("attachment");
377
378                         g_string_append_printf (appendix, "<a href=\"%s%d\">%s</a> \n",
379                                                 ATT_PREFIX, index, filename);                    
380                 }
381                 tny_iterator_iface_next (iter);
382         }
383         g_object_unref (G_OBJECT(iter));
384         
385         if (appendix->len == 0) 
386                 return g_string_free (appendix, TRUE);
387
388         html = g_strdup_printf ("<strong>%s:</strong> %s\n<hr>",
389                                 _("Attachments"), appendix->str);                        
390         g_string_free (appendix, TRUE);
391         
392         return html;
393 }
394
395
396
397 static void
398 hyperlinkify_plain_text (GString *txt)
399 {
400         GSList *cursor;
401         GSList *match_list = get_url_matches (txt);
402
403         /* we will work backwards, so the offsets stay valid */
404         for (cursor = match_list; cursor; cursor = cursor->next) {
405
406                 url_match_t *match = (url_match_t*) cursor->data;
407                 gchar *url  = g_strndup (txt->str + match->offset, match->len);
408                 gchar *repl = NULL; /* replacement  */
409
410                 /* the prefix is NULL: use the one that is already there */
411                 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
412                                         match->prefix ? match->prefix : "", url, url);
413
414                 /* replace the old thing with our hyperlink
415                  * replacement thing */
416                 g_string_erase  (txt, match->offset, match->len);
417                 g_string_insert (txt, match->offset, repl);
418                 
419                 g_free (url);
420                 g_free (repl);
421
422                 g_free (cursor->data);  
423         }
424         
425         g_slist_free (match_list);
426 }
427
428
429
430 static gchar *
431 convert_to_html (const gchar *data)
432 {
433         int              i;
434         gboolean         first_space = TRUE;
435         GString         *html;      
436         gsize           len;
437
438         if (!data)
439                 return NULL;
440
441         len = strlen (data);
442         html = g_string_sized_new (len + 100);  /* just a  guess... */
443         
444         g_string_append_printf (html,
445                                 "<html>"
446                                 "<head>"
447                                 "<meta http-equiv=\"content-type\""
448                                 " content=\"text/html; charset=utf8\">"
449                                 "</head>"
450                                 "<body><tt>");
451         
452         /* replace with special html chars where needed*/
453         for (i = 0; i != len; ++i)  {
454                 char    kar = data[i]; 
455                 switch (kar) {
456                         
457                 case 0:  break; /* ignore embedded \0s */       
458                 case '<' : g_string_append   (html, "&lt;"); break;
459                 case '>' : g_string_append   (html, "&gt;"); break;
460                 case '&' : g_string_append   (html, "&quot;"); break;
461                 case '\n': g_string_append   (html, "<br>\n"); break;
462                 default:
463                         if (kar == ' ') {
464                                 g_string_append (html, first_space ? " " : "&nbsp;");
465                                 first_space = FALSE;
466                         } else  if (kar == '\t')
467                                 g_string_append (html, "&nbsp; &nbsp;&nbsp;");
468                         else {
469                                 int charnum = 0;
470                                 first_space = TRUE;
471                                 /* optimization trick: accumulate 'normal' chars, then copy */
472                                 do {
473                                         kar = data [++charnum + i];
474                                         
475                                 } while ((i + charnum < len) &&
476                                          (kar > '>' || (kar != '<' && kar != '>'
477                                                         && kar != '&' && kar !=  ' '
478                                                         && kar != '\n' && kar != '\t')));
479                                 g_string_append_len (html, &data[i], charnum);
480                                 i += (charnum  - 1);
481                         }
482                 }
483         }
484         
485         g_string_append (html, "</tt></body></html>");
486         hyperlinkify_plain_text (html);
487
488         return g_string_free (html, FALSE);
489 }
490
491
492
493
494 static gint 
495 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
496 {
497         return match2->offset - match1->offset;
498 }
499
500
501
502 /*
503  * check if the match is inside an existing match... */
504 static void
505 chk_partial_match (const url_match_t *match, int* offset)
506 {
507         if (*offset >= match->offset && *offset < match->offset + match->len)
508                 *offset = -1;
509 }
510
511 static GSList*
512 get_url_matches (GString *txt)
513 {
514         regmatch_t rm;
515         int rv, i, offset = 0;
516         GSList *match_list = NULL;
517
518         static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
519         const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
520
521         /* initalize the regexps */
522         for (i = 0; i != pattern_num; ++i) {
523                 patterns[i].preg = g_new0 (regex_t,1);
524                 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
525                                   REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
526         }
527         /* find all the matches */
528         for (i = 0; i != pattern_num; ++i) {
529                 offset     = 0; 
530                 while (1) {
531                         int test_offset;
532                         if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
533                                 g_assert (rv == REG_NOMATCH); /* this should not happen */
534                                 break; /* try next regexp */ 
535                         }
536                         if (rm.rm_so == -1)
537                                 break;
538
539                         /* FIXME: optimize this */
540                         /* to avoid partial matches on something that was already found... */
541                         /* check_partial_match will put -1 in the data ptr if that is the case */
542                         test_offset = offset + rm.rm_so;
543                         g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
544                         
545                         /* make a list of our matches (<offset, len, prefix> tupels)*/
546                         if (test_offset != -1) {
547                                 url_match_t *match = g_new (url_match_t,1);
548                                 match->offset = offset + rm.rm_so;
549                                 match->len    = rm.rm_eo - rm.rm_so;
550                                 match->prefix = patterns[i].prefix;
551                                 match_list = g_slist_prepend (match_list, match);
552                         }
553                         offset += rm.rm_eo;
554                 }
555         }
556
557         for (i = 0; i != pattern_num; ++i) {
558                 regfree (patterns[i].preg);
559                 g_free  (patterns[i].preg);
560         } /* don't free patterns itself -- it's static */
561         
562         /* now sort the list, so the matches are in reverse order of occurence.
563          * that way, we can do the replacements starting from the end, so we don't need
564          * to recalculate the offsets
565          */
566         match_list = g_slist_sort (match_list,
567                                    (GCompareFunc)cmp_offsets_reverse); 
568         return match_list;      
569 }
570
571
572
573 static gboolean
574 set_html_message (ModestMsgView *self, const TnyMsgMimePartIface *tny_body,
575                   const TnyMsgIface *msg)
576 {
577         gchar *html_attachments;
578         TnyStreamIface *gtkhtml_stream; 
579         ModestMsgViewPrivate *priv;
580         
581         g_return_val_if_fail (self, FALSE);
582         g_return_val_if_fail (tny_body, FALSE);
583         
584         priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
585
586         gtkhtml_stream =
587                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
588                                  (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
589         
590         tny_stream_iface_reset (gtkhtml_stream);
591         
592         html_attachments = attachments_as_html(self, msg);
593         if (html_attachments) {
594                 tny_stream_iface_write (gtkhtml_stream, html_attachments,
595                                         strlen(html_attachments));
596                 tny_stream_iface_reset (gtkhtml_stream);
597                 g_free (html_attachments);
598         }
599
600         // FIXME: tinymail
601         tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)tny_body,
602                                                   gtkhtml_stream);
603
604         g_object_unref (G_OBJECT(gtkhtml_stream));
605         
606         return TRUE;
607 }
608
609
610 /* this is a hack --> we use the tny_text_buffer_stream to
611  * get the message text, then write to gtkhtml 'by hand' */
612 static gboolean
613 set_text_message (ModestMsgView *self, const TnyMsgMimePartIface *tny_body,
614                   const TnyMsgIface *msg)
615 {
616         GtkTextBuffer *buf;
617         GtkTextIter begin, end;
618         TnyStreamIface* txt_stream, *gtkhtml_stream;
619         gchar *txt, *html_attachments;
620         ModestMsgViewPrivate *priv;
621                 
622         g_return_val_if_fail (self, FALSE);
623         g_return_val_if_fail (tny_body, FALSE);
624
625         priv           = MODEST_MSG_VIEW_GET_PRIVATE(self);
626         
627         buf            = gtk_text_buffer_new (NULL);
628         txt_stream     = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
629                 
630         tny_stream_iface_reset (txt_stream);
631         
632         gtkhtml_stream =
633                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
634                                  (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
635
636         html_attachments = attachments_as_html(self, msg);
637         if (html_attachments) {
638                 tny_stream_iface_write (gtkhtml_stream, html_attachments,
639                                         strlen(html_attachments));
640                 tny_stream_iface_reset (gtkhtml_stream);
641                 g_free (html_attachments);
642         }
643
644         // FIXME: tinymail
645         tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)tny_body,
646                                                   txt_stream);
647         tny_stream_iface_reset (txt_stream);            
648         
649         gtk_text_buffer_get_bounds (buf, &begin, &end);
650         txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
651         if (txt) {
652                 gchar *html = convert_to_html (txt);
653                 tny_stream_iface_write (gtkhtml_stream, html, strlen(html));
654                 tny_stream_iface_reset (gtkhtml_stream);
655                 g_free (txt);
656                 g_free (html);
657         }
658         
659         g_object_unref (G_OBJECT(gtkhtml_stream));
660         g_object_unref (G_OBJECT(txt_stream));
661         g_object_unref (G_OBJECT(buf));
662
663         return TRUE;
664 }
665
666
667 static gboolean
668 set_empty_message (ModestMsgView *self)
669 {
670         ModestMsgViewPrivate *priv;
671         
672         g_return_val_if_fail (self, FALSE);
673         priv           = MODEST_MSG_VIEW_GET_PRIVATE(self);
674
675         gtk_html_load_from_string (GTK_HTML(priv->gtkhtml),
676                                    "", 1);
677         
678         return TRUE;
679 }
680
681
682 gchar *
683 modest_msg_view_get_selected_text (ModestMsgView *self)
684 {
685         ModestMsgViewPrivate *priv;
686         gchar *sel;
687         GtkWidget *html;
688         int len;
689         GtkClipboard *clip;
690
691         g_return_val_if_fail (self, NULL);
692         priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
693         html = priv->gtkhtml;
694         
695         /* I'm sure there is a better way to check for selected text */
696         sel = gtk_html_get_selection_html(GTK_HTML(html), &len);
697         if (!sel)
698                 return NULL;
699         
700         g_free(sel);
701         
702         clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
703         return gtk_clipboard_wait_for_text(clip);
704 }
705
706
707 void
708 modest_msg_view_set_message (ModestMsgView *self, const TnyMsgIface *msg)
709 {
710         TnyMsgMimePartIface *body;
711         ModestMsgViewPrivate *priv;
712
713         g_return_if_fail (self);
714         
715         priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
716
717         priv->msg = msg;
718         
719         if (!msg) {
720                 set_empty_message (self);
721                 return;
722         }
723                 
724         body = modest_tny_msg_actions_find_body_part (msg, TRUE);
725         if (body) {
726                 if (tny_msg_mime_part_iface_content_type_is (body, "text/html"))
727                         set_html_message (self, body, msg);
728                 else
729                         set_text_message (self, body, msg);
730                 return;
731         } else 
732                 set_empty_message (self);
733 }