X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fmodest-text-utils.c;h=cc02fb52b9e9e3d53ced5efb77cab0942eac0631;hb=54610a0e8f44ebf17691887661cc54b78b379702;hp=23942cccaec7679aaecdfa54a28e6c13a0fcd570;hpb=f0b372e4c57cc9f999fefa3d69d08b608d8319f4;p=modest diff --git a/src/modest-text-utils.c b/src/modest-text-utils.c index 23942cc..cc02fb5 100644 --- a/src/modest-text-utils.c +++ b/src/modest-text-utils.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -207,23 +208,26 @@ modest_text_utils_cite (const gchar *text, const gchar *from, time_t sent_date) { - gchar *retval; - gchar *tmp_sig; - + gchar *retval, *tmp; + g_return_val_if_fail (text, NULL); g_return_val_if_fail (content_type, NULL); - - if (!signature) { - tmp_sig = g_strdup (text); - } else { - tmp_sig = g_strconcat (text, "\n", MODEST_TEXT_UTILS_SIGNATURE_MARKER, "\n", signature, NULL); - } if (strcmp (content_type, "text/html") == 0) { - retval = modest_text_utils_convert_to_html_body (tmp_sig, -1, TRUE); - g_free (tmp_sig); + tmp = modest_text_utils_convert_to_html_body (text, -1, TRUE); + if (signature) { + gchar *colored_signature = modest_text_utils_create_colored_signature (signature); + retval = g_strconcat (tmp, colored_signature, NULL); + g_free (colored_signature); + g_free (tmp); + } else { + retval = tmp; + } } else { - retval = tmp_sig; + if (signature) + retval = g_strconcat (text, "\n", MODEST_TEXT_UTILS_SIGNATURE_MARKER, "\n", signature, NULL); + else + retval = g_strdup (text); } return retval; @@ -302,8 +306,7 @@ modest_text_utils_derived_subject (const gchar *subject, gboolean is_reply) gchar *tmp, *subject_dup, *retval, *prefix; const gchar *untranslated_prefix; gint prefix_len, untranslated_prefix_len; - gboolean translated_found = FALSE; - gboolean first_time; + gboolean found = FALSE; if (!subject || subject[0] == '\0') subject = _("mail_va_no_subject"); @@ -318,54 +321,33 @@ modest_text_utils_derived_subject (const gchar *subject, gboolean is_reply) untranslated_prefix = (is_reply) ? "Re:" : "Fw:"; untranslated_prefix_len = 3; - /* We do not want things like "Re: Re: Re:" or "Fw: Fw:" so - delete the previous ones */ - first_time = TRUE; - do { - if (g_str_has_prefix (tmp, prefix)) { - tmp += prefix_len; - tmp = g_strchug (tmp); - /* Do not consider translated prefixes in the - middle of a Re:Re:..Re: like sequence */ - if (G_UNLIKELY (first_time)) - translated_found = TRUE; - } else if (g_str_has_prefix (tmp, untranslated_prefix)) { - tmp += untranslated_prefix_len; - tmp = g_strchug (tmp); - } else { - gchar *prefix_down, *tmp_down; - - /* We need this to properly check the cases of - some clients adding FW: instead of Fw: for - example */ - prefix_down = g_utf8_strdown (prefix, -1); - tmp_down = g_utf8_strdown (tmp, -1); - if (g_str_has_prefix (tmp_down, prefix_down)) { - tmp += prefix_len; - tmp = g_strchug (tmp); - g_free (prefix_down); - g_free (tmp_down); - } else { - g_free (prefix_down); - g_free (tmp_down); - break; - } - } - first_time = FALSE; - } while (tmp); + if (g_str_has_prefix (tmp, prefix) || + g_str_has_prefix (tmp, untranslated_prefix)) { + found = TRUE; + } else { + gchar *prefix_down, *tmp_down, *untranslated_down; - if (!g_strcmp0 (subject, tmp)) { - /* normal case */ - retval = g_strdup_printf ("%s %s", untranslated_prefix, tmp); + prefix_down = g_utf8_strdown (prefix, -1); + untranslated_down = g_utf8_strdown (untranslated_prefix, -1); + tmp_down = g_utf8_strdown (tmp, -1); + if (g_str_has_prefix (tmp_down, prefix_down) || + g_str_has_prefix (tmp_down, untranslated_down) || + (!is_reply && g_str_has_prefix (tmp_down, "fwd:"))) + found = TRUE; + + g_free (prefix_down); + g_free (untranslated_down); + g_free (tmp_down); + } + + if (found) { + /* If the prefix is already present do not touch the subject */ + retval = subject_dup; } else { - if (translated_found) { - /* Found a translated prefix, i.e, "VS:" in Finish */ - retval = g_strdup_printf ("%s %s", prefix, tmp); - } else { - retval = g_strdup_printf ("%s %s", untranslated_prefix, tmp); - } + /* Normal case, add the prefix */ + retval = g_strdup_printf ("%s %s", untranslated_prefix, tmp); + g_free (subject_dup); } - g_free (subject_dup); g_free (prefix); return retval; @@ -901,9 +883,11 @@ unquote_line (GString * l, const gchar *quote_symbol) quote_len = strlen (quote_symbol); while (p[0]) { if (g_str_has_prefix (p, quote_symbol)) { - if (p[quote_len] == ' ') { - p += quote_len; + p+=quote_len; + while (p[0] && p[0] == ' ') { + p++; } + continue; } else { break; } @@ -941,6 +925,9 @@ get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit) const gchar *pos, *last; gunichar *uni; + if (2*indent >= limit) + return strlen (s); + indent = indent < 0 ? abs (indent) - 1 : indent; last = NULL; @@ -1024,15 +1011,42 @@ modest_text_utils_quote_body (GString *output, const gchar *text, gsize len; gint indent, breakpoint, rem_indent = 0; GString *l, *remaining; + gchar *forced_wrap_append; iter = text; len = strlen(text); remaining = g_string_new (""); + forced_wrap_append = NULL; do { - l = get_next_line (text, len, iter); - iter = iter + l->len + 1; - indent = get_indent_level (l->str); - unquote_line (l, quote_symbol); + + if (forced_wrap_append) { + gint next_line_indent; + gint l_len_with_indent; + + g_string_erase (remaining, 0, -1); + next_line_indent = get_indent_level (iter); + l = get_next_line (text, len, iter); + l_len_with_indent = l->len; + unquote_line (l, quote_symbol); + if ((l->str && l->str[0] == '\0') || (next_line_indent != indent)) { + g_string_free (l, TRUE); + l = g_string_new (forced_wrap_append); + } else { + gunichar first_in_l; + iter = iter + l_len_with_indent + 1; + first_in_l = g_utf8_get_char_validated (l->str, l->len); + if (!g_unichar_isspace (first_in_l)) + l = g_string_prepend (l, " "); + l = g_string_prepend (l, forced_wrap_append); + } + g_free (forced_wrap_append); + forced_wrap_append = NULL; + } else { + l = get_next_line (text, len, iter); + iter = iter + l->len + 1; + indent = get_indent_level (l->str); + unquote_line (l, quote_symbol); + } if (remaining->len) { if (l->len && indent == rem_indent) { @@ -1045,8 +1059,14 @@ modest_text_utils_quote_body (GString *output, const gchar *text, get_breakpoint (remaining->str, rem_indent, limit); - append_quoted (output, quote_symbol, rem_indent, - remaining, breakpoint); + if (breakpoint < remaining->len) { + g_free (forced_wrap_append); + forced_wrap_append = g_strdup (remaining->str + breakpoint); + } else { + if (!forced_wrap_append) + append_quoted (output, quote_symbol, rem_indent, + remaining, breakpoint); + } g_string_erase (remaining, 0, breakpoint); remaining_first = g_utf8_get_char_validated (remaining->str, remaining->len); @@ -1065,9 +1085,13 @@ modest_text_utils_quote_body (GString *output, const gchar *text, g_string_erase (remaining, 0, 1); } rem_indent = indent; + if (remaining->len > 0) { + g_free (forced_wrap_append); + forced_wrap_append = g_strdup (remaining->str); + } append_quoted (output, quote_symbol, indent, l, breakpoint); g_string_free (l, TRUE); - } while ((iter < text + len) || (remaining->str[0])); + } while ((iter < text + len) || (remaining->str[0]) || forced_wrap_append); return output; } @@ -1122,18 +1146,19 @@ modest_text_utils_quote_html (const gchar *text, { GString *result_string; - result_string = + result_string = g_string_new ( \ "\n" \ "\n" \ - "\n
\n"); + "\n"); if (text || cite || signature) { GString *quoted_text; g_string_append (result_string, "
\n");
 		if (signature) {
-			quote_html_add_to_gstring (result_string, MODEST_TEXT_UTILS_SIGNATURE_MARKER);
-			quote_html_add_to_gstring (result_string, signature);
+			gchar *colored_signature = modest_text_utils_create_colored_signature (signature);
+			g_string_append_printf (result_string, "%s
", colored_signature); + g_free (colored_signature); } quote_html_add_to_gstring (result_string, cite); quoted_text = g_string_new (""); @@ -2283,3 +2308,60 @@ modest_text_utils_no_recipient (GtkTextBuffer *buffer) return retval; } + +gchar * +modest_text_utils_create_colored_signature (const gchar *signature) +{ + gchar *gray_color_markup = NULL, *retval; + GdkColor color; + GtkWidget *widget; + + /* Get color from widgets */ + widget = (GtkWidget *) modest_window_mgr_get_current_top (modest_runtime_get_window_mgr ()); + if (widget && gtk_style_lookup_color (gtk_widget_get_style (widget), "SecondaryTextColor", &color)) + gray_color_markup = modest_text_utils_get_color_string (&color); + + retval = g_strdup_printf ("
\n%s
\n%s
\n
", + (gray_color_markup) ? gray_color_markup : "#babababababa", + MODEST_TEXT_UTILS_SIGNATURE_MARKER, + signature); + + if (gray_color_markup) + g_free (gray_color_markup); + + return retval; +} + +gboolean +modest_text_utils_live_search_find (const gchar *haystack, const gchar *needles) +{ + gchar *haystack_fold; + gchar *needles_fold; + gchar **needle, **current; + gboolean match; + + match = FALSE; + + haystack_fold = g_utf8_casefold (haystack, -1); + needles_fold = g_utf8_casefold (needles, -1); + + needle = g_strsplit (needles_fold, " ", -1); + + current = needle; + while (current && *current != NULL) { + + if (g_strstr_len (haystack_fold, -1, *current) != NULL) { + match = TRUE; + } else { + match = FALSE; + break; + } + current++; + } + + g_strfreev (needle); + g_free (needles_fold); + g_free (haystack_fold); + + return match; +}