/*  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 <QtGui/QApplication>
#include <QtCore/QFile>
#include <QtCore/QDataStream>
#include <QtCore/QHash>
#include <Phonon/BackendInterface>

#include "comm.h"

namespace Phonon
{
namespace Proxy
{

class ProxyApp : public QApplication
{
    Q_OBJECT
    public:
        ProxyApp(int argc, char **argv);

    private slots:
        void readyRead();

    private:
        QFile m_inputDevice;
        QFile m_outputDevice;
        QDataStream m_inputStream;
        QDataStream m_outputStream;
        QHash<void *, QObject *> m_backendObjects;
        Phonon::BackendInterface *m_backendIface;
};

ProxyApp::ProxyApp(int argc, char **argv)
    : QApplication(argc, argv),
    m_backendIface(0)
{
    m_inputDevice.open(stdin, QIODevice::ReadOnly);
    m_outputDevice.open(stdout, QIODevice::WriteOnly);
    m_inputStream.setDevice(&m_inputDevice);
    m_outputStream.setDevice(&m_outputDevice);
    connect(&m_inputDevice, SIGNAL(readyRead()), SLOT(readyRead()));
}

void ProxyApp::readyRead()
{
    quint32 fId = 0;
    m_inputStream >> fId;
    switch (static_cast<FunctionId>(fId)) {
    case BackendCreateObject:
        {
            void *proxyObject = 0;
            quint32 c;
            QVariantList args;
            m_inputStream >> proxyObject >> c >> args;
            Q_ASSERT(proxyObject);
            QObject *newObject = m_backendIface->createObject(static_cast<BackendInterface::Class>(c), 0, args);
            m_backendObjects.insert(proxyObject, newObject);
            m_outputStream << fId << newObject;
        }
        break;
    }
}

} // namespace Proxy
} // namespace Phonon

int main(int argc, char **argv)
{
    Phonon::Proxy::ProxyApp app(argc, argv);
    return app.exec();
}

#include "proxyapp.moc"
