* src/modest-text-utils.c:
[modest] / src / modest-mail-operation.c
index 9d48dae..a84ba17 100644 (file)
@@ -58,7 +58,6 @@
 #include "modest-mail-operation.h"
 
 #define KB 1024
-#define GET_SIZE_BUFFER_SIZE 128
 
 /* 
  * Remove all these #ifdef stuff when the tinymail's idle calls become
@@ -131,6 +130,7 @@ typedef struct _XFerMsgAsyncHelper
        TnyList *headers;
        TnyFolder *dest_folder;
        XferMsgsAsynUserCallback user_callback; 
+       gboolean delete;
        gpointer user_data;
 } XFerMsgAsyncHelper;
 
@@ -575,9 +575,14 @@ idle_create_msg_cb (gpointer idle_data)
 {
        CreateMsgIdleInfo *info = (CreateMsgIdleInfo *) idle_data;
 
+       /* GDK LOCK: this one is here because we call this from a thread  */
+       /* callback could be 'send mail' or 'save to draft'; i dont knonw */
+       /* why these tow operations must serialized, i think it could be  */
+       /* removed */
        gdk_threads_enter ();
        info->callback (info->mail_op, info->msg, info->userdata);
        gdk_threads_leave ();
+
        g_object_unref (info->mail_op);
        if (info->msg)
                g_object_unref (info->msg);
@@ -824,21 +829,21 @@ modest_mail_operation_save_to_drafts_cb (ModestMailOperation *self,
                goto end;
        }
 
-       if (info->draft_msg != NULL) {
+       if (!priv->error)
+               tny_folder_add_msg (folder, msg, &(priv->error));
+
+       if ((!priv->error) && (info->draft_msg != NULL)) {
                header = tny_msg_get_header (info->draft_msg);
                src_folder = tny_header_get_folder (header); 
                /* Remove the old draft expunging it */
                tny_folder_remove_msg (src_folder, header, NULL);
                tny_header_set_flags (header, TNY_HEADER_FLAG_DELETED);
                tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
-               tny_folder_sync (src_folder, TRUE, &(priv->error));  /* expunge */
+               tny_folder_sync_async (src_folder, TRUE, NULL, NULL, NULL);  /* expunge */
                g_object_unref (header);
        }
        
        if (!priv->error)
-               tny_folder_add_msg (folder, msg, &(priv->error));
-
-       if (!priv->error)
                priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
        else
                priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
@@ -1044,6 +1049,11 @@ idle_notify_progress (gpointer data)
 
        state = modest_mail_operation_clone_state (mail_op);
 #ifdef TINYMAIL_IDLES_NOT_LOCKED_YET
+       /* GDK LOCK: this one is here because we call this from a thread  */
+       /* However, refresh_account operations call async operations on   */
+       /* on tinymail, which also emmit 'progress-changed' signal. This  */
+       /* may be is required to serialize executions of progress-changed'*/
+       /* signal handlers.                                               */
        gdk_threads_enter ();
 #endif
        g_signal_emit (G_OBJECT (mail_op), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
@@ -1067,6 +1077,11 @@ idle_notify_progress_once (gpointer data)
 
        pair = (ModestPair *) data;
 
+       /* GDK LOCK: this one is here because we call this from a thread  */
+       /* However, refresh_account operations call async operations on   */
+       /* on tinymail, which also emmit 'progress-changed' signal. This  */
+       /* may be is required to serialize executions of progress-changed'*/
+       /* signal handlers                                                */
 #ifdef TINYMAIL_IDLES_NOT_LOCKED_YET
        gdk_threads_enter ();
 #endif
@@ -1122,6 +1137,10 @@ compare_headers_by_date (gconstpointer a,
 static gboolean 
 set_last_updated_idle (gpointer data)
 {
+       /* GDK LOCK: this one is here because we call this from a thread  */
+       /* and executed with g_idle_add_full during process of method     */
+       /* update_account_thread, to serialize access to LAST_UPDATED     */
+       /* property of each account                                       */
        gdk_threads_enter ();
 
        /* It does not matter if the time is not exactly the same than
@@ -1145,6 +1164,10 @@ idle_update_account_cb (gpointer data)
 
        idle_info = (UpdateAccountInfo *) data;
 
+       /* GDK LOCK: this one is here because we call this from a thread  */
+       /* and executed with g_idle_add during update_account_thread, to  */
+       /* serialize the execution of refresh user callback (mainloop)    */
+       /* received as parameter in modest_mail_operation_update_account  */
        gdk_threads_enter ();
        idle_info->callback (idle_info->mail_op,
                             idle_info->new_headers,
@@ -1664,6 +1687,11 @@ transfer_folder_status_cb (GObject *obj,
 
        state = modest_mail_operation_clone_state (self);
 #ifdef TINYMAIL_IDLES_NOT_LOCKED_YET
+       /* GDK LOCK: this one is here because we call this from a thread  */
+       /* However, refresh_account operations call async operations on   */
+       /* on tinymail, which also emmit 'progress-changed' signal. This  */
+       /* may be is required to serialize executions of progress-changed'*/
+       /* signal handlers.                                               */
        gdk_threads_enter ();
 #endif
        g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
@@ -1712,6 +1740,9 @@ transfer_folder_cb (TnyFolder *folder,
 
        /* If user defined callback function was defined, call it */
        if (helper->user_callback) {
+               /* GDK LOCK: I think this one can be removed if Tinymail */
+               /* wraps the lock itself.                                */
+               /* This function is called as idle in tinymail.          */
                gdk_threads_enter ();
                helper->user_callback (priv->source, helper->user_data);
                gdk_threads_leave ();
@@ -1957,99 +1988,6 @@ void modest_mail_operation_get_msg (ModestMailOperation *self,
        }
 }
 
-static gboolean
-idle_get_mime_part_size_cb (gpointer userdata)
-{
-       GetMimePartSizeInfo *idle_info;
-
-       idle_info = (GetMimePartSizeInfo *) userdata;
-
-       gdk_threads_enter ();
-       idle_info->callback (idle_info->mail_op,
-                            idle_info->size,
-                            idle_info->userdata);
-       gdk_threads_leave ();
-
-       g_object_unref (idle_info->mail_op);
-       g_slice_free (GetMimePartSizeInfo, idle_info);
-
-       return FALSE;
-}
-
-static gpointer
-get_mime_part_size_thread (gpointer thr_user_data)
-{
-       GetMimePartSizeInfo *info;
-       gchar read_buffer[GET_SIZE_BUFFER_SIZE];
-       TnyStream *stream;
-       gssize readed_size;
-       gssize total = 0;
-       ModestMailOperationPrivate *priv;
-
-       info = (GetMimePartSizeInfo *) thr_user_data;
-       priv = MODEST_MAIL_OPERATION_GET_PRIVATE (info->mail_op);
-
-       stream = tny_camel_mem_stream_new ();
-       tny_mime_part_decode_to_stream (info->mime_part, stream);
-       tny_stream_reset (stream);
-       if (tny_stream_is_eos (stream)) {
-               tny_stream_close (stream);
-               stream = tny_mime_part_get_stream (info->mime_part);
-       }
-       
-       while (!tny_stream_is_eos (stream)) {
-               readed_size = tny_stream_read (stream, read_buffer, GET_SIZE_BUFFER_SIZE);
-               total += readed_size;
-       }
-
-       if (info->callback) {
-               GetMimePartSizeInfo *idle_info;
-
-               idle_info = g_slice_new0 (GetMimePartSizeInfo);
-               idle_info->mail_op = g_object_ref (info->mail_op);
-               idle_info->size = total;
-               idle_info->callback = info->callback;
-               idle_info->userdata = info->userdata;
-               g_idle_add (idle_get_mime_part_size_cb, idle_info);
-       }
-
-       g_idle_add (idle_notify_queue, g_object_ref (info->mail_op));
-
-       g_object_unref (info->mail_op);
-       g_object_unref (stream);
-       g_object_unref (info->mime_part);
-       g_slice_free  (GetMimePartSizeInfo, info);
-
-       return NULL;
-}
-
-void          
-modest_mail_operation_get_mime_part_size (ModestMailOperation *self,
-                                         TnyMimePart *part,
-                                         GetMimePartSizeCallback user_callback,
-                                         gpointer user_data,
-                                         GDestroyNotify notify)
-{
-       GetMimePartSizeInfo *info;
-       ModestMailOperationPrivate *priv;
-       GThread *thread;
-       
-       g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
-       g_return_if_fail (TNY_IS_MIME_PART (part));
-       
-       priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
-
-       priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
-       info = g_slice_new0 (GetMimePartSizeInfo);
-       info->mail_op = g_object_ref (self);
-       info->mime_part = g_object_ref (part);
-       info->callback = user_callback;
-       info->userdata = user_data;
-
-       thread = g_thread_create (get_mime_part_size_thread, info, FALSE, NULL);
-
-}
-
 static void
 get_msg_cb (TnyFolder *folder, 
            gboolean cancelled, 
@@ -2126,6 +2064,11 @@ get_msg_status_cb (GObject *obj,
        state->bytes_done = status->position;
        state->bytes_total = status->of_total;
 #ifdef TINYMAIL_IDLES_NOT_LOCKED_YET
+       /* GDK LOCK: this one is here because we call this from a thread  */
+       /* However, refresh_account operations call async operations on   */
+       /* on tinymail, which also emmit 'progress-changed' signal. This  */
+       /* may be is required to serialize executions of progress-changed'*/
+       /* signal handlers.                                               */
        gdk_threads_enter ();
 #endif
        g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
@@ -2187,6 +2130,9 @@ get_msgs_full_destroyer (gpointer data)
        info = (GetFullMsgsInfo *) data;
 
        if (info->notify) {
+               /* GDK LOCK: this one is here because we call this from */
+               /* a thread.                                            */
+               /* It's used to free user_data, like pasted attachments */
                gdk_threads_enter ();   
                info->notify (info->user_data);
                gdk_threads_leave ();
@@ -2396,20 +2342,23 @@ modest_mail_operation_remove_msg (ModestMailOperation *self,  TnyHeader *header,
 
        priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
 
-
+       /* remove message from folder */
        tny_folder_remove_msg (folder, header, &(priv->error));
-       if (!priv->error) {
-               tny_header_set_flags (header, TNY_HEADER_FLAG_DELETED);
-               tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
-
-               if (TNY_IS_CAMEL_IMAP_FOLDER (folder))
-                       tny_folder_sync(folder, FALSE, &(priv->error)); /* FALSE --> don't expunge */
-               else if (TNY_IS_CAMEL_POP_FOLDER (folder))
-                       tny_folder_sync(folder, TRUE, &(priv->error)); /* TRUE --> expunge */
-               else
-                       /* lcoal folders */
-                       tny_folder_sync(folder, TRUE, &(priv->error)); /* TRUE --> expunge */
-       }
+/*     if (!priv->error) { */
+/*             tny_header_set_flags (header, TNY_HEADER_FLAG_DELETED); */
+/*             tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN); */
+
+/*             if (TNY_IS_CAMEL_IMAP_FOLDER (folder)) */
+/*                     tny_folder_sync_async(folder, FALSE, NULL, NULL, NULL); /\* FALSE --> don't expunge *\/ */
+/* /\*                         tny_folder_sync (folder, FALSE, &(priv->error)); /\\* FALSE --> don't expunge *\\/ *\/ */
+/*             else if (TNY_IS_CAMEL_POP_FOLDER (folder)) */
+/*                     tny_folder_sync_async(folder, FALSE, NULL, NULL, NULL); /\* TRUE --> dont expunge *\/ */
+/* /\*                         tny_folder_sync (folder, TRUE, &(priv->error)); /\\* TRUE --> expunge *\\/ *\/ */
+/*             else */
+/*                     /\* local folders *\/ */
+/*                     tny_folder_sync_async(folder, TRUE, NULL, NULL, NULL); /\* TRUE --> expunge *\/ */
+/* /\*                         tny_folder_sync (folder, TRUE, &(priv->error)); /\\* TRUE --> expunge *\\/ *\/ */
+/*     } */
        
        
        /* Set status */
@@ -2450,6 +2399,11 @@ transfer_msgs_status_cb (GObject *obj,
 
        state = modest_mail_operation_clone_state (self);
 #ifdef TINYMAIL_IDLES_NOT_LOCKED_YET
+       /* GDK LOCK: this one is here because we call this from a thread  */
+       /* However, refresh_account operations call async operations on   */
+       /* on tinymail, which also emmit 'progress-changed' signal. This  */
+       /* may be is required to serialize executions of progress-changed'*/
+       /* signal handlers.                                               */
        gdk_threads_enter ();
 #endif
        g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
@@ -2466,6 +2420,8 @@ transfer_msgs_cb (TnyFolder *folder, gboolean cancelled, GError **err, gpointer
        XFerMsgAsyncHelper *helper;
        ModestMailOperation *self;
        ModestMailOperationPrivate *priv;
+       TnyIterator *iter = NULL;
+       TnyHeader *header = NULL;
 
        helper = (XFerMsgAsyncHelper *) user_data;
        self = helper->mail_op;
@@ -2487,22 +2443,48 @@ transfer_msgs_cb (TnyFolder *folder, gboolean cancelled, GError **err, gpointer
                priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
        }
 
+       
+       /* Mark headers as deleted and seen */
+       if ((helper->delete) && 
+           (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS)) {
+               iter = tny_list_create_iterator (helper->headers);
+               while (!tny_iterator_is_done (iter)) {
+                       header = TNY_HEADER (tny_iterator_get_current (iter));
+                       tny_header_set_flags (header, TNY_HEADER_FLAG_DELETED);
+                       tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
+                       g_object_unref (header);
+
+                       tny_iterator_next (iter);
+               }
+
+       }
+               
+
        /* Notify about operation end */
        modest_mail_operation_notify_end (self);
 
        /* If user defined callback function was defined, call it */
        if (helper->user_callback) {
+               /* GDK LOCK: I think this one can be removed if Tinymail */
+               /* wraps the lock itself.                                */
+               /* This function is called as idle in tinymail.          */
                gdk_threads_enter ();
                helper->user_callback (priv->source, helper->user_data);
                gdk_threads_leave ();
        }
 
        /* Free */
-       g_object_unref (helper->headers);
-       g_object_unref (helper->dest_folder);
-       g_object_unref (helper->mail_op);
+       if (helper->headers)
+               g_object_unref (helper->headers);
+       if (helper->dest_folder)
+               g_object_unref (helper->dest_folder);
+       if (helper->mail_op)
+               g_object_unref (helper->mail_op);
+       if (folder)
+               g_object_unref (folder);
+       if (iter)
+               g_object_unref (iter);
        g_slice_free   (XFerMsgAsyncHelper, helper);
-       g_object_unref (folder);
 
 }
 
@@ -2582,6 +2564,7 @@ modest_mail_operation_xfer_msgs (ModestMailOperation *self,
        helper->headers = g_object_ref(headers);
        helper->user_callback = user_callback;
        helper->user_data = user_data;
+       helper->delete = delete_original;
 
        /* Get account and set it into mail_operation */
        priv->account = modest_tny_folder_get_account (src_folder);
@@ -2611,6 +2594,8 @@ on_refresh_folder (TnyFolder   *folder,
        self = helper->mail_op;
        priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
 
+       g_return_if_fail(priv!=NULL);
+
        if (*error) {
                priv->error = g_error_copy (*error);
                priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
@@ -2630,6 +2615,9 @@ on_refresh_folder (TnyFolder   *folder,
  out:
        /* Call user defined callback, if it exists */
        if (helper->user_callback) {
+               /* GDK LOCK: I think this one can be removed if Tinymail */
+               /* wraps the lock itself.                                */
+               /* This function is called as idle in tinymail.          */
                gdk_threads_enter ();
                helper->user_callback (self, folder, helper->user_data);
                gdk_threads_leave ();
@@ -2640,6 +2628,7 @@ on_refresh_folder (TnyFolder   *folder,
 
        /* Notify about operation end */
        modest_mail_operation_notify_end (self);
+       g_object_unref(self);
 }
 
 static void
@@ -2667,6 +2656,11 @@ on_refresh_folder_status_update (GObject *obj,
 
        state = modest_mail_operation_clone_state (self);
 #ifdef TINYMAIL_IDLES_NOT_LOCKED_YET
+       /* GDK LOCK: this one is here because we call this from a thread  */
+       /* However, refresh_account operations call async operations on   */
+       /* on tinymail, which also emmit 'progress-changed' signal. This  */
+       /* may be is required to serialize executions of progress-changed'*/
+       /* signal handlers.                                               */
        gdk_threads_enter ();
 #endif
        g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
@@ -2694,7 +2688,7 @@ modest_mail_operation_refresh_folder  (ModestMailOperation *self,
 
        /* Create the helper */
        helper = g_slice_new0 (RefreshAsyncHelper);
-       helper->mail_op = self;
+       helper->mail_op = g_object_ref(self);
        helper->user_callback = user_callback;
        helper->user_data = user_data;