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 *self,
83 TnyFolderStore *parent,
84 gboolean delete_original);
86 static void modest_mail_operation_xfer_msg (ModestMailOperation *self,
89 gboolean delete_original);
91 static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *self,
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));
167 * ModestMailOperation::progress-changed
168 * @self: the #MailOperation that emits the signal
169 * @user_data: user data set when the signal handler was connected
171 * Emitted when the progress of a mail operation changes
173 signals[PROGRESS_CHANGED_SIGNAL] =
174 g_signal_new ("progress_changed",
175 G_TYPE_FROM_CLASS (gobject_class),
177 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
179 g_cclosure_marshal_VOID__VOID,
184 modest_mail_operation_init (ModestMailOperation *obj)
186 ModestMailOperationPrivate *priv;
188 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
190 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
197 modest_mail_operation_finalize (GObject *obj)
199 ModestMailOperationPrivate *priv;
201 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
204 g_error_free (priv->error);
208 G_OBJECT_CLASS(parent_class)->finalize (obj);
212 modest_mail_operation_new (void)
214 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
219 modest_mail_operation_send_mail (ModestMailOperation *self,
220 TnyTransportAccount *transport_account,
223 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
224 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
226 tny_transport_account_send (transport_account, msg, NULL); /* FIXME */
230 modest_mail_operation_send_new_mail (ModestMailOperation *self,
231 TnyTransportAccount *transport_account,
236 const gchar *subject,
238 const GList *attachments_list)
243 ModestMailOperationPrivate *priv = NULL;
245 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
246 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
248 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
250 /* Check parametters */
252 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
253 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
254 _("Error trying to send a mail. You need to set almost one a recipient"));
259 new_msg = TNY_MSG (tny_camel_msg_new ());
260 header = TNY_HEADER (tny_camel_header_new ());
262 /* WARNING: set the header before assign values to it */
263 tny_msg_set_header (new_msg, header);
264 tny_header_set_from (TNY_HEADER (header), from);
265 tny_header_set_replyto (TNY_HEADER (header), from);
266 tny_header_set_to (TNY_HEADER (header), to);
267 tny_header_set_cc (TNY_HEADER (header), cc);
268 tny_header_set_bcc (TNY_HEADER (header), bcc);
269 tny_header_set_subject (TNY_HEADER (header), subject);
271 content_type = get_content_type(body);
273 /* Add the body of the new mail */
274 add_body_part (new_msg, body, (const gchar *) content_type,
275 (attachments_list == NULL) ? FALSE : TRUE);
277 /* Add attachments */
278 add_attachments (new_msg, (GList*) attachments_list);
281 tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */
284 g_object_unref (header);
285 g_object_unref (new_msg);
286 g_free(content_type);
290 add_if_attachment (gpointer data, gpointer user_data)
293 GList *attachments_list;
295 part = TNY_MIME_PART (data);
296 attachments_list = (GList *) user_data;
298 if (tny_mime_part_is_attachment (part))
299 attachments_list = g_list_prepend (attachments_list, part);
304 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
307 TnyHeader *new_header, *header;
310 ModestFormatter *formatter;
312 /* Get body from original msg */
313 header = tny_msg_get_header (msg);
314 body = modest_tny_msg_actions_find_body_part (msg, TRUE);
316 /* TODO: select the formatter from account prefs */
317 formatter = modest_formatter_new ("text/plain");
319 /* Format message body */
322 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
324 new_msg = modest_formatter_cite (formatter, body, header);
326 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
327 new_msg = modest_formatter_quote (formatter, body, header);
332 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
334 new_msg = modest_formatter_inline (formatter, body, header);
336 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
337 new_msg = modest_formatter_attach (formatter, body, header);
341 g_object_unref (G_OBJECT (formatter));
343 /* Fill the header */
344 new_header = TNY_HEADER (tny_camel_header_new ());
345 tny_msg_set_header (new_msg, new_header);
346 tny_header_set_from (new_header, from);
347 tny_header_set_replyto (new_header, from);
349 /* Change the subject */
350 new_subject = (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
351 (is_reply) ? _("Re:") : _("Fwd:"));
352 tny_header_set_subject (new_header, (const gchar *) new_subject);
353 g_free (new_subject);
356 g_object_unref (G_OBJECT (new_header));
357 g_object_unref (G_OBJECT (header));
363 modest_mail_operation_create_forward_mail (TnyMsg *msg,
365 ModestMailOperationForwardType forward_type)
368 TnyList *parts = NULL;
369 GList *attachments_list = NULL;
371 new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
373 /* Add attachments */
374 parts = TNY_LIST (tny_simple_list_new());
375 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
376 tny_list_foreach (parts, add_if_attachment, attachments_list);
377 add_attachments (new_msg, attachments_list);
380 if (attachments_list) g_list_free (attachments_list);
381 g_object_unref (G_OBJECT (parts));
387 modest_mail_operation_create_reply_mail (TnyMsg *msg,
389 ModestMailOperationReplyType reply_type,
390 ModestMailOperationReplyMode reply_mode)
393 TnyHeader *new_header, *header;
394 const gchar* reply_to;
396 new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
398 /* Fill the header */
399 header = tny_msg_get_header (msg);
400 new_header = tny_msg_get_header (new_msg);
401 reply_to = tny_header_get_replyto (header);
403 tny_header_set_to (new_header, reply_to);
405 tny_header_set_to (new_header, tny_header_get_from (header));
407 switch (reply_mode) {
408 gchar *new_cc = NULL;
409 const gchar *cc = NULL, *bcc = NULL;
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;
512 RefreshFolderAsyncHelper *helper;
514 self = MODEST_MAIL_OPERATION (user_data);
515 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
518 priv->error = g_error_copy (*err);
519 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
523 priv->total = tny_list_get_length (list);
525 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
527 helper = g_slice_new0 (RefreshFolderAsyncHelper);
528 helper->mail_op = self;
529 helper->iter = tny_list_create_iterator (list);
531 helper->canceled = 0;
533 /* Async refresh folders */
534 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
541 modest_mail_operation_update_account (ModestMailOperation *self,
542 TnyStoreAccount *store_account)
544 ModestMailOperationPrivate *priv;
546 TnyFolderStoreQuery *query;
548 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
549 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
551 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
553 /* Get subscribed folders & refresh them */
554 folders = TNY_LIST (tny_simple_list_new ());
555 query = tny_folder_store_query_new ();
556 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
557 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
558 folders, update_folders_cb, query, self);
559 g_object_unref (query);
564 ModestMailOperationStatus
565 modest_mail_operation_get_status (ModestMailOperation *self)
567 ModestMailOperationPrivate *priv;
569 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
570 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
571 MODEST_MAIL_OPERATION_STATUS_INVALID);
573 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
578 modest_mail_operation_get_error (ModestMailOperation *self)
580 ModestMailOperationPrivate *priv;
582 g_return_val_if_fail (self, NULL);
583 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
585 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
590 modest_mail_operation_cancel (ModestMailOperation *self)
597 modest_mail_operation_get_task_done (ModestMailOperation *self)
599 ModestMailOperationPrivate *priv;
601 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
603 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
608 modest_mail_operation_get_task_total (ModestMailOperation *self)
610 ModestMailOperationPrivate *priv;
612 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
614 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
619 modest_mail_operation_is_finished (ModestMailOperation *self)
621 ModestMailOperationPrivate *priv;
622 gboolean retval = FALSE;
624 if (!MODEST_IS_MAIL_OPERATION (self)) {
625 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
629 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
631 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
632 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
633 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
634 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
643 /* ******************************************************************* */
644 /* ************************** STORE ACTIONS ************************* */
645 /* ******************************************************************* */
649 modest_mail_operation_create_folder (ModestMailOperation *self,
650 TnyFolderStore *parent,
653 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
654 g_return_val_if_fail (name, NULL);
656 TnyFolder *new_folder = NULL;
657 TnyStoreAccount *store_account;
659 /* Create the folder */
660 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
664 /* Subscribe to folder */
665 if (!tny_folder_is_subscribed (new_folder)) {
666 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
667 tny_store_account_subscribe (store_account, new_folder);
668 g_object_unref (G_OBJECT (store_account));
675 modest_mail_operation_remove_folder (ModestMailOperation *self,
677 gboolean remove_to_trash)
679 TnyFolderStore *folder_store;
681 g_return_if_fail (TNY_IS_FOLDER (folder));
683 /* Get folder store */
684 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
686 /* Delete folder or move to trash */
687 if (remove_to_trash) {
688 TnyFolder *trash_folder;
690 trash_folder = modest_mail_operation_find_trash_folder (self,
691 TNY_STORE_ACCOUNT (folder_store));
693 /* TODO: error_handling */
694 modest_mail_operation_move_folder (self,
696 TNY_FOLDER_STORE (trash_folder));
698 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
699 g_object_unref (G_OBJECT (folder));
703 g_object_unref (G_OBJECT (folder_store));
707 modest_mail_operation_rename_folder (ModestMailOperation *self,
711 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
712 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
713 g_return_if_fail (name);
715 /* FIXME: better error handling */
716 if (strrchr (name, '/') != NULL)
719 /* Rename. Camel handles folder subscription/unsubscription */
720 tny_folder_set_name (folder, name, NULL); /* FIXME */
724 modest_mail_operation_move_folder (ModestMailOperation *self,
726 TnyFolderStore *parent)
728 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
729 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
730 g_return_if_fail (TNY_IS_FOLDER (folder));
732 modest_mail_operation_xfer_folder (self, folder, parent, TRUE);
736 modest_mail_operation_copy_folder (ModestMailOperation *self,
738 TnyFolderStore *parent)
740 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
741 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
742 g_return_if_fail (TNY_IS_FOLDER (folder));
744 modest_mail_operation_xfer_folder (self, folder, parent, FALSE);
748 modest_mail_operation_xfer_folder (ModestMailOperation *self,
750 TnyFolderStore *parent,
751 gboolean delete_original)
753 const gchar *folder_name;
754 TnyFolder *dest_folder, *child;
756 TnyList *folders, *headers;
758 g_return_if_fail (TNY_IS_FOLDER (folder));
759 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
761 /* Create the destination folder */
762 folder_name = tny_folder_get_name (folder);
763 dest_folder = modest_mail_operation_create_folder (self,
764 parent, folder_name);
766 /* Transfer messages */
767 headers = TNY_LIST (tny_simple_list_new ());
768 tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
769 tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
771 /* Recurse children */
772 folders = TNY_LIST (tny_simple_list_new ());
773 tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
774 iter = tny_list_create_iterator (folders);
776 while (!tny_iterator_is_done (iter)) {
778 child = TNY_FOLDER (tny_iterator_get_current (iter));
779 modest_mail_operation_xfer_folder (self, child,
780 TNY_FOLDER_STORE (dest_folder),
782 tny_iterator_next (iter);
785 /* Delete source folder (if needed) */
787 modest_mail_operation_remove_folder (self, folder, FALSE);
790 g_object_unref (G_OBJECT (dest_folder));
791 g_object_unref (G_OBJECT (headers));
792 g_object_unref (G_OBJECT (folders));
793 g_object_unref (G_OBJECT (iter));
797 /* FIXME: this method should be rewritten when the policy for the
798 Trash folder becomes clearer */
800 modest_mail_operation_find_trash_folder (ModestMailOperation *self,
801 TnyStoreAccount *store_account)
806 /*TnyFolderStoreQuery *query;*/
807 TnyFolder *trash_folder;
809 /* Look for Trash folder */
810 folders = TNY_LIST (tny_simple_list_new ());
811 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
812 folders, NULL, NULL); /* FIXME */
813 iter = tny_list_create_iterator (folders);
816 while (!tny_iterator_is_done (iter) && !found) {
818 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
819 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
822 tny_iterator_next (iter);
826 g_object_unref (G_OBJECT (folders));
827 g_object_unref (G_OBJECT (iter));
829 /* TODO: better error handling management */
836 /* ******************************************************************* */
837 /* ************************** MSG ACTIONS ************************* */
838 /* ******************************************************************* */
841 modest_mail_operation_copy_msg (ModestMailOperation *self,
845 g_return_if_fail (TNY_IS_HEADER (header));
846 g_return_if_fail (TNY_IS_FOLDER (folder));
848 modest_mail_operation_xfer_msg (self, header, folder, FALSE);
852 modest_mail_operation_move_msg (ModestMailOperation *self,
856 g_return_if_fail (TNY_IS_HEADER (header));
857 g_return_if_fail (TNY_IS_FOLDER (folder));
859 modest_mail_operation_xfer_msg (self, header, folder, TRUE);
863 modest_mail_operation_remove_msg (ModestMailOperation *self,
865 gboolean remove_to_trash)
869 g_return_if_fail (TNY_IS_HEADER (header));
871 folder = tny_header_get_folder (header);
873 /* Delete or move to trash */
874 if (remove_to_trash) {
875 TnyFolder *trash_folder;
876 TnyStoreAccount *store_account;
878 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
879 trash_folder = modest_mail_operation_find_trash_folder (self, store_account);
881 modest_mail_operation_move_msg (self, header, trash_folder);
883 g_object_unref (G_OBJECT (store_account));
885 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
886 tny_folder_expunge (folder, NULL); /* FIXME */
890 g_object_unref (folder);
894 modest_mail_operation_xfer_msg (ModestMailOperation *self,
897 gboolean delete_original)
899 TnyFolder *src_folder;
902 src_folder = tny_header_get_folder (header);
903 headers = tny_simple_list_new ();
906 tny_list_prepend (headers, G_OBJECT (header));
907 tny_folder_transfer_msgs (src_folder, headers, folder, delete_original, NULL); /* FIXME */
910 g_object_unref (headers);
911 g_object_unref (folder);
915 /* ******************************************************************* */
916 /* ************************* UTILIY FUNCTIONS ************************ */
917 /* ******************************************************************* */
919 is_ascii(const gchar *s)
922 if (s[0] & 128 || s[0] < 32)
930 get_content_type(const gchar *s)
934 type = g_string_new("text/plain");
936 if (g_utf8_validate(s, -1, NULL)) {
937 g_string_append(type, "; charset=\"utf-8\"");
939 /* it should be impossible to reach this, but better safe than sorry */
940 g_warning("invalid utf8 in message");
941 g_string_append(type, "; charset=\"latin1\"");
944 return g_string_free(type, FALSE);
948 modest_error_quark (void)
950 static GQuark err_q = 0;
953 err_q = g_quark_from_static_string ("modest-error-quark");
959 add_attachments (TnyMsg *msg, GList *attachments_list)
962 TnyMimePart *attachment_part, *old_attachment;
963 const gchar *attachment_content_type;
964 const gchar *attachment_filename;
965 TnyStream *attachment_stream;
967 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
969 old_attachment = pos->data;
970 attachment_filename = tny_mime_part_get_filename (old_attachment);
971 attachment_stream = tny_mime_part_get_stream (old_attachment);
972 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
974 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
976 tny_mime_part_construct_from_stream (attachment_part,
978 attachment_content_type);
979 tny_stream_reset (attachment_stream);
981 tny_mime_part_set_filename (attachment_part, attachment_filename);
983 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
984 /* g_object_unref (attachment_part); */
990 add_body_part (TnyMsg *msg,
992 const gchar *content_type,
993 gboolean has_attachments)
995 TnyMimePart *text_body_part = NULL;
996 TnyStream *text_body_stream;
998 /* Create the stream */
999 text_body_stream = TNY_STREAM (tny_camel_stream_new
1000 (camel_stream_mem_new_with_buffer
1001 (body, strlen(body))));
1003 /* Create body part if needed */
1004 if (has_attachments)
1006 TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
1008 text_body_part = TNY_MIME_PART(msg);
1010 /* Construct MIME part */
1011 tny_stream_reset (text_body_stream);
1012 tny_mime_part_construct_from_stream (text_body_part,
1015 tny_stream_reset (text_body_stream);
1017 /* Add part if needed */
1018 if (has_attachments) {
1019 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1020 g_object_unref (G_OBJECT(text_body_part));
1024 g_object_unref (text_body_stream);
1026 return text_body_part;