]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Aggiunta SolipaMail::set_subject.
authorAndrea Zagli <azagli@libero.it>
Sun, 28 Nov 2010 11:58:33 +0000 (12:58 +0100)
committerAndrea Zagli <azagli@libero.it>
Sun, 28 Nov 2010 11:58:33 +0000 (12:58 +0100)
Cambiato il tipo dei recipients in SolipaMailPrivate (da GSList a
CamelInternetAddress.
Implementata SolipaMail::get_as_string.

src/mail.c
src/mail.h

index 52a90056c0bbc064387676fc57f6b930aba8fcba..37ec69399447e4595d7b684700a2cbe62336f6e9 100644 (file)
@@ -20,6 +20,8 @@
        #include <config.h>
 #endif
 
+#include <string.h>
+
 #include "mail.h"
 
 static void solipa_mail_class_init (SolipaMailClass *class);
@@ -41,11 +43,13 @@ struct _SolipaMailPrivate
        {
                Solipa *solipa;
 
+               gchar *subject;
+
                CamelInternetAddress *from;
 
-               GSList *recipients_to; /* CamelInternetAddress */
-               GSList *recipients_cc; /* CamelInternetAddress */
-               GSList *recipients_bcc; /* CamelInternetAddress */
+               CamelInternetAddress *recipients_to;
+               CamelInternetAddress *recipients_cc;
+               CamelInternetAddress *recipients_bcc;
 
                gchar *body_plain;
                gchar *body_html;
@@ -71,6 +75,7 @@ solipa_mail_init (SolipaMail *solipa_mail)
 {
        SolipaMailPrivate *priv = SOLIPA_MAIL_GET_PRIVATE (solipa_mail);
 
+       priv->subject = "{No subject}";
        priv->from = NULL;
        priv->recipients_to = NULL;
        priv->recipients_cc = NULL;
@@ -103,16 +108,56 @@ SolipaMail
        return solipa_mail;
 }
 
+void
+solipa_mail_set_subject (SolipaMail *mail, const gchar *subject)
+{
+       SolipaMailPrivate *priv;
+       gchar *sbj;
+
+       g_return_if_fail (IS_SOLIPA_MAIL (mail));
+
+       priv = SOLIPA_MAIL_GET_PRIVATE (mail);
+
+       sbj = g_strdup ("{No subject}");
+
+       if (subject != NULL)
+               {
+                       sbj = g_strstrip (g_strdup (subject));
+                       if (g_strcmp0 (sbj, "") == 0)
+                               {
+                                       sbj = g_strdup ("{No subject}");
+                               }
+               }
+
+       priv->subject = g_strdup (sbj);
+       g_free (sbj);
+}
+
 void
 solipa_mail_set_from (SolipaMail *mail, CamelInternetAddress *from)
 {
        SolipaMailPrivate *priv;
 
+       guint addrs;
+
        g_return_if_fail (IS_SOLIPA_MAIL (mail));
 
        priv = SOLIPA_MAIL_GET_PRIVATE (mail);
 
        priv->from = g_memdup (from, sizeof (CamelInternetAddress));
+
+       /* if addresses are more then one
+        * then it removes they */
+       addrs = camel_address_length (CAMEL_ADDRESS (priv->from));
+       if (addrs > 1)
+               {
+                       guint i;
+
+                       for (i = addrs - 1; i > 0; i--)
+                               {
+                                       camel_address_remove (CAMEL_ADDRESS (priv->from), i);
+                               }
+               }
 }
 
 void
@@ -120,27 +165,50 @@ solipa_mail_add_recipient (SolipaMail *mail, CamelInternetAddress *recipient, So
 {
        SolipaMailPrivate *priv;
 
+       CamelInternetAddress *rcpt;
+
+       gint addrs;
+       gint i;
+
+       const gchar *namep;
+       const gchar *addressp;
+
        g_return_if_fail (IS_SOLIPA_MAIL (mail));
 
+       g_return_if_fail (CAMEL_IS_INTERNET_ADDRESS (recipient));
+
        priv = SOLIPA_MAIL_GET_PRIVATE (mail);
 
        switch (type)
                {
                        case SOLIPA_MAIL_RECIPIENT_TYPE_TO:
-                               priv->recipients_to = g_slist_append (priv->recipients_to, (gpointer)recipient);
+                               rcpt = priv->recipients_to;
                                break;
 
                        case SOLIPA_MAIL_RECIPIENT_TYPE_CC:
-                               priv->recipients_cc = g_slist_append (priv->recipients_to, (gpointer)recipient);
+                               rcpt = priv->recipients_cc;
                                break;
 
                        case SOLIPA_MAIL_RECIPIENT_TYPE_BCC:
-                               priv->recipients_bcc = g_slist_append (priv->recipients_to, (gpointer)recipient);
+                               rcpt = priv->recipients_bcc;
                                break;
 
                        default:
                                g_warning ("Recipient of type «%d» not supported.", type);
-                               break;
+                               return;
+               }
+
+       if (!CAMEL_IS_INTERNET_ADDRESS (rcpt))
+               {
+                       rcpt = camel_internet_address_new ();
+               }
+
+       addrs = camel_address_length (CAMEL_ADDRESS (recipient));
+       for (i = 0; i < addrs; i++)
+               {
+                       camel_internet_address_get (recipient, i,
+                                                   &namep, &addressp);
+                       camel_internet_address_add (rcpt, namep, addressp);
                }
 }
 
@@ -169,6 +237,106 @@ solipa_mail_add_attachment (SolipaMail *mail, const gchar *filename)
        priv->attachments = g_slist_append (priv->attachments, (gpointer)filename);
 }
 
+gchar *
+solipa_mail_get_as_string (SolipaMail *mail)
+{
+       gchar *ret;
+
+       SolipaMailPrivate *priv;
+
+       CamelMimeMessage *msg;
+
+       CamelStream *mem;
+       gchar tmp_buf[4096];
+       gssize nb_read;
+
+       g_return_val_if_fail (IS_SOLIPA_MAIL (mail), NULL);
+
+       priv = SOLIPA_MAIL_GET_PRIVATE (mail);
+
+       if (priv->from == NULL)
+               {
+                       g_warning ("From cannot be empty.");
+                       return NULL;
+               }
+
+       if ((priv->recipients_to == NULL
+            || camel_address_length (CAMEL_ADDRESS (priv->recipients_to)) == 0)
+           && (priv->recipients_cc == NULL
+               || camel_address_length (CAMEL_ADDRESS (priv->recipients_cc))== 0)
+           && (priv->recipients_bcc == NULL
+               || camel_address_length (CAMEL_ADDRESS (priv->recipients_bcc))== 0))
+               {
+                       g_warning ("Recipients cannot be empty.");
+                       return NULL;
+               }
+
+       g_thread_init (NULL);
+
+       camel_init (NULL, FALSE);
+       camel_provider_init ();
+
+       msg = camel_mime_message_new ();
+       camel_mime_message_set_subject (msg, priv->subject);
+
+       camel_mime_message_set_from (msg, priv->from);
+
+       if (priv->recipients_to != NULL
+           && camel_address_length (CAMEL_ADDRESS (priv->recipients_to)) > 0)
+               {
+                       camel_mime_message_set_recipients (msg, CAMEL_RECIPIENT_TYPE_TO, priv->recipients_to);
+               }
+       if (priv->recipients_cc != NULL
+           && camel_address_length (CAMEL_ADDRESS (priv->recipients_cc)) > 0)
+               {
+                       camel_mime_message_set_recipients (msg, CAMEL_RECIPIENT_TYPE_CC, priv->recipients_cc);
+               }
+       if (priv->recipients_bcc != NULL
+           && camel_address_length (CAMEL_ADDRESS (priv->recipients_bcc)) > 0)
+               {
+                       camel_mime_message_set_recipients (msg, CAMEL_RECIPIENT_TYPE_BCC, priv->recipients_bcc);
+               }
+
+       if (priv->body_plain != NULL)
+               {
+                       camel_mime_part_set_content (CAMEL_MIME_PART (msg), priv->body_plain, strlen (priv->body_plain), "text/plain");
+               }
+       if (priv->body_html != NULL)
+               {
+                       camel_mime_part_set_content (CAMEL_MIME_PART (msg), priv->body_html, strlen (priv->body_html), "text/html");
+               }
+
+       if (priv->attachments != NULL)
+               {
+                       /* multipart message */
+               }
+
+       mem = camel_stream_mem_new ();
+       camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (msg), mem);
+       if (camel_stream_reset (mem) < 0)
+               {
+                       g_warning ("Unable to reset the mem stream.");
+                       return NULL;
+               }
+
+       ret = g_strdup ("");
+       while (!camel_stream_eos (mem))
+               {
+                       nb_read = camel_stream_read (mem, tmp_buf, sizeof (tmp_buf));
+                       if (nb_read < 0)
+                               {
+                                       g_warning ("Errore.");
+                                       break;
+                               }
+                       else if (nb_read > 0)
+                               {
+                                       ret = g_strconcat (ret, tmp_buf, NULL);
+                               }
+               }
+
+       return ret;
+}
+
 /* PRIVATE */
 static void
 solipa_mail_set_property (GObject *object,
index 45decd126d0d2e22ee0679868e60c5656feb03b6..f2873d948fa8db4735da2ff4e236ebee81a6b2b6 100644 (file)
@@ -55,6 +55,8 @@ GType solipa_mail_get_type (void) G_GNUC_CONST;
 
 SolipaMail *solipa_mail_new (Solipa *solipa);
 
+void solipa_mail_set_subject (SolipaMail *mail, const gchar *subject);
+
 void solipa_mail_set_from (SolipaMail *mail, CamelInternetAddress *from);
 
 typedef enum