Better parsing support
[modest] / src / widgets / modest-recpt-editor.c
index b4caefc..14c8d11 100644 (file)
@@ -43,6 +43,7 @@
 #include <string.h>
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtk.h>
+#include <modest-ui-constants.h>
 
 /* FIXNE: we should have no maemo-deps in widgets/ */
 #ifndef MODEST_TOOLKIT_GTK
@@ -70,7 +71,8 @@ struct _ModestRecptEditorPrivate
        GtkWidget *abook_button;
        GtkWidget *scrolled_window;
        gchar *recipients;
-
+       gulong on_mark_set_handler;
+       gboolean show_abook;
 };
 
 #define MODEST_RECPT_EDITOR_GET_PRIVATE(o)     \
@@ -86,18 +88,25 @@ static void modest_recpt_editor_class_init (ModestRecptEditorClass *klass);
 /* widget events */
 static void modest_recpt_editor_on_abook_clicked (GtkButton *button,
                                                  ModestRecptEditor *editor);
-static gboolean modest_recpt_editor_on_button_release_event (GtkWidget *widget,
-                                                            GdkEventButton *event,
-                                                            ModestRecptEditor *editor);
-static void modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor);
+static void modest_recpt_editor_add_tags (ModestRecptEditor *editor,
+                                         const gchar * recipient_id);
 static gboolean modest_recpt_editor_on_focus_in (GtkTextView *text_view,
                                             GdkEventFocus *event,
                                             ModestRecptEditor *editor);
+static void modest_recpt_editor_on_mark_set (GtkTextBuffer *buffer,
+                                            GtkTextIter *iter,
+                                            GtkTextMark *mark,
+                                            ModestRecptEditor *editor);
 static void modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
                                                GtkTextIter *location,
                                                gchar *text,
                                                gint len,
                                                ModestRecptEditor *editor);
+static void modest_recpt_editor_on_insert_text_after (GtkTextBuffer *buffer,
+                                                     GtkTextIter *location,
+                                                     gchar *text,
+                                                     gint len,
+                                                     ModestRecptEditor *editor);
 static gboolean modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
                                                          GdkEventKey *key,
                                                          ModestRecptEditor *editor);
@@ -121,7 +130,7 @@ modest_recpt_editor_new (void)
 {
        ModestRecptEditor *self = g_object_new (MODEST_TYPE_RECPT_EDITOR, 
                                                "homogeneous", FALSE,
-                                               "spacing", 1,
+                                               "spacing", MODEST_MARGIN_NONE,
                                                NULL);
 
        return GTK_WIDGET (self);
@@ -145,11 +154,13 @@ modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar
 
        valid_recipients = create_valid_text (recipients, -1);
        g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
+       g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
        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);
+       g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
 
 }
 
@@ -182,10 +193,12 @@ modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar
        gtk_text_buffer_get_end_iter (buffer, &iter);
 
        g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
+       g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
 
        gtk_text_buffer_insert (buffer, &iter, string_to_add, -1);
-       
+
        g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
+       g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
 
        if (GTK_WIDGET_REALIZED (recpt_editor))
                gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
@@ -199,7 +212,6 @@ modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSL
        ModestRecptEditorPrivate *priv;
        GtkTextBuffer *buffer = NULL;
        GtkTextIter start, end, iter;
-       GtkTextTag *tag = NULL;
        GSList *node;
        gboolean is_first_recipient = TRUE;
       
@@ -213,6 +225,7 @@ modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSL
 #endif
 
        g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
+       g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
        gtk_text_buffer_get_bounds (buffer, &start, &end);
        if (gtk_text_buffer_get_char_count (buffer) > 0) {
                gchar * buffer_contents;
@@ -230,14 +243,6 @@ modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSL
 
        gtk_text_buffer_get_end_iter (buffer, &iter);
 
-       tag = gtk_text_buffer_create_tag (buffer, NULL, 
-                                         "underline", PANGO_UNDERLINE_SINGLE,
-                                         "wrap-mode", GTK_WRAP_NONE,
-                                         "editable", TRUE, NULL);
-
-       g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
-       g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
-
        for (node = email_list; node != NULL; node = g_slist_next (node)) {
                gchar *recipient = (gchar *) node->data;
 
@@ -246,15 +251,15 @@ modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSL
                        if (!is_first_recipient)
                        gtk_text_buffer_insert (buffer, &iter, "\n", -1);
 
-                       gtk_text_buffer_insert_with_tags (buffer, &iter, recipient, -1, tag, NULL);
+                       gtk_text_buffer_insert (buffer, &iter, recipient, -1);
                        gtk_text_buffer_insert (buffer, &iter, ";", -1);
                        is_first_recipient = FALSE;
                }
        }
-       g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
-
-       modest_recpt_editor_move_cursor_to_end (recpt_editor);
+       modest_recpt_editor_add_tags (recpt_editor, recipient_id);
 
+       g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
+       g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
 }
 
 void 
@@ -262,6 +267,25 @@ modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_ed
                                                     GtkTextIter *start, GtkTextIter *end,
                                                     GSList *email_list, const gchar * recipient_id)
 {
+       GSList *email_lists_list;
+       GSList *recipient_ids_list;
+
+       email_lists_list = g_slist_append (NULL, email_list);
+       recipient_ids_list = g_slist_append (NULL, (gpointer) recipient_id);
+
+       modest_recpt_editor_replace_with_resolved_recipients (recpt_editor, start, end,
+                                                             email_lists_list, recipient_ids_list);
+
+       g_slist_free (email_lists_list);
+       g_slist_free (recipient_ids_list);
+
+}
+
+void 
+modest_recpt_editor_replace_with_resolved_recipients (ModestRecptEditor *recpt_editor, 
+                                                    GtkTextIter *start, GtkTextIter *end,
+                                                    GSList *email_lists_list, GSList * recipient_ids_list)
+{
        ModestRecptEditorPrivate *priv;
        GtkTextBuffer *buffer;
        GtkTextTag *tag;
@@ -277,11 +301,14 @@ modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_ed
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
 #endif
        g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
+       g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
 
        gtk_text_buffer_delete (buffer, start, end);
 
-       if (email_list) {
-         
+       while (email_lists_list && recipient_ids_list) {
+               gchar *recipient_id = (gchar *) recipient_ids_list->data;
+               GSList *email_list = (GSList *) email_lists_list->data;
+
                tag = gtk_text_buffer_create_tag (buffer, NULL, 
                                                  "underline", PANGO_UNDERLINE_SINGLE,
                                                  "wrap-mode", GTK_WRAP_NONE,
@@ -289,24 +316,32 @@ modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_ed
 
                g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
                g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
-               
+
                for (node = email_list; node != NULL; node = g_slist_next (node)) {
                        gchar *recipient = (gchar *) node->data;
 
                        if ((recipient) && (strlen (recipient) != 0)) {
-                               
+
                                if (!is_first_recipient)
                                        gtk_text_buffer_insert (buffer, start, "\n", -1);
 
                                gtk_text_buffer_insert_with_tags (buffer, start, recipient, -1, tag, NULL);
-                               
+
                                if (node->next != NULL)
                                        gtk_text_buffer_insert (buffer, start, ";", -1);
                                is_first_recipient = FALSE;
                        }
                }
+
+               email_lists_list = g_slist_next (email_lists_list);
+               recipient_ids_list = g_slist_next (recipient_ids_list);
+
+               /* Add a separator between lists of emails*/
+               if (recipient_ids_list)
+                       gtk_text_buffer_insert (buffer, start, ";", -1);
        }
        g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
+       g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, recpt_editor);
 
 }
 
@@ -352,11 +387,15 @@ modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
 {
        ModestRecptEditorPrivate *priv;
        GtkWidget *abook_icon;
+       GtkWidget *abook_align;
        GtkTextBuffer *buffer;
 
        priv = MODEST_RECPT_EDITOR_GET_PRIVATE (instance);
 
+       priv->show_abook = TRUE;
        priv->abook_button = gtk_button_new ();
+       gtk_widget_set_no_show_all (GTK_WIDGET (priv->abook_button), TRUE);
+       gtk_widget_show (priv->abook_button);
 #ifdef MODEST_TOOLKIT_HILDON2
        gtk_widget_set_size_request (priv->abook_button, RECPT_BUTTON_WIDTH_HILDON2, -1);
 #else
@@ -398,7 +437,10 @@ modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
        gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
 /*     gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0); */
 #endif
-       gtk_box_pack_end (GTK_BOX (instance), priv->abook_button, FALSE, FALSE, 0);
+       abook_align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
+       gtk_alignment_set_padding (GTK_ALIGNMENT (abook_align), 0, 0, MODEST_MARGIN_DEFAULT, 0);
+       gtk_container_add (GTK_CONTAINER (abook_align), priv->abook_button);
+       gtk_box_pack_end (GTK_BOX (instance), abook_align, FALSE, FALSE, 0);
 
        gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
        gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (priv->text_view), TRUE);
@@ -415,12 +457,18 @@ modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
 #endif
        g_signal_connect (G_OBJECT (priv->abook_button), "clicked", G_CALLBACK (modest_recpt_editor_on_abook_clicked), instance);
-       g_signal_connect (G_OBJECT (priv->text_view), "button-release-event", G_CALLBACK (modest_recpt_editor_on_button_release_event), instance);
        g_signal_connect (G_OBJECT (priv->text_view), "key-press-event", G_CALLBACK (modest_recpt_editor_on_key_press_event), instance);
        g_signal_connect (G_OBJECT (priv->text_view), "focus-in-event", G_CALLBACK (modest_recpt_editor_on_focus_in), instance);
-       g_signal_connect (G_OBJECT (buffer), "insert-text", G_CALLBACK (modest_recpt_editor_on_insert_text), instance);
+       g_signal_connect (G_OBJECT (buffer), "insert-text",
+                         G_CALLBACK (modest_recpt_editor_on_insert_text),
+                         instance);
+       g_signal_connect_after (G_OBJECT (buffer), "insert-text",
+                               G_CALLBACK (modest_recpt_editor_on_insert_text_after),
+                               instance);
 
-/*     gtk_container_set_focus_child (GTK_CONTAINER (instance), priv->text_view); */
+       priv->on_mark_set_handler = g_signal_connect (G_OBJECT (buffer), "mark-set", 
+                                                     G_CALLBACK (modest_recpt_editor_on_mark_set), 
+                                                     instance);
 
        return;
 }
@@ -464,23 +512,25 @@ modest_recpt_editor_on_abook_clicked (GtkButton *button, ModestRecptEditor *edit
        g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
 }
 
-static gboolean
-modest_recpt_editor_on_button_release_event (GtkWidget *widget,
-                                            GdkEventButton *event,
-                                            ModestRecptEditor *recpt_editor)
+static void
+modest_recpt_editor_on_mark_set (GtkTextBuffer *buffer,
+                                GtkTextIter *iter,
+                                GtkTextMark *mark,
+                                ModestRecptEditor *recpt_editor)
 {
        ModestRecptEditorPrivate *priv;
-       GtkTextIter location, start, end;
-       GtkTextMark *mark;
-       GtkTextBuffer *buffer;
+       GtkTextIter start, end;
+       GtkTextMark *selection_bound;
        GtkTextTag *tag;
        gboolean selection_changed = FALSE;
-       
+
        priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
 
        buffer = modest_recpt_editor_get_buffer (recpt_editor);
-       mark = gtk_text_buffer_get_insert (buffer);
-       gtk_text_buffer_get_iter_at_mark (buffer, &location, mark);
+       selection_bound = gtk_text_buffer_get_selection_bound (buffer);
+
+       if (mark != selection_bound)
+               return;
 
        gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
 
@@ -489,22 +539,24 @@ modest_recpt_editor_on_button_release_event (GtkWidget *widget,
                if (!gtk_text_iter_begins_tag (&start, tag)) {
                        gtk_text_iter_backward_to_tag_toggle (&start, tag);
                        selection_changed = TRUE;
-               } 
+               }
 
        tag = iter_has_recipient (&end);
-       if (tag != NULL) 
+       if (tag != NULL)
                if (!gtk_text_iter_ends_tag (&end, tag)) {
                        gtk_text_iter_forward_to_tag_toggle (&end, tag);
                        selection_changed = TRUE;
                }
 
        if (selection_changed) {
+               /* We block this signal handler in order to prevent a
+                  stack overflow caused by recursive calls to this
+                  handler as the select_range call could issue a
+                  "mark-set" signal */
+               g_signal_handler_block (buffer, priv->on_mark_set_handler);
                gtk_text_buffer_select_range (buffer, &start, &end);
-       } else {
-               GTK_TEXT_VIEW (priv->text_view)->pending_place_cursor_button = 0;
+               g_signal_handler_unblock (buffer, priv->on_mark_set_handler);
        }
-
-       return FALSE;
 }
 
 static gboolean 
@@ -580,45 +632,84 @@ create_valid_text (const gchar *text, gint len)
                        next_c = g_utf8_get_char (g_utf8_next_char (current));
                else
                        next_c = 0;
-               if (c != 0x2022 && c != 0xfffc && 
+               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 ((next_c != 0) && (next_c != g_utf8_get_char ("\n"))) {
+                               gchar *last_separator = MAX (g_utf8_strrchr(str->str, -1, g_utf8_get_char (",")),
+                                                            g_utf8_strrchr(str->str, -1, g_utf8_get_char (";")));
+                               if (last_separator) {
+                                       gchar *last_at = g_utf8_strrchr (str->str, -1, g_utf8_get_char ("@"));
+                                       if (last_at) {
+                                               if (last_at > last_separator)
+                                                       str = g_string_append_c (str, '\n');
+                                       }
+                               } else {
+                                       if (g_utf8_strrchr (str->str, -1, g_utf8_get_char ("@")))
+                                               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 
+/* Called after the default handler, and thus after the text was
+   inserted. We use this to insert a break after a ',' or a ';'*/
+static void
+modest_recpt_editor_on_insert_text_after (GtkTextBuffer *buffer,
+                                         GtkTextIter *location,
+                                         gchar *text,
+                                         gint len,
+                                         ModestRecptEditor *editor)
+{
+       GtkTextIter prev;
+       gunichar prev_char;
+       ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
+
+       prev = *location;
+       if (!gtk_text_iter_backward_char (&prev))
+               return;
+
+       prev_char = gtk_text_iter_get_char (&prev);
+       g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
+       g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
+       if ((prev_char == ';'||prev_char == ',')&&(!quote_opened(location))) {
+               GtkTextMark *insert;
+               gtk_text_buffer_insert (buffer, location, "\n",-1);
+               insert = gtk_text_buffer_get_insert (buffer);
+               gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->text_view), location, 0.0,TRUE, 0.0, 1.0);
+       }
+       g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
+       g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
+}
+
+/* Called before the default handler, we use it to validate the inputs */
+static void
 modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
                                    GtkTextIter *location,
                                    gchar *text,
                                    gint len,
                                    ModestRecptEditor *editor)
 {
-       GtkTextIter prev;
-       gunichar prev_char;
-       ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
-       
        if (len > 1024)
                len = 1024;
 
        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");
-               g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, 
-                                                editor);
+               g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
+               g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
                gtk_text_buffer_insert (buffer, location, new_text, -1);
-               g_signal_handlers_unblock_by_func (buffer, 
-                                                  modest_recpt_editor_on_insert_text, 
-                                                  editor);
+               g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
+               g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
                g_free (new_text);
                return;
        }
@@ -627,26 +718,6 @@ modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer,
                gtk_text_buffer_get_end_iter (buffer, location);
                gtk_text_buffer_place_cursor (buffer, location);
        }
-
-       if (gtk_text_iter_is_start (location))
-               return;
-
-       if (gtk_text_iter_is_end (location)) {
-               prev = *location;
-               if (!gtk_text_iter_backward_char (&prev))
-                       return;
-               prev_char = gtk_text_iter_get_char (&prev);
-               g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
-               if ((prev_char == ';'||prev_char == ',')&&(!quote_opened(location))) {
-                       GtkTextMark *insert;
-                       gtk_text_buffer_insert (buffer, location, "\n",-1);
-                       insert = gtk_text_buffer_get_insert (buffer);
-                       gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->text_view), location, 0.0,TRUE, 0.0, 1.0);
-               }
-               g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
-               
-       }
-
 }
 
 static GtkTextTag *
@@ -860,6 +931,7 @@ modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
                insert_offset = gtk_text_iter_get_offset (&location);
                selection_offset = gtk_text_iter_get_offset (&selection_loc);
                g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
+               g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
                if (selection_offset > insert_offset)
                        location = selection_loc;
                tag = iter_has_recipient (&location);
@@ -881,6 +953,7 @@ modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
                        }
                }
                g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, editor);
+               g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text_after, editor);
                return TRUE;
        }
        break;
@@ -915,22 +988,55 @@ modest_recpt_editor_on_key_press_event (GtkTextView *text_view,
        }
 }
 
-static void 
-modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor)
+/* NOTE: before calling this function be sure that both
+   modest_recpt_editor_on_insert_text and
+   modest_recpt_editor_on_insert_text_after won't be triggered during
+   the execution of the procedure. You'll have to block both signal
+   handlers otherwise you'll get an infinite loop and most likely a
+   SIGSEV caused by a stack overflow */
+static void
+modest_recpt_editor_add_tags (ModestRecptEditor *editor,
+                             const gchar * recipient_id)
 {
+
        ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
 #ifdef MODEST_TOOLKIT_HILDON2
        GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
 #else
        GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
 #endif
+       GtkTextTag *tag;
        GtkTextIter start, end;
+       gchar * buffer_contents;
+       GtkTextIter start_match, end_match;
 
-       gtk_text_buffer_get_end_iter (buffer, &start);
-       end = start;
-       gtk_text_buffer_select_range (buffer, &start, &end);
+       /* This would move the cursor to the end of the buffer
+          containing new line character. */
+       gtk_text_buffer_get_bounds (buffer, &start, &end);
+       buffer_contents = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+       gtk_text_buffer_set_text(buffer,buffer_contents,strlen(buffer_contents));
 
+       tag = gtk_text_buffer_create_tag (buffer, NULL,
+                                         "underline", PANGO_UNDERLINE_SINGLE,
+                                         "wrap-mode", GTK_WRAP_NONE,
+                                         "editable", TRUE, NULL);
 
+       g_object_set_data (G_OBJECT (tag), "recipient-tag-id", GINT_TO_POINTER (RECIPIENT_TAG_ID));
+       g_object_set_data_full (G_OBJECT (tag), "recipient-id", g_strdup (recipient_id), (GDestroyNotify) g_free);
+
+       /* Formatting the buffer content by applying tag */
+       gtk_text_buffer_get_bounds (buffer, &start, &end);
+       while (gtk_text_iter_forward_search(&start, ";",
+                                           GTK_TEXT_SEARCH_TEXT_ONLY |
+                                           GTK_TEXT_SEARCH_VISIBLE_ONLY,
+                                           &start_match, &end_match, &end )) {
+               int offset;
+
+               gtk_text_buffer_apply_tag(buffer, tag, &start, &start_match);
+               offset = gtk_text_iter_get_offset (&end_match);
+               gtk_text_buffer_get_iter_at_offset(buffer, &start, offset);
+       }
+       g_free (buffer_contents);
 }
 
 void
@@ -958,12 +1064,44 @@ modest_recpt_editor_has_focus (ModestRecptEditor *recpt_editor)
                gtk_widget_is_focus (priv->text_view);
 }
 
+void 
+modest_recpt_editor_set_show_abook_button (ModestRecptEditor *recpt_editor, gboolean show)
+{
+       ModestRecptEditorPrivate *priv;
+       
+       g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
+       priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
+
+       priv->show_abook = show;
+
+       if (show)
+               gtk_widget_show (priv->abook_button);
+       else
+               gtk_widget_hide (priv->abook_button);
+}
+
+gboolean
+modest_recpt_editor_get_show_abook_button (ModestRecptEditor *recpt_editor, gboolean show)
+{
+       ModestRecptEditorPrivate *priv;
+       
+       g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE);
+       priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
+
+       return priv->show_abook;
+       
+}
+
 static void
 modest_recpt_editor_finalize (GObject *object)
 {
        ModestRecptEditorPrivate *priv;
        priv = MODEST_RECPT_EDITOR_GET_PRIVATE (object);
 
+       if (g_signal_handler_is_connected (object, priv->on_mark_set_handler))
+               g_signal_handler_disconnect (object, priv->on_mark_set_handler);
+       priv->on_mark_set_handler = 0;
+
        if (priv->recipients) {
                g_free (priv->recipients);
                priv->recipients = NULL;
@@ -978,11 +1116,9 @@ static void
 modest_recpt_editor_class_init (ModestRecptEditorClass *klass)
 {
        GObjectClass *object_class;
-       GtkWidgetClass *widget_class;
 
        parent_class = g_type_class_peek_parent (klass);
        object_class = (GObjectClass*) klass;
-       widget_class = GTK_WIDGET_CLASS (klass);
 
        object_class->finalize = modest_recpt_editor_finalize;