3 /* insert (c)/licensing information) */
9 #include "modest-text-utils.h"
14 #endif /*HAVE_CONFIG_H */
17 static GString *get_next_line (const char *b, const gsize blen, const gchar * iter);
18 static int get_indent_level (const char *l);
19 static void unquote_line (GString * l);
20 static void append_quoted (GString * buf, const int indent,
21 const GString * str, const int cutpoint);
22 static int get_breakpoint_utf8 (const gchar * s, const gint indent,
24 static int get_breakpoint_ascii (const gchar * s, const gint indent,
26 static int get_breakpoint (const gchar * s, const gint indent,
30 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
36 return g_string_new("");
44 gs = g_string_new_len (i0, iter - i0);
48 get_indent_level (const char *l)
65 /* if we hit the signature marker "-- ", we return -(indent + 1). This
68 if (strcmp (l, "-- ") == 0) {
76 unquote_line (GString * l)
91 g_string_erase (l, 0, p - l->str);
95 append_quoted (GString * buf, int indent, const GString * str,
100 indent = indent < 0 ? abs (indent) - 1 : indent;
101 for (i = 0; i <= indent; i++) {
102 g_string_append (buf, "> ");
105 g_string_append_len (buf, str->str, cutpoint);
107 g_string_append (buf, str->str);
109 g_string_append (buf, "\n");
113 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
116 const gchar *pos, *last;
119 indent = indent < 0 ? abs (indent) - 1 : indent;
123 uni = g_utf8_to_ucs4_fast (s, -1, NULL);
125 if ((index + 2 * indent > limit) && last) {
129 if (g_unichar_isspace (uni[index])) {
132 pos = g_utf8_next_char (pos);
140 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
145 if (last + 2 * indent < limit)
148 for (i = strlen (s); i > 0; i--) {
150 if (i + 2 * indent <= limit) {
161 get_breakpoint (const gchar * s, const gint indent, const gint limit)
164 if (g_utf8_validate (s, -1, NULL)) {
165 return get_breakpoint_utf8 (s, indent, limit);
166 } else { /* assume ASCII */
167 //g_warning("invalid UTF-8 in msg");
168 return get_breakpoint_ascii (s, indent, limit);
174 modest_text_utils_quote (const gchar * to_quote, const gchar * from,
175 const time_t sent_date, const int limit)
178 gint indent, breakpoint, rem_indent = 0;
180 GString *q, *l, *remaining;
183 /* format sent_date */
184 strftime (sent_str, 100, "%c", localtime (&sent_date));
185 q = g_string_new ("");
186 g_string_printf (q, "On %s, %s wrote:\n", sent_str, from);
188 /* remaining will store the rest of the line if we have to break it */
189 remaining = g_string_new ("");
192 len = strlen(to_quote);
194 l = get_next_line (to_quote, len, iter);
195 iter = iter + l->len + 1;
196 indent = get_indent_level (l->str);
199 if (remaining->len) {
200 if (l->len && indent == rem_indent) {
201 g_string_prepend (l, " ");
202 g_string_prepend (l, remaining->str);
206 get_breakpoint (remaining-> str,
209 append_quoted (q, rem_indent,
210 remaining, breakpoint);
211 g_string_erase (remaining, 0,
213 if (remaining->str[0] == ' ') {
214 g_string_erase (remaining, 0,
217 } while (remaining->len);
220 g_string_free (remaining, TRUE);
221 breakpoint = get_breakpoint (l->str, indent, limit);
222 remaining = g_string_new (l->str + breakpoint);
223 if (remaining->str[0] == ' ') {
224 g_string_erase (remaining, 0, 1);
227 append_quoted (q, indent, l, breakpoint);
228 g_string_free (l, TRUE);
229 } while ((iter < to_quote + len) || (remaining->str[0]));
231 return g_string_free (q, FALSE);