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"
51 /* 'private'/'protected' functions */
52 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
53 static void modest_mail_operation_init (ModestMailOperation *obj);
54 static void modest_mail_operation_finalize (GObject *obj);
56 #define MODEST_ERROR modest_error_quark ()
58 typedef enum _ModestMailOperationErrorCode ModestMailOperationErrorCode;
59 enum _ModestMailOperationErrorCode {
60 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
61 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
63 MODEST_MAIL_OPERATION_NUM_ERROR_CODES
68 ModestMailOperation *mail_op;
73 static void set_error (ModestMailOperation *mail_operation,
74 ModestMailOperationErrorCode error_code,
75 const gchar *fmt, ...);
76 static void status_update_cb (TnyFolder *folder,
80 static void folder_refresh_cb (TnyFolder *folder,
84 static void add_attachments (TnyMsg *msg,
85 const GList *attachments_list);
88 static TnyMimePart * add_body_part (TnyMsg *msg,
90 const gchar *content_type,
91 gboolean has_attachments);
94 static void modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
96 TnyFolderStore *parent,
97 gboolean delete_original);
99 static void modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
102 gboolean delete_original);
104 static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
105 TnyStoreAccount *store_account);
108 enum _ModestMailOperationSignals
110 PROGRESS_CHANGED_SIGNAL,
115 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
116 struct _ModestMailOperationPrivate {
120 ModestMailOperationStatus status;
123 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
124 MODEST_TYPE_MAIL_OPERATION, \
125 ModestMailOperationPrivate))
127 /* some utility functions */
128 static char * get_content_type(const gchar *s);
129 static gboolean is_ascii(const gchar *s);
132 static GObjectClass *parent_class = NULL;
134 static guint signals[NUM_SIGNALS] = {0};
137 modest_mail_operation_get_type (void)
139 static GType my_type = 0;
141 static const GTypeInfo my_info = {
142 sizeof(ModestMailOperationClass),
143 NULL, /* base init */
144 NULL, /* base finalize */
145 (GClassInitFunc) modest_mail_operation_class_init,
146 NULL, /* class finalize */
147 NULL, /* class data */
148 sizeof(ModestMailOperation),
150 (GInstanceInitFunc) modest_mail_operation_init,
153 my_type = g_type_register_static (G_TYPE_OBJECT,
154 "ModestMailOperation",
161 modest_mail_operation_class_init (ModestMailOperationClass *klass)
163 GObjectClass *gobject_class;
164 gobject_class = (GObjectClass*) klass;
166 parent_class = g_type_class_peek_parent (klass);
167 gobject_class->finalize = modest_mail_operation_finalize;
169 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
171 /* signal definitions go here, e.g.: */
172 signals[PROGRESS_CHANGED_SIGNAL] =
173 g_signal_new ("progress_changed",
174 G_TYPE_FROM_CLASS (gobject_class),
176 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
178 g_cclosure_marshal_VOID__VOID,
183 modest_mail_operation_init (ModestMailOperation *obj)
185 ModestMailOperationPrivate *priv;
187 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
189 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
193 priv->cb_lock = g_mutex_new ();
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_mutex_free (priv->cb_lock);
210 G_OBJECT_CLASS(parent_class)->finalize (obj);
214 modest_mail_operation_new (void)
216 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
221 modest_mail_operation_send_mail (ModestMailOperation *mail_op,
222 TnyTransportAccount *transport_account,
225 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
226 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
228 tny_transport_account_send (transport_account, msg, NULL); /* FIXME */
232 modest_mail_operation_send_new_mail (ModestMailOperation *mail_op,
233 TnyTransportAccount *transport_account,
238 const gchar *subject,
240 const GList *attachments_list)
246 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
247 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
249 /* Check parametters */
252 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
253 _("Error trying to send a mail. You need to set almost one a recipient"));
258 new_msg = TNY_MSG (tny_camel_msg_new ());
259 header = TNY_HEADER (tny_camel_header_new ());
261 /* WARNING: set the header before assign values to it */
262 tny_msg_set_header (new_msg, header);
263 tny_header_set_from (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, 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 * modest_mail_operation_create_forward_mail:
289 * @msg: a valid #TnyMsg instance
290 * @forward_type: the type of forwarded message
292 * creates a forwarded message from an existing one
294 * Returns: a new #TnyMsg, or NULL in case of error
297 modest_mail_operation_create_forward_mail (TnyMsg *msg,
299 ModestMailOperationForwardType forward_type)
302 TnyHeader *new_header, *header;
303 gchar *new_subject, *new_body, *content_type;
304 TnyMimePart *text_body_part = NULL;
305 GList *attachments_list;
309 g_return_val_if_fail (TNY_IS_MSG (msg), NULL);
310 g_return_val_if_fail (from != NULL , NULL);
311 g_return_val_if_fail (forward_type > 0, NULL);
313 /* Create new objects */
314 new_msg = TNY_MSG (tny_camel_msg_new ());
315 new_header = TNY_HEADER (tny_camel_header_new ());
317 header = tny_msg_get_header (msg);
319 /* Fill the header */
320 tny_msg_set_header (new_msg, new_header);
321 tny_header_set_from (new_header, from);
323 /* Change the subject */
324 new_subject = (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
326 tny_header_set_subject (new_header, (const gchar *) new_subject);
327 g_free (new_subject);
329 /* Get body from original msg */
330 new_body = (gchar *) modest_tny_msg_actions_find_body (msg, TRUE);
332 g_object_unref (new_msg);
335 content_type = get_content_type(new_body);
337 /* Create the list of attachments */
338 parts = TNY_LIST (tny_simple_list_new());
339 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
340 iter = tny_list_create_iterator (parts);
341 attachments_list = NULL;
343 while (!tny_iterator_is_done(iter)) {
346 part = TNY_MIME_PART (tny_iterator_get_current (iter));
347 if (tny_mime_part_is_attachment (part))
348 attachments_list = g_list_prepend (attachments_list, part);
350 tny_iterator_next (iter);
353 /* Add attachments */
354 add_attachments (new_msg, attachments_list);
356 switch (forward_type) {
357 TnyMimePart *attachment_part;
360 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
361 /* Prepend "Original message" text */
362 inlined_text = (gchar *)
363 modest_text_utils_inlined_text (tny_header_get_from (header),
364 tny_header_get_date_sent (header),
365 tny_header_get_to (header),
366 tny_header_get_subject (header),
367 (const gchar*) new_body);
369 new_body = inlined_text;
372 add_body_part (new_msg, new_body,
373 (const gchar *) content_type,
374 (tny_list_get_length (parts) > 0) ? TRUE : FALSE);
377 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
378 attachment_part = add_body_part (new_msg, new_body,
379 (const gchar *) content_type, TRUE);
381 /* Set the subject as the name of the attachment */
382 tny_mime_part_set_filename (attachment_part, tny_header_get_subject (header));
386 g_warning (_("Invalid forward type"));
391 if (attachments_list) g_list_free (attachments_list);
392 g_object_unref (parts);
393 if (text_body_part) g_free (text_body_part);
394 g_free (content_type);
401 * modest_mail_operation_create_reply_mail:
402 * @msg: a valid #TnyMsg instance
403 * @reply_type: the format of the new message
404 * @reply_mode: the mode of reply, to the sender only, to a mail list or to all
406 * creates a new message to reply to an existing one
408 * Returns: Returns: a new #TnyMsg, or NULL in case of error
411 modest_mail_operation_create_reply_mail (TnyMsg *msg,
413 ModestMailOperationReplyType reply_type,
414 ModestMailOperationReplyMode reply_mode)
417 TnyHeader *new_header, *header;
418 gchar *new_subject, *new_body, *content_type, *quoted;
419 TnyMimePart *text_body_part;
421 /* Create new objects */
422 new_msg = TNY_MSG (tny_camel_msg_new ());
423 new_header = TNY_HEADER (tny_camel_header_new ());
424 header = tny_msg_get_header (msg);
426 /* Fill the header */
427 tny_msg_set_header (new_msg, new_header);
428 tny_header_set_to (new_header, tny_header_get_from (header));
429 tny_header_set_from (new_header, from);
431 switch (reply_mode) {
432 gchar *new_cc = NULL;
433 const gchar *cc = NULL, *bcc = NULL;
436 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
437 /* Do not fill neither cc nor bcc */
439 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
442 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
443 /* Concatenate to, cc and bcc */
444 cc = tny_header_get_cc (header);
445 bcc = tny_header_get_bcc (header);
447 tmp = g_string_new (tny_header_get_to (header));
448 if (cc) g_string_append_printf (tmp, ",%s",cc);
449 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
451 /* Remove my own address from the cc list */
453 modest_text_utils_remove_address ((const gchar *) tmp->str,
454 (const gchar *) from);
455 /* FIXME: remove also the mails from the new To: */
456 tny_header_set_cc (new_header, new_cc);
459 g_string_free (tmp, TRUE);
464 /* Change the subject */
465 new_subject = (gchar*) modest_text_utils_derived_subject (tny_header_get_subject(header),
467 tny_header_set_subject (new_header, (const gchar *) new_subject);
468 g_free (new_subject);
470 /* Get body from original msg */
471 new_body = (gchar*) modest_tny_msg_actions_find_body (msg, TRUE);
473 g_object_unref (new_msg);
476 content_type = get_content_type(new_body);
478 switch (reply_type) {
481 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
482 /* Prepend "Original message" text */
483 cited_text = (gchar *) modest_text_utils_cited_text (tny_header_get_from (header),
484 tny_header_get_date_sent (header),
485 (const gchar*) new_body);
487 new_body = cited_text;
489 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
490 /* FIXME: replace 80 with a value from ModestConf */
491 quoted = (gchar*) modest_text_utils_quote (new_body,
492 tny_header_get_from (header),
493 tny_header_get_date_sent (header),
500 text_body_part = add_body_part (new_msg, new_body,
501 (const gchar *) content_type, TRUE);
504 /* g_free (text_body_part); */
505 g_free (content_type);
512 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gpointer user_data)
514 g_print ("%s status: %d\n", what, status);
518 folder_refresh_cb (TnyFolder *folder, gboolean cancelled, GError **err, gpointer user_data)
520 AsyncHelper *helper = NULL;
521 ModestMailOperation *mail_op = NULL;
522 ModestMailOperationPrivate *priv = NULL;
524 helper = (AsyncHelper *) user_data;
525 mail_op = MODEST_MAIL_OPERATION (helper->mail_op);
526 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
528 g_mutex_lock (priv->cb_lock);
533 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELLED;
535 g_signal_emit (G_OBJECT (mail_op), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
537 g_mutex_unlock (priv->cb_lock);
539 if (priv->done == priv->total) {
540 ((ModestUpdateAccountCallback) (helper->cb)) (mail_op, helper->user_data);
541 g_slice_free (AsyncHelper, helper);
546 modest_mail_operation_update_account (ModestMailOperation *mail_op,
547 TnyStoreAccount *store_account,
548 ModestUpdateAccountCallback callback,
551 ModestMailOperationPrivate *priv;
553 TnyIterator *ifolders;
554 TnyFolder *cur_folder;
555 TnyFolderStoreQuery *query;
558 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
559 g_return_if_fail (TNY_IS_STORE_ACCOUNT(store_account));
561 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
563 /* Get subscribed folders */
564 folders = TNY_LIST (tny_simple_list_new ());
565 query = tny_folder_store_query_new ();
566 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
567 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
568 folders, query, NULL); /* FIXME */
569 g_object_unref (query);
571 ifolders = tny_list_create_iterator (folders);
572 priv->total = tny_list_get_length (folders);
576 /* Async refresh folders */
577 for (tny_iterator_first (ifolders);
578 !tny_iterator_is_done (ifolders);
579 tny_iterator_next (ifolders)) {
581 cur_folder = TNY_FOLDER (tny_iterator_get_current (ifolders));
582 helper = g_slice_new0 (AsyncHelper);
583 helper->mail_op = mail_op;
584 helper->user_data = user_data;
585 helper->cb = G_CALLBACK (callback);
587 tny_folder_refresh_async (cur_folder, folder_refresh_cb,
588 status_update_cb, helper);
591 g_object_unref (ifolders);
594 ModestMailOperationStatus
595 modest_mail_operation_get_status (ModestMailOperation *mail_op)
597 ModestMailOperationPrivate *priv;
599 /* g_return_val_if_fail (mail_op, MODEST_MAIL_OPERATION_STATUS_INVALID); */
600 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), */
601 /* MODEST_MAIL_OPERATION_STATUS_INVALID); */
603 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
608 modest_mail_operation_get_error (ModestMailOperation *mail_op)
610 ModestMailOperationPrivate *priv;
612 /* g_return_val_if_fail (mail_op, NULL); */
613 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), NULL); */
615 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
620 modest_mail_operation_cancel (ModestMailOperation *mail_op)
626 modest_mail_operation_get_task_done (ModestMailOperation *mail_op)
628 ModestMailOperationPrivate *priv;
630 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
632 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
637 modest_mail_operation_get_task_total (ModestMailOperation *mail_op)
639 ModestMailOperationPrivate *priv;
641 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
643 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
647 /* ******************************************************************* */
648 /* ************************** STORE ACTIONS ************************* */
649 /* ******************************************************************* */
653 modest_mail_operation_create_folder (ModestMailOperation *mail_op,
654 TnyFolderStore *parent,
657 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
658 g_return_val_if_fail (name, NULL);
660 TnyFolder *new_folder = NULL;
661 TnyStoreAccount *store_account;
663 /* Create the folder */
664 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
668 /* Subscribe to folder */
669 if (!tny_folder_is_subscribed (new_folder)) {
670 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
671 tny_store_account_subscribe (store_account, new_folder);
672 g_object_unref (G_OBJECT (store_account));
679 modest_mail_operation_remove_folder (ModestMailOperation *mail_op,
681 gboolean remove_to_trash)
683 TnyFolderStore *folder_store;
685 g_return_if_fail (TNY_IS_FOLDER (folder));
687 /* Get folder store */
688 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
690 /* Delete folder or move to trash */
691 if (remove_to_trash) {
692 TnyFolder *trash_folder;
694 trash_folder = modest_mail_operation_find_trash_folder (mail_op,
695 TNY_STORE_ACCOUNT (folder_store));
697 /* TODO: error_handling */
698 modest_mail_operation_move_folder (mail_op,
700 TNY_FOLDER_STORE (trash_folder));
702 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
703 g_object_unref (G_OBJECT (folder));
707 g_object_unref (G_OBJECT (folder_store));
711 modest_mail_operation_rename_folder (ModestMailOperation *mail_op,
715 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
716 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
718 /* FIXME: better error handling */
719 if (strrchr (name, '/') != NULL)
722 /* Rename. Camel handles folder subscription/unsubscription */
723 tny_folder_set_name (folder, name, NULL); /* FIXME */
727 modest_mail_operation_move_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, TRUE);
739 modest_mail_operation_copy_folder (ModestMailOperation *mail_op,
741 TnyFolderStore *parent)
743 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
744 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
745 g_return_if_fail (TNY_IS_FOLDER (folder));
747 modest_mail_operation_xfer_folder (mail_op, folder, parent, FALSE);
751 modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
753 TnyFolderStore *parent,
754 gboolean delete_original)
756 const gchar *folder_name;
757 TnyFolder *dest_folder, *child;
759 TnyList *folders, *headers;
761 g_return_if_fail (TNY_IS_FOLDER (folder));
762 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
764 /* Create the destination folder */
765 folder_name = tny_folder_get_name (folder);
766 dest_folder = modest_mail_operation_create_folder (mail_op,
767 parent, folder_name);
769 /* Transfer messages */
770 headers = TNY_LIST (tny_simple_list_new ());
771 tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
772 tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
774 /* Recurse children */
775 folders = TNY_LIST (tny_simple_list_new ());
776 tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
777 iter = tny_list_create_iterator (folders);
779 while (!tny_iterator_is_done (iter)) {
781 child = TNY_FOLDER (tny_iterator_get_current (iter));
782 modest_mail_operation_xfer_folder (mail_op, child,
783 TNY_FOLDER_STORE (dest_folder),
785 tny_iterator_next (iter);
788 /* Delete source folder (if needed) */
790 modest_mail_operation_remove_folder (mail_op, folder, FALSE);
793 g_object_unref (G_OBJECT (dest_folder));
794 g_object_unref (G_OBJECT (headers));
795 g_object_unref (G_OBJECT (folders));
796 g_object_unref (G_OBJECT (iter));
800 modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
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 *mail_op,
845 g_return_if_fail (TNY_IS_HEADER (header));
846 g_return_if_fail (TNY_IS_FOLDER (folder));
848 modest_mail_operation_xfer_msg (mail_op, header, folder, FALSE);
852 modest_mail_operation_move_msg (ModestMailOperation *mail_op,
856 g_return_if_fail (TNY_IS_HEADER (header));
857 g_return_if_fail (TNY_IS_FOLDER (folder));
859 modest_mail_operation_xfer_msg (mail_op, header, folder, TRUE);
863 modest_mail_operation_remove_msg (ModestMailOperation *mail_op,
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 (mail_op, store_account);
881 modest_mail_operation_move_msg (mail_op, 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 *mail_op,
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");
960 set_error (ModestMailOperation *mail_op,
961 ModestMailOperationErrorCode error_code,
962 const gchar *fmt, ...)
964 ModestMailOperationPrivate *priv;
969 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
971 va_start (args, fmt);
973 orig = g_strdup_vprintf(fmt, args);
974 error = g_error_new (MODEST_ERROR, error_code, orig);
979 g_object_unref (priv->error);
982 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
986 add_attachments (TnyMsg *msg, const GList *attachments_list)
989 TnyMimePart *attachment_part, *old_attachment;
990 const gchar *attachment_content_type;
991 const gchar *attachment_filename;
992 TnyStream *attachment_stream;
994 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
996 old_attachment = pos->data;
997 attachment_filename = tny_mime_part_get_filename (old_attachment);
998 attachment_stream = tny_mime_part_get_stream (old_attachment);
999 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
1001 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
1003 tny_mime_part_construct_from_stream (attachment_part,
1005 attachment_content_type);
1006 tny_stream_reset (attachment_stream);
1008 tny_mime_part_set_filename (attachment_part, attachment_filename);
1010 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
1011 /* g_object_unref (attachment_part); */
1016 static TnyMimePart *
1017 add_body_part (TnyMsg *msg,
1019 const gchar *content_type,
1020 gboolean has_attachments)
1022 TnyMimePart *text_body_part = NULL;
1023 TnyStream *text_body_stream;
1025 /* Create the stream */
1026 text_body_stream = TNY_STREAM (tny_camel_stream_new
1027 (camel_stream_mem_new_with_buffer
1028 (body, strlen(body))));
1030 /* Create body part if needed */
1031 if (has_attachments)
1033 TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
1035 text_body_part = TNY_MIME_PART(msg);
1037 /* Construct MIME part */
1038 tny_stream_reset (text_body_stream);
1039 tny_mime_part_construct_from_stream (text_body_part,
1042 tny_stream_reset (text_body_stream);
1044 /* Add part if needed */
1045 if (has_attachments) {
1046 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1047 g_object_unref (G_OBJECT(text_body_part));
1051 g_object_unref (text_body_stream);
1053 return text_body_part;