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);
23 * we need these regexps to find URLs in plain text e-mails
25 typedef struct _UrlMatchPattern UrlMatchPattern;
26 struct _UrlMatchPattern {
31 #define MAIL_VIEWER_URL_MATCH_PATTERNS {\
32 { "(file|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
34 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
36 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
38 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
40 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
42 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
54 typedef struct _ModestTnyMsgViewPrivate ModestTnyMsgViewPrivate;
55 struct _ModestTnyMsgViewPrivate {
58 #define MODEST_TNY_MSG_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59 MODEST_TYPE_TNY_MSG_VIEW, \
60 ModestTnyMsgViewPrivate))
62 static GtkContainerClass *parent_class = NULL;
64 /* uncomment the following if you have defined any signals */
65 /* static guint signals[LAST_SIGNAL] = {0}; */
68 modest_tny_msg_view_get_type (void)
70 static GType my_type = 0;
72 static const GTypeInfo my_info = {
73 sizeof(ModestTnyMsgViewClass),
75 NULL, /* base finalize */
76 (GClassInitFunc) modest_tny_msg_view_class_init,
77 NULL, /* class finalize */
78 NULL, /* class data */
79 sizeof(ModestTnyMsgView),
81 (GInstanceInitFunc) modest_tny_msg_view_init,
83 my_type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
91 modest_tny_msg_view_class_init (ModestTnyMsgViewClass *klass)
93 GObjectClass *gobject_class;
94 gobject_class = (GObjectClass*) klass;
96 parent_class = g_type_class_peek_parent (klass);
97 gobject_class->finalize = modest_tny_msg_view_finalize;
99 g_type_class_add_private (gobject_class, sizeof(ModestTnyMsgViewPrivate));
103 modest_tny_msg_view_init (ModestTnyMsgView *obj)
105 ModestTnyMsgViewPrivate *priv;
107 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(obj),
108 GTK_POLICY_AUTOMATIC,
109 GTK_POLICY_AUTOMATIC);
112 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
113 priv->gtkhtml = gtk_html_new();
114 gtk_html_set_editable (GTK_HTML(priv->gtkhtml), FALSE);
115 gtk_html_allow_selection (GTK_HTML(priv->gtkhtml), TRUE);
116 gtk_html_set_caret_mode (GTK_HTML(priv->gtkhtml), TRUE);
117 gtk_html_set_blocking (GTK_HTML(priv->gtkhtml), FALSE);
118 gtk_html_set_images_blocking (GTK_HTML(priv->gtkhtml), FALSE);
120 gtk_widget_show (priv->gtkhtml);
121 gtk_container_add (GTK_CONTAINER(obj), priv->gtkhtml);
126 modest_tny_msg_view_finalize (GObject *obj)
128 ModestTnyMsgViewPrivate *priv;
129 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(obj);
132 g_object_unref (G_OBJECT(priv->gtkhtml));
136 modest_tny_msg_view_new (TnyMsgIface *msg)
139 ModestTnyMsgView* self;
141 obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_MSG_VIEW, NULL));
142 self = MODEST_TNY_MSG_VIEW(obj);
145 modest_tny_msg_view_set_message (self, msg);
147 return GTK_WIDGET(self);
161 hyperlinkify_plain_text (GString *txt)
164 GSList *match_list = get_url_matches (txt);
166 /* we will work backwards, so the offsets stay valid */
167 for (cursor = match_list; cursor; cursor = cursor->next) {
169 url_match_t *match = (url_match_t*) cursor->data;
170 gchar *url = g_strndup (txt->str + match->offset, match->len);
171 gchar *repl = NULL; /* replacement */
173 /* the prefix is NULL: use the one that is already there */
174 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
175 match->prefix ? match->prefix : "", url, url);
177 /* replace the old thing with our hyperlink
178 * replacement thing */
179 g_string_erase (txt, match->offset, match->len);
180 g_string_insert (txt, match->offset, repl);
186 g_slist_free (match_list);
192 convert_to_html (gchar *data)
195 gboolean first_space = TRUE;
203 html = g_string_sized_new (len + 100); /* just a guess... */
205 g_string_append_printf (html,
208 "<meta http-equiv=\"content-type\""
209 " content=\"text/html; charset=utf8\">"
213 /* replace with special html chars where needed*/
214 for (i = 0; i != len; ++i) {
218 case 0: break; /* ignore embedded \0s */
219 case '<' : g_string_append (html, "<"); break;
220 case '>' : g_string_append (html, ">"); break;
221 case '&' : g_string_append (html, """); break;
222 case '\n': g_string_append (html, "<br>\n"); break;
225 g_string_append (html, first_space ? " " : " ");
227 } else if (kar == '\t')
228 g_string_append (html, " ");
232 /* optimization trick: accumulate 'normal' chars, then copy */
234 kar = data [++charnum + i];
236 } while ((i + charnum < len) &&
237 (kar > '>' || (kar != '<' && kar != '>'
238 && kar != '&' && kar != ' '
239 && kar != '\n' && kar != '\t')));
240 g_string_append_len (html, &data[i], charnum);
246 g_string_append (html, "</tt></body></html>");
247 hyperlinkify_plain_text (html);
249 return g_string_free (html, FALSE);
256 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
258 return match2->offset - match1->offset;
264 * check if the match is inside an existing match... */
266 chk_partial_match (const url_match_t *match, int* offset)
268 if (*offset >= match->offset && *offset < match->offset + match->len)
273 get_url_matches (GString *txt)
276 int rv, i, offset = 0;
277 GSList *match_list = NULL;
279 static UrlMatchPattern patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
280 const size_t pattern_num = sizeof(patterns)/sizeof(UrlMatchPattern);
282 /* initalize the regexps */
283 for (i = 0; i != pattern_num; ++i) {
284 patterns[i].preg = g_new0 (regex_t,1);
285 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
286 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
288 /* find all the matches */
289 for (i = 0; i != pattern_num; ++i) {
293 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
294 g_assert (rv == REG_NOMATCH); /* this should not happen */
295 break; /* try next regexp */
300 /* FIXME: optimize this */
301 /* to avoid partial matches on something that was already found... */
302 /* check_partial_match will put -1 in the data ptr if that is the case */
303 test_offset = offset + rm.rm_so;
304 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
306 /* make a list of our matches (<offset, len, prefix> tupels)*/
307 if (test_offset != -1) {
308 url_match_t *match = g_new (url_match_t,1);
309 match->offset = offset + rm.rm_so;
310 match->len = rm.rm_eo - rm.rm_so;
311 match->prefix = patterns[i].prefix;
312 match_list = g_slist_prepend (match_list, match);
318 for (i = 0; i != pattern_num; ++i) {
319 regfree (patterns[i].preg);
320 g_free (patterns[i].preg);
321 } /* don't free patterns itself -- it's static */
323 /* now sort the list, so the matches are in reverse order of occurence.
324 * that way, we can do the replacements starting from the end, so we don't need
325 * to recalculate the offsets
327 match_list = g_slist_sort (match_list,
328 (GCompareFunc)cmp_offsets_reverse);
333 fill_gtkhtml_with_txt (GtkHTML* gtkhtml, gchar* txt)
337 g_return_val_if_fail (gtkhtml, FALSE);
338 g_return_val_if_fail (txt, FALSE);
340 html = convert_to_html (txt);
341 gtk_html_load_from_string (gtkhtml, html, strlen(html));
349 static TnyMsgMimePartIface *
350 find_body_part (TnyMsgIface *msg, const gchar *mime_type)
352 TnyMsgMimePartIface *part = NULL;
355 g_return_val_if_fail (msg, NULL);
356 g_return_val_if_fail (mime_type, NULL);
358 parts = (GList*) tny_msg_iface_get_parts (msg);
359 while (parts && !part) {
360 part = TNY_MSG_MIME_PART_IFACE(parts->data);
361 if (!tny_msg_mime_part_iface_content_type_is (part, mime_type))
370 set_html_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
372 TnyStreamIface *gtkhtml_stream;
373 ModestTnyMsgViewPrivate *priv;
375 g_return_val_if_fail (self, FALSE);
376 g_return_val_if_fail (tny_body, FALSE);
378 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
381 TNY_STREAM_IFACE(modest_tny_stream_gtkhtml_new(GTK_HTML(priv->gtkhtml)));
383 tny_stream_iface_reset (gtkhtml_stream);
384 tny_msg_mime_part_iface_decode_to_stream (tny_body, gtkhtml_stream);
385 tny_stream_iface_reset (gtkhtml_stream);
387 g_object_unref (G_OBJECT(gtkhtml_stream));
393 /* this is a hack --> we use the tny_text_buffer_stream to
394 * get the message text, then write to gtkhtml 'by hand' */
396 set_text_message (ModestTnyMsgView *self, TnyMsgMimePartIface *tny_body)
399 GtkTextIter begin, end;
400 TnyStreamIface* txt_stream;
402 ModestTnyMsgViewPrivate *priv;
404 g_return_val_if_fail (self, FALSE);
405 g_return_val_if_fail (tny_body, FALSE);
407 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
409 buf = gtk_text_buffer_new (NULL);
410 txt_stream = TNY_STREAM_IFACE(tny_text_buffer_stream_new (buf));
412 tny_stream_iface_reset (txt_stream);
413 tny_msg_mime_part_iface_decode_to_stream (tny_body, txt_stream);
414 tny_stream_iface_reset (txt_stream);
416 gtk_text_buffer_get_bounds (buf, &begin, &end);
417 txt = gtk_text_buffer_get_text (buf, &begin, &end, FALSE);
419 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), txt);
421 g_object_unref (G_OBJECT(txt_stream));
422 g_object_unref (G_OBJECT(buf));
431 modest_tny_msg_view_set_message (ModestTnyMsgView *self, TnyMsgIface *msg)
433 TnyMsgMimePartIface *body;
434 ModestTnyMsgViewPrivate *priv;
436 g_return_if_fail (self);
438 priv = MODEST_TNY_MSG_VIEW_GET_PRIVATE(self);
441 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml), "");
445 body = find_body_part (msg, "text/html");
447 set_html_message (self, body);
451 body = find_body_part (msg, "text/plain");
453 set_text_message (self, body);
458 fill_gtkhtml_with_txt (GTK_HTML(priv->gtkhtml),
459 _("Unsupported message type"));