Treat a message as modified if the From: field changes
[modest] / src / maemo / modest-msg-edit-window.c
index efbc641..3fd0e73 100644 (file)
@@ -179,6 +179,8 @@ static void text_buffer_mark_set (GtkTextBuffer *buffer,
 void vadj_changed (GtkAdjustment *adj, 
                   ModestMsgEditWindow *window);
 
+static ModestPair *find_transport_from_message_sender (ModestPairList *transports,
+                                                      TnyMsg *msg);
 
 
 
@@ -243,6 +245,7 @@ struct _ModestMsgEditWindowPrivate {
        
        ModestPairList *from_field_protos;
        GtkWidget   *from_field;
+       const gchar *original_account_name;
        
        GtkWidget   *to_field;
        GtkWidget   *cc_field;
@@ -492,6 +495,41 @@ get_transports (void)
        return transports;
 }
 
+/**
+ * Search an (account, address) ModestPairList for a pair whose
+ * address matches the one in the From: header of a TnyMsg
+ *
+ * @result: A ModestPair * with a matching address, or NULL if none found
+ */
+static ModestPair *
+find_transport_from_message_sender (ModestPairList *transports, TnyMsg *msg)
+{
+       g_return_val_if_fail (transports, NULL);
+       g_return_val_if_fail (msg, NULL);
+
+       ModestPair *account_pair = NULL;
+       TnyHeader *header = tny_msg_get_header (msg);
+
+       if (header != NULL && tny_header_get_from (header)) {
+               char *from_addr = modest_text_utils_get_email_address (tny_header_get_from (header));
+               GSList *iter;
+               for (iter = transports; iter && !account_pair; iter = iter->next) {
+                       ModestPair *pair = (ModestPair *) iter->data;
+                       char *account_addr = modest_text_utils_get_email_address ((char *) pair->second);
+                       if (account_addr && !strcasecmp(from_addr, account_addr)) {
+                               account_pair = pair;
+                       }
+                       g_free (account_addr);
+               }
+               g_free (from_addr);
+       }
+
+       if (header)
+               g_object_unref (header);
+
+       return account_pair;
+}
+
 static void window_focus (GtkWindow *window,
                          GtkWidget *widget,
                          gpointer userdata)
@@ -1447,10 +1485,15 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, gboolean pre
                
        modest_window_set_active_account (MODEST_WINDOW(obj), account_name);
 
-       account_pair = modest_pair_list_find_by_first_as_string (priv->from_field_protos, account_name);
+       account_pair = find_transport_from_message_sender (priv->from_field_protos, msg);
+       if (account_pair == NULL) {
+               account_pair = modest_pair_list_find_by_first_as_string (priv->from_field_protos, account_name);
+       }
        if (account_pair != NULL)
                modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->from_field), account_pair->first);
 
+       priv->original_account_name = account_pair ? (const gchar *) account_pair->first : NULL;
+
        parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new ();
        menu_rules_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_MENU, FALSE);
        toolbar_rules_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_TOOLBAR, TRUE);
@@ -2970,6 +3013,7 @@ gboolean
 modest_msg_edit_window_is_modified (ModestMsgEditWindow *editor)
 {
        ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (editor);
+       const char *account_name;
        GtkTextBuffer *buffer;
 
        buffer = modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR(priv->to_field));
@@ -2983,6 +3027,10 @@ modest_msg_edit_window_is_modified (ModestMsgEditWindow *editor)
                return TRUE;
        if (gtk_text_buffer_get_modified (priv->text_buffer))
                return TRUE;
+       account_name = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->from_field));
+       if (!priv->original_account_name || strcmp(account_name, priv->original_account_name)) {
+               return TRUE;
+       }
 
        return FALSE;
 }