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-text-utils.h"
44 #include "modest-tny-msg-actions.h"
45 #include "modest-tny-platform-factory.h"
46 #include "modest-marshal.h"
47 #include "modest-formatter.h"
48 #include "modest-error.h"
50 /* 'private'/'protected' functions */
51 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
52 static void modest_mail_operation_init (ModestMailOperation *obj);
53 static void modest_mail_operation_finalize (GObject *obj);
55 static void status_update_cb (TnyFolder *folder,
60 static void folder_refresh_cb (TnyFolder *folder,
64 static void update_folders_cb (TnyFolderStore *self,
68 static void add_attachments (TnyMsg *msg,
69 GList *attachments_list);
72 static TnyMimePart * add_body_part (TnyMsg *msg,
74 const gchar *content_type,
75 gboolean has_attachments);
78 static void modest_mail_operation_xfer_folder (ModestMailOperation *self,
80 TnyFolderStore *parent,
81 gboolean delete_original);
83 static gboolean modest_mail_operation_xfer_msg (ModestMailOperation *self,
86 gboolean delete_original);
88 enum _ModestMailOperationSignals
90 PROGRESS_CHANGED_SIGNAL,
95 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
96 struct _ModestMailOperationPrivate {
99 ModestMailOperationStatus status;
102 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
103 MODEST_TYPE_MAIL_OPERATION, \
104 ModestMailOperationPrivate))
106 typedef struct _RefreshFolderAsyncHelper
108 ModestMailOperation *mail_op;
113 } RefreshFolderAsyncHelper;
115 /* some utility functions */
116 static char * get_content_type(const gchar *s);
117 static gboolean is_ascii(const gchar *s);
120 static GObjectClass *parent_class = NULL;
122 static guint signals[NUM_SIGNALS] = {0};
125 modest_mail_operation_get_type (void)
127 static GType my_type = 0;
129 static const GTypeInfo my_info = {
130 sizeof(ModestMailOperationClass),
131 NULL, /* base init */
132 NULL, /* base finalize */
133 (GClassInitFunc) modest_mail_operation_class_init,
134 NULL, /* class finalize */
135 NULL, /* class data */
136 sizeof(ModestMailOperation),
138 (GInstanceInitFunc) modest_mail_operation_init,
141 my_type = g_type_register_static (G_TYPE_OBJECT,
142 "ModestMailOperation",
149 modest_mail_operation_class_init (ModestMailOperationClass *klass)
151 GObjectClass *gobject_class;
152 gobject_class = (GObjectClass*) klass;
154 parent_class = g_type_class_peek_parent (klass);
155 gobject_class->finalize = modest_mail_operation_finalize;
157 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
160 * ModestMailOperation::progress-changed
161 * @self: the #MailOperation that emits the signal
162 * @user_data: user data set when the signal handler was connected
164 * Emitted when the progress of a mail operation changes
166 signals[PROGRESS_CHANGED_SIGNAL] =
167 g_signal_new ("progress_changed",
168 G_TYPE_FROM_CLASS (gobject_class),
170 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
172 g_cclosure_marshal_VOID__VOID,
177 modest_mail_operation_init (ModestMailOperation *obj)
179 ModestMailOperationPrivate *priv;
181 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
183 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
190 modest_mail_operation_finalize (GObject *obj)
192 ModestMailOperationPrivate *priv;
194 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
197 g_error_free (priv->error);
201 G_OBJECT_CLASS(parent_class)->finalize (obj);
205 modest_mail_operation_new (void)
207 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
212 modest_mail_operation_send_mail (ModestMailOperation *self,
213 TnyTransportAccount *transport_account,
216 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
217 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
219 tny_transport_account_send (transport_account, msg, NULL); /* FIXME */
223 modest_mail_operation_send_new_mail (ModestMailOperation *self,
224 TnyTransportAccount *transport_account,
229 const gchar *subject,
231 const GList *attachments_list)
233 TnyPlatformFactory *fact;
237 ModestMailOperationPrivate *priv = NULL;
239 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
240 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
242 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
244 /* Check parametters */
246 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
247 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
248 _("Error trying to send a mail. You need to set almost one a recipient"));
253 fact = modest_tny_platform_factory_get_instance ();
254 new_msg = tny_platform_factory_new_msg (fact);
255 header = tny_platform_factory_new_header (fact);
257 /* WARNING: set the header before assign values to it */
258 tny_msg_set_header (new_msg, header);
259 tny_header_set_from (TNY_HEADER (header), from);
260 tny_header_set_replyto (TNY_HEADER (header), from);
261 tny_header_set_to (TNY_HEADER (header), to);
262 tny_header_set_cc (TNY_HEADER (header), cc);
263 tny_header_set_bcc (TNY_HEADER (header), bcc);
264 tny_header_set_subject (TNY_HEADER (header), subject);
266 content_type = get_content_type(body);
268 /* Add the body of the new mail */
269 add_body_part (new_msg, body, (const gchar *) content_type,
270 (attachments_list == NULL) ? FALSE : TRUE);
272 /* Add attachments */
273 add_attachments (new_msg, (GList*) attachments_list);
276 tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */
279 g_object_unref (header);
280 g_object_unref (new_msg);
281 g_free(content_type);
285 add_if_attachment (gpointer data, gpointer user_data)
288 GList *attachments_list;
290 part = TNY_MIME_PART (data);
291 attachments_list = (GList *) user_data;
293 if (tny_mime_part_is_attachment (part))
294 attachments_list = g_list_prepend (attachments_list, part);
299 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
301 TnyPlatformFactory *fact;
303 TnyHeader *new_header, *header;
306 ModestFormatter *formatter;
308 /* Get body from original msg. Always look for the text/plain
309 part of the message to create the reply/forwarded mail */
310 header = tny_msg_get_header (msg);
311 body = modest_tny_msg_actions_find_body_part (msg, FALSE);
313 /* TODO: select the formatter from account prefs */
314 formatter = modest_formatter_new ("text/plain");
316 /* Format message body */
319 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
321 new_msg = modest_formatter_cite (formatter, body, header);
323 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
324 new_msg = modest_formatter_quote (formatter, body, header);
329 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
331 new_msg = modest_formatter_inline (formatter, body, header);
333 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
334 new_msg = modest_formatter_attach (formatter, body, header);
338 g_object_unref (G_OBJECT (formatter));
340 /* Fill the header */
341 fact = modest_tny_platform_factory_get_instance ();
342 new_header = TNY_HEADER (tny_platform_factory_new_header (fact));
343 tny_msg_set_header (new_msg, new_header);
344 tny_header_set_from (new_header, from);
345 tny_header_set_replyto (new_header, from);
347 /* Change the subject */
349 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
350 (is_reply) ? _("Re:") : _("Fwd:"));
351 tny_header_set_subject (new_header, (const gchar *) new_subject);
352 g_free (new_subject);
355 g_object_unref (G_OBJECT (new_header));
356 g_object_unref (G_OBJECT (header));
362 modest_mail_operation_create_forward_mail (TnyMsg *msg,
364 ModestMailOperationForwardType forward_type)
367 TnyList *parts = NULL;
368 GList *attachments_list = NULL;
370 new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
372 /* Add attachments */
373 parts = TNY_LIST (tny_simple_list_new());
374 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
375 tny_list_foreach (parts, add_if_attachment, attachments_list);
376 add_attachments (new_msg, attachments_list);
379 if (attachments_list) g_list_free (attachments_list);
380 g_object_unref (G_OBJECT (parts));
386 modest_mail_operation_create_reply_mail (TnyMsg *msg,
388 ModestMailOperationReplyType reply_type,
389 ModestMailOperationReplyMode reply_mode)
391 TnyMsg *new_msg = NULL;
392 TnyHeader *new_header, *header;
393 const gchar* reply_to;
394 gchar *new_cc = NULL;
395 const gchar *cc = NULL, *bcc = NULL;
398 new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
400 /* Fill the header */
401 header = tny_msg_get_header (msg);
402 new_header = tny_msg_get_header (new_msg);
403 reply_to = tny_header_get_replyto (header);
405 /* TODO: tinymail returns Invalid and it should return NULL */
406 if (strcmp (reply_to, "Invalid"))
407 tny_header_set_to (new_header, reply_to);
409 tny_header_set_to (new_header, tny_header_get_from (header));
411 switch (reply_mode) {
412 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
413 /* Do not fill neither cc nor bcc */
415 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
418 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
419 /* Concatenate to, cc and bcc */
420 cc = tny_header_get_cc (header);
421 bcc = tny_header_get_bcc (header);
423 tmp = g_string_new (tny_header_get_to (header));
424 if (cc) g_string_append_printf (tmp, ",%s",cc);
425 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
427 /* Remove my own address from the cc list. TODO:
428 remove also the To: of the new message, needed due
429 to the new reply_to feature */
431 modest_text_utils_remove_address ((const gchar *) tmp->str,
433 /* FIXME: remove also the mails from the new To: */
434 tny_header_set_cc (new_header, new_cc);
437 g_string_free (tmp, TRUE);
443 g_object_unref (G_OBJECT (new_header));
444 g_object_unref (G_OBJECT (header));
450 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data)
452 g_print ("%s status: %d, of total %d\n", what, status, oftotal);
456 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
458 ModestMailOperation *self = NULL;
459 ModestMailOperationPrivate *priv = NULL;
460 RefreshFolderAsyncHelper *helper;
462 helper = (RefreshFolderAsyncHelper *) user_data;
463 self = MODEST_MAIL_OPERATION (helper->mail_op);
464 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
466 if ((canceled && *err) || *err) {
467 priv->error = g_error_copy (*err);
469 } else if (canceled) {
471 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
472 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
473 _("Error trying to refresh folder %s. Operation canceled"),
474 tny_folder_get_name (folder));
479 if (priv->done == priv->total)
480 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
481 else if ((priv->done + helper->canceled + helper->failed) == priv->total) {
482 if (helper->failed == priv->total)
483 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
484 else if (helper->failed == priv->total)
485 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
487 priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
489 tny_iterator_next (helper->iter);
490 if (tny_iterator_is_done (helper->iter)) {
492 list = tny_iterator_get_list (helper->iter);
493 g_object_unref (G_OBJECT (helper->iter));
494 g_object_unref (G_OBJECT (list));
495 g_slice_free (RefreshFolderAsyncHelper, helper);
497 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
502 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
507 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
509 ModestMailOperation *self;
510 ModestMailOperationPrivate *priv;
511 RefreshFolderAsyncHelper *helper;
513 self = MODEST_MAIL_OPERATION (user_data);
514 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
517 priv->error = g_error_copy (*err);
518 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
522 priv->total = tny_list_get_length (list);
524 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
526 helper = g_slice_new0 (RefreshFolderAsyncHelper);
527 helper->mail_op = self;
528 helper->iter = tny_list_create_iterator (list);
530 helper->canceled = 0;
532 /* Async refresh folders */
533 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
540 modest_mail_operation_update_account (ModestMailOperation *self,
541 TnyStoreAccount *store_account)
543 ModestMailOperationPrivate *priv;
545 TnyFolderStoreQuery *query;
547 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
548 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
550 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
552 /* Get subscribed folders & refresh them */
553 folders = TNY_LIST (tny_simple_list_new ());
554 query = tny_folder_store_query_new ();
555 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
556 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
557 folders, update_folders_cb, query, self);
558 g_object_unref (query);
563 ModestMailOperationStatus
564 modest_mail_operation_get_status (ModestMailOperation *self)
566 ModestMailOperationPrivate *priv;
568 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
569 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
570 MODEST_MAIL_OPERATION_STATUS_INVALID);
572 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
577 modest_mail_operation_get_error (ModestMailOperation *self)
579 ModestMailOperationPrivate *priv;
581 g_return_val_if_fail (self, NULL);
582 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
584 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
589 modest_mail_operation_cancel (ModestMailOperation *self)
596 modest_mail_operation_get_task_done (ModestMailOperation *self)
598 ModestMailOperationPrivate *priv;
600 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
602 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
607 modest_mail_operation_get_task_total (ModestMailOperation *self)
609 ModestMailOperationPrivate *priv;
611 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
613 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
618 modest_mail_operation_is_finished (ModestMailOperation *self)
620 ModestMailOperationPrivate *priv;
621 gboolean retval = FALSE;
623 if (!MODEST_IS_MAIL_OPERATION (self)) {
624 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
628 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
630 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
631 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
632 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
633 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
642 /* ******************************************************************* */
643 /* ************************** STORE ACTIONS ************************* */
644 /* ******************************************************************* */
648 modest_mail_operation_create_folder (ModestMailOperation *self,
649 TnyFolderStore *parent,
652 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
653 g_return_val_if_fail (name, NULL);
655 TnyFolder *new_folder = NULL;
656 TnyStoreAccount *store_account;
658 /* Create the folder */
659 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
663 /* Subscribe to folder */
664 if (!tny_folder_is_subscribed (new_folder)) {
665 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
666 tny_store_account_subscribe (store_account, new_folder);
667 g_object_unref (G_OBJECT (store_account));
674 modest_mail_operation_remove_folder (ModestMailOperation *self,
676 gboolean remove_to_trash)
678 TnyFolderStore *folder_store;
680 g_return_if_fail (TNY_IS_FOLDER (folder));
682 /* Get folder store */
683 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
685 /* Delete folder or move to trash */
686 if (remove_to_trash) {
687 TnyFolder *trash_folder;
688 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(folder_store),
689 TNY_FOLDER_TYPE_TRASH);
691 /* TODO: error_handling */
692 modest_mail_operation_move_folder (self,
694 TNY_FOLDER_STORE (trash_folder));
696 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
697 g_object_unref (G_OBJECT (folder));
701 g_object_unref (G_OBJECT (folder_store));
705 modest_mail_operation_rename_folder (ModestMailOperation *self,
709 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
710 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
711 g_return_if_fail (name);
713 /* FIXME: better error handling */
714 if (strrchr (name, '/') != NULL)
717 /* Rename. Camel handles folder subscription/unsubscription */
718 tny_folder_set_name (folder, name, NULL); /* FIXME */
722 modest_mail_operation_move_folder (ModestMailOperation *self,
724 TnyFolderStore *parent)
726 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
727 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
728 g_return_if_fail (TNY_IS_FOLDER (folder));
730 modest_mail_operation_xfer_folder (self, folder, parent, TRUE);
734 modest_mail_operation_copy_folder (ModestMailOperation *self,
736 TnyFolderStore *parent)
738 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
739 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
740 g_return_if_fail (TNY_IS_FOLDER (folder));
742 modest_mail_operation_xfer_folder (self, folder, parent, FALSE);
746 modest_mail_operation_xfer_folder (ModestMailOperation *self,
748 TnyFolderStore *parent,
749 gboolean delete_original)
751 const gchar *folder_name;
752 TnyFolder *dest_folder, *child;
754 TnyList *folders, *headers;
756 g_return_if_fail (TNY_IS_FOLDER (folder));
757 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
759 /* Create the destination folder */
760 folder_name = tny_folder_get_name (folder);
761 dest_folder = modest_mail_operation_create_folder (self,
762 parent, folder_name);
764 /* Transfer messages */
765 headers = TNY_LIST (tny_simple_list_new ());
766 tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
767 tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
769 /* Recurse children */
770 folders = TNY_LIST (tny_simple_list_new ());
771 tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
772 iter = tny_list_create_iterator (folders);
774 while (!tny_iterator_is_done (iter)) {
776 child = TNY_FOLDER (tny_iterator_get_current (iter));
777 modest_mail_operation_xfer_folder (self, child,
778 TNY_FOLDER_STORE (dest_folder),
780 tny_iterator_next (iter);
783 /* Delete source folder (if needed) */
785 modest_mail_operation_remove_folder (self, folder, FALSE);
788 g_object_unref (G_OBJECT (dest_folder));
789 g_object_unref (G_OBJECT (headers));
790 g_object_unref (G_OBJECT (folders));
791 g_object_unref (G_OBJECT (iter));
795 /* ******************************************************************* */
796 /* ************************** MSG ACTIONS ************************* */
797 /* ******************************************************************* */
800 modest_mail_operation_copy_msg (ModestMailOperation *self,
804 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
805 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
807 return modest_mail_operation_xfer_msg (self, header, folder, FALSE);
811 modest_mail_operation_move_msg (ModestMailOperation *self,
815 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
816 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
818 return modest_mail_operation_xfer_msg (self, header, folder, TRUE);
822 modest_mail_operation_remove_msg (ModestMailOperation *self,
824 gboolean remove_to_trash)
828 g_return_if_fail (TNY_IS_HEADER (header));
830 folder = tny_header_get_folder (header);
832 /* Delete or move to trash */
833 if (remove_to_trash) {
834 TnyFolder *trash_folder;
835 TnyStoreAccount *store_account;
837 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
838 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
839 TNY_FOLDER_TYPE_TRASH);
841 modest_mail_operation_move_msg (self, header, trash_folder);
842 /* g_object_unref (trash_folder); */
844 ModestMailOperationPrivate *priv;
846 /* Set status failed and set an error */
847 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
848 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
849 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
850 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
851 _("Error trying to delete a message. Trash folder not found"));
854 g_object_unref (G_OBJECT (store_account));
856 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
857 tny_folder_sync(folder, TRUE, NULL); /* FIXME */
861 g_object_unref (folder);
865 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
867 ModestMailOperationPrivate *priv;
869 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(user_data);
871 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
873 g_signal_emit (G_OBJECT (user_data), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
877 modest_mail_operation_xfer_msg (ModestMailOperation *self,
880 gboolean delete_original)
882 ModestMailOperationPrivate *priv;
883 TnyFolder *src_folder;
886 src_folder = tny_header_get_folder (header);
887 headers = tny_simple_list_new ();
889 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
892 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
894 tny_list_prepend (headers, G_OBJECT (header));
895 tny_folder_transfer_msgs_async (src_folder, headers, folder,
896 delete_original, transfer_msgs_cb, self);
899 g_object_unref (headers);
900 g_object_unref (folder);
906 /* ******************************************************************* */
907 /* ************************* UTILIY FUNCTIONS ************************ */
908 /* ******************************************************************* */
910 is_ascii(const gchar *s)
913 if (s[0] & 128 || s[0] < 32)
921 get_content_type(const gchar *s)
925 type = g_string_new("text/plain");
927 if (g_utf8_validate(s, -1, NULL)) {
928 g_string_append(type, "; charset=\"utf-8\"");
930 /* it should be impossible to reach this, but better safe than sorry */
931 g_warning("invalid utf8 in message");
932 g_string_append(type, "; charset=\"latin1\"");
935 return g_string_free(type, FALSE);
939 add_attachments (TnyMsg *msg, GList *attachments_list)
942 TnyMimePart *attachment_part, *old_attachment;
943 const gchar *attachment_content_type;
944 const gchar *attachment_filename;
945 TnyStream *attachment_stream;
946 TnyPlatformFactory *fact;
948 fact = modest_tny_platform_factory_get_instance ();
949 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
951 old_attachment = pos->data;
952 attachment_filename = tny_mime_part_get_filename (old_attachment);
953 attachment_stream = tny_mime_part_get_stream (old_attachment);
954 attachment_part = tny_platform_factory_new_mime_part (fact);
956 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
958 tny_mime_part_construct_from_stream (attachment_part,
960 attachment_content_type);
961 tny_stream_reset (attachment_stream);
963 tny_mime_part_set_filename (attachment_part, attachment_filename);
965 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
966 /* g_object_unref (attachment_part); */
972 add_body_part (TnyMsg *msg,
974 const gchar *content_type,
975 gboolean has_attachments)
977 TnyMimePart *text_body_part = NULL;
978 TnyStream *text_body_stream;
979 TnyPlatformFactory *fact;
981 fact = modest_tny_platform_factory_get_instance ();
983 /* Create the stream */
984 text_body_stream = TNY_STREAM (tny_camel_stream_new
985 (camel_stream_mem_new_with_buffer
986 (body, strlen(body))));
988 /* Create body part if needed */
990 text_body_part = tny_platform_factory_new_mime_part (fact);
992 text_body_part = TNY_MIME_PART(msg);
994 /* Construct MIME part */
995 tny_stream_reset (text_body_stream);
996 tny_mime_part_construct_from_stream (text_body_part,
999 tny_stream_reset (text_body_stream);
1001 /* Add part if needed */
1002 if (has_attachments) {
1003 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1004 g_object_unref (G_OBJECT(text_body_part));
1008 g_object_unref (text_body_stream);
1010 return text_body_part;