/*
 *   Copyright 2006-2008 Aaron Seigo <aseigo@kde.org>
 *   Copyright 2009 Marco Martin <notmart@gmail.com>
 *
 *   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, 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 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 "plasmaapp.h"

#include <unistd.h>

#include <QApplication>
#include <QPixmapCache>
#include <QTimer>
#include <QVBoxLayout>
#include <QtDBus/QtDBus>

#include <KAction>
#include <KCrash>
#include <KDebug>
#include <KCmdLineArgs>
#include <KStandardAction>
#include <KWindowSystem>

#include <ksmserver_interface.h>

#include <kephal/screens.h>

#include <Plasma/Containment>
#include <Plasma/Theme>
#include <Plasma/Dialog>
#include <Plasma/WindowEffects>

#include "netcorona.h"
#include "netview.h"

#include "widgetsexplorer/widgetexplorer.h"
#include "plasmagenericshell/backgrounddialog.h"

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

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

    return qobject_cast<PlasmaApp*>(kapp);
}


class GlowBar : public QWidget
{
public:
    GlowBar(Plasma::Direction direction, const QRect &triggerZone)
        : QWidget(0),
          m_strength(0.3),
          m_svg(new Plasma::Svg(this)),
          m_direction(direction)
    {
        setAttribute(Qt::WA_TranslucentBackground);
        KWindowSystem::setOnAllDesktops(winId(), true);
        unsigned long state = NET::Sticky | NET::StaysOnTop | NET::KeepAbove;
        KWindowSystem::setState(winId(), state);
        KWindowSystem::setType(winId(), NET::Dock);
        m_svg->setImagePath("widgets/glowbar");

#ifdef Q_WS_X11
        QRegion region(QRect(0,0,1,1));
        XShapeCombineRegion(QX11Info::display(), winId(), ShapeInput, 0, 0,
                            region.handle(), ShapeSet);
#endif

        QPalette pal = palette();
        pal.setColor(backgroundRole(), Qt::transparent);
        setPalette(pal);

        QRect glowGeom = triggerZone;
        QSize s = sizeHint();
        switch (m_direction) {
            case Plasma::Up:
                glowGeom.setY(glowGeom.y() - s.height() + 1);
                // fallthrough
            case Plasma::Down:
                glowGeom.setHeight(s.height());
                break;
            case Plasma::Left:
                glowGeom.setX(glowGeom.x() - s.width() + 1);
                // fallthrough
            case Plasma::Right:
                glowGeom.setWidth(s.width());
                break;
        }

        //kDebug() << "glow geom is" << glowGeom << "from" << triggerZone;
        setGeometry(glowGeom);
        m_buffer = QPixmap(size());
    }

    void paintEvent(QPaintEvent* e)
    {
        Q_UNUSED(e)
        QPixmap pixmap;
        const QSize glowRadius = m_svg->elementSize("hint-glow-radius");
        QPoint pixmapPosition(0, 0);

        m_buffer.fill(QColor(0, 0, 0, int(qreal(255)*m_strength)));
        QPainter p(&m_buffer);
        p.setCompositionMode(QPainter::CompositionMode_SourceIn);

        switch (m_direction) {
            case Plasma::Down:
                pixmap = m_svg->pixmap("bottom");
                pixmapPosition = QPoint(0, -glowRadius.height());
                break;
            case Plasma::Up:
                pixmap = m_svg->pixmap("top");
                break;
            case Plasma::Right:
                pixmap = m_svg->pixmap("right");
                pixmapPosition = QPoint(-glowRadius.width(), 0);
                break;
            case Plasma::Left:
                pixmap = m_svg->pixmap("left");
                break;
        }

        if (m_direction == Plasma::Left || m_direction == Plasma::Right) {
            p.drawTiledPixmap(QRect(0, pixmapPosition.x(), pixmap.width(), height()), pixmap);
        } else {
            p.drawTiledPixmap(QRect(0, pixmapPosition.y(), width(), pixmap.height()), pixmap);
        }

        p.end();
        p.begin(this);
        p.drawPixmap(QPoint(0, 0), m_buffer);
    }

    QSize sizeHint() const
    {
        return m_svg->elementSize("bottomright") - m_svg->elementSize("hint-glow-radius");
    }

    bool event(QEvent *event)
    {
        if (event->type() == QEvent::Paint) {
            QPainter p(this);
            p.setCompositionMode(QPainter::CompositionMode_Source);
            p.fillRect(rect(), Qt::transparent);
        }
        return QWidget::event(event);
    }

    void updateStrength(QPoint point)
    {
        QPoint localPoint = mapFromGlobal(point);

        qreal newStrength;
        switch (m_direction) {
        case Plasma::Up: // when the panel is at the bottom.
            newStrength = 1 - qreal(-localPoint.y())/m_triggerDistance;
            break;
        case Plasma::Right:
            newStrength = 1 - qreal(localPoint.x())/m_triggerDistance;
            break;
        case Plasma::Left: // when the panel is right-aligned
            newStrength = 1 - qreal(-localPoint.x())/m_triggerDistance;
            break;
        case Plasma::Down:
        default:
            newStrength = 1- qreal(localPoint.y())/m_triggerDistance;
            break;
        }
        if (qAbs(newStrength - m_strength) > 0.01 && newStrength >= 0 && newStrength <= 1) {
            m_strength = newStrength;
            update();
        }
    }


private:
    static const int m_triggerDistance = 30;
    qreal m_strength;
    Plasma::Svg *m_svg;
    Plasma::Direction m_direction;
    QPixmap m_buffer;
};


PlasmaApp::PlasmaApp()
    : KUniqueApplication(),
      m_corona(0),
      m_widgetExplorerView(0),
      m_widgetExplorer(0),
      m_glowBar(0),
      m_mousePollTimer(0),
      m_controlBar(0),
      m_mainView(0),
      m_isDesktop(false),
      m_autoHideControlBar(true),
      m_raiseTimer(new QTimer(this)),
      m_unHideTimer(0)
{
    KGlobal::locale()->insertCatalog("libplasma");
    KGlobal::locale()->insertCatalog("plasmagenericshell");


    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    bool isDesktop = args->isSet("desktop");
    if (isDesktop) {
        notifyStartup(false);
        KCrash::setFlags(KCrash::AutoRestart);
    }

    //TODO: decide how to handle the cache size; possibilities:
    //      * % of ram, as in desktop
    //      * fixed size, hardcoded (uck)
    //      * optional size, specified on command line
    //      * optional size, in a config file
    //      * don't do anything special at all
    //QPixmapCache::setCacheLimit(cacheSize);

    KConfigGroup cg(KSharedConfig::openConfig("plasmarc"), "Theme-plasma-netbook");
    const QString themeName = cg.readEntry("name", "air-netbook");
    Plasma::Theme::defaultTheme()->setUseGlobalSettings(false);
    Plasma::Theme::defaultTheme()->setThemeName(themeName);

    cg = KConfigGroup(KGlobal::config(), "General");

    Plasma::Theme::defaultTheme()->setFont(cg.readEntry("desktopFont", font()));

    m_mainView = new NetView(0, NetView::mainViewId(), 0);

    m_raiseTimer->setSingleShot(true);
    connect(m_raiseTimer, SIGNAL(timeout()), this, SLOT(raiseMainView()));
	
    connect(m_mainView, SIGNAL(containmentActivated()), this, SLOT(mainContainmentActivated()));
    connect(KWindowSystem::self(), SIGNAL(workAreaChanged()), this, SLOT(positionPanel()));
    m_mainView->installEventFilter(this);

    connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(controlBarVisibilityUpdate()));

    int width = 400;
    int height = 200;
    if (isDesktop) {
        QRect rect = Kephal::ScreenUtils::screenGeometry(m_mainView->screen());
        width = rect.width();
        height = rect.height();
    } else {
        QAction *action = KStandardAction::quit(qApp, SLOT(quit()), m_mainView);
        m_mainView->addAction(action);

        QString geom = args->getOption("screen");
        int x = geom.indexOf('x');

        if (x > 0)  {
            width = qMax(width, geom.left(x).toInt());
            height = qMax(height, geom.right(geom.length() - x - 1).toInt());
        }
    }

    setIsDesktop(isDesktop);
    m_mainView->setFixedSize(width, height);
    m_mainView->move(0,0);

    // this line initializes the corona.
    corona();
    //setIsDesktop(isDesktop);
    reserveStruts();

    if (isDesktop) {
        notifyStartup(true);
    }

    connect(this, SIGNAL(aboutToQuit()), this, SLOT(cleanup()));
}

PlasmaApp::~PlasmaApp()
{
}

void PlasmaApp::cleanup()
{
    if (m_corona) {
        m_corona->saveLayout();
    }

    if (!m_mainView->containment()) {
        return;
    }

    // save the mapping of Views to Containments at the moment
    // of application exit so we can restore that when we start again.
    KConfigGroup viewIds(KGlobal::config(), "ViewIds");
    viewIds.deleteGroup();
    viewIds.writeEntry(QString::number(m_mainView->containment()->id()), NetView::mainViewId());

    if (m_controlBar) {
        viewIds.writeEntry(QString::number(m_controlBar->containment()->id()), NetView::controlBarId());
    }

    delete m_mainView;
    m_mainView = 0;

    delete m_corona;
    m_corona = 0;

    //TODO: This manual sync() should not be necessary?
    syncConfig();
}

void PlasmaApp::syncConfig()
{
    KGlobal::config()->sync();
}

void PlasmaApp::positionPanel()
{
    if (!m_controlBar) {
        return;
    }

    QRect screenRect = Kephal::ScreenUtils::screenGeometry(m_controlBar->screen());

    //move
    controlBarMoved(m_controlBar);

    if (m_controlBar->formFactor() == Plasma::Horizontal) {
        m_controlBar->setFixedSize(screenRect.width(), m_controlBar->size().height());
    } else if (m_controlBar->formFactor() == Plasma::Vertical) {
        m_controlBar->setFixedSize(m_controlBar->size().width(), screenRect.height());
    }


    int left = 0;
    int right = 0;
    int top = 0;
    int bottom = 0;

    switch (m_controlBar->location()) {
    case Plasma::LeftEdge:
        left = m_controlBar->width();
        break;
    case Plasma::RightEdge:
        right = m_controlBar->width();
        break;
    case Plasma::TopEdge:
        top = m_controlBar->height();
        break;
    case Plasma::BottomEdge:
        bottom = m_controlBar->height();
        break;
    default:
        break;
    }

    m_controlBar->containment()->setMaximumSize(m_controlBar->size());
    m_controlBar->containment()->setMinimumSize(m_controlBar->size());

    if (m_widgetExplorerView) {
        top += m_widgetExplorerView->size().height();
    }

    foreach (Plasma::Containment *containment, m_corona->containments()) {
        if (containment->location() == Plasma::Desktop ||
            containment->location() == Plasma::Floating) {
            containment->setContentsMargins(left, top, right, bottom);
        }
    }

    if (m_autoHideControlBar && m_controlBar->isVisible()) {
        destroyUnHideTrigger();
        createUnhideTrigger();
    }
}

void PlasmaApp::mainContainmentActivated()
{
    m_mainView->setWindowTitle(m_mainView->containment()->activity());

    if (!m_isDesktop) {
        return;
    }

    const WId id = m_mainView->effectiveWinId();

    QWidget * activeWindow = QApplication::activeWindow();
    KWindowSystem::raiseWindow(id);

    if (activeWindow) {
        KWindowSystem::clearState(m_mainView->winId(), NET::KeepBelow);
        KWindowSystem::raiseWindow(activeWindow->effectiveWinId());
        m_mainView->activateWindow();
        activeWindow->setFocus();
    } else {
        m_mainView->activateWindow();
    }
}

void PlasmaApp::setIsDesktop(bool isDesktop)
{
    m_isDesktop = isDesktop;

    if (isDesktop) {
        m_mainView->setWindowFlags(m_mainView->windowFlags() | Qt::FramelessWindowHint);
        KWindowSystem::setOnAllDesktops(m_mainView->winId(), true);
        if (m_controlBar) {
            KWindowSystem::setOnAllDesktops(m_controlBar->winId(), true);
        }
        m_mainView->show();
        KWindowSystem::setType(m_mainView->winId(), NET::Normal);
    } else {
        m_mainView->setWindowFlags(m_mainView->windowFlags() & ~Qt::FramelessWindowHint);
        KWindowSystem::setOnAllDesktops(m_mainView->winId(), false);
        if (m_controlBar) {
            KWindowSystem::setOnAllDesktops(m_controlBar->winId(), false);
        }
        KWindowSystem::setType(m_mainView->winId(), NET::Normal);
    }
}

bool PlasmaApp::isDesktop() const
{
    return m_isDesktop;
}

void PlasmaApp::adjustSize(Kephal::Screen *screen)
{
    Q_UNUSED(screen)

    QRect rect = Kephal::ScreenUtils::screenGeometry(m_mainView->screen());

    int width = rect.width();
    int height = rect.height();
    //FIXME: ugly hack there too
    m_mainView->setFixedSize(width, height);
    positionPanel();
    reserveStruts();
}

void PlasmaApp::reserveStruts()
{
    if (!m_controlBar || !isDesktop()) {
        return;
    }

    NETExtendedStrut strut;

    if (!m_autoHideControlBar || hasForegroundWindows()) {
        switch (m_controlBar->location()) {
        case Plasma::LeftEdge:
            strut.left_width = m_controlBar->width();
            strut.left_start = m_controlBar->y();
            strut.left_end = m_controlBar->y() + m_controlBar->height() - 1;
            break;
        case Plasma::RightEdge:
            strut.right_width = m_controlBar->width();
            strut.right_start = m_controlBar->y();
            strut.right_end = m_controlBar->y() + m_controlBar->height() - 1;
            break;
        case Plasma::TopEdge:
            strut.top_width = m_controlBar->height();
            strut.top_start = m_controlBar->x();
            strut.top_end = m_controlBar->x() + m_controlBar->width() - 1;
            break;
        case Plasma::BottomEdge:
        default:
            strut.bottom_width = m_controlBar->height();
            strut.bottom_start = m_controlBar->x();
            strut.bottom_end = m_controlBar->x() + m_controlBar->width() - 1;
        }
    }

    KWindowSystem::setExtendedStrut(m_controlBar->winId(),
                                    strut.left_width, strut.left_start, strut.left_end,
                                    strut.right_width, strut.right_start, strut.right_end,
                                    strut.top_width, strut.top_start, strut.top_end,
                                    strut.bottom_width, strut.bottom_start, strut.bottom_end);

    //ensure the main view is at the proper position too
    QRect screenRect = Kephal::ScreenUtils::screenGeometry(m_controlBar->screen());
    m_mainView->move(screenRect.topLeft());
}

NetView *PlasmaApp::controlBar() const
{
    return m_controlBar;
}

NetView *PlasmaApp::mainView() const
{
    return m_mainView;
}

Plasma::Corona* PlasmaApp::corona()
{
    if (!m_corona) {
        m_corona = new NetCorona(this);
        connect(m_corona, SIGNAL(containmentAdded(Plasma::Containment*)),
                this, SLOT(createView(Plasma::Containment*)));
        connect(m_corona, SIGNAL(configSynced()), this, SLOT(syncConfig()));


        m_corona->setItemIndexMethod(QGraphicsScene::NoIndex);
        m_corona->initializeLayout();

        m_mainView->show();

        connect(m_corona, SIGNAL(screenOwnerChanged(int,int,Plasma::Containment*)),
                m_mainView, SLOT(screenOwnerChanged(int,int,Plasma::Containment*)));

    }

    return m_corona;
}

bool PlasmaApp::hasComposite()
{
    return KWindowSystem::compositingActive();
}

void PlasmaApp::notifyStartup(bool completed)
{
    org::kde::KSMServerInterface ksmserver("org.kde.ksmserver", "/KSMServer", QDBusConnection::sessionBus());

    const QString startupID("workspace desktop");
    if (completed) {
        ksmserver.resumeStartup(startupID);
    } else {
        ksmserver.suspendStartup(startupID);
    }
}

void PlasmaApp::createView(Plasma::Containment *containment)
{
    connect(containment, SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showWidgetExplorer()));
    connect(containment, SIGNAL(configureRequested(Plasma::Containment*)),
            this, SLOT(configureContainment(Plasma::Containment*)));
    connect(containment, SIGNAL(toolBoxVisibilityChanged(bool)),
            this, SLOT(updateToolBoxVisibility(bool)));

    KConfigGroup viewIds(KGlobal::config(), "ViewIds");
    int defaultId = 0;
    if (containment->containmentType() == Plasma::Containment::PanelContainment && 
        (!m_controlBar || m_controlBar->containment() == 0) ) {
        defaultId = NetView::controlBarId();
    } else if (containment->containmentType() == Plasma::Containment::PanelContainment && 
        m_mainView->containment() == 0 ) {
        defaultId = NetView::mainViewId();
    }

    int id = viewIds.readEntry(QString::number(containment->id()), defaultId);


    kDebug() << "new containment" << (QObject*)containment << containment->id() << "view id" << id;

    //is it a desktop -and- is it active?
    if ((m_mainView && id == NetView::mainViewId()) ||
        (containment->containmentType() != Plasma::Containment::PanelContainment &&
         containment->containmentType() != Plasma::Containment::CustomPanelContainment &&
         !viewIds.exists() && containment->screen() == 0)) {
        m_mainView->setContainment(containment);
        containment->setScreen(0);
    //is it a panel?
    } else if (id == NetView::controlBarId()) {
        if (!m_controlBar) {
            m_controlBar = new NetView(0, NetView::controlBarId(), 0);

            Kephal::Screens *screens = Kephal::Screens::self();
            connect(screens, SIGNAL(screenResized(Kephal::Screen *, QSize, QSize)),
                    this, SLOT(adjustSize(Kephal::Screen *)));

            m_controlBar->setAutoFillBackground(false);
            m_controlBar->viewport()->setAutoFillBackground(false);
            m_controlBar->setAttribute(Qt::WA_TranslucentBackground);

            connect(m_controlBar, SIGNAL(locationChanged(const NetView *)), this, SLOT(positionPanel()));
            connect(m_controlBar, SIGNAL(geometryChanged()), this, SLOT(positionPanel()));
            connect(m_controlBar, SIGNAL(containmentActivated()), this, SLOT(showControlBar()));
            connect(m_controlBar, SIGNAL(autoHideChanged(bool)), this, SLOT(setAutoHideControlBar(bool)));
        }

        m_controlBar->setContainment(containment);
        setControlBarVisible(true);
        containment->setMaximumSize(m_controlBar->size());
        containment->setMinimumSize(m_controlBar->size());
        containment->setImmutability(Plasma::UserImmutable);

        m_autoHideControlBar = m_controlBar->config().readEntry("panelAutoHide", true);

        setAutoHideControlBar(m_autoHideControlBar);
    } else {
        containment->setScreen(-1);
    }
}

void PlasmaApp::closeWidgetExplorer()
{
    if (m_widgetExplorer) {
        Plasma::WindowEffects::slideWindow(m_widgetExplorerView, m_controlBar->location());
        m_widgetExplorer->deleteLater();
        m_widgetExplorerView->deleteLater();
    }
}

void PlasmaApp::updateToolBoxVisibility(bool visible)
{
    foreach (Plasma::Containment *cont, m_corona->containments()) {
         cont->setToolBoxOpen(visible);
    }

    if (!visible) {
        closeWidgetExplorer();
    }
}

void PlasmaApp::controlBarMoved(const NetView *controlBar)
{
    if (!m_controlBar || controlBar != m_controlBar) {
        return;
    }

    QRect screenRect = Kephal::ScreenUtils::screenGeometry(m_controlBar->screen());

    Plasma::Containment *cont = m_controlBar->containment();

    switch (controlBar->location()) {
    case Plasma::LeftEdge:
        m_controlBar->move(screenRect.topLeft());
        break;
    case Plasma::RightEdge:
        m_controlBar->move(screenRect.topRight()-QPoint(m_controlBar->size().width(), 0));
        break;
    case Plasma::TopEdge:
        m_controlBar->move(screenRect.topLeft());
        break;
    case Plasma::BottomEdge:
        m_controlBar->move(screenRect.bottomLeft()-QPoint(0,m_controlBar->size().height()));
    default:
        break;
    }

    //flip height and width
    if (controlBar->formFactor() == Plasma::Vertical) {
        if (cont && m_controlBar->size().width() > m_controlBar->size().height()) {
            cont->setMinimumSize(cont->size().height(), cont->size().width());
            cont->setMaximumSize(cont->minimumSize());
        }
    } else if (controlBar->formFactor() == Plasma::Horizontal) {
        if (cont && m_controlBar->size().width() < m_controlBar->size().height()) {
            cont->setMinimumSize(cont->size().height(), cont->size().width());
            cont->setMaximumSize(cont->minimumSize());
        }
    }

    reserveStruts();
}

void PlasmaApp::setAutoHideControlBar(bool autoHide)
{
    if (!m_controlBar) {
        return;
    }

    if (autoHide && !m_unHideTimer) {
        m_unHideTimer = new QTimer(this);
        m_unHideTimer->setSingleShot(true);
        connect(m_unHideTimer, SIGNAL(timeout()), this, SLOT(controlBarVisibilityUpdate()));
        m_controlBar->installEventFilter(this);
        controlBarVisibilityUpdate();
    } else {
        m_controlBar->removeEventFilter(this);
        destroyUnHideTrigger();
        delete m_unHideTimer;
        m_unHideTimer = 0;
        setControlBarVisible(true);
    }

    m_autoHideControlBar = autoHide;
    reserveStruts();
    m_controlBar->config().writeEntry("panelAutoHide", autoHide);
}

void PlasmaApp::showWidgetExplorer()
{
    Plasma::Containment *containment = dynamic_cast<Plasma::Containment *>(sender());

    if (!containment) {
        return;
    }

    showWidgetExplorer(containment);
}

void PlasmaApp::showWidgetExplorer(Plasma::Containment *containment)
{
    if (!containment) {
        return;
    }

    containment->setToolBoxOpen(true);

    if (!m_widgetExplorerView) {

        m_widgetExplorerView = new Plasma::Dialog();

        KWindowSystem::setOnAllDesktops(m_widgetExplorerView->winId(), true);
        m_widgetExplorerView->show();
        KWindowSystem::activateWindow(m_widgetExplorerView->winId());
        m_widgetExplorerView->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
        m_widgetExplorerView->setAttribute(Qt::WA_TranslucentBackground);
        m_widgetExplorerView->setAttribute(Qt::WA_DeleteOnClose);
        KWindowSystem::setState(m_widgetExplorerView->winId(), NET::StaysOnTop|NET::KeepAbove);
        connect(m_widgetExplorerView, SIGNAL(destroyed()), this, SLOT(widgetExplorerDestroyed()));

        if (m_controlBar) {
            switch (m_controlBar->location()) {
            case Plasma::TopEdge:
                m_widgetExplorerView->resize(m_mainView->size().width(), KIconLoader::SizeEnormous);
                m_widgetExplorerView->move(m_controlBar->geometry().bottomLeft());
                break;
            case Plasma::LeftEdge:
                m_widgetExplorerView->resize(KIconLoader::SizeEnormous, m_mainView->size().height());
                m_widgetExplorerView->move(m_controlBar->geometry().topRight());
                break;
            case Plasma::RightEdge:
                m_widgetExplorerView->resize(KIconLoader::SizeEnormous, m_mainView->size().height());
                m_widgetExplorerView->move(m_controlBar->geometry().topLeft() - QPoint(m_widgetExplorerView->size().width(), 0));
                break;
            case Plasma::BottomEdge:
            default:
                m_widgetExplorerView->resize(m_mainView->size().width(), KIconLoader::SizeEnormous);
                m_widgetExplorerView->move(m_controlBar->geometry().topLeft() - QPoint(0, m_widgetExplorerView->size().height()));
                break;
            }
        } else {
            m_widgetExplorerView->resize(m_mainView->size().width(), KIconLoader::SizeEnormous);
            m_widgetExplorerView->move(0,0);
        }
    }

    if (!m_widgetExplorer) {
        m_widgetExplorer = new Plasma::WidgetExplorer(m_controlBar->containment());
        connect(m_widgetExplorer, SIGNAL(closeClicked()), this, SLOT(closeWidgetExplorer()));
        m_widgetExplorer->setContainment(m_mainView->containment());
        m_widgetExplorer->populateWidgetList();

        m_corona->addOffscreenWidget(m_widgetExplorer);

        QSize viewSize = m_widgetExplorerView->size();
        m_widgetExplorerView->setGraphicsWidget(m_widgetExplorer);

        m_widgetExplorer->setIconSize(KIconLoader::SizeLarge);
        m_widgetExplorer->resize(m_widgetExplorerView->size());
        m_widgetExplorerView->setFixedSize(viewSize);
        m_widgetExplorerView->installEventFilter(this);
    }

    m_widgetExplorer->setOrientation(Qt::Horizontal);
    positionPanel();


    m_widgetExplorer->show();
    Plasma::WindowEffects::slideWindow(m_widgetExplorerView, m_controlBar->location());
    m_widgetExplorerView->show();
}

void PlasmaApp::widgetExplorerDestroyed()
{
    m_widgetExplorer = 0;
    m_widgetExplorerView = 0;
    positionPanel();
    m_mainView->containment()->setToolBoxOpen(false);
}


void PlasmaApp::configureContainment(Plasma::Containment *containment)
{
    const QString id = "plasma_containment_settings_" + QString::number(containment->id());
    BackgroundDialog *configDialog = qobject_cast<BackgroundDialog*>(KConfigDialog::exists(id));
    kDebug() << configDialog;

    if (configDialog) {
        configDialog->reloadConfig();
    } else {
        const QSize resolution = Kephal::ScreenUtils::screenGeometry(m_mainView->screen()).size();


        KConfigSkeleton *nullManager = new KConfigSkeleton(0);
        configDialog = new BackgroundDialog(resolution, containment, m_mainView, 0, id, nullManager);
        configDialog->setAttribute(Qt::WA_DeleteOnClose);

        connect(configDialog, SIGNAL(destroyed(QObject*)), nullManager, SLOT(deleteLater()));
    }

    configDialog->show();
    KWindowSystem::setOnDesktop(configDialog->winId(), KWindowSystem::currentDesktop());
    KWindowSystem::activateWindow(configDialog->winId());
}


bool PlasmaApp::eventFilter(QObject * watched, QEvent *event)
{
    if (watched == m_mainView && event->type() == QEvent::WindowActivate) {
        destroyUnHideTrigger();
        if (m_controlBar) {
            Plasma::WindowEffects::slideWindow(m_controlBar, m_controlBar->location());
            setControlBarVisible(true);
            m_raiseTimer->start(100);
            reserveStruts();
        }
    } else if ((watched == m_mainView &&
                event->type() == QEvent::WindowDeactivate &&
                !hasForegroundWindows()) ||
               (watched == m_controlBar &&
                event->type() == QEvent::Leave &&
                !hasForegroundWindows())) {
kWarning()<<event;
        //delayed hide
        if (m_unHideTimer) {
            m_unHideTimer->start(400);
            reserveStruts();
        }
    } else if (watched == m_widgetExplorerView && event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Escape) {
            closeWidgetExplorer();
        }
    } else if (watched == m_widgetExplorerView && event->type() == QEvent::Resize) {
         m_widgetExplorer->resize(m_widgetExplorerView->contentsRect().size());
    } else if (!m_isDesktop && watched == m_mainView && event->type() == QEvent::Close) {
        exit();
    }
    return false;
}

bool PlasmaApp::x11EventFilter(XEvent *event)
{

    if (m_controlBar && m_autoHideControlBar && !m_controlBar->isVisible() && event->xcrossing.window == m_unhideTrigger &&
        (event->xany.send_event != True && event->type == EnterNotify)) {
        //delayed show
        if (!m_glowBar && KWindowSystem::compositingActive() && !m_triggerZone.contains(QCursor::pos())) {
            Plasma::Direction direction = Plasma::locationToDirection(m_controlBar->location());
            m_glowBar = new GlowBar(direction, m_triggerZone);
            m_glowBar->show();
            XMoveResizeWindow(QX11Info::display(), m_unhideTrigger, m_triggerZone.x(), m_triggerZone.y(), m_triggerZone.width(), m_triggerZone.height());

            //FIXME: This is ugly as hell but well, yeah
            if (!m_mousePollTimer) {
                m_mousePollTimer = new QTimer(this);
            }

            disconnect(m_mousePollTimer, SIGNAL(timeout()), this, SLOT(unhideHintMousePoll()));
            connect(m_mousePollTimer, SIGNAL(timeout()), this, SLOT(unhideHintMousePoll()));
            m_mousePollTimer->start(200);
        } else {
            m_unHideTimer->start(400);
        }
    } else if ((event->xany.send_event != True && event->type == FocusOut)) {
        QTimer::singleShot(100, this, SLOT(lowerMainView()));
    } else if ((event->xany.send_event != True && event->type == ButtonPress)) {
        m_raiseTimer->stop();
    }
    return KUniqueApplication::x11EventFilter(event);
}

bool PlasmaApp::hasForegroundWindows() const
{
    return QApplication::activeWindow();
}

void PlasmaApp::lowerMainView()
{
    if (m_isDesktop && !hasForegroundWindows()) {
        KWindowSystem::lowerWindow(m_mainView->winId());
        KWindowSystem::setState(m_mainView->winId(), NET::KeepBelow);
    }
}

void PlasmaApp::raiseMainView()
{
    if (m_isDesktop) {
        KWindowSystem::clearState(m_mainView->winId(), NET::KeepBelow);
        KWindowSystem::raiseWindow(m_mainView->winId());
    }
}

void PlasmaApp::controlBarVisibilityUpdate()
{
    if (!m_controlBar) {
        return;
    }

    if (!m_autoHideControlBar) {
        setControlBarVisible(true);
        return;
    } else if (m_autoHideControlBar && hasForegroundWindows() && m_controlBar->isVisible()) {
        return;
    }

    //would be nice to avoid this
    QPoint cursorPos = QCursor::pos();

    if (m_triggerZone.adjusted(-1, -1, 1, 1).contains(cursorPos)) { 
        if (!m_controlBar->isVisible()) {
            destroyUnHideTrigger();
            Plasma::WindowEffects::slideWindow(m_controlBar, m_controlBar->location());
            setControlBarVisible(true);
        }
    } else if (!m_controlBar->geometry().contains(cursorPos)) {
        Plasma::WindowEffects::slideWindow(m_controlBar, m_controlBar->location());
        m_controlBar->hide();
        createUnhideTrigger();
    }
}

void PlasmaApp::showControlBar()
{
    setControlBarVisible(true);
}

void PlasmaApp::hideControlBar()
{
    setControlBarVisible(false);
}

void PlasmaApp::setControlBarVisible(bool visible)
{
    if (!m_controlBar || m_controlBar->isVisible() == visible) {
        return;
    }

    if (visible) {
        destroyUnHideTrigger();
        Plasma::WindowEffects::slideWindow(m_controlBar, m_controlBar->location());
        m_controlBar->show();
        KWindowSystem::setOnAllDesktops(m_controlBar->effectiveWinId(), m_isDesktop);
        m_controlBar->setWindowFlags(m_mainView->windowFlags() | Qt::FramelessWindowHint);
        m_controlBar->setFrameShape(QFrame::NoFrame);
        unsigned long state = NET::Sticky | NET::StaysOnTop | NET::KeepAbove;
        KWindowSystem::setState(m_controlBar->effectiveWinId(), state);
        KWindowSystem::setType(m_controlBar->effectiveWinId(), NET::Dock);
    } else if (!m_autoHideControlBar) {
        Plasma::WindowEffects::slideWindow(m_controlBar, m_controlBar->location());
        m_controlBar->hide();
        createUnhideTrigger();
    }
}

void PlasmaApp::toggleControlBarVisibility()
{
    setControlBarVisible(!m_controlBar->isVisible());
}

void PlasmaApp::unhideHintMousePoll()
{
#ifdef Q_WS_X11
    QPoint mousePos = QCursor::pos();
    m_glowBar->updateStrength(mousePos);

    if (!m_unhideTriggerGeom.contains(mousePos)) {
        //kDebug() << "hide the glow";
        if (m_mousePollTimer) {
            m_mousePollTimer->stop();
            disconnect(m_mousePollTimer, SIGNAL(timeout()), this, SLOT(unhideHintMousePoll()));
        }

        delete m_glowBar;
        m_glowBar = 0;
        XMoveResizeWindow(QX11Info::display(), m_unhideTrigger, m_unhideTriggerGeom.x(), m_unhideTriggerGeom.y(), m_unhideTriggerGeom.width(), m_unhideTriggerGeom.height());
    } else {
        controlBarVisibilityUpdate();
    }
#endif
}

void PlasmaApp::createUnhideTrigger()
{
#ifdef Q_WS_X11
    //kDebug() << m_unhideTrigger << None;
    if (!m_autoHideControlBar || m_unhideTrigger != None || !m_controlBar || m_controlBar->isVisible()) {
        return;
    }

    int actualWidth = 1;
    int actualHeight = 1;
    int triggerWidth = 1;
    int triggerHeight = 1;

    if (KWindowSystem::compositingActive()) {
        triggerWidth = 30;
        triggerHeight = 30;
    }

    QPoint actualTriggerPoint;
    QPoint triggerPoint = actualTriggerPoint = QPoint(qMax(0, m_controlBar->pos().x()), qMax(0, m_controlBar->pos().y()));

    switch (m_controlBar->location()) {
        case Plasma::TopEdge:
            actualWidth = triggerWidth = m_controlBar->width();
            actualHeight = 1;

            break;
        case Plasma::BottomEdge:
            actualWidth = triggerWidth = m_controlBar->width();
            actualTriggerPoint = triggerPoint = m_controlBar->geometry().bottomLeft();

            break;
        case Plasma::RightEdge:
            actualHeight = triggerHeight = m_controlBar->height();
            actualTriggerPoint = triggerPoint = m_controlBar->geometry().topRight();

            break;
        case Plasma::LeftEdge:
            actualHeight = triggerHeight = m_controlBar->height();

            break;
        default:
            // no hiding unless we're on an edge.
            return;
            break;
    }


    XSetWindowAttributes attributes;
    attributes.override_redirect = True;
    attributes.event_mask = EnterWindowMask;


    attributes.event_mask = EnterWindowMask | LeaveWindowMask | PointerMotionMask |
                            KeyPressMask | KeyPressMask | ButtonPressMask |
                            ButtonReleaseMask | ButtonMotionMask |
                            KeymapStateMask | VisibilityChangeMask |
                            StructureNotifyMask | ResizeRedirectMask |
                            SubstructureNotifyMask |
                            SubstructureRedirectMask | FocusChangeMask |
                            PropertyChangeMask | ColormapChangeMask | OwnerGrabButtonMask;

    unsigned long valuemask = CWOverrideRedirect | CWEventMask;
    m_unhideTrigger = XCreateWindow(QX11Info::display(), QX11Info::appRootWindow(),
                                    triggerPoint.x(), triggerPoint.y(), triggerWidth, triggerHeight,
                                    0, CopyFromParent, InputOnly, CopyFromParent,
                                    valuemask, &attributes);

    XMapWindow(QX11Info::display(), m_unhideTrigger);
    m_unhideTriggerGeom = QRect(triggerPoint, QSize(triggerWidth, triggerHeight));
    m_triggerZone = QRect(actualTriggerPoint, QSize(actualWidth, actualHeight));
#endif
}

void PlasmaApp::destroyUnHideTrigger()
{
#ifdef Q_WS_X11
    if (m_unhideTrigger != None) {
        XDestroyWindow(QX11Info::display(), m_unhideTrigger);
        m_unhideTrigger = None;
        m_triggerZone = m_unhideTriggerGeom = QRect();
    }
#endif
}

#include "plasmaapp.moc"
