X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fmodest-mail-operation.c;h=c4d5b05be986c581c22cf177ee14acc1754e2397;hp=2012d79b70b69c9f12bcdfdf1d1c3057e5bdcaf7;hb=f65cf12510835b6adb63cf583872995940d6daa4;hpb=dcb607d30e118e1a68562a0f0cc9ba1751068dac diff --git a/src/modest-mail-operation.c b/src/modest-mail-operation.c index 2012d79..c4d5b05 100644 --- a/src/modest-mail-operation.c +++ b/src/modest-mail-operation.c @@ -37,14 +37,16 @@ #include #include #include +#include #include #include #include +#include +#include #include "modest-text-utils.h" #include "modest-tny-msg.h" #include "modest-tny-platform-factory.h" #include "modest-marshal.h" -#include "modest-formatter.h" #include "modest-error.h" /* 'private'/'protected' functions */ @@ -65,25 +67,6 @@ static void update_folders_cb (TnyFolderStore *self, TnyList *list, GError **err, gpointer user_data); -static void add_attachments (TnyMsg *msg, - GList *attachments_list); - - -static TnyMimePart * add_body_part (TnyMsg *msg, - const gchar *body, - const gchar *content_type, - gboolean has_attachments); - - -static void modest_mail_operation_xfer_folder (ModestMailOperation *self, - TnyFolder *folder, - TnyFolderStore *parent, - gboolean delete_original); - -static gboolean modest_mail_operation_xfer_msg (ModestMailOperation *self, - TnyHeader *header, - TnyFolder *folder, - gboolean delete_original); enum _ModestMailOperationSignals { @@ -99,10 +82,16 @@ struct _ModestMailOperationPrivate { ModestMailOperationStatus status; GError *error; }; + #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \ MODEST_TYPE_MAIL_OPERATION, \ ModestMailOperationPrivate)) +#define CHECK_EXCEPTION(priv, new_status, op) if (priv->error) {\ + priv->status = new_status;\ + op;\ + } + typedef struct _RefreshFolderAsyncHelper { ModestMailOperation *mail_op; @@ -112,10 +101,6 @@ typedef struct _RefreshFolderAsyncHelper } RefreshFolderAsyncHelper; -/* some utility functions */ -static char * get_content_type(const gchar *s); -static gboolean is_ascii(const gchar *s); - /* globals */ static GObjectClass *parent_class = NULL; @@ -213,28 +198,38 @@ modest_mail_operation_send_mail (ModestMailOperation *self, TnyTransportAccount *transport_account, TnyMsg* msg) { + TnySendQueue *send_queue; + g_return_if_fail (MODEST_IS_MAIL_OPERATION (self)); g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account)); - - tny_transport_account_send (transport_account, msg, NULL); /* FIXME */ + + send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account)); + if (!TNY_IS_SEND_QUEUE(send_queue)) + g_printerr ("modest: could not find send queue for account\n"); + else { + GError *err = NULL; + tny_send_queue_add (send_queue, msg, &err); + if (err) { + g_printerr ("modest: error adding msg to send queue: %s\n", + err->message); + g_error_free (err); + } else + g_message ("modest: message added to send queue"); + } } void modest_mail_operation_send_new_mail (ModestMailOperation *self, TnyTransportAccount *transport_account, - const gchar *from, - const gchar *to, - const gchar *cc, - const gchar *bcc, - const gchar *subject, - const gchar *body, + const gchar *from, const gchar *to, + const gchar *cc, const gchar *bcc, + const gchar *subject, const gchar *plain_body, + const gchar *html_body, const GList *attachments_list) { - TnyPlatformFactory *fact; TnyMsg *new_msg; - TnyHeader *header; - gchar *content_type; ModestMailOperationPrivate *priv = NULL; + GList *node = NULL; g_return_if_fail (MODEST_IS_MAIL_OPERATION (self)); g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account)); @@ -245,205 +240,23 @@ modest_mail_operation_send_new_mail (ModestMailOperation *self, if (to == NULL) { g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR, MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER, - _("Error trying to send a mail. You need to set almost one a recipient")); + _("Error trying to send a mail. You need to set at least one recipient")); return; } - /* Create new msg */ - fact = modest_tny_platform_factory_get_instance (); - new_msg = tny_platform_factory_new_msg (fact); - header = tny_platform_factory_new_header (fact); - - /* WARNING: set the header before assign values to it */ - tny_msg_set_header (new_msg, header); - tny_header_set_from (TNY_HEADER (header), from); - tny_header_set_replyto (TNY_HEADER (header), from); - tny_header_set_to (TNY_HEADER (header), to); - tny_header_set_cc (TNY_HEADER (header), cc); - tny_header_set_bcc (TNY_HEADER (header), bcc); - tny_header_set_subject (TNY_HEADER (header), subject); - - content_type = get_content_type(body); - - /* Add the body of the new mail */ - add_body_part (new_msg, body, (const gchar *) content_type, - (attachments_list == NULL) ? FALSE : TRUE); - - /* Add attachments */ - add_attachments (new_msg, (GList*) attachments_list); - - /* Send mail */ - tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */ - - /* Clean */ - g_object_unref (header); - g_object_unref (new_msg); - g_free(content_type); -} - -static void -add_if_attachment (gpointer data, gpointer user_data) -{ - TnyMimePart *part; - GList *attachments_list; - - part = TNY_MIME_PART (data); - attachments_list = (GList *) user_data; - - if (tny_mime_part_is_attachment (part)) - attachments_list = g_list_prepend (attachments_list, part); -} - - -static TnyMsg * -create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type) -{ - TnyPlatformFactory *fact; - TnyMsg *new_msg; - TnyHeader *new_header, *header; - gchar *new_subject; - TnyMimePart *body; - ModestFormatter *formatter; - - /* Get body from original msg. Always look for the text/plain - part of the message to create the reply/forwarded mail */ - header = tny_msg_get_header (msg); - body = modest_tny_msg_find_body_part (msg, FALSE); - - /* TODO: select the formatter from account prefs */ - formatter = modest_formatter_new ("text/plain"); - - /* Format message body */ - if (is_reply) { - switch (type) { - case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE: - default: - new_msg = modest_formatter_cite (formatter, body, header); - break; - case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE: - new_msg = modest_formatter_quote (formatter, body, header); - break; - } + if (html_body == NULL) { + new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */ } else { - switch (type) { - case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE: - default: - new_msg = modest_formatter_inline (formatter, body, header); - break; - case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT: - new_msg = modest_formatter_attach (formatter, body, header); - break; - } + new_msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list); } - g_object_unref (G_OBJECT(formatter)); - g_object_unref (G_OBJECT(body)); - - /* Fill the header */ - fact = modest_tny_platform_factory_get_instance (); - new_header = TNY_HEADER (tny_platform_factory_new_header (fact)); - tny_msg_set_header (new_msg, new_header); - tny_header_set_from (new_header, from); - tny_header_set_replyto (new_header, from); - - /* Change the subject */ - new_subject = - (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header), - (is_reply) ? _("Re:") : _("Fwd:")); - tny_header_set_subject (new_header, (const gchar *) new_subject); - g_free (new_subject); - - /* Clean */ - g_object_unref (G_OBJECT (new_header)); - g_object_unref (G_OBJECT (header)); - - return new_msg; -} - -TnyMsg* -modest_mail_operation_create_forward_mail (TnyMsg *msg, - const gchar *from, - ModestMailOperationForwardType forward_type) -{ - TnyMsg *new_msg; - TnyList *parts = NULL; - GList *attachments_list = NULL; - - new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type); - - /* Add attachments */ - parts = TNY_LIST (tny_simple_list_new()); - tny_mime_part_get_parts (TNY_MIME_PART (msg), parts); - tny_list_foreach (parts, add_if_attachment, attachments_list); - add_attachments (new_msg, attachments_list); - - /* Clean */ - if (attachments_list) g_list_free (attachments_list); - g_object_unref (G_OBJECT (parts)); - - return new_msg; -} - -TnyMsg* -modest_mail_operation_create_reply_mail (TnyMsg *msg, - const gchar *from, - ModestMailOperationReplyType reply_type, - ModestMailOperationReplyMode reply_mode) -{ - TnyMsg *new_msg = NULL; - TnyHeader *new_header, *header; - const gchar* reply_to; - gchar *new_cc = NULL; - const gchar *cc = NULL, *bcc = NULL; - GString *tmp = NULL; - - new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type); - - /* Fill the header */ - header = tny_msg_get_header (msg); - new_header = tny_msg_get_header (new_msg); - reply_to = tny_header_get_replyto (header); - - if (reply_to) - tny_header_set_to (new_header, reply_to); - else - tny_header_set_to (new_header, tny_header_get_from (header)); - - switch (reply_mode) { - case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER: - /* Do not fill neither cc nor bcc */ - break; - case MODEST_MAIL_OPERATION_REPLY_MODE_LIST: - /* TODO */ - break; - case MODEST_MAIL_OPERATION_REPLY_MODE_ALL: - /* Concatenate to, cc and bcc */ - cc = tny_header_get_cc (header); - bcc = tny_header_get_bcc (header); - - tmp = g_string_new (tny_header_get_to (header)); - if (cc) g_string_append_printf (tmp, ",%s",cc); - if (bcc) g_string_append_printf (tmp, ",%s",bcc); - - /* Remove my own address from the cc list. TODO: - remove also the To: of the new message, needed due - to the new reply_to feature */ - new_cc = (gchar *) - modest_text_utils_remove_address ((const gchar *) tmp->str, - from); - /* FIXME: remove also the mails from the new To: */ - tny_header_set_cc (new_header, new_cc); - - /* Clean */ - g_string_free (tmp, TRUE); - g_free (new_cc); - break; + if (!new_msg) { + g_printerr ("modest: failed to create a new msg\n"); + return; } + + modest_mail_operation_send_mail (self, transport_account, new_msg); - /* Clean */ - g_object_unref (G_OBJECT (new_header)); - g_object_unref (G_OBJECT (header)); - - return new_msg; + g_object_unref (G_OBJECT(new_msg)); } static void @@ -494,10 +307,13 @@ folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer g_object_unref (G_OBJECT (list)); g_slice_free (RefreshFolderAsyncHelper, helper); } else { - tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)), - folder_refresh_cb, - status_update_cb, - helper); + TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (helper->iter)); + if (folder) { + g_message ("modest: refreshing folder %s", + tny_folder_get_name (folder)); + tny_folder_refresh_async (folder, folder_refresh_cb, status_update_cb, helper); + g_object_unref (G_OBJECT(folder)); // FIXME: don't unref yet + } } g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL); } @@ -509,7 +325,8 @@ update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gp ModestMailOperation *self; ModestMailOperationPrivate *priv; RefreshFolderAsyncHelper *helper; - + TnyFolder *folder; + self = MODEST_MAIL_OPERATION (user_data); priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self); @@ -530,10 +347,13 @@ update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gp helper->canceled = 0; /* Async refresh folders */ - tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)), - folder_refresh_cb, - status_update_cb, - helper); + folder = TNY_FOLDER (tny_iterator_get_current (helper->iter)); + if (folder) { + g_message ("modest: refreshing folder %s", tny_folder_get_name (folder)); + tny_folder_refresh_async (folder, folder_refresh_cb, + status_update_cb, helper); + } + //g_object_unref (G_OBJECT(folder)); /* FIXME -==> don't unref yet... */ } gboolean @@ -551,12 +371,17 @@ modest_mail_operation_update_account (ModestMailOperation *self, /* Get subscribed folders & refresh them */ folders = TNY_LIST (tny_simple_list_new ()); - query = tny_folder_store_query_new (); - tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED); + query = NULL; //tny_folder_store_query_new (); + + /* FIXME: is this needed? */ +// tny_device_force_online (TNY_DEVICE(modest_runtime_get_device())); + + /* FIXME: let query be NULL: do it for all */ + //tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED); tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account), folders, update_folders_cb, query, self); - g_object_unref (query); - + //g_object_unref (query); /* FIXME */ + return TRUE; } @@ -649,54 +474,65 @@ modest_mail_operation_create_folder (ModestMailOperation *self, TnyFolderStore *parent, const gchar *name) { + ModestMailOperationPrivate *priv; + TnyFolder *new_folder = NULL; + //TnyStoreAccount *store_account; + g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL); g_return_val_if_fail (name, NULL); - TnyFolder *new_folder = NULL; - TnyStoreAccount *store_account; + priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self); /* Create the folder */ - new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */ - if (!new_folder) - return NULL; - - /* Subscribe to folder */ - if (!tny_folder_is_subscribed (new_folder)) { - store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent))); - tny_store_account_subscribe (store_account, new_folder); - g_object_unref (G_OBJECT (store_account)); - } + new_folder = tny_folder_store_create_folder (parent, name, &(priv->error)); + CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, return NULL); + +/* /\* Subscribe to folder *\/ */ +/* if (!tny_folder_is_subscribed (new_folder)) { */ +/* store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent))); */ +/* tny_store_account_subscribe (store_account, new_folder); */ +/* g_object_unref (G_OBJECT (store_account)); */ +/* } */ return new_folder; } void modest_mail_operation_remove_folder (ModestMailOperation *self, - TnyFolder *folder, - gboolean remove_to_trash) + TnyFolder *folder, + gboolean remove_to_trash) { - TnyFolderStore *folder_store; + TnyFolderStore *parent; + TnyAccount *account; + ModestMailOperationPrivate *priv; + g_return_if_fail (MODEST_IS_MAIL_OPERATION (self)); g_return_if_fail (TNY_IS_FOLDER (folder)); - /* Get folder store */ - folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder)); + priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self); + + /* Get the account */ + account = tny_folder_get_account (folder); /* Delete folder or move to trash */ if (remove_to_trash) { - TnyFolder *trash_folder; - trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(folder_store), + TnyFolder *trash_folder, *new_folder; + trash_folder = modest_tny_account_get_special_folder (account, TNY_FOLDER_TYPE_TRASH); /* TODO: error_handling */ - modest_mail_operation_move_folder (self, folder, - TNY_FOLDER_STORE (trash_folder)); + new_folder = modest_mail_operation_xfer_folder (self, folder, + TNY_FOLDER_STORE (trash_folder), TRUE); + g_object_unref (G_OBJECT (new_folder)); } else { - tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */ - g_object_unref (G_OBJECT (folder)); - } + parent = tny_folder_get_folder_store (folder); - /* Free instances */ - g_object_unref (G_OBJECT (folder_store)); + tny_folder_store_remove_folder (parent, folder, &(priv->error)); + CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, ); + + if (parent) + g_object_unref (G_OBJECT (parent)); + } + g_object_unref (G_OBJECT (account)); } void @@ -704,87 +540,42 @@ modest_mail_operation_rename_folder (ModestMailOperation *self, TnyFolder *folder, const gchar *name) { + ModestMailOperationPrivate *priv; + g_return_if_fail (MODEST_IS_MAIL_OPERATION (self)); g_return_if_fail (TNY_IS_FOLDER_STORE (folder)); g_return_if_fail (name); + priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self); + /* FIXME: better error handling */ if (strrchr (name, '/') != NULL) return; /* Rename. Camel handles folder subscription/unsubscription */ - tny_folder_set_name (folder, name, NULL); /* FIXME */ + tny_folder_set_name (folder, name, &(priv->error)); + CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, return); } -void -modest_mail_operation_move_folder (ModestMailOperation *self, - TnyFolder *folder, - TnyFolderStore *parent) -{ - g_return_if_fail (MODEST_IS_MAIL_OPERATION (self)); - g_return_if_fail (TNY_IS_FOLDER_STORE (parent)); - g_return_if_fail (TNY_IS_FOLDER (folder)); - - modest_mail_operation_xfer_folder (self, folder, parent, TRUE); -} - -void -modest_mail_operation_copy_folder (ModestMailOperation *self, - TnyFolder *folder, - TnyFolderStore *parent) -{ - g_return_if_fail (MODEST_IS_MAIL_OPERATION (self)); - g_return_if_fail (TNY_IS_FOLDER_STORE (parent)); - g_return_if_fail (TNY_IS_FOLDER (folder)); - - modest_mail_operation_xfer_folder (self, folder, parent, FALSE); -} - -static void +TnyFolder * modest_mail_operation_xfer_folder (ModestMailOperation *self, TnyFolder *folder, TnyFolderStore *parent, gboolean delete_original) { - const gchar *folder_name; - TnyFolder *dest_folder, *child; - TnyIterator *iter; - TnyList *folders, *headers; + ModestMailOperationPrivate *priv; - g_return_if_fail (TNY_IS_FOLDER (folder)); - g_return_if_fail (TNY_IS_FOLDER_STORE (parent)); - - /* Create the destination folder */ - folder_name = tny_folder_get_name (folder); - dest_folder = modest_mail_operation_create_folder (self, parent, folder_name); - - /* Transfer messages */ - headers = TNY_LIST (tny_simple_list_new ()); - tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */ - tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */ - - /* Recurse children */ - folders = TNY_LIST (tny_simple_list_new ()); - tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */ - iter = tny_list_create_iterator (folders); - - while (!tny_iterator_is_done (iter)) { - child = TNY_FOLDER (tny_iterator_get_current (iter)); - modest_mail_operation_xfer_folder (self, child, TNY_FOLDER_STORE (dest_folder), - delete_original); - tny_iterator_next (iter); - g_object_unref (G_OBJECT(child)); - } + g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL); + g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL); + g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL); - /* Delete source folder (if needed) */ - if (delete_original) - modest_mail_operation_remove_folder (self, folder, FALSE); + priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self); - /* Clean up */ - g_object_unref (G_OBJECT (dest_folder)); - g_object_unref (G_OBJECT (headers)); - g_object_unref (G_OBJECT (folders)); - g_object_unref (G_OBJECT (iter)); + return tny_folder_copy (folder, + parent, + tny_folder_get_name (folder), + delete_original, + &(priv->error)); } @@ -792,28 +583,6 @@ modest_mail_operation_xfer_folder (ModestMailOperation *self, /* ************************** MSG ACTIONS ************************* */ /* ******************************************************************* */ -gboolean -modest_mail_operation_copy_msg (ModestMailOperation *self, - TnyHeader *header, - TnyFolder *folder) -{ - g_return_val_if_fail (TNY_IS_HEADER (header), FALSE); - g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE); - - return modest_mail_operation_xfer_msg (self, header, folder, FALSE); -} - -gboolean -modest_mail_operation_move_msg (ModestMailOperation *self, - TnyHeader *header, - TnyFolder *folder) -{ - g_return_val_if_fail (TNY_IS_HEADER (header), FALSE); - g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE); - - return modest_mail_operation_xfer_msg (self, header, folder, TRUE); -} - void modest_mail_operation_remove_msg (ModestMailOperation *self, TnyHeader *header, @@ -834,7 +603,7 @@ modest_mail_operation_remove_msg (ModestMailOperation *self, trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account), TNY_FOLDER_TYPE_TRASH); if (trash_folder) { - modest_mail_operation_move_msg (self, header, trash_folder); + modest_mail_operation_xfer_msg (self, header, trash_folder, TRUE); /* g_object_unref (trash_folder); */ } else { ModestMailOperationPrivate *priv; @@ -863,13 +632,20 @@ transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data) ModestMailOperationPrivate *priv; priv = MODEST_MAIL_OPERATION_GET_PRIVATE(user_data); - priv->done = 1; - priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS; + + if (*err) { + priv->error = g_error_copy (*err); + priv->done = 0; + priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED; + } else { + priv->done = 1; + priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS; + } g_signal_emit (G_OBJECT (user_data), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL); } -static gboolean +gboolean modest_mail_operation_xfer_msg (ModestMailOperation *self, TnyHeader *header, TnyFolder *folder, @@ -879,6 +655,10 @@ modest_mail_operation_xfer_msg (ModestMailOperation *self, TnyFolder *src_folder; TnyList *headers; + g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE); + g_return_val_if_fail (TNY_IS_HEADER (header), FALSE); + g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE); + src_folder = tny_header_get_folder (header); headers = tny_simple_list_new (); @@ -889,119 +669,13 @@ modest_mail_operation_xfer_msg (ModestMailOperation *self, tny_list_prepend (headers, G_OBJECT (header)); tny_folder_transfer_msgs_async (src_folder, headers, folder, - delete_original, transfer_msgs_cb, self); + delete_original, transfer_msgs_cb, + g_object_ref(self)); /* Free */ - g_object_unref (headers); - g_object_unref (folder); - - return TRUE; -} + /* FIXME: don't free 'm yet */ + ///g_object_unref (headers); + ///g_object_unref (src_folder); - -/* ******************************************************************* */ -/* ************************* UTILIY FUNCTIONS ************************ */ -/* ******************************************************************* */ -static gboolean -is_ascii(const gchar *s) -{ - while (s[0]) { - if (s[0] & 128 || s[0] < 32) - return FALSE; - s++; - } return TRUE; } - -static char * -get_content_type(const gchar *s) -{ - GString *type; - - type = g_string_new("text/plain"); - if (!is_ascii(s)) { - if (g_utf8_validate(s, -1, NULL)) { - g_string_append(type, "; charset=\"utf-8\""); - } else { - /* it should be impossible to reach this, but better safe than sorry */ - g_warning("invalid utf8 in message"); - g_string_append(type, "; charset=\"latin1\""); - } - } - return g_string_free(type, FALSE); -} - -static void -add_attachments (TnyMsg *msg, GList *attachments_list) -{ - GList *pos; - TnyMimePart *attachment_part, *old_attachment; - const gchar *attachment_content_type; - const gchar *attachment_filename; - TnyStream *attachment_stream; - TnyPlatformFactory *fact; - - fact = modest_tny_platform_factory_get_instance (); - for (pos = (GList *)attachments_list; pos; pos = pos->next) { - - old_attachment = pos->data; - attachment_filename = tny_mime_part_get_filename (old_attachment); - attachment_stream = tny_mime_part_get_stream (old_attachment); - attachment_part = tny_platform_factory_new_mime_part (fact); - - attachment_content_type = tny_mime_part_get_content_type (old_attachment); - - tny_mime_part_construct_from_stream (attachment_part, - attachment_stream, - attachment_content_type); - tny_stream_reset (attachment_stream); - - tny_mime_part_set_filename (attachment_part, attachment_filename); - - tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part); -/* g_object_unref (attachment_part); */ - } -} - - -static TnyMimePart * -add_body_part (TnyMsg *msg, - const gchar *body, - const gchar *content_type, - gboolean has_attachments) -{ - TnyMimePart *text_body_part = NULL; - TnyStream *text_body_stream; - TnyPlatformFactory *fact; - - fact = modest_tny_platform_factory_get_instance (); - - /* Create the stream */ - text_body_stream = TNY_STREAM (tny_camel_stream_new - (camel_stream_mem_new_with_buffer - (body, strlen(body)))); - - /* Create body part if needed */ - if (has_attachments) - text_body_part = tny_platform_factory_new_mime_part (fact); - else - text_body_part = TNY_MIME_PART(msg); - - /* Construct MIME part */ - tny_stream_reset (text_body_stream); - tny_mime_part_construct_from_stream (text_body_part, - text_body_stream, - content_type); - tny_stream_reset (text_body_stream); - - /* Add part if needed */ - if (has_attachments) { - tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part); - g_object_unref (G_OBJECT(text_body_part)); - } - - /* Clean */ - g_object_unref (text_body_stream); - - return text_body_part; -}