* modest-mail-operation.c: refactored some common code.
[modest] / src / modest-text-utils.c
index c43475e..2f28c91 100644 (file)
@@ -1,72 +1,82 @@
-/* modest-ui.c */
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 
-/* insert (c)/licensing information) */
+/* modest-ui.c */
 
 #include <gtk/gtk.h>
 #include <string.h>
+#include <stdlib.h>
+#include <glib/gi18n.h>
+#include "modest-text-utils.h"
 
 
 #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++;
@@ -77,24 +87,24 @@ 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) {
-       GString *r;
+unquote_line (GString * l)
+{
        gchar *p;
-       
+
        p = l->str;
        while (p[0]) {
                if (p[0] == '>') {
@@ -110,56 +120,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;
@@ -172,63 +186,223 @@ 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);
        }
-}      
+}
+
+
+
+/* just to prevent warnings:
+ * warning: `%x' yields only last 2 digits of year in some locales
+ */
+static size_t
+my_strftime(char *s, size_t max, const char  *fmt,  const
+           struct tm *tm) {
+       return strftime(s, max, fmt, tm);
+}
+
+static gchar *
+cite (const time_t sent_date, const gchar *from) {
+       gchar *str;
+       gchar sent_str[101];
+
+       /* format sent_date */
+       my_strftime (sent_str, 100, "%c", localtime (&sent_date));
+       return g_strdup_printf (N_("On %s, %s wrote:\n"), sent_str, from);
+}
+
 
 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;
-       gchar sent_str[101];
-       GString *q, *l, *remaining; /* quoted msg, line */
-       
+       const gchar *iter;
+       gint indent, breakpoint, rem_indent = 0;
+       GString *q, *l, *remaining;
+       gsize len;
+       gchar *tmp;
 
        /* 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);
-       
+       tmp = cite (sent_date, from);
+       q = g_string_new (tmp);
+       g_free (tmp);
+
        /* 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);
+}
+
+static gchar *
+create_derivated_subject (const gchar *subject, const gchar *prefix)
+{
+       gchar *tmp, *buffer;
+
+       if (!subject)
+               return g_strdup_printf ("%s ", prefix);
+
+       tmp = g_strchug (g_strdup (subject));
+
+       if (!strncmp (tmp, prefix, strlen (prefix))) {
+               return tmp;
+       } else {
+               g_free (tmp);
+               return g_strdup_printf ("%s %s", prefix, subject);
+       }
+}
+
+/**
+ * modest_text_utils_create_reply_subject:
+ * @subject: 
+ * 
+ * creates a new subject with a reply prefix if not present before
+ * 
+ * Returns: a new subject with the reply prefix
+ **/
+gchar * 
+modest_text_utils_create_reply_subject (const gchar *subject)
+{
+       return create_derivated_subject (subject, _("Re:"));
+}
+
+/**
+ * modest_text_utils_create_forward_subject:
+ * @subject: 
+ * 
+ * creates a new subject with a forward prefix if not present before
+ * 
+ * Returns:  a new subject with the forward prefix
+ **/
+gchar * 
+modest_text_utils_create_forward_subject (const gchar *subject)
+{
+       return create_derivated_subject (subject, _("Fw:"));
+}
+
+gchar *
+modest_text_utils_create_cited_text (const gchar *from, 
+                                    time_t sent_date, 
+                                    const gchar *text)
+{
+       gchar *tmp, *retval;
+
+       tmp = cite (sent_date, from);
+       retval = g_strdup_printf ("%s%s\n", tmp, text);
+       g_free (tmp);
+
+       return retval;
+}
+
+/**
+ * modest_text_utils_create_inlined_text:
+ * @text: the original text
+ * 
+ * creates a new string with the "Original message" text prepended to
+ * the text passed as argument and some data of the header
+ * 
+ * Returns:  a newly allocated text
+ **/
+gchar * 
+modest_text_utils_create_inlined_text (const gchar *from,
+                                      time_t sent_date,
+                                      const gchar *to,
+                                     const gchar *subject,
+                                     const gchar *text)
+{
+       gchar sent_str[101];
+
+       my_strftime (sent_str, 100, "%c", localtime (&sent_date));
+
+       return g_strdup_printf ("%s\n%s %s\n%s %s\n%s %s\n%s %s\n\n%s", 
+                               _("-----Forwarded Message-----"),
+                               _("From:"), from,
+                               _("Sent:"), sent_str,
+                               _("To:"), to,
+                               _("Subject:"), subject,
+                               text);
+}
+
+gchar *
+modest_text_utils_remove_mail_from_mail_list (const gchar *emails,
+                                             const gchar *email)
+{
+       char *dup, *token, *ptr, *result;
+       GString *filtered_emails;
+
+       if (!emails)
+               return NULL;
+
+       /* Search for substring */
+       if (!strstr ((const char *) emails, (const char *) email))
+               return g_strdup (emails);
+
+       dup = g_strdup (emails);
+       filtered_emails = g_string_new (NULL);
+
+       token = strtok_r (dup, ",", &ptr);
+
+       while (token != NULL) {
+               /* Add to list if not found */
+               if (!strstr ((const char *) token, (const char *) email)) {
+                       if (G_UNLIKELY (filtered_emails->len) == 0)
+                               g_string_append_printf (filtered_emails, "%s", token);
+                       else
+                               g_string_append_printf (filtered_emails, ",%s", token);
+               }
+               token = strtok_r (NULL, ",", &ptr);
+       }
+       result = filtered_emails->str;
+
+       /* Clean */
+       g_free (dup);
+       g_string_free (filtered_emails, FALSE);
+
+       return result;
 }