* (quote) utf8 actually works now
[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 <tny-text-buffer-stream.h>
8 #include <string.h>
9 #include <regex.h>
10 #include <ctype.h>
11 #include <glib/gi18n.h>
12
13 /* 'private'/'protected' functions */
14 static void     modest_tny_msg_view_class_init   (ModestTnyMsgViewClass *klass);
15 static void     modest_tny_msg_view_init         (ModestTnyMsgView *obj);
16 static void     modest_tny_msg_view_finalize     (GObject *obj);
17
18
19 static GSList*  get_url_matches (GString *txt);
20 static gboolean fill_gtkhtml_with_txt (GtkHTML* gtkhtml, const gchar* txt);
21
22 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri,
23                                  ModestTnyMsgView *msg_view);
24 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri,
25                                   GtkHTMLStream *stream,
26                                   ModestTnyMsgView *msg_view);
27
28
29 /*
30  * we need these regexps to find URLs in plain text e-mails
31  */
32 typedef struct _UrlMatchPattern UrlMatchPattern;
33 struct _UrlMatchPattern {
34         gchar   *regex;
35         regex_t *preg;
36         gchar   *prefix;
37         
38 };
39 #define MAIL_VIEWER_URL_MATCH_PATTERNS  {\
40         { "(file|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
41           NULL, NULL },\
42         { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
43           NULL, "http://" },\
44         { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
45           NULL, "ftp://" },\
46         { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
47            NULL, NULL},                                             \
48         { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+",                    \
49           NULL, NULL},\
50         { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
51           NULL, "mailto:"}\
52         }
53
54
55 /* list my signals */
56 enum {
57         /* MY_SIGNAL_1, */
58         /* MY_SIGNAL_2, */
59         LAST_SIGNAL
60 };
61
62 typedef struct _ModestTnyMsgViewPrivate ModestTnyMsgViewPrivate;
63 struct _ModestTnyMsgViewPrivate {
64         GtkWidget *gtkhtml;
65         TnyMsgIface *msg;
66 };
67 #define MODEST_TNY_MSG_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
68                                                  MODEST_TYPE_TNY_MSG_VIEW, \
69                                                  ModestTnyMsgViewPrivate))
70 /* globals */
71 static GtkContainerClass *parent_class = NULL;
72
73 /* uncomment the following if you have defined any signals */
74 /* static guint signals[LAST_SIGNAL] = {0}; */
75
76 GType
77 modest_tny_msg_view_get_type (void)
78 {
79         static GType my_type = 0;
80         if (!my_type) {
81                 static const GTypeInfo my_info = {
82                         sizeof(ModestTnyMsgViewClass),
83                         NULL,           /* base init */
84                         NULL,           /* base finalize */
85                         (GClassInitFunc) modest_tny_msg_view_class_init,
86                         NULL,           /* class finalize */
87                         NULL,           /* class data */
88                         sizeof(ModestTnyMsgView),
89                         1,              /* n_preallocs */
90                         (GInstanceInitFunc) modest_tny_msg_view_init,
91                 };
92                 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
93                                                   "ModestTnyMsgView",
94                                                   &my_info, 0);
95         }
96         return my_type;
97 }
98
99 static void
100 modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass)
101 {
102         GObjectClass *gobject_class;
103         gobject_class = (GObjectClass*) klass;
104
105         parent_class            = g_type_class_peek_parent (klass);
106         gobject_class->finalize = modest_tny_msg_view_finalize;
107
108         g_type_class_add_private (gobject_class, sizeof(ModestTnyMsgViewPrivate));
109 }
110
111 static void
112 modest_tny_msg_view_init (ModestTnyMsgView *obj)
113 {
114         ModestTnyMsgViewPrivate *priv;
115         
116         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
117
118         priv->msg = NULL;
119         
120         priv->gtkhtml = gtk_html_new();
121         gtk_html_set_editable        (GTK_HTML(priv->gtkhtml), FALSE);
122         gtk_html_allow_selection     (GTK_HTML(priv->gtkhtml), TRUE);
123         gtk_html_set_caret_mode      (GTK_HTML(priv->gtkhtml), FALSE);
124         gtk_html_set_blocking        (GTK_HTML(priv->gtkhtml), FALSE);
125         gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
126         
127         g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
128                           G_CALLBACK(on_link_clicked), obj);
129         
130         g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
131                           G_CALLBACK(on_url_requested), obj);
132 }
133         
134
135 static void
136 modest_tny_msg_view_finalize (GObject *obj)
137 {       
138         ModestTnyMsgViewPrivate *priv;
139         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
140
141         if (priv->gtkhtml)
142                 g_object_unref (G_OBJECT(priv->gtkhtml));
143 }
144
145 GtkWidget*
146 modest_tny_msg_view_new (TnyMsgIface *msg)
147 {
148         GObject *obj;
149         ModestTnyMsgView* self;
150         ModestTnyMsgViewPrivate *priv;
151         
152         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_MSG_VIEW, NULL));
153         self = MODEST_TNY_MSG_VIEW(obj);
154         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
155
156         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
157                                        GTK_POLICY_AUTOMATIC,
158                                        GTK_POLICY_AUTOMATIC);
159
160         if (priv->gtkhtml) 
161                 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);  
162         
163         if (msg)
164                 modest_tny_msg_view_set_message (self, msg);
165
166         return GTK_WIDGET(self);
167 }
168
169
170
171 static gboolean
172 on_link_clicked (GtkWidget *widget, const gchar *uri,
173                                  ModestTnyMsgView *msg_view)
174 {
175         g_message ("link clicked: %s", uri); /* FIXME */
176 }
177
178
179
180 static TnyMsgMimePartIface *
181 find_cid_image (TnyMsgIface *msg, const gchar *cid)
182 {
183         TnyMsgMimePartIface *part = NULL;
184         GList *parts;
185
186         g_return_val_if_fail (msg, NULL);
187         g_return_val_if_fail (cid, NULL);
188         
189         parts  = (GList*) tny_msg_iface_get_parts (msg);
190         while (parts && !part) {
191                 const gchar *part_cid;
192                 part = TNY_MSG_MIME_PART_IFACE(parts->data);
193                 part_cid = tny_msg_mime_part_iface_get_content_id (part);
194                 if (part_cid && strcmp (cid, part_cid) == 0)
195                         return part; /* we found it! */
196                 
197                 part = NULL;
198                 parts = parts->next;
199         }
200         
201         return part;
202 }
203
204
205 static gboolean
206 on_url_requested (GtkWidget *widget, const gchar *uri,
207                   GtkHTMLStream *stream,
208                   ModestTnyMsgView *msg_view)
209 {
210         
211         ModestTnyMsgViewPrivate *priv;
212         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (msg_view);
213
214         g_message ("url requested: %s", uri);
215         
216         if (g_str_has_prefix (uri, "cid:")) {
217                 /* +4 ==> skip "cid:" */
218                 
219                 TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
220                 if (!part) {
221                         g_message ("%s not found", uri + 4);
222                         gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
223                 } else {
224                         TnyStreamIface *tny_stream =
225                                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
226                         tny_msg_mime_part_iface_decode_to_stream (part,tny_stream);
227                         gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
228                 }
229         }
230         return TRUE;
231 }
232
233
234
235
236 typedef struct  {
237         guint offset;
238         guint len;
239         const gchar* prefix;
240 } url_match_t;
241
242
243 static void
244 hyperlinkify_plain_text (GString *txt)
245 {
246         GSList *cursor;
247         GSList *match_list = get_url_matches (txt);
248
249         /* we will work backwards, so the offsets stay valid */
250         for (cursor = match_list; cursor; cursor = cursor->next) {
251
252                 url_match_t *match = (url_match_t*) cursor->data;
253                 gchar *url  = g_strndup (txt->str + match->offset, match->len);
254                 gchar *repl = NULL; /* replacement  */
255
256                 /* the prefix is NULL: use the one that is already there */
257                 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
258                                         match->prefix ? match->prefix : "", url, url);
259
260                 /* replace the old thing with our hyperlink
261                  * replacement thing */
262                 g_string_erase  (txt, match->offset, match->len);
263                 g_string_insert (txt, match->offset, repl);
264                 
265                 g_free (url);
266                 g_free (repl);
267                 
268         }
269         g_slist_free (match_list);
270 }
271
272
273
274 static gchar *
275 convert_to_html (const gchar *data)
276 {
277         int              i;
278         gboolean         first_space = TRUE;
279         GString         *html;      
280         gsize           len;
281
282         if (!data)
283                 return NULL;
284
285         len = strlen (data);
286         html = g_string_sized_new (len + 100);  /* just a  guess... */
287         
288         g_string_append_printf (html,
289                                 "<html>"
290                                 "<head>"
291                                 "<meta http-equiv=\"content-type\""
292                                 " content=\"text/html; charset=utf8\">"
293                                 "</head>"
294                                 "<body><tt>");
295         
296         /* replace with special html chars where needed*/
297         for (i = 0; i != len; ++i)  {
298                 char    kar = data[i]; 
299                 switch (kar) {
300                         
301                 case 0:  break; /* ignore embedded \0s */       
302                 case '<' : g_string_append   (html, "&lt;"); break;
303                 case '>' : g_string_append   (html, "&gt;"); break;
304                 case '&' : g_string_append   (html, "&quot;"); break;
305                 case '\n': g_string_append   (html, "<br>\n"); break;
306                 default:
307                         if (kar == ' ') {
308                                 g_string_append (html, first_space ? " " : "&nbsp;");
309                                 first_space = FALSE;
310                         } else  if (kar == '\t')
311                                 g_string_append (html, "&nbsp; &nbsp;&nbsp;");
312                         else {
313                                 int charnum = 0;
314                                 first_space = TRUE;
315                                 /* optimization trick: accumulate 'normal' chars, then copy */
316                                 do {
317                                         kar = data [++charnum + i];
318                                         
319                                 } while ((i + charnum < len) &&
320                                          (kar > '>' || (kar != '<' && kar != '>'
321                                                         && kar != '&' && kar !=  ' '
322                                                         && kar != '\n' && kar != '\t')));
323                                 g_string_append_len (html, &data[i], charnum);
324                                 i += (charnum  - 1);
325                         }
326                 }
327         }
328
329         g_string_append (html, "</tt></body></html>");
330         hyperlinkify_plain_text (html);
331
332         return g_string_free (html, FALSE);
333 }
334
335
336
337
338 static gint 
339 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
340 {
341         return match2->offset - match1->offset;
342 }
343
344
345
346 /*
347  * check if the match is inside an existing match... */
348 static void
349 chk_partial_match (const url_match_t *match, int* offset)
350 {
351         if (*offset >= match->offset && *offset < match->offset + match->len)
352                 *offset = -1;
353 }
354
355 static GSList*
356 get_url_matches (GString *txt)
357 {
358         regmatch_t rm;
359         int rv, i, offset = 0;
360         GSList *match_list = NULL;
361
362         static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
363         const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
364
365         /* initalize the regexps */
366         for (i = 0; i != pattern_num; ++i) {
367                 patterns[i].preg = g_new0 (regex_t,1);
368                 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
369                                   REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
370         }
371         /* find all the matches */
372         for (i = 0; i != pattern_num; ++i) {
373                 offset     = 0; 
374                 while (1) {
375                         int test_offset;
376                         if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
377                                 g_assert (rv == REG_NOMATCH); /* this should not happen */
378                                 break; /* try next regexp */ 
379                         }
380                         if (rm.rm_so == -1)
381                                 break;
382
383                         /* FIXME: optimize this */
384                         /* to avoid partial matches on something that was already found... */
385                         /* check_partial_match will put -1 in the data ptr if that is the case */
386                         test_offset = offset + rm.rm_so;
387                         g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
388                         
389                         /* make a list of our matches (<offset, len, prefix> tupels)*/
390                         if (test_offset != -1) {
391                                 url_match_t *match = g_new (url_match_t,1);
392                                 match->offset = offset + rm.rm_so;
393                                 match->len    = rm.rm_eo - rm.rm_so;
394                                 match->prefix = patterns[i].prefix;
395                                 match_list = g_slist_prepend (match_list, match);
396                         }
397                         offset += rm.rm_eo;
398                 }
399         }
400
401         for (i = 0; i != pattern_num; ++i) {
402                 regfree (patterns[i].preg);
403                 g_free  (patterns[i].preg);
404         } /* don't free patterns itself -- it's static */
405         
406         /* now sort the list, so the matches are in reverse order of occurence.
407          * that way, we can do the replacements starting from the end, so we don't need
408          * to recalculate the offsets
409          */
410         match_list = g_slist_sort (match_list,
411                                    (GCompareFunc)cmp_offsets_reverse); 
412         return match_list;      
413 }
414
415 static gboolean
416 fill_gtkhtml_with_txt (GtkHTML* gtkhtml, const gchar* txt)
417 {
418         gchar *html;
419         
420         g_return_val_if_fail (gtkhtml, FALSE);
421         g_return_val_if_fail (txt, FALSE);
422
423         html = convert_to_html (txt);
424         gtk_html_load_from_string (gtkhtml, html,  strlen(html));
425         g_free (html);
426
427         return TRUE;
428 }
429
430
431
432 static TnyMsgMimePartIface *
433 find_body_part (TnyMsgIface *msg, const gchar *mime_type)
434 {
435         TnyMsgMimePartIface *part = NULL;
436         GList *parts;
437
438         g_return_val_if_fail (msg, NULL);
439         g_return_val_if_fail (mime_type, NULL);
440
441         parts  = (GList*) tny_msg_iface_get_parts (msg);
442         while (parts && !part) {
443                 part = TNY_MSG_MIME_PART_IFACE(parts->data);
444                 if (!tny_msg_mime_part_iface_content_type_is (part, mime_type))
445                         part = NULL;
446                 parts = parts->next;
447         }
448         
449         return part;
450 }
451
452 static gboolean
453 set_html_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
454 {
455         TnyStreamIface *gtkhtml_stream; 
456         ModestTnyMsgViewPrivate *priv;
457         
458         g_return_val_if_fail (self, FALSE);
459         g_return_val_if_fail (tny_body, FALSE);
460         
461         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
462
463         gtkhtml_stream =
464                 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
465                                  (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
466         
467         tny_stream_iface_reset (gtkhtml_stream);
468         tny_msg_mime_part_iface_decode_to_stream (tny_body, gtkhtml_stream);
469         tny_stream_iface_reset (gtkhtml_stream);
470
471         g_object_unref (G_OBJECT(gtkhtml_stream));
472         
473         return TRUE;
474 }
475
476
477 /* this is a hack --> we use the tny_text_buffer_stream to
478  * get the message text, then write to gtkhtml 'by hand' */
479 static gboolean
480 set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
481 {
482         GtkTextBuffer *buf;
483         GtkTextIter begin, end;
484         TnyStreamIface* txt_stream;
485         gchar *txt;
486         ModestTnyMsgViewPrivate *priv;
487                 
488         g_return_val_if_fail (self, FALSE);
489         g_return_val_if_fail (tny_body, FALSE);
490
491         priv           = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
492         
493         buf            = gtk_text_buffer_new (NULL);
494         txt_stream     = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
495                 
496         tny_stream_iface_reset (txt_stream);
497         tny_msg_mime_part_iface_decode_to_stream (tny_body, txt_stream);
498         tny_stream_iface_reset (txt_stream);            
499         
500         gtk_text_buffer_get_bounds (buf, &begin, &end);
501         txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
502         
503         fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), txt);
504         
505         g_object_unref (G_OBJECT(txt_stream));
506         g_object_unref (G_OBJECT(buf));
507
508         g_free (txt);
509         return TRUE;
510 }
511
512
513
514 void
515 modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg)
516 {
517         TnyMsgMimePartIface *body;
518         ModestTnyMsgViewPrivate *priv;
519
520         g_return_if_fail (self);
521         
522         priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
523
524         priv->msg = msg;
525         
526         fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), "");
527
528         if (!msg) 
529                 return;
530         
531         body = find_body_part (msg, "text/html");
532         if (body) {
533                 set_html_message (self, body);
534                 return;
535         }
536         
537         body = find_body_part (msg, "text/plain");
538         if (body) {
539                 set_text_message (self, body);
540                 return;
541         }
542
543         /* hmmmmm */
544         fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml),
545                                 _("Unsupported message type"));
546 }
547
548
549