* moved most quote-related funtions to (new) modest-text-utils.[ch]
authorArne Zellentin <arne@kernelconcepts.de>
Wed, 24 May 2006 13:44:11 +0000 (13:44 +0000)
committerArne Zellentin <arne@kernelconcepts.de>
Wed, 24 May 2006 13:44:11 +0000 (13:44 +0000)
pmo-trunk-r107

src/Makefile.am
src/gtk-glade/modest-ui.c
src/modest-text-utils.c [new file with mode: 0644]
src/modest-text-utils.h [new file with mode: 0644]

index 1e1381d..1ac803d 100644 (file)
@@ -36,6 +36,8 @@ modest_SOURCES=\
        modest-proto.h\
        modest-proto.c\
        modest-ui.h\
+       modest-text-utils.h\
+       modest-text-utils.c\
        modest-tny-stream-gtkhtml.h\
        modest-tny-stream-gtkhtml.c\
        modest-tny-transport-actions.c\
index 34d81eb..843edea 100644 (file)
@@ -24,6 +24,7 @@
 #include "../modest-tny-msg-view.h"
 #include "../modest-tny-transport-actions.h"
 
+#include "../modest-text-utils.h"
 
 #define MODEST_GLADE          PREFIX "/share/modest/glade/modest.glade"
 #define MODEST_GLADE_MAIN_WIN "main"
@@ -665,130 +666,6 @@ on_new_mail_clicked (GtkWidget *widget, ModestUI *modest_ui)
 }
 
 
-static GString *
-get_next_line(GtkTextBuffer *b, GtkTextIter *iter)
-{
-       GtkTextIter iter2;
-       gchar *tmp;
-       
-       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);
-}
-
-static int
-get_indent_level(const char *l)
-{
-       int indent = 0;
-       while (l[0]) {
-               if (l[0] == '>') {
-                       indent++;
-                       if (l[1] == ' ') {
-                               l++;
-                       }
-               } else {
-                       break;
-               }
-               l++;
-               
-       }
-       return indent;
-}
-
-static void
-unquote_line(GString *l) {
-       GString *r;
-       gchar *p;
-       
-       p = l->str;
-       while (p[0]) {
-               if (p[0] == '>') {
-                       if (p[1] == ' ') {
-                               p++;
-                       }
-               } else {
-                       break;
-               }
-               p++;
-       }
-       g_string_erase (l, 0, p - l->str);
-}
-
-static void
-append_quoted(GString *buf, const int indent, const GString *str, const int cutpoint) {
-       int i;
-       
-       for (i=0; i<=indent; i++) {
-               g_string_append(buf, "> ");
-       }
-       if (cutpoint > 0) {
-               g_string_append_len(buf, str->str, cutpoint);
-       } else {
-               g_string_append(buf, str->str);
-       }
-       g_string_append(buf, "\n");
-}
-
-static int
-get_breakpoint_utf8(const gchar *s, const gint indent, const gint limit) {
-       gint index = 0;
-       const gchar *pos, *last;
-       gunichar *uni;
-       
-       last = NULL;
-       pos = s;
-       uni = g_utf8_to_ucs4_fast(s, -1, NULL);
-       while (pos[0]) {
-               if ((index + 2 * indent > limit) && last) {
-                       g_free(uni);
-                       return last - s;
-               }
-               if (g_unichar_isspace(uni[index])) {
-                       last = pos;
-               }
-               pos = g_utf8_next_char(pos);
-               index++;
-       }
-       g_free(uni);
-       return index;
-}
-
-static int
-get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit) {
-       gint i, last;
-       
-       last = strlen(s);
-       if (last + 2 * indent < limit)
-               return last;
-       
-       for ( i=strlen(s) ; i>0; i-- ) {
-               if (s[i] == ' ') {
-                       if (i + 2 * indent <= limit) {
-                               return i;
-                       } else {
-                               last = i;
-                       }
-               }
-       }
-       return last;
-}
-
-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);
-       }
-}      
-
 static gchar *
 modest_ui_quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date)
 {
@@ -798,13 +675,9 @@ modest_ui_quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date)
        TnyTextBufferStream *dest;
        TnyMsgMimePartIface *body = NULL;
        GtkTextBuffer *buf;
-       GtkTextIter iter;
-       gint limit = 76;
-       gint indent, breakpoint, rem_indent;
-       gchar sent_str[101];
-       GString *q, *l, *remaining; /* quoted msg, line */
+       gint limit = 76; /* TODO: make configurable */
+       gchar *quoted;
        
-
        buf = gtk_text_buffer_new(NULL);
        dest = tny_text_buffer_stream_new(buf);
        
@@ -831,48 +704,11 @@ modest_ui_quote_msg(const TnyMsgIface *src, const gchar *from, time_t sent_date)
        tny_msg_mime_part_iface_decode_to_stream (body, stream);
        tny_stream_iface_reset (stream);
 
-       /* 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);
-       
-       /* 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);
-       do {
-               l = get_next_line(buf, &iter);
-               indent = get_indent_level(l->str);
-               unquote_line(l);
+       quoted = modest_text_utils_quote (buf, from, sent_date, limit);
                
-               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 (!gtk_text_iter_is_end(&iter));
-       
        g_object_unref(stream);
        g_object_unref(buf);
-       return g_string_free(q, FALSE);
+       return quoted;
 }
 
 static void
diff --git a/src/modest-text-utils.c b/src/modest-text-utils.c
new file mode 100644 (file)
index 0000000..3039ced
--- /dev/null
@@ -0,0 +1,209 @@
+/* modest-ui.c */
+
+/* insert (c)/licensing information) */
+
+#include <gtk/gtk.h>
+#include <string.h>
+
+
+#ifdef HAVE_CONFIG_H
+#include <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(GtkTextBuffer *b, GtkTextIter *iter)
+{
+       GtkTextIter iter2;
+       gchar *tmp;
+       
+       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);
+}
+
+static int
+get_indent_level(const char *l)
+{
+       int indent = 0;
+       while (l[0]) {
+               if (l[0] == '>') {
+                       indent++;
+                       if (l[1] == ' ') {
+                               l++;
+                       }
+               } else {
+                       break;
+               }
+               l++;
+               
+       }
+       return indent;
+}
+
+static void
+unquote_line(GString *l) {
+       GString *r;
+       gchar *p;
+       
+       p = l->str;
+       while (p[0]) {
+               if (p[0] == '>') {
+                       if (p[1] == ' ') {
+                               p++;
+                       }
+               } else {
+                       break;
+               }
+               p++;
+       }
+       g_string_erase (l, 0, p - l->str);
+}
+
+static void
+append_quoted(GString *buf, const int indent, const GString *str, const int cutpoint) {
+       int i;
+       
+       for (i=0; i<=indent; i++) {
+               g_string_append(buf, "> ");
+       }
+       if (cutpoint > 0) {
+               g_string_append_len(buf, str->str, cutpoint);
+       } else {
+               g_string_append(buf, str->str);
+       }
+       g_string_append(buf, "\n");
+}
+
+static int
+get_breakpoint_utf8(const gchar *s, const gint indent, const gint limit) {
+       gint index = 0;
+       const gchar *pos, *last;
+       gunichar *uni;
+       
+       last = NULL;
+       pos = s;
+       uni = g_utf8_to_ucs4_fast(s, -1, NULL);
+       while (pos[0]) {
+               if ((index + 2 * indent > limit) && last) {
+                       g_free(uni);
+                       return last - s;
+               }
+               if (g_unichar_isspace(uni[index])) {
+                       last = pos;
+               }
+               pos = g_utf8_next_char(pos);
+               index++;
+       }
+       g_free(uni);
+       return index;
+}
+
+static int
+get_breakpoint_ascii(const gchar *s, const gint indent, const gint limit) {
+       gint i, last;
+       
+       last = strlen(s);
+       if (last + 2 * indent < limit)
+               return last;
+       
+       for ( i=strlen(s) ; i>0; i-- ) {
+               if (s[i] == ' ') {
+                       if (i + 2 * indent <= limit) {
+                               return i;
+                       } else {
+                               last = i;
+                       }
+               }
+       }
+       return last;
+}
+
+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);
+       }
+}      
+
+gchar *
+modest_text_utils_quote(GtkTextBuffer *buf, const gchar *from, const time_t sent_date, const int limit)
+{
+       GtkTextIter iter;
+       gint indent, breakpoint, rem_indent;
+       gchar sent_str[101];
+       GString *q, *l, *remaining; /* quoted msg, line */
+       
+
+       /* 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);
+       
+       /* 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);
+       do {
+               l = get_next_line(buf, &iter);
+               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 (!gtk_text_iter_is_end(&iter));
+       
+       return g_string_free(q, FALSE);
+}
diff --git a/src/modest-text-utils.h b/src/modest-text-utils.h
new file mode 100644 (file)
index 0000000..28723f0
--- /dev/null
@@ -0,0 +1,25 @@
+/* modest-text-utils.h */
+/* insert (c)/licensing information) */
+
+#ifndef __MODEST_TEXT_UTILS_H__
+#define __MODEST_TEXT_UTILS_H__
+
+/* public */
+
+/**
+ * modest_text_utils_quote:
+ * @buf: a GtkTextBuffer which contains the message to quote
+ * @from: the sender of the original message
+ * @sent_date: sent date/time of the original message
+ * @limit: specifies the maximum characters per line in the quoted text
+ * 
+ * Returns: a string containing the quoted message
+ */
+gchar *
+modest_text_utils_quote(GtkTextBuffer *buf,
+                        const gchar *from,
+                        const time_t sent_date,
+                        const int limit);
+
+
+#endif