1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <tny-text-buffer-stream.h>
35 #include <glib/gi18n.h>
36 #include <gtkhtml/gtkhtml.h>
37 #include <gtkhtml/gtkhtml-stream.h>
38 #include <tny-list-iface.h>
40 #include <modest-tny-msg-actions.h>
41 #include "modest-msg-view.h"
42 #include "modest-tny-stream-gtkhtml.h"
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);
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);
61 * we need these regexps to find URLs in plain text e-mails
63 typedef struct _UrlMatchPattern UrlMatchPattern;
64 struct _UrlMatchPattern {
71 #define ATT_PREFIX "att:"
73 #define MAIL_VIEWER_URL_MATCH_PATTERNS { \
74 { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
76 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
78 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
80 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
82 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
84 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
93 ATTACHMENT_CLICKED_SIGNAL,
97 typedef struct _ModestMsgViewPrivate ModestMsgViewPrivate;
98 struct _ModestMsgViewPrivate {
100 const TnyMsgIface *msg;
102 #define MODEST_MSG_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
103 MODEST_TYPE_MSG_VIEW, \
104 ModestMsgViewPrivate))
106 static GtkContainerClass *parent_class = NULL;
108 /* uncomment the following if you have defined any signals */
109 static guint signals[LAST_SIGNAL] = {0};
112 modest_msg_view_get_type (void)
114 static GType my_type = 0;
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),
125 (GInstanceInitFunc) modest_msg_view_init,
127 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
135 modest_msg_view_class_init (ModestMsgViewClass *klass)
137 GObjectClass *gobject_class;
138 gobject_class = (GObjectClass*) klass;
140 parent_class = g_type_class_peek_parent (klass);
141 gobject_class->finalize = modest_msg_view_finalize;
143 g_type_class_add_private (gobject_class, sizeof(ModestMsgViewPrivate));
146 signals[LINK_CLICKED_SIGNAL] =
147 g_signal_new ("link_clicked",
148 G_TYPE_FROM_CLASS (gobject_class),
150 G_STRUCT_OFFSET(ModestMsgViewClass, link_clicked),
152 g_cclosure_marshal_VOID__STRING,
153 G_TYPE_NONE, 1, G_TYPE_STRING);
155 signals[ATTACHMENT_CLICKED_SIGNAL] =
156 g_signal_new ("attachment_clicked",
157 G_TYPE_FROM_CLASS (gobject_class),
159 G_STRUCT_OFFSET(ModestMsgViewClass, attachment_clicked),
161 g_cclosure_marshal_VOID__POINTER,
162 G_TYPE_NONE, 1, G_TYPE_INT);
164 signals[LINK_HOVER_SIGNAL] =
165 g_signal_new ("link_hover",
166 G_TYPE_FROM_CLASS (gobject_class),
168 G_STRUCT_OFFSET(ModestMsgViewClass, link_hover),
170 g_cclosure_marshal_VOID__STRING,
171 G_TYPE_NONE, 1, G_TYPE_STRING);
175 modest_msg_view_init (ModestMsgView *obj)
177 ModestMsgViewPrivate *priv;
179 priv = MODEST_MSG_VIEW_GET_PRIVATE(obj);
182 priv->gtkhtml = gtk_html_new();
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);
190 g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
191 G_CALLBACK(on_link_clicked), obj);
193 g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
194 G_CALLBACK(on_url_requested), obj);
196 g_signal_connect (G_OBJECT(priv->gtkhtml), "on_url",
197 G_CALLBACK(on_link_hover), obj);
202 modest_msg_view_finalize (GObject *obj)
204 G_OBJECT_CLASS(parent_class)->finalize (obj);
209 modest_msg_view_new (const TnyMsgIface *msg)
213 ModestMsgViewPrivate *priv;
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);
219 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
220 GTK_POLICY_AUTOMATIC,
221 GTK_POLICY_AUTOMATIC);
224 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
227 modest_msg_view_set_message (self, msg);
229 return GTK_WIDGET(self);
234 on_link_clicked (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
238 g_return_val_if_fail (msg_view, FALSE);
240 /* is it an attachment? */
241 if (g_str_has_prefix(uri, ATT_PREFIX)) {
243 index = atoi (uri + strlen(ATT_PREFIX));
246 /* index is 1-based, so 0 indicates an error */
247 g_printerr ("modest: invalid attachment id: %s\n", uri);
251 g_signal_emit (G_OBJECT(msg_view), signals[ATTACHMENT_CLICKED_SIGNAL],
256 g_signal_emit (G_OBJECT(msg_view), signals[LINK_CLICKED_SIGNAL],
265 on_link_hover (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
267 if (uri && g_str_has_prefix (uri, ATT_PREFIX))
270 g_signal_emit (G_OBJECT(msg_view), signals[LINK_HOVER_SIGNAL],
278 static TnyMsgMimePartIface *
279 find_cid_image (const TnyMsgIface *msg, const gchar *cid)
281 TnyMsgMimePartIface *part = NULL;
282 const TnyListIface *parts;
283 TnyIteratorIface *iter;
285 g_return_val_if_fail (msg, NULL);
286 g_return_val_if_fail (cid, NULL);
288 parts = tny_msg_iface_get_parts ((TnyMsgIface*)msg); // FIXME: tinymail
289 iter = tny_list_iface_create_iterator ((TnyListIface*)parts);
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);
296 if (part_cid && strcmp (cid, part_cid) == 0)
300 tny_iterator_iface_next (iter);
303 g_object_unref (G_OBJECT(iter));
309 on_url_requested (GtkWidget *widget, const gchar *uri,
310 GtkHTMLStream *stream,
311 ModestMsgView *msg_view)
313 ModestMsgViewPrivate *priv;
314 priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
316 if (g_str_has_prefix (uri, "cid:")) {
317 /* +4 ==> skip "cid:" */
318 const TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
320 g_printerr ("modest: '%s' not found\n", uri + 4);
321 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
323 TnyStreamIface *tny_stream =
324 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
326 tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)part,
328 gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
344 /* render the attachments as hyperlinks in html */
346 attachments_as_html (ModestMsgView *self, const TnyMsgIface *msg)
348 ModestMsgViewPrivate *priv;
350 const TnyListIface *parts;
351 TnyIteratorIface *iter;
358 priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
359 parts = tny_msg_iface_get_parts ((TnyMsgIface*)msg);
361 iter = tny_list_iface_create_iterator ((TnyListIface*)parts);
363 appendix= g_string_new ("");
365 while (!tny_iterator_iface_is_done(iter)) {
366 TnyMsgMimePartIface *part;
368 ++index; /* attachment numbers are 1-based */
370 part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current (iter));
372 if (tny_msg_mime_part_iface_is_attachment (part)) {
374 const gchar *filename = tny_msg_mime_part_iface_get_filename(part);
376 filename = _("attachment");
378 g_string_append_printf (appendix, "<a href=\"%s%d\">%s</a> \n",
379 ATT_PREFIX, index, filename);
381 tny_iterator_iface_next (iter);
383 g_object_unref (G_OBJECT(iter));
385 if (appendix->len == 0)
386 return g_string_free (appendix, TRUE);
388 html = g_strdup_printf ("<strong>%s:</strong> %s\n<hr>",
389 _("Attachments"), appendix->str);
390 g_string_free (appendix, TRUE);
398 hyperlinkify_plain_text (GString *txt)
401 GSList *match_list = get_url_matches (txt);
403 /* we will work backwards, so the offsets stay valid */
404 for (cursor = match_list; cursor; cursor = cursor->next) {
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 */
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);
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);
422 g_free (cursor->data);
425 g_slist_free (match_list);
431 convert_to_html (const gchar *data)
434 gboolean first_space = TRUE;
442 html = g_string_sized_new (len + 100); /* just a guess... */
444 g_string_append_printf (html,
447 "<meta http-equiv=\"content-type\""
448 " content=\"text/html; charset=utf8\">"
452 /* replace with special html chars where needed*/
453 for (i = 0; i != len; ++i) {
457 case 0: break; /* ignore embedded \0s */
458 case '<' : g_string_append (html, "<"); break;
459 case '>' : g_string_append (html, ">"); break;
460 case '&' : g_string_append (html, """); break;
461 case '\n': g_string_append (html, "<br>\n"); break;
464 g_string_append (html, first_space ? " " : " ");
466 } else if (kar == '\t')
467 g_string_append (html, " ");
471 /* optimization trick: accumulate 'normal' chars, then copy */
473 kar = data [++charnum + i];
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);
485 g_string_append (html, "</tt></body></html>");
486 hyperlinkify_plain_text (html);
488 return g_string_free (html, FALSE);
495 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
497 return match2->offset - match1->offset;
503 * check if the match is inside an existing match... */
505 chk_partial_match (const url_match_t *match, int* offset)
507 if (*offset >= match->offset && *offset < match->offset + match->len)
512 get_url_matches (GString *txt)
515 int rv, i, offset = 0;
516 GSList *match_list = NULL;
518 static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
519 const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
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);
527 /* find all the matches */
528 for (i = 0; i != pattern_num; ++i) {
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 */
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);
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);
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 */
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
566 match_list = g_slist_sort (match_list,
567 (GCompareFunc)cmp_offsets_reverse);
574 set_html_message (ModestMsgView *self, const TnyMsgMimePartIface *tny_body,
575 const TnyMsgIface *msg)
577 gchar *html_attachments;
578 TnyStreamIface *gtkhtml_stream;
579 ModestMsgViewPrivate *priv;
581 g_return_val_if_fail (self, FALSE);
582 g_return_val_if_fail (tny_body, FALSE);
584 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
587 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
588 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
590 tny_stream_iface_reset (gtkhtml_stream);
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);
601 tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)tny_body,
604 g_object_unref (G_OBJECT(gtkhtml_stream));
610 /* this is a hack --> we use the tny_text_buffer_stream to
611 * get the message text, then write to gtkhtml 'by hand' */
613 set_text_message (ModestMsgView *self, const TnyMsgMimePartIface *tny_body,
614 const TnyMsgIface *msg)
617 GtkTextIter begin, end;
618 TnyStreamIface* txt_stream, *gtkhtml_stream;
619 gchar *txt, *html_attachments;
620 ModestMsgViewPrivate *priv;
622 g_return_val_if_fail (self, FALSE);
623 g_return_val_if_fail (tny_body, FALSE);
625 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
627 buf = gtk_text_buffer_new (NULL);
628 txt_stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
630 tny_stream_iface_reset (txt_stream);
633 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
634 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
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);
645 tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)tny_body,
647 tny_stream_iface_reset (txt_stream);
649 gtk_text_buffer_get_bounds (buf, &begin, &end);
650 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
652 gchar *html = convert_to_html (txt);
653 tny_stream_iface_write (gtkhtml_stream, html, strlen(html));
654 tny_stream_iface_reset (gtkhtml_stream);
659 g_object_unref (G_OBJECT(gtkhtml_stream));
660 g_object_unref (G_OBJECT(txt_stream));
661 g_object_unref (G_OBJECT(buf));
668 set_empty_message (ModestMsgView *self)
670 ModestMsgViewPrivate *priv;
672 g_return_val_if_fail (self, FALSE);
673 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
675 gtk_html_load_from_string (GTK_HTML(priv->gtkhtml),
683 modest_msg_view_get_selected_text (ModestMsgView *self)
685 ModestMsgViewPrivate *priv;
691 g_return_val_if_fail (self, NULL);
692 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
693 html = priv->gtkhtml;
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);
702 clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
703 return gtk_clipboard_wait_for_text(clip);
708 modest_msg_view_set_message (ModestMsgView *self, const TnyMsgIface *msg)
710 TnyMsgMimePartIface *body;
711 ModestMsgViewPrivate *priv;
713 g_return_if_fail (self);
715 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
720 set_empty_message (self);
724 body = modest_tny_msg_actions_find_body_part (msg, TRUE);
726 if (tny_msg_mime_part_iface_content_type_is (body, "text/html"))
727 set_html_message (self, body, msg);
729 set_text_message (self, body, msg);
732 set_empty_message (self);