/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 8 -*- */
%%
headers
#define NO_IMPORT_PYGOBJECT
#include "pygobject.h"
#include <nautilus-burn.h>
/* #include "nb_typebuiltins.h" */
#include "nb_track.h"

#include "config.h"

%%
modulename nautilusburn
%%
import gtk.ComboBox as PyGtkComboBox_Type
import gobject.GObject as PyGObject_Type
%%
ignore-glob
  *_get_type
%%
ignore
  nautilus_burn_drive_ref
  nautilus_burn_drive_unref
  nautilus_burn_recorder_error_quark
%%
override nautilus_burn_recorder_write_tracks

static PyObject *
_wrap_nautilus_burn_recorder_write_tracks (PyGObject *self, PyObject *args, PyObject *kwargs)
{
    PyObject *drive;
    nb_Track *track;
    PyObject *tracks;
    GList *g_tracks = NULL;
    GError *error = NULL;

    int speed, flags, i, len, ret;

    if (!PyArg_ParseTuple(args, "O!Oii", &PyNautilusBurnDrive_Type, &drive, &tracks, &speed, &flags))
        return NULL;
    if (!PyList_Check(tracks)) {
        PyErr_SetString(PyExc_TypeError, "second parameter (tracks) must be a list");
        return NULL;
    }

    len = PyList_GET_SIZE(tracks);
    for (i = 0; i < len; i++) {
        track = (nb_Track *)PyList_GET_ITEM(tracks, i);
        if (!nb_AudioTrack_Check((PyObject*)track) && !nb_DataTrack_Check((PyObject*)track)) {
            PyErr_SetString(PyExc_TypeError, "Elements must be "
                            "nautilusburn.AudioTrack or "
                            "nautilusburn.DataTrack");
            g_list_free(g_tracks);
            return NULL;
        }
        g_tracks = g_list_append(g_tracks, &track->track);
    }
    
    pyg_begin_allow_threads;
    ret = nautilus_burn_recorder_write_tracks((NautilusBurnRecorder*)self->obj,
                                              NAUTILUS_BURN_DRIVE(pygobject_get(drive)), 
                                              g_tracks, 
                                              speed, 
                                              flags, &error);
    pyg_end_allow_threads;
    
    g_list_free(g_tracks);
    if (pyg_error_check(&error))
        return NULL;
    return PyInt_FromLong(ret);
}

%%
override nautilus_burn_drive_monitor_get_drives noargs

static PyObject *
_wrap_nautilus_burn_drive_monitor_get_drives(PyObject *self, PyObject *args, PyObject *kwargs)
{
	int index, len;
	GList *cds, *iter;
	PyObject *cds_tuple, *cd;
	
	cds = nautilus_burn_drive_monitor_get_drives(
            NAUTILUS_BURN_DRIVE_MONITOR(pygobject_get(self)));
	len = g_list_length (cds);
	cds_tuple = PyTuple_New (len);
	
	for (iter = g_list_first (cds), index = 0; iter; iter = iter->next, index++) {
		/* create the new Drive object */
		assert (iter->data);
		cd = pygobject_new((GObject *) iter->data);
		if (!cd) {
			return NULL;
		}
		if (PyTuple_SetItem (cds_tuple, index, cd)) {
			return NULL;
		}
                g_object_unref((GObject *) iter->data);
	}

	g_list_free (cds);
	return cds_tuple;
}

%%
define bytes_to_seconds
static PyObject *
_wrap_bytes_to_seconds (PyObject *self, PyObject *args)
{
	long int size;
	if (!PyArg_ParseTuple (args, "l", &size))
		return NULL;
	return Py_BuildValue("l", (long int) NAUTILUS_BURN_DRIVE_SIZE_TO_TIME(size));
}

%%
override nautilus_burn_drive_lock kwargs
static PyObject *
_wrap_nautilus_burn_drive_lock(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "reason", NULL };
    char *reason, *failure = NULL;
    int ret;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,"s:NautilusBurnDrive.lock", kwlist, &reason))
        return NULL;
    
    ret = nautilus_burn_drive_lock(NAUTILUS_BURN_DRIVE(self->obj), reason, &failure);
    if (failure) {
        PyErr_SetString(PyExc_RuntimeError, failure);
        return NULL;
    }
    return PyBool_FromLong(ret);
}

%%
override nautilus_burn_drive_get_write_speeds noargs
static PyObject *
_wrap_nautilus_burn_drive_get_write_speeds(PyGObject *self)
{
    const int *write_speeds;
    int i;
    PyObject *py_write_speeds;

    write_speeds = nautilus_burn_drive_get_write_speeds(NAUTILUS_BURN_DRIVE(self->obj));
    py_write_speeds = PyList_New(0);
    for (i = 0; write_speeds[i]; ++i) {
        PyObject *speed = PyInt_FromLong(write_speeds[i]);
        PyList_Append(py_write_speeds, speed);
        Py_DECREF(speed);
    }
    return py_write_speeds;
}

%%
override nautilus_burn_drive_get_media_type_full noargs
static PyObject *
_wrap_nautilus_burn_drive_get_media_type_full(PyGObject *self)
{
    gint ret;
    gboolean is_rewritable, is_blank, has_data, has_audio;
    
    ret = nautilus_burn_drive_get_media_type_full(NAUTILUS_BURN_DRIVE(self->obj),
                                                  &is_rewritable, &is_blank,
                                                  &has_data, &has_audio);
    
    return Py_BuildValue("Niiii", pyg_enum_from_gtype(G_TYPE_NONE, ret),
                         is_rewritable, is_blank, has_data, has_audio);
}

%%
override nautilus_burn_drive_monitor_get_recorder_drives noargs
static PyObject *
_wrap_nautilus_burn_drive_monitor_get_recorder_drives(PyGObject *self)
{
    GList *drives, *l;
    PyObject *py_drives;

    drives = nautilus_burn_drive_monitor_get_recorder_drives(NAUTILUS_BURN_DRIVE_MONITOR(self->obj));
    py_drives = PyList_New(0);
    for (l = drives; l; l = l->next) {
        GObject *drive = G_OBJECT(l->data);
        PyObject *py_drive = pygobject_new(drive);
        PyList_Append(py_drives, py_drive);
        Py_DECREF(py_drive);
        g_object_unref(drive);
    }
    g_list_free(drives);
    return py_drives;
}

