/*
 *   Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License version 2 as
 *   published by the Free Software Foundation
 *
 *   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 Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "krunnerapp.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <QClipboard>
#include <QObject>
#include <QProcess>
#include <QTimer>
#include <QtDBus/QtDBus>
#include <QLineEdit>

#include <KAction>
#include <KActionCollection>
#include <KCrash>
#include <KDialog>
#include <KAuthorized>
#include <KGlobalAccel>
#include <KGlobalSettings>
#include <KLocale>
#include <KMessageBox>
#include <KWindowSystem>

#include <Plasma/RunnerManager>
#include <Plasma/AbstractRunner>

#include "appadaptor.h"
#include "kworkspace.h"
#include "ksystemactivitydialog.h"
#include "interfaces/default/interface.h"
#include "interfaces/quicksand/qs_dialog.h"
#ifdef Q_WS_X11
#include "startupid.h"
#endif
#include "klaunchsettings.h"
#include "krunnersettings.h"

#ifdef Q_WS_X11
#include <X11/extensions/Xrender.h>
#endif

KRunnerApp* KRunnerApp::self()
{
    if (!kapp) {
        return new KRunnerApp();
    }

    return qobject_cast<KRunnerApp*>(kapp);
}

KRunnerApp::KRunnerApp()
    : KUniqueApplication(),
      m_interface(0),
      m_tasks(0),
      m_startupId(NULL),
      m_firstTime(true)
{
    initialize();
    connect(this, SIGNAL(aboutToQuit()), this, SLOT(cleanUp()));
}

KRunnerApp::~KRunnerApp()
{
}

void KRunnerApp::cleanUp()
{
    disconnect(KRunnerSettings::self(), SIGNAL(configChanged()), this, SLOT(reloadConfig()));
    kDebug() << "deleting interface";
    delete m_interface;
    m_interface = 0;
    delete m_runnerManager;
    m_runnerManager = 0;
    delete m_tasks;
    m_tasks = 0;
    KGlobal::config()->sync();
}

KActionCollection* KRunnerApp::actionCollection()
{
    return m_actionCollection;
}

void KRunnerApp::initialize()
{
    setWindowIcon(KIcon("system-run"));

    setQuitOnLastWindowClosed(false);
    KCrash::setFlags(KCrash::AutoRestart);
    initializeStartupNotification();

    connect(KRunnerSettings::self(), SIGNAL(configChanged()), this, SLOT(reloadConfig()));

    m_runnerManager = new Plasma::RunnerManager;

    new AppAdaptor(this);
    QDBusConnection::sessionBus().registerObject( "/App", this );

    // Global keys
    m_actionCollection = new KActionCollection(this);
    KAction* a = 0;

    if (KAuthorized::authorize("run_command")) {
        a = m_actionCollection->addAction("Run Command");
        a->setText(i18n("Run Command"));
        a->setGlobalShortcut(KShortcut(Qt::ALT+Qt::Key_F2));
        connect(a, SIGNAL(triggered(bool)), SLOT(displayOrHide()));

        a = m_actionCollection->addAction("Run Command on clipboard contents");
        a->setText(i18n("Run Command on clipboard contents"));
        a->setGlobalShortcut(KShortcut(Qt::ALT+Qt::SHIFT+Qt::Key_F2));
        connect(a, SIGNAL(triggered(bool)), SLOT(displayWithClipboardContents()));
    }

    a = m_actionCollection->addAction("Show System Activity");
    a->setText(i18n("Show System Activity"));
    a->setGlobalShortcut(KShortcut(Qt::CTRL+Qt::Key_Escape));
    connect(a, SIGNAL(triggered(bool)), SLOT(showTaskManager()));

/*
 * TODO: doesn't this belong in the window manager?
    a = m_actionCollection->addAction("Show Window List");
    a->setText(i18n("Show Window List"));
    a->setGlobalShortcut(KShortcut(Qt::ALT+Qt::Key_F5));
    connect( a, SIGNAL(triggered(bool)), SLOT(slotShowWindowList()));
*/
    if (KAuthorized::authorize("switch_user")) {
        a = m_actionCollection->addAction("Switch User");
        a->setText(i18n("Switch User"));
        a->setGlobalShortcut(KShortcut(Qt::ALT+Qt::CTRL+Qt::Key_Insert));
        connect(a, SIGNAL(triggered(bool)), SLOT(switchUser()));
    }

    //FIXME: lock/logout should be in the session management runner which also provides similar
    // functions
#ifdef Q_WS_X11
    if (KAuthorized::authorize("lock_screen")) {
        a = m_actionCollection->addAction("Lock Session");
        a->setText(i18n("Lock Session"));
        a->setGlobalShortcut(KShortcut(Qt::ALT+Qt::CTRL+Qt::Key_L));
        connect(a, SIGNAL(triggered(bool)), &m_saver, SLOT(Lock()));
    }
#endif

    if (KAuthorized::authorize("logout")) {
        a = m_actionCollection->addAction("Log Out");
        a->setText(i18n("Log Out"));
        a->setGlobalShortcut(KShortcut(Qt::ALT+Qt::CTRL+Qt::Key_Delete));
        connect(a, SIGNAL(triggered(bool)), SLOT(logout()));

        a = m_actionCollection->addAction("Log Out Without Confirmation");
        a->setText(i18n("Log Out Without Confirmation"));
        a->setGlobalShortcut(KShortcut(Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Delete));
        connect(a, SIGNAL(triggered(bool)), SLOT(logoutWithoutConfirmation()));

        a = m_actionCollection->addAction("Halt Without Confirmation");
        a->setText(i18n("Halt Without Confirmation"));
        a->setGlobalShortcut(KShortcut(Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_PageDown));
        connect(a, SIGNAL(triggered(bool)), SLOT(haltWithoutConfirmation()));

        a = m_actionCollection->addAction("Reboot Without Confirmation");
        a->setText(i18n("Reboot Without Confirmation"));
        a->setGlobalShortcut(KShortcut(Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_PageUp));
        connect(a, SIGNAL(triggered(bool)), SLOT(rebootWithoutConfirmation()));
    }

    //Setup the interface after we have set up the actions
    //TODO: if !KAuthorized::authorize("run_comand") (and !"switch_user" i suppose?)
    //      then we probably don't need the interface at all. would be another place
    //      for some small improvements in footprint in that case
    switch (KRunnerSettings::interface()) {
        default:
        case KRunnerSettings::EnumInterface::CommandOriented:
            m_interface = new Interface(m_runnerManager);
            break;
        case KRunnerSettings::EnumInterface::TaskOriented:
            m_interface = new QsDialog(m_runnerManager);
            break;
    }

#ifdef Q_WS_X11
    //FIXME: if argb visuals enabled Qt will always set WM_CLASS as "qt-subapplication" no matter what
    //the application name is we set the proper XClassHint here, hopefully won't be necessary anymore when
    //qapplication will manage apps with argvisuals in a better way
    XClassHint classHint;
    classHint.res_name = const_cast<char*>("krunner");
    classHint.res_class = const_cast<char*>("krunner");
    XSetClassHint(QX11Info::display(), m_interface->winId(), &classHint);
#endif


    m_actionCollection->readSettings();
    if (KAuthorized::authorize("run_command")) {
        m_runnerManager->reloadConfiguration(); // pre-load the runners

        // Single runner mode actions shortcuts

        foreach (const QString &runnerId, m_runnerManager->singleModeAdvertisedRunnerIds()) {
            a = m_actionCollection->addAction(runnerId);
            a->setText(i18nc("Run krunner restricting the search only to runner %1", "Run Command (runner \"%1\" only)",
                       m_runnerManager->runnerName(runnerId)));
            a->setGlobalShortcut(KShortcut());
            connect(a, SIGNAL(triggered(bool)), SLOT(singleRunnerModeActionTriggered()));
        }
    }
}

void KRunnerApp::singleRunnerModeActionTriggered()
{
    KAction * action = qobject_cast<KAction*>(sender());
    if (action) {
        displaySingleRunner(action->objectName());
    }
}

void KRunnerApp::querySingleRunner(const QString& runnerId, const QString &term)
{
    if (!KAuthorized::authorize("run_command")) {
        return;
    }

    m_runnerManager->setSingleModeRunnerId(runnerId);
    m_runnerManager->setSingleMode(!runnerId.isEmpty());

    if (m_runnerManager->singleMode()) {
        m_interface->display(term);
    }
}

QStringList KRunnerApp::singleModeAdvertisedRunnerIds() const
{
    return m_runnerManager->singleModeAdvertisedRunnerIds();
}

void KRunnerApp::initializeStartupNotification()
{
    // Startup notification
    KLaunchSettings::self()->readConfig();
#ifdef Q_WS_X11
    if (!KLaunchSettings::busyCursor()) {
        delete m_startupId;
        m_startupId = NULL;
    } else {
        if (m_startupId == NULL ) {
            m_startupId = new StartupId;
        }

        m_startupId->configure();
    }
#endif
}

void KRunnerApp::showTaskManager()
{
    showTaskManagerWithFilter(QString());
}
void KRunnerApp::showTaskManagerWithFilter(const QString &filterText)
{
#ifndef Q_WS_WIN
    //kDebug(1204) << "Launching KSysGuard...";
    if (!m_tasks) {
        m_tasks = new KSystemActivityDialog;
        connect(m_tasks, SIGNAL(finished()),
                this, SLOT(taskDialogFinished()));
    }
    m_tasks->run();
    m_tasks->setFilterText(filterText);
#endif
}

void KRunnerApp::display()
{
    if (!KAuthorized::authorize("run_command")) {
        return;
    }

    m_runnerManager->setSingleMode(false);
    m_interface->display();
}

void KRunnerApp::displaySingleRunner(const QString &runnerId)
{
    if (!KAuthorized::authorize("run_command")) {
        return;
    }

    m_runnerManager->setSingleModeRunnerId(runnerId);
    m_runnerManager->setSingleMode(!runnerId.isEmpty());
    m_interface->display();
}

void KRunnerApp::displayOrHide()
{
    if (!KAuthorized::authorize("run_command")) {
        m_interface->hide();
        return;
    }

    if (!m_interface->isVisible()) {
        m_runnerManager->setSingleMode(false);
    }

    if (m_interface->freeFloating()) {
        if (m_interface->isVisible()) {
            m_interface->hide();
        } else {
            m_interface->display();
        }
    } else if (m_interface->isActiveWindow()) {
        m_interface->hide();
    } else {
        m_interface->display();
    }
}

void KRunnerApp::query(const QString &term)
{
    if (!KAuthorized::authorize("run_command")) {
        return;
    }

    m_interface->display(term);
}

void KRunnerApp::displayWithClipboardContents()
{
    if (!KAuthorized::authorize("run_command")) {
        return;
    }

    QString clipboardData = QApplication::clipboard()->text(QClipboard::Selection);
    m_interface->display(clipboardData);
}

void KRunnerApp::switchUser()
{
    m_interface->switchUser();
}

void KRunnerApp::clearHistory()
{
    m_interface->clearHistory();
}

void KRunnerApp::taskDialogFinished()
{
#ifndef Q_WS_WIN
    m_tasks->deleteLater();
    m_tasks = 0;
#endif
}

void KRunnerApp::logout()
{
    logout(KWorkSpace::ShutdownConfirmYes, KWorkSpace::ShutdownTypeDefault);
}

void KRunnerApp::logoutWithoutConfirmation()
{
    logout(KWorkSpace::ShutdownConfirmNo, KWorkSpace::ShutdownTypeNone);
}

void KRunnerApp::haltWithoutConfirmation()
{
    logout(KWorkSpace::ShutdownConfirmNo, KWorkSpace::ShutdownTypeHalt);
}

void KRunnerApp::rebootWithoutConfirmation()
{
    logout(KWorkSpace::ShutdownConfirmNo, KWorkSpace::ShutdownTypeReboot);
}

void KRunnerApp::logout(KWorkSpace::ShutdownConfirm confirm, KWorkSpace::ShutdownType sdtype)
{
#ifndef Q_WS_WIN
    if (!KWorkSpace::requestShutDown(confirm, sdtype)) {
        // TODO: should we show these errors in Interface?
        KMessageBox::error( 0, i18n("Could not log out properly.\nThe session manager cannot "
                                    "be contacted. You can try to force a shutdown by pressing "
                                    "Ctrl+Alt+Backspace; note, however, that your current session "
                                    "will not be saved with a forced shutdown." ) );
    }
#endif
}

int KRunnerApp::newInstance()
{
    if (m_firstTime) {
        m_firstTime = false;
    } else {
        display();
    }

    return KUniqueApplication::newInstance();
    //return 0;
}

bool KRunnerApp::hasCompositeManager() const
{
#ifdef Q_WS_X11
    return KWindowSystem::compositingActive();
#else
    return false;
#endif
}

void KRunnerApp::reloadConfig()
{
    //Prevent Interface destructor from triggering this method
    disconnect(KRunnerSettings::self(), SIGNAL(configChanged()), this, SLOT(reloadConfig()));

    const int interface = KRunnerSettings::interface();
    if (!qobject_cast<QsDialog*>(m_interface) &&
        interface == KRunnerSettings::EnumInterface::TaskOriented) {
        m_interface->deleteLater();
        m_interface = new QsDialog(m_runnerManager);
    } else if (!qobject_cast<Interface*>(m_interface) &&
               interface == KRunnerSettings::EnumInterface::CommandOriented) {
        m_interface->deleteLater();
        m_interface = new Interface(m_runnerManager);
    }

    m_interface->setFreeFloating(KRunnerSettings::freeFloating());
    connect(KRunnerSettings::self(), SIGNAL(configChanged()), this, SLOT(reloadConfig()));
    display();
}

#include "krunnerapp.moc"
