/***************************************************************************
                           filtermanager.cpp
                           -----------------
    begin                : Sun Jun 13 2004
    copyright            : (C) 2004 by Dirk Ziegelmeier
    email                : dziegel@gmx.de
 ***************************************************************************/

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

#include "filtermanager.h"
#include "kdetvfilterplugin.h"
#include "pluginfactory.h"
#include "kdetvvideo/kdetvimagefilter.h"
#include <QList>

FilterManager::FilterManager(PluginFactory* pf)
    : QObject(),
      _pf(pf)
{
    _chain = new KdetvImageFilterChain();
    _plugs.setAutoDelete(false);
    scanPlugins();
}

FilterManager::~FilterManager()
{
    clear();
    delete _chain;
}

void FilterManager::clear()
{
    _chain->clear();

    while (KdetvFilterPlugin *p = _plugs.first()) {
        p->destroy();
        _plugs.remove(p);
    }
}

void FilterManager::scanPlugins()
{
    clear();

    QList<PluginDesc*>& filterPlugins(_pf->filterPlugins());
    for (QList<PluginDesc*>::iterator it = filterPlugins.begin();
         it != filterPlugins.end(); ++it) {
        if ((*it)->enabled) {
            KdetvFilterPlugin* p = _pf->getFilterPlugin((*it));
#warning "kde4: fix crash here"
            if(p) {
            _plugs.append(p);
            _chain->append(p->getFilter());
                }
            break; // There must be only one enabled filter plugin
        }
    }

    QList<PluginDesc*>& postProcessPlugins(_pf->postProcessPlugins());
    for (QList<PluginDesc*>::iterator it = postProcessPlugins.begin();
         it != postProcessPlugins.end(); ++it) {
        if ((*it)->enabled) {
            KdetvFilterPlugin* p = _pf->getPostProcessPlugin((*it));
            _plugs.append(p);
            _chain->append(p->getFilter());
        }
    }
}

#include "filtermanager.moc"

// vim: sw=4 ts=4 noet
