From d56c675a5d1e7a12e322e48abb5bec035dddb176 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 14 Nov 2011 17:25:21 +0100 Subject: [PATCH] In SolipaMail implemented attachments (closes #93). --- src/mail.c | 121 ++++++++++++++++++++++++++++++++++++------ tests/gnome-globe.png | Bin 0 -> 4780 bytes tests/mail.c | 4 ++ 3 files changed, 110 insertions(+), 15 deletions(-) create mode 100644 tests/gnome-globe.png diff --git a/src/mail.c b/src/mail.c index 095cbb3..4a969bb 100644 --- a/src/mail.c +++ b/src/mail.c @@ -242,6 +242,8 @@ static CamelMimeMessage SolipaMailPrivate *priv; CamelMimeMessage *msg; + CamelMultipart *mp; + CamelMimePart *part; g_return_val_if_fail (IS_SOLIPA_MAIL (mail), NULL); @@ -265,6 +267,8 @@ static CamelMimeMessage return NULL; } + mp = NULL; + msg = camel_mime_message_new (); camel_mime_message_set_subject (msg, priv->subject); @@ -286,18 +290,28 @@ static CamelMimeMessage camel_mime_message_set_recipients (msg, CAMEL_RECIPIENT_TYPE_BCC, priv->recipients_bcc); } - /* TODO multipart message */ - if (priv->body_plain != NULL - && priv->body_html != NULL) + if (priv->attachments != NULL + || (priv->body_plain != NULL + && priv->body_html != NULL)) { - CamelMultipart *mp; - CamelMimePart *part; - mp = camel_multipart_new (); - camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (mp), "multipart/alternative"); + if (priv->attachments == NULL) + { + camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (mp), "multipart/alternative"); + } + else + { + camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (mp), "multipart/mixed"); + } + camel_multipart_set_boundary (mp, NULL); camel_multipart_set_preface (mp, "This is a multipart message in MIME format."); + } + /* multipart/alternative message */ + if (priv->body_plain != NULL + && priv->body_html != NULL) + { part = camel_mime_part_new (); camel_mime_part_set_content (part, priv->body_plain, strlen (priv->body_plain), "text/plain"); camel_multipart_add_part (mp, part); @@ -307,24 +321,103 @@ static CamelMimeMessage camel_mime_part_set_content (part, priv->body_html, strlen (priv->body_html), "text/html"); camel_multipart_add_part (mp, part); g_object_unref (part); - - camel_medium_set_content (CAMEL_MEDIUM (msg), CAMEL_DATA_WRAPPER (mp)); } else { + if (mp != NULL) + { + part = camel_mime_part_new (); + } + else + { + part = CAMEL_MIME_PART (msg); + } + if (priv->body_plain != NULL) { - camel_mime_part_set_content (CAMEL_MIME_PART (msg), priv->body_plain, strlen (priv->body_plain), "text/plain"); + camel_mime_part_set_content (CAMEL_MIME_PART (part), priv->body_plain, strlen (priv->body_plain), "text/plain"); + } + else if (priv->body_html != NULL) + { + camel_mime_part_set_content (CAMEL_MIME_PART (part), priv->body_html, strlen (priv->body_html), "text/html"); } - if (priv->body_html != NULL) + + if (mp != NULL) { - camel_mime_part_set_content (CAMEL_MIME_PART (msg), priv->body_html, strlen (priv->body_html), "text/html"); + camel_multipart_add_part (mp, part); + g_object_unref (part); } } + /* multipart message with attachment(s) */ if (priv->attachments != NULL) { - /* multipart message */ + GSList *attachs; + GFile *file; + CamelDataWrapper *dw; + GError *error; + GFileInfo *file_info; + CamelStream *cstream; + + attachs = priv->attachments; + while (attachs != NULL) + { + file = g_file_new_for_path ((gchar *)attachs->data); + if (!g_file_query_exists (file, NULL)) + { + g_warning ("File doesn't exists: %s", (gchar *)attachs->data); + } + else + { + error = NULL; + file_info = g_file_query_info (file, + "standard::*", + 0, + NULL, + &error); + + if (file_info == NULL || error != NULL) + { + g_warning ("Unable to get mime type of file %s: %s.", + error != NULL && error->message != NULL ? error->message : "no details"); + } + else + { + dw = camel_data_wrapper_new (); + + camel_data_wrapper_set_mime_type (dw, g_content_type_get_mime_type (g_file_info_get_content_type (file_info))); + + error = NULL; + cstream = camel_stream_fs_new_with_name ((gchar *)attachs->data, O_RDONLY, 0, &error); + error = NULL; +#ifdef CAMEL3 + camel_data_wrapper_construct_from_stream_sync (dw, cstream, NULL, &error); +#else + camel_data_wrapper_construct_from_stream (dw, cstream, &error); +#endif + g_object_unref (cstream); + + part = camel_mime_part_new (); + camel_medium_set_content (CAMEL_MEDIUM (part), dw); + g_object_unref (dw); + + camel_mime_part_set_filename (CAMEL_MIME_PART (part), g_file_get_basename (file)); + camel_mime_part_set_disposition (CAMEL_MIME_PART (part), "attachment"); + + camel_multipart_add_part (mp, part); + g_object_unref (part); + } + } + g_object_unref (file); + + attachs = g_slist_next (attachs); + } + } + + if (mp != NULL) + { + camel_medium_set_content (CAMEL_MEDIUM (msg), CAMEL_DATA_WRAPPER (mp)); + g_object_unref (mp); } return msg; @@ -595,7 +688,6 @@ solipa_mail_set_property (GObject *object, GParamSpec *pspec) { SolipaMail *solipa_mail = (SolipaMail *)object; - SolipaMailPrivate *priv = SOLIPA_MAIL_GET_PRIVATE (solipa_mail); switch (property_id) @@ -613,7 +705,6 @@ solipa_mail_get_property (GObject *object, GParamSpec *pspec) { SolipaMail *solipa_mail = (SolipaMail *)object; - SolipaMailPrivate *priv = SOLIPA_MAIL_GET_PRIVATE (solipa_mail); switch (property_id) diff --git a/tests/gnome-globe.png b/tests/gnome-globe.png new file mode 100644 index 0000000000000000000000000000000000000000..c85ca9f475f2973ab7ae6809f27fd19ded285432 GIT binary patch literal 4780 zcmV;d5>xGoP)j1#bpjpg04jj^o7 z+NIXglDgIFdv|`c)|M>EY^h3es!sJ&zkc`L@11+jJ@>pPA*JMRNtD0z06uK_n*-K9 z%+AEMuiGGyi>A0S6a9FNPWk*Du@7|&ACwP^kNv z34_4x-y%~F3Bc8C!K;U zJHAYvG+kiRARLEK+@z;JPFwE?T|Ge#)pc?BL?@wO#8He=tr0g}79S8W4(uKV-7+ zL@x)9bg=ua^D;CXdt}B&qtLD4v_Wnf zq-hd~AR061?hRs?5?6*5ccvYHy81r6E*k~eY2$7BLvVfwA_?dZCV1wh(`m{=Ba-D8qxFk%#~J#Sax&+`Qw9 zHmmaJ*X~}SePmTO3BzQWCjo|m&8j05K{y=4ZnH2r66bV7A2rKzu~<}&zTHDnz7x08 zikV1Yn&t)jQ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p#ahTe)?*U(EY|4pap1g)~9Ao}0{0000\n" ""); + solipa_mail_add_attachment (smail, "mail.c"); + solipa_mail_add_attachment (smail, "gnome-globe.png"); + solipa_mail_add_attachment (smail, "ooo.odt"); + strmail = solipa_mail_get_as_string (smail); g_message ("The email text:\n%s", strmail); -- 2.49.0