locationcentre/lcserver/src/lcregapporder.cpp
branchRCL_3
changeset 16 4721bd00d3da
parent 14 3a25f69541ff
child 21 e15b7f06eba6
equal deleted inserted replaced
14:3a25f69541ff 16:4721bd00d3da
     1 /*
       
     2 * Copyright (c) 2007 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:  Location Centre Server object.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDES
       
    20 #include<s32mem.h>
       
    21 
       
    22 // USER INCLUDES
       
    23 #include "lcregapporder.h"
       
    24 #include "lcprivatecrkeys.h"
       
    25 #include "lcregappstore.h"
       
    26 
       
    27 // CONSTANT DEFINTIONS
       
    28 const TInt KLcNoofDigitsInInteger = 4;
       
    29 const TInt KLcMaxBufLength = 1024;
       
    30 
       
    31 // ----- Member funtions for CLcRegAppOrder ---------------------------------
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // CLcRegAppOrder::CLcRegAppOrder
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CLcRegAppOrder::CLcRegAppOrder( CLcRegAppStore& aLcRegAppStore )
       
    38 	:iLcRegAppStore( &aLcRegAppStore )
       
    39     {
       
    40     // C++ Default constructor. No allocations or functions which can Leave
       
    41     // should be called from here.Initiallize all the variable here
       
    42     }
       
    43          
       
    44 // ---------------------------------------------------------------------------
       
    45 // CLcRegAppOrder::~CLcRegAppOrder
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CLcRegAppOrder::~CLcRegAppOrder()
       
    49     {
       
    50     // C++ Destructor. Free all resources associated with this class.
       
    51 	delete iRepository;
       
    52 	iRepository = NULL;	
       
    53 	// clear the top key value.
       
    54 	iTopArray.ResetAndDestroy();
       
    55 	iTopArray.Close();
       
    56 	// clear the bottom key value
       
    57 	iBottomArray.ResetAndDestroy();
       
    58 	iBottomArray.Close();
       
    59 	// clear middle uuid array
       
    60 	iMiddleArray.ResetAndDestroy();
       
    61 	iMiddleArray.Close();
       
    62     }
       
    63         
       
    64 // ---------------------------------------------------------------------------
       
    65 // CLcRegAppOrder* CLcRegAppOrder::NewL
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CLcRegAppOrder* CLcRegAppOrder::NewL( CLcRegAppStore& aLcRegAppStore )
       
    69     {
       
    70     // Symbian Two phased constructor. Leaves the object on the Clean-up
       
    71     // stack.
       
    72     CLcRegAppOrder* self = new ( ELeave )CLcRegAppOrder( aLcRegAppStore );
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     CleanupStack::Pop( self );
       
    76     return self;         
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // void CLcRegAppOrder::ConstructL
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 void CLcRegAppOrder::ConstructL()
       
    84     {
       
    85 	// Create the Central repository object for manipulating Avkon Central
       
    86 	// repository for application order key
       
    87 	iRepository = CRepository::NewL( KCRUidLocationCentre );
       
    88     HBufC* buffer = HBufC::NewLC( NCentralRepositoryConstants::KMaxUnicodeStringLength );
       
    89     TPtr bufferPtr( buffer->Des() );
       
    90     // get the top order services
       
    91     User::LeaveIfError( iRepository->Get( ELcTop, bufferPtr ) );    
       
    92 	ParseCenRepKeyL( iTopArray, bufferPtr );
       
    93 	// get the middle order services
       
    94 	User::LeaveIfError( iRepository->Get( ELcBottom, bufferPtr ) );
       
    95 	ParseCenRepKeyL( iBottomArray, bufferPtr );
       
    96 	// get the bottom order services if any
       
    97 	TBool isMidleKeySet = EFalse;
       
    98 	User::LeaveIfError( iRepository->Get( ELcMiddle, isMidleKeySet ) );
       
    99 	
       
   100 	if( isMidleKeySet )
       
   101 		{
       
   102         // Allocate the id Array buffer    
       
   103         CBufFlat* idArrayBuf = CBufFlat::NewL( KLcMaxBufLength );
       
   104         CleanupStack::PushL( idArrayBuf );
       
   105         idArrayBuf->ResizeL( KLcMaxBufLength );
       
   106         
       
   107         TPtr8   idArrayPtr( idArrayBuf->Ptr(0));
       
   108         
       
   109         iLcRegAppStore->GetUuidValueL( idArrayPtr );        
       
   110         
       
   111         if( idArrayPtr.Length() > 0 )
       
   112 	        {
       
   113 	        // Create a Read buffer stream to read the App Ids    
       
   114 	        RBufReadStream  appReadStream( *idArrayBuf, 0 );
       
   115 	        CleanupClosePushL( appReadStream );
       
   116 	        
       
   117 	        iMiddleArray.ResetAndDestroy();  
       
   118 	                        
       
   119 	        // Internalize the structure to obtain the Actual list of Ids    
       
   120 	        iMiddleArray.InternalizeL( appReadStream );  
       
   121 	        CleanupStack::PopAndDestroy(); // appReadStream
       
   122 	        }
       
   123         CleanupStack::PopAndDestroy( idArrayBuf );
       
   124 		}
       
   125 	else
       
   126 		{
       
   127 		// Deep RFS handling
       
   128 	    iLcRegAppStore->AddDataToTableL( KNullDesC8 );	
       
   129 		}
       
   130 		
       
   131     CleanupStack::PopAndDestroy( buffer );   
       
   132     }  
       
   133 
       
   134 // ---------------------------------------------------------------------------
       
   135 // void CLcRegAppOrder::ParseCenRepKey
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138 void CLcRegAppOrder::ParseCenRepKeyL( RPointerArray< HBufC >&	aArray,
       
   139     				     			 const TDesC&				aKey )
       
   140 	{
       
   141 	// If the length of the buffer is less than 4 digits, then terminate
       
   142 	// the function.
       
   143 	if( aKey.Length() < KLcNoofDigitsInInteger )
       
   144 		{
       
   145 		return;
       
   146 		}
       
   147 	TUint count = ParseInteger( aKey );
       
   148 	TInt currentlength = KLcNoofDigitsInInteger;
       
   149 		
       
   150 	for ( TInt i = 0; i < count; i ++ )
       
   151 		{
       
   152 		// If the length of the buffer is less than 4 digits, then terminate
       
   153 		// the function.		
       
   154 		if( aKey.Right( aKey.Length() - currentlength ).Length() < KLcNoofDigitsInInteger )
       
   155 			{
       
   156 			return;
       
   157 			}
       
   158 			
       
   159 		TUint32 len = ParseInteger( aKey.Right( aKey.Length() - currentlength ));
       
   160 		currentlength += KLcNoofDigitsInInteger;
       
   161 		
       
   162 		TPtrC buffer( aKey.Right( aKey.Length() - currentlength ));
       
   163 		
       
   164 		// If the length of the buffer is less than the length obtained from the
       
   165 		// previous operation then terminate the function.
       
   166 		if( buffer.Length() < len )
       
   167 			{
       
   168 			return;
       
   169 			}
       
   170 		HBufC* identifier = HBufC::NewLC( currentlength );
       
   171 		identifier->Des().Copy( buffer.Ptr(), len );
       
   172 		
       
   173 		TInt error = aArray.Append( identifier );
       
   174 		if( error )
       
   175 			{
       
   176 			CleanupStack::PopAndDestroy( identifier );
       
   177 			}
       
   178 		else
       
   179 			{
       
   180 			CleanupStack::Pop( identifier );
       
   181 			}
       
   182 		currentlength += len;			
       
   183 		}
       
   184 	}
       
   185     				     
       
   186 // ---------------------------------------------------------------------------
       
   187 // void CLcRegAppOrder::ParseInteger
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 TUint32	CLcRegAppOrder::ParseInteger( const TDesC&	aBuffer )
       
   191 	{
       
   192 	TPtrC buffer( aBuffer.Ptr(), KLcNoofDigitsInInteger );
       
   193 		
       
   194 	TBuf8< KLcNoofDigitsInInteger > integerBuf;
       
   195 	integerBuf.Copy( buffer );
       
   196 	
       
   197 	TUint8*	currentPtr = const_cast< TUint8 *>( integerBuf.Ptr());
       
   198 	TUint32 integer = 0;
       
   199 	for( TInt i = 0; i < KLcNoofDigitsInInteger; i++ )
       
   200 		{
       
   201 		integer += ( integer * 10 ) + ( currentPtr[i] - '0' );
       
   202 		}
       
   203 	return integer;
       
   204 	}
       
   205 	
       
   206 // ---------------------------------------------------------------------------
       
   207 // void CLcRegAppOrder::GetRegisteredAppOrder
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 TInt CLcRegAppOrder::GetRegisteredAppOrder(const TDesC& aUuid,
       
   211 												 TInt& aRowPos )
       
   212     {
       
   213     // search the identifier in the top array.
       
   214     for(TInt i = 0; i <iTopArray.Count(); i++  )
       
   215 	    {
       
   216 	    HBufC* identifier = iTopArray[i];
       
   217 	    if( !identifier->Des().Compare( aUuid ) )
       
   218 		    {
       
   219 			aRowPos = i;
       
   220 			return KErrNone;	    			    	
       
   221 		    }
       
   222 	    }
       
   223 	    
       
   224     // search the identifier in the middle array.
       
   225     for(TInt i = 0; i <iMiddleArray.Count(); i++  )
       
   226 	    {
       
   227 	    HBufC* identifier = iMiddleArray[i];
       
   228 	    if( !identifier->Des().Compare( aUuid ) )
       
   229 		    {
       
   230 			aRowPos = iTopArray.Count() + i;
       
   231 			return KErrNone;	    			    	
       
   232 		    }
       
   233 	    }
       
   234 	    	
       
   235     // search the identifier in the top array.
       
   236     for(TInt i = 0; i <iBottomArray.Count(); i++  )
       
   237 	    {
       
   238 	    HBufC* identifier = iBottomArray[i];
       
   239 	    if( !identifier->Des().Compare( aUuid ) )
       
   240 		    {
       
   241 			aRowPos = iTopArray.Count() + iMiddleArray.Count() + i;
       
   242 			return KErrNone;	    			    	
       
   243 		    }
       
   244 	    }
       
   245 	return KErrNotFound;	    
       
   246     }  
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // void CLcRegAppOrder::RemoveArray
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 void CLcRegAppOrder::RemoveArrayL(const TDesC& aUuid )
       
   253     {
       
   254     // search the identifier in the middle array.
       
   255     for(TInt i = 0; i <iMiddleArray.Count(); i++  )
       
   256 	    {
       
   257 	    HBufC* identifier = iMiddleArray[i];
       
   258 	    if( !identifier->Des().Compare( aUuid ) )
       
   259 		    {
       
   260 			iMiddleArray.Remove(i);
       
   261 			// now delete the identifier
       
   262 			delete identifier;
       
   263 			identifier = NULL;
       
   264 			break;	    			    	
       
   265 		    }
       
   266 	    }
       
   267 	    
       
   268     // Create the buffer for packing the Application Information
       
   269     // structure and pack the contents into this buffer
       
   270     CBufFlat* buffer = CBufFlat::NewL( iMiddleArray.BufferLength());
       
   271     CleanupStack::PushL( buffer );
       
   272     
       
   273     RBufWriteStream writeStream( *buffer, 0 );
       
   274     CleanupClosePushL( writeStream );                
       
   275     iMiddleArray.ExternalizeL( writeStream );
       
   276     CleanupStack::PopAndDestroy(); // writeStream    	
       
   277     iLcRegAppStore->AddDataToTableL( buffer->Ptr(0));
       
   278 	CleanupStack::PopAndDestroy( buffer );
       
   279 	// Now get the id index.
       
   280 	if( iMiddleArray.Count() == 0 )
       
   281 		{
       
   282 		User::LeaveIfError( iRepository->Set( ELcMiddle, 0 ));			
       
   283 		}
       
   284     }  
       
   285 
       
   286 // ---------------------------------------------------------------------------
       
   287 // void CLcRegAppOrder::AppendArray
       
   288 // ---------------------------------------------------------------------------
       
   289 //
       
   290 TInt CLcRegAppOrder::AppendArrayL(const TDesC& aUuid )
       
   291     {
       
   292     HBufC* identifier = HBufC::NewL( aUuid.Length() );
       
   293     identifier->Des().Copy( aUuid );	
       
   294 	User::LeaveIfError( iMiddleArray.Append( identifier ));
       
   295     // Create the buffer for packing the Application Information
       
   296     // structure and pack the contents into this buffer
       
   297     CBufFlat* buffer = CBufFlat::NewL( iMiddleArray.BufferLength());
       
   298     CleanupStack::PushL( buffer );
       
   299     
       
   300     RBufWriteStream writeStream( *buffer, 0 );
       
   301     CleanupClosePushL( writeStream );                
       
   302     iMiddleArray.ExternalizeL( writeStream );
       
   303     CleanupStack::PopAndDestroy(); // writeStream
       
   304    	
       
   305 	iLcRegAppStore->AddDataToTableL( buffer->Ptr(0));
       
   306 	
       
   307 	CleanupStack::PopAndDestroy( buffer );
       
   308 
       
   309 	TInt index;
       
   310 	if ( GetRegisteredAppOrder(aUuid, index ) == KErrNotFound )
       
   311 		{
       
   312 		User::Leave( KErrNotFound );
       
   313 		}
       
   314 	User::LeaveIfError( iRepository->Set( ELcMiddle, 1 ));	
       
   315 	return index;			
       
   316     }
       
   317     
       
   318 //End of file
       
   319 
       
   320