* 2 warnings fixed
[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         
183         if (g_str_has_prefix(uri, "attachment:")) {
184                 /* save or open attachment */
185                 return TRUE;
186         }
187         g_message ("link clicked: %s", uri); /* FIXME */
188         return FALSE;
189         
190 }
191
192
193 static TnyMsgMimePartIface *
194 find_cid_image (TnyMsgIface *msg, const gchar *cid)
195 {
196         TnyMsgMimePartIface *part = NULL;
197         GList *parts;
198
199         g_return_val_if_fail (msg, NULL);
200         g_return_val_if_fail (cid, NULL);
201         
202         parts  = (GList*) tny_msg_iface_get_parts (msg);
203         while (parts && !part) {
204                 const gchar *part_cid;
205                 part = TNY_MSG_MIME_PART_IFACE(parts->data);
206                 part_cid = tny_msg_mime_part_iface_get_content_id (part);
207                 printf("CMP:%s:%s\n", cid, part_cid);
208                 if (part_cid && strcmp (cid, part_cid) == 0)
209                         return part; /* we found it! */
210                 
211                 part = NULL;
212                 parts = parts->next;
213         }
214         
215         return part;
216 }
217
218
219 static TnyMsgMimePartIface *
220 find_attachment_by_filename (TnyMsgIface *msg, const gchar *fn)
221 {
222         TnyMsgMimePartIface *part = NULL;
223         GList *parts;
224         gchar *dummy;
225         gint pos;
226
227         g_return_val_if_fail (msg, NULL);
228         g_return_val_if_fail (fn, NULL);
229         
230         parts  = (GList*) tny_msg_iface_get_parts (msg);
231         pos = virtual_filename_get_pos(fn);
232         
233         g_return_val_if_fail(((pos >= 0) && (pos < g_list_length(parts))), NULL);
234         
235         part = g_list_nth_data(parts, pos);
236         
237         dummy = construct_virtual_filename_from_mime_part(part, pos);
238         if (strcmp(dummy, fn) == 0) {
239                 g_free(dummy);
240                 return part;
241         } else {
242                 g_free(dummy);
243                 return NULL;
244         }
245 }
246
247
248 static gboolean
249 on_url_requested (GtkWidget *widget, const gchar *uri,
250                   GtkHTMLStream *stream,
251                   ModestTnyMsgView *msg_view)
252 {
253         
254         ModestTnyMsgViewPrivate *priv;
255         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (msg_view);
256
257         g_message ("url requested: %s", uri);
258         
259         if (g_str_has_prefix (uri, "cid:")) {
260                 /* +4 ==> skip "cid:" */
261                 
262                 TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
263                 if (!part) {
264                         g_message ("%s not found", uri + 4);
265                         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
266                 } else {
267                         TnyStreamIface *tny_stream =
268                                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
269                         tny_msg_mime_part_iface_decode_to_stream (part,tny_stream);
270                         gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
271                 }
272         } else if (g_str_has_prefix (uri, "Attachment:")) {
273                 TnyMsgMimePartIface *part = find_attachment_by_filename (priv->msg, uri);
274                 if (!part) {
275                         g_message ("%s not found", uri);
276                         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
277                 } else {
278                         TnyStreamIface *tny_stream =
279                                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
280                         tny_msg_mime_part_iface_decode_to_stream (part,tny_stream);
281                         gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
282                 }
283         }
284         return TRUE;
285 }
286
287
288
289
290 typedef struct  {
291         guint offset;
292         guint len;
293         const gchar* prefix;
294 } url_match_t;
295
296
297
298 static gchar *
299 construct_virtual_filename(const gchar *filename, const gint position, const gchar *id, const gboolean active)
300 {
301         GString *s;
302         g_return_val_if_fail((position >= 0), "AttachmentInvalid");
303
304         s = g_string_new("");
305         if (active)
306                 g_string_append(s, "Attachment:");
307         else
308                 g_string_append(s, "attachment:");
309         g_string_append_printf(s, "%d:", position);
310         if (id)
311                 g_string_append(s, id);
312         g_string_append_c(s, ':');
313         if (filename)
314                 g_string_append(s, filename);
315         g_string_append_c(s, ':');
316         return g_string_free(s, FALSE);
317 }
318
319
320 static gchar *
321 construct_virtual_filename_from_mime_part(TnyMsgMimePartIface *msg, const gint position)
322 {
323         const gchar *id, *filename;
324         const gboolean active = TRUE;
325         
326         filename = tny_msg_mime_part_iface_get_filename(
327                                                                         TNY_MSG_MIME_PART_IFACE(msg));
328         if (!filename)
329                 filename = "[unknown]";
330         id = tny_msg_mime_part_iface_get_content_id(
331                                                                         TNY_MSG_MIME_PART_IFACE(msg));
332         
333         return construct_virtual_filename(filename, position, id, active);
334 }
335
336 const gchar *
337 get_next_token(const gchar *s, gint *len)
338 {
339         gchar *i1, *i2;
340         i1 = (char *) s;
341         i2 = (char *) s;
342         
343         while (i2[0]) {
344                 if (i2[0] == ':')
345                         break;
346                 i2++;
347         }
348         if (!i2[0])
349                 return NULL;
350         *len = i2 - i1;
351         return ++i2;
352 }
353
354 /* maybe I should use libregexp */
355 gint
356 virtual_filename_get_pos(const gchar *filename)
357 {
358         const gchar *i1, *i2;
359         gint len, pos;
360         GString *dummy;
361         
362         i1 = filename;
363         i2 = filename;
364         
365         /* check prefix */
366         i2 = get_next_token(i2, &len);
367         if (strncmp(i1, "Attachment", len) != 0)
368                 return -1;
369         i1 = i2;
370                 
371         /* get position */
372         i2 = get_next_token(i2, &len);
373         if (i2 == NULL)
374                 return -1;
375         dummy = g_string_new_len(i1, len);
376         pos = atoi(dummy->str);
377         g_string_free(dummy, FALSE);
378         return pos;
379 }       
380
381
382 static gchar *
383 attachments_as_html(ModestTnyMsgView *self, TnyMsgIface *msg)
384 {
385         ModestTnyMsgViewPrivate *priv;
386         gboolean attachments_found = FALSE;
387         GString *appendix;
388         const GList *attachment_list, *attachment;
389         const gchar *content_type, *filename, *id;
390         gchar *virtual_filename;
391         gboolean show_attachments_inline;
392         
393         if (!msg)
394                 return g_malloc0(1);
395         
396         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
397         
398         /* CLEANUP: starting a new HTML may be unsupported */
399         appendix = g_string_new("<HTML><BODY>\n<hr><h5>Attachments:</h5>\n");
400         
401         attachment_list = tny_msg_iface_get_parts(msg);
402         attachment = attachment_list;
403         while (attachment) {
404                 filename = "";
405                 content_type = tny_msg_mime_part_iface_get_content_type(
406                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data));
407                 g_return_val_if_fail(content_type, NULL);
408                 if (      tny_msg_mime_part_iface_content_type_is(
409                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data),
410                                                                                 "image/jpeg")
411                            || tny_msg_mime_part_iface_content_type_is(
412                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data),
413                                                                                 "image/gif")) {
414                         filename = tny_msg_mime_part_iface_get_filename(
415                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data));
416                         if (!filename)
417                                 filename = "[unknown]";
418                         else
419                                 attachments_found = TRUE;
420                         id = tny_msg_mime_part_iface_get_content_id(
421                                                                                 TNY_MSG_MIME_PART_IFACE(attachment->data));
422                         show_attachments_inline = modest_conf_get_bool(priv->conf, MODEST_CONF_MSG_VIEW_SHOW_ATTACHMENTS_INLINE, NULL);
423                         virtual_filename = construct_virtual_filename(filename,
424                                          g_list_position((GList *)attachment_list, (GList *) attachment),
425                                          id, show_attachments_inline);
426                         printf("VF:%s\n", virtual_filename);
427                         if (show_attachments_inline) {
428                                 g_string_append_printf(appendix, "<IMG src=\"%s\">\n<BR>", virtual_filename);
429                         }
430                         g_string_append_printf(appendix, "<A href=\"attachment:%s\">%s</A>: %s<BR>\n", filename, filename, content_type);
431                         g_free(virtual_filename);
432                 }
433                 attachment = attachment->next;
434         }
435         g_string_append(appendix, "</BODY></HTML>");
436         if (!attachments_found)
437                 g_string_assign(appendix, "");
438         return g_string_free(appendix, FALSE);
439 }
440
441 static void
442 hyperlinkify_plain_text (GString *txt)
443 {
444         GSList *cursor;
445         GSList *match_list = get_url_matches (txt);
446
447         /* we will work backwards, so the offsets stay valid */
448         for (cursor = match_list; cursor; cursor = cursor->next) {
449
450                 url_match_t *match = (url_match_t*) cursor->data;
451                 gchar *url  = g_strndup (txt->str + match->offset, match->len);
452                 gchar *repl = NULL; /* replacement  */
453
454                 /* the prefix is NULL: use the one that is already there */
455                 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
456                                         match->prefix ? match->prefix : "", url, url);
457
458                 /* replace the old thing with our hyperlink
459                  * replacement thing */
460                 g_string_erase  (txt, match->offset, match->len);
461                 g_string_insert (txt, match->offset, repl);
462                 
463                 g_free (url);
464                 g_free (repl);
465
466                 g_free (cursor->data);  
467         }
468         
469         g_slist_free (match_list);
470 }
471
472
473
474 static gchar *
475 convert_to_html (const gchar *data)
476 {
477         int              i;
478         gboolean         first_space = TRUE;
479         GString         *html;      
480         gsize           len;
481
482         if (!data)
483                 return NULL;
484
485         len = strlen (data);
486         html = g_string_sized_new (len + 100);  /* just a  guess... */
487         
488         g_string_append_printf (html,
489                                 "<html>"
490                                 "<head>"
491                                 "<meta http-equiv=\"content-type\""
492                                 " content=\"text/html; charset=utf8\">"
493                                 "</head>"
494                                 "<body><tt>");
495         
496         /* replace with special html chars where needed*/
497         for (i = 0; i != len; ++i)  {
498                 char    kar = data[i]; 
499                 switch (kar) {
500                         
501                 case 0:  break; /* ignore embedded \0s */       
502                 case '<' : g_string_append   (html, "&lt;"); break;
503                 case '>' : g_string_append   (html, "&gt;"); break;
504                 case '&' : g_string_append   (html, "&quot;"); break;
505                 case '\n': g_string_append   (html, "<br>\n"); break;
506                 default:
507                         if (kar == ' ') {
508                                 g_string_append (html, first_space ? " " : "&nbsp;");
509                                 first_space = FALSE;
510                         } else  if (kar == '\t')
511                                 g_string_append (html, "&nbsp; &nbsp;&nbsp;");
512                         else {
513                                 int charnum = 0;
514                                 first_space = TRUE;
515                                 /* optimization trick: accumulate 'normal' chars, then copy */
516                                 do {
517                                         kar = data [++charnum + i];
518                                         
519                                 } while ((i + charnum < len) &&
520                                          (kar > '>' || (kar != '<' && kar != '>'
521                                                         && kar != '&' && kar !=  ' '
522                                                         && kar != '\n' && kar != '\t')));
523                                 g_string_append_len (html, &data[i], charnum);
524                                 i += (charnum  - 1);
525                         }
526                 }
527         }
528         
529         g_string_append (html, "</tt></body></html>");
530         hyperlinkify_plain_text (html);
531
532         return g_string_free (html, FALSE);
533 }
534
535
536
537
538 static gint 
539 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
540 {
541         return match2->offset - match1->offset;
542 }
543
544
545
546 /*
547  * check if the match is inside an existing match... */
548 static void
549 chk_partial_match (const url_match_t *match, int* offset)
550 {
551         if (*offset >= match->offset && *offset < match->offset + match->len)
552                 *offset = -1;
553 }
554
555 static GSList*
556 get_url_matches (GString *txt)
557 {
558         regmatch_t rm;
559         int rv, i, offset = 0;
560         GSList *match_list = NULL;
561
562         static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
563         const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
564
565         /* initalize the regexps */
566         for (i = 0; i != pattern_num; ++i) {
567                 patterns[i].preg = g_new0 (regex_t,1);
568                 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
569                                   REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
570         }
571         /* find all the matches */
572         for (i = 0; i != pattern_num; ++i) {
573                 offset     = 0; 
574                 while (1) {
575                         int test_offset;
576                         if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
577                                 g_assert (rv == REG_NOMATCH); /* this should not happen */
578                                 break; /* try next regexp */ 
579                         }
580                         if (rm.rm_so == -1)
581                                 break;
582
583                         /* FIXME: optimize this */
584                         /* to avoid partial matches on something that was already found... */
585                         /* check_partial_match will put -1 in the data ptr if that is the case */
586                         test_offset = offset + rm.rm_so;
587                         g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
588                         
589                         /* make a list of our matches (<offset, len, prefix> tupels)*/
590                         if (test_offset != -1) {
591                                 url_match_t *match = g_new (url_match_t,1);
592                                 match->offset = offset + rm.rm_so;
593                                 match->len    = rm.rm_eo - rm.rm_so;
594                                 match->prefix = patterns[i].prefix;
595                                 match_list = g_slist_prepend (match_list, match);
596                         }
597                         offset += rm.rm_eo;
598                 }
599         }
600
601         for (i = 0; i != pattern_num; ++i) {
602                 regfree (patterns[i].preg);
603                 g_free  (patterns[i].preg);
604         } /* don't free patterns itself -- it's static */
605         
606         /* now sort the list, so the matches are in reverse order of occurence.
607          * that way, we can do the replacements starting from the end, so we don't need
608          * to recalculate the offsets
609          */
610         match_list = g_slist_sort (match_list,
611                                    (GCompareFunc)cmp_offsets_reverse); 
612         return match_list;      
613 }
614
615 static gboolean
616 fill_gtkhtml_with_txt (ModestTnyMsgView *self, GtkHTML* gtkhtml, const gchar* txt, TnyMsgIface *msg)
617 {
618         GString *html;
619         gchar *html_attachments;
620         
621         g_return_val_if_fail (gtkhtml, FALSE);
622         g_return_val_if_fail (txt, FALSE);
623
624         html = g_string_new(convert_to_html (txt));
625         html_attachments = attachments_as_html(self, msg);
626         g_string_append(html, html_attachments);
627
628         gtk_html_load_from_string (gtkhtml, html->str, html->len);
629         g_string_free (html, TRUE);
630         g_free(html_attachments);
631
632         return TRUE;
633 }
634
635
636
637 static gboolean
638 set_html_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body, TnyMsgIface *msg)
639 {
640         gchar *html_attachments;
641         TnyStreamIface *gtkhtml_stream; 
642         ModestTnyMsgViewPrivate *priv;
643         
644         g_return_val_if_fail (self, FALSE);
645         g_return_val_if_fail (tny_body, FALSE);
646         
647         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
648
649         gtkhtml_stream =
650                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
651                                  (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
652         
653         tny_stream_iface_reset (gtkhtml_stream);
654         tny_msg_mime_part_iface_decode_to_stream (tny_body, gtkhtml_stream);
655         html_attachments = attachments_as_html(self, msg);
656         /* is this clean? */
657         gtkhtml_write(gtkhtml_stream, html_attachments, strlen(html_attachments));
658         tny_stream_iface_reset (gtkhtml_stream);
659
660         g_object_unref (G_OBJECT(gtkhtml_stream));
661         g_free (html_attachments);
662         
663         return TRUE;
664 }
665
666
667 /* this is a hack --> we use the tny_text_buffer_stream to
668  * get the message text, then write to gtkhtml 'by hand' */
669 static gboolean
670 set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body, TnyMsgIface *msg)
671 {
672         GtkTextBuffer *buf;
673         GtkTextIter begin, end;
674         TnyStreamIface* txt_stream;
675         gchar *txt;
676         ModestTnyMsgViewPrivate *priv;
677                 
678         g_return_val_if_fail (self, FALSE);
679         g_return_val_if_fail (tny_body, FALSE);
680
681         priv           = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
682         
683         buf            = gtk_text_buffer_new (NULL);
684         txt_stream     = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
685                 
686         tny_stream_iface_reset (txt_stream);
687         tny_msg_mime_part_iface_decode_to_stream (tny_body, txt_stream);
688         tny_stream_iface_reset (txt_stream);            
689         
690         gtk_text_buffer_get_bounds (buf, &begin, &end);
691         txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
692         
693         fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml), txt, msg);
694
695         g_object_unref (G_OBJECT(txt_stream));
696         g_object_unref (G_OBJECT(buf));
697
698         g_free (txt);
699         return TRUE;
700 }
701
702 gchar *
703 modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self)
704 {
705         ModestTnyMsgViewPrivate *priv;
706         gchar *sel;
707         GtkWidget *html;
708         int len;
709         GtkClipboard *clip;
710
711         g_return_val_if_fail (self, NULL);
712         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
713         html = priv->gtkhtml;
714         
715         /* I'm sure there is a better way to check for selected text */
716         sel = gtk_html_get_selection_html(GTK_HTML(html), &len);
717         if (!sel)
718                 return NULL;
719         
720         g_free(sel);
721         
722         clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
723         return gtk_clipboard_wait_for_text(clip);
724 }
725
726 void
727 modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg)
728 {
729         TnyMsgMimePartIface *body;
730         ModestTnyMsgViewPrivate *priv;
731
732         g_return_if_fail (self);
733         
734         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
735
736         priv->msg = msg;
737         
738         fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml), "", msg);
739
740         if (!msg) 
741                 return;
742         
743         body = modest_tny_msg_actions_find_body_part (msg, "text/html");
744         if (body) {
745                 set_html_message (self, body, msg);
746                 return;
747         }
748         
749         body = modest_tny_msg_actions_find_body_part (msg, "text/plain");
750         if (body) {
751                 set_text_message (self, body, msg);
752                 return;
753         }
754
755         /* hmmmmm */
756         fill_gtkhtml_with_txt (self, GTK_HTML(priv->gtkhtml),
757                                 _("Unsupported message type"), msg);
758 }
759
760 void
761 modest_tny_msg_view_redraw (ModestTnyMsgView *self)
762 {
763         ModestTnyMsgViewPrivate *priv;
764
765         g_return_if_fail (self);
766         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
767         modest_tny_msg_view_set_message(self, priv->msg);
768 }