Fix for bug NB#81584.
[modest] / src / modest-text-utils.c
index 38f55ad..5906bb7 100644 (file)
@@ -391,7 +391,7 @@ modest_text_utils_remove_duplicate_addresses (const gchar *address_list)
 static void
 modest_text_utils_convert_buffer_to_html_start (GString *html, const gchar *data, gssize n)
 {
-       guint           i = 0;
+       guint           i;
        gboolean        space_seen = FALSE;
        guint           break_dist = 0; /* distance since last break point */
 
@@ -399,8 +399,8 @@ modest_text_utils_convert_buffer_to_html_start (GString *html, const gchar *data
                n = strlen (data);
 
        /* replace with special html chars where needed*/
-       while (i != n) {
-               char kar = data[i];
+       for (i = 0; i != n; ++i)  {
+               guchar kar = data[i];
                
                if (space_seen && kar != ' ') {
                        g_string_append_c (html, ' ');
@@ -410,8 +410,10 @@ modest_text_utils_convert_buffer_to_html_start (GString *html, const gchar *data
                /* we artificially insert a breakpoint (newline)
                 * after 256, to make sure our lines are not so long
                 * they will DOS the regexping later
+                * Also, check that kar is ASCII to make sure that we
+                * don't break a UTF8 char in two
                 */
-               if (++break_dist == 256) {
+               if (++break_dist >= 256 && kar < 127) {
                        g_string_append_c (html, '\n');
                        break_dist = 0;
                }
@@ -443,25 +445,8 @@ modest_text_utils_convert_buffer_to_html_start (GString *html, const gchar *data
                                space_seen = TRUE;
                        break;
                default:
-                       /* Optimization to copy single ascii
-                        * characters faster */
-                       if (kar > 31 && kar < 127) {
-                               g_string_append_c (html, kar);
-                       } else {
-                               /* Important: copy full UTF-8 characters,
-                                * don't copy them byte by byte */
-                               gunichar c = g_utf8_get_char_validated (data+i, -1);
-                               if (c != (gunichar) -1 && c != (gunichar) -2) {
-                                       const gchar *copyfrom = data + i;
-                                       int len = g_utf8_next_char(copyfrom) - copyfrom;
-                                       g_string_append_len (html, copyfrom, len);
-                                       i += len - 1;
-                               } else {
-                                       g_warning ("%s: non-UTF8 byte found, skipping", __FUNCTION__);
-                               }
-                       }
+                       g_string_append_c (html, kar);
                }
-               i++;
        }
 }
 
@@ -668,6 +653,30 @@ modest_text_utils_address_range_at_position (const gchar *recipients_list,
                *end = range_end;
 }
 
+gchar *
+modest_text_utils_address_with_standard_length (const gchar *recipients_list)
+{
+       gchar ** splitted;
+       gchar ** current;
+       GString *buffer = g_string_new ("");
+
+       splitted = g_strsplit (recipients_list, "\n", 0);
+       current = splitted;
+       while (*current) {
+               gchar *line;
+               if (current != splitted)
+                       buffer = g_string_append_c (buffer, '\n');
+               line = g_strndup (*splitted, 1000);
+               buffer = g_string_append (buffer, line);
+               g_free (line);
+               current++;
+       }
+
+       g_strfreev (splitted);
+
+       return g_string_free (buffer, FALSE);
+}
+
 
 /* ******************************************************************* */
 /* ************************* UTILIY FUNCTIONS ************************ */
@@ -849,10 +858,15 @@ modest_text_utils_quote_plain_text (const gchar *text,
        gsize len;
        gchar *attachments_string = NULL;
 
-       /* remaining will store the rest of the line if we have to break it */
        q = g_string_new ("\n");
+       if (signature != NULL) {
+               q = g_string_append (q, signature);
+               q = g_string_append_c (q, '\n');
+       }
        q = g_string_append (q, cite);
        q = g_string_append_c (q, '\n');
+
+       /* remaining will store the rest of the line if we have to break it */
        remaining = g_string_new ("");
 
        iter = text;
@@ -899,11 +913,6 @@ modest_text_utils_quote_plain_text (const gchar *text,
        q = g_string_append (q, attachments_string);
        g_free (attachments_string);
 
-       if (signature != NULL) {
-               q = g_string_append_c (q, '\n');
-               q = g_string_append (q, signature);
-       }
-
        return g_string_free (q, FALSE);
 }
 
@@ -1702,3 +1711,23 @@ modest_text_utils_buffer_selection_is_valid (GtkTextBuffer *buffer)
 
        return result;
 }
+
+gchar *
+modest_text_utils_escape_mnemonics (const gchar *text)
+{
+       const gchar *p;
+       GString *result = NULL;
+
+       if (text == NULL)
+               return NULL;
+
+       result = g_string_new ("");
+       for (p = text; *p != '\0'; p++) {
+               if (*p == '_')
+                       result = g_string_append (result, "__");
+               else
+                       result = g_string_append_c (result, *p);
+       }
+       
+       return g_string_free (result, FALSE);
+}