/*  This file is part of the KDE project
    Copyright (C) 2004-2006 Matthias Kretz <kretz@kde.org>
    Copyright (C) 2006-2007 Tim Beaulen <tbscope@gmail.com>

    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.
*/

#ifndef PHONON_GSTREAMER_BACKEND_PRIVATE_H
#define PHONON_GSTREAMER_BACKEND_PRIVATE_H

#include "qbtgstreamer/qbtgstreamerpluginfeature.h"
#include "qbtgstreamer/qbtgstreamerregistry.h"
#include "qbtgstreamer/qbtgstreamerdatapointer.h"
#include "qbtgstreamer/qbtgstreamerelementfactory.h"
#include "qbtgstreamer/qbtgstreamerpipeline.h"
#include "qbtgstreamer/qbtgstreamermessage.h"

#include "mediaobject.h"

#include <phonon/audiodevice.h>
#include <phonon/audiodeviceenumerator.h>

#include <QString>
#include <QPointer>
#include <QSet>
#include <QDomDocument>
#include <QList>
#include <QStringList>

#include <kconfiggroup.h>

namespace Phonon
{
namespace GStreamer
{

class findFeature : public QbtGStreamerRegistry
{
    public:
        findFeature(const QString &feature);
        ~findFeature();

        virtual bool featureFilter(QbtGStreamerPluginFeature *feature, QbtGStreamerDataPointer *data);

    private:
        QString m_feature;
};

class GstBackendPrivate: public QObject
{
    Q_OBJECT

    public:
        GstBackendPrivate();
        ~GstBackendPrivate();

        void initBackend(const QString &name);
        void addMediaObject(QPointer<MediaObject> object);

        KSharedConfigPtr config();
        void setConfig(const KSharedConfigPtr& newConfig);

        QSet<int> audioOutputIndexes();
        QString audioOutputName(int audioDevice);
        QString audioOutputDescription(int audioDevice);
        QString audioOutputIcon(int audioDevice);
        bool audioOutputAvailable(int audioDevice);

        QbtGStreamerPipeline *gstreamerPipeline();

        // Debug functions
        void debugmessage(int type, int lever, const QString &objname, const QString &message);

    private slots:
        void slotBusMessage(QbtGStreamerMessage* msg, QbtGStreamerDataPointer* data);

    private:
        void rebuildXmlStructure();
        void findParentProgramDetails();
        void checkAudioOutputs();
        QDomDocument xmlStructure;
        QList<QPointer<MediaObject> > mediaObjects;
        struct AudioOutputInfo
        {
            AudioOutputInfo(int idx, const QString &n, const QString &desc, const QString &ic, const QString &dr, const QStringList &dev)
                : available(false), index(idx), name(n), description(desc), icon(ic), driver(dr), devices(dev)
            {}
            bool available;
            int index;
            QString name;
            QString description;
            QString icon;
            QString driver;
            QStringList devices;
            bool operator==(const AudioOutputInfo &rhs) { return name == rhs.name && driver == rhs.driver; }
        };
        QList<AudioOutputInfo> m_audioOutputInfos;
        void addAudioOutput(AudioDevice dev, QString driver);
        void addAudioOutput(int index, const QString &name, const QString &description, const QString &icon, const QString &driver, const QStringList &deviceIds);
        
        QString backendName;
        QString backendPid;
        QString backendParentProgramName;
        KSharedConfigPtr m_config;

        QbtGStreamerPipeline *m_pipeline;
        QbtGStreamerBus *m_bus;
};

}} // Phonon::GStreamer

#endif //PHONON_GSTREAMER_BACKEND_PRIVATE_H
