#include <tny-status.h>
#include <camel/camel-stream-mem.h>
#include <glib/gi18n.h>
+#include "modest-platform.h"
#include <modest-tny-account.h>
#include <modest-tny-send-queue.h>
#include <modest-runtime.h>
static void modest_mail_operation_init (ModestMailOperation *obj);
static void modest_mail_operation_finalize (GObject *obj);
-static void update_folders_cb (TnyFolderStore *self,
- TnyList *list,
- GError **err,
- gpointer user_data);
+/* static void update_process_msg_status_cb (GObject *obj, */
+/* TnyStatus *status, */
+/* gpointer user_data); */
+static void get_msg_cb (TnyFolder *folder,
+ gboolean cancelled,
+ TnyMsg *msg,
+ GError **err,
+ gpointer user_data);
+
+static void get_msg_status_cb (GObject *obj,
+ TnyStatus *status,
+ gpointer user_data);
+
+static void modest_mail_operation_notify_end (ModestMailOperation *self);
enum _ModestMailOperationSignals
{
guint total;
ModestMailOperationStatus status;
ModestMailOperationId id;
+ GObject *source;
+ ErrorCheckingUserCallback error_checking;
GError *error;
};
priv->status = new_status;\
}
-typedef struct _RefreshFolderAsyncHelper
-{
+typedef struct _GetMsgAsyncHelper {
ModestMailOperation *mail_op;
- TnyIterator *iter;
- guint failed;
- guint canceled;
-
-} RefreshFolderAsyncHelper;
+ GetMsgAsynUserCallback user_callback;
+ guint pending_ops;
+ gpointer user_data;
+} GetMsgAsyncHelper;
typedef struct _XFerMsgAsyncHelper
{
ModestMailOperation *mail_op;
TnyList *headers;
TnyFolder *dest_folder;
-
+ XferMsgsAsynUserCallback user_callback;
+ gpointer user_data;
} XFerMsgAsyncHelper;
+typedef struct _XFerFolderAsyncHelper
+{
+ ModestMailOperation *mail_op;
+
+} XFerFolderAsyncHelper;
/* globals */
static GObjectClass *parent_class = NULL;
priv->error = NULL;
priv->done = 0;
priv->total = 0;
+ priv->source = NULL;
}
static void
g_error_free (priv->error);
priv->error = NULL;
}
+ if (priv->source) {
+ g_object_unref (priv->source);
+ priv->source = NULL;
+ }
G_OBJECT_CLASS(parent_class)->finalize (obj);
}
ModestMailOperation*
-modest_mail_operation_new (ModestMailOperationId id)
+modest_mail_operation_new (ModestMailOperationId id,
+ GObject *source)
{
ModestMailOperation *obj;
ModestMailOperationPrivate *priv;
-
-
+
obj = MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
priv->id = id;
+ if (source != NULL)
+ priv->source = g_object_ref(source);
return obj;
}
+ModestMailOperation*
+modest_mail_operation_new_with_error_handling (ModestMailOperationId id,
+ GObject *source,
+ ErrorCheckingUserCallback error_handler)
+{
+ ModestMailOperation *obj;
+ ModestMailOperationPrivate *priv;
+
+ obj = modest_mail_operation_new (id, source);
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
+
+ g_return_val_if_fail (error_handler != NULL, obj);
+ priv->error_checking = error_handler;
+
+ return obj;
+}
+
+void
+modest_mail_operation_execute_error_handler (ModestMailOperation *self)
+{
+ ModestMailOperationPrivate *priv;
+
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
+ g_return_if_fail(priv->status != MODEST_MAIL_OPERATION_STATUS_SUCCESS);
+
+ if (priv->error_checking == NULL) return;
+ priv->error_checking (priv->source, self);
+}
ModestMailOperationId
return priv->id;
}
+gboolean
+modest_mail_operation_is_mine (ModestMailOperation *self,
+ GObject *me)
+{
+ ModestMailOperationPrivate *priv;
+
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
+ if (priv->source == NULL) return FALSE;
+
+ return priv->source == me;
+}
+
+
void
modest_mail_operation_send_mail (ModestMailOperation *self,
TnyTransportAccount *transport_account,
g_printerr ("modest: error adding msg to send queue: %s\n",
err->message);
g_error_free (err);
- } else
- g_message ("modest: message added to send queue");
+ } else {
+ /* g_message ("modest: message added to send queue"); */
+ }
}
- /* Notify the queue */
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
}
void
goto cleanup;
}
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ modest_mail_operation_notify_end (self);
/* Free */
cleanup:
g_object_unref (G_OBJECT(folder));
}
+typedef struct
+{
+ ModestMailOperation *mail_op;
+ TnyStoreAccount *account;
+ TnyTransportAccount *transport_account;
+} UpdateAccountInfo;
+
static void
recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
{
TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
tny_list_prepend (all_folders, G_OBJECT (folder));
-
- recurse_folders (folder, query, all_folders);
-
+ recurse_folders (folder, query, all_folders);
g_object_unref (G_OBJECT (folder));
tny_iterator_next (iter);
g_object_unref (G_OBJECT (folders));
}
-static void
-update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
+/*
+ * Used by update_account_thread to emit the "progress-changed" signal
+ * from the main loop. We call it inside an idle call to achieve that
+ */
+static gboolean
+notify_update_account_observers (gpointer data)
{
- ModestMailOperation *self;
+ ModestMailOperation *mail_op = MODEST_MAIL_OPERATION (data);
+
+ g_signal_emit (G_OBJECT (mail_op), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
+
+ return TRUE;
+}
+
+/*
+ * Used by update_account_thread to notify the queue from the main
+ * loop. We call it inside an idle call to achieve that
+ */
+static gboolean
+notify_update_account_queue (gpointer data)
+{
+ ModestMailOperation *mail_op = MODEST_MAIL_OPERATION (data);
+
+ modest_mail_operation_notify_end (mail_op);
+ g_object_unref (mail_op);
+
+ return FALSE;
+}
+
+static gpointer
+update_account_thread (gpointer thr_user_data)
+{
+ UpdateAccountInfo *info;
+ TnyList *all_folders = NULL;
+ TnyIterator *iter = NULL;
+ TnyFolderStoreQuery *query = NULL;
ModestMailOperationPrivate *priv;
- TnyIterator *iter;
- TnyList *all_folders;
-
- self = MODEST_MAIL_OPERATION (user_data);
- priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+ ModestTnySendQueue *send_queue;
+ gint timeout;
- g_message (__FUNCTION__);
-
- if (*err) {
- priv->error = g_error_copy (*err);
+ info = (UpdateAccountInfo *) thr_user_data;
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(info->mail_op);
+
+ /* Get all the folders We can do it synchronously because
+ we're already running in a different thread than the UI */
+ all_folders = tny_simple_list_new ();
+ query = tny_folder_store_query_new ();
+ tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
+ tny_folder_store_get_folders (TNY_FOLDER_STORE (info->account),
+ all_folders,
+ query,
+ &(priv->error));
+ if (priv->error) {
priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
goto out;
}
- /* Get all the folders We can do it synchronously because
- we're already running in a different thread than the UI */
- all_folders = tny_list_copy (list);
iter = tny_list_create_iterator (all_folders);
while (!tny_iterator_is_done (iter)) {
TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
- recurse_folders (folder, NULL, all_folders);
+ recurse_folders (folder, query, all_folders);
tny_iterator_next (iter);
}
g_object_unref (G_OBJECT (iter));
+ /* Update status and notify. We need to call the notification
+ with a source functopm in order to call it from the main
+ loop. We need that in order not to get into trouble with
+ Gtk+. We use a timeout in order to provide more status
+ information, because the sync tinymail call does not
+ provide it for the moment */
+ timeout = g_timeout_add (250, notify_update_account_observers, info->mail_op);
+
/* Refresh folders */
iter = tny_list_create_iterator (all_folders);
- priv->total = tny_list_get_length (all_folders);
-
while (!tny_iterator_is_done (iter) && !priv->error) {
TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
/* Refresh the folder */
tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
+ /* TODO: Apply retrieval types */
+
+ /* TODO: apply per-message size limits */
+
+ /* TODO: apply message count limit */
+
if (priv->error) {
priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
- } else {
- /* Update status and notify */
- priv->done++;
- g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
}
-
+
g_object_unref (G_OBJECT (folder));
-
tny_iterator_next (iter);
}
-
g_object_unref (G_OBJECT (iter));
- out:
- g_object_unref (G_OBJECT (list));
+ g_source_remove (timeout);
+
+ /* Perform send */
+ priv->id = MODEST_MAIL_OPERATION_ID_SEND;
+
+ send_queue = modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(info->transport_account));
+ timeout = g_timeout_add (250, notify_update_account_observers, info->mail_op);
+ modest_tny_send_queue_flush (send_queue);
+ g_source_remove (timeout);
+
+ g_object_unref (G_OBJECT(send_queue));
+
/* Check if the operation was a success */
- if (priv->done == priv->total && !priv->error)
+ if (!priv->error) {
priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
- /* Free */
- g_object_unref (G_OBJECT (folder_store));
+ /* Update the last updated key */
+ modest_account_mgr_set_int (modest_runtime_get_account_mgr (),
+ tny_account_get_id (TNY_ACCOUNT (info->account)),
+ MODEST_ACCOUNT_LAST_UPDATED,
+ time(NULL),
+ TRUE);
+ }
- /* Notify the queue */
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ out:
+ /* Notify about operation end. Note that the info could be
+ freed before this idle happens, but the mail operation will
+ be still alive */
+ g_idle_add (notify_update_account_queue, info->mail_op);
+
+ /* Frees */
+ g_object_unref (query);
+ g_object_unref (all_folders);
+ g_object_unref (info->account);
+ g_object_unref (info->transport_account);
+ g_slice_free (UpdateAccountInfo, info);
+
+ return NULL;
}
gboolean
modest_mail_operation_update_account (ModestMailOperation *self,
- TnyStoreAccount *store_account)
+ const gchar *account_name)
{
+ GThread *thread;
+ UpdateAccountInfo *info;
ModestMailOperationPrivate *priv;
- TnyList *folders;
+ TnyStoreAccount *modest_account;
+ TnyTransportAccount *transport_account;
g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
- g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
-
- /* Pick async call reference */
- g_object_ref (store_account);
+ g_return_val_if_fail (account_name, FALSE);
+ /* Init mail operation. Set total and done to 0, and do not
+ update them, this way the progress objects will know that
+ we have no clue about the number of the objects */
priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
-
priv->total = 0;
priv->done = 0;
priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
- /* Get subscribed folders & refresh them */
- folders = TNY_LIST (tny_simple_list_new ());
+ /* Get the Modest account */
+ modest_account = (TnyStoreAccount *)
+ modest_tny_account_store_get_tny_account_by_account (modest_runtime_get_account_store (),
+ account_name,
+ TNY_ACCOUNT_TYPE_STORE);
+
+ if (!modest_account) {
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
+ "cannot get tny store account for %s\n", account_name);
+ modest_mail_operation_notify_end (self);
+ return FALSE;
+ }
+
+ /* Get the transport account, we can not do it in the thread
+ due to some problems with dbus */
+ transport_account = (TnyTransportAccount *)
+ modest_tny_account_store_get_transport_account_for_open_connection (modest_runtime_get_account_store(),
+ account_name);
+ if (!transport_account) {
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
+ "cannot get tny transport account for %s\n", account_name);
+ modest_mail_operation_notify_end (self);
+ return FALSE;
+ }
+
+ /* Create the helper object */
+ info = g_slice_new (UpdateAccountInfo);
+ info->mail_op = self;
+ info->account = modest_account;
+ info->transport_account = transport_account;
+
+ thread = g_thread_create (update_account_thread, info, FALSE, NULL);
- g_message ("tny_folder_store_get_folders_async");
- tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
- folders, update_folders_cb, NULL, NULL, self);
-
return TRUE;
}
gboolean
modest_mail_operation_cancel (ModestMailOperation *self)
{
- /* TODO */
+ ModestMailOperationPrivate *priv;
+
+ if (!MODEST_IS_MAIL_OPERATION (self)) {
+ g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
+ return FALSE;
+ }
+
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+ /* TODO: Tinymail does not support cancel operation */
+/* tny_account_cancel (); */
+
+ /* Set new status */
+ priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
+
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
+
return TRUE;
}
g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
g_return_val_if_fail (name, NULL);
-
+
priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
/* Check parent */
CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
}
- /* Notify the queue */
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
return new_folder;
}
g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
g_return_if_fail (TNY_IS_FOLDER (folder));
-
+
priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
-
+
/* Check folder rules */
rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
if (rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE) {
/* Delete folder or move to trash */
if (remove_to_trash) {
- TnyFolder *trash_folder, *new_folder;
+ TnyFolder *trash_folder = NULL;
+/* TnyFolder *trash_folder, *new_folder; */
trash_folder = modest_tny_account_get_special_folder (account,
TNY_FOLDER_TYPE_TRASH);
/* TODO: error_handling */
- new_folder = modest_mail_operation_xfer_folder (self, folder,
- TNY_FOLDER_STORE (trash_folder), TRUE);
- g_object_unref (G_OBJECT (new_folder));
+ modest_mail_operation_xfer_folder (self, folder,
+ TNY_FOLDER_STORE (trash_folder), TRUE);
+/* new_folder = modest_mail_operation_xfer_folder (self, folder, */
+/* TNY_FOLDER_STORE (trash_folder), TRUE); */
+/* g_object_unref (G_OBJECT (new_folder)); */
} else {
TnyFolderStore *parent = tny_folder_get_folder_store (folder);
g_object_unref (G_OBJECT (account));
end:
- /* Notify the queue */
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
}
void
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);
/* Check folder rules */
_("FIXME: unable to rename"));
} else {
/* Rename. Camel handles folder subscription/unsubscription */
- tny_folder_set_name (folder, name, &(priv->error));
+ TnyFolderStore *into;
+ TnyFolder *nfol;
+
+ into = tny_folder_get_folder_store (folder);
+ nfol = tny_folder_copy (folder, into, name, TRUE, &(priv->error));
+ if (into)
+ g_object_unref (into);
+ if (nfol)
+ g_object_unref (nfol);
+
CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
+
}
- /* Notify the queue */
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
}
+static void
+transfer_folder_status_cb (GObject *obj,
+ TnyStatus *status,
+ gpointer user_data)
+{
+ XFerMsgAsyncHelper *helper = NULL;
+ ModestMailOperation *self;
+ ModestMailOperationPrivate *priv;
+
+ g_return_if_fail (status != NULL);
+ g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_COPY_FOLDER);
+
+ helper = (XFerMsgAsyncHelper *) user_data;
+ g_return_if_fail (helper != NULL);
+
+ self = helper->mail_op;
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
+
+ if ((status->position == 1) && (status->of_total == 100))
+ return;
+
+ priv->done = status->position;
+ priv->total = status->of_total;
+
+ g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
+}
+
+
+static void
+transfer_folder_cb (TnyFolder *folder, TnyFolderStore *into, gboolean cancelled, TnyFolder *new_folder, GError **err, gpointer user_data)
+{
+ XFerFolderAsyncHelper *helper = NULL;
+ ModestMailOperation *self = NULL;
+ ModestMailOperationPrivate *priv = NULL;
+
+ helper = (XFerFolderAsyncHelper *) user_data;
+ self = helper->mail_op;
+
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+ if (*err) {
+ priv->error = g_error_copy (*err);
+ priv->done = 0;
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ } else if (cancelled) {
+ priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
+ _("Transference of %s was cancelled."),
+ tny_folder_get_name (folder));
+ } else {
+ priv->done = 1;
+ priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
+ }
+
+ /* Free */
+ g_slice_free (XFerFolderAsyncHelper, helper);
+ g_object_unref (folder);
+ g_object_unref (into);
+ if (new_folder != NULL)
+ g_object_unref (new_folder);
+
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
+}
+
TnyFolder *
modest_mail_operation_xfer_folder (ModestMailOperation *self,
TnyFolder *folder,
new_folder = tny_folder_copy (folder,
parent,
tny_folder_get_name (folder),
- delete_original,
+ delete_original,
&(priv->error));
}
- /* Notify the queue */
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
return new_folder;
}
+void
+modest_mail_operation_xfer_folder_async (ModestMailOperation *self,
+ TnyFolder *folder,
+ TnyFolderStore *parent,
+ gboolean delete_original)
+{
+ XFerFolderAsyncHelper *helper = NULL;
+ ModestMailOperationPrivate *priv = NULL;
+ ModestTnyFolderRules rules;
+
+ g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
+ g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
+ g_return_if_fail (TNY_IS_FOLDER (folder));
+
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+ /* Pick references for async calls */
+ g_object_ref (folder);
+ g_object_ref (parent);
+
+ /* The moveable restriction is applied also to copy operation */
+ rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
+ if (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE) {
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
+ _("FIXME: unable to rename"));
+
+ /* Notify the queue */
+ modest_mail_operation_notify_end (self);
+ } else {
+ helper = g_slice_new0 (XFerFolderAsyncHelper);
+ helper->mail_op = self;
+
+ /* Move/Copy folder */
+ tny_folder_copy_async (folder,
+ parent,
+ tny_folder_get_name (folder),
+ delete_original,
+ transfer_folder_cb,
+ transfer_folder_status_cb,
+ helper);
+ }
+}
+
/* ******************************************************************* */
/* ************************** MSG ACTIONS ************************* */
/* ******************************************************************* */
+void modest_mail_operation_get_msg (ModestMailOperation *self,
+ TnyHeader *header,
+ GetMsgAsynUserCallback user_callback,
+ gpointer user_data)
+{
+ GetMsgAsyncHelper *helper = NULL;
+ TnyFolder *folder;
+ ModestMailOperationPrivate *priv;
+
+ g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
+ g_return_if_fail (TNY_IS_HEADER (header));
+
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+ folder = tny_header_get_folder (header);
+
+ priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
+
+ /* Get message from folder */
+ if (folder) {
+ helper = g_slice_new0 (GetMsgAsyncHelper);
+ helper->mail_op = self;
+ helper->user_callback = user_callback;
+ helper->pending_ops = 1;
+ helper->user_data = user_data;
+
+ tny_folder_get_msg_async (folder, header, get_msg_cb, get_msg_status_cb, helper);
+
+ g_object_unref (G_OBJECT (folder));
+ } else {
+ /* Set status failed and set an error */
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
+ _("Error trying to get a message. No folder found for header"));
+ }
+}
+
+static void
+get_msg_cb (TnyFolder *folder,
+ gboolean cancelled,
+ TnyMsg *msg,
+ GError **error,
+ gpointer user_data)
+{
+ GetMsgAsyncHelper *helper = NULL;
+ ModestMailOperation *self = NULL;
+ ModestMailOperationPrivate *priv = NULL;
+
+ helper = (GetMsgAsyncHelper *) user_data;
+ g_return_if_fail (helper != NULL);
+ self = helper->mail_op;
+ g_return_if_fail (MODEST_IS_MAIL_OPERATION(self));
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
+
+ helper->pending_ops--;
+
+ /* Check errors and cancel */
+ if (*error) {
+ priv->error = g_error_copy (*error);
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ goto out;
+ }
+ if (cancelled) {
+ priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
+ _("Error trying to refresh the contents of %s"),
+ tny_folder_get_name (folder));
+ goto out;
+ }
+
+ priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
+
+ /* If user defined callback function was defined, call it */
+ if (helper->user_callback) {
+ helper->user_callback (priv->source, msg, helper->user_data);
+ }
+
+ /* Free */
+ out:
+ if (helper->pending_ops == 0) {
+ g_slice_free (GetMsgAsyncHelper, helper);
+
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
+ }
+}
+
+static void
+get_msg_status_cb (GObject *obj,
+ TnyStatus *status,
+ gpointer user_data)
+{
+ GetMsgAsyncHelper *helper = NULL;
+ ModestMailOperation *self;
+ ModestMailOperationPrivate *priv;
+
+ g_return_if_fail (status != NULL);
+ g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_GET_MSG);
+
+ helper = (GetMsgAsyncHelper *) user_data;
+ g_return_if_fail (helper != NULL);
+
+ /* Temporary FIX: useful when tinymail send us status
+ information *after* calling the function callback */
+ if (!MODEST_IS_MAIL_OPERATION (helper->mail_op))
+ return;
+
+ self = helper->mail_op;
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
+
+ if ((status->position == 1) && (status->of_total == 100))
+ return;
+
+ priv->done = 1;
+ priv->total = 1;
+
+ g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
+}
+
+/****************************************************/
+typedef struct {
+ ModestMailOperation *mail_op;
+ TnyList *headers;
+ GetMsgAsynUserCallback user_callback;
+ gpointer user_data;
+ GDestroyNotify notify;
+} GetFullMsgsInfo;
+
+/*
+ * Used by get_msgs_full_thread to emit the "progress-changed" signal
+ * from the main loop. We call it inside an idle call to achieve that
+ */
+static gboolean
+notify_get_msgs_full_observers (gpointer data)
+{
+ ModestMailOperation *mail_op = MODEST_MAIL_OPERATION (data);
+
+ g_signal_emit (G_OBJECT (mail_op), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
+
+ g_object_unref (mail_op);
+
+ return FALSE;
+}
+
+typedef struct {
+ GetMsgAsynUserCallback user_callback;
+ TnyMsg *msg;
+ gpointer user_data;
+ GObject *source;
+} NotifyGetMsgsInfo;
+
+
+/*
+ * Used by get_msgs_full_thread to call the user_callback for each
+ * message that has been read
+ */
+static gboolean
+notify_get_msgs_full (gpointer data)
+{
+ NotifyGetMsgsInfo *info;
+
+ info = (NotifyGetMsgsInfo *) data;
+
+ /* Call the user callback */
+ info->user_callback (info->source, info->msg, info->user_data);
+
+ g_slice_free (NotifyGetMsgsInfo, info);
+
+ return FALSE;
+}
+
+/*
+ * Used by get_msgs_full_thread to free al the thread resources and to
+ * call the destroy function for the passed user_data
+ */
+static gboolean
+get_msgs_full_destroyer (gpointer data)
+{
+ GetFullMsgsInfo *info;
+
+ info = (GetFullMsgsInfo *) data;
+
+ if (info->notify)
+ info->notify (info->user_data);
+
+ /* free */
+ g_object_unref (info->headers);
+ g_slice_free (GetFullMsgsInfo, info);
+
+ return FALSE;
+}
+
+static gpointer
+get_msgs_full_thread (gpointer thr_user_data)
+{
+ GetFullMsgsInfo *info;
+ ModestMailOperationPrivate *priv = NULL;
+ TnyIterator *iter = NULL;
+
+ info = (GetFullMsgsInfo *) thr_user_data;
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (info->mail_op);
+
+ iter = tny_list_create_iterator (info->headers);
+ while (!tny_iterator_is_done (iter)) {
+ TnyHeader *header;
+ TnyFolder *folder;
+
+ header = TNY_HEADER (tny_iterator_get_current (iter));
+ folder = tny_header_get_folder (header);
+
+ /* Get message from folder */
+ if (folder) {
+ TnyMsg *msg;
+ /* The callback will call it per each header */
+ msg = tny_folder_get_msg (folder, header, &(priv->error));
+
+ if (msg) {
+ priv->done++;
+
+ /* notify progress */
+ g_idle_add_full (G_PRIORITY_HIGH_IDLE,
+ notify_get_msgs_full_observers,
+ g_object_ref (info->mail_op), NULL);
+
+ /* The callback is the responsible for
+ freeing the message */
+ if (info->user_callback) {
+ NotifyGetMsgsInfo *info_notify;
+ info_notify = g_slice_new0 (NotifyGetMsgsInfo);
+ info_notify->user_callback = info->user_callback;
+ info_notify->source = priv->source;
+ info_notify->msg = msg;
+ info_notify->user_data = info->user_data;
+ g_idle_add_full (G_PRIORITY_HIGH_IDLE,
+ notify_get_msgs_full,
+ info_notify, NULL);
+ } else {
+ g_object_unref (msg);
+ }
+ }
+ } else {
+ /* Set status failed and set an error */
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
+ "Error trying to get a message. No folder found for header");
+ }
+ g_object_unref (header);
+ tny_iterator_next (iter);
+ }
+
+ /* Notify about operation end */
+ g_idle_add (notify_update_account_queue, info->mail_op);
+
+ /* Free thread resources. Will be called after all previous idles */
+ g_idle_add_full (G_PRIORITY_DEFAULT_IDLE + 1, get_msgs_full_destroyer, info, NULL);
+
+ return NULL;
+}
+
+void
+modest_mail_operation_get_msgs_full (ModestMailOperation *self,
+ TnyList *header_list,
+ GetMsgAsynUserCallback user_callback,
+ gpointer user_data,
+ GDestroyNotify notify)
+{
+ GThread *thread;
+ ModestMailOperationPrivate *priv = NULL;
+ GetFullMsgsInfo *info = NULL;
+ gboolean size_ok = TRUE;
+ gint max_size;
+ GError *error = NULL;
+ const gint KB = 1024;
+
+ g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
+
+ /* Init mail operation */
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+ priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
+ priv->done = 0;
+ priv->total = tny_list_get_length(header_list);
+
+ /* Get msg size limit */
+ max_size = modest_conf_get_int (modest_runtime_get_conf (),
+ MODEST_CONF_MSG_SIZE_LIMIT,
+ &error);
+ if (error) {
+ g_clear_error (&error);
+ max_size = G_MAXINT;
+ } else {
+ max_size = max_size * KB;
+ }
+
+ /* Check message size limits. If there is only one message
+ always retrieve it */
+ if (tny_list_get_length (header_list) > 1) {
+ TnyIterator *iter;
+
+ iter = tny_list_create_iterator (header_list);
+ while (!tny_iterator_is_done (iter) && size_ok) {
+ TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
+ if (tny_header_get_message_size (header) >= max_size)
+ size_ok = FALSE;
+ g_object_unref (header);
+ tny_iterator_next (iter);
+ }
+ g_object_unref (iter);
+ }
+
+ if (size_ok) {
+ /* Create the info */
+ info = g_slice_new0 (GetFullMsgsInfo);
+ info->mail_op = self;
+ info->user_callback = user_callback;
+ info->user_data = user_data;
+ info->headers = g_object_ref (header_list);
+ info->notify = notify;
+
+ thread = g_thread_create (get_msgs_full_thread, info, FALSE, NULL);
+ } else {
+ /* FIXME: the error msg is different for pop */
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
+ _("emev_ni_ui_imap_msg_sizelimit_error"));
+ /* Remove from queue and free resources */
+ modest_mail_operation_notify_end (self);
+ if (notify)
+ notify (user_data);
+ }
+}
+
+
void
modest_mail_operation_remove_msg (ModestMailOperation *self,
TnyHeader *header,
g_object_unref (header);
/* Move to trash */
- modest_mail_operation_xfer_msgs (self, headers, trash_folder, TRUE);
+ modest_mail_operation_xfer_msgs (self, headers, trash_folder, TRUE, NULL, NULL);
g_object_unref (headers);
/* g_object_unref (trash_folder); */
} else {
/* Free */
g_object_unref (G_OBJECT (folder));
- /* Notify the queue */
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
}
static void
-transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
+transfer_msgs_status_cb (GObject *obj,
+ TnyStatus *status,
+ gpointer user_data)
+{
+ XFerMsgAsyncHelper *helper = NULL;
+ ModestMailOperation *self;
+ ModestMailOperationPrivate *priv;
+
+ g_return_if_fail (status != NULL);
+ g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_XFER_MSGS);
+
+ helper = (XFerMsgAsyncHelper *) user_data;
+ g_return_if_fail (helper != NULL);
+
+ self = helper->mail_op;
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
+
+ if ((status->position == 1) && (status->of_total == 100))
+ return;
+
+ priv->done = status->position;
+ priv->total = status->of_total;
+
+ g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
+}
+
+
+static void
+transfer_msgs_cb (TnyFolder *folder, gboolean cancelled, GError **err, gpointer user_data)
{
XFerMsgAsyncHelper *helper;
ModestMailOperation *self;
helper = (XFerMsgAsyncHelper *) user_data;
self = helper->mail_op;
+
priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
if (*err) {
priv->error = g_error_copy (*err);
priv->done = 0;
- priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ } else if (cancelled) {
+ priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
+ g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+ MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
+ _("Error trying to refresh the contents of %s"),
+ tny_folder_get_name (folder));
} else {
priv->done = 1;
priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
}
+ /* If user defined callback function was defined, call it */
+ if (helper->user_callback) {
+ helper->user_callback (priv->source, helper->user_data);
+ }
+
/* Free */
g_object_unref (helper->headers);
g_object_unref (helper->dest_folder);
+ g_object_unref (helper->mail_op);
+ g_slice_free (XFerMsgAsyncHelper, helper);
g_object_unref (folder);
- g_free (helper);
- /* Notify the queue */
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
}
void
modest_mail_operation_xfer_msgs (ModestMailOperation *self,
TnyList *headers,
TnyFolder *folder,
- gboolean delete_original)
+ gboolean delete_original,
+ XferMsgsAsynUserCallback user_callback,
+ gpointer user_data)
{
ModestMailOperationPrivate *priv;
TnyIterator *iter;
g_return_if_fail (TNY_IS_LIST (headers));
g_return_if_fail (TNY_IS_FOLDER (folder));
- /* Pick references for async calls */
- g_object_ref (folder);
-
priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
priv->total = 1;
priv->done = 0;
priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
/* Create the helper */
- helper = g_malloc0 (sizeof (XFerMsgAsyncHelper));
- helper->mail_op = self;
- helper->dest_folder = folder;
- helper->headers = headers;
+ helper = g_slice_new0 (XFerMsgAsyncHelper);
+ helper->mail_op = g_object_ref(self);
+ helper->dest_folder = g_object_ref(folder);
+ helper->headers = g_object_ref(headers);
+ helper->user_callback = user_callback;
+ helper->user_data = user_data;
/* Get source folder */
iter = tny_list_create_iterator (headers);
folder,
delete_original,
transfer_msgs_cb,
- NULL,
+ transfer_msgs_status_cb,
helper);
}
+
static void
on_refresh_folder (TnyFolder *folder,
gboolean cancelled,
/* Free */
g_object_unref (folder);
- /* Notify the queue */
- modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
+ /* Notify about operation end */
+ modest_mail_operation_notify_end (self);
}
static void
on_refresh_folder_status_update (GObject *obj,
- TnyStatus *status,
+ TnyStatus *status,
gpointer user_data)
{
ModestMailOperation *self;
g_return_if_fail (status != NULL);
g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
+ /* Temporary FIX: useful when tinymail send us status
+ information *after* calling the function callback */
+ if (!MODEST_IS_MAIL_OPERATION (user_data))
+ return;
+
self = MODEST_MAIL_OPERATION (user_data);
priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
priv->done = status->position;
priv->total = status->of_total;
- if (priv->done == 1 && priv->total == 100)
- return;
-
g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
}
tny_folder_refresh_async (folder,
on_refresh_folder,
on_refresh_folder_status_update,
-/* NULL, */
self);
}
-void
-_modest_mail_operation_notify_end (ModestMailOperation *self)
+/**
+ *
+ * It's used by the mail operation queue to notify the observers
+ * attached to that signal that the operation finished. We need to use
+ * that because tinymail does not give us the progress of a given
+ * operation when it finishes (it directly calls the operation
+ * callback).
+ */
+static void
+modest_mail_operation_notify_end (ModestMailOperation *self)
{
+ /* Notify the observers about the mail opertation end */
g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
+
+ /* Notify the queue */
+ modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
}