/***************************************************************************
 *
 * knetworkmanager-devicestore_dbus.cpp - A NetworkManager frontend for KDE
 *
 * Copyright (C) 2005, 2006 Novell, Inc.
 *
 * Author: Helmut Schaa <hschaa@suse.de>, <helmut.schaa@gmx.de>
 *
 * 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
 *
 **************************************************************************/

/* qt headers */
#include <tqhostaddress.h>
#include <tqvariant.h>
#include <tqregexp.h>

/* kde headers */
#include <kdebug.h>
#include <tdelocale.h>

/* TQT_DBus headers*/
#include <tqdbusdata.h>
#include <tqdbusdatamap.h>

/* knetworkmanager headers */
#include "knetworkmanager.h"
#include "knetworkmanager-connection_setting_wireless.h"
#include "knetworkmanager-connection_setting_wireless_security.h"
#include "knetworkmanager-accesspoint.h"

// the bssid should look like XX:XX:XX:XX:XX:XX where X is a hexadecimal digit
#define MAC_ADDRESS_PATTERN "[0-9A-Fa-f]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}"

using namespace ConnectionSettings;

/*
	class Wireless
*/
Wireless::Wireless(Connection* conn, ::AccessPoint* ap, WirelessSecurity* security)
	: ConnectionSetting(conn, NM_SETTING_WIRELESS_SETTING_NAME)
{
	_security = TQString();

	if (ap)
	{
		_essid = ap->getSsidByteArray();
		_mode = ap->getMode() == 0 ? MODE_ADHOC : MODE_INFRASTRUCTURE;
	}
	else
	{
		_essid = TQByteArray();
		_mode = MODE_INFRASTRUCTURE;
	}

	_modeMap[MODE_INFRASTRUCTURE] = "infrastructure";
	_modeMap[MODE_ADHOC] = "adhoc";
}

SettingsMap
Wireless::toMap() const
{
	SettingsMap map;

	map.insert(NM_SETTING_WIRELESS_MODE, TQT_DBusData::fromString(_modeMap[_mode]));

	TQValueList<TQT_DBusData> essid;
	for (TQByteArray::ConstIterator it = _essid.begin(); it != _essid.end(); ++it)
		essid.append(TQT_DBusData::fromByte(*it));

	if (essid.size() > 0)
		map.insert(NM_SETTING_WIRELESS_SSID, TQT_DBusData::fromTQValueList(essid));
	else
		kdWarning() << k_funcinfo << " SSID undefined" << endl;

	if (!_security.isEmpty())
		map.insert(NM_SETTING_WIRELESS_SEC, TQT_DBusData::fromString(_security));

	if (!_seenBssids.empty())
	{
		TQValueList<TQT_DBusData> bssids;
		for (TQValueList<TQString>::ConstIterator it = _seenBssids.begin(); it != _seenBssids.end(); ++it)
			bssids.append(TQT_DBusData::fromString(*it));

		map.insert(NM_SETTING_WIRELESS_SEEN_BSSIDS, TQT_DBusData::fromTQValueList(bssids));
	}

	return map;
}

void
Wireless::fromMap(const SettingsMap& map)
{
	SettingsMap::ConstIterator it;

	// Mode
	if ((it = map.find(NM_SETTING_WIRELESS_MODE)) != map.end())
	{
		TQBiDirectionalMap<MODES, TQString>::Iterator mode_it = _modeMap.findData(it.data().toString());
		if (mode_it != _modeMap.end())
			_mode = mode_it.key();
		else
			_mode = MODE_INFRASTRUCTURE;
	}

	// Essid
	if ((it = map.find(NM_SETTING_WIRELESS_SSID)) != map.end())
	{
		TQValueList<TQT_DBusData> dbus_essid = (*it).toTQValueList();
		TQByteArray essid(dbus_essid.size());

		int index = 0;
		for (TQValueList<TQT_DBusData>::ConstIterator byte_it = dbus_essid.begin(); byte_it != dbus_essid.end(); ++byte_it)
		{
			essid[index] = (*byte_it).toByte();
			index++;
		}
		_essid = essid;
	}

	if ((it = map.find(NM_SETTING_WIRELESS_SEC)) != map.end())
	{
		_security = (*it).toString();
	}

	// Seen BSSIDS
	if ((it = map.find(NM_SETTING_WIRELESS_SEEN_BSSIDS)) != map.end())
	{
		TQRegExp exp(MAC_ADDRESS_PATTERN);
		TQValueList<TQT_DBusData> bssids = (*it).toTQValueList();
		_seenBssids.clear();

		for(TQValueList<TQT_DBusData>::Iterator it = bssids.begin(); it != bssids.end(); ++it)
		{
			TQString bssid = (*it).toString();
			if (exp.exactMatch(bssid))
				_seenBssids.append(bssid);
		}
	}
}

void
Wireless::setEssid(const TQByteArray& essid)
{
	_essid = essid;
	emitValidityChanged();
}

TQByteArray
Wireless::getEssid(void) const
{
	return _essid;
}

void
Wireless::setMode(MODES mode)
{
	_mode = mode;
	emitValidityChanged();
}

Wireless::MODES
Wireless::getMode(void) const
{
	return _mode;
}

void
Wireless::setSecurity(const TQString& security)
{
	_security = security;
	emitValidityChanged();
}

TQString
Wireless::getSecurity(void) const
{
	return _security;
}

bool
Wireless::isValid() const
{
	// ESSID is essential
	if (_essid.isEmpty())
		return false;

	return true;
}

void
Wireless::addSeenBssid(const TQString& bssid)
{
	TQRegExp exp(MAC_ADDRESS_PATTERN);
	if (!exp.exactMatch(bssid))
		return;

	// no duplicates please
	for(TQValueList<TQString>::Iterator it = _seenBssids.begin(); it != _seenBssids.end(); ++it)
	{
		if ((*it) == bssid)
			return;
	}
	
	// insert this bssid
	_seenBssids.append(bssid);
	emitValidityChanged();
}
