1 /* modest-tny-msg-view.c */
3 /* insert (c)/licensing information) */
5 #include "modest-tny-msg-view.h"
6 #include "modest-tny-stream-gtkhtml.h"
7 #include <tny-text-buffer-stream.h>
11 #include <glib/gi18n.h>
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);
19 static GSList* get_url_matches (GString *txt);
20 static gboolean fill_gtkhtml_with_txt (GtkHTML* gtkhtml, const gchar* txt);
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);
30 * we need these regexps to find URLs in plain text e-mails
32 typedef struct _UrlMatchPattern UrlMatchPattern;
33 struct _UrlMatchPattern {
39 #define MAIL_VIEWER_URL_MATCH_PATTERNS {\
40 { "(file|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
42 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
44 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
46 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
48 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
50 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
62 typedef struct _ModestTnyMsgViewPrivate ModestTnyMsgViewPrivate;
63 struct _ModestTnyMsgViewPrivate {
67 #define MODEST_TNY_MSG_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
68 MODEST_TYPE_TNY_MSG_VIEW, \
69 ModestTnyMsgViewPrivate))
71 static GtkContainerClass *parent_class = NULL;
73 /* uncomment the following if you have defined any signals */
74 /* static guint signals[LAST_SIGNAL] = {0}; */
77 modest_tny_msg_view_get_type (void)
79 static GType my_type = 0;
81 static const GTypeInfo my_info = {
82 sizeof(ModestTnyMsgViewClass),
84 NULL, /* base finalize */
85 (GClassInitFunc) modest_tny_msg_view_class_init,
86 NULL, /* class finalize */
87 NULL, /* class data */
88 sizeof(ModestTnyMsgView),
90 (GInstanceInitFunc) modest_tny_msg_view_init,
92 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
100 modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass)
102 GObjectClass *gobject_class;
103 gobject_class = (GObjectClass*) klass;
105 parent_class = g_type_class_peek_parent (klass);
106 gobject_class->finalize = modest_tny_msg_view_finalize;
108 g_type_class_add_private (gobject_class, sizeof(ModestTnyMsgViewPrivate));
112 modest_tny_msg_view_init (ModestTnyMsgView *obj)
114 ModestTnyMsgViewPrivate *priv;
116 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
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);
127 g_signal_connect (G_OBJECT(priv->gtkhtml), "link_clicked",
128 G_CALLBACK(on_link_clicked), obj);
130 g_signal_connect (G_OBJECT(priv->gtkhtml), "url_requested",
131 G_CALLBACK(on_url_requested), obj);
136 modest_tny_msg_view_finalize (GObject *obj)
138 ModestTnyMsgViewPrivate *priv;
139 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
142 g_object_unref (G_OBJECT(priv->gtkhtml));
146 modest_tny_msg_view_new (TnyMsgIface *msg)
149 ModestTnyMsgView* self;
150 ModestTnyMsgViewPrivate *priv;
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);
156 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(self),
157 GTK_POLICY_AUTOMATIC,
158 GTK_POLICY_AUTOMATIC);
161 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
164 modest_tny_msg_view_set_message (self, msg);
166 return GTK_WIDGET(self);
172 on_link_clicked (GtkWidget *widget, const gchar *uri,
173 ModestTnyMsgView *msg_view)
175 g_message ("link clicked: %s", uri); /* FIXME */
180 static TnyMsgMimePartIface *
181 find_cid_image (TnyMsgIface *msg, const gchar *cid)
183 TnyMsgMimePartIface *part = NULL;
186 g_return_val_if_fail (msg, NULL);
187 g_return_val_if_fail (cid, NULL);
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! */
206 on_url_requested (GtkWidget *widget, const gchar *uri,
207 GtkHTMLStream *stream,
208 ModestTnyMsgView *msg_view)
211 ModestTnyMsgViewPrivate *priv;
212 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE (msg_view);
214 g_message ("url requested: %s", uri);
216 if (g_str_has_prefix (uri, "cid:")) {
217 /* +4 ==> skip "cid:" */
219 TnyMsgMimePartIface *part = find_cid_image (priv->msg, uri + 4);
221 g_message ("%s not found", uri + 4);
222 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
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);
244 hyperlinkify_plain_text (GString *txt)
247 GSList *match_list = get_url_matches (txt);
249 /* we will work backwards, so the offsets stay valid */
250 for (cursor = match_list; cursor; cursor = cursor->next) {
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 */
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);
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);
269 g_slist_free (match_list);
275 convert_to_html (const gchar *data)
278 gboolean first_space = TRUE;
286 html = g_string_sized_new (len + 100); /* just a guess... */
288 g_string_append_printf (html,
291 "<meta http-equiv=\"content-type\""
292 " content=\"text/html; charset=utf8\">"
296 /* replace with special html chars where needed*/
297 for (i = 0; i != len; ++i) {
301 case 0: break; /* ignore embedded \0s */
302 case '<' : g_string_append (html, "<"); break;
303 case '>' : g_string_append (html, ">"); break;
304 case '&' : g_string_append (html, """); break;
305 case '\n': g_string_append (html, "<br>\n"); break;
308 g_string_append (html, first_space ? " " : " ");
310 } else if (kar == '\t')
311 g_string_append (html, " ");
315 /* optimization trick: accumulate 'normal' chars, then copy */
317 kar = data [++charnum + i];
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);
329 g_string_append (html, "</tt></body></html>");
330 hyperlinkify_plain_text (html);
332 return g_string_free (html, FALSE);
339 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
341 return match2->offset - match1->offset;
347 * check if the match is inside an existing match... */
349 chk_partial_match (const url_match_t *match, int* offset)
351 if (*offset >= match->offset && *offset < match->offset + match->len)
356 get_url_matches (GString *txt)
359 int rv, i, offset = 0;
360 GSList *match_list = NULL;
362 static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
363 const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
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);
371 /* find all the matches */
372 for (i = 0; i != pattern_num; ++i) {
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 */
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);
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);
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 */
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
410 match_list = g_slist_sort (match_list,
411 (GCompareFunc)cmp_offsets_reverse);
416 fill_gtkhtml_with_txt (GtkHTML* gtkhtml, const gchar* txt)
420 g_return_val_if_fail (gtkhtml, FALSE);
421 g_return_val_if_fail (txt, FALSE);
423 html = convert_to_html (txt);
424 gtk_html_load_from_string (gtkhtml, html, strlen(html));
432 static TnyMsgMimePartIface *
433 find_body_part (TnyMsgIface *msg, const gchar *mime_type)
435 TnyMsgMimePartIface *part = NULL;
438 g_return_val_if_fail (msg, NULL);
439 g_return_val_if_fail (mime_type, NULL);
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))
453 set_html_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
455 TnyStreamIface *gtkhtml_stream;
456 ModestTnyMsgViewPrivate *priv;
458 g_return_val_if_fail (self, FALSE);
459 g_return_val_if_fail (tny_body, FALSE);
461 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
464 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new
465 (gtk_html_begin(GTK_HTML(priv->gtkhtml))));
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);
471 g_object_unref (G_OBJECT(gtkhtml_stream));
477 /* this is a hack --> we use the tny_text_buffer_stream to
478 * get the message text, then write to gtkhtml 'by hand' */
480 set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
483 GtkTextIter begin, end;
484 TnyStreamIface* txt_stream;
486 ModestTnyMsgViewPrivate *priv;
488 g_return_val_if_fail (self, FALSE);
489 g_return_val_if_fail (tny_body, FALSE);
491 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
493 buf = gtk_text_buffer_new (NULL);
494 txt_stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
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);
500 gtk_text_buffer_get_bounds (buf, &begin, &end);
501 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
503 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), txt);
505 g_object_unref (G_OBJECT(txt_stream));
506 g_object_unref (G_OBJECT(buf));
515 modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg)
517 TnyMsgMimePartIface *body;
518 ModestTnyMsgViewPrivate *priv;
520 g_return_if_fail (self);
522 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
526 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), "");
531 body = find_body_part (msg, "text/html");
533 set_html_message (self, body);
537 body = find_body_part (msg, "text/plain");
539 set_text_message (self, body);
544 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml),
545 _("Unsupported message type"));