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

#include "kdetvmiscplugin.h"
#include "pluginfactory.h"
#include <kdebug.h>

MiscManager::MiscManager(PluginFactory *pf, QWidget* view)
    : _view(view),
      _pf(pf)
{
    scanPlugins();
}

MiscManager::~MiscManager()
{
    for (QList<KdetvMiscPlugin*>::iterator it = _plugs.begin(); it != _plugs.end(); it++)
        delete(*it);
    _plugs.clear();
}

void MiscManager::scanPlugins()
{
    if (!_view) {
        //        kDebug() << "MiscManager: Info: No screenwidget set. Cannot scan Misc plugins.";
        return;
    }

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

    for (QList<PluginDesc*>::iterator plug = miscPlugins.begin();
         plug != miscPlugins.end(); ++plug) {
        bool skip = false;
        for (QList<KdetvMiscPlugin*>::iterator p = _plugs.begin(); p != _plugs.end(); ++p) {
            if ((*p)->pluginDescription() == (*plug)) {
                if (!(*plug)->enabled) {
                    delete(*p);
                    p = _plugs.erase(p);
                }
                skip = true;
                break;
            }
        }
	
        if (skip)
            continue;

        kDebug() << "Found a plugin: " << (*plug)->name;
        if ((*plug)->enabled) {
            KdetvMiscPlugin *p = _pf->getMiscPlugin((*plug), _view);
            if (p)
                _plugs.append(p);
        } else
            kDebug() << "The plugin is disabled";
    }
}

bool MiscManager::filterNumberKey(int key)
{
    for (QList<KdetvMiscPlugin*>::iterator it = _plugs.begin(); it != _plugs.end(); ++it)
        if ((*it)->filterNumberKey(key))
            return true;
    return false;
}

void MiscManager::setScreen(QWidget* screen)
{
    _view = screen;
    scanPlugins();
}

#include "miscmanager.moc"

// vim: sw=4 ts=4
