* all:
[modest] / src / modest-mail-operation.c
index 8c1aa43..db8b916 100644 (file)
 #include <tny-simple-list.h>
 #include <camel/camel-stream-mem.h>
 #include <glib/gi18n.h>
-
+#include <modest-tny-account.h>
+#include <modest-tny-send-queue.h>
+#include <modest-runtime.h>
 #include "modest-text-utils.h"
-#include "modest-tny-msg-actions.h"
+#include "modest-tny-msg.h"
 #include "modest-tny-platform-factory.h"
 #include "modest-marshal.h"
 #include "modest-formatter.h"
@@ -68,13 +70,6 @@ static void     update_folders_cb    (TnyFolderStore *self,
 static void     add_attachments      (TnyMsg *msg, 
                                      GList *attachments_list);
 
-
-static TnyMimePart *         add_body_part    (TnyMsg *msg, 
-                                              const gchar *body,
-                                              const gchar *content_type, 
-                                              gboolean has_attachments);
-
-
 static void        modest_mail_operation_xfer_folder       (ModestMailOperation *self,
                                                            TnyFolder *folder,
                                                            TnyFolderStore *parent,
@@ -85,10 +80,6 @@ static gboolean    modest_mail_operation_xfer_msg          (ModestMailOperation
                                                            TnyFolder *folder, 
                                                            gboolean delete_original);
 
-static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *self,
-                                                           TnyStoreAccount *store_account);
-
-
 enum _ModestMailOperationSignals 
 {
        PROGRESS_CHANGED_SIGNAL,
@@ -103,10 +94,16 @@ struct _ModestMailOperationPrivate {
        ModestMailOperationStatus  status;
        GError                    *error;
 };
+
 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                    MODEST_TYPE_MAIL_OPERATION, \
                                                    ModestMailOperationPrivate))
 
+#define CHECK_EXCEPTION(priv, new_status, op)  if (priv->error) {\
+                                                   priv->status = new_status;\
+                                                   op;\
+                                               }
+
 typedef struct _RefreshFolderAsyncHelper
 {
        ModestMailOperation *mail_op;
@@ -116,10 +113,6 @@ typedef struct _RefreshFolderAsyncHelper
 
 } RefreshFolderAsyncHelper;
 
-/* some utility functions */
-static char * get_content_type(const gchar *s);
-static gboolean is_ascii(const gchar *s);
-
 /* globals */
 static GObjectClass *parent_class = NULL;
 
@@ -217,27 +210,27 @@ modest_mail_operation_send_mail (ModestMailOperation *self,
                                 TnyTransportAccount *transport_account,
                                 TnyMsg* msg)
 {
+       TnySendQueue *send_queue;
+       
        g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
        g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
 
-       tny_transport_account_send (transport_account, msg, NULL); /* FIXME */
+       send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
+       if (!TNY_IS_SEND_QUEUE(send_queue))
+               g_printerr ("modest: could not find send queue for account\n");
+       else
+               tny_send_queue_add (send_queue, msg);
 }
 
 void
 modest_mail_operation_send_new_mail (ModestMailOperation *self,
                                     TnyTransportAccount *transport_account,
-                                    const gchar *from,
-                                    const gchar *to,
-                                    const gchar *cc,
-                                    const gchar *bcc,
-                                    const gchar *subject,
-                                    const gchar *body,
+                                    const gchar *from,  const gchar *to,
+                                    const gchar *cc,  const gchar *bcc,
+                                    const gchar *subject, const gchar *body,
                                     const GList *attachments_list)
 {
-       TnyPlatformFactory *fact;
        TnyMsg *new_msg;
-       TnyHeader *header;
-       gchar *content_type;
        ModestMailOperationPrivate *priv = NULL;
 
        g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
@@ -249,40 +242,19 @@ modest_mail_operation_send_new_mail (ModestMailOperation *self,
        if (to == NULL) {
                g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
                             MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
-                            _("Error trying to send a mail. You need to set almost one a recipient"));
+                            _("Error trying to send a mail. You need to set at least one recipient"));
                return;
        }
 
-       /* Create new msg */
-       fact    = modest_tny_platform_factory_get_instance ();
-       new_msg = tny_platform_factory_new_msg (fact);
-       header  = tny_platform_factory_new_header (fact);
-
-       /* WARNING: set the header before assign values to it */
-       tny_msg_set_header (new_msg, header);
-       tny_header_set_from (TNY_HEADER (header), from);
-       tny_header_set_replyto (TNY_HEADER (header), from);
-       tny_header_set_to (TNY_HEADER (header), to);
-       tny_header_set_cc (TNY_HEADER (header), cc);
-       tny_header_set_bcc (TNY_HEADER (header), bcc);
-       tny_header_set_subject (TNY_HEADER (header), subject);
-
-       content_type = get_content_type(body);
-
-       /* Add the body of the new mail */      
-       add_body_part (new_msg, body, (const gchar *) content_type,
-                      (attachments_list == NULL) ? FALSE : TRUE);
-
-       /* Add attachments */
-       add_attachments (new_msg, (GList*) attachments_list);
-
-       /* Send mail */
-       tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */
+       new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, body, NULL); /* FIXME: attachments */
+       if (!new_msg) {
+               g_printerr ("modest: failed to create a new msg\n");
+               return;
+       }
+       
+       modest_mail_operation_send_mail (self, transport_account, new_msg);
 
-       /* Clean */
-       g_object_unref (header);
-       g_object_unref (new_msg);
-       g_free(content_type);
+       g_object_unref (G_OBJECT(new_msg));
 }
 
 static void
@@ -312,7 +284,7 @@ create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, gu
        /* Get body from original msg. Always look for the text/plain
           part of the message to create the reply/forwarded mail */
        header = tny_msg_get_header (msg);
-       body   = modest_tny_msg_actions_find_body_part (msg, FALSE);
+       body   = modest_tny_msg_find_body_part (msg, FALSE);
 
        /* TODO: select the formatter from account prefs */
        formatter = modest_formatter_new ("text/plain");
@@ -339,8 +311,9 @@ create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, gu
                        break;
                }
        }
-       g_object_unref (G_OBJECT (formatter));
-
+       g_object_unref (G_OBJECT(formatter));
+       g_object_unref (G_OBJECT(body));
+       
        /* Fill the header */
        fact = modest_tny_platform_factory_get_instance ();
        new_header = TNY_HEADER (tny_platform_factory_new_header (fact));
@@ -380,7 +353,8 @@ modest_mail_operation_create_forward_mail (TnyMsg *msg,
        add_attachments (new_msg, attachments_list);
 
        /* Clean */
-       if (attachments_list) g_list_free (attachments_list);
+       if (attachments_list)
+               g_list_free (attachments_list);
        g_object_unref (G_OBJECT (parts));
 
        return new_msg;
@@ -405,6 +379,7 @@ modest_mail_operation_create_reply_mail (TnyMsg *msg,
        header = tny_msg_get_header (msg);
        new_header = tny_msg_get_header (new_msg);
        reply_to = tny_header_get_replyto (header);
+
        if (reply_to)
                tny_header_set_to (new_header, reply_to);
        else
@@ -496,10 +471,11 @@ folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer
                g_object_unref (G_OBJECT (list));
                g_slice_free (RefreshFolderAsyncHelper, helper);
        } else {
-               tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
-                                         folder_refresh_cb,
+               TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (helper->iter));
+               tny_folder_refresh_async (folder, folder_refresh_cb,
                                          status_update_cb, 
                                          helper);
+               g_object_unref (G_OBJECT(folder));
        }
        g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
 }
@@ -511,7 +487,8 @@ update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gp
        ModestMailOperation *self;
        ModestMailOperationPrivate *priv;
        RefreshFolderAsyncHelper *helper;
-
+       TnyFolder *folder;
+       
        self  = MODEST_MAIL_OPERATION (user_data);
        priv  = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
 
@@ -532,10 +509,10 @@ update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gp
        helper->canceled = 0;
 
        /* Async refresh folders */
-       tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
-                                 folder_refresh_cb,
-                                 status_update_cb, 
-                                 helper);
+       folder = TNY_FOLDER (tny_iterator_get_current (helper->iter));
+       tny_folder_refresh_async (folder, folder_refresh_cb,
+                                 status_update_cb, helper);
+       g_object_unref (G_OBJECT(folder));
 }
 
 gboolean
@@ -651,16 +628,18 @@ modest_mail_operation_create_folder (ModestMailOperation *self,
                                     TnyFolderStore *parent,
                                     const gchar *name)
 {
+       ModestMailOperationPrivate *priv;
+       TnyFolder *new_folder = NULL;
+       TnyStoreAccount *store_account;
+
        g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
        g_return_val_if_fail (name, NULL);
 
-       TnyFolder *new_folder = NULL;
-       TnyStoreAccount *store_account;
+       priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
 
        /* Create the folder */
-       new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
-       if (!new_folder) 
-               return NULL;
+       new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
+       CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, return NULL);
 
        /* Subscribe to folder */
        if (!tny_folder_is_subscribed (new_folder)) {
@@ -687,13 +666,10 @@ modest_mail_operation_remove_folder (ModestMailOperation *self,
        /* Delete folder or move to trash */
        if (remove_to_trash) {
                TnyFolder *trash_folder;
-
-               trash_folder = modest_mail_operation_find_trash_folder (self,
-                                                                       TNY_STORE_ACCOUNT (folder_store));
-
+               trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(folder_store),
+                                                                     TNY_FOLDER_TYPE_TRASH);
                /* TODO: error_handling */
-               modest_mail_operation_move_folder (self, 
-                                                  folder, 
+               modest_mail_operation_move_folder (self, folder, 
                                                   TNY_FOLDER_STORE (trash_folder));
        } else {
                tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
@@ -709,16 +685,21 @@ modest_mail_operation_rename_folder (ModestMailOperation *self,
                                     TnyFolder *folder,
                                     const gchar *name)
 {
+       ModestMailOperationPrivate *priv;
+
        g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
        g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
        g_return_if_fail (name);
 
+       priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
        /* FIXME: better error handling */
        if (strrchr (name, '/') != NULL)
                return;
 
        /* Rename. Camel handles folder subscription/unsubscription */
-       tny_folder_set_name (folder, name, NULL); /* FIXME */
+       tny_folder_set_name (folder, name, &(priv->error));
+       CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, return);
  }
 
 void
@@ -751,36 +732,42 @@ modest_mail_operation_xfer_folder (ModestMailOperation *self,
                                   TnyFolderStore *parent,
                                   gboolean delete_original)
 {
+       ModestMailOperationPrivate *priv;
        const gchar *folder_name;
-       TnyFolder *dest_folder, *child;
-       TnyIterator *iter;
-       TnyList *folders, *headers;
+       TnyFolder *dest_folder = NULL, *child = NULL;
+       TnyIterator *iter = NULL;
+       TnyList *folders = NULL, *headers = NULL;
 
        g_return_if_fail (TNY_IS_FOLDER (folder));
        g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
 
+       priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
        /* Create the destination folder */
        folder_name = tny_folder_get_name (folder);
-       dest_folder = modest_mail_operation_create_folder (self, 
-                                                          parent, folder_name);
+       dest_folder = modest_mail_operation_create_folder (self, parent, folder_name);
+       CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto clean);
 
        /* Transfer messages */
        headers = TNY_LIST (tny_simple_list_new ());
-       tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
-       tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
+       tny_folder_get_headers (folder, headers, FALSE, &(priv->error));
+       CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto clean);
+
+       tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, &(priv->error));
+       CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto clean);
 
        /* Recurse children */
        folders = TNY_LIST (tny_simple_list_new ());
-       tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
-       iter = tny_list_create_iterator (folders);
+       tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL,  &(priv->error));
+       CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto clean);
 
+       iter = tny_list_create_iterator (folders);
        while (!tny_iterator_is_done (iter)) {
-
                child = TNY_FOLDER (tny_iterator_get_current (iter));
-               modest_mail_operation_xfer_folder (self, child,
-                                                  TNY_FOLDER_STORE (dest_folder),
+               modest_mail_operation_xfer_folder (self, child, TNY_FOLDER_STORE (dest_folder),
                                                   delete_original);
                tny_iterator_next (iter);
+               g_object_unref (G_OBJECT(child));
        }
 
        /* Delete source folder (if needed) */
@@ -788,52 +775,18 @@ modest_mail_operation_xfer_folder (ModestMailOperation *self,
                modest_mail_operation_remove_folder (self, folder, FALSE);
 
        /* Clean up */
-       g_object_unref (G_OBJECT (dest_folder));
-       g_object_unref (G_OBJECT (headers));
-       g_object_unref (G_OBJECT (folders));
-       g_object_unref (G_OBJECT (iter));
+ clean:
+       if (dest_folder)
+               g_object_unref (G_OBJECT (dest_folder));
+       if (headers)
+               g_object_unref (G_OBJECT (headers));
+       if (folders)
+               g_object_unref (G_OBJECT (folders));
+       if (iter)
+               g_object_unref (G_OBJECT (iter));
 }
 
 
-/* FIXME: this method should be rewritten when the policy for the
-   Trash folder becomes clearer */
-static TnyFolder *
-modest_mail_operation_find_trash_folder (ModestMailOperation *self,
-                                        TnyStoreAccount *store_account)
-{
-       TnyList *folders;
-       TnyIterator *iter;
-       gboolean found;
-       /*TnyFolderStoreQuery *query;*/
-       TnyFolder *trash_folder;
-
-       /* Look for Trash folder */
-       folders = TNY_LIST (tny_simple_list_new ());
-       tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
-                                     folders, NULL, NULL); /* FIXME */
-       iter = tny_list_create_iterator (folders);
-
-       found = FALSE;
-       while (!tny_iterator_is_done (iter) && !found) {
-
-               trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
-               if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
-                       found = TRUE;
-               else
-                       tny_iterator_next (iter);
-       }
-
-       /* Clean up */
-       g_object_unref (G_OBJECT (folders));
-       g_object_unref (G_OBJECT (iter));
-
-       /* TODO: better error handling management */
-       if (!found) 
-               return NULL;
-       else
-               return trash_folder;
-}
-
 /* ******************************************************************* */
 /* **************************  MSG  ACTIONS  ************************* */
 /* ******************************************************************* */
@@ -877,8 +830,8 @@ modest_mail_operation_remove_msg (ModestMailOperation *self,
                TnyStoreAccount *store_account;
 
                store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
-               trash_folder = modest_mail_operation_find_trash_folder (self, store_account);
-
+               trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
+                                                                     TNY_FOLDER_TYPE_TRASH);
                if (trash_folder) {
                        modest_mail_operation_move_msg (self, header, trash_folder);
 /*                     g_object_unref (trash_folder); */
@@ -896,7 +849,7 @@ modest_mail_operation_remove_msg (ModestMailOperation *self,
                g_object_unref (G_OBJECT (store_account));
        } else {
                tny_folder_remove_msg (folder, header, NULL); /* FIXME */
-               tny_folder_expunge (folder, NULL); /* FIXME */
+               tny_folder_sync(folder, TRUE, NULL); /* FIXME */
        }
 
        /* Free */
@@ -948,34 +901,6 @@ modest_mail_operation_xfer_msg (ModestMailOperation *self,
 /* ******************************************************************* */
 /* ************************* UTILIY FUNCTIONS ************************ */
 /* ******************************************************************* */
-static gboolean
-is_ascii(const gchar *s)
-{
-       while (s[0]) {
-               if (s[0] & 128 || s[0] < 32)
-                       return FALSE;
-               s++;
-       }
-       return TRUE;
-}
-
-static char *
-get_content_type(const gchar *s)
-{
-       GString *type;
-       
-       type = g_string_new("text/plain");
-       if (!is_ascii(s)) {
-               if (g_utf8_validate(s, -1, NULL)) {
-                       g_string_append(type, "; charset=\"utf-8\"");
-               } else {
-                       /* it should be impossible to reach this, but better safe than sorry */
-                       g_warning("invalid utf8 in message");
-                       g_string_append(type, "; charset=\"latin1\"");
-               }
-       }
-       return g_string_free(type, FALSE);
-}
 
 static void
 add_attachments (TnyMsg *msg, GList *attachments_list)
@@ -1009,45 +934,3 @@ add_attachments (TnyMsg *msg, GList *attachments_list)
        }
 }
 
-
-static TnyMimePart *
-add_body_part (TnyMsg *msg, 
-              const gchar *body,
-              const gchar *content_type,
-              gboolean has_attachments)
-{
-       TnyMimePart *text_body_part = NULL;
-       TnyStream *text_body_stream;
-       TnyPlatformFactory *fact;
-
-       fact = modest_tny_platform_factory_get_instance ();
-
-       /* Create the stream */
-       text_body_stream = TNY_STREAM (tny_camel_stream_new
-                                      (camel_stream_mem_new_with_buffer
-                                       (body, strlen(body))));
-
-       /* Create body part if needed */
-       if (has_attachments)
-               text_body_part = tny_platform_factory_new_mime_part (fact);
-       else
-               text_body_part = TNY_MIME_PART(msg);
-
-       /* Construct MIME part */
-       tny_stream_reset (text_body_stream);
-       tny_mime_part_construct_from_stream (text_body_part,
-                                            text_body_stream,
-                                            content_type);
-       tny_stream_reset (text_body_stream);
-
-       /* Add part if needed */
-       if (has_attachments) {
-               tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
-               g_object_unref (G_OBJECT(text_body_part));
-       }
-
-       /* Clean */
-       g_object_unref (text_body_stream);
-
-       return text_body_part;
-}