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-msg.h>
39 #include <tny-camel-header.h>
40 #include <tny-camel-stream.h>
41 #include <tny-camel-mime-part.h>
42 #include <tny-simple-list.h>
43 #include <camel/camel-stream-mem.h>
44 #include <glib/gi18n.h>
46 #include "modest-text-utils.h"
47 #include "modest-tny-msg-actions.h"
48 #include "modest-tny-platform-factory.h"
49 #include "modest-marshal.h"
50 #include "modest-formatter.h"
51 #include "modest-error.h"
53 /* 'private'/'protected' functions */
54 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
55 static void modest_mail_operation_init (ModestMailOperation *obj);
56 static void modest_mail_operation_finalize (GObject *obj);
58 static void status_update_cb (TnyFolder *folder,
63 static void folder_refresh_cb (TnyFolder *folder,
67 static void update_folders_cb (TnyFolderStore *self,
71 static void add_attachments (TnyMsg *msg,
72 GList *attachments_list);
75 static TnyMimePart * add_body_part (TnyMsg *msg,
77 const gchar *content_type,
78 gboolean has_attachments);
81 static void modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
83 TnyFolderStore *parent,
84 gboolean delete_original);
86 static void modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
89 gboolean delete_original);
91 static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
92 TnyStoreAccount *store_account);
95 enum _ModestMailOperationSignals
97 PROGRESS_CHANGED_SIGNAL,
102 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
103 struct _ModestMailOperationPrivate {
106 ModestMailOperationStatus status;
109 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
110 MODEST_TYPE_MAIL_OPERATION, \
111 ModestMailOperationPrivate))
113 typedef struct _RefreshFolderAsyncHelper
115 ModestMailOperation *mail_op;
120 } RefreshFolderAsyncHelper;
122 /* some utility functions */
123 static char * get_content_type(const gchar *s);
124 static gboolean is_ascii(const gchar *s);
127 static GObjectClass *parent_class = NULL;
129 static guint signals[NUM_SIGNALS] = {0};
132 modest_mail_operation_get_type (void)
134 static GType my_type = 0;
136 static const GTypeInfo my_info = {
137 sizeof(ModestMailOperationClass),
138 NULL, /* base init */
139 NULL, /* base finalize */
140 (GClassInitFunc) modest_mail_operation_class_init,
141 NULL, /* class finalize */
142 NULL, /* class data */
143 sizeof(ModestMailOperation),
145 (GInstanceInitFunc) modest_mail_operation_init,
148 my_type = g_type_register_static (G_TYPE_OBJECT,
149 "ModestMailOperation",
156 modest_mail_operation_class_init (ModestMailOperationClass *klass)
158 GObjectClass *gobject_class;
159 gobject_class = (GObjectClass*) klass;
161 parent_class = g_type_class_peek_parent (klass);
162 gobject_class->finalize = modest_mail_operation_finalize;
164 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
166 /* signal definitions go here, e.g.: */
167 signals[PROGRESS_CHANGED_SIGNAL] =
168 g_signal_new ("progress_changed",
169 G_TYPE_FROM_CLASS (gobject_class),
171 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
173 g_cclosure_marshal_VOID__VOID,
178 modest_mail_operation_init (ModestMailOperation *obj)
180 ModestMailOperationPrivate *priv;
182 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
184 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
191 modest_mail_operation_finalize (GObject *obj)
193 ModestMailOperationPrivate *priv;
195 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
198 g_error_free (priv->error);
202 G_OBJECT_CLASS(parent_class)->finalize (obj);
206 modest_mail_operation_new (void)
208 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
213 modest_mail_operation_send_mail (ModestMailOperation *mail_op,
214 TnyTransportAccount *transport_account,
217 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
218 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
220 tny_transport_account_send (transport_account, msg, NULL); /* FIXME */
224 modest_mail_operation_send_new_mail (ModestMailOperation *mail_op,
225 TnyTransportAccount *transport_account,
230 const gchar *subject,
232 const GList *attachments_list)
237 ModestMailOperationPrivate *priv = NULL;
239 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
240 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
242 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
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 new_msg = TNY_MSG (tny_camel_msg_new ());
254 header = TNY_HEADER (tny_camel_header_new ());
256 /* WARNING: set the header before assign values to it */
257 tny_msg_set_header (new_msg, header);
258 tny_header_set_from (TNY_HEADER (header), from);
259 tny_header_set_replyto (TNY_HEADER (header), from);
260 tny_header_set_to (TNY_HEADER (header), to);
261 tny_header_set_cc (TNY_HEADER (header), cc);
262 tny_header_set_bcc (TNY_HEADER (header), bcc);
263 tny_header_set_subject (TNY_HEADER (header), subject);
265 content_type = get_content_type(body);
267 /* Add the body of the new mail */
268 add_body_part (new_msg, body, (const gchar *) content_type,
269 (attachments_list == NULL) ? FALSE : TRUE);
271 /* Add attachments */
272 add_attachments (new_msg, (GList*) attachments_list);
275 tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */
278 g_object_unref (header);
279 g_object_unref (new_msg);
280 g_free(content_type);
284 add_if_attachment (gpointer data, gpointer user_data)
287 GList *attachments_list;
289 part = TNY_MIME_PART (data);
290 attachments_list = (GList *) user_data;
292 if (tny_mime_part_is_attachment (part))
293 attachments_list = g_list_prepend (attachments_list, part);
298 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
301 TnyHeader *new_header, *header;
304 ModestFormatter *formatter;
306 /* Get body from original msg */
307 header = tny_msg_get_header (msg);
308 body = modest_tny_msg_actions_find_body_part (msg, TRUE);
310 /* TODO: select the formatter from account prefs */
311 formatter = modest_formatter_new ("text/plain");
313 /* Format message body */
316 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
318 new_msg = modest_formatter_cite (formatter, body, header);
320 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
321 new_msg = modest_formatter_quote (formatter, body, header);
326 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
328 new_msg = modest_formatter_inline (formatter, body, header);
330 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
331 new_msg = modest_formatter_attach (formatter, body, header);
335 g_object_unref (G_OBJECT (formatter));
337 /* Fill the header */
338 new_header = TNY_HEADER (tny_camel_header_new ());
339 tny_msg_set_header (new_msg, new_header);
340 tny_header_set_from (new_header, from);
341 tny_header_set_replyto (new_header, from);
343 /* Change the subject */
344 new_subject = (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
345 (is_reply) ? _("Re:") : _("Fwd:"));
346 tny_header_set_subject (new_header, (const gchar *) new_subject);
347 g_free (new_subject);
350 g_object_unref (G_OBJECT (new_header));
351 g_object_unref (G_OBJECT (header));
357 * modest_mail_operation_create_forward_mail:
358 * @msg: a valid #TnyMsg instance
359 * @forward_type: the type of forwarded message
361 * creates a forwarded message from an existing one
363 * Returns: a new #TnyMsg, or NULL in case of error
366 modest_mail_operation_create_forward_mail (TnyMsg *msg,
368 ModestMailOperationForwardType forward_type)
371 TnyList *parts = NULL;
372 GList *attachments_list = NULL;
374 new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
376 /* Add attachments */
377 parts = TNY_LIST (tny_simple_list_new());
378 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
379 tny_list_foreach (parts, add_if_attachment, attachments_list);
380 add_attachments (new_msg, attachments_list);
383 if (attachments_list) g_list_free (attachments_list);
384 g_object_unref (G_OBJECT (parts));
390 * modest_mail_operation_create_reply_mail:
391 * @msg: a valid #TnyMsg instance
392 * @reply_type: the format of the new message
393 * @reply_mode: the mode of reply, to the sender only, to a mail list or to all
395 * creates a new message to reply to an existing one
397 * Returns: Returns: a new #TnyMsg, or NULL in case of error
400 modest_mail_operation_create_reply_mail (TnyMsg *msg,
402 ModestMailOperationReplyType reply_type,
403 ModestMailOperationReplyMode reply_mode)
406 TnyHeader *new_header, *header;
407 const gchar* reply_to;
409 new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
411 /* Fill the header */
412 header = tny_msg_get_header (msg);
413 new_header = tny_msg_get_header (new_msg);
414 reply_to = tny_header_get_replyto (header);
416 tny_header_set_to (new_header, reply_to);
418 tny_header_set_to (new_header, tny_header_get_from (header));
420 switch (reply_mode) {
421 gchar *new_cc = NULL;
422 const gchar *cc = NULL, *bcc = NULL;
425 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
426 /* Do not fill neither cc nor bcc */
428 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
431 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
432 /* Concatenate to, cc and bcc */
433 cc = tny_header_get_cc (header);
434 bcc = tny_header_get_bcc (header);
436 tmp = g_string_new (tny_header_get_to (header));
437 if (cc) g_string_append_printf (tmp, ",%s",cc);
438 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
440 /* Remove my own address from the cc list. TODO:
441 remove also the To: of the new message, needed due
442 to the new reply_to feature */
444 modest_text_utils_remove_address ((const gchar *) tmp->str,
446 /* FIXME: remove also the mails from the new To: */
447 tny_header_set_cc (new_header, new_cc);
450 g_string_free (tmp, TRUE);
456 g_object_unref (G_OBJECT (new_header));
457 g_object_unref (G_OBJECT (header));
463 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data)
465 g_print ("%s status: %d, of total %d\n", what, status, oftotal);
469 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
471 ModestMailOperation *mail_op = NULL;
472 ModestMailOperationPrivate *priv = NULL;
473 RefreshFolderAsyncHelper *helper;
475 helper = (RefreshFolderAsyncHelper *) user_data;
476 mail_op = MODEST_MAIL_OPERATION (helper->mail_op);
477 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
479 if ((canceled && *err) || *err) {
480 priv->error = g_error_copy (*err);
482 } else if (canceled) {
484 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
485 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
486 _("Error trying to refresh folder %s. Operation canceled"),
487 tny_folder_get_name (folder));
492 if (priv->done == priv->total)
493 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
494 else if ((priv->done + helper->canceled + helper->failed) == priv->total)
495 if (helper->failed == priv->total)
496 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
497 else if (helper->failed == priv->total)
498 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
500 priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
502 tny_iterator_next (helper->iter);
503 if (tny_iterator_is_done (helper->iter)) {
505 list = tny_iterator_get_list (helper->iter);
506 g_object_unref (G_OBJECT (helper->iter));
507 g_object_unref (G_OBJECT (list));
508 g_slice_free (RefreshFolderAsyncHelper, helper);
510 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
515 g_signal_emit (G_OBJECT (mail_op), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
520 update_folders_cb (TnyFolderStore *self, TnyList *list, GError **err, gpointer user_data)
522 ModestMailOperation *mail_op;
523 ModestMailOperationPrivate *priv;
525 RefreshFolderAsyncHelper *helper;
527 mail_op = MODEST_MAIL_OPERATION (user_data);
528 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
530 priv->total = tny_list_get_length (list);
532 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
534 helper = g_slice_new0 (RefreshFolderAsyncHelper);
535 helper->mail_op = mail_op;
536 helper->iter = tny_list_create_iterator (list);
538 helper->canceled = 0;
540 /* Async refresh folders */
541 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
548 modest_mail_operation_update_account (ModestMailOperation *mail_op,
549 TnyStoreAccount *store_account)
551 ModestMailOperationPrivate *priv;
553 TnyFolderStoreQuery *query;
555 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), FALSE);
556 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
558 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
560 /* Get subscribed folders & refresh them */
561 folders = TNY_LIST (tny_simple_list_new ());
562 query = tny_folder_store_query_new ();
563 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
564 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
565 folders, update_folders_cb, query, mail_op);
566 g_object_unref (query);
571 ModestMailOperationStatus
572 modest_mail_operation_get_status (ModestMailOperation *mail_op)
574 ModestMailOperationPrivate *priv;
576 /* g_return_val_if_fail (mail_op, MODEST_MAIL_OPERATION_STATUS_INVALID); */
577 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), */
578 /* MODEST_MAIL_OPERATION_STATUS_INVALID); */
580 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
585 modest_mail_operation_get_error (ModestMailOperation *mail_op)
587 ModestMailOperationPrivate *priv;
589 /* g_return_val_if_fail (mail_op, NULL); */
590 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), NULL); */
592 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
597 modest_mail_operation_cancel (ModestMailOperation *mail_op)
603 modest_mail_operation_get_task_done (ModestMailOperation *mail_op)
605 ModestMailOperationPrivate *priv;
607 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
609 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
614 modest_mail_operation_get_task_total (ModestMailOperation *mail_op)
616 ModestMailOperationPrivate *priv;
618 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
620 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
625 modest_mail_operation_is_finished (ModestMailOperation *mail_op)
627 ModestMailOperationPrivate *priv;
628 gboolean retval = FALSE;
630 if (!MODEST_IS_MAIL_OPERATION (mail_op)) {
631 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
635 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
637 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
638 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
639 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
640 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
649 /* ******************************************************************* */
650 /* ************************** STORE ACTIONS ************************* */
651 /* ******************************************************************* */
655 modest_mail_operation_create_folder (ModestMailOperation *mail_op,
656 TnyFolderStore *parent,
659 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
660 g_return_val_if_fail (name, NULL);
662 TnyFolder *new_folder = NULL;
663 TnyStoreAccount *store_account;
665 /* Create the folder */
666 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
670 /* Subscribe to folder */
671 if (!tny_folder_is_subscribed (new_folder)) {
672 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
673 tny_store_account_subscribe (store_account, new_folder);
674 g_object_unref (G_OBJECT (store_account));
681 modest_mail_operation_remove_folder (ModestMailOperation *mail_op,
683 gboolean remove_to_trash)
685 TnyFolderStore *folder_store;
687 g_return_if_fail (TNY_IS_FOLDER (folder));
689 /* Get folder store */
690 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
692 /* Delete folder or move to trash */
693 if (remove_to_trash) {
694 TnyFolder *trash_folder;
696 trash_folder = modest_mail_operation_find_trash_folder (mail_op,
697 TNY_STORE_ACCOUNT (folder_store));
699 /* TODO: error_handling */
700 modest_mail_operation_move_folder (mail_op,
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 *mail_op,
717 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
718 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
720 /* FIXME: better error handling */
721 if (strrchr (name, '/') != NULL)
724 /* Rename. Camel handles folder subscription/unsubscription */
725 tny_folder_set_name (folder, name, NULL); /* FIXME */
729 modest_mail_operation_move_folder (ModestMailOperation *mail_op,
731 TnyFolderStore *parent)
733 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
734 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
735 g_return_if_fail (TNY_IS_FOLDER (folder));
737 modest_mail_operation_xfer_folder (mail_op, folder, parent, TRUE);
741 modest_mail_operation_copy_folder (ModestMailOperation *mail_op,
743 TnyFolderStore *parent)
745 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
746 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
747 g_return_if_fail (TNY_IS_FOLDER (folder));
749 modest_mail_operation_xfer_folder (mail_op, folder, parent, FALSE);
753 modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
755 TnyFolderStore *parent,
756 gboolean delete_original)
758 const gchar *folder_name;
759 TnyFolder *dest_folder, *child;
761 TnyList *folders, *headers;
763 g_return_if_fail (TNY_IS_FOLDER (folder));
764 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
766 /* Create the destination folder */
767 folder_name = tny_folder_get_name (folder);
768 dest_folder = modest_mail_operation_create_folder (mail_op,
769 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)) {
783 child = TNY_FOLDER (tny_iterator_get_current (iter));
784 modest_mail_operation_xfer_folder (mail_op, child,
785 TNY_FOLDER_STORE (dest_folder),
787 tny_iterator_next (iter);
790 /* Delete source folder (if needed) */
792 modest_mail_operation_remove_folder (mail_op, folder, FALSE);
795 g_object_unref (G_OBJECT (dest_folder));
796 g_object_unref (G_OBJECT (headers));
797 g_object_unref (G_OBJECT (folders));
798 g_object_unref (G_OBJECT (iter));
802 /* FIXME: this method should be rewritten when the policy for the
803 Trash folder becomes clearer */
805 modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
806 TnyStoreAccount *store_account)
811 /*TnyFolderStoreQuery *query;*/
812 TnyFolder *trash_folder;
814 /* Look for Trash folder */
815 folders = TNY_LIST (tny_simple_list_new ());
816 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
817 folders, NULL, NULL); /* FIXME */
818 iter = tny_list_create_iterator (folders);
821 while (!tny_iterator_is_done (iter) && !found) {
823 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
824 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
827 tny_iterator_next (iter);
831 g_object_unref (G_OBJECT (folders));
832 g_object_unref (G_OBJECT (iter));
834 /* TODO: better error handling management */
841 /* ******************************************************************* */
842 /* ************************** MSG ACTIONS ************************* */
843 /* ******************************************************************* */
846 modest_mail_operation_copy_msg (ModestMailOperation *mail_op,
850 g_return_if_fail (TNY_IS_HEADER (header));
851 g_return_if_fail (TNY_IS_FOLDER (folder));
853 modest_mail_operation_xfer_msg (mail_op, header, folder, FALSE);
857 modest_mail_operation_move_msg (ModestMailOperation *mail_op,
861 g_return_if_fail (TNY_IS_HEADER (header));
862 g_return_if_fail (TNY_IS_FOLDER (folder));
864 modest_mail_operation_xfer_msg (mail_op, header, folder, TRUE);
868 modest_mail_operation_remove_msg (ModestMailOperation *mail_op,
870 gboolean remove_to_trash)
874 g_return_if_fail (TNY_IS_HEADER (header));
876 folder = tny_header_get_folder (header);
878 /* Delete or move to trash */
879 if (remove_to_trash) {
880 TnyFolder *trash_folder;
881 TnyStoreAccount *store_account;
883 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
884 trash_folder = modest_mail_operation_find_trash_folder (mail_op, store_account);
886 modest_mail_operation_move_msg (mail_op, header, trash_folder);
888 g_object_unref (G_OBJECT (store_account));
890 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
891 tny_folder_expunge (folder, NULL); /* FIXME */
895 g_object_unref (folder);
899 modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
902 gboolean delete_original)
904 TnyFolder *src_folder;
907 src_folder = tny_header_get_folder (header);
908 headers = tny_simple_list_new ();
911 tny_list_prepend (headers, G_OBJECT (header));
912 tny_folder_transfer_msgs (src_folder, headers, folder, delete_original, NULL); /* FIXME */
915 g_object_unref (headers);
916 g_object_unref (folder);
920 /* ******************************************************************* */
921 /* ************************* UTILIY FUNCTIONS ************************ */
922 /* ******************************************************************* */
924 is_ascii(const gchar *s)
927 if (s[0] & 128 || s[0] < 32)
935 get_content_type(const gchar *s)
939 type = g_string_new("text/plain");
941 if (g_utf8_validate(s, -1, NULL)) {
942 g_string_append(type, "; charset=\"utf-8\"");
944 /* it should be impossible to reach this, but better safe than sorry */
945 g_warning("invalid utf8 in message");
946 g_string_append(type, "; charset=\"latin1\"");
949 return g_string_free(type, FALSE);
953 modest_error_quark (void)
955 static GQuark err_q = 0;
958 err_q = g_quark_from_static_string ("modest-error-quark");
964 add_attachments (TnyMsg *msg, GList *attachments_list)
967 TnyMimePart *attachment_part, *old_attachment;
968 const gchar *attachment_content_type;
969 const gchar *attachment_filename;
970 TnyStream *attachment_stream;
972 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
974 old_attachment = pos->data;
975 attachment_filename = tny_mime_part_get_filename (old_attachment);
976 attachment_stream = tny_mime_part_get_stream (old_attachment);
977 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
979 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
981 tny_mime_part_construct_from_stream (attachment_part,
983 attachment_content_type);
984 tny_stream_reset (attachment_stream);
986 tny_mime_part_set_filename (attachment_part, attachment_filename);
988 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
989 /* g_object_unref (attachment_part); */
995 add_body_part (TnyMsg *msg,
997 const gchar *content_type,
998 gboolean has_attachments)
1000 TnyMimePart *text_body_part = NULL;
1001 TnyStream *text_body_stream;
1003 /* Create the stream */
1004 text_body_stream = TNY_STREAM (tny_camel_stream_new
1005 (camel_stream_mem_new_with_buffer
1006 (body, strlen(body))));
1008 /* Create body part if needed */
1009 if (has_attachments)
1011 TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
1013 text_body_part = TNY_MIME_PART(msg);
1015 /* Construct MIME part */
1016 tny_stream_reset (text_body_stream);
1017 tny_mime_part_construct_from_stream (text_body_part,
1020 tny_stream_reset (text_body_stream);
1022 /* Add part if needed */
1023 if (has_attachments) {
1024 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1025 g_object_unref (G_OBJECT(text_body_part));
1029 g_object_unref (text_body_stream);
1031 return text_body_part;