/***************************************************************************
 * part.cpp
 * This file is part of the KDE project
 * copyright (C)2006 by Sebastian Sauer (mail@dipe.org)
 *
 * This program 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 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
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "part.h"
#include "module.h"

#include <QVBoxLayout>
#include <QLabel>
#include <QAction>

#include <kdebug.h>
#include <kgenericfactory.h>
#include <kaction.h>
#include <kactioncollection.h>
#include <kmenu.h>
#include <kmessagebox.h>
//#include <kcmdlineargs.h>
//#include <kurl.h>

typedef KGenericFactory< Part > KrossModuleTutorialFactory;
K_EXPORT_COMPONENT_FACTORY( krossmoduletutorial, KrossModuleTutorialFactory( "krossmoduletutorial" ) )

Part::Part(QWidget* widget, QObject* parent, const QStringList&)
    : KParts::ReadWritePart(parent)
    , m_guiclient( new Kross::GUIClient(this, this) )
    , m_action(0)
{
    kDebug() << "Part::Part className=" << metaObject()->className() << " parent.className=" << (parent ? parent->metaObject()->className() : "NULL") << endl;
    QWidget* w = new QWidget(widget);
    w->setLayout( new QVBoxLayout(w) );
    setWidget(w);
    //w->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));

    //m_guiclient->setXMLFile(locate("data","krosstutorialplugins/scripting.rc"), true);

    KAction* execaction = new KAction(i18n("Execute Script File..."), actionCollection(), "executescriptfile");
    connect(execaction, SIGNAL(triggered(bool)), m_guiclient, SLOT(executeFile()));
    KAction* manageraction = new KAction(i18n("Script Manager..."), actionCollection(), "configurescripts");
    connect(manageraction, SIGNAL(triggered(bool)), m_guiclient, SLOT(showManager()));
    KAction* scriptmenuaction = m_guiclient->action("scripts");
    actionCollection()->insert(scriptmenuaction);

    Module* module = dynamic_cast< Module* >( Kross::Manager::self().object("KrossModuleTutorial") );
    if( ! module ) {
        module = new Module(this);
        Kross::Manager::self().addObject(module, "KrossModuleTutorial");
    }
}

void Part::closeFile()
{
    delete m_action; m_action = 0;
    if( widget() && widget()->layout() ) {
        QLayoutItem *i;
        while( (i = widget()->layout()->takeAt(0)) != 0 )
            if(i) delete i->widget();
    }
}

bool Part::openFile()
{
    kDebug() << "Part::openFile() file=" << m_file << endl;
    closeFile();
    if( ! m_file.isEmpty() ) {
        m_action = new Kross::Action(m_file);
        connect(m_action, SIGNAL(started(Kross::Action*)), SLOT(slotStarted(Kross::Action*)));
        connect(m_action, SIGNAL(finished(Kross::Action*)), SLOT(slotFinished(Kross::Action*)));
        m_action->trigger();
    }
    return true;
}

bool Part::openUrl(const KUrl &url)
{
    closeFile();
    return KParts::ReadWritePart::openUrl(url);
}

bool Part::saveFile()
{
    kDebug()<<"Part::saveFile()"<<endl;
    //TODO
    return false;
}

void Part::slotStarted(Kross::Action*)
{
    kDebug()<<"Part::slotStarted()"<<endl;
}

void Part::slotFinished(Kross::Action*)
{
    kDebug()<<"Part::slotFinished()"<<endl;
    if( m_action && m_action->hadError() ) {
        const QString err = m_action->errorMessage();
        const QString trace = m_action->errorTrace();
        int lineno = m_action->errorLineNo();
        closeFile();
        if( widget() && widget()->layout() ) {
            QLabel* errlabel = new QLabel( widget() );
            errlabel->setWordWrap(true);
            errlabel->setText( QString("Error in line %1:<br>%2<br>%3").arg(lineno).arg(err).arg(trace) );
            if( widget()->layout() )
                widget()->layout()->addWidget(errlabel);
        }
    }
}

#include "part.moc"
