multimediacommsengine/tsrc/testdriver/testclient/ui/src/CTcAppUi.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2004 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:  Implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <bldvariant.hrh>
       
    19 #include <uikon.hrh>	// EEikCmdExit
       
    20 #include <eikmenup.h>
       
    21 #include <eikdoc.h>
       
    22 #include <barsread.h>
       
    23 
       
    24 #ifdef __SERIES60_
       
    25 #include <errorui.h>
       
    26 #include <aknquerydialog.h>
       
    27 #else
       
    28 #ifdef _SERIES_90_
       
    29 #include <cknconf.h>
       
    30 #else
       
    31 #include <eikinfo.h>
       
    32 #endif
       
    33 #include <eikcore.rsg>
       
    34 #endif
       
    35 
       
    36 #include "CTcBearer.h"
       
    37 #include "TTcBtFactory.h"
       
    38 #include "TTcTcpFactory.h"
       
    39 #include "TTcSerialFactory.h"
       
    40 #include "CTcTestRunner.h"
       
    41 #include "CTcWatcher.h"
       
    42 
       
    43 #include "CTcAppUi.h"
       
    44 #include "CTcContainer.h"
       
    45 #include "CTcSettingsDialog.h"
       
    46 #include "CTcBacklight.h"
       
    47 #include "TestClientConstants.h"
       
    48 #include "TestClient.hrh"
       
    49 #include <TestClient.rsg>
       
    50 	
       
    51 CTcAppUi::CTcAppUi()
       
    52 	{
       
    53 	}
       
    54 
       
    55 CTcAppUi::~CTcAppUi()
       
    56 	{
       
    57 	// These must be deleted first, they might generate some observer
       
    58 	// calls to the container
       
    59 	delete iRunner;
       
    60 	delete iBearer;
       
    61 
       
    62 #ifdef __SERIES60_
       
    63 	delete iErrorUi;
       
    64 #endif
       
    65 
       
    66 	// Remove the container from the control stack and destroy
       
    67 	if( iContainer )
       
    68 		{
       
    69 		RemoveFromStack( iContainer );
       
    70 		delete iContainer;
       
    71 		}
       
    72 
       
    73 	delete iWatcher;
       
    74 	delete iBacklight;
       
    75 	}
       
    76 
       
    77 void CTcAppUi::ConstructL()
       
    78 	{
       
    79 	// Have to use this one with the SIP stack
       
    80 	CCoeEnv::Static()->DisableExitChecks( ETrue );
       
    81 
       
    82 #ifdef __SERIES60_
       
    83     // Skin support is required by S60 3.0 sdk (wk02 and later?)
       
    84 	BaseConstructL( EAknEnableSkin );	
       
    85 #else
       
    86     BaseConstructL();
       
    87 #endif
       
    88 
       
    89 	// Load settings
       
    90 	iSettings.Load();
       
    91 
       
    92 #ifdef __SERIES60_
       
    93 	// Construct error ui
       
    94 	iErrorUi = CErrorUI::NewL();
       
    95 #endif
       
    96 
       
    97 	// Construct our main view
       
    98 	iContainer = CTcContainer::NewL( ClientRect() );
       
    99 	AddToStackL( iContainer );
       
   100 
       
   101 	// If this is the first we are running, automatically display settings.
       
   102 	if( iSettings.iIsFirstRun )
       
   103 		{
       
   104 		iSettings.iIsFirstRun = EFalse;
       
   105 		CmdSettingsL();
       
   106 		}
       
   107 
       
   108 	// Initiate connect if AutoConnect is requested by user settings
       
   109 	if( iSettings.iAutoConnect )
       
   110 		{
       
   111 		CmdConnectL();
       
   112 		}
       
   113 	else
       
   114 		{
       
   115 		SetStatus( KTcNotConnected );
       
   116 		}
       
   117 
       
   118 /*  causes crash!! 
       
   119 	iWatcher = CTcWatcher::NewL( Document()->Application()->DllName(),
       
   120 								 KWatcherTimeout );*/
       
   121 	iBacklight = CTcBacklight::NewL();
       
   122 	}
       
   123 
       
   124 void CTcAppUi::HandleCommandL( TInt aCommand )
       
   125 	{
       
   126 	// Dispatch commands
       
   127 	switch( aCommand )
       
   128 		{
       
   129 		case ECmdConnect:
       
   130 			{
       
   131 			CmdConnectL();
       
   132 			break;
       
   133 			}
       
   134 		case ECmdDisconnect:
       
   135 			{
       
   136 			CmdDisconnect();
       
   137 			break;
       
   138 			}
       
   139 		case ECmdSettings:
       
   140 			{
       
   141 			CmdSettingsL();
       
   142 			break;
       
   143 			}
       
   144 		case ECmdLocalAddress:
       
   145 			{
       
   146 			CmdLocalAddressL();
       
   147 			break;
       
   148 			}
       
   149 		case EEikCmdExit:
       
   150 			{
       
   151 			Exit();
       
   152 			break;
       
   153 			}
       
   154 		default:
       
   155 			{
       
   156 			// do nothing
       
   157 			break;
       
   158 			}
       
   159 		}
       
   160 	}
       
   161 
       
   162 void CTcAppUi::DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane )
       
   163 	{
       
   164 	switch( aResourceId )
       
   165 		{
       
   166 		// Hide "Connect" in connected mode and "Disconnect" in disconnected mode
       
   167 		case R_TC_MENU:
       
   168 			{
       
   169 			TBool state( EFalse );
       
   170 			if( iBearer )
       
   171 				{
       
   172 				state = ETrue;
       
   173 				}
       
   174 			aMenuPane->SetItemDimmed( ECmdConnect, state );
       
   175 			aMenuPane->SetItemDimmed( ECmdDisconnect, !state );
       
   176 			aMenuPane->SetItemDimmed( ECmdLocalAddress, !state );
       
   177 			break;
       
   178 			}
       
   179 		default:
       
   180 			{
       
   181 			// not for us..
       
   182 			break;
       
   183 			}
       
   184 		}
       
   185 	}
       
   186 
       
   187 void CTcAppUi::BearerCompletion( MTcBearerObserver::TOperation aOp,
       
   188 							     TInt aStatus )
       
   189 	{
       
   190 	// If there are any errors, disconnect and show an appropriate error note
       
   191 	if( aStatus != KErrNone )
       
   192 		{
       
   193 		ShowErrorNote( aStatus );
       
   194 		CmdDisconnect();
       
   195 		return;
       
   196 		}
       
   197 
       
   198 	switch( aOp )
       
   199 		{
       
   200 		// Bearer is now listening for incoming connections
       
   201 		case MTcBearerObserver::EListen:
       
   202 			{
       
   203 			SetStatus( KTcListening );
       
   204 			break;
       
   205 			}
       
   206 
       
   207 		case MTcBearerObserver::EConnecting:
       
   208 		    {
       
   209 		    SetStatus( KTcConnecting );
       
   210 		    break;
       
   211 		    }
       
   212 		// Bearer is now connected
       
   213 		case MTcBearerObserver::EConnect:
       
   214 			{
       
   215 			SetStatus( KTcConnected );
       
   216 			TRAPD( err,
       
   217 				{
       
   218 				// CTcTestRunner takes observer role for iBearer
       
   219 				iRunner = CTcTestRunner::NewL( *iBearer, *this, iSettings.iIAPId );
       
   220 				// Start test case runner
       
   221 				iRunner->Start();
       
   222 				} );
       
   223 			// Report any errors and disconnect
       
   224 			if( err != KErrNone )
       
   225 				{
       
   226 				ShowErrorNote( err );
       
   227 				CmdDisconnect();
       
   228 				}
       
   229 			break;
       
   230 			}
       
   231 
       
   232 		default:
       
   233 			{
       
   234 			// do nothing for other events (basically not encountered)
       
   235 			break;
       
   236 			}
       
   237 		}
       
   238 
       
   239 	}
       
   240 
       
   241 void CTcAppUi::NotifyDisconnect()
       
   242 	{
       
   243 	// Test Runner has detected a remote disconnect, connect again
       
   244 	TRAP_IGNORE( CmdConnectL() )
       
   245 	}
       
   246 
       
   247 void CTcAppUi::NotifyStatusChange()
       
   248 	{
       
   249 	// iRunner may be NULL if CTestRunner construction failed but
       
   250 	// it still caused a status change notification.
       
   251 	if( iRunner )
       
   252 		{
       
   253 		// Test Runner status has changed, update main view labels
       
   254 		iContainer->Update( *iRunner );
       
   255 		}
       
   256 	}
       
   257 
       
   258 void CTcAppUi::CmdConnectL()
       
   259 	{
       
   260 	// Make sure we're properly disconnected
       
   261 	CmdDisconnect();
       
   262 	SetStatus( KTcInitializing );
       
   263 
       
   264 	// Create bearer
       
   265 	TTcSettingsBearerType btype = static_cast<TTcSettingsBearerType>( iSettings.iBearerType );
       
   266 	
       
   267 	switch ( btype )
       
   268 	    {
       
   269 	    case EBearerBluetooth:
       
   270 	        {
       
   271 	        TTcBtFactory factory;
       
   272 		    iBearer = CTcBearer::NewL( factory );
       
   273 	        break;
       
   274 	        }
       
   275 	    case EBearerTCP:
       
   276 	        {
       
   277 	        TTcTcpFactory factory( iSettings.iTCPPort, iSettings.iIAPId );
       
   278 		    iBearer = CTcBearer::NewL( factory );
       
   279 	        break;
       
   280 	        }
       
   281 	    case EBearerSerial:
       
   282 	        {
       
   283 	        HBufC* csyName = ReadFromResourceArrayLC( R_TC_SETTINGS_CSYNAME, iSettings.iCSYName );
       
   284 	        HBufC* commPort = ReadFromResourceArrayLC( R_TC_SETTINGS_COMMPORT, iSettings.iCommPort );
       
   285 	        TTcSerialFactory factory( *csyName, *commPort, iSettings.iBaudrate ); 
       
   286 	        CleanupStack::PopAndDestroy( 2 );
       
   287 		    iBearer = CTcBearer::NewL( factory );
       
   288 	        break;
       
   289 	        }
       
   290 	    default:
       
   291 	        {
       
   292 	        User::Leave( KErrArgument );
       
   293 	        break;
       
   294 	        }
       
   295 	    }
       
   296 
       
   297 	// Register for bearer events (->CTcAppUi::BearerCompletion)
       
   298 	iBearer->SetObserver( this );
       
   299 	// Start the connect sequence
       
   300 	
       
   301 	TInetAddr* addr = NULL;
       
   302 	if ( !iSettings.iTCPRemoteAddr.IsUnspecified() )
       
   303 	    {
       
   304 	    addr = new ( ELeave ) TInetAddr;
       
   305 	    if(iSettings.iTCPRemoteAddr.Family() == KAfInet6)
       
   306 	        {
       
   307 	        addr->SetAddress( iSettings.iTCPRemoteAddr.Ip6Address() );
       
   308 	        }
       
   309 	    else
       
   310 	        {
       
   311 	        addr->SetAddress( iSettings.iTCPRemoteAddr.Address() );
       
   312 	        }
       
   313 	    addr->SetPort( iSettings.iTCPPort );
       
   314 	    }
       
   315 	
       
   316 	TRAPD( err, iBearer->ConnectL( addr ) );
       
   317 	if( err != KErrNone )
       
   318 		{
       
   319 		delete addr;
       
   320 		CmdDisconnect();
       
   321 		User::Leave( err );
       
   322 		}
       
   323     delete addr;
       
   324 	}
       
   325 
       
   326 void CTcAppUi::CmdDisconnect()
       
   327 	{
       
   328 	SetStatus( KTcNotConnected );
       
   329 
       
   330 	// Delete Test Runner and Bearer
       
   331 	delete iRunner;
       
   332 	iRunner = NULL;
       
   333 	delete iBearer;
       
   334 	iBearer = NULL;
       
   335 	}
       
   336 
       
   337 void CTcAppUi::CmdSettingsL()
       
   338 	{
       
   339 	// Construct the settings dialog
       
   340 	CTcSettingsDialog* dlg = new( ELeave ) CTcSettingsDialog( iSettings );
       
   341 	CleanupStack::PushL( dlg );
       
   342 	dlg->ConstructL();
       
   343 	CleanupStack::Pop();	// dlg
       
   344 
       
   345 	// ..and display it. It is automatically deleted
       
   346 	dlg->ExecuteLD( R_TC_SETTINGS_DLG );
       
   347 	}
       
   348 
       
   349 void CTcAppUi::CmdLocalAddressL()
       
   350 	{
       
   351 	// Fetch local address(es)
       
   352 	TBuf<200> address;
       
   353 	iBearer->GetLocalAddressL( address );
       
   354 	address.Insert( 0, _L("Local IP:\n") );
       
   355 
       
   356 #ifdef __SERIES60_
       
   357 	// Construct and show a query dialog
       
   358 	CAknQueryDialog* dlg = new( ELeave ) CAknQueryDialog;
       
   359 	dlg->ExecuteLD( R_TC_LOCAL_ADDRESS_DLG, address );
       
   360 #else
       
   361 #ifdef _SERIES_90_
       
   362 	// Construct and show a confirmation dialog
       
   363 	CCknConfirmationDialog::RunDlgWithDefaultIconLD( address, R_EIK_BUTTONS_CONTINUE );
       
   364 #else
       
   365 	CEikInfoDialog::RunDlgLD( _L("Local IP"), address, CEikInfoDialog::EAllowEnter );
       
   366 #endif
       
   367 #endif
       
   368 	}
       
   369 
       
   370 void CTcAppUi::SetStatus( const TDesC8& aText )
       
   371 	{
       
   372 	iContainer->UpdateStatus( aText );
       
   373 	}
       
   374 
       
   375 #ifdef __SERIES60_
       
   376 
       
   377 HBufC* CTcAppUi::ReadFromResourceArrayLC( TInt aResourceId, TInt aSettingIndex )
       
   378     {
       
   379     __ASSERT_ALWAYS( aSettingIndex >= 0, User::Leave( KErrArgument ) );
       
   380     
       
   381     TResourceReader reader;
       
   382     CEikonEnv::Static()->CreateResourceReaderLC( reader, aResourceId );
       
   383     
       
   384     TInt numberOfItems = reader.ReadInt16(); // item count
       
   385 
       
   386     __ASSERT_ALWAYS( aSettingIndex < numberOfItems, User::Leave( KErrArgument ) );
       
   387     
       
   388     TBool done( EFalse );
       
   389     HBufC* temp = 0;
       
   390     for ( TInt i = 0; i < numberOfItems && !done; i++ )
       
   391         {
       
   392         TInt value = reader.ReadInt16();  
       
   393         TPtrC text = reader.ReadTPtrC();
       
   394         if ( i == aSettingIndex )
       
   395             {
       
   396             done = ETrue;
       
   397             temp = text.AllocL();
       
   398             }
       
   399         }
       
   400     CleanupStack::PopAndDestroy(); // reader
       
   401     CleanupStack::PushL( temp );
       
   402     return temp;
       
   403     }
       
   404     
       
   405 #else
       
   406 
       
   407 HBufC* CTcAppUi::ReadFromResourceArrayLC( TInt aResourceId, TInt aSettingIndex )
       
   408     {
       
   409     __ASSERT_ALWAYS( aSettingIndex >= 0, User::Leave( KErrArgument ) );
       
   410     CDesC16ArrayFlat* array = iCoeEnv->ReadDesC16ArrayResourceL( aResourceId );
       
   411     CleanupStack::PushL( array );
       
   412     __ASSERT_ALWAYS( aSettingIndex < array->MdcaCount(), User::Leave( KErrArgument ) );
       
   413     HBufC* temp = array->MdcaPoint( aSettingIndex ).AllocL();
       
   414     CleanupStack::PopAndDestroy( array );
       
   415     CleanupStack::PushL( temp );
       
   416     return temp;
       
   417     }
       
   418     
       
   419 #endif
       
   420     
       
   421 void CTcAppUi::ShowErrorNote( TInt aStatus )
       
   422 	{
       
   423 	if( aStatus != KErrNone )
       
   424 		{
       
   425 #ifdef __SERIES60_
       
   426 		// If we run out of memory (etc.), just ignore the error
       
   427 		TRAP_IGNORE( iErrorUi->ShowGlobalErrorNoteL( aStatus ) )
       
   428 #else
       
   429 		// Construct and show a confirmation dialog
       
   430 		TBuf< 32 > errorMsg;
       
   431 		errorMsg.Format( _L("Fatal error: %d"), aStatus );
       
   432 #ifdef _SERIES_90_
       
   433 		TRAP_IGNORE( CCknConfirmationDialog::RunDlgWithDefaultIconLD( errorMsg, R_EIK_BUTTONS_CONTINUE ) )
       
   434 #else
       
   435 		TRAP_IGNORE( CEikInfoDialog::RunDlgLD( _L("Error"), errorMsg, CEikInfoDialog::EAllowEnter ) )
       
   436 #endif
       
   437 #endif
       
   438 		}
       
   439 	}