/*
 * 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 <dialogs.h>
#include <dbsdglobal.h>

void	LANConfig::init() {
	/* Initialize Graphics */
	DBSDIconLoader	*iloader = DBSDGlobal::getIconLoader();
	btnDNSAdd->setPixmap(iloader->loadIcon("filenew", DBSDIconLoader::Button));
	btnDNSDel->setPixmap(iloader->loadIcon("editdelete", DBSDIconLoader::Button));
	btnDNSUp->setPixmap(iloader->loadIcon("up", DBSDIconLoader::Button));
	btnDNSDown->setPixmap(iloader->loadIcon("down", DBSDIconLoader::Button));
	pixGatewayHint->setPixmap(iloader->loadIcon("messagebox_info", DBSDIconLoader::HintIcon));
}

void  LANConfig::DNSMove(int  i) {
	QListBoxItem  *item = lstDNS->selectedItem();

	if(item != NULL) {
		int  pos = lstDNS->index(item);
		if(pos + i >= 0 && pos + i <= lstDNS->count())
		{
			lstDNS->setSelected(item, false);
			lstDNS->takeItem(item);
			lstDNS->insertItem(item, pos + i);
			lstDNS->setSelected(item, true);
		}
	}
}

void  LANConfig::DNSUp() {
	DNSMove(-1);
}

void  LANConfig::DNSDown() {
	DNSMove(+1);
}

void  LANConfig::removeDNS() {
	delete  lstDNS->selectedItem();
}

void  LANConfig::addDNS() {
	IPv4Addr  ns;

	bool		ok;
	QString	strDNS = Dialogs::strInput(tr("Nameserver IP Address:"), &ok);

	if(ok) {
		if(!ns.setAddr(strDNS))
			Dialogs::warnDlg(tr("Invalid IP Address."));
		else
			lstDNS->insertItem(strDNS);
	}
}

void  LANConfig::setLAN(LAN  *lan) {
	this->lan = lan;
	if(lan->usesDHCP())
		grpConf->setButton(grpConf->id(radDHCP));
	else
		grpConf->setButton(grpConf->id(radCustom));

	editIP->setText(lan->getIP().toString());
	editGateway->setText(lan->getGateway().toString());
	editNetmask->setText(lan->getNetmask().toString());

	lstDNS->clear();
	QPtrList<IPv4Addr>  dns = lan->getNameservers();
	IPv4Addr  *addr = dns.first();
	while(addr != NULL) {
		lstDNS->insertItem(addr->toString());

		addr = dns.next();
	}
}

bool  LANConfig::save() {
	bool			dhcpEnabled;
	IPv4Addr	  ipAddr, gateway;
	IPv4Netmask  netmask;
	QPtrList<IPv4Addr>  nameservers;

	dhcpEnabled = grpConf->selectedId() == grpConf->id(radDHCP);

	/** Displays error messages about invalid user input
	  * only if "Enable DHCP" is not checked.
	  */

	if(!ipAddr.setAddr(editIP->text())  &&  !dhcpEnabled) {
		Dialogs::warnDlg(tr("The IP address you entered is invalid."));
		return  false;
	}

	QString	gw = editGateway->text();
	if(!gateway.setAddr(gw)  &&  !dhcpEnabled)
		if(gw.isEmpty())
			gateway.setInvalid();
		else {
			Dialogs::warnDlg(tr("The gateway IP address you entered is invalid."));
			return  false;
		}

	if(!netmask.setAddr(editNetmask->text())  &&  !dhcpEnabled) {
		Dialogs::warnDlg(tr("The netmask you entered is invalid."));
		return  false;
	}

	QListBoxItem  *item = lstDNS->firstItem();
	while(item != NULL) {
		nameservers.append(new IPv4Addr(item->text()));
		item = item->next();
	}

	lan->setIP(ipAddr);
	lan->setNetmask(netmask);
	lan->setGateway(gateway);
	lan->setDNSList(nameservers);
	lan->setDHCPEnabled(dhcpEnabled);

	return  true;
}


