+static gchar* cite (const time_t sent_date, const gchar *from);
+static void hyperlinkify_plain_text (GString *txt);
+static gint cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2);
+static void chk_partial_match (const url_match_t *match, guint* offset);
+static GSList* get_url_matches (GString *txt);
+
+static GString* get_next_line (const char *b, const gsize blen, const gchar * iter);
+static int get_indent_level (const char *l);
+static void unquote_line (GString * l);
+static void append_quoted (GString * buf, const int indent, const GString * str,
+ const int cutpoint);
+static int get_breakpoint_utf8 (const gchar * s, const gint indent, const gint limit);
+static int get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit);
+static int get_breakpoint (const gchar * s, const gint indent, const gint limit);
+
+static gchar* modest_text_utils_quote_plain_text (const gchar *text,
+ const gchar *cite,
+ int limit);
+
+static gchar* modest_text_utils_quote_html (const gchar *text,
+ const gchar *cite,
+ int limit);
+
+
+/* ******************************************************************* */
+/* ************************* PUBLIC FUNCTIONS ************************ */
+/* ******************************************************************* */
+
+gchar *
+modest_text_utils_quote (const gchar *text,
+ const gchar *content_type,
+ const gchar *from,
+ const time_t sent_date,
+ int limit)
+{
+ gchar *retval, *cited;
+
+ g_return_val_if_fail (text, NULL);
+ g_return_val_if_fail (content_type, NULL);
+
+ cited = cite (sent_date, from);
+
+ if (content_type && strcmp (content_type, "text/html") == 0)
+ /* TODO: extract the <body> of the HTML and pass it to
+ the function */
+ retval = modest_text_utils_quote_html (text, cited, limit);
+ else
+ retval = modest_text_utils_quote_plain_text (text, cited, limit);
+
+ g_free (cited);
+
+ return retval;
+}
+
+
+gchar *
+modest_text_utils_cite (const gchar *text,
+ const gchar *content_type,
+ const gchar *from,
+ time_t sent_date)
+{
+ gchar *tmp, *retval;
+
+ g_return_val_if_fail (text, NULL);
+ g_return_val_if_fail (content_type, NULL);
+
+ tmp = cite (sent_date, from);
+ retval = g_strdup_printf ("%s%s\n", tmp, text);
+ g_free (tmp);
+
+ return retval;
+}
+
+gchar *
+modest_text_utils_inline (const gchar *text,
+ const gchar *content_type,
+ const gchar *from,
+ time_t sent_date,
+ const gchar *to,
+ const gchar *subject)
+{
+ gchar sent_str[101];
+ const gchar *plain_format = "%s\n%s %s\n%s %s\n%s %s\n%s %s\n\n%s";
+ const gchar *html_format = \
+ "%s<br>\n<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"2\">\n" \
+ "<tr><td>%s</td><td>%s</td></tr>\n" \
+ "<tr><td>%s</td><td>%s</td></tr>\n" \
+ "<tr><td>%s</td><td>%s</td></tr>\n" \
+ "<tr><td>%s</td><td>%s</td></tr>\n" \
+ "<br><br>%s";
+ const gchar *format;
+
+ g_return_val_if_fail (text, NULL);
+ g_return_val_if_fail (content_type, NULL);
+ g_return_val_if_fail (text, NULL);
+
+ modest_text_utils_strftime (sent_str, 100, "%c", sent_date);
+
+ if (!strcmp (content_type, "text/html"))
+ /* TODO: extract the <body> of the HTML and pass it to
+ the function */
+ format = html_format;
+ else
+ format = plain_format;
+
+ return g_strdup_printf (format,
+ FORWARD_STRING,
+ FROM_STRING, (from) ? from : EMPTY_STRING,
+ SENT_STRING, sent_str,
+ TO_STRING, (to) ? to : EMPTY_STRING,
+ SUBJECT_STRING, (subject) ? subject : EMPTY_STRING,
+ text);
+}
+
+/* just to prevent warnings:
+ * warning: `%x' yields only last 2 digits of year in some locales
+ */
+gsize
+modest_text_utils_strftime(char *s, gsize max, const char *fmt, time_t timet)
+{
+ static GDate date;
+
+#if MODEST_PLATFORM_ID==1 /* gtk */
+ g_date_set_time_t (&date, timet);
+#elif MODEST_PLATFORM_ID==2 /* hildon (maemo) */
+ g_date_set_time (&date, (GTime) timet);
+#endif
+ return g_date_strftime (s, max, fmt, (const GDate*) &date);
+}
+
+gchar *
+modest_text_utils_derived_subject (const gchar *subject, const gchar *prefix)
+{
+ gchar *tmp;
+
+ g_return_val_if_fail (prefix, NULL);
+
+ if (!subject)
+ return g_strdup (prefix);
+
+ tmp = g_strchug (g_strdup (subject));
+
+ if (!strncmp (tmp, prefix, strlen (prefix))) {
+ return tmp;
+ } else {
+ g_free (tmp);
+ return g_strdup_printf ("%s %s", prefix, subject);
+ }
+}
+
+gchar*
+modest_text_utils_remove_address (const gchar *address_list, const gchar *address)
+{
+ gchar *dup, *token, *ptr, *result;
+ GString *filtered_emails;
+
+ g_return_val_if_fail (address_list, NULL);
+
+ if (!address)
+ return g_strdup (address_list);
+
+ /* search for substring */
+ if (!strstr ((const char *) address_list, (const char *) address))
+ return g_strdup (address_list);
+
+ dup = g_strdup (address_list);
+ filtered_emails = g_string_new (NULL);
+
+ token = strtok_r (dup, ",", &ptr);
+
+ while (token != NULL) {
+ /* Add to list if not found */
+ if (!strstr ((const char *) token, (const char *) address)) {
+ if (filtered_emails->len == 0)
+ g_string_append_printf (filtered_emails, "%s", g_strstrip (token));
+ else
+ g_string_append_printf (filtered_emails, ",%s", g_strstrip (token));
+ }
+ token = strtok_r (NULL, ",", &ptr);
+ }
+ result = filtered_emails->str;
+
+ /* Clean */
+ g_free (dup);
+ g_string_free (filtered_emails, FALSE);
+
+ return result;
+}
+
+gchar*
+modest_text_utils_convert_to_html (const gchar *data)
+{
+ guint i;
+ gboolean first_space = TRUE;
+ GString *html;
+ gsize len;
+
+ if (!data)
+ return NULL;
+
+ len = strlen (data);
+ html = g_string_sized_new (len + 100); /* just a guess... */
+
+ g_string_append_printf (html,
+ "<html>"
+ "<head>"
+ "<meta http-equiv=\"content-type\""
+ " content=\"text/html; charset=utf8\">"
+ "</head>"
+ "<body><tt>");
+
+ /* replace with special html chars where needed*/
+ for (i = 0; i != len; ++i) {
+ char kar = data[i];
+ switch (kar) {
+
+ case 0: break; /* ignore embedded \0s */
+ case '<' : g_string_append (html, "<"); break;
+ case '>' : g_string_append (html, ">"); break;
+ case '&' : g_string_append (html, """); break;
+ case '\n': g_string_append (html, "<br>\n"); break;
+ default:
+ if (kar == ' ') {
+ g_string_append (html, first_space ? " " : " ");
+ first_space = FALSE;
+ } else if (kar == '\t')
+ g_string_append (html, " ");
+ else {
+ int charnum = 0;
+ first_space = TRUE;
+ /* optimization trick: accumulate 'normal' chars, then copy */
+ do {
+ kar = data [++charnum + i];
+
+ } while ((i + charnum < len) &&
+ (kar > '>' || (kar != '<' && kar != '>'
+ && kar != '&' && kar != ' '
+ && kar != '\n' && kar != '\t')));
+ g_string_append_len (html, &data[i], charnum);
+ i += (charnum - 1);
+ }
+ }
+ }
+
+ g_string_append (html, "</tt></body></html>");
+ hyperlinkify_plain_text (html);
+
+ return g_string_free (html, FALSE);
+}
+
+GSList *
+modest_text_utils_split_addresses_list (const gchar *addresses)
+{
+ gchar *current, *start, *last_blank;
+ GSList *result = NULL;
+
+ start = (gchar *) addresses;
+ current = start;
+ last_blank = start;
+
+ while (*current != '\0') {
+ if ((start == current)&&((*current == ' ')||(*current == ','))) {
+ start++;
+ last_blank = current;
+ } else if (*current == ',') {
+ gchar *new_address = NULL;
+ new_address = g_strndup (start, current - last_blank);
+ result = g_slist_prepend (result, new_address);
+ start = current + 1;
+ last_blank = start;
+ } else if (*current == '\"') {
+ if (current == start) {
+ current++;
+ start++;
+ }
+ while ((*current != '\"')&&(*current != '\0'))
+ current++;
+ }
+
+ current++;
+ }
+
+ if (start != current) {
+ gchar *new_address = NULL;
+ new_address = g_strndup (start, current - last_blank);
+ result = g_slist_prepend (result, new_address);
+ }
+
+ result = g_slist_reverse (result);
+ return result;
+
+}
+
+
+/* ******************************************************************* */
+/* ************************* UTILIY FUNCTIONS ************************ */
+/* ******************************************************************* */