/* SUSE2 KWin window decoration
  Copyright (C) 2005 Gerd Fleischer <gerdfleischer@web.de>

  based on the window decoration "Plastik"
  Copyright (C) 2003 Sandro Giessl <ceebx@users.sourceforge.net>

  based on the window decoration "Web":
  Copyright (C) 2001 Rik Hemsley (rikkus) <rik@kde.org>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU 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
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  Boston, MA  02110-1301  USA.
*/

//#include <kdebug.h>

#include <tqbuttongroup.h>
#include <tqcheckbox.h>
#include <tqradiobutton.h>
#include <tqslider.h>
#include <tqspinbox.h>
#include <tqwhatsthis.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqcombobox.h>

#include <tdeversion.h>
#include <tdeconfig.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <kurlrequester.h>
#include <kstandarddirs.h>
#include <kurl.h>
#include <tdefiledialog.h>
#include <knuminput.h>
#include <tdefileitem.h>
#include <kcolorbutton.h>

#include "config.h"
#include "configdialog.h"

SUSE2Config::SUSE2Config(TDEConfig *config, TQWidget *parent)
    : TQObject(parent), m_config(0), m_dialog(0)
{
    m_parent = parent;
    // create the configuration object
    m_config = new TDEConfig("twinSUSE2rc");
    TDEGlobal::locale()->insertCatalogue("twin_clients");
    TDEGlobal::locale()->insertCatalogue("twin-style-suse2");

    // create and show the configuration dialog
    m_dialog = new ConfigDialog(parent);

    m_dialog->show();

    // load the configuration
    load(config);
    toggleIconSettings(m_dialog->useTitleProps->isChecked());

    // setup the connections
    connect(m_dialog->titleAlign, TQ_SIGNAL(clicked(int)), TQ_SIGNAL(changed()));
    connect(m_dialog->roundCorners, TQ_SIGNAL(clicked(int)), TQ_SIGNAL(changed()));
    connect(m_dialog->menuClose, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(changed()));
    connect(m_dialog->titleShadow, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(changed()));
    connect(m_dialog->addSpace, TQ_SIGNAL(valueChanged(int)), TQ_SIGNAL(changed()));
    connect(m_dialog->titlebarStyle, TQ_SIGNAL(activated(int)), TQ_SIGNAL(changed()));

    connect(m_dialog->buttonType, TQ_SIGNAL(activated(int)), TQ_SIGNAL(changed()));
    connect(m_dialog->customColors, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(changed()));
    connect(m_dialog->useTitleProps, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(changed()));
    connect(m_dialog->animateButtons, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(changed()));
    connect(m_dialog->redCloseButton, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(changed()));
    connect(m_dialog->iconSize, TQ_SIGNAL(valueChanged(int)), TQ_SIGNAL(changed()));
    connect(m_dialog->customIconColors, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(changed()));
    connect(m_dialog->activeFgColor, TQ_SIGNAL(changed(const TQColor &)), TQ_SIGNAL(changed()));
    connect(m_dialog->activeBgColor, TQ_SIGNAL(changed(const TQColor &)), TQ_SIGNAL(changed()));
    connect(m_dialog->inactiveFgColor, TQ_SIGNAL(changed(const TQColor &)), TQ_SIGNAL(changed()));
    connect(m_dialog->inactiveBgColor, TQ_SIGNAL(changed(const TQColor &)), TQ_SIGNAL(changed()));
    connect(m_dialog->iconShadow, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(changed()));

    connect(m_dialog->titleBarLogo, TQ_SIGNAL(toggled(bool)), TQ_SIGNAL(changed()));
    connect(m_dialog->titleBarLogoOffset, TQ_SIGNAL(valueChanged(int)), TQ_SIGNAL(changed()));

    connect(m_dialog->useTitleProps, TQ_SIGNAL(toggled(bool)), TQ_SLOT(toggleIconSettings(bool)));
    connect(m_dialog->selectButton, TQ_SIGNAL(clicked()), TQ_SLOT(selectImage()));
}

SUSE2Config::~SUSE2Config()
{
    if (m_dialog) delete m_dialog;
    if (m_config) delete m_config;
}

void SUSE2Config::load(TDEConfig *)
{
    m_config->setGroup("General");

    TQString alignValue = m_config->readEntry("TitleAlignment", "AlignLeft");
    TQRadioButton *alignButton = (TQRadioButton*)m_dialog->titleAlign->child(alignValue.latin1());
    if (alignButton) alignButton->setChecked(true);

    TQString roundValue = m_config->readEntry("RoundCorners", "NotMaximized");
    TQRadioButton *roundButton = (TQRadioButton*)m_dialog->roundCorners->child(roundValue.latin1());
    if (roundButton) roundButton->setChecked(true);

    bool menuClose = m_config->readBoolEntry("CloseOnMenuDoubleClick", false);
    m_dialog->menuClose->setChecked(menuClose);

    bool titleShadow = m_config->readBoolEntry("TitleShadow", true);
    m_dialog->titleShadow->setChecked(titleShadow);

    int addSpace = m_config->readNumEntry("AddSpace", 4);
    m_dialog->addSpace->setValue(addSpace);

    int titlebarStyle = m_config->readNumEntry("TitleBarStyle", 0);
    m_dialog->titlebarStyle->setCurrentItem(titlebarStyle);

    int titleButtonType = m_config->readNumEntry("TitleBarButtonType", 2);
    m_dialog->buttonType->setCurrentItem(titleButtonType);

    bool customColors = m_config->readBoolEntry("CustomColors", false);
    m_dialog->customColors->setChecked(customColors);

    bool useTitleProps = m_config->readBoolEntry("UseTitleProps", true);
    m_dialog->useTitleProps->setChecked(useTitleProps);

    bool animateButtons = m_config->readBoolEntry("AnimateButtons", true);
    m_dialog->animateButtons->setChecked(animateButtons);

    bool redCloseButton = m_config->readBoolEntry("RedCloseButton", false);
    m_dialog->redCloseButton->setChecked(redCloseButton);

    int iconSize = m_config->readNumEntry("IconSize", 45);
    m_dialog->iconSize->setValue(iconSize);

    bool customIconColors = m_config->readBoolEntry("CustomIconColors", false);
    m_dialog->customIconColors->setChecked(customIconColors);

    TQColor afgcolor = TQColor(10, 20, 40);
    TQColor aFgColor = m_config->readColorEntry("AFgColor", &afgcolor);
    m_dialog->activeFgColor->setColor(aFgColor);

    TQColor abgcolor = TQColor(210, 220, 240);
    TQColor aBgColor = m_config->readColorEntry("ABgColor", &abgcolor);
    m_dialog->activeBgColor->setColor(aBgColor);

    TQColor ifgcolor = TQColor(40, 40, 40);
    TQColor iFgColor = m_config->readColorEntry("IFgColor", &ifgcolor);
    m_dialog->inactiveFgColor->setColor(iFgColor);

    TQColor ibgcolor = TQColor(240, 240, 240);
    TQColor iBgColor = m_config->readColorEntry("AFgColor", &ibgcolor);
    m_dialog->inactiveBgColor->setColor(iBgColor);

    bool iconShadow = m_config->readBoolEntry("IconShadow", true);
    m_dialog->iconShadow->setChecked(iconShadow);

    bool titleBarLogo = m_config->readBoolEntry("TitleBarLogo", false);
    m_dialog->titleBarLogo->setChecked(titleBarLogo);

    int titleBarLogoOffset = m_config->readNumEntry("TitleBarLogoOffset", 3);
    m_dialog->titleBarLogoOffset->setValue(titleBarLogoOffset);

    TQString titleBarImage = locate("data", "twin/pics/titlebar_decor.png");
    titlebarLogoURL = m_config->readEntry("TitleBarLogoURL", titleBarImage);
    TQImage tmpLogo(titlebarLogoURL);
    m_dialog->logoImage->setPixmap(TQPixmap(tmpLogo.smoothScale(120, 20, TQImage::ScaleMin)));

}

void SUSE2Config::save(TDEConfig *) const
{
    m_config->setGroup("General");

    TQRadioButton *alignButton = (TQRadioButton*)m_dialog->titleAlign->selected();
    if (alignButton) m_config->writeEntry("TitleAlignment", TQString(alignButton->name()));
    TQRadioButton *roundButton = (TQRadioButton*)m_dialog->roundCorners->selected();
    if (roundButton) m_config->writeEntry("RoundCorners", TQString(roundButton->name()));
    m_config->writeEntry("CloseOnMenuDoubleClick", m_dialog->menuClose->isChecked());
    m_config->writeEntry("TitleShadow", m_dialog->titleShadow->isChecked());
    m_config->writeEntry("AddSpace", m_dialog->addSpace->value());
    m_config->writeEntry("TitleBarStyle", m_dialog->titlebarStyle->currentItem());

    m_config->writeEntry("TitleBarButtonType", m_dialog->buttonType->currentItem());
    m_config->writeEntry("CustomColors", m_dialog->customColors->isChecked());
    m_config->writeEntry("UseTitleProps", m_dialog->useTitleProps->isChecked());
    m_config->writeEntry("AnimateButtons", m_dialog->animateButtons->isChecked());
    m_config->writeEntry("RedCloseButton", m_dialog->redCloseButton->isChecked());
    m_config->writeEntry("IconSize", m_dialog->iconSize->value());
    m_config->writeEntry("CustomIconColors", m_dialog->customIconColors->isChecked());
    m_config->writeEntry("AFgColor", m_dialog->activeFgColor->color());
    m_config->writeEntry("ABgColor", m_dialog->activeBgColor->color());
    m_config->writeEntry("IFgColor", m_dialog->inactiveFgColor->color());
    m_config->writeEntry("IBgColor", m_dialog->inactiveBgColor->color());
    m_config->writeEntry("IconShadow", m_dialog->iconShadow->isChecked());

    m_config->writeEntry("TitleBarLogo", m_dialog->titleBarLogo->isChecked());
    m_config->writeEntry("TitleBarLogoOffset", m_dialog->titleBarLogoOffset->value());
    m_config->writeEntry("TitleBarLogoURL", TQString(titlebarLogoURL));
    m_config->sync();
}

void SUSE2Config::defaults()
{
    TQRadioButton *alignButton = (TQRadioButton*)m_dialog->titleAlign->child("AlignLeft");
    if (alignButton) alignButton->setChecked(true);
    TQRadioButton *roundButton = (TQRadioButton*)m_dialog->roundCorners->child("NotMaximized");
    if (roundButton) roundButton->setChecked(true);
    m_dialog->menuClose->setChecked(false);
    m_dialog->titleShadow->setChecked(true);
    m_dialog->addSpace->setValue(4);
    m_dialog->titlebarStyle->setCurrentItem(0);

    m_dialog->buttonType->setCurrentItem(2);
    m_dialog->customColors->setChecked(false);
    m_dialog->useTitleProps->setChecked(true);
    m_dialog->animateButtons->setChecked(true);
    m_dialog->redCloseButton->setChecked(false);
    m_dialog->iconSize->setValue(45);
    m_dialog->customIconColors->setChecked(false);
    m_dialog->activeFgColor->setColor(TQColor(10, 20, 40));
    m_dialog->activeBgColor->setColor(TQColor(210, 220, 240));
    m_dialog->inactiveFgColor->setColor(TQColor(40, 40, 40));
    m_dialog->inactiveBgColor->setColor(TQColor(240, 240, 240));
    m_dialog->iconShadow->setChecked(true);

    m_dialog->titleBarLogo->setChecked(false);
    m_dialog->titleBarLogoOffset->setValue(3);
    titlebarLogoURL = locate("data", "twin/pics/SUSE2/titlebar_decor.png");
    TQImage tmpLogo(titlebarLogoURL);
    m_dialog->logoImage->setPixmap(TQPixmap(tmpLogo.smoothScale(120, 20, TQImage::ScaleMin)));
}

void SUSE2Config::toggleIconSettings(bool checked) const
{
    m_dialog->customIconColors->setEnabled(!checked);
    m_dialog->activeLbl->setEnabled(!checked);
    m_dialog->activeDownLbl->setEnabled(!checked);
    m_dialog->inactiveLbl->setEnabled(!checked);
    m_dialog->inactiveDownLbl->setEnabled(!checked);
    m_dialog->activeFgColor->setEnabled(!checked);
    m_dialog->activeBgColor->setEnabled(!checked);
    m_dialog->inactiveFgColor->setEnabled(!checked);
    m_dialog->inactiveBgColor->setEnabled(!checked);
    m_dialog->iconShadow->setEnabled(!checked);
}

void SUSE2Config::selectImage()
{
    KURL logoURL = KFileDialog::getImageOpenURL(titlebarLogoURL, m_parent, i18n("Select Logo Image"));
    KFileItem tmpFileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, logoURL);
    if (!logoURL.isEmpty() && tmpFileItem.isFile() && tmpFileItem.isReadable()) {
        titlebarLogoURL = logoURL.path();
        TQImage tmpLogo(titlebarLogoURL);
        m_dialog->logoImage->setPixmap(TQPixmap(tmpLogo.smoothScale(120, 20, TQImage::ScaleMin)));
        emit changed();
    }
}

//////////////////////////////////////////////////////////////////////////////
// Plugin Stuff                                                             //
//////////////////////////////////////////////////////////////////////////////

extern "C"
{
    TDE_EXPORT TQObject *allocate_config(TDEConfig *config, TQWidget *parent) {
        return (new SUSE2Config(config, parent));
    }
}

#include "config.moc"
