/*
    Copyright (c) 2008 Louai Al-Khanji

    This file is part of Safra.

    Safra is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2 as published
    by the Free Software Foundation.

    Safra 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 Safra; see the file COPYING. If not, see
    <http://www.gnu.org/licenses/>.
*/

#include <QCloseEvent>

#include <KLineEdit>
#include <KLocale>
#include <KAction>
#include <KIcon>
#include <KActionCollection>
#include <KStandardAction>
#include <KAnimatedButton>
#include <KConfigDialog>
#include <KDebug>
#include <KMenuBar>

#include "mainwindow.h"
#include "webview.h"
#include "mainwidget.h"

#include "ui_mainsettingswidget.h"
#include "settings.h"

#include <QInputDialog> // this has to go

MainWindow::MainWindow(QStringList urls, QWidget* parent)
    : KXmlGuiWindow(parent), m_main(0), m_locationEdit(0), m_spinner(0)
{
    m_locationEdit = new KLineEdit(this);
    m_locationEdit->setClearButtonShown(true);

    m_main = new MainWidget(this);
    setCentralWidget(m_main);

    m_spinner = new KAnimatedButton(this);
    m_spinner->setFocusPolicy(Qt::NoFocus);
    m_spinner->setIcons("process-working-kde");
    m_spinner->setAutoRaise(true);
    m_spinner->setToolButtonStyle(Qt::ToolButtonIconOnly);
    menuBar()->setCornerWidget(m_spinner);

    createActions();

    if (urls.isEmpty()) {
        home();
    } else {
        foreach(QString url, urls) {
            openLinkInNewTab(url);
        }
    }

    setupGUI();
}

void MainWindow::openLinkInNewTab(const QString& link)
{
    int index = m_main->newView();
    m_main->activateView(index);
    WebView* view = m_main->currentView();
    if (view) {
        view->load(link);
    }
}

void MainWindow::openLinkInCurrentTab(const QString& link)
{
    WebView* view = m_main->currentView();
    if (view) {
        view->load(link);
    } else {
        openLinkInNewTab(link);
    }
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    event->accept();
}

void MainWindow::createActions()
{
    KAction* act = new KAction(this);
    act->setIcon(KIcon("view-refresh"));
    act->setText(i18n("Reload"));
    act->setShortcut(Qt::CTRL + Qt::Key_R);
    actionCollection()->addAction("reload", act);
    connect(act, SIGNAL(triggered()), this, SLOT(reload()));

    act = new KAction(this);
    act->setIcon(KIcon("process-stop"));
    act->setText(i18n("Stop"));
    act->setShortcut(Qt::Key_Escape);
    actionCollection()->addAction("stop", act);
    connect(act, SIGNAL(triggered()), this, SLOT(stop()));

    act = new KAction(this);
    act->setIcon(KIcon("tab-new"));
    act->setText(i18n("New Tab"));
    act->setShortcut(Qt::CTRL + Qt::Key_T);
    actionCollection()->addAction("tab_new", act);
    connect(act, SIGNAL(triggered()), this, SLOT(newView()));

    act = new KAction(this);
    act->setText(i18n("Location Bar"));
    act->setDefaultWidget(m_locationEdit);
    actionCollection()->addAction("locationbar", act);

    act = new KAction(this);
    act->setIcon(KIcon("go-jump"));
    act->setText(i18n("Go"));
    actionCollection()->addAction("go", act);
    connect(m_locationEdit, SIGNAL(returnPressed()), act, SLOT(trigger()));
    connect(act, SIGNAL(triggered()), this, SLOT(locationRequested()));

    act = new KAction(this);
    act->setShortcut(Qt::CTRL + Qt::Key_J);
    act->setIcon(KIcon("go-jump"));
    act->setText(i18n("Jump to..."));
    actionCollection()->addAction("jump_to", act);
    connect(act, SIGNAL(triggered()), this, SLOT(jumpTo()));

    KStandardAction::quit(this, SLOT(close()), actionCollection());
    KStandardAction::back(this, SLOT(back()), actionCollection());
    KStandardAction::forward(this, SLOT(forward()), actionCollection());
    KStandardAction::home(this, SLOT(home()), actionCollection());
    KStandardAction::preferences(this, SLOT(showConfigDialog()),
                                 actionCollection());
}

void MainWindow::home()
{
    QString home = Settings::homepage();
    openLinkInCurrentTab(home);
}

void MainWindow::reload()
{
    WebView* view = m_main->currentView();
    if (view) {
        view->reload();
    }
}

void MainWindow::back()
{
    WebView* view = m_main->currentView();
    if (view) {
        view->back();
    }
}

void MainWindow::forward()
{
    WebView* view = m_main->currentView();
    if (view) {
        view->forward();
    }
}

void MainWindow::stop()
{
    WebView* view = m_main->currentView();
    if (view) {
        view->stop();
    }
}


void MainWindow::locationRequested()
{
    openLinkInCurrentTab(m_locationEdit->text());
}

void MainWindow::jumpTo()
{
    QString url = QInputDialog::getText(this, "Url", "Enter Url plz");
    if (!url.isNull()) {
        openLinkInNewTab(url);
    }
}

void MainWindow::showConfigDialog()
{
    if (KConfigDialog::showDialog("settings")) {
        return;
    }

    KConfigDialog* dialog = new KConfigDialog(this, "settings",
                                              Settings::self());

    QWidget* mainSettingsWidget = new QWidget(dialog);
    Ui::MainSettings().setupUi(mainSettingsWidget);
    dialog->addPage(mainSettingsWidget, i18n("Main Settings"),
                    "preferences-system");

    dialog->show();
}

void MainWindow::newView()
{
    int index = m_main->newView();
    m_main->activateView(index);
    WebView* view = m_main->currentView();
    if (view) {
        view->load(Settings::homepage());
    }
}

#include "mainwindow.moc"
