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

#include <QCloseEvent>
#include <QKeySequence>
#include <klocale.h>
#include <kmenu.h>
#include <kmenubar.h>
#include <kstandarddirs.h>
#include <klibloader.h>
#include <kurl.h>
#include <kfiledialog.h>
#include <kapplication.h>

#include <kross/core/manager.h>
#include <kross/core/interpreter.h>

MainWindow::MainWindow()
    : KParts::MainWindow()
{
    QMenu *filemenu = menuBar()->addMenu( i18n("File") );
    filemenu->addAction( i18n("Open..."), this, SLOT(slotFileOpen()), QKeySequence(Qt::CTRL + Qt::Key_O) );
    m_reopenmenu = filemenu->addMenu( i18n("Reopen") );
    connect(m_reopenmenu, SIGNAL(triggered(QAction*)), this, SLOT(slotFileReopen(QAction*)));
    filemenu->addAction( i18n("Reload"), this, SLOT(slotFileReload()), QKeySequence(Qt::Key_F5) );
    filemenu->addAction( i18n("Close"), this, SLOT(slotFileClose()), QKeySequence(Qt::CTRL + Qt::Key_W) );

    KLibFactory* factory = KLibLoader::self()->factory("krossmoduletutorial");
    Q_ASSERT(factory);
    m_part = dynamic_cast<KParts::ReadWritePart*>( factory->create(this) );
    Q_ASSERT(m_part);

    QMenu *toolsmenu = menuBar()->addMenu( i18n("Tools") );
    toolsmenu->addAction( m_part->action("executescriptfile") );
    toolsmenu->addAction( m_part->action("configurescripts") );
    toolsmenu->addAction( m_part->action("scripts") );

    setCentralWidget( m_part->widget() );
    setMinimumSize(560,380);

    readProperties( KApplication::kApplication()->sessionConfig() );
}

MainWindow::~MainWindow()
{
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    saveProperties( KApplication::kApplication()->sessionConfig() );
    event->accept();
}

void MainWindow::readProperties(KConfig* config)
{
    m_reopenmenu->clear();
    foreach(QString s, config->readEntry("reopen", QStringList())) {
        QAction* a = m_reopenmenu->addAction(s);
        a->setData(s);
    }
    m_reopenmenu->setEnabled( m_reopenmenu->actions().count() > 0 );
}

void MainWindow::saveProperties(KConfig* config)
{
    QStringList list;
    for(int i = 0; i < 10 && i < m_reopenmenu->actions().count(); ++i)
        list << m_reopenmenu->actions()[i]->data().toString();
    config->writeEntry("reopen", list);
    config->sync();
}

void MainWindow::slotFileOpen()
{
    QStringList mimetypes;
    foreach(QString interpretername, Kross::Manager::self().interpreters()) {
        Kross::InterpreterInfo* info = Kross::Manager::self().interpreterInfo(interpretername);
        mimetypes.append( info->mimeTypes().join(" ").trimmed() );
    }
    KFileDialog dlg(KUrl("kfiledialog:///KrossTutorial"),mimetypes.join(" "),0,0);
    dlg.setCaption( i18n("Open...") );
    dlg.setOperationMode( KFileDialog::Opening );
    dlg.setMode( KFile::File | KFile::ExistingOnly /* | KFile::LocalOnly */ );
    if( dlg.exec() ) {
        KUrl url = dlg.selectedUrl();
        if( m_part->openUrl(url) ) {
            QAction* a = new QAction(url.url(), this);
            a->setData(url.url());
            m_reopenmenu->insertAction(m_reopenmenu->actions().count() > 0 ? m_reopenmenu->actions()[0] : 0, a);
            m_reopenmenu->setEnabled(true);
        }
    }
}

void MainWindow::slotFileReopen(QAction* action)
{
    slotFileClose();
    m_part->openUrl( KUrl(action->data().toString()) );
}

void MainWindow::slotFileReload()
{
    m_part->openUrl( m_part->url() );
}

void MainWindow::slotFileClose()
{
    m_part->openUrl( KUrl() );
}

#include "mainwindow.moc"
