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