logsui/logsserviceextension/src/clogsextpresentityidfetcher.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Fetches the presentity id for a certain contact link.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <s32mem.h>
       
    19 
       
    20 //for service settings table
       
    21 #include <spsettings.h>
       
    22 #include <spentry.h>
       
    23 #include <spproperty.h>
       
    24 #include <spdefinitions.h>
       
    25 
       
    26 //Virtual Phonebook
       
    27 #include <TVPbkContactStoreUriPtr.h>
       
    28 #include <CVPbkContactManager.h>
       
    29 #include <CVPbkContactStoreUriArray.h>
       
    30 #include <MVPbkContactStoreList.h>
       
    31 #include <MVPbkContactStore.h>
       
    32 #include <MVPbkContactLink.h>
       
    33 #include <CVPbkFieldTypeRefsList.h>    
       
    34 #include <MVPbkStoreContact.h>    
       
    35 #include <MVPbkContactFieldData.h>
       
    36 #include <CVPbkContactFieldIterator.h>
       
    37 #include <MVPbkContactOperationBase.h>    
       
    38 #include <VPbkEng.rsg>  // url field resource id
       
    39 #include <CVPbkContactLinkArray.h>
       
    40 #include <MVPbkContactFieldUriData.h>
       
    41 
       
    42 #include "clogsextpresentityidfetcher.h"
       
    43 #include "mlogsextpresentityidfetcherobserver.h"
       
    44 #include "logsextconsts.h"
       
    45 #include "simpledebug.h"
       
    46 
       
    47 _LIT( KAt, "@" );
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // NewL
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CLogsExtPresentityIdFetcher* CLogsExtPresentityIdFetcher::NewL(
       
    54         const TUint32 aServiceId,
       
    55         const TDesC8& aCntLink,
       
    56         const TLogId aLogId,
       
    57         MLogsExtPresentityIdFetcherObserver& aObserver)
       
    58     {    
       
    59     CLogsExtPresentityIdFetcher* self = 
       
    60         CLogsExtPresentityIdFetcher::NewLC( aServiceId, 
       
    61                                             aCntLink, 
       
    62                                             aLogId, 
       
    63                                             aObserver );
       
    64     CleanupStack::Pop( self );
       
    65     return self;
       
    66     }
       
    67 
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // NewLC
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CLogsExtPresentityIdFetcher* CLogsExtPresentityIdFetcher::NewLC(
       
    74         const TUint32 aServiceId,
       
    75         const TDesC8& aCntLink,
       
    76         const TLogId aLogId, 
       
    77         MLogsExtPresentityIdFetcherObserver& aObserver )
       
    78     {
       
    79     _LOG("CLogsExtPresentityIdFetcher::NewLC(): begin" )
       
    80     CLogsExtPresentityIdFetcher* self = 
       
    81         new( ELeave ) CLogsExtPresentityIdFetcher( aServiceId, 
       
    82                                                    aLogId, 
       
    83                                                    aObserver );
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL( aCntLink );
       
    86     _LOG("CLogsExtPresentityIdFetcher::NewLC(): end" )
       
    87     return self;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Constructor
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CLogsExtPresentityIdFetcher::CLogsExtPresentityIdFetcher(
       
    95     const TUint32 aServiceId,
       
    96     const TLogId aLogId,    
       
    97     MLogsExtPresentityIdFetcherObserver& aObserver) : 
       
    98         iServiceId( aServiceId ),
       
    99         iFieldTypeResId( R_VPBK_FIELD_TYPE_IMPP ),
       
   100         iLogId( aLogId ),
       
   101         iObserver( &aObserver ),
       
   102         iCntStore( NULL ),
       
   103         iPresentityId( NULL ),
       
   104         iContactStoreId( NULL ),
       
   105         iContactManager( NULL ),
       
   106         iContactLinkArray( NULL ),
       
   107         iContactOperationBase( NULL )
       
   108     {    
       
   109     }
       
   110 
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // ConstructL 
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CLogsExtPresentityIdFetcher::ConstructL( const TDesC8& aCntLink )
       
   117     {
       
   118     iCntLink = aCntLink.AllocL();
       
   119     }
       
   120     
       
   121     
       
   122 // ---------------------------------------------------------------------------
       
   123 // Destructor
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 CLogsExtPresentityIdFetcher::~CLogsExtPresentityIdFetcher()
       
   127     {
       
   128     _LOG("CLogsExtPresentityIdFetcher::~CLogsExtPresenceIdFetcher(): begin" )
       
   129 
       
   130     if ( iCntStore )
       
   131         {
       
   132         // its safe to close the store even if it was never opened before
       
   133         iCntStore->Close( *this );
       
   134         }
       
   135     delete iCntLink;
       
   136     delete iPresentityId;
       
   137     delete iContactStoreId;    
       
   138     delete iContactLinkArray;
       
   139     delete iContactOperationBase;
       
   140     delete iContactManager;
       
   141 
       
   142     _LOG("CLogsExtPresentityIdFetcher::~CLogsExtPresenceIdFetcher(): end" )
       
   143     }
       
   144 
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // Starts the fetching process.
       
   148 // ---------------------------------------------------------------------------
       
   149 //
       
   150 TInt CLogsExtPresentityIdFetcher::Fetch()
       
   151     {
       
   152     _LOG("CLogsExtPresentityIdFetcher::Fetch: begin" )   
       
   153     
       
   154     TInt error;
       
   155     TRAP( error, DoPresenceIdFetchL() );
       
   156 
       
   157     _LOGP("CLogsExtPresentityIdFetcher::Fetch: end returns=%d", error )
       
   158     return error;   
       
   159     }
       
   160 
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // Initiates the actual fetching.
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 void CLogsExtPresentityIdFetcher::DoPresenceIdFetchL()
       
   167     {
       
   168     _LOG("CLogsExtPresentityIdFetcher::DoPresenceIdFetchL(): begin" )
       
   169     
       
   170     CSPSettings* spSettings = CSPSettings::NewLC();
       
   171     
       
   172     // get the contactstoreid from the service provider settings table
       
   173     GetContactStoreIdL( *spSettings );
       
   174     
       
   175     CleanupStack::PopAndDestroy( spSettings );
       
   176     spSettings = NULL;        
       
   177         
       
   178     CVPbkContactStoreUriArray* contactStoreUriArray = 
       
   179         CVPbkContactStoreUriArray::NewLC();
       
   180         
       
   181     const TVPbkContactStoreUriPtr storeUri( *iContactStoreId );          
       
   182     contactStoreUriArray->AppendL( storeUri ); 
       
   183     
       
   184     iContactManager = CVPbkContactManager::NewL( *contactStoreUriArray );
       
   185               
       
   186     iContactLinkArray = iContactManager->CreateLinksLC( iCntLink->Des() );
       
   187     
       
   188     CleanupStack::Pop(); // iContactLinkArray
       
   189     CleanupStack::PopAndDestroy( contactStoreUriArray );
       
   190     contactStoreUriArray = NULL;
       
   191         
       
   192     iContactManager->LoadContactStoreL( storeUri );    
       
   193     iCntStore = iContactManager->ContactStoresL().Find( storeUri );
       
   194     
       
   195     if ( iCntStore )
       
   196         {
       
   197         _LOG("this::DoPresenceIdFetchL(): open contact store" )
       
   198         // remember to call close when finished/cancelled/deleted! 
       
   199         iCntStore->OpenL( *this );   
       
   200         }
       
   201     else
       
   202         {
       
   203         _LOG("CLogsExtPresentityIdFetcher::DoPresenceIdFetchL(): error=-1" )
       
   204         User::Leave( KErrNotFound );
       
   205         }
       
   206    
       
   207     _LOG("CLogsExtPresentityIdFetcher::DoPresenceIdFetchL(): end" )
       
   208     }
       
   209 
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // Gets the contact store id of a certain service from the SPSettings Table.
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void CLogsExtPresentityIdFetcher::GetContactStoreIdL(
       
   216         CSPSettings& aSPSettings )
       
   217     {
       
   218     _LOG("CLogsExtPresentityIdFetcher::GetContactStoreIdL: begin")
       
   219     delete iContactStoreId;
       
   220     iContactStoreId = NULL;
       
   221     iContactStoreId = HBufC::NewL( KSPMaxDesLength );
       
   222     
       
   223     CSPProperty* spProperty = CSPProperty::NewLC();
       
   224     
       
   225     User::LeaveIfError( 
       
   226         aSPSettings.FindPropertyL( 
       
   227             ServiceId(),
       
   228             EPropertyContactStoreId,
       
   229             *spProperty ) );
       
   230     
       
   231     User::LeaveIfNull( spProperty );
       
   232     TPtr contactStoreIdPtr( iContactStoreId->Des() );
       
   233     User::LeaveIfError( spProperty->GetValue( contactStoreIdPtr ) );
       
   234 
       
   235     CleanupStack::PopAndDestroy( spProperty );
       
   236     spProperty = NULL;
       
   237       
       
   238     _LOG("CLogsExtPresentityIdFetcher::GetContactStoreIdL: end")
       
   239     }
       
   240 
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // Gets the contact store id of a certain service from the SPSettings Table.
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 void CLogsExtPresentityIdFetcher::GetPresentityIDFieldTypeL(
       
   247         CSPSettings& aSPSettings )
       
   248     {
       
   249     _LOG("CLogsExtPresentityIdFetcher::GetPresentityIDFieldTypeL: begin")
       
   250     
       
   251     CSPProperty* spProperty = CSPProperty::NewLC();
       
   252     
       
   253     User::LeaveIfError( 
       
   254         aSPSettings.FindPropertyL( 
       
   255             ServiceId(),
       
   256             ESubPropertyPresencePresentityIDFieldType,
       
   257             *spProperty ) );
       
   258     
       
   259     User::LeaveIfNull( spProperty );  
       
   260     User::LeaveIfError( spProperty->GetValue( iFieldTypeResId ) );
       
   261     
       
   262     CleanupStack::PopAndDestroy( spProperty );
       
   263     spProperty = NULL;
       
   264 
       
   265     _LOG("CLogsExtPresentityIdFetcher::GetPresentityIDFieldTypeL: end")
       
   266     }
       
   267 
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // Called when the operation is completed.
       
   271 //
       
   272 // A client has the operation as a member and it can delete the operation
       
   273 // instance in this function call. If the implementation of the store
       
   274 // calls the function from the operation instance it must not handle
       
   275 // any member data after calling it.
       
   276 // ---------------------------------------------------------------------------
       
   277 //    
       
   278 void CLogsExtPresentityIdFetcher::VPbkSingleContactOperationComplete(
       
   279         MVPbkContactOperationBase& /*aOperation*/,
       
   280         MVPbkStoreContact* aContact )
       
   281     {
       
   282     _LOG("CLogsExtPresentityIdFetcher::VPbkSingleContactOperationComplete...")
       
   283     _LOG("...begin")
       
   284     
       
   285     // delete the operation, since it is completed
       
   286     delete iContactOperationBase;
       
   287     iContactOperationBase = NULL;
       
   288     
       
   289     // Handles the retrieved contact
       
   290     TRAP_IGNORE( HandleRetrievedContactL( aContact ) )  
       
   291 
       
   292     _LOG("... VPbkSingleContactOperationComplete: end")
       
   293     }
       
   294 
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // Handles the completion of an operation.
       
   298 // ---------------------------------------------------------------------------
       
   299 //
       
   300 void CLogsExtPresentityIdFetcher::HandleRetrievedContactL(
       
   301         MVPbkStoreContact* aContact )
       
   302     {
       
   303     _LOG("CLogsExtPresentityIdFetcher::HandleRetrievedContactL: begin")
       
   304 
       
   305     aContact->PushL(); //Ownership transferred
       
   306    
       
   307     const MVPbkFieldType* fieldType = 
       
   308         iContactManager->FieldTypes().Find( iFieldTypeResId );
       
   309     if ( fieldType )
       
   310         {        
       
   311         _LOG("CLogsExtPresentityIdFetcher::HandleRetrievedContactL...")
       
   312         _LOG("...fieldType exists.")
       
   313         
       
   314         TPtrC fieldData( KNullDesC );
       
   315         CVPbkBaseContactFieldTypeIterator* itr = 
       
   316             CVPbkBaseContactFieldTypeIterator::NewLC( *fieldType, 
       
   317                 aContact->Fields() );
       
   318         
       
   319         // If several fields with same fieldId: 
       
   320         // use the first with right prefix
       
   321         TBool found( EFalse );
       
   322         while( itr->HasNext() && !found )
       
   323             {
       
   324             _LOG("CLogsExtPresentityIdFetcher::HandleRetrievedContactL...")
       
   325             _LOG("...field exists.")
       
   326             const MVPbkBaseContactField* field = itr->Next();
       
   327             
       
   328             // Check field type before casting.
       
   329             const MVPbkContactFieldData& contactFieldData = field->FieldData();
       
   330             if ( EVPbkFieldStorageTypeUri == contactFieldData.DataType() )
       
   331                 {
       
   332                 // get field data
       
   333                 const MVPbkContactFieldUriData& data = 
       
   334                     MVPbkContactFieldUriData::Cast( contactFieldData );
       
   335                 fieldData.Set( data.Uri() );
       
   336                 
       
   337                 CSPEntry* entry = CSPEntry::NewLC();
       
   338                 CSPSettings* settings = CSPSettings::NewLC();
       
   339                 TInt err = settings->FindEntryL( iServiceId, *entry );
       
   340                 if ( err )
       
   341                     {
       
   342                     // If service name not found leave with error code
       
   343                     User::Leave( err );
       
   344                     }
       
   345                 const TDesC& serviceName = entry->GetServiceName();
       
   346 
       
   347                 if ( 0 == fieldData.Find( serviceName ) )
       
   348                     {
       
   349 					// Service name found from position 0
       
   350                     found = ETrue;
       
   351                     
       
   352                     delete iPresentityId;
       
   353                     iPresentityId = NULL;
       
   354                     iPresentityId = fieldData.AllocL();
       
   355                     _LIT( KSkypeServiceName, "Skype" );
       
   356                     if ( KErrNotFound == iPresentityId->Find( KAt ) &&
       
   357                          serviceName.Compare( KSkypeServiceName ) )
       
   358                         {
       
   359                         // TODO: This "if ()" branch can be removed when meco automatically
       
   360                         // completes domain part
       
   361                         CSPProperty* property = CSPProperty::NewLC();
       
   362                             
       
   363                         User::LeaveIfError( settings->FindPropertyL( 
       
   364                                                 ServiceId(),
       
   365                                                 ESubPropertyPresenceAddrScheme,
       
   366                                                 *property ) );        
       
   367                         
       
   368                         User::LeaveIfNull( property );
       
   369                         
       
   370                         // property was found, read the value    
       
   371                         HBufC* domain = HBufC::NewLC( KSPMaxDesLength );
       
   372                         TPtr domainPtr( domain->Des() );
       
   373                         User::LeaveIfError( property->GetValue( domainPtr ) );
       
   374     
       
   375                         iPresentityId = iPresentityId->ReAllocL( 
       
   376                             iPresentityId->Length() + 
       
   377                             KAt().Length() + 
       
   378                             domain->Length() );
       
   379                         iPresentityId->Des().Append( KAt );
       
   380                         iPresentityId->Des().Append( *domain );
       
   381     
       
   382                         CleanupStack::PopAndDestroy( domain );
       
   383                         CleanupStack::PopAndDestroy( property );
       
   384                         }
       
   385                     _LOG("...presentityId==(nextline)")
       
   386                     _LOGDES(PresentityId())
       
   387                     iObserver->PresentityIdFetchDoneL( ServiceId(), 
       
   388                                                        PresentityId(), 
       
   389                                                        LogId() );
       
   390                     }
       
   391                 CleanupStack::PopAndDestroy( settings );
       
   392                 CleanupStack::PopAndDestroy( entry );
       
   393                 }
       
   394             }
       
   395         if ( !found )
       
   396             {            
       
   397             User::Leave( KErrNotFound );
       
   398             }
       
   399         CleanupStack::PopAndDestroy( itr );
       
   400         itr = NULL;
       
   401         } 
       
   402     else
       
   403         {        
       
   404         User::Leave( KErrNotFound ); 
       
   405         } 
       
   406     
       
   407     CleanupStack::PopAndDestroy( aContact ); // aContact
       
   408     aContact = NULL;
       
   409     
       
   410     _LOG("CLogsExtPresentityIdFetcher::HandleRetrievedContactL: end")
       
   411     }
       
   412 
       
   413 
       
   414 // ---------------------------------------------------------------------------
       
   415 // Called if the operation fails.
       
   416 //
       
   417 // A client has the operation as a member and it can delete the operation
       
   418 // instance in this function call. If the implementation of the store
       
   419 // calls the function from the operation instance it must not handle
       
   420 // any member data after calling it.
       
   421 // ---------------------------------------------------------------------------
       
   422 //
       
   423 void CLogsExtPresentityIdFetcher::VPbkSingleContactOperationFailed(
       
   424             MVPbkContactOperationBase& /*aOperation*/, 
       
   425             TInt aError )
       
   426     {
       
   427     _LOG("CLogsExtPresentityIdFetcher::VPbkSingleContactOperationFailed...")
       
   428     _LOGP("...begin : aError = %d", aError)
       
   429     
       
   430     // delete the operation, since it is completed
       
   431     delete iContactOperationBase;
       
   432     iContactOperationBase = NULL;
       
   433     
       
   434     _LOG("CLogsExtPresentityIdFetcher::VPbkSingleContactOperationFailed: end ")
       
   435     }
       
   436 
       
   437 
       
   438 // ---------------------------------------------------------------------------
       
   439 // Called when a contact store is ready to use.
       
   440 // ---------------------------------------------------------------------------
       
   441 //     
       
   442 void CLogsExtPresentityIdFetcher::StoreReady( 
       
   443         MVPbkContactStore& /*aContactStore*/ )
       
   444     {
       
   445     _LOG("CLogsExtPresentityIdFetcher::StoreReady: begin")
       
   446 
       
   447     
       
   448     const MVPbkContactLink& contactLink = iContactLinkArray->At( 0 );
       
   449     
       
   450     delete iContactOperationBase;
       
   451     iContactOperationBase = NULL;
       
   452     
       
   453     TRAP_IGNORE( iContactOperationBase = 
       
   454         iContactManager->RetrieveContactL( contactLink, *this ); )
       
   455     
       
   456     _LOG("CLogsExtPresentityIdFetcher::StoreReady: end")
       
   457     }
       
   458 
       
   459 
       
   460 // ---------------------------------------------------------------------------
       
   461 // Called when a contact store becomes unavailable.
       
   462 // ---------------------------------------------------------------------------
       
   463 //    
       
   464 void CLogsExtPresentityIdFetcher::StoreUnavailable( 
       
   465         MVPbkContactStore& /*aContactStore*/, 
       
   466         TInt /*aReason*/ )
       
   467     {
       
   468     _LOG("CLogsExtPresentityIdFetcher::StoreUnavailable: begin")
       
   469     _LOG("CLogsExtPresentityIdFetcher::StoreUnavailable: end")
       
   470     }
       
   471 
       
   472 
       
   473 // ---------------------------------------------------------------------------
       
   474 // Called when changes occur in the contact store.
       
   475 // ---------------------------------------------------------------------------
       
   476 //    
       
   477 void CLogsExtPresentityIdFetcher::HandleStoreEventL(
       
   478             MVPbkContactStore& /*aContactStore*/, 
       
   479             TVPbkContactStoreEvent /*aStoreEvent*/ )
       
   480     {
       
   481     _LOG("CLogsExtPresentityIdFetcher::HandleStoreEventL: begin")
       
   482     _LOG("CLogsExtPresentityIdFetcher::HandleStoreEventL: end")
       
   483     }
       
   484     
       
   485 // ---------------------------------------------------------------------------
       
   486 // Returns the Service id.
       
   487 // ---------------------------------------------------------------------------
       
   488 //  
       
   489 TUint32 CLogsExtPresentityIdFetcher::ServiceId()
       
   490     {    
       
   491     _LOGP("CLogsExtPresentityIdFetcher::ServiceId = %d", iServiceId)
       
   492     return iServiceId;
       
   493     }
       
   494 
       
   495 
       
   496 // ---------------------------------------------------------------------------
       
   497 // Returns the unique log event id,which the presentity id is fetched for.
       
   498 // ---------------------------------------------------------------------------
       
   499 //
       
   500 TLogId CLogsExtPresentityIdFetcher::LogId()
       
   501     {    
       
   502     _LOGP("CLogsExtPresentityIdFetcher::logid = %d", iLogId)
       
   503     return iLogId;
       
   504     }
       
   505 
       
   506 
       
   507 // ---------------------------------------------------------------------------
       
   508 // Returns the presentity id.
       
   509 // ---------------------------------------------------------------------------
       
   510 //
       
   511 const TDesC& CLogsExtPresentityIdFetcher::PresentityId()
       
   512     {    
       
   513     if( iPresentityId )
       
   514         {
       
   515          _LOGDES( *iPresentityId )
       
   516          return *iPresentityId;
       
   517         }    
       
   518     _LOG("CLogsExtPresentityIdFetcher::iPresentityId end")
       
   519     return KNullDesC();
       
   520     }