omads/omadsextensions/adapters/sms/src/smsadaptermsvapi.cpp
changeset 40 b63e67867dcd
child 43 3daf89f0874a
child 64 a62b67d1f67c
equal deleted inserted replaced
39:9905f7d46607 40:b63e67867dcd
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Part of SyncML Data Synchronization Plug In Adapter
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <txtrich.h>
       
    22 #include <smsclnt.h>
       
    23 #include <smutset.h>
       
    24 #include <smuthdr.h>
       
    25 #include <smscmds.h>
       
    26 #include <sysutil.h> 
       
    27 #include <gsmuelem.h>
       
    28 #include <cntdb.h>
       
    29 #include <cntitem.h>
       
    30 #include <cntfldst.h>
       
    31 #include "smsadaptermsvapi.h"
       
    32 #include "logger.h" 
       
    33 #include "vmessageparser.h"
       
    34 #include "smsdataproviderdefs.h"
       
    35 
       
    36 
       
    37 // CONSTANTS
       
    38 
       
    39 _LIT16(KSmsNonUnicodeChars, "èéùìòÇØøÅåÆæßÉ£$¥¡ÄÖÑܧ¿äöñüà");
       
    40         
       
    41 // OTHER DEFINITIONS
       
    42 
       
    43 
       
    44 
       
    45 // ============================= LOCAL FUNCTIONS ===============================
       
    46 
       
    47 
       
    48 
       
    49 // ============================ MEMBER FUNCTIONS ===============================
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CSmsAdapterMsvApi::NewL
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55  CSmsAdapterMsvApi* CSmsAdapterMsvApi::NewL()
       
    56     {
       
    57     CSmsAdapterMsvApi* self = new( ELeave ) CSmsAdapterMsvApi;
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL();
       
    60     CleanupStack::Pop( self );
       
    61     return self;
       
    62     }
       
    63     
       
    64 // -----------------------------------------------------------------------------
       
    65 // CSmsAdapterMsvApi::~CSmsAdapterMsvApi()
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 CSmsAdapterMsvApi::~CSmsAdapterMsvApi()
       
    69     {
       
    70     LOGGER_ENTERFN( "CSmsAdapterMsvApi::~CSmsAdapterMsvApi" );
       
    71     SAFEDELETE( iContactsDb );
       
    72 	SAFEDELETE( iMtm );
       
    73 	SAFEDELETE( iMtmReg );     
       
    74     SAFEDELETE( iSession );
       
    75     LOGGER_LEAVEFN( "CSmsAdapterMsvApi::~CSmsAdapterMsvApi" );
       
    76     }
       
    77        
       
    78 // -----------------------------------------------------------------------------
       
    79 // CSmsAdapterMsvApi::CSmsAdapterMsvApi
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CSmsAdapterMsvApi::CSmsAdapterMsvApi()
       
    83     {     
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CSmsAdapterMsvApi::ConstructL
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void CSmsAdapterMsvApi::ConstructL()
       
    91     {
       
    92     LOGGER_ENTERFN( "CSmsAdapterMsvApi::ConstructL" );
       
    93 
       
    94     iSession = CMsvSession::OpenSyncL( *this );
       
    95     iMtmReg = CClientMtmRegistry::NewL( *iSession );    
       
    96 	iMtm = static_cast<CSmsClientMtm*>( iMtmReg->NewMtmL(KUidMsgTypeSMS) ); 
       
    97 
       
    98 	iFs = iSession->FileSession();
       
    99 	iMessageDrive = MessageServer::CurrentDriveL( iFs );
       
   100 	
       
   101 	iContactsDb = CContactDatabase::OpenL();
       
   102 
       
   103     LOGGER_LEAVEFN( "CSmsAdapterMsvApi::ConstructL" ); 
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CSmsAdapterMsvApi::AddSML
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void CSmsAdapterMsvApi::AddSML(
       
   111     CVMessageParser& aSm,
       
   112     TMsvId aFolder,                                                                                                                                                                                                        
       
   113     TMsvId& aSmId )
       
   114     {
       
   115     LOGGER_ENTERFN( "CSmsAdapterMsvApi::AddSML" );
       
   116    
       
   117     if (!ValidFolderL( aFolder ))
       
   118         {
       
   119 		LOGGER_WRITE( "AddSML: wrong folder" );
       
   120         User::Leave( KErrArgument );
       
   121         }
       
   122            
       
   123     // Set first default flags  
       
   124     TMsvEntry newEntry;  
       
   125     newEntry.iType = KUidMsvMessageEntry;
       
   126     newEntry.iServiceId = KMsvLocalServiceIndexEntryId;
       
   127     newEntry.iMtm = KUidMsgTypeSMS;
       
   128     newEntry.SetVisible(EFalse);
       
   129     newEntry.SetInPreparation(ETrue);           
       
   130 
       
   131     // Create new message entry
       
   132     CMsvEntry* entry = iSession->GetEntryL( aFolder );
       
   133     CleanupStack::PushL( entry );   
       
   134     entry->CreateL( newEntry );
       
   135     aSmId = newEntry.Id();  
       
   136     entry->SetEntryL(newEntry.Id());
       
   137     
       
   138     // Create message header
       
   139 
       
   140     CParaFormatLayer* paraFormatLayer = CParaFormatLayer::NewL();
       
   141     CleanupStack::PushL( paraFormatLayer );
       
   142     CCharFormatLayer* charFormatLayer = CCharFormatLayer::NewL();
       
   143     CleanupStack::PushL( charFormatLayer );
       
   144     CRichText* richText = CRichText::NewL( paraFormatLayer, charFormatLayer );
       
   145     CleanupStack::PushL( richText ); 
       
   146     
       
   147     CSmsPDU::TSmsPDUType pduType;
       
   148     
       
   149     if (aFolder == KMsvGlobalInBoxIndexEntryId)
       
   150         {
       
   151         pduType = CSmsPDU::ESmsDeliver;
       
   152         }
       
   153     else if (aFolder == KMsvGlobalOutBoxIndexEntryId ||
       
   154              aFolder == KMsvDraftEntryId ||
       
   155              aFolder == KMsvSentEntryId)  
       
   156         {
       
   157         pduType = CSmsPDU::ESmsSubmit;
       
   158         }
       
   159     else if (aSm.iRecipients.Count() > 0)
       
   160         {
       
   161         pduType = CSmsPDU::ESmsSubmit;
       
   162         }
       
   163     else
       
   164         {
       
   165         pduType = CSmsPDU::ESmsDeliver;
       
   166         }       
       
   167     
       
   168     CSmsHeader* smsHeader = CSmsHeader::NewL( pduType, *richText );
       
   169     CleanupStack::PushL( smsHeader );
       
   170     
       
   171     // Set the message header in the entry's store
       
   172     CMsvStore* store = entry->EditStoreL();
       
   173     CleanupStack::PushL( store );
       
   174     smsHeader->StoreL( *store );
       
   175     store->StoreBodyTextL( *richText );
       
   176     store->CommitL();
       
   177     
       
   178     CleanupStack::PopAndDestroy( store );
       
   179     CleanupStack::PopAndDestroy( smsHeader );
       
   180     CleanupStack::PopAndDestroy( richText );
       
   181     CleanupStack::PopAndDestroy( charFormatLayer );
       
   182     CleanupStack::PopAndDestroy( paraFormatLayer );
       
   183     CleanupStack::PopAndDestroy( entry );
       
   184    
       
   185 	DoUpdateSML( aSmId, aSm, ETrue );
       
   186 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::AddSML" );
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CSmsAdapterMsvApi::ReplaceSML
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193  void CSmsAdapterMsvApi::ReplaceSML( TMsvId aSmId, CVMessageParser& aSm ) 
       
   194     {
       
   195     LOGGER_ENTERFN( "CSmsAdapterMsvApi::ReplaceSML" );
       
   196 	DoUpdateSML( aSmId, aSm, EFalse );
       
   197 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::ReplaceSML" );
       
   198     }
       
   199     
       
   200 // -----------------------------------------------------------------------------
       
   201 // CSmsAdapterMsvApi::DeleteSML
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204  void CSmsAdapterMsvApi::DeleteSML( TMsvId aSmId )
       
   205     {
       
   206     LOGGER_ENTERFN( "CSmsAdapterMsvApi::DeleteSML" );
       
   207     
       
   208     iMtm->SwitchCurrentEntryL( aSmId );
       
   209     
       
   210     TMsvEntry tEntry = iMtm->Entry().Entry();  		
       
   211     if (tEntry.iType != KUidMsvMessageEntry || tEntry.iMtm != KUidMsgTypeSMS)
       
   212         {
       
   213         LOGGER_WRITE( "Not SMS entry" );
       
   214         User::Leave(KErrNotSupported);
       
   215         }
       
   216     
       
   217 	iMtm->SwitchCurrentEntryL( tEntry.Parent() );
       
   218 	iMtm->Entry().DeleteL( aSmId );
       
   219 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::DeleteSML" );
       
   220     }
       
   221     
       
   222 // -----------------------------------------------------------------------------
       
   223 // CSmsAdapterMsvApi::DeleteUserFolderL
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226  TInt CSmsAdapterMsvApi::DeleteUserFolderL( TMsvId aUid )
       
   227     {
       
   228     LOGGER_ENTERFN( "CSmsAdapterMsvApi::DeleteUserFolderL" );
       
   229     
       
   230     iMtm->SwitchCurrentEntryL( aUid );
       
   231     CMsvEntry& entry = iMtm->Entry();
       
   232     TMsvEntry tEntry = entry.Entry();  		
       
   233     
       
   234     if (tEntry.iType != KUidMsvFolderEntry || tEntry.Parent() != KMsvMyFoldersEntryIdValue)
       
   235         {
       
   236         LOGGER_WRITE( "Not correct folder" );
       
   237         User::Leave(KErrNotSupported);
       
   238         }
       
   239         
       
   240     CMsvEntrySelection* children = entry.ChildrenL();
       
   241     TInt count = children->Count();
       
   242     delete children;
       
   243     
       
   244   	if (count > 0)
       
   245 	    {
       
   246 	    LOGGER_WRITE( "Folder not empty" );
       
   247 	    return KErrInUse;
       
   248 	    }
       
   249 	    
       
   250 	tEntry.SetReadOnly(EFalse);
       
   251 	entry.ChangeL( tEntry );
       
   252        
       
   253 	iMtm->SwitchCurrentEntryL( tEntry.Parent() );	
       
   254 	iMtm->Entry().DeleteL( aUid );
       
   255 	
       
   256 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::DeleteUserFolderL" );
       
   257 	return KErrNone;
       
   258     }   
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CSmsAdapterMsvApi::RetrieveSML
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264  void CSmsAdapterMsvApi::RetrieveSML(
       
   265     TMsvId aSmId,
       
   266     TMsvId& aParent,
       
   267     CVMessageParser& aSm,
       
   268     TBool& aUnread)
       
   269     {
       
   270     LOGGER_ENTERFN( "CSmsAdapterMsvApi::RetrieveSML" );
       
   271                           
       
   272     iMtm->SwitchCurrentEntryL( aSmId );
       
   273     iMtm->LoadMessageL();
       
   274     
       
   275     CRichText& mtmBody = iMtm->Body();
       
   276 	aSm.StoreMessageBodyL( mtmBody );
       
   277        
       
   278     TMsvEntry tEntry = iMtm->Entry().Entry();
       
   279     
       
   280     aUnread = tEntry.Unread();
       
   281 	if (aUnread)
       
   282 		{
       
   283 		aSm.iStatus = KVMsgStatusUnread;
       
   284 		}
       
   285 	else
       
   286 		{
       
   287 		aSm.iStatus = KVMsgStatusRead;
       
   288 		}
       
   289 	
       
   290 	aSm.iUniversalTime = tEntry.iDate;	
       
   291 	aSm.iHomeTime = HomeTimeFromUniversalTime( aSm.iUniversalTime );
       
   292     
       
   293 	aParent = tEntry.Parent();	
       
   294 	switch (aParent)
       
   295 		{
       
   296 		case KMsvGlobalInBoxIndexEntryId:
       
   297 			aSm.iFolder = KFolderInbox;
       
   298 			break;
       
   299 		case KMsvGlobalOutBoxIndexEntryId:
       
   300 			aSm.iFolder = KFolderOutbox;
       
   301 			break;
       
   302 		case KMsvDraftEntryId:
       
   303 			aSm.iFolder = KFolderDraft;
       
   304 			break;
       
   305 		case KMsvSentEntryId:
       
   306 			aSm.iFolder = KFolderSent;
       
   307 			break;
       
   308 		case KMsvMyFoldersEntryIdValue:
       
   309 		    aSm.iFolder = KFolderMyFolders;    
       
   310 			break;
       
   311 		default:
       
   312 		    TPtrC folderName;
       
   313 		    TTime time;
       
   314 		    TBool found = FindUserFolderL(aParent, folderName, time);
       
   315 		    if (found && folderName.Length() <= KMaxFolderNameLength)
       
   316 		        {
       
   317 		        aSm.iFolder = folderName;
       
   318 		        }
       
   319 		    else
       
   320 		        {
       
   321 		        LOGGER_WRITE_1( "Not folder name found for folder: %d", aParent );
       
   322 		        }       	
       
   323 		}
       
   324 		LOG( aSm.iFolder );
       
   325 		
       
   326 	const CSmsHeader& smsHeader = iMtm->SmsHeader();
       
   327 	
       
   328 	if ( smsHeader.Type() == CSmsPDU::ESmsDeliver )
       
   329 	    {
       
   330 		TPtrC fromAddr = smsHeader.FromAddress();
       
   331 		aSm.ParseTelephoneNumber( fromAddr, aSm.iSender );
       
   332 		}
       
   333 	else
       
   334 		{
       
   335 		const CMsvRecipientList& recipients = iMtm->AddresseeList();
       
   336 		for (TInt i = 0; i < recipients.Count(); i++)
       
   337 			{
       
   338 			CVMessageParser::TTelephoneNumber recipientInfo;
       
   339 			aSm.ParseTelephoneNumber( recipients[i], recipientInfo );
       
   340 			aSm.iRecipients.Append( recipientInfo );
       
   341 			}
       
   342 		}	
       
   343 
       
   344 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::RetrieveSML" );
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CSmsAdapterMsvApi::SendSML
       
   349 // -----------------------------------------------------------------------------
       
   350 //
       
   351  void CSmsAdapterMsvApi::SendSML( TMsvId aSmId )
       
   352     {
       
   353     LOGGER_ENTERFN( "CSmsAdapterMsvApi::SendSML" );
       
   354    
       
   355 	iMtm->SwitchCurrentEntryL( aSmId );
       
   356 
       
   357 	TMsvEntry msvEntry = iMtm->Entry().Entry();	
       
   358 	
       
   359 	if (msvEntry.Parent() != KMsvGlobalOutBoxIndexEntryId)
       
   360 		{
       
   361  		LOGGER_WRITE_1( "Wrong folder, parent: %d", msvEntry.Parent() );
       
   362  		return;
       
   363  		}
       
   364 
       
   365 	iMtm->LoadMessageL();
       
   366 
       
   367 	msvEntry.SetInPreparation( EFalse );
       
   368 	msvEntry.SetSendingState( KMsvSendStateWaiting );
       
   369 	msvEntry.iDate.UniversalTime();
       
   370 	
       
   371 	iMtm->RestoreServiceAndSettingsL();
       
   372 	CSmsHeader& header = iMtm->SmsHeader();
       
   373 	
       
   374 	CSmsSettings* sendOptions = CSmsSettings::NewL();
       
   375 	CleanupStack::PushL( sendOptions );
       
   376 		
       
   377 	sendOptions->CopyL( iMtm->ServiceSettings() );
       
   378 	sendOptions->SetDelivery( ESmsDeliveryImmediately );
       
   379 
       
   380 	TSmsDataCodingScheme::TSmsAlphabet dataCoding = TSmsDataCodingScheme::ESmsAlphabet7Bit;
       
   381 	CRichText& msgBody = iMtm->Body();
       
   382 	HBufC* msgBodyBuf = HBufC::NewLC( msgBody.DocumentLength() );
       
   383 	TPtr16 ptrBody = msgBodyBuf->Des();
       
   384 	msgBody.Extract( ptrBody, 0, msgBody.DocumentLength() );
       
   385 	LOG(ptrBody);
       
   386 
       
   387 	for (TInt i = 0; i < ptrBody.Length(); i++)
       
   388 		{
       
   389 		if (IsUnicode( ptrBody[i] ))
       
   390 			{
       
   391 			LOGGER_WRITE_1( "Character %d is unicode", i );
       
   392 			dataCoding = TSmsDataCodingScheme::ESmsAlphabetUCS2;
       
   393 			break;
       
   394 			}
       
   395 		}
       
   396 
       
   397 	sendOptions->SetCharacterSet( dataCoding );
       
   398 	CleanupStack::PopAndDestroy( msgBodyBuf ); 
       
   399 		
       
   400 	header.SetSmsSettingsL( *sendOptions );
       
   401 	if( header.Message().ServiceCenterAddress().Length() == 0 )
       
   402 		{
       
   403 		LOGGER_WRITE( "header.Message().ServiceCenterAddress().Length() == 0" );    
       
   404 		
       
   405 		CSmsSettings* serviceSettings = &( iMtm->ServiceSettings() );
       
   406         
       
   407 		if (!serviceSettings->ServiceCenterCount())
       
   408 			{
       
   409 			LOGGER_WRITE("Cervice Center not found, could not send message");
       
   410 			User::Leave( KErrCompletion );
       
   411 			}
       
   412 		else
       
   413 			{
       
   414 			CSmsServiceCenter* sc =  &serviceSettings->GetServiceCenter( serviceSettings->DefaultServiceCenter() );
       
   415 			header.Message().SetServiceCenterAddressL( sc->Address() );	
       
   416 			}				
       
   417 		}
       
   418 
       
   419     const CMsvRecipientList& addrList = iMtm->AddresseeList();
       
   420     if ( addrList.Count() == 0 )
       
   421         {
       
   422 		LOGGER_WRITE( "SendSML: no recipient" );
       
   423         User::Leave( KErrGeneral );
       
   424         }
       
   425    
       
   426 	CMsvEntry* entry = &( iMtm->Entry() );
       
   427 	entry->ChangeL( msvEntry );	
       
   428 	iMtm->SaveMessageL();
       
   429 
       
   430 	CMsvEntrySelection* sel = new (ELeave) CMsvEntrySelection;
       
   431 	CleanupStack::PushL( sel );
       
   432 	sel->AppendL( entry->EntryId() );
       
   433 
       
   434 	TBuf8<1> dummy;
       
   435 	CMsvOperationActiveSchedulerWait* waiter = CMsvOperationActiveSchedulerWait::NewLC();
       
   436 	waiter->iStatus = KRequestPending;
       
   437 	CMsvOperation* op = iMtm->InvokeAsyncFunctionL( ESmsMtmCommandScheduleCopy, *sel, dummy, waiter->iStatus );
       
   438 	CleanupStack::PushL( op );
       
   439 	waiter->Start();
       
   440 	
       
   441 	LOGGER_WRITE_1( "InvokeAsyncFunctionL: status %d", waiter->iStatus.Int());
       
   442     
       
   443 	CleanupStack::PopAndDestroy( 4 ); // op, waiter, sel, sendOptions	
       
   444 	
       
   445 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::SendSML" );
       
   446     }
       
   447     
       
   448 // -----------------------------------------------------------------------------
       
   449 // CSmsAdapterMsvApi::HandleSessionEventL
       
   450 // -----------------------------------------------------------------------------
       
   451 //
       
   452 void CSmsAdapterMsvApi::HandleSessionEventL( TMsvSessionEvent aEvent, TAny*, TAny*, TAny* )
       
   453     {
       
   454     LOGGER_WRITE_1( "CSmsAdapterMsvApi::HandleSessionEventL: %d", aEvent);
       
   455     
       
   456     switch ( aEvent )
       
   457         {
       
   458         case EMsvCloseSession: // The client should immediately close the session with the Message Server.
       
   459         case EMsvServerTerminated: // The Message Server has been terminated.
       
   460                                    // All clients must close their sessions immediately. 
       
   461             {
       
   462             if (iSession)
       
   463                 {
       
   464 				SAFEDELETE( iMtm );
       
   465 				SAFEDELETE( iMtmReg );
       
   466                 SAFEDELETE( iSession );
       
   467                 }
       
   468             }   
       
   469             break;
       
   470         
       
   471         default:
       
   472             // Nothing is done
       
   473             break;
       
   474         } 
       
   475     }
       
   476 
       
   477 // -----------------------------------------------------------------------------
       
   478 // CSmsAdapterMsvApi::MsvSession
       
   479 // -----------------------------------------------------------------------------
       
   480 //
       
   481 CMsvSession* CSmsAdapterMsvApi::MsvSessionL()
       
   482 	{
       
   483 	if (!iSession)
       
   484 		{
       
   485 		User::Leave( KErrGeneral );
       
   486 		}
       
   487 	return iSession;
       
   488 	}
       
   489 
       
   490 // -----------------------------------------------------------------------------
       
   491 //	CSmsAdapterMsvApi::CleanFolderGetMsvIdsL
       
   492 // -----------------------------------------------------------------------------
       
   493 //
       
   494 CMsvEntrySelection* CSmsAdapterMsvApi::CleanFolderGetMsvIdsL(TMsvId aFolderId)
       
   495     {
       
   496     LOGGER_ENTERFN( "CMsvEntrySelection::CleanFolderGetMsvIdsL" );
       
   497     
       
   498     CMsvEntry* cEntry = iSession->GetEntryL( KMsvRootIndexEntryId );
       
   499     CleanupStack::PushL( cEntry );
       
   500     
       
   501     cEntry->SetEntryL( aFolderId );
       
   502     cEntry->SetSortTypeL( TMsvSelectionOrdering( KMsvNoGrouping, EMsvSortByNone, EFalse ) );
       
   503     CMsvEntrySelection* msvEntrySelection = cEntry->ChildrenWithMtmL( KUidMsgTypeSMS );
       
   504     
       
   505     CleanupStack::PopAndDestroy( cEntry );
       
   506     CleanupStack::PushL( msvEntrySelection );
       
   507     
       
   508     for (TInt i = 0; i < msvEntrySelection->Count(); i++)
       
   509         {
       
   510         CMsvEntry* entry = iSession->GetEntryL( msvEntrySelection->At(i) );
       
   511         CleanupStack::PushL( entry );
       
   512         TMsvEntry tEntry = entry->Entry();
       
   513         tEntry.SetReadOnly( EFalse );
       
   514         entry->ChangeL( tEntry );
       
   515         DeleteSML( msvEntrySelection->At(i) );
       
   516         CleanupStack::PopAndDestroy(); //entry
       
   517         }
       
   518         
       
   519     CleanupStack::Pop(msvEntrySelection);
       
   520     LOGGER_LEAVEFN( "CSmsAdapterMsvApi::CleanFolderGetMsvIdsL" );
       
   521     return msvEntrySelection;
       
   522     }
       
   523     
       
   524 // -----------------------------------------------------------------------------
       
   525 //	CSmsAdapterMsvApi::CleanFolderL
       
   526 // -----------------------------------------------------------------------------
       
   527 //
       
   528 void CSmsAdapterMsvApi::CleanFolderL(TMsvId aFolderId)
       
   529     {
       
   530     CMsvEntrySelection* msvEntrySelection = CleanFolderGetMsvIdsL(aFolderId);
       
   531     delete msvEntrySelection;
       
   532     }
       
   533     
       
   534 // -----------------------------------------------------------------------------
       
   535 // CSmsAdapterMsvApi::CleanUserFoldersL
       
   536 // Cleans all user folders from SMS messages
       
   537 // -----------------------------------------------------------------------------
       
   538 void CSmsAdapterMsvApi::CleanUserFoldersL() 
       
   539     {
       
   540     LOGGER_ENTERFN( "CSmsAdapterMsvApi::CleanUserFoldersL" );
       
   541     
       
   542     // Get the folder	
       
   543 	CMsvEntry* msvEntry = iSession->GetEntryL(KMsvMyFoldersEntryIdValue);
       
   544 	CleanupStack::PushL(msvEntry);
       
   545 	
       
   546 	// Find all of it's childs
       
   547 	CMsvEntrySelection* folders = msvEntry->ChildrenWithTypeL(KUidMsvFolderEntry);
       
   548 	CleanupStack::PopAndDestroy(msvEntry); 
       
   549 	CleanupStack::PushL(folders);
       
   550 	
       
   551 	TInt error(KErrNone);
       
   552 
       
   553     for (TInt index = 0; index < folders->Count(); index++)
       
   554         {
       
   555         TMsvId folderId = folders->At(index);
       
   556         
       
   557         if (folderId != KMsvMyFoldersTemplatesFolderId)
       
   558             {
       
   559             CleanFolderL(folderId);
       
   560             error = DeleteUserFolderL(folderId);
       
   561             if (error != KErrNone)
       
   562                 {
       
   563                 // Note: folder is not deleted if contains other message items (like MMS)
       
   564                 // In this case DeleteUserFolderL returns KErrInUse.
       
   565                 LOGGER_WRITE_1("iMsvApi->DeleteUserFolderL failed with %d", error);
       
   566                 }
       
   567             }
       
   568         }
       
   569 	
       
   570 	CleanupStack::PopAndDestroy(folders);
       
   571 	
       
   572 	// Delete also SMS messages directly under My Folders
       
   573 	CleanFolderL(KMsvMyFoldersEntryIdValue);
       
   574 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::CleanUserFoldersL" );
       
   575     }
       
   576    
       
   577 // -----------------------------------------------------------------------------
       
   578 //	CSmsAdapterMsvApi::DiskSpaceBelowCriticalLevelL
       
   579 // -----------------------------------------------------------------------------
       
   580 //
       
   581 TBool CSmsAdapterMsvApi::DiskSpaceBelowCriticalLevelL( TInt aDataSize )
       
   582     {
       
   583     return SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, aDataSize, iMessageDrive );
       
   584     }
       
   585 
       
   586 // -----------------------------------------------------------------------------
       
   587 //	CSmsAdapterMsvApi::UpdateSMStatusL
       
   588 // -----------------------------------------------------------------------------
       
   589 //
       
   590 void CSmsAdapterMsvApi::UpdateSMStatusL( TMsvId aSmId, CVMessageParser &aSm )
       
   591     {
       
   592 	LOGGER_ENTERFN( "CSmsAdapterMsvApi::UpdateSMStatusL" );
       
   593 
       
   594 	iMtm->SwitchCurrentEntryL( aSmId );
       
   595 
       
   596 	CMsvEntry& msvEntry = iMtm->Entry();
       
   597 	const TMsvEntry& oldEntry = msvEntry.Entry();
       
   598 	
       
   599 	TMsvEntry newEntry( oldEntry );
       
   600 
       
   601 	// STATUS
       
   602 	if (aSm.iStatus.Compare( KVMsgStatusUnread ) == 0)
       
   603 		{
       
   604 		newEntry.SetUnread( ETrue );
       
   605 		}
       
   606 	else if (aSm.iStatus.Compare( KVMsgStatusRead ) == 0)
       
   607 		{
       
   608 		newEntry.SetUnread( EFalse );
       
   609 		}
       
   610 	else 
       
   611 		{
       
   612 		LOGGER_WRITE( "Unexpected status, not updated" );
       
   613 		LOG( aSm.iStatus );
       
   614 		}
       
   615 
       
   616 	msvEntry.ChangeL( newEntry );
       
   617 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::UpdateSMStatusL" );
       
   618     }
       
   619 
       
   620 // -----------------------------------------------------------------------------
       
   621 //	CSmsAdapterMsvApi::DoUpdateSML
       
   622 // -----------------------------------------------------------------------------
       
   623 //
       
   624 void CSmsAdapterMsvApi::DoUpdateSML( TMsvId aSmId, CVMessageParser &aSm, TBool aNewSm )
       
   625     {
       
   626 	LOGGER_WRITE_1( "CSmsAdapterMsvApi::DoUpdateSML: %d", aSmId );
       
   627 	TInt i;
       
   628 
       
   629 	iMtm->SwitchCurrentEntryL( aSmId );
       
   630 	iMtm->LoadMessageL();	
       
   631 	CMsvEntry& msvEntry = iMtm->Entry();
       
   632 	const TMsvEntry& oldEntry = msvEntry.Entry();
       
   633 	TMsvEntry newEntry(oldEntry);
       
   634 
       
   635 	// Set message status
       
   636 	if (aSm.iStatus.Compare( KVMsgStatusUnread ) == 0)
       
   637 		{
       
   638 		newEntry.SetUnread( ETrue );
       
   639 		}
       
   640 	else if (aSm.iStatus.Compare( KVMsgStatusRead ) == 0)
       
   641 		{
       
   642 		newEntry.SetUnread( EFalse );
       
   643 		}
       
   644 	else if (aNewSm)
       
   645 		{
       
   646 		newEntry.SetUnread( EFalse ); // by default msg is not unread, if we don't know
       
   647 		}	
       
   648 
       
   649 	// Set time. store format is universal
       
   650 	if ( aSm.iUniversalTime.Int64() > 0 )
       
   651 		{
       
   652 		newEntry.iDate = aSm.iUniversalTime;
       
   653 		}
       
   654 	else if ( aSm.iHomeTime.Int64() > 0 )
       
   655 		{
       
   656 		newEntry.iDate = UniversalTimeFromHomeTime( aSm.iHomeTime );
       
   657 		}
       
   658 	else
       
   659 		{
       
   660 		newEntry.iDate.UniversalTime();
       
   661 		}	
       
   662 	
       
   663 	// ADDRESS INFORMATION
       
   664 	TMsvId parent = newEntry.Parent();
       
   665 	LOGGER_WRITE_1( "Parent is %d", parent );
       
   666 	
       
   667 	TBuf<KNameMaxLength> addrBookName;
       
   668 	
       
   669 	CSmsHeader& header = iMtm->SmsHeader();
       
   670 	
       
   671 	if (header.Type() == CSmsPDU::ESmsDeliver)
       
   672 		{
       
   673 		if (aSm.iSender.iName.Length() > 0)
       
   674 			{
       
   675 			newEntry.iDetails.Set( aSm.iSender.iName );
       
   676 			header.SetFromAddressL( aSm.iSender.iName );
       
   677 			}
       
   678 		else if (aSm.iSender.iNumber.Length() > 0)
       
   679 			{
       
   680 			FetchNameFromContactsL(aSm.iSender.iNumber, addrBookName);
       
   681 			if (addrBookName.Length() > 0)
       
   682 				{
       
   683 				newEntry.iDetails.Set( addrBookName );
       
   684 				header.SetFromAddressL( addrBookName );
       
   685 				}
       
   686 			else
       
   687 				{		
       
   688 				newEntry.iDetails.Set( aSm.iSender.iNumber );
       
   689 				header.SetFromAddressL( aSm.iSender.iNumber );
       
   690 				}
       
   691 			}
       
   692 		CSmsMessage& smsMsg = header.Message();
       
   693 		CSmsPDU& smsPdu = smsMsg.SmsPDU();
       
   694 		CSmsDeliver* smsDeliver = reinterpret_cast<CSmsDeliver*>( &smsPdu );	
       
   695 		smsDeliver->SetServiceCenterTimeStamp( newEntry.iDate );
       
   696 		}
       
   697 	else // message to be sent
       
   698 		{
       
   699 		if (!aNewSm)
       
   700 			{
       
   701 			const CMsvRecipientList& addrList = iMtm->AddresseeList();
       
   702     		TInt numOldAddr = addrList.Count();		
       
   703     		for (i = 0; i < numOldAddr; i++)
       
   704     			{
       
   705     			iMtm->RemoveAddressee( i );
       
   706     			}	
       
   707 			}		
       
   708 		
       
   709 		TInt numRecipients = aSm.iRecipients.Count();
       
   710 		
       
   711 		if (numRecipients > 0)
       
   712 			{
       
   713 			if (aSm.iRecipients[0].iName.Length() > 0)
       
   714 				{
       
   715 				newEntry.iDetails.Set( aSm.iRecipients[0].iName );
       
   716 				}
       
   717 			else if (aSm.iRecipients[0].iNumber.Length() > 0)
       
   718 				{
       
   719 				FetchNameFromContactsL(aSm.iRecipients[0].iNumber, addrBookName);
       
   720 				if (addrBookName.Length() > 0)
       
   721 					{
       
   722 					newEntry.iDetails.Set( addrBookName );
       
   723 					}
       
   724 				else
       
   725 					{
       
   726 					newEntry.iDetails.Set( aSm.iRecipients[0].iNumber );
       
   727 					}	
       
   728 				}
       
   729 			}
       
   730 			
       
   731 		for (i = 0; i < numRecipients; i++)
       
   732 			{
       
   733 			if (aSm.iRecipients[i].iNumber.Length() > 0)
       
   734 				{
       
   735 				if (aSm.iRecipients[i].iName.Length() > 0)
       
   736 					{
       
   737 					iMtm->AddAddresseeL( aSm.iRecipients[i].iNumber, aSm.iRecipients[i].iName );
       
   738 					}
       
   739 				else
       
   740 					{
       
   741 					FetchNameFromContactsL( aSm.iRecipients[i].iNumber, addrBookName );
       
   742 					iMtm->AddAddresseeL( aSm.iRecipients[i].iNumber, addrBookName );
       
   743 					}	
       
   744 				}
       
   745 			} 			
       
   746 		} // else
       
   747 
       
   748 	// MESSAGE BODY
       
   749 	LOGGER_WRITE( "Add message body" );
       
   750 	CRichText& mtmBody = iMtm->Body();
       
   751 	mtmBody.Reset();
       
   752 	aSm.LoadMessageBodyL( mtmBody );
       
   753 
       
   754     TBuf<KSmsDescriptionLength> description;
       
   755 	description.Zero();
       
   756 	if (aSm.iMessageBody)
       
   757 		{
       
   758 		TPtrC16 leftBody = aSm.iMessageBody->Left( KSmsDescriptionLength );	
       
   759 		description.Copy( leftBody );
       
   760 		
       
   761 		for (i = 0; i < description.Length(); i++)
       
   762 			{
       
   763 			if (description[i] == '\n' || description[i] == '\r')
       
   764 				{
       
   765 				description[i] = ' ';
       
   766 				}
       
   767 			}	
       
   768 		newEntry.iDescription.Set( description );
       
   769 		}
       
   770 		
       
   771 	newEntry.SetVisible( ETrue );
       
   772     newEntry.SetComplete( ETrue );
       
   773     newEntry.SetInPreparation( EFalse );    
       
   774     newEntry.SetSendingState( KMsvSendStateUponRequest );		
       
   775 
       
   776 	msvEntry.ChangeL( newEntry );
       
   777 
       
   778 	LOGGER_WRITE( "Save message" );
       
   779 	iMtm->SaveMessageL(); 
       
   780 	
       
   781 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::DoUpdateSML" );		
       
   782     }
       
   783 
       
   784 // -----------------------------------------------------------------------------
       
   785 // CSmsAdapterMsvApi::ValidUserFolder
       
   786 // -----------------------------------------------------------------------------
       
   787 //
       
   788 TBool CSmsAdapterMsvApi::ValidFolderL( TMsvId aFolder, TBool aOutboxValid )
       
   789     {
       
   790     TBool valid(EFalse);
       
   791     
       
   792     switch ( aFolder )
       
   793         {
       
   794         case KMsvGlobalInBoxIndexEntryId:
       
   795             valid = ETrue;  
       
   796             break;
       
   797         case KMsvDraftEntryId:
       
   798            valid = ETrue;  
       
   799            break;        
       
   800         case KMsvSentEntryId:
       
   801            valid = ETrue;  
       
   802            break;
       
   803         case KMsvGlobalOutBoxIndexEntryId:
       
   804             if (aOutboxValid)
       
   805                 {
       
   806                 valid = ETrue;
       
   807                 }
       
   808             break;
       
   809         case KMsvMyFoldersEntryIdValue:
       
   810            valid = ETrue;  
       
   811            break;             
       
   812         default:
       
   813             valid = FindUserFolderL(aFolder);
       
   814         }
       
   815     
       
   816     return valid;          
       
   817     }
       
   818 
       
   819 // -----------------------------------------------------------------------------
       
   820 // CSmsAdapterMsvApi::FindUserFolderL
       
   821 // -----------------------------------------------------------------------------
       
   822 //    
       
   823 TBool CSmsAdapterMsvApi::FindUserFolderL(TMsvId aFolder, TPtrC& aName, TTime& aDate)
       
   824     {
       
   825     TBool found(EFalse);
       
   826     
       
   827     CMsvEntry* entry = iSession->GetEntryL( KMsvMyFoldersEntryIdValue );
       
   828     CleanupStack::PushL( entry );
       
   829      
       
   830     CMsvEntrySelection* selection = entry->ChildrenL();
       
   831     CleanupStack::PushL( selection );
       
   832     
       
   833     TMsvId serviceId;
       
   834     TMsvEntry entryT;
       
   835 
       
   836     for( TInt i = 0; i < selection->Count(); i++ )
       
   837         {
       
   838         User::LeaveIfError( iSession->GetEntry( selection->At( i ), serviceId, entryT ) );
       
   839         
       
   840         if ( !entryT.Deleted() && entryT.iType == KUidMsvFolderEntry && entryT.Id() == aFolder )
       
   841             {
       
   842             found = ETrue;
       
   843             aDate = entryT.iDate;
       
   844             aName.Set(entryT.iDetails);
       
   845             break;
       
   846             }
       
   847         }
       
   848     
       
   849     CleanupStack::PopAndDestroy( 2 ); // entry, selection    
       
   850     
       
   851     return found;        
       
   852     }
       
   853     
       
   854 // -----------------------------------------------------------------------------
       
   855 // CSmsAdapterMsvApi::FindUserFolderL
       
   856 // -----------------------------------------------------------------------------
       
   857 //    
       
   858 TBool CSmsAdapterMsvApi::FindUserFolderL(TMsvId aFolder)
       
   859     {
       
   860     TPtrC name;
       
   861     TTime time;
       
   862     
       
   863     return FindUserFolderL(aFolder, name, time); 
       
   864     }    
       
   865     
       
   866 // -----------------------------------------------------------------------------
       
   867 // CSmsAdapterMsvApi::FindUserFolderL
       
   868 // -----------------------------------------------------------------------------
       
   869 //    
       
   870 TBool CSmsAdapterMsvApi::FindUserFolderL(const TDesC& aName, TMsvId& aFolder)
       
   871     {
       
   872     CMsvEntry* entry = iSession->GetEntryL( KMsvMyFoldersEntryIdValue );
       
   873     CleanupStack::PushL( entry );
       
   874      
       
   875     CMsvEntrySelection* selection = entry->ChildrenL();
       
   876     CleanupStack::PushL( selection );
       
   877     
       
   878     TBool found(EFalse);
       
   879     TMsvId serviceId;
       
   880     TMsvEntry entryT;
       
   881 
       
   882     for( TInt i = 0; i < selection->Count(); i++ )
       
   883         {
       
   884         User::LeaveIfError( iSession->GetEntry( selection->At( i ), serviceId, entryT ) );
       
   885         
       
   886         if ( !entryT.Deleted() && entryT.iType == KUidMsvFolderEntry && 
       
   887             aName.Compare(entryT.iDescription) == 0 )
       
   888             {
       
   889             found = ETrue;
       
   890             aFolder = entryT.Id();
       
   891             break;
       
   892             }
       
   893         }
       
   894     
       
   895     CleanupStack::PopAndDestroy( 2 ); // entry, selection    
       
   896     
       
   897     return found;           
       
   898     }
       
   899 
       
   900 // -----------------------------------------------------------------------------
       
   901 // CSmsAdapterMsvApi::IsUnicode
       
   902 // -----------------------------------------------------------------------------
       
   903 //
       
   904 TBool CSmsAdapterMsvApi::IsUnicode( const TUint16 aValue )
       
   905     {
       
   906 	if ( aValue > 0x7F && KSmsNonUnicodeChars().Locate( aValue ) < 0 )
       
   907 		{
       
   908 		LOGGER_WRITE_1( "IsUnicode: Found UC char %d", aValue );
       
   909 		return ETrue;
       
   910 		}
       
   911 	else
       
   912 		{
       
   913 		return EFalse;
       
   914 		}
       
   915     }
       
   916 
       
   917 // -----------------------------------------------------------------------------
       
   918 // CSmsAdapterMsvApi::MoveSML
       
   919 // Moves SM to another folder.
       
   920 // -----------------------------------------------------------------------------
       
   921 void CSmsAdapterMsvApi::MoveSML( TMsvId aSmId, TMsvId aParentId )
       
   922     {
       
   923     LOGGER_ENTERFN( "CSmsAdapterMsvApi::MoveSML starts" );
       
   924     
       
   925     if ( !ValidFolderL( aParentId ) )
       
   926         {
       
   927 		LOGGER_WRITE( "MoveSML: wrong folder" );
       
   928         User::Leave( KErrArgument );
       
   929         }
       
   930 	
       
   931 	// Find the parent
       
   932 	CMsvEntry* clientEntry( NULL );
       
   933 	
       
   934 	// Find this entry and put it to cleanup stack
       
   935 	clientEntry = iSession->GetEntryL( aSmId );
       
   936 	CleanupStack::PushL( clientEntry );
       
   937 	
       
   938 	// Check that this is a SMS message
       
   939 	TMsvEntry entryT = clientEntry->Entry();
       
   940 	if (entryT.iType != KUidMsvMessageEntry || entryT.iMtm != KUidMsgTypeSMS)
       
   941 	    {
       
   942 	    LOGGER_WRITE( "MoveSML: wrong entry type" );
       
   943 	    User::Leave( KErrArgument );
       
   944 	    }
       
   945    
       
   946 	// Find parent id, we'll be moving it's childs
       
   947 	TMsvId parentId = entryT.Parent();
       
   948 	
       
   949 	// Make sure that the parent has changed
       
   950 	if (parentId != aParentId)
       
   951 		{
       
   952 	    // Set parent as context
       
   953 	    clientEntry->SetEntryL( parentId );
       
   954 
       
   955 		// Move the child item to another branch, use temporary waiter object
       
   956 		CMsvOperationActiveSchedulerWait* w = CMsvOperationActiveSchedulerWait::NewLC();
       
   957 		CMsvOperation* op = clientEntry->MoveL( aSmId, aParentId, w->iStatus );
       
   958 		w->Start();
       
   959 		SAFEDELETE( op );
       
   960 		CleanupStack::PopAndDestroy();
       
   961 		
       
   962 		}
       
   963 	else
       
   964 		{
       
   965 		LOGGER_WRITE( "CSmsAdapterMsvApi::MoveSML, identical parents." );
       
   966 		}		  
       
   967 	
       
   968  	CleanupStack::PopAndDestroy(); // entry
       
   969  	
       
   970  	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::MoveSML" );
       
   971     }
       
   972 
       
   973 // -----------------------------------------------------------------------------
       
   974 // CSmsAdapterMsvApi::AddUserFolderL
       
   975 // Creates new user folder
       
   976 // -----------------------------------------------------------------------------        
       
   977 TInt CSmsAdapterMsvApi::AddUserFolderL( TMsvId& aFolder, const TDesC& aName )
       
   978     {
       
   979     LOGGER_ENTERFN( "CSmsAdapterMsvApi::AddUserFolderL" );
       
   980     LOG(aName);
       
   981         
       
   982     // Make sure that we are not going to add same folder twise
       
   983     TBool found(EFalse);
       
   984     found = FindUserFolderL(aName, aFolder);
       
   985     if ( found )
       
   986         {
       
   987         LOGGER_WRITE( "Folder already exists" );
       
   988         return KErrNone;
       
   989         } 
       
   990         
       
   991     CMsvEntry* entry = iSession->GetEntryL(KMsvMyFoldersEntryIdValue);
       
   992     CleanupStack::PushL( entry );
       
   993     
       
   994     TTime date;
       
   995     date.UniversalTime();      
       
   996     
       
   997     TMsvEntry folderEntry;
       
   998     folderEntry.iType = KUidMsvFolderEntry;
       
   999     folderEntry.iMtm = KUidMsvLocalServiceMtm;
       
  1000     folderEntry.iDetails.Set(aName);   
       
  1001     folderEntry.iServiceId = KMsvLocalServiceIndexEntryIdValue;
       
  1002     folderEntry.iSize = sizeof(folderEntry);
       
  1003     folderEntry.iDate = date;
       
  1004     folderEntry.SetStandardFolder(EFalse);
       
  1005     folderEntry.SetVisible(ETrue);
       
  1006     folderEntry.SetComplete(ETrue);
       
  1007     folderEntry.SetInPreparation(EFalse); 
       
  1008     folderEntry.SetReadOnly(EFalse);  
       
  1009     
       
  1010     entry->CreateL(folderEntry);
       
  1011     CleanupStack::PopAndDestroy(entry);
       
  1012     
       
  1013     aFolder = folderEntry.Id();
       
  1014     LOGGER_LEAVEFN( "CSmsAdapterMsvApi::AddUserFolderL" );
       
  1015     return KErrNone;
       
  1016     }
       
  1017     
       
  1018 // -----------------------------------------------------------------------------
       
  1019 // CSmsAdapterMsvApi::UpdateUserFolderL
       
  1020 // Updates user folder (changes name)
       
  1021 // -----------------------------------------------------------------------------    
       
  1022 TInt CSmsAdapterMsvApi::UpdateUserFolderL( TMsvId aFolder, const TDesC& aName )
       
  1023     {
       
  1024     LOGGER_ENTERFN( "CSmsAdapterMsvApi::UpdateUserFolderL" );
       
  1025     LOG(aName);
       
  1026     
       
  1027     CMsvEntry* entry = iSession->GetEntryL( aFolder );
       
  1028     CleanupStack::PushL( entry );
       
  1029     
       
  1030     TMsvEntry tEntry = entry->Entry();
       
  1031     
       
  1032     if ( tEntry.iType != KUidMsvFolderEntry )
       
  1033         {
       
  1034         CleanupStack::PopAndDestroy( entry );
       
  1035         LOGGER_WRITE( "No message folder" );
       
  1036         return KErrGeneral;
       
  1037         }
       
  1038        
       
  1039     tEntry.iDetails.Set(aName);   
       
  1040     tEntry.iDescription.Set(aName);
       
  1041     
       
  1042     entry->ChangeL(tEntry);
       
  1043     
       
  1044     CleanupStack::PopAndDestroy( entry );
       
  1045     LOGGER_LEAVEFN( "CSmsAdapterMsvApi::UpdateUserFolderL" );
       
  1046     return KErrNone;
       
  1047     } 
       
  1048 
       
  1049 // -----------------------------------------------------------------------------
       
  1050 // CSmsAdapterMsvApi::UniversalTimeFromHomeTime
       
  1051 // Converts local time to universal time.
       
  1052 // -----------------------------------------------------------------------------    
       
  1053 TTime CSmsAdapterMsvApi::UniversalTimeFromHomeTime( TTime aTime )
       
  1054 	{	
       
  1055 	TLocale locale;
       
  1056 	locale.Refresh();
       
  1057 	
       
  1058 	TTimeIntervalSeconds universalTimeOffset = locale.UniversalTimeOffset();
       
  1059 	aTime -= universalTimeOffset;
       
  1060 	
       
  1061 	if (locale.QueryHomeHasDaylightSavingOn())
       
  1062     	{
       
  1063     	TTimeIntervalHours daylightSaving( 1 );
       
  1064     	aTime -= daylightSaving;
       
  1065     	}	
       
  1066 	
       
  1067 	return aTime;
       
  1068 	}
       
  1069 
       
  1070 // -----------------------------------------------------------------------------
       
  1071 // CSmsAdapterMsvApi::HomeTimeFromUniversalTime
       
  1072 // Converts universal time to local time.
       
  1073 // -----------------------------------------------------------------------------    
       
  1074 TTime CSmsAdapterMsvApi::HomeTimeFromUniversalTime( TTime aTime )
       
  1075 	{
       
  1076 	TLocale locale;
       
  1077 	locale.Refresh();
       
  1078 	
       
  1079 	TTimeIntervalSeconds universalTimeOffset = locale.UniversalTimeOffset();
       
  1080 	aTime += universalTimeOffset;
       
  1081 	
       
  1082 	if (locale.QueryHomeHasDaylightSavingOn())
       
  1083     	{
       
  1084     	TTimeIntervalHours daylightSaving( 1 );
       
  1085     	aTime += daylightSaving;
       
  1086     	}
       
  1087 	
       
  1088 	return aTime;
       
  1089 	}
       
  1090 	
       
  1091 // -----------------------------------------------------------------------------
       
  1092 // CSmsAdapterMsvApi::FetchNameFromContactsL
       
  1093 // Searches contact name of given number from phone book
       
  1094 // If not found, empty descriptor is returned.
       
  1095 // -----------------------------------------------------------------------------    
       
  1096 void CSmsAdapterMsvApi::FetchNameFromContactsL(const TDesC& aNumber, TDes& aName)
       
  1097 	{
       
  1098 	LOGGER_ENTERFN( "CSmsAdapterMsvApi::FetchNameFromContactsL" );
       
  1099 	LOG(aNumber);
       
  1100 	
       
  1101 	aName.Zero(); 
       
  1102 	
       
  1103 	const TInt KNumDigitsToMatch(8);			
       
  1104 	CContactIdArray* contactIds = iContactsDb->MatchPhoneNumberL(aNumber, KNumDigitsToMatch);		
       
  1105 	if (contactIds->Count() != 1)
       
  1106 		{
       
  1107 		delete contactIds;
       
  1108 		return;
       
  1109 		}
       
  1110 	CleanupStack::PushL(contactIds);		
       
  1111  
       
  1112 	CContactItem* item = iContactsDb->ReadContactLC((*contactIds)[0]);
       
  1113 	CContactItemFieldSet& fieldSet = item->CardFields();
       
  1114 		
       
  1115 	TPtrC familyName;
       
  1116 	TPtrC givenName;
       
  1117 	TInt pos;
       
  1118 	
       
  1119 	pos = fieldSet.Find(KUidContactFieldFamilyName);
       
  1120 	if (pos >= 0)
       
  1121 		{
       
  1122 		CContactItemField& itemField=fieldSet[pos];
       
  1123 		if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
       
  1124 			{
       
  1125 			CContactTextField* textField = itemField.TextStorage();
       
  1126 			familyName.Set(textField->Text());
       
  1127 			}				
       
  1128 		}
       
  1129 	pos = fieldSet.Find(KUidContactFieldGivenName);
       
  1130 	if (pos >= 0)
       
  1131 		{
       
  1132 		CContactItemField& itemField=fieldSet[pos];
       
  1133 		if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
       
  1134 			{
       
  1135 			CContactTextField* textField = itemField.TextStorage();
       
  1136 			givenName.Set(textField->Text());
       
  1137 			}				
       
  1138 		}	
       
  1139 	
       
  1140 	TInt spaceLeft = aName.MaxLength();
       
  1141 	
       
  1142 	if (familyName.Length() <= spaceLeft)
       
  1143 		{
       
  1144 		aName.Append(familyName);
       
  1145 		aName.Trim();
       
  1146 		spaceLeft -= aName.Length();
       
  1147 		}
       
  1148 		
       
  1149 	if ((givenName.Length() + 1) <= spaceLeft)
       
  1150 		{
       
  1151 		aName.Append(' ');
       
  1152 		aName.Append(givenName);
       
  1153 		aName.Trim();
       
  1154 		}
       
  1155 		
       
  1156 	LOG(aName);	
       
  1157 	
       
  1158 	CleanupStack::PopAndDestroy(2); // item, contactIds
       
  1159 	LOGGER_LEAVEFN( "CSmsAdapterMsvApi::FetchNameFromContactsL" );
       
  1160 	}
       
  1161 		
       
  1162 // End of file