+get_breakpoint (const gchar * s, const gint indent, const gint limit)
+{
+
+ if (g_utf8_validate (s, -1, NULL)) {
+ return get_breakpoint_utf8 (s, indent, limit);
+ } else { /* assume ASCII */
+ //g_warning("invalid UTF-8 in msg");
+ return get_breakpoint_ascii (s, indent, limit);
+ }
+}
+
+static gchar *
+cite (const time_t sent_date, const gchar *from)
+{
+ return g_strdup (_("mcen_ia_editor_original_message"));
+}
+
+static gchar *
+quoted_attachments (GList *attachments)
+{
+ GList *node = NULL;
+ GString *result = g_string_new ("");
+ for (node = attachments; node != NULL; node = g_list_next (node)) {
+ gchar *filename = (gchar *) node->data;
+ g_string_append_printf ( result, "%s %s\n", _("mcen_ia_editor_attach_filename"), filename);
+ }
+
+ return g_string_free (result, FALSE);
+
+}
+
+static gchar *
+modest_text_utils_quote_plain_text (const gchar *text,
+ const gchar *cite,
+ const gchar *signature,
+ GList *attachments,
+ int limit)
+{
+ const gchar *iter;
+ gint indent, breakpoint, rem_indent = 0;
+ GString *q, *l, *remaining;
+ gsize len;
+ gchar *attachments_string = NULL;
+
+ q = g_string_new ("\n");
+ if (signature != NULL) {
+ q = g_string_append (q, signature);
+ q = g_string_append_c (q, '\n');
+ }
+ q = g_string_append (q, cite);
+ q = g_string_append_c (q, '\n');
+
+ /* remaining will store the rest of the line if we have to break it */
+ remaining = g_string_new ("");
+
+ iter = text;
+ len = strlen(text);
+ do {
+ l = get_next_line (text, len, iter);
+ iter = iter + l->len + 1;
+ indent = get_indent_level (l->str);
+ unquote_line (l);
+
+ if (remaining->len) {
+ if (l->len && indent == rem_indent) {
+ g_string_prepend (l, " ");
+ g_string_prepend (l, remaining->str);
+ } else {
+ do {
+ breakpoint =
+ get_breakpoint (remaining->str,
+ rem_indent,
+ limit);
+ append_quoted (q, rem_indent,
+ remaining, breakpoint);
+ g_string_erase (remaining, 0,
+ breakpoint);
+ if (remaining->str[0] == ' ') {
+ g_string_erase (remaining, 0,
+ 1);
+ }
+ } while (remaining->len);
+ }
+ }
+ g_string_free (remaining, TRUE);
+ breakpoint = get_breakpoint (l->str, indent, limit);
+ remaining = g_string_new (l->str + breakpoint);
+ if (remaining->str[0] == ' ') {
+ g_string_erase (remaining, 0, 1);
+ }
+ rem_indent = indent;
+ append_quoted (q, indent, l, breakpoint);
+ g_string_free (l, TRUE);
+ } while ((iter < text + len) || (remaining->str[0]));
+
+ attachments_string = quoted_attachments (attachments);
+ q = g_string_append (q, attachments_string);
+ g_free (attachments_string);
+
+ return g_string_free (q, FALSE);
+}
+
+static gchar*
+modest_text_utils_quote_html (const gchar *text,
+ const gchar *cite,
+ const gchar *signature,
+ GList *attachments,
+ int limit)
+{
+ gchar *result = NULL;
+ gchar *signature_result = NULL;
+ const gchar *format = \
+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" \
+ "<html>\n" \
+ "<body>\n" \
+ "<br/>%s<br/>" \
+ "<pre>%s<br/>%s<br/>%s</pre>\n" \
+ "</body>\n" \
+ "</html>\n";
+ gchar *attachments_string = NULL;
+ gchar *q_attachments_string = NULL;
+ gchar *q_cite = NULL;
+ gchar *html_text = NULL;
+
+ if (signature == NULL)
+ signature_result = g_strdup ("");
+ else
+ signature_result = modest_text_utils_convert_to_html_body (signature, -1, TRUE);
+
+ attachments_string = quoted_attachments (attachments);
+ q_attachments_string = modest_text_utils_convert_to_html_body (attachments_string, -1, TRUE);
+ q_cite = modest_text_utils_convert_to_html_body (cite, -1, TRUE);
+ html_text = modest_text_utils_convert_to_html_body (text, -1, TRUE);
+ result = g_strdup_printf (format, signature_result, q_cite, html_text, q_attachments_string);
+ g_free (q_cite);
+ g_free (html_text);
+ g_free (attachments_string);
+ g_free (q_attachments_string);
+ g_free (signature_result);