+
+gboolean
+modest_text_utils_is_forbidden_char (const gchar character,
+ ModestTextUtilsForbiddenCharType type)
+{
+ gint i, len;
+ const gchar *forbidden_chars = NULL;
+
+ /* We need to get the length in the switch because the
+ compiler needs to know the size at compile time */
+ switch (type) {
+ case ACCOUNT_TITLE_FORBIDDEN_CHARS:
+ forbidden_chars = account_title_forbidden_chars;
+ len = G_N_ELEMENTS (account_title_forbidden_chars);
+ break;
+ case FOLDER_NAME_FORBIDDEN_CHARS:
+ forbidden_chars = folder_name_forbidden_chars;
+ len = G_N_ELEMENTS (folder_name_forbidden_chars);
+ break;
+ case USER_NAME_FORBIDDEN_NAMES:
+ forbidden_chars = user_name_forbidden_chars;
+ len = G_N_ELEMENTS (user_name_forbidden_chars);
+ break;
+ default:
+ g_return_val_if_reached (TRUE);
+ }
+
+ for (i = 0; i < len ; i++)
+ if (forbidden_chars[i] == character)
+ return TRUE;
+
+ return FALSE; /* it's valid! */
+}
+
+gchar *
+modest_text_utils_label_get_selection (GtkLabel *label)
+{
+ gint start, end;
+ gchar *selection;
+
+ if (gtk_label_get_selection_bounds (GTK_LABEL (label), &start, &end)) {
+ const gchar *start_offset;
+ const gchar *end_offset;
+ start_offset = gtk_label_get_text (GTK_LABEL (label));
+ start_offset = g_utf8_offset_to_pointer (start_offset, start);
+ end_offset = gtk_label_get_text (GTK_LABEL (label));
+ end_offset = g_utf8_offset_to_pointer (end_offset, end);
+ selection = g_strndup (start_offset, end_offset - start_offset);
+ return selection;
+ } else {
+ return g_strdup ("");
+ }
+}
+
+static gboolean
+_forward_search_image_char (gunichar ch,
+ gpointer userdata)
+{
+ return (ch == 0xFFFC);
+}
+
+gboolean
+modest_text_utils_buffer_selection_is_valid (GtkTextBuffer *buffer)
+{
+ gboolean result;
+ GtkTextIter start, end;
+
+ g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
+
+ result = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (buffer));
+
+ /* check there are no images in selection */
+ if (result) {
+ gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
+ if (gtk_text_iter_get_char (&start)== 0xFFFC)
+ result = FALSE;
+ else {
+ gtk_text_iter_backward_char (&end);
+ if (gtk_text_iter_forward_find_char (&start, _forward_search_image_char,
+ NULL, &end))
+ result = FALSE;
+ }
+
+ }
+
+ return result;
+}
+
+static void
+remove_quotes (gchar **quotes)
+{
+ if (g_str_has_prefix (*quotes, "\"") && g_str_has_suffix (*quotes, "\"")) {
+ gchar *result;
+ result = g_strndup ((*quotes)+1, strlen (*quotes) - 2);
+ g_free (*quotes);
+ *quotes = 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);
+}
+
+gchar *
+modest_text_utils_simplify_recipients (const gchar *recipients)
+{
+ GSList *addresses, *node;
+ GString *result;
+ gboolean is_first = TRUE;
+
+ if (recipients == NULL)
+ return g_strdup ("");
+
+ addresses = modest_text_utils_split_addresses_list (recipients);
+ result = g_string_new ("");
+
+ for (node = addresses; node != NULL; node = g_slist_next (node)) {
+ const gchar *address = (const gchar *) node->data;
+ gchar *left_limit, *right_limit;
+
+ left_limit = strstr (address, "<");
+ right_limit = g_strrstr (address, ">");
+
+ if (is_first)
+ is_first = FALSE;
+ else
+ result = g_string_append (result, ", ");
+
+ if ((left_limit == NULL)||(right_limit == NULL)|| (left_limit > right_limit)) {
+ result = g_string_append (result, address);
+ } else {
+ gchar *name_side;
+ gchar *email_side;
+ name_side = g_strndup (address, left_limit - address);
+ name_side = g_strstrip (name_side);
+ remove_quotes (&name_side);
+ email_side = get_email_from_address (address);
+ if (name_side && email_side && !strcmp (name_side, email_side)) {
+ result = g_string_append (result, email_side);
+ } else {
+ result = g_string_append (result, address);
+ }
+ g_free (name_side);
+ g_free (email_side);
+ }
+
+ }
+ g_slist_foreach (addresses, (GFunc)g_free, NULL);
+ g_slist_free (addresses);
+
+ return g_string_free (result, FALSE);
+
+}
+
+GSList *
+modest_text_utils_remove_duplicate_addresses_list (GSList *address_list)
+{
+ GSList *new_list, *iter;
+ GHashTable *table;
+
+ g_return_val_if_fail (address_list, NULL);
+
+ table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+ new_list = address_list;
+ iter = address_list;
+ while (iter) {
+ const gchar* address = (const gchar*)iter->data;
+
+ /* We need only the email to just compare it and not
+ the full address which would make "a <a@a.com>"
+ different from "a@a.com" */
+ const gchar *email = get_email_from_address (address);
+
+ /* ignore the address if already seen */
+ if (g_hash_table_lookup (table, email) == 0) {
+ g_hash_table_insert (table, (gchar*)email, GINT_TO_POINTER(1));
+ iter = g_slist_next (iter);
+ } else {
+ GSList *tmp = g_slist_next (iter);
+ new_list = g_slist_delete_link (new_list, iter);
+ iter = tmp;
+ }
+ }
+
+ g_hash_table_unref (table);
+
+ return new_list;
+}
+
+gchar *
+modest_text_utils_get_secure_header (const gchar *value,
+ const gchar *header)
+{
+ const gint max_len = 128;
+ gchar *new_value = NULL;
+ gchar *needle = g_strrstr (value, header);
+
+ if (needle && value != needle)
+ new_value = g_strdup (needle + strlen (header));
+
+ if (!new_value)
+ new_value = g_strdup (value);
+
+ /* Do a max length check to prevent DoS attacks caused by huge
+ malformed headers */
+ if (g_utf8_validate (new_value, -1, NULL)) {
+ if (g_utf8_strlen (new_value, -1) > max_len) {
+ gchar *tmp = g_malloc0 (max_len * 4);
+ g_utf8_strncpy (tmp, (const gchar *) new_value, max_len);
+ g_free (new_value);
+ new_value = tmp;
+ }
+ } else {
+ if (strlen (new_value) > max_len) {
+ gchar *tmp = g_malloc0 (max_len);
+ strncpy (new_value, tmp, max_len);
+ g_free (new_value);
+ new_value = tmp;
+ }
+ }
+
+ return new_value;
+}