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"
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 #define MODEST_ERROR modest_error_quark ()
57 typedef enum _ModestMailOperationErrorCode ModestMailOperationErrorCode;
58 enum _ModestMailOperationErrorCode {
59 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
60 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
62 MODEST_MAIL_OPERATION_NUM_ERROR_CODES
67 ModestMailOperation *mail_op;
72 static void set_error (ModestMailOperation *mail_operation,
73 ModestMailOperationErrorCode error_code,
74 const gchar *fmt, ...);
75 static void status_update_cb (TnyFolder *folder,
79 static void folder_refresh_cb (TnyFolder *folder,
83 static void add_attachments (TnyMsg *msg,
84 const GList *attachments_list);
87 static TnyMimePart * add_body_part (TnyMsg *msg,
89 const gchar *content_type,
90 gboolean has_attachments);
93 static void modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
95 TnyFolderStore *parent,
96 gboolean delete_original);
98 static void modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
101 gboolean delete_original);
103 static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
104 TnyStoreAccount *store_account);
107 enum _ModestMailOperationSignals
109 PROGRESS_CHANGED_SIGNAL,
114 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
115 struct _ModestMailOperationPrivate {
119 ModestMailOperationStatus status;
122 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
123 MODEST_TYPE_MAIL_OPERATION, \
124 ModestMailOperationPrivate))
126 /* some utility functions */
127 static char * get_content_type(const gchar *s);
128 static gboolean is_ascii(const gchar *s);
131 static GObjectClass *parent_class = NULL;
133 static guint signals[NUM_SIGNALS] = {0};
136 modest_mail_operation_get_type (void)
138 static GType my_type = 0;
140 static const GTypeInfo my_info = {
141 sizeof(ModestMailOperationClass),
142 NULL, /* base init */
143 NULL, /* base finalize */
144 (GClassInitFunc) modest_mail_operation_class_init,
145 NULL, /* class finalize */
146 NULL, /* class data */
147 sizeof(ModestMailOperation),
149 (GInstanceInitFunc) modest_mail_operation_init,
152 my_type = g_type_register_static (G_TYPE_OBJECT,
153 "ModestMailOperation",
160 modest_mail_operation_class_init (ModestMailOperationClass *klass)
162 GObjectClass *gobject_class;
163 gobject_class = (GObjectClass*) klass;
165 parent_class = g_type_class_peek_parent (klass);
166 gobject_class->finalize = modest_mail_operation_finalize;
168 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
170 /* signal definitions go here, e.g.: */
171 signals[PROGRESS_CHANGED_SIGNAL] =
172 g_signal_new ("progress_changed",
173 G_TYPE_FROM_CLASS (gobject_class),
175 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
177 g_cclosure_marshal_VOID__VOID,
182 modest_mail_operation_init (ModestMailOperation *obj)
184 ModestMailOperationPrivate *priv;
186 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
188 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
192 priv->cb_lock = g_mutex_new ();
196 modest_mail_operation_finalize (GObject *obj)
198 ModestMailOperationPrivate *priv;
200 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
203 g_error_free (priv->error);
207 g_mutex_free (priv->cb_lock);
209 G_OBJECT_CLASS(parent_class)->finalize (obj);
213 modest_mail_operation_new (void)
215 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
220 modest_mail_operation_send_mail (ModestMailOperation *mail_op,
221 TnyTransportAccount *transport_account,
224 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
225 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
227 tny_transport_account_send (transport_account, msg, NULL); /* FIXME */
231 modest_mail_operation_send_new_mail (ModestMailOperation *mail_op,
232 TnyTransportAccount *transport_account,
237 const gchar *subject,
239 const GList *attachments_list)
245 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
246 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
248 /* Check parametters */
251 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
252 _("Error trying to send a mail. You need to set almost one a recipient"));
257 new_msg = TNY_MSG (tny_camel_msg_new ());
258 header = TNY_HEADER (tny_camel_header_new ());
260 /* WARNING: set the header before assign values to it */
261 tny_msg_set_header (new_msg, header);
262 tny_header_set_from (TNY_HEADER (header), from);
263 tny_header_set_to (TNY_HEADER (header), to);
264 tny_header_set_cc (TNY_HEADER (header), cc);
265 tny_header_set_bcc (TNY_HEADER (header), bcc);
266 tny_header_set_subject (TNY_HEADER (header), subject);
268 content_type = get_content_type(body);
270 /* Add the body of the new mail */
271 add_body_part (new_msg, body, (const gchar *) content_type,
272 (attachments_list == NULL) ? FALSE : TRUE);
274 /* Add attachments */
275 add_attachments (new_msg, attachments_list);
278 tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */
281 g_object_unref (header);
282 g_object_unref (new_msg);
283 g_free(content_type);
287 * modest_mail_operation_create_forward_mail:
288 * @msg: a valid #TnyMsg instance
289 * @forward_type: the type of forwarded message
291 * creates a forwarded message from an existing one
293 * Returns: a new #TnyMsg, or NULL in case of error
296 modest_mail_operation_create_forward_mail (TnyMsg *msg,
298 ModestMailOperationForwardType forward_type)
301 TnyHeader *new_header, *header;
302 gchar *new_subject, *new_body, *content_type;
303 TnyMimePart *text_body_part = NULL;
304 GList *attachments_list;
308 /* Create new objects */
309 new_msg = TNY_MSG (tny_camel_msg_new ());
310 new_header = TNY_HEADER (tny_camel_header_new ());
312 header = tny_msg_get_header (msg);
314 /* Fill the header */
315 tny_msg_set_header (new_msg, new_header);
316 tny_header_set_from (new_header, from);
318 /* Change the subject */
319 new_subject = (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
321 tny_header_set_subject (new_header, (const gchar *) new_subject);
322 g_free (new_subject);
324 /* Get body from original msg */
325 new_body = (gchar *) modest_tny_msg_actions_find_body (msg, FALSE);
327 g_object_unref (new_msg);
330 content_type = get_content_type(new_body);
332 /* Create the list of attachments */
333 parts = TNY_LIST (tny_simple_list_new());
334 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
335 iter = tny_list_create_iterator (parts);
336 attachments_list = NULL;
338 while (!tny_iterator_is_done(iter)) {
341 part = TNY_MIME_PART (tny_iterator_get_current (iter));
342 if (tny_mime_part_is_attachment (part))
343 attachments_list = g_list_prepend (attachments_list, part);
345 tny_iterator_next (iter);
348 /* Add attachments */
349 add_attachments (new_msg, attachments_list);
351 switch (forward_type) {
352 TnyMimePart *attachment_part;
355 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
356 /* Prepend "Original message" text */
357 inlined_text = (gchar *)
358 modest_text_utils_inlined_text (tny_header_get_from (header),
359 tny_header_get_date_sent (header),
360 tny_header_get_to (header),
361 tny_header_get_subject (header),
362 (const gchar*) new_body);
364 new_body = inlined_text;
367 add_body_part (new_msg, new_body,
368 (const gchar *) content_type,
369 (tny_list_get_length (parts) > 0) ? TRUE : FALSE);
372 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
373 attachment_part = add_body_part (new_msg, new_body,
374 (const gchar *) content_type, TRUE);
376 /* Set the subject as the name of the attachment */
377 tny_mime_part_set_filename (attachment_part, tny_header_get_subject (header));
383 if (attachments_list) g_list_free (attachments_list);
384 g_object_unref (parts);
385 if (text_body_part) g_free (text_body_part);
386 g_free (content_type);
393 * modest_mail_operation_create_reply_mail:
394 * @msg: a valid #TnyMsg instance
395 * @reply_type: the format of the new message
396 * @reply_mode: the mode of reply, to the sender only, to a mail list or to all
398 * creates a new message to reply to an existing one
400 * Returns: Returns: a new #TnyMsg, or NULL in case of error
403 modest_mail_operation_create_reply_mail (TnyMsg *msg,
405 ModestMailOperationReplyType reply_type,
406 ModestMailOperationReplyMode reply_mode)
409 TnyHeader *new_header, *header;
410 gchar *new_subject, *new_body, *content_type, *quoted;
411 TnyMimePart *text_body_part;
413 /* Create new objects */
414 new_msg = TNY_MSG (tny_camel_msg_new ());
415 new_header = TNY_HEADER (tny_camel_header_new ());
416 header = tny_msg_get_header (msg);
418 /* Fill the header */
419 tny_msg_set_header (new_msg, new_header);
420 tny_header_set_to (new_header, tny_header_get_from (header));
421 tny_header_set_from (new_header, from);
423 switch (reply_mode) {
424 gchar *new_cc = NULL;
425 const gchar *cc = NULL, *bcc = NULL;
428 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
429 /* Do not fill neither cc nor bcc */
431 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
434 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
435 /* Concatenate to, cc and bcc */
436 cc = tny_header_get_cc (header);
437 bcc = tny_header_get_bcc (header);
439 tmp = g_string_new (tny_header_get_to (header));
440 if (cc) g_string_append_printf (tmp, ",%s",cc);
441 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
443 /* Remove my own address from the cc list */
445 modest_text_utils_remove_address ((const gchar *) tmp->str,
446 (const gchar *) from);
447 /* FIXME: remove also the mails from the new To: */
448 tny_header_set_cc (new_header, new_cc);
451 g_string_free (tmp, TRUE);
456 /* Change the subject */
457 new_subject = (gchar*) modest_text_utils_derived_subject (tny_header_get_subject(header),
459 tny_header_set_subject (new_header, (const gchar *) new_subject);
460 g_free (new_subject);
462 /* Get body from original msg */
463 new_body = (gchar*) modest_tny_msg_actions_find_body (msg, FALSE);
465 g_object_unref (new_msg);
468 content_type = get_content_type(new_body);
470 switch (reply_type) {
473 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
474 /* Prepend "Original message" text */
475 cited_text = (gchar *) modest_text_utils_cited_text (tny_header_get_from (header),
476 tny_header_get_date_sent (header),
477 (const gchar*) new_body);
479 new_body = cited_text;
481 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
482 /* FIXME: replace 80 with a value from ModestConf */
483 quoted = (gchar*) modest_text_utils_quote (new_body,
484 tny_header_get_from (header),
485 tny_header_get_date_sent (header),
492 text_body_part = add_body_part (new_msg, new_body,
493 (const gchar *) content_type, TRUE);
496 /* g_free (text_body_part); */
497 g_free (content_type);
504 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gpointer user_data)
506 g_print ("%s status: %d\n", what, status);
510 folder_refresh_cb (TnyFolder *folder, gboolean cancelled, GError **err, gpointer user_data)
512 AsyncHelper *helper = NULL;
513 ModestMailOperation *mail_op = NULL;
514 ModestMailOperationPrivate *priv = NULL;
516 helper = (AsyncHelper *) user_data;
517 mail_op = MODEST_MAIL_OPERATION (helper->mail_op);
518 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
520 g_mutex_lock (priv->cb_lock);
525 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELLED;
527 g_signal_emit (G_OBJECT (mail_op), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
529 g_mutex_unlock (priv->cb_lock);
531 if (priv->done == priv->total) {
532 ((ModestUpdateAccountCallback) (helper->cb)) (mail_op, helper->user_data);
533 g_slice_free (AsyncHelper, helper);
538 modest_mail_operation_update_account (ModestMailOperation *mail_op,
539 TnyStoreAccount *store_account,
540 ModestUpdateAccountCallback callback,
543 ModestMailOperationPrivate *priv;
545 TnyIterator *ifolders;
546 TnyFolder *cur_folder;
547 TnyFolderStoreQuery *query;
550 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
551 g_return_if_fail (TNY_IS_STORE_ACCOUNT(store_account));
553 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
555 /* Get subscribed folders */
556 folders = TNY_LIST (tny_simple_list_new ());
557 query = tny_folder_store_query_new ();
558 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
559 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
560 folders, query, NULL); /* FIXME */
561 g_object_unref (query);
563 ifolders = tny_list_create_iterator (folders);
564 priv->total = tny_list_get_length (folders);
568 /* Async refresh folders */
569 for (tny_iterator_first (ifolders);
570 !tny_iterator_is_done (ifolders);
571 tny_iterator_next (ifolders)) {
573 cur_folder = TNY_FOLDER (tny_iterator_get_current (ifolders));
574 helper = g_slice_new0 (AsyncHelper);
575 helper->mail_op = mail_op;
576 helper->user_data = user_data;
577 helper->cb = callback;
578 tny_folder_refresh_async (cur_folder, folder_refresh_cb,
579 status_update_cb, helper);
582 g_object_unref (ifolders);
585 ModestMailOperationStatus
586 modest_mail_operation_get_status (ModestMailOperation *mail_op)
588 ModestMailOperationPrivate *priv;
590 /* g_return_val_if_fail (mail_op, MODEST_MAIL_OPERATION_STATUS_INVALID); */
591 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), */
592 /* MODEST_MAIL_OPERATION_STATUS_INVALID); */
594 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
599 modest_mail_operation_get_error (ModestMailOperation *mail_op)
601 ModestMailOperationPrivate *priv;
603 /* g_return_val_if_fail (mail_op, NULL); */
604 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), NULL); */
606 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
611 modest_mail_operation_cancel (ModestMailOperation *mail_op)
617 modest_mail_operation_get_task_done (ModestMailOperation *mail_op)
619 ModestMailOperationPrivate *priv;
621 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
623 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
628 modest_mail_operation_get_task_total (ModestMailOperation *mail_op)
630 ModestMailOperationPrivate *priv;
632 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
634 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
638 /* ******************************************************************* */
639 /* ************************** STORE ACTIONS ************************* */
640 /* ******************************************************************* */
644 modest_mail_operation_create_folder (ModestMailOperation *mail_op,
645 TnyFolderStore *parent,
648 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
649 g_return_val_if_fail (name, NULL);
651 TnyFolder *new_folder = NULL;
652 TnyStoreAccount *store_account;
654 /* Create the folder */
655 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
659 /* Subscribe to folder */
660 if (!tny_folder_is_subscribed (new_folder)) {
661 store_account = tny_folder_get_account (TNY_FOLDER (parent));
662 tny_store_account_subscribe (store_account, new_folder);
663 g_object_unref (G_OBJECT (store_account));
670 modest_mail_operation_remove_folder (ModestMailOperation *mail_op,
672 gboolean remove_to_trash)
674 TnyFolderStore *folder_store;
676 g_return_if_fail (TNY_IS_FOLDER (folder));
678 /* Get folder store */
679 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
681 /* Delete folder or move to trash */
682 if (remove_to_trash) {
683 TnyFolder *trash_folder;
685 trash_folder = modest_mail_operation_find_trash_folder (mail_op,
686 TNY_STORE_ACCOUNT (folder_store));
688 /* TODO: error_handling */
689 modest_mail_operation_move_folder (mail_op,
691 TNY_FOLDER_STORE (trash_folder));
693 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
694 g_object_unref (G_OBJECT (folder));
698 g_object_unref (G_OBJECT (folder_store));
702 modest_mail_operation_rename_folder (ModestMailOperation *mail_op,
706 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
707 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
709 /* FIXME: better error handling */
710 if (strrchr (name, '/') != NULL)
713 /* Rename. Camel handles folder subscription/unsubscription */
714 tny_folder_set_name (folder, name, NULL); /* FIXME */
718 modest_mail_operation_move_folder (ModestMailOperation *mail_op,
720 TnyFolderStore *parent)
722 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
723 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
724 g_return_if_fail (TNY_IS_FOLDER (folder));
726 modest_mail_operation_xfer_folder (mail_op, folder, parent, TRUE);
730 modest_mail_operation_copy_folder (ModestMailOperation *mail_op,
732 TnyFolderStore *parent)
734 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
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 (mail_op, folder, parent, FALSE);
742 modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
744 TnyFolderStore *parent,
745 gboolean delete_original)
747 const gchar *folder_name;
748 TnyFolder *dest_folder, *child;
750 TnyList *folders, *headers;
752 g_return_if_fail (TNY_IS_FOLDER (folder));
753 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
755 /* Create the destination folder */
756 folder_name = tny_folder_get_name (folder);
757 dest_folder = modest_mail_operation_create_folder (mail_op,
758 parent, folder_name);
760 /* Transfer messages */
761 headers = TNY_LIST (tny_simple_list_new ());
762 tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
763 tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
765 /* Recurse children */
766 folders = TNY_LIST (tny_simple_list_new ());
767 tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
768 iter = tny_list_create_iterator (folders);
770 while (!tny_iterator_is_done (iter)) {
772 child = TNY_FOLDER (tny_iterator_get_current (iter));
773 modest_mail_operation_xfer_folder (mail_op, child,
774 TNY_FOLDER_STORE (dest_folder),
776 tny_iterator_next (iter);
779 /* Delete source folder (if needed) */
781 modest_mail_operation_remove_folder (mail_op, folder, FALSE);
784 g_object_unref (G_OBJECT (dest_folder));
785 g_object_unref (G_OBJECT (headers));
786 g_object_unref (G_OBJECT (folders));
787 g_object_unref (G_OBJECT (iter));
791 modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
792 TnyStoreAccount *store_account)
797 /*TnyFolderStoreQuery *query;*/
798 TnyFolder *trash_folder;
800 /* Look for Trash folder */
801 folders = TNY_LIST (tny_simple_list_new ());
802 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
803 folders, NULL, NULL); /* FIXME */
804 iter = tny_list_create_iterator (folders);
807 while (!tny_iterator_is_done (iter) && !found) {
809 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
810 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
813 tny_iterator_next (iter);
817 g_object_unref (G_OBJECT (folders));
818 g_object_unref (G_OBJECT (iter));
820 /* TODO: better error handling management */
827 /* ******************************************************************* */
828 /* ************************** MSG ACTIONS ************************* */
829 /* ******************************************************************* */
832 modest_mail_operation_copy_msg (ModestMailOperation *mail_op,
836 g_return_if_fail (TNY_IS_HEADER (header));
837 g_return_if_fail (TNY_IS_FOLDER (folder));
839 modest_mail_operation_xfer_msg (mail_op, header, folder, FALSE);
843 modest_mail_operation_move_msg (ModestMailOperation *mail_op,
847 g_return_if_fail (TNY_IS_HEADER (header));
848 g_return_if_fail (TNY_IS_FOLDER (folder));
850 modest_mail_operation_xfer_msg (mail_op, header, folder, TRUE);
854 modest_mail_operation_remove_msg (ModestMailOperation *mail_op,
856 gboolean remove_to_trash)
860 g_return_if_fail (TNY_IS_HEADER (header));
862 folder = tny_header_get_folder (header);
864 /* Delete or move to trash */
865 if (remove_to_trash) {
866 TnyFolder *trash_folder;
867 TnyStoreAccount *store_account;
869 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
870 trash_folder = modest_mail_operation_find_trash_folder (mail_op, store_account);
872 modest_mail_operation_move_msg (mail_op, header, trash_folder);
874 g_object_unref (G_OBJECT (store_account));
876 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
877 tny_folder_expunge (folder, NULL); /* FIXME */
881 g_object_unref (folder);
885 modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
888 gboolean delete_original)
890 TnyFolder *src_folder;
893 src_folder = tny_header_get_folder (header);
894 headers = tny_simple_list_new ();
897 tny_list_prepend (headers, G_OBJECT (header));
898 tny_folder_transfer_msgs (src_folder, headers, folder, delete_original, NULL); /* FIXME */
901 g_object_unref (headers);
902 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 modest_error_quark (void)
941 static GQuark err_q = 0;
944 err_q = g_quark_from_static_string ("modest-error-quark");
951 set_error (ModestMailOperation *mail_op,
952 ModestMailOperationErrorCode error_code,
953 const gchar *fmt, ...)
955 ModestMailOperationPrivate *priv;
960 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
962 va_start (args, fmt);
964 orig = g_strdup_vprintf(fmt, args);
965 error = g_error_new (MODEST_ERROR, error_code, orig);
970 g_object_unref (priv->error);
973 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
977 add_attachments (TnyMsg *msg, const GList *attachments_list)
980 TnyMimePart *attachment_part, *old_attachment;
981 const gchar *attachment_content_type;
982 const gchar *attachment_filename;
983 TnyStream *attachment_stream;
985 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
987 old_attachment = pos->data;
988 attachment_filename = tny_mime_part_get_filename (old_attachment);
989 attachment_stream = tny_mime_part_get_stream (old_attachment);
990 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
992 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
994 tny_mime_part_construct_from_stream (attachment_part,
996 attachment_content_type);
997 tny_stream_reset (attachment_stream);
999 tny_mime_part_set_filename (attachment_part, attachment_filename);
1001 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
1002 /* g_object_unref (attachment_part); */
1007 static TnyMimePart *
1008 add_body_part (TnyMsg *msg,
1010 const gchar *content_type,
1011 gboolean has_attachments)
1013 TnyMimePart *text_body_part = NULL;
1014 TnyStream *text_body_stream;
1016 /* Create the stream */
1017 text_body_stream = TNY_STREAM (tny_camel_stream_new
1018 (camel_stream_mem_new_with_buffer
1019 (body, strlen(body))));
1021 /* Create body part if needed */
1022 if (has_attachments)
1024 TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
1026 text_body_part = TNY_MIME_PART(msg);
1028 /* Construct MIME part */
1029 tny_stream_reset (text_body_stream);
1030 tny_mime_part_construct_from_stream (text_body_part,
1033 tny_stream_reset (text_body_stream);
1035 /* Add part if needed */
1036 if (has_attachments) {
1037 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1038 g_object_unref (G_OBJECT(text_body_part));
1042 g_object_unref (text_body_stream);
1044 return text_body_part;