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>
34 #include <glib/gi18n.h>
35 #include <gtkhtml/gtkhtml.h>
36 #include <gtkhtml/gtkhtml-stream.h>
37 #include <tny-list-iface.h>
39 #include "modest-tny-msg-view.h"
40 #include "modest-tny-stream-gtkhtml.h"
41 #include "modest-tny-msg-actions.h"
44 /* 'private'/'protected' functions */
45 static void modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass);
46 static void modest_tny_msg_view_init (ModestTnyMsgView *obj);
47 static void modest_tny_msg_view_finalize (GObject *obj);
50 static GSList* get_url_matches (GString *txt);
51 static gboolean on_link_clicked (GtkWidget *widget, const gchar *uri,
52 ModestTnyMsgView *msg_view);
53 static gboolean on_url_requested (GtkWidget *widget, const gchar *uri,
54 GtkHTMLStream *stream,
55 ModestTnyMsgView *msg_view);
58 * we need these regexps to find URLs in plain text e-mails
60 typedef struct _UrlMatchPattern UrlMatchPattern;
61 struct _UrlMatchPattern {
68 #define ATT_PREFIX "att:"
70 #define MAIL_VIEWER_URL_MATCH_PATTERNS { \
71 { "(file|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.]+",\
89 ATTACHMENT_CLICKED_SIGNAL,
93 typedef struct _ModestTnyMsgViewPrivate ModestTnyMsgViewPrivate;
94 struct _ModestTnyMsgViewPrivate {
96 const TnyMsgIface *msg;
98 #define MODEST_TNY_MSG_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
99 MODEST_TYPE_TNY_MSG_VIEW, \
100 ModestTnyMsgViewPrivate))
102 static GtkContainerClass *parent_class = NULL;
104 /* uncomment the following if you have defined any signals */
105 static guint signals[LAST_SIGNAL] = {0};
108 modest_tny_msg_view_get_type (void)
110 static GType my_type = 0;
112 static const GTypeInfo my_info = {
113 sizeof(ModestTnyMsgViewClass),
114 NULL, /* base init */
115 NULL, /* base finalize */
116 (GClassInitFunc) modest_tny_msg_view_class_init,
117 NULL, /* class finalize */
118 NULL, /* class data */
119 sizeof(ModestTnyMsgView),
121 (GInstanceInitFunc) modest_tny_msg_view_init,
123 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
131 modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass)
133 GObjectClass *gobject_class;
134 gobject_class = (GObjectClass*) klass;
136 parent_class = g_type_class_peek_parent (klass);
137 gobject_class->finalize = modest_tny_msg_view_finalize;
139 g_type_class_add_private (gobject_class, sizeof(ModestTnyMsgViewPrivate));
142 signals[LINK_CLICKED_SIGNAL] =
143 g_signal_new ("link_clicked",
144 G_TYPE_FROM_CLASS (gobject_class),
146 G_STRUCT_OFFSET(ModestTnyMsgViewClass, link_clicked),
148 g_cclosure_marshal_VOID__STRING,
149 G_TYPE_NONE, 1, G_TYPE_STRING);
151 signals[ATTACHMENT_CLICKED_SIGNAL] =
152 g_signal_new ("attachment_clicked",
153 G_TYPE_FROM_CLASS (gobject_class),
155 G_STRUCT_OFFSET(ModestTnyMsgViewClass, attachment_clicked),
157 g_cclosure_marshal_VOID__POINTER,
158 G_TYPE_NONE, 1, G_TYPE_INT);
163 modest_tny_msg_view_init (ModestTnyMsgView *obj)
165 ModestTnyMsgViewPrivate *priv;
167 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
170 priv->gtkhtml = gtk_html_new();
172 gtk_html_set_editable (GTK_HTML(priv->gtkhtml), FALSE);
173 gtk_html_allow_selection (GTK_HTML(priv->gtkhtml), TRUE);
174 gtk_html_set_caret_mode (GTK_HTML(priv->gtkhtml), FALSE);
175 gtk_html_set_blocking (GTK_HTML(priv->gtkhtml), FALSE);
176 gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
178 g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
179 G_CALLBACK(on_link_clicked), obj);
181 g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
182 G_CALLBACK(on_url_requested), obj);
187 modest_tny_msg_view_finalize (GObject *obj)
193 modest_tny_msg_view_new (const TnyMsgIface *msg)
196 ModestTnyMsgView* self;
197 ModestTnyMsgViewPrivate *priv;
199 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_MSG_VIEW, NULL));
200 self = MODEST_TNY_MSG_VIEW(obj);
201 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
203 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
204 GTK_POLICY_AUTOMATIC,
205 GTK_POLICY_AUTOMATIC);
208 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
211 modest_tny_msg_view_set_message (self, msg);
213 return GTK_WIDGET(self);
218 on_link_clicked (GtkWidget *widget, const gchar *uri, ModestTnyMsgView *msg_view)
223 g_return_val_if_fail (msg_view, FALSE);
225 /* is it an attachment? */
226 if (g_str_has_prefix(uri, ATT_PREFIX)) {
228 index = atoi (uri + strlen(ATT_PREFIX));
231 /* index is 1-based, so 0 indicates an error */
232 g_printerr ("modest: invalid attachment id: %s\n", uri);
236 g_signal_emit (G_OBJECT(msg_view), signals[ATTACHMENT_CLICKED_SIGNAL],
241 g_signal_emit (G_OBJECT(msg_view), signals[LINK_CLICKED_SIGNAL], 0, uri);
247 static TnyMsgMimePartIface *
248 find_cid_image (const TnyMsgIface *msg, const gchar *cid)
250 TnyMsgMimePartIface *part = NULL;
251 const TnyListIface *parts;
252 TnyIteratorIface *iter;
254 g_return_val_if_fail (msg, NULL);
255 g_return_val_if_fail (cid, NULL);
257 parts = tny_msg_iface_get_parts ((TnyMsgIface*)msg); // FIXME: tinymail
258 iter = tny_list_iface_create_iterator ((TnyListIface*)parts);
260 while (!tny_iterator_iface_is_done(iter)) {
261 const gchar *part_cid;
262 part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current(iter));
263 part_cid = tny_msg_mime_part_iface_get_content_id (part);
265 if (part_cid && strcmp (cid, part_cid) == 0)
269 tny_iterator_iface_next (iter);
272 g_object_unref (G_OBJECT(iter));
278 on_url_requested (GtkWidget *widget, const gchar *uri,
279 GtkHTMLStream *stream,
280 ModestTnyMsgView *msg_view)
282 ModestTnyMsgViewPrivate *priv;
283 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (msg_view);
285 if (g_str_has_prefix (uri, "cid:")) {
286 /* +4 ==> skip "cid:" */
287 const TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
289 g_printerr ("modest: '%s' not found\n", uri + 4);
290 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
292 TnyStreamIface *tny_stream =
293 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
295 tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)part,
297 gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
313 /* render the attachments as hyperlinks in html */
315 attachments_as_html (ModestTnyMsgView *self, const TnyMsgIface *msg)
317 ModestTnyMsgViewPrivate *priv;
319 const TnyListIface *parts;
320 TnyIteratorIface *iter;
327 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
328 parts = tny_msg_iface_get_parts ((TnyMsgIface*)msg);
330 iter = tny_list_iface_create_iterator ((TnyListIface*)parts);
332 appendix= g_string_new ("");
334 while (!tny_iterator_iface_is_done(iter)) {
335 TnyMsgMimePartIface *part;
337 ++index; /* attachment numbers are 1-based */
339 part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current (iter));
341 if (tny_msg_mime_part_iface_is_attachment (part)) {
343 const gchar *filename = tny_msg_mime_part_iface_get_filename(part);
345 filename = _("attachment");
347 g_string_append_printf (appendix, "<a href=\"%s%d\">%s</a> \n",
348 ATT_PREFIX, index, filename);
350 tny_iterator_iface_next (iter);
352 g_object_unref (G_OBJECT(iter));
354 if (appendix->len == 0)
355 return g_string_free (appendix, TRUE);
357 html = g_strdup_printf ("<strong>%s:</strong> %s\n<hr>",
358 _("Attachments"), appendix->str);
359 g_string_free (appendix, TRUE);
367 hyperlinkify_plain_text (GString *txt)
370 GSList *match_list = get_url_matches (txt);
372 /* we will work backwards, so the offsets stay valid */
373 for (cursor = match_list; cursor; cursor = cursor->next) {
375 url_match_t *match = (url_match_t*) cursor->data;
376 gchar *url = g_strndup (txt->str + match->offset, match->len);
377 gchar *repl = NULL; /* replacement */
379 /* the prefix is NULL: use the one that is already there */
380 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
381 match->prefix ? match->prefix : "", url, url);
383 /* replace the old thing with our hyperlink
384 * replacement thing */
385 g_string_erase (txt, match->offset, match->len);
386 g_string_insert (txt, match->offset, repl);
391 g_free (cursor->data);
394 g_slist_free (match_list);
400 convert_to_html (const gchar *data)
403 gboolean first_space = TRUE;
411 html = g_string_sized_new (len + 100); /* just a guess... */
413 g_string_append_printf (html,
416 "<meta http-equiv=\"content-type\""
417 " content=\"text/html; charset=utf8\">"
421 /* replace with special html chars where needed*/
422 for (i = 0; i != len; ++i) {
426 case 0: break; /* ignore embedded \0s */
427 case '<' : g_string_append (html, "<"); break;
428 case '>' : g_string_append (html, ">"); break;
429 case '&' : g_string_append (html, """); break;
430 case '\n': g_string_append (html, "<br>\n"); break;
433 g_string_append (html, first_space ? " " : " ");
435 } else if (kar == '\t')
436 g_string_append (html, " ");
440 /* optimization trick: accumulate 'normal' chars, then copy */
442 kar = data [++charnum + i];
444 } while ((i + charnum < len) &&
445 (kar > '>' || (kar != '<' && kar != '>'
446 && kar != '&' && kar != ' '
447 && kar != '\n' && kar != '\t')));
448 g_string_append_len (html, &data[i], charnum);
454 g_string_append (html, "</tt></body></html>");
455 hyperlinkify_plain_text (html);
457 return g_string_free (html, FALSE);
464 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
466 return match2->offset - match1->offset;
472 * check if the match is inside an existing match... */
474 chk_partial_match (const url_match_t *match, int* offset)
476 if (*offset >= match->offset && *offset < match->offset + match->len)
481 get_url_matches (GString *txt)
484 int rv, i, offset = 0;
485 GSList *match_list = NULL;
487 static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
488 const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
490 /* initalize the regexps */
491 for (i = 0; i != pattern_num; ++i) {
492 patterns[i].preg = g_new0 (regex_t,1);
493 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
494 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
496 /* find all the matches */
497 for (i = 0; i != pattern_num; ++i) {
501 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
502 g_assert (rv == REG_NOMATCH); /* this should not happen */
503 break; /* try next regexp */
508 /* FIXME: optimize this */
509 /* to avoid partial matches on something that was already found... */
510 /* check_partial_match will put -1 in the data ptr if that is the case */
511 test_offset = offset + rm.rm_so;
512 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
514 /* make a list of our matches (<offset, len, prefix> tupels)*/
515 if (test_offset != -1) {
516 url_match_t *match = g_new (url_match_t,1);
517 match->offset = offset + rm.rm_so;
518 match->len = rm.rm_eo - rm.rm_so;
519 match->prefix = patterns[i].prefix;
520 match_list = g_slist_prepend (match_list, match);
526 for (i = 0; i != pattern_num; ++i) {
527 regfree (patterns[i].preg);
528 g_free (patterns[i].preg);
529 } /* don't free patterns itself -- it's static */
531 /* now sort the list, so the matches are in reverse order of occurence.
532 * that way, we can do the replacements starting from the end, so we don't need
533 * to recalculate the offsets
535 match_list = g_slist_sort (match_list,
536 (GCompareFunc)cmp_offsets_reverse);
543 set_html_message (ModestTnyMsgView *self, const TnyMsgMimePartIface *tny_body,
544 const TnyMsgIface *msg)
546 gchar *html_attachments;
547 TnyStreamIface *gtkhtml_stream;
548 ModestTnyMsgViewPrivate *priv;
550 g_return_val_if_fail (self, FALSE);
551 g_return_val_if_fail (tny_body, FALSE);
553 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
556 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
557 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
559 tny_stream_iface_reset (gtkhtml_stream);
561 html_attachments = attachments_as_html(self, msg);
562 if (html_attachments) {
563 tny_stream_iface_write (gtkhtml_stream, html_attachments,
564 strlen(html_attachments));
565 tny_stream_iface_reset (gtkhtml_stream);
566 g_free (html_attachments);
570 tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)tny_body,
573 g_object_unref (G_OBJECT(gtkhtml_stream));
579 /* this is a hack --> we use the tny_text_buffer_stream to
580 * get the message text, then write to gtkhtml 'by hand' */
582 set_text_message (ModestTnyMsgView *self, const TnyMsgMimePartIface *tny_body,
583 const TnyMsgIface *msg)
586 GtkTextIter begin, end;
587 TnyStreamIface* txt_stream, *gtkhtml_stream;
588 gchar *txt, *html_attachments;
589 ModestTnyMsgViewPrivate *priv;
591 g_return_val_if_fail (self, FALSE);
592 g_return_val_if_fail (tny_body, FALSE);
594 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
596 buf = gtk_text_buffer_new (NULL);
597 txt_stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
599 tny_stream_iface_reset (txt_stream);
602 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
603 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
605 html_attachments = attachments_as_html(self, msg);
606 if (html_attachments) {
607 tny_stream_iface_write (gtkhtml_stream, html_attachments,
608 strlen(html_attachments));
609 tny_stream_iface_reset (gtkhtml_stream);
610 g_free (html_attachments);
614 tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)tny_body,
616 tny_stream_iface_reset (txt_stream);
618 gtk_text_buffer_get_bounds (buf, &begin, &end);
619 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
621 gchar *html = convert_to_html (txt);
622 tny_stream_iface_write (gtkhtml_stream, html, strlen(html));
623 tny_stream_iface_reset (gtkhtml_stream);
628 g_object_unref (G_OBJECT(gtkhtml_stream));
629 g_object_unref (G_OBJECT(txt_stream));
630 g_object_unref (G_OBJECT(buf));
636 modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self)
638 ModestTnyMsgViewPrivate *priv;
644 g_return_val_if_fail (self, NULL);
645 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
646 html = priv->gtkhtml;
648 /* I'm sure there is a better way to check for selected text */
649 sel = gtk_html_get_selection_html(GTK_HTML(html), &len);
655 clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
656 return gtk_clipboard_wait_for_text(clip);
661 modest_tny_msg_view_set_message (ModestTnyMsgView *self, const TnyMsgIface *msg)
663 TnyMsgMimePartIface *body;
664 ModestTnyMsgViewPrivate *priv;
666 g_return_if_fail (self);
668 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
672 body = modest_tny_msg_actions_find_body_part (msg, TRUE);
674 if (tny_msg_mime_part_iface_content_type_is (body, "text/html"))
675 set_html_message (self, body, msg);
677 set_text_message (self, body, msg);
680 /* nothing to show */