/*  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 "mixerlist.h"
#include "mixerlist_p.h"

#include "alsaclasses.h"
#include "mixer.h"

#include <kdebug.h>
#include <kglobal.h>
#include <Phonon/BackendCapabilities>
#include "defs_p.h"

namespace Phonon
{
namespace Mixer
{

K_GLOBAL_STATIC(MixerListSharedData, globalMixerListData)

const QObject *MixerList::notifier()
{
    return globalMixerListData;
}

MixerList::MixerList()
    : d_ptr(new MixerListPrivate)
{
    d_ptr->q_ptr = this;
}

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

MixerList::MixerList(const MixerList &)
    : d_ptr(new MixerListPrivate)
{
    d_ptr->q_ptr = this;
}

MixerList &MixerList::operator=(const MixerList &)
{
    return *this;
}

QList<Mixer> MixerList::availableMixers()
{
    globalMixerListData->updateMixerList();
    return globalMixerListData->m_mixerList;
}

MixerListSharedData::MixerListSharedData()
{
    // call availableAudioOutputDevices to make sure the backend is initialized and notifies us of
    // device changes
    Phonon::BackendCapabilities::availableAudioOutputDevices();
    connect(Phonon::BackendCapabilities::notifier(), SIGNAL(availableAudioOutputDevicesChanged()), SLOT(availableAudioOutputDevicesChanged()));
}

void MixerListSharedData::availableAudioOutputDevicesChanged()
{
    QList<Mixer> oldlist = m_mixerList;
    updateMixerList();
    if (oldlist != m_mixerList) {
        QList<Mixer> added = m_mixerList;
        foreach (const Mixer &m, oldlist) {
            const int idx = added.indexOf(m);
            if (idx == -1) {
                kDebug() << "mixerRemoved" << m.name();
                emit mixerRemoved(m);
            } else {
                added.removeAt(idx);
            }
        }
        foreach (const Mixer &m, added) {
            kDebug() << "mixerAdded" << m.name();
            emit mixerAdded(m);
        }
    }
}

void MixerListSharedData::updateMixerList()
{
    QList<Mixer> l;
    for (int card = AlsaCard::first(); card >= 0; card = AlsaCard::next(card)) {
        kDebug() << card << AlsaCard::nameFor(card) << AlsaCard::longnameFor(card);
        Mixer m("hw:" + QByteArray::number(card));
        Q_ASSERT(m.isValid());
        l << m;
    }
    m_mixerList = l;
}

} // namespace Mixer
} // namespace Phonon
// vim: sw=4 sts=4 et tw=100
