terminalsecurity/SCP/SCPHistoryPlugin/src/SCPHistoryPlugin.cpp
changeset 0 b497e44ab2fc
child 9 57a65a3a658c
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2000 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 of terminalsecurity components
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <e32std.h>
       
    22 #include <ecom/implementationproxy.h>
       
    23 #include <SCPParamObject.h>
       
    24 
       
    25 #include "SCPHistoryPlugin.h"
       
    26 #include <SCPHistoryPluginLang.rsg>
       
    27 #include "SCP_IDs.h"
       
    28 
       
    29 
       
    30 // ============================= LOCAL FUNCTIONS  =============================
       
    31 
       
    32 // ============================= MEMBER FUNCTIONS =============================
       
    33 
       
    34 // ----------------------------------------------------------------------------
       
    35 // CSCPHistoryPlugin::NewL
       
    36 // Two-phased contructor
       
    37 // (static, may leave)
       
    38 // Status : Approved
       
    39 // ----------------------------------------------------------------------------
       
    40 //
       
    41 CSCPHistoryPlugin* CSCPHistoryPlugin::NewL()
       
    42     {
       
    43     Dprint ( ( _L( "CSCPHistoryPlugin::NewL()" ) ) );
       
    44 	CSCPHistoryPlugin* self = new ( ELeave ) CSCPHistoryPlugin();
       
    45 	CleanupStack::PushL( self );
       
    46 	self->ConstructL();
       
    47 	CleanupStack::Pop( self );
       
    48 	
       
    49     Dprint ( ( _L( "( 0x%x ) CSCPHistoryPlugin::NewL()" ), self ) );
       
    50 
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ----------------------------------------------------------------------------
       
    55 // CSCPHistoryPlugin::CSCPHistoryPlugin
       
    56 // Constructor
       
    57 // Status : Approved
       
    58 // ----------------------------------------------------------------------------
       
    59 //
       
    60 CSCPHistoryPlugin::CSCPHistoryPlugin()
       
    61    
       
    62     {
       
    63     Dprint ( ( _L( "CSCPHistoryPlugin::CSCPHistoryPlugin()" ) ) );
       
    64     return;
       
    65     }
       
    66 
       
    67 // ----------------------------------------------------------------------------
       
    68 // CSCPHistoryPlugin::ConstructL
       
    69 // 2nd phase construction
       
    70 // (may leave)
       
    71 // Status : Approved
       
    72 // ----------------------------------------------------------------------------
       
    73 //
       
    74 void CSCPHistoryPlugin::ConstructL()
       
    75     {        
       
    76     Dprint ( ( _L( "CSCPHistoryPlugin::ConstructL()" ) ) );
       
    77     return;
       
    78     }
       
    79     
       
    80 // ----------------------------------------------------------------------------
       
    81 // CSCPHistoryPlugin::HandleEvent
       
    82 // 
       
    83 // 
       
    84 // Status : Approved
       
    85 // ----------------------------------------------------------------------------
       
    86 //    
       
    87 CSCPParamObject* CSCPHistoryPlugin::HandleEvent( TInt aID, CSCPParamObject& aParam )
       
    88 	{				
       
    89 	Dprint ( ( _L( "CSCPHistoryPlugin::HandleEvent()" ) ) );
       
    90 	// Make the ParamObject for success ack, Delete later
       
    91 	CSCPParamObject* retParams = NULL;
       
    92 	
       
    93 	TBool errRaised;
       
    94 	errRaised = EFalse;
       
    95 	
       
    96 	TBool isInvalid = EFalse;
       
    97 	
       
    98 	if ( iFs == NULL )
       
    99 	    {
       
   100 	    return NULL; // Eventhandler not available
       
   101 	    }	
       
   102 	
       
   103 	// Insert the default security code into the history-buffer if not there yet
       
   104     {	        
       
   105     TInt errSCF = SetConfigFile ();
       
   106 	if (errSCF != KErrNone)
       
   107 	    {		
       
   108 		return NULL;
       
   109 		}
       
   110 		
       
   111     TInt historyItemCounter = 0;
       
   112     if ( GetHistoryItemCount( historyItemCounter ) != KErrNone )
       
   113 	    {
       
   114 	    Dprint ( ( _L( "CSCPHistoryPlugin::HandleEvent historyItemCounter = %d" ), historyItemCounter ) );
       
   115 	    // Hash the security code	
       
   116 		TBuf<KSCPPasscodeMaxLength> codeBuf;
       
   117 		TBuf<KSCPMaxHashLength> hashBuf;
       
   118 		
       
   119 		codeBuf.Copy( KSCPDefaultEnchSecCode );
       
   120 		hashBuf.Zero();
       
   121 		
       
   122 		iEventHandler->HashInput( codeBuf, hashBuf );		        
       
   123         
       
   124         CSCPParamObject* historyObject = NULL;
       
   125 	    TRAPD( err, historyObject = CSCPParamObject::NewL() );
       
   126 	    if ( err == KErrNone )
       
   127 	        {
       
   128  		    historyObject->Set( KHistoryCounterParamID, 1 );
       
   129 		    historyObject->Set( KHistoryItemParamBase, hashBuf );
       
   130         
       
   131             TRAP_IGNORE( historyObject->WriteToFileL( iCfgFilenamepath, iFs ) );
       
   132 	        }
       
   133 	    
       
   134 	    delete historyObject;
       
   135 	    }
       
   136     }
       
   137 	
       
   138 	
       
   139 	// check for Case
       
   140     switch ( aID )
       
   141         {
       
   142 
       
   143         case ( KSCPEventValidate ) :
       
   144             {            
       
   145            	// Obtain the paramValue
       
   146            	Dprint ( ( _L( "CSCPHistoryPlugin::KSCPEventValidate" ) ) );
       
   147 			TInt passhistoryParamValue;
       
   148 			passhistoryParamValue = GetHistoryCountParamValue();
       
   149 			Dprint ( ( _L( "CSCPHistoryPlugin::HandleEvent passhistoryParamValue = %d" ), passhistoryParamValue ) );
       
   150 			// if all required bounds are zero, there is nothing to do.
       
   151 			if ( passhistoryParamValue != 0)
       
   152 			    {
       
   153 				// Get the configFile's path.
       
   154 				// If this fails, there is something badly wrong(Private folder is not there)
       
   155 				TInt errSCF = SetConfigFile ();
       
   156 				if (errSCF != KErrNone)
       
   157 				{
       
   158 					errRaised = ETrue;
       
   159 					break; // Break out from Case
       
   160 				}
       
   161 
       
   162 				// Get the historyItemCount, If the err is raised, the file is not there
       
   163 				// This will lead to KSCPEventPasswordChanged event and new history file will
       
   164 				// be created
       
   165 				TInt historyItemCounter;
       
   166 				TInt errHC = GetHistoryItemCount( historyItemCounter );
       
   167 				Dprint ( ( _L( "CSCPHistoryPlugin::HandleEvent historyItemCounter = %d" ), historyItemCounter ) );
       
   168 				if (errHC != KErrNone)
       
   169 				{
       
   170 					errRaised = ETrue;
       
   171 					break; // Break out from Case
       
   172 				}
       
   173 
       
   174 				// continue with the KSCPEventValidate Check
       
   175 
       
   176 				// Get the password from the paramObject
       
   177 				TBuf<KSCPPasscodeMaxLength> seccode;
       
   178 				if ( aParam.Get( KSCPParamPassword, seccode ) != KErrNone )
       
   179 				{
       
   180 					// Nothing to do anymore
       
   181 					Dprint( (_L("CSCPHistoryPlugin::HandleEvent()\
       
   182 					ERROR: KSCPEventValidate/KSCPParamPassword is != KErrNone") ));
       
   183 					errRaised = ETrue;
       
   184 					break; // Break out from Case
       
   185 				}            
       
   186 				
       
   187 				// Hash  the securitycode	
       
   188 				TBuf<KSCPPasscodeMaxLength> securityhash;
       
   189 				iEventHandler->HashInput(seccode,securityhash);
       
   190 			
       
   191 				// get history
       
   192 				CDesCArrayFlat* array = NULL;
       
   193 				TInt errGH = KErrNone;
       
   194 				
       
   195 				array = new CDesCArrayFlat(1);			
       
   196 				if ( array != NULL )
       
   197 				    {
       
   198 				    TRAPD( err2, errGH = GetHistoryArrayL( *array ) );
       
   199 				    if ( err2 != KErrNone )
       
   200 				        {
       
   201 				        errGH = err2;
       
   202 				        }
       
   203 				    }
       
   204 				else
       
   205 				    {
       
   206 				    errGH = KErrNoMemory;
       
   207 				    }
       
   208 			
       
   209 				// If for some reason err is raised, break out
       
   210 				// If the Historyonfig file get deleted on the fly ex
       
   211 				if (errGH != KErrNone)
       
   212 				    {
       
   213 					errRaised = ETrue;
       
   214 					array->Reset();
       
   215 					delete array;
       
   216 					break; // Break out from Case
       
   217 				    }
       
   218 				TInt correction;
       
   219 				correction = 0;
       
   220 
       
   221 				if ( array->Count() >=  passhistoryParamValue )
       
   222         {
       
   223         correction =  array->Count() - passhistoryParamValue;
       
   224         }
       
   225 				// check for match
       
   226 				TBuf<KSCPPasscodeMaxLength> arrayItem;
       
   227 				
       
   228 				// Set the historyobject
       
   229 				for (TInt i= 0 + correction; i < array->Count(); i++)
       
   230 				    {
       
   231 					arrayItem =  array->MdcaPoint(i);
       
   232 					if (arrayItem.Compare(securityhash) == KErrNone)
       
   233 					    {
       
   234 						// Get the filesystem for Resource
       
   235 						// If fail, bail out
       
   236 						TInt errgGR = GetResource();
       
   237 						if (errgGR != KErrNone)
       
   238 						    {
       
   239 							errRaised = ETrue;
       
   240 							break; // Break out from the For
       
   241 						    }	
       
   242 
       
   243 						// Prompt buf, iNote can show only 97 chars,
       
   244 						// without ... markings.
       
   245 						HBufC* hbuf = NULL;					
       
   246 						
       
   247 						if ( passhistoryParamValue == 1 )
       
   248 						    {
       
   249 		                    isInvalid = ETrue;
       
   250 		                    TRAP_IGNORE(
       
   251 		                        hbuf = LoadAndFormatResL( R_SET_SEC_CODE_INFO_PREVIOUS );
       
   252 		                        );
       
   253 						    }
       
   254 						else
       
   255 						    {
       
   256 		                    isInvalid = ETrue;
       
   257 		                    TRAP_IGNORE(
       
   258 		                        hbuf = LoadAndFormatResL( 
       
   259 		                            R_SET_SEC_CODE_INFO_CHECK, 
       
   260 		                            &passhistoryParamValue );
       
   261 		                        );							    
       
   262 						    }														
       
   263 
       
   264                         if ( isInvalid )
       
   265     					    {	    							
       
   266     				    	// Create the result-object to return
       
   267     					    TRAPD( err, retParams  = CSCPParamObject::NewL() );
       
   268                             
       
   269                             if ( err == KErrNone )
       
   270     					        {
       
   271         			            retParams->Set( KSCPParamStatus, KErrSCPInvalidCode );
       
   272     	    		            retParams->Set( KSCPParamAction, KSCPActionShowUI );
       
   273     		    	            retParams->Set( KSCPParamUIMode, KSCPUINote );
       
   274     			                
       
   275     			                if ( hbuf != NULL )
       
   276     			                    {
       
   277     			                    TPtr ptr = hbuf->Des();
       
   278     			                    retParams->Set( KSCPParamPromptText, ptr );
       
   279     			                    delete hbuf;
       
   280     			                    }
       
   281     					        }
       
   282     					    
       
   283     					    break;
       
   284     					    }
       
   285 																															
       
   286 					    } // End of compare IF
       
   287 				    } // End of For
       
   288 				    
       
   289 				// kill the local
       
   290 				array->Reset();
       
   291 				delete array;
       
   292 															
       
   293 			    } // passhistoryParamValue
       
   294 			else
       
   295 			    {
       
   296 				retParams = NULL;
       
   297 			    }
       
   298 
       
   299 			break;
       
   300 			} // end of KSCPEventValidate
       
   301                     
       
   302         // Someone has changed the Seccode and I need to include it to history
       
   303          case ( KSCPEventPasswordChanged ) :
       
   304 			{																
       
   305 			// Get the configFile's path.
       
   306 			Dprint ( ( _L( "CSCPHistoryPlugin::KSCPEventPasswordChanged" ) ) );
       
   307 			TInt errSCF = SetConfigFile ();
       
   308 			if (errSCF != KErrNone)
       
   309 			    {
       
   310 				errRaised = ETrue;
       
   311 				break; // Break out from the case
       
   312 			    }
       
   313 			
       
   314 			// Get the password from the paramObject
       
   315      		TBuf<KSCPPasscodeMaxLength> securitycode;
       
   316             if ( aParam.Get( KSCPParamPassword, securitycode ) != KErrNone )
       
   317                 {
       
   318             	// Nothing to do anymore
       
   319                	Dprint( (_L("CSCPHistoryPlugin::HandleEvent()\
       
   320                	ERROR: KSCPEventPasswordChanged/KSCPParamPassword is  != KErrNone") ));
       
   321             	errRaised = ETrue;
       
   322 				break; // Break out from the Case
       
   323                 }          
       
   324 
       
   325 			// Hash  the securitycode	
       
   326 			TBuf<KSCPPasscodeMaxLength> securityhash;
       
   327 			iEventHandler->HashInput(securitycode,securityhash);
       
   328 
       
   329 			// Get the historyItemCount, If error occures, File is not there yet, Make one
       
   330 			TInt historyItemCounter;
       
   331 			TInt errHC = GetHistoryItemCount( historyItemCounter );
       
   332 			Dprint ( ( _L( "CSCPHistoryPlugin::HandleEvent historyItemCounter = %d" ), historyItemCounter ) );
       
   333 			if (errHC != KErrNone)
       
   334 			    {
       
   335 				// The file does not exist yet (should not happen)
       
   336 				// Make the ParamObject,  Set the New historyData with count of 1
       
   337 				CSCPParamObject* historyObject = NULL;
       
   338 				TRAPD( err, historyObject = CSCPParamObject::NewL() );
       
   339 		 		if ( err == KErrNone )
       
   340 		 		    {
       
   341 			 		historyObject->Set(KHistoryCounterParamID,1);
       
   342     				historyObject->Set(KHistoryItemParamBase,securityhash );
       
   343                     
       
   344                     TRAPD( errWC, historyObject->WriteToFileL( iCfgFilenamepath, iFs ) );
       
   345 					if ( errWC != KErrNone )
       
   346     					{
       
   347 						Dprint( (_L("CSCPHistoryPlugin::HandleEvent(): WARNING:\
       
   348 						failed to write plugin configuration: %d"), errWC ));
       
   349 						errRaised = ETrue;
       
   350 						break; // Break out from the Case
       
   351 	    				}
       
   352 					delete historyObject;
       
   353 		 		    }
       
   354 			    }
       
   355 			// There are passwords avail.
       
   356 			else
       
   357 			    {
       
   358 				// Append the new passwords
       
   359 				TInt err = KErrNone;
       
   360 				TRAPD( err2, err = AppendAndWriteSecurityCodeL( securityhash  ) );
       
   361 				if ( ( err != KErrNone ) || ( err2 != KErrNone ) )
       
   362 				    {
       
   363 					errRaised = ETrue;
       
   364 					break; // Break out from the Case						
       
   365 				    }										
       
   366 			    }    					
       
   367 			break;
       
   368 			} // end of KSCPEventPasswordChanged
       
   369           
       
   370     	case ( KSCPEventConfigurationQuery ):
       
   371             {            
       
   372             Dprint ( ( _L( "CSCPHistoryPlugin::KSCPEventConfigurationQuery" ) ) );
       
   373         	TInt paramID = -1; 
       
   374             // Get the ID from the paramObject      
       
   375             if ( aParam.Get( KSCPParamID, paramID ) != KErrNone )
       
   376                 {
       
   377                 // Nothing to do anymore
       
   378                 break;
       
   379                 }            
       
   380                     
       
   381             // 1011
       
   382             if ( paramID == (RTerminalControl3rdPartySession::EPasscodeHistoryBuffer))
       
   383                 {
       
   384 				// OK, we're interested, check that the value is valid
       
   385 				TRAPD( err, retParams  = CSCPParamObject::NewL() );
       
   386 				if ( err != KErrNone )
       
   387 				    {
       
   388 				    break; // Nothing we can do
       
   389 				    }
       
   390 
       
   391 				// All of our params are TInts
       
   392 				TInt paramValue;
       
   393 				if ( aParam.Get( KSCPParamValue, paramValue ) != KErrNone )
       
   394 				    {
       
   395 					retParams->Set( KSCPParamStatus, KErrGeneral );
       
   396 					break;
       
   397 				    }
       
   398             	            
       
   399 					TInt retStatus = KErrNone;
       
   400 					switch ( paramID )
       
   401 					    {
       
   402 				
       
   403 						case ( RTerminalControl3rdPartySession::EPasscodeHistoryBuffer ):
       
   404 						    {
       
   405 							// Bounds are be be
       
   406 							if ( ( paramValue < KPasscodeHistoryBufferMinValue ) 
       
   407 							|| ( paramValue > KPasscodeHistoryBufferMaxValue ) )                                 
       
   408 							    {
       
   409 								// This is not a valid valuerange
       
   410 								retStatus = KErrArgument;
       
   411 						        }     
       
   412 							           
       
   413 						    break;				
       
   414 						    } // end of case EPasscodeHistoryBuffer
       
   415 					    } // end of switch ( paramID )
       
   416 								
       
   417 			        retParams->Set( KSCPParamStatus, retStatus );
       
   418                     }
       
   419 			    else
       
   420 			        {
       
   421 				    retParams = NULL;
       
   422 			        }
       
   423 			 			 
       
   424 			 break;	            
       
   425 	         } //End of KSCPEventConfigurationQuery Case
       
   426             
       
   427             
       
   428           case ( KSCPEventReset ):
       
   429               {
       
   430               Dprint ( ( _L( "CSCPHistoryPlugin::KSCPEventReset" ) ) );
       
   431               // Reset the configuration for this plugin.
       
   432               TRAP_IGNORE( FlushConfigFileL() );
       
   433               
       
   434               break;
       
   435               }
       
   436                           
       
   437           } // End of  switch ( aID )
       
   438                              
       
   439        // Check if Any errors were raised and handle it
       
   440     if (errRaised) 
       
   441         {
       
   442         if ( retParams != NULL )
       
   443             {
       
   444             delete retParams;
       
   445             }
       
   446         retParams = NULL;
       
   447         }
       
   448        
       
   449     return retParams; 
       
   450     }
       
   451 
       
   452 // ----------------------------------------------------------------------------
       
   453 // CSCPHistoryPlugin::SetEventHandler
       
   454 // SetEventHandler
       
   455 // 
       
   456 // Status : Approved
       
   457 // ----------------------------------------------------------------------------
       
   458 //    
       
   459 void CSCPHistoryPlugin::SetEventHandler( MSCPPluginEventHandler* aHandler )
       
   460 	{
       
   461 	Dprint ( ( _L( "CSCPHistoryPlugin::SetEventHandler()" ) ) );
       
   462 	iEventHandler = aHandler;
       
   463 	
       
   464 	iFs = &(iEventHandler->GetFsSession());
       
   465 	}	
       
   466 
       
   467 // ----------------------------------------------------------------------------
       
   468 // CSCPHistoryPlugin::~CSCPHistoryPlugin
       
   469 // Destructor
       
   470 // Status : Approved
       
   471 // ----------------------------------------------------------------------------
       
   472 //
       
   473 CSCPHistoryPlugin::~CSCPHistoryPlugin()
       
   474     {
       
   475 	Dprint( (_L("CSCPHistoryPlugin::~CSCPHistoryPlugin()") ));
       
   476     
       
   477 	iRf.Close();   
       
   478 	    
       
   479 	return;
       
   480     }
       
   481 
       
   482 
       
   483 // ----------------------------------------------------------------------------
       
   484 // CSCPHistoryPlugin::GetHistoryItemCount
       
   485 // GetHistoryItemCount
       
   486 // Status : Approved
       
   487 // ----------------------------------------------------------------------------
       
   488 //
       
   489 TInt CSCPHistoryPlugin::GetHistoryItemCount( TInt& aHistoryCount )
       
   490     {
       
   491     Dprint ( ( _L( "CSCPHistoryPlugin::GetHistoryItemCount()" ) ) );
       
   492     // Make the ParamObject for success ack, 
       
   493     CSCPParamObject* historyObject = NULL;
       
   494     TRAPD( err, historyObject = CSCPParamObject::NewL() );
       
   495     if ( err != KErrNone )
       
   496         {
       
   497         return err;
       
   498         }
       
   499 
       
   500     TRAP( err, historyObject->ReadFromFileL( iCfgFilenamepath, iFs ) );
       
   501     if (  err != KErrNone  )
       
   502         {
       
   503     	// Something is wrong with the config file
       
   504     	// it is missing, caller will create new file
       
   505     	Dprint( (_L("CSCPHistoryPlugin::SetConfigFile:\
       
   506     	ERROR: Failed to ReadFromFileL: %d"), err ));
       
   507         }
       
   508     else
       
   509         {
       
   510     	historyObject->Get(KHistoryCounterParamID,aHistoryCount);
       
   511         }
       
   512 	Dprint ( ( _L( "CSCPHistoryPlugin::GetHistoryItemCount aHistoryCount = %d" ), aHistoryCount ) );
       
   513     delete historyObject;
       
   514 		             
       
   515 	return err;
       
   516     }
       
   517 
       
   518 
       
   519 // ----------------------------------------------------------------------------
       
   520 // CSCPHistoryPlugin::SetConfigFile
       
   521 // SetConfigFile
       
   522 // Status : Approved
       
   523 // ----------------------------------------------------------------------------
       
   524 //
       
   525 
       
   526 TInt CSCPHistoryPlugin::SetConfigFile()
       
   527     {	
       
   528     Dprint ( ( _L( "CSCPHistoryPlugin::SetConfigFile()" ) ) );
       
   529 	TRAPD( err, iEventHandler->GetStoragePathL( iCfgFilenamepath ) );
       
   530 	if ( err != KErrNone )
       
   531 	    {
       
   532 		Dprint( (_L("CSCPHistoryPlugin::SetConfigFile:\
       
   533 		ERROR: Failed to get storage path: %d"), err ));             
       
   534 		return err;
       
   535 	    }
       
   536 	iCfgFilenamepath.Append( KConfigFile );
       
   537 	return err;
       
   538     }
       
   539 
       
   540 
       
   541 // ----------------------------------------------------------------------------
       
   542 // CSCPHistoryPlugin::AppendSecurityCode
       
   543 // AppendSecurityCode
       
   544 // Status : Approved
       
   545 // ----------------------------------------------------------------------------
       
   546 //
       
   547 
       
   548 TInt CSCPHistoryPlugin::AppendAndWriteSecurityCodeL ( TDes& aSecuritycode )
       
   549     {
       
   550     Dprint ( ( _L( "CSCPHistoryPlugin::AppendAndWriteSecurityCodeL()" ) ) );
       
   551 	TInt err;
       
   552 	err = KErrNone;
       
   553 		
       
   554 	TInt passhistoryParamValue;
       
   555 	passhistoryParamValue = GetHistoryCountParamValue();
       
   556 	Dprint ( ( _L( "CSCPHistoryPlugin::AppendAndWriteSecurityCodeL passhistoryParamValue = %d" ), passhistoryParamValue ) );
       
   557 	if ( passhistoryParamValue == 0 )
       
   558 	    {
       
   559 	    // We must still save the currect password..
       
   560 	    passhistoryParamValue = 1;
       
   561 	    }
       
   562 
       
   563 	// Make the ParamObject
       
   564 	CSCPParamObject* historyObject = CSCPParamObject::NewL();
       
   565 	CleanupStack::PushL( historyObject );
       
   566 	
       
   567 	// get history
       
   568 	CDesCArrayFlat*  array = new (ELeave) CDesCArrayFlat(1);
       
   569 	CleanupStack::PushL( array );
       
   570 	
       
   571 	err = GetHistoryArrayL( *array );
       
   572 	// If for some reason err is raised, break out
       
   573 	if (err != KErrNone)
       
   574 	    {
       
   575 		array->Reset();
       
   576 		CleanupStack::PopAndDestroy( array );
       
   577 		CleanupStack::PopAndDestroy( historyObject );
       
   578 		return err;
       
   579 	    }
       
   580 
       
   581 	TBuf<KSCPPasscodeMaxLength> arrayItem;
       
   582 	
       
   583   	// append the new pass to history to last index.  	
       
   584 	array->AppendL( aSecuritycode );
       
   585 	
       
   586 	TInt correction;
       
   587 	correction = 0;
       
   588 
       
   589 	if ( array->Count() >=  passhistoryParamValue )
       
   590         {
       
   591         correction =  array->Count() - passhistoryParamValue;
       
   592         }    
       
   593 
       
   594   	// Set the historyobject
       
   595 	for ( TInt i = 0 + correction; i <  array->Count(); i++ )
       
   596 		{
       
   597         arrayItem =  array->MdcaPoint(i);
       
   598         historyObject->Set( KHistoryItemParamBase + i - correction, arrayItem );
       
   599         arrayItem.Zero();
       
   600 		}
       
   601 					
       
   602 	// Set the historycount
       
   603 	historyObject->Set(KHistoryCounterParamID, array->Count() - correction );
       
   604 
       
   605 	// Write
       
   606 	TRAP( err, historyObject->WriteToFileL( iCfgFilenamepath, iFs ) );
       
   607 	if (  err != KErrNone  )
       
   608 	    {
       
   609 		Dprint( (_L("CSCPHistoryPlugin::AppendSecurityCode(): WARNING:\
       
   610 		failed to write plugin configuration: %d"), err )); 
       
   611 	    }
       
   612 	
       
   613 	// Kill the local array
       
   614 	array->Reset();
       
   615 	CleanupStack::PopAndDestroy( array );
       
   616 	
       
   617 	CleanupStack::PopAndDestroy( historyObject );
       
   618 	
       
   619 	return err;	
       
   620     }
       
   621 
       
   622 
       
   623 // ----------------------------------------------------------------------------
       
   624 // CSCPHistoryPlugin::GetHistoryArray
       
   625 // Reads the historyconfig file and retrieves the history data
       
   626 // Status : Approved
       
   627 // ----------------------------------------------------------------------------
       
   628 //
       
   629 TInt CSCPHistoryPlugin::GetHistoryArrayL ( CDesCArrayFlat& array )
       
   630     {   
       
   631 	Dprint( (_L("CSCPHistoryPlugin::GetHistoryArray()") ));
       
   632 	
       
   633     TBuf<KSCPPasscodeMaxLength> arrayItem;
       
   634     TInt historyCount;
       
   635 	 
       
   636 	// Make the ParamObject
       
   637 	CSCPParamObject* historyObject = CSCPParamObject::NewL();
       
   638 	CleanupStack::PushL( historyObject );
       
   639 
       
   640 	TRAPD( err, historyObject->ReadFromFileL( iCfgFilenamepath, iFs ) );
       
   641 	if ( err != KErrNone ) 
       
   642 	    {
       
   643 		// Reading from history fails. 
       
   644 		Dprint( (_L("CSCPHistoryPlugin::GetHistoryArray():\
       
   645 		failed to read plugin configuration: %d"), err )); 
       
   646 		CleanupStack::PopAndDestroy( historyObject );
       
   647 		return err;
       
   648 	    }
       
   649 
       
   650 	// Get the historyCount
       
   651 	historyObject->Get(KHistoryCounterParamID,historyCount);
       
   652 	Dprint ( ( _L( "CSCPHistoryPlugin::GetHistoryArrayL historyCount = %d" ), historyCount ) );
       
   653 	// Loop them into array
       
   654 	
       
   655 	for (TInt i = 0 ; i < historyCount ; i ++)
       
   656     	{
       
   657 		historyObject->Get(KHistoryItemParamBase+i,arrayItem);
       
   658 		array.AppendL(arrayItem);
       
   659 		arrayItem.Zero();
       
   660 	    }
       
   661 
       
   662     CleanupStack::PopAndDestroy( historyObject );
       
   663 	return err;	
       
   664     }
       
   665 
       
   666 // ----------------------------------------------------------------------------
       
   667 // CSCPHistoryPlugin::GetHistoryCountParamValue
       
   668 // GetHistoryCountParamValue
       
   669 // Status : Approved
       
   670 // ----------------------------------------------------------------------------
       
   671 //
       
   672 
       
   673 TInt CSCPHistoryPlugin::GetHistoryCountParamValue()
       
   674     {
       
   675 	Dprint( (_L("CSCPHistoryPlugin::GetHistoryCountParamValue()") ));
       
   676 	// Get required params for bounds for EPasscodeHistoryBuffer
       
   677 	CSCPParamObject& config = iEventHandler->GetParameters();	
       
   678 
       
   679 	// These are the dault values for ThisPlugIn's functions
       
   680 	TInt passcodehistorycount;
       
   681 
       
   682 	// Get Values with ID's
       
   683 	if ( config.Get( ( RTerminalControl3rdPartySession::EPasscodeHistoryBuffer), 
       
   684 	                   passcodehistorycount ) !=  KErrNone )
       
   685 	    {
       
   686 	    passcodehistorycount = 0;
       
   687 	    }	
       
   688 	Dprint ( ( _L( "CSCPHistoryPlugin::GetHistoryCountParamValue passcodehistorycount = %d" ), passcodehistorycount ) );
       
   689 	return passcodehistorycount;
       
   690     }
       
   691 
       
   692 // ----------------------------------------------------------------------------
       
   693 // CSCPHistoryPlugin::GetResource
       
   694 // GetResource
       
   695 // Status : Approved
       
   696 // ----------------------------------------------------------------------------
       
   697 //
       
   698 
       
   699 TInt CSCPHistoryPlugin::GetResource()
       
   700     {
       
   701  	Dprint( (_L("CSCPSpecificStringsPlugin::GetResource()") ));
       
   702 	// The resource has to be loaded manually since it is not an application.
       
   703           	
       
   704 	TFileName resourceFile;
       
   705 	resourceFile.Append( KDriveZ );
       
   706 	resourceFile.Append( SCPHistoryPluginSrcFile );
       
   707 	BaflUtils::NearestLanguageFile( *iFs, resourceFile );
       
   708 	TRAPD( err, iRf.OpenL( *iFs, resourceFile ) );
       
   709 
       
   710 	if ( err == KErrNone )
       
   711 	    {
       
   712 		TRAP( err, iRf.ConfirmSignatureL() );
       
   713     	}          
       
   714     
       
   715     return err;       
       
   716     }
       
   717 
       
   718 
       
   719 // ----------------------------------------------------------------------------
       
   720 // CSCPHistoryPlugin::LoadResourceLC
       
   721 // GetResource
       
   722 // Status : Approved
       
   723 // ----------------------------------------------------------------------------
       
   724 //
       
   725 HBufC16* CSCPHistoryPlugin::LoadResourceLC ( TInt aResId )
       
   726     {
       
   727 	Dprint( (_L("CSCPHistoryPlugin::LoadResourceLC()") ));
       
   728 
       
   729 	// load the actual resource
       
   730     HBufC8* readBuffer = iRf.AllocReadLC( aResId );
       
   731     // as we are expecting HBufC16...
       
   732     const TPtrC16 ptrReadBuffer( (TText16*) readBuffer->Ptr(),
       
   733                                  ( readBuffer->Length() + 1 ) >> 1 );
       
   734                                  
       
   735     HBufC16* textBuffer=HBufC16::NewL( ptrReadBuffer.Length() );    
       
   736     *textBuffer=ptrReadBuffer;
       
   737     CleanupStack::PopAndDestroy( readBuffer ); // readBuffer
       
   738     CleanupStack::PushL( textBuffer );
       
   739   	return textBuffer;
       
   740     }
       
   741 
       
   742 
       
   743 // ----------------------------------------------------------------------------
       
   744 // CSCPHistoryPlugin::FlushConfigFile
       
   745 // Remove all the other passwords from the file, except the last one (current)
       
   746 // 
       
   747 // Status : Approved
       
   748 // ----------------------------------------------------------------------------
       
   749 //
       
   750 TInt CSCPHistoryPlugin::FlushConfigFileL()
       
   751     {			
       
   752     Dprint ( ( _L( "CSCPHistoryPlugin::FlushConfigFileL()" ) ) );
       
   753 	// Make the ParamObject
       
   754 	CSCPParamObject* historyObject = CSCPParamObject::NewL();
       
   755 	CleanupStack::PushL( historyObject );
       
   756 	
       
   757 	// get history
       
   758 	CDesCArrayFlat*  array = new (ELeave) CDesCArrayFlat(1);
       
   759 	CleanupStack::PushL( array );
       
   760 	
       
   761 	TInt err = GetHistoryArrayL( *array );
       
   762 	// If for some reason err is raised, break out
       
   763 	if (err != KErrNone)
       
   764 	    {
       
   765 		array->Reset();
       
   766 		CleanupStack::PopAndDestroy( array );
       
   767 		CleanupStack::PopAndDestroy( historyObject );
       
   768 		return err;
       
   769 	    }
       
   770 	
       
   771 	// Set the parameters
       
   772 	TBuf<KSCPPasscodeMaxLength> arrayItem = array->MdcaPoint( array->Count() - 1 );
       
   773 	
       
   774 	historyObject->Set( KHistoryCounterParamID, 1 );			
       
   775 	historyObject->Set( KHistoryItemParamBase, arrayItem );
       
   776 	
       
   777 	// Write
       
   778 	TRAP( err, historyObject->WriteToFileL( iCfgFilenamepath, iFs ) );
       
   779 	if (  err != KErrNone  )
       
   780 	    {
       
   781 		Dprint( (_L("CSCPHistoryPlugin::AppendSecurityCode(): WARNING:\
       
   782 		failed to write plugin configuration: %d"), err )); 
       
   783 	    }
       
   784 	
       
   785 	// Kill the local array
       
   786 	array->Reset();
       
   787 	CleanupStack::PopAndDestroy( array );
       
   788 	
       
   789 	CleanupStack::PopAndDestroy( historyObject );
       
   790 	
       
   791 	return err;			
       
   792     }
       
   793 
       
   794 
       
   795 // ----------------------------------------------------------------------------
       
   796 // CSCPHistoryPlugin::LoadAndFormatResL
       
   797 // Load the given resouce, and format the string according to the TInt parameters
       
   798 // if given.
       
   799 // 
       
   800 // Status : Approved
       
   801 // ----------------------------------------------------------------------------
       
   802 //
       
   803 HBufC* CSCPHistoryPlugin::LoadAndFormatResL( TInt aResId, TInt* aParam1, TInt* aParam2 )
       
   804     {
       
   805     Dprint ( ( _L( "CSCPHistoryPlugin::LoadAndFormatResL()" ) ) );
       
   806     HBufC16* resource = NULL;
       
   807     HBufC* hbuf = NULL;
       
   808     
       
   809     resource = LoadResourceLC( aResId );
       
   810     FormatResourceString (*resource);
       
   811     TInt allocLen = 0;
       
   812     if ( aParam1 != NULL )
       
   813         {
       
   814         allocLen += KSCPMaxIntLength;
       
   815         }
       
   816     if ( aParam2 != NULL )
       
   817         {
       
   818         allocLen += KSCPMaxIntLength;
       
   819         }
       
   820                 
       
   821 	hbuf = HBufC::NewL( resource->Length() + allocLen );
       
   822 	
       
   823 	if ( ( aParam1 == NULL ) && ( aParam2 == NULL ) )
       
   824 	    {
       
   825 	    hbuf->Des().Copy( resource->Des() );
       
   826 	    }
       
   827 	else
       
   828 	    {
       
   829 	    if ( aParam1 == NULL )
       
   830 	        {
       
   831 	        hbuf->Des().Format( resource->Des(), *aParam2 );
       
   832 	        }
       
   833 	    else if ( aParam2 == NULL )
       
   834 	        {
       
   835 	        hbuf->Des().Format(resource->Des(), *aParam1 );
       
   836 	        }
       
   837 	    else
       
   838 	        {
       
   839 	        hbuf->Des().Format(resource->Des(), *aParam1, *aParam2 );
       
   840 	        }	    
       
   841 	    }
       
   842 								    
       
   843 	CleanupStack::PopAndDestroy( resource );
       
   844 	return hbuf;
       
   845     }
       
   846 
       
   847 // ----------------------------------------------------------------------------
       
   848 // CSCPHistoryPlugin::FormatResourceString
       
   849 // The buffer that is passed is formatted to have only %i as a format specifier instead of %N or %0N etc.
       
   850 // 
       
   851 // Status : Approved
       
   852 // ----------------------------------------------------------------------------
       
   853 //  
       
   854 void CSCPHistoryPlugin::FormatResourceString(HBufC16 &aResStr)
       
   855 {
       
   856 		TInt pos = 0;
       
   857 		TInt flag = 0;
       
   858         TPtr16 bufPtr = aResStr.Des();
       
   859         _LIT (mess1, "%N");
       
   860         _LIT (mess2, "%i");
       
   861         _LIT (mess3, "%0N");
       
   862         _LIT (mess4, "%1N");
       
   863                               
       
   864         while ((pos = bufPtr.Find(mess1)) !=KErrNotFound)
       
   865         {
       
   866               bufPtr.Replace(pos,2,mess2); 
       
   867               flag = 1;
       
   868               break;                    
       
   869         }
       
   870                
       
   871         if(flag == 0)
       
   872         {
       
   873               while ((pos = bufPtr.Find(mess3)) != KErrNotFound)
       
   874               {
       
   875               		bufPtr.Replace(pos,3,mess2);
       
   876               }
       
   877                		
       
   878               while ((pos = bufPtr.Find(mess4)) != KErrNotFound)
       
   879               {
       
   880                 	bufPtr.Replace(pos,3,mess2);
       
   881               }
       
   882         }	
       
   883 }
       
   884   
       
   885 // End of File