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 <glib/gi18n.h>
40 /* 'private'/'protected' functions */
41 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
42 static void modest_mail_operation_init (ModestMailOperation *obj);
43 static void modest_mail_operation_finalize (GObject *obj);
45 #define MODEST_ERROR modest_error_quark ()
47 typedef enum _ModestMailOperationErrorCode ModestMailOperationErrorCode;
48 enum _ModestMailOperationErrorCode {
49 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
50 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
52 MODEST_MAIL_OPERATION_NUM_ERROR_CODES
55 static void set_error (ModestMailOperation *mail_operation,
56 ModestMailOperationErrorCode error_code,
57 const gchar *fmt, ...);
58 static void status_update_cb (TnyFolder *folder, const gchar *what,
59 gint status, gpointer user_data);
60 static void folder_refresh_cb (TnyFolder *folder, gboolean cancelled,
62 static void add_attachments (TnyMsg *msg, const GList *attachments_list);
63 static TnyMimePart * add_body_part (TnyMsg *msg, const gchar *body,
64 const gchar *content_type, gboolean has_attachments);
73 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
74 struct _ModestMailOperationPrivate {
76 ModestMailOperationStatus status;
79 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
80 MODEST_TYPE_MAIL_OPERATION, \
81 ModestMailOperationPrivate))
83 /* some utility functions */
84 static char * get_content_type(const gchar *s);
85 static gboolean is_ascii(const gchar *s);
88 static GObjectClass *parent_class = NULL;
90 /* uncomment the following if you have defined any signals */
91 /* static guint signals[LAST_SIGNAL] = {0}; */
94 modest_mail_operation_get_type (void)
96 static GType my_type = 0;
98 static const GTypeInfo my_info = {
99 sizeof(ModestMailOperationClass),
100 NULL, /* base init */
101 NULL, /* base finalize */
102 (GClassInitFunc) modest_mail_operation_class_init,
103 NULL, /* class finalize */
104 NULL, /* class data */
105 sizeof(ModestMailOperation),
107 (GInstanceInitFunc) modest_mail_operation_init,
110 my_type = g_type_register_static (G_TYPE_OBJECT,
111 "ModestMailOperation",
118 modest_mail_operation_class_init (ModestMailOperationClass *klass)
120 GObjectClass *gobject_class;
121 gobject_class = (GObjectClass*) klass;
123 parent_class = g_type_class_peek_parent (klass);
124 gobject_class->finalize = modest_mail_operation_finalize;
126 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
128 /* signal definitions go here, e.g.: */
129 /* signals[MY_SIGNAL_1] = */
130 /* g_signal_new ("my_signal_1",....); */
131 /* signals[MY_SIGNAL_2] = */
132 /* g_signal_new ("my_signal_2",....); */
137 modest_mail_operation_init (ModestMailOperation *obj)
139 ModestMailOperationPrivate *priv;
141 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
143 priv->account = NULL;
144 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
149 modest_mail_operation_finalize (GObject *obj)
151 ModestMailOperationPrivate *priv;
153 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
156 g_object_unref (priv->account);
157 priv->account = NULL;
160 g_object_unref (priv->error);
164 G_OBJECT_CLASS(parent_class)->finalize (obj);
168 modest_mail_operation_new (TnyAccount *account)
170 ModestMailOperation *mail_operation;
171 ModestMailOperationPrivate *priv;
174 MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
175 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
177 priv->account = g_object_ref (account);
179 return mail_operation;
184 modest_mail_operation_send_mail (ModestMailOperation *mail_operation,
187 ModestMailOperationPrivate *priv;
188 TnyTransportAccount *transport_account;
190 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
192 if (!TNY_IS_TRANSPORT_ACCOUNT (priv->account)) {
193 set_error (mail_operation,
194 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
195 _("Error trying to send a mail. Use a transport account"));
198 transport_account = TNY_TRANSPORT_ACCOUNT (priv->account);
200 mail_operation = modest_mail_operation_new (NULL);
201 tny_transport_account_send (transport_account, msg);
205 modest_mail_operation_send_new_mail (ModestMailOperation *mail_operation,
210 const gchar *subject,
212 const GList *attachments_list)
216 TnyTransportAccount *transport_account;
217 ModestMailOperationPrivate *priv;
220 g_return_if_fail (mail_operation);
222 /* Check parametters */
224 set_error (mail_operation,
225 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
226 _("Error trying to send a mail. You need to set almost one a recipient"));
230 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
232 if (!TNY_IS_TRANSPORT_ACCOUNT (priv->account)) {
233 set_error (mail_operation,
234 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
235 _("Error trying to send a mail. Use a transport account"));
240 transport_account = TNY_TRANSPORT_ACCOUNT (priv->account);
242 new_msg = TNY_MSG (tny_camel_msg_new ());
243 header = TNY_HEADER (tny_camel_header_new ());
245 /* IMPORTANT: set the header before assign values to it */
246 tny_msg_set_header (new_msg, header);
247 tny_header_set_from (TNY_HEADER (header), from);
248 tny_header_set_to (TNY_HEADER (header), to);
249 tny_header_set_cc (TNY_HEADER (header), cc);
250 tny_header_set_bcc (TNY_HEADER (header), bcc);
251 tny_header_set_subject (TNY_HEADER (header), subject);
253 content_type = get_content_type(body);
255 /* Add the body of the new mail */
256 add_body_part (new_msg, body, (const gchar *) content_type,
257 (attachments_list == NULL) ? FALSE : TRUE);
259 /* Add attachments */
260 add_attachments (new_msg, attachments_list);
263 tny_transport_account_send (transport_account, new_msg);
266 g_object_unref (header);
267 g_object_unref (new_msg);
268 g_free(content_type);
272 * modest_mail_operation_create_forward_mail:
273 * @msg: a valid #TnyMsg instance
274 * @forward_type: the type of forwarded message
276 * creates a forwarded message from an existing one
278 * Returns: a new #TnyMsg, or NULL in case of error
281 modest_mail_operation_create_forward_mail (TnyMsg *msg,
282 ModestMailOperationForwardType forward_type)
285 TnyHeader *new_header, *header;
286 TnyStream *attachment_stream;
287 gchar *new_subject, *new_body, *content_type, *quoted;
288 TnyMimePart *text_body_part = NULL;
289 GList *attachments_list;
294 /* Create new objects */
295 new_msg = TNY_MSG (tny_camel_msg_new ());
296 new_header = TNY_HEADER (tny_camel_header_new ());
298 header = tny_msg_get_header (msg);
300 /* Fill the header */
301 tny_msg_set_header (new_msg, new_header);
302 /* FIXME: set it from default account, current account ... */
303 tny_header_set_from (new_header, "<svsdrozo@terra.es>");
305 /* Change the subject */
306 new_subject = (gchar *) modest_text_utils_create_forward_subject (tny_header_get_subject(header));
307 tny_header_set_subject (new_header, (const gchar *) new_subject);
308 g_free (new_subject);
310 /* Get body from original msg */
311 new_body = (gchar *) modest_tny_msg_actions_find_body (msg, FALSE);
313 g_object_unref (new_msg);
316 content_type = get_content_type(new_body);
318 /* Create the list of attachments */
319 parts = TNY_LIST (tny_simple_list_new());
320 tny_msg_get_parts (msg, parts);
321 iter = tny_list_create_iterator (parts);
322 attachments_list = NULL;
324 while (!tny_iterator_is_done(iter)) {
327 part = TNY_MIME_PART (tny_iterator_get_current (iter));
328 if (tny_mime_part_is_attachment (part))
329 attachments_list = g_list_prepend (attachments_list, part);
331 tny_iterator_next (iter);
334 /* Add attachments */
335 add_attachments (new_msg, attachments_list);
337 switch (forward_type) {
338 TnyMimePart *attachment_part;
341 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
342 /* Prepend "Original message" text */
343 inlined_text = (gchar *)
344 modest_text_utils_create_inlined_text (tny_header_get_from (header),
345 tny_header_get_date_sent (header),
346 tny_header_get_to (header),
347 tny_header_get_subject (header),
348 (const gchar*) new_body);
350 new_body = inlined_text;
353 add_body_part (new_msg, new_body,
354 (const gchar *) content_type,
355 (tny_list_get_length (parts) > 0) ? TRUE : FALSE);
358 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
359 attachment_part = add_body_part (new_msg, new_body,
360 (const gchar *) content_type, TRUE);
362 /* Set the subject as the name of the attachment */
363 tny_mime_part_set_filename (attachment_part, tny_header_get_subject (header));
369 g_object_unref (parts);
370 if (attachments_list) g_list_free (attachments_list);
371 if (text_body_part) g_object_unref (text_body_part);
372 g_free (content_type);
379 * modest_mail_operation_create_reply_mail:
380 * @msg: a valid #TnyMsg instance
381 * @reply_type: the format of the new message
382 * @reply_mode: the mode of reply, to the sender only, to a mail list or to all
384 * creates a new message to reply to an existing one
386 * Returns: Returns: a new #TnyMsg, or NULL in case of error
389 modest_mail_operation_create_reply_mail (TnyMsg *msg,
390 ModestMailOperationReplyType reply_type,
391 ModestMailOperationReplyMode reply_mode)
394 TnyHeader *new_header, *header;
395 TnyStream *attachment_stream;
396 gchar *new_subject, *new_body, *content_type, *quoted;
398 TnyMimePart *text_body_part;
399 gchar *my_email = NULL;
401 /* Create new objects */
402 new_msg = TNY_MSG (tny_camel_msg_new ());
403 new_header = TNY_HEADER (tny_camel_header_new ());
405 header = tny_msg_get_header (msg);
407 /* Fill the header */
408 tny_msg_set_header (new_msg, new_header);
409 tny_header_set_to (new_header, tny_header_get_from (header));
410 /* TODO: set "From:" from my current account */
411 /* current_account = modest_account_mgr_get_current_account (account_mgr); */
412 /* my_email = modest_account_mgr_get_string (account_mgr, */
413 /* current_account, */
414 /* MODEST_ACCOUNT_EMAIL, */
417 /* tny_header_set_from (new_header, email); */
418 my_email = g_strdup ("svsdrozo@terra.es");
420 switch (reply_mode) {
421 gchar *new_cc = NULL;
422 const gchar *cc = NULL, *bcc = NULL;
425 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
426 /* Do not fill neither cc nor bcc */
428 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
431 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
432 /* Concatenate to, cc and bcc */
433 cc = tny_header_get_cc (header);
434 bcc = tny_header_get_bcc (header);
436 tmp = g_string_new (tny_header_get_to (header));
437 if (cc) g_string_append_printf (tmp, ",%s",cc);
438 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
440 /* Remove my own address from the cc list */
442 modest_text_utils_remove_mail_from_mail_list ((const gchar *) tmp->str,
443 (const gchar *) my_email);
444 /* FIXME: remove also the mails from the new To: */
445 tny_header_set_cc (new_header, new_cc);
448 g_string_free (tmp, TRUE);
454 /* Change the subject */
455 new_subject = (gchar*) modest_text_utils_create_reply_subject (tny_header_get_subject(header));
456 tny_header_set_subject (new_header, (const gchar *) new_subject);
457 g_free (new_subject);
459 /* Get body from original msg */
460 new_body = (gchar*) modest_tny_msg_actions_find_body (msg, FALSE);
462 g_object_unref (new_msg);
465 content_type = get_content_type(new_body);
467 switch (reply_type) {
470 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
471 /* Prepend "Original message" text */
472 cited_text = (gchar *) modest_text_utils_create_cited_text (tny_header_get_from (header),
473 tny_header_get_date_sent (header),
474 (const gchar*) new_body);
476 new_body = cited_text;
478 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
479 /* FIXME: replace 80 with a value from ModestConf */
480 quoted = (gchar*) modest_text_utils_quote (new_body,
481 tny_header_get_from (header),
482 tny_header_get_date_sent (header),
489 text_body_part = add_body_part (new_msg, new_body,
490 (const gchar *) content_type, TRUE);
493 g_object_unref (G_OBJECT(text_body_part));
494 g_free (content_type);
501 modest_mail_operation_update_account (ModestMailOperation *mail_operation)
503 TnyStoreAccount *storage_account;
504 ModestMailOperationPrivate *priv;
506 TnyIterator *ifolders;
507 TnyFolder *cur_folder;
508 TnyFolderStoreQuery *query;
510 g_return_if_fail (mail_operation);
511 g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_operation));
513 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
515 /* Check that it is a store account */
516 if (!TNY_IS_STORE_ACCOUNT (priv->account)) {
517 set_error (mail_operation,
518 MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
519 _("Error trying to update an account. Use a store account"));
522 storage_account = TNY_STORE_ACCOUNT (priv->account);
524 /* Get subscribed folders */
525 folders = TNY_LIST (tny_simple_list_new ());
526 query = tny_folder_store_query_new ();
527 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
528 tny_folder_store_get_folders (TNY_FOLDER_STORE (storage_account),
530 g_object_unref (query);
532 ifolders = tny_list_create_iterator (folders);
534 /* Async refresh folders */
535 for (tny_iterator_first (ifolders);
536 !tny_iterator_is_done (ifolders);
537 tny_iterator_next (ifolders)) {
539 cur_folder = TNY_FOLDER (tny_iterator_get_current (ifolders));
540 tny_folder_refresh_async (cur_folder, folder_refresh_cb,
541 status_update_cb, mail_operation);
544 g_object_unref (ifolders);
547 ModestMailOperationStatus
548 modest_mail_operation_get_status (ModestMailOperation *mail_operation)
550 ModestMailOperationPrivate *priv;
552 /* g_return_val_if_fail (mail_operation, MODEST_MAIL_OPERATION_STATUS_INVALID); */
553 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_operation), */
554 /* MODEST_MAIL_OPERATION_STATUS_INVALID); */
556 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_operation);
561 modest_mail_operation_get_error (ModestMailOperation *mail_operation)
563 ModestMailOperationPrivate *priv;
565 /* g_return_val_if_fail (mail_operation, NULL); */
566 /* g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_operation), NULL); */
568 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_operation);
573 modest_mail_operation_cancel (ModestMailOperation *mail_operation)
579 is_ascii(const gchar *s)
582 if (s[0] & 128 || s[0] < 32)
590 get_content_type(const gchar *s)
594 type = g_string_new("text/plain");
596 if (g_utf8_validate(s, -1, NULL)) {
597 g_string_append(type, "; charset=\"utf-8\"");
599 /* it should be impossible to reach this, but better safe than sorry */
600 g_warning("invalid utf8 in message");
601 g_string_append(type, "; charset=\"latin1\"");
604 return g_string_free(type, FALSE);
608 modest_error_quark (void)
610 static GQuark err_q = 0;
613 err_q = g_quark_from_static_string ("modest-error-quark");
620 set_error (ModestMailOperation *mail_operation,
621 ModestMailOperationErrorCode error_code,
622 const gchar *fmt, ...)
624 ModestMailOperationPrivate *priv;
629 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
631 va_start (args, fmt);
633 orig = g_strdup_vprintf(fmt, args);
634 error = g_error_new (MODEST_ERROR, error_code, orig);
639 g_object_unref (priv->error);
642 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
646 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gpointer user_data)
648 /* TODO: update main window progress bar */
652 folder_refresh_cb (TnyFolder *folder, gboolean cancelled, gpointer user_data)
655 ModestMailOperation *mail_operation;
656 ModestMailOperationPrivate *priv;
658 mail_operation = MODEST_MAIL_OPERATION (user_data);
659 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
661 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELLED;
666 add_attachments (TnyMsg *msg, const GList *attachments_list)
669 TnyMimePart *attachment_part, *old_attachment;
670 const gchar *attachment_content_type;
671 const gchar *attachment_filename;
672 TnyStream *attachment_stream;
674 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
676 old_attachment = pos->data;
677 attachment_filename = tny_mime_part_get_filename (old_attachment);
678 attachment_stream = tny_mime_part_get_stream (old_attachment);
679 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
681 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
683 tny_mime_part_construct_from_stream (attachment_part,
685 attachment_content_type);
686 tny_stream_reset (attachment_stream);
688 tny_mime_part_set_filename (attachment_part, attachment_filename);
690 tny_msg_add_part (msg, attachment_part);
691 g_object_unref(G_OBJECT(attachment_part));
696 add_body_part (TnyMsg *msg,
698 const gchar *content_type,
699 gboolean has_attachments)
701 TnyMimePart *text_body_part = NULL;
702 TnyStream *text_body_stream;
704 /* Create the stream */
705 text_body_stream = TNY_STREAM (tny_camel_stream_new
706 (camel_stream_mem_new_with_buffer
707 (body, strlen(body))));
709 /* Create body part if needed */
712 TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
714 text_body_part = TNY_MIME_PART(msg);
716 /* Construct MIME part */
717 tny_stream_reset (text_body_stream);
718 tny_mime_part_construct_from_stream (text_body_part,
721 tny_stream_reset (text_body_stream);
723 /* Add part if needed */
724 if (has_attachments) {
725 tny_msg_add_part (msg, text_body_part);
726 g_object_unref (G_OBJECT(text_body_part));
730 g_object_unref (text_body_stream);
732 return text_body_part;