+ 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;