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,
107 static gchar* modest_text_utils_quote_html (const gchar *text,
109 const gchar *signature,
112 static gchar* get_email_from_address (const gchar *address);
115 /* ******************************************************************* */
116 /* ************************* PUBLIC FUNCTIONS ************************ */
117 /* ******************************************************************* */
120 modest_text_utils_quote (const gchar *text,
121 const gchar *content_type,
122 const gchar *signature,
124 const time_t sent_date,
128 gchar *retval, *cited;
130 g_return_val_if_fail (text, NULL);
131 g_return_val_if_fail (content_type, NULL);
133 cited = cite (sent_date, from);
135 if (content_type && strcmp (content_type, "text/html") == 0)
136 /* TODO: extract the <body> of the HTML and pass it to
138 retval = modest_text_utils_quote_html (text, cited, signature, attachments, limit);
140 retval = modest_text_utils_quote_plain_text (text, cited, signature, attachments, limit);
149 modest_text_utils_cite (const gchar *text,
150 const gchar *content_type,
151 const gchar *signature,
158 g_return_val_if_fail (text, NULL);
159 g_return_val_if_fail (content_type, NULL);
162 retval = g_strdup ("");
163 else if (!strcmp(content_type, "text/html")) {
164 tmp_sig = g_strconcat ("\n", signature, NULL);
165 retval = modest_text_utils_convert_to_html_body(tmp_sig);
168 retval = g_strconcat ("\n", signature, NULL);
175 forward_cite (const gchar *from,
178 const gchar *subject)
180 return g_strdup_printf ("%s\n%s %s\n%s %s\n%s %s\n%s %s\n",
182 FROM_STRING, (from)?from:"",
184 TO_STRING, (to)?to:"",
185 SUBJECT_STRING, (subject)?subject:"");
189 modest_text_utils_inline (const gchar *text,
190 const gchar *content_type,
191 const gchar *signature,
195 const gchar *subject)
201 g_return_val_if_fail (text, NULL);
202 g_return_val_if_fail (content_type, NULL);
204 modest_text_utils_strftime (sent_str, 100, "%c", sent_date);
206 cited = forward_cite (from, sent_str, to, subject);
208 if (content_type && strcmp (content_type, "text/html") == 0)
209 retval = modest_text_utils_quote_html (text, cited, signature, NULL, 80);
211 retval = modest_text_utils_quote_plain_text (text, cited, signature, NULL, 80);
217 /* just to prevent warnings:
218 * warning: `%x' yields only last 2 digits of year in some locales
221 modest_text_utils_strftime(char *s, gsize max, const char *fmt, time_t timet)
225 /* does not work on old maemo glib:
226 * g_date_set_time_t (&date, timet);
228 localtime_r (&timet, &tm);
229 return strftime(s, max, fmt, &tm);
233 modest_text_utils_derived_subject (const gchar *subject, const gchar *prefix)
237 g_return_val_if_fail (prefix, NULL);
240 return g_strdup (prefix);
242 tmp = g_strchug (g_strdup (subject));
244 if (!strncmp (tmp, prefix, strlen (prefix))) {
248 return g_strdup_printf ("%s %s", prefix, subject);
253 modest_text_utils_remove_address (const gchar *address_list, const gchar *address)
255 gchar *dup, *token, *ptr, *result;
256 GString *filtered_emails;
257 gchar *email_address;
259 g_return_val_if_fail (address_list, NULL);
262 return g_strdup (address_list);
264 email_address = get_email_from_address (address);
266 /* search for substring */
267 if (!strstr ((const char *) address_list, (const char *) email_address)) {
268 g_free (email_address);
269 return g_strdup (address_list);
272 dup = g_strdup (address_list);
273 filtered_emails = g_string_new (NULL);
275 token = strtok_r (dup, ",", &ptr);
277 while (token != NULL) {
278 /* Add to list if not found */
279 if (!strstr ((const char *) token, (const char *) email_address)) {
280 if (filtered_emails->len == 0)
281 g_string_append_printf (filtered_emails, "%s", g_strstrip (token));
283 g_string_append_printf (filtered_emails, ",%s", g_strstrip (token));
285 token = strtok_r (NULL, ",", &ptr);
287 result = filtered_emails->str;
290 g_free (email_address);
292 g_string_free (filtered_emails, FALSE);
298 modest_text_utils_convert_buffer_to_html (GString *html, const gchar *data)
301 gboolean space_seen = FALSE;
306 /* replace with special html chars where needed*/
307 for (i = 0; i != len; ++i) {
310 if (space_seen && kar != ' ') {
311 g_string_append_c (html, ' ');
316 case 0: break; /* ignore embedded \0s */
317 case '<' : g_string_append (html, "<"); break;
318 case '>' : g_string_append (html, ">"); break;
319 case '&' : g_string_append (html, "&"); break;
320 case '"' : g_string_append (html, """); break;
321 case '\'' : g_string_append (html, "'"); break;
322 case '\n' : g_string_append (html, "<br>\n"); break;
323 case '\t' : g_string_append (html, " "); break; /* note the space at the end*/
325 if (space_seen) { /* second space in a row */
326 g_string_append (html, " ");
332 g_string_append_c (html, kar);
338 modest_text_utils_convert_to_html (const gchar *data)
347 html = g_string_sized_new (1.5 * len); /* just a guess... */
349 g_string_append_printf (html,
351 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf8\">"
355 modest_text_utils_convert_buffer_to_html (html, data);
357 g_string_append (html, "</body></html>");
358 hyperlinkify_plain_text (html);
360 return g_string_free (html, FALSE);
364 modest_text_utils_convert_to_html_body (const gchar *data)
373 html = g_string_sized_new (1.5 * len); /* just a guess... */
375 modest_text_utils_convert_buffer_to_html (html, data);
377 hyperlinkify_plain_text (html);
379 return g_string_free (html, FALSE);
383 modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes)
385 gchar *current, *start, *last_blank;
386 gint start_offset = 0, current_offset = 0;
388 g_return_if_fail (start_indexes != NULL);
389 g_return_if_fail (end_indexes != NULL);
391 start = (gchar *) addresses;
395 while (*current != '\0') {
396 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
397 start = g_utf8_next_char (start);
399 last_blank = current;
400 } else if ((*current == ',')||(*current == ';')) {
401 gint *start_index, *end_index;
402 start_index = g_new0(gint, 1);
403 end_index = g_new0(gint, 1);
404 *start_index = start_offset;
405 *end_index = current_offset;
406 *start_indexes = g_slist_prepend (*start_indexes, start_index);
407 *end_indexes = g_slist_prepend (*end_indexes, end_index);
408 start = g_utf8_next_char (current);
409 start_offset = current_offset + 1;
411 } else if (*current == '"') {
412 current = g_utf8_next_char (current);
414 while ((*current != '"')&&(*current != '\0')) {
415 current = g_utf8_next_char (current);
420 current = g_utf8_next_char (current);
424 if (start != current) {
425 gint *start_index, *end_index;
426 start_index = g_new0(gint, 1);
427 end_index = g_new0(gint, 1);
428 *start_index = start_offset;
429 *end_index = current_offset;
430 *start_indexes = g_slist_prepend (*start_indexes, start_index);
431 *end_indexes = g_slist_prepend (*end_indexes, end_index);
434 *start_indexes = g_slist_reverse (*start_indexes);
435 *end_indexes = g_slist_reverse (*end_indexes);
441 modest_text_utils_split_addresses_list (const gchar *addresses)
443 gchar *current, *start, *last_blank;
444 GSList *result = NULL;
446 start = (gchar *) addresses;
450 while (*current != '\0') {
451 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
452 start = g_utf8_next_char (start);
453 last_blank = current;
454 } else if ((*current == ',')||(*current == ';')) {
455 gchar *new_address = NULL;
456 new_address = g_strndup (start, current - last_blank);
457 result = g_slist_prepend (result, new_address);
458 start = g_utf8_next_char (current);
460 } else if (*current == '\"') {
461 if (current == start) {
462 current = g_utf8_next_char (current);
463 start = g_utf8_next_char (start);
465 while ((*current != '\"')&&(*current != '\0'))
466 current = g_utf8_next_char (current);
469 current = g_utf8_next_char (current);
472 if (start != current) {
473 gchar *new_address = NULL;
474 new_address = g_strndup (start, current - last_blank);
475 result = g_slist_prepend (result, new_address);
478 result = g_slist_reverse (result);
484 modest_text_utils_address_range_at_position (const gchar *recipients_list,
489 gchar *current = NULL;
490 gint range_start = 0;
493 gboolean is_quoted = FALSE;
496 for (current = (gchar *) recipients_list; *current != '\0'; current = g_utf8_find_next_char (current, NULL)) {
497 gunichar c = g_utf8_get_char (current);
499 if ((c == ',') && (!is_quoted)) {
500 if (index < position) {
501 range_start = index + 1;
505 } else if (c == '\"') {
506 is_quoted = !is_quoted;
507 } else if ((c == ' ') &&(range_start == index)) {
515 *start = range_start;
521 /* ******************************************************************* */
522 /* ************************* UTILIY FUNCTIONS ************************ */
523 /* ******************************************************************* */
526 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
532 return g_string_new("");
540 gs = g_string_new_len (i0, iter - i0);
544 get_indent_level (const char *l)
561 /* if we hit the signature marker "-- ", we return -(indent + 1). This
562 * stops reformatting.
564 if (strcmp (l, "-- ") == 0) {
572 unquote_line (GString * l)
587 g_string_erase (l, 0, p - l->str);
591 append_quoted (GString * buf, int indent, const GString * str,
596 indent = indent < 0 ? abs (indent) - 1 : indent;
597 for (i = 0; i <= indent; i++) {
598 g_string_append (buf, "> ");
601 g_string_append_len (buf, str->str, cutpoint);
603 g_string_append (buf, str->str);
605 g_string_append (buf, "\n");
609 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
612 const gchar *pos, *last;
615 indent = indent < 0 ? abs (indent) - 1 : indent;
619 uni = g_utf8_to_ucs4_fast (s, -1, NULL);
621 if ((index + 2 * indent > limit) && last) {
625 if (g_unichar_isspace (uni[index])) {
628 pos = g_utf8_next_char (pos);
636 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
641 if (last + 2 * indent < limit)
644 for (i = strlen (s); i > 0; i--) {
646 if (i + 2 * indent <= limit) {
657 get_breakpoint (const gchar * s, const gint indent, const gint limit)
660 if (g_utf8_validate (s, -1, NULL)) {
661 return get_breakpoint_utf8 (s, indent, limit);
662 } else { /* assume ASCII */
663 //g_warning("invalid UTF-8 in msg");
664 return get_breakpoint_ascii (s, indent, limit);
669 cite (const time_t sent_date, const gchar *from)
671 return g_strdup (_("mcen_ia_editor_original_message"));
675 quoted_attachments (GList *attachments)
678 GString *result = g_string_new ("");
679 for (node = attachments; node != NULL; node = g_list_next (node)) {
680 gchar *filename = (gchar *) node->data;
681 g_string_append_printf ( result, "%s %s\n", _("mcen_ia_editor_attach_filename"), filename);
684 return g_string_free (result, FALSE);
689 modest_text_utils_quote_plain_text (const gchar *text,
691 const gchar *signature,
696 gint indent, breakpoint, rem_indent = 0;
697 GString *q, *l, *remaining;
699 gchar *attachments_string = NULL;
701 /* remaining will store the rest of the line if we have to break it */
702 q = g_string_new ("\n");
703 q = g_string_append (q, cite);
704 q = g_string_append_c (q, '\n');
705 remaining = g_string_new ("");
710 l = get_next_line (text, len, iter);
711 iter = iter + l->len + 1;
712 indent = get_indent_level (l->str);
715 if (remaining->len) {
716 if (l->len && indent == rem_indent) {
717 g_string_prepend (l, " ");
718 g_string_prepend (l, remaining->str);
722 get_breakpoint (remaining->str,
725 append_quoted (q, rem_indent,
726 remaining, breakpoint);
727 g_string_erase (remaining, 0,
729 if (remaining->str[0] == ' ') {
730 g_string_erase (remaining, 0,
733 } while (remaining->len);
736 g_string_free (remaining, TRUE);
737 breakpoint = get_breakpoint (l->str, indent, limit);
738 remaining = g_string_new (l->str + breakpoint);
739 if (remaining->str[0] == ' ') {
740 g_string_erase (remaining, 0, 1);
743 append_quoted (q, indent, l, breakpoint);
744 g_string_free (l, TRUE);
745 } while ((iter < text + len) || (remaining->str[0]));
747 attachments_string = quoted_attachments (attachments);
748 q = g_string_append (q, attachments_string);
749 g_free (attachments_string);
751 if (signature != NULL) {
752 q = g_string_append_c (q, '\n');
753 q = g_string_append (q, signature);
756 return g_string_free (q, FALSE);
760 modest_text_utils_quote_html (const gchar *text,
762 const gchar *signature,
766 gchar *result = NULL;
767 gchar *signature_result = NULL;
768 const gchar *format = \
769 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" \
773 "<pre>%s<br/>%s<br/>%s</pre>\n" \
776 gchar *attachments_string = NULL;
777 gchar *q_attachments_string = NULL;
778 gchar *q_cite = NULL;
779 gchar *html_text = NULL;
781 if (signature == NULL)
782 signature_result = g_strdup ("");
784 signature_result = modest_text_utils_convert_to_html_body (signature);
786 attachments_string = quoted_attachments (attachments);
787 q_attachments_string = modest_text_utils_convert_to_html_body (attachments_string);
788 q_cite = modest_text_utils_convert_to_html_body (cite);
789 html_text = modest_text_utils_convert_to_html_body (text);
790 result = g_strdup_printf (format, signature_result, q_cite, html_text, q_attachments_string);
793 g_free (attachments_string);
794 g_free (q_attachments_string);
795 g_free (signature_result);
800 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
802 return match2->offset - match1->offset;
808 * check if the match is inside an existing match... */
810 chk_partial_match (const url_match_t *match, guint* offset)
812 if (*offset >= match->offset && *offset < match->offset + match->len)
817 get_url_matches (GString *txt)
820 guint rv, i, offset = 0;
821 GSList *match_list = NULL;
823 static url_match_pattern_t patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
824 const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
826 /* initalize the regexps */
827 for (i = 0; i != pattern_num; ++i) {
828 patterns[i].preg = g_slice_new0 (regex_t);
830 /* this should not happen */
831 g_return_val_if_fail (regcomp (patterns[i].preg, patterns[i].regex,
832 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0, NULL);
834 /* find all the matches */
835 for (i = 0; i != pattern_num; ++i) {
839 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
840 g_return_val_if_fail (rv == REG_NOMATCH, NULL); /* this should not happen */
841 break; /* try next regexp */
846 /* FIXME: optimize this */
847 /* to avoid partial matches on something that was already found... */
848 /* check_partial_match will put -1 in the data ptr if that is the case */
849 test_offset = offset + rm.rm_so;
850 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
852 /* make a list of our matches (<offset, len, prefix> tupels)*/
853 if (test_offset != -1) {
854 url_match_t *match = g_slice_new (url_match_t);
855 match->offset = offset + rm.rm_so;
856 match->len = rm.rm_eo - rm.rm_so;
857 match->prefix = patterns[i].prefix;
858 match_list = g_slist_prepend (match_list, match);
864 for (i = 0; i != pattern_num; ++i) {
865 regfree (patterns[i].preg);
866 g_slice_free (regex_t, patterns[i].preg);
867 } /* don't free patterns itself -- it's static */
869 /* now sort the list, so the matches are in reverse order of occurence.
870 * that way, we can do the replacements starting from the end, so we don't need
871 * to recalculate the offsets
873 match_list = g_slist_sort (match_list,
874 (GCompareFunc)cmp_offsets_reverse);
881 hyperlinkify_plain_text (GString *txt)
884 GSList *match_list = get_url_matches (txt);
886 /* we will work backwards, so the offsets stay valid */
887 for (cursor = match_list; cursor; cursor = cursor->next) {
889 url_match_t *match = (url_match_t*) cursor->data;
890 gchar *url = g_strndup (txt->str + match->offset, match->len);
891 gchar *repl = NULL; /* replacement */
893 /* the prefix is NULL: use the one that is already there */
894 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
895 match->prefix ? match->prefix : EMPTY_STRING,
898 /* replace the old thing with our hyperlink
899 * replacement thing */
900 g_string_erase (txt, match->offset, match->len);
901 g_string_insert (txt, match->offset, repl);
906 g_slice_free (url_match_t, match);
909 g_slist_free (match_list);
915 modest_text_utils_get_display_address (gchar *address)
922 g_return_val_if_fail (g_utf8_validate (address, -1, NULL), NULL);
924 g_strchug (address); /* remove leading whitespace */
926 /* <email@address> from display name */
927 cursor = g_strstr_len (address, strlen(address), "<");
928 if (cursor == address) /* there's nothing else? leave it */
933 /* remove (bla bla) from display name */
934 cursor = g_strstr_len (address, strlen(address), "(");
935 if (cursor == address) /* there's nothing else? leave it */
940 g_strchomp (address); /* remove trailing whitespace */
948 modest_text_utils_get_subject_prefix_len (const gchar *sub)
951 static const gchar* prefix[] = {
952 "Re:", "RE:", "RV:", "re:"
953 "Fwd:", "FWD:", "FW:", "fwd:", "Fw:", "fw:", NULL
956 if (!sub || (sub[0] != 'R' && sub[0] != 'F' && sub[0] != 'r' && sub[0] != 'f')) /* optimization */
962 if (g_str_has_prefix(sub, prefix[i])) {
963 int prefix_len = strlen(prefix[i]);
964 if (sub[prefix_len] == ' ')
965 ++prefix_len; /* ignore space after prefix as well */
975 modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive)
980 /* work even when s1 and/or s2 == NULL */
981 if (G_UNLIKELY(s1 == s2))
984 /* if it's not case sensitive */
986 return strcmp (s1 ? s1 : "", s2 ? s2 : "");
988 n1 = g_utf8_collate_key (s1 ? s1 : "", -1);
989 n2 = g_utf8_collate_key (s2 ? s2 : "", -1);
991 result = strcmp (n1, n2);
1001 modest_text_utils_get_display_date (time_t date)
1004 const guint BUF_SIZE = 64;
1005 gchar date_buf[BUF_SIZE];
1006 gchar now_buf [BUF_SIZE];
1010 /* use the localized dates */
1011 modest_text_utils_strftime (date_buf, BUF_SIZE, "%x", date);
1012 modest_text_utils_strftime (now_buf, BUF_SIZE, "%x", now);
1014 /* if this is today, get the time instead of the date */
1015 if (strcmp (date_buf, now_buf) == 0)
1016 modest_text_utils_strftime (date_buf, BUF_SIZE, "%X", date);
1018 return g_strdup(date_buf);
1022 modest_text_utils_validate_email_address (const gchar *email_address)
1025 const gchar *c = NULL, *domain = NULL;
1026 static gchar *rfc822_specials = "()<>@,;:\\\"[]";
1028 /* first we validate the name portion (name@domain) */
1029 for (c = email_address; *c; c++) {
1031 (c == email_address ||
1033 *(c - 1) == '\"')) {
1037 if (*c == '\\' && (*++c == ' '))
1039 if (*c <= ' ' || *c >= 127)
1052 if (*c <= ' ' || *c >= 127)
1054 if (strchr(rfc822_specials, *c))
1057 if (c == email_address || *(c - 1) == '.')
1060 /* next we validate the domain portion (name@domain) */
1061 if (!*(domain = ++c))
1065 if (c == domain || *(c - 1) == '.')
1069 if (*c <= ' ' || *c >= 127)
1071 if (strchr(rfc822_specials, *c))
1075 return (count >= 1) ? TRUE : FALSE;
1079 modest_text_utils_validate_recipient (const gchar *recipient)
1081 gchar *stripped, *current;
1083 gboolean has_error = FALSE;
1085 if (modest_text_utils_validate_email_address (recipient))
1087 stripped = g_strdup (recipient);
1088 stripped = g_strstrip (stripped);
1091 if (*current == '\0') {
1097 if (*current == '\"') {
1098 current = g_utf8_next_char (current);
1100 for (; *current != '\0'; current = g_utf8_next_char (current)) {
1101 if (*current == '\\') {
1102 /* TODO: This causes a warning, which breaks the build,
1103 * because a gchar cannot be < 0.
1105 if (current[1] <0) {
1110 } else if (*current == '\"') {
1112 current = g_utf8_next_char (current);
1118 for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
1119 if (*current == '<') {
1131 right_part = g_strdup (current);
1133 right_part = g_strstrip (right_part);
1135 if (g_str_has_prefix (right_part, "<") &&
1136 g_str_has_suffix (right_part, ">")) {
1140 address = g_strndup (right_part+1, strlen (right_part) - 2);
1141 g_free (right_part);
1142 valid = modest_text_utils_validate_email_address (address);
1146 g_free (right_part);
1153 modest_text_utils_get_display_size (guint64 size)
1155 const guint KB=1024;
1156 const guint MB=1024 * KB;
1157 const guint GB=1024 * MB;
1160 return g_strdup_printf(_FM("sfil_li_size_kb"), 0);
1161 if (0 < size && size < KB)
1162 return g_strdup_printf (_FM("sfil_li_size_kb"), 1);
1163 else if (KB <= size && size < 100 * KB)
1164 return g_strdup_printf (_FM("sfil_li_size_1kb_99kb"), size / KB);
1165 else if (100*KB <= size && size < MB)
1166 return g_strdup_printf (_FM("sfil_li_size_100kb_1mb"), (float) size / MB);
1167 else if (MB <= size && size < 10*MB)
1168 return g_strdup_printf (_FM("sfil_li_size_1mb_10mb"), (float) size / MB);
1169 else if (10*MB <= size && size < GB)
1170 return g_strdup_printf (_FM("sfil_li_size_10mb_1gb"), size / MB);
1172 return g_strdup_printf (_FM("sfil_li_size_1gb_or_greater"), (float) size / GB);
1176 get_email_from_address (const gchar * address)
1178 gchar *left_limit, *right_limit;
1180 left_limit = strstr (address, "<");
1181 right_limit = g_strrstr (address, ">");
1183 if ((left_limit == NULL)||(right_limit == NULL)|| (left_limit > right_limit))
1184 return g_strdup (address);
1186 return g_strndup (left_limit + 1, (right_limit - left_limit) - 1);