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>
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
65 static void set_error (ModestMailOperation *mail_operation,
66 ModestMailOperationErrorCode error_code,
67 const gchar *fmt, ...);
68 static void status_update_cb (TnyFolder *folder,
72 static void folder_refresh_cb (TnyFolder *folder,
75 static void add_attachments (TnyMsg *msg,
76 const GList *attachments_list);
79 static TnyMimePart * add_body_part (TnyMsg *msg,
81 const gchar *content_type,
82 gboolean has_attachments);
85 static void modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
87 TnyFolderStore *parent,
88 gboolean delete_original);
90 static void modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
93 gboolean delete_original);
95 static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
96 TnyStoreAccount *store_account);
106 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
107 struct _ModestMailOperationPrivate {
108 ModestMailOperationStatus status;
111 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
112 MODEST_TYPE_MAIL_OPERATION, \
113 ModestMailOperationPrivate))
115 /* some utility functions */
116 static char * get_content_type(const gchar *s);
117 static gboolean is_ascii(const gchar *s);
120 static GObjectClass *parent_class = NULL;
122 /* uncomment the following if you have defined any signals */
123 /* static guint signals[LAST_SIGNAL] = {0}; */
126 modest_mail_operation_get_type (void)
128 static GType my_type = 0;
130 static const GTypeInfo my_info = {
131 sizeof(ModestMailOperationClass),
132 NULL, /* base init */
133 NULL, /* base finalize */
134 (GClassInitFunc) modest_mail_operation_class_init,
135 NULL, /* class finalize */
136 NULL, /* class data */
137 sizeof(ModestMailOperation),
139 (GInstanceInitFunc) modest_mail_operation_init,
142 my_type = g_type_register_static (G_TYPE_OBJECT,
143 "ModestMailOperation",
150 modest_mail_operation_class_init (ModestMailOperationClass *klass)
152 GObjectClass *gobject_class;
153 gobject_class = (GObjectClass*) klass;
155 parent_class = g_type_class_peek_parent (klass);
156 gobject_class->finalize = modest_mail_operation_finalize;
158 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
160 /* signal definitions go here, e.g.: */
161 /* signals[MY_SIGNAL_1] = */
162 /* g_signal_new ("my_signal_1",....); */
163 /* signals[MY_SIGNAL_2] = */
164 /* g_signal_new ("my_signal_2",....); */
169 modest_mail_operation_init (ModestMailOperation *obj)
171 ModestMailOperationPrivate *priv;
173 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
175 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
180 modest_mail_operation_finalize (GObject *obj)
182 ModestMailOperationPrivate *priv;
184 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
187 g_error_free (priv->error);
191 G_OBJECT_CLASS(parent_class)->finalize (obj);
195 modest_mail_operation_new (void)
197 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
202 modest_mail_operation_send_mail (ModestMailOperation *mail_op,
203 TnyTransportAccount *transport_account,
206 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
207 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
209 tny_transport_account_send (transport_account, msg);
213 modest_mail_operation_send_new_mail (ModestMailOperation *mail_op,
214 TnyTransportAccount *transport_account,
219 const gchar *subject,
221 const GList *attachments_list)
227 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
228 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
230 /* Check parametters */
233 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
234 _("Error trying to send a mail. You need to set almost one a recipient"));
239 new_msg = TNY_MSG (tny_camel_msg_new ());
240 header = TNY_HEADER (tny_camel_header_new ());
242 /* WARNING: set the header before assign values to it */
243 tny_msg_set_header (new_msg, header);
244 tny_header_set_from (TNY_HEADER (header), from);
245 tny_header_set_to (TNY_HEADER (header), to);
246 tny_header_set_cc (TNY_HEADER (header), cc);
247 tny_header_set_bcc (TNY_HEADER (header), bcc);
248 tny_header_set_subject (TNY_HEADER (header), subject);
250 content_type = get_content_type(body);
252 /* Add the body of the new mail */
253 add_body_part (new_msg, body, (const gchar *) content_type,
254 (attachments_list == NULL) ? FALSE : TRUE);
256 /* Add attachments */
257 add_attachments (new_msg, attachments_list);
260 tny_transport_account_send (transport_account, new_msg);
263 g_object_unref (header);
264 g_object_unref (new_msg);
265 g_free(content_type);
269 * modest_mail_operation_create_forward_mail:
270 * @msg: a valid #TnyMsg instance
271 * @forward_type: the type of forwarded message
273 * creates a forwarded message from an existing one
275 * Returns: a new #TnyMsg, or NULL in case of error
278 modest_mail_operation_create_forward_mail (TnyMsg *msg,
280 ModestMailOperationForwardType forward_type)
283 TnyHeader *new_header, *header;
284 gchar *new_subject, *new_body, *content_type;
285 TnyMimePart *text_body_part = NULL;
286 GList *attachments_list;
290 /* Create new objects */
291 new_msg = TNY_MSG (tny_camel_msg_new ());
292 new_header = TNY_HEADER (tny_camel_header_new ());
294 header = tny_msg_get_header (msg);
296 /* Fill the header */
297 tny_msg_set_header (new_msg, new_header);
298 tny_header_set_from (new_header, from);
300 /* Change the subject */
301 new_subject = (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
303 tny_header_set_subject (new_header, (const gchar *) new_subject);
304 g_free (new_subject);
306 /* Get body from original msg */
307 new_body = (gchar *) modest_tny_msg_actions_find_body (msg, FALSE);
309 g_object_unref (new_msg);
312 content_type = get_content_type(new_body);
314 /* Create the list of attachments */
315 parts = TNY_LIST (tny_simple_list_new());
316 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
317 iter = tny_list_create_iterator (parts);
318 attachments_list = NULL;
320 while (!tny_iterator_is_done(iter)) {
323 part = TNY_MIME_PART (tny_iterator_get_current (iter));
324 if (tny_mime_part_is_attachment (part))
325 attachments_list = g_list_prepend (attachments_list, part);
327 tny_iterator_next (iter);
330 /* Add attachments */
331 add_attachments (new_msg, attachments_list);
333 switch (forward_type) {
334 TnyMimePart *attachment_part;
337 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
338 /* Prepend "Original message" text */
339 inlined_text = (gchar *)
340 modest_text_utils_inlined_text (tny_header_get_from (header),
341 tny_header_get_date_sent (header),
342 tny_header_get_to (header),
343 tny_header_get_subject (header),
344 (const gchar*) new_body);
346 new_body = inlined_text;
349 add_body_part (new_msg, new_body,
350 (const gchar *) content_type,
351 (tny_list_get_length (parts) > 0) ? TRUE : FALSE);
354 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
355 attachment_part = add_body_part (new_msg, new_body,
356 (const gchar *) content_type, TRUE);
358 /* Set the subject as the name of the attachment */
359 tny_mime_part_set_filename (attachment_part, tny_header_get_subject (header));
365 if (attachments_list) g_list_free (attachments_list);
366 g_object_unref (parts);
367 if (text_body_part) g_free (text_body_part);
368 g_free (content_type);
375 * modest_mail_operation_create_reply_mail:
376 * @msg: a valid #TnyMsg instance
377 * @reply_type: the format of the new message
378 * @reply_mode: the mode of reply, to the sender only, to a mail list or to all
380 * creates a new message to reply to an existing one
382 * Returns: Returns: a new #TnyMsg, or NULL in case of error
385 modest_mail_operation_create_reply_mail (TnyMsg *msg,
387 ModestMailOperationReplyType reply_type,
388 ModestMailOperationReplyMode reply_mode)
391 TnyHeader *new_header, *header;
392 gchar *new_subject, *new_body, *content_type, *quoted;
393 TnyMimePart *text_body_part;
395 /* Create new objects */
396 new_msg = TNY_MSG (tny_camel_msg_new ());
397 new_header = TNY_HEADER (tny_camel_header_new ());
398 header = tny_msg_get_header (msg);
400 /* Fill the header */
401 tny_msg_set_header (new_msg, new_header);
402 tny_header_set_to (new_header, tny_header_get_from (header));
403 tny_header_set_from (new_header, from);
405 switch (reply_mode) {
406 gchar *new_cc = NULL;
407 const gchar *cc = NULL, *bcc = NULL;
410 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
411 /* Do not fill neither cc nor bcc */
413 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
416 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
417 /* Concatenate to, cc and bcc */
418 cc = tny_header_get_cc (header);
419 bcc = tny_header_get_bcc (header);
421 tmp = g_string_new (tny_header_get_to (header));
422 if (cc) g_string_append_printf (tmp, ",%s",cc);
423 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
425 /* Remove my own address from the cc list */
427 modest_text_utils_remove_address ((const gchar *) tmp->str,
428 (const gchar *) from);
429 /* FIXME: remove also the mails from the new To: */
430 tny_header_set_cc (new_header, new_cc);
433 g_string_free (tmp, TRUE);
438 /* Change the subject */
439 new_subject = (gchar*) modest_text_utils_derived_subject (tny_header_get_subject(header),
441 tny_header_set_subject (new_header, (const gchar *) new_subject);
442 g_free (new_subject);
444 /* Get body from original msg */
445 new_body = (gchar*) modest_tny_msg_actions_find_body (msg, FALSE);
447 g_object_unref (new_msg);
450 content_type = get_content_type(new_body);
452 switch (reply_type) {
455 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
456 /* Prepend "Original message" text */
457 cited_text = (gchar *) modest_text_utils_cited_text (tny_header_get_from (header),
458 tny_header_get_date_sent (header),
459 (const gchar*) new_body);
461 new_body = cited_text;
463 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
464 /* FIXME: replace 80 with a value from ModestConf */
465 quoted = (gchar*) modest_text_utils_quote (new_body,
466 tny_header_get_from (header),
467 tny_header_get_date_sent (header),
474 text_body_part = add_body_part (new_msg, new_body,
475 (const gchar *) content_type, TRUE);
478 /* g_free (text_body_part); */
479 g_free (content_type);
486 modest_mail_operation_update_account (ModestMailOperation *mail_op,
487 TnyStoreAccount *store_account)
489 TnyStoreAccount *storage_account;
490 ModestMailOperationPrivate *priv;
492 TnyIterator *ifolders;
493 TnyFolder *cur_folder;
494 TnyFolderStoreQuery *query;
496 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
497 g_return_if_fail (TNY_IS_STORE_ACCOUNT(store_account));
499 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
501 /* Get subscribed folders */
502 folders = TNY_LIST (tny_simple_list_new ());
503 query = tny_folder_store_query_new ();
504 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
505 tny_folder_store_get_folders (TNY_FOLDER_STORE (storage_account),
507 g_object_unref (query);
509 ifolders = tny_list_create_iterator (folders);
511 /* Async refresh folders */
512 for (tny_iterator_first (ifolders);
513 !tny_iterator_is_done (ifolders);
514 tny_iterator_next (ifolders)) {
516 cur_folder = TNY_FOLDER (tny_iterator_get_current (ifolders));
517 tny_folder_refresh_async (cur_folder, folder_refresh_cb,
518 status_update_cb, mail_op);
521 g_object_unref (ifolders);
524 ModestMailOperationStatus
525 modest_mail_operation_get_status (ModestMailOperation *mail_op)
527 ModestMailOperationPrivate *priv;
529 /* g_return_val_if_fail (mail_op, MODEST_MAIL_OPERATION_STATUS_INVALID); */
530 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), */
531 /* MODEST_MAIL_OPERATION_STATUS_INVALID); */
533 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
538 modest_mail_operation_get_error (ModestMailOperation *mail_op)
540 ModestMailOperationPrivate *priv;
542 /* g_return_val_if_fail (mail_op, NULL); */
543 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), NULL); */
545 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
550 modest_mail_operation_cancel (ModestMailOperation *mail_op)
555 /* ******************************************************************* */
556 /* ************************** STORE ACTIONS ************************* */
557 /* ******************************************************************* */
561 modest_mail_operation_create_folder (ModestMailOperation *mail_op,
562 TnyFolderStore *parent,
565 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
566 g_return_val_if_fail (name, NULL);
568 TnyFolder *new_folder = NULL;
569 TnyStoreAccount *store_account;
571 /* Create the folder */
572 new_folder = tny_folder_store_create_folder (parent, name);
576 /* Subscribe to folder */
577 if (!tny_folder_is_subscribed (new_folder)) {
578 store_account = tny_folder_get_account (TNY_FOLDER (parent));
579 tny_store_account_subscribe (store_account, new_folder);
580 g_object_unref (G_OBJECT (store_account));
587 modest_mail_operation_remove_folder (ModestMailOperation *mail_op,
589 gboolean remove_to_trash)
591 TnyFolderStore *folder_store;
593 g_return_if_fail (TNY_IS_FOLDER (folder));
595 /* Get folder store */
596 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
598 /* Delete folder or move to trash */
599 if (remove_to_trash) {
600 TnyFolder *trash_folder;
602 trash_folder = modest_mail_operation_find_trash_folder (mail_op,
603 TNY_STORE_ACCOUNT (folder_store));
605 /* TODO: error_handling */
606 modest_mail_operation_move_folder (mail_op,
608 TNY_FOLDER_STORE (trash_folder));
610 tny_folder_store_remove_folder (folder_store, folder);
611 g_object_unref (G_OBJECT (folder));
615 g_object_unref (G_OBJECT (folder_store));
619 modest_mail_operation_rename_folder (ModestMailOperation *mail_op,
623 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
624 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
626 /* FIXME: better error handling */
627 if (strrchr (name, '/') != NULL)
630 /* Rename. Camel handles folder subscription/unsubscription */
631 tny_folder_set_name (folder, name);
635 modest_mail_operation_move_folder (ModestMailOperation *mail_op,
637 TnyFolderStore *parent)
639 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
640 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
641 g_return_if_fail (TNY_IS_FOLDER (folder));
643 modest_mail_operation_xfer_folder (mail_op, folder, parent, TRUE);
647 modest_mail_operation_copy_folder (ModestMailOperation *mail_op,
649 TnyFolderStore *parent)
651 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
652 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
653 g_return_if_fail (TNY_IS_FOLDER (folder));
655 modest_mail_operation_xfer_folder (mail_op, folder, parent, FALSE);
659 modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
661 TnyFolderStore *parent,
662 gboolean delete_original)
664 const gchar *folder_name;
665 TnyFolder *dest_folder, *child;
667 TnyList *folders, *headers;
669 g_return_if_fail (TNY_IS_FOLDER (folder));
670 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
672 /* Create the destination folder */
673 folder_name = tny_folder_get_name (folder);
674 dest_folder = modest_mail_operation_create_folder (mail_op,
675 parent, folder_name);
677 /* Transfer messages */
678 headers = TNY_LIST (tny_simple_list_new ());
679 tny_folder_get_headers (folder, headers, FALSE);
680 tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original);
682 /* Recurse children */
683 folders = TNY_LIST (tny_simple_list_new ());
684 tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL);
685 iter = tny_list_create_iterator (folders);
687 while (!tny_iterator_is_done (iter)) {
689 child = TNY_FOLDER (tny_iterator_get_current (iter));
690 modest_mail_operation_xfer_folder (mail_op, child,
691 TNY_FOLDER_STORE (dest_folder),
693 tny_iterator_next (iter);
696 /* Delete source folder (if needed) */
698 modest_mail_operation_remove_folder (mail_op, folder, FALSE);
701 g_object_unref (G_OBJECT (dest_folder));
702 g_object_unref (G_OBJECT (headers));
703 g_object_unref (G_OBJECT (folders));
704 g_object_unref (G_OBJECT (iter));
708 modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
709 TnyStoreAccount *store_account)
714 TnyFolderStoreQuery *query;
715 TnyFolder *trash_folder;
717 /* Look for Trash folder */
718 folders = TNY_LIST (tny_simple_list_new ());
719 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account), folders, NULL);
720 iter = tny_list_create_iterator (folders);
723 while (!tny_iterator_is_done (iter) && !found) {
725 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
726 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
729 tny_iterator_next (iter);
733 g_object_unref (G_OBJECT (folders));
734 g_object_unref (G_OBJECT (iter));
736 /* TODO: better error handling management */
743 /* ******************************************************************* */
744 /* ************************** MSG ACTIONS ************************* */
745 /* ******************************************************************* */
748 modest_mail_operation_copy_msg (ModestMailOperation *mail_op,
752 g_return_if_fail (TNY_IS_HEADER (header));
753 g_return_if_fail (TNY_IS_FOLDER (folder));
755 modest_mail_operation_xfer_msg (mail_op, header, folder, FALSE);
759 modest_mail_operation_move_msg (ModestMailOperation *mail_op,
763 g_return_if_fail (TNY_IS_HEADER (header));
764 g_return_if_fail (TNY_IS_FOLDER (folder));
766 modest_mail_operation_xfer_msg (mail_op, header, folder, TRUE);
770 modest_mail_operation_remove_msg (ModestMailOperation *mail_op,
772 gboolean remove_to_trash)
776 g_return_if_fail (TNY_IS_HEADER (header));
778 folder = tny_header_get_folder (header);
780 /* Delete or move to trash */
781 if (remove_to_trash) {
782 TnyFolder *trash_folder;
783 TnyStoreAccount *store_account;
785 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
786 trash_folder = modest_mail_operation_find_trash_folder (mail_op, store_account);
788 modest_mail_operation_move_msg (mail_op, header, trash_folder);
790 g_object_unref (G_OBJECT (store_account));
792 tny_folder_remove_msg (folder, header);
793 tny_folder_expunge (folder);
797 g_object_unref (folder);
801 modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
804 gboolean delete_original)
806 TnyFolder *src_folder;
809 src_folder = tny_header_get_folder (header);
810 headers = tny_simple_list_new ();
813 tny_list_prepend (headers, G_OBJECT (header));
814 tny_folder_transfer_msgs (src_folder, headers, folder, delete_original);
817 g_object_unref (headers);
818 g_object_unref (folder);
822 /* ******************************************************************* */
823 /* ************************* UTILIY FUNCTIONS ************************ */
824 /* ******************************************************************* */
826 is_ascii(const gchar *s)
829 if (s[0] & 128 || s[0] < 32)
837 get_content_type(const gchar *s)
841 type = g_string_new("text/plain");
843 if (g_utf8_validate(s, -1, NULL)) {
844 g_string_append(type, "; charset=\"utf-8\"");
846 /* it should be impossible to reach this, but better safe than sorry */
847 g_warning("invalid utf8 in message");
848 g_string_append(type, "; charset=\"latin1\"");
851 return g_string_free(type, FALSE);
855 modest_error_quark (void)
857 static GQuark err_q = 0;
860 err_q = g_quark_from_static_string ("modest-error-quark");
867 set_error (ModestMailOperation *mail_op,
868 ModestMailOperationErrorCode error_code,
869 const gchar *fmt, ...)
871 ModestMailOperationPrivate *priv;
876 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
878 va_start (args, fmt);
880 orig = g_strdup_vprintf(fmt, args);
881 error = g_error_new (MODEST_ERROR, error_code, orig);
886 g_object_unref (priv->error);
889 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
893 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gpointer user_data)
895 /* TODO: update main window progress bar */
899 folder_refresh_cb (TnyFolder *folder, gboolean cancelled, gpointer user_data)
902 ModestMailOperation *mail_op;
903 ModestMailOperationPrivate *priv;
905 mail_op = MODEST_MAIL_OPERATION (user_data);
906 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
908 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELLED;
913 add_attachments (TnyMsg *msg, const GList *attachments_list)
916 TnyMimePart *attachment_part, *old_attachment;
917 const gchar *attachment_content_type;
918 const gchar *attachment_filename;
919 TnyStream *attachment_stream;
921 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
923 old_attachment = pos->data;
924 attachment_filename = tny_mime_part_get_filename (old_attachment);
925 attachment_stream = tny_mime_part_get_stream (old_attachment);
926 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
928 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
930 tny_mime_part_construct_from_stream (attachment_part,
932 attachment_content_type);
933 tny_stream_reset (attachment_stream);
935 tny_mime_part_set_filename (attachment_part, attachment_filename);
937 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
938 /* g_object_unref (attachment_part); */
944 add_body_part (TnyMsg *msg,
946 const gchar *content_type,
947 gboolean has_attachments)
949 TnyMimePart *text_body_part = NULL;
950 TnyStream *text_body_stream;
952 /* Create the stream */
953 text_body_stream = TNY_STREAM (tny_camel_stream_new
954 (camel_stream_mem_new_with_buffer
955 (body, strlen(body))));
957 /* Create body part if needed */
960 TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
962 text_body_part = TNY_MIME_PART(msg);
964 /* Construct MIME part */
965 tny_stream_reset (text_body_stream);
966 tny_mime_part_construct_from_stream (text_body_part,
969 tny_stream_reset (text_body_stream);
971 /* Add part if needed */
972 if (has_attachments) {
973 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
974 g_object_unref (G_OBJECT(text_body_part));
978 g_object_unref (text_body_stream);
980 return text_body_part;