Added the function that quotes emails
[modest] / src / hildon2 / modest-msg-edit-window.c
index 01ea821..d320080 100644 (file)
@@ -92,6 +92,9 @@
 #define IMAGE_MAX_WIDTH 560
 #define DEFAULT_FONT_SCALE 1.5
 #define ATTACHMENT_BUTTON_WIDTH 118
+#define MAX_FROM_VALUE 36
+#define MAX_BODY_LENGTH 128*1024
+#define MAX_BODY_LINES 2048
 
 static gboolean is_wp_text_buffer_started = FALSE;
 
@@ -110,6 +113,11 @@ static void  text_buffer_apply_tag (GtkTextBuffer *buffer, GtkTextTag *tag,
                                    GtkTextIter *start, GtkTextIter *end,
                                    gpointer userdata);
 static void  text_buffer_delete_images_by_id (GtkTextBuffer *buffer, const gchar * image_id);
+static void  body_insert_text (GtkTextBuffer *buffer, 
+                                     GtkTextIter *location,
+                                     gchar *text,
+                                     gint len,
+                                     ModestMsgEditWindow *window);
 static void  subject_field_changed (GtkEditable *editable, ModestMsgEditWindow *window);
 static void  subject_field_insert_text (GtkEditable *editable, 
                                        gchar *new_text,
@@ -177,11 +185,19 @@ static void font_size_clicked (GtkToolButton *button,
                               ModestMsgEditWindow *window);
 static void font_face_clicked (GtkToolButton *button,
                               ModestMsgEditWindow *window);
+static void update_signature (ModestMsgEditWindow *self,
+                             const gchar *old_account, 
+                             const gchar *new_account);
+static void update_branding (ModestMsgEditWindow *self,
+                            const gchar *new_account);
+static GtkWidget *_create_addressbook_box (GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group,
+                                          const gchar *label, GtkWidget *control);
+static void max_chars_banner_unref (ModestMsgEditWindow *self, GObject *old_ref);
 static void DEBUG_BUFFER (WPTextBuffer *buffer)
 {
 #ifdef DEBUG
        GtkTextIter iter;
-       g_message ("BEGIN BUFFER OF SIZE %d", gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER (buffer)));
+       g_debug ("BEGIN BUFFER OF SIZE %d", gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER (buffer)));
 
        gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &iter);
        while (!gtk_text_iter_is_end (&iter)) {
@@ -208,11 +224,11 @@ static void DEBUG_BUFFER (WPTextBuffer *buffer)
                        g_string_append (output, " ");
                }
                output = g_string_append (output, "]\n");
-               g_message ("%s", output->str);
+               g_debug ("%s", output->str);
                g_string_free (output, TRUE);
                gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
        }
-       g_message ("END BUFFER");
+       g_debug ("END BUFFER");
 #endif
 }
 
@@ -255,7 +271,7 @@ struct _ModestMsgEditWindowPrivate {
        GtkWidget   *attachments_view;
        GtkWidget   *priority_icon;
        GtkWidget   *subject_box;
-       GtkWidget   *add_attachment_button;
+       GtkWidget   *send_button;
 
        GtkWidget   *cc_caption;
        GtkWidget   *bcc_caption;
@@ -310,6 +326,12 @@ struct _ModestMsgEditWindowPrivate {
        GtkWidget   *app_menu;
        GtkWidget   *cc_button;
        GtkWidget   *bcc_button;
+
+       GtkWidget   *max_chars_banner;
+
+       GtkWidget   *brand_icon;
+       GtkWidget   *brand_label;
+       GtkWidget   *brand_container;
 };
 
 #define MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
@@ -433,6 +455,7 @@ modest_msg_edit_window_init (ModestMsgEditWindow *obj)
 
        priv->references = NULL;
        priv->in_reply_to = NULL;
+       priv->max_chars_banner = NULL;
 
        if (!is_wp_text_buffer_started) {
                is_wp_text_buffer_started = TRUE;
@@ -445,6 +468,45 @@ modest_msg_edit_window_init (ModestMsgEditWindow *obj)
                                   HILDON_WINDOW(obj));
 }
 
+static gchar *
+multimailbox_get_default_mailbox (const gchar *account_name)
+{
+       gchar *transport_account;
+       gchar *result = NULL;
+
+       transport_account = modest_account_mgr_get_server_account_name (modest_runtime_get_account_mgr (),
+                                                                       account_name,
+                                                                       TNY_ACCOUNT_TYPE_TRANSPORT);
+       if (transport_account) {
+               gchar *proto;
+               ModestProtocolRegistry *registry;
+
+               registry = modest_runtime_get_protocol_registry ();
+
+               proto = modest_account_mgr_get_string (modest_runtime_get_account_mgr (), transport_account, 
+                                                      MODEST_ACCOUNT_PROTO, TRUE);
+               if (proto != NULL) {
+                       ModestProtocol *protocol = 
+                               modest_protocol_registry_get_protocol_by_name (registry,
+                                                                              MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
+                                                                              proto);
+                       if (MODEST_ACCOUNT_PROTOCOL (protocol)) {
+                               ModestPairList *pair_list;
+
+                               pair_list = modest_account_protocol_get_from_list (MODEST_ACCOUNT_PROTOCOL (protocol),
+                                                                                  account_name);
+                               if (pair_list) {
+                                       ModestPair *pair = (ModestPair *) pair_list->data;
+                                       result = g_strdup ((const gchar *) pair->first);
+                                       modest_pair_list_free (pair_list);
+                               }
+                       }
+                       
+               }
+       }
+
+       return result;
+}
 
 /** 
  * @result: A ModestPairList, which must be freed with modest_pair_list_free().
@@ -465,30 +527,23 @@ get_transports (void)
 
                        gchar *transport_account;
                        gboolean multi_mailbox = FALSE;
-                       gchar *proto;
-
-                       transport_account = modest_account_mgr_get_server_account_name (modest_runtime_get_account_mgr (),
-                                                                                       account_name,
-                                                                                       TNY_ACCOUNT_TYPE_TRANSPORT);
-                       if (transport_account) {
-                               proto = modest_account_mgr_get_string (modest_runtime_get_account_mgr (), transport_account, 
-                                                                      MODEST_ACCOUNT_PROTO, TRUE);
-                               if (proto != NULL) {
-                                       ModestProtocol *protocol = 
-                                               modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
-                                                                                              MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
-                                                                                              proto);
-                                       if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
-                                               ModestPairList *pair_list;
-                                               pair_list = modest_account_protocol_get_from_list (MODEST_ACCOUNT_PROTOCOL (protocol),
-                                                                                                  account_name);
-                                               if (pair_list) {
-                                                       transports = g_slist_concat (transports, pair_list);
-                                                       multi_mailbox = TRUE;
-                                               }
+                       ModestProtocol *protocol = NULL;
+
+                       if (modest_account_mgr_account_is_multimailbox (account_mgr, account_name, &protocol)) {
+
+                               transport_account = modest_account_mgr_get_server_account_name 
+                                       (modest_runtime_get_account_mgr (),
+                                        account_name,
+                                        TNY_ACCOUNT_TYPE_TRANSPORT);
+                               if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
+                                       ModestPairList *pair_list;
+                                       pair_list = modest_account_protocol_get_from_list (MODEST_ACCOUNT_PROTOCOL (protocol),
+                                                                                          account_name);
+                                       if (pair_list) {
+                                               transports = g_slist_concat (transports, pair_list);
+                                               multi_mailbox = TRUE;
                                        }
                                }
-                               g_free (proto);
                        }
 
                        if (!multi_mailbox) {
@@ -663,6 +718,16 @@ attachment_deleted (ModestAttachmentsView *attachments_view,
 }
 
 static void
+body_size_request (GtkWidget *body,
+                  GtkRequisition *req,
+                  gpointer user_data)
+{
+       /* Make sure the body always get at least 70 pixels */
+       if (req->height < 70)
+               req->height = 70;
+}
+
+static void
 connect_signals (ModestMsgEditWindow *obj)
 {
        ModestMsgEditWindowPrivate *priv;
@@ -677,6 +742,8 @@ connect_signals (ModestMsgEditWindow *obj)
                          G_CALLBACK (text_buffer_can_redo), obj);
        g_signal_connect (G_OBJECT (priv->text_buffer), "changed",
                           G_CALLBACK (body_changed), obj);
+       g_signal_connect (G_OBJECT (priv->text_buffer), "insert-text", 
+                         G_CALLBACK (body_insert_text), obj);
        g_signal_connect (G_OBJECT (priv->text_buffer), "modified-changed",
                           G_CALLBACK (body_changed), obj);
        g_signal_connect (G_OBJECT (priv->text_buffer), "end-user-action",
@@ -692,8 +759,9 @@ connect_signals (ModestMsgEditWindow *obj)
        g_signal_connect_swapped (G_OBJECT (priv->bcc_field), "open-addressbook", 
                                  G_CALLBACK (modest_msg_edit_window_open_addressbook), obj);
 
-       g_signal_connect (G_OBJECT (priv->add_attachment_button), "clicked",
-                         G_CALLBACK (modest_msg_edit_window_add_attachment_clicked), obj);
+       g_signal_connect (G_OBJECT (priv->send_button), "clicked",
+                         G_CALLBACK (modest_ui_actions_on_send), obj);
+
        g_signal_connect (G_OBJECT (priv->from_field), "value-changed",
                          G_CALLBACK (from_field_changed), obj);
 
@@ -701,6 +769,8 @@ connect_signals (ModestMsgEditWindow *obj)
                          G_CALLBACK (msg_body_focus), obj);
        g_signal_connect (G_OBJECT (priv->msg_body), "focus-out-event",
                          G_CALLBACK (msg_body_focus), obj);
+       g_signal_connect (G_OBJECT (priv->msg_body), "size-request",
+                         G_CALLBACK (body_size_request), obj);
        g_signal_connect (G_OBJECT (obj), "set-focus", G_CALLBACK (window_focus), obj);
        g_signal_connect (G_OBJECT (modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (priv->to_field))),
                          "changed", G_CALLBACK (recpt_field_changed), obj);
@@ -743,7 +813,6 @@ init_window (ModestMsgEditWindow *obj)
 {
        GtkWidget *to_caption, *subject_caption;
        GtkWidget *main_vbox;
-       GtkWidget *add_attachment_align;
        ModestMsgEditWindowPrivate *priv;
        GtkActionGroup *action_group;
        ModestWindowPrivate *parent_priv;
@@ -752,12 +821,15 @@ init_window (ModestMsgEditWindow *obj)
 
        GtkSizeGroup *title_size_group;
        GtkSizeGroup *value_size_group;
-       GtkWidget *attachment_icon;
        GtkWidget *window_box;
        GtkWidget *window_align;
 #if (GTK_MINOR_VERSION >= 10)
        GdkAtom deserialize_type;
 #endif
+       GtkWidget *from_send_hbox;
+       GtkWidget *send_icon;
+       GtkWidget *attachments_label;
+       GtkWidget *branding_box;
 
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(obj);
        parent_priv = MODEST_WINDOW_GET_PRIVATE (obj);
@@ -823,12 +895,19 @@ init_window (ModestMsgEditWindow *obj)
        priv->from_field    = modest_selector_picker_new (MODEST_EDITABLE_SIZE,
                                                          HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
                                                          NULL, g_str_equal);
-       modest_maemo_utils_set_hbutton_layout (title_size_group, value_size_group, 
+       modest_selector_picker_set_value_max_chars (MODEST_SELECTOR_PICKER (priv->from_field), MAX_FROM_VALUE);
+       modest_maemo_utils_set_hbutton_layout (title_size_group, NULL, 
                                               _("mail_va_from"), priv->from_field);
+       hildon_button_set_alignment (HILDON_BUTTON (priv->from_field), 0.0, 0.5, 1.0, 1.0);
+       hildon_button_set_title_alignment (HILDON_BUTTON (priv->from_field), 0.0, 0.5);
+       hildon_button_set_value_alignment (HILDON_BUTTON (priv->from_field), 1.0, 0.5);
 
        priv->to_field      = modest_recpt_editor_new ();
        priv->cc_field      = modest_recpt_editor_new ();
        priv->bcc_field     = modest_recpt_editor_new ();
+       modest_recpt_editor_set_show_abook_button (MODEST_RECPT_EDITOR (priv->to_field), FALSE);
+       modest_recpt_editor_set_show_abook_button (MODEST_RECPT_EDITOR (priv->cc_field), FALSE);
+       modest_recpt_editor_set_show_abook_button (MODEST_RECPT_EDITOR (priv->bcc_field), FALSE);
        priv->subject_box = gtk_hbox_new (FALSE, MODEST_MARGIN_NONE);
        priv->priority_icon = gtk_image_new ();
        gtk_box_pack_start (GTK_BOX (priv->subject_box), priv->priority_icon, FALSE, FALSE, 0);
@@ -838,54 +917,71 @@ init_window (ModestMsgEditWindow *obj)
        hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->subject_field), 
                                         HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
        gtk_box_pack_start (GTK_BOX (priv->subject_box), priv->subject_field, TRUE, TRUE, 0);
-       priv->add_attachment_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
-       gtk_widget_set_size_request (priv->add_attachment_button, ATTACHMENT_BUTTON_WIDTH, -1);
-       GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (priv->add_attachment_button), GTK_CAN_FOCUS);
-       gtk_button_set_focus_on_click (GTK_BUTTON (priv->add_attachment_button), FALSE);
-       attachment_icon = gtk_image_new_from_icon_name (MODEST_HEADER_ICON_ATTACH, HILDON_ICON_SIZE_FINGER);
-       hildon_button_set_image (HILDON_BUTTON (priv->add_attachment_button), attachment_icon);
-       add_attachment_align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
-       gtk_alignment_set_padding (GTK_ALIGNMENT (add_attachment_align), 0, 0, MODEST_MARGIN_DEFAULT, 0);
-       gtk_container_add (GTK_CONTAINER (add_attachment_align), priv->add_attachment_button);
-       gtk_box_pack_start (GTK_BOX (priv->subject_box), add_attachment_align, FALSE, FALSE, 0);
        priv->attachments_view = modest_attachments_view_new (NULL);
        modest_attachments_view_set_style (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
                                           MODEST_ATTACHMENTS_VIEW_STYLE_NO_FOCUS);
        
        priv->header_box = gtk_vbox_new (FALSE, 0);
        
-       to_caption = modest_maemo_utils_create_captioned_with_size_type 
+       to_caption = _create_addressbook_box
                (title_size_group, value_size_group,
-                _("mail_va_to"), FALSE, priv->to_field,
-                HILDON_SIZE_AUTO_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
-       priv->cc_caption = modest_maemo_utils_create_captioned_with_size_type 
+                _("mail_va_to"), priv->to_field);
+       priv->cc_caption = _create_addressbook_box
                (title_size_group, value_size_group,
-                _("mail_va_cc"), FALSE, priv->cc_field,
-                HILDON_SIZE_AUTO_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
-       priv->bcc_caption = modest_maemo_utils_create_captioned_with_size_type
+                _("mail_va_cc"), priv->cc_field);
+       priv->bcc_caption = _create_addressbook_box
                (title_size_group, value_size_group,
-                _("mail_va_hotfix1"), FALSE, priv->bcc_field,
-                HILDON_SIZE_AUTO_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
+                _("mail_va_hotfix1"), priv->bcc_field);
        subject_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
                                                               _("mail_va_subject"), FALSE, priv->subject_box);
-       priv->attachments_caption = modest_maemo_utils_create_captioned_with_size_type (title_size_group, value_size_group,
+       priv->attachments_caption = modest_maemo_utils_create_captioned_with_size_type (NULL, NULL,
                                                                                        _("mail_va_attachment"), 
                                                                                        FALSE,
                                                                                        priv->attachments_view,
                                                                                        HILDON_SIZE_AUTO_WIDTH |
                                                                                        HILDON_SIZE_AUTO_HEIGHT);
-       /* modest_recpt_editor_set_field_size_group (MODEST_RECPT_EDITOR (priv->to_field), value_size_group); */
-       /* modest_recpt_editor_set_field_size_group (MODEST_RECPT_EDITOR (priv->cc_field), value_size_group); */
-       /* modest_recpt_editor_set_field_size_group (MODEST_RECPT_EDITOR (priv->bcc_field), value_size_group); */
+       attachments_label = modest_maemo_utils_captioned_get_label_widget (priv->attachments_caption);
+       hildon_gtk_widget_set_theme_size (attachments_label, HILDON_SIZE_AUTO_HEIGHT);
+
+
+       priv->send_button = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT);
+       send_icon = gtk_image_new_from_icon_name (MODEST_TOOLBAR_ICON_MAIL_SEND, HILDON_ICON_SIZE_FINGER);
+       gtk_container_add (GTK_CONTAINER (priv->send_button), send_icon);
+       gtk_widget_set_size_request (GTK_WIDGET (priv->send_button), 148, -1);
+
        g_object_unref (title_size_group);
        g_object_unref (value_size_group);
 
-       gtk_box_pack_start (GTK_BOX (priv->header_box), priv->from_field, FALSE, FALSE, 0);
+       priv->brand_icon = gtk_image_new ();
+       gtk_misc_set_alignment (GTK_MISC (priv->brand_icon), 0.5, 0.5);
+       priv->brand_label = gtk_label_new (NULL);
+       hildon_helper_set_logical_font (priv->brand_label, "SmallSystemFont");
+       gtk_misc_set_alignment (GTK_MISC (priv->brand_label), 0.0, 0.5);
+       gtk_widget_set_no_show_all (priv->brand_icon, TRUE);
+       gtk_widget_set_no_show_all (priv->brand_label, TRUE);
+
+       from_send_hbox = gtk_hbox_new (FALSE, 0);
+       gtk_box_pack_start (GTK_BOX (from_send_hbox), priv->from_field, TRUE, TRUE, 0);
+       gtk_box_pack_start (GTK_BOX (from_send_hbox), priv->send_button, FALSE, FALSE, 0);
+
+       branding_box = gtk_hbox_new (FALSE, MODEST_MARGIN_DEFAULT);
+       gtk_widget_show (branding_box);
+       gtk_box_pack_start (GTK_BOX (branding_box), priv->brand_label, FALSE, FALSE, 0);
+       gtk_box_pack_start (GTK_BOX (branding_box), priv->brand_icon, FALSE, FALSE, 0);
+
+       priv->brand_container = gtk_alignment_new (0.0, 0.5, 0.0, 1.0);
+       gtk_alignment_set_padding (GTK_ALIGNMENT (priv->brand_container), 0, 0, MODEST_MARGIN_DOUBLE, 0);
+       gtk_container_add (GTK_CONTAINER (priv->brand_container), branding_box);
+       gtk_widget_set_no_show_all (priv->brand_container, TRUE);
+
+
+       gtk_box_pack_start (GTK_BOX (priv->header_box), from_send_hbox, FALSE, FALSE, 0);
        gtk_box_pack_start (GTK_BOX (priv->header_box), to_caption, FALSE, FALSE, 0);
        gtk_box_pack_start (GTK_BOX (priv->header_box), priv->cc_caption, FALSE, FALSE, 0);
        gtk_box_pack_start (GTK_BOX (priv->header_box), priv->bcc_caption, FALSE, FALSE, 0);
        gtk_box_pack_start (GTK_BOX (priv->header_box), subject_caption, FALSE, FALSE, 0);
        gtk_box_pack_start (GTK_BOX (priv->header_box), priv->attachments_caption, FALSE, FALSE, 0);
+       gtk_box_pack_start (GTK_BOX (priv->header_box), priv->brand_container, FALSE, FALSE, 0);
        gtk_widget_set_no_show_all (priv->attachments_caption, TRUE);
 
        init_wp_text_view_style ();
@@ -912,6 +1008,8 @@ init_window (ModestMsgEditWindow *obj)
 /*     g_signal_connect (G_OBJECT (obj), "key_pressed", G_CALLBACK (on_key_pressed), NULL) */
 
        priv->pannable = hildon_pannable_area_new ();
+
+       g_object_set (G_OBJECT (priv->pannable), "hscrollbar-policy", GTK_POLICY_NEVER, NULL);
        
        main_vbox = gtk_vbox_new  (FALSE, DEFAULT_MAIN_VBOX_SPACING);
        window_align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
@@ -964,7 +1062,12 @@ static void
 modest_msg_edit_window_finalize (GObject *obj)
 {
        ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (obj);
-       
+
+       if (priv->max_chars_banner) {
+               g_object_weak_unref (G_OBJECT (priv->max_chars_banner), (GWeakNotify) max_chars_banner_unref, obj);
+               priv->max_chars_banner = FALSE;
+       }
+
        /* Sanity check: shouldn't be needed, the window mgr should
           call this function before */
        modest_msg_edit_window_disconnect_signals (MODEST_WINDOW (obj));
@@ -1016,8 +1119,6 @@ modest_msg_edit_window_finalize (GObject *obj)
        g_free (priv->last_search);
        g_free (priv->references);
        g_free (priv->in_reply_to);
-       g_object_unref (priv->faces_model);
-       g_object_unref (priv->sizes_model);
        g_object_unref (priv->attachments);
        g_object_unref (priv->images);
 
@@ -1160,9 +1261,9 @@ get_related_images (ModestMsgEditWindow *self, TnyMsg *msg)
 
        content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
 
-       if (content_type && !g_strcasecmp (content_type, "multipart/related")) {
+       if (content_type && !g_ascii_strcasecmp (content_type, "multipart/related")) {
                parent = g_object_ref (msg);
-       } else if (content_type && !g_strcasecmp (content_type, "multipart/mixed")) {
+       } else if (content_type && !g_ascii_strcasecmp (content_type, "multipart/mixed")) {
                TnyList *parts = TNY_LIST (tny_simple_list_new ());
                TnyIterator *iter;
 
@@ -1172,7 +1273,7 @@ get_related_images (ModestMsgEditWindow *self, TnyMsg *msg)
                        TnyMimePart *part;
                        part = TNY_MIME_PART (tny_iterator_get_current (iter));
                        content_type = tny_mime_part_get_content_type (part);
-                       if (content_type && !g_strcasecmp (content_type, "multipart/related")) {
+                       if (content_type && !g_ascii_strcasecmp (content_type, "multipart/related")) {
                                parent = part;
                                break;
                        } else {
@@ -1258,12 +1359,17 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
        modest_tny_msg_get_references (TNY_MSG (msg), NULL, &(priv->references), &(priv->in_reply_to));
        priority_flags = tny_header_get_priority (header);
 
-       if (to)
-               modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->to_field),  to);
+       if (to) {
+               gchar *quoted_names = modest_text_utils_quote_names (to);
+               modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->to_field), quoted_names);
+               g_free (quoted_names);
+       }
 
        field_view_set = TRUE;
        if (cc) {
-               modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->cc_field),  cc);
+               gchar *quoted_names = modest_text_utils_quote_names (cc);
+               modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->cc_field),  quoted_names);
+               g_free (quoted_names);
                gtk_widget_set_no_show_all (priv->cc_caption, FALSE);
                gtk_widget_show (priv->cc_caption);
        } else if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_CC, NULL)) {
@@ -1275,7 +1381,9 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
 
        field_view_set = TRUE;
        if (bcc) {
-               modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->bcc_field), bcc);
+               gchar *quoted_names = modest_text_utils_quote_names (bcc);
+               modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->bcc_field), quoted_names);
+               g_free (quoted_names);
                gtk_widget_set_no_show_all (priv->bcc_caption, FALSE);
                gtk_widget_show (priv->bcc_caption);
        } else if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_BCC, NULL)) {
@@ -1498,9 +1606,6 @@ modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window)
        priv->font_face_toolitem = tool_item;
 
        /* Set expand and homogeneous for remaining items */
-       tool_item = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarSend");
-       gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
-       gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
        tool_item = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ActionsBold");
        gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
        gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
@@ -1543,13 +1648,27 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, const gchar
 
        /* Menubar. Update the state of some toggles */
        priv->from_field_protos = get_transports ();
+       priv->original_mailbox = NULL;
        modest_selector_picker_set_pair_list (MODEST_SELECTOR_PICKER (priv->from_field), priv->from_field_protos);
+       modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->from_field), (gpointer) account_name);
+       priv->last_from_account = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
        if (mailbox && modest_pair_list_find_by_first_as_string (priv->from_field_protos, mailbox)) {
                modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->from_field), (gpointer) mailbox);
+               priv->original_mailbox = g_strdup (mailbox);
+       } else if (modest_account_mgr_account_is_multimailbox (modest_runtime_get_account_mgr (), account_name, NULL)) {
+               /* We set the first mailbox as the active mailbox */
+               priv->original_mailbox = multimailbox_get_default_mailbox (account_name);
+               if (priv->original_mailbox != NULL)
+                       modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->from_field),
+                                                             (gpointer) priv->original_mailbox);
+               else
+                       modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->from_field),
+                                                             (gpointer) account_name);
        } else {
                modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->from_field), (gpointer) account_name);
        }
        priv->last_from_account = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
+       update_branding (MODEST_MSG_EDIT_WINDOW (obj), priv->last_from_account);
        hildon_button_set_title (HILDON_BUTTON (priv->from_field),
                                 _("mail_va_from"));
        hildon_button_set_value (HILDON_BUTTON (priv->from_field), 
@@ -1564,10 +1683,9 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, const gchar
        restore_settings (MODEST_MSG_EDIT_WINDOW(obj));
                
        modest_window_set_active_account (MODEST_WINDOW(obj), account_name);
-       modest_window_set_active_mailbox (MODEST_WINDOW(obj), mailbox);
+       modest_window_set_active_mailbox (MODEST_WINDOW(obj), priv->original_mailbox);
 
        priv->original_account_name = (account_name) ? g_strdup (account_name) : NULL;
-       priv->original_mailbox = (mailbox) ? g_strdup (mailbox) : NULL;
 
        toolbar_rules_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_TOOLBAR, TRUE);
        clipboard_rules_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_CLIPBOARD, FALSE);
@@ -1585,6 +1703,9 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, const gchar
        modest_dimming_rules_group_add_widget_rule (toolbar_rules_group, priv->font_face_toolitem,
                                                    G_CALLBACK (modest_ui_dimming_rules_on_set_style),
                                                    MODEST_WINDOW (obj));
+       modest_dimming_rules_group_add_widget_rule (toolbar_rules_group, priv->send_button,
+                                                   G_CALLBACK (modest_ui_dimming_rules_on_send),
+                                                   MODEST_WINDOW (obj));
        /* Insert dimming rules group for this window */
        modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, toolbar_rules_group);
        modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, clipboard_rules_group);
@@ -1660,14 +1781,15 @@ modest_msg_edit_window_get_msg_data (ModestMsgEditWindow *edit_window)
        const gchar *account_name;
        ModestMsgEditWindowPrivate *priv;
        TnyIterator *att_iter;
+       const gchar *picker_active_id;
        
        g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (edit_window), NULL);
 
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (edit_window);
-                                                                       
-       account_name = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
-       g_return_val_if_fail (account_name, NULL);
        
+       picker_active_id = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
+       g_return_val_if_fail (picker_active_id, NULL);
+       account_name = modest_utils_get_account_name_from_recipient (picker_active_id, NULL);
        
        /* don't free these (except from) */
        data = g_slice_new0 (MsgData);
@@ -1832,16 +1954,20 @@ modest_msg_edit_window_set_format (ModestMsgEditWindow *self,
                                   ModestMsgEditFormat format)
 {
        ModestMsgEditWindowPrivate *priv;
+       ModestWindowPrivate *parent_priv;
 
        g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
+       parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
 
        switch (format) {
        case MODEST_MSG_EDIT_FORMAT_HTML:
                wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
+               if (parent_priv->toolbar) gtk_widget_show (parent_priv->toolbar);
                break;
        case MODEST_MSG_EDIT_FORMAT_TEXT:
                wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
+               if (parent_priv->toolbar) gtk_widget_hide (parent_priv->toolbar);
                break;
        default:
                g_return_if_reached ();
@@ -2398,7 +2524,7 @@ on_attach_file_response (GtkDialog *dialog,
                        modest_msg_edit_window_attach_file_one (window, uri, allowed_size);
 
                if (total_size > allowed_size) {
-                       g_warning ("%s: total size: %u", 
+                       g_debug ("%s: total size: %u", 
                                   __FUNCTION__, (unsigned int)total_size);
                        break;
                }
@@ -2427,14 +2553,19 @@ modest_msg_edit_window_offer_attach_file (ModestMsgEditWindow *window)
        dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), 
                                                 GTK_FILE_CHOOSER_ACTION_OPEN);
        gtk_window_set_title (GTK_WINDOW (dialog), _("mcen_ti_select_attachment_title"));
-       conf_folder = modest_conf_get_string (modest_runtime_get_conf (), MODEST_CONF_LATEST_ATTACH_FILE_PATH, NULL);
+       conf_folder = modest_conf_get_string (modest_runtime_get_conf (),
+                                             MODEST_CONF_LATEST_ATTACH_FILE_PATH, NULL);
        if (conf_folder && conf_folder[0] != '\0') {
                gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dialog), conf_folder);
        } else {
                gchar *docs_folder;
-               /* Set the default folder to images folder */
-               docs_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
-                                               ".documents", NULL);
+               /* Set the default folder to documents folder */
+               docs_folder = (gchar *) g_strdup(g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS));
+               if (!docs_folder) {
+                       /* fallback */
+                       docs_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
+                                                       ".documents", NULL);
+               }
                gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), docs_folder);
                g_free (docs_folder);
        }
@@ -2502,13 +2633,13 @@ modest_msg_edit_window_attach_file_one (ModestMsgEditWindow *window,
                    info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE) {
                        size = info->size;
                        if (size > allowed_size) {
-                               modest_platform_information_banner (NULL, NULL, 
-                                                                   _FM("sfil_ib_opening_not_allowed"));
+                               modest_platform_information_banner (NULL, NULL,
+                                                                   _("mail_ib_error_attachment_size"));
                                g_free (filename);
                                return 0;
                        }
                } else
-                       g_warning ("%s: could not get attachment size", __FUNCTION__);
+                       g_debug ("%s: could not get attachment size", __FUNCTION__);
                
                stream = create_stream_for_uri (uri);
                
@@ -2565,7 +2696,6 @@ modest_msg_edit_window_remove_attachments (ModestMsgEditWindow *window,
                        g_object_unref (att_list);
                        return;
                }
-               
        } else {
                g_object_ref (att_list);
        }
@@ -2636,7 +2766,7 @@ modest_msg_edit_window_remove_attachments (ModestMsgEditWindow *window,
        g_object_unref (att_list);
 
        /* if the last attachment has been removed, focus the Subject: field */
-       if (!modest_attachments_view_has_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view))) 
+       if (!modest_attachments_view_has_attachments (MODEST_ATTACHMENTS_VIEW (priv->attachments_view)))
                gtk_widget_grab_focus (priv->subject_field);
 }
 
@@ -2727,6 +2857,7 @@ font_face_clicked (GtkToolButton *button,
 
        selector = hildon_touch_selector_new ();
        renderer = gtk_cell_renderer_text_new ();
+       g_object_set (G_OBJECT (renderer), "alignment", PANGO_ALIGN_CENTER, "xalign", 0.5, NULL);
        hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), priv->faces_model, 
                                             renderer, "family", 0, "text", 0, NULL);
        hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, priv->current_face_index);
@@ -2826,7 +2957,7 @@ modest_msg_edit_window_open_addressbook (ModestMsgEditWindow *window,
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
 
        if (editor == NULL) {
-               GtkWidget *view_focus;
+               GtkWidget *view_focus, *parent;
                view_focus = gtk_window_get_focus (GTK_WINDOW (window));
 
                /* This code should be kept in sync with ModestRecptEditor. The
@@ -2835,24 +2966,15 @@ modest_msg_edit_window_open_addressbook (ModestMsgEditWindow *window,
                   hbox recpt editor inherits from, we'll need to go up in the 
                   hierarchy to know if the text view is part of the recpt editor
                   or if it's a different text entry */
-
-               if (gtk_widget_get_parent (view_focus)) {
-                       GtkWidget *first_parent;
-
-                       first_parent = gtk_widget_get_parent (view_focus);
-                       if (gtk_widget_get_parent (first_parent) && 
-                           MODEST_IS_RECPT_EDITOR (gtk_widget_get_parent (first_parent))) {
-                               editor = MODEST_RECPT_EDITOR (gtk_widget_get_parent (first_parent));
-                       }
-               }
+               parent = gtk_widget_get_parent (view_focus);
+               if (parent && MODEST_IS_RECPT_EDITOR (parent))
+                       editor = MODEST_RECPT_EDITOR (parent);
 
                if (editor == NULL)
                        editor = MODEST_RECPT_EDITOR (priv->to_field);
-
        }
 
-       modest_address_book_select_addresses (editor);
-
+       modest_address_book_select_addresses (editor, GTK_WINDOW (window));
 }
 
 void
@@ -2868,17 +2990,27 @@ modest_msg_edit_window_show_toolbar (ModestWindow *self,
                                     gboolean show_toolbar)
 {
        ModestWindowPrivate *parent_priv;
+       ModestMsgEditWindowPrivate *priv;
 
        g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
        parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
 
        /* We can not just use the code of
           modest_msg_edit_window_setup_toolbar because it has a
           mixture of both initialization and creation code. */
-       if (show_toolbar)
-               gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
-       else
+       if (show_toolbar) {
+               gint current_format;
+               current_format = wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer))
+                       ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
+               if (current_format == MODEST_FILE_FORMAT_PLAIN_TEXT) {
+                       gtk_widget_hide (GTK_WIDGET (parent_priv->toolbar));
+               } else {
+                       gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
+               }
+       } else {
                gtk_widget_hide (GTK_WIDGET (parent_priv->toolbar));
+       }
 }
 
 void
@@ -2943,11 +3075,15 @@ modest_msg_edit_window_set_file_format (ModestMsgEditWindow *window,
        current_format = wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer))
                ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
 
+       gtk_widget_set_no_show_all (GTK_WIDGET (parent_priv->toolbar), TRUE);
+
        if (current_format != file_format) {
                switch (file_format) {
                case MODEST_FILE_FORMAT_FORMATTED_TEXT:
                        wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
                        remove_tags (WP_TEXT_BUFFER (priv->text_buffer));
+                       if (parent_priv->toolbar)
+                               gtk_widget_show (parent_priv->toolbar);
                        break;
                case MODEST_FILE_FORMAT_PLAIN_TEXT:
                {
@@ -2958,6 +3094,8 @@ modest_msg_edit_window_set_file_format (ModestMsgEditWindow *window,
                        gtk_widget_destroy (dialog);
                        if (response == GTK_RESPONSE_OK) {
                                wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
+                               if (parent_priv->toolbar)
+                                       gtk_widget_hide (parent_priv->toolbar);
                        } else {
                                GtkToggleAction *action = GTK_TOGGLE_ACTION (gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/FormatMenu/FileFormatFormattedTextMenu"));
                                modest_utils_toggle_action_set_active_block_notify (action, TRUE);
@@ -3287,10 +3425,10 @@ modest_msg_edit_window_is_modified (ModestMsgEditWindow *editor)
                return TRUE;
 
        account_name = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
-       if ((priv->original_mailbox) && 
-           (!account_name || strcmp (account_name, priv->original_mailbox)))
-               return TRUE;
-       if (!priv->original_account_name || strcmp(account_name, priv->original_account_name)) {
+       if (priv->original_mailbox) {
+               if (!account_name || strcmp (account_name, priv->original_mailbox))
+                       return TRUE;
+       } else if (!priv->original_account_name || strcmp(account_name, priv->original_account_name)) {
                return TRUE;
        }
 
@@ -3304,7 +3442,8 @@ gboolean
 modest_msg_edit_window_check_names (ModestMsgEditWindow *window, gboolean add_to_addressbook)
 {
        ModestMsgEditWindowPrivate *priv = NULL;
-       
+       GSList *address_list = NULL;
+
        g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window), FALSE);
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
 
@@ -3317,23 +3456,28 @@ modest_msg_edit_window_check_names (ModestMsgEditWindow *window, gboolean add_to
                return FALSE;
        }
 
+       /* Check names */
        g_object_ref (window);
-       if (!modest_address_book_check_names (MODEST_RECPT_EDITOR (priv->to_field),  add_to_addressbook)) {
+       if (!modest_address_book_check_names (MODEST_RECPT_EDITOR (priv->to_field),  &address_list)) {
                modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (priv->to_field));
                g_object_unref (window);
                return FALSE;
        }
-       if (!modest_address_book_check_names (MODEST_RECPT_EDITOR (priv->cc_field),  add_to_addressbook)) {
+       if (!modest_address_book_check_names (MODEST_RECPT_EDITOR (priv->cc_field),  &address_list)) {
                modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (priv->cc_field));
                g_object_unref (window);
                return FALSE;
        }
-       if (!modest_address_book_check_names (MODEST_RECPT_EDITOR (priv->bcc_field), add_to_addressbook)) {
+       if (!modest_address_book_check_names (MODEST_RECPT_EDITOR (priv->bcc_field), &address_list)) {
                modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (priv->bcc_field));
                g_object_unref (window);
                return FALSE;
        }
 
+       /* Add contacts to address book */
+       if (address_list)
+               modest_address_book_add_address_list (address_list);
+
        if (!modest_recpt_editor_has_focus (MODEST_RECPT_EDITOR (priv->cc_field)) &&
            !modest_recpt_editor_has_focus (MODEST_RECPT_EDITOR (priv->bcc_field)))
                modest_recpt_editor_grab_focus (MODEST_RECPT_EDITOR (priv->to_field));
@@ -3441,6 +3585,70 @@ update_window_title (ModestMsgEditWindow *window)
 
 }
 
+
+static void  
+body_insert_text (GtkTextBuffer *buffer, 
+                 GtkTextIter *location,
+                 gchar *text,
+                 gint len,
+                 ModestMsgEditWindow *window)
+{
+       GtkTextIter end_iter;
+       gint offset;
+       glong utf8_len;
+       gint line;
+       gchar *text_offset;
+       gint text_lines;
+
+       ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
+
+       gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (buffer), &end_iter);
+
+       offset = gtk_text_iter_get_offset (&end_iter);
+       line = gtk_text_iter_get_line (&end_iter);
+
+       text_offset = text;
+       text_lines = 0;
+       while (text_offset < text + len) {
+               if (*text_offset == '\n')
+                       text_lines++;
+               if (text_lines + line >= MAX_BODY_LINES) {
+                       len = text_offset - text;
+                       break;
+               }
+               text_offset++;
+       }
+
+       utf8_len = g_utf8_strlen (text, len);
+
+       if (line > MAX_BODY_LINES || offset + utf8_len > MAX_BODY_LENGTH) {
+               g_signal_stop_emission_by_name (G_OBJECT (buffer), "insert-text");
+               if (line <= MAX_BODY_LINES && offset < MAX_BODY_LENGTH)
+               {
+                       gchar *result;
+                       gchar *utf8_end;
+
+                       utf8_end = g_utf8_offset_to_pointer (text, MAX_BODY_LENGTH - offset);
+
+                       /* Prevent endless recursion */
+                       result = g_strndup (text, utf8_end - text);
+                       g_signal_handlers_block_by_func (G_OBJECT (buffer), G_CALLBACK (body_insert_text), window);
+                       g_signal_emit_by_name (G_OBJECT (buffer), "insert-text", location,
+                                              (gpointer) result, (gpointer) (utf8_end - text),
+                                              (gpointer) window);
+                       g_signal_handlers_unblock_by_func (G_OBJECT (buffer), G_CALLBACK (body_insert_text), window);
+               }
+
+       }
+       if (line > MAX_BODY_LINES || offset + utf8_len > MAX_BODY_LENGTH) {
+               if (priv->max_chars_banner == NULL) {
+                       priv->max_chars_banner = hildon_banner_show_information (GTK_WIDGET (window), NULL, 
+                                                                                _CS("ckdg_ib_maximum_characters_reached"));
+                       g_object_weak_ref (G_OBJECT (priv->max_chars_banner), (GWeakNotify) max_chars_banner_unref, window);
+               }
+       }
+}
+
 static void  
 subject_field_changed (GtkEditable *editable, 
                       ModestMsgEditWindow *window)
@@ -3451,7 +3659,6 @@ subject_field_changed (GtkEditable *editable,
        modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window));
        modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
 }
-
 static void  
 subject_field_insert_text (GtkEditable *editable, 
                           gchar *new_text,
@@ -3563,7 +3770,7 @@ gtk_text_iter_forward_search_insensitive (const GtkTextIter *iter,
                        result = TRUE;
                        if (!gtk_text_iter_forward_search (iter, found_text, GTK_TEXT_SEARCH_VISIBLE_ONLY|GTK_TEXT_SEARCH_TEXT_ONLY,
                                                           match_start, match_end, NULL)) {
-                               g_warning ("Matched string with collate, but not matched in model");
+                               g_debug ("Matched string with collate, but not matched in model");
                        }
                        g_free (found_text);
                }
@@ -3801,43 +4008,120 @@ on_account_removed (TnyAccountStore *account_store,
 }
 
 static void
-from_field_changed (HildonPickerButton *button,
-                   ModestMsgEditWindow *self)
+update_signature (ModestMsgEditWindow *self,
+                 const gchar *old_account,
+                 const gchar *new_account)
 {
        ModestMsgEditWindowPrivate *priv;
        gboolean has_old_signature, has_new_signature;
        GtkTextIter iter;
-       GtkTextIter match_start, match_end;
        ModestAccountMgr *mgr;
        gchar *signature;
-       gchar *full_signature;
 
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
 
+       gtk_text_buffer_begin_user_action (priv->text_buffer);
+
        gtk_text_buffer_get_start_iter (priv->text_buffer, &iter);
        mgr = modest_runtime_get_account_mgr ();
-       signature = modest_account_mgr_get_signature (mgr, priv->last_from_account, &has_old_signature);
-       if (has_old_signature) {
-               full_signature = g_strconcat ("\n--\n", signature, NULL);
-               if (gtk_text_iter_forward_search (&iter, full_signature, 0, &match_start, &match_end, NULL)) {
-                       gtk_text_buffer_delete (priv->text_buffer, &match_start, &match_end);
-                       iter = match_start;
-               } else if (gtk_text_iter_forward_search (&iter, _("mcen_ia_editor_original_message"), 0,
-                                                        &match_start, &match_end, NULL)) {
-                       iter = match_start;
+
+
+       if (old_account) {
+               signature = modest_account_mgr_get_signature_from_recipient (mgr, old_account, &has_old_signature);
+               if (has_old_signature) {
+                       GtkTextIter match_start, match_end;
+                       /* We cannot use
+                          MODEST_TEXT_UTILS_SIGNATURE_MARKER as it
+                          seems that the search has some problems
+                          with the blank space at the end */
+                       if (gtk_text_iter_forward_search (&iter, "--",
+                                                         GTK_TEXT_SEARCH_TEXT_ONLY,
+                                                         &match_start, NULL, NULL)) {
+                               gtk_text_buffer_get_end_iter (priv->text_buffer ,&match_end);
+                               gtk_text_buffer_delete (priv->text_buffer, &match_start, &match_end);
+                               iter = match_start;
+                       } else if (gtk_text_iter_forward_search (&iter, _("mcen_ia_editor_original_message"), 0,
+                                                                &match_start, &match_end, NULL)) {
+                               iter = match_start;
+                       }
                }
-               g_free (full_signature);
+               g_free (signature);
        }
-       g_free (signature);
 
        priv->last_from_account = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
-       signature = modest_account_mgr_get_signature (mgr, priv->last_from_account, &has_new_signature);
+       signature = modest_account_mgr_get_signature_from_recipient (mgr, new_account, &has_new_signature);
        if (has_new_signature) {
-               full_signature = g_strconcat ("\n--\n", signature, NULL);
+               gchar *full_signature = g_strconcat (MODEST_TEXT_UTILS_SIGNATURE_MARKER, "\n",
+                                                    signature, NULL);
                gtk_text_buffer_insert (priv->text_buffer, &iter, full_signature, -1);
                g_free (full_signature);
        }
        g_free (signature);
+       gtk_text_buffer_end_user_action (priv->text_buffer);
+}
+
+static void update_branding (ModestMsgEditWindow *self,
+                            const gchar *new_account)
+{
+       ModestMsgEditWindowPrivate *priv;
+       ModestAccountMgr *mgr;
+       const GdkPixbuf *new_icon = NULL;
+       gchar *new_label = NULL;
+       gboolean show = FALSE;
+
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
+
+       mgr = modest_runtime_get_account_mgr ();
+
+       modest_account_mgr_get_branding_from_recipient (mgr, new_account, &new_label, &new_icon, MODEST_ICON_SIZE_SMALL);
+       if (new_icon) {
+               gtk_image_set_from_pixbuf (GTK_IMAGE (priv->brand_icon), (GdkPixbuf *) new_icon);
+               gtk_widget_show (priv->brand_icon);
+               show = TRUE;
+       } else {
+               gtk_widget_hide (priv->brand_icon);
+       }
+       if (new_label) {
+               gtk_label_set_text (GTK_LABEL (priv->brand_label), new_label);
+               gtk_widget_show (priv->brand_label);
+               g_free (new_label);
+               show = TRUE;
+       } else {
+               gtk_widget_hide (priv->brand_label);
+       }
+
+       if (show)
+               gtk_widget_show (priv->brand_container);
+       else
+               gtk_widget_hide (priv->brand_container);
+}
+
+static void
+from_field_changed (HildonPickerButton *button,
+                   ModestMsgEditWindow *self)
+{
+       ModestMsgEditWindowPrivate *priv;
+       gchar *old_account, *new_account;
+
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
+
+       old_account = priv->last_from_account;
+       new_account = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
+
+       if (!new_account) {
+               g_warning ("%s, could not get the new account", __FUNCTION__);
+               return;
+       }
+
+       /* If the From is the same do nothing */
+       if (old_account && new_account && !strcmp (old_account, new_account))
+               return;
+
+       priv->last_from_account = new_account;
+
+       update_signature (self, old_account, new_account);
+       update_branding (self, new_account);
+
 }
 
 typedef struct _MessageSettingsHelper {
@@ -4032,6 +4316,9 @@ modest_msg_edit_window_show_msg_settings_dialog (ModestMsgEditWindow *window)
        g_signal_connect (G_OBJECT (html_toggle), "toggled", G_CALLBACK (on_format_toggle), &helper);
        g_signal_connect (G_OBJECT (text_toggle), "toggled", G_CALLBACK (on_format_toggle), &helper);
 
+       modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
+                                    GTK_WINDOW (dialog), GTK_WINDOW (window));
+
        /* Save settings if the user clicked on done */
        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
                TnyHeaderFlags flags;
@@ -4107,6 +4394,9 @@ setup_menu (ModestMsgEditWindow *self)
                                                  NULL);
        g_signal_connect (G_OBJECT (priv->cc_button), "toggled",
                          G_CALLBACK (on_cc_button_toggled), (gpointer) self);
+       gtk_button_set_alignment (GTK_BUTTON (priv->cc_button), 0.5, 0.5);
+       gtk_button_set_alignment (GTK_BUTTON (priv->cc_button), 0.5, 0.5);
+
        priv->bcc_button = hildon_check_button_new (0);
        gtk_button_set_label (GTK_BUTTON (priv->bcc_button), _("mcen_me_editor_showbcc"));
        hildon_check_button_set_active (HILDON_CHECK_BUTTON (priv->bcc_button),
@@ -4115,10 +4405,15 @@ setup_menu (ModestMsgEditWindow *self)
                                                  NULL);
        g_signal_connect (G_OBJECT (priv->bcc_button), "toggled",
                          G_CALLBACK (on_bcc_button_toggled), (gpointer) self);
+       gtk_button_set_alignment (GTK_BUTTON (priv->bcc_button), 0.5, 0.5);
+       gtk_button_set_alignment (GTK_BUTTON (priv->bcc_button), 0.5, 0.5);
 
        modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_editor_attach_inlineimage"), NULL,
                                           APP_MENU_CALLBACK (modest_ui_actions_on_insert_image),
                                           MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_set_style));
+       modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_editor_add_attachment"), NULL,
+                                          APP_MENU_CALLBACK (modest_msg_edit_window_add_attachment_clicked),
+                                          NULL);
        modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_remove_attachments"), NULL,
                                           APP_MENU_CALLBACK (modest_ui_actions_on_remove_attachments),
                                           MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_editor_remove_attachment));
@@ -4130,3 +4425,54 @@ setup_menu (ModestMsgEditWindow *self)
                                           NULL);
 }
 
+static void
+emit_open_addressbook (GtkButton *button,
+                      ModestRecptEditor *editor)
+{
+       g_signal_emit_by_name (G_OBJECT (editor), "open-addressbook");
+}
+
+static GtkWidget *
+_create_addressbook_box (GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group,
+                        const gchar *label, GtkWidget *control)
+{
+       GtkWidget *abook_button;
+       GtkWidget *align;
+       GtkWidget *box;
+       GtkWidget *label_widget;
+
+       box = gtk_hbox_new (FALSE, 0);
+
+       align = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
+       gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, 0, MODEST_MARGIN_DEFAULT);
+
+       abook_button = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT);
+       label_widget = gtk_label_new (label);
+       gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
+       gtk_container_add (GTK_CONTAINER (abook_button), label_widget);
+
+       gtk_container_add (GTK_CONTAINER (align), abook_button);
+       gtk_widget_set_size_request (label_widget, 148 - MODEST_MARGIN_DOUBLE, -1);
+       gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
+       gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
+       if (title_size_group)
+               gtk_size_group_add_widget (title_size_group, label_widget);
+       if (value_size_group)
+               gtk_size_group_add_widget (value_size_group, control);
+
+       g_signal_connect (G_OBJECT (abook_button), "clicked",
+                         G_CALLBACK (emit_open_addressbook), control);
+  
+       return box;  
+}
+
+static void 
+max_chars_banner_unref (ModestMsgEditWindow *self, GObject *old_ref)
+{
+       ModestMsgEditWindowPrivate *priv = NULL;
+
+       g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(self));
+
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
+       priv->max_chars_banner = NULL;
+}