/*
 * 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 "networks.h"

#include <qstring.h>
#include <dbsdsettings.h>

QMap<QString,  LAN*>	Networks::networks;
QMap<QString,  WLAN*>  Networks::wirelessNetworks;

LAN	*Networks::lanTemplate;
WLAN  *Networks::wlanTemplate;


/** Returns the LAN template. */
LAN	*Networks::getLANTemplate() {
	return lanTemplate;
}

/** Returns the WLAN template. */
WLAN  *Networks::getWLANTemplate() {
	return wlanTemplate;
}

/** @return A copy of the WLAN list.
  * @see allLANs()
  */
QMap<QString,  WLAN *>  Networks::allWLANs() {
	return  wirelessNetworks;
}

bool  Networks::addLAN(const  QString&  name,  LAN*  lan) {
	QPair<const QString, LAN *>  pair(name, lan);
	return  networks.insert(pair).second;
}

bool  Networks::addWLAN(const  QString&  name,  WLAN*  wlan) {
	QPair<const QString, WLAN *>  pair(name, wlan);
	return  wirelessNetworks.insert(pair).second;
}

void  Networks::removeLAN(const  QString&  name) {
	networks.remove(name);
}

void  Networks::removeWLAN(const  QString&  name) {
	wirelessNetworks.remove(name);
}

/** @return  A copy of the LAN list. You can change the LAN objects
  *			 in the list, but changing the list itself has no effect.
  *			 Use the addLAN(), addWLAN(), removeLAN() and removeWLAN()
  *			 methods for manipulating it.
  */
QMap<QString, LAN *>  Networks::allLANs() {
	return  networks;
}

/** Returns the requested LAN or NULL
  * if it couldn't be found.
  */
LAN  *Networks::getLAN(const  QString &  name) {
	QMap<QString, LAN *>::ConstIterator  it = networks.find(name);

	return (it == networks.end()) ? (NULL) : (*it);
}

/** Returns the requested WLAN or NULL
  * if it couldn't be found.
  */
WLAN  *Networks::getWLAN(const  QString &  name) {
	QMap<QString, WLAN *>::ConstIterator  it = wirelessNetworks.find(name);

	return (it == wirelessNetworks.end()) ? (NULL) : (*it);
}


/** Loads all saved networks from the settings file.
  * If loading the LAN profiles or  WLAN profiles or
  * both fails, FALSE is returned, otherwise TRUE.
  */
void  Networks::load() {
	DBSDSettings  settings;

	QStringList  strl;
	QStringList::ConstIterator  it;

	strl = settings.entryList(SETTINGS_NETWORKS);
	settings.beginGroup(SETTINGS_NETWORKS);
	{
		for(it = strl.begin();  it != strl.end();  ++it)
			networks.insert(*it, new LAN(LAN::fromString(settings.readEntry(*it))));
	}
	settings.endGroup();

	strl = settings.entryList(SETTINGS_WIRELESS);
	settings.beginGroup(SETTINGS_WIRELESS);
	{
		for(it = strl.begin();  it != strl.end();  ++it)
			wirelessNetworks.insert(*it, new WLAN(WLAN::fromString(settings.readEntry(*it))));
	}
	settings.endGroup();

	QString lanTemplateString = settings.readEntry(LAN_TEMPLATE, "0||||");
	lanTemplate = new LAN(LAN::fromString(lanTemplateString));

	QString wlanTemplateString = settings.readEntry(LAN_TEMPLATE, "1||||||0|");
	wlanTemplate = new WLAN(WLAN::fromString(wlanTemplateString));
}

void  Networks::save() {
	DBSDSettings  settings;

	removeEntries(settings, SETTINGS_NETWORKS);
	removeEntries(settings, SETTINGS_WIRELESS);

	settings.beginGroup(SETTINGS_NETWORKS);
	{
		QMap<QString, LAN*>::ConstIterator  it;
		for(it = networks.begin();  it != networks.end();  ++it)
			settings.writeEntry(it.key(), it.data()->toString());
	}
	settings.endGroup();

	settings.beginGroup(SETTINGS_WIRELESS);
	{
		QMap<QString, WLAN*>::ConstIterator  it;
		for(it = wirelessNetworks.begin();  it != wirelessNetworks.end();  ++it)
			settings.writeEntry(it.key(), it.data()->toString());
	}
	settings.endGroup();

	settings.writeEntry(LAN_TEMPLATE, lanTemplate->toString());
	settings.writeEntry(WLAN_TEMPLATE, wlanTemplate->toString());
}



void  Networks::removeEntries(QSettings &  settings,  QString  path) {
	if(!path.endsWith("/"))
		path += '/';

	QStringList  strl = settings.entryList(path);
	QStringList::ConstIterator  it;
	for(it = strl.begin();  it != strl.end();  ++it)
		settings.removeEntry(path + *it);
}

