[evolution-kolab/EDS_IMAPX_nobuild] updated IMAPX files as of EVOLUTION_DATA_SERVER_3_3_3



commit ecd91a2a037f229d936720a35bc00aabdbe394a9
Author: Christian Hilberg <hilberg kernelconcepts de>
Date:   Mon Dec 19 19:41:39 2011 +0100

    updated IMAPX files as of EVOLUTION_DATA_SERVER_3_3_3
    
    * updated the local IMAPX code from upstream
    * now at version EVOLUTION_DATA_SERVER_3_3_3

 src/camel/providers/imapx/camel-imapx-server.c |  562 ++++++++++++++----------
 src/camel/providers/imapx/camel-imapx-server.h |    4 +-
 2 files changed, 326 insertions(+), 240 deletions(-)
---
diff --git a/src/camel/providers/imapx/camel-imapx-server.c b/src/camel/providers/imapx/camel-imapx-server.c
index 751b205..9ca9623 100644
--- a/src/camel/providers/imapx/camel-imapx-server.c
+++ b/src/camel/providers/imapx/camel-imapx-server.c
@@ -130,6 +130,8 @@ typedef void (*CamelIMAPXCommandFunc)(struct _CamelIMAPXServer *engine, struct _
 struct _CamelIMAPXCommand {
 	struct _CamelIMAPXCommand *next, *prev;
 
+	volatile gint ref_count;
+
 	CamelIMAPXServer *is;
 	gint pri;
 
@@ -160,10 +162,7 @@ struct _CamelIMAPXCommand {
 	struct _CamelIMAPXJob *job;
 };
 
-CamelIMAPXCommand *camel_imapx_command_new (CamelIMAPXServer *is, const gchar *name, CamelFolder *select, GCancellable *cancellable, const gchar *fmt, ...);
-void camel_imapx_command_add (CamelIMAPXCommand *ic, const gchar *fmt, ...);
-void camel_imapx_command_free (CamelIMAPXCommand *ic);
-void camel_imapx_command_close (CamelIMAPXCommand *ic);
+static void imapx_command_add (CamelIMAPXCommand *ic, const gchar *fmt, ...);
 static gboolean imapx_is_command_queue_empty (CamelIMAPXServer *is);
 
 /* states for the connection? */
@@ -226,8 +225,13 @@ struct _imapx_flag_change {
 };
 
 typedef struct _CamelIMAPXJob CamelIMAPXJob;
+
 struct _CamelIMAPXJob {
-	CamelMsg msg;
+	volatile gint ref_count;
+
+	GCond *done_cond;
+	GMutex *done_mutex;
+	gboolean done_flag;
 
 	GCancellable *cancellable;
 	GError *error;
@@ -388,7 +392,7 @@ imapx_uidset_done (struct _uidset_state *ss,
 	gint ret = 0;
 
 	if (ss->last != 0 && ss->last != ss->start) {
-		camel_imapx_command_add(ic, ":%d", ss->last);
+		imapx_command_add (ic, ":%d", ss->last);
 	}
 
 	ret = ss->last != 0;
@@ -418,18 +422,18 @@ imapx_uidset_add (struct _uidset_state *ss,
 
 	if (ss->last == 0) {
 		e(ic->is->tagprefix, " start\n");
-		camel_imapx_command_add(ic, "%d", uidn);
+		imapx_command_add (ic, "%d", uidn);
 		ss->entries++;
 		ss->start = uidn;
 	} else {
 		if (ss->last != uidn - 1) {
 			if (ss->last == ss->start) {
 				e(ic->is->tagprefix, " ,next\n");
-				camel_imapx_command_add(ic, ",%d", uidn);
+				imapx_command_add (ic, ",%d", uidn);
 				ss->entries++;
 			} else {
 				e(ic->is->tagprefix, " :range\n");
-				camel_imapx_command_add(ic, ":%d,%d", ss->last, uidn);
+				imapx_command_add (ic, ":%d,%d", ss->last, uidn);
 				ss->entries+=2;
 			}
 			ss->start = uidn;
@@ -753,12 +757,12 @@ imapx_command_addv (CamelIMAPXCommand *ic,
 	camel_stream_write ((CamelStream *) ic->mem, ps, p - ps - 1, ic->cancellable, NULL);
 }
 
-CamelIMAPXCommand *
-camel_imapx_command_new (CamelIMAPXServer *is,
-                         const gchar *name,
-                         CamelFolder *select,
-                         GCancellable *cancellable,
-                         const gchar *fmt, ...)
+static CamelIMAPXCommand *
+imapx_command_new (CamelIMAPXServer *is,
+                   const gchar *name,
+                   CamelFolder *select,
+                   GCancellable *cancellable,
+                   const gchar *fmt, ...)
 {
 	CamelIMAPXCommand *ic;
 	static gint tag = 0;
@@ -767,7 +771,8 @@ camel_imapx_command_new (CamelIMAPXServer *is,
 	if (cancellable != NULL)
 		g_object_ref (cancellable);
 
-	ic = g_malloc0 (sizeof (*ic));
+	ic = g_slice_new0 (CamelIMAPXCommand);
+	ic->ref_count = 1;
 	ic->tag = tag++;
 	ic->name = name;
 	ic->mem = (CamelStreamMem *) camel_stream_mem_new ();
@@ -785,10 +790,10 @@ camel_imapx_command_new (CamelIMAPXServer *is,
 	return ic;
 }
 
-void
-camel_imapx_command_add (CamelIMAPXCommand *ic,
-                         const gchar *fmt,
-                         ...)
+static void
+imapx_command_add (CamelIMAPXCommand *ic,
+                   const gchar *fmt,
+                   ...)
 {
 	va_list ap;
 
@@ -801,45 +806,59 @@ camel_imapx_command_add (CamelIMAPXCommand *ic,
 	}
 }
 
-void
-camel_imapx_command_free (CamelIMAPXCommand *ic)
+static CamelIMAPXCommand *
+imapx_command_ref (CamelIMAPXCommand *ic)
 {
-	CamelIMAPXCommandPart *cp;
+	g_return_val_if_fail (ic != NULL, NULL);
+	g_return_val_if_fail (ic->ref_count > 0, NULL);
 
-	if (ic == NULL)
-		return;
+	g_atomic_int_inc (&ic->ref_count);
 
-	if (ic->mem)
-		g_object_unref (ic->mem);
-	imapx_free_status (ic->status);
-
-	while ((cp = ((CamelIMAPXCommandPart *) camel_dlist_remhead (&ic->parts)))) {
-		g_free (cp->data);
-		if (cp->ob) {
-			switch (cp->type & CAMEL_IMAPX_COMMAND_MASK) {
-			case CAMEL_IMAPX_COMMAND_FILE:
-			case CAMEL_IMAPX_COMMAND_STRING:
-				g_free (cp->ob);
-				break;
-			default:
-				g_object_unref (cp->ob);
+	return ic;
+}
+
+static void
+imapx_command_unref (CamelIMAPXCommand *ic)
+{
+	g_return_if_fail (ic != NULL);
+	g_return_if_fail (ic->ref_count > 0);
+
+	if (g_atomic_int_dec_and_test (&ic->ref_count)) {
+		CamelIMAPXCommandPart *cp;
+
+		if (ic->mem != NULL)
+			g_object_unref (ic->mem);
+
+		imapx_free_status (ic->status);
+
+		while ((cp = ((CamelIMAPXCommandPart *) camel_dlist_remhead (&ic->parts)))) {
+			g_free (cp->data);
+			if (cp->ob) {
+				switch (cp->type & CAMEL_IMAPX_COMMAND_MASK) {
+				case CAMEL_IMAPX_COMMAND_FILE:
+				case CAMEL_IMAPX_COMMAND_STRING:
+					g_free (cp->ob);
+					break;
+				default:
+					g_object_unref (cp->ob);
+				}
 			}
+			g_free (cp);
 		}
-		g_free (cp);
-	}
 
-	if (ic->cancellable != NULL)
-		g_object_unref (ic->cancellable);
+		if (ic->cancellable != NULL)
+			g_object_unref (ic->cancellable);
 
-	/* Do NOT try to free the GError.  If set it should have been
-	 * propagated to the CamelIMAPXJob, so it's either NULL or the
-	 * CamelIMAPXJob owns it now. */
+		/* Do NOT try to free the GError.  If set it should have been
+		 * propagated to the CamelIMAPXJob, so it's either NULL or the
+		 * CamelIMAPXJob owns it now. */
 
-	g_free (ic);
+		g_slice_free (CamelIMAPXCommand, ic);
+	}
 }
 
-void
-camel_imapx_command_close (CamelIMAPXCommand *ic)
+static void
+imapx_command_close (CamelIMAPXCommand *ic)
 {
 	if (ic->mem) {
 		GByteArray *byte_array;
@@ -867,7 +886,7 @@ imapx_command_start (CamelIMAPXServer *imap,
 	CamelIMAPXCommandPart *cp;
 	gint retval;
 
-	camel_imapx_command_close (ic);
+	imapx_command_close (ic);
 	cp = (CamelIMAPXCommandPart *) ic->parts.head;
 	g_assert (cp->next);
 	ic->current = cp;
@@ -1103,7 +1122,7 @@ imapx_command_queue (CamelIMAPXServer *is,
 	/* We enqueue in priority order, new messages have
 	 * higher priority than older messages with the same priority */
 
-	camel_imapx_command_close (ic);
+	imapx_command_close (ic);
 
 	c(is->tagprefix, "enqueue job '%.*s'\n", ((CamelIMAPXCommandPart *)ic->parts.head)->data_size, ((CamelIMAPXCommandPart *)ic->parts.head)->data);
 
@@ -1226,14 +1245,16 @@ imapx_is_job_in_queue (CamelIMAPXServer *is,
                        guint32 type,
                        const gchar *uid)
 {
-	CamelDListNode *node;
+	GList *head, *link;
 	CamelIMAPXJob *job = NULL;
 	gboolean found = FALSE;
 
 	QUEUE_LOCK (is);
 
-	for (node = is->jobs.head; node->next; node = job->msg.ln.next) {
-		job = (CamelIMAPXJob *) node;
+	head = g_queue_peek_head_link (&is->jobs);
+
+	for (link = head; link != NULL; link = g_list_next (link)) {
+		job = (CamelIMAPXJob *) link->data;
 
 		if (!job || !(job->type & type))
 			continue;
@@ -2116,7 +2137,7 @@ imapx_command_run (CamelIMAPXServer *is,
                    CamelIMAPXCommand *ic)
 /* throws IO,PARSE exception */
 {
-	camel_imapx_command_close (ic);
+	imapx_command_close (ic);
 
 	QUEUE_LOCK (is);
 	imapx_command_start (is, ic);
@@ -2143,6 +2164,23 @@ imapx_command_complete (CamelIMAPXServer *is,
 	ic->run_sync_done = TRUE;
 	g_cond_broadcast (ic->run_sync_cond);
 	g_mutex_unlock (ic->run_sync_mutex);
+
+	imapx_command_unref (ic);
+}
+
+static void
+imapx_command_cancelled (GCancellable *cancellable,
+                         CamelIMAPXCommand *ic)
+{
+	/* Unblock imapx_command_run_sync() immediately.
+	 *
+	 * If imapx_command_complete() is called sometime later,
+	 * the GCond will broadcast but no one will be listening. */
+
+	g_mutex_lock (ic->run_sync_mutex);
+	ic->run_sync_done = TRUE;
+	g_cond_broadcast (ic->run_sync_cond);
+	g_mutex_unlock (ic->run_sync_mutex);
 }
 
 /* The caller should free the command as well */
@@ -2150,12 +2188,32 @@ static void
 imapx_command_run_sync (CamelIMAPXServer *is,
                         CamelIMAPXCommand *ic)
 {
+	guint cancel_id = 0;
+
 	ic->run_sync_done = FALSE;
 	ic->run_sync_cond = g_cond_new ();
 	ic->run_sync_mutex = g_mutex_new ();
 
-	if (!ic->complete)
-		ic->complete = imapx_command_complete;
+	/* FIXME The only caller of this function currently does not set
+	 *       a "complete" callback function, so we can get away with
+	 *       referencing the command here and dropping the reference
+	 *       in imapx_command_complete().  The queueing/dequeueing
+	 *       of these things is too complex for my little mind, so
+	 *       we may have to revisit the reference counting if this
+	 *       function gets another caller. */
+
+	g_warn_if_fail (ic->complete == NULL);
+	ic->complete = imapx_command_complete;
+
+	if (G_IS_CANCELLABLE (ic->cancellable))
+		cancel_id = g_cancellable_connect (
+			ic->cancellable,
+			G_CALLBACK (imapx_command_cancelled),
+			imapx_command_ref (ic),
+			(GDestroyNotify) imapx_command_unref);
+
+	/* Unref'ed in imapx_command_complete(). */
+	imapx_command_ref (ic);
 
 	imapx_command_queue (is, ic);
 
@@ -2164,6 +2222,12 @@ imapx_command_run_sync (CamelIMAPXServer *is,
 		g_cond_wait (ic->run_sync_cond, ic->run_sync_mutex);
 	g_mutex_unlock (ic->run_sync_mutex);
 
+	if (cancel_id > 0)
+		g_cancellable_disconnect (ic->cancellable, cancel_id);
+
+	/* XXX Might this overwrite an existing error? */
+	g_cancellable_set_error_if_cancelled (ic->cancellable, &ic->error);
+
 	g_cond_free (ic->run_sync_cond);
 	g_mutex_free (ic->run_sync_mutex);
 	ic->run_sync_done = FALSE;
@@ -2182,27 +2246,47 @@ imapx_job_new (GCancellable *cancellable)
 	if (cancellable != NULL)
 		g_object_ref (cancellable);
 
-	job = g_malloc0 (sizeof (CamelIMAPXJob));
+	job = g_slice_new0 (CamelIMAPXJob);
+	job->ref_count = 1;
+	job->done_cond = g_cond_new ();
+	job->done_mutex = g_mutex_new ();
 	job->cancellable = cancellable;
 
 	return job;
 }
 
+static CamelIMAPXJob *
+imapx_job_ref (CamelIMAPXJob *job)
+{
+	g_return_val_if_fail (job != NULL, NULL);
+	g_return_val_if_fail (job->ref_count > 0, NULL);
+
+	g_atomic_int_inc (&job->ref_count);
+
+	return job;
+}
+
 static void
-imapx_job_free (CamelIMAPXJob *job)
+imapx_job_unref (CamelIMAPXJob *job)
 {
-	if (!job)
-		return;
+	g_return_if_fail (job != NULL);
+	g_return_if_fail (job->ref_count > 0);
 
-	g_clear_error (&job->error);
+	if (g_atomic_int_dec_and_test (&job->ref_count)) {
 
-	if (job->cancellable) {
-		if (job->with_operation_msg)
-			camel_operation_pop_message (job->cancellable);
-		g_object_unref (job->cancellable);
-	}
+		g_cond_free (job->done_cond);
+		g_mutex_free (job->done_mutex);
 
-	g_free (job);
+		g_clear_error (&job->error);
+
+		if (job->cancellable) {
+			if (job->with_operation_msg)
+				camel_operation_pop_message (job->cancellable);
+			g_object_unref (job->cancellable);
+		}
+
+		g_slice_free (CamelIMAPXJob, job);
+	}
 }
 
 static gboolean
@@ -2215,14 +2299,32 @@ static void
 imapx_job_done (CamelIMAPXServer *is,
                 CamelIMAPXJob *job)
 {
+	if (!job->noreply) {
+		g_mutex_lock (job->done_mutex);
+		job->done_flag = TRUE;
+		g_cond_broadcast (job->done_cond);
+		g_mutex_unlock (job->done_mutex);
+	}
+
 	QUEUE_LOCK (is);
-	camel_dlist_remove ((CamelDListNode *) job);
+	if (g_queue_remove (&is->jobs, job))
+		imapx_job_unref (job);
 	QUEUE_UNLOCK (is);
+}
 
-	if (job->noreply)
-		imapx_job_free (job);
-	else
-		camel_msgport_reply ((CamelMsg *) job);
+static void
+imapx_job_cancelled (GCancellable *cancellable,
+                     CamelIMAPXJob *job)
+{
+	/* Unblock imapx_run_job() immediately.
+	 *
+	 * If imapx_job_done() is called sometime later, the
+	 * GCond will broadcast but no one will be listening. */
+
+	g_mutex_lock (job->done_mutex);
+	job->done_flag = TRUE;
+	g_cond_broadcast (job->done_cond);
+	g_mutex_unlock (job->done_mutex);
 }
 
 static gboolean
@@ -2232,7 +2334,7 @@ imapx_register_job (CamelIMAPXServer *is,
 {
 	if (is->state >= IMAPX_INITIALISED) {
 		QUEUE_LOCK (is);
-		camel_dlist_addhead (&is->jobs, (CamelDListNode *) job);
+		g_queue_push_head (&is->jobs, imapx_job_ref (job));
 		QUEUE_UNLOCK (is);
 
 	} else {
@@ -2252,24 +2354,34 @@ imapx_run_job (CamelIMAPXServer *is,
                CamelIMAPXJob *job,
                GError **error)
 {
-	CamelMsgPort *reply = NULL;
+	gulong cancel_id;
 
-	if (!job->noreply) {
-		reply = camel_msgport_new ();
-		job->msg.reply_port = reply;
-	}
+	g_warn_if_fail (job->done_flag == FALSE);
+
+	if (g_cancellable_set_error_if_cancelled (is->cancellable, error))
+		return FALSE;
+
+	if (G_IS_CANCELLABLE (job->cancellable))
+		cancel_id = g_cancellable_connect (
+			job->cancellable,
+			G_CALLBACK (imapx_job_cancelled),
+			imapx_job_ref (job),
+			(GDestroyNotify) imapx_job_unref);
 
-	/* Any exceptions to the start should be reported async through our reply msgport */
 	job->start (is, job);
 
 	if (!job->noreply) {
-		CamelMsg *completed;
+		g_mutex_lock (job->done_mutex);
+		while (!job->done_flag)
+			g_cond_wait (job->done_cond, job->done_mutex);
+		g_mutex_unlock (job->done_mutex);
+	}
 
-		completed = camel_msgport_pop (reply);
-		camel_msgport_destroy (reply);
+	if (cancel_id > 0)
+		g_cancellable_disconnect (job->cancellable, cancel_id);
 
-		g_assert (completed == (CamelMsg *) job);
-	}
+	if (g_cancellable_set_error_if_cancelled (job->cancellable, error))
+		return FALSE;
 
 	if (job->error != NULL) {
 		g_propagate_error (error, job->error);
@@ -2351,7 +2463,7 @@ imapx_command_idle_done (CamelIMAPXServer *is,
 	IDLE_UNLOCK (idle);
 
 	imapx_job_done (is, ic->job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -2361,14 +2473,14 @@ imapx_job_idle_start (CamelIMAPXServer *is,
 	CamelIMAPXCommand *ic;
 	CamelIMAPXCommandPart *cp;
 
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "IDLE", job->folder,
 		job->cancellable, "IDLE");
 	ic->job = job;
 	ic->pri = job->pri;
 	ic->complete = imapx_command_idle_done;
 
-	camel_imapx_command_close (ic);
+	imapx_command_close (ic);
 	cp = (CamelIMAPXCommandPart *) ic->parts.head;
 	cp->type |= CAMEL_IMAPX_COMMAND_CONTINUATION;
 
@@ -2380,7 +2492,7 @@ imapx_job_idle_start (CamelIMAPXServer *is,
 		imapx_command_start (is, ic);
 	} else {
 		imapx_job_done (is, ic->job);
-		camel_imapx_command_free (ic);
+		imapx_command_unref (ic);
 	}
 	IDLE_UNLOCK (is->idle);
 	QUEUE_UNLOCK (is);
@@ -2402,7 +2514,7 @@ camel_imapx_server_idle (CamelIMAPXServer *is,
 
 	success = imapx_submit_job (is, job, error);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return success;
 }
@@ -2429,7 +2541,7 @@ imapx_server_fetch_new_messages (CamelIMAPXServer *is,
 	success = imapx_submit_job (is, job, error);
 
 	if (!async)
-		imapx_job_free (job);
+		imapx_job_unref (job);
 
 	return success;
 }
@@ -2729,7 +2841,7 @@ imapx_command_select_done (CamelIMAPXServer *is,
 	}
 
 	is->select_pending = NULL;
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 
 	g_signal_emit (is, signals[SELECT_CHANGED], 0, selected_folder);
 }
@@ -2788,7 +2900,8 @@ imapx_select (CamelIMAPXServer *is,
 	/* Hrm, what about reconnecting? */
 	is->state = IMAPX_INITIALISED;
 
-	ic = camel_imapx_command_new(is, "SELECT", NULL, cancellable, "SELECT %f", folder);
+	ic = imapx_command_new (
+		is, "SELECT", NULL, cancellable, "SELECT %f", folder);
 
 	if (is->use_qresync) {
 		CamelIMAPXSummary *isum = (CamelIMAPXSummary *) folder->summary;
@@ -2805,12 +2918,13 @@ imapx_select (CamelIMAPXServer *is,
 			  " %" G_GUINT64_FORMAT "\n",
 			  ifolder->uidvalidity_on_server, isum->modseq);
 
-			camel_imapx_command_add(ic, " (QRESYNC (%"
-						G_GUINT64_FORMAT " %"
-						G_GUINT64_FORMAT " %s:%s",
-						ifolder->uidvalidity_on_server,
-						isum->modseq,
-						firstuid, lastuid);
+			imapx_command_add (
+				ic, " (QRESYNC (%"
+				G_GUINT64_FORMAT " %"
+				G_GUINT64_FORMAT " %s:%s",
+				ifolder->uidvalidity_on_server,
+				isum->modseq,
+				firstuid, lastuid);
 
 			g_free (firstuid);
 			g_free (lastuid);
@@ -2852,14 +2966,14 @@ imapx_select (CamelIMAPXServer *is,
 				g_string_prepend(seqs, " (");
 
 				c(is->tagprefix, "adding QRESYNC seq/uidset %s%s\n", seqs->str, uids->str);
-				camel_imapx_command_add (ic, seqs->str);
-				camel_imapx_command_add (ic, uids->str);
+				imapx_command_add (ic, seqs->str);
+				imapx_command_add (ic, uids->str);
 
 				g_string_free (seqs, TRUE);
 				g_string_free (uids, TRUE);
 
 			}
-			camel_imapx_command_add(ic, "))");
+			imapx_command_add (ic, "))");
 		}
 	}
 
@@ -3090,7 +3204,7 @@ imapx_connect_to_server (CamelIMAPXServer *is,
 	}
 
 	if (!is->cinfo) {
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "CAPABILITY", NULL,
 			cancellable, "CAPABILITY");
 		imapx_command_run (is, ic);
@@ -3106,10 +3220,10 @@ imapx_connect_to_server (CamelIMAPXServer *is,
 				ic->error = NULL;
 			}
 
-			camel_imapx_command_free (ic);
+			imapx_command_unref (ic);
 			return FALSE;
 		}
-		camel_imapx_command_free (ic);
+		imapx_command_unref (ic);
 	}
 
 	if (method == CAMEL_NETWORK_SECURITY_METHOD_STARTTLS_ON_STANDARD_PORT) {
@@ -3123,7 +3237,7 @@ imapx_connect_to_server (CamelIMAPXServer *is,
 			goto exit;
 		}
 
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "STARTTLS", NULL,
 			cancellable, "STARTTLS");
 		imapx_command_run (is, ic);
@@ -3139,7 +3253,7 @@ imapx_connect_to_server (CamelIMAPXServer *is,
 				ic->error = NULL;
 			}
 
-			camel_imapx_command_free (ic);
+			imapx_command_unref (ic);
 			goto exit;
 		}
 
@@ -3152,9 +3266,11 @@ imapx_connect_to_server (CamelIMAPXServer *is,
 			c(is->tagprefix, "got capability flags %08x\n", is->cinfo->capa);
 		}
 
-		camel_imapx_command_free (ic);
+		imapx_command_unref (ic);
 
-		if (camel_tcp_stream_ssl_enable_ssl (CAMEL_TCP_STREAM_SSL (tcp_stream), &local_error) == -1) {
+		if (camel_tcp_stream_ssl_enable_ssl (
+			CAMEL_TCP_STREAM_SSL (tcp_stream),
+			cancellable, &local_error) == -1) {
 			g_prefix_error (
 				&local_error,
 				_("Failed to connect to IMAP server %s in secure mode: "),
@@ -3163,17 +3279,17 @@ imapx_connect_to_server (CamelIMAPXServer *is,
 		}
 		/* Get new capabilities if they weren't already given */
 		if (!is->cinfo) {
-			ic = camel_imapx_command_new (
+			ic = imapx_command_new (
 				is, "CAPABILITY", NULL,
 				cancellable, "CAPABILITY");
 			if (!imapx_command_run (is, ic)) {
 				g_propagate_error (&local_error, ic->error);
 				ic->error = NULL;
-				camel_imapx_command_free (ic);
+				imapx_command_unref (ic);
 				goto exit;
 			}
 
-			camel_imapx_command_free (ic);
+			imapx_command_unref (ic);
 		}
 	}
 
@@ -3245,8 +3361,8 @@ camel_imapx_server_authenticate (CamelIMAPXServer *is,
 	}
 
 	if (sasl != NULL) {
-		ic = camel_imapx_command_new (
-			is, "AUTHENTICATE", NULL, cancellable,
+		ic = imapx_command_new (
+			is, "AUTHENTICATE", NULL,cancellable,
 			"AUTHENTICATE %A", sasl);
 	} else {
 		const gchar *password;
@@ -3269,7 +3385,7 @@ camel_imapx_server_authenticate (CamelIMAPXServer *is,
 			return CAMEL_AUTHENTICATION_ERROR;
 		}
 
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "LOGIN", NULL, cancellable,
 			"LOGIN %s %s", user, password);
 	}
@@ -3301,7 +3417,7 @@ camel_imapx_server_authenticate (CamelIMAPXServer *is,
 		}
 	}
 
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 
 	if (sasl != NULL)
 		g_object_unref (sasl);
@@ -3347,17 +3463,17 @@ imapx_reconnect (CamelIMAPXServer *is,
 
 	/* After login we re-capa unless the server already told us */
 	if (!is->cinfo) {
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "CAPABILITY", NULL,
 			cancellable, "CAPABILITY");
 		if (!imapx_command_run (is, ic)) {
 			g_propagate_error (error, ic->error);
 			ic->error = NULL;
-			camel_imapx_command_free (ic);
+			imapx_command_unref (ic);
 			goto exception;
 		}
 
-		camel_imapx_command_free (ic);
+		imapx_command_unref (ic);
 	}
 
 	is->state = IMAPX_AUTHENTICATED;
@@ -3370,31 +3486,31 @@ imapx_reconnect (CamelIMAPXServer *is,
 
 	/* Fetch namespaces */
 	if (is->cinfo->capa & IMAPX_CAPABILITY_NAMESPACE) {
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "NAMESPACE", NULL,
 			cancellable, "NAMESPACE");
 		if (!imapx_command_run (is, ic)) {
 			g_propagate_error (error, ic->error);
 			ic->error = NULL;
-			camel_imapx_command_free (ic);
+			imapx_command_unref (ic);
 			goto exception;
 		}
 
-		camel_imapx_command_free (ic);
+		imapx_command_unref (ic);
 	}
 
 	if (use_qresync && is->cinfo->capa & IMAPX_CAPABILITY_QRESYNC) {
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "ENABLE", NULL, cancellable,
 			"ENABLE CONDSTORE QRESYNC");
 		if (!imapx_command_run (is, ic)) {
 			g_propagate_error (error, ic->error);
 			ic->error = NULL;
-			camel_imapx_command_free (ic);
+			imapx_command_unref (ic);
 			goto exception;
 		}
 
-		camel_imapx_command_free (ic);
+		imapx_command_unref (ic);
 
 		is->use_qresync = TRUE;
 	} else
@@ -3459,16 +3575,16 @@ imapx_command_fetch_message_done (CamelIMAPXServer *is,
 		 * time) until the data actually stop coming. */
 		if (job->u.get_message.fetch_offset < job->u.get_message.size ||
 		    job->u.get_message.fetch_offset == really_fetched) {
-			camel_imapx_command_free (ic);
+			imapx_command_unref (ic);
 			camel_operation_progress (
 				job->cancellable,
 				(job->u.get_message.fetch_offset *100) / job->u.get_message.size);
 
-			ic = camel_imapx_command_new (
+			ic = imapx_command_new (
 				is, "FETCH", job->folder, job->cancellable,
 				"UID FETCH %t (BODY.PEEK[]", job->u.get_message.uid);
-			camel_imapx_command_add(ic, "<%u.%u>", job->u.get_message.fetch_offset, MULTI_SIZE);
-			camel_imapx_command_add(ic, ")");
+			imapx_command_add (ic, "<%u.%u>", job->u.get_message.fetch_offset, MULTI_SIZE);
+			imapx_command_add (ic, ")");
 			ic->complete = imapx_command_fetch_message_done;
 			ic->job = job;
 			ic->pri = job->pri - 1;
@@ -3523,7 +3639,7 @@ imapx_command_fetch_message_done (CamelIMAPXServer *is,
 		imapx_job_done (is, job);
 	}
 
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -3535,11 +3651,11 @@ imapx_job_get_message_start (CamelIMAPXServer *is,
 
 	if (job->u.get_message.use_multi_fetch) {
 		for (i = 0; i < 3 && job->u.get_message.fetch_offset < job->u.get_message.size; i++) {
-			ic = camel_imapx_command_new (
+			ic = imapx_command_new (
 				is, "FETCH", job->folder, job->cancellable,
 				"UID FETCH %t (BODY.PEEK[]", job->u.get_message.uid);
-			camel_imapx_command_add(ic, "<%u.%u>", job->u.get_message.fetch_offset, MULTI_SIZE);
-			camel_imapx_command_add(ic, ")");
+			imapx_command_add (ic, "<%u.%u>", job->u.get_message.fetch_offset, MULTI_SIZE);
+			imapx_command_add (ic, ")");
 			ic->complete = imapx_command_fetch_message_done;
 			ic->job = job;
 			ic->pri = job->pri;
@@ -3548,7 +3664,7 @@ imapx_job_get_message_start (CamelIMAPXServer *is,
 			imapx_command_queue (is, ic);
 		}
 	} else {
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "FETCH", job->folder, job->cancellable,
 			"UID FETCH %t (BODY.PEEK[])", job->u.get_message.uid);
 		ic->complete = imapx_command_fetch_message_done;
@@ -3570,7 +3686,7 @@ imapx_command_copy_messages_step_start (CamelIMAPXServer *is,
 	GPtrArray *uids = job->u.copy_messages.uids;
 	gint i = index;
 
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "COPY", job->folder,
 		job->cancellable, "UID COPY ");
 	ic->complete = imapx_command_copy_messages_step_done;
@@ -3584,7 +3700,7 @@ imapx_command_copy_messages_step_start (CamelIMAPXServer *is,
 
 		res = imapx_uidset_add (&job->u.copy_messages.uidset, ic, uid);
 		if (res == 1) {
-			camel_imapx_command_add (ic, " %f", job->u.copy_messages.dest);
+			imapx_command_add (ic, " %f", job->u.copy_messages.dest);
 			job->u.copy_messages.index = i;
 			imapx_command_queue (is, ic);
 			return;
@@ -3593,7 +3709,7 @@ imapx_command_copy_messages_step_start (CamelIMAPXServer *is,
 
 	job->u.copy_messages.index = i;
 	if (imapx_uidset_done (&job->u.copy_messages.uidset, ic)) {
-		camel_imapx_command_add (ic, " %f", job->u.copy_messages.dest);
+		imapx_command_add (ic, " %f", job->u.copy_messages.dest);
 		imapx_command_queue (is, ic);
 		return;
 	}
@@ -3634,7 +3750,7 @@ imapx_command_copy_messages_step_done (CamelIMAPXServer *is,
 	}
 
 	if (i < uids->len) {
-		camel_imapx_command_free (ic);
+		imapx_command_unref (ic);
 		imapx_command_copy_messages_step_start (is, job, i);
 		return;
 	}
@@ -3644,7 +3760,7 @@ cleanup:
 	g_object_unref (job->folder);
 
 	imapx_job_done (is, job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -3719,7 +3835,7 @@ imapx_command_append_message_done (CamelIMAPXServer *is,
 	g_object_unref (job->folder);
 
 	imapx_job_done (is, job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -3729,7 +3845,7 @@ imapx_job_append_message_start (CamelIMAPXServer *is,
 	CamelIMAPXCommand *ic;
 
 	/* TODO: we could supply the original append date from the file timestamp */
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "APPEND", NULL, job->cancellable,
 		"APPEND %f %F %P", job->folder,
 		((CamelMessageInfoBase *) job->u.append_message.info)->flags,
@@ -3847,9 +3963,9 @@ imapx_command_step_fetch_done (CamelIMAPXServer *is,
 	camel_folder_change_info_clear (job->u.refresh_info.changes);
 
 	if (i < infos->len) {
-		camel_imapx_command_free (ic);
+		imapx_command_unref (ic);
 
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "FETCH", job->folder,
 			job->cancellable, "UID FETCH ");
 		ic->complete = imapx_command_step_fetch_done;
@@ -3864,7 +3980,7 @@ imapx_command_step_fetch_done (CamelIMAPXServer *is,
 			if (!r->exists) {
 				res = imapx_uidset_add (&job->u.refresh_info.uidset, ic, r->uid);
 				if (res == 1) {
-					camel_imapx_command_add(ic, " (RFC822.SIZE RFC822.HEADER)");
+					imapx_command_add (ic, " (RFC822.SIZE RFC822.HEADER)");
 					job->u.refresh_info.index = i;
 					imapx_command_queue (is, ic);
 					return;
@@ -3874,7 +3990,7 @@ imapx_command_step_fetch_done (CamelIMAPXServer *is,
 
 		job->u.refresh_info.index = i;
 		if (imapx_uidset_done (&job->u.refresh_info.uidset, ic)) {
-			camel_imapx_command_add(ic, " (RFC822.SIZE RFC822.HEADER)");
+			imapx_command_add (ic, " (RFC822.SIZE RFC822.HEADER)");
 			imapx_command_queue (is, ic);
 			return;
 		}
@@ -3908,7 +4024,7 @@ imapx_command_step_fetch_done (CamelIMAPXServer *is,
 		camel_folder_change_info_free (job->u.refresh_info.changes);
 
 	imapx_job_done (is, job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static gint
@@ -4103,7 +4219,7 @@ imapx_job_scan_changes_done (CamelIMAPXServer *is,
 
 	g_array_free (job->u.refresh_info.infos, TRUE);
 	imapx_job_done (is, job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4121,7 +4237,7 @@ imapx_job_scan_changes_start (CamelIMAPXServer *is,
 			camel_folder_get_display_name (job->folder));
 	}
 
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "FETCH", job->folder, job->cancellable,
 		"UID FETCH 1:* (UID FLAGS)");
 	ic->job = job;
@@ -4171,7 +4287,7 @@ exception:
 	camel_folder_change_info_free (ic->job->u.refresh_info.changes);
 
 	imapx_job_done (is, ic->job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4230,7 +4346,7 @@ imapx_job_fetch_new_messages_start (CamelIMAPXServer *is,
 	}
 
 	if (diff > uidset_size || fetch_order == CAMEL_SORT_DESCENDING) {
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "FETCH", job->folder, job->cancellable,
 			"UID FETCH %s:* (UID FLAGS)", uid);
 		imapx_uidset_init (&job->u.refresh_info.uidset, uidset_size, 0);
@@ -4242,7 +4358,7 @@ imapx_job_fetch_new_messages_start (CamelIMAPXServer *is,
 		else
 			ic->complete = imapx_command_step_fetch_done;
 	} else {
-		ic = camel_imapx_command_new (
+		ic = imapx_command_new (
 			is, "FETCH", job->folder, job->cancellable,
 			"UID FETCH %s:* (RFC822.SIZE RFC822.HEADER FLAGS)", uid);
 		ic->pri = job->pri;
@@ -4328,11 +4444,11 @@ imapx_job_refresh_info_start (CamelIMAPXServer *is,
 			}
 		} else {
 			if (is->cinfo->capa & IMAPX_CAPABILITY_CONDSTORE)
-				ic = camel_imapx_command_new (
+				ic = imapx_command_new (
 					is, "STATUS", NULL, job->cancellable,
 					"STATUS %f (MESSAGES UNSEEN UIDVALIDITY UIDNEXT HIGHESTMODSEQ)", folder);
 			else
-				ic = camel_imapx_command_new (
+				ic = imapx_command_new (
 					is, "STATUS", NULL, job->cancellable,
 					"STATUS %f (MESSAGES UNSEEN UIDVALIDITY UIDNEXT)", folder);
 
@@ -4343,10 +4459,10 @@ imapx_job_refresh_info_start (CamelIMAPXServer *is,
 
 			if (ic->error != NULL || ic->status->result != IMAPX_OK) {
 				propagate_ic_error (job, ic, "Error refreshing folder: %s");
-				camel_imapx_command_free (ic);
+				imapx_command_unref (ic);
 				goto done;
 			}
-			camel_imapx_command_free (ic);
+			imapx_command_unref (ic);
 		}
 
 		/* Recalulate need_rescan */
@@ -4463,7 +4579,7 @@ imapx_command_expunge_done (CamelIMAPXServer *is,
 	}
 
 	imapx_job_done (is, ic->job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4477,7 +4593,7 @@ imapx_job_expunge_start (CamelIMAPXServer *is,
 		job->cancellable, &job->error);
 
 	/* TODO handle UIDPLUS capability */
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "EXPUNGE", job->folder,
 		job->cancellable, "EXPUNGE");
 	ic->job = job;
@@ -4498,7 +4614,7 @@ imapx_command_list_done (CamelIMAPXServer *is,
 
 	e (is->tagprefix, "==== list or lsub completed ==== \n");
 	imapx_job_done (is, ic->job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4507,7 +4623,7 @@ imapx_job_list_start (CamelIMAPXServer *is,
 {
 	CamelIMAPXCommand *ic;
 
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "LIST", NULL, job->cancellable,
 		"%s \"\" %s",
 		(job->u.list.flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED) ?
@@ -4515,8 +4631,8 @@ imapx_job_list_start (CamelIMAPXServer *is,
 		job->u.list.pattern);
 	if (job->u.list.ext) {
 		/* Hm, we need a way to add atoms _without_ quoting or using literals */
-		camel_imapx_command_add(ic, " ");
-		camel_imapx_command_add (ic, job->u.list.ext);
+		imapx_command_add (ic, " ");
+		imapx_command_add (ic, job->u.list.ext);
 	}
 	ic->pri = job->pri;
 	ic->job = job;
@@ -4550,7 +4666,7 @@ imapx_command_subscription_done (CamelIMAPXServer *is,
 	}
 
 	imapx_job_done (is, ic->job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4569,7 +4685,7 @@ imapx_job_manage_subscription_start (CamelIMAPXServer *is,
 	encoded_fname = imapx_encode_folder_name (
 		(CamelIMAPXStore *) is->store,
 		job->u.manage_subscriptions.folder_name);
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, str, NULL, job->cancellable,
 		"%s %s", str, encoded_fname);
 
@@ -4592,7 +4708,7 @@ imapx_command_create_folder_done (CamelIMAPXServer *is,
 	}
 
 	imapx_job_done (is, ic->job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4603,7 +4719,7 @@ imapx_job_create_folder_start (CamelIMAPXServer *is,
 	gchar *encoded_fname = NULL;
 
 	encoded_fname = camel_utf8_utf7 (job->u.folder_name);
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "CREATE", NULL, job->cancellable,
 		"CREATE %s", encoded_fname);
 	ic->pri = job->pri;
@@ -4625,7 +4741,7 @@ imapx_command_delete_folder_done (CamelIMAPXServer *is,
 	}
 
 	imapx_job_done (is, ic->job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4641,7 +4757,7 @@ imapx_job_delete_folder_start (CamelIMAPXServer *is,
 		is->store, "INBOX", 0, job->cancellable, &job->error);
 
 	/* make sure to-be-deleted folder is not selected by selecting INBOX for this operation */
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "DELETE", job->folder, job->cancellable,
 		"DELETE %s", encoded_fname);
 	ic->pri = job->pri;
@@ -4663,7 +4779,7 @@ imapx_command_rename_folder_done (CamelIMAPXServer *is,
 	}
 
 	imapx_job_done (is, ic->job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4679,7 +4795,7 @@ imapx_job_rename_folder_start (CamelIMAPXServer *is,
 	en_ofname = imapx_encode_folder_name ((CamelIMAPXStore *) is->store, job->u.rename_folder.ofolder_name);
 	en_nfname = imapx_encode_folder_name ((CamelIMAPXStore *) is->store, job->u.rename_folder.nfolder_name);
 
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "RENAME", job->folder, job->cancellable,
 		"RENAME %s %s", en_ofname, en_nfname);
 	ic->pri = job->pri;
@@ -4702,7 +4818,7 @@ imapx_command_noop_done (CamelIMAPXServer *is,
 	}
 
 	imapx_job_done (is, ic->job);
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4711,7 +4827,7 @@ imapx_job_noop_start (CamelIMAPXServer *is,
 {
 	CamelIMAPXCommand *ic;
 
-	ic = camel_imapx_command_new (
+	ic = imapx_command_new (
 		is, "NOOP", job->folder, job->cancellable, "NOOP");
 
 	ic->job = job;
@@ -4832,7 +4948,7 @@ imapx_command_sync_changes_done (CamelIMAPXServer *is,
 
 		imapx_job_done (is, job);
 	}
-	camel_imapx_command_free (ic);
+	imapx_command_unref (ic);
 }
 
 static void
@@ -4874,7 +4990,7 @@ imapx_job_sync_changes_start (CamelIMAPXServer *is,
 				if ( (on && (((flags ^ sflags) & flags) & flag))
 				     || (!on && (((flags ^ sflags) & ~flags) & flag))) {
 					if (ic == NULL) {
-						ic = camel_imapx_command_new (
+						ic = imapx_command_new (
 							is, "STORE", job->folder,
 							job->cancellable, "UID STORE ");
 						ic->complete = imapx_command_sync_changes_done;
@@ -4885,7 +5001,7 @@ imapx_job_sync_changes_start (CamelIMAPXServer *is,
 				}
 				if (send || (i == uids->len - 1 && imapx_uidset_done (&ss, ic))) {
 					job->commands++;
-					camel_imapx_command_add(ic, " %tFLAGS.SILENT (%t)", on?"+":"-", flags_table[j].name);
+					imapx_command_add (ic, " %tFLAGS.SILENT (%t)", on?"+":"-", flags_table[j].name);
 					imapx_command_queue (is, ic);
 					ic = NULL;
 				}
@@ -4912,7 +5028,7 @@ imapx_job_sync_changes_start (CamelIMAPXServer *is,
 					CamelIMAPXMessageInfo *info = c->infos->pdata[i];
 
 					if (ic == NULL) {
-						ic = camel_imapx_command_new (
+						ic = imapx_command_new (
 							is, "STORE", job->folder,
 							job->cancellable, "UID STORE ");
 						ic->complete = imapx_command_sync_changes_done;
@@ -4923,7 +5039,7 @@ imapx_job_sync_changes_start (CamelIMAPXServer *is,
 					if (imapx_uidset_add (&ss, ic, camel_message_info_uid (info))
 					    || (i == c->infos->len - 1 && imapx_uidset_done (&ss, ic))) {
 						job->commands++;
-						camel_imapx_command_add(ic, " %tFLAGS.SILENT (%t)", on?"+":"-", c->name);
+						imapx_command_add (ic, " %tFLAGS.SILENT (%t)", on?"+":"-", c->name);
 						imapx_command_queue (is, ic);
 						ic = NULL;
 					}
@@ -4991,13 +5107,6 @@ parse_contents (CamelIMAPXServer *is,
 		g_propagate_error (error, local_error);
 }
 
-static void
-imapx_poll_cancelled (GCancellable *cancellable,
-                      PRThread *thread)
-{
-	PR_Interrupt (thread);
-}
-
 /*
  * The main processing (reading) loop.
  *
@@ -5019,6 +5128,7 @@ imapx_parser_thread (gpointer d)
 
 	while (local_error == NULL && is->stream) {
 		g_cancellable_reset (cancellable);
+
 #ifndef G_OS_WIN32
 		if (is->is_process_stream)	{
 			GPollFD fds[2] = { {0, 0, 0}, {0, 0, 0} };
@@ -5039,41 +5149,11 @@ imapx_parser_thread (gpointer d)
 		} else
 #endif
 		{
-			PRPollDesc pollfds = { };
-			gulong cancel_id;
-			gint res;
-
-			pollfds.fd = camel_tcp_stream_get_file_desc (CAMEL_TCP_STREAM (is->stream->source));
-			pollfds.in_flags = PR_POLL_READ;
-			pollfds.out_flags = 0;
-
-			cancel_id = g_cancellable_connect (
-				cancellable, G_CALLBACK (imapx_poll_cancelled),
-				PR_GetCurrentThread (), (GDestroyNotify) NULL);
-
-			res = PR_Poll (&pollfds, 1, PR_INTERVAL_NO_TIMEOUT);
-
-			g_cancellable_disconnect (cancellable, cancel_id);
-
-			if (res == -1)
-				g_usleep (1) /* ?? */ ;
-			else if (res == 0) {
-				/* timed out */
-			} else if ((pollfds.out_flags & PR_POLL_READ))
-				parse_contents (is, cancellable, &local_error);
+			parse_contents (is, cancellable, &local_error);
 		}
 
-		/* Jump out of the loop if an error occurred. */
-		if (local_error != NULL)
-			break;
-
-		if (is->parser_quit) {
-			g_set_error (
-				&local_error, G_IO_ERROR,
-				G_IO_ERROR_CANCELLED,
-				_("Cancelled"));
-			break;
-		}
+		if (is->parser_quit)
+			g_cancellable_cancel (cancellable);
 
 		if (g_cancellable_is_cancelled (cancellable)) {
 			gint is_empty;
@@ -5082,14 +5162,18 @@ imapx_parser_thread (gpointer d)
 			is_empty = camel_dlist_empty (&is->active);
 			QUEUE_UNLOCK (is);
 
-			if ((is_empty || (imapx_idle_supported (is) && imapx_in_idle (is))))
+			if (is_empty || (imapx_idle_supported (is) && imapx_in_idle (is))) {
 				g_cancellable_reset (cancellable);
-			else
-				g_set_error (
-					&local_error, G_IO_ERROR,
-					G_IO_ERROR_CANCELLED,
-					_("Cancelled"));
+				g_clear_error (&local_error);
+			} else {
+				/* Cancelled error should be set. */
+				g_warn_if_fail (local_error != NULL);
+			}
 		}
+
+		/* Jump out of the loop if an error occurred. */
+		if (local_error != NULL)
+			break;
 	}
 
 	QUEUE_LOCK (is);
@@ -5240,7 +5324,7 @@ camel_imapx_server_init (CamelIMAPXServer *is)
 	camel_dlist_init (&is->queue);
 	camel_dlist_init (&is->active);
 	camel_dlist_init (&is->done);
-	camel_dlist_init (&is->jobs);
+	g_queue_init (&is->jobs);
 
 	/* not used at the moment. Use it in future */
 	is->job_timeout = 29 * 60 * 1000 * 1000;
@@ -5429,7 +5513,7 @@ imapx_server_get_message (CamelIMAPXServer *is,
 	else if (job->u.get_message.stream != NULL)
 		g_object_unref (job->u.get_message.stream);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	g_mutex_lock (is->fetch_mutex);
 	is->fetch_count++;
@@ -5587,7 +5671,7 @@ camel_imapx_server_append_message (CamelIMAPXServer *is,
 
 	success = imapx_submit_job (is, job, error);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return success;
 }
@@ -5609,7 +5693,7 @@ camel_imapx_server_noop (CamelIMAPXServer *is,
 
 	success = imapx_submit_job (is, job, error);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return success;
 }
@@ -5655,7 +5739,7 @@ camel_imapx_server_refresh_info (CamelIMAPXServer *is,
 
 	camel_folder_change_info_free (job->u.refresh_info.changes);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return success;
 }
@@ -5831,7 +5915,7 @@ imapx_server_sync_changes (CamelIMAPXServer *is,
 
 	success = registered && imapx_run_job (is, job, error);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 done:
 	imapx_sync_free_user (on_user);
@@ -5884,7 +5968,7 @@ camel_imapx_server_expunge (CamelIMAPXServer *is,
 
 	success = registered && imapx_run_job (is, job, error);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return success;
 }
@@ -5970,7 +6054,7 @@ camel_imapx_server_list (CamelIMAPXServer *is,
 
 	g_hash_table_destroy (job->u.list.folders);
 	g_free (encoded_name);
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return folders;
 }
@@ -5994,7 +6078,7 @@ camel_imapx_server_manage_subscription (CamelIMAPXServer *is,
 
 	success = imapx_submit_job (is, job, error);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return success;
 }
@@ -6016,7 +6100,7 @@ camel_imapx_server_create_folder (CamelIMAPXServer *is,
 
 	success = imapx_submit_job (is, job, error);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return success;
 }
@@ -6038,7 +6122,7 @@ camel_imapx_server_delete_folder (CamelIMAPXServer *is,
 
 	success = imapx_submit_job (is, job, error);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return success;
 }
@@ -6062,7 +6146,7 @@ camel_imapx_server_rename_folder (CamelIMAPXServer *is,
 
 	success = imapx_submit_job (is, job, error);
 
-	imapx_job_free (job);
+	imapx_job_unref (job);
 
 	return success;
 }
@@ -6071,16 +6155,18 @@ IMAPXJobQueueInfo *
 camel_imapx_server_get_job_queue_info (CamelIMAPXServer *is)
 {
 	IMAPXJobQueueInfo *jinfo = g_new0 (IMAPXJobQueueInfo, 1);
-	CamelDListNode *node;
 	CamelIMAPXJob *job = NULL;
+	GList *head, *link;
 
 	QUEUE_LOCK (is);
 
-	jinfo->queue_len = camel_dlist_length (&is->jobs);
+	jinfo->queue_len = g_queue_get_length (&is->jobs);
 	jinfo->folders = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free, NULL);
 
-	for (node = is->jobs.head; node->next; node = job->msg.ln.next) {
-		job = (CamelIMAPXJob *) node;
+	head = g_queue_peek_head_link (&is->jobs);
+
+	for (link = head; link != NULL; link = g_list_next (link)) {
+		job = (CamelIMAPXJob *) link->data;
 
 		if (job->folder) {
 			const gchar *full_name = camel_folder_get_full_name (job->folder);
diff --git a/src/camel/providers/imapx/camel-imapx-server.h b/src/camel/providers/imapx/camel-imapx-server.h
index f4c8fb7..f7ed66d 100644
--- a/src/camel/providers/imapx/camel-imapx-server.h
+++ b/src/camel/providers/imapx/camel-imapx-server.h
@@ -70,8 +70,8 @@ struct _CamelIMAPXServer {
 	CamelIMAPXNamespaceList *nsl;
 
 	/* incoming jobs */
-	CamelMsgPort *port;
-	CamelDList jobs;
+	GQueue jobs;
+
 	/* in micro seconds */
 	guint job_timeout;
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]