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.
34 #endif /*_GNU_SOURCE*/
35 #include <string.h> /* for strcasestr */
40 #include <glib/gi18n.h>
42 #include <modest-tny-platform-factory.h>
43 #include <modest-text-utils.h>
44 #include <modest-runtime.h>
49 #endif /*HAVE_CONFIG_H */
52 #define FORWARD_STRING _("mcen_ia_editor_original_message")
53 #define FROM_STRING _("mail_va_from")
54 #define SENT_STRING _("mcen_fi_message_properties_sent")
55 #define TO_STRING _("mail_va_to")
56 #define SUBJECT_STRING _("mail_va_subject")
57 #define EMPTY_STRING ""
60 * do the hyperlinkification only for texts < 50 Kb,
61 * as it's quite slow. Without this, e.g. mail with
62 * an uuencoded part (which is not recognized as attachment,
65 #define HYPERLINKIFY_MAX_LENGTH (1024*50)
69 * we mark the ampersand with \007 when converting text->html
70 * because after text->html we do hyperlink detecting, which
71 * could be screwed up by the ampersand
73 #define MARK_AMP '\007'
74 #define MARK_AMP_STR "\007"
78 * we need these regexps to find URLs in plain text e-mails
80 typedef struct _url_match_pattern_t url_match_pattern_t;
81 struct _url_match_pattern_t {
87 typedef struct _url_match_t url_match_t;
94 #define MAIL_VIEWER_URL_MATCH_PATTERNS { \
95 { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
97 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
99 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
101 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
103 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
105 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
109 const gchar account_title_forbidden_chars[] = {
110 '\\', '/', ':', '*', '?', '\'', '<', '>', '|', '^'
112 const gchar folder_name_forbidden_chars[] = {
113 '<', '>', ':', '\'', '/', '\\', '|', '?', '*', '^', '%', '$', '#', '&'
115 const gchar user_name_forbidden_chars[] = {
118 const guint ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH = G_N_ELEMENTS (account_title_forbidden_chars);
119 const guint FOLDER_NAME_FORBIDDEN_CHARS_LENGTH = G_N_ELEMENTS (folder_name_forbidden_chars);
120 const guint USER_NAME_FORBIDDEN_CHARS_LENGTH = G_N_ELEMENTS (user_name_forbidden_chars);
123 static gchar* cite (const time_t sent_date, const gchar *from);
124 static void hyperlinkify_plain_text (GString *txt);
125 static gint cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2);
126 static GSList* get_url_matches (GString *txt);
128 static GString* get_next_line (const char *b, const gsize blen, const gchar * iter);
129 static int get_indent_level (const char *l);
130 static void unquote_line (GString * l);
131 static void append_quoted (GString * buf, const int indent, const GString * str,
133 static int get_breakpoint_utf8 (const gchar * s, const gint indent, const gint limit);
134 static int get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit);
135 static int get_breakpoint (const gchar * s, const gint indent, const gint limit);
137 static gchar* modest_text_utils_quote_plain_text (const gchar *text,
139 const gchar *signature,
143 static gchar* modest_text_utils_quote_html (const gchar *text,
145 const gchar *signature,
148 static gchar* get_email_from_address (const gchar *address);
151 /* ******************************************************************* */
152 /* ************************* PUBLIC FUNCTIONS ************************ */
153 /* ******************************************************************* */
156 modest_text_utils_quote (const gchar *text,
157 const gchar *content_type,
158 const gchar *signature,
160 const time_t sent_date,
164 gchar *retval, *cited;
166 g_return_val_if_fail (text, NULL);
167 g_return_val_if_fail (content_type, NULL);
169 cited = cite (sent_date, from);
171 if (content_type && strcmp (content_type, "text/html") == 0)
172 /* TODO: extract the <body> of the HTML and pass it to
174 retval = modest_text_utils_quote_html (text, cited, signature, attachments, limit);
176 retval = modest_text_utils_quote_plain_text (text, cited, signature, attachments, limit);
185 modest_text_utils_cite (const gchar *text,
186 const gchar *content_type,
187 const gchar *signature,
194 g_return_val_if_fail (text, NULL);
195 g_return_val_if_fail (content_type, NULL);
198 retval = g_strdup ("");
199 else if (strcmp(content_type, "text/html") == 0) {
200 tmp_sig = g_strconcat ("\n", signature, NULL);
201 retval = modest_text_utils_convert_to_html_body(tmp_sig, -1, TRUE);
204 retval = g_strconcat (text, "\n", signature, NULL);
211 forward_cite (const gchar *from,
214 const gchar *subject)
216 return g_strdup_printf ("%s\n%s %s\n%s %s\n%s %s\n%s %s\n",
218 FROM_STRING, (from)?from:"",
220 TO_STRING, (to)?to:"",
221 SUBJECT_STRING, (subject)?subject:"");
225 modest_text_utils_inline (const gchar *text,
226 const gchar *content_type,
227 const gchar *signature,
231 const gchar *subject)
237 g_return_val_if_fail (text, NULL);
238 g_return_val_if_fail (content_type, NULL);
240 modest_text_utils_strftime (sent_str, 100, "%c", sent_date);
242 cited = forward_cite (from, sent_str, to, subject);
244 if (content_type && strcmp (content_type, "text/html") == 0)
245 retval = modest_text_utils_quote_html (text, cited, signature, NULL, 80);
247 retval = modest_text_utils_quote_plain_text (text, cited, signature, NULL, 80);
253 /* just to prevent warnings:
254 * warning: `%x' yields only last 2 digits of year in some locales
257 modest_text_utils_strftime(char *s, gsize max, const char *fmt, time_t timet)
261 /* does not work on old maemo glib:
262 * g_date_set_time_t (&date, timet);
264 localtime_r (&timet, &tm);
265 return strftime(s, max, fmt, &tm);
269 modest_text_utils_derived_subject (const gchar *subject, const gchar *prefix)
273 g_return_val_if_fail (prefix, NULL);
275 if (!subject || subject[0] == '\0')
276 subject = _("mail_va_no_subject");
278 tmp = g_strchug (g_strdup (subject));
280 if (!strncmp (tmp, prefix, strlen (prefix))) {
284 return g_strdup_printf ("%s %s", prefix, subject);
289 modest_text_utils_remove_address (const gchar *address_list, const gchar *address)
291 gchar *dup, *token, *ptr = NULL, *result;
292 GString *filtered_emails;
293 gchar *email_address;
295 g_return_val_if_fail (address_list, NULL);
298 return g_strdup (address_list);
300 email_address = get_email_from_address (address);
302 /* search for substring */
303 if (!strstr ((const char *) address_list, (const char *) email_address)) {
304 g_free (email_address);
305 return g_strdup (address_list);
308 dup = g_strdup (address_list);
309 filtered_emails = g_string_new (NULL);
311 token = strtok_r (dup, ",", &ptr);
313 while (token != NULL) {
314 /* Add to list if not found */
315 if (!strstr ((const char *) token, (const char *) email_address)) {
316 if (filtered_emails->len == 0)
317 g_string_append_printf (filtered_emails, "%s", g_strstrip (token));
319 g_string_append_printf (filtered_emails, ",%s", g_strstrip (token));
321 token = strtok_r (NULL, ",", &ptr);
323 result = filtered_emails->str;
326 g_free (email_address);
328 g_string_free (filtered_emails, FALSE);
335 modest_text_utils_remove_duplicate_addresses (const gchar *address_list)
337 GSList *addresses, *cursor;
341 g_return_val_if_fail (address_list, NULL);
343 table = g_hash_table_new (g_str_hash, g_str_equal);
344 addresses = modest_text_utils_split_addresses_list (address_list);
346 new_list = g_strdup("");
349 const gchar* address = (const gchar*)cursor->data;
351 /* ignore the address if already seen */
352 if (g_hash_table_lookup (table, address) == 0) {
354 gchar *tmp = g_strjoin (",", new_list, address, NULL);
358 g_hash_table_insert (table, (gchar*)address, GINT_TO_POINTER(1));
360 cursor = g_slist_next (cursor);
363 g_hash_table_destroy (table);
364 g_slist_foreach (addresses, (GFunc)g_free, NULL);
365 g_slist_free (addresses);
372 modest_text_utils_convert_buffer_to_html_start (GString *html, const gchar *data, gssize n)
375 gboolean space_seen = FALSE;
376 guint break_dist = 0; /* distance since last break point */
381 /* replace with special html chars where needed*/
382 for (i = 0; i != n; ++i) {
385 if (space_seen && kar != ' ') {
386 g_string_append_c (html, ' ');
390 /* we artificially insert a breakpoint (newline)
391 * after 256, to make sure our lines are not so long
392 * they will DOS the regexping later
394 if (++break_dist == 256) {
395 g_string_append_c (html, '\n');
401 case MARK_AMP: /* this is a temp place holder for '&'; we can only
402 * set the real '&' after hyperlink translation, otherwise
403 * we might screw that up */
404 break; /* ignore embedded \0s and MARK_AMP */
405 case '<' : g_string_append (html, MARK_AMP_STR "lt;"); break;
406 case '>' : g_string_append (html, MARK_AMP_STR "gt;"); break;
407 case '&' : g_string_append (html, MARK_AMP_STR "amp;"); break;
408 case '"' : g_string_append (html, MARK_AMP_STR "quot;"); break;
410 /* don't convert ' --> wpeditor will try to re-convert it... */
411 //case '\'' : g_string_append (html, "'"); break;
412 case '\n' : g_string_append (html, "<br>\n");break_dist= 0; break;
413 case '\t' : g_string_append (html, MARK_AMP_STR "nbsp;" MARK_AMP_STR "nbsp;" MARK_AMP_STR "nbsp; ");
414 break_dist=0; break; /* note the space at the end*/
417 if (space_seen) { /* second space in a row */
418 g_string_append (html, " ");
424 g_string_append_c (html, kar);
431 modest_text_utils_convert_buffer_to_html_finish (GString *html)
434 /* replace all our MARK_AMPs with real ones */
435 for (i = 0; i != html->len; ++i)
436 if ((html->str)[i] == MARK_AMP)
437 (html->str)[i] = '&';
442 modest_text_utils_convert_to_html (const gchar *data)
447 g_return_val_if_fail (data, NULL);
453 html = g_string_sized_new (1.5 * len); /* just a guess... */
455 g_string_append_printf (html,
457 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf8\">"
461 modest_text_utils_convert_buffer_to_html_start (html, data, -1);
463 g_string_append (html, "</body></html>");
465 if (len <= HYPERLINKIFY_MAX_LENGTH)
466 hyperlinkify_plain_text (html);
468 modest_text_utils_convert_buffer_to_html_finish (html);
470 return g_string_free (html, FALSE);
474 modest_text_utils_convert_to_html_body (const gchar *data, gssize n, gboolean hyperlinkify)
478 g_return_val_if_fail (data, NULL);
485 html = g_string_sized_new (1.5 * n); /* just a guess... */
487 modest_text_utils_convert_buffer_to_html_start (html, data, n);
489 if (hyperlinkify && (n < HYPERLINKIFY_MAX_LENGTH))
490 hyperlinkify_plain_text (html);
492 modest_text_utils_convert_buffer_to_html_finish (html);
494 return g_string_free (html, FALSE);
498 modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes)
500 gchar *current, *start, *last_blank;
501 gint start_offset = 0, current_offset = 0;
503 g_return_if_fail (start_indexes != NULL);
504 g_return_if_fail (end_indexes != NULL);
506 start = (gchar *) addresses;
510 while (*current != '\0') {
511 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
512 start = g_utf8_next_char (start);
514 last_blank = current;
515 } else if ((*current == ',')||(*current == ';')) {
516 gint *start_index, *end_index;
517 start_index = g_new0(gint, 1);
518 end_index = g_new0(gint, 1);
519 *start_index = start_offset;
520 *end_index = current_offset;
521 *start_indexes = g_slist_prepend (*start_indexes, start_index);
522 *end_indexes = g_slist_prepend (*end_indexes, end_index);
523 start = g_utf8_next_char (current);
524 start_offset = current_offset + 1;
526 } else if (*current == '"') {
527 current = g_utf8_next_char (current);
529 while ((*current != '"')&&(*current != '\0')) {
530 current = g_utf8_next_char (current);
535 current = g_utf8_next_char (current);
539 if (start != current) {
540 gint *start_index, *end_index;
541 start_index = g_new0(gint, 1);
542 end_index = g_new0(gint, 1);
543 *start_index = start_offset;
544 *end_index = current_offset;
545 *start_indexes = g_slist_prepend (*start_indexes, start_index);
546 *end_indexes = g_slist_prepend (*end_indexes, end_index);
549 *start_indexes = g_slist_reverse (*start_indexes);
550 *end_indexes = g_slist_reverse (*end_indexes);
557 modest_text_utils_split_addresses_list (const gchar *addresses)
559 gchar *current, *start, *last_blank;
560 GSList *result = NULL;
562 start = (gchar *) addresses;
566 while (*current != '\0') {
567 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
568 start = g_utf8_next_char (start);
569 last_blank = current;
570 } else if ((*current == ',')||(*current == ';')) {
571 gchar *new_address = NULL;
572 new_address = g_strndup (start, current - last_blank);
573 result = g_slist_prepend (result, new_address);
574 start = g_utf8_next_char (current);
576 } else if (*current == '\"') {
577 if (current == start) {
578 current = g_utf8_next_char (current);
579 start = g_utf8_next_char (start);
581 while ((*current != '\"')&&(*current != '\0'))
582 current = g_utf8_next_char (current);
585 current = g_utf8_next_char (current);
588 if (start != current) {
589 gchar *new_address = NULL;
590 new_address = g_strndup (start, current - last_blank);
591 result = g_slist_prepend (result, new_address);
594 result = g_slist_reverse (result);
604 modest_text_utils_split_addresses_list (const gchar *addresses)
607 const gchar *my_addrs = addresses;
611 /* skip any space, ',', ';' at the start */
612 while (my_addrs && (my_addrs[0] == ' ' || my_addrs[0] == ',' || my_addrs[0] == ';'))
615 /* are we at the end of addresses list? */
619 /* nope, we are at the start of some address
620 * now, let's find the end of the address */
622 while (end[0] && end[0] != ',' && end[0] != ';')
625 /* we got the address; copy it and remove trailing whitespace */
626 addr = g_strndup (my_addrs, end - my_addrs);
629 head = g_slist_append (NULL, addr);
630 head->next = modest_text_utils_split_addresses_list (end); /* recurse */
637 modest_text_utils_address_range_at_position (const gchar *recipients_list,
642 gchar *current = NULL;
643 gint range_start = 0;
646 gboolean is_quoted = FALSE;
648 g_return_if_fail (recipients_list);
649 g_return_if_fail (position < g_utf8_strlen(recipients_list, -1));
652 for (current = (gchar *) recipients_list; *current != '\0';
653 current = g_utf8_find_next_char (current, NULL)) {
654 gunichar c = g_utf8_get_char (current);
656 if ((c == ',') && (!is_quoted)) {
657 if (index < position) {
658 range_start = index + 1;
662 } else if (c == '\"') {
663 is_quoted = !is_quoted;
664 } else if ((c == ' ') &&(range_start == index)) {
672 *start = range_start;
678 /* ******************************************************************* */
679 /* ************************* UTILIY FUNCTIONS ************************ */
680 /* ******************************************************************* */
683 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
689 return g_string_new("");
697 gs = g_string_new_len (i0, iter - i0);
701 get_indent_level (const char *l)
718 /* if we hit the signature marker "-- ", we return -(indent + 1). This
719 * stops reformatting.
721 if (strcmp (l, "-- ") == 0) {
729 unquote_line (GString * l)
744 g_string_erase (l, 0, p - l->str);
748 append_quoted (GString * buf, int indent, const GString * str,
753 indent = indent < 0 ? abs (indent) - 1 : indent;
754 for (i = 0; i <= indent; i++) {
755 g_string_append (buf, "> ");
758 g_string_append_len (buf, str->str, cutpoint);
760 g_string_append (buf, str->str);
762 g_string_append (buf, "\n");
766 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
769 const gchar *pos, *last;
772 indent = indent < 0 ? abs (indent) - 1 : indent;
776 uni = g_utf8_to_ucs4_fast (s, -1, NULL);
778 if ((index + 2 * indent > limit) && last) {
782 if (g_unichar_isspace (uni[index])) {
785 pos = g_utf8_next_char (pos);
793 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
798 if (last + 2 * indent < limit)
801 for (i = strlen (s); i > 0; i--) {
803 if (i + 2 * indent <= limit) {
814 get_breakpoint (const gchar * s, const gint indent, const gint limit)
817 if (g_utf8_validate (s, -1, NULL)) {
818 return get_breakpoint_utf8 (s, indent, limit);
819 } else { /* assume ASCII */
820 //g_warning("invalid UTF-8 in msg");
821 return get_breakpoint_ascii (s, indent, limit);
826 cite (const time_t sent_date, const gchar *from)
828 return g_strdup (_("mcen_ia_editor_original_message"));
832 quoted_attachments (GList *attachments)
835 GString *result = g_string_new ("");
836 for (node = attachments; node != NULL; node = g_list_next (node)) {
837 gchar *filename = (gchar *) node->data;
838 g_string_append_printf ( result, "%s %s\n", _("mcen_ia_editor_attach_filename"), filename);
841 return g_string_free (result, FALSE);
846 modest_text_utils_quote_plain_text (const gchar *text,
848 const gchar *signature,
853 gint indent, breakpoint, rem_indent = 0;
854 GString *q, *l, *remaining;
856 gchar *attachments_string = NULL;
858 /* remaining will store the rest of the line if we have to break it */
859 q = g_string_new ("\n");
860 q = g_string_append (q, cite);
861 q = g_string_append_c (q, '\n');
862 remaining = g_string_new ("");
867 l = get_next_line (text, len, iter);
868 iter = iter + l->len + 1;
869 indent = get_indent_level (l->str);
872 if (remaining->len) {
873 if (l->len && indent == rem_indent) {
874 g_string_prepend (l, " ");
875 g_string_prepend (l, remaining->str);
879 get_breakpoint (remaining->str,
882 append_quoted (q, rem_indent,
883 remaining, breakpoint);
884 g_string_erase (remaining, 0,
886 if (remaining->str[0] == ' ') {
887 g_string_erase (remaining, 0,
890 } while (remaining->len);
893 g_string_free (remaining, TRUE);
894 breakpoint = get_breakpoint (l->str, indent, limit);
895 remaining = g_string_new (l->str + breakpoint);
896 if (remaining->str[0] == ' ') {
897 g_string_erase (remaining, 0, 1);
900 append_quoted (q, indent, l, breakpoint);
901 g_string_free (l, TRUE);
902 } while ((iter < text + len) || (remaining->str[0]));
904 attachments_string = quoted_attachments (attachments);
905 q = g_string_append (q, attachments_string);
906 g_free (attachments_string);
908 if (signature != NULL) {
909 q = g_string_append_c (q, '\n');
910 q = g_string_append (q, signature);
913 return g_string_free (q, FALSE);
917 modest_text_utils_quote_html (const gchar *text,
919 const gchar *signature,
923 gchar *result = NULL;
924 gchar *signature_result = NULL;
925 const gchar *format = \
926 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" \
930 "<pre>%s<br/>%s<br/>%s</pre>\n" \
933 gchar *attachments_string = NULL;
934 gchar *q_attachments_string = NULL;
935 gchar *q_cite = NULL;
936 gchar *html_text = NULL;
938 if (signature == NULL)
939 signature_result = g_strdup ("");
941 signature_result = modest_text_utils_convert_to_html_body (signature, -1, TRUE);
943 attachments_string = quoted_attachments (attachments);
944 q_attachments_string = modest_text_utils_convert_to_html_body (attachments_string, -1, TRUE);
945 q_cite = modest_text_utils_convert_to_html_body (cite, -1, TRUE);
946 html_text = modest_text_utils_convert_to_html_body (text, -1, TRUE);
947 result = g_strdup_printf (format, signature_result, q_cite, html_text, q_attachments_string);
950 g_free (attachments_string);
951 g_free (q_attachments_string);
952 g_free (signature_result);
958 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
960 return match2->offset - match1->offset;
963 static gboolean url_matches_block = 0;
964 static url_match_pattern_t patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
971 const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
972 for (i = 0; i != pattern_num; ++i) {
973 patterns[i].preg = g_slice_new0 (regex_t);
975 /* this should not happen */
976 g_return_val_if_fail (regcomp (patterns[i].preg, patterns[i].regex,
977 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0, FALSE);
986 const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
987 for (i = 0; i != pattern_num; ++i) {
988 regfree (patterns[i].preg);
989 g_slice_free (regex_t, patterns[i].preg);
990 } /* don't free patterns itself -- it's static */
994 modest_text_utils_hyperlinkify_begin (void)
996 if (url_matches_block == 0)
998 url_matches_block ++;
1002 modest_text_utils_hyperlinkify_end (void)
1004 url_matches_block--;
1005 if (url_matches_block <= 0)
1011 get_url_matches (GString *txt)
1014 guint rv, i, offset = 0;
1015 GSList *match_list = NULL;
1017 const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
1019 /* initalize the regexps */
1020 modest_text_utils_hyperlinkify_begin ();
1022 /* find all the matches */
1023 for (i = 0; i != pattern_num; ++i) {
1027 gboolean is_submatch;
1030 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
1031 g_return_val_if_fail (rv == REG_NOMATCH, NULL); /* this should not happen */
1032 break; /* try next regexp */
1037 is_submatch = FALSE;
1038 /* check old matches to see if this has already been matched */
1039 cursor = match_list;
1040 while (cursor && !is_submatch) {
1041 const url_match_t *old_match =
1042 (const url_match_t *) cursor->data;
1043 guint new_offset = offset + rm.rm_so;
1044 is_submatch = (new_offset > old_match->offset &&
1045 new_offset < old_match->offset + old_match->len);
1046 cursor = g_slist_next (cursor);
1050 /* make a list of our matches (<offset, len, prefix> tupels)*/
1051 match = g_slice_new (url_match_t);
1052 match->offset = offset + rm.rm_so;
1053 match->len = rm.rm_eo - rm.rm_so;
1054 match->prefix = patterns[i].prefix;
1055 match_list = g_slist_prepend (match_list, match);
1061 modest_text_utils_hyperlinkify_end ();
1063 /* now sort the list, so the matches are in reverse order of occurence.
1064 * that way, we can do the replacements starting from the end, so we don't need
1065 * to recalculate the offsets
1067 match_list = g_slist_sort (match_list,
1068 (GCompareFunc)cmp_offsets_reverse);
1075 hyperlinkify_plain_text (GString *txt)
1078 GSList *match_list = get_url_matches (txt);
1080 /* we will work backwards, so the offsets stay valid */
1081 for (cursor = match_list; cursor; cursor = cursor->next) {
1083 url_match_t *match = (url_match_t*) cursor->data;
1084 gchar *url = g_strndup (txt->str + match->offset, match->len);
1085 gchar *repl = NULL; /* replacement */
1087 /* the prefix is NULL: use the one that is already there */
1088 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
1089 match->prefix ? match->prefix : EMPTY_STRING,
1092 /* replace the old thing with our hyperlink
1093 * replacement thing */
1094 g_string_erase (txt, match->offset, match->len);
1095 g_string_insert (txt, match->offset, repl);
1100 g_slice_free (url_match_t, match);
1103 g_slist_free (match_list);
1107 /* for optimization reasons, we change the string in-place */
1109 modest_text_utils_get_display_address (gchar *address)
1113 g_return_if_fail (address);
1118 /* should not be needed, and otherwise, we probably won't screw up the address
1119 * more than it already is :)
1120 * g_return_val_if_fail (g_utf8_validate (address, -1, NULL), NULL);
1123 /* remove leading whitespace */
1124 if (address[0] == ' ')
1125 g_strchug (address);
1127 for (i = 0; address[i]; ++i) {
1128 if (address[i] == '<') {
1129 if (G_UNLIKELY(i == 0))
1130 return; /* there's nothing else, leave it */
1132 address[i] = '\0'; /* terminate the string here */
1144 modest_text_utils_get_email_address (const gchar *full_address)
1146 const gchar *left, *right;
1148 g_return_val_if_fail (full_address, NULL);
1153 g_return_val_if_fail (g_utf8_validate (full_address, -1, NULL), NULL);
1155 left = g_strrstr_len (full_address, strlen(full_address), "<");
1157 return g_strdup (full_address);
1159 right = g_strstr_len (left, strlen(left), ">");
1161 return g_strdup (full_address);
1163 return g_strndup (left + 1, right - left - 1);
1167 modest_text_utils_get_subject_prefix_len (const gchar *sub)
1169 gint prefix_len = 0;
1171 g_return_val_if_fail (sub, 0);
1176 /* optimization: "Re", "RE", "re","Fwd", "FWD", "fwd","FW","Fw", "fw" */
1177 if (sub[0] != 'R' && sub[0] != 'F' && sub[0] != 'r' && sub[0] != 'f')
1179 else if (sub[0] && sub[1] != 'e' && sub[1] != 'E' && sub[1] != 'w' && sub[1] != 'W')
1186 /* skip over a [...] block */
1187 if (sub[prefix_len] == '[') {
1188 int c = prefix_len + 1;
1189 while (sub[c] && sub[c] != ']')
1192 return 0; /* no end to the ']' found */
1197 /* did we find the ':' ? */
1198 if (sub[prefix_len] == ':') {
1200 if (sub[prefix_len] == ' ')
1202 prefix_len += modest_text_utils_get_subject_prefix_len (sub + prefix_len);
1203 /* g_warning ("['%s','%s']", sub, (char*) sub + prefix_len); */
1211 modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive)
1214 /* work even when s1 and/or s2 == NULL */
1215 if (G_UNLIKELY(s1 == s2))
1217 if (G_UNLIKELY(!s1))
1219 if (G_UNLIKELY(!s2))
1222 /* if it's not case sensitive */
1225 /* optimization: short cut if first char is ascii */
1226 if (((s1[0] & 0xf0)== 0) && ((s2[0] & 0xf0) == 0))
1227 return s1[0] - s2[0];
1229 return g_utf8_collate (s1, s2);
1235 /* optimization: short cut iif first char is ascii */
1236 if (((s1[0] & 0xf0) == 0) && ((s2[0] & 0xf0) == 0))
1237 return tolower(s1[0]) - tolower(s2[0]);
1239 n1 = g_utf8_strdown (s1, -1);
1240 n2 = g_utf8_strdown (s2, -1);
1242 result = g_utf8_collate (n1, n2);
1253 modest_text_utils_get_display_date (time_t date)
1255 #define DATE_BUF_SIZE 64
1256 static gchar date_buf[DATE_BUF_SIZE];
1258 /* calculate the # of days since epoch for
1259 * for today and for the date provided
1260 * based on idea from pvanhoof */
1261 int day = time(NULL) / (24 * 60 * 60);
1262 int date_day = date / (24 * 60 * 60);
1264 /* if it's today, show the time, if it's not today, show the date instead */
1266 if (day == date_day) /* is the date today? */
1267 modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, "%X", date);
1269 modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, "%x", date);
1271 return date_buf; /* this is a static buffer, don't free! */
1277 modest_text_utils_validate_folder_name (const gchar *folder_name)
1279 /* based on http://msdn2.microsoft.com/en-us/library/aa365247.aspx,
1280 * with some extras */
1284 const gchar **cursor = NULL;
1285 const gchar *forbidden_names[] = { /* windows does not like these */
1286 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6",
1287 "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
1291 /* cannot be NULL */
1295 /* cannot be empty */
1296 len = strlen(folder_name);
1300 /* cannot start or end with a space */
1301 if (g_ascii_isspace(folder_name[0]) || g_ascii_isspace(folder_name[len - 1]))
1304 /* cannot contain a forbidden char */
1305 for (i = 0; i < len; i++)
1306 if (modest_text_utils_is_forbidden_char (folder_name[i], FOLDER_NAME_FORBIDDEN_CHARS))
1309 /* cannot contain a forbidden word */
1311 for (cursor = forbidden_names; cursor && *cursor; ++cursor) {
1312 if (g_ascii_strcasecmp (folder_name, *cursor) == 0)
1317 return TRUE; /* it's valid! */
1323 modest_text_utils_validate_domain_name (const gchar *domain)
1325 gboolean valid = FALSE;
1327 const gchar* domain_regex = "^([a-z0-9-]*[a-z0-9]\\.)+[a-z0-9-]*[a-z0-9]$";
1329 g_return_val_if_fail (domain, FALSE);
1334 memset (&rx, 0, sizeof(regex_t)); /* coverity wants this... */
1336 /* domain name: all alphanum or '-' or '.',
1337 * but beginning/ending in alphanum */
1338 if (regcomp (&rx, domain_regex, REG_ICASE|REG_EXTENDED|REG_NOSUB)) {
1339 g_warning ("BUG: error in regexp");
1343 valid = (regexec (&rx, domain, 1, NULL, 0) == 0);
1352 modest_text_utils_validate_email_address (const gchar *email_address,
1353 const gchar **invalid_char_position)
1356 const gchar *c = NULL, *domain = NULL;
1357 static gchar *rfc822_specials = "()<>@,;:\\\"[]&";
1359 if (invalid_char_position)
1360 *invalid_char_position = NULL;
1362 g_return_val_if_fail (email_address, FALSE);
1364 /* check that the email adress contains exactly one @ */
1365 if (!strstr(email_address, "@") ||
1366 (strstr(email_address, "@") != g_strrstr(email_address, "@")))
1369 /* first we validate the name portion (name@domain) */
1370 for (c = email_address; *c; c++) {
1372 (c == email_address ||
1374 *(c - 1) == '\"')) {
1378 if (*c == '\\' && (*++c == ' '))
1380 if (*c <= ' ' || *c >= 127)
1393 if (*c <= ' ' || *c >= 127)
1395 if (strchr(rfc822_specials, *c)) {
1396 if (invalid_char_position)
1397 *invalid_char_position = c;
1401 if (c == email_address || *(c - 1) == '.')
1404 /* next we validate the domain portion (name@domain) */
1405 if (!*(domain = ++c))
1409 if (c == domain || *(c - 1) == '.' || *(c + 1) == '\0')
1413 if (*c <= ' ' || *c >= 127)
1415 if (strchr(rfc822_specials, *c)) {
1416 if (invalid_char_position)
1417 *invalid_char_position = c;
1422 return (count >= 1) ? TRUE : FALSE;
1426 modest_text_utils_validate_recipient (const gchar *recipient, const gchar **invalid_char_position)
1428 gchar *stripped, *current;
1430 gboolean has_error = FALSE;
1432 if (invalid_char_position)
1433 *invalid_char_position = NULL;
1435 g_return_val_if_fail (recipient, FALSE);
1437 if (modest_text_utils_validate_email_address (recipient, invalid_char_position))
1440 stripped = g_strdup (recipient);
1441 stripped = g_strstrip (stripped);
1444 if (*current == '\0') {
1450 if (*current == '\"') {
1451 current = g_utf8_next_char (current);
1453 for (; *current != '\0'; current = g_utf8_next_char (current)) {
1454 if (*current == '\\') {
1455 /* TODO: This causes a warning, which breaks the build,
1456 * because a gchar cannot be < 0.
1458 if (current[1] <0) {
1463 } else if (*current == '\"') {
1465 current = g_utf8_next_char (current);
1471 for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
1472 if (*current == '<') {
1484 right_part = g_strdup (current);
1486 right_part = g_strstrip (right_part);
1488 if (g_str_has_prefix (right_part, "<") &&
1489 g_str_has_suffix (right_part, ">")) {
1493 address = g_strndup (right_part+1, strlen (right_part) - 2);
1494 g_free (right_part);
1495 valid = modest_text_utils_validate_email_address (address, invalid_char_position);
1499 g_free (right_part);
1506 modest_text_utils_get_display_size (guint64 size)
1508 const guint KB=1024;
1509 const guint MB=1024 * KB;
1510 const guint GB=1024 * MB;
1513 return g_strdup_printf(_FM("sfil_li_size_kb"), 0);
1514 if (0 < size && size < KB)
1515 return g_strdup_printf (_FM("sfil_li_size_kb"), 1);
1516 else if (KB <= size && size < 100 * KB)
1517 return g_strdup_printf (_FM("sfil_li_size_1kb_99kb"), size / KB);
1518 else if (100*KB <= size && size < MB)
1519 return g_strdup_printf (_FM("sfil_li_size_100kb_1mb"), (float) size / MB);
1520 else if (MB <= size && size < 10*MB)
1521 return g_strdup_printf (_FM("sfil_li_size_1mb_10mb"), (float) size / MB);
1522 else if (10*MB <= size && size < GB)
1523 return g_strdup_printf (_FM("sfil_li_size_10mb_1gb"), size / MB);
1525 return g_strdup_printf (_FM("sfil_li_size_1gb_or_greater"), (float) size / GB);
1529 get_email_from_address (const gchar * address)
1531 gchar *left_limit, *right_limit;
1533 left_limit = strstr (address, "<");
1534 right_limit = g_strrstr (address, ">");
1536 if ((left_limit == NULL)||(right_limit == NULL)|| (left_limit > right_limit))
1537 return g_strdup (address);
1539 return g_strndup (left_limit + 1, (right_limit - left_limit) - 1);
1543 modest_text_utils_get_color_string (GdkColor *color)
1545 g_return_val_if_fail (color, NULL);
1547 return g_strdup_printf ("#%x%x%x%x%x%x%x%x%x%x%x%x",
1548 (color->red >> 12) & 0xf, (color->red >> 8) & 0xf,
1549 (color->red >> 4) & 0xf, (color->red) & 0xf,
1550 (color->green >> 12) & 0xf, (color->green >> 8) & 0xf,
1551 (color->green >> 4) & 0xf, (color->green) & 0xf,
1552 (color->blue >> 12) & 0xf, (color->blue >> 8) & 0xf,
1553 (color->blue >> 4) & 0xf, (color->blue) & 0xf);
1557 modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer)
1559 GtkTextIter start, end;
1560 gchar *slice, *current;
1561 GString *result = g_string_new ("");
1563 g_return_val_if_fail (buffer && GTK_IS_TEXT_BUFFER (buffer), NULL);
1565 gtk_text_buffer_get_start_iter (buffer, &start);
1566 gtk_text_buffer_get_end_iter (buffer, &end);
1568 slice = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
1571 while (current && current != '\0') {
1572 if (g_utf8_get_char (current) == 0xFFFC) {
1573 result = g_string_append_c (result, ' ');
1574 current = g_utf8_next_char (current);
1576 gchar *next = g_utf8_strchr (current, -1, 0xFFFC);
1578 result = g_string_append (result, current);
1580 result = g_string_append_len (result, current, next - current);
1587 return g_string_free (result, FALSE);
1592 modest_text_utils_is_forbidden_char (const gchar character,
1593 ModestTextUtilsForbiddenCharType type)
1596 const gchar *forbidden_chars = NULL;
1598 /* We need to get the length in the switch because the
1599 compiler needs to know the size at compile time */
1601 case ACCOUNT_TITLE_FORBIDDEN_CHARS:
1602 forbidden_chars = account_title_forbidden_chars;
1603 len = G_N_ELEMENTS (account_title_forbidden_chars);
1605 case FOLDER_NAME_FORBIDDEN_CHARS:
1606 forbidden_chars = folder_name_forbidden_chars;
1607 len = G_N_ELEMENTS (folder_name_forbidden_chars);
1609 case USER_NAME_FORBIDDEN_NAMES:
1610 forbidden_chars = user_name_forbidden_chars;
1611 len = G_N_ELEMENTS (user_name_forbidden_chars);
1614 g_return_val_if_reached (TRUE);
1617 for (i = 0; i < len ; i++)
1618 if (forbidden_chars[i] == character)
1621 return FALSE; /* it's valid! */