X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fmodest-text-utils.c;h=4ea7ec71610f0bbea6fd119ecf557fc1480e568e;hb=fd2fd1985a2218f833f9d480cc2398ced1ff0e0a;hp=002897daf6f76ab8eb9a4ad1f2669e7c2a9f145c;hpb=cc5c378787a44ac2581fb1e3ef4f0b64d4ad437e;p=modest diff --git a/src/modest-text-utils.c b/src/modest-text-utils.c index 002897d..4ea7ec7 100644 --- a/src/modest-text-utils.c +++ b/src/modest-text-utils.c @@ -4,56 +4,51 @@ #include #include +#include + +#include "modest-text-utils.h" #ifdef HAVE_CONFIG_H #include -#endif /*HAVE_CONFIG_H*/ - +#endif /*HAVE_CONFIG_H */ /* private */ -static GString * -get_next_line(GtkTextBuffer *b, GtkTextIter *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 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 GString * -get_next_line(GtkTextBuffer *b, GtkTextIter *iter) +get_next_line (const gchar * b, const gsize blen, const gchar * iter) { - GtkTextIter iter2; - gchar *tmp; + GString *gs; + const gchar *i0; - gtk_text_buffer_get_iter_at_line_offset(b, - &iter2, - gtk_text_iter_get_line(iter), - gtk_text_iter_get_chars_in_line(iter) -1 - ); - tmp = gtk_text_buffer_get_text(b, &iter2, iter, FALSE); - gtk_text_iter_forward_line(iter); - return g_string_new(tmp); + if (iter > b + blen) + return g_string_new(""); + + i0 = iter; + while (iter[0]) { + if (iter[0] == '\n') + break; + iter++; + } + gs = g_string_new_len (i0, iter - i0); + return gs; } - static int -get_indent_level(const char *l) +get_indent_level (const char *l) { int indent = 0; - + while (l[0]) { if (l[0] == '>') { indent++; @@ -64,20 +59,24 @@ get_indent_level(const char *l) break; } l++; - + } - if (strcmp("-- ", l) == 0) { - return -1-indent; + + /* if we hit the signature marker "-- ", we return -(indent + 1). This + * stops reformatting. + */ + if (strcmp (l, "-- ") == 0) { + return -1 - indent; } else { return indent; } } static void -unquote_line(GString *l) { - GString *r; +unquote_line (GString * l) +{ gchar *p; - + p = l->str; while (p[0]) { if (p[0] == '>') { @@ -93,56 +92,60 @@ unquote_line(GString *l) { } static void -append_quoted(GString *buf, int indent, const GString *str, const int cutpoint) { +append_quoted (GString * buf, int indent, const GString * str, + const int cutpoint) +{ int i; - - indent = indent < 0? abs(indent) -1 : indent; - for (i=0; i<=indent; i++) { - g_string_append(buf, "> "); + + indent = indent < 0 ? abs (indent) - 1 : indent; + for (i = 0; i <= indent; i++) { + g_string_append (buf, "> "); } if (cutpoint > 0) { - g_string_append_len(buf, str->str, cutpoint); + g_string_append_len (buf, str->str, cutpoint); } else { - g_string_append(buf, str->str); + g_string_append (buf, str->str); } - g_string_append(buf, "\n"); + g_string_append (buf, "\n"); } static int -get_breakpoint_utf8(const gchar *s, gint indent, const gint limit) { +get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit) +{ gint index = 0; const gchar *pos, *last; gunichar *uni; - - indent = indent < 0? abs(indent) -1 : indent; - + + indent = indent < 0 ? abs (indent) - 1 : indent; + last = NULL; pos = s; - uni = g_utf8_to_ucs4_fast(s, -1, NULL); + uni = g_utf8_to_ucs4_fast (s, -1, NULL); while (pos[0]) { if ((index + 2 * indent > limit) && last) { - g_free(uni); + g_free (uni); return last - s; } - if (g_unichar_isspace(uni[index])) { + if (g_unichar_isspace (uni[index])) { last = pos; } - pos = g_utf8_next_char(pos); + pos = g_utf8_next_char (pos); index++; } - g_free(uni); - return strlen(s); + g_free (uni); + return strlen (s); } static int -get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit) { +get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit) +{ gint i, last; - - last = strlen(s); + + last = strlen (s); if (last + 2 * indent < limit) return last; - - for ( i=strlen(s) ; i>0; i-- ) { + + for (i = strlen (s); i > 0; i--) { if (s[i] == ' ') { if (i + 2 * indent <= limit) { return i; @@ -155,63 +158,75 @@ 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) { - - 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); +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); } -} +} + gchar * -modest_text_utils_quote(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit) +modest_text_utils_quote (const gchar * to_quote, const gchar * from, + const time_t sent_date, const int limit) { - GtkTextIter iter; - gint indent, breakpoint, rem_indent; + const gchar *iter; + gint indent, breakpoint, rem_indent = 0; gchar sent_str[101]; - GString *q, *l, *remaining; /* quoted msg, line */ - + GString *q, *l, *remaining; + gsize len; /* format sent_date */ - strftime(sent_str, 100, "%c", localtime(&sent_date)); - q = g_string_new(""); - g_string_printf(q, "On %s, %s wrote:\n", sent_str, from); - + strftime (sent_str, 100, "%c", localtime (&sent_date)); + q = g_string_new (""); + g_string_printf (q, "On %s, %s wrote:\n", sent_str, from); + /* remaining will store the rest of the line if we have to break it */ - remaining = g_string_new(""); - gtk_text_buffer_get_iter_at_line(buf, &iter, 0); + remaining = g_string_new (""); + + iter = to_quote; + len = strlen(to_quote); do { - l = get_next_line(buf, &iter); - indent = get_indent_level(l->str); - unquote_line(l); - + l = get_next_line (to_quote, 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); + 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); + 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); - } + 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); + 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); + g_string_erase (remaining, 0, 1); } rem_indent = indent; - append_quoted(q, indent, l, breakpoint); - g_string_free(l, TRUE); - } while (!gtk_text_iter_is_end(&iter)); - - return g_string_free(q, FALSE); + append_quoted (q, indent, l, breakpoint); + g_string_free (l, TRUE); + } while ((iter < to_quote + len) || (remaining->str[0])); + + return g_string_free (q, FALSE); }