emailservices/psmruadapter/src/CPsMruAdapter.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Data Store Adapter for MRU list
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <CPsData.h>
       
    21 #include <CPcsDefs.h>
       
    22 #include <coemain.h>
       
    23 #include <data_caging_path_literals.hrh>
       
    24 #include <VPbkEng.rsg>
       
    25 //<cmail>
       
    26 #include "CFSMailClient.h"
       
    27 #include "CPsMruAdapter.h"
       
    28 //</cmail>
       
    29 #include <mmf/common/mmfcontrollerpluginresolver.h> // CleanupResetAndDestroy
       
    30 
       
    31 
       
    32 #define PRINT(x)
       
    33 
       
    34 // ============================== MEMBER FUNCTIONS ============================
       
    35 
       
    36 // Identifier for mailbox Uri
       
    37 _LIT(KDefaultMailBoxURI, "mailbox://");
       
    38 // Separator for plugin/mailbox identifiers inside the Uri string
       
    39 const TText8 KDefaultMailBoxURISeparator = '/';
       
    40 // Maximum length of uri
       
    41 const TInt KMaximumMailboxUriLength = 64;
       
    42 
       
    43 // ----------------------------------------------------------------------------
       
    44 // CPsMruAdapter::NewL
       
    45 // Two Phase Construction
       
    46 // ----------------------------------------------------------------------------
       
    47 CPsMruAdapter* CPsMruAdapter::NewL( TAny* aPsDataPluginParameters )
       
    48 	{
       
    49 	PRINT ( _L( "Enter CPsMruAdapter::NewL" ) );
       
    50 
       
    51 	// Get the PsData plugin parametrs
       
    52 	TPsDataPluginParams* params =
       
    53 			reinterpret_cast<TPsDataPluginParams*>(aPsDataPluginParameters );
       
    54 	CPsMruAdapter* self = new ( ELeave ) CPsMruAdapter();
       
    55 	CleanupStack::PushL(self);
       
    56 	self->ConstructL( params->iDataStoreObserver, params->iStoreListObserver );
       
    57 	CleanupStack::Pop( self );
       
    58 
       
    59 	PRINT ( _L( "End CPsMruAdapter::NewL" ) );
       
    60 
       
    61 	return self;
       
    62 	}
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // CPsMruAdapter::CPsMruAdapter
       
    66 // Two Phase Construction
       
    67 // ----------------------------------------------------------------------------
       
    68 CPsMruAdapter::CPsMruAdapter()
       
    69 	{
       
    70 	PRINT ( _L( "Enter CPsMruAdapter::CPsMruAdapter" ) );
       
    71 	PRINT ( _L( "End CPsMruAdapter::CPsMruAdapter" ) );
       
    72 	}
       
    73 
       
    74 // ----------------------------------------------------------------------------
       
    75 // CPsMruAdapter::ConstructL
       
    76 // Two Phase Construction
       
    77 // ----------------------------------------------------------------------------
       
    78 void CPsMruAdapter::ConstructL( MDataStoreObserver* aObserverForDataStore,
       
    79 		MStoreListObserver* aStoreListObserver )
       
    80 	{
       
    81 	PRINT ( _L( "Enter CPsMruAdapter::ConstructL" ) );
       
    82 
       
    83 	// Mail client for FS Email framework
       
    84 	iMailClient = CFSMailClient::NewL();
       
    85 
       
    86 	// Register as observer for mail client, we'll need this to observe MRU list changes
       
    87 	// When i.e mail is sent
       
    88 	iMailClient->AddObserverL( *this );
       
    89 
       
    90 	// This updates the list of all the supported datastores
       
    91 	UpdateSupportedDataStoresList();
       
    92 
       
    93 	// Store these for later use
       
    94 	iDataStoreObserver = aObserverForDataStore;
       
    95 	iStoreListObserver = aStoreListObserver;
       
    96 
       
    97 	PRINT ( _L( "End CPsMruAdapter::ConstructL" ) );
       
    98 	}
       
    99 
       
   100 // ----------------------------------------------------------------------------
       
   101 // CPsMruAdapter::~CPsMruAdapter
       
   102 // Destructor
       
   103 // ----------------------------------------------------------------------------
       
   104 CPsMruAdapter::~CPsMruAdapter()
       
   105     {
       
   106     PRINT ( _L( "Enter CPsMruAdapter::~CPsMruAdapter" ) );
       
   107 
       
   108     iSupportedUris.ResetAndDestroy();
       
   109 
       
   110     // Remove observer, we don't need it anymore because we're shutting down
       
   111     RemoveAllMailboxObservers();
       
   112     iObservedMailboxes.Close();
       
   113 
       
   114     if ( iMailClient )
       
   115         {
       
   116         iMailClient->RemoveObserver( *this );
       
   117         iMailClient->Close();
       
   118         }
       
   119 
       
   120     PRINT ( _L( "End CPsMruAdapter::~CPsMruAdapter" ) );
       
   121     }
       
   122 
       
   123 // ----------------------------------------------------------------------------
       
   124 // CPsMruAdapter::RequestForDataL
       
   125 // 
       
   126 // ----------------------------------------------------------------------------
       
   127 void CPsMruAdapter::RequestForDataL( TDesC& aDataStoreURI )
       
   128 	{
       
   129 	PRINT ( _L( "Enter CPsMruAdapter::RequestForDataL" ) );
       
   130 
       
   131 	// Add data to datastore, fill only requested one
       
   132 	FillDataStoreL( aDataStoreURI );
       
   133 
       
   134 	PRINT ( _L( "Enter CPsMruAdapter::RequestForDataL" ) );
       
   135 	}
       
   136 
       
   137 // ----------------------------------------------------------------------------
       
   138 // CPsMruAdapter::GetSupportedDataStoresL
       
   139 // 
       
   140 // ----------------------------------------------------------------------------
       
   141 
       
   142 void CPsMruAdapter::GetSupportedDataStoresL(
       
   143 		RPointerArray<TDesC> &aDataStoresURIs )
       
   144 	{
       
   145 	for ( TInt i = 0; i < iSupportedUris.Count(); i++ )
       
   146 		{
       
   147 		aDataStoresURIs.Append( iSupportedUris[i] );
       
   148 		}
       
   149 	}
       
   150 
       
   151 // ----------------------------------------------------------------------------
       
   152 // CPsMruAdapter::RequestForDataExtensionL
       
   153 // 
       
   154 // ----------------------------------------------------------------------------
       
   155 TAny* CPsMruAdapter::RequestForDataExtensionL(TInt /*aItemId*/)
       
   156 	{
       
   157 	// No extention required for this since we have and interger as itemId
       
   158 	// Simply return NULL
       
   159 	return NULL;
       
   160 	}
       
   161 
       
   162 // ----------------------------------------------------------------------------
       
   163 // CPsMruAdapter::UpdateSupportedDataStoresList
       
   164 // 
       
   165 // ----------------------------------------------------------------------------
       
   166 void CPsMruAdapter::UpdateSupportedDataStoresList()
       
   167 	{
       
   168 	// Mailboxes will be fetched to this array
       
   169 	RPointerArray<CFSMailBox> mailBoxes;
       
   170 
       
   171 	// List all mailboxes
       
   172 	TFSMailMsgId plugin;
       
   173 	iMailClient->ListMailBoxes( plugin, mailBoxes );
       
   174 
       
   175 	iSupportedUris.ResetAndDestroy();
       
   176 
       
   177 	// Add all mailboxes as data stores
       
   178 	for ( TInt i = 0; i < mailBoxes.Count(); i++ )
       
   179 		{
       
   180 		// Get id of mailbox
       
   181 		TFSMailMsgId id = mailBoxes[i]->GetId();
       
   182 
       
   183 		// Convert it to string, this will be the unique identifier for this mailbox
       
   184 		HBufC* identifier = HBufC::New( KMaximumMailboxUriLength );
       
   185 		if ( identifier && GetUriFromMailboxIdentifier( id, *identifier ) )
       
   186 			{
       
   187 			// Add to supported Uris list
       
   188 			if ( iSupportedUris.Append( identifier ) == KErrNone )
       
   189 			    {
       
   190 			    // Ownership of the string is successfully moved to the array
       
   191 			    identifier = NULL;
       
   192 			    }
       
   193 			}
       
   194 		delete identifier;
       
   195 		}
       
   196 
       
   197 	// Release allocated memory
       
   198 	mailBoxes.ResetAndDestroy();
       
   199 	}
       
   200 
       
   201 // ----------------------------------------------------------------------------
       
   202 // CPsMruAdapter::FillDataStoreL
       
   203 // ----------------------------------------------------------------------------
       
   204 TBool CPsMruAdapter::FillDataStoreL( TDesC& aDataStoreURI )
       
   205 	{
       
   206 	TBool result = EFalse;
       
   207 
       
   208 	TFSMailMsgId dataStoreId;
       
   209 
       
   210 	if ( GetMailboxIdentifierFromUri( aDataStoreURI, dataStoreId ) )
       
   211 		{
       
   212 		result = FillDataStoreL( dataStoreId, aDataStoreURI );
       
   213 		}
       
   214 
       
   215 	return result;
       
   216 	}
       
   217 
       
   218 TBool CPsMruAdapter::FillDataStoreL( TFSMailMsgId& aId )
       
   219 	{
       
   220 	TBool result = EFalse;
       
   221 
       
   222 	// Create Uri for this mailbox
       
   223 	HBufC* identifier = HBufC::NewLC( KMaximumMailboxUriLength );
       
   224 	if ( GetUriFromMailboxIdentifier( aId, *identifier ) )
       
   225 		{
       
   226 		result = FillDataStoreL( aId, *identifier );
       
   227 		}
       
   228 	CleanupStack::PopAndDestroy( identifier );
       
   229 	return result;
       
   230 	}
       
   231 
       
   232 TBool CPsMruAdapter::FillDataStoreL( TFSMailMsgId& aId, TDesC& aDataStoreURI )
       
   233 	{
       
   234 	TBool result = EFalse;
       
   235 
       
   236 	// Mailboxes will be fetched to this array
       
   237 	RPointerArray<CFSMailBox> mailBoxes;
       
   238 	CleanupResetAndDestroyPushL( mailBoxes );
       
   239 
       
   240 	// List all mailboxes
       
   241 	TFSMailMsgId plugin;
       
   242 	iMailClient->ListMailBoxes( plugin, mailBoxes );
       
   243 
       
   244 	// Find the requested mailbox
       
   245 	for ( TInt i = 0; i < mailBoxes.Count(); i++ )
       
   246 		{
       
   247 		// Get id of mailbox
       
   248 		TFSMailMsgId id = mailBoxes[i]->GetId();
       
   249 
       
   250 		// Check that this is the requested mailbox
       
   251 		if ( id == aId )
       
   252 			{
       
   253 			AddMailboxObserverL( aId );
       
   254 			
       
   255 			// update the caching status as InProgress
       
   256 			iDataStoreObserver->UpdateCachingStatus( aDataStoreURI,
       
   257 					ECachingInProgress );
       
   258 
       
   259 			// Update datastore contents, first reset
       
   260 			iDataStoreObserver->RemoveAll( aDataStoreURI );
       
   261 
       
   262 			// Get MRU list for this mailbox
       
   263 			MDesCArray* mruList = mailBoxes[i]->ListMrusL();
       
   264 
       
   265 			if ( mruList )
       
   266 				{
       
   267 				TInt entryIndex = 0;
       
   268 	
       
   269 				// Add all data to data store, the format is:
       
   270 				// index0: displayname
       
   271 				// index1: email
       
   272 				// index2: next displayname
       
   273 				// index3: next email
       
   274 				// etc..
       
   275 				for ( int mruIndex = 0; mruIndex < mruList->MdcaCount(); mruIndex += 2 )
       
   276 					{
       
   277 					TPtrC displayName = mruList->MdcaPoint( mruIndex );
       
   278 					TPtrC emailAddress = mruList->MdcaPoint( mruIndex + 1 );
       
   279 	
       
   280 					CPsData* mruData = CPsData::NewL();
       
   281 					mruData->SetId( entryIndex++ );
       
   282 	
       
   283 					// Set the data
       
   284 					mruData->SetDataL( 0, displayName );
       
   285 					mruData->SetDataL( 1, KNullDesC );
       
   286 					mruData->SetDataL( 2, emailAddress );
       
   287 	
       
   288 					iDataStoreObserver->AddData( aDataStoreURI, mruData );
       
   289 					}
       
   290 	
       
   291 				delete mruList;
       
   292 				}
       
   293 			// update the caching status as Complete
       
   294 			iDataStoreObserver->UpdateCachingStatus( aDataStoreURI,
       
   295 					ECachingComplete );
       
   296 
       
   297 			result = ETrue;
       
   298 			break;
       
   299 			}
       
   300 		}
       
   301 
       
   302 	CleanupStack::PopAndDestroy( &mailBoxes );
       
   303 	return result;
       
   304 	}
       
   305 
       
   306 // ----------------------------------------------------------------------------
       
   307 // CPsContactDataAdapter::IsDataStoresSupportedL
       
   308 // 
       
   309 // ----------------------------------------------------------------------------
       
   310 TBool CPsMruAdapter::IsDataStoresSupportedL( TDesC& aDataStoreURI )
       
   311 	{
       
   312 	for ( TInt i = 0; i < iSupportedUris.Count(); i++ )
       
   313 		{
       
   314 		if ( iSupportedUris[i]->Compare( aDataStoreURI ) == 0 )
       
   315 			return ETrue;
       
   316 		}
       
   317 
       
   318 	return EFalse;
       
   319 	}
       
   320 
       
   321 // ----------------------------------------------------------------------------
       
   322 // CPsContactDataAdapter::GetSupportedDataFieldsL
       
   323 // 
       
   324 // ----------------------------------------------------------------------------
       
   325 void CPsMruAdapter::GetSupportedDataFieldsL( RArray<TInt>& aDataFields )
       
   326 	{
       
   327 	aDataFields.Append( R_VPBK_FIELD_TYPE_FIRSTNAME );
       
   328 	aDataFields.Append( R_VPBK_FIELD_TYPE_LASTNAME );
       
   329 	aDataFields.Append( R_VPBK_FIELD_TYPE_EMAILGEN );
       
   330 	}
       
   331 
       
   332 TBool CPsMruAdapter::GetMailboxIdentifierFromUri( TDesC& aUri, TFSMailMsgId& aId )
       
   333 	{
       
   334 	// Find first separator in reverse order
       
   335 	TInt lastSeparator = aUri.LocateReverseF( KDefaultMailBoxURISeparator );
       
   336 	if ( lastSeparator == KErrNotFound )
       
   337 		return EFalse;
       
   338 
       
   339 	// This is where plugin id string starts
       
   340 	TInt pluginIdStartPosition = KDefaultMailBoxURI().Length();
       
   341 	TInt mailboxIdStartPosition = lastSeparator + 1;
       
   342 
       
   343 	// It cannot be further than mailbox id
       
   344 	if ( pluginIdStartPosition >= mailboxIdStartPosition )
       
   345 		return EFalse;
       
   346 
       
   347 	// Use TLex to convert string to integer
       
   348 	TLex pluginIdConverter( aUri.Mid( pluginIdStartPosition,
       
   349 			mailboxIdStartPosition - pluginIdStartPosition - 1 ) );
       
   350 
       
   351 	// Use TLex to convert string to integer
       
   352 	TLex mailboxIdConverter( aUri.Mid( mailboxIdStartPosition, aUri.Length()
       
   353 			- mailboxIdStartPosition ) );
       
   354 
       
   355 	TInt pluginId;
       
   356 	TInt mailboxId;
       
   357 
       
   358 	// Get plugin ID
       
   359 	if ( pluginIdConverter.Val( pluginId ) != KErrNone)
       
   360 		return EFalse;
       
   361 
       
   362 	// Get mailbox ID
       
   363 	if ( mailboxIdConverter.Val( mailboxId ) != KErrNone)
       
   364 		return EFalse;
       
   365 
       
   366 	// Store and we're ready
       
   367 	aId.SetPluginId( TUid::Uid( pluginId ) );
       
   368 	aId.SetId( mailboxId );
       
   369 
       
   370 	return ETrue;
       
   371 	}
       
   372 
       
   373 TBool CPsMruAdapter::GetUriFromMailboxIdentifier( TFSMailMsgId& aId, HBufC& aUri )
       
   374 	{
       
   375 	// Add the uri identifier
       
   376 	aUri.Des().Copy( KDefaultMailBoxURI );
       
   377 	// Add plugin ID
       
   378 	aUri.Des().AppendNum( aId.PluginId().iUid );
       
   379 	// Add separator
       
   380 	aUri.Des().Append( KDefaultMailBoxURISeparator );
       
   381 	// Add mailbox id
       
   382 	aUri.Des().AppendNum( aId.Id() );
       
   383 
       
   384 	return true;
       
   385 	}
       
   386 
       
   387 void CPsMruAdapter::EventL( TFSMailEvent aEvent, TFSMailMsgId aMailbox,
       
   388 		TAny* /*aParam1*/, TAny* aParam2, TAny* /*aParam3*/ )
       
   389 	{
       
   390 	switch ( aEvent )
       
   391 		{
       
   392 		case TFSEventMailMoved:
       
   393 		case TFSEventMailCopied:		
       
   394 		case TFSEventNewMail:
       
   395 			{
       
   396 			// Check the new parent folder id for this message
       
   397 			// For all these events, param2 indicates the new parent folder
       
   398 			TFSMailMsgId* parentFolderId =
       
   399 					static_cast< TFSMailMsgId* >( aParam2 );
       
   400 			if ( parentFolderId )
       
   401 				{
       
   402 				// Get the parent folder object
       
   403 				CFSMailFolder* parentFolder = iMailClient->GetFolderByUidL(
       
   404 						aMailbox, *parentFolderId );
       
   405 				if ( parentFolder )
       
   406 					{
       
   407 					CleanupStack::PushL( parentFolder );
       
   408 
       
   409 					// If it's sent/outbox folder,
       
   410 					// we'll consider that as a new message being sent
       
   411 					// and therefore we'll update the MRU list here
       
   412 					if ( ( parentFolder->GetFolderType() == EFSSentFolder ) || 
       
   413 						 ( parentFolder->GetFolderType() == EFSOutbox ) )
       
   414 						{
       
   415 						FillDataStoreL( aMailbox );
       
   416 						}
       
   417 					CleanupStack::PopAndDestroy( parentFolder );
       
   418 					}
       
   419 				}
       
   420 			}
       
   421 			break;
       
   422 			
       
   423 		case TFSEventNewMailbox:
       
   424 			{
       
   425 			HBufC* identifier = HBufC::NewLC( KMaximumMailboxUriLength );
       
   426 			if ( GetUriFromMailboxIdentifier( aMailbox, *identifier ) )
       
   427 				{
       
   428 				// Add to supported Uri list
       
   429 				iSupportedUris.AppendL( identifier );
       
   430 
       
   431 				// Add new data store to cache
       
   432 				iStoreListObserver->AddDataStore( *identifier );
       
   433 
       
   434 				// Add all data to data store
       
   435 				FillDataStoreL( *identifier );
       
   436 
       
   437 				CleanupStack::Pop( identifier );
       
   438 				}
       
   439 			else
       
   440 				{
       
   441 				CleanupStack::PopAndDestroy( identifier );
       
   442 				}
       
   443 			}
       
   444 			break;
       
   445 
       
   446 		case TFSEventMailboxDeleted:
       
   447 			{
       
   448 			HBufC* identifier = HBufC::NewL( KMaximumMailboxUriLength );
       
   449 			if ( GetUriFromMailboxIdentifier( aMailbox, *identifier ) )
       
   450 				{
       
   451 				RemoveMailboxObserver( aMailbox );
       
   452 				
       
   453 				// Remove data store from cache
       
   454 				iStoreListObserver->RemoveDataStore( *identifier );
       
   455 
       
   456 				// Remove from supported Uri list
       
   457 				for ( TInt i = 0; i < iSupportedUris.Count(); i++ )
       
   458 					{
       
   459 					if ( iSupportedUris[i]->Compare( *identifier ) == 0 )
       
   460 						{
       
   461 						delete iSupportedUris[i];
       
   462 						iSupportedUris.Remove( i );
       
   463 						break;
       
   464 						}
       
   465 					}
       
   466 				}
       
   467 			delete identifier;
       
   468 			}
       
   469 			break;
       
   470 		}
       
   471 	}
       
   472 
       
   473 TBool CPsMruAdapter::AddMailboxObserverL( TFSMailMsgId& aId )
       
   474 	{
       
   475 	for( TInt index = 0; index < iObservedMailboxes.Count(); index++ )
       
   476 		{
       
   477 		if( iObservedMailboxes[index] == aId )
       
   478 			{
       
   479 			// Already observing
       
   480 			return EFalse;
       
   481 			}
       
   482 		}
       
   483 	
       
   484 	iMailClient->SubscribeMailboxEventsL( aId, *this );
       
   485 	iObservedMailboxes.Append( aId );
       
   486 	return ETrue;
       
   487 	}
       
   488 
       
   489 TBool CPsMruAdapter::RemoveMailboxObserver( TFSMailMsgId& aId )
       
   490 	{
       
   491 	for( TInt index = 0; index < iObservedMailboxes.Count(); index++ )
       
   492 		{
       
   493 		if( iObservedMailboxes[index] == aId )
       
   494 			{
       
   495 			iMailClient->UnsubscribeMailboxEvents( aId, *this );
       
   496 			iObservedMailboxes.Remove( index );
       
   497 			return ETrue;
       
   498 			}
       
   499 		}
       
   500 	
       
   501 	return EFalse;
       
   502 	}
       
   503 
       
   504 void CPsMruAdapter::RemoveAllMailboxObservers()
       
   505 	{
       
   506 	for( TInt index = 0; index < iObservedMailboxes.Count(); index++ )
       
   507 		{
       
   508 		iMailClient->UnsubscribeMailboxEvents( iObservedMailboxes[index], *this );
       
   509 		}
       
   510 
       
   511 	iObservedMailboxes.Reset();	
       
   512 	}
       
   513 
       
   514 
       
   515 // End of file
       
   516 
       
   517