/*  This file is part of the KDE project
    Copyright (C) 2007 Matthias Kretz <kretz@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library 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 Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#include "pcmiodevice.h"
#include "pcmiodevice_p.h"

namespace Phonon
{

PcmIODevice::PcmIODevice(QObject *parent)
    : QIODevice(parent),
    d_ptr(new PcmIODevicePrivate)
{
    d_ptr->q_ptr = this;
}

PcmIODevice::~PcmIODevice()
{
    delete d_ptr;
}

void PcmIODevice::setOutputDevice(const Phonon::AudioOutputDevice &outDevice)
{
    Q_D(PcmIODevice);
    d->outputDevice = outDevice;
    d->updateDevices();
}
/*
void PcmIODevice::setCaptureDevice(const Phonon::AudioCaptureDevice &inDevice)
{
    Q_D(PcmIODevice);
    d->inputDevice = inDevice;
    d->updateDevices();
}
*/
AudioOutputDevice PcmIODevice::outputDevice() const
{
    return d_func()->outputDevice;
}
/*
AudioCaptureDevice PcmIODevice::captureDevice() const
{
    return d_func()->inputDevice;
}
*/
void PcmIODevice::setCategory(Phonon::Category c)
{
    Q_D(PcmIODevice);
    d->category = c;
    d->updateDevices();
}

Phonon::Category PcmIODevice::category() const
{
    return d_func()->category;
}

void PcmIODevice::setFormat(int sampleRate, BitRate bitRate, int channels, QSysInfo::Endian byteOrder)
{
    Q_D(PcmIODevice);
    d->formats.clear();
    Format f(sampleRate, bitRate, channels, byteOrder);
    d->formats.insert(f);
}

void PcmIODevice::addFormat(int sampleRate, BitRate bitRate, int channels, QSysInfo::Endian byteOrder)
{
    Q_D(PcmIODevice);
    Format f(sampleRate, bitRate, channels, byteOrder);
    d->formats.insert(f);
}

int PcmIODevice::sampleRate() const
{
    return d_func()->formatInUse.sampleRate;
}

PcmIODevice::BitRate PcmIODevice::bitRate() const
{
    return d_func()->formatInUse.bitRate;
}

int PcmIODevice::channels() const
{
    return d_func()->formatInUse.channels;
}

QSysInfo::Endian PcmIODevice::byteOrder() const
{
    return d_func()->formatInUse.byteOrder;
}

PcmIODevice::PcmIOMode PcmIODevice::mode() const
{
    return d_func()->mode;
}

bool PcmIODevice::open(QIODevice::OpenMode openMode)
{
    return open(openMode, Relaxed);
}

bool PcmIODevice::open(QIODevice::OpenMode openMode, PcmIOMode hints)
{
    Q_D(PcmIODevice);
    d->mode = hints;
    if (d->open(openMode)) {
        return QIODevice::open(openMode & 0x03);
    }
    return false;
}

void PcmIODevice::close()
{
    if (isOpen()) {
        d_func()->close();
    }
}

qint64 PcmIODevice::readData(char *data, qint64 maxSize)
{
    return d_func()->readData(data, maxSize);
}

qint64 PcmIODevice::writeData(const char *data, qint64 maxSize)
{
    return d_func()->writeData(data, maxSize);
}

} // namespace Phonon
