/***************************************************************************
 *   Copyright (C) 2006 by Sascha Hlusiak                                  *
 *   Spam84@gmx.de                                                         *
 *                                                                         *
 *   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; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include <tdeconfig.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <tqbuttongroup.h>
#include <tqlabel.h>
#include <tqgroupbox.h>
#include <tqbuttongroup.h>
#include <tqcheckbox.h>
#include <tqradiobutton.h>
#include <tqwhatsthis.h>
#include <tqspinbox.h>
#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqwidgetstack.h>
#include <tqlineedit.h>
#include <tqfiledialog.h>
#include <kcolorbutton.h>
#include <tdefiledialog.h>
#include <tqpicture.h>
#include <tdeapplication.h>
#include <kurlrequester.h>

#include "configdialog.h"
#include "infodialog.h"
#include "crystalconfig.h"


CrystalConfig::CrystalConfig(TDEConfig*, TQWidget* parent)
    : TQObject(parent), config_(0), dialog_(0)
{
	TDEGlobal::locale()->insertCatalogue("twin_clients");
	TDEGlobal::locale()->insertCatalogue("twin-style-crystal");

	config_ = new TDEConfig("twincrystalrc");
	dialog_ = new ConfigDialog(parent);
	dialog_->show();
		
	connect(dialog_->titlealign, TQ_SIGNAL(clicked(int)),this, TQ_SLOT(selectionChanged(int)));
	
	connect(dialog_->drawCaption, TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->textshadow, TQ_SIGNAL(stateChanged(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->tooltip,TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->wheelTask,TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	
	connect(dialog_->trackdesktop, TQ_SIGNAL(stateChanged(int)),this, TQ_SLOT(selectionChanged(int)));
	
	connect(dialog_->shade1, TQ_SIGNAL(valueChanged(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->shade2, TQ_SIGNAL(valueChanged(int)),this, TQ_SLOT(selectionChanged(int)));
	
	connect(dialog_->frame1, TQ_SIGNAL(activated(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->frame2, TQ_SIGNAL(activated(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->frameColor1, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->frameColor2, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	
	connect(dialog_->inline1, TQ_SIGNAL(activated(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->inline2, TQ_SIGNAL(activated(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->inlineColor1, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->inlineColor2, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
		
	connect(dialog_->type1,TQ_SIGNAL(activated(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->type2,TQ_SIGNAL(activated(int)),this,TQ_SLOT(selectionChanged(int)));
	
	connect(dialog_->enableTransparency,TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	
	connect(dialog_->borderwidth, TQ_SIGNAL(valueChanged(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->titlebarheight, TQ_SIGNAL(valueChanged(int)),this, TQ_SLOT(selectionChanged(int)));
	
	connect(dialog_->tlc, TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->trc, TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->blc, TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->brc, TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->buttonColor1, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->buttonColor2, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->buttonColor3, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->minColor1, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->minColor2, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->minColor3, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->maxColor1, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->maxColor2, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->maxColor3, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->closeColor1, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->closeColor2, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	connect(dialog_->closeColor3, TQ_SIGNAL(changed(const TQColor&)),this,TQ_SLOT(colorChanged(const TQColor&)));
	
	connect(dialog_->hover, TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->animateHover, TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->buttonTheme, TQ_SIGNAL(activated(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->tintButtons, TQ_SIGNAL(toggled(bool)),this,TQ_SLOT(boolChanged(bool)));
	connect(dialog_->menuimage, TQ_SIGNAL(stateChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	
	connect(dialog_->repaintMode, TQ_SIGNAL(clicked(int)),this, TQ_SLOT(selectionChanged(int)));
	
	connect(dialog_->updateTime, TQ_SIGNAL(valueChanged(int)),this, TQ_SLOT(selectionChanged(int)));
		
	connect(dialog_->infoButton, TQ_SIGNAL(clicked(void)),this,TQ_SLOT(infoDialog(void)));

	connect(dialog_->active_blur, TQ_SIGNAL(valueChanged(int)),this,TQ_SLOT(selectionChanged(int)));
	connect(dialog_->inactive_blur, TQ_SIGNAL(valueChanged(int)),this,TQ_SLOT(selectionChanged(int)));

	connect(dialog_->userPicture1, TQ_SIGNAL(stateChanged(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->userPicture2, TQ_SIGNAL(stateChanged(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->activeFile,TQ_SIGNAL(textChanged(const TQString&)),this,TQ_SLOT(textChanged( const TQString& )));
	connect(dialog_->inactiveFile,TQ_SIGNAL(textChanged(const TQString&)),this,TQ_SLOT(textChanged( const TQString& )));


	connect(dialog_->overlay_active, TQ_SIGNAL(activated(int)),this, TQ_SLOT(overlay_active_changed(int)));
	connect(dialog_->overlay_inactive, TQ_SIGNAL(activated(int)),this, TQ_SLOT(overlay_inactive_changed(int)));

	connect(dialog_->overlay_active_file,TQ_SIGNAL(textChanged(const TQString&)),this,TQ_SLOT(textChanged(const TQString &)));
	connect(dialog_->overlay_inactive_file,TQ_SIGNAL(textChanged(const TQString&)),this,TQ_SLOT(textChanged(const TQString &)));


	connect(dialog_->logoEnabled, TQ_SIGNAL(clicked(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->logoFile, TQ_SIGNAL(textChanged(const TQString &)),this, TQ_SLOT(logoTextChanged(const TQString&)));
	connect(dialog_->logoStretch, TQ_SIGNAL(activated(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->logoActive, TQ_SIGNAL(stateChanged(int)),this, TQ_SLOT(selectionChanged(int)));
	connect(dialog_->logoDistance,TQ_SIGNAL(valueChanged(int)),this,TQ_SLOT(selectionChanged(int)));

	load(config_);
}

CrystalConfig::~CrystalConfig()
{
	if (dialog_) delete dialog_;
	if (config_) delete config_;
}

void CrystalConfig::selectionChanged(int)
{
	emit changed();
}

void CrystalConfig::load(TDEConfig*)
{
	TQColor color(255,255,255);

	config_->setGroup("General");

	TQString value = config_->readEntry("TitleAlignment", "AlignHCenter");
	TQRadioButton *button = (TQRadioButton*)dialog_->titlealign->child(value.utf8());
	if (button) button->setChecked(true);
	
	dialog_->drawCaption->setChecked(config_->readBoolEntry("DrawCaption",true));
	dialog_->textshadow->setChecked(config_->readBoolEntry("TextShadow",true));
	dialog_->tooltip->setChecked(config_->readBoolEntry("CaptionTooltip",true));
	dialog_->wheelTask->setChecked(config_->readBoolEntry("WheelTask",false));

	dialog_->enableTransparency->setChecked(config_->readBoolEntry("EnableTransparency",true));
	dialog_->trackdesktop->setChecked(config_->readBoolEntry("TrackDesktop",false));

	dialog_->frame1->setCurrentItem(config_->readNumEntry("ActiveFrame",1));
	color=TQColor(192,192,192);
	dialog_->frameColor1->setColor(config_->readColorEntry("FrameColor1",&color));
	dialog_->frame2->setCurrentItem(config_->readNumEntry("InactiveFrame",1));
	color=TQColor(192,192,192);
	dialog_->frameColor2->setColor(config_->readColorEntry("FrameColor2",&color));

	dialog_->inline1->setCurrentItem(config_->readNumEntry("ActiveInline",0));
	color=TQColor(192,192,192);
	dialog_->inlineColor1->setColor(config_->readColorEntry("InlineColor1",&color));
	dialog_->inline2->setCurrentItem(config_->readNumEntry("InactiveInline",0));
	color=TQColor(192,192,192);
	dialog_->inlineColor2->setColor(config_->readColorEntry("InlineColor2",&color));


	dialog_->borderwidth->setValue(config_->readNumEntry("Borderwidth",5));
	dialog_->titlebarheight->setValue(config_->readNumEntry("Titlebarheight",19));

	int active=config_->readNumEntry("ActiveShade",30);
	dialog_->shade1->setValue(active);

	active=config_->readNumEntry("InactiveShade",-30);
	dialog_->shade2->setValue(active);

	dialog_->type1->setCurrentItem(config_->readNumEntry("ActiveMode",0));
	dialog_->type2->setCurrentItem(config_->readNumEntry("InactiveMode",2));

	int cornersFlag = config_->readNumEntry("RoundCorners",TOP_LEFT & TOP_RIGHT );
	dialog_->tlc->setChecked( cornersFlag & TOP_LEFT );
	dialog_->trc->setChecked( cornersFlag & TOP_RIGHT );
	dialog_->blc->setChecked( cornersFlag & BOT_LEFT );
	dialog_->brc->setChecked( cornersFlag & BOT_RIGHT );

	dialog_->hover->setChecked(config_->readBoolEntry("HoverEffect",true));
	dialog_->animateHover->setChecked(config_->readBoolEntry("AnimateHover",true));
	dialog_->menuimage->setChecked(config_->readBoolEntry("MenuImage",true));

	color=TQColor(255,255,255);
	dialog_->buttonColor1->setColor(config_->readColorEntry("ButtonColor",&color));
	dialog_->buttonColor2->setColor(config_->readColorEntry("ButtonColor2",&color));
	dialog_->buttonColor3->setColor(config_->readColorEntry("ButtonColor3",&color));
	dialog_->minColor1->setColor(config_->readColorEntry("MinColor",&color));
	dialog_->minColor2->setColor(config_->readColorEntry("MinColor2",&color));
	dialog_->minColor3->setColor(config_->readColorEntry("MinColor3",&color));
	dialog_->maxColor1->setColor(config_->readColorEntry("MaxColor",&color));
	dialog_->maxColor2->setColor(config_->readColorEntry("MaxColor2",&color));
	dialog_->maxColor3->setColor(config_->readColorEntry("MaxColor3",&color));
	dialog_->closeColor1->setColor(config_->readColorEntry("CloseColor",&color));
	dialog_->closeColor2->setColor(config_->readColorEntry("CloseColor2",&color));
	dialog_->closeColor3->setColor(config_->readColorEntry("CloseColor3",&color));

	dialog_->tintButtons->setChecked(config_->readBoolEntry("TintButtons",dialog_->buttonColor1->color()!=TQColor(255,255,255)));

	dialog_->buttonTheme->setCurrentItem(config_->readNumEntry("ButtonTheme",8));
	
	dialog_->updateTime->setValue(config_->readNumEntry("RepaintTime",200));
	button=(TQRadioButton*)dialog_->repaintMode->find(config_->readNumEntry("RepaintMode",1));
	if (button)button->setChecked(true);

	dialog_->active_blur->setValue(config_->readNumEntry("ActiveBlur",0));
	dialog_->inactive_blur->setValue(config_->readNumEntry("InactiveBlur",0));


	dialog_->activeFile->setURL(config_->readEntry("ActiveUserdefinedPicture",""));
	dialog_->userPicture1->setChecked(config_->readBoolEntry("ActiveUserdefined",false));
	dialog_->inactiveFile->setURL(config_->readEntry("InactiveUserdefinedPicture",""));
	dialog_->userPicture2->setChecked(config_->readBoolEntry("InactiveUserdefined",false));


	dialog_->overlay_active->setCurrentItem(config_->readNumEntry("OverlayModeActive",0));
	dialog_->overlay_active_file->setURL(config_->readEntry("OverlayFileActive",""));
	overlay_active_changed(dialog_->overlay_active->currentItem());

	dialog_->overlay_inactive->setCurrentItem(config_->readNumEntry("OverlayModeInactive",0));
	dialog_->overlay_inactive_file->setURL(config_->readEntry("OverlayFileInactive",""));
	overlay_inactive_changed(dialog_->overlay_inactive->currentItem());

	dialog_->logoEnabled->setButton(config_->readNumEntry("LogoAlignment",1));
	dialog_->logoFile->setURL(config_->readEntry("LogoFile",""));
	dialog_->logoActive->setChecked(config_->readBoolEntry("LogoActive",1));
	dialog_->logoStretch->setCurrentItem(config_->readNumEntry("LogoStretch",0));
	dialog_->logoDistance->setValue(config_->readNumEntry("LogoDistance",0));
	updateLogo();
}

void CrystalConfig::save(TDEConfig*)
{
	config_->setGroup("General");

	TQRadioButton *button = (TQRadioButton*)dialog_->titlealign->selected();
	if (button) config_->writeEntry("TitleAlignment", TQString(button->name()));
	config_->writeEntry("DrawCaption",dialog_->drawCaption->isChecked());
	config_->writeEntry("TextShadow",dialog_->textshadow->isChecked());
	config_->writeEntry("CaptionTooltip",dialog_->tooltip->isChecked());
	config_->writeEntry("WheelTask",dialog_->wheelTask->isChecked());

	config_->writeEntry("EnableTransparency",dialog_->enableTransparency->isChecked());
	config_->writeEntry("TrackDesktop",dialog_->trackdesktop->isChecked());

	config_->writeEntry("Borderwidth",dialog_->borderwidth->value());
	config_->writeEntry("Titlebarheight",dialog_->titlebarheight->value());

	config_->writeEntry("ActiveShade",dialog_->shade1->value());
	config_->writeEntry("InactiveShade",dialog_->shade2->value());
	config_->writeEntry("ActiveFrame",dialog_->frame1->currentItem());
	config_->writeEntry("FrameColor1",dialog_->frameColor1->color());
	config_->writeEntry("InactiveFrame",dialog_->frame2->currentItem());
	config_->writeEntry("ActiveMode",dialog_->type1->currentItem());
	config_->writeEntry("InactiveMode",dialog_->type2->currentItem());
	config_->writeEntry("FrameColor2",dialog_->frameColor2->color());

	config_->writeEntry("ActiveInline",dialog_->inline1->currentItem());
	config_->writeEntry("InlineColor1",dialog_->inlineColor1->color());
	config_->writeEntry("InactiveInline",dialog_->inline2->currentItem());
	config_->writeEntry("InlineColor2",dialog_->inlineColor2->color());

	config_->writeEntry("ButtonColor",dialog_->buttonColor1->color());
	config_->writeEntry("ButtonColor2",dialog_->buttonColor2->color());
	config_->writeEntry("ButtonColor3",dialog_->buttonColor3->color());
	config_->writeEntry("MinColor",dialog_->minColor1->color());
	config_->writeEntry("MinColor2",dialog_->minColor2->color());
	config_->writeEntry("MinColor3",dialog_->minColor3->color());
	config_->writeEntry("MaxColor",dialog_->maxColor1->color());
	config_->writeEntry("MaxColor2",dialog_->maxColor2->color());
	config_->writeEntry("MaxColor3",dialog_->maxColor3->color());
	config_->writeEntry("CloseColor",dialog_->closeColor1->color());
	config_->writeEntry("CloseColor2",dialog_->closeColor2->color());
	config_->writeEntry("CloseColor3",dialog_->closeColor3->color());

	int cornersFlag = 0;
	if(dialog_->tlc->isChecked()) cornersFlag += TOP_LEFT;
	if(dialog_->trc->isChecked()) cornersFlag += TOP_RIGHT;
	if(dialog_->blc->isChecked()) cornersFlag += BOT_LEFT;
	if(dialog_->brc->isChecked()) cornersFlag += BOT_RIGHT;
	config_->writeEntry("RoundCorners", cornersFlag );
	
	config_->writeEntry("HoverEffect",dialog_->hover->isChecked());
	config_->writeEntry("AnimateHover",dialog_->animateHover->isChecked());
	config_->writeEntry("TintButtons",dialog_->tintButtons->isChecked());
	config_->writeEntry("MenuImage",dialog_->menuimage->isChecked());
	
	config_->writeEntry("ButtonTheme",dialog_->buttonTheme->currentItem());
	config_->writeEntry("RepaintMode",dialog_->repaintMode->selectedId());
	config_->writeEntry("RepaintTime",dialog_->updateTime->value());

	config_->writeEntry("ActiveBlur",dialog_->active_blur->value());
	config_->writeEntry("InactiveBlur",dialog_->inactive_blur->value());

	config_->writeEntry("ActiveUserdefined",dialog_->userPicture1->isChecked());
	config_->writeEntry("ActiveUserdefinedPicture",dialog_->activeFile->url());
	config_->writeEntry("InactiveUserdefined",dialog_->userPicture2->isChecked());
	config_->writeEntry("InactiveUserdefinedPicture",dialog_->inactiveFile->url());

	config_->writeEntry("OverlayModeActive",dialog_->overlay_active->currentItem());
	config_->writeEntry("OverlayFileActive",dialog_->overlay_active_file->url());
	config_->writeEntry("OverlayModeInactive",dialog_->overlay_inactive->currentItem());
	config_->writeEntry("OverlayFileInactive",dialog_->overlay_inactive_file->url());

	config_->writeEntry("LogoAlignment",dialog_->logoEnabled->selectedId());
	config_->writeEntry("LogoFile",dialog_->logoFile->url());
	config_->writeEntry("LogoActive",dialog_->logoActive->isChecked());
	config_->writeEntry("LogoStretch",dialog_->logoStretch->currentItem());
	config_->writeEntry("LogoDistance",dialog_->logoDistance->value());

	config_->sync();
}

void CrystalConfig::infoDialog()
{
	InfoDialog d(dialog_);
	connect((TQLabel*)(d.kURLLabel1),TQ_SIGNAL(leftClickedURL(const TQString&)),TDEApplication::kApplication(),TQ_SLOT(invokeBrowser(const TQString &)));
	d.exec();
}

void CrystalConfig::logoTextChanged(const TQString&)
{
	updateLogo();
	emit changed();
}

void CrystalConfig::overlay_active_changed(int a)
{
	dialog_->overlay_active_file->setEnabled(a==4);
	emit changed();
}

void CrystalConfig::overlay_inactive_changed(int a)
{
	dialog_->overlay_inactive_file->setEnabled(a==4);
	emit changed();
}

void CrystalConfig::updateLogo()
{
	TQPixmap pic;
	pic.load(dialog_->logoFile->url());
	dialog_->logoPreview->setPixmap(pic);
}

void CrystalConfig::defaults()
{
	TQRadioButton *button =
		(TQRadioButton*)dialog_->titlealign->child("AlignHCenter");
	if (button) button->setChecked(true);
	dialog_->shade1->setValue(50);
	dialog_->shade2->setValue(50);
}

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

#include "crystalconfig.moc"
