/***************************************************************************
 *   Copyright (C) 2004 by Hugo Parente Lima                               *
 *   hugo_pl@users.sourceforge.net                                         *
 *                                                                         *
 *   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 "configure.h"
#include "knetstats.h"

// TQt includes
#include <tqstringlist.h>
#include <tqlistbox.h>
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqwidgetstack.h>
#include <tqpopupmenu.h>
// Kde includes
#include <tdeapplication.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <tdeconfig.h>
#include <tdefontrequester.h>
#include <knuminput.h>
#include <kcolorbutton.h>
#include <kcombobox.h>
#include <tdemessagebox.h>
#include <kpushbutton.h>


Configure::Configure(KNetStats* parent, const InterfaceMap& ifs) : ConfigureBase(parent) {
	// Load configuration
	TDEIconLoader* loader = kapp->iconLoader();

	TQPixmap iconPCI = loader->loadIcon("icon_pci.png", TDEIcon::Small, 16);

	// Clone the configuration.
	for (InterfaceMap::ConstIterator it = ifs.begin(); it != ifs.end(); ++it) {
		mInterfaces->insertItem(iconPCI, it.key());
		mConfig[it.key()] = it.data()->options();

//		parent->readInterfaceOptions(*it, &mConfig[*it]);
	}

	mInterfaces->setCurrentItem(0);
	changeInterface(mInterfaces->selectedItem());

	connect(mInterfaces, TQT_SIGNAL(selectionChanged(TQListBoxItem*)), this, TQT_SLOT(changeInterface(TQListBoxItem*)));
	connect(mTheme, TQT_SIGNAL(activated(int)), this, TQT_SLOT(changeTheme(int)));
	//connect(mInterfaces, TQT_SIGNAL(contextMenuRequested(TQListBoxItem*, const TQPoint&)), this, TQT_SLOT(showInterfaceContextMenu(TQListBoxItem*, const TQPoint&)));
}

void Configure::changeInterface(TQListBoxItem* item) {
	TQString interface = item->text();

	if (!mCurrentItem.isEmpty())
	{
		// Salvas as modificações passadas
		KNetStatsView::Options& oldview = mConfig[mCurrentItem];
		// general options
		oldview.mMonitoring = mMonitoring->isChecked();
		oldview.mUpdateInterval = mUpdateInterval->value();
		oldview.mViewMode = (KNetStatsView::Mode) mViewMode->currentItem();
		// txt view options
		oldview.mTxtUplColor = mTxtUplColor->color();
		oldview.mTxtDldColor = mTxtDldColor->color();
		oldview.mTxtFont = mTxtFont->font();
		// icon view
		oldview.mTheme = mTheme->currentItem();
		// chart view
		oldview.mChartUplColor = mChartUplColor->color();
		oldview.mChartDldColor = mChartDldColor->color();
		oldview.mChartBgColor = mChartBgColor->color();
		oldview.mChartTransparentBackground = mChartTransparentBackground->isChecked();
	}

	if (interface == mCurrentItem)
		return;
	// Carrega as opt. da nova interface
	KNetStatsView::Options& view = mConfig[interface];
	// general
	mMonitoring->setChecked(view.mMonitoring);
	mUpdateInterval->setValue(view.mUpdateInterval);
	mViewMode->setCurrentItem(view.mViewMode);
	mWdgStack->raiseWidget(view.mViewMode);
	// txt options
	mTxtUplColor->setColor(view.mTxtUplColor);
	mTxtDldColor->setColor(view.mTxtDldColor);
	mTxtFont->setFont( view.mTxtFont );
	// icon options
	mTheme->setCurrentItem(view.mTheme);
	changeTheme(view.mTheme);
	// chart options
	mChartUplColor->setColor(view.mChartUplColor);
	mChartDldColor->setColor(view.mChartDldColor);
	mChartBgColor->setColor(view.mChartBgColor);
	mChartTransparentBackground->setChecked(view.mChartTransparentBackground);

	mCurrentItem = interface;
}

bool Configure::canSaveConfig()
{
	// update the options
	changeInterface(mInterfaces->item( mInterfaces->currentItem() ));

	bool ok = false;
	for(OptionsMap::ConstIterator i = mConfig.begin(); i != mConfig.end(); ++i)
		if (i.data().mMonitoring) {
			ok = true;
			break;
		}

	if (!ok)
		KMessageBox::error(this, i18n("You need to select at least one interface to monitor."));
	return ok;
}

void Configure::changeTheme(int theme)
{
	TDEIconLoader* loader = kapp->iconLoader();
	mIconError->setPixmap(loader->loadIcon("theme"+TQString::number(theme)+"_error.png",
						  TDEIcon::Panel, ICONSIZE));
	mIconNone->setPixmap(loader->loadIcon("theme"+TQString::number(theme)+"_none.png",
						 TDEIcon::Panel, ICONSIZE));
	mIconTx->setPixmap(loader->loadIcon("theme"+TQString::number(theme)+"_tx.png",
					   TDEIcon::Panel, ICONSIZE));
	mIconRx->setPixmap(loader->loadIcon("theme"+TQString::number(theme)+"_rx.png",
					   TDEIcon::Panel, ICONSIZE));
	mIconBoth->setPixmap(loader->loadIcon("theme"+TQString::number(theme)+"_both.png",
						 TDEIcon::Panel, ICONSIZE));
}
/*
void Configure::showInterfaceContextMenu(TQListBoxItem* item, const TQPoint& point) {
	if (!item && mConfig.size() == 1)
		return;
	TQPixmap icon = kapp->iconLoader()->loadIcon("edit-delete", TDEIcon::Small, 16);
	TQPopupMenu* menu = new TQPopupMenu(this);
	menu->insertItem(icon, i18n("Renomve Interface"), this, TQT_SLOT(removeInterface()));
	menu->exec(point);
}

void Configure::removeInterface() {
	mConfig.erase(mInterfaces->currentText());
	mInterfaces->removeItem(mInterfaces->currentItem());
}
*/
#include "configure.moc"
