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-gtk-text-buffer-stream.h>
35 #include <glib/gi18n.h>
36 #include <gtkhtml/gtkhtml.h>
37 #include <gtkhtml/gtkhtml-stream.h>
39 #include <tny-simple-list.h>
41 #include <modest-tny-msg-actions.h>
42 #include "modest-msg-view.h"
43 #include "modest-tny-stream-gtkhtml.h"
46 /* 'private'/'protected' functions */
47 static void modest_msg_view_class_init (ModestMsgViewClass *klass);
48 static void modest_msg_view_init (ModestMsgView *obj);
49 static void modest_msg_view_finalize (GObject *obj);
52 static GSList* get_url_matches (GString *txt);
53 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view);
54 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri, GtkHTMLStream *stream,
55 ModestMsgView *msg_view);
56 static gboolean on_link_hover (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view);
59 * we need these regexps to find URLs in plain text e-mails
61 typedef struct _UrlMatchPattern UrlMatchPattern;
62 struct _UrlMatchPattern {
68 #define ATT_PREFIX "att:"
70 #define MAIL_VIEWER_URL_MATCH_PATTERNS { \
71 { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
73 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
75 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
77 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
79 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
81 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
90 ATTACHMENT_CLICKED_SIGNAL,
94 typedef struct _ModestMsgViewPrivate ModestMsgViewPrivate;
95 struct _ModestMsgViewPrivate {
100 gulong sig1, sig2, sig3;
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,
128 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
136 modest_msg_view_class_init (ModestMsgViewClass *klass)
138 GObjectClass *gobject_class;
139 gobject_class = (GObjectClass*) klass;
141 parent_class = g_type_class_peek_parent (klass);
142 gobject_class->finalize = modest_msg_view_finalize;
144 g_type_class_add_private (gobject_class, sizeof(ModestMsgViewPrivate));
147 signals[LINK_CLICKED_SIGNAL] =
148 g_signal_new ("link_clicked",
149 G_TYPE_FROM_CLASS (gobject_class),
151 G_STRUCT_OFFSET(ModestMsgViewClass, link_clicked),
153 g_cclosure_marshal_VOID__STRING,
154 G_TYPE_NONE, 1, G_TYPE_STRING);
156 signals[ATTACHMENT_CLICKED_SIGNAL] =
157 g_signal_new ("attachment_clicked",
158 G_TYPE_FROM_CLASS (gobject_class),
160 G_STRUCT_OFFSET(ModestMsgViewClass, attachment_clicked),
162 g_cclosure_marshal_VOID__POINTER,
163 G_TYPE_NONE, 1, G_TYPE_INT);
165 signals[LINK_HOVER_SIGNAL] =
166 g_signal_new ("link_hover",
167 G_TYPE_FROM_CLASS (gobject_class),
169 G_STRUCT_OFFSET(ModestMsgViewClass, link_hover),
171 g_cclosure_marshal_VOID__STRING,
172 G_TYPE_NONE, 1, G_TYPE_STRING);
176 modest_msg_view_init (ModestMsgView *obj)
178 ModestMsgViewPrivate *priv;
180 priv = MODEST_MSG_VIEW_GET_PRIVATE(obj);
183 priv->gtkhtml = gtk_html_new();
185 gtk_html_set_editable (GTK_HTML(priv->gtkhtml), FALSE);
186 gtk_html_allow_selection (GTK_HTML(priv->gtkhtml), TRUE);
187 gtk_html_set_caret_mode (GTK_HTML(priv->gtkhtml), FALSE);
188 gtk_html_set_blocking (GTK_HTML(priv->gtkhtml), FALSE);
189 gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
191 priv->sig1 = g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
192 G_CALLBACK(on_link_clicked), obj);
193 priv->sig2 = g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
194 G_CALLBACK(on_url_requested), obj);
195 priv->sig3 = g_signal_connect (G_OBJECT(priv->gtkhtml), "on_url",
196 G_CALLBACK(on_link_hover), obj);
201 modest_msg_view_finalize (GObject *obj)
203 ModestMsgViewPrivate *priv;
204 priv = MODEST_MSG_VIEW_GET_PRIVATE (obj);
207 g_object_unref (G_OBJECT(priv->msg));
211 /* we cannot disconnect sigs, because priv->gtkhtml is
214 priv->gtkhtml = NULL;
216 G_OBJECT_CLASS(parent_class)->finalize (obj);
221 modest_msg_view_new (TnyMsg *msg)
225 ModestMsgViewPrivate *priv;
227 obj = G_OBJECT(g_object_new(MODEST_TYPE_MSG_VIEW, NULL));
228 self = MODEST_MSG_VIEW(obj);
229 priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
231 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
232 GTK_POLICY_AUTOMATIC,
233 GTK_POLICY_AUTOMATIC);
236 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
239 modest_msg_view_set_message (self, msg);
241 return GTK_WIDGET(self);
246 on_link_clicked (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
250 g_return_val_if_fail (msg_view, FALSE);
252 /* is it an attachment? */
253 if (g_str_has_prefix(uri, ATT_PREFIX)) {
255 index = atoi (uri + strlen(ATT_PREFIX));
258 /* index is 1-based, so 0 indicates an error */
259 g_printerr ("modest: invalid attachment id: %s\n", uri);
263 g_signal_emit (G_OBJECT(msg_view), signals[ATTACHMENT_CLICKED_SIGNAL],
268 g_signal_emit (G_OBJECT(msg_view), signals[LINK_CLICKED_SIGNAL],
277 on_link_hover (GtkWidget *widget, const gchar *uri, ModestMsgView *msg_view)
279 if (uri && g_str_has_prefix (uri, ATT_PREFIX))
282 g_signal_emit (G_OBJECT(msg_view), signals[LINK_HOVER_SIGNAL],
291 find_cid_image (TnyMsg *msg, const gchar *cid)
293 TnyMimePart *part = NULL;
297 g_return_val_if_fail (msg, NULL);
298 g_return_val_if_fail (cid, NULL);
300 parts = TNY_LIST (tny_simple_list_new());
302 tny_msg_get_parts (msg, parts);
303 iter = tny_list_create_iterator (parts);
305 while (!tny_iterator_is_done(iter)) {
306 const gchar *part_cid;
307 part = TNY_MIME_PART(tny_iterator_get_current(iter));
308 part_cid = tny_mime_part_get_content_id (part);
310 if (part_cid && strcmp (cid, part_cid) == 0)
314 tny_iterator_next (iter);
318 g_object_ref (G_OBJECT(part));
320 g_object_unref (G_OBJECT(iter));
321 g_object_unref (G_OBJECT(parts));
328 on_url_requested (GtkWidget *widget, const gchar *uri,
329 GtkHTMLStream *stream,
330 ModestMsgView *msg_view)
332 ModestMsgViewPrivate *priv;
333 priv = MODEST_MSG_VIEW_GET_PRIVATE (msg_view);
335 if (g_str_has_prefix (uri, "cid:")) {
336 /* +4 ==> skip "cid:" */
337 TnyMimePart *part = find_cid_image (priv->msg, uri + 4);
339 g_printerr ("modest: '%s' not found\n", uri + 4);
340 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
342 TnyStream *tny_stream =
343 TNY_STREAM(modest_tny_stream_gtkhtml_new(stream));
344 tny_mime_part_decode_to_stream ((TnyMimePart*)part,
346 gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
348 g_object_unref (G_OBJECT(tny_stream));
349 g_object_unref (G_OBJECT(part));
365 /* render the attachments as hyperlinks in html */
367 attachments_as_html (ModestMsgView *self, TnyMsg *msg)
369 ModestMsgViewPrivate *priv;
379 priv = MODEST_MSG_VIEW_GET_PRIVATE (self);
381 parts = TNY_LIST(tny_simple_list_new());
382 tny_msg_get_parts (msg, parts);
383 iter = tny_list_create_iterator (parts);
385 appendix= g_string_new ("");
387 while (!tny_iterator_is_done(iter)) {
390 ++index; /* attachment numbers are 1-based */
392 part = TNY_MIME_PART(tny_iterator_get_current (iter));
394 if (tny_mime_part_is_attachment (part)) {
396 const gchar *filename = tny_mime_part_get_filename(part);
398 filename = _("attachment");
400 g_string_append_printf (appendix, "<a href=\"%s%d\">%s</a> \n",
401 ATT_PREFIX, index, filename);
403 tny_iterator_next (iter);
405 g_object_unref (G_OBJECT(iter));
407 if (appendix->len == 0)
408 return g_string_free (appendix, TRUE);
410 html = g_strdup_printf ("<strong>%s:</strong> %s\n<hr>",
411 _("Attachments"), appendix->str);
412 g_string_free (appendix, TRUE);
420 hyperlinkify_plain_text (GString *txt)
423 GSList *match_list = get_url_matches (txt);
425 /* we will work backwards, so the offsets stay valid */
426 for (cursor = match_list; cursor; cursor = cursor->next) {
428 url_match_t *match = (url_match_t*) cursor->data;
429 gchar *url = g_strndup (txt->str + match->offset, match->len);
430 gchar *repl = NULL; /* replacement */
432 /* the prefix is NULL: use the one that is already there */
433 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
434 match->prefix ? match->prefix : "", url, url);
436 /* replace the old thing with our hyperlink
437 * replacement thing */
438 g_string_erase (txt, match->offset, match->len);
439 g_string_insert (txt, match->offset, repl);
444 g_free (cursor->data);
447 g_slist_free (match_list);
453 convert_to_html (const gchar *data)
456 gboolean first_space = TRUE;
464 html = g_string_sized_new (len + 100); /* just a guess... */
466 g_string_append_printf (html,
469 "<meta http-equiv=\"content-type\""
470 " content=\"text/html; charset=utf8\">"
474 /* replace with special html chars where needed*/
475 for (i = 0; i != len; ++i) {
479 case 0: break; /* ignore embedded \0s */
480 case '<' : g_string_append (html, "<"); break;
481 case '>' : g_string_append (html, ">"); break;
482 case '&' : g_string_append (html, """); break;
483 case '\n': g_string_append (html, "<br>\n"); break;
486 g_string_append (html, first_space ? " " : " ");
488 } else if (kar == '\t')
489 g_string_append (html, " ");
493 /* optimization trick: accumulate 'normal' chars, then copy */
495 kar = data [++charnum + i];
497 } while ((i + charnum < len) &&
498 (kar > '>' || (kar != '<' && kar != '>'
499 && kar != '&' && kar != ' '
500 && kar != '\n' && kar != '\t')));
501 g_string_append_len (html, &data[i], charnum);
507 g_string_append (html, "</tt></body></html>");
508 hyperlinkify_plain_text (html);
510 return g_string_free (html, FALSE);
517 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
519 return match2->offset - match1->offset;
525 * check if the match is inside an existing match... */
527 chk_partial_match (const url_match_t *match, guint* offset)
529 if (*offset >= match->offset && *offset < match->offset + match->len)
534 get_url_matches (GString *txt)
537 guint rv, i, offset = 0;
538 GSList *match_list = NULL;
540 static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
541 const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
543 /* initalize the regexps */
544 for (i = 0; i != pattern_num; ++i) {
545 patterns[i].preg = g_new0 (regex_t,1);
546 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
547 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
549 /* find all the matches */
550 for (i = 0; i != pattern_num; ++i) {
554 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
555 g_assert (rv == REG_NOMATCH); /* this should not happen */
556 break; /* try next regexp */
561 /* FIXME: optimize this */
562 /* to avoid partial matches on something that was already found... */
563 /* check_partial_match will put -1 in the data ptr if that is the case */
564 test_offset = offset + rm.rm_so;
565 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
567 /* make a list of our matches (<offset, len, prefix> tupels)*/
568 if (test_offset != -1) {
569 url_match_t *match = g_new (url_match_t,1);
570 match->offset = offset + rm.rm_so;
571 match->len = rm.rm_eo - rm.rm_so;
572 match->prefix = patterns[i].prefix;
573 match_list = g_slist_prepend (match_list, match);
579 for (i = 0; i != pattern_num; ++i) {
580 regfree (patterns[i].preg);
581 g_free (patterns[i].preg);
582 } /* don't free patterns itself -- it's static */
584 /* now sort the list, so the matches are in reverse order of occurence.
585 * that way, we can do the replacements starting from the end, so we don't need
586 * to recalculate the offsets
588 match_list = g_slist_sort (match_list,
589 (GCompareFunc)cmp_offsets_reverse);
596 set_html_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
598 gchar *html_attachments;
599 TnyStream *gtkhtml_stream;
600 ModestMsgViewPrivate *priv;
602 g_return_val_if_fail (self, FALSE);
603 g_return_val_if_fail (tny_body, FALSE);
605 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
608 TNY_STREAM(modest_tny_stream_gtkhtml_new
609 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
611 tny_stream_reset (gtkhtml_stream);
613 html_attachments = attachments_as_html(self, msg);
614 if (html_attachments) {
615 tny_stream_write (gtkhtml_stream, html_attachments,
616 strlen(html_attachments));
617 tny_stream_reset (gtkhtml_stream);
618 g_free (html_attachments);
622 tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body,
625 g_object_unref (G_OBJECT(gtkhtml_stream));
631 /* this is a hack --> we use the tny_text_buffer_stream to
632 * get the message text, then write to gtkhtml 'by hand' */
634 set_text_message (ModestMsgView *self, TnyMimePart *tny_body, TnyMsg *msg)
637 GtkTextIter begin, end;
638 TnyStream* txt_stream, *gtkhtml_stream;
639 gchar *txt, *html_attachments;
640 ModestMsgViewPrivate *priv;
642 g_return_val_if_fail (self, FALSE);
643 g_return_val_if_fail (tny_body, FALSE);
645 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
647 buf = gtk_text_buffer_new (NULL);
648 txt_stream = TNY_STREAM(tny_gtk_text_buffer_stream_new (buf));
650 tny_stream_reset (txt_stream);
653 TNY_STREAM(modest_tny_stream_gtkhtml_new
654 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
656 html_attachments = attachments_as_html(self, msg);
657 if (html_attachments) {
658 tny_stream_write (gtkhtml_stream, html_attachments,
659 strlen(html_attachments));
660 tny_stream_reset (gtkhtml_stream);
661 g_free (html_attachments);
665 tny_mime_part_decode_to_stream ((TnyMimePart*)tny_body,
667 tny_stream_reset (txt_stream);
669 gtk_text_buffer_get_bounds (buf, &begin, &end);
670 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
672 gchar *html = convert_to_html (txt);
673 tny_stream_write (gtkhtml_stream, html, strlen(html));
674 tny_stream_reset (gtkhtml_stream);
679 g_object_unref (G_OBJECT(gtkhtml_stream));
680 g_object_unref (G_OBJECT(txt_stream));
681 g_object_unref (G_OBJECT(buf));
688 set_empty_message (ModestMsgView *self)
690 ModestMsgViewPrivate *priv;
692 g_return_val_if_fail (self, FALSE);
693 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
695 gtk_html_load_from_string (GTK_HTML(priv->gtkhtml),
703 modest_msg_view_get_selected_text (ModestMsgView *self)
705 ModestMsgViewPrivate *priv;
711 g_return_val_if_fail (self, NULL);
712 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
713 html = priv->gtkhtml;
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);
722 clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
723 return gtk_clipboard_wait_for_text(clip);
728 modest_msg_view_set_message (ModestMsgView *self, TnyMsg *msg)
731 ModestMsgViewPrivate *priv;
733 g_return_if_fail (self);
735 priv = MODEST_MSG_VIEW_GET_PRIVATE(self);
738 if (msg != priv->msg) {
740 g_object_unref (G_OBJECT(priv->msg));
742 g_object_ref (G_OBJECT(msg));
747 set_empty_message (self);
751 body = modest_tny_msg_actions_find_body_part (msg, TRUE);
753 if (tny_mime_part_content_type_is (body, "text/html"))
754 set_html_message (self, body, msg);
756 set_text_message (self, body, msg);
759 set_empty_message (self);