phonebookui/Phonebook2/xSPExtensionManager/src/CxSPContactManager.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     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 	CleanupClosePushL( aPbkContactLinks );
       
   142 	TInt count = iContactMap.Count();
       
   143 	for( TInt i = 0; i < count; i++ )
       
   144 		{
       
   145 		const CxSPPbkContactMap* map = iContactMap[i];
       
   146 		if( map->iId == aId && map->ixSPContactId == axSPContactId && map->iPbkContactLink )
       
   147 			{
       
   148 			User::LeaveIfError( aPbkContactLinks.Append( map->iPbkContactLink ) );
       
   149 			}
       
   150 		}
       
   151 	CleanupStack::Pop();
       
   152 	}		
       
   153 
       
   154 void CxSPContactManager::GetMappedxSPContactsL( TUint32 aId, 
       
   155 												const MVPbkContactLink& aPbkContactLink,
       
   156         										RArray<TInt32>& axSPContactIds ) const
       
   157 	{
       
   158 	CleanupClosePushL( axSPContactIds );
       
   159 	const TInt count = iContactMap.Count();
       
   160 	for( TInt i = 0; i < count; i++ )
       
   161 		{
       
   162 		const CxSPPbkContactMap* map = iContactMap[i];
       
   163 		if( map->iId == aId && map->iPbkContactLink )
       
   164 			{
       
   165 			if( map->iPbkContactLink->IsSame( aPbkContactLink ) )
       
   166 				{
       
   167 				User::LeaveIfError( axSPContactIds.Append( map->ixSPContactId ) );
       
   168 				}
       
   169 			}			
       
   170 		}
       
   171 	CleanupStack::Pop();
       
   172 	}        										
       
   173 	
       
   174 void CxSPContactManager::ViewActivatedL( const TVwsViewId& aPrevViewId,
       
   175                          				TUid aCustomMessageId,
       
   176                          				const TDesC8& aCustomMessage )
       
   177 	{
       
   178 	if( aCustomMessageId == CPbk2ViewState::Uid() && 
       
   179     						aPrevViewId.iViewUid.iUid == EPbk2NamesListViewId )
       
   180 		{
       
   181 		delete iState;
       
   182 		iState = NULL;
       
   183 		iState = CPbk2ViewState::NewL( aCustomMessage );
       
   184 		}								
       
   185 	}
       
   186 			
       
   187 const CPbk2ViewState* CxSPContactManager::NamesListState() const
       
   188 	{
       
   189 	return iState;
       
   190 	}		  	   
       
   191  
       
   192 TInt CxSPContactManager::CreatePrivateFolder( RFs& aSession, 
       
   193 											  TDes& aPath, 
       
   194 											  TDriveNumber aDrive ) const
       
   195 	{
       
   196 	_LIT( KColon, ":" );
       
   197 	TChar drive;
       
   198 	TInt err = aSession.DriveToChar( aDrive, drive );							
       
   199 	if( !err )
       
   200 		{
       
   201 		err = aSession.CreatePrivatePath( aDrive );
       
   202 		if( !err )
       
   203 			{
       
   204 			TFileName path;			
       
   205 			aSession.PrivatePath( path );
       
   206 			aPath.Zero();
       
   207 			aPath.Append( drive );
       
   208 			aPath.Append( KColon );
       
   209 			aPath.Append( path );														
       
   210 			}		
       
   211 		}	
       
   212 	return err;
       
   213 	} 
       
   214  
       
   215 TInt CxSPContactManager::FindMap( TUint32 aId, 
       
   216 									const MVPbkContactLink& aPbkContactLink, 
       
   217 									TInt32 axSPContactId ) const
       
   218 	{
       
   219 	TInt index( KErrNotFound );
       
   220 	TInt count = iContactMap.Count();
       
   221 	for( TInt i = 0; i < count && index == KErrNotFound; i++ )
       
   222 		{
       
   223 		const CxSPPbkContactMap* map = iContactMap[i];
       
   224 		if( map->iId == aId && map->ixSPContactId == axSPContactId && map->iPbkContactLink )
       
   225 			{
       
   226 			if( map->iPbkContactLink->IsSame( aPbkContactLink ) )
       
   227 				{
       
   228 				index = i;
       
   229 				}
       
   230 			}			
       
   231 		}
       
   232 	return index;
       
   233 	}
       
   234 	
       
   235 void CxSPContactManager::SaveArrayL( const TDesC& aFileName )
       
   236 	{
       
   237 	RFs fs; // CSI: 76 #
       
   238 	User::LeaveIfError( fs.Connect() );
       
   239 	CleanupClosePushL( fs );
       
   240 	TFileName orderFile;
       
   241 	User::LeaveIfError( CreatePrivateFolder( fs, orderFile, EDriveC ) );
       
   242 	orderFile.Append( aFileName );
       
   243 	RFileWriteStream writeStream;
       
   244 	TInt err = writeStream.Replace( fs, orderFile, EFileWrite );
       
   245 	if( !err )
       
   246 		{
       
   247 		CleanupClosePushL( writeStream );		
       
   248 		TUint32 count = iContactMap.Count();
       
   249 		writeStream.WriteUint32L( count );
       
   250 		for( TUint32 i = 0; i < count; i++ )
       
   251 			{
       
   252 			writeStream << *iContactMap[i];
       
   253 			}			
       
   254 		CleanupStack::PopAndDestroy(); // writeStream
       
   255 		}
       
   256 	CleanupStack::PopAndDestroy(); // fs
       
   257 	}
       
   258 	
       
   259 void CxSPContactManager::RestoreArrayL( const TDesC& aFileName,
       
   260 										CArrayPtrFlat<MxSPFactory>& aFactories,
       
   261 										MVPbkContactStoreList& aContactStoreList )
       
   262 	{
       
   263 	// read the array from file
       
   264     RFs fs; // CSI: 76 #
       
   265 	User::LeaveIfError( fs.Connect() );
       
   266 	CleanupClosePushL( fs );	
       
   267 	TFileName orderFile;
       
   268 	User::LeaveIfError( CreatePrivateFolder( fs, orderFile, EDriveC ) );
       
   269 	orderFile.Append( aFileName );
       
   270 	RFileReadStream readStream;
       
   271 	TInt err = readStream.Open( fs, orderFile, EFileRead );
       
   272 	if( !err )
       
   273 		{
       
   274 		CleanupClosePushL( readStream );
       
   275 		TUint32 count = readStream.ReadUint32L();
       
   276 		for( TUint32 i = 0; i < count; i++ )
       
   277 			{
       
   278 			CxSPPbkContactMap* map = new (ELeave) CxSPPbkContactMap;
       
   279 			CleanupStack::PushL( map );
       
   280 			map->InternalizeL( readStream, aContactStoreList );
       
   281 			
       
   282 			// only use mappings from the present xSP extensions
       
   283 			// removed xSP extension mappings are not used
       
   284 			TBool found( EFalse );
       
   285 			for( TInt j = 0; j < aFactories.Count() && !found; j++ )
       
   286 				{
       
   287 				if( map->iId == aFactories[j]->Id() )
       
   288 					{
       
   289 					found = ETrue;
       
   290 					}
       
   291 				}			
       
   292 			if( found )
       
   293 				{
       
   294 				User::LeaveIfError( iContactMap.Append( map ) );
       
   295 				CleanupStack::Pop(); // map	
       
   296 				}
       
   297 			else
       
   298 				{
       
   299 				CleanupStack::PopAndDestroy(); // map
       
   300 				}			
       
   301 			}
       
   302 		CleanupStack::PopAndDestroy(); // readStream
       
   303 		}		
       
   304 	CleanupStack::PopAndDestroy(); // fs
       
   305 	}	
       
   306 	
       
   307 HBufC8* CxSPContactManager::LinkToBufferL( const MVPbkContactLink* aLink )
       
   308 	{
       
   309 	HBufC8* buffer = NULL;	
       
   310 	if( aLink )
       
   311 		{
       
   312 		const MVPbkStreamable* streamable = aLink->Streamable();
       
   313 		if( streamable )
       
   314 			{
       
   315 			CBufFlat* buf = CBufFlat::NewL( KExpandSize );
       
   316 			CleanupStack::PushL( buf );
       
   317 			RBufWriteStream writeStream( *buf );
       
   318 			CleanupClosePushL( writeStream );
       
   319 			streamable->ExternalizeL( writeStream );
       
   320 			writeStream.CommitL();
       
   321 			CleanupStack::PopAndDestroy(); // writeStream				
       
   322 			TPtr8 ptr = buf->Ptr( 0 );
       
   323 			buffer = ptr.AllocL();	// CSI: 35 #
       
   324 			CleanupStack::PopAndDestroy(); // buf			
       
   325 			}
       
   326 		}
       
   327 	if( !buffer )
       
   328 		{
       
   329 		buffer = KNullDesC8().AllocL();
       
   330 		}
       
   331 	return buffer;
       
   332 	}
       
   333 	
       
   334 MVPbkContactLink* CxSPContactManager::BufferToLinkL( const TDesC8& aBuffer,
       
   335 									const MVPbkContactStoreList& aContactStores )
       
   336 	{
       
   337 	MVPbkContactLink* contactLink = NULL;
       
   338 	if( aBuffer.Length() )
       
   339 		{
       
   340 		CBufFlat* buf = CBufFlat::NewL( KExpandSize );
       
   341 		CleanupStack::PushL( buf );
       
   342 		buf->InsertL( 0, aBuffer );
       
   343 		RBufReadStream readStream( *buf );
       
   344 		CleanupClosePushL( readStream );	
       
   345 		CVPbkContactLinkArray* array = CVPbkContactLinkArray::NewLC( readStream, aContactStores );
       
   346 		TInt count = array->Count();												
       
   347 		if( count == 1 ) // should contain only single contact link
       
   348 			{
       
   349 			const MVPbkContactLink& link = array->At( 0 );
       
   350 			contactLink = link.CloneLC();
       
   351 			CleanupStack::Pop(); // contactLink
       
   352 			}						
       
   353 		CleanupStack::PopAndDestroy( 3 ); // array, readStream, buf	
       
   354 		}
       
   355 	return contactLink;
       
   356 	}			
       
   357         								
       
   358 // End of File