testconns/statdesktop/desktop/source/desktop/src/statmanageconnection.cpp
changeset 4 b8d1455fddc0
equal deleted inserted replaced
2:73b88125830c 4:b8d1455fddc0
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "stdafx.h"
       
    23 #include "statdesktop.h"
       
    24 #include "STATManageConnection.h"
       
    25 
       
    26 #ifdef _DEBUG
       
    27 #define new DEBUG_NEW
       
    28 #undef THIS_FILE
       
    29 static char THIS_FILE[] = __FILE__;
       
    30 #endif
       
    31 
       
    32 // global var to hold our selection when we exit the dialog
       
    33 CString cConnectionInfo;
       
    34 
       
    35 /////////////////////////////////////////////////////////////////////////////
       
    36 // STATManageConnection dialog
       
    37 
       
    38 
       
    39 STATManageConnection::STATManageConnection(CWnd* pParent /*=NULL*/)
       
    40 	: CDialog(STATManageConnection::IDD, pParent)
       
    41 {
       
    42 	//{{AFX_DATA_INIT(STATManageConnection)
       
    43 	//}}AFX_DATA_INIT
       
    44 }
       
    45 
       
    46 
       
    47 void STATManageConnection::DoDataExchange(CDataExchange* pDX)
       
    48 {
       
    49 	CDialog::DoDataExchange(pDX);
       
    50 	//{{AFX_DATA_MAP(STATManageConnection)
       
    51 	DDX_Control(pDX, IDC_ADDRESS, m_Address);
       
    52 	DDX_Control(pDX, IDC_TRANSPORT, m_Transport);
       
    53 	DDX_Control(pDX, IDC_LISTCONNECTIONS, m_ListConnections);
       
    54 	//}}AFX_DATA_MAP
       
    55 }
       
    56 
       
    57 
       
    58 BEGIN_MESSAGE_MAP(STATManageConnection, CDialog)
       
    59 	//{{AFX_MSG_MAP(STATManageConnection)
       
    60 	ON_BN_CLICKED(IDC_REMOVE, OnRemove)
       
    61 	ON_BN_CLICKED(IDC_ADD, OnAdd)
       
    62 	ON_LBN_SELCHANGE(IDC_LISTCONNECTIONS, OnSelchangeListconnections)
       
    63 	ON_CBN_SELCHANGE(IDC_TRANSPORT, OnSelchangeTransport)
       
    64 	//}}AFX_MSG_MAP
       
    65 END_MESSAGE_MAP()
       
    66 
       
    67 /////////////////////////////////////////////////////////////////////////////
       
    68 // STATManageConnection message handlers
       
    69 
       
    70 BOOL STATManageConnection::OnInitDialog()
       
    71 {
       
    72 	CDialog::OnInitDialog();
       
    73 
       
    74 	cConnectionInfo.ReleaseBuffer();
       
    75 	m_Transport.SetCurSel(2);
       
    76 	statIniFile.SetIniFileName(STAT_INI_NAME);
       
    77 		CString setting;
       
    78 //	lCount = 0;
       
    79 	if(statIniFile.SectionExists(ST_TEST_KEY) )
       
    80 	{
       
    81 
       
    82 		lCount = 0;
       
    83 		setting.Empty();
       
    84 		setting=statIniFile.GetKeyValue(ST_CONNECTIONIDX,ST_TEST_KEY);
       
    85 		if(!setting.IsEmpty())
       
    86 		{
       
    87 			lCount = _ttol(setting);
       
    88 			m_ListConnections.SetCurSel(lCount);
       
    89 			OnSelchangeListconnections();
       
    90 		}
       
    91 	}
       
    92 
       
    93 	if(statIniFile.SectionExists(ST_CONNECTION_LIST) )
       
    94 	{
       
    95 		for( int i=0;i<lCount;++i)
       
    96 		{
       
    97 			TCHAR buf[6];
       
    98 			_ltot(i+1, buf, 10);
       
    99 			setting.Empty();
       
   100 			setting=statIniFile.GetKeyValue(buf,ST_CONNECTION_LIST);
       
   101 			if(!setting.IsEmpty())
       
   102 				m_ListConnections.AddString(setting);
       
   103 		}
       
   104 	}
       
   105 	return TRUE;
       
   106 }
       
   107 
       
   108 
       
   109 void STATManageConnection::OnAdd() 
       
   110 {
       
   111 	CString transport;
       
   112 	CString address;
       
   113 	switch(m_Transport.GetCurSel())
       
   114 	{
       
   115 	case 0:	// socket
       
   116 		transport = "SymbianSocket:";
       
   117 		break;
       
   118 	case 1:	// serial
       
   119 		transport = "SymbianSerial:";
       
   120 		break;
       
   121 	case 2:	// infra-red
       
   122 		transport = "SymbianInfrared:";
       
   123 		break;
       
   124 	case 3:	// bluetooth
       
   125 		transport = "SymbianBluetooth:";
       
   126 		break;
       
   127 	case 4:	// usb
       
   128 
       
   129 		//break;
       
   130 	default:
       
   131 		return;
       
   132 	}
       
   133 
       
   134 	m_Address.GetWindowText(address);
       
   135 	transport += address;
       
   136 
       
   137 	if (!transport.IsEmpty() && !InList(transport))
       
   138 	{
       
   139 		m_ListConnections.AddString(transport);
       
   140 		// highlight it in the list
       
   141 		m_ListConnections.SetCurSel(m_ListConnections.FindStringExact(0, transport.GetBuffer(0)));
       
   142 	}
       
   143 	else
       
   144 		AfxMessageBox(_T("This configuration already exists in the list."));
       
   145 }
       
   146 
       
   147 void STATManageConnection::OnRemove() 
       
   148 {
       
   149 	int index = m_ListConnections.GetCurSel();
       
   150 	if (index != LB_ERR)
       
   151 	{
       
   152 		m_ListConnections.DeleteString(index);
       
   153 		m_ListConnections.SetCurSel(0);
       
   154 		OnSelchangeListconnections();
       
   155 	}
       
   156 }
       
   157 
       
   158 void STATManageConnection::OnOK() 
       
   159 {
       
   160 	int index = m_ListConnections.GetCurSel();
       
   161 	if (index != LB_ERR)
       
   162 	{
       
   163 		CString item;
       
   164 		TCHAR buf[16];
       
   165 		CWaitCursor oWait;
       
   166 		m_ListConnections.GetText(index, cConnectionInfo);
       
   167 		lCount=m_ListConnections.GetCount();
       
   168 		_ltot(lCount, buf, 10);
       
   169 		statIniFile.WriteKey(buf,ST_CONNECTIONIDX,ST_TEST_KEY);
       
   170 		//empty connection list
       
   171 		statIniFile.DeleteSection(ST_CONNECTION_LIST);
       
   172 		for( int i=0;i<lCount;++i)
       
   173 		{
       
   174 			_ltot(i+1, buf, 10);
       
   175 			m_ListConnections.GetText(i, item);
       
   176 			statIniFile.WriteKey(item,buf,ST_CONNECTION_LIST);
       
   177 		}
       
   178 		CDialog::OnOK();
       
   179 	}
       
   180 }
       
   181 
       
   182 void STATManageConnection::OnCancel() 
       
   183 {
       
   184 	cConnectionInfo.ReleaseBuffer();
       
   185 	CDialog::OnCancel();
       
   186 }
       
   187 
       
   188 /////////////////////////////////////////////////////////////////////////////
       
   189 // 
       
   190 bool STATManageConnection::InList(CString & Entry)
       
   191 {
       
   192 	CString item;
       
   193 	int iCount = m_ListConnections.GetCount();
       
   194 	for (int i=0;i<iCount;i++)
       
   195 	{
       
   196 		m_ListConnections.GetText(i, item);
       
   197 
       
   198 		if (item.Compare(Entry.GetBuffer(0)) == 0)
       
   199 		{
       
   200 			return true;
       
   201 		}
       
   202 	}
       
   203 
       
   204 	return false;
       
   205 }
       
   206 
       
   207 //////////////////////////////////////////////////////////////////////////////////////////
       
   208 // Converts a char * to it's Unicode equivalent
       
   209 //
       
   210 LPTSTR STATManageConnection::ToUnicode(const char *string)
       
   211 {
       
   212 #ifdef UNICODE
       
   213 	static TCHAR szBuffer[MAX_UNICODE_LEN + 1] = {0};
       
   214 	szBuffer[0] = (TCHAR)0;
       
   215 
       
   216     // Convert to UNICODE.
       
   217     if (!MultiByteToWideChar(CP_ACP,					// conversion type
       
   218 							 0,							// flags
       
   219 							 string,					// source
       
   220 							 -1,						// length
       
   221 							 szBuffer,					// dest
       
   222 							 MAX_UNICODE_LEN))			// length
       
   223     {
       
   224         return _T("Could not convert");
       
   225     }
       
   226 
       
   227     return szBuffer;
       
   228 #else
       
   229 	return (LPTSTR)string;
       
   230 #endif
       
   231 }
       
   232 
       
   233 
       
   234 //////////////////////////////////////////////////////////////////////////////////////////
       
   235 // Converts a Unicode to it's char * equivalent
       
   236 //
       
   237 char * STATManageConnection::ToAnsi(LPCTSTR string)
       
   238 {
       
   239 #ifdef UNICODE
       
   240 	static char szBuffer[MAX_UNICODE_LEN + 1] = {0};
       
   241 	szBuffer[0] = (char)0;
       
   242 
       
   243     // Convert to ANSI.
       
   244     if (!WideCharToMultiByte(CP_ACP,					// conversion type
       
   245 							 0,							// flags
       
   246 							 string,					// source
       
   247 							 -1,						// length
       
   248 							 szBuffer,					// dest
       
   249 							 MAX_UNICODE_LEN,			// length
       
   250 							 NULL,
       
   251 							 NULL ))
       
   252     {
       
   253         return "Could not convert";
       
   254     }
       
   255 
       
   256     return szBuffer;
       
   257 #else
       
   258 	return (char *)string;
       
   259 #endif
       
   260 }
       
   261 
       
   262 
       
   263 void STATManageConnection::OnSelchangeListconnections() 
       
   264 {
       
   265 	CString item;
       
   266 	int index = m_ListConnections.GetCurSel();
       
   267 	if (index != LB_ERR)
       
   268 	{
       
   269 		m_ListConnections.GetText(index, item);
       
   270 
       
   271 		int i = item.Find(_T(':'));
       
   272 		if (i != -1)
       
   273 		{
       
   274 			CString transport = item.Left(i);
       
   275 
       
   276 			if (transport.CompareNoCase(_T("SymbianSocket")) == 0)
       
   277 			{
       
   278 				m_Transport.SetCurSel(SymbianSocket - 1);
       
   279 			}
       
   280 			else if (transport.CompareNoCase(_T("SymbianSerial")) == 0)
       
   281 			{
       
   282 				m_Transport.SetCurSel(SymbianSerial - 1);
       
   283 			}
       
   284 			else if (transport.CompareNoCase(_T("SymbianInfrared")) == 0)
       
   285 			{
       
   286 				m_Transport.SetCurSel(SymbianInfrared - 1);
       
   287 			}
       
   288 			else if (transport.CompareNoCase(_T("SymbianBluetooth")) == 0)
       
   289 			{
       
   290 				m_Transport.SetCurSel(SymbianBluetooth - 1);
       
   291 			}
       
   292 			else if (transport.CompareNoCase(_T("SymbianUsb")) == 0)
       
   293 			{
       
   294 
       
   295 			}
       
   296 
       
   297 			m_Address.SetWindowText(item.Mid(i + 1));
       
   298 		}
       
   299 	}
       
   300 }
       
   301 
       
   302 void STATManageConnection::OnSelchangeTransport() 
       
   303 {
       
   304 	// If the user has clicked to a selection of the transport
       
   305 	// list then we warn them if they selected 'USB'.
       
   306 
       
   307 	CString item;
       
   308 	int index = m_Transport.GetCurSel();
       
   309 	if (index != LB_ERR)
       
   310 	{
       
   311 		m_Transport.GetLBText(index, item);
       
   312 
       
   313 		if ((item.CompareNoCase(_T("SymbianInfrared")) == 0) ||
       
   314 			(item.CompareNoCase(_T("SymbianBluetooth")) == 0))
       
   315 		{
       
   316 			// If the transport selection is infrared we show a 
       
   317 			// warning that infrared is treated the same as 
       
   318 			// serial and specify that the user should enter a 
       
   319 			// port number in the address field.
       
   320 			CString	symbian;
       
   321 			CString	appName;
       
   322 			CString	prompt;
       
   323 
       
   324 			if( ( symbian.LoadString( IDS_SYMBIAN ) ) &&
       
   325 				( appName.LoadString( IDS_APPNAME ) ) &&
       
   326 				( prompt.LoadString( IDS_PORTPROMPT ) ) )
       
   327 			{
       
   328 				CString	msg;
       
   329 				msg.Format( prompt, symbian, appName );
       
   330 
       
   331 				::AfxMessageBox( msg );
       
   332 			}
       
   333 		}
       
   334 	}
       
   335 }