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"
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 #define MODEST_ERROR modest_error_quark ()
59 typedef enum _ModestMailOperationErrorCode ModestMailOperationErrorCode;
60 enum _ModestMailOperationErrorCode {
61 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
62 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
63 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
65 MODEST_MAIL_OPERATION_NUM_ERROR_CODES
68 static void set_error (ModestMailOperation *mail_operation,
69 ModestMailOperationErrorCode error_code,
70 const gchar *fmt, ...);
71 static void status_update_cb (TnyFolder *folder,
75 static void folder_refresh_cb (TnyFolder *folder,
79 static void add_attachments (TnyMsg *msg,
80 GList *attachments_list);
83 static TnyMimePart * add_body_part (TnyMsg *msg,
85 const gchar *content_type,
86 gboolean has_attachments);
89 static void modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
91 TnyFolderStore *parent,
92 gboolean delete_original);
94 static void modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
97 gboolean delete_original);
99 static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
100 TnyStoreAccount *store_account);
103 enum _ModestMailOperationSignals
105 PROGRESS_CHANGED_SIGNAL,
110 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
111 struct _ModestMailOperationPrivate {
117 ModestMailOperationStatus status;
120 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
121 MODEST_TYPE_MAIL_OPERATION, \
122 ModestMailOperationPrivate))
124 /* some utility functions */
125 static char * get_content_type(const gchar *s);
126 static gboolean is_ascii(const gchar *s);
129 static GObjectClass *parent_class = NULL;
131 static guint signals[NUM_SIGNALS] = {0};
134 modest_mail_operation_get_type (void)
136 static GType my_type = 0;
138 static const GTypeInfo my_info = {
139 sizeof(ModestMailOperationClass),
140 NULL, /* base init */
141 NULL, /* base finalize */
142 (GClassInitFunc) modest_mail_operation_class_init,
143 NULL, /* class finalize */
144 NULL, /* class data */
145 sizeof(ModestMailOperation),
147 (GInstanceInitFunc) modest_mail_operation_init,
150 my_type = g_type_register_static (G_TYPE_OBJECT,
151 "ModestMailOperation",
158 modest_mail_operation_class_init (ModestMailOperationClass *klass)
160 GObjectClass *gobject_class;
161 gobject_class = (GObjectClass*) klass;
163 parent_class = g_type_class_peek_parent (klass);
164 gobject_class->finalize = modest_mail_operation_finalize;
166 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
168 /* signal definitions go here, e.g.: */
169 signals[PROGRESS_CHANGED_SIGNAL] =
170 g_signal_new ("progress_changed",
171 G_TYPE_FROM_CLASS (gobject_class),
173 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
175 g_cclosure_marshal_VOID__VOID,
180 modest_mail_operation_init (ModestMailOperation *obj)
182 ModestMailOperationPrivate *priv;
184 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
186 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_replyto (TNY_HEADER (header), from);
264 tny_header_set_to (TNY_HEADER (header), to);
265 tny_header_set_cc (TNY_HEADER (header), cc);
266 tny_header_set_bcc (TNY_HEADER (header), bcc);
267 tny_header_set_subject (TNY_HEADER (header), subject);
269 content_type = get_content_type(body);
271 /* Add the body of the new mail */
272 add_body_part (new_msg, body, (const gchar *) content_type,
273 (attachments_list == NULL) ? FALSE : TRUE);
275 /* Add attachments */
276 add_attachments (new_msg, (GList*) attachments_list);
279 tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */
282 g_object_unref (header);
283 g_object_unref (new_msg);
284 g_free(content_type);
288 add_if_attachment (gpointer data, gpointer user_data)
291 GList *attachments_list;
293 part = TNY_MIME_PART (data);
294 attachments_list = (GList *) user_data;
296 if (tny_mime_part_is_attachment (part))
297 attachments_list = g_list_prepend (attachments_list, part);
302 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
305 TnyHeader *new_header, *header;
308 ModestFormatter *formatter;
310 /* Get body from original msg */
311 header = tny_msg_get_header (msg);
312 body = modest_tny_msg_actions_find_body_part (msg, TRUE);
314 /* TODO: select the formatter from account prefs */
315 formatter = modest_formatter_new ("text/plain");
317 /* Format message body */
320 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
322 new_msg = modest_formatter_cite (formatter, body, header);
324 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
325 new_msg = modest_formatter_quote (formatter, body, header);
330 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
332 new_msg = modest_formatter_inline (formatter, body, header);
334 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
335 new_msg = modest_formatter_attach (formatter, body, header);
339 g_object_unref (G_OBJECT (formatter));
341 /* Fill the header */
342 new_header = TNY_HEADER (tny_camel_header_new ());
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 */
348 new_subject = (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
349 (is_reply) ? _("Re:") : _("Fwd:"));
350 tny_header_set_subject (new_header, (const gchar *) new_subject);
351 g_free (new_subject);
354 g_object_unref (G_OBJECT (new_header));
355 g_object_unref (G_OBJECT (header));
361 * modest_mail_operation_create_forward_mail:
362 * @msg: a valid #TnyMsg instance
363 * @forward_type: the type of forwarded message
365 * creates a forwarded message from an existing one
367 * Returns: a new #TnyMsg, or NULL in case of error
370 modest_mail_operation_create_forward_mail (TnyMsg *msg,
372 ModestMailOperationForwardType forward_type)
375 TnyList *parts = NULL;
376 GList *attachments_list = NULL;
378 new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
380 /* Add attachments */
381 parts = TNY_LIST (tny_simple_list_new());
382 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
383 tny_list_foreach (parts, add_if_attachment, attachments_list);
384 add_attachments (new_msg, attachments_list);
387 if (attachments_list) g_list_free (attachments_list);
388 g_object_unref (G_OBJECT (parts));
394 * modest_mail_operation_create_reply_mail:
395 * @msg: a valid #TnyMsg instance
396 * @reply_type: the format of the new message
397 * @reply_mode: the mode of reply, to the sender only, to a mail list or to all
399 * creates a new message to reply to an existing one
401 * Returns: Returns: a new #TnyMsg, or NULL in case of error
404 modest_mail_operation_create_reply_mail (TnyMsg *msg,
406 ModestMailOperationReplyType reply_type,
407 ModestMailOperationReplyMode reply_mode)
410 TnyHeader *new_header, *header;
411 const gchar* reply_to;
413 new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
415 /* Fill the header */
416 header = tny_msg_get_header (msg);
417 new_header = tny_msg_get_header (new_msg);
418 reply_to = tny_header_get_replyto (header);
420 tny_header_set_to (new_header, reply_to);
422 tny_header_set_to (new_header, tny_header_get_from (header));
424 switch (reply_mode) {
425 gchar *new_cc = NULL;
426 const gchar *cc = NULL, *bcc = NULL;
429 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
430 /* Do not fill neither cc nor bcc */
432 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
435 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
436 /* Concatenate to, cc and bcc */
437 cc = tny_header_get_cc (header);
438 bcc = tny_header_get_bcc (header);
440 tmp = g_string_new (tny_header_get_to (header));
441 if (cc) g_string_append_printf (tmp, ",%s",cc);
442 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
444 /* Remove my own address from the cc list */
446 modest_text_utils_remove_address ((const gchar *) tmp->str,
447 (const gchar *) from);
448 /* FIXME: remove also the mails from the new To: */
449 tny_header_set_cc (new_header, new_cc);
452 g_string_free (tmp, TRUE);
458 g_object_unref (G_OBJECT (new_header));
459 g_object_unref (G_OBJECT (header));
465 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gpointer user_data)
467 g_print ("%s status: %d\n", what, status);
471 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
473 ModestMailOperation *mail_op = NULL;
474 ModestMailOperationPrivate *priv = NULL;
476 mail_op = MODEST_MAIL_OPERATION (user_data);
477 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
479 g_mutex_lock (priv->cb_lock);
481 if ((canceled && *err) || *err) {
482 priv->error = g_error_copy (*err);
484 } else if (canceled) {
487 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
488 _("Error trying to refresh folder %s. Operation canceled"),
489 tny_folder_get_name (folder));
494 if (priv->done == priv->total)
495 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
496 else if ((priv->done + priv->canceled + priv->failed) == priv->total)
497 if (priv->failed == priv->total)
498 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
499 else if (priv->failed == priv->total)
500 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
502 priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
504 g_mutex_unlock (priv->cb_lock);
506 g_signal_emit (G_OBJECT (mail_op), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
510 modest_mail_operation_update_account (ModestMailOperation *mail_op,
511 TnyStoreAccount *store_account)
513 ModestMailOperationPrivate *priv;
515 TnyIterator *ifolders;
516 TnyFolder *cur_folder;
517 TnyFolderStoreQuery *query;
519 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
520 g_return_if_fail (TNY_IS_STORE_ACCOUNT(store_account));
522 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
524 /* Get subscribed folders */
525 folders = TNY_LIST (tny_simple_list_new ());
526 query = tny_folder_store_query_new ();
527 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
528 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
529 folders, query, NULL); /* FIXME */
530 g_object_unref (query);
532 ifolders = tny_list_create_iterator (folders);
533 priv->total = tny_list_get_length (folders);
535 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
538 /* Async refresh folders. Reference the mail_op because
539 tinymail destroys the user_data */
540 for (tny_iterator_first (ifolders);
541 !tny_iterator_is_done (ifolders);
542 tny_iterator_next (ifolders)) {
544 cur_folder = TNY_FOLDER (tny_iterator_get_current (ifolders));
545 tny_folder_refresh_async (cur_folder, folder_refresh_cb,
546 status_update_cb, g_object_ref (mail_op));
549 g_object_unref (ifolders);
554 ModestMailOperationStatus
555 modest_mail_operation_get_status (ModestMailOperation *mail_op)
557 ModestMailOperationPrivate *priv;
559 /* g_return_val_if_fail (mail_op, MODEST_MAIL_OPERATION_STATUS_INVALID); */
560 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), */
561 /* MODEST_MAIL_OPERATION_STATUS_INVALID); */
563 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
568 modest_mail_operation_get_error (ModestMailOperation *mail_op)
570 ModestMailOperationPrivate *priv;
572 /* g_return_val_if_fail (mail_op, NULL); */
573 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), NULL); */
575 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
580 modest_mail_operation_cancel (ModestMailOperation *mail_op)
586 modest_mail_operation_get_task_done (ModestMailOperation *mail_op)
588 ModestMailOperationPrivate *priv;
590 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
592 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
597 modest_mail_operation_get_task_total (ModestMailOperation *mail_op)
599 ModestMailOperationPrivate *priv;
601 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
603 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
608 modest_mail_operation_is_finished (ModestMailOperation *mail_op)
610 ModestMailOperationPrivate *priv;
611 gboolean retval = FALSE;
613 if (!MODEST_IS_MAIL_OPERATION (mail_op)) {
614 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
618 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
620 g_mutex_lock (priv->cb_lock);
622 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
623 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
624 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
625 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
630 g_mutex_unlock (priv->cb_lock);
635 /* ******************************************************************* */
636 /* ************************** STORE ACTIONS ************************* */
637 /* ******************************************************************* */
641 modest_mail_operation_create_folder (ModestMailOperation *mail_op,
642 TnyFolderStore *parent,
645 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
646 g_return_val_if_fail (name, NULL);
648 TnyFolder *new_folder = NULL;
649 TnyStoreAccount *store_account;
651 /* Create the folder */
652 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
656 /* Subscribe to folder */
657 if (!tny_folder_is_subscribed (new_folder)) {
658 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
659 tny_store_account_subscribe (store_account, new_folder);
660 g_object_unref (G_OBJECT (store_account));
667 modest_mail_operation_remove_folder (ModestMailOperation *mail_op,
669 gboolean remove_to_trash)
671 TnyFolderStore *folder_store;
673 g_return_if_fail (TNY_IS_FOLDER (folder));
675 /* Get folder store */
676 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
678 /* Delete folder or move to trash */
679 if (remove_to_trash) {
680 TnyFolder *trash_folder;
682 trash_folder = modest_mail_operation_find_trash_folder (mail_op,
683 TNY_STORE_ACCOUNT (folder_store));
685 /* TODO: error_handling */
686 modest_mail_operation_move_folder (mail_op,
688 TNY_FOLDER_STORE (trash_folder));
690 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
691 g_object_unref (G_OBJECT (folder));
695 g_object_unref (G_OBJECT (folder_store));
699 modest_mail_operation_rename_folder (ModestMailOperation *mail_op,
703 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
704 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
706 /* FIXME: better error handling */
707 if (strrchr (name, '/') != NULL)
710 /* Rename. Camel handles folder subscription/unsubscription */
711 tny_folder_set_name (folder, name, NULL); /* FIXME */
715 modest_mail_operation_move_folder (ModestMailOperation *mail_op,
717 TnyFolderStore *parent)
719 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
720 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
721 g_return_if_fail (TNY_IS_FOLDER (folder));
723 modest_mail_operation_xfer_folder (mail_op, folder, parent, TRUE);
727 modest_mail_operation_copy_folder (ModestMailOperation *mail_op,
729 TnyFolderStore *parent)
731 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
732 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
733 g_return_if_fail (TNY_IS_FOLDER (folder));
735 modest_mail_operation_xfer_folder (mail_op, folder, parent, FALSE);
739 modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
741 TnyFolderStore *parent,
742 gboolean delete_original)
744 const gchar *folder_name;
745 TnyFolder *dest_folder, *child;
747 TnyList *folders, *headers;
749 g_return_if_fail (TNY_IS_FOLDER (folder));
750 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
752 /* Create the destination folder */
753 folder_name = tny_folder_get_name (folder);
754 dest_folder = modest_mail_operation_create_folder (mail_op,
755 parent, folder_name);
757 /* Transfer messages */
758 headers = TNY_LIST (tny_simple_list_new ());
759 tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
760 tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
762 /* Recurse children */
763 folders = TNY_LIST (tny_simple_list_new ());
764 tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
765 iter = tny_list_create_iterator (folders);
767 while (!tny_iterator_is_done (iter)) {
769 child = TNY_FOLDER (tny_iterator_get_current (iter));
770 modest_mail_operation_xfer_folder (mail_op, child,
771 TNY_FOLDER_STORE (dest_folder),
773 tny_iterator_next (iter);
776 /* Delete source folder (if needed) */
778 modest_mail_operation_remove_folder (mail_op, folder, FALSE);
781 g_object_unref (G_OBJECT (dest_folder));
782 g_object_unref (G_OBJECT (headers));
783 g_object_unref (G_OBJECT (folders));
784 g_object_unref (G_OBJECT (iter));
788 modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
789 TnyStoreAccount *store_account)
794 /*TnyFolderStoreQuery *query;*/
795 TnyFolder *trash_folder;
797 /* Look for Trash folder */
798 folders = TNY_LIST (tny_simple_list_new ());
799 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
800 folders, NULL, NULL); /* FIXME */
801 iter = tny_list_create_iterator (folders);
804 while (!tny_iterator_is_done (iter) && !found) {
806 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
807 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
810 tny_iterator_next (iter);
814 g_object_unref (G_OBJECT (folders));
815 g_object_unref (G_OBJECT (iter));
817 /* TODO: better error handling management */
824 /* ******************************************************************* */
825 /* ************************** MSG ACTIONS ************************* */
826 /* ******************************************************************* */
829 modest_mail_operation_copy_msg (ModestMailOperation *mail_op,
833 g_return_if_fail (TNY_IS_HEADER (header));
834 g_return_if_fail (TNY_IS_FOLDER (folder));
836 modest_mail_operation_xfer_msg (mail_op, header, folder, FALSE);
840 modest_mail_operation_move_msg (ModestMailOperation *mail_op,
844 g_return_if_fail (TNY_IS_HEADER (header));
845 g_return_if_fail (TNY_IS_FOLDER (folder));
847 modest_mail_operation_xfer_msg (mail_op, header, folder, TRUE);
851 modest_mail_operation_remove_msg (ModestMailOperation *mail_op,
853 gboolean remove_to_trash)
857 g_return_if_fail (TNY_IS_HEADER (header));
859 folder = tny_header_get_folder (header);
861 /* Delete or move to trash */
862 if (remove_to_trash) {
863 TnyFolder *trash_folder;
864 TnyStoreAccount *store_account;
866 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
867 trash_folder = modest_mail_operation_find_trash_folder (mail_op, store_account);
869 modest_mail_operation_move_msg (mail_op, header, trash_folder);
871 g_object_unref (G_OBJECT (store_account));
873 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
874 tny_folder_expunge (folder, NULL); /* FIXME */
878 g_object_unref (folder);
882 modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
885 gboolean delete_original)
887 TnyFolder *src_folder;
890 src_folder = tny_header_get_folder (header);
891 headers = tny_simple_list_new ();
894 tny_list_prepend (headers, G_OBJECT (header));
895 tny_folder_transfer_msgs (src_folder, headers, folder, delete_original, NULL); /* FIXME */
898 g_object_unref (headers);
899 g_object_unref (folder);
903 /* ******************************************************************* */
904 /* ************************* UTILIY FUNCTIONS ************************ */
905 /* ******************************************************************* */
907 is_ascii(const gchar *s)
910 if (s[0] & 128 || s[0] < 32)
918 get_content_type(const gchar *s)
922 type = g_string_new("text/plain");
924 if (g_utf8_validate(s, -1, NULL)) {
925 g_string_append(type, "; charset=\"utf-8\"");
927 /* it should be impossible to reach this, but better safe than sorry */
928 g_warning("invalid utf8 in message");
929 g_string_append(type, "; charset=\"latin1\"");
932 return g_string_free(type, FALSE);
936 modest_error_quark (void)
938 static GQuark err_q = 0;
941 err_q = g_quark_from_static_string ("modest-error-quark");
948 set_error (ModestMailOperation *mail_op,
949 ModestMailOperationErrorCode error_code,
950 const gchar *fmt, ...)
952 ModestMailOperationPrivate *priv;
957 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
959 va_start (args, fmt);
961 orig = g_strdup_vprintf(fmt, args);
962 error = g_error_new (MODEST_ERROR, error_code, orig);
967 g_object_unref (priv->error);
973 add_attachments (TnyMsg *msg, GList *attachments_list)
976 TnyMimePart *attachment_part, *old_attachment;
977 const gchar *attachment_content_type;
978 const gchar *attachment_filename;
979 TnyStream *attachment_stream;
981 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
983 old_attachment = pos->data;
984 attachment_filename = tny_mime_part_get_filename (old_attachment);
985 attachment_stream = tny_mime_part_get_stream (old_attachment);
986 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
988 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
990 tny_mime_part_construct_from_stream (attachment_part,
992 attachment_content_type);
993 tny_stream_reset (attachment_stream);
995 tny_mime_part_set_filename (attachment_part, attachment_filename);
997 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
998 /* g_object_unref (attachment_part); */
1003 static TnyMimePart *
1004 add_body_part (TnyMsg *msg,
1006 const gchar *content_type,
1007 gboolean has_attachments)
1009 TnyMimePart *text_body_part = NULL;
1010 TnyStream *text_body_stream;
1012 /* Create the stream */
1013 text_body_stream = TNY_STREAM (tny_camel_stream_new
1014 (camel_stream_mem_new_with_buffer
1015 (body, strlen(body))));
1017 /* Create body part if needed */
1018 if (has_attachments)
1020 TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
1022 text_body_part = TNY_MIME_PART(msg);
1024 /* Construct MIME part */
1025 tny_stream_reset (text_body_stream);
1026 tny_mime_part_construct_from_stream (text_body_part,
1029 tny_stream_reset (text_body_stream);
1031 /* Add part if needed */
1032 if (has_attachments) {
1033 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1034 g_object_unref (G_OBJECT(text_body_part));
1038 g_object_unref (text_body_stream);
1040 return text_body_part;