phonebookui/Phonebook2/xSPExtensionManager/src/CxSPContactManager.cpp
changeset 0 e686773b3f54
child 11 2828b4d142c0
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2006 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 *       Contact manager.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <s32file.h>
       
    22 #include <s32mem.h>
       
    23 #include <CPbk2ViewState.h>
       
    24 #include <Pbk2ViewId.hrh>
       
    25 #include "CxSPContactManager.h"
       
    26 #include "MxSPFactory.h"
       
    27 #include <MVPbkContactLink.h>
       
    28 #include <MVPbkStreamable.h>
       
    29 #include <CVPbkContactManager.h>
       
    30 #include <CVPbkContactLinkArray.h>
       
    31 
       
    32 // CONSTANTS
       
    33 namespace
       
    34 	{
       
    35 	_LIT( KExtContactMapFile, "xSPcontactMap.dat" );
       
    36 	const TInt KExpandSize = 4; // needed data buffer size
       
    37 	}
       
    38 
       
    39 // ==================== MEMBER FUNCTIONS ====================
       
    40 
       
    41 CxSPPbkContactMap::CxSPPbkContactMap()
       
    42 	{	
       
    43 	}
       
    44         
       
    45 CxSPPbkContactMap::~CxSPPbkContactMap()
       
    46 	{
       
    47 	delete iPbkContactLink;
       
    48 	}
       
    49 
       
    50 void CxSPPbkContactMap::ExternalizeL( RWriteStream& aStream ) const
       
    51 	{
       
    52 	aStream.WriteUint32L( iId );
       
    53 	aStream.WriteInt32L( ixSPContactId );
       
    54 	HBufC8* buffer = CxSPContactManager::LinkToBufferL( iPbkContactLink );
       
    55 	CleanupStack::PushL( buffer );
       
    56 	aStream.WriteInt32L( buffer->Length() );
       
    57 	aStream.WriteL( *buffer, buffer->Length() );
       
    58 	CleanupStack::PopAndDestroy(); // buffer		
       
    59 	}
       
    60 
       
    61 void CxSPPbkContactMap::InternalizeL( RReadStream& aStream,
       
    62 										const MVPbkContactStoreList& aContactStores )
       
    63 	{
       
    64 	iId = aStream.ReadUint32L();
       
    65 	ixSPContactId = aStream.ReadInt32L();
       
    66 	TInt length = aStream.ReadInt32L();
       
    67 	HBufC8* bufLink = HBufC8::NewLC( length );
       
    68 	TPtr8 ptr = bufLink->Des();
       
    69 	aStream.ReadL( ptr, length );		
       
    70 	iPbkContactLink = CxSPContactManager::BufferToLinkL( *bufLink, aContactStores );	
       
    71 	CleanupStack::PopAndDestroy(); // bufLink	
       
    72 	}	
       
    73 
       
    74 CxSPContactManager::CxSPContactManager()
       
    75     {
       
    76     }
       
    77     
       
    78 void CxSPContactManager::ConstructL( CArrayPtrFlat<MxSPFactory>& aFactories,
       
    79 										CVPbkContactManager& aVPbkContactManager )
       
    80     {
       
    81     MVPbkContactStoreList& contactStoreList = aVPbkContactManager.ContactStoresL();    
       
    82     RestoreArrayL( KExtContactMapFile, aFactories, contactStoreList );	
       
    83 	// Register this instance as the contact manager of all xSP factories
       
    84 	for( TInt i = 0; i < aFactories.Count(); i++ )
       
    85 		{
       
    86 		aFactories[i]->RegisterContactManager( *this );
       
    87 		}
       
    88     } 
       
    89          
       
    90 CxSPContactManager::~CxSPContactManager()
       
    91     {
       
    92     iContactMap.ResetAndDestroy(); 
       
    93     iContactMap.Close();
       
    94     delete iState;
       
    95     }
       
    96 
       
    97 CxSPContactManager* CxSPContactManager::NewL( CArrayPtrFlat<MxSPFactory>& aFactories,
       
    98 												CVPbkContactManager& aVPbkContactManager )
       
    99     {
       
   100     CxSPContactManager* self = new (ELeave) CxSPContactManager;
       
   101     CleanupStack::PushL(self);
       
   102     self->ConstructL( aFactories, aVPbkContactManager );
       
   103     CleanupStack::Pop(self);
       
   104     return self;
       
   105     }
       
   106     
       
   107 void CxSPContactManager::MapContactL( TUint32 aId, const MVPbkContactLink& aPbkContactLink,
       
   108         								TInt32 axSPContactId )
       
   109 	{
       
   110 	TInt index = FindMap( aId, aPbkContactLink, axSPContactId );
       
   111 	if( index == KErrNotFound )
       
   112 		{
       
   113 		CxSPPbkContactMap* map = new (ELeave) CxSPPbkContactMap;
       
   114 		CleanupStack::PushL( map );		
       
   115 		map->iId = aId;
       
   116 		map->ixSPContactId = axSPContactId;
       
   117 		map->iPbkContactLink = aPbkContactLink.CloneLC();
       
   118 		CleanupStack::Pop(); // map->iPbkContactLink
       
   119 		User::LeaveIfError( iContactMap.Append( map ) );		
       
   120 		CleanupStack::Pop(); // map		
       
   121 		SaveArrayL( KExtContactMapFile );
       
   122 		}
       
   123 	}
       
   124 	
       
   125 void CxSPContactManager::UnmapContactL( TUint32 aId, const MVPbkContactLink& aPbkContactLink,
       
   126         								TInt32 axSPContactId )
       
   127 	{
       
   128 	TInt index = FindMap( aId, aPbkContactLink, axSPContactId );
       
   129 	if( index != KErrNotFound )
       
   130 		{
       
   131 		CxSPPbkContactMap* map = iContactMap[index];
       
   132 		delete map;
       
   133 		iContactMap.Remove( index );		
       
   134 		SaveArrayL( KExtContactMapFile );
       
   135 		}
       
   136 	}
       
   137 		
       
   138 void CxSPContactManager::GetMappedPbkContactsL( TUint32 aId, TInt32 axSPContactId,
       
   139         							RPointerArray<MVPbkContactLink>& aPbkContactLinks ) const
       
   140 	{
       
   141 	TInt count = iContactMap.Count();
       
   142 	for( TInt i = 0; i < count; i++ )
       
   143 		{
       
   144 		const CxSPPbkContactMap* map = iContactMap[i];
       
   145 		if( map->iId == aId && map->ixSPContactId == axSPContactId && map->iPbkContactLink )
       
   146 			{
       
   147 			User::LeaveIfError( aPbkContactLinks.Append( map->iPbkContactLink ) );
       
   148 			}
       
   149 		}
       
   150 	}		
       
   151 
       
   152 void CxSPContactManager::GetMappedxSPContactsL( TUint32 aId, 
       
   153 												const MVPbkContactLink& aPbkContactLink,
       
   154         										RArray<TInt32>& axSPContactIds ) const
       
   155 	{
       
   156 	const TInt count = iContactMap.Count();
       
   157 	for( TInt i = 0; i < count; i++ )
       
   158 		{
       
   159 		const CxSPPbkContactMap* map = iContactMap[i];
       
   160 		if( map->iId == aId && map->iPbkContactLink )
       
   161 			{
       
   162 			if( map->iPbkContactLink->IsSame( aPbkContactLink ) )
       
   163 				{
       
   164 				User::LeaveIfError( axSPContactIds.Append( map->ixSPContactId ) );
       
   165 				}
       
   166 			}			
       
   167 		}
       
   168 	}        										
       
   169 	
       
   170 void CxSPContactManager::ViewActivatedL( const TVwsViewId& aPrevViewId,
       
   171                          				TUid aCustomMessageId,
       
   172                          				const TDesC8& aCustomMessage )
       
   173 	{
       
   174 	if( aCustomMessageId == CPbk2ViewState::Uid() && 
       
   175     						aPrevViewId.iViewUid.iUid == EPbk2NamesListViewId )
       
   176 		{
       
   177 		delete iState;
       
   178 		iState = NULL;
       
   179 		iState = CPbk2ViewState::NewL( aCustomMessage );
       
   180 		}								
       
   181 	}
       
   182 			
       
   183 const CPbk2ViewState* CxSPContactManager::NamesListState() const
       
   184 	{
       
   185 	return iState;
       
   186 	}		  	   
       
   187  
       
   188 TInt CxSPContactManager::CreatePrivateFolder( RFs& aSession, 
       
   189 											  TDes& aPath, 
       
   190 											  TDriveNumber aDrive ) const
       
   191 	{
       
   192 	_LIT( KColon, ":" );
       
   193 	TChar drive;
       
   194 	TInt err = aSession.DriveToChar( aDrive, drive );							
       
   195 	if( !err )
       
   196 		{
       
   197 		err = aSession.CreatePrivatePath( aDrive );
       
   198 		if( !err )
       
   199 			{
       
   200 			TFileName path;			
       
   201 			aSession.PrivatePath( path );
       
   202 			aPath.Zero();
       
   203 			aPath.Append( drive );
       
   204 			aPath.Append( KColon );
       
   205 			aPath.Append( path );														
       
   206 			}		
       
   207 		}	
       
   208 	return err;
       
   209 	} 
       
   210  
       
   211 TInt CxSPContactManager::FindMap( TUint32 aId, 
       
   212 									const MVPbkContactLink& aPbkContactLink, 
       
   213 									TInt32 axSPContactId ) const
       
   214 	{
       
   215 	TInt index( KErrNotFound );
       
   216 	TInt count = iContactMap.Count();
       
   217 	for( TInt i = 0; i < count && index == KErrNotFound; i++ )
       
   218 		{
       
   219 		const CxSPPbkContactMap* map = iContactMap[i];
       
   220 		if( map->iId == aId && map->ixSPContactId == axSPContactId && map->iPbkContactLink )
       
   221 			{
       
   222 			if( map->iPbkContactLink->IsSame( aPbkContactLink ) )
       
   223 				{
       
   224 				index = i;
       
   225 				}
       
   226 			}			
       
   227 		}
       
   228 	return index;
       
   229 	}
       
   230 	
       
   231 void CxSPContactManager::SaveArrayL( const TDesC& aFileName )
       
   232 	{
       
   233 	RFs fs; // CSI: 76 #
       
   234 	User::LeaveIfError( fs.Connect() );
       
   235 	CleanupClosePushL( fs );
       
   236 	TFileName orderFile;
       
   237 	User::LeaveIfError( CreatePrivateFolder( fs, orderFile, EDriveC ) );
       
   238 	orderFile.Append( aFileName );
       
   239 	RFileWriteStream writeStream;
       
   240 	TInt err = writeStream.Replace( fs, orderFile, EFileWrite );
       
   241 	if( !err )
       
   242 		{
       
   243 		CleanupClosePushL( writeStream );		
       
   244 		TUint32 count = iContactMap.Count();
       
   245 		writeStream.WriteUint32L( count );
       
   246 		for( TUint32 i = 0; i < count; i++ )
       
   247 			{
       
   248 			writeStream << *iContactMap[i];
       
   249 			}			
       
   250 		CleanupStack::PopAndDestroy(); // writeStream
       
   251 		}
       
   252 	CleanupStack::PopAndDestroy(); // fs
       
   253 	}
       
   254 	
       
   255 void CxSPContactManager::RestoreArrayL( const TDesC& aFileName,
       
   256 										CArrayPtrFlat<MxSPFactory>& aFactories,
       
   257 										MVPbkContactStoreList& aContactStoreList )
       
   258 	{
       
   259 	// read the array from file
       
   260     RFs fs; // CSI: 76 #
       
   261 	User::LeaveIfError( fs.Connect() );
       
   262 	CleanupClosePushL( fs );	
       
   263 	TFileName orderFile;
       
   264 	User::LeaveIfError( CreatePrivateFolder( fs, orderFile, EDriveC ) );
       
   265 	orderFile.Append( aFileName );
       
   266 	RFileReadStream readStream;
       
   267 	TInt err = readStream.Open( fs, orderFile, EFileRead );
       
   268 	if( !err )
       
   269 		{
       
   270 		CleanupClosePushL( readStream );
       
   271 		TUint32 count = readStream.ReadUint32L();
       
   272 		for( TUint32 i = 0; i < count; i++ )
       
   273 			{
       
   274 			CxSPPbkContactMap* map = new (ELeave) CxSPPbkContactMap;
       
   275 			CleanupStack::PushL( map );
       
   276 			map->InternalizeL( readStream, aContactStoreList );
       
   277 			
       
   278 			// only use mappings from the present xSP extensions
       
   279 			// removed xSP extension mappings are not used
       
   280 			TBool found( EFalse );
       
   281 			for( TInt j = 0; j < aFactories.Count() && !found; j++ )
       
   282 				{
       
   283 				if( map->iId == aFactories[j]->Id() )
       
   284 					{
       
   285 					found = ETrue;
       
   286 					}
       
   287 				}			
       
   288 			if( found )
       
   289 				{
       
   290 				User::LeaveIfError( iContactMap.Append( map ) );
       
   291 				CleanupStack::Pop(); // map	
       
   292 				}
       
   293 			else
       
   294 				{
       
   295 				CleanupStack::PopAndDestroy(); // map
       
   296 				}			
       
   297 			}
       
   298 		CleanupStack::PopAndDestroy(); // readStream
       
   299 		}		
       
   300 	CleanupStack::PopAndDestroy(); // fs
       
   301 	}	
       
   302 	
       
   303 HBufC8* CxSPContactManager::LinkToBufferL( const MVPbkContactLink* aLink )
       
   304 	{
       
   305 	HBufC8* buffer = NULL;	
       
   306 	if( aLink )
       
   307 		{
       
   308 		const MVPbkStreamable* streamable = aLink->Streamable();
       
   309 		if( streamable )
       
   310 			{
       
   311 			CBufFlat* buf = CBufFlat::NewL( KExpandSize );
       
   312 			CleanupStack::PushL( buf );
       
   313 			RBufWriteStream writeStream( *buf );
       
   314 			CleanupClosePushL( writeStream );
       
   315 			streamable->ExternalizeL( writeStream );
       
   316 			writeStream.CommitL();
       
   317 			CleanupStack::PopAndDestroy(); // writeStream				
       
   318 			TPtr8 ptr = buf->Ptr( 0 );
       
   319 			buffer = ptr.AllocL();	// CSI: 35 #
       
   320 			CleanupStack::PopAndDestroy(); // buf			
       
   321 			}
       
   322 		}
       
   323 	if( !buffer )
       
   324 		{
       
   325 		buffer = KNullDesC8().AllocL();
       
   326 		}
       
   327 	return buffer;
       
   328 	}
       
   329 	
       
   330 MVPbkContactLink* CxSPContactManager::BufferToLinkL( const TDesC8& aBuffer,
       
   331 									const MVPbkContactStoreList& aContactStores )
       
   332 	{
       
   333 	MVPbkContactLink* contactLink = NULL;
       
   334 	if( aBuffer.Length() )
       
   335 		{
       
   336 		CBufFlat* buf = CBufFlat::NewL( KExpandSize );
       
   337 		CleanupStack::PushL( buf );
       
   338 		buf->InsertL( 0, aBuffer );
       
   339 		RBufReadStream readStream( *buf );
       
   340 		CleanupClosePushL( readStream );	
       
   341 		CVPbkContactLinkArray* array = CVPbkContactLinkArray::NewLC( readStream, aContactStores );
       
   342 		TInt count = array->Count();												
       
   343 		if( count == 1 ) // should contain only single contact link
       
   344 			{
       
   345 			const MVPbkContactLink& link = array->At( 0 );
       
   346 			contactLink = link.CloneLC();
       
   347 			CleanupStack::Pop(); // contactLink
       
   348 			}						
       
   349 		CleanupStack::PopAndDestroy( 3 ); // array, readStream, buf	
       
   350 		}
       
   351 	return contactLink;
       
   352 	}			
       
   353         								
       
   354 // End of File