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)
189 G_OBJECT_CLASS(parent_class)->finalize (obj);
194 modest_tny_msg_view_new (const TnyMsgIface *msg)
197 ModestTnyMsgView* self;
198 ModestTnyMsgViewPrivate *priv;
200 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_MSG_VIEW, NULL));
201 self = MODEST_TNY_MSG_VIEW(obj);
202 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
204 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
205 GTK_POLICY_AUTOMATIC,
206 GTK_POLICY_AUTOMATIC);
209 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
212 modest_tny_msg_view_set_message (self, msg);
214 return GTK_WIDGET(self);
219 on_link_clicked (GtkWidget *widget, const gchar *uri, ModestTnyMsgView *msg_view)
224 g_return_val_if_fail (msg_view, FALSE);
226 /* is it an attachment? */
227 if (g_str_has_prefix(uri, ATT_PREFIX)) {
229 index = atoi (uri + strlen(ATT_PREFIX));
232 /* index is 1-based, so 0 indicates an error */
233 g_printerr ("modest: invalid attachment id: %s\n", uri);
237 g_signal_emit (G_OBJECT(msg_view), signals[ATTACHMENT_CLICKED_SIGNAL],
242 g_signal_emit (G_OBJECT(msg_view), signals[LINK_CLICKED_SIGNAL], 0, uri);
248 static TnyMsgMimePartIface *
249 find_cid_image (const TnyMsgIface *msg, const gchar *cid)
251 TnyMsgMimePartIface *part = NULL;
252 const TnyListIface *parts;
253 TnyIteratorIface *iter;
255 g_return_val_if_fail (msg, NULL);
256 g_return_val_if_fail (cid, NULL);
258 parts = tny_msg_iface_get_parts ((TnyMsgIface*)msg); // FIXME: tinymail
259 iter = tny_list_iface_create_iterator ((TnyListIface*)parts);
261 while (!tny_iterator_iface_is_done(iter)) {
262 const gchar *part_cid;
263 part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current(iter));
264 part_cid = tny_msg_mime_part_iface_get_content_id (part);
266 if (part_cid && strcmp (cid, part_cid) == 0)
270 tny_iterator_iface_next (iter);
273 g_object_unref (G_OBJECT(iter));
279 on_url_requested (GtkWidget *widget, const gchar *uri,
280 GtkHTMLStream *stream,
281 ModestTnyMsgView *msg_view)
283 ModestTnyMsgViewPrivate *priv;
284 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (msg_view);
286 if (g_str_has_prefix (uri, "cid:")) {
287 /* +4 ==> skip "cid:" */
288 const TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
290 g_printerr ("modest: '%s' not found\n", uri + 4);
291 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
293 TnyStreamIface *tny_stream =
294 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(stream));
296 tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)part,
298 gtk_html_stream_close (stream, GTK_HTML_STREAM_OK);
314 /* render the attachments as hyperlinks in html */
316 attachments_as_html (ModestTnyMsgView *self, const TnyMsgIface *msg)
318 ModestTnyMsgViewPrivate *priv;
320 const TnyListIface *parts;
321 TnyIteratorIface *iter;
328 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (self);
329 parts = tny_msg_iface_get_parts ((TnyMsgIface*)msg);
331 iter = tny_list_iface_create_iterator ((TnyListIface*)parts);
333 appendix= g_string_new ("");
335 while (!tny_iterator_iface_is_done(iter)) {
336 TnyMsgMimePartIface *part;
338 ++index; /* attachment numbers are 1-based */
340 part = TNY_MSG_MIME_PART_IFACE(tny_iterator_iface_current (iter));
342 if (tny_msg_mime_part_iface_is_attachment (part)) {
344 const gchar *filename = tny_msg_mime_part_iface_get_filename(part);
346 filename = _("attachment");
348 g_string_append_printf (appendix, "<a href=\"%s%d\">%s</a> \n",
349 ATT_PREFIX, index, filename);
351 tny_iterator_iface_next (iter);
353 g_object_unref (G_OBJECT(iter));
355 if (appendix->len == 0)
356 return g_string_free (appendix, TRUE);
358 html = g_strdup_printf ("<strong>%s:</strong> %s\n<hr>",
359 _("Attachments"), appendix->str);
360 g_string_free (appendix, TRUE);
368 hyperlinkify_plain_text (GString *txt)
371 GSList *match_list = get_url_matches (txt);
373 /* we will work backwards, so the offsets stay valid */
374 for (cursor = match_list; cursor; cursor = cursor->next) {
376 url_match_t *match = (url_match_t*) cursor->data;
377 gchar *url = g_strndup (txt->str + match->offset, match->len);
378 gchar *repl = NULL; /* replacement */
380 /* the prefix is NULL: use the one that is already there */
381 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
382 match->prefix ? match->prefix : "", url, url);
384 /* replace the old thing with our hyperlink
385 * replacement thing */
386 g_string_erase (txt, match->offset, match->len);
387 g_string_insert (txt, match->offset, repl);
392 g_free (cursor->data);
395 g_slist_free (match_list);
401 convert_to_html (const gchar *data)
404 gboolean first_space = TRUE;
412 html = g_string_sized_new (len + 100); /* just a guess... */
414 g_string_append_printf (html,
417 "<meta http-equiv=\"content-type\""
418 " content=\"text/html; charset=utf8\">"
422 /* replace with special html chars where needed*/
423 for (i = 0; i != len; ++i) {
427 case 0: break; /* ignore embedded \0s */
428 case '<' : g_string_append (html, "<"); break;
429 case '>' : g_string_append (html, ">"); break;
430 case '&' : g_string_append (html, """); break;
431 case '\n': g_string_append (html, "<br>\n"); break;
434 g_string_append (html, first_space ? " " : " ");
436 } else if (kar == '\t')
437 g_string_append (html, " ");
441 /* optimization trick: accumulate 'normal' chars, then copy */
443 kar = data [++charnum + i];
445 } while ((i + charnum < len) &&
446 (kar > '>' || (kar != '<' && kar != '>'
447 && kar != '&' && kar != ' '
448 && kar != '\n' && kar != '\t')));
449 g_string_append_len (html, &data[i], charnum);
455 g_string_append (html, "</tt></body></html>");
456 hyperlinkify_plain_text (html);
458 return g_string_free (html, FALSE);
465 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
467 return match2->offset - match1->offset;
473 * check if the match is inside an existing match... */
475 chk_partial_match (const url_match_t *match, int* offset)
477 if (*offset >= match->offset && *offset < match->offset + match->len)
482 get_url_matches (GString *txt)
485 int rv, i, offset = 0;
486 GSList *match_list = NULL;
488 static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
489 const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
491 /* initalize the regexps */
492 for (i = 0; i != pattern_num; ++i) {
493 patterns[i].preg = g_new0 (regex_t,1);
494 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
495 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
497 /* find all the matches */
498 for (i = 0; i != pattern_num; ++i) {
502 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
503 g_assert (rv == REG_NOMATCH); /* this should not happen */
504 break; /* try next regexp */
509 /* FIXME: optimize this */
510 /* to avoid partial matches on something that was already found... */
511 /* check_partial_match will put -1 in the data ptr if that is the case */
512 test_offset = offset + rm.rm_so;
513 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
515 /* make a list of our matches (<offset, len, prefix> tupels)*/
516 if (test_offset != -1) {
517 url_match_t *match = g_new (url_match_t,1);
518 match->offset = offset + rm.rm_so;
519 match->len = rm.rm_eo - rm.rm_so;
520 match->prefix = patterns[i].prefix;
521 match_list = g_slist_prepend (match_list, match);
527 for (i = 0; i != pattern_num; ++i) {
528 regfree (patterns[i].preg);
529 g_free (patterns[i].preg);
530 } /* don't free patterns itself -- it's static */
532 /* now sort the list, so the matches are in reverse order of occurence.
533 * that way, we can do the replacements starting from the end, so we don't need
534 * to recalculate the offsets
536 match_list = g_slist_sort (match_list,
537 (GCompareFunc)cmp_offsets_reverse);
544 set_html_message (ModestTnyMsgView *self, const TnyMsgMimePartIface *tny_body,
545 const TnyMsgIface *msg)
547 gchar *html_attachments;
548 TnyStreamIface *gtkhtml_stream;
549 ModestTnyMsgViewPrivate *priv;
551 g_return_val_if_fail (self, FALSE);
552 g_return_val_if_fail (tny_body, FALSE);
554 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
557 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
558 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
560 tny_stream_iface_reset (gtkhtml_stream);
562 html_attachments = attachments_as_html(self, msg);
563 if (html_attachments) {
564 tny_stream_iface_write (gtkhtml_stream, html_attachments,
565 strlen(html_attachments));
566 tny_stream_iface_reset (gtkhtml_stream);
567 g_free (html_attachments);
571 tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)tny_body,
574 g_object_unref (G_OBJECT(gtkhtml_stream));
580 /* this is a hack --> we use the tny_text_buffer_stream to
581 * get the message text, then write to gtkhtml 'by hand' */
583 set_text_message (ModestTnyMsgView *self, const TnyMsgMimePartIface *tny_body,
584 const TnyMsgIface *msg)
587 GtkTextIter begin, end;
588 TnyStreamIface* txt_stream, *gtkhtml_stream;
589 gchar *txt, *html_attachments;
590 ModestTnyMsgViewPrivate *priv;
592 g_return_val_if_fail (self, FALSE);
593 g_return_val_if_fail (tny_body, FALSE);
595 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
597 buf = gtk_text_buffer_new (NULL);
598 txt_stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
600 tny_stream_iface_reset (txt_stream);
603 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
604 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
606 html_attachments = attachments_as_html(self, msg);
607 if (html_attachments) {
608 tny_stream_iface_write (gtkhtml_stream, html_attachments,
609 strlen(html_attachments));
610 tny_stream_iface_reset (gtkhtml_stream);
611 g_free (html_attachments);
615 tny_msg_mime_part_iface_decode_to_stream ((TnyMsgMimePartIface*)tny_body,
617 tny_stream_iface_reset (txt_stream);
619 gtk_text_buffer_get_bounds (buf, &begin, &end);
620 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
622 gchar *html = convert_to_html (txt);
623 tny_stream_iface_write (gtkhtml_stream, html, strlen(html));
624 tny_stream_iface_reset (gtkhtml_stream);
629 g_object_unref (G_OBJECT(gtkhtml_stream));
630 g_object_unref (G_OBJECT(txt_stream));
631 g_object_unref (G_OBJECT(buf));
638 set_empty_message (ModestTnyMsgView *self)
640 ModestTnyMsgViewPrivate *priv;
642 g_return_val_if_fail (self, FALSE);
643 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
645 gtk_html_load_from_string (priv->gtkhtml, "", 1);
652 modest_tny_msg_view_get_selected_text (ModestTnyMsgView *self)
654 ModestTnyMsgViewPrivate *priv;
660 g_return_val_if_fail (self, NULL);
661 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
662 html = priv->gtkhtml;
664 /* I'm sure there is a better way to check for selected text */
665 sel = gtk_html_get_selection_html(GTK_HTML(html), &len);
671 clip = gtk_widget_get_clipboard(html, GDK_SELECTION_PRIMARY);
672 return gtk_clipboard_wait_for_text(clip);
677 modest_tny_msg_view_set_message (ModestTnyMsgView *self, const TnyMsgIface *msg)
679 TnyMsgMimePartIface *body;
680 ModestTnyMsgViewPrivate *priv;
682 g_return_if_fail (self);
684 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
689 set_empty_message (self);
693 body = modest_tny_msg_actions_find_body_part (msg, TRUE);
695 if (tny_msg_mime_part_iface_content_type_is (body, "text/html"))
696 set_html_message (self, body, msg);
698 set_text_message (self, body, msg);
701 set_empty_message (self);