/*
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Peter Hofer <ph@desktopbsd.net>
 *			(C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#include <dbsdglobal.h>
#include <dialogs.h>


#define _WEP_ON	0
#define _WEP_OFF  1


void  frmWLAN::init() {
}


void  frmWLAN::wepOnOff(int  onoff) {
	editWEPKey->setEnabled((onoff == _WEP_ON) ? (true) : (false));
}


void  frmWLAN::setWLAN(WLAN  *wlan) {
	editSSID->setText(wlan->getSSID());

	WEPMode  mode = wlan->getWEPMode();
	if(mode == WEPOn) {
		cmbWEPMode->setCurrentItem(_WEP_ON);
		wepOnOff(_WEP_ON);
	} else if(mode == WEPOff) {
		cmbWEPMode->setCurrentItem(_WEP_OFF);
		wepOnOff(_WEP_OFF);
	}

	editWEPKey->setText(wlan->getWEPKey());

	this->wlan = wlan;
	lanConfig->setLAN(wlan);
}

void  frmWLAN::setTitle(const QString &  name) {
	lblNetworkName->setText(name);
}

bool  frmWLAN::save() {
	if(!wlan->setSSID(editSSID->text())) {
		Dialogs::warnDlg(tr("Invalid network name.\n"
								  "A network name must be between 1 and 32 characters."));
		return  false;
	}

	WEPMode  mode = (cmbWEPMode->currentItem() == _WEP_ON) ? (WEPOn) : (WEPOff);

	if(mode == WEPOn) {
		if(!wlan->setWEPKey(editWEPKey->text())) {
			Dialogs::warnDlg(tr("The WEP key you entered is invalid.\n"
									  "\n"
									  "It must be a hex number with 10 or 16 characters for\n"
									  "64-bit, 26 or 32 characters for 128-bit, or 58 or 64\n"
									  "characters for 256-bit encryption."));
			return  false;
		}
	} else
		wlan->setWEPKeyNull();


	wlan->setWEPMode(mode);

	return  true;
}

void  frmWLAN::accept() {
	if(lanConfig->save()  &&  save())
		QDialog::accept();
}


void frmWLAN::initGraphics() {
	DBSDIconLoader *iloader = DBSDGlobal::getIconLoader();
	lblNetworkIcon->setPixmap(iloader->loadIcon("network", DBSDIconLoader::SmallLogo));
	btnOk->setIconSet(iloader->loadIcon("button_ok", DBSDIconLoader::Button));
	btnClose->setIconSet(iloader->loadIcon("button_cancel", DBSDIconLoader::Button));
}
