/*
 *
 * Copyright (C) 2002 George Staikos <staikos@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 as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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 Steet, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */


#include "audiomanager.h"

#include "kdetvmixerplugin.h"
#include "pluginfactory.h"
#include <kdebug.h>
#include <QList>

AudioManager::AudioManager(PluginFactory *pf)
{
	_mixer = 0;
	_pf = pf;
	scanPlugins();
}

void AudioManager::rescanPlugins()
{
	scanPlugins();
}

void AudioManager::scanPlugins()
{
	QList<PluginDesc*>& mixerPlugins(_pf->mixerPlugins());

	bool hasMixer = false;
	for (QList<PluginDesc*>::iterator it = mixerPlugins.begin();
         it != mixerPlugins.end(); ++it) {
		kDebug() << "AudioManager: Found Mixer plugin:";
		kDebug() << (*it)->name;
		kDebug() << (*it)->author;
		kDebug() << (*it)->comment;
		if ((*it)->enabled) {
			if (_mixer) {
				if (_mixer->pluginDescription() == (*it)) {
					hasMixer = true;
					break;
				} else {
					_mixer->destroy();
					_mixer = 0;
				}
			}
			KdetvMixerPlugin *f = _pf->getMixerPlugin((*it));

			if (f) {
				_mixer = f;
				hasMixer = true;
				break;
			}
		} else {
		    kDebug() << "AudioManager: This plugin is disabled";
    }
	}

	// if no enabled mixer found then delete old
	if (!hasMixer && _mixer) {
        _mixer->destroy();
		_mixer = 0;
	}

	emit mixerAvailable(hasMixer);
}


AudioManager::~AudioManager()
{
    if (_mixer)
        _mixer->destroy();
}

int AudioManager::setVolume(int left, int right)
{
	if (!_mixer)
		return -1;

	return _mixer->setVolume(left, right);
}

int AudioManager::setMuted(bool mute)
{
	if (!_mixer)
		return -1;

	return _mixer->setMuted(mute);
}

KdetvMixerPlugin *AudioManager::plugin()
{
    return _mixer;
}


#include "audiomanager.moc"

// vim: ts=4 sw=4
