// NetworkListManagerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NetworkListManager.h"
#include "NetworkListManagerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


bool ShowError(const int Error)
{
	bool Result = (Error != WCL_E_SUCCESS);
	if (Result)
	{
		CString s;
		s.Format(_T("Error: 0x%.8X"), Error);
		AfxMessageBox(s);
	}
	return Result;
}

CString GetEnumName(const wclNlmConnectivity Enum)
{
	switch (Enum)
	{
		case nlmDisconnected:
			return _T("nlmDisconnected");
		case nlmIPv4NoTraffic:
			return _T("nlmIPv4NoTraffic");
		case nlmIPv6NoTraffic:
			return _T("nlmIPv6NoTraffic");
		case nlmIPv4Subnet:
			return _T("nlmIPv4Subnet");
		case nlmIPv4LocalNetwork:
			return _T("nlmIPv4LocalNetwork");
		case nlmIPv4Internet:
			return _T("nlmIPv4Internet");
		case nlmIPv6Subnet:
			return _T("nlmIPv6Subnet");
		case nlmIPv6LocalNetwork:
			return _T("nlmIPv6LocalNetwork");
		case nlmIPv6Internet:
			return _T("nlmIPv6Internet");
		default:
			return _T("UNKNOWN");
	}
}

CString GetEnumName(const wclNlmNetworkPropertyChange Enum)
{
	switch (Enum)
	{
		case nlmNetworkChangeConnection:
			return _T("nlmNetworkChangeConnection");
		case nlmNetworkChangeDescription:
			return _T("nlmNetworkChangeDescription");
		case nlmNetworkChangeName:
			return _T("nlmNetworkChangeName");
		case nlmNetworkChangeIcon:
			return _T("nlmNetworkChangeIcon");
		case nlmNetworkChangeCategoryValue:
			return _T("nlmNetworkChangeCategoryValue");
		default:
			return _T("UNKNOWN");
	}
}

CString GetEnumName(const wclNlmConnectionProperty Enum)
{
	switch (Enum)
	{
		case nlmAuthentication:
			return _T("nlmAuthentication");;
		default:
			return _T("UNKNOWN");
	}
}

CString GetEnumName(const wclNlmNetworkCategory Enum)
{
	switch (Enum)
	{
		case nlmCategoryPublic:
			return _T("nlmCategoryPublic");
		case nlmCategoryPrivate:
			return _T("nlmCategoryPrivate");
		case nlmCategoryDomainAuthenticated:
			return _T("nlmCategoryDomainAuthenticated");
		default:
			return _T("UNKNOWN");
	}
}

CString GetEnumName(const wclNlmDomainType Enum)
{
	switch (Enum)
	{
		case nlmNonDomainNetwork:
			return _T("nlmNonDomainNetwork");
		case nlmDomainNetwork:
			return _T("nlmDomainNetwork");
		case nlmDomainAuthenticated:
			return _T("nlmDomainAuthenticated");
		default:
			return _T("UNKNOWN");
	}
}

CString DecodeFlags(const wclNlmConnectivityFlags& Flags)
{
	CString Result = _T("");
	if (Flags.find(nlmDisconnected) != Flags.end())
		Result += GetEnumName(nlmDisconnected) + _T(" ");
	if (Flags.find(nlmIPv4NoTraffic) != Flags.end())
		Result += GetEnumName(nlmIPv4NoTraffic) + _T(" ");
	if (Flags.find(nlmIPv6NoTraffic) != Flags.end())
		Result += GetEnumName(nlmIPv6NoTraffic) + _T(" ");
	if (Flags.find(nlmIPv4Subnet) != Flags.end())
		Result += GetEnumName(nlmIPv4Subnet) + _T(" ");
	if (Flags.find(nlmIPv4LocalNetwork) != Flags.end())
		Result += GetEnumName(nlmIPv4LocalNetwork) + _T(" ");
	if (Flags.find(nlmIPv4Internet) != Flags.end())
		Result += GetEnumName(nlmIPv4Internet) + _T(" ");
	if (Flags.find(nlmIPv6Subnet) != Flags.end())
		Result += GetEnumName(nlmIPv6Subnet) + _T(" ");
	if (Flags.find(nlmIPv6LocalNetwork) != Flags.end())
		Result += GetEnumName(nlmIPv6LocalNetwork) + _T(" ");
	if (Flags.find(nlmIPv6Internet) != Flags.end())
		Result += GetEnumName(nlmIPv6Internet) + _T(" ");
	return Result;
}

CString DecodeFlags(const wclNlmNetworkPropertyChangeFlags& Flags)
{
	CString Result = _T("");
	if (Flags.find(nlmNetworkChangeConnection) != Flags.end())
		Result += GetEnumName(nlmNetworkChangeConnection) + _T(" ");
	if (Flags.find(nlmNetworkChangeDescription) != Flags.end())
		Result += GetEnumName(nlmNetworkChangeDescription) + _T(" ");
	if (Flags.find(nlmNetworkChangeName) != Flags.end())
		Result += GetEnumName(nlmNetworkChangeName) + _T(" ");
	if (Flags.find(nlmNetworkChangeIcon) != Flags.end())
		Result += GetEnumName(nlmNetworkChangeIcon) + _T(" ");
	if (Flags.find(nlmNetworkChangeCategoryValue) != Flags.end())
		Result += GetEnumName(nlmNetworkChangeCategoryValue) + _T(" ");
	return Result;
}

CString GuidToString(const GUID& Id)
{
	CString res;
	
	LPOLESTR lpszClsID;
	StringFromCLSID(Id, &lpszClsID);
	res = CString(lpszClsID);
	CoTaskMemFree(lpszClsID);
	return res;
}

GUID StringToGuid(const CString& s)
{
	GUID Guid;
	CLSIDFromString((LPOLESTR)const_cast<LPTSTR>((LPCTSTR)s), &Guid);
	return Guid;
}

// CNetworkListManagerDlg dialog




CNetworkListManagerDlg::CNetworkListManagerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNetworkListManagerDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNetworkListManagerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_NETWORK_TYPE, cbNetworkType);
	DDX_Control(pDX, IDC_LIST_NETWORKS, lvNetworks);
	DDX_Control(pDX, IDC_EDIT_NEW_NAME_OR_DESCRIPTION, edNewNameOrDescription);
	DDX_Control(pDX, IDC_COMBO_CATEGORY, cbCategory);
	DDX_Control(pDX, IDC_LIST_CONNECTIONS, lvConnections);
	DDX_Control(pDX, IDC_LIST_EVENTS, lbEvents);
}

BEGIN_MESSAGE_MAP(CNetworkListManagerDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, &CNetworkListManagerDlg::OnBnClickedButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, &CNetworkListManagerDlg::OnBnClickedButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CNetworkListManagerDlg::OnBnClickedButtonClose)
	ON_BN_CLICKED(IDC_BUTTON_IS_CONNECTED, &CNetworkListManagerDlg::OnBnClickedButtonIsConnected)
	ON_BN_CLICKED(IDC_BUTTON_IS_INTERNET, &CNetworkListManagerDlg::OnBnClickedButtonIsInternet)
	ON_BN_CLICKED(IDC_BUTTON_CONNECTIVITY, &CNetworkListManagerDlg::OnBnClickedButtonConnectivity)
	ON_BN_CLICKED(IDC_BUTTON_ENUM_NETWORKS, &CNetworkListManagerDlg::OnBnClickedButtonEnumNetworks)
	ON_BN_CLICKED(IDC_BUTTON_REFRESH, &CNetworkListManagerDlg::OnBnClickedButtonRefresh)
	ON_BN_CLICKED(IDC_BUTTON_SET_NAME, &CNetworkListManagerDlg::OnBnClickedButtonSetName)
	ON_BN_CLICKED(IDC_BUTTON_SET_DESCRIPTION, &CNetworkListManagerDlg::OnBnClickedButtonSetDescription)
	ON_BN_CLICKED(IDC_BUTTON_SET_CATEGORY, &CNetworkListManagerDlg::OnBnClickedButtonSetCategory)
	ON_BN_CLICKED(IDC_BUTTON_GET_MACHINE_CONNECTIONS, &CNetworkListManagerDlg::OnBnClickedButtonGetMachineConnections)
	ON_BN_CLICKED(IDC_BUTTON_GET_NETWORK_CONNECTIONS, &CNetworkListManagerDlg::OnBnClickedButtonGetNetworkConnections)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CNetworkListManagerDlg::OnBnClickedButtonDelete)
END_MESSAGE_MAP()


// CNetworkListManagerDlg message handlers

BOOL CNetworkListManagerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	cbNetworkType.InsertString(0, _T("Connected"));
	cbNetworkType.InsertString(1, _T("Disconnected"));
	cbNetworkType.InsertString(2, _T("All"));
	cbNetworkType.SetCurSel(2);

	lvNetworks.InsertColumn(0, _T("Id"), 0, 250);
	lvNetworks.InsertColumn(1, _T("Category"), 0, 100);
	lvNetworks.InsertColumn(2, _T("Connectivity"), 0, 100);
	lvNetworks.InsertColumn(3, _T("Description"), 0, 100);
	lvNetworks.InsertColumn(4, _T("Domain type"), 0, 100);
	lvNetworks.InsertColumn(5, _T("Name"), 0, 100);
	lvNetworks.InsertColumn(6, _T("Creation Date Time"), 0, 100);
	lvNetworks.InsertColumn(7, _T("Connected Date Time"), 0, 100);
	lvNetworks.InsertColumn(8, _T("Connected"), 0, 80);
	lvNetworks.InsertColumn(9, _T("Connected to Internet"), 0, 100);
	lvNetworks.SetExtendedStyle(lvNetworks.GetExtendedStyle() | LVS_EX_FULLROWSELECT);

	edNewNameOrDescription.SetWindowText(_T("NewNameOrDescription"));

	cbCategory.InsertString(0, _T("Public"));
	cbCategory.InsertString(0, _T("Private"));
	cbCategory.InsertString(0, _T("Domain Authenticated"));
	cbCategory.SetCurSel(0);

	lvConnections.InsertColumn(0, _T("Adapter Id"), 0, 200);
	lvConnections.InsertColumn(1, _T("Connected"), 0, 80);
	lvConnections.InsertColumn(2, _T("Connected to Internet"), 0, 100);
	lvConnections.InsertColumn(3, _T("Connectivity"), 0, 100);
	lvConnections.InsertColumn(4, _T("Domain type"), 0, 100);
	lvConnections.InsertColumn(5, _T("Id"), 0, 200);
	lvConnections.SetExtendedStyle(lvConnections.GetExtendedStyle() | LVS_EX_FULLROWSELECT);

	__hook(&CwclNetworkListManager::AfterOpen, &FManager, &CNetworkListManagerDlg::AfterOpen);
	__hook(&CwclNetworkListManager::BeforeClose, &FManager, &CNetworkListManagerDlg::BeforeClose);
	__hook(&CwclNetworkListManager::OnConnectivityChanged, &FManager, &CNetworkListManagerDlg::ConnectivityChanged);
	__hook(&CwclNetworkListManager::OnNetworkAdded, &FManager, &CNetworkListManagerDlg::NetworkAdded);
	__hook(&CwclNetworkListManager::OnNetworkConnectivityChanged, &FManager, &CNetworkListManagerDlg::NetworkConnectivityChanged);
	__hook(&CwclNetworkListManager::OnNetworkDeleted, &FManager, &CNetworkListManagerDlg::NetworkDeleted);
	__hook(&CwclNetworkListManager::OnNetworkPropertyChanged, &FManager, &CNetworkListManagerDlg::NetworkPropertyChanged);	
	__hook(&CwclNetworkListManager::OnConnectionConnectivityChanged, &FManager, &CNetworkListManagerDlg::ConnectionConnectivityChanged);
	__hook(&CwclNetworkListManager::OnConnectionPropertyChanged, &FManager, &CNetworkListManagerDlg::ConnectionPropertyChanged);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CNetworkListManagerDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNetworkListManagerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CNetworkListManagerDlg::ShowConnections(wclNlmConnections& Connections)
{
	lvConnections.DeleteAllItems();
	
	for (size_t i = 0; i < Connections.size(); i++)
	{
		CString str;
		int Res;

		CwclNlmConnection* Connection = Connections[i];

		GUID Id;
		Res = Connection->GetAdapterId(Id);
		if (Res == WCL_E_SUCCESS)
			str = GuidToString(Id);
		else
			str.Format(_T("Error: 0x%.8X"), Res);
		lvConnections.InsertItem((int)i, str);

		bool Connected;
		Res = Connection->GetConnected(Connected);
		if (Res == WCL_E_SUCCESS)
		{
			if (Connected)
				str = _T("true");
			else
				str = _T("false");
		}
		else
			str.Format(_T("Error: 0x%.8X"), Res);
		lvConnections.SetItemText((int)i, 1, str);
			
		Res = Connection->GetConnectedToInternet(Connected);
		if (Res == WCL_E_SUCCESS)
		{
			if (Connected)
				str = _T("true");
			else
				str = _T("false");
		}
		else
			str.Format(_T("Error: 0x%.8X"), Res);
		lvConnections.SetItemText((int)i, 2, str);

		wclNlmConnectivityFlags Connectivity;
		Res = Connection->GetConnectivity(Connectivity);
		if (Res == WCL_E_SUCCESS)
			str = DecodeFlags(Connectivity);
		else
			str.Format(_T("Error: 0x%.8X"), Res);
		lvConnections.SetItemText((int)i, 3, str);

		wclNlmDomainType Domain;
		Res = Connection->GetDomainType(Domain);
		if (Res == WCL_E_SUCCESS)
			str = GetEnumName(Domain);
		else
			str.Format(_T("Error: 0x%.8X"), Res);
		lvConnections.SetItemText((int)i, 4, str);
			
		Res = Connection->GetId(Id);
		if (Res == WCL_E_SUCCESS)
			str = GuidToString(Id);
		else
			str.Format(_T("Error: 0x%.8X"), Res);
		lvConnections.SetItemText((int)i, 5, str);
	}
	
	for (size_t i = 0; i < Connections.size(); i++)
		delete Connections[i];
	Connections.clear();
}

void CNetworkListManagerDlg::OnBnClickedButtonClear()
{
	lbEvents.ResetContent();
}

void CNetworkListManagerDlg::OnBnClickedButtonOpen()
{
	ShowError(FManager.Open());
}

void CNetworkListManagerDlg::OnBnClickedButtonClose()
{
	ShowError(FManager.Close());
}

void CNetworkListManagerDlg::OnBnClickedButtonIsConnected()
{
	bool Connected;
	if (!ShowError(FManager.GetConnected(Connected)))
	{
		if (Connected)
			AfxMessageBox(_T("Connected"));
		else
			AfxMessageBox(_T("NOT Connected"));
	}
}

void CNetworkListManagerDlg::OnBnClickedButtonIsInternet()
{
	bool Connected;
	if (!ShowError(FManager.GetConnectedToInternet(Connected)))
	{
		if (Connected)
			AfxMessageBox(_T("Connected"));
		else
			AfxMessageBox(_T("NOT Connected"));
	}
}

void CNetworkListManagerDlg::OnBnClickedButtonConnectivity()
{
	wclNlmConnectivityFlags Connectivity;
	if (!ShowError(FManager.GetConnectivity(Connectivity)))
		AfxMessageBox(_T("Connectivity: ") + DecodeFlags(Connectivity));
}

void CNetworkListManagerDlg::OnBnClickedButtonEnumNetworks()
{
	lvNetworks.DeleteAllItems();
	
	wclNlmEnumNetwork Flag = (wclNlmEnumNetwork)cbNetworkType.GetCurSel();
	
	wclNlmNetworks Networks;
	int Res = FManager.GetNetworks(Flag, Networks);
	if (!ShowError(Res))
	{
		for (size_t i = 0; i < Networks.size(); i++)
		{
			CString str;
			CwclNlmNetwork* Network = Networks[i];

			GUID Id;
			Res = Network->GetNetworkId(Id);
			if (Res == WCL_E_SUCCESS)
				str = GuidToString(Id);
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.InsertItem((int)i, str);

			wclNlmNetworkCategory Category;
			Res = Network->GetCategory(Category);
			if (Res == WCL_E_SUCCESS)
				str = GetEnumName(Category);
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText((int)i, 1, str);
				
			wclNlmConnectivityFlags Connectivity;
			Res = Network->GetConnectivity(Connectivity);
			if (Res == WCL_E_SUCCESS)
				str = DecodeFlags(Connectivity);
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText((int)i, 2, str);
				
			tstring d;
			Res = Network->GetDescription(d);
			if (Res == WCL_E_SUCCESS)
				str = CString(d.c_str());
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText((int)i, 3, str);

			wclNlmDomainType Domain;
			Res = Network->GetDomainType(Domain);
			if (Res == WCL_E_SUCCESS)
				str = GetEnumName(Domain);
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText((int)i, 4, str);
				
			Res = Network->GetName(d);
			if (Res == WCL_E_SUCCESS)
				str = CString(d.c_str());
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText((int)i, 5, str);

			SYSTEMTIME CreationDateTime;
			SYSTEMTIME ConnectedDateTime;
			CString str2;
			Res = Network->GetTimeCreatedAndConnected(CreationDateTime,
				ConnectedDateTime);
			if (Res == WCL_E_SUCCESS)
			{
				str.Format(_T("%04u.%02u.%02u %02u:%02u:%02u"), CreationDateTime.wYear,
					CreationDateTime.wMonth, CreationDateTime.wDay, CreationDateTime.wHour,
					CreationDateTime.wMinute, CreationDateTime.wSecond);
				str2.Format(_T("%04u.%02u.%02u %02u:%02u:%02u"), ConnectedDateTime.wYear,
					ConnectedDateTime.wMonth, ConnectedDateTime.wDay, ConnectedDateTime.wHour,
					ConnectedDateTime.wMinute, ConnectedDateTime.wSecond);
			}
			else
			{
				str.Format(_T("Error: 0x%.8X"), Res);
				str2.Format(_T("Error: 0x%.8X"), Res);
			}
			lvNetworks.SetItemText((int)i, 6, str);
			lvNetworks.SetItemText((int)i, 7, str2);
				
			bool Connected;
			Res = Network->GetConnected(Connected);
			if (Res == WCL_E_SUCCESS)
			{
				if (Connected)
					str = _T("true");
				else
					str = _T("false");
			}
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText((int)i, 8, str);

			Res = Network->GetConnectedToInternet(Connected);
			if (Res == WCL_E_SUCCESS)
			{
				if (Connected)
					str = _T("true");
				else
					str = _T("false");
			}
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText((int)i, 9, str);
		}
	}
	
	// Cleanup
	for (size_t i = 0; i < Networks.size(); i++)
		delete Networks[i];
	Networks.clear();
}

void CNetworkListManagerDlg::OnBnClickedButtonRefresh()
{
	POSITION Pos = lvNetworks.GetFirstSelectedItemPosition();
	if (!Pos)
		AfxMessageBox(_T("Select network"));
	else
	{
		int Item = lvNetworks.GetNextSelectedItem(Pos);
		
		GUID Id = StringToGuid(lvNetworks.GetItemText(Item, 0));
		CwclNlmNetwork* Network;
		if (!ShowError(FManager.GetNetwork(Id, Network)))
		{
			CString str;
			wclNlmNetworkCategory Category;
			int Res = Network->GetCategory(Category);
			if (Res == WCL_E_SUCCESS)
				str = GetEnumName(Category);
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText(Item, 1, str);
				
			wclNlmConnectivityFlags Connectivity;
			Res = Network->GetConnectivity(Connectivity);
			if (Res == WCL_E_SUCCESS)
				str = DecodeFlags(Connectivity);
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText(Item, 2, str);
				
			tstring d;
			Res = Network->GetDescription(d);
			if (Res == WCL_E_SUCCESS)
				str = CString(d.c_str());
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText(Item, 3, str);

			wclNlmDomainType Domain;
			Res = Network->GetDomainType(Domain);
			if (Res == WCL_E_SUCCESS)
				str = GetEnumName(Domain);
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText(Item, 4, str);
				
			Res = Network->GetName(d);
			if (Res == WCL_E_SUCCESS)
				str = CString(d.c_str());
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText(Item, 5, str);

			SYSTEMTIME CreationDateTime;
			SYSTEMTIME ConnectedDateTime;
			CString str2;
			Res = Network->GetTimeCreatedAndConnected(CreationDateTime,
				ConnectedDateTime);
			if (Res == WCL_E_SUCCESS)
			{
				str.Format(_T("%04u.%02u.%02u %02u:%02u:%02u"), CreationDateTime.wYear,
					CreationDateTime.wMonth, CreationDateTime.wDay, CreationDateTime.wHour,
					CreationDateTime.wMinute, CreationDateTime.wSecond);
				str2.Format(_T("%04u.%02u.%02u %02u:%02u:%02u"), ConnectedDateTime.wYear,
					ConnectedDateTime.wMonth, ConnectedDateTime.wDay, ConnectedDateTime.wHour,
					ConnectedDateTime.wMinute, ConnectedDateTime.wSecond);
			}
			else
			{
				str.Format(_T("Error: 0x%.8X"), Res);
				str2.Format(_T("Error: 0x%.8X"), Res);
			}
			lvNetworks.SetItemText(Item, 6, str);
			lvNetworks.SetItemText(Item, 7, str2);
				
			bool Connected;
			Res = Network->GetConnected(Connected);
			if (Res == WCL_E_SUCCESS)
			{
				if (Connected)
					str = _T("true");
				else
					str = _T("false");
			}
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText(Item, 8, str);

			Res = Network->GetConnectedToInternet(Connected);
			if (Res == WCL_E_SUCCESS)
			{
				if (Connected)
					str = _T("true");
				else
					str = _T("false");
			}
			else
				str.Format(_T("Error: 0x%.8X"), Res);
			lvNetworks.SetItemText(Item, 9, str);
			
			delete Network;
		}
	}
}

void CNetworkListManagerDlg::OnBnClickedButtonSetName()
{
	POSITION Pos = lvNetworks.GetFirstSelectedItemPosition();
	if (!Pos)
		AfxMessageBox(_T("Select network"));
	else
	{
		int Item = lvNetworks.GetNextSelectedItem(Pos);
		CwclNlmNetwork* Network;
		int Res = FManager.GetNetwork(StringToGuid(lvNetworks.GetItemText(Item, 0)), Network);
		if (!ShowError(Res))
		{
			CString str;
			edNewNameOrDescription.GetWindowText(str);
			ShowError(Network->SetName(tstring(str)));
			delete Network;
		}
	}
}

void CNetworkListManagerDlg::OnBnClickedButtonSetDescription()
{
	POSITION Pos = lvNetworks.GetFirstSelectedItemPosition();
	if (!Pos)
		AfxMessageBox(_T("Select network"));
	else
	{
		int Item = lvNetworks.GetNextSelectedItem(Pos);
		CwclNlmNetwork* Network;
		int Res = FManager.GetNetwork(StringToGuid(lvNetworks.GetItemText(Item, 0)), Network);
		if (!ShowError(Res))
		{
			CString str;
			edNewNameOrDescription.GetWindowText(str);
			ShowError(Network->SetDescription(tstring(str)));
			delete Network;
		}
	}
}

void CNetworkListManagerDlg::OnBnClickedButtonSetCategory()
{
	POSITION Pos = lvNetworks.GetFirstSelectedItemPosition();
	if (!Pos)
		AfxMessageBox(_T("Select network"));
	else
	{
		int Item = lvNetworks.GetNextSelectedItem(Pos);
		CwclNlmNetwork* Network;
		int Res = FManager.GetNetwork(StringToGuid(lvNetworks.GetItemText(Item, 0)), Network);
		if (!ShowError(Res))
		{
			if (ShowError(Network->SetCategory((wclNlmNetworkCategory)cbCategory.GetCurSel())))
				AfxMessageBox(_T("Admin privilegies required"));
			delete Network;
		}
	}
}

void CNetworkListManagerDlg::OnBnClickedButtonGetMachineConnections()
{
	wclNlmConnections Connections;
	if (!ShowError(FManager.GetConnections(Connections)))
		ShowConnections(Connections);
}

void CNetworkListManagerDlg::OnBnClickedButtonGetNetworkConnections()
{
	POSITION Pos = lvNetworks.GetFirstSelectedItemPosition();
	if (!Pos)
		AfxMessageBox(_T("Select network"));
	else
	{
		int Item = lvNetworks.GetNextSelectedItem(Pos);
		CwclNlmNetwork* Network;
		int Res = FManager.GetNetwork(StringToGuid(lvNetworks.GetItemText(Item, 0)), Network);
		if (!ShowError(Res))
		{
			wclNlmConnections Connections;
			if (!ShowError(Network->GetConnections(Connections)))
				ShowConnections(Connections);
			delete Network;
		}
	}
}

void CNetworkListManagerDlg::AfterOpen(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	lbEvents.InsertString(lbEvents.GetCount(), _T("AfterOpen"));
}

void CNetworkListManagerDlg::BeforeClose(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	lbEvents.InsertString(lbEvents.GetCount(), _T("BeforeClose"));
}

void CNetworkListManagerDlg::ConnectivityChanged(void* Sender, 
	const wclNlmConnectivityFlags& Connectivity)
{
	UNREFERENCED_PARAMETER(Sender);

	lbEvents.InsertString(lbEvents.GetCount(), _T("Connectivity changed: ") +
		DecodeFlags(Connectivity));
}

void CNetworkListManagerDlg::NetworkAdded(void* Sender, const GUID& NetworkId)
{
	UNREFERENCED_PARAMETER(Sender);

	lbEvents.InsertString(lbEvents.GetCount(), _T("Network added: ") + GuidToString(NetworkId));
}

void CNetworkListManagerDlg::NetworkConnectivityChanged(void* Sender, const GUID& NetworkId,
	const wclNlmConnectivityFlags& Connectivity)
{
	UNREFERENCED_PARAMETER(Sender);

	lbEvents.InsertString(lbEvents.GetCount(), _T("Network ") + GuidToString(NetworkId) +
		_T(" connectivity changed: ")+ DecodeFlags(Connectivity));
}

void CNetworkListManagerDlg::NetworkDeleted(void* Sender, const GUID& NetworkId)
{
	UNREFERENCED_PARAMETER(Sender);

	lbEvents.InsertString(lbEvents.GetCount(), _T("Network deleted: ") + GuidToString(NetworkId));
}

void CNetworkListManagerDlg::NetworkPropertyChanged(void* Sender, const GUID& NetworkId,
	const wclNlmNetworkPropertyChangeFlags& Change)
{
	UNREFERENCED_PARAMETER(Sender);

	lbEvents.InsertString(lbEvents.GetCount(), _T("Network ") + GuidToString(NetworkId) +
		_T(" property changed: ") + DecodeFlags(Change));
}

void CNetworkListManagerDlg::ConnectionConnectivityChanged(void* Sender, const GUID& ConnectionId,
	const wclNlmConnectivityFlags& Connectivity)
{
	UNREFERENCED_PARAMETER(Sender);

	lbEvents.InsertString(lbEvents.GetCount(), _T("Connection ") + GuidToString(ConnectionId) +
		_T(" connectivity changed: ") + DecodeFlags(Connectivity));
}

void CNetworkListManagerDlg::ConnectionPropertyChanged(void* Sender, const GUID& ConnectionId,
	const wclNlmConnectionProperty Prop)
{
	UNREFERENCED_PARAMETER(Sender);

	lbEvents.InsertString(lbEvents.GetCount(), _T("Connection ") + GuidToString(ConnectionId) +
		_T(" property changed: ") + GetEnumName(Prop));
}

void CNetworkListManagerDlg::OnDestroy()
{
	CDialog::OnDestroy();

	FManager.Close();
	__unhook(&FManager);
}

void CNetworkListManagerDlg::OnBnClickedButtonDelete()
{
	POSITION Pos = lvNetworks.GetFirstSelectedItemPosition();
	if (!Pos)
		AfxMessageBox(_T("Select network"));
	else
	{
		int Item = lvNetworks.GetNextSelectedItem(Pos);
		int Res = FManager.DeleteNetwork(StringToGuid(lvNetworks.GetItemText(Item, 0)));
		if (!ShowError(Res))
			lvNetworks.DeleteItem(Item);
	}
}
