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 * we need these regexps to find URLs in plain text e-mails
56 typedef struct _url_match_pattern_t url_match_pattern_t;
57 struct _url_match_pattern_t {
63 typedef struct _url_match_t url_match_t;
70 #define MAIL_VIEWER_URL_MATCH_PATTERNS { \
71 { "(file|rtsp|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.]+",\
86 static gchar* cite (const time_t sent_date, const gchar *from);
87 static void hyperlinkify_plain_text (GString *txt);
88 static gint cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2);
89 static void chk_partial_match (const url_match_t *match, guint* offset);
90 static GSList* get_url_matches (GString *txt);
92 static GString* get_next_line (const char *b, const gsize blen, const gchar * iter);
93 static int get_indent_level (const char *l);
94 static void unquote_line (GString * l);
95 static void append_quoted (GString * buf, const int indent, const GString * str,
97 static int get_breakpoint_utf8 (const gchar * s, const gint indent, const gint limit);
98 static int get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit);
99 static int get_breakpoint (const gchar * s, const gint indent, const gint limit);
101 static gchar* modest_text_utils_quote_plain_text (const gchar *text,
103 const gchar *signature,
106 static gchar* modest_text_utils_quote_html (const gchar *text,
108 const gchar *signature,
110 static gchar* get_email_from_address (const gchar *address);
113 /* ******************************************************************* */
114 /* ************************* PUBLIC FUNCTIONS ************************ */
115 /* ******************************************************************* */
118 modest_text_utils_quote (const gchar *text,
119 const gchar *content_type,
120 const gchar *signature,
122 const time_t sent_date,
125 gchar *retval, *cited;
127 g_return_val_if_fail (text, NULL);
128 g_return_val_if_fail (content_type, NULL);
130 cited = cite (sent_date, from);
132 if (content_type && strcmp (content_type, "text/html") == 0)
133 /* TODO: extract the <body> of the HTML and pass it to
135 retval = modest_text_utils_quote_html (text, cited, signature, limit);
137 retval = modest_text_utils_quote_plain_text (text, cited, signature, limit);
146 modest_text_utils_cite (const gchar *text,
147 const gchar *content_type,
148 const gchar *signature,
155 g_return_val_if_fail (text, NULL);
156 g_return_val_if_fail (content_type, NULL);
159 tmp_sig = g_strdup ("");
160 else if (!strcmp(content_type, "text/html")) {
161 tmp_sig = modest_text_utils_convert_to_html_body(signature);
163 tmp_sig = g_strdup (signature);
166 retval = g_strdup_printf ("\n%s\n", tmp_sig);
173 modest_text_utils_inline (const gchar *text,
174 const gchar *content_type,
175 const gchar *signature,
179 const gchar *subject)
182 gchar *formatted_signature;
183 const gchar *plain_format = "%s%s\n%s %s\n%s %s\n%s %s\n%s %s\n\n%s";
184 const gchar *html_format = \
185 "%s%s<br>\n<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"2\">\n" \
186 "<tr><td>%s</td><td>%s</td></tr>\n" \
187 "<tr><td>%s</td><td>%s</td></tr>\n" \
188 "<tr><td>%s</td><td>%s</td></tr>\n" \
189 "<tr><td>%s</td><td>%s</td></tr>\n" \
193 g_return_val_if_fail (text, NULL);
194 g_return_val_if_fail (content_type, NULL);
195 g_return_val_if_fail (text, NULL);
197 modest_text_utils_strftime (sent_str, 100, "%c", sent_date);
199 if (!strcmp (content_type, "text/html"))
200 /* TODO: extract the <body> of the HTML and pass it to
202 format = html_format;
204 format = plain_format;
206 if (signature != NULL) {
207 if (!strcmp (content_type, "text/html")) {
208 formatted_signature = g_strconcat (signature, "<br/>", NULL);
210 formatted_signature = g_strconcat (signature, "\n", NULL);
213 formatted_signature = "";
216 return g_strdup_printf (format, formatted_signature,
218 FROM_STRING, (from) ? from : EMPTY_STRING,
219 SENT_STRING, sent_str,
220 TO_STRING, (to) ? to : EMPTY_STRING,
221 SUBJECT_STRING, (subject) ? subject : EMPTY_STRING,
225 /* just to prevent warnings:
226 * warning: `%x' yields only last 2 digits of year in some locales
229 modest_text_utils_strftime(char *s, gsize max, const char *fmt, time_t timet)
233 /* does not work on old maemo glib:
234 * g_date_set_time_t (&date, timet);
236 localtime_r (&timet, &tm);
237 return strftime(s, max, fmt, &tm);
241 modest_text_utils_derived_subject (const gchar *subject, const gchar *prefix)
245 g_return_val_if_fail (prefix, NULL);
248 return g_strdup (prefix);
250 tmp = g_strchug (g_strdup (subject));
252 if (!strncmp (tmp, prefix, strlen (prefix))) {
256 return g_strdup_printf ("%s %s", prefix, subject);
261 modest_text_utils_remove_address (const gchar *address_list, const gchar *address)
263 gchar *dup, *token, *ptr, *result;
264 GString *filtered_emails;
265 gchar *email_address;
267 g_return_val_if_fail (address_list, NULL);
270 return g_strdup (address_list);
272 email_address = get_email_from_address (address);
274 /* search for substring */
275 if (!strstr ((const char *) address_list, (const char *) email_address)) {
276 g_free (email_address);
277 return g_strdup (address_list);
280 dup = g_strdup (address_list);
281 filtered_emails = g_string_new (NULL);
283 token = strtok_r (dup, ",", &ptr);
285 while (token != NULL) {
286 /* Add to list if not found */
287 if (!strstr ((const char *) token, (const char *) email_address)) {
288 if (filtered_emails->len == 0)
289 g_string_append_printf (filtered_emails, "%s", g_strstrip (token));
291 g_string_append_printf (filtered_emails, ",%s", g_strstrip (token));
293 token = strtok_r (NULL, ",", &ptr);
295 result = filtered_emails->str;
298 g_free (email_address);
300 g_string_free (filtered_emails, FALSE);
306 modest_text_utils_convert_buffer_to_html (GString *html, const gchar *data)
309 gboolean space_seen = FALSE;
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, ' ');
324 case 0: break; /* ignore embedded \0s */
325 case '<' : g_string_append (html, "<"); break;
326 case '>' : g_string_append (html, ">"); break;
327 case '&' : g_string_append (html, "&"); break;
328 case '"' : g_string_append (html, """); break;
329 case '\'' : g_string_append (html, "'"); break;
330 case '\n' : g_string_append (html, "<br>\n"); break;
331 case '\t' : g_string_append (html, " "); break; /* note the space at the end*/
333 if (space_seen) { /* second space in a row */
334 g_string_append (html, "  ");
340 g_string_append_c (html, kar);
346 modest_text_utils_convert_to_html (const gchar *data)
355 html = g_string_sized_new (1.5 * len); /* just a guess... */
357 g_string_append_printf (html,
359 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf8\">"
363 modest_text_utils_convert_buffer_to_html (html, data);
365 g_string_append (html, "</body></html>");
366 hyperlinkify_plain_text (html);
368 return g_string_free (html, FALSE);
372 modest_text_utils_convert_to_html_body (const gchar *data)
381 html = g_string_sized_new (1.5 * len); /* just a guess... */
383 modest_text_utils_convert_buffer_to_html (html, data);
385 hyperlinkify_plain_text (html);
387 return g_string_free (html, FALSE);
391 modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes)
393 gchar *current, *start, *last_blank;
394 gint start_offset = 0, current_offset = 0;
396 g_return_if_fail (start_indexes != NULL);
397 g_return_if_fail (end_indexes != NULL);
399 start = (gchar *) addresses;
403 while (*current != '\0') {
404 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
405 start = g_utf8_next_char (start);
407 last_blank = current;
408 } else if ((*current == ',')||(*current == ';')) {
409 gint *start_index, *end_index;
410 start_index = g_new0(gint, 1);
411 end_index = g_new0(gint, 1);
412 *start_index = start_offset;
413 *end_index = current_offset;
414 *start_indexes = g_slist_prepend (*start_indexes, start_index);
415 *end_indexes = g_slist_prepend (*end_indexes, end_index);
416 start = g_utf8_next_char (current);
417 start_offset = current_offset + 1;
419 } else if (*current == '"') {
420 current = g_utf8_next_char (current);
422 while ((*current != '"')&&(*current != '\0')) {
423 current = g_utf8_next_char (current);
428 current = g_utf8_next_char (current);
432 if (start != current) {
433 gint *start_index, *end_index;
434 start_index = g_new0(gint, 1);
435 end_index = g_new0(gint, 1);
436 *start_index = start_offset;
437 *end_index = current_offset;
438 *start_indexes = g_slist_prepend (*start_indexes, start_index);
439 *end_indexes = g_slist_prepend (*end_indexes, end_index);
442 *start_indexes = g_slist_reverse (*start_indexes);
443 *end_indexes = g_slist_reverse (*end_indexes);
449 modest_text_utils_split_addresses_list (const gchar *addresses)
451 gchar *current, *start, *last_blank;
452 GSList *result = NULL;
454 start = (gchar *) addresses;
458 while (*current != '\0') {
459 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
460 start = g_utf8_next_char (start);
461 last_blank = current;
462 } else if ((*current == ',')||(*current == ';')) {
463 gchar *new_address = NULL;
464 new_address = g_strndup (start, current - last_blank);
465 result = g_slist_prepend (result, new_address);
466 start = g_utf8_next_char (current);
468 } else if (*current == '\"') {
469 if (current == start) {
470 current = g_utf8_next_char (current);
471 start = g_utf8_next_char (start);
473 while ((*current != '\"')&&(*current != '\0'))
474 current = g_utf8_next_char (current);
477 current = g_utf8_next_char (current);
480 if (start != current) {
481 gchar *new_address = NULL;
482 new_address = g_strndup (start, current - last_blank);
483 result = g_slist_prepend (result, new_address);
486 result = g_slist_reverse (result);
492 modest_text_utils_address_range_at_position (const gchar *recipients_list,
497 gchar *current = NULL;
498 gint range_start = 0;
501 gboolean is_quoted = FALSE;
504 for (current = (gchar *) recipients_list; *current != '\0'; current = g_utf8_find_next_char (current, NULL)) {
505 gunichar c = g_utf8_get_char (current);
507 if ((c == ',') && (!is_quoted)) {
508 if (index < position) {
509 range_start = index + 1;
513 } else if (c == '\"') {
514 is_quoted = !is_quoted;
515 } else if ((c == ' ') &&(range_start == index)) {
523 *start = range_start;
529 /* ******************************************************************* */
530 /* ************************* UTILIY FUNCTIONS ************************ */
531 /* ******************************************************************* */
534 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
540 return g_string_new("");
548 gs = g_string_new_len (i0, iter - i0);
552 get_indent_level (const char *l)
569 /* if we hit the signature marker "-- ", we return -(indent + 1). This
570 * stops reformatting.
572 if (strcmp (l, "-- ") == 0) {
580 unquote_line (GString * l)
595 g_string_erase (l, 0, p - l->str);
599 append_quoted (GString * buf, int indent, const GString * str,
604 indent = indent < 0 ? abs (indent) - 1 : indent;
605 for (i = 0; i <= indent; i++) {
606 g_string_append (buf, "> ");
609 g_string_append_len (buf, str->str, cutpoint);
611 g_string_append (buf, str->str);
613 g_string_append (buf, "\n");
617 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
620 const gchar *pos, *last;
623 indent = indent < 0 ? abs (indent) - 1 : indent;
627 uni = g_utf8_to_ucs4_fast (s, -1, NULL);
629 if ((index + 2 * indent > limit) && last) {
633 if (g_unichar_isspace (uni[index])) {
636 pos = g_utf8_next_char (pos);
644 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
649 if (last + 2 * indent < limit)
652 for (i = strlen (s); i > 0; i--) {
654 if (i + 2 * indent <= limit) {
665 get_breakpoint (const gchar * s, const gint indent, const gint limit)
668 if (g_utf8_validate (s, -1, NULL)) {
669 return get_breakpoint_utf8 (s, indent, limit);
670 } else { /* assume ASCII */
671 //g_warning("invalid UTF-8 in msg");
672 return get_breakpoint_ascii (s, indent, limit);
677 cite (const time_t sent_date, const gchar *from)
681 /* format sent_date */
682 modest_text_utils_strftime (sent_str, 100, "%c", sent_date);
683 return g_strdup_printf (N_("On %s, %s wrote:\n"),
685 (from) ? from : EMPTY_STRING);
690 modest_text_utils_quote_plain_text (const gchar *text,
692 const gchar *signature,
696 gint indent, breakpoint, rem_indent = 0;
697 GString *q, *l, *remaining;
700 /* remaining will store the rest of the line if we have to break it */
701 q = g_string_new (cite);
702 remaining = g_string_new ("");
707 l = get_next_line (text, len, iter);
708 iter = iter + l->len + 1;
709 indent = get_indent_level (l->str);
712 if (remaining->len) {
713 if (l->len && indent == rem_indent) {
714 g_string_prepend (l, " ");
715 g_string_prepend (l, remaining->str);
719 get_breakpoint (remaining->str,
722 append_quoted (q, rem_indent,
723 remaining, breakpoint);
724 g_string_erase (remaining, 0,
726 if (remaining->str[0] == ' ') {
727 g_string_erase (remaining, 0,
730 } while (remaining->len);
733 g_string_free (remaining, TRUE);
734 breakpoint = get_breakpoint (l->str, indent, limit);
735 remaining = g_string_new (l->str + breakpoint);
736 if (remaining->str[0] == ' ') {
737 g_string_erase (remaining, 0, 1);
740 append_quoted (q, indent, l, breakpoint);
741 g_string_free (l, TRUE);
742 } while ((iter < text + len) || (remaining->str[0]));
744 if (signature != NULL) {
745 q = g_string_append_c (q, '\n');
746 q = g_string_append (q, signature);
749 return g_string_free (q, FALSE);
753 modest_text_utils_quote_html (const gchar *text,
755 const gchar *signature,
758 gchar *result = NULL;
759 gchar *signature_result = NULL;
760 const gchar *format = \
761 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" \
765 "<blockquote type=\"cite\">\n%s\n</blockquote>\n" \
769 const gchar *signature_format = \
774 if (signature == NULL)
775 signature_result = g_strdup ("");
777 signature_result = g_strdup_printf (signature_format, signature);
779 result = g_strdup_printf (format, cite, text, signature_result);
780 g_free (signature_result);
785 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
787 return match2->offset - match1->offset;
793 * check if the match is inside an existing match... */
795 chk_partial_match (const url_match_t *match, guint* offset)
797 if (*offset >= match->offset && *offset < match->offset + match->len)
802 get_url_matches (GString *txt)
805 guint rv, i, offset = 0;
806 GSList *match_list = NULL;
808 static url_match_pattern_t patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
809 const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
811 /* initalize the regexps */
812 for (i = 0; i != pattern_num; ++i) {
813 patterns[i].preg = g_slice_new0 (regex_t);
815 /* this should not happen */
816 g_return_val_if_fail (regcomp (patterns[i].preg, patterns[i].regex,
817 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0, NULL);
819 /* find all the matches */
820 for (i = 0; i != pattern_num; ++i) {
824 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
825 g_return_val_if_fail (rv == REG_NOMATCH, NULL); /* this should not happen */
826 break; /* try next regexp */
831 /* FIXME: optimize this */
832 /* to avoid partial matches on something that was already found... */
833 /* check_partial_match will put -1 in the data ptr if that is the case */
834 test_offset = offset + rm.rm_so;
835 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
837 /* make a list of our matches (<offset, len, prefix> tupels)*/
838 if (test_offset != -1) {
839 url_match_t *match = g_slice_new (url_match_t);
840 match->offset = offset + rm.rm_so;
841 match->len = rm.rm_eo - rm.rm_so;
842 match->prefix = patterns[i].prefix;
843 match_list = g_slist_prepend (match_list, match);
849 for (i = 0; i != pattern_num; ++i) {
850 regfree (patterns[i].preg);
851 g_slice_free (regex_t, patterns[i].preg);
852 } /* don't free patterns itself -- it's static */
854 /* now sort the list, so the matches are in reverse order of occurence.
855 * that way, we can do the replacements starting from the end, so we don't need
856 * to recalculate the offsets
858 match_list = g_slist_sort (match_list,
859 (GCompareFunc)cmp_offsets_reverse);
866 hyperlinkify_plain_text (GString *txt)
869 GSList *match_list = get_url_matches (txt);
871 /* we will work backwards, so the offsets stay valid */
872 for (cursor = match_list; cursor; cursor = cursor->next) {
874 url_match_t *match = (url_match_t*) cursor->data;
875 gchar *url = g_strndup (txt->str + match->offset, match->len);
876 gchar *repl = NULL; /* replacement */
878 /* the prefix is NULL: use the one that is already there */
879 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
880 match->prefix ? match->prefix : EMPTY_STRING,
883 /* replace the old thing with our hyperlink
884 * replacement thing */
885 g_string_erase (txt, match->offset, match->len);
886 g_string_insert (txt, match->offset, repl);
891 g_slice_free (url_match_t, match);
894 g_slist_free (match_list);
900 modest_text_utils_get_display_address (gchar *address)
907 g_return_val_if_fail (g_utf8_validate (address, -1, NULL), NULL);
909 g_strchug (address); /* remove leading whitespace */
911 /* <email@address> from display name */
912 cursor = g_strstr_len (address, strlen(address), "<");
913 if (cursor == address) /* there's nothing else? leave it */
918 /* remove (bla bla) from display name */
919 cursor = g_strstr_len (address, strlen(address), "(");
920 if (cursor == address) /* there's nothing else? leave it */
925 g_strchomp (address); /* remove trailing whitespace */
933 modest_text_utils_get_subject_prefix_len (const gchar *sub)
936 static const gchar* prefix[] = {
937 "Re:", "RE:", "Fwd:", "FWD:", "FW:", NULL
940 if (!sub || (sub[0] != 'R' && sub[0] != 'F')) /* optimization */
946 if (g_str_has_prefix(sub, prefix[i])) {
947 int prefix_len = strlen(prefix[i]);
948 if (sub[prefix_len] == ' ')
949 ++prefix_len; /* ignore space after prefix as well */
959 modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive)
964 /* work even when s1 and/or s2 == NULL */
965 if (G_UNLIKELY(s1 == s2))
968 /* if it's not case sensitive */
970 return strcmp (s1 ? s1 : "", s2 ? s2 : "");
972 n1 = g_utf8_collate_key (s1 ? s1 : "", -1);
973 n2 = g_utf8_collate_key (s2 ? s2 : "", -1);
975 result = strcmp (n1, n2);
985 modest_text_utils_get_display_date (time_t date)
988 const guint BUF_SIZE = 64;
989 gchar date_buf[BUF_SIZE];
990 gchar now_buf [BUF_SIZE];
994 /* use the localized dates */
995 modest_text_utils_strftime (date_buf, BUF_SIZE, "%x", date);
996 modest_text_utils_strftime (now_buf, BUF_SIZE, "%x", now);
998 /* if this is today, get the time instead of the date */
999 if (strcmp (date_buf, now_buf) == 0)
1000 modest_text_utils_strftime (date_buf, BUF_SIZE, "%X", date);
1002 return g_strdup(date_buf);
1006 modest_text_utils_validate_email_address (const gchar *email_address)
1009 const gchar *c = NULL, *domain = NULL;
1010 static gchar *rfc822_specials = "()<>@,;:\\\"[]";
1012 /* first we validate the name portion (name@domain) */
1013 for (c = email_address; *c; c++) {
1015 (c == email_address ||
1017 *(c - 1) == '\"')) {
1021 if (*c == '\\' && (*++c == ' '))
1023 if (*c <= ' ' || *c >= 127)
1036 if (*c <= ' ' || *c >= 127)
1038 if (strchr(rfc822_specials, *c))
1041 if (c == email_address || *(c - 1) == '.')
1044 /* next we validate the domain portion (name@domain) */
1045 if (!*(domain = ++c))
1049 if (c == domain || *(c - 1) == '.')
1053 if (*c <= ' ' || *c >= 127)
1055 if (strchr(rfc822_specials, *c))
1059 return (count >= 1) ? TRUE : FALSE;
1063 modest_text_utils_validate_recipient (const gchar *recipient)
1065 gchar *stripped, *current;
1067 gboolean has_error = FALSE;
1069 if (modest_text_utils_validate_email_address (recipient))
1071 stripped = g_strdup (recipient);
1072 stripped = g_strstrip (stripped);
1075 if (*current == '\0') {
1081 if (*current == '\"') {
1082 current = g_utf8_next_char (current);
1084 for (; *current != '\0'; current = g_utf8_next_char (current)) {
1085 if (*current == '\\') {
1086 /* TODO: This causes a warning, which breaks the build,
1087 * because a gchar cannot be < 0.
1089 if (current[1] <0) {
1094 } else if (*current == '\"') {
1096 current = g_utf8_next_char (current);
1102 for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
1103 if (*current == '<') {
1115 right_part = g_strdup (current);
1117 right_part = g_strstrip (right_part);
1119 if (g_str_has_prefix (right_part, "<") &&
1120 g_str_has_suffix (right_part, ">")) {
1124 address = g_strndup (right_part+1, strlen (right_part) - 2);
1125 g_free (right_part);
1126 valid = modest_text_utils_validate_email_address (address);
1130 g_free (right_part);
1137 modest_text_utils_get_display_size (guint64 size)
1139 const guint KB=1024;
1140 const guint MB=1024 * KB;
1141 const guint GB=1024 * MB;
1144 return g_strdup_printf(_FM("sfil_li_size_kb"), 0);
1145 if (0 < size && size < KB)
1146 return g_strdup_printf (_FM("sfil_li_size_kb"), 1);
1147 else if (KB <= size && size < 100 * KB)
1148 return g_strdup_printf (_FM("sfil_li_size_1kb_99kb"), size / KB);
1149 else if (100*KB <= size && size < MB)
1150 return g_strdup_printf (_FM("sfil_li_size_100kb_1mb"), (float) size / MB);
1151 else if (MB <= size && size < 10*MB)
1152 return g_strdup_printf (_FM("sfil_li_size_1mb_10mb"), (float) size / MB);
1153 else if (10*MB <= size && size < GB)
1154 return g_strdup_printf (_FM("sfil_li_size_10mb_1gb"), size / MB);
1156 return g_strdup_printf (_FM("sfil_li_size_1gb_or_greater"), (float) size / GB);
1160 get_email_from_address (const gchar * address)
1162 gchar *left_limit, *right_limit;
1164 left_limit = strstr (address, "<");
1165 right_limit = g_strrstr (address, ">");
1167 if ((left_limit == NULL)||(right_limit == NULL)|| (left_limit > right_limit))
1168 return g_strdup (address);
1170 return g_strndup (left_limit + 1, (right_limit - left_limit) - 1);