* src/widgets/modest-recpt-editor.c:
[modest] / src / widgets / modest-recpt-editor.c
index ddf648c..3f857c8 100644 (file)
@@ -100,6 +100,8 @@ static GtkTextTag *prev_iter_has_recipient (GtkTextIter *iter);
 /* static GtkTextTag *next_iter_has_recipient (GtkTextIter *iter); */
 static void select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag);
 static gboolean quote_opened (GtkTextIter *iter);
+static gboolean is_valid_insert (const gchar *text, gint len);
+static gchar *create_valid_text (const gchar *text, gint len);
 
 /**
  * modest_recpt_editor_new:
@@ -122,14 +124,17 @@ modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar
 {
        ModestRecptEditorPrivate *priv;
        GtkTextBuffer *buffer = NULL;
+       gchar *valid_recipients = NULL;
 
        g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
        priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
 
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
 
+       valid_recipients = create_valid_text (recipients, -1);
        g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
-       gtk_text_buffer_set_text (buffer, recipients, -1);
+       gtk_text_buffer_set_text (buffer, valid_recipients, -1);
+       g_free (valid_recipients);
        if (GTK_WIDGET_REALIZED (recpt_editor))
                gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
        g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
@@ -440,6 +445,80 @@ modest_recpt_editor_on_focus_in (GtkTextView *text_view,
        gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (priv->text_view));
 }
 
+static gboolean
+is_valid_insert (const gchar *text, gint len)
+{
+       gunichar c;
+       gunichar next_c;
+       gint i= 0;
+       gboolean quoted = FALSE;
+       const gchar *current, *next_current;
+       if (text == NULL)
+               return TRUE;
+       current = text;
+
+       while (((len == -1)||(i < len)) && (*current != '\0')) {
+               c = g_utf8_get_char (current);
+               next_current = g_utf8_next_char (current);
+               if (next_current && *next_current != '\0')
+                       next_c = g_utf8_get_char (g_utf8_next_char (current));
+               else
+                       next_c = 0;
+               if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
+                       if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
+                               return FALSE;
+               }
+               if (c == 0x2022 || c == 0xfffc ||
+                   c == g_utf8_get_char ("\n") ||
+                   c == g_utf8_get_char ("\t"))
+                       return FALSE;
+               if (c == g_utf8_get_char ("\""))
+                       quoted = !quoted;
+               current = g_utf8_next_char (current);
+               i = current - text;
+       }
+       return TRUE;
+}
+
+static gchar *
+create_valid_text (const gchar *text, gint len)
+{
+       gunichar c;
+       gunichar next_c;
+       gint i= 0;
+       GString *str;
+       gboolean quoted = FALSE;
+       const gchar *current, *next_current;
+
+       if (text == NULL)
+               return NULL;
+
+       str = g_string_new ("");
+       current = text;
+
+       while (((len == -1)||(i < len)) && (*current != '\0')) {
+               c = g_utf8_get_char (current);
+               next_current = g_utf8_next_char (current);
+               if (next_current && *next_current != '\0')
+                       next_c = g_utf8_get_char (g_utf8_next_char (current));
+               else
+                       next_c = 0;
+               if (c != 0x2022 && c != 0xfffc && 
+                   c != g_utf8_get_char ("\n") &&
+                   c != g_utf8_get_char ("\t"))
+                       str = g_string_append_unichar (str, c);
+               if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) {
+                       if ((next_c != 0) && (next_c != g_utf8_get_char ("\n")))
+                               str = g_string_append_c (str, '\n');
+               }
+               if (c == g_utf8_get_char ("\""))
+                       quoted = !quoted;
+               current = g_utf8_next_char (current);
+               i = current - text;
+       }
+       return g_string_free (str, FALSE);
+}
+
 static void 
 modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
                                    GtkTextIter *location,
@@ -451,6 +530,13 @@ modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
        gunichar prev_char;
        ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
 
+       if (!is_valid_insert (text, len)) {
+               gchar *new_text = create_valid_text (text, len);
+               g_signal_stop_emission_by_name (G_OBJECT (buffer), "insert-text");
+               gtk_text_buffer_insert (buffer, location, new_text, -1);
+               g_free (new_text);
+       }
+
        if (iter_has_recipient (location)) {
                gtk_text_buffer_get_end_iter (buffer, location);
                gtk_text_buffer_place_cursor (buffer, location);