/***************************************************************************
 *   Copyright (C) 2008 by S. MANKOWSKI / G. DE BURE support@mankowski.fr  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>  *
 ***************************************************************************/
/** @file
* This file defines classes SKGAdvicesDataEngine.
*
* @author Stephane MANKOWSKI / Guillaume DE BURE
 */

#include "skgadvicesdataengine.h"
#include "skgmainpanel.h"
#include "skginterfaceplugin.h"

SKGAdvicesDataEngine::SKGAdvicesDataEngine(QObject* parent, const QVariantList& args): DataEngine(parent, args)
{}

SKGAdvicesDataEngine::~SKGAdvicesDataEngine()
{}

void SKGAdvicesDataEngine::init()
{
    Plasma::DataEngine::init();

    // Get the document behind the current document
    SKGMainPanel* panel = SKGMainPanel::getMainPanel();
    if (panel) document = (panel->getDocument());

    // Whenever a table is modified, update sources
    connect(document, SIGNAL(tableModified(QString,int)), this, SLOT(updateAllSources()));
}


bool SKGAdvicesDataEngine::updateSourceEvent(const QString& source)
{

    if (source != "skrooge") {
        return false;
    }

    //Get list of ignored advices
    QString currentMonth = QDate::currentDate().toString("yyyy-MM");
    QStringList ignoredAdvices = document->getParameters("advices", "t_value='I' OR t_value='I_" % currentMonth % '\'');

    //Build the list of all advices by requesting all plugins
    int index = 0;
    while (index >= 0) {

        SKGInterfacePlugin* plugin = SKGMainPanel::getMainPanel()->getPluginByIndex(index);

        if (plugin) {

            foreach(const SKGAdvice & advice, plugin->advices()) {

                if (!ignoredAdvices.contains(advice.getUUID()) && !ignoredAdvices.contains(SKGServices::splitCSVLine(advice.getUUID(), '|').at(0))) {

                    QVariantHash values;
                    values["priority"] = SKGServices::intToString(advice.getPriority());
                    values["shortMessage"] = advice.getShortMessage();
                    values["longMessage"] = advice.getLongMessage();
                    values["correction"] = advice.getAutoCorrections();

                    setData("skrooge", advice.getUUID(), values);

                }
            }
        } else index = -2;
        ++index;
    }

    return true;
}
