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;
303 guint break_dist = 0; /* distance since last break point */
307 /* replace with special html chars where needed*/
308 for (i = 0; i != len; ++i) {
311 if (space_seen && kar != ' ') {
312 g_string_append_c (html, ' ');
316 /* we artificially insert a breakpoint (newline)
317 * after 256, to make sure our lines are not so long
318 * they will DOS the regexping later
320 if (++break_dist == 256) {
321 g_string_append_c (html, '\n');
326 case 0: break; /* ignore embedded \0s */
327 case '<' : g_string_append (html, "<"); break;
328 case '>' : g_string_append (html, ">"); break;
329 case '&' : g_string_append (html, "&"); break;
330 case '"' : g_string_append (html, """); break;
331 case '\'' : g_string_append (html, "'"); break;
332 case '\n' : g_string_append (html, "<br>\n"); break_dist= 0; break;
333 case '\t' : g_string_append (html, " "); break_dist=0; break; /* note the space at the end*/
336 if (space_seen) { /* second space in a row */
337 g_string_append (html, " ");
343 g_string_append_c (html, kar);
349 modest_text_utils_convert_to_html (const gchar *data)
358 html = g_string_sized_new (1.5 * len); /* just a guess... */
360 g_string_append_printf (html,
362 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf8\">"
366 modest_text_utils_convert_buffer_to_html (html, data);
368 g_string_append (html, "</body></html>");
369 hyperlinkify_plain_text (html);
371 return g_string_free (html, FALSE);
375 modest_text_utils_convert_to_html_body (const gchar *data)
384 html = g_string_sized_new (1.5 * len); /* just a guess... */
386 modest_text_utils_convert_buffer_to_html (html, data);
388 hyperlinkify_plain_text (html);
390 return g_string_free (html, FALSE);
394 modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes)
396 gchar *current, *start, *last_blank;
397 gint start_offset = 0, current_offset = 0;
399 g_return_if_fail (start_indexes != NULL);
400 g_return_if_fail (end_indexes != NULL);
402 start = (gchar *) addresses;
406 while (*current != '\0') {
407 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
408 start = g_utf8_next_char (start);
410 last_blank = current;
411 } else if ((*current == ',')||(*current == ';')) {
412 gint *start_index, *end_index;
413 start_index = g_new0(gint, 1);
414 end_index = g_new0(gint, 1);
415 *start_index = start_offset;
416 *end_index = current_offset;
417 *start_indexes = g_slist_prepend (*start_indexes, start_index);
418 *end_indexes = g_slist_prepend (*end_indexes, end_index);
419 start = g_utf8_next_char (current);
420 start_offset = current_offset + 1;
422 } else if (*current == '"') {
423 current = g_utf8_next_char (current);
425 while ((*current != '"')&&(*current != '\0')) {
426 current = g_utf8_next_char (current);
431 current = g_utf8_next_char (current);
435 if (start != current) {
436 gint *start_index, *end_index;
437 start_index = g_new0(gint, 1);
438 end_index = g_new0(gint, 1);
439 *start_index = start_offset;
440 *end_index = current_offset;
441 *start_indexes = g_slist_prepend (*start_indexes, start_index);
442 *end_indexes = g_slist_prepend (*end_indexes, end_index);
445 *start_indexes = g_slist_reverse (*start_indexes);
446 *end_indexes = g_slist_reverse (*end_indexes);
452 modest_text_utils_split_addresses_list (const gchar *addresses)
454 gchar *current, *start, *last_blank;
455 GSList *result = NULL;
457 start = (gchar *) addresses;
461 while (*current != '\0') {
462 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
463 start = g_utf8_next_char (start);
464 last_blank = current;
465 } else if ((*current == ',')||(*current == ';')) {
466 gchar *new_address = NULL;
467 new_address = g_strndup (start, current - last_blank);
468 result = g_slist_prepend (result, new_address);
469 start = g_utf8_next_char (current);
471 } else if (*current == '\"') {
472 if (current == start) {
473 current = g_utf8_next_char (current);
474 start = g_utf8_next_char (start);
476 while ((*current != '\"')&&(*current != '\0'))
477 current = g_utf8_next_char (current);
480 current = g_utf8_next_char (current);
483 if (start != current) {
484 gchar *new_address = NULL;
485 new_address = g_strndup (start, current - last_blank);
486 result = g_slist_prepend (result, new_address);
489 result = g_slist_reverse (result);
495 modest_text_utils_address_range_at_position (const gchar *recipients_list,
500 gchar *current = NULL;
501 gint range_start = 0;
504 gboolean is_quoted = FALSE;
507 for (current = (gchar *) recipients_list; *current != '\0'; current = g_utf8_find_next_char (current, NULL)) {
508 gunichar c = g_utf8_get_char (current);
510 if ((c == ',') && (!is_quoted)) {
511 if (index < position) {
512 range_start = index + 1;
516 } else if (c == '\"') {
517 is_quoted = !is_quoted;
518 } else if ((c == ' ') &&(range_start == index)) {
526 *start = range_start;
532 /* ******************************************************************* */
533 /* ************************* UTILIY FUNCTIONS ************************ */
534 /* ******************************************************************* */
537 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
543 return g_string_new("");
551 gs = g_string_new_len (i0, iter - i0);
555 get_indent_level (const char *l)
572 /* if we hit the signature marker "-- ", we return -(indent + 1). This
573 * stops reformatting.
575 if (strcmp (l, "-- ") == 0) {
583 unquote_line (GString * l)
598 g_string_erase (l, 0, p - l->str);
602 append_quoted (GString * buf, int indent, const GString * str,
607 indent = indent < 0 ? abs (indent) - 1 : indent;
608 for (i = 0; i <= indent; i++) {
609 g_string_append (buf, "> ");
612 g_string_append_len (buf, str->str, cutpoint);
614 g_string_append (buf, str->str);
616 g_string_append (buf, "\n");
620 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
623 const gchar *pos, *last;
626 indent = indent < 0 ? abs (indent) - 1 : indent;
630 uni = g_utf8_to_ucs4_fast (s, -1, NULL);
632 if ((index + 2 * indent > limit) && last) {
636 if (g_unichar_isspace (uni[index])) {
639 pos = g_utf8_next_char (pos);
647 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
652 if (last + 2 * indent < limit)
655 for (i = strlen (s); i > 0; i--) {
657 if (i + 2 * indent <= limit) {
668 get_breakpoint (const gchar * s, const gint indent, const gint limit)
671 if (g_utf8_validate (s, -1, NULL)) {
672 return get_breakpoint_utf8 (s, indent, limit);
673 } else { /* assume ASCII */
674 //g_warning("invalid UTF-8 in msg");
675 return get_breakpoint_ascii (s, indent, limit);
680 cite (const time_t sent_date, const gchar *from)
682 return g_strdup (_("mcen_ia_editor_original_message"));
686 quoted_attachments (GList *attachments)
689 GString *result = g_string_new ("");
690 for (node = attachments; node != NULL; node = g_list_next (node)) {
691 gchar *filename = (gchar *) node->data;
692 g_string_append_printf ( result, "%s %s\n", _("mcen_ia_editor_attach_filename"), filename);
695 return g_string_free (result, FALSE);
700 modest_text_utils_quote_plain_text (const gchar *text,
702 const gchar *signature,
707 gint indent, breakpoint, rem_indent = 0;
708 GString *q, *l, *remaining;
710 gchar *attachments_string = NULL;
712 /* remaining will store the rest of the line if we have to break it */
713 q = g_string_new ("\n");
714 q = g_string_append (q, cite);
715 q = g_string_append_c (q, '\n');
716 remaining = g_string_new ("");
721 l = get_next_line (text, len, iter);
722 iter = iter + l->len + 1;
723 indent = get_indent_level (l->str);
726 if (remaining->len) {
727 if (l->len && indent == rem_indent) {
728 g_string_prepend (l, " ");
729 g_string_prepend (l, remaining->str);
733 get_breakpoint (remaining->str,
736 append_quoted (q, rem_indent,
737 remaining, breakpoint);
738 g_string_erase (remaining, 0,
740 if (remaining->str[0] == ' ') {
741 g_string_erase (remaining, 0,
744 } while (remaining->len);
747 g_string_free (remaining, TRUE);
748 breakpoint = get_breakpoint (l->str, indent, limit);
749 remaining = g_string_new (l->str + breakpoint);
750 if (remaining->str[0] == ' ') {
751 g_string_erase (remaining, 0, 1);
754 append_quoted (q, indent, l, breakpoint);
755 g_string_free (l, TRUE);
756 } while ((iter < text + len) || (remaining->str[0]));
758 attachments_string = quoted_attachments (attachments);
759 q = g_string_append (q, attachments_string);
760 g_free (attachments_string);
762 if (signature != NULL) {
763 q = g_string_append_c (q, '\n');
764 q = g_string_append (q, signature);
767 return g_string_free (q, FALSE);
771 modest_text_utils_quote_html (const gchar *text,
773 const gchar *signature,
777 gchar *result = NULL;
778 gchar *signature_result = NULL;
779 const gchar *format = \
780 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" \
784 "<pre>%s<br/>%s<br/>%s</pre>\n" \
787 gchar *attachments_string = NULL;
788 gchar *q_attachments_string = NULL;
789 gchar *q_cite = NULL;
790 gchar *html_text = NULL;
792 if (signature == NULL)
793 signature_result = g_strdup ("");
795 signature_result = modest_text_utils_convert_to_html_body (signature);
797 attachments_string = quoted_attachments (attachments);
798 q_attachments_string = modest_text_utils_convert_to_html_body (attachments_string);
799 q_cite = modest_text_utils_convert_to_html_body (cite);
800 html_text = modest_text_utils_convert_to_html_body (text);
801 result = g_strdup_printf (format, signature_result, q_cite, html_text, q_attachments_string);
804 g_free (attachments_string);
805 g_free (q_attachments_string);
806 g_free (signature_result);
811 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
813 return match2->offset - match1->offset;
819 * check if the match is inside an existing match... */
821 chk_partial_match (const url_match_t *match, guint* offset)
823 if (*offset >= match->offset && *offset < match->offset + match->len)
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 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
851 g_return_val_if_fail (rv == REG_NOMATCH, NULL); /* this should not happen */
852 break; /* try next regexp */
857 /* FIXME: optimize this */
858 /* to avoid partial matches on something that was already found... */
859 /* check_partial_match will put -1 in the data ptr if that is the case */
860 test_offset = offset + rm.rm_so;
861 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
863 /* make a list of our matches (<offset, len, prefix> tupels)*/
864 if (test_offset != -1) {
865 url_match_t *match = g_slice_new (url_match_t);
866 match->offset = offset + rm.rm_so;
867 match->len = rm.rm_eo - rm.rm_so;
868 match->prefix = patterns[i].prefix;
869 match_list = g_slist_prepend (match_list, match);
875 for (i = 0; i != pattern_num; ++i) {
876 regfree (patterns[i].preg);
877 g_slice_free (regex_t, patterns[i].preg);
878 } /* don't free patterns itself -- it's static */
880 /* now sort the list, so the matches are in reverse order of occurence.
881 * that way, we can do the replacements starting from the end, so we don't need
882 * to recalculate the offsets
884 match_list = g_slist_sort (match_list,
885 (GCompareFunc)cmp_offsets_reverse);
892 hyperlinkify_plain_text (GString *txt)
895 GSList *match_list = get_url_matches (txt);
897 /* we will work backwards, so the offsets stay valid */
898 for (cursor = match_list; cursor; cursor = cursor->next) {
900 url_match_t *match = (url_match_t*) cursor->data;
901 gchar *url = g_strndup (txt->str + match->offset, match->len);
902 gchar *repl = NULL; /* replacement */
904 /* the prefix is NULL: use the one that is already there */
905 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
906 match->prefix ? match->prefix : EMPTY_STRING,
909 /* replace the old thing with our hyperlink
910 * replacement thing */
911 g_string_erase (txt, match->offset, match->len);
912 g_string_insert (txt, match->offset, repl);
917 g_slice_free (url_match_t, match);
920 g_slist_free (match_list);
926 modest_text_utils_get_display_address (gchar *address)
933 g_return_val_if_fail (g_utf8_validate (address, -1, NULL), NULL);
935 g_strchug (address); /* remove leading whitespace */
937 /* <email@address> from display name */
938 cursor = g_strstr_len (address, strlen(address), "<");
939 if (cursor == address) /* there's nothing else? leave it */
944 /* remove (bla bla) from display name */
945 cursor = g_strstr_len (address, strlen(address), "(");
946 if (cursor == address) /* there's nothing else? leave it */
951 g_strchomp (address); /* remove trailing whitespace */
959 modest_text_utils_get_subject_prefix_len (const gchar *sub)
962 static const gchar* prefix[] = {
963 "Re:", "RE:", "RV:", "re:"
964 "Fwd:", "FWD:", "FW:", "fwd:", "Fw:", "fw:", NULL
967 if (!sub || (sub[0] != 'R' && sub[0] != 'F' && sub[0] != 'r' && sub[0] != 'f')) /* optimization */
973 if (g_str_has_prefix(sub, prefix[i])) {
974 int prefix_len = strlen(prefix[i]);
975 if (sub[prefix_len] == ' ')
976 ++prefix_len; /* ignore space after prefix as well */
986 modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive)
991 /* work even when s1 and/or s2 == NULL */
992 if (G_UNLIKELY(s1 == s2))
995 /* if it's not case sensitive */
997 return strcmp (s1 ? s1 : "", s2 ? s2 : "");
999 n1 = g_utf8_collate_key (s1 ? s1 : "", -1);
1000 n2 = g_utf8_collate_key (s2 ? s2 : "", -1);
1002 result = strcmp (n1, n2);
1012 modest_text_utils_get_display_date (time_t date)
1015 const guint BUF_SIZE = 64;
1016 gchar date_buf[BUF_SIZE];
1017 gchar now_buf [BUF_SIZE];
1021 /* use the localized dates */
1022 modest_text_utils_strftime (date_buf, BUF_SIZE, "%x", date);
1023 modest_text_utils_strftime (now_buf, BUF_SIZE, "%x", now);
1025 /* if this is today, get the time instead of the date */
1026 if (strcmp (date_buf, now_buf) == 0)
1027 modest_text_utils_strftime (date_buf, BUF_SIZE, "%X", date);
1029 return g_strdup(date_buf);
1033 modest_text_utils_validate_email_address (const gchar *email_address, const gchar **invalid_char_position)
1036 const gchar *c = NULL, *domain = NULL;
1037 static gchar *rfc822_specials = "()<>@,;:\\\"[]&";
1039 if (invalid_char_position != NULL)
1040 *invalid_char_position = NULL;
1042 /* first we validate the name portion (name@domain) */
1043 for (c = email_address; *c; c++) {
1045 (c == email_address ||
1047 *(c - 1) == '\"')) {
1051 if (*c == '\\' && (*++c == ' '))
1053 if (*c <= ' ' || *c >= 127)
1066 if (*c <= ' ' || *c >= 127)
1068 if (strchr(rfc822_specials, *c)) {
1069 if (invalid_char_position)
1070 *invalid_char_position = c;
1074 if (c == email_address || *(c - 1) == '.')
1077 /* next we validate the domain portion (name@domain) */
1078 if (!*(domain = ++c))
1082 if (c == domain || *(c - 1) == '.')
1086 if (*c <= ' ' || *c >= 127)
1088 if (strchr(rfc822_specials, *c)) {
1089 if (invalid_char_position)
1090 *invalid_char_position = c;
1095 return (count >= 1) ? TRUE : FALSE;
1099 modest_text_utils_validate_recipient (const gchar *recipient, const gchar **invalid_char_position)
1101 gchar *stripped, *current;
1103 gboolean has_error = FALSE;
1105 if (modest_text_utils_validate_email_address (recipient, invalid_char_position))
1107 stripped = g_strdup (recipient);
1108 stripped = g_strstrip (stripped);
1111 if (*current == '\0') {
1117 if (*current == '\"') {
1118 current = g_utf8_next_char (current);
1120 for (; *current != '\0'; current = g_utf8_next_char (current)) {
1121 if (*current == '\\') {
1122 /* TODO: This causes a warning, which breaks the build,
1123 * because a gchar cannot be < 0.
1125 if (current[1] <0) {
1130 } else if (*current == '\"') {
1132 current = g_utf8_next_char (current);
1138 for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
1139 if (*current == '<') {
1151 right_part = g_strdup (current);
1153 right_part = g_strstrip (right_part);
1155 if (g_str_has_prefix (right_part, "<") &&
1156 g_str_has_suffix (right_part, ">")) {
1160 address = g_strndup (right_part+1, strlen (right_part) - 2);
1161 g_free (right_part);
1162 valid = modest_text_utils_validate_email_address (address, invalid_char_position);
1166 g_free (right_part);
1173 modest_text_utils_get_display_size (guint64 size)
1175 const guint KB=1024;
1176 const guint MB=1024 * KB;
1177 const guint GB=1024 * MB;
1180 return g_strdup_printf(_FM("sfil_li_size_kb"), 0);
1181 if (0 < size && size < KB)
1182 return g_strdup_printf (_FM("sfil_li_size_kb"), 1);
1183 else if (KB <= size && size < 100 * KB)
1184 return g_strdup_printf (_FM("sfil_li_size_1kb_99kb"), size / KB);
1185 else if (100*KB <= size && size < MB)
1186 return g_strdup_printf (_FM("sfil_li_size_100kb_1mb"), (float) size / MB);
1187 else if (MB <= size && size < 10*MB)
1188 return g_strdup_printf (_FM("sfil_li_size_1mb_10mb"), (float) size / MB);
1189 else if (10*MB <= size && size < GB)
1190 return g_strdup_printf (_FM("sfil_li_size_10mb_1gb"), size / MB);
1192 return g_strdup_printf (_FM("sfil_li_size_1gb_or_greater"), (float) size / GB);
1196 get_email_from_address (const gchar * address)
1198 gchar *left_limit, *right_limit;
1200 left_limit = strstr (address, "<");
1201 right_limit = g_strrstr (address, ">");
1203 if ((left_limit == NULL)||(right_limit == NULL)|| (left_limit > right_limit))
1204 return g_strdup (address);
1206 return g_strndup (left_limit + 1, (right_limit - left_limit) - 1);
1210 modest_text_utils_get_color_string (GdkColor *color)
1213 return g_strdup_printf ("#%x%x%x%x%x%x%x%x%x%x%x%x",
1214 (color->red >> 12) & 0xf, (color->red >> 8) & 0xf,
1215 (color->red >> 4) & 0xf, (color->red) & 0xf,
1216 (color->green >> 12) & 0xf, (color->green >> 8) & 0xf,
1217 (color->green >> 4) & 0xf, (color->green) & 0xf,
1218 (color->blue >> 12) & 0xf, (color->blue >> 8) & 0xf,
1219 (color->blue >> 4) & 0xf, (color->blue) & 0xf);