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

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) version 3.

    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 "comm.h"
#include <Phonon/AbstractMediaStream>
#include <Phonon/MediaSource>
#include <Phonon/Global>
#include <QtCore/QUrl>

using Phonon::MediaSource;

QDataStream &operator<<(QDataStream &s, const Phonon::EffectParameter &p)
{
    quint32 h = 0;
    if (p.type() == QVariant::Bool) {
        h |= Phonon::EffectParameter::ToggledHint;
    }
    if (p.isLogarithmicControl()) {
        h |= Phonon::EffectParameter::LogarithmicHint;
    }
    if (p.type() == QVariant::Int) {
        h |= Phonon::EffectParameter::IntegerHint;
    }
    return s << p.id() << p.name() << h << p.defaultValue() << p.minimumValue() << p.maximumValue() << p.possibleValues() << p.description();
}

QDataStream &operator>>(QDataStream &s, Phonon::EffectParameter &p)
{
    int id;
    QString name, desc;
    quint32 hints;
    QVariant defaultValue, min, max;
    QVariantList values;
    s >> id >> name >> hints >> defaultValue >> min >> max >> values >> desc;
    p = Phonon::EffectParameter(id, name, static_cast<Phonon::EffectParameter::Hints>(hints), defaultValue, min, max, values, desc);
    return s;
}

QDataStream &operator<<(QDataStream &s, const Phonon::MediaSource &ms)
{
    s << static_cast<quint32>(ms.type());
    switch (ms.type()) {
    case MediaSource::Invalid:
        break;
    case MediaSource::LocalFile:
        s << ms.fileName();
        break;
    case MediaSource::Url:
        s << ms.url();
        break;
    case MediaSource::Disc:
        s << static_cast<quint32>(ms.discType()) << ms.deviceName();
        break;
    case MediaSource::Stream:
        s << QVariant::fromValue(static_cast<void *>(ms.stream()));
        break;
    }
    return s;
}

QDataStream &operator>>(QDataStream &s, Phonon::MediaSource &ms)
{
    quint32 type;
    s >> type;
    switch (static_cast<MediaSource::Type>(type)) {
    case MediaSource::Invalid:
        ms = MediaSource();
        break;
    case MediaSource::LocalFile:
        {
            QString fileName;
            s >> fileName;
            ms = MediaSource(fileName);
        }
        break;
    case MediaSource::Url:
        {
            QUrl url;
            s >> url;
            ms = MediaSource(url);
        }
        break;
    case MediaSource::Disc:
        {
            quint32 t;
            QString deviceName;
            s >> t >> deviceName;
            ms = MediaSource(static_cast<Phonon::DiscType>(t), deviceName);
        }
        break;
    case MediaSource::Stream:
        {
            QVariant v;
            s >> v;
            ms = MediaSource(reinterpret_cast<Phonon::AbstractMediaStream *>(qvariant_cast<void *>(v)));
        }
        break;
    }
    return s;
}
