From: Andrea Zagli Date: Mon, 14 Nov 2011 16:25:21 +0000 (+0100) Subject: In SolipaMail implemented attachments (closes #93). X-Git-Tag: 0.5.0~29 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=d56c675a5d1e7a12e322e48abb5bec035dddb176;p=solipa%2Flibsolipa In SolipaMail implemented attachments (closes #93). --- 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 0000000..c85ca9f Binary files /dev/null and b/tests/gnome-globe.png differ diff --git a/tests/mail.c b/tests/mail.c index 045ea37..2032f6a 100644 --- a/tests/mail.c +++ b/tests/mail.c @@ -54,6 +54,10 @@ main (int argc, char **argv) "\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);