+static void
+run_queue_stop (ModestTnySendQueue *queue,
+ ModestMailOperation *self)
+{
+ ModestMailOperationPrivate *priv;
+
+ g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
+ g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE (queue));
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+ priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
+
+ modest_mail_operation_notify_end (self);
+ g_signal_handlers_disconnect_by_func (queue, run_queue_stop, self);
+ g_object_unref (self);
+}
+
+void
+modest_mail_operation_run_queue (ModestMailOperation *self,
+ ModestTnySendQueue *queue)
+{
+ ModestMailOperationPrivate *priv;
+
+ g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
+ g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE (queue));
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+ priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
+ priv->account = TNY_ACCOUNT (tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE (queue)));
+ priv->op_type = MODEST_MAIL_OPERATION_TYPE_RUN_QUEUE;
+
+ modest_mail_operation_notify_start (self);
+ g_object_ref (self);
+ g_signal_connect ((gpointer) queue, "queue-stop", G_CALLBACK (run_queue_stop), (gpointer) self);
+}
+
+static void
+shutdown_callback (ModestTnyAccountStore *account_store, gpointer userdata)
+{
+ ModestMailOperation *self = (ModestMailOperation *) userdata;
+ ModestMailOperationPrivate *priv;
+
+ g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
+ g_return_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (account_store));
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+ priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
+
+ modest_mail_operation_notify_end (self);
+ g_object_unref (self);
+}
+
+void
+modest_mail_operation_shutdown (ModestMailOperation *self, ModestTnyAccountStore *account_store)
+{
+ ModestMailOperationPrivate *priv;
+
+ g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
+ g_return_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (account_store));
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+ modest_mail_operation_queue_set_running_shutdown (modest_runtime_get_mail_operation_queue ());
+
+ priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
+ priv->account = NULL;
+ priv->op_type = MODEST_MAIL_OPERATION_TYPE_SHUTDOWN;
+
+ modest_mail_operation_notify_start (self);
+ g_object_ref (self);
+ modest_tny_account_store_shutdown (account_store, shutdown_callback, self);
+}
+
+static void
+sync_folder_finish_callback (TnyFolder *self,
+ gboolean cancelled,
+ GError *err,
+ gpointer user_data)
+
+{
+ ModestMailOperation *mail_op;
+ ModestMailOperationPrivate *priv;
+
+ mail_op = (ModestMailOperation *) user_data;
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
+
+ /* If canceled by the user, ignore the error given by Tinymail */
+ if (cancelled) {
+ priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
+ } else if (err) {
+ /* If the operation was a sync then the status is
+ failed, but if it's part of another operation then
+ just set it as finished with errors */
+ if (priv->op_type == MODEST_MAIL_OPERATION_TYPE_SYNC_FOLDER)
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+ else
+ priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
+ priv->error = g_error_copy ((const GError *) err);
+ priv->error->domain = MODEST_MAIL_OPERATION_ERROR;
+ } else {
+ priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
+ }
+
+ modest_mail_operation_notify_end (mail_op);
+ g_object_unref (mail_op);
+}
+
+void
+modest_mail_operation_sync_folder (ModestMailOperation *self,
+ TnyFolder *folder, gboolean expunge)
+{
+ ModestMailOperationPrivate *priv;
+
+ g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
+ g_return_if_fail (TNY_IS_FOLDER (folder));
+ priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+ priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
+ priv->account = modest_tny_folder_get_account (folder);
+ priv->op_type = MODEST_MAIL_OPERATION_TYPE_SYNC_FOLDER;
+
+ modest_mail_operation_notify_start (self);
+ g_object_ref (self);
+ tny_folder_sync_async (folder, expunge,
+ (TnyFolderCallback) sync_folder_finish_callback,
+ NULL, self);
+}