/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
// SCTS3DevManDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SCTS3DevMan.h"
#include "SCTS3DevManDlg.h"
#include "SctsDevManSession.h"
#include "WaitPane.h"
#include "ServersPane.h"
#include "TreePane.h"
#include "ClientDevicePane.h"
#include "AddServerDevicePane.h"
#include "WBXML2XMLConv.h"
#include "About.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static void dmTreeCallback(SctsDMTree *tree,SctsDMNode *node,SctsCommandType command)
{
	if (!tree || !node) return;
	if (command==SCTS_ADD)
	{
		SctsDMNode *current=node;
		while (true)
		{
			if (!current->markedForDelete)
			{
				current->uiData=((CTreePane *)tree->uiCallbackData)->m_tree.InsertItem(current->name,0,0,current->parent?(HTREEITEM)current->parent->uiData:TVI_ROOT);
				((CTreePane *)tree->uiCallbackData)->m_tree.SetItemData((HTREEITEM)current->uiData,(DWORD)current);
				if (current->child) 
				{
					current=current->child;
					continue;
				}
			}
			if (current==node) break;
			if (current->next)
			{
				current=current->next;
				continue;
			}
			while (current!=node && !current->next)	current=current->parent;
			if (current==node) break;
			current=current->next;
		}
	}
	if (command==SCTS_DELETE && !node->markedForDelete)
	{
		((CTreePane *)tree->uiCallbackData)->m_tree.DeleteItem((HTREEITEM)node->uiData);
		node->uiData=0;
	}
	if (command==SCTS_REPLACE && ((CTreePane *)tree->uiCallbackData)->IsWindowVisible() && ((CTreePane *)tree->uiCallbackData)->m_tree.GetSelectedItem()==node->uiData)
	{
		((CTreePane *)tree->uiCallbackData)->UploadNode(node);
	}
}


/////////////////////////////////////////////////////////////////////////////
// CSCTS3DevManDlg dialog

CSCTS3DevManDlg::CSCTS3DevManDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSCTS3DevManDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSCTS3DevManDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	currentLeftPane=0;
	currentRightPane=0;
	currentRightChildPane=0;
	activeGuide=0;
	credCalc=0;
	lastSize.bottom=0;
	lastSize.top=0;
	lastSize.left=0;
	lastSize.right=0;
	navigatorWidth=0;
}

void CSCTS3DevManDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSCTS3DevManDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSCTS3DevManDlg, CDialog)
	//{{AFX_MSG_MAP(CSCTS3DevManDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_COMMAND(ID_SETTINGS_SERVERS, OnSettingsServers)
	ON_COMMAND(ID_SETTINGS_LOGSPATH, OnSettingsLogspath)
	ON_WM_SIZE()
	ON_WM_SIZING()
	ON_BN_CLICKED(IDC_DEVADD, OnDevadd)
	ON_BN_CLICKED(IDC_DEVDEL, OnDevdel)
	ON_COMMAND(ID_HELP_ABOUTSCTS, OnHelpAboutscts)
	ON_COMMAND(ID_HELP_REPORTINGABUG, OnHelpReportingabug)
	ON_COMMAND(ID_TOOLS_CREDENTIALCALCULATOR, OnToolsCredentialcalculator)
	ON_COMMAND(ID_TOOLS_WBXMLXMLCONVERTER, OnToolsWbxmlxmlconverter)
	ON_COMMAND(ID_SETTINGS_USEOBEXIRCLIENT, OnSettingsUseobexirclient)
	ON_COMMAND(ID_SETTINGS_USEOBEXTCPCLIENT, OnSettingsUseobextcpclient)
	ON_COMMAND(ID_CONFIGURATION_CLEARSESSIONS, OnConfigurationClearsessions)
	ON_COMMAND(ID_CONFIGURATION_REMOVEALL, OnConfigurationRemoveall)
	ON_COMMAND(ID_CONFIGURATION_SAVE, OnConfigurationSave)
	ON_MESSAGE(WM_SESSIONCOMPLETE,SessionComplete)
	ON_MESSAGE(WM_ADDSESSION,AddSession)
	ON_MESSAGE(WM_ADDCLIENTDEV,AddClientDevice)
	ON_MESSAGE(WM_ADDSERVERDEV,AddServerDevice)
	ON_MESSAGE(WM_REMCLIENTDEV,RemoveClientDevice)
	ON_MESSAGE(WM_REMSERVERDEV,RemoveServerDevice)
	ON_MESSAGE(WM_DISPLAYUIA,DisplayUIA)
	ON_MESSAGE(WM_DESTROYUIA,DestroyUIA)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSCTS3DevManDlg message handlers

BOOL CSCTS3DevManDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	RECT rect;

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	GetDlgItem(IDC_DEVADD)->GetWindowRect(&rect);
	toolbarHeight=rect.bottom-rect.top;
	GetWindowRect(&minSize);
	/* Resize to last size */
	if (mainPane->lastSize.right && mainPane->lastSize.bottom) 
	{
		/* If value is -1 maximize window */
		if (mainPane->lastSize.right==-1)
			ShowWindow(SW_SHOWMAXIMIZED);
		else
			MoveWindow(&mainPane->lastSize);
	}
	GetClientRect(&rect);
	/* Set the navigator width */
	if (!navigatorWidth) navigatorWidth=rect.right/4;
	currentRightPane=new CRightPane();
	currentRightPane->Create(IDD_RIGHTPANE,this);
	currentLeftPane=new CLeftPane();
	currentLeftPane->Create(IDD_LEFTPANE,this);
	currentLeftPane->MoveWindow(0,toolbarHeight,navigatorWidth,rect.bottom-toolbarHeight);
	currentRightPane->MoveWindow(navigatorWidth,toolbarHeight,rect.right-navigatorWidth,rect.bottom-toolbarHeight);
	currentLeftPane->m_accountTree.SelectItem(0);
	
	/* Toolbar Images */
	((CButton *)GetDlgItem(IDC_DEVADD))->SetIcon((HICON)LoadImage(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_DEVADD),IMAGE_ICON,0,0,LR_DEFAULTCOLOR));
	((CButton *)GetDlgItem(IDC_DEVDEL))->SetIcon((HICON)LoadImage(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_DEVDEL),IMAGE_ICON,0,0,LR_DEFAULTCOLOR));

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

void CSCTS3DevManDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAbout about;
		about.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CSCTS3DevManDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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 to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSCTS3DevManDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSCTS3DevManDlg::OnDestroy() 
{

	
	CWaitPane waitPane;
	waitPane.DoModal();
	CDialog::OnDestroy();
}

void CSCTS3DevManDlg::OnSettingsServers() 
{
	CServersPane servers;
	servers.DoModal();
}

void CSCTS3DevManDlg::OnSettingsLogspath() 
{
	char buffer[256]="";
	if (!logPath.IsEmpty())
		sprintf(buffer,"Current log folder : %s\r\n",(LPCTSTR)logPath);
	strcat(buffer,"Select new log folder :");
	BROWSEINFO browseInfo={GetSafeHwnd(),0,0,buffer,BIF_RETURNONLYFSDIRS,0,0,0};
	if (SHGetPathFromIDList(SHBrowseForFolder(&browseInfo),buffer))
	{
		SetCurrentDirectory(buffer);
		logPath=buffer;
	}
}

void CSCTS3DevManDlg::OnSize(UINT nType, int cx, int cy) 
{


	CDialog::OnSize(nType, cx, cy);
	int leftPaneWidth=0;
	RECT rect;
	/* Size left and right panes in appropriate ratios */
	if (currentLeftPane)
	{
		currentLeftPane->GetWindowRect(&rect);
		leftPaneWidth=rect.right-rect.left;
		currentLeftPane->MoveWindow(0,toolbarHeight,leftPaneWidth,cy-toolbarHeight);
	}
	if (currentRightPane) currentRightPane->MoveWindow(leftPaneWidth,toolbarHeight,cx-leftPaneWidth,cy-toolbarHeight);
}

void CSCTS3DevManDlg::OnSizing(UINT fwSide, LPRECT pRect) 
{
	UINT nSide=fwSide;
	bool bottom=false;
	bool top=false;
	if (nSide>=6) {bottom=true;nSide-=6;}
	if (nSide>=3) {top=true;nSide-=3;}
	/* Check minimum size */
	if (top && pRect->bottom-pRect->top<minSize.bottom) pRect->top=pRect->bottom-minSize.bottom;
	if (bottom && pRect->bottom-pRect->top<minSize.bottom) pRect->bottom=pRect->top+minSize.bottom;
	if (nSide==1 && pRect->right-pRect->left<minSize.right) pRect->left=pRect->right-minSize.right;
	if (nSide==2 && pRect->right-pRect->left<minSize.right) pRect->right=pRect->left+minSize.right;
	CDialog::OnSizing(fwSide, pRect);
}

BOOL CSCTS3DevManDlg::PreTranslateMessage(MSG* pMsg) 
{
	/* Disable ESC  and Enter */
	if (pMsg->message==WM_KEYDOWN && (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN)) return TRUE;

	return CDialog::PreTranslateMessage(pMsg);
}

/* Callbacks */

LRESULT CSCTS3DevManDlg::SessionComplete(WPARAM wParam,LPARAM lParam)
{
    SctsDMSession *session = (SctsDMSession *)wParam;
	HTREEITEM item;
	int isServer;
	int img;
	int messageCnt=1;
	char buffer[20];
	void *itemData;
	FILE *logFileInf;
	/* Ref#0004 */
	item=currentLeftPane->m_sessionTree.InsertItem("Logs",6,6,(HTREEITEM)session->uiData,TVI_LAST);
	SCTSASSERT(item);
	if ((logFileInf=fopen(session->logFileName,"rb")))
	{
		isServer=fgetc(logFileInf);
		while (fgetc(logFileInf))
		{
			itemData=malloc(sizeof(long)*2);
			SCTSASSERT(fread(itemData,sizeof(long),2,logFileInf)==2);
			img=9+((messageCnt+isServer)%2);
			sprintf(buffer,"Message #%d",messageCnt++);
			currentLeftPane->m_sessionTree.InsertItem(TVIF_PARAM|TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE,buffer,img,img,0,0,(DWORD)itemData,item,TVI_LAST);
		}
		fclose(logFileInf);
		session->logFileName[strlen(session->logFileName)-4]=0;
		strcat(session->logFileName,".xml");
		currentLeftPane->m_sessionTree.SetItemData(item,(DWORD)strdup(session->logFileName));
	}
	if (session->errorLog->HasErrors() || !session->IsSuccessful())
	{
		item=currentLeftPane->m_sessionTree.InsertItem("Errors",(HTREEITEM)session->uiData);
		currentLeftPane->m_sessionTree.SetItemData(item,(DWORD)session->errorLog);
		currentLeftPane->m_sessionTree.SetItemImage(item,7,7);
		session->errorLog->uiData=item;
		session->errorLog=0;
		currentLeftPane->m_sessionTree.SetItemImage((HTREEITEM)session->uiData,8,8);
	}
	else
		currentLeftPane->m_sessionTree.SetItemImage((HTREEITEM)session->uiData,11,11);
	session->uiData=0;
	currentLeftPane->m_sessionTree.RedrawWindow();
	return 0;
}

LRESULT CSCTS3DevManDlg::AddSession(WPARAM wParam,LPARAM lParam)
{
	CSessionPane *sessionPane=new CSessionPane((SctsDMSession *)wParam);
	SctsDMDevice *device=((SctsDMSession *)wParam)->GetDevice();
	char *id="????";
	HTREEITEM item;
	if (device)
	{
		if (device->IsServerDevice())
			id=((SctsDMServerDevice *)device)->userID;
		else
			id=((SctsDMClientDevice *)device)->devID;
	}
	if (((SctsDMSession *)wParam)->uiData)
	{

		currentLeftPane->m_sessionTree.SetItemText((HTREEITEM)((SctsDMSession *)wParam)->uiData,id);
		return 0;
	}
	sessionPane->Create(IDD_SESSIONPANE,currentRightPane);
	item=currentLeftPane->m_sessionTree.InsertItem(id);
	if (currentLeftPane->m_sessionTree.GetRootItem()==item) currentLeftPane->SetTimer(0,160,NULL);
	((SctsDMSession *)wParam)->uiData=item;
	currentLeftPane->m_sessionTree.SetItemData(item,(DWORD)sessionPane);
	currentLeftPane->m_sessionTree.SetItemImage(item,1,1);
	return 0;
}

LRESULT CSCTS3DevManDlg::AddClientDevice(WPARAM wParam,LPARAM lParam)
{
    SctsDMClientDevice *device = (SctsDMClientDevice *)wParam;
	CTreePane *treePane;
	HTREEITEM uiData;
	device->uiData=currentLeftPane->m_accountTree.InsertItem(device->devID,NODE_CLIENTDEV,NODE_CLIENTDEV+NODE_COUNT,currentLeftPane->clientDevices);
	currentLeftPane->m_accountTree.SetItemData((HTREEITEM)device->uiData,(DWORD)device);
	uiData=currentLeftPane->m_accountTree.InsertItem("Tree",NODE_CDEVTREE,NODE_CDEVTREE+NODE_COUNT,(HTREEITEM)device->uiData);
	currentLeftPane->m_accountTree.SetItemData(uiData,(DWORD)device);
	uiData=currentLeftPane->m_accountTree.InsertItem("Tests",NODE_TEST,NODE_TEST+NODE_COUNT,(HTREEITEM)device->uiData);
	currentLeftPane->m_accountTree.SetItemData(uiData,(DWORD)device->tester);
	uiData=currentLeftPane->m_accountTree.InsertItem("DM Accounts",NODE_ACCOUNTS,NODE_COUNT+NODE_ACCOUNTS,(HTREEITEM)device->uiData);
	currentLeftPane->m_accountTree.SetItemData(uiData,(DWORD)(SctsDMTree *)device);
	uiData=currentLeftPane->m_accountTree.InsertItem("Connections",NODE_CONREF,NODE_COUNT+NODE_CONREF,(HTREEITEM)device->uiData);
	currentLeftPane->m_accountTree.SetItemData(uiData,(DWORD)(SctsDMTree *)device);
	treePane=new CTreePane((SctsDMTree *)device);
	treePane->Create(IDD_TREEPANE,currentRightPane);
	device->root->uiData=treePane->m_tree.InsertItem(".");
	treePane->m_tree.SetItemData((HTREEITEM)device->root->uiData,(DWORD)device->root);
	device->uiCallbackData=treePane;
	device->uiCallback=dmTreeCallback;
	return 0;
}

LRESULT CSCTS3DevManDlg::RemoveClientDevice(WPARAM wParam,LPARAM lParam)
{
    SctsDMClientDevice *device = (SctsDMClientDevice *)wParam;
	currentLeftPane->ClearPanes();
	((CTreePane *)device->uiCallbackData)->DestroyWindow();
	delete ((CTreePane *)device->uiCallbackData);
	currentLeftPane->m_accountTree.DeleteItem((HTREEITEM)device->uiData);
	return 0;
}

LRESULT CSCTS3DevManDlg::AddServerDevice(WPARAM wParam,LPARAM lParam)
{
    SctsDMServerDevice *device = (SctsDMServerDevice *)wParam;
	CTreePane *treePane;
	HTREEITEM uiData;
	device->uiData=currentLeftPane->m_accountTree.InsertItem(device->userID,NODE_SERVERDEV,NODE_SERVERDEV+NODE_COUNT,currentLeftPane->serverDevices);
	currentLeftPane->m_accountTree.SetItemData((HTREEITEM)device->uiData,(DWORD)device);
	uiData=currentLeftPane->m_accountTree.InsertItem("Tree",NODE_SDEVTREE,NODE_SDEVTREE+NODE_COUNT,(HTREEITEM)device->uiData);
	currentLeftPane->m_accountTree.SetItemData(uiData,(DWORD)device);
	uiData=currentLeftPane->m_accountTree.InsertItem("Tests",NODE_TEST,NODE_TEST+NODE_COUNT,(HTREEITEM)device->uiData);
	currentLeftPane->m_accountTree.SetItemData(uiData,(DWORD)device->tester);
	uiData=currentLeftPane->m_accountTree.InsertItem("DM Accounts",NODE_ACCOUNTS,NODE_COUNT+NODE_ACCOUNTS,(HTREEITEM)device->uiData);
	currentLeftPane->m_accountTree.SetItemData(uiData,(DWORD)(SctsDMTree *)device);
	uiData=currentLeftPane->m_accountTree.InsertItem("Connections",NODE_CONREF,NODE_COUNT+NODE_CONREF,(HTREEITEM)device->uiData);
	currentLeftPane->m_accountTree.SetItemData(uiData,(DWORD)(SctsDMTree *)device);
	uiData=currentLeftPane->m_accountTree.InsertItem("Commands",NODE_COMMANDS,NODE_COUNT+NODE_COMMANDS,(HTREEITEM)device->uiData);
	currentLeftPane->m_accountTree.SetItemData(uiData,(DWORD)(SctsDMServerTree *)device);
	treePane=new CTreePane((SctsDMTree *)device);
	treePane->Create(IDD_TREEPANE,currentRightPane);
	device->root->uiData=treePane->m_tree.InsertItem(".");
	treePane->m_tree.SetItemData((HTREEITEM)device->root->uiData,(DWORD)device->root);
	device->uiCallbackData=treePane;
	device->uiCallback=dmTreeCallback;
	return 0;
}

LRESULT CSCTS3DevManDlg::RemoveServerDevice(WPARAM wParam,LPARAM lParam)
{
    SctsDMServerDevice *device = (SctsDMServerDevice *)wParam;
	currentLeftPane->ClearPanes();
	((CTreePane *)device->uiCallbackData)->DestroyWindow();
	delete ((CTreePane *)device->uiCallbackData);
	currentLeftPane->m_accountTree.DeleteItem((HTREEITEM)device->uiData);
	return 0;
}

void CSCTS3DevManDlg::OnDevadd() 
{
	int nodeType;
	int tempInt;
	HTREEITEM item1,item2;
	item1=currentLeftPane->m_accountTree.GetSelectedItem();
	while ((item2=currentLeftPane->m_accountTree.GetParentItem(item1))) item1=item2;

	currentLeftPane->ClearPanes();
	currentLeftPane->m_accountTree.GetItemImage(item1,nodeType,tempInt);
	if (nodeType==NODE_CDEV)
	{
		currentRightChildPane=new CClientDevicePane(0);
		currentRightChildPane->Create(IDD_CLIENTDEVICEPANE,currentRightPane);
	}
	else
	{
		CAddServerDevicePane addServerDevice;
		if (addServerDevice.DoModal()==IDCANCEL) return;
		currentRightChildPane->SetFocus();
	}
	deleteChild=true;
	currentRightPane->SetResizeLogic();
}

void CSCTS3DevManDlg::OnDevdel() 
{
	SctsDMDevice::RemoveDevice((SctsDMDevice *)currentLeftPane->m_accountTree.GetItemData(currentLeftPane->m_accountTree.GetSelectedItem()));
}

LRESULT CSCTS3DevManDlg::DisplayUIA(WPARAM wParam,LPARAM lParam)
{
    CDialog *dialog = (CDialog *)wParam;
    UINT dialogID = (UINT)lParam;
	dialog->Create(dialogID);
	return 0;
}

LRESULT CSCTS3DevManDlg::DestroyUIA(WPARAM wParam,LPARAM lParam)
{
    CDialog *dialog = (CDialog *)wParam;
	dialog->DestroyWindow();
	return 0;
}
void CSCTS3DevManDlg::OnHelpAboutscts() 
{
	CAbout about;
	about.DoModal();
}

void CSCTS3DevManDlg::OnHelpReportingabug() 
{
	CLoadError error(appTimeStamp,"Reporting a bug","The files listed below should be e-mailed to the SCTS developement team (OMA-SCTS@MAIL.OPENMOBILEALLIANCE.ORG) along with a description of the bug.");
	error.DoModal();
}

void CSCTS3DevManDlg::OnToolsCredentialcalculator() 
{
	if (credCalc) 
	{
		credCalc->ShowWindow(SW_SHOW);
		return;
	}
	credCalc=new CCredCalc;
	credCalc->Create(IDD_CREDENTIALCALC);
}

void CSCTS3DevManDlg::OnToolsWbxmlxmlconverter() 
{
	CWBXML2XMLConv::Convert();
}

void CSCTS3DevManDlg::OnSettingsUseobexirclient() 
{
	if (!SctsDMClientSession::useOBEXIR)
	{
		GetMenu()->GetSubMenu(0)->CheckMenuItem(ID_SETTINGS_USEOBEXTCPCLIENT,MF_UNCHECKED | MF_BYCOMMAND);
		GetMenu()->GetSubMenu(0)->CheckMenuItem(ID_SETTINGS_USEOBEXIRCLIENT,MF_CHECKED | MF_BYCOMMAND);
		SctsDMClientSession::useOBEXIR=true;
	}
}

void CSCTS3DevManDlg::OnSettingsUseobextcpclient() 
{
	if (SctsDMClientSession::useOBEXIR)
	{
		GetMenu()->GetSubMenu(0)->CheckMenuItem(ID_SETTINGS_USEOBEXTCPCLIENT,MF_CHECKED | MF_BYCOMMAND);
		GetMenu()->GetSubMenu(0)->CheckMenuItem(ID_SETTINGS_USEOBEXIRCLIENT,MF_UNCHECKED | MF_BYCOMMAND);
		SctsDMClientSession::useOBEXIR=false;
	}
}

void CSCTS3DevManDlg::OnConfigurationClearsessions() 
{
	HTREEITEM item1,item2;
	currentLeftPane->m_sessionTree.SelectItem(0);
	item1=currentLeftPane->m_sessionTree.GetRootItem();
	/* Delete only inactive sessions -> session items with logs -> session items with children */
	while (item1)
	{
		item2=currentLeftPane->m_sessionTree.GetNextSiblingItem(item1);
		if (currentLeftPane->m_sessionTree.ItemHasChildren(item1)) currentLeftPane->m_sessionTree.DeleteItem(item1);
		item1=item2;
	}
}

void CSCTS3DevManDlg::OnConfigurationRemoveall() 
{
	SctsDMDevice::RemoveAll();
	currentLeftPane->ClearPanes();
}

void CSCTS3DevManDlg::OnConfigurationSave() 
{
	CRegKey key;
	CString tempStr;
	CFileStatus fileStatus;
	FILE *fp;

	/* Open the main key */
	tempStr="SOFTWARE\\SyncML Conformance Test Suite\\";
	tempStr+=SCTS_VERSION;
	if (key.Open(HKEY_LOCAL_MACHINE,tempStr)!=ERROR_SUCCESS) return;
	/* Write accounts file */
	fp=fopen(appPath,"wb");
	if (fp)
	{
		SctsDMDevice::SaveAll(fp);
		fclose(fp);
	}
	if (CFile::GetStatus(appPath,fileStatus)) SCTSASSERT(key.SetValue(fileStatus.m_mtime.GetTime(),"AccountsTimestamp")==ERROR_SUCCESS);
	/* Close key */
	key.Close();
}

void CSCTS3DevManDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
		/* Set the last size, if window is maximized set all values to -1 */

	if (IsZoomed())
	{
		lastSize.top=-1;
		lastSize.bottom=-1;
		lastSize.left=-1;
		lastSize.right=-1;
	}
	else
	{
		GetWindowRect(&lastSize);
		if( (0 >=lastSize.top) && (0 >= lastSize.bottom) && (0 >=lastSize.left) && (0 >= lastSize.right))
		{ 
			lastSize.top  = 0;
			lastSize.left = 0;
			lastSize.right= 1000;
			lastSize.bottom = 700;
		
		}
	}
	CDialog::OnClose();
}
