X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fmodest-mail-operation.c;h=d6882a94fb5e9bdba92173d1e84073750092c3e3;hb=9102255b85bbf4588d9e8fb41d404e58c5b15174;hp=4f277ab18e53bf8f16f38a9ab506cfb7acd2268d;hpb=54b2b4b64a81caaf214c47da59057b54bf7b067c;p=modest diff --git a/src/modest-mail-operation.c b/src/modest-mail-operation.c index 4f277ab..d6882a9 100644 --- a/src/modest-mail-operation.c +++ b/src/modest-mail-operation.c @@ -66,6 +66,7 @@ #ifdef MODEST_USE_LIBTIME #include #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; @@ -223,6 +223,12 @@ typedef struct _XFerFolderAsyncHelper gpointer user_data; } XFerFolderAsyncHelper; +typedef struct _SyncFolderHelper { + ModestMailOperation *mail_op; + SyncFolderCallback user_callback; + gpointer user_data; +} SyncFolderHelper; + typedef void (*ModestMailOperationCreateMsgCallback) (ModestMailOperation *mail_op, TnyMsg *msg, gpointer userdata); @@ -1139,6 +1145,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 +1203,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 +1248,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 +1285,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); } @@ -1528,7 +1601,6 @@ update_account_get_msg_async_cb (TnyFolder *folder, /* Call the user callback and free */ new_headers = tny_iterator_get_list (msg_info->more_msgs); update_account_notify_user_and_free (info, new_headers); - g_object_unref (new_headers); /* Delete the helper */ g_object_unref (msg_info->more_msgs); @@ -1572,7 +1644,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 +1733,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 +1750,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 +1770,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 +1945,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 +2001,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); @@ -1961,7 +2018,7 @@ modest_mail_operation_update_account (ModestMailOperation *self, /* Get all folders and continue in the callback */ folders = tny_simple_list_new (); tny_folder_store_get_folders_async (TNY_FOLDER_STORE (priv->account), - folders, NULL, FALSE, + folders, NULL, TRUE, recurse_folders_async_cb, NULL, info); g_object_unref (folders); @@ -2097,7 +2154,7 @@ modest_mail_operation_create_folder (ModestMailOperation *self, } } - if (!strcmp (name, " ") || strchr (name, '/')) { + if (!priv->error && (!strcmp (name, " ") || strchr (name, '/'))) { priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED; g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR, MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES, @@ -2411,7 +2468,7 @@ modest_mail_operation_xfer_folder (ModestMailOperation *self, priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED; g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR, MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES, - error_msg); + "%s", error_msg); /* Call the user callback if exists */ if (user_callback) @@ -2495,6 +2552,69 @@ modest_mail_operation_rename_folder (ModestMailOperation *self, /* ******************************************************************* */ void +modest_mail_operation_find_msg (ModestMailOperation *self, + TnyFolder *folder, + const gchar *msg_uid, + gboolean progress_feedback, + GetMsgAsyncUserCallback user_callback, + gpointer user_data) +{ + GetMsgInfo *helper = NULL; + ModestMailOperationPrivate *priv; + + g_return_if_fail (MODEST_IS_MAIL_OPERATION (self)); + g_return_if_fail (msg_uid != NULL); + + priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self); + priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS; + priv->total = 1; + priv->done = 0; + + /* Check memory low */ + if (_check_memory_low (self)) { + if (user_callback) + user_callback (self, NULL, FALSE, NULL, priv->error, user_data); + modest_mail_operation_notify_end (self); + return; + } + + /* Get account and set it into mail_operation */ + priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder)); + + /* Check for cached messages */ + if (progress_feedback) { + priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE; + } else { + priv->op_type = MODEST_MAIL_OPERATION_TYPE_UNKNOWN; + } + + /* Create the helper */ + helper = g_slice_new0 (GetMsgInfo); + helper->header = NULL; + helper->mail_op = g_object_ref (self); + helper->user_callback = user_callback; + helper->user_data = user_data; + helper->destroy_notify = NULL; + helper->last_total_bytes = 0; + helper->sum_total_bytes = 0; + helper->total_bytes = 0; + helper->more_msgs = NULL; + + modest_mail_operation_notify_start (self); + + /* notify about the start of the operation */ + ModestMailOperationState *state; + state = modest_mail_operation_clone_state (self); + state->done = 0; + state->total = 0; + g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], + 0, state, NULL); + g_slice_free (ModestMailOperationState, state); + + tny_folder_find_msg_async (folder, msg_uid, get_msg_async_cb, get_msg_status_cb, helper); +} + +void modest_mail_operation_get_msg (ModestMailOperation *self, TnyHeader *header, gboolean progress_feedback, @@ -2620,13 +2740,15 @@ get_msg_async_cb (TnyFolder *folder, } else { g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR, MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND, - err->message); + "%s", err->message); } } else if (finished && priv->status == MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS) { /* Set the success status before calling the user callback */ priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS; } + if (info->header == NULL && msg) + info->header = tny_msg_get_header (msg); /* Call the user callback */ if (info->user_callback) @@ -2645,7 +2767,8 @@ get_msg_async_cb (TnyFolder *folder, /* Clean */ if (info->more_msgs) g_object_unref (info->more_msgs); - g_object_unref (info->header); + if (info->header) + g_object_unref (info->header); g_object_unref (info->mail_op); g_slice_free (GetMsgInfo, info); } else if (info->more_msgs) { @@ -2790,6 +2913,7 @@ remove_msgs_async_cb (TnyFolder *folder, ModestMailOperation *self; ModestMailOperationPrivate *priv; ModestProtocolRegistry *protocol_registry; + SyncFolderHelper *helper; self = (ModestMailOperation *) user_data; priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self); @@ -2825,9 +2949,17 @@ remove_msgs_async_cb (TnyFolder *folder, else expunge = FALSE; + /* Create helper */ + helper = g_slice_new0 (SyncFolderHelper); + helper->mail_op = g_object_ref (self); + helper->user_callback = NULL; + helper->user_data = NULL; + /* Sync folder */ - tny_folder_sync_async(folder, expunge, sync_folder_finish_callback, - NULL, self); + tny_folder_sync_async(folder, expunge, sync_folder_finish_callback, NULL, helper); + + /* Remove the extra reference */ + g_object_unref (self); } void @@ -3556,17 +3688,17 @@ modest_mail_operation_shutdown (ModestMailOperation *self, ModestTnyAccountStore } static void -sync_folder_finish_callback (TnyFolder *self, - gboolean cancelled, - GError *err, +sync_folder_finish_callback (TnyFolder *self, + gboolean cancelled, + GError *err, gpointer user_data) { - ModestMailOperation *mail_op; ModestMailOperationPrivate *priv; + SyncFolderHelper *helper; - mail_op = (ModestMailOperation *) user_data; - priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op); + helper = (SyncFolderHelper *) user_data; + priv = MODEST_MAIL_OPERATION_GET_PRIVATE (helper->mail_op); /* If canceled by the user, ignore the error given by Tinymail */ if (cancelled) { @@ -3585,15 +3717,26 @@ sync_folder_finish_callback (TnyFolder *self, priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS; } - modest_mail_operation_notify_end (mail_op); - g_object_unref (mail_op); + /* User callback */ + if (helper->user_callback) + helper->user_callback (helper->mail_op, self, helper->user_data); + + modest_mail_operation_notify_end (helper->mail_op); + + /* Frees */ + g_object_unref (helper->mail_op); + g_slice_free (SyncFolderHelper, helper); } void modest_mail_operation_sync_folder (ModestMailOperation *self, - TnyFolder *folder, gboolean expunge) + TnyFolder *folder, + gboolean expunge, + SyncFolderCallback callback, + gpointer user_data) { ModestMailOperationPrivate *priv; + SyncFolderHelper *helper; g_return_if_fail (MODEST_IS_MAIL_OPERATION (self)); g_return_if_fail (TNY_IS_FOLDER (folder)); @@ -3603,11 +3746,16 @@ modest_mail_operation_sync_folder (ModestMailOperation *self, priv->account = modest_tny_folder_get_account (folder); priv->op_type = MODEST_MAIL_OPERATION_TYPE_SYNC_FOLDER; + /* Create helper */ + helper = g_slice_new0 (SyncFolderHelper); + helper->mail_op = g_object_ref (self); + helper->user_callback = callback; + helper->user_data = user_data; + modest_mail_operation_notify_start (self); - g_object_ref (self); - tny_folder_sync_async (folder, expunge, - (TnyFolderCallback) sync_folder_finish_callback, - NULL, self); + tny_folder_sync_async (folder, expunge, + (TnyFolderCallback) sync_folder_finish_callback, + NULL, helper); } static void @@ -3702,12 +3850,14 @@ modest_mail_operation_to_string (ModestMailOperation *self) switch (priv->op_type) { case MODEST_MAIL_OPERATION_TYPE_SEND: type= "SEND"; break; + case MODEST_MAIL_OPERATION_TYPE_SEND_AND_RECEIVE: type= "SEND-AND-RECEIVE"; break; case MODEST_MAIL_OPERATION_TYPE_RECEIVE: type= "RECEIVE"; break; case MODEST_MAIL_OPERATION_TYPE_OPEN: type= "OPEN"; break; case MODEST_MAIL_OPERATION_TYPE_DELETE: type= "DELETE"; break; case MODEST_MAIL_OPERATION_TYPE_INFO: type= "INFO"; break; case MODEST_MAIL_OPERATION_TYPE_RUN_QUEUE: type= "RUN-QUEUE"; break; case MODEST_MAIL_OPERATION_TYPE_SYNC_FOLDER: type= "SYNC-FOLDER"; break; + case MODEST_MAIL_OPERATION_TYPE_SHUTDOWN: type= "SHUTDOWN"; break; case MODEST_MAIL_OPERATION_TYPE_UNKNOWN: type= "UNKNOWN"; break; default: type = "UNEXPECTED"; break; }