* Fixes NB#95165, remove duplicate addresses in Cc when "reply to all"
[modest] / src / modest-tny-msg.c
index b7c761d..a249f7c 100644 (file)
@@ -52,7 +52,7 @@
 static TnyMimePart * add_body_part (TnyMsg *msg, const gchar *body,
                                    const gchar *content_type);
 static TnyMimePart * add_html_body_part (TnyMsg *msg, const gchar *body);
-static void add_attachments (TnyMimePart *part, GList *attachments_list, gboolean add_inline, GError **err);
+static gint add_attachments (TnyMimePart *part, GList *attachments_list, gboolean add_inline, GError **err);
 static void add_images (TnyMsg *msg, GList *attachments_list, GError **err);
 static char * get_content_type(const gchar *s);
 static gboolean is_ascii(const gchar *s);
@@ -61,11 +61,12 @@ static gboolean is_ascii(const gchar *s);
 TnyMsg*
 modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
                    const gchar *bcc, const gchar* subject, const gchar *body,
-                   GList *attachments, GError **err)
+                   GList *attachments, gint *attached, GError **err)
 {
        TnyMsg *new_msg;
        TnyHeader *header;
        gchar *content_type;
+       gint tmp_attached = 0;
        
        /* Create new msg */
        new_msg = modest_formatter_create_message (NULL, TRUE, (attachments != NULL), FALSE);
@@ -75,8 +76,11 @@ modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
                tny_header_set_from (TNY_HEADER (header), from);
                tny_header_set_replyto (TNY_HEADER (header), from);
        }
-       if ((mailto != NULL) && (strlen(mailto) > 0)) 
-               tny_header_set_to (TNY_HEADER (header), mailto);
+       if ((mailto != NULL) && (strlen(mailto) > 0)) {
+               gchar *removed_to = modest_text_utils_remove_duplicate_addresses (mailto);
+               tny_header_set_to (TNY_HEADER (header), removed_to);
+               g_free (removed_to);
+       }
        if ((cc != NULL) && (strlen(cc) > 0)) 
                tny_header_set_cc (TNY_HEADER (header), cc);
        if ((bcc != NULL) && (strlen(bcc) > 0)) 
@@ -101,7 +105,9 @@ modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
                       
        /* Add attachments */
        if (attachments)
-               add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, err);
+               tmp_attached = add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, err);
+       if (attached)
+               *attached = tmp_attached;
        if (header)
                g_object_unref(header);
 
@@ -112,11 +118,12 @@ TnyMsg*
 modest_tny_msg_new_html_plain (const gchar* mailto, const gchar* from, const gchar *cc,
                               const gchar *bcc, const gchar* subject, 
                               const gchar *html_body, const gchar *plain_body,
-                              GList *attachments, GList *images, GError **err)
+                              GList *attachments, GList *images, gint *attached, GError **err)
 {
        TnyMsg *new_msg;
        TnyHeader *header;
        gchar *content_type;
+       gint tmp_attached;
        
        /* Create new msg */
        new_msg = modest_formatter_create_message (NULL, FALSE, (attachments != NULL), (images != NULL));
@@ -151,7 +158,9 @@ modest_tny_msg_new_html_plain (const gchar* mailto, const gchar* from, const gch
        g_free (content_type);
                       
        /* Add attachments */
-       add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, err);
+       tmp_attached = add_attachments (TNY_MIME_PART (new_msg), attachments, FALSE, err);
+       if (attached)
+               *attached = tmp_attached;
        add_images (new_msg, images, err);
        if (header)
                g_object_unref(header);
@@ -297,12 +306,13 @@ copy_mime_part (TnyMimePart *part, GError **err)
        return result;
 }
 
-static void
+static gint
 add_attachments (TnyMimePart *part, GList *attachments_list, gboolean add_inline, GError **err)
 {
        GList *pos;
        TnyMimePart *attachment_part, *old_attachment;
        gint ret;
+       gint attached = 0;
 
        for (pos = (GList *)attachments_list; pos; pos = pos->next) {
 
@@ -312,16 +322,28 @@ add_attachments (TnyMimePart *part, GList *attachments_list, gboolean add_inline
                        old_cid = tny_mime_part_get_content_id (old_attachment);
                        attachment_part = copy_mime_part (old_attachment, err);
                        if (attachment_part != NULL) {
-                               tny_mime_part_set_header_pair (attachment_part, "Content-Disposition", 
-                                                              add_inline?"inline":"attachment");
+                               if (add_inline) {
+                                       tny_mime_part_set_header_pair (attachment_part, "Content-Disposition",
+                                                                      "inline");
+                               } else {
+                                       const gchar *filename;
+                                       filename = tny_mime_part_get_filename (old_attachment);
+                                       if (filename)
+                                               tny_mime_part_set_filename (attachment_part, filename);
+                                       else
+                                               tny_mime_part_set_header_pair (attachment_part, "Content-Disposition",
+                                                                              "attachment");
+                               }
                                tny_mime_part_set_transfer_encoding (TNY_MIME_PART (attachment_part), "base64");
                                ret = tny_mime_part_add_part (TNY_MIME_PART (part), attachment_part);
+                               attached++;
                                if (old_cid)
                                        tny_mime_part_set_content_id (attachment_part, old_cid);
                                g_object_unref (attachment_part);
                        }
                }
        }
+       return attached;
 }
 
 static void
@@ -431,7 +453,7 @@ modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_ht
                g_free (header_content_type_lower);
                g_free (header_content_type);
                return NULL;
-       }
+       }       
        g_free (header_content_type_lower);
        g_free (header_content_type);
 
@@ -442,11 +464,17 @@ modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_ht
 
        /* no parts? assume it's single-part message */
        if (tny_iterator_is_done(iter)) {
-               const gchar *content_type;
+               gchar *content_type;
+               gboolean is_text_part;
                g_object_unref (G_OBJECT(iter));
-               content_type = tny_mime_part_get_content_type (msg);
+               content_type = modest_tny_mime_part_get_content_type (msg);
+               if (content_type == NULL)
+                       return NULL;
+               is_text_part = 
+                       g_str_has_prefix (content_type, "text/");
+               g_free (content_type);
                /* if this part cannot be a supported body return NULL */
-               if (!g_str_has_prefix (content_type, "text/")) {
+               if (!is_text_part) {
                        return NULL;
                } else {
                        return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
@@ -466,6 +494,7 @@ modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_ht
                        /* it's a message --> ignore */
                        if (part && TNY_IS_MSG (part)) {
                                g_object_unref (part);
+                               part = NULL;
                                tny_iterator_next (iter);
                                continue;
                        }                       
@@ -487,7 +516,9 @@ modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_ht
                                g_free (content_disp);
                        }
                        
-                       if (g_str_has_prefix (content_type, desired_mime_type) && !has_content_disp_name) {
+                       if (g_str_has_prefix (content_type, desired_mime_type) && 
+                           !has_content_disp_name &&
+                           !modest_tny_mime_part_is_attachment_for_modest (part)) {
                                /* we found the desired mime-type! */
                                g_free (content_type);
                                break;
@@ -562,17 +593,17 @@ create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from,
           part of the message to create the reply/forwarded mail */
        if (msg != NULL)
                body   = modest_tny_msg_find_body_part (msg, FALSE);
-       
+
        if (modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT,
                                  NULL))
                formatter = modest_formatter_new ("text/html", signature);
        else
                formatter = modest_formatter_new ("text/plain", signature);
-       
+
 
        /* if we don't have a text-part */
        no_text_part = (!body) || (strcmp (tny_mime_part_get_content_type (body), "text/html")==0);
-       
+
        /* when we're reply, include the text part if we have it, or nothing otherwise. */
        if (is_reply)
                new_msg = modest_formatter_quote  (formatter, no_text_part ? NULL: body, header,
@@ -587,11 +618,11 @@ create_reply_forward_mail (TnyMsg *msg, TnyHeader *header, const gchar *from,
                                                            attachments);
                }
        }
-       
+
        g_object_unref (G_OBJECT(formatter));
        if (body)
                g_object_unref (G_OBJECT(body));
-       
+
        /* Fill the header */
        new_header = tny_msg_get_header (new_msg);      
        tny_header_set_from (new_header, from);
@@ -719,7 +750,7 @@ static gchar*
 get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
            ModestTnyMsgReplyMode reply_mode)
 {
-       const gchar* old_reply_to;
+       gchar* old_reply_to;
        gchar* old_from;
        gchar* new_to;
        
@@ -735,12 +766,11 @@ get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
 
 
        /* reply to sender, use ReplyTo or From */
-       //old_reply_to = tny_header_get_replyto (header);
        old_reply_to = modest_tny_mime_part_get_header_value (TNY_MIME_PART(msg), 
                                                              "Reply-To"); 
        old_from     = tny_header_dup_from (header);
        
-       if (!old_from &&  !old_reply_to) {
+       if (!old_from && !old_reply_to) {
                g_warning ("%s: failed to get either Reply-To: or From: from header",
                           __FUNCTION__);
                return NULL;
@@ -756,6 +786,7 @@ get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
                /* otherwise use either Reply-To: (preferred) or From: */
                new_to = g_strdup (old_reply_to ? old_reply_to : old_from);
        g_free (old_from);
+       g_free (old_reply_to);
 
        /* in case of ReplyAll, we need to add the Recipients in the old To: */
        if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
@@ -794,15 +825,16 @@ get_new_to (TnyMsg *msg, TnyHeader *header, const gchar* from,
 static gchar*
 get_new_cc (TnyHeader *header, const gchar* from)
 {
-       gchar *old_cc;
-       gchar *result;
+       gchar *old_cc, *result, *dup;
 
        old_cc = tny_header_dup_cc (header);
        if (!old_cc)
                return NULL;
 
        /* remove me (the new From:) from the Cc: list */
-       result =  modest_text_utils_remove_address (old_cc, from);
+       dup =  modest_text_utils_remove_address (old_cc, from);
+       result = modest_text_utils_remove_duplicate_addresses (dup);
+       g_free (dup);
        g_free (old_cc);
        return result;
 }
@@ -851,8 +883,8 @@ modest_tny_msg_create_reply_msg (TnyMsg *msg,
        else {
                tny_header_set_to (new_header, new_to);
                g_free (new_to);
-       }       
-       
+       }
+
        if (reply_mode == MODEST_TNY_MSG_REPLY_MODE_ALL) {
                gchar *new_cc = get_new_cc (header, from);
                if (new_cc) { 
@@ -860,7 +892,7 @@ modest_tny_msg_create_reply_msg (TnyMsg *msg,
                        g_free (new_cc);
                }
        }
-       
+
        /* Clean */
        g_object_unref (G_OBJECT (new_header));
        g_object_unref (G_OBJECT (header));
@@ -926,3 +958,55 @@ modest_tny_msg_estimate_size (const gchar *plain_body, const gchar *html_body,
 
        return result;
 }
+
+GSList *
+modest_tny_msg_header_get_all_recipients_list (TnyHeader *header)
+{
+       GSList *recipients = NULL;
+       gchar *from = NULL, *to = NULL, *cc = NULL, *bcc = NULL;
+
+       if (header == NULL)
+               return NULL;
+
+       from = tny_header_dup_from (header);
+       to = tny_header_dup_to (header);
+       cc = tny_header_dup_cc (header);
+       bcc = tny_header_dup_bcc (header);
+
+       recipients = NULL;
+       if (from)
+               recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (from));
+       if (to)
+               recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (to));
+       if (cc)
+               recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (cc));
+       if (bcc)
+               recipients = g_slist_concat (recipients, modest_text_utils_split_addresses_list (bcc));
+
+       g_free (from);
+       g_free (to);
+       g_free (cc);
+       g_free (bcc);
+
+       return recipients;
+}
+
+GSList *
+modest_tny_msg_get_all_recipients_list (TnyMsg *msg)
+{
+       TnyHeader *header = NULL;
+       GSList *recipients = NULL;
+
+       if (msg == NULL)
+               return NULL;
+
+       header = tny_msg_get_header (msg);
+       if (header == NULL)
+               return NULL;
+
+       recipients = modest_tny_msg_header_get_all_recipients_list (header);
+       g_object_unref (header);
+
+       return recipients;
+}
+