Fixes RevNULL 2/14
[modest] / src / modest-tny-msg.c
index 2790914..b64155a 100644 (file)
@@ -33,6 +33,7 @@
 #include <tny-simple-list.h>
 #include <tny-folder.h>
 #include <modest-runtime.h>
+#include <modest-defs.h>
 #include "modest-formatter.h"
 #include <tny-camel-mem-stream.h>
 #include <tny-camel-mime-part.h>
@@ -367,9 +368,9 @@ add_images (TnyMsg *msg, GList *images_list, GError **err)
                        if (part && !g_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
                                related_part = part;
                                break;
-                       } else {
-                               g_object_unref (part);
                        }
+                       if (part)
+                               g_object_unref (part);
                        tny_iterator_next (iter);
                }
                g_object_unref (iter);
@@ -488,6 +489,7 @@ modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_ht
 
                        if (!part) {
                                g_warning ("%s: not a valid mime part", __FUNCTION__);
+                               tny_iterator_next (iter);
                                continue;
                        }
 
@@ -593,17 +595,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,
@@ -618,11 +620,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);
@@ -825,15 +827,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;
 }
@@ -865,8 +868,7 @@ modest_tny_msg_create_reply_msg (TnyMsg *msg,
                g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
                g_list_free (attachments_list);
        }
-       if (parts)
-               g_object_unref (G_OBJECT (parts));
+       g_object_unref (parts);
 
        /* Fill the header */
        if (header)
@@ -882,8 +884,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) { 
@@ -891,7 +893,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));
@@ -957,3 +959,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;
+}
+