/***************************************************************************
 *   Copyright (C) 2005 by yunfan                                          *
 *   yunfan_zg@163.com                                                     *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "evasyssettingwindow.h"
#include "evaresource.h"
#include "evausersetting.h"

#include <qcombobox.h>
#include <qpushbutton.h>
#include <qslider.h>
#include <qtextedit.h>
#include <qlineedit.h>
#include <qspinbox.h>
#include <qcheckbox.h>
#include <qlabel.h>
#include <qfiledialog.h>
#include <klocale.h>
#include <inttypes.h>

EvaSysSettingWindow::EvaSysSettingWindow( EvaImageResource * res, EvaUserSetting * setting , QWidget *parent)
	: EvaSysSettingUIBase(parent, 0, WType_Dialog | WShowModal)
{
	userSetting = setting;
	lblLogo->setPixmap(*(res->getIcon("SETTING_LOGO")));
	loadSetting();
	slotCbbAutoNoActivated(0);
	slotCbbQuickNoActivated(0);
	QObject::connect(pbFaceDefault, SIGNAL(clicked()), SLOT(slotPbFaceDefaultClicked()));
	QObject::connect(pbSave, SIGNAL(clicked()), SLOT(slotPbSaveClicked()));
	QObject::connect(pbClose, SIGNAL(clicked()), SLOT(slotPbCloseClicked()));
	
	QObject::connect(pbTheme, SIGNAL(clicked()), SLOT(slotPbThemeClicked()));
	QObject::connect(pbSound, SIGNAL(clicked()), SLOT(slotPbSoundClicked()));
	
	QObject::connect(cbbAutoNo, SIGNAL(activated(int)), SLOT(slotCbbAutoNoActivated(int)));
	QObject::connect(pbAutoNew, SIGNAL(clicked()), SLOT(slotPbAutoNewClicked()));
	QObject::connect(pbAutoDelete, SIGNAL(clicked()), SLOT(slotPbAutoDeleteClicked()));
	QObject::connect(teAutoMessage, SIGNAL(textChanged()), SLOT(slotTeAutoTextChanged()));
	
	QObject::connect(cbbQuickNo, SIGNAL(activated(int)), SLOT(slotCbbQuickNoActivated(int)));
	QObject::connect(pbQuickNew, SIGNAL(clicked()), SLOT(slotPbQuickNewClicked()));
	QObject::connect(pbQuickDelete, SIGNAL(clicked()), SLOT(slotPbQuickDeleteClicked()));
	QObject::connect(teQuickMessage, SIGNAL(textChanged()), SLOT(slotTeQuickTextChanged()));
}

EvaSysSettingWindow::~ EvaSysSettingWindow( )
{
}

void EvaSysSettingWindow::saveSetting( )
{
	userSetting->setShowBudyOnlineNotifyEnabled(chbOnline->isChecked());
	userSetting->setShowMessageTipEnabled(chbMessage->isChecked());
	userSetting->setShowOnlineEnabled(chbShowOnlineUsers->isChecked());
	userSetting->setShowSystemBroadcastEnabled(chbSystemBroadcast->isChecked());
	userSetting->setSoundEnabled(chbSound->isChecked());
	userSetting->setSendKeyEnterEnabled(chbSendKey->isChecked());
	
	userSetting->setFaceSize(QSize(sliderFaceSize->value(), sliderFaceSize->value()));
	userSetting->setPageSize(sbPageSize->value());
	
	userSetting->setThemeDir(leThemePath->text());
	userSetting->setSoundDir(leSoundPath->text());
	//userSetting->setShowOnlineEnabled(false);
	
	userSetting->setAutoReplyList(autoList);
	userSetting->setQuickReplyList(quickList);
	
	userSetting->saveSettings();
}

void EvaSysSettingWindow::loadSetting( const bool isLoadDefault)
{	
	chbOnline->setChecked(userSetting->isShowBudyOnlineNotifyEnabled());
	chbMessage->setChecked(userSetting->isShowMessageTipEnabled());
	chbShowOnlineUsers->setChecked(userSetting->isShowOnlineEnabled());
	chbSystemBroadcast->setChecked(userSetting->isShowSystemBroadcastEnabled());
	chbSound->setChecked(userSetting->isSoundEnabled());
	chbSendKey->setChecked(userSetting->isSendKeyEnterEnabled());
		
	sliderFaceSize->setValue(userSetting->getFaceSize().width()); // assume width == height
	sbPageSize->setValue(userSetting->getPageSize());
	
	chbAutoReply->setChecked(userSetting->isAutoReplyEnabled());
	
	leThemePath->setText(userSetting->getThemeDir());
	leSoundPath->setText(userSetting->getSoundDir());
	//userSetting->isShowOnlineEnabled()
	if(!isLoadDefault){
		autoList = userSetting->getAutoReplyList();
		cbbAutoNo->clear();
		for(uint i=0; i<autoList.size(); i++)
			cbbAutoNo->insertItem(QString::number(i+1));
		slotCbbAutoNoActivated(0);
				
		quickList = userSetting->getQuickReplyList();
		cbbQuickNo->clear();
		for(uint i=0; i<quickList.size(); i++)
			cbbQuickNo->insertItem(QString::number(i+1));
		slotCbbQuickNoActivated(0);
	}
}

void EvaSysSettingWindow::slotPbFaceDefaultClicked( )
{
	userSetting->loadDefaultSettings();
	loadSetting(false);
}

void EvaSysSettingWindow::slotPbSaveClicked( )
{
	saveSetting( );
}

void EvaSysSettingWindow::slotPbCloseClicked( )
{
	close();
}

void EvaSysSettingWindow::slotPbThemeClicked( )
{
	QFileDialog *fd = new QFileDialog(this, i18n("Choose Theme Path  - Eva"), true);
	fd->setMode(QFileDialog::DirectoryOnly);
	fd->setFilter("Theme (eva.theme)");
	QString path;
	if(fd->exec() == QDialog::Accepted)
		path = fd->selectedFile();
	else
		return;
		
	//path = path.left(path.length() - strlen("/eva.theme"));
	
	leThemePath->setText(path);
}

void EvaSysSettingWindow::slotPbSoundClicked( )
{
	QFileDialog *fd = new QFileDialog(this, i18n("Choose Sound Path  - Eva"), true);
	fd->setMode(QFileDialog::DirectoryOnly);
	fd->setFilter("all (*)");
	QString path;
	if(fd->exec() == QDialog::Accepted)
		path = fd->selectedFile();
	else
		return;
	
	leSoundPath->setText(path);
}

void EvaSysSettingWindow::slotCbbAutoNoActivated( int index)
{
	if((uint)index>=autoList.size()) return;
	std::list<QString>::iterator iter = autoList.begin();
	for(int i=0; i<index; i++)
		iter++;
	teAutoMessage->setText(*iter);
}

void EvaSysSettingWindow::slotPbAutoNewClicked( )
{	
	cbbAutoNo->insertItem(QString::number(cbbAutoNo->count()+1));
	cbbAutoNo->setCurrentItem(cbbAutoNo->count()-1);
	autoList.push_back(teAutoMessage->text());
	teAutoMessage->selectAll(true);
}

void EvaSysSettingWindow::slotPbAutoDeleteClicked( )
{
	cbbAutoNo->removeItem(cbbAutoNo->count()-1);
	QString txt = teAutoMessage->text();
	std::list<QString>::iterator iter;
	for(iter = autoList.begin(); iter!=autoList.end(); ++iter){
		if(*iter == txt){			
			autoList.remove(*iter);			
			break;
		}
	}
	slotCbbAutoNoActivated(0);
}

void EvaSysSettingWindow::slotTeAutoTextChanged()
{
	int index = cbbAutoNo->currentItem();
	QString txt = teAutoMessage->text();
	if((uint)index > autoList.size()) return;
	
	std::list<QString>::iterator iter = autoList.begin();
	for(int i=0; i<index; i++)
		iter++;
	*iter = txt;
}

void EvaSysSettingWindow::slotCbbQuickNoActivated( int index)
{
	if((uint)index>=quickList.size()) return;
	std::list<QString>::iterator iter = quickList.begin();
	for(int i=0; i<index; i++)
		iter++;
	teQuickMessage->setText(*iter);
}

void EvaSysSettingWindow::slotPbQuickNewClicked( )
{
	cbbQuickNo->insertItem(QString::number(cbbQuickNo->count()+1));
	cbbQuickNo->setCurrentItem(cbbQuickNo->count()-1);
	quickList.push_back(teQuickMessage->text());
	teQuickMessage->selectAll(true);
}

void EvaSysSettingWindow::slotPbQuickDeleteClicked( )
{
	cbbQuickNo->removeItem(cbbQuickNo->count()-1);
	QString txt = teQuickMessage->text();
	std::list<QString>::iterator iter;
	for(iter= quickList.begin(); iter!=quickList.end(); ++iter){
		if(*iter == txt){			
			quickList.remove(*iter);			
			break;
		}
	}
	slotCbbQuickNoActivated(0);
}

void EvaSysSettingWindow::slotTeQuickTextChanged()
{
	int index = cbbQuickNo->currentItem();
	QString txt = teQuickMessage->text();
	if((uint)index > quickList.size()) return;
	
	std::list<QString>::iterator iter = quickList.begin();
	for(int i=0; i<index; i++)
		iter++;
	*iter = txt;
}


