* modest-text-utils.[ch], modest-header-view.c:
[modest] / src / modest-text-utils.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30
31 #include <glib.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <glib/gi18n.h>
35 #include <regex.h>
36 #include "modest-text-utils.h"
37
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif /*HAVE_CONFIG_H */
42
43 /* defines */
44 #define FORWARD_STRING _("-----Forwarded Message-----")
45 #define FROM_STRING _("From:")
46 #define SENT_STRING _("Sent:")
47 #define TO_STRING _("To:")
48 #define SUBJECT_STRING _("Subject:")
49
50 /*
51  * we need these regexps to find URLs in plain text e-mails
52  */
53 typedef struct _url_match_pattern_t url_match_pattern_t;
54 struct _url_match_pattern_t {
55         gchar   *regex;
56         regex_t *preg;
57         gchar   *prefix;
58 };
59
60 typedef struct _url_match_t url_match_t;
61 struct _url_match_t {
62         guint offset;
63         guint len;
64         const gchar* prefix;
65 };
66
67 #define MAIL_VIEWER_URL_MATCH_PATTERNS  {                               \
68         { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
69           NULL, NULL },\
70         { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
71           NULL, "http://" },\
72         { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
73           NULL, "ftp://" },\
74         { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
75            NULL, NULL},                                             \
76         { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+",                    \
77           NULL, NULL},\
78         { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
79           NULL, "mailto:"}\
80         }
81
82 /* private */
83 static gchar*   cite                    (const time_t sent_date, const gchar *from);
84 static void     hyperlinkify_plain_text (GString *txt);
85 static gint     cmp_offsets_reverse     (const url_match_t *match1, const url_match_t *match2);
86 static void     chk_partial_match       (const url_match_t *match, guint* offset);
87 static GSList*  get_url_matches         (GString *txt);
88
89 static GString* get_next_line           (const char *b, const gsize blen, const gchar * iter);
90 static int      get_indent_level        (const char *l);
91 static void     unquote_line            (GString * l);
92 static void     append_quoted           (GString * buf, const int indent, const GString * str, 
93                                          const int cutpoint);
94 static int      get_breakpoint_utf8     (const gchar * s, const gint indent, const gint limit);
95 static int      get_breakpoint_ascii    (const gchar * s, const gint indent, const gint limit);
96 static int      get_breakpoint          (const gchar * s, const gint indent, const gint limit);
97
98 static gchar*   modest_text_utils_quote_plain_text (const gchar *text, 
99                                                     const gchar *cite, 
100                                                     int limit);
101
102 static gchar*   modest_text_utils_quote_html       (const gchar *text, 
103                                                     const gchar *cite, 
104                                                     int limit);
105
106
107 /* ******************************************************************* */
108 /* ************************* PUBLIC FUNCTIONS ************************ */
109 /* ******************************************************************* */
110
111 gchar *
112 modest_text_utils_quote (const gchar *text, 
113                          const gchar *content_type,
114                          const gchar *from,
115                          const time_t sent_date, 
116                          int limit)
117 {
118         gchar *retval, *cited;
119
120         cited = cite (sent_date, from);
121
122         if (!strcmp (content_type, "text/html"))
123                 /* TODO: extract the <body> of the HTML and pass it to
124                    the function */
125                 retval = modest_text_utils_quote_html (text, cited, limit);
126         else
127                 retval = modest_text_utils_quote_plain_text (text, cited, limit);
128                 
129         g_free (cited);
130
131         return retval;
132 }
133
134
135 gchar *
136 modest_text_utils_cite (const gchar *text,
137                         const gchar *content_type,
138                         const gchar *from,
139                         time_t sent_date)
140 {
141         gchar *tmp, *retval;
142
143         tmp = cite (sent_date, from);
144         retval = g_strdup_printf ("%s%s\n", tmp, text);
145         g_free (tmp);
146
147         return retval;
148 }
149
150 gchar * 
151 modest_text_utils_inline (const gchar *text,
152                           const gchar *content_type,
153                           const gchar *from,
154                           time_t sent_date,
155                           const gchar *to,
156                           const gchar *subject)
157 {
158         gchar sent_str[101];
159         const gchar *plain_format = "%s\n%s %s\n%s %s\n%s %s\n%s %s\n\n%s";
160         const gchar *html_format = \
161                 "%s<br>\n<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"2\">\n" \
162                 "<tr><td>%s</td><td>%s</td></tr>\n" \
163                 "<tr><td>%s</td><td>%s</td></tr>\n" \
164                 "<tr><td>%s</td><td>%s</td></tr>\n" \
165                 "<tr><td>%s</td><td>%s</td></tr>\n" \
166                 "<br><br>%s";
167         const gchar *format;
168
169         modest_text_utils_strftime (sent_str, 100, "%c", localtime (&sent_date));
170
171         if (!strcmp (content_type, "text/html"))
172                 /* TODO: extract the <body> of the HTML and pass it to
173                    the function */
174                 format = html_format;
175         else
176                 format = plain_format;
177
178         return g_strdup_printf (format, 
179                                 FORWARD_STRING,
180                                 FROM_STRING, from,
181                                 SENT_STRING, sent_str,
182                                 TO_STRING, to,
183                                 SUBJECT_STRING, subject,
184                                 text);
185 }
186
187 /* just to prevent warnings:
188  * warning: `%x' yields only last 2 digits of year in some locales
189  */
190 size_t
191 modest_text_utils_strftime(char *s, size_t max, const char  *fmt, const  struct tm *tm)
192 {
193         return strftime(s, max, fmt, tm);
194 }
195
196 gchar *
197 modest_text_utils_derived_subject (const gchar *subject, const gchar *prefix)
198 {
199         gchar *tmp;
200
201         g_return_val_if_fail (prefix, NULL);
202         
203         if (!subject)
204                 return g_strdup (prefix);
205
206         tmp = g_strchug (g_strdup (subject));
207
208         if (!strncmp (tmp, prefix, strlen (prefix))) {
209                 return tmp;
210         } else {
211                 g_free (tmp);
212                 return g_strdup_printf ("%s %s", prefix, subject);
213         }
214 }
215
216 gchar*
217 modest_text_utils_remove_address (const gchar *address_list, const gchar *address)
218 {
219         gchar *dup, *token, *ptr, *result;
220         GString *filtered_emails;
221
222         g_return_val_if_fail (address_list, NULL);
223
224         if (!address)
225                 return g_strdup (address_list);
226         
227         /* search for substring */
228         if (!strstr ((const char *) address_list, (const char *) address))
229                 return g_strdup (address_list);
230
231         dup = g_strdup (address_list);
232         filtered_emails = g_string_new (NULL);
233         
234         token = strtok_r (dup, ",", &ptr);
235
236         while (token != NULL) {
237                 /* Add to list if not found */
238                 if (!strstr ((const char *) token, (const char *) address)) {
239                         if (filtered_emails->len == 0)
240                                 g_string_append_printf (filtered_emails, "%s", g_strstrip (token));
241                         else
242                                 g_string_append_printf (filtered_emails, ",%s", g_strstrip (token));
243                 }
244                 token = strtok_r (NULL, ",", &ptr);
245         }
246         result = filtered_emails->str;
247
248         /* Clean */
249         g_free (dup);
250         g_string_free (filtered_emails, FALSE);
251
252         return result;
253 }
254
255 gchar*
256 modest_text_utils_convert_to_html (const gchar *data)
257 {
258         guint            i;
259         gboolean         first_space = TRUE;
260         GString         *html;      
261         gsize           len;
262
263         if (!data)
264                 return NULL;
265
266         len = strlen (data);
267         html = g_string_sized_new (len + 100);  /* just a  guess... */
268         
269         g_string_append_printf (html,
270                                 "<html>"
271                                 "<head>"
272                                 "<meta http-equiv=\"content-type\""
273                                 " content=\"text/html; charset=utf8\">"
274                                 "</head>"
275                                 "<body><tt>");
276         
277         /* replace with special html chars where needed*/
278         for (i = 0; i != len; ++i)  {
279                 char    kar = data[i]; 
280                 switch (kar) {
281                         
282                 case 0:  break; /* ignore embedded \0s */       
283                 case '<' : g_string_append   (html, "&lt;"); break;
284                 case '>' : g_string_append   (html, "&gt;"); break;
285                 case '&' : g_string_append   (html, "&quot;"); break;
286                 case '\n': g_string_append   (html, "<br>\n"); break;
287                 default:
288                         if (kar == ' ') {
289                                 g_string_append (html, first_space ? " " : "&nbsp;");
290                                 first_space = FALSE;
291                         } else  if (kar == '\t')
292                                 g_string_append (html, "&nbsp; &nbsp;&nbsp;");
293                         else {
294                                 int charnum = 0;
295                                 first_space = TRUE;
296                                 /* optimization trick: accumulate 'normal' chars, then copy */
297                                 do {
298                                         kar = data [++charnum + i];
299                                         
300                                 } while ((i + charnum < len) &&
301                                          (kar > '>' || (kar != '<' && kar != '>'
302                                                         && kar != '&' && kar !=  ' '
303                                                         && kar != '\n' && kar != '\t')));
304                                 g_string_append_len (html, &data[i], charnum);
305                                 i += (charnum  - 1);
306                         }
307                 }
308         }
309         
310         g_string_append (html, "</tt></body></html>");
311         hyperlinkify_plain_text (html);
312
313         return g_string_free (html, FALSE);
314 }
315
316 /* ******************************************************************* */
317 /* ************************* UTILIY FUNCTIONS ************************ */
318 /* ******************************************************************* */
319
320 static GString *
321 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
322 {
323         GString *gs;
324         const gchar *i0;
325         
326         if (iter > b + blen)
327                 return g_string_new("");
328         
329         i0 = iter;
330         while (iter[0]) {
331                 if (iter[0] == '\n')
332                         break;
333                 iter++;
334         }
335         gs = g_string_new_len (i0, iter - i0);
336         return gs;
337 }
338 static int
339 get_indent_level (const char *l)
340 {
341         int indent = 0;
342
343         while (l[0]) {
344                 if (l[0] == '>') {
345                         indent++;
346                         if (l[1] == ' ') {
347                                 l++;
348                         }
349                 } else {
350                         break;
351                 }
352                 l++;
353
354         }
355
356         /*      if we hit the signature marker "-- ", we return -(indent + 1). This
357          *      stops reformatting.
358          */
359         if (strcmp (l, "-- ") == 0) {
360                 return -1 - indent;
361         } else {
362                 return indent;
363         }
364 }
365
366 static void
367 unquote_line (GString * l)
368 {
369         gchar *p;
370
371         p = l->str;
372         while (p[0]) {
373                 if (p[0] == '>') {
374                         if (p[1] == ' ') {
375                                 p++;
376                         }
377                 } else {
378                         break;
379                 }
380                 p++;
381         }
382         g_string_erase (l, 0, p - l->str);
383 }
384
385 static void
386 append_quoted (GString * buf, int indent, const GString * str,
387                const int cutpoint)
388 {
389         int i;
390
391         indent = indent < 0 ? abs (indent) - 1 : indent;
392         for (i = 0; i <= indent; i++) {
393                 g_string_append (buf, "> ");
394         }
395         if (cutpoint > 0) {
396                 g_string_append_len (buf, str->str, cutpoint);
397         } else {
398                 g_string_append (buf, str->str);
399         }
400         g_string_append (buf, "\n");
401 }
402
403 static int
404 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
405 {
406         gint index = 0;
407         const gchar *pos, *last;
408         gunichar *uni;
409
410         indent = indent < 0 ? abs (indent) - 1 : indent;
411
412         last = NULL;
413         pos = s;
414         uni = g_utf8_to_ucs4_fast (s, -1, NULL);
415         while (pos[0]) {
416                 if ((index + 2 * indent > limit) && last) {
417                         g_free (uni);
418                         return last - s;
419                 }
420                 if (g_unichar_isspace (uni[index])) {
421                         last = pos;
422                 }
423                 pos = g_utf8_next_char (pos);
424                 index++;
425         }
426         g_free (uni);
427         return strlen (s);
428 }
429
430 static int
431 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
432 {
433         gint i, last;
434
435         last = strlen (s);
436         if (last + 2 * indent < limit)
437                 return last;
438
439         for (i = strlen (s); i > 0; i--) {
440                 if (s[i] == ' ') {
441                         if (i + 2 * indent <= limit) {
442                                 return i;
443                         } else {
444                                 last = i;
445                         }
446                 }
447         }
448         return last;
449 }
450
451 static int
452 get_breakpoint (const gchar * s, const gint indent, const gint limit)
453 {
454
455         if (g_utf8_validate (s, -1, NULL)) {
456                 return get_breakpoint_utf8 (s, indent, limit);
457         } else {                /* assume ASCII */
458                 //g_warning("invalid UTF-8 in msg");
459                 return get_breakpoint_ascii (s, indent, limit);
460         }
461 }
462
463 static gchar *
464 cite (const time_t sent_date, const gchar *from)
465 {
466         gchar sent_str[101];
467
468         /* format sent_date */
469         modest_text_utils_strftime (sent_str, 100, "%c", localtime (&sent_date));
470         return g_strdup_printf (N_("On %s, %s wrote:\n"), sent_str, from);
471 }
472
473
474 static gchar *
475 modest_text_utils_quote_plain_text (const gchar *text, 
476                                     const gchar *cite, 
477                                     int limit)
478 {
479         const gchar *iter;
480         gint indent, breakpoint, rem_indent = 0;
481         GString *q, *l, *remaining;
482         gsize len;
483         gchar *tmp;
484
485         /* remaining will store the rest of the line if we have to break it */
486         q = g_string_new (cite);
487         remaining = g_string_new ("");
488
489         iter = text;
490         len = strlen(text);
491         do {
492                 l = get_next_line (text, len, iter);
493                 iter = iter + l->len + 1;
494                 indent = get_indent_level (l->str);
495                 unquote_line (l);
496
497                 if (remaining->len) {
498                         if (l->len && indent == rem_indent) {
499                                 g_string_prepend (l, " ");
500                                 g_string_prepend (l, remaining->str);
501                         } else {
502                                 do {
503                                         breakpoint =
504                                                 get_breakpoint (remaining->     str,
505                                                                 rem_indent,
506                                                                 limit);
507                                         append_quoted (q, rem_indent,
508                                                        remaining, breakpoint);
509                                         g_string_erase (remaining, 0,
510                                                         breakpoint);
511                                         if (remaining->str[0] == ' ') {
512                                                 g_string_erase (remaining, 0,
513                                                                 1);
514                                         }
515                                 } while (remaining->len);
516                         }
517                 }
518                 g_string_free (remaining, TRUE);
519                 breakpoint = get_breakpoint (l->str, indent, limit);
520                 remaining = g_string_new (l->str + breakpoint);
521                 if (remaining->str[0] == ' ') {
522                         g_string_erase (remaining, 0, 1);
523                 }
524                 rem_indent = indent;
525                 append_quoted (q, indent, l, breakpoint);
526                 g_string_free (l, TRUE);
527         } while ((iter < text + len) || (remaining->str[0]));
528
529         return g_string_free (q, FALSE);
530 }
531
532 static gchar*
533 modest_text_utils_quote_html (const gchar *text, 
534                               const gchar *cite, 
535                               int limit)
536 {
537         const gchar *format = \
538                 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" \
539                 "<html>\n" \
540                 "<body>\n" \
541                 "%s" \
542                 "<blockquote type=\"cite\">\n%s\n</blockquote>\n" \
543                 "</body>\n" \
544                 "</html>\n";
545
546         return g_strdup_printf (format, cite, text);
547 }
548
549 static gint 
550 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
551 {
552         return match2->offset - match1->offset;
553 }
554
555
556
557 /*
558  * check if the match is inside an existing match... */
559 static void
560 chk_partial_match (const url_match_t *match, guint* offset)
561 {
562         if (*offset >= match->offset && *offset < match->offset + match->len)
563                 *offset = -1;
564 }
565
566 static GSList*
567 get_url_matches (GString *txt)
568 {
569         regmatch_t rm;
570         guint rv, i, offset = 0;
571         GSList *match_list = NULL;
572
573         static url_match_pattern_t patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
574         const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
575
576         /* initalize the regexps */
577         for (i = 0; i != pattern_num; ++i) {
578                 patterns[i].preg = g_new0 (regex_t,1);
579                 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
580                                   REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
581         }
582         /* find all the matches */
583         for (i = 0; i != pattern_num; ++i) {
584                 offset     = 0; 
585                 while (1) {
586                         int test_offset;
587                         if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
588                                 g_assert (rv == REG_NOMATCH); /* this should not happen */
589                                 break; /* try next regexp */ 
590                         }
591                         if (rm.rm_so == -1)
592                                 break;
593
594                         /* FIXME: optimize this */
595                         /* to avoid partial matches on something that was already found... */
596                         /* check_partial_match will put -1 in the data ptr if that is the case */
597                         test_offset = offset + rm.rm_so;
598                         g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
599                         
600                         /* make a list of our matches (<offset, len, prefix> tupels)*/
601                         if (test_offset != -1) {
602                                 url_match_t *match = g_new (url_match_t,1);
603                                 match->offset = offset + rm.rm_so;
604                                 match->len    = rm.rm_eo - rm.rm_so;
605                                 match->prefix = patterns[i].prefix;
606                                 match_list = g_slist_prepend (match_list, match);
607                         }
608                         offset += rm.rm_eo;
609                 }
610         }
611
612         for (i = 0; i != pattern_num; ++i) {
613                 regfree (patterns[i].preg);
614                 g_free  (patterns[i].preg);
615         } /* don't free patterns itself -- it's static */
616         
617         /* now sort the list, so the matches are in reverse order of occurence.
618          * that way, we can do the replacements starting from the end, so we don't need
619          * to recalculate the offsets
620          */
621         match_list = g_slist_sort (match_list,
622                                    (GCompareFunc)cmp_offsets_reverse); 
623         return match_list;      
624 }
625
626
627
628 static void
629 hyperlinkify_plain_text (GString *txt)
630 {
631         GSList *cursor;
632         GSList *match_list = get_url_matches (txt);
633
634         /* we will work backwards, so the offsets stay valid */
635         for (cursor = match_list; cursor; cursor = cursor->next) {
636
637                 url_match_t *match = (url_match_t*) cursor->data;
638                 gchar *url  = g_strndup (txt->str + match->offset, match->len);
639                 gchar *repl = NULL; /* replacement  */
640
641                 /* the prefix is NULL: use the one that is already there */
642                 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
643                                         match->prefix ? match->prefix : "", url, url);
644
645                 /* replace the old thing with our hyperlink
646                  * replacement thing */
647                 g_string_erase  (txt, match->offset, match->len);
648                 g_string_insert (txt, match->offset, repl);
649                 
650                 g_free (url);
651                 g_free (repl);
652
653                 g_free (cursor->data);  
654         }
655         
656         g_slist_free (match_list);
657 }
658
659
660
661 gchar*
662 modest_text_utils_get_display_address (gchar *address)
663 {
664         gchar *cursor;
665         
666         if (!address)
667                 return NULL;
668
669         g_return_val_if_fail (g_utf8_validate (address, -1, NULL), NULL);
670
671         g_strchug (address); /* remove leading whitespace */
672
673         /*  <email@address> from display name */
674         cursor = g_strstr_len (address, strlen(address), "<");
675         if (cursor == address) /* there's nothing else? leave it */
676                 return address;
677         if (cursor) 
678                 cursor[0]='\0';
679
680         /* remove (bla bla) from display name */
681         cursor = g_strstr_len (address, strlen(address), "(");
682         if (cursor == address) /* there's nothing else? leave it */
683                 return address;
684         if (cursor) 
685                 cursor[0]='\0';
686
687         g_strchomp (address); /* remove trailing whitespace */
688
689         return address;
690 }
691
692
693
694 gint 
695 modest_text_utils_get_subject_prefix_len (const gchar *sub)
696 {
697         gint i;
698         static const gchar* prefix[] = {
699                 "Re:", "RE:", "Fwd:", "FWD:", "FW:", NULL
700         };
701                 
702         if (!sub || (sub[0] != 'R' && sub[0] != 'F')) /* optimization */
703                 return 0;
704
705         i = 0;
706         
707         while (prefix[i]) {
708                 if (g_str_has_prefix(sub, prefix[i])) {
709                         int prefix_len = strlen(prefix[i]); 
710                         if (sub[prefix_len] == ' ')
711                                 ++prefix_len; /* ignore space after prefix as well */
712                         return prefix_len; 
713                 }
714                 ++i;
715         }
716         return 0;
717 }
718
719
720 gint
721 modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive)
722 {
723         gint result = 0;
724         gchar *n1, *n2;
725
726         /* work even when s1 and/or s2 == NULL */
727         if (G_UNLIKELY(s1 == s2))
728                 return 0;
729         if (G_UNLIKELY(!s1))
730                 return -1;
731         if (G_UNLIKELY(!s2))
732                 return 1;       
733
734         /* if it's not case sensitive */
735         if (!insensitive)
736                 return strcmp (s1, s2);
737         
738         n1 = g_utf8_collate_key (s1, -1);
739         n2 = g_utf8_collate_key (s2, -1);
740         
741         result = strcmp (n1, n2);
742
743         g_free (n1);
744         g_free (n2);
745         
746         return result;
747 }
748
749
750 const gchar*
751 modest_text_utils_get_display_date (time_t date)
752 {
753         struct tm date_tm, now_tm; 
754         time_t now;
755
756         const gint buf_size = 64; 
757         static gchar date_buf[64]; /* buf_size is not ... */
758         static gchar now_buf[64];  /* ...const enough... */
759         
760         now = time (NULL);
761         
762         localtime_r(&now, &now_tm);
763         localtime_r(&date, &date_tm);
764
765         /* get today's date */
766         modest_text_utils_strftime (date_buf, buf_size, "%x", &date_tm);
767         modest_text_utils_strftime (now_buf,  buf_size, "%x",  &now_tm);
768         /* today */
769
770         /* if this is today, get the time instead of the date */
771         if (strcmp (date_buf, now_buf) == 0)
772                 strftime (date_buf, buf_size, _("%X"), &date_tm); 
773                 
774         return date_buf;
775 }