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);
92 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
93 struct _ModestMailOperationPrivate {
95 ModestMailOperationStatus status;
98 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
99 MODEST_TYPE_MAIL_OPERATION, \
100 ModestMailOperationPrivate))
102 /* some utility functions */
103 static char * get_content_type(const gchar *s);
104 static gboolean is_ascii(const gchar *s);
107 static GObjectClass *parent_class = NULL;
109 /* uncomment the following if you have defined any signals */
110 /* static guint signals[LAST_SIGNAL] = {0}; */
113 modest_mail_operation_get_type (void)
115 static GType my_type = 0;
117 static const GTypeInfo my_info = {
118 sizeof(ModestMailOperationClass),
119 NULL, /* base init */
120 NULL, /* base finalize */
121 (GClassInitFunc) modest_mail_operation_class_init,
122 NULL, /* class finalize */
123 NULL, /* class data */
124 sizeof(ModestMailOperation),
126 (GInstanceInitFunc) modest_mail_operation_init,
129 my_type = g_type_register_static (G_TYPE_OBJECT,
130 "ModestMailOperation",
137 modest_mail_operation_class_init (ModestMailOperationClass *klass)
139 GObjectClass *gobject_class;
140 gobject_class = (GObjectClass*) klass;
142 parent_class = g_type_class_peek_parent (klass);
143 gobject_class->finalize = modest_mail_operation_finalize;
145 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
147 /* signal definitions go here, e.g.: */
148 /* signals[MY_SIGNAL_1] = */
149 /* g_signal_new ("my_signal_1",....); */
150 /* signals[MY_SIGNAL_2] = */
151 /* g_signal_new ("my_signal_2",....); */
156 modest_mail_operation_init (ModestMailOperation *obj)
158 ModestMailOperationPrivate *priv;
160 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
162 priv->account = NULL;
163 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
168 modest_mail_operation_finalize (GObject *obj)
170 ModestMailOperationPrivate *priv;
172 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
175 g_object_unref (priv->account);
176 priv->account = NULL;
179 g_error_free (priv->error);
183 G_OBJECT_CLASS(parent_class)->finalize (obj);
187 modest_mail_operation_new (TnyAccount *account)
189 ModestMailOperation *mail_operation;
190 ModestMailOperationPrivate *priv;
193 MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
194 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
196 priv->account = g_object_ref (account);
198 return mail_operation;
203 modest_mail_operation_send_mail (ModestMailOperation *mail_operation,
206 ModestMailOperationPrivate *priv;
207 TnyTransportAccount *transport_account;
209 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
211 if (!TNY_IS_TRANSPORT_ACCOUNT (priv->account)) {
212 set_error (mail_operation,
213 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
214 _("Error trying to send a mail. Use a transport account"));
217 transport_account = TNY_TRANSPORT_ACCOUNT (priv->account);
219 mail_operation = modest_mail_operation_new (NULL);
220 tny_transport_account_send (transport_account, msg);
224 modest_mail_operation_send_new_mail (ModestMailOperation *mail_operation,
229 const gchar *subject,
231 const GList *attachments_list)
235 TnyTransportAccount *transport_account;
236 ModestMailOperationPrivate *priv;
239 g_return_if_fail (mail_operation);
241 /* Check parametters */
243 set_error (mail_operation,
244 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
245 _("Error trying to send a mail. You need to set almost one a recipient"));
249 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
251 if (!TNY_IS_TRANSPORT_ACCOUNT (priv->account)) {
252 set_error (mail_operation,
253 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
254 _("Error trying to send a mail. Use a transport account"));
259 transport_account = TNY_TRANSPORT_ACCOUNT (priv->account);
261 new_msg = TNY_MSG (tny_camel_msg_new ());
262 header = TNY_HEADER (tny_camel_header_new ());
264 /* WARNING: set the header before assign values to it */
265 tny_msg_set_header (new_msg, header);
266 tny_header_set_from (TNY_HEADER (header), from);
267 tny_header_set_to (TNY_HEADER (header), to);
268 tny_header_set_cc (TNY_HEADER (header), cc);
269 tny_header_set_bcc (TNY_HEADER (header), bcc);
270 tny_header_set_subject (TNY_HEADER (header), subject);
272 content_type = get_content_type(body);
274 /* Add the body of the new mail */
275 add_body_part (new_msg, body, (const gchar *) content_type,
276 (attachments_list == NULL) ? FALSE : TRUE);
278 /* Add attachments */
279 add_attachments (new_msg, attachments_list);
282 tny_transport_account_send (transport_account, new_msg);
285 g_object_unref (header);
286 g_object_unref (new_msg);
287 g_free(content_type);
291 * modest_mail_operation_create_forward_mail:
292 * @msg: a valid #TnyMsg instance
293 * @forward_type: the type of forwarded message
295 * creates a forwarded message from an existing one
297 * Returns: a new #TnyMsg, or NULL in case of error
300 modest_mail_operation_create_forward_mail (TnyMsg *msg,
302 ModestMailOperationForwardType forward_type)
305 TnyHeader *new_header, *header;
306 gchar *new_subject, *new_body, *content_type;
307 TnyMimePart *text_body_part = NULL;
308 GList *attachments_list;
312 /* Create new objects */
313 new_msg = TNY_MSG (tny_camel_msg_new ());
314 new_header = TNY_HEADER (tny_camel_header_new ());
316 header = tny_msg_get_header (msg);
318 /* Fill the header */
319 tny_msg_set_header (new_msg, new_header);
320 tny_header_set_from (new_header, from);
322 /* Change the subject */
323 new_subject = (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
325 tny_header_set_subject (new_header, (const gchar *) new_subject);
326 g_free (new_subject);
328 /* Get body from original msg */
329 new_body = (gchar *) modest_tny_msg_actions_find_body (msg, FALSE);
331 g_object_unref (new_msg);
334 content_type = get_content_type(new_body);
336 /* Create the list of attachments */
337 parts = TNY_LIST (tny_simple_list_new());
338 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
339 iter = tny_list_create_iterator (parts);
340 attachments_list = NULL;
342 while (!tny_iterator_is_done(iter)) {
345 part = TNY_MIME_PART (tny_iterator_get_current (iter));
346 if (tny_mime_part_is_attachment (part))
347 attachments_list = g_list_prepend (attachments_list, part);
349 tny_iterator_next (iter);
352 /* Add attachments */
353 add_attachments (new_msg, attachments_list);
355 switch (forward_type) {
356 TnyMimePart *attachment_part;
359 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
360 /* Prepend "Original message" text */
361 inlined_text = (gchar *)
362 modest_text_utils_inlined_text (tny_header_get_from (header),
363 tny_header_get_date_sent (header),
364 tny_header_get_to (header),
365 tny_header_get_subject (header),
366 (const gchar*) new_body);
368 new_body = inlined_text;
371 add_body_part (new_msg, new_body,
372 (const gchar *) content_type,
373 (tny_list_get_length (parts) > 0) ? TRUE : FALSE);
376 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
377 attachment_part = add_body_part (new_msg, new_body,
378 (const gchar *) content_type, TRUE);
380 /* Set the subject as the name of the attachment */
381 tny_mime_part_set_filename (attachment_part, tny_header_get_subject (header));
387 if (attachments_list) g_list_free (attachments_list);
388 g_object_unref (parts);
389 if (text_body_part) g_free (text_body_part);
390 g_free (content_type);
397 * modest_mail_operation_create_reply_mail:
398 * @msg: a valid #TnyMsg instance
399 * @reply_type: the format of the new message
400 * @reply_mode: the mode of reply, to the sender only, to a mail list or to all
402 * creates a new message to reply to an existing one
404 * Returns: Returns: a new #TnyMsg, or NULL in case of error
407 modest_mail_operation_create_reply_mail (TnyMsg *msg,
409 ModestMailOperationReplyType reply_type,
410 ModestMailOperationReplyMode reply_mode)
413 TnyHeader *new_header, *header;
414 gchar *new_subject, *new_body, *content_type, *quoted;
415 TnyMimePart *text_body_part;
417 /* Create new objects */
418 new_msg = TNY_MSG (tny_camel_msg_new ());
419 new_header = TNY_HEADER (tny_camel_header_new ());
420 header = tny_msg_get_header (msg);
422 /* Fill the header */
423 tny_msg_set_header (new_msg, new_header);
424 tny_header_set_to (new_header, tny_header_get_from (header));
425 tny_header_set_from (new_header, from);
427 switch (reply_mode) {
428 gchar *new_cc = NULL;
429 const gchar *cc = NULL, *bcc = NULL;
432 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
433 /* Do not fill neither cc nor bcc */
435 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
438 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
439 /* Concatenate to, cc and bcc */
440 cc = tny_header_get_cc (header);
441 bcc = tny_header_get_bcc (header);
443 tmp = g_string_new (tny_header_get_to (header));
444 if (cc) g_string_append_printf (tmp, ",%s",cc);
445 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
447 /* Remove my own address from the cc list */
449 modest_text_utils_remove_address ((const gchar *) tmp->str,
450 (const gchar *) from);
451 /* FIXME: remove also the mails from the new To: */
452 tny_header_set_cc (new_header, new_cc);
455 g_string_free (tmp, TRUE);
460 /* Change the subject */
461 new_subject = (gchar*) modest_text_utils_derived_subject (tny_header_get_subject(header),
463 tny_header_set_subject (new_header, (const gchar *) new_subject);
464 g_free (new_subject);
466 /* Get body from original msg */
467 new_body = (gchar*) modest_tny_msg_actions_find_body (msg, FALSE);
469 g_object_unref (new_msg);
472 content_type = get_content_type(new_body);
474 switch (reply_type) {
477 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
478 /* Prepend "Original message" text */
479 cited_text = (gchar *) modest_text_utils_cited_text (tny_header_get_from (header),
480 tny_header_get_date_sent (header),
481 (const gchar*) new_body);
483 new_body = cited_text;
485 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
486 /* FIXME: replace 80 with a value from ModestConf */
487 quoted = (gchar*) modest_text_utils_quote (new_body,
488 tny_header_get_from (header),
489 tny_header_get_date_sent (header),
496 text_body_part = add_body_part (new_msg, new_body,
497 (const gchar *) content_type, TRUE);
500 /* g_free (text_body_part); */
501 g_free (content_type);
508 modest_mail_operation_update_account (ModestMailOperation *mail_operation)
510 TnyStoreAccount *storage_account;
511 ModestMailOperationPrivate *priv;
513 TnyIterator *ifolders;
514 TnyFolder *cur_folder;
515 TnyFolderStoreQuery *query;
517 g_return_if_fail (mail_operation);
518 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_operation));
520 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
522 /* Check that it is a store account */
523 if (!TNY_IS_STORE_ACCOUNT (priv->account)) {
524 set_error (mail_operation,
525 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
526 _("Error trying to update an account. Use a store account"));
529 storage_account = TNY_STORE_ACCOUNT (priv->account);
531 /* Get subscribed folders */
532 folders = TNY_LIST (tny_simple_list_new ());
533 query = tny_folder_store_query_new ();
534 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
535 tny_folder_store_get_folders (TNY_FOLDER_STORE (storage_account),
537 g_object_unref (query);
539 ifolders = tny_list_create_iterator (folders);
541 /* Async refresh folders */
542 for (tny_iterator_first (ifolders);
543 !tny_iterator_is_done (ifolders);
544 tny_iterator_next (ifolders)) {
546 cur_folder = TNY_FOLDER (tny_iterator_get_current (ifolders));
547 tny_folder_refresh_async (cur_folder, folder_refresh_cb,
548 status_update_cb, mail_operation);
551 g_object_unref (ifolders);
554 ModestMailOperationStatus
555 modest_mail_operation_get_status (ModestMailOperation *mail_operation)
557 ModestMailOperationPrivate *priv;
559 /* g_return_val_if_fail (mail_operation, MODEST_MAIL_OPERATION_STATUS_INVALID); */
560 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_operation), */
561 /* MODEST_MAIL_OPERATION_STATUS_INVALID); */
563 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_operation);
568 modest_mail_operation_get_error (ModestMailOperation *mail_operation)
570 ModestMailOperationPrivate *priv;
572 /* g_return_val_if_fail (mail_operation, NULL); */
573 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_operation), NULL); */
575 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_operation);
580 modest_mail_operation_cancel (ModestMailOperation *mail_operation)
586 is_ascii(const gchar *s)
589 if (s[0] & 128 || s[0] < 32)
597 get_content_type(const gchar *s)
601 type = g_string_new("text/plain");
603 if (g_utf8_validate(s, -1, NULL)) {
604 g_string_append(type, "; charset=\"utf-8\"");
606 /* it should be impossible to reach this, but better safe than sorry */
607 g_warning("invalid utf8 in message");
608 g_string_append(type, "; charset=\"latin1\"");
611 return g_string_free(type, FALSE);
615 modest_error_quark (void)
617 static GQuark err_q = 0;
620 err_q = g_quark_from_static_string ("modest-error-quark");
627 set_error (ModestMailOperation *mail_operation,
628 ModestMailOperationErrorCode error_code,
629 const gchar *fmt, ...)
631 ModestMailOperationPrivate *priv;
636 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
638 va_start (args, fmt);
640 orig = g_strdup_vprintf(fmt, args);
641 error = g_error_new (MODEST_ERROR, error_code, orig);
646 g_object_unref (priv->error);
649 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
653 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gpointer user_data)
655 /* TODO: update main window progress bar */
659 folder_refresh_cb (TnyFolder *folder, gboolean cancelled, gpointer user_data)
662 ModestMailOperation *mail_operation;
663 ModestMailOperationPrivate *priv;
665 mail_operation = MODEST_MAIL_OPERATION (user_data);
666 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
668 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELLED;
673 add_attachments (TnyMsg *msg, const GList *attachments_list)
676 TnyMimePart *attachment_part, *old_attachment;
677 const gchar *attachment_content_type;
678 const gchar *attachment_filename;
679 TnyStream *attachment_stream;
681 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
683 old_attachment = pos->data;
684 attachment_filename = tny_mime_part_get_filename (old_attachment);
685 attachment_stream = tny_mime_part_get_stream (old_attachment);
686 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
688 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
690 tny_mime_part_construct_from_stream (attachment_part,
692 attachment_content_type);
693 tny_stream_reset (attachment_stream);
695 tny_mime_part_set_filename (attachment_part, attachment_filename);
697 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
698 /* g_object_unref (attachment_part); */
704 add_body_part (TnyMsg *msg,
706 const gchar *content_type,
707 gboolean has_attachments)
709 TnyMimePart *text_body_part = NULL;
710 TnyStream *text_body_stream;
712 /* Create the stream */
713 text_body_stream = TNY_STREAM (tny_camel_stream_new
714 (camel_stream_mem_new_with_buffer
715 (body, strlen(body))));
717 /* Create body part if needed */
720 TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
722 text_body_part = TNY_MIME_PART(msg);
724 /* Construct MIME part */
725 tny_stream_reset (text_body_stream);
726 tny_mime_part_construct_from_stream (text_body_part,
729 tny_stream_reset (text_body_stream);
731 /* Add part if needed */
732 if (has_attachments) {
733 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
734 g_object_unref (G_OBJECT(text_body_part));
738 g_object_unref (text_body_stream);
740 return text_body_part;