/*
 *
 * 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 "osdmanager.h"

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


OSDManager::OSDManager(PluginFactory* pf, QWidget* widget)
    : _osd(0L),
      _widget(widget),
      _pf(pf)
{
    scanPlugins();
}

OSDManager::~OSDManager()
{
    if (_osd) _osd->destroy();
}

void OSDManager::scanPlugins()
{
    if (!_widget) {
        if (_osd) _osd->destroy();
        _osd = 0L;
        //        kDebug() << "OSDManager: Info: No screenwidget set. Cannot scan OSD plugins.";
        return;
    }

    QList<PluginDesc*>& osdPlugins(_pf->osdPlugins());

    bool hasOSD = false;
    for (QList<PluginDesc*>::iterator it = osdPlugins.begin(); it != osdPlugins.end(); ++it) {
        kDebug() << "Found a plugin: " << (*it)->name;
        if ((*it)->enabled) {
            if (_osd) {
                if (_osd->pluginDescription() == (*it)) {
                    hasOSD = true;
                    break;
                } else {
                    _osd->destroy();
                    _osd = 0;
                }
            }
            _osd = _pf->getOSDPlugin((*it), _widget);
            if (_osd) {       // accept the first plugin we find
                connect(this, SIGNAL(colourKeyChanged(QColor)),
                        _osd, SLOT(colourKeyChanged(QColor)));
                hasOSD = true;
                break;
            }
        }
        else
            kDebug() << "This plugin is disabled";
    }

    // if no enabled OSD found then delete old
    if (!hasOSD && _osd) {
        _osd->destroy();
        _osd = 0;
    }
}

void OSDManager::displayMisc(const QString& text)
{
    if (_osd)
        _osd->displayMisc(text);
}

void OSDManager::displayChannel(int channel, const QString& name)
{
    if (_osd)
        _osd->displayChannel(channel, name);
}

void OSDManager::displayMuted(bool muted)
{
    if (_osd)
        _osd->displayMuted(muted);
}

void OSDManager::displayVolume(int vol)
{
    if (_osd)
        _osd->displayVolume(vol);
}

void OSDManager::displayVolume(int left, int right)
{
    displayVolume((left + right) / 2);
}

void OSDManager::displayCC(const QString& text)
{
    if (_osd)
        _osd->displayCC(text);
}

QColor OSDManager::colourKey() const
{
    return _colourKey;
}

void OSDManager::setColourKey(QColor key)
{
    _colourKey = key;
    emit colourKeyChanged(key);
}

void OSDManager::setScreen(QWidget* screen)
{
    _widget = screen;
    scanPlugins();
}

#include "osdmanager.moc"

// vim: sw=4 ts=4
