/*  This file is part of the KDE project
    Copyright (C) 2006-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.

*/

#ifndef PROXYBACKEND_BACKEND_H
#define PROXYBACKEND_BACKEND_H

#include <QtCore/QDataStream>
#include <QtCore/QObject>

#include <Phonon/ObjectDescription>
#include <Phonon/BackendInterface>

#include "proxyobject.h"
class QProcess;

namespace Phonon
{
namespace Proxy
{

class Backend : public QObject, public BackendInterface, public ProxyObject
{
    Q_OBJECT
    Q_INTERFACES(Phonon::BackendInterface)
    public:
        Backend(QObject *parent, const QVariantList &args);
        ~Backend();

        static inline Backend *instance() { return s_instance; }
        static QDataStream &_createProxiedObject(BackendInterface::Class, ProxyObject *, const QVariantList &);
        static QDataStream &_send(FunctionId func, void *o);
        static QDataStream &_receive(quint32 func);
        static void addProxyObject(ProxyObject *);
        static void removeProxyObject(ProxyObject *);
        void handleSignal(SignalId, QDataStream &) const;

        QObject *createObject(BackendInterface::Class, QObject *parent, const QList<QVariant> &args);

        QList<int> objectDescriptionIndexes(ObjectDescriptionType) const;
        QHash<QByteArray, QVariant> objectDescriptionProperties(ObjectDescriptionType, int) const;

        bool startConnectionChange(QSet<QObject *>);
        bool connectNodes(QObject *, QObject *);
        bool disconnectNodes(QObject *, QObject *);
        bool endConnectionChange(QSet<QObject *>);

        QStringList availableMimeTypes() const;

        QObject *qObject() { return this; }

    signals:
        void objectDescriptionChanged(ObjectDescriptionType) const;

    protected:
        bool event(QEvent *e);

    private slots:
        void readyRead();

    private:
        static Backend *s_instance;
        QProcess *m_backendProcess;
        QDataStream m_stream;
        quint32 m_lastFunction;
        QSet<ProxyObject *> m_validObjects;
};

inline QDataStream &Backend::_createProxiedObject(BackendInterface::Class c, ProxyObject *o, const QVariantList &args)
{
    return s_instance->m_stream << static_cast<quint32>(BackendCreateObject) << o << c << args;
}

inline QDataStream &Backend::_send(FunctionId func, void *o)
{
    return s_instance->m_stream << static_cast<quint32>(func) << o;
}

inline void Backend::addProxyObject(ProxyObject *o)
{
    s_instance->m_validObjects.insert(o);
}

inline void Backend::removeProxyObject(ProxyObject *o)
{
    s_instance->m_validObjects.remove(o);
}

}} // namespace Phonon::Proxy

#endif // PROXYBACKEND_BACKEND_H
