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 #include <glib/gi18n.h>
36 #include <modest-tny-platform-factory.h>
37 #include <modest-text-utils.h>
38 #include <modest-runtime.h>
43 #endif /*HAVE_CONFIG_H */
46 #define FORWARD_STRING _("-----Forwarded Message-----")
47 #define FROM_STRING _("From:")
48 #define SENT_STRING _("Sent:")
49 #define TO_STRING _("To:")
50 #define SUBJECT_STRING _("Subject:")
51 #define EMPTY_STRING ""
54 * do the hyperlinkification only for texts < 50 Kb,
55 * as it's quite slow. Without this, e.g. mail with
56 * an uuencoded part (which is not recognized as attachment,
59 #define HYPERLINKIFY_MAX_LENGTH (1024*50)
62 * we need these regexps to find URLs in plain text e-mails
64 typedef struct _url_match_pattern_t url_match_pattern_t;
65 struct _url_match_pattern_t {
71 typedef struct _url_match_t url_match_t;
78 #define MAIL_VIEWER_URL_MATCH_PATTERNS { \
79 { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
81 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
83 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
85 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
87 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
89 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
94 static gchar* cite (const time_t sent_date, const gchar *from);
95 static void hyperlinkify_plain_text (GString *txt);
96 static gint cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2);
97 static GSList* get_url_matches (GString *txt);
99 static GString* get_next_line (const char *b, const gsize blen, const gchar * iter);
100 static int get_indent_level (const char *l);
101 static void unquote_line (GString * l);
102 static void append_quoted (GString * buf, const int indent, const GString * str,
104 static int get_breakpoint_utf8 (const gchar * s, const gint indent, const gint limit);
105 static int get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit);
106 static int get_breakpoint (const gchar * s, const gint indent, const gint limit);
108 static gchar* modest_text_utils_quote_plain_text (const gchar *text,
110 const gchar *signature,
114 static gchar* modest_text_utils_quote_html (const gchar *text,
116 const gchar *signature,
119 static gchar* get_email_from_address (const gchar *address);
122 /* ******************************************************************* */
123 /* ************************* PUBLIC FUNCTIONS ************************ */
124 /* ******************************************************************* */
127 modest_text_utils_quote (const gchar *text,
128 const gchar *content_type,
129 const gchar *signature,
131 const time_t sent_date,
135 gchar *retval, *cited;
137 g_return_val_if_fail (text, NULL);
138 g_return_val_if_fail (content_type, NULL);
140 cited = cite (sent_date, from);
142 if (content_type && strcmp (content_type, "text/html") == 0)
143 /* TODO: extract the <body> of the HTML and pass it to
145 retval = modest_text_utils_quote_html (text, cited, signature, attachments, limit);
147 retval = modest_text_utils_quote_plain_text (text, cited, signature, attachments, limit);
156 modest_text_utils_cite (const gchar *text,
157 const gchar *content_type,
158 const gchar *signature,
165 g_return_val_if_fail (text, NULL);
166 g_return_val_if_fail (content_type, NULL);
169 retval = g_strdup ("");
170 else if (!strcmp(content_type, "text/html")) {
171 tmp_sig = g_strconcat ("\n", signature, NULL);
172 retval = modest_text_utils_convert_to_html_body(tmp_sig);
175 retval = g_strconcat ("\n", signature, NULL);
182 forward_cite (const gchar *from,
185 const gchar *subject)
187 return g_strdup_printf ("%s\n%s %s\n%s %s\n%s %s\n%s %s\n",
189 FROM_STRING, (from)?from:"",
191 TO_STRING, (to)?to:"",
192 SUBJECT_STRING, (subject)?subject:"");
196 modest_text_utils_inline (const gchar *text,
197 const gchar *content_type,
198 const gchar *signature,
202 const gchar *subject)
208 g_return_val_if_fail (text, NULL);
209 g_return_val_if_fail (content_type, NULL);
211 modest_text_utils_strftime (sent_str, 100, "%c", sent_date);
213 cited = forward_cite (from, sent_str, to, subject);
215 if (content_type && strcmp (content_type, "text/html") == 0)
216 retval = modest_text_utils_quote_html (text, cited, signature, NULL, 80);
218 retval = modest_text_utils_quote_plain_text (text, cited, signature, NULL, 80);
224 /* just to prevent warnings:
225 * warning: `%x' yields only last 2 digits of year in some locales
228 modest_text_utils_strftime(char *s, gsize max, const char *fmt, time_t timet)
232 /* does not work on old maemo glib:
233 * g_date_set_time_t (&date, timet);
235 localtime_r (&timet, &tm);
236 return strftime(s, max, fmt, &tm);
240 modest_text_utils_derived_subject (const gchar *subject, const gchar *prefix)
244 g_return_val_if_fail (prefix, NULL);
247 return g_strdup (prefix);
249 tmp = g_strchug (g_strdup (subject));
251 if (!strncmp (tmp, prefix, strlen (prefix))) {
255 return g_strdup_printf ("%s %s", prefix, subject);
260 modest_text_utils_remove_address (const gchar *address_list, const gchar *address)
262 gchar *dup, *token, *ptr, *result;
263 GString *filtered_emails;
264 gchar *email_address;
266 g_return_val_if_fail (address_list, NULL);
269 return g_strdup (address_list);
271 email_address = get_email_from_address (address);
273 /* search for substring */
274 if (!strstr ((const char *) address_list, (const char *) email_address)) {
275 g_free (email_address);
276 return g_strdup (address_list);
279 dup = g_strdup (address_list);
280 filtered_emails = g_string_new (NULL);
282 token = strtok_r (dup, ",", &ptr);
284 while (token != NULL) {
285 /* Add to list if not found */
286 if (!strstr ((const char *) token, (const char *) email_address)) {
287 if (filtered_emails->len == 0)
288 g_string_append_printf (filtered_emails, "%s", g_strstrip (token));
290 g_string_append_printf (filtered_emails, ",%s", g_strstrip (token));
292 token = strtok_r (NULL, ",", &ptr);
294 result = filtered_emails->str;
297 g_free (email_address);
299 g_string_free (filtered_emails, FALSE);
305 modest_text_utils_convert_buffer_to_html (GString *html, const gchar *data)
308 gboolean space_seen = FALSE;
310 guint break_dist = 0; /* distance since last break point */
314 /* replace with special html chars where needed*/
315 for (i = 0; i != len; ++i) {
318 if (space_seen && kar != ' ') {
319 g_string_append_c (html, ' ');
323 /* we artificially insert a breakpoint (newline)
324 * after 256, to make sure our lines are not so long
325 * they will DOS the regexping later
327 if (++break_dist == 256) {
328 g_string_append_c (html, '\n');
333 case 0: break; /* ignore embedded \0s */
334 case '<' : g_string_append (html, "<"); break;
335 case '>' : g_string_append (html, ">"); break;
336 case '&' : g_string_append (html, "&"); break;
337 case '"' : g_string_append (html, """); break;
338 case '\'' : g_string_append (html, "'"); break;
339 case '\n' : g_string_append (html, "<br>\n"); break_dist= 0; break;
340 case '\t' : g_string_append (html, " "); break_dist=0; break; /* note the space at the end*/
343 if (space_seen) { /* second space in a row */
344 g_string_append (html, " ");
350 g_string_append_c (html, kar);
356 modest_text_utils_convert_to_html (const gchar *data)
365 html = g_string_sized_new (1.5 * len); /* just a guess... */
367 g_string_append_printf (html,
369 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf8\">"
373 modest_text_utils_convert_buffer_to_html (html, data);
375 g_string_append (html, "</body></html>");
377 if (len <= HYPERLINKIFY_MAX_LENGTH)
378 hyperlinkify_plain_text (html);
380 return g_string_free (html, FALSE);
384 modest_text_utils_convert_to_html_body (const gchar *data)
393 html = g_string_sized_new (1.5 * len); /* just a guess... */
395 modest_text_utils_convert_buffer_to_html (html, data);
397 if (len < HYPERLINKIFY_MAX_LENGTH)
398 hyperlinkify_plain_text (html);
400 return g_string_free (html, FALSE);
404 modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes)
406 gchar *current, *start, *last_blank;
407 gint start_offset = 0, current_offset = 0;
409 g_return_if_fail (start_indexes != NULL);
410 g_return_if_fail (end_indexes != NULL);
412 start = (gchar *) addresses;
416 while (*current != '\0') {
417 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
418 start = g_utf8_next_char (start);
420 last_blank = current;
421 } else if ((*current == ',')||(*current == ';')) {
422 gint *start_index, *end_index;
423 start_index = g_new0(gint, 1);
424 end_index = g_new0(gint, 1);
425 *start_index = start_offset;
426 *end_index = current_offset;
427 *start_indexes = g_slist_prepend (*start_indexes, start_index);
428 *end_indexes = g_slist_prepend (*end_indexes, end_index);
429 start = g_utf8_next_char (current);
430 start_offset = current_offset + 1;
432 } else if (*current == '"') {
433 current = g_utf8_next_char (current);
435 while ((*current != '"')&&(*current != '\0')) {
436 current = g_utf8_next_char (current);
441 current = g_utf8_next_char (current);
445 if (start != current) {
446 gint *start_index, *end_index;
447 start_index = g_new0(gint, 1);
448 end_index = g_new0(gint, 1);
449 *start_index = start_offset;
450 *end_index = current_offset;
451 *start_indexes = g_slist_prepend (*start_indexes, start_index);
452 *end_indexes = g_slist_prepend (*end_indexes, end_index);
455 *start_indexes = g_slist_reverse (*start_indexes);
456 *end_indexes = g_slist_reverse (*end_indexes);
462 modest_text_utils_split_addresses_list (const gchar *addresses)
464 gchar *current, *start, *last_blank;
465 GSList *result = NULL;
467 start = (gchar *) addresses;
471 while (*current != '\0') {
472 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
473 start = g_utf8_next_char (start);
474 last_blank = current;
475 } else if ((*current == ',')||(*current == ';')) {
476 gchar *new_address = NULL;
477 new_address = g_strndup (start, current - last_blank);
478 result = g_slist_prepend (result, new_address);
479 start = g_utf8_next_char (current);
481 } else if (*current == '\"') {
482 if (current == start) {
483 current = g_utf8_next_char (current);
484 start = g_utf8_next_char (start);
486 while ((*current != '\"')&&(*current != '\0'))
487 current = g_utf8_next_char (current);
490 current = g_utf8_next_char (current);
493 if (start != current) {
494 gchar *new_address = NULL;
495 new_address = g_strndup (start, current - last_blank);
496 result = g_slist_prepend (result, new_address);
499 result = g_slist_reverse (result);
505 modest_text_utils_address_range_at_position (const gchar *recipients_list,
510 gchar *current = NULL;
511 gint range_start = 0;
514 gboolean is_quoted = FALSE;
517 for (current = (gchar *) recipients_list; *current != '\0'; current = g_utf8_find_next_char (current, NULL)) {
518 gunichar c = g_utf8_get_char (current);
520 if ((c == ',') && (!is_quoted)) {
521 if (index < position) {
522 range_start = index + 1;
526 } else if (c == '\"') {
527 is_quoted = !is_quoted;
528 } else if ((c == ' ') &&(range_start == index)) {
536 *start = range_start;
542 /* ******************************************************************* */
543 /* ************************* UTILIY FUNCTIONS ************************ */
544 /* ******************************************************************* */
547 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
553 return g_string_new("");
561 gs = g_string_new_len (i0, iter - i0);
565 get_indent_level (const char *l)
582 /* if we hit the signature marker "-- ", we return -(indent + 1). This
583 * stops reformatting.
585 if (strcmp (l, "-- ") == 0) {
593 unquote_line (GString * l)
608 g_string_erase (l, 0, p - l->str);
612 append_quoted (GString * buf, int indent, const GString * str,
617 indent = indent < 0 ? abs (indent) - 1 : indent;
618 for (i = 0; i <= indent; i++) {
619 g_string_append (buf, "> ");
622 g_string_append_len (buf, str->str, cutpoint);
624 g_string_append (buf, str->str);
626 g_string_append (buf, "\n");
630 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
633 const gchar *pos, *last;
636 indent = indent < 0 ? abs (indent) - 1 : indent;
640 uni = g_utf8_to_ucs4_fast (s, -1, NULL);
642 if ((index + 2 * indent > limit) && last) {
646 if (g_unichar_isspace (uni[index])) {
649 pos = g_utf8_next_char (pos);
657 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
662 if (last + 2 * indent < limit)
665 for (i = strlen (s); i > 0; i--) {
667 if (i + 2 * indent <= limit) {
678 get_breakpoint (const gchar * s, const gint indent, const gint limit)
681 if (g_utf8_validate (s, -1, NULL)) {
682 return get_breakpoint_utf8 (s, indent, limit);
683 } else { /* assume ASCII */
684 //g_warning("invalid UTF-8 in msg");
685 return get_breakpoint_ascii (s, indent, limit);
690 cite (const time_t sent_date, const gchar *from)
692 return g_strdup (_("mcen_ia_editor_original_message"));
696 quoted_attachments (GList *attachments)
699 GString *result = g_string_new ("");
700 for (node = attachments; node != NULL; node = g_list_next (node)) {
701 gchar *filename = (gchar *) node->data;
702 g_string_append_printf ( result, "%s %s\n", _("mcen_ia_editor_attach_filename"), filename);
705 return g_string_free (result, FALSE);
710 modest_text_utils_quote_plain_text (const gchar *text,
712 const gchar *signature,
717 gint indent, breakpoint, rem_indent = 0;
718 GString *q, *l, *remaining;
720 gchar *attachments_string = NULL;
722 /* remaining will store the rest of the line if we have to break it */
723 q = g_string_new ("\n");
724 q = g_string_append (q, cite);
725 q = g_string_append_c (q, '\n');
726 remaining = g_string_new ("");
731 l = get_next_line (text, len, iter);
732 iter = iter + l->len + 1;
733 indent = get_indent_level (l->str);
736 if (remaining->len) {
737 if (l->len && indent == rem_indent) {
738 g_string_prepend (l, " ");
739 g_string_prepend (l, remaining->str);
743 get_breakpoint (remaining->str,
746 append_quoted (q, rem_indent,
747 remaining, breakpoint);
748 g_string_erase (remaining, 0,
750 if (remaining->str[0] == ' ') {
751 g_string_erase (remaining, 0,
754 } while (remaining->len);
757 g_string_free (remaining, TRUE);
758 breakpoint = get_breakpoint (l->str, indent, limit);
759 remaining = g_string_new (l->str + breakpoint);
760 if (remaining->str[0] == ' ') {
761 g_string_erase (remaining, 0, 1);
764 append_quoted (q, indent, l, breakpoint);
765 g_string_free (l, TRUE);
766 } while ((iter < text + len) || (remaining->str[0]));
768 attachments_string = quoted_attachments (attachments);
769 q = g_string_append (q, attachments_string);
770 g_free (attachments_string);
772 if (signature != NULL) {
773 q = g_string_append_c (q, '\n');
774 q = g_string_append (q, signature);
777 return g_string_free (q, FALSE);
781 modest_text_utils_quote_html (const gchar *text,
783 const gchar *signature,
787 gchar *result = NULL;
788 gchar *signature_result = NULL;
789 const gchar *format = \
790 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" \
794 "<pre>%s<br/>%s<br/>%s</pre>\n" \
797 gchar *attachments_string = NULL;
798 gchar *q_attachments_string = NULL;
799 gchar *q_cite = NULL;
800 gchar *html_text = NULL;
802 if (signature == NULL)
803 signature_result = g_strdup ("");
805 signature_result = modest_text_utils_convert_to_html_body (signature);
807 attachments_string = quoted_attachments (attachments);
808 q_attachments_string = modest_text_utils_convert_to_html_body (attachments_string);
809 q_cite = modest_text_utils_convert_to_html_body (cite);
810 html_text = modest_text_utils_convert_to_html_body (text);
811 result = g_strdup_printf (format, signature_result, q_cite, html_text, q_attachments_string);
814 g_free (attachments_string);
815 g_free (q_attachments_string);
816 g_free (signature_result);
821 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
823 return match2->offset - match1->offset;
828 get_url_matches (GString *txt)
831 guint rv, i, offset = 0;
832 GSList *match_list = NULL;
834 static url_match_pattern_t patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
835 const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
837 /* initalize the regexps */
838 for (i = 0; i != pattern_num; ++i) {
839 patterns[i].preg = g_slice_new0 (regex_t);
841 /* this should not happen */
842 g_return_val_if_fail (regcomp (patterns[i].preg, patterns[i].regex,
843 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0, NULL);
845 /* find all the matches */
846 for (i = 0; i != pattern_num; ++i) {
850 gboolean is_submatch;
853 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
854 g_return_val_if_fail (rv == REG_NOMATCH, NULL); /* this should not happen */
855 break; /* try next regexp */
861 /* check old matches to see if this has already been matched */
863 while (cursor && !is_submatch) {
864 const url_match_t *old_match =
865 (const url_match_t *) cursor->data;
866 guint new_offset = offset + rm.rm_so;
867 is_submatch = (new_offset > old_match->offset &&
868 new_offset < old_match->offset + old_match->len);
869 cursor = g_slist_next (cursor);
873 /* make a list of our matches (<offset, len, prefix> tupels)*/
874 match = g_slice_new (url_match_t);
875 match->offset = offset + rm.rm_so;
876 match->len = rm.rm_eo - rm.rm_so;
877 match->prefix = patterns[i].prefix;
878 g_warning ("<%d, %d, %s>", match->offset, match->len, match->prefix);
879 match_list = g_slist_prepend (match_list, match);
886 for (i = 0; i != pattern_num; ++i) {
887 regfree (patterns[i].preg);
888 g_slice_free (regex_t, patterns[i].preg);
889 } /* don't free patterns itself -- it's static */
891 /* now sort the list, so the matches are in reverse order of occurence.
892 * that way, we can do the replacements starting from the end, so we don't need
893 * to recalculate the offsets
895 match_list = g_slist_sort (match_list,
896 (GCompareFunc)cmp_offsets_reverse);
903 hyperlinkify_plain_text (GString *txt)
906 GSList *match_list = get_url_matches (txt);
908 /* we will work backwards, so the offsets stay valid */
909 for (cursor = match_list; cursor; cursor = cursor->next) {
911 url_match_t *match = (url_match_t*) cursor->data;
912 gchar *url = g_strndup (txt->str + match->offset, match->len);
913 gchar *repl = NULL; /* replacement */
915 /* the prefix is NULL: use the one that is already there */
916 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
917 match->prefix ? match->prefix : EMPTY_STRING,
920 /* replace the old thing with our hyperlink
921 * replacement thing */
922 g_string_erase (txt, match->offset, match->len);
923 g_string_insert (txt, match->offset, repl);
928 g_slice_free (url_match_t, match);
931 g_slist_free (match_list);
937 modest_text_utils_get_display_address (gchar *address)
944 g_return_val_if_fail (g_utf8_validate (address, -1, NULL), NULL);
946 g_strchug (address); /* remove leading whitespace */
948 /* <email@address> from display name */
949 cursor = g_strstr_len (address, strlen(address), "<");
950 if (cursor == address) /* there's nothing else? leave it */
955 /* remove (bla bla) from display name */
956 cursor = g_strstr_len (address, strlen(address), "(");
957 if (cursor == address) /* there's nothing else? leave it */
962 g_strchomp (address); /* remove trailing whitespace */
970 modest_text_utils_get_subject_prefix_len (const gchar *sub)
973 static const gchar* prefix[] = {
974 "Re:", "RE:", "RV:", "re:"
975 "Fwd:", "FWD:", "FW:", "fwd:", "Fw:", "fw:", NULL
978 if (!sub || (sub[0] != 'R' && sub[0] != 'F' && sub[0] != 'r' && sub[0] != 'f')) /* optimization */
984 if (g_str_has_prefix(sub, prefix[i])) {
985 int prefix_len = strlen(prefix[i]);
986 if (sub[prefix_len] == ' ')
987 ++prefix_len; /* ignore space after prefix as well */
997 modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive)
1002 /* work even when s1 and/or s2 == NULL */
1003 if (G_UNLIKELY(s1 == s2))
1006 /* if it's not case sensitive */
1008 return strcmp (s1 ? s1 : "", s2 ? s2 : "");
1010 n1 = g_utf8_collate_key (s1 ? s1 : "", -1);
1011 n2 = g_utf8_collate_key (s2 ? s2 : "", -1);
1013 result = strcmp (n1, n2);
1023 modest_text_utils_get_display_date (time_t date)
1026 static const guint BUF_SIZE = 64;
1027 static const guint ONE_DAY = 24 * 60 * 60; /* seconds in one day */
1028 gchar date_buf[BUF_SIZE];
1029 gchar today_buf [BUF_SIZE];
1031 modest_text_utils_strftime (date_buf, BUF_SIZE, "%x", date);
1035 /* we check if the date is within the last 24h, if not, we don't
1036 * have to do the extra, expensive strftime, which was very visible
1039 if (abs(now - date) < ONE_DAY) {
1041 /* it's within the last 24 hours, but double check */
1042 /* use the localized dates */
1043 modest_text_utils_strftime (today_buf, BUF_SIZE, "%x", now);
1045 /* if it's today, use the time instead */
1046 if (strcmp (date_buf, today_buf) == 0)
1047 modest_text_utils_strftime (date_buf, BUF_SIZE, "%X", date);
1050 return g_strdup(date_buf);
1054 modest_text_utils_validate_email_address (const gchar *email_address, const gchar **invalid_char_position)
1057 const gchar *c = NULL, *domain = NULL;
1058 static gchar *rfc822_specials = "()<>@,;:\\\"[]&";
1060 if (invalid_char_position != NULL)
1061 *invalid_char_position = NULL;
1063 /* first we validate the name portion (name@domain) */
1064 for (c = email_address; *c; c++) {
1066 (c == email_address ||
1068 *(c - 1) == '\"')) {
1072 if (*c == '\\' && (*++c == ' '))
1074 if (*c <= ' ' || *c >= 127)
1087 if (*c <= ' ' || *c >= 127)
1089 if (strchr(rfc822_specials, *c)) {
1090 if (invalid_char_position)
1091 *invalid_char_position = c;
1095 if (c == email_address || *(c - 1) == '.')
1098 /* next we validate the domain portion (name@domain) */
1099 if (!*(domain = ++c))
1103 if (c == domain || *(c - 1) == '.')
1107 if (*c <= ' ' || *c >= 127)
1109 if (strchr(rfc822_specials, *c)) {
1110 if (invalid_char_position)
1111 *invalid_char_position = c;
1116 return (count >= 1) ? TRUE : FALSE;
1120 modest_text_utils_validate_recipient (const gchar *recipient, const gchar **invalid_char_position)
1122 gchar *stripped, *current;
1124 gboolean has_error = FALSE;
1126 if (modest_text_utils_validate_email_address (recipient, invalid_char_position))
1128 stripped = g_strdup (recipient);
1129 stripped = g_strstrip (stripped);
1132 if (*current == '\0') {
1138 if (*current == '\"') {
1139 current = g_utf8_next_char (current);
1141 for (; *current != '\0'; current = g_utf8_next_char (current)) {
1142 if (*current == '\\') {
1143 /* TODO: This causes a warning, which breaks the build,
1144 * because a gchar cannot be < 0.
1146 if (current[1] <0) {
1151 } else if (*current == '\"') {
1153 current = g_utf8_next_char (current);
1159 for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
1160 if (*current == '<') {
1172 right_part = g_strdup (current);
1174 right_part = g_strstrip (right_part);
1176 if (g_str_has_prefix (right_part, "<") &&
1177 g_str_has_suffix (right_part, ">")) {
1181 address = g_strndup (right_part+1, strlen (right_part) - 2);
1182 g_free (right_part);
1183 valid = modest_text_utils_validate_email_address (address, invalid_char_position);
1187 g_free (right_part);
1194 modest_text_utils_get_display_size (guint64 size)
1196 const guint KB=1024;
1197 const guint MB=1024 * KB;
1198 const guint GB=1024 * MB;
1201 return g_strdup_printf(_FM("sfil_li_size_kb"), 0);
1202 if (0 < size && size < KB)
1203 return g_strdup_printf (_FM("sfil_li_size_kb"), 1);
1204 else if (KB <= size && size < 100 * KB)
1205 return g_strdup_printf (_FM("sfil_li_size_1kb_99kb"), size / KB);
1206 else if (100*KB <= size && size < MB)
1207 return g_strdup_printf (_FM("sfil_li_size_100kb_1mb"), (float) size / MB);
1208 else if (MB <= size && size < 10*MB)
1209 return g_strdup_printf (_FM("sfil_li_size_1mb_10mb"), (float) size / MB);
1210 else if (10*MB <= size && size < GB)
1211 return g_strdup_printf (_FM("sfil_li_size_10mb_1gb"), size / MB);
1213 return g_strdup_printf (_FM("sfil_li_size_1gb_or_greater"), (float) size / GB);
1217 get_email_from_address (const gchar * address)
1219 gchar *left_limit, *right_limit;
1221 left_limit = strstr (address, "<");
1222 right_limit = g_strrstr (address, ">");
1224 if ((left_limit == NULL)||(right_limit == NULL)|| (left_limit > right_limit))
1225 return g_strdup (address);
1227 return g_strndup (left_limit + 1, (right_limit - left_limit) - 1);
1231 modest_text_utils_get_color_string (GdkColor *color)
1234 return g_strdup_printf ("#%x%x%x%x%x%x%x%x%x%x%x%x",
1235 (color->red >> 12) & 0xf, (color->red >> 8) & 0xf,
1236 (color->red >> 4) & 0xf, (color->red) & 0xf,
1237 (color->green >> 12) & 0xf, (color->green >> 8) & 0xf,
1238 (color->green >> 4) & 0xf, (color->green) & 0xf,
1239 (color->blue >> 12) & 0xf, (color->blue >> 8) & 0xf,
1240 (color->blue >> 4) & 0xf, (color->blue) & 0xf);