+print_queue_item (ModestMailOperation *op, const gchar* prefix)
+{
+ gchar *op_str = modest_mail_operation_to_string (op);
+ g_debug ("%s: %s",
+ prefix ? prefix : "",
+ op_str);
+ g_free (op_str);
+}
+
+static void
+on_finalize_foreach(gpointer op,
+ gpointer user_data)
+{
+ ModestMailOperationQueue *queue;
+ ModestMailOperationQueuePrivate *priv;
+ ModestMailOperation *mail_op;
+
+ queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
+ priv = MODEST_MAIL_OPERATION_QUEUE_GET_PRIVATE (queue);
+ mail_op = MODEST_MAIL_OPERATION (op);
+
+ /* Simply remove from queue, but without emitting a
+ * QUEUE_CHANGED_SIGNAL because we are in finalize anyway and have
+ * the lock acquired. */
+ g_signal_handlers_disconnect_by_func (mail_op, G_CALLBACK (on_operation_finished), user_data);
+
+ MODEST_DEBUG_BLOCK (print_queue_item (mail_op, "cancel/remove"););
+
+ modest_mail_operation_cancel (mail_op);
+ g_queue_remove (priv->op_queue, mail_op);
+ g_object_unref (G_OBJECT (mail_op));
+}
+
+static void