Fix reference leaks causing power consuption because of running IDLE.
[modest] / src / modest-mail-operation.c
index 49e1818..aa1d1cd 100644 (file)
@@ -66,6 +66,7 @@
 #ifdef MODEST_USE_LIBTIME
 #include <clockd/libtime.h>
 #endif
+#include "modest-account-protocol.h"
 
 #define KB 1024
 
@@ -135,7 +136,6 @@ typedef struct
        gint pending_calls;
        gboolean poke_all;
        TnyFolderObserver *inbox_observer;
-       RetrieveAllCallback retrieve_all_cb;
        gboolean interactive;
        gboolean msg_readed;
 } UpdateAccountInfo;
@@ -1139,6 +1139,46 @@ modest_mail_operation_send_new_mail (ModestMailOperation *self,
 
 typedef struct
 {
+       ModestMailOperation *mailop;
+       TnyMsg *msg;
+       SaveToDraftstCallback callback;
+       gpointer userdata;
+} FinishSaveRemoteDraftInfo;
+
+static void
+finish_save_remote_draft (ModestAccountProtocol *protocol,
+                         GError *err,
+                         const gchar *account_id,
+                         TnyMsg *new_remote_msg,
+                         TnyMsg *new_msg,
+                         TnyMsg *old_msg,
+                         gpointer userdata)
+{
+       FinishSaveRemoteDraftInfo *info = (FinishSaveRemoteDraftInfo *) userdata;
+       ModestMailOperationPrivate *priv = NULL;
+
+       priv = MODEST_MAIL_OPERATION_GET_PRIVATE(info->mailop);
+
+       if (!priv->error && err != NULL) {
+               /* Priority for errors in save to local stage */
+               priv->error = g_error_copy (err);
+               priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+       }
+
+       if (info->callback)
+               info->callback (info->mailop, info->msg, info->userdata);
+
+       if (info->msg)
+               g_object_unref (info->msg);
+
+       modest_mail_operation_notify_end (info->mailop);
+       g_object_unref (info->mailop);
+
+       g_slice_free (FinishSaveRemoteDraftInfo, info);
+}
+
+typedef struct
+{
        TnyTransportAccount *transport_account;
        TnyMsg *draft_msg;
        SaveToDraftstCallback callback;
@@ -1157,6 +1197,7 @@ modest_mail_operation_save_to_drafts_add_msg_cb(TnyFolder *self,
        ModestMailOperationPrivate *priv = NULL;
        SaveToDraftsAddMsgInfo *info = (SaveToDraftsAddMsgInfo *) userdata;
        GError *io_error = NULL;
+       gboolean callback_called = FALSE;
 
        priv = MODEST_MAIL_OPERATION_GET_PRIVATE(info->mailop);
 
@@ -1201,8 +1242,32 @@ modest_mail_operation_save_to_drafts_add_msg_cb(TnyFolder *self,
                priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
        }
 
+       if (info->transport_account) {
+               ModestProtocolType transport_protocol_type;
+               ModestProtocol *transport_protocol;
+
+               transport_protocol_type = modest_tny_account_get_protocol_type (TNY_ACCOUNT (info->transport_account));
+
+               transport_protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
+                                                                                   transport_protocol_type);
+               if (transport_protocol && MODEST_IS_ACCOUNT_PROTOCOL (transport_protocol)) {
+                       FinishSaveRemoteDraftInfo *srd_info = g_slice_new (FinishSaveRemoteDraftInfo);
+                       srd_info->mailop = info->mailop?g_object_ref (info->mailop):NULL;
+                       srd_info->msg = info->msg?g_object_ref (info->msg):NULL;
+                       srd_info->callback = info->callback;
+                       srd_info->userdata = info->user_data;
+                       modest_account_protocol_save_remote_draft (MODEST_ACCOUNT_PROTOCOL (transport_protocol), 
+                                                                  tny_account_get_id (TNY_ACCOUNT (info->transport_account)),
+                                                                  info->msg, info->draft_msg,
+                                                                  finish_save_remote_draft,
+                                                                  srd_info);
+                                                                  
+                       callback_called = TRUE;
+               }
+       }
+
        /* Call the user callback */
-       if (info->callback)
+       if (!callback_called && info->callback)
                info->callback (info->mailop, info->msg, info->user_data);
 
        if (info->transport_account)
@@ -1214,8 +1279,10 @@ modest_mail_operation_save_to_drafts_add_msg_cb(TnyFolder *self,
        if (info->msg)
                g_object_unref (G_OBJECT (info->msg));
 
-       modest_mail_operation_notify_end (info->mailop);
-       g_object_unref(info->mailop);
+       if (!callback_called)
+               modest_mail_operation_notify_end (info->mailop);
+        if (info->mailop)
+               g_object_unref(info->mailop);
        g_slice_free (SaveToDraftsAddMsgInfo, info);
 }
 
@@ -1572,7 +1639,7 @@ inbox_refreshed_cb (TnyFolder *inbox,
        ModestAccountMgr *mgr;
        ModestAccountRetrieveType retrieve_type;
        TnyList *new_headers = NULL;
-       gboolean headers_only, ignore_limit;
+       gboolean headers_only;
        time_t time_to_store;
 
        info = (UpdateAccountInfo *) user_data;
@@ -1661,25 +1728,13 @@ inbox_refreshed_cb (TnyFolder *inbox,
        /* Order by date */
        g_ptr_array_sort (new_headers_array, (GCompareFunc) compare_headers_by_date);
 
-       /* Ask the users if they want to retrieve all the messages
-          even though the limit was exceeded */
-       ignore_limit = FALSE;
-       if (new_headers_array->len > retrieve_limit) {
-               /* Ask the user if a callback has been specified and
-                  if the mail operation has a source (this means that
-                  was invoked by the user and not automatically by a
-                  D-Bus method) */
-               if (info->retrieve_all_cb && priv->source)
-                       ignore_limit = info->retrieve_all_cb (priv->source,
-                                                             new_headers_array->len,
-                                                             retrieve_limit);
-       }
-
        /* Copy the headers to a list and free the array */
        new_headers = tny_simple_list_new ();
        for (i=0; i < new_headers_array->len; i++) {
                TnyHeader *header = TNY_HEADER (g_ptr_array_index (new_headers_array, i));
-               tny_list_append (new_headers, G_OBJECT (header));
+               /* We want the first element to be the most recent
+                  one, that's why we reverse the list */
+               tny_list_prepend (new_headers, G_OBJECT (header));
        }
        g_ptr_array_foreach (new_headers_array, (GFunc) g_object_unref, NULL);
        g_ptr_array_free (new_headers_array, FALSE);
@@ -1690,10 +1745,7 @@ inbox_refreshed_cb (TnyFolder *inbox,
                GetMsgInfo *msg_info;
 
                priv->done = 0;
-               if (ignore_limit)
-                       priv->total = tny_list_get_length (new_headers);
-               else
-                       priv->total = MIN (tny_list_get_length (new_headers), retrieve_limit);
+               priv->total = MIN (tny_list_get_length (new_headers), retrieve_limit);
 
                iter = tny_list_create_iterator (new_headers);
 
@@ -1713,11 +1765,13 @@ inbox_refreshed_cb (TnyFolder *inbox,
                                                  NULL, msg_info);
 
                        g_object_unref (folder);
+                       g_object_unref (header);
 
                        msg_num++;
                        tny_iterator_next (iter);
                }
                g_object_unref (iter);
+               g_object_unref (new_headers);
 
                /* The mail operation will finish when the last
                   message is retrieved */
@@ -1886,7 +1940,6 @@ modest_mail_operation_update_account (ModestMailOperation *self,
                                      const gchar *account_name,
                                      gboolean poke_all,
                                      gboolean interactive,
-                                     RetrieveAllCallback retrieve_all_cb,
                                      UpdateAccountCallback callback,
                                      gpointer user_data)
 {
@@ -1943,7 +1996,6 @@ modest_mail_operation_update_account (ModestMailOperation *self,
        info->account_name = g_strdup (account_name);
        info->callback = callback;
        info->user_data = user_data;
-       info->retrieve_all_cb = retrieve_all_cb;
 
        /* Set account busy */
        modest_account_mgr_set_account_busy (modest_runtime_get_account_mgr (), account_name, TRUE);