1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "modest-mail-operation.h"
31 /* include other impl specific header files */
34 #include <tny-mime-part.h>
35 #include <tny-store-account.h>
36 #include <tny-folder-store.h>
37 #include <tny-folder-store-query.h>
38 #include <tny-camel-stream.h>
39 #include <tny-simple-list.h>
40 #include <camel/camel-stream-mem.h>
41 #include <glib/gi18n.h>
42 #include <modest-tny-account.h>
43 #include <modest-tny-send-queue.h>
44 #include <modest-runtime.h>
45 #include "modest-text-utils.h"
46 #include "modest-tny-msg.h"
47 #include "modest-tny-platform-factory.h"
48 #include "modest-marshal.h"
49 #include "modest-formatter.h"
50 #include "modest-error.h"
52 /* 'private'/'protected' functions */
53 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
54 static void modest_mail_operation_init (ModestMailOperation *obj);
55 static void modest_mail_operation_finalize (GObject *obj);
57 static void status_update_cb (TnyFolder *folder,
62 static void folder_refresh_cb (TnyFolder *folder,
66 static void update_folders_cb (TnyFolderStore *self,
70 static void add_attachments (TnyMsg *msg,
71 GList *attachments_list);
74 static TnyMimePart * add_body_part (TnyMsg *msg,
76 const gchar *content_type,
77 gboolean has_attachments);
80 static void modest_mail_operation_xfer_folder (ModestMailOperation *self,
82 TnyFolderStore *parent,
83 gboolean delete_original);
85 static gboolean modest_mail_operation_xfer_msg (ModestMailOperation *self,
88 gboolean delete_original);
90 enum _ModestMailOperationSignals
92 PROGRESS_CHANGED_SIGNAL,
97 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
98 struct _ModestMailOperationPrivate {
101 ModestMailOperationStatus status;
104 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
105 MODEST_TYPE_MAIL_OPERATION, \
106 ModestMailOperationPrivate))
108 typedef struct _RefreshFolderAsyncHelper
110 ModestMailOperation *mail_op;
115 } RefreshFolderAsyncHelper;
117 /* some utility functions */
118 static char * get_content_type(const gchar *s);
119 static gboolean is_ascii(const gchar *s);
122 static GObjectClass *parent_class = NULL;
124 static guint signals[NUM_SIGNALS] = {0};
127 modest_mail_operation_get_type (void)
129 static GType my_type = 0;
131 static const GTypeInfo my_info = {
132 sizeof(ModestMailOperationClass),
133 NULL, /* base init */
134 NULL, /* base finalize */
135 (GClassInitFunc) modest_mail_operation_class_init,
136 NULL, /* class finalize */
137 NULL, /* class data */
138 sizeof(ModestMailOperation),
140 (GInstanceInitFunc) modest_mail_operation_init,
143 my_type = g_type_register_static (G_TYPE_OBJECT,
144 "ModestMailOperation",
151 modest_mail_operation_class_init (ModestMailOperationClass *klass)
153 GObjectClass *gobject_class;
154 gobject_class = (GObjectClass*) klass;
156 parent_class = g_type_class_peek_parent (klass);
157 gobject_class->finalize = modest_mail_operation_finalize;
159 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
162 * ModestMailOperation::progress-changed
163 * @self: the #MailOperation that emits the signal
164 * @user_data: user data set when the signal handler was connected
166 * Emitted when the progress of a mail operation changes
168 signals[PROGRESS_CHANGED_SIGNAL] =
169 g_signal_new ("progress_changed",
170 G_TYPE_FROM_CLASS (gobject_class),
172 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
174 g_cclosure_marshal_VOID__VOID,
179 modest_mail_operation_init (ModestMailOperation *obj)
181 ModestMailOperationPrivate *priv;
183 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
185 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
192 modest_mail_operation_finalize (GObject *obj)
194 ModestMailOperationPrivate *priv;
196 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
199 g_error_free (priv->error);
203 G_OBJECT_CLASS(parent_class)->finalize (obj);
207 modest_mail_operation_new (void)
209 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
214 modest_mail_operation_send_mail (ModestMailOperation *self,
215 TnyTransportAccount *transport_account,
218 TnySendQueue *send_queue;
220 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
221 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
223 send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
224 if (!TNY_IS_SEND_QUEUE(send_queue))
225 g_printerr ("modest: could not find send queue for account\n");
227 tny_send_queue_add (send_queue, msg);
231 modest_mail_operation_send_new_mail (ModestMailOperation *self,
232 TnyTransportAccount *transport_account,
237 const gchar *subject,
239 const GList *attachments_list)
241 TnyPlatformFactory *fact;
245 ModestMailOperationPrivate *priv = NULL;
247 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
248 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
250 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
252 /* Check parametters */
254 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
255 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
256 _("Error trying to send a mail. You need to set almost one a recipient"));
261 fact = modest_tny_platform_factory_get_instance ();
262 new_msg = tny_platform_factory_new_msg (fact);
263 header = tny_platform_factory_new_header (fact);
265 /* WARNING: set the header before assign values to it */
266 tny_msg_set_header (new_msg, header);
267 tny_header_set_from (TNY_HEADER (header), from);
268 tny_header_set_replyto (TNY_HEADER (header), from);
269 tny_header_set_to (TNY_HEADER (header), to);
270 tny_header_set_cc (TNY_HEADER (header), cc);
271 tny_header_set_bcc (TNY_HEADER (header), bcc);
272 tny_header_set_subject (TNY_HEADER (header), subject);
274 content_type = get_content_type(body);
276 /* Add the body of the new mail */
277 add_body_part (new_msg, body, (const gchar *) content_type,
278 (attachments_list == NULL) ? FALSE : TRUE);
280 /* Add attachments */
281 add_attachments (new_msg, (GList*) attachments_list);
284 modest_mail_operation_send_mail (self, transport_account, new_msg);
287 g_object_unref (header);
288 g_object_unref (new_msg);
289 g_free(content_type);
293 add_if_attachment (gpointer data, gpointer user_data)
296 GList *attachments_list;
298 part = TNY_MIME_PART (data);
299 attachments_list = (GList *) user_data;
301 if (tny_mime_part_is_attachment (part))
302 attachments_list = g_list_prepend (attachments_list, part);
307 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
309 TnyPlatformFactory *fact;
311 TnyHeader *new_header, *header;
314 ModestFormatter *formatter;
316 /* Get body from original msg. Always look for the text/plain
317 part of the message to create the reply/forwarded mail */
318 header = tny_msg_get_header (msg);
319 body = modest_tny_msg_find_body_part (msg, FALSE);
321 /* TODO: select the formatter from account prefs */
322 formatter = modest_formatter_new ("text/plain");
324 /* Format message body */
327 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
329 new_msg = modest_formatter_cite (formatter, body, header);
331 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
332 new_msg = modest_formatter_quote (formatter, body, header);
337 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
339 new_msg = modest_formatter_inline (formatter, body, header);
341 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
342 new_msg = modest_formatter_attach (formatter, body, header);
346 g_object_unref (G_OBJECT(formatter));
347 g_object_unref (G_OBJECT(body));
349 /* Fill the header */
350 fact = modest_tny_platform_factory_get_instance ();
351 new_header = TNY_HEADER (tny_platform_factory_new_header (fact));
352 tny_msg_set_header (new_msg, new_header);
353 tny_header_set_from (new_header, from);
354 tny_header_set_replyto (new_header, from);
356 /* Change the subject */
358 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
359 (is_reply) ? _("Re:") : _("Fwd:"));
360 tny_header_set_subject (new_header, (const gchar *) new_subject);
361 g_free (new_subject);
364 g_object_unref (G_OBJECT (new_header));
365 g_object_unref (G_OBJECT (header));
371 modest_mail_operation_create_forward_mail (TnyMsg *msg,
373 ModestMailOperationForwardType forward_type)
376 TnyList *parts = NULL;
377 GList *attachments_list = NULL;
379 new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
381 /* Add attachments */
382 parts = TNY_LIST (tny_simple_list_new());
383 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
384 tny_list_foreach (parts, add_if_attachment, attachments_list);
385 add_attachments (new_msg, attachments_list);
388 if (attachments_list) g_list_free (attachments_list);
389 g_object_unref (G_OBJECT (parts));
395 modest_mail_operation_create_reply_mail (TnyMsg *msg,
397 ModestMailOperationReplyType reply_type,
398 ModestMailOperationReplyMode reply_mode)
400 TnyMsg *new_msg = NULL;
401 TnyHeader *new_header, *header;
402 const gchar* reply_to;
403 gchar *new_cc = NULL;
404 const gchar *cc = NULL, *bcc = NULL;
407 new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
409 /* Fill the header */
410 header = tny_msg_get_header (msg);
411 new_header = tny_msg_get_header (new_msg);
412 reply_to = tny_header_get_replyto (header);
415 tny_header_set_to (new_header, reply_to);
417 tny_header_set_to (new_header, tny_header_get_from (header));
419 switch (reply_mode) {
420 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
421 /* Do not fill neither cc nor bcc */
423 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
426 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
427 /* Concatenate to, cc and bcc */
428 cc = tny_header_get_cc (header);
429 bcc = tny_header_get_bcc (header);
431 tmp = g_string_new (tny_header_get_to (header));
432 if (cc) g_string_append_printf (tmp, ",%s",cc);
433 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
435 /* Remove my own address from the cc list. TODO:
436 remove also the To: of the new message, needed due
437 to the new reply_to feature */
439 modest_text_utils_remove_address ((const gchar *) tmp->str,
441 /* FIXME: remove also the mails from the new To: */
442 tny_header_set_cc (new_header, new_cc);
445 g_string_free (tmp, TRUE);
451 g_object_unref (G_OBJECT (new_header));
452 g_object_unref (G_OBJECT (header));
458 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data)
460 g_print ("%s status: %d, of total %d\n", what, status, oftotal);
464 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
466 ModestMailOperation *self = NULL;
467 ModestMailOperationPrivate *priv = NULL;
468 RefreshFolderAsyncHelper *helper;
470 helper = (RefreshFolderAsyncHelper *) user_data;
471 self = MODEST_MAIL_OPERATION (helper->mail_op);
472 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
474 if ((canceled && *err) || *err) {
475 priv->error = g_error_copy (*err);
477 } else if (canceled) {
479 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
480 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
481 _("Error trying to refresh folder %s. Operation canceled"),
482 tny_folder_get_name (folder));
487 if (priv->done == priv->total)
488 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
489 else if ((priv->done + helper->canceled + helper->failed) == priv->total) {
490 if (helper->failed == priv->total)
491 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
492 else if (helper->failed == priv->total)
493 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
495 priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
497 tny_iterator_next (helper->iter);
498 if (tny_iterator_is_done (helper->iter)) {
500 list = tny_iterator_get_list (helper->iter);
501 g_object_unref (G_OBJECT (helper->iter));
502 g_object_unref (G_OBJECT (list));
503 g_slice_free (RefreshFolderAsyncHelper, helper);
505 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (helper->iter));
506 tny_folder_refresh_async (folder, folder_refresh_cb,
509 g_object_unref (G_OBJECT(folder));
511 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
516 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
518 ModestMailOperation *self;
519 ModestMailOperationPrivate *priv;
520 RefreshFolderAsyncHelper *helper;
523 self = MODEST_MAIL_OPERATION (user_data);
524 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
527 priv->error = g_error_copy (*err);
528 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
532 priv->total = tny_list_get_length (list);
534 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
536 helper = g_slice_new0 (RefreshFolderAsyncHelper);
537 helper->mail_op = self;
538 helper->iter = tny_list_create_iterator (list);
540 helper->canceled = 0;
542 /* Async refresh folders */
543 folder = TNY_FOLDER (tny_iterator_get_current (helper->iter));
544 tny_folder_refresh_async (folder, folder_refresh_cb,
545 status_update_cb, helper);
546 g_object_unref (G_OBJECT(folder));
550 modest_mail_operation_update_account (ModestMailOperation *self,
551 TnyStoreAccount *store_account)
553 ModestMailOperationPrivate *priv;
555 TnyFolderStoreQuery *query;
557 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
558 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
560 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
562 /* Get subscribed folders & refresh them */
563 folders = TNY_LIST (tny_simple_list_new ());
564 query = tny_folder_store_query_new ();
565 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
566 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
567 folders, update_folders_cb, query, self);
568 g_object_unref (query);
573 ModestMailOperationStatus
574 modest_mail_operation_get_status (ModestMailOperation *self)
576 ModestMailOperationPrivate *priv;
578 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
579 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
580 MODEST_MAIL_OPERATION_STATUS_INVALID);
582 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
587 modest_mail_operation_get_error (ModestMailOperation *self)
589 ModestMailOperationPrivate *priv;
591 g_return_val_if_fail (self, NULL);
592 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
594 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
599 modest_mail_operation_cancel (ModestMailOperation *self)
606 modest_mail_operation_get_task_done (ModestMailOperation *self)
608 ModestMailOperationPrivate *priv;
610 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
612 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
617 modest_mail_operation_get_task_total (ModestMailOperation *self)
619 ModestMailOperationPrivate *priv;
621 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
623 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
628 modest_mail_operation_is_finished (ModestMailOperation *self)
630 ModestMailOperationPrivate *priv;
631 gboolean retval = FALSE;
633 if (!MODEST_IS_MAIL_OPERATION (self)) {
634 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
638 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
640 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
641 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
642 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
643 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
652 /* ******************************************************************* */
653 /* ************************** STORE ACTIONS ************************* */
654 /* ******************************************************************* */
658 modest_mail_operation_create_folder (ModestMailOperation *self,
659 TnyFolderStore *parent,
662 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
663 g_return_val_if_fail (name, NULL);
665 TnyFolder *new_folder = NULL;
666 TnyStoreAccount *store_account;
668 /* Create the folder */
669 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
673 /* Subscribe to folder */
674 if (!tny_folder_is_subscribed (new_folder)) {
675 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
676 tny_store_account_subscribe (store_account, new_folder);
677 g_object_unref (G_OBJECT (store_account));
684 modest_mail_operation_remove_folder (ModestMailOperation *self,
686 gboolean remove_to_trash)
688 TnyFolderStore *folder_store;
690 g_return_if_fail (TNY_IS_FOLDER (folder));
692 /* Get folder store */
693 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
695 /* Delete folder or move to trash */
696 if (remove_to_trash) {
697 TnyFolder *trash_folder;
698 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(folder_store),
699 TNY_FOLDER_TYPE_TRASH);
700 /* TODO: error_handling */
701 modest_mail_operation_move_folder (self, folder,
702 TNY_FOLDER_STORE (trash_folder));
704 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
705 g_object_unref (G_OBJECT (folder));
709 g_object_unref (G_OBJECT (folder_store));
713 modest_mail_operation_rename_folder (ModestMailOperation *self,
717 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
718 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
719 g_return_if_fail (name);
721 /* FIXME: better error handling */
722 if (strrchr (name, '/') != NULL)
725 /* Rename. Camel handles folder subscription/unsubscription */
726 tny_folder_set_name (folder, name, NULL); /* FIXME */
730 modest_mail_operation_move_folder (ModestMailOperation *self,
732 TnyFolderStore *parent)
734 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
735 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
736 g_return_if_fail (TNY_IS_FOLDER (folder));
738 modest_mail_operation_xfer_folder (self, folder, parent, TRUE);
742 modest_mail_operation_copy_folder (ModestMailOperation *self,
744 TnyFolderStore *parent)
746 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
747 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
748 g_return_if_fail (TNY_IS_FOLDER (folder));
750 modest_mail_operation_xfer_folder (self, folder, parent, FALSE);
754 modest_mail_operation_xfer_folder (ModestMailOperation *self,
756 TnyFolderStore *parent,
757 gboolean delete_original)
759 const gchar *folder_name;
760 TnyFolder *dest_folder, *child;
762 TnyList *folders, *headers;
764 g_return_if_fail (TNY_IS_FOLDER (folder));
765 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
767 /* Create the destination folder */
768 folder_name = tny_folder_get_name (folder);
769 dest_folder = modest_mail_operation_create_folder (self, parent, folder_name);
771 /* Transfer messages */
772 headers = TNY_LIST (tny_simple_list_new ());
773 tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
774 tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
776 /* Recurse children */
777 folders = TNY_LIST (tny_simple_list_new ());
778 tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
779 iter = tny_list_create_iterator (folders);
781 while (!tny_iterator_is_done (iter)) {
782 child = TNY_FOLDER (tny_iterator_get_current (iter));
783 modest_mail_operation_xfer_folder (self, child, TNY_FOLDER_STORE (dest_folder),
785 tny_iterator_next (iter);
786 g_object_unref (G_OBJECT(child));
789 /* Delete source folder (if needed) */
791 modest_mail_operation_remove_folder (self, folder, FALSE);
794 g_object_unref (G_OBJECT (dest_folder));
795 g_object_unref (G_OBJECT (headers));
796 g_object_unref (G_OBJECT (folders));
797 g_object_unref (G_OBJECT (iter));
801 /* ******************************************************************* */
802 /* ************************** MSG ACTIONS ************************* */
803 /* ******************************************************************* */
806 modest_mail_operation_copy_msg (ModestMailOperation *self,
810 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
811 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
813 return modest_mail_operation_xfer_msg (self, header, folder, FALSE);
817 modest_mail_operation_move_msg (ModestMailOperation *self,
821 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
822 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
824 return modest_mail_operation_xfer_msg (self, header, folder, TRUE);
828 modest_mail_operation_remove_msg (ModestMailOperation *self,
830 gboolean remove_to_trash)
834 g_return_if_fail (TNY_IS_HEADER (header));
836 folder = tny_header_get_folder (header);
838 /* Delete or move to trash */
839 if (remove_to_trash) {
840 TnyFolder *trash_folder;
841 TnyStoreAccount *store_account;
843 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
844 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
845 TNY_FOLDER_TYPE_TRASH);
847 modest_mail_operation_move_msg (self, header, trash_folder);
848 /* g_object_unref (trash_folder); */
850 ModestMailOperationPrivate *priv;
852 /* Set status failed and set an error */
853 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
854 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
855 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
856 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
857 _("Error trying to delete a message. Trash folder not found"));
860 g_object_unref (G_OBJECT (store_account));
862 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
863 tny_folder_sync(folder, TRUE, NULL); /* FIXME */
867 g_object_unref (folder);
871 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
873 ModestMailOperationPrivate *priv;
875 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(user_data);
877 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
879 g_signal_emit (G_OBJECT (user_data), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
883 modest_mail_operation_xfer_msg (ModestMailOperation *self,
886 gboolean delete_original)
888 ModestMailOperationPrivate *priv;
889 TnyFolder *src_folder;
892 src_folder = tny_header_get_folder (header);
893 headers = tny_simple_list_new ();
895 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
898 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
900 tny_list_prepend (headers, G_OBJECT (header));
901 tny_folder_transfer_msgs_async (src_folder, headers, folder,
902 delete_original, transfer_msgs_cb, self);
905 g_object_unref (headers);
906 g_object_unref (folder);
912 /* ******************************************************************* */
913 /* ************************* UTILIY FUNCTIONS ************************ */
914 /* ******************************************************************* */
916 is_ascii(const gchar *s)
919 if (s[0] & 128 || s[0] < 32)
927 get_content_type(const gchar *s)
931 type = g_string_new("text/plain");
933 if (g_utf8_validate(s, -1, NULL)) {
934 g_string_append(type, "; charset=\"utf-8\"");
936 /* it should be impossible to reach this, but better safe than sorry */
937 g_warning("invalid utf8 in message");
938 g_string_append(type, "; charset=\"latin1\"");
941 return g_string_free(type, FALSE);
945 add_attachments (TnyMsg *msg, GList *attachments_list)
948 TnyMimePart *attachment_part, *old_attachment;
949 const gchar *attachment_content_type;
950 const gchar *attachment_filename;
951 TnyStream *attachment_stream;
952 TnyPlatformFactory *fact;
954 fact = modest_tny_platform_factory_get_instance ();
955 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
957 old_attachment = pos->data;
958 attachment_filename = tny_mime_part_get_filename (old_attachment);
959 attachment_stream = tny_mime_part_get_stream (old_attachment);
960 attachment_part = tny_platform_factory_new_mime_part (fact);
962 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
964 tny_mime_part_construct_from_stream (attachment_part,
966 attachment_content_type);
967 tny_stream_reset (attachment_stream);
969 tny_mime_part_set_filename (attachment_part, attachment_filename);
971 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
972 /* g_object_unref (attachment_part); */
978 add_body_part (TnyMsg *msg,
980 const gchar *content_type,
981 gboolean has_attachments)
983 TnyMimePart *text_body_part = NULL;
984 TnyStream *text_body_stream;
985 TnyPlatformFactory *fact;
987 fact = modest_tny_platform_factory_get_instance ();
989 /* Create the stream */
990 text_body_stream = TNY_STREAM (tny_camel_stream_new
991 (camel_stream_mem_new_with_buffer
992 (body, strlen(body))));
994 /* Create body part if needed */
996 text_body_part = tny_platform_factory_new_mime_part (fact);
998 text_body_part = TNY_MIME_PART(msg);
1000 /* Construct MIME part */
1001 tny_stream_reset (text_body_stream);
1002 tny_mime_part_construct_from_stream (text_body_part,
1005 tny_stream_reset (text_body_stream);
1007 /* Add part if needed */
1008 if (has_attachments) {
1009 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1010 g_object_unref (G_OBJECT(text_body_part));
1014 g_object_unref (text_body_stream);
1016 return text_body_part;