* got rid of most GtkTextBuffers in quote code
authorArne Zellentin <arne@kernelconcepts.de>
Tue, 30 May 2006 17:18:21 +0000 (17:18 +0000)
committerArne Zellentin <arne@kernelconcepts.de>
Tue, 30 May 2006 17:18:21 +0000 (17:18 +0000)
* some cleanup / reformatting

pmo-trunk-r127

src/modest-text-utils.c
src/modest-text-utils.h
src/modest-tny-msg-actions.c
src/modest-tny-msg-actions.h

index 0dd7229..ddf2476 100644 (file)
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
-#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, iter3;
-       gchar *tmp;
-       GString *line;
+       GString *gs;
+       const gchar *i0;
        
-       if (gtk_text_iter_is_end(iter))
+       if (iter > b + blen)
                return g_string_new("");
        
-       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
-               );
-       iter3 = iter2;
-       gtk_text_iter_forward_char(&iter2);
-       if (gtk_text_iter_is_end(&iter2)) {
-               tmp = gtk_text_buffer_get_text(b, &iter2, iter, FALSE);
-       } else {
-               tmp = gtk_text_buffer_get_text(b, &iter3, iter, FALSE);
+       i0 = iter;
+       while (iter[0]) {
+               if (iter[0] == '\n')
+                       break;
+               iter++;
        }
-       line = g_string_new(tmp);
-               
-       gtk_text_iter_forward_line(iter);
-       
-       return line;
+       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++;
@@ -79,24 +58,25 @@ get_indent_level(const char *l)
                        break;
                }
                l++;
-               
+
        }
 
-       /*      if we hit the signature marker "-- ", we return -(indent + 1). This
-       *       stops reformatting.
-       */
-       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) {
+unquote_line (GString * l)
+{
        GString *r;
        gchar *p;
-       
+
        p = l->str;
        while (p[0]) {
                if (p[0] == '>') {
@@ -112,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;
@@ -174,76 +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);
-       }
-}      
-
-
-gchar *
-modest_text_utils_quote(const gchar *to_quote, const gchar *from, const time_t sent_date, const int limit)
+get_breakpoint (const gchar * s, const gint indent, const gint limit)
 {
-       GtkTextBuffer *buf;
-       
-       buf = gtk_text_buffer_new (NULL);
-       gtk_text_buffer_set_text(buf, to_quote, -1);
-       return modest_text_utils_quote_text_buffer(buf, from, sent_date, 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_text_buffer(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;
+       const gchar *iter;
        gint indent, breakpoint, rem_indent;
        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 (remaining->str[0] || !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);
 }
index 9205e0f..53098f3 100644 (file)
@@ -21,22 +21,4 @@ modest_text_utils_quote(const gchar *buf,
                         const time_t sent_date,
                         const int limit);
 
-/**
- * modest_text_utils_quote_text_buffer:
- * @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
- * Please do not use this function, it may be removed
- */
-gchar *
-modest_text_utils_quote_text_buffer(GtkTextBuffer *buf,
-                        const gchar *from,
-                        const time_t sent_date,
-                        const int limit);
-
-
-
-#endif
+#endif /* __MODEST_TEXT_UTILS_H__ */
index 8b0e68c..4adafaf 100644 (file)
 static gchar *quote_msg (const TnyMsgIface * src, const gchar * from,
                         time_t sent_date, gint limit, gboolean textorhtml);
 static GtkTextBuffer *htmltotext (TnyMsgMimePartIface * body);
+
+
 static GtkTextBuffer *
 htmltotext (TnyMsgMimePartIface * body)
 {
        GtkTextBuffer *buf;
 
-#ifdef ACTIVATE_HACKS
+#ifdef ACTIVATE_HACKS /* it still doesn't work, don't bother! */
        GtkWidget *html, *win;
        TnyStreamIface *stream;
        GtkClipboard *clip;
@@ -49,7 +51,7 @@ htmltotext (TnyMsgMimePartIface * body)
        gtk_widget_show_all (win);
        gtk_html_select_all (GTK_HTML (html));
        clip = gtk_widget_get_clipboard (html, GDK_SELECTION_PRIMARY);
-       //clip = gtk_widget_get_clipboard(html, GDK_SELECTION_CLIPBOARD);
+       /*clip = gtk_widget_get_clipboard(html, GDK_SELECTION_CLIPBOARD);*/
        text = gtk_clipboard_wait_for_text (clip);
 
        buf = gtk_text_buffer_new (NULL);
@@ -65,7 +67,7 @@ htmltotext (TnyMsgMimePartIface * body)
 gchar *
 modest_tny_msg_actions_quote (const TnyMsgIface * self, const gchar * from,
                              time_t sent_date, gint limit,
-                             char * to_quote)
+                             gchar * to_quote)
 {
        gchar *quoted;
 
@@ -94,6 +96,8 @@ quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date,
        TnyStreamIface *stream;
        TnyMsgMimePartIface *body;
        GtkTextBuffer *buf;
+       GtkTextIter start, end;
+       const gchar *to_quote;
        gchar *quoted;
 
        /* the cast makes me uneasy... */
@@ -113,9 +117,10 @@ quote_msg (const TnyMsgIface * src, const gchar * from, time_t sent_date,
                tny_stream_iface_reset (stream);
                g_object_unref (stream);
        }
-
-       quoted = modest_text_utils_quote_text_buffer (buf, from, sent_date, limit);
-
+       
+       gtk_text_buffer_get_bounds (buf, &start, &end);
+       to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
+       quoted = modest_text_utils_quote (to_quote, from, sent_date, limit);
        g_object_unref (buf);
        return quoted;
 }
index d3fc496..f25c5c2 100644 (file)
@@ -16,7 +16,7 @@
 gchar *modest_tny_msg_actions_quote (const TnyMsgIface * self,
                                     const gchar * from,
                                     time_t sent_date,
-                                    gint limit, char *to_quote);
+                                    gint limit, gchar *to_quote);
 
 TnyMsgMimePartIface *modest_tny_msg_actions_find_body_part (TnyMsgIface * self,
                                                            const gchar * mime_type);