Fix of a crash when provider does not provide any mailbox
[modest] / src / hildon2 / modest-msg-edit-window.c
index 3d4837d..6898f64 100644 (file)
@@ -177,6 +177,9 @@ 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 DEBUG_BUFFER (WPTextBuffer *buffer)
 {
 #ifdef DEBUG
@@ -445,46 +448,6 @@ modest_msg_edit_window_init (ModestMsgEditWindow *obj)
                                   HILDON_WINDOW(obj));
 }
 
-static gboolean account_is_multimailbox (const gchar *account_name, ModestProtocol **mmb_protocol)
-{
-       gchar *transport_account;
-       gboolean result = FALSE;
-
-       if (mmb_protocol)
-               *mmb_protocol = 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 (protocol &&
-                           modest_protocol_registry_protocol_type_has_tag 
-                           (registry,
-                            modest_protocol_get_type_id (protocol),
-                            MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS)) {
-                               if (mmb_protocol)
-                                       *mmb_protocol = protocol;
-                               result = TRUE;
-                       }
-                       
-               }
-       }
-
-       return result;
-}
-
 static gchar *
 multimailbox_get_default_mailbox (const gchar *account_name)
 {
@@ -546,7 +509,7 @@ get_transports (void)
                        gboolean multi_mailbox = FALSE;
                        ModestProtocol *protocol = NULL;
 
-                       if (account_is_multimailbox (account_name, &protocol)) {
+                       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 (),
@@ -1617,14 +1580,20 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, const gchar
        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 (account_is_multimailbox (account_name, NULL)) {
+       } 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);
-               modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->from_field),
-                                                                             (gpointer) priv->original_mailbox);
+               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);
        }
@@ -3879,9 +3848,9 @@ on_account_removed (TnyAccountStore *account_store,
        }
 }
 
-static void
-from_field_changed (HildonPickerButton *button,
-                   ModestMsgEditWindow *self)
+static void update_signature (ModestMsgEditWindow *self,
+                             const gchar *old_account, 
+                             const gchar *new_account)
 {
        ModestMsgEditWindowPrivate *priv;
        gboolean has_old_signature, has_new_signature;
@@ -3893,30 +3862,54 @@ from_field_changed (HildonPickerButton *button,
 
        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) {
+                       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;
+                       }
+                       g_free (full_signature);
                }
-               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);
                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
+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;
+       priv->last_from_account = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->from_field));
+       new_account = priv->last_from_account;
+
+       update_signature (self, old_account, new_account);
+
 }
 
 typedef struct _MessageSettingsHelper {