usbmgmt/usbmgrtest/t_ncm/src/commandengine.cpp
branchRCL_3
changeset 16 012cc2ee6408
parent 15 f92a4f87e424
equal deleted inserted replaced
15:f92a4f87e424 16:012cc2ee6408
     1 /*
       
     2 * Copyright (c) 2002-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 /** @file
       
    19  @internalComponent
       
    20  @test
       
    21  */
       
    22 
       
    23 #include "commandengine.h"
       
    24 #include "ncmtestconsole.h"
       
    25 
       
    26 CNcmCommandEngine* CNcmCommandEngine::NewL(CUsbNcmConsole& aTestConsole)
       
    27 /**
       
    28 Constructs a CNcmCommandEngine object.
       
    29   @param  aTestConsole The main console 
       
    30 */
       
    31 	{
       
    32 	LOG_STATIC_FUNC_ENTRY
       
    33 	CNcmCommandEngine* self = new(ELeave) CNcmCommandEngine(aTestConsole);
       
    34 	CleanupStack::PushL(self);
       
    35 	self->ConstructL();
       
    36 	CleanupStack::Pop(self);
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 CNcmCommandEngine::~CNcmCommandEngine()
       
    41 	{
       
    42 	Cancel();
       
    43 	RemoveAllCommand();
       
    44 	}
       
    45 
       
    46 CNcmCommandEngine::CNcmCommandEngine(CUsbNcmConsole& aTestConsole)
       
    47 	: CActive(EPriorityStandard)
       
    48 	, iTestConsole(aTestConsole)
       
    49 	, iLastPage(0), iLastItem(-1), iRemainLine(COMMAND_MAX_COUNT_PER_PAGE)
       
    50 	{
       
    51 	CActiveScheduler::Add(this);
       
    52 	}
       
    53 
       
    54 void CNcmCommandEngine::ConstructL()
       
    55 	{
       
    56 	//wait user select command
       
    57 	iTestConsole.iConsole->Read(iStatus);
       
    58 	SetActive();
       
    59 	}
       
    60 
       
    61 void CNcmCommandEngine::DoCancel()
       
    62 	{
       
    63 	iTestConsole.iConsole->ReadCancel();
       
    64 	}
       
    65 
       
    66 void CNcmCommandEngine::RunL()
       
    67 	{
       
    68 	LOG_FUNC
       
    69 	
       
    70 	User::LeaveIfError(iStatus.Int());
       
    71 	
       
    72 	TUint key = iTestConsole.iConsole->KeyCode();
       
    73 
       
    74 	__FLOG_STATIC1(KSubSys, KLogComponent , _L8("key = %c"), key);
       
    75 	
       
    76 	if((key >= '0') && (key <= '9'))
       
    77 		{
       
    78 		iTestConsole.ScheduleDraw(key-0x30);
       
    79 		}
       
    80 	else
       
    81 		{
       
    82 		CNcmCommandBase* command = NULL;
       
    83 		if(iCommandMap.Find(key))
       
    84 			{
       
    85 			command = *(iCommandMap.Find(key));
       
    86 			if(command)
       
    87 				{
       
    88 				command->DoCommandL();
       
    89 				}
       
    90 			else
       
    91 				{
       
    92 				__FLOG_STATIC0(KSubSys, KLogComponent , _L8("The command in hashmap is NULL"));				
       
    93 				}
       
    94 			}						
       
    95 		else
       
    96 			{
       
    97 			CUsbNcmConsoleEvent* event = CUsbNcmConsoleEvent::NewL();
       
    98 			event->iEvent.AppendFormat(_L("G:Unknown:Key[%c]"), key);
       
    99 			iTestConsole.NotifyEvent(event);			
       
   100 			}
       
   101 
       
   102 		}
       
   103 	iTestConsole.iConsole->Read(iStatus);
       
   104 	
       
   105 	SetActive();
       
   106 	}
       
   107 
       
   108 void CNcmCommandEngine::RegisterCommand(CNcmCommandBase* aCommand)
       
   109 /**
       
   110 Add command object into command map
       
   111   @param aCommand  a command which will be used by user 
       
   112 */
       
   113 	{
       
   114 	//Assert if aCommand is NULL
       
   115 	__ASSERT_ALWAYS(aCommand, Panic(ENcmCommandIsNull));
       
   116 	//Assert if the key exists in command map
       
   117 	__ASSERT_ALWAYS(!iCommandMap.Find(aCommand->Key()), Panic(ENcmCommandKeyExists));
       
   118 	
       
   119 	//add command
       
   120 	TInt err = iCommandMap.Insert(aCommand->Key(), aCommand);
       
   121 	if(err != KErrNone)
       
   122 		{
       
   123 		User::Panic(_L("RegisterCommand"), err);
       
   124 		}
       
   125 	
       
   126 	//make the command to be displayed in which page, which line
       
   127 	TInt line = (aCommand->Description().Length() + NUM_CHARACTERS_ON_LINE + 4) / (NUM_CHARACTERS_ON_LINE);
       
   128 	iRemainLine -= line;
       
   129 	if(iRemainLine < 0)
       
   130 		{
       
   131 		iLastPage ++;
       
   132 		iRemainLine = COMMAND_MAX_COUNT_PER_PAGE - line;
       
   133 		iLastItem = 0;
       
   134 		}
       
   135 	else
       
   136 		{
       
   137 		iLastItem ++;
       
   138 		}
       
   139 	iCommandKeys[iLastPage][iLastItem] = aCommand->Key();
       
   140 	
       
   141 	}
       
   142 
       
   143 void CNcmCommandEngine::RemoveAllCommand()
       
   144 /**
       
   145 Destroy all commands in command map  
       
   146 */
       
   147 	{
       
   148 	CNcmCommandBase* currentitem;
       
   149 
       
   150 	RHashMap<TUint, CNcmCommandBase*>::TIter hashMapIter(iCommandMap);
       
   151 	TInt count = iCommandMap.Count();
       
   152 	for(TInt i=0; i< count; i++)
       
   153 	    {
       
   154 	    currentitem = *(hashMapIter.NextValue());
       
   155 	    if(currentitem)
       
   156 	    	{
       
   157 	    	hashMapIter.RemoveCurrent();
       
   158 	    	delete currentitem;
       
   159 	    	}
       
   160 	    }
       
   161 	iCommandMap.Close();
       
   162 	
       
   163 	}
       
   164 
       
   165 
       
   166 void CNcmCommandEngine::PrintHelp(TInt aPage)
       
   167 /**
       
   168 Get command help info (command key - command description) 
       
   169 @param aPage  the command help info of specified page
       
   170 */
       
   171 	{		
       
   172 	LOG_FUNC
       
   173 	
       
   174 	__ASSERT_ALWAYS(((aPage <= COMMAND_MAX_PAGE) && (aPage > 0)), Panic(ENcmArrayBound));
       
   175 	
       
   176 	TUint key = iCommandKeys[aPage-1][0];
       
   177 	if(key == 0)
       
   178 		{
       
   179 		//No commands in this page
       
   180 		iTestConsole.iConsole->Printf(_L("No commands\n"));
       
   181 		}
       
   182 	else
       
   183 		{
       
   184 		//Display command
       
   185 		TInt i = 0;
       
   186 		while((key != 0) && (i < COMMAND_MAX_COUNT_PER_PAGE))
       
   187 			{
       
   188 			CNcmCommandBase* command = NULL;
       
   189 			command = *(iCommandMap.Find(key));
       
   190 			iTestConsole.iConsole->Printf(_L("%c - %S\n"), command->Key(), &command->Description());
       
   191 			i++;
       
   192 			key = iCommandKeys[aPage-1][i];
       
   193 			}
       
   194 		}
       
   195 
       
   196 	}
       
   197 
       
   198 CNcmCommandBase::CNcmCommandBase(TInt aPriority, CUsbNcmConsole& aConsole, TUint aKey)
       
   199 	: CActive(aPriority), iTestConsole(aConsole), iKey(aKey)
       
   200 /**
       
   201 Constructor
       
   202 @param aPriority  	Active object's priority
       
   203 @param aConsole		The main console
       
   204 @param aKey			command key
       
   205 */
       
   206 	{
       
   207 	}
       
   208 
       
   209 CNcmCommandBase::~CNcmCommandBase()
       
   210 	{	
       
   211 	}
       
   212 
       
   213 TUint CNcmCommandBase::Key() const
       
   214 	{
       
   215 	return iKey;
       
   216 	}
       
   217 
       
   218 void CNcmCommandBase::SetKey(TUint aKey)
       
   219 	{ 
       
   220 	iKey = aKey; 
       
   221 	}
       
   222 
       
   223 
       
   224 const TDesC& CNcmCommandBase::Description()
       
   225 	{ 
       
   226 	return iDescription; 
       
   227 	}
       
   228 
       
   229 void CNcmCommandBase::SetDescription(const TDesC& aDescription)
       
   230 /**
       
   231 Set command description
       
   232 @param aDescription  the new description for the command 
       
   233 */
       
   234 	{
       
   235 	iDescription.SetLength(0);
       
   236 	iDescription.Copy(aDescription.Left(NUM_CHARACTERS_ON_LINE));
       
   237 	}
       
   238 void CNcmCommandBase::RunL()
       
   239 	{	
       
   240 	}
       
   241 
       
   242 void CNcmCommandBase::DoCancel()
       
   243 	{	
       
   244 	}
       
   245 TInt CNcmCommandEngine::RunError(TInt aError)
       
   246 	{
       
   247 	User::Panic(_L("CNcmCommandEngine"), aError);
       
   248 	return aError;
       
   249 	}