/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Libburner-burn
 * Copyright (C) Philippe Rouquier 2005-2009 <bonfire-app@wanadoo.fr>
 *
 * Libburner-burn is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Libburner-burn authors hereby grant permission for non-GPL compatible
 * GStreamer plugins to be used and distributed together with GStreamer
 * and Libburner-burn. This permission is above and beyond the permissions granted
 * by the GPL license by which Libburner-burn is covered. If you modify this code
 * you may extend this exception to your version of the code, but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version.
 * 
 * Libburner-burn is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to:
 * 	The Free Software Foundation, Inc.,
 * 	51 Franklin Street, Fifth Floor
 * 	Boston, MA  02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include <gmodule.h>

#include <libburn/libburn.h>

#include "burner-units.h"
#include "burn-job.h"
#include "burn-debug.h"
#include "burner-plugin-registration.h"
#include "burn-libburn-common.h"
#include "burn-libburnia.h"
#include "burner-track-image.h"
#include "burner-track-stream.h"


#define BURNER_TYPE_LIBBURN         (burner_libburn_get_type ())
#define BURNER_LIBBURN(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), BURNER_TYPE_LIBBURN, BurnerLibburn))
#define BURNER_LIBBURN_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), BURNER_TYPE_LIBBURN, BurnerLibburnClass))
#define BURNER_IS_LIBBURN(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), BURNER_TYPE_LIBBURN))
#define BURNER_IS_LIBBURN_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), BURNER_TYPE_LIBBURN))
#define BURNER_LIBBURN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), BURNER_TYPE_LIBBURN, BurnerLibburnClass))

BURNER_PLUGIN_BOILERPLATE (BurnerLibburn, burner_libburn, BURNER_TYPE_JOB, BurnerJob);

#define BURNER_PVD_SIZE	32ULL * 2048ULL

struct _BurnerLibburnPrivate {
	BurnerLibburnCtx *ctx;

	/* This buffer is used to capture Primary Volume Descriptor for
	 * for overwrite media so as to "grow" the latter. */
	unsigned char *pvd;

	guint sig_handler:1;
};
typedef struct _BurnerLibburnPrivate BurnerLibburnPrivate;

#define BURNER_LIBBURN_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), BURNER_TYPE_LIBBURN, BurnerLibburnPrivate))

/**
 * taken from scsi-get-configuration.h
 */

typedef enum {
BURNER_SCSI_PROF_DVD_RW_RESTRICTED	= 0x0013,
BURNER_SCSI_PROF_DVD_RW_PLUS		= 0x001A,
} BurnerScsiProfile;

static GObjectClass *parent_class = NULL;

struct _BurnerLibburnSrcData {
	int fd;
	off_t size;

	/* That's for the primary volume descriptor used for overwrite media */
	int pvd_size;						/* in blocks */
	unsigned char *pvd;

	int read_pvd:1;
};
typedef struct _BurnerLibburnSrcData BurnerLibburnSrcData;

static void
burner_libburn_src_free_data (struct burn_source *src)
{
	BurnerLibburnSrcData *data;

	data = src->data;
	close (data->fd);
	g_free (data);
}

static off_t
burner_libburn_src_get_size (struct burn_source *src)
{
	BurnerLibburnSrcData *data;

	data = src->data;
	return data->size;
}

static int
burner_libburn_src_set_size (struct burn_source *src,
			      off_t size)
{
	BurnerLibburnSrcData *data;

	data = src->data;
	data->size = size;
	return 1;
}

/**
 * This is a copy from burn-volume.c
 */

struct _BurnerVolDesc {
	guchar type;
	gchar id			[5];
	guchar version;
};
typedef struct _BurnerVolDesc BurnerVolDesc;

static int
burner_libburn_src_read_xt (struct burn_source *src,
			     unsigned char *buffer,
			     int size)
{
	int total;
	BurnerLibburnSrcData *data;

	data = src->data;

	total = 0;
	while (total < size) {
		int bytes;

		bytes = read (data->fd, buffer + total, size - total);
		if (bytes < 0)
			return -1;

		if (!bytes)
			break;

		total += bytes;
	}

	/* copy the primary volume descriptor if a buffer is provided */
	if (data->pvd
	&& !data->read_pvd
	&&  data->pvd_size < BURNER_PVD_SIZE) {
		unsigned char *current_pvd;
		int i;

		current_pvd = data->pvd + data->pvd_size;

		/* read volume descriptors until we reach the end of the
		 * buffer or find a volume descriptor set end. */
		for (i = 0; (i << 11) < size && data->pvd_size + (i << 11) < BURNER_PVD_SIZE; i ++) {
			BurnerVolDesc *desc;

			/* No need to check the first 16 blocks */
			if ((data->pvd_size >> 11) + i < 16)
				continue;

			desc = (BurnerVolDesc *) (buffer + sizeof (BurnerVolDesc) * i);
			if (desc->type == 255) {
				data->read_pvd = 1;
				BURNER_BURN_LOG ("found volume descriptor set end");
				break;
			}
		}

		memcpy (current_pvd, buffer, i << 11);
		data->pvd_size += i << 11;
	}

	return total;
}

static struct burn_source *
burner_libburn_create_fd_source (int fd,
				  gint64 size,
				  unsigned char *pvd)
{
	struct burn_source *src;
	BurnerLibburnSrcData *data;

	data = g_new0 (BurnerLibburnSrcData, 1);
	data->fd = fd;
	data->size = size;
	data->pvd = pvd;

	/* FIXME: this could be wrapped into a fifo source to get a smoother
	 * data delivery. But that means another thread ... */
	src = g_new0 (struct burn_source, 1);
	src->version = 1;
	src->refcount = 1;
	src->read_xt = burner_libburn_src_read_xt;
	src->get_size = burner_libburn_src_get_size;
	src->set_size = burner_libburn_src_set_size;
	src->free_data = burner_libburn_src_free_data;
	src->data = data;

	return src;
}

static BurnerBurnResult
burner_libburn_add_track (struct burn_session *session,
			   struct burn_track *track,
			   struct burn_source *src,
			   gint mode,
			   GError **error)
{
	if (burn_track_set_source (track, src) != BURN_SOURCE_OK) {
		g_set_error (error,
			     BURNER_BURN_ERROR,
			     BURNER_BURN_ERROR_GENERAL,
			     _("libburn track could not be created"));
		return BURNER_BURN_ERR;
	}

	if (!burn_session_add_track (session, track, BURN_POS_END)) {
		g_set_error (error,
			     BURNER_BURN_ERROR,
			     BURNER_BURN_ERROR_GENERAL,
			     _("libburn track could not be created"));
		return BURNER_BURN_ERR;
	}

	return BURNER_BURN_OK;
}

static BurnerBurnResult
burner_libburn_add_fd_track (struct burn_session *session,
			      int fd,
			      gint mode,
			      gint64 size,
			      unsigned char *pvd,
			      GError **error)
{
	struct burn_source *src;
	struct burn_track *track;
	BurnerBurnResult result;

	track = burn_track_create ();
	burn_track_define_data (track, 0, 0, 0, mode);

	src = burner_libburn_create_fd_source (fd, size, pvd);
	result = burner_libburn_add_track (session, track, src, mode, error);

	burn_source_free (src);
	burn_track_free (track);

	return result;
}

static BurnerBurnResult
burner_libburn_add_file_track (struct burn_session *session,
				const gchar *path,
				gint mode,
				off_t size,
				unsigned char *pvd,
				GError **error)
{
	int fd;

	fd = open (path, O_RDONLY);
	if (fd == -1) {
		g_set_error (error,
			     BURNER_BURN_ERROR,
			     BURNER_BURN_ERROR_GENERAL,
			     "%s",
			     g_strerror (errno));
		return BURNER_BURN_ERR;
	}

	return burner_libburn_add_fd_track (session, fd, mode, size, pvd, error);
}

static BurnerBurnResult
burner_libburn_setup_session_fd (BurnerLibburn *self,
			          struct burn_session *session,
			          GError **error)
{
	int fd;
	goffset bytes = 0;
	BurnerLibburnPrivate *priv;
	BurnerTrackType *type = NULL;
	BurnerBurnResult result = BURNER_BURN_OK;

	priv = BURNER_LIBBURN_PRIVATE (self);

	burner_job_get_fd_in (BURNER_JOB (self), &fd);

	/* need to find out what type of track the imager will output */
	type = burner_track_type_new ();
	burner_job_get_input_type (BURNER_JOB (self), type);

	if (burner_track_type_get_has_image (type)) {
		gint mode;

		/* FIXME: implement other IMAGE types */
		if (burner_track_type_get_image_format (type) == BURNER_IMAGE_FORMAT_BIN)
			mode = BURN_MODE1;
		else
			mode = BURN_MODE1|BURN_MODE_RAW|BURN_SUBCODE_R96;

		burner_track_type_free (type);

		burner_job_get_session_output_size (BURNER_JOB (self),
						     NULL,
						     &bytes);

		result = burner_libburn_add_fd_track (session,
						       fd,
						       mode,
						       bytes,
						       priv->pvd,
						       error);
	}
	else if (burner_track_type_get_has_stream (type)) {
		GSList *tracks;
		guint64 length = 0;

		burner_track_type_free (type);

		burner_job_get_tracks (BURNER_JOB (self), &tracks);
		for (; tracks; tracks = tracks->next) {
			BurnerTrack *track;

			track = tracks->data;
			burner_track_stream_get_length (BURNER_TRACK_STREAM (track), &length);
			bytes = BURNER_DURATION_TO_BYTES (length);

			/* we dup the descriptor so the same 
			 * will be shared by all tracks */
			result = burner_libburn_add_fd_track (session,
							       dup (fd),
							       BURN_AUDIO,
							       bytes,
							       NULL,
							       error);
			if (result != BURNER_BURN_OK)
				return result;
		}
	}
	else
		BURNER_JOB_NOT_SUPPORTED (self);

	return result;
}

static BurnerBurnResult
burner_libburn_setup_session_file (BurnerLibburn *self, 
				    struct burn_session *session,
				    GError **error)
{
	BurnerLibburnPrivate *priv;
	BurnerBurnResult result;
	GSList *tracks = NULL;

	priv = BURNER_LIBBURN_PRIVATE (self);

	/* create the track(s) */
	result = BURNER_BURN_OK;
	burner_job_get_tracks (BURNER_JOB (self), &tracks);
	for (; tracks; tracks = tracks->next) {
		BurnerTrack *track;

		track = tracks->data;
		if (BURNER_IS_TRACK_STREAM (track)) {
			gchar *audiopath;
			guint64 size;

			audiopath = burner_track_stream_get_source (BURNER_TRACK_STREAM (track), FALSE);
			burner_track_stream_get_length (BURNER_TRACK_STREAM (track), &size);
			size = BURNER_DURATION_TO_BYTES (size);

			result = burner_libburn_add_file_track (session,
								 audiopath,
								 BURN_AUDIO,
								 size,
								 NULL,
								 error);
			if (result != BURNER_BURN_OK)
				break;
		}
		else if (BURNER_IS_TRACK_IMAGE (track)) {
			BurnerImageFormat format;
			gchar *imagepath;
			goffset bytes;
			gint mode;

			format = burner_track_image_get_format (BURNER_TRACK_IMAGE (track));
			if (format == BURNER_IMAGE_FORMAT_BIN) {
				mode = BURN_MODE1;
				imagepath = burner_track_image_get_source (BURNER_TRACK_IMAGE (track), FALSE);
			}
			else if (format == BURNER_IMAGE_FORMAT_NONE) {
				mode = BURN_MODE1;
				imagepath = burner_track_image_get_source (BURNER_TRACK_IMAGE (track), FALSE);
			}
			else
				BURNER_JOB_NOT_SUPPORTED (self);

			if (!imagepath)
				return BURNER_BURN_ERR;

			result = burner_track_get_size (track,
							 NULL,
							 &bytes);
			if (result != BURNER_BURN_OK)
				return BURNER_BURN_ERR;

			result = burner_libburn_add_file_track (session,
								 imagepath,
								 mode,
								 bytes,
								 priv->pvd,
								 error);
			g_free (imagepath);
		}
		else
			BURNER_JOB_NOT_SUPPORTED (self);
	}

	return result;
}

static BurnerBurnResult
burner_libburn_create_disc (BurnerLibburn *self,
			     struct burn_disc **retval,
			     GError **error)
{
	struct burn_disc *disc;
	BurnerBurnResult result;
	struct burn_session *session;

	/* set the source image */
	disc = burn_disc_create ();

	/* create the session */
	session = burn_session_create ();
	burn_disc_add_session (disc, session, BURN_POS_END);
	burn_session_free (session);

	if (burner_job_get_fd_in (BURNER_JOB (self), NULL) == BURNER_BURN_OK)
		result = burner_libburn_setup_session_fd (self, session, error);
	else
		result = burner_libburn_setup_session_file (self, session, error);

	if (result != BURNER_BURN_OK) {
		burn_disc_free (disc);
		return result;
	}

	*retval = disc;
	return result;
}

static BurnerBurnResult
burner_libburn_start_record (BurnerLibburn *self,
			      GError **error)
{
	guint64 rate;
	goffset blocks = 0;
	BurnerMedia media;
	BurnerBurnFlag flags;
	BurnerBurnResult result;
	BurnerLibburnPrivate *priv;
	struct burn_write_opts *opts;
	gchar reason [BURN_REASONS_LEN];

	priv = BURNER_LIBBURN_PRIVATE (self);

	/* if appending a DVD+-RW get PVD */
	burner_job_get_flags (BURNER_JOB (self), &flags);
	burner_job_get_media (BURNER_JOB (self), &media);

	if (flags & (BURNER_BURN_FLAG_MERGE|BURNER_BURN_FLAG_APPEND)
	&&  BURNER_MEDIUM_RANDOM_WRITABLE (media)
	&& (media & BURNER_MEDIUM_HAS_DATA))
		priv->pvd = g_new0 (unsigned char, BURNER_PVD_SIZE);

	result = burner_libburn_create_disc (self, &priv->ctx->disc, error);
	if (result != BURNER_BURN_OK)
		return result;

	/* Note: we don't need to call burn_drive_get_status nor
	 * burn_disc_get_status since we took care of the disc
	 * checking thing earlier ourselves. Now there is a proper
	 * disc and tray is locked. */
	opts = burn_write_opts_new (priv->ctx->drive);

	/* only turn this on for CDs */
	if ((media & BURNER_MEDIUM_CD) != 0)
		burn_write_opts_set_perform_opc (opts, 0);
	else
		burn_write_opts_set_perform_opc (opts, 0);

	if (flags & BURNER_BURN_FLAG_DAO)
		burn_write_opts_set_write_type (opts,
						BURN_WRITE_SAO,
						BURN_BLOCK_SAO);
	else {
		burn_write_opts_set_write_type (opts,
						BURN_WRITE_TAO,
						BURN_BLOCK_MODE1);

		/* we also set the start block to write from if MERGE is set.
		 * That only for random writable media; for other media libburn
		 * handles all by himself where to start writing. */
		if (BURNER_MEDIUM_RANDOM_WRITABLE (media)
		&& (flags & BURNER_BURN_FLAG_MERGE)) {
			goffset address = 0;

			burner_job_get_next_writable_address (BURNER_JOB (self), &address);

			BURNER_JOB_LOG (self, "Starting to write at block = %lli and byte %lli", address, address * 2048);
			burn_write_opts_set_start_byte (opts, address * 2048);
		}
	}

	if (!BURNER_MEDIUM_RANDOM_WRITABLE (media)) {
		BURNER_JOB_LOG (BURNER_JOB (self), "Setting multi %i", (flags & BURNER_BURN_FLAG_MULTI) != 0);
		burn_write_opts_set_multi (opts, (flags & BURNER_BURN_FLAG_MULTI) != 0);
	}

	burn_write_opts_set_underrun_proof (opts, (flags & BURNER_BURN_FLAG_BURNPROOF) != 0);
	BURNER_JOB_LOG (BURNER_JOB (self), "Setting burnproof %i", (flags & BURNER_BURN_FLAG_BURNPROOF) != 0);

	burn_write_opts_set_simulate (opts, (flags & BURNER_BURN_FLAG_DUMMY) != 0);
	BURNER_JOB_LOG (BURNER_JOB (self), "Setting dummy %i", (flags & BURNER_BURN_FLAG_DUMMY) != 0);

	burner_job_get_rate (BURNER_JOB (self), &rate);
	burn_drive_set_speed (priv->ctx->drive, 0, 0);

	if (burn_precheck_write (opts, priv->ctx->disc, reason, 0) < 1) {
		BURNER_JOB_LOG (BURNER_JOB (self), "Precheck failed %s", reason);
		g_set_error (error,
			     BURNER_BURN_ERROR,
			     BURNER_BURN_ERROR_GENERAL,
			     "%s",
			     reason);
		return BURNER_BURN_ERR;
	}

	/* If we're writing to a disc remember that the session can't be under
	 * 300 sectors (= 614400 bytes) */
	burner_job_get_session_output_size (BURNER_JOB (self), &blocks, NULL);
	if (blocks < 300)
		burner_job_set_output_size_for_current_track (BURNER_JOB (self),
							       300L - blocks,
							       614400L - blocks * 2048);

	if (!priv->sig_handler) {
		burn_set_signal_handling ("burner", NULL, 0);
		priv->sig_handler = 1;
	}

	burn_disc_write (opts, priv->ctx->disc);
	burn_write_opts_free (opts);

	return BURNER_BURN_OK;
}

static BurnerBurnResult
burner_libburn_start_erase (BurnerLibburn *self,
			     GError **error)
{
	char reasons [BURN_REASONS_LEN];
	struct burn_session *session;
	struct burn_write_opts *opts;
	BurnerLibburnPrivate *priv;
	BurnerBurnResult result;
	BurnerBurnFlag flags;
	char prof_name [80];
	int profile;
	int fd;

	priv = BURNER_LIBBURN_PRIVATE (self);
	if (burn_disc_get_profile (priv->ctx->drive, &profile, prof_name) <= 0) {
		g_set_error (error,
			     BURNER_BURN_ERROR,
			     BURNER_BURN_ERROR_MEDIUM_INVALID,
			     _("The disc is not supported"));
		return BURNER_BURN_ERR;
	}

	/* here we try to respect the current formatting of DVD-RW. For 
	 * overwritable media fast option means erase the first 64 Kib
	 * and long a forced reformatting */
	burner_job_get_flags (BURNER_JOB (self), &flags);
	if (profile == BURNER_SCSI_PROF_DVD_RW_RESTRICTED) {
		if (!(flags & BURNER_BURN_FLAG_FAST_BLANK)) {
			/* leave libburn choose the best format */
			if (!priv->sig_handler) {
				burn_set_signal_handling ("burner", NULL, 0);
				priv->sig_handler = 1;
			}

			burn_disc_format (priv->ctx->drive,
					  (off_t) 0,
					  (1 << 4));
			return BURNER_BURN_OK;
		}
	}
	else if (profile == BURNER_SCSI_PROF_DVD_RW_PLUS) {
		if (!(flags & BURNER_BURN_FLAG_FAST_BLANK)) {
			/* Bit 2 is for format max available size
			 * Bit 4 is enforce (re)-format if needed
			 * 0x26 is DVD+RW format is to be set from bit 8
			 * in the latter case bit 7 needs to be set as 
			 * well. */
			if (!priv->sig_handler) {
				burn_set_signal_handling ("burner", NULL, 0);
				priv->sig_handler = 1;
			}

			burn_disc_format (priv->ctx->drive,
					  (off_t) 0,
					  (1 << 2)|(1 << 4));
			return BURNER_BURN_OK;
		}
	}
	else if (burn_disc_erasable (priv->ctx->drive)) {
		/* This is mainly for CDRW and sequential DVD-RW */
		if (!priv->sig_handler) {
			burn_set_signal_handling ("burner", NULL, 0);
			priv->sig_handler = 1;
		}

		/* NOTE: for an unknown reason (to me)
		 * libburn when minimally blanking a DVD-RW
		 * will only allow one to write to it with DAO 
		 * afterwards... */
		burn_disc_erase (priv->ctx->drive, (flags & BURNER_BURN_FLAG_FAST_BLANK) != 0);
		return BURNER_BURN_OK;
	}
	else
		BURNER_JOB_NOT_SUPPORTED (self);

	/* This is the "fast option": basically we only write 64 Kib of 0 from
	 * /dev/null. If we reached that part it means we're dealing with
	 * overwrite media. */
	fd = open ("/dev/null", O_RDONLY);
	if (fd == -1) {
		int errnum = errno;

		g_set_error (error,
			     BURNER_BURN_ERROR,
			     BURNER_BURN_ERROR_GENERAL,
			     /* Translators: first %s is the filename, second %s is the error
			      * generated from errno */
			     _("\"%s\" could not be opened (%s)"),
			     "/dev/null",
			     g_strerror (errnum));
		return BURNER_BURN_ERR;
	}

	priv->ctx->disc = burn_disc_create ();

	/* create the session */
	session = burn_session_create ();
	burn_disc_add_session (priv->ctx->disc, session, BURN_POS_END);
	burn_session_free (session);

	result = burner_libburn_add_fd_track (session,
					       fd,
					       BURN_MODE1,
					       65536,		/* 32 blocks */
					       priv->pvd,
					       error);
	close (fd);

	opts = burn_write_opts_new (priv->ctx->drive);
	burn_write_opts_set_perform_opc (opts, 0);
	burn_write_opts_set_underrun_proof (opts, 1);
	burn_write_opts_set_simulate (opts, (flags & BURNER_BURN_FLAG_DUMMY));

	burn_drive_set_speed (priv->ctx->drive, 0, 0);
	burn_write_opts_set_write_type (opts,
					BURN_WRITE_TAO,
					BURN_BLOCK_MODE1);

	if (burn_precheck_write (opts, priv->ctx->disc, reasons, 0) <= 0) {
		burn_write_opts_free (opts);

		g_set_error (error,
			     BURNER_BURN_ERROR,
			     BURNER_BURN_ERROR_GENERAL,
			     /* Translators: %s is the error returned by libburn */
			     _("An internal error occurred (%s)"),
			     reasons);
		return BURNER_BURN_ERR;
	}

	if (!priv->sig_handler) {
		burn_set_signal_handling ("burner", NULL, 0);
		priv->sig_handler = 1;
	}

	burn_disc_write (opts, priv->ctx->disc);
	burn_write_opts_free (opts);

	return result;
}

static BurnerBurnResult
burner_libburn_start (BurnerJob *job,
		       GError **error)
{
	BurnerLibburn *self;
	BurnerJobAction action;
	BurnerBurnResult result;
	BurnerLibburnPrivate *priv;

	self = BURNER_LIBBURN (job);
	priv = BURNER_LIBBURN_PRIVATE (self);

	burner_job_get_action (job, &action);
	if (action == BURNER_JOB_ACTION_RECORD) {
		GError *ret_error = NULL;

		/* TRUE is a context that helps to adapt action
		 * messages like for DVD+RW which need a
		 * pre-formatting before actually writing
		 * and without this we would not know if
		 * we are actually formatting or just pre-
		 * formatting == starting to record */
		priv->ctx = burner_libburn_common_ctx_new (job, TRUE, &ret_error);
		if (!priv->ctx) {
			if (ret_error && ret_error->code == BURNER_BURN_ERROR_DRIVE_BUSY) {
				g_propagate_error (error, ret_error);
				return BURNER_BURN_RETRY;
			}

			if (error)
				g_propagate_error (error, ret_error);
			return BURNER_BURN_ERR;
		}

		result = burner_libburn_start_record (self, error);
		if (result != BURNER_BURN_OK)
			return result;

		burner_job_set_current_action (job,
						BURNER_BURN_ACTION_START_RECORDING,
						NULL,
						FALSE);
	}
	else if (action == BURNER_JOB_ACTION_ERASE) {
		GError *ret_error = NULL;

		priv->ctx = burner_libburn_common_ctx_new (job, FALSE, &ret_error);
		if (!priv->ctx) {
			if (ret_error && ret_error->code == BURNER_BURN_ERROR_DRIVE_BUSY) {
				g_propagate_error (error, ret_error);
				return BURNER_BURN_RETRY;
			}

			if (error)
				g_propagate_error (error, ret_error);
			return BURNER_BURN_ERR;
		}

		result = burner_libburn_start_erase (self, error);
		if (result != BURNER_BURN_OK)
			return result;

		burner_job_set_current_action (job,
						BURNER_BURN_ACTION_BLANKING,
						NULL,
						FALSE);
	}
	else
		BURNER_JOB_NOT_SUPPORTED (self);

	return BURNER_BURN_OK;
}

static BurnerBurnResult
burner_libburn_stop (BurnerJob *job,
		      GError **error)
{
	BurnerLibburn *self;
	BurnerLibburnPrivate *priv;

	self = BURNER_LIBBURN (job);
	priv = BURNER_LIBBURN_PRIVATE (self);

	if (priv->sig_handler) {
		priv->sig_handler = 0;
		burn_set_signal_handling (NULL, NULL, 1);
	}

	if (priv->ctx) {
		burner_libburn_common_ctx_free (priv->ctx);
		priv->ctx = NULL;
	}

	if (priv->pvd) {
		g_free (priv->pvd);
		priv->pvd = NULL;
	}

	if (BURNER_JOB_CLASS (parent_class)->stop)
		BURNER_JOB_CLASS (parent_class)->stop (job, error);

	return BURNER_BURN_OK;
}

static BurnerBurnResult
burner_libburn_clock_tick (BurnerJob *job)
{
	BurnerLibburnPrivate *priv;
	BurnerBurnResult result;
	int ret;

	priv = BURNER_LIBBURN_PRIVATE (job);
	result = burner_libburn_common_status (job, priv->ctx);

	if (result != BURNER_BURN_OK)
		return BURNER_BURN_OK;

	/* Double check that everything went well */
	if (!burn_drive_wrote_well (priv->ctx->drive)) {
		BURNER_JOB_LOG (job, "Something went wrong");
		burner_job_error (job,
				   g_error_new (BURNER_BURN_ERROR,
						BURNER_BURN_ERROR_WRITE_MEDIUM,
						_("An error occurred while writing to disc")));
		return BURNER_BURN_OK;
	}

	/* That's finished */
	if (!priv->pvd) {
		burner_job_set_dangerous (job, FALSE);
		burner_job_finished_session (job);
		return BURNER_BURN_OK;
	}

	/* In case we append data to a DVD+RW or DVD-RW
	 * (restricted overwrite) medium , we're not
	 * done since we need to overwrite the primary
	 * volume descriptor at sector 0.
	 * NOTE: This is a synchronous call but given the size of the buffer
	 * that shouldn't block.
	 * NOTE 2: in source we read the volume descriptors until we reached
	 * either the end of the buffer or the volume descriptor set end. That's
	 * kind of useless since for a DVD 16 blocks are written at a time. */
	BURNER_JOB_LOG (job, "Starting to overwrite primary volume descriptor");
	ret = burn_random_access_write (priv->ctx->drive,
					0,
					(char*)priv->pvd,
					BURNER_PVD_SIZE,
					0);
	if (ret != 1) {
		BURNER_JOB_LOG (job, "Random write failed");
		burner_job_error (job,
				   g_error_new (BURNER_BURN_ERROR,
						BURNER_BURN_ERROR_WRITE_MEDIUM,
						_("An error occurred while writing to disc")));
		return BURNER_BURN_OK;
	}

	burner_job_set_dangerous (job, FALSE);
	burner_job_finished_session (job);

	return BURNER_BURN_OK;
}

static void
burner_libburn_class_init (BurnerLibburnClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	BurnerJobClass *job_class = BURNER_JOB_CLASS (klass);

	g_type_class_add_private (klass, sizeof (BurnerLibburnPrivate));

	parent_class = g_type_class_peek_parent(klass);
	object_class->finalize = burner_libburn_finalize;

	job_class->start = burner_libburn_start;
	job_class->stop = burner_libburn_stop;
	job_class->clock_tick = burner_libburn_clock_tick;
}

static void
burner_libburn_init (BurnerLibburn *obj)
{

}

static void
burner_libburn_finalize (GObject *object)
{
	BurnerLibburn *cobj;
	BurnerLibburnPrivate *priv;

	cobj = BURNER_LIBBURN (object);
	priv = BURNER_LIBBURN_PRIVATE (cobj);

	if (priv->ctx) {
		burner_libburn_common_ctx_free (priv->ctx);
		priv->ctx = NULL;
	}

	G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
burner_libburn_export_caps (BurnerPlugin *plugin)
{
	const BurnerMedia media_cd = BURNER_MEDIUM_CD|
				      BURNER_MEDIUM_REWRITABLE|
				      BURNER_MEDIUM_WRITABLE|
				      BURNER_MEDIUM_BLANK|
				      BURNER_MEDIUM_APPENDABLE|
				      BURNER_MEDIUM_HAS_AUDIO|
				      BURNER_MEDIUM_HAS_DATA;
	const BurnerMedia media_dvd_w = BURNER_MEDIUM_DVD|
					 BURNER_MEDIUM_PLUS|
					 BURNER_MEDIUM_SEQUENTIAL|
					 BURNER_MEDIUM_WRITABLE|
					 BURNER_MEDIUM_APPENDABLE|
					 BURNER_MEDIUM_HAS_DATA|
					 BURNER_MEDIUM_BLANK;
	const BurnerMedia media_dvd_rw = BURNER_MEDIUM_DVD|
					  BURNER_MEDIUM_SEQUENTIAL|
					  BURNER_MEDIUM_REWRITABLE|
					  BURNER_MEDIUM_APPENDABLE|
					  BURNER_MEDIUM_HAS_DATA|
					  BURNER_MEDIUM_BLANK;
	const BurnerMedia media_dvd_rw_plus = BURNER_MEDIUM_DVD|
					       BURNER_MEDIUM_DUAL_L|
					       BURNER_MEDIUM_PLUS|
					       BURNER_MEDIUM_RESTRICTED|
					       BURNER_MEDIUM_REWRITABLE|
					       BURNER_MEDIUM_UNFORMATTED|
					       BURNER_MEDIUM_BLANK|
					       BURNER_MEDIUM_APPENDABLE|
					       BURNER_MEDIUM_CLOSED|
					       BURNER_MEDIUM_HAS_DATA;
	GSList *output;
	GSList *input;

	burner_plugin_define (plugin,
			       "libburn",
	                       NULL,
			       _("Burns, blanks and formats CDs, DVDs and BDs"),
			       "Philippe Rouquier",
			       15);

	/* libburn has no OVERBURN capabilities */

	/* CD(R)W */
	/* Use DAO for first session since AUDIO need it to write CD-TEXT
	 * Though libburn is unable to write CD-TEXT.... */
	/* Note: when burning multiple tracks to a CD (like audio for example)
	 * in dummy mode with TAO libburn will fail probably because it does
	 * not use a correct next writable address for the second track (it uses
	 * the same as for track #1). So remove dummy mode.
	 * This is probably the same reason why it fails at merging another
	 * session to a data CD in dummy mode. */
	BURNER_PLUGIN_ADD_STANDARD_CDR_FLAGS (plugin,
	                                       BURNER_BURN_FLAG_OVERBURN|
	                                       BURNER_BURN_FLAG_DUMMY);
	BURNER_PLUGIN_ADD_STANDARD_CDRW_FLAGS (plugin,
	                                        BURNER_BURN_FLAG_OVERBURN|
	                                        BURNER_BURN_FLAG_DUMMY);

	/* audio support for CDs only */
	input = burner_caps_audio_new (BURNER_PLUGIN_IO_ACCEPT_PIPE|
					BURNER_PLUGIN_IO_ACCEPT_FILE,
					BURNER_AUDIO_FORMAT_RAW_LITTLE_ENDIAN);
	
	output = burner_caps_disc_new (media_cd);
	burner_plugin_link_caps (plugin, output, input);
	g_slist_free (input);

	/* Image support for CDs ... */
	input = burner_caps_image_new (BURNER_PLUGIN_IO_ACCEPT_PIPE|
					BURNER_PLUGIN_IO_ACCEPT_FILE,
					BURNER_IMAGE_FORMAT_BIN);

	burner_plugin_link_caps (plugin, output, input);
	g_slist_free (output);

	/* ... and DVD-R and DVD+R ... */
	output = burner_caps_disc_new (media_dvd_w);
	burner_plugin_link_caps (plugin, output, input);
	g_slist_free (output);

	BURNER_PLUGIN_ADD_STANDARD_DVDR_PLUS_FLAGS (plugin, BURNER_BURN_FLAG_NONE);
	BURNER_PLUGIN_ADD_STANDARD_DVDR_FLAGS (plugin, BURNER_BURN_FLAG_NONE);

	/* ... and DVD-RW (sequential) */
	output = burner_caps_disc_new (media_dvd_rw);
	burner_plugin_link_caps (plugin, output, input);
	g_slist_free (output);

	BURNER_PLUGIN_ADD_STANDARD_DVDRW_FLAGS (plugin, BURNER_BURN_FLAG_NONE);

	/* for DVD+/-RW restricted */
	output = burner_caps_disc_new (media_dvd_rw_plus);
	burner_plugin_link_caps (plugin, output, input);
	g_slist_free (output);
	g_slist_free (input);

	BURNER_PLUGIN_ADD_STANDARD_DVDRW_RESTRICTED_FLAGS (plugin, BURNER_BURN_FLAG_NONE);
	BURNER_PLUGIN_ADD_STANDARD_DVDRW_PLUS_FLAGS (plugin, BURNER_BURN_FLAG_NONE);

	/* add blank caps */
	output = burner_caps_disc_new (BURNER_MEDIUM_CD|
					BURNER_MEDIUM_REWRITABLE|
					BURNER_MEDIUM_APPENDABLE|
					BURNER_MEDIUM_CLOSED|
					BURNER_MEDIUM_HAS_DATA|
					BURNER_MEDIUM_HAS_AUDIO|
					BURNER_MEDIUM_BLANK);
	burner_plugin_blank_caps (plugin, output);
	g_slist_free (output);

	output = burner_caps_disc_new (BURNER_MEDIUM_DVD|
					BURNER_MEDIUM_PLUS|
					BURNER_MEDIUM_SEQUENTIAL|
					BURNER_MEDIUM_RESTRICTED|
					BURNER_MEDIUM_REWRITABLE|
					BURNER_MEDIUM_APPENDABLE|
					BURNER_MEDIUM_CLOSED|
					BURNER_MEDIUM_HAS_DATA|
					BURNER_MEDIUM_UNFORMATTED|
				        BURNER_MEDIUM_BLANK);
	burner_plugin_blank_caps (plugin, output);
	g_slist_free (output);

	burner_plugin_set_blank_flags (plugin,
					BURNER_MEDIUM_CD|
					BURNER_MEDIUM_DVD|
					BURNER_MEDIUM_SEQUENTIAL|
					BURNER_MEDIUM_RESTRICTED|
					BURNER_MEDIUM_REWRITABLE|
					BURNER_MEDIUM_APPENDABLE|
					BURNER_MEDIUM_CLOSED|
					BURNER_MEDIUM_HAS_DATA|
					BURNER_MEDIUM_HAS_AUDIO|
					BURNER_MEDIUM_UNFORMATTED|
				        BURNER_MEDIUM_BLANK,
					BURNER_BURN_FLAG_NOGRACE|
					BURNER_BURN_FLAG_FAST_BLANK,
					BURNER_BURN_FLAG_NONE);

	/* no dummy mode for DVD+RW */
	burner_plugin_set_blank_flags (plugin,
					BURNER_MEDIUM_DVDRW_PLUS|
					BURNER_MEDIUM_APPENDABLE|
					BURNER_MEDIUM_CLOSED|
					BURNER_MEDIUM_HAS_DATA|
					BURNER_MEDIUM_UNFORMATTED|
				        BURNER_MEDIUM_BLANK,
					BURNER_BURN_FLAG_NOGRACE|
					BURNER_BURN_FLAG_FAST_BLANK,
					BURNER_BURN_FLAG_NONE);

	burner_plugin_register_group (plugin, _(LIBBURNIA_DESCRIPTION));
}
