ccservices/cmsservices/bpas/src/bpas.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2007, 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:  This class handles presence related tasks for branding
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bamdesca.h>
       
    20 #include <utf.h>
       
    21 
       
    22 #include <contactpresencebrandids.h> 
       
    23 #include "bpasheaders.h"
       
    24 
       
    25 
       
    26 // ======== MEMBER FUNCTIONS ========
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // CBPAS::NewL
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 EXPORT_C CBPAS* CBPAS::NewL()
       
    33     {
       
    34     CBPAS* self = new( ELeave ) CBPAS( NULL );
       
    35     CleanupStack::PushL( self );
       
    36     self->ConstructL();
       
    37     CleanupStack::Pop( self );
       
    38     return self;
       
    39     }
       
    40    
       
    41 // ---------------------------------------------------------------------------
       
    42 // CBPAS::NewL
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C CBPAS* CBPAS::NewL( MBPASObserver* aObserver )
       
    46     {
       
    47     CBPAS* self = new( ELeave ) CBPAS( aObserver );
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop( self );
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CBPAS::CBPAS
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CBPAS::CBPAS( MBPASObserver* aObserver ): CActive( CActive::EPriorityStandard ),
       
    59 iObserver ( aObserver ),
       
    60 iState( CBPAS::EBPASIdle ),
       
    61 iSchedulerWait( NULL ),
       
    62 iSettings( NULL )
       
    63     {
       
    64     CActiveScheduler::Add( this );
       
    65     }
       
    66    
       
    67 // ---------------------------------------------------------------------------
       
    68 // CBPAS::~CBPAS
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 CBPAS::~CBPAS()
       
    72     {
       
    73     Cancel();
       
    74     if ( iContacts )
       
    75         {
       
    76         iContacts->Reset();
       
    77         delete iContacts;
       
    78         }
       
    79     delete iSchedulerWait;
       
    80     delete iSettings;
       
    81     delete iCacheReader;
       
    82     
       
    83     iObserver = NULL;
       
    84     
       
    85     if(iSPNames)
       
    86         {
       
    87         iSPNames->Reset();
       
    88         delete iSPNames;
       
    89         }
       
    90         
       
    91     iSPIds.Close();
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // CBPAS::ConstructL
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 void CBPAS::ConstructL( )
       
    99     {
       
   100     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ConstructL"));
       
   101 
       
   102     iContacts = new (ELeave) CPtrCArray(5);
       
   103     iSchedulerWait = new (ELeave)CActiveSchedulerWait();                            
       
   104     iSettings = CSPSettings::NewL();
       
   105     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS:: Settings created"));
       
   106     iCacheReader = MPresenceCacheReader2::CreateReaderL();
       
   107     iCacheReader->SetObserverForSubscribedNotifications(this);
       
   108     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS:: Cache created"));
       
   109     //Needed for AknIconUtils
       
   110     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ConstructL: Done."));
       
   111     
       
   112     iSPNames = new (ELeave) CDesC16ArraySeg(10);
       
   113     ReadSPSettingsToLocalStoresL();
       
   114     }
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // CBPAS::SetObserver
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 EXPORT_C void CBPAS::SetObserver( MBPASObserver* aObserver )
       
   121     {
       
   122     iObserver = aObserver;
       
   123     }
       
   124         
       
   125 // ---------------------------------------------------------------------------
       
   126 // CBPAS::GiveInfosL
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 EXPORT_C void CBPAS::GiveInfosL( MDesCArray& aContacts, 
       
   130                           RPointerArray<CBPASInfo>& aInfos )
       
   131     {            
       
   132     iContacts->CopyL( aContacts );           
       
   133     CreateInfosL( aInfos );
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // CBPAS::GiveInfosL
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 EXPORT_C void CBPAS::GiveInfosL( MDesCArray& aContacts )
       
   141     {
       
   142     if ( IsActive() )
       
   143         {
       
   144         User::Leave( KErrInUse );
       
   145         }
       
   146     if ( iObserver )
       
   147         {
       
   148         iContacts->CopyL( aContacts );
       
   149         iState = CBPAS::EBPASProcessing;
       
   150         SetActive();
       
   151         TRequestStatus* status = &iStatus;
       
   152         User::RequestComplete( status, KErrNone );
       
   153         iSchedulerWait->Start();
       
   154         }
       
   155     else
       
   156         {
       
   157         User::Leave( KErrNotSupported );
       
   158         }
       
   159     return;    
       
   160     }
       
   161     
       
   162 // ---------------------------------------------------------------------------
       
   163 // CBPAS::CreateInfosL
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 void CBPAS::CreateInfosL( RPointerArray<CBPASInfo>& aInfos )
       
   167     {
       
   168     // creating aInfos
       
   169     // presence
       
   170     SetPresenceInfosL( aInfos );    
       
   171     
       
   172     // service data
       
   173     SetServiceInfosL( aInfos );
       
   174     
       
   175     // branded data
       
   176     SetIconInfosL( aInfos );
       
   177     }
       
   178                           
       
   179 // ---------------------------------------------------------------------------
       
   180 // CBPAS::GivePresenceInfosL
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 void CBPAS::GivePresenceInfosL( MDesCArray& aContacts,
       
   184                                    RPointerArray<CBPASPresenceInfo>& aInfos )
       
   185     {
       
   186     ReadPresenceInfosL( aContacts, aInfos );
       
   187     }
       
   188    
       
   189 // ---------------------------------------------------------------------------
       
   190 // CBPAS::GiveIconInfoL
       
   191 // ---------------------------------------------------------------------------
       
   192 //
       
   193 CBPASIconInfo* CBPAS::GiveIconInfoL( const TDesC8& aImageId, 
       
   194                                      const TDesC8& aTextId )
       
   195     {
       
   196     CBPASIconInfo* iconInfo = CBPASIconInfo::NewL(aImageId);
       
   197     iconInfo->SetTextElementIdL(aTextId);
       
   198     return iconInfo;
       
   199     }
       
   200                                    
       
   201    
       
   202 // ---------------------------------------------------------------------------
       
   203 // CBPAS::CreateElementIdLC
       
   204 // ---------------------------------------------------------------------------
       
   205 //  
       
   206 HBufC8* CBPAS::CreateElementIdLC( EBPASPresenceType aPresenceType,
       
   207                                   EBPASElementType aElementType,
       
   208                                   const TDesC& aAttributeValue )
       
   209     {
       
   210     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS:: CreateElementIdLC start") ); 
       
   211     HBufC8* elementId = NULL;
       
   212     TInt length = aAttributeValue.Length(); 
       
   213     TPtrC8 presence;
       
   214     TPtrC8 element;
       
   215     switch ( aPresenceType )
       
   216         {
       
   217         case EBPASPersonPresence:
       
   218             {
       
   219             length += KCPBrandElementIdPerson().Length();
       
   220             presence.Set( KCPBrandElementIdPerson );
       
   221             break;
       
   222             }
       
   223         case EBPASDevicePresence:
       
   224             {
       
   225             length += KCPBrandElementIdDevice().Length();
       
   226             presence.Set( KCPBrandElementIdDevice );
       
   227             break;
       
   228             }
       
   229         case EBPASServicePresence:
       
   230             {
       
   231             length += KCPBrandElementIdService().Length();
       
   232             presence.Set( KCPBrandElementIdService );
       
   233             break;
       
   234             }
       
   235         default:
       
   236             {
       
   237             User::Leave( KErrArgument );
       
   238             }
       
   239         }
       
   240 
       
   241     switch ( aElementType )
       
   242         {
       
   243         case EBPASElementImage:
       
   244             {
       
   245             length += KCPBrandElementIdImage().Length();
       
   246             element.Set( KCPBrandElementIdImage );
       
   247             break;
       
   248             }
       
   249         case EBPASElementText:
       
   250             {
       
   251             length += KCPBrandElementIdText().Length();
       
   252             element.Set( KCPBrandElementIdText );
       
   253             break;
       
   254             }
       
   255         default:
       
   256             {
       
   257             User::Leave( KErrArgument );
       
   258             }
       
   259         }
       
   260     elementId = HBufC8::NewLC( length ); // << elementId
       
   261 
       
   262     TPtr8 elementIdPtr = elementId->Des();
       
   263     elementIdPtr.Append( presence );
       
   264     elementIdPtr.Append( aAttributeValue );
       
   265     elementIdPtr.Append( element );
       
   266     elementIdPtr.LowerCase();   
       
   267     
       
   268 #ifdef _DEBUG
       
   269 	HBufC8* buf = HBufC8::NewLC ( elementIdPtr.Length ());
       
   270 	buf->Des().Copy ( elementIdPtr );
       
   271 	TPtrC8 numberDesc( buf->Des ());
       
   272     CMS_DP(KBPasLoggerFile, CMS_L8( "CBPAS:: CreateElementIdLC    %S" ), &numberDesc );
       
   273 	CleanupStack::PopAndDestroy (); //buf
       
   274 #endif
       
   275 
       
   276  
       
   277     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS:: CreateElementIdLC end") );   
       
   278     return elementId;
       
   279     }
       
   280    
       
   281 // ---------------------------------------------------------------------------
       
   282 // CBPAS::RunL
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 void CBPAS::RunL()
       
   286     {
       
   287     switch ( iState )
       
   288         {
       
   289         case CBPAS::EBPASProcessing:
       
   290             {
       
   291             RPointerArray<CBPASInfo> infos;
       
   292             CleanupClosePushL( infos ); // << infos
       
   293             TInt err( KErrNone );
       
   294             TRAP( err, CreateInfosL( infos ) );
       
   295             if ( KErrNone != err )
       
   296                 {
       
   297                 CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::RunL CreateInfosL error=%d"), err );
       
   298                 }
       
   299             TRAP( err, iObserver->HandleInfosL(KErrNone, infos) );
       
   300             if ( KErrNone != err )
       
   301                 {
       
   302                 CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::RunL HandleInfosL error=%d"), err );       
       
   303                 }
       
   304             iState = CBPAS::EBPASIdle;
       
   305             CleanupStack::Pop();        // >> infos
       
   306             infos.ResetAndDestroy();
       
   307             iSchedulerWait->AsyncStop();
       
   308             break;
       
   309             }
       
   310         default:
       
   311             {
       
   312             break;
       
   313             }
       
   314         }
       
   315     }       
       
   316        
       
   317 // ---------------------------------------------------------------------------
       
   318 // CBPAS::RunError
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 TInt CBPAS::RunError( TInt aError )
       
   322     {
       
   323     return aError;
       
   324     }
       
   325    
       
   326 // ---------------------------------------------------------------------------
       
   327 // CBPAS::DoCancel
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 void CBPAS::DoCancel()
       
   331     {
       
   332     }
       
   333     
       
   334 // ---------------------------------------------------------------------------
       
   335 // CBPAS::ReadPresenceInfosL
       
   336 // Gets the presence info from presence cache and extracts the supported 
       
   337 // attributes. The following attributes are supported:
       
   338 //  - Availability
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 void CBPAS::ReadPresenceInfosL( MDesCArray& aContacts,
       
   342                                 RPointerArray<CBPASPresenceInfo>& aInfos )
       
   343     {
       
   344     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ReadPresenceInfosL start"));
       
   345     TInt count( aContacts.MdcaCount() );
       
   346     for ( TInt i(0); i < count; i++ )
       
   347         {
       
   348         ReadAndCreatePresenceInfoL( aContacts.MdcaPoint(i), aInfos );
       
   349         }
       
   350     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ReadPresenceInfosL end"));
       
   351     }
       
   352 
       
   353 // ---------------------------------------------------------------------------
       
   354 // CBPAS::ReadServiceDataLC
       
   355 // ---------------------------------------------------------------------------
       
   356 //
       
   357 CBPASServiceInfo* CBPAS::ReadServiceDataLC(TInt aServiceId)
       
   358     {
       
   359     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ReadServiceDataLC start") );
       
   360     TInt err(KErrNone);
       
   361     
       
   362     // note that service id is already verified by caller
       
   363     CBPASServiceInfo* serviceInfo = CBPASServiceInfo::NewLC(aServiceId);
       
   364     
       
   365     // Now we are searching for other properties and if found we will set them
       
   366     // to our serviceInfo
       
   367     
       
   368     // Read ServiceType
       
   369     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ReadServiceDataLC read serviceType") ); 
       
   370     CSPProperty* serviceType = CSPProperty::NewLC();   // << serviceType   
       
   371     err = iSettings->FindPropertyL( aServiceId, EPropertyServiceAttributeMask, *serviceType );
       
   372     TInt type( KErrNotFound );
       
   373     TInt errInType(KErrNone);
       
   374     if ( KErrNone == err )
       
   375         { 
       
   376         errInType = serviceType->GetValue( type );
       
   377         CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ReadServiceDataLC serviceType value = %d, err = %d"), type, errInType );
       
   378         if(errInType == KErrNone)
       
   379             serviceInfo->SetServiceType(type);        
       
   380         }
       
   381     CleanupStack::PopAndDestroy( serviceType ); // >>> serviceType
       
   382     
       
   383     // Read brand id
       
   384     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ReadServiceDataLC read brandId") );
       
   385     CSPProperty* brandId = CSPProperty::NewLC();   // << brandId   
       
   386     err = iSettings->FindPropertyL( aServiceId, EPropertyBrandId, *brandId );
       
   387     if ( KErrNone == err )
       
   388         {
       
   389         RBuf brandValue;
       
   390         brandValue.Create( KCPBrandElementIdMaxLength );
       
   391         CleanupClosePushL( brandValue ); // << brandValue
       
   392         
       
   393         RBuf8 brandValue8;
       
   394         brandValue8.Create( KCPBrandElementIdMaxLength );
       
   395         CleanupClosePushL( brandValue8 ); // << brandValue
       
   396 
       
   397         err = brandId->GetValue( brandValue );
       
   398         if(err == KErrNone)
       
   399             {
       
   400             CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ReadServiceDataLC create service info") );
       
   401             err = CnvUtfConverter::ConvertFromUnicodeToUtf8(brandValue8, brandValue);
       
   402             if(err==KErrNone)
       
   403                 serviceInfo->SetBrandIdL(brandValue8);
       
   404             }
       
   405         CleanupStack::PopAndDestroy(); // >>> brandValue8
       
   406         CleanupStack::PopAndDestroy(); // >>> brandValue
       
   407         }
       
   408     CleanupStack::PopAndDestroy( brandId ); // >>> brandId
       
   409     
       
   410     
       
   411     //Read language ID
       
   412     CSPProperty* langId = CSPProperty::NewLC();   // << langId   
       
   413     err = iSettings->FindPropertyL( aServiceId, EPropertyBrandLanguage, *langId );
       
   414     if ( KErrNone == err)
       
   415         {
       
   416         RBuf langValue;
       
   417         langValue.Create( KCPBrandElementIdMaxLength );
       
   418         CleanupClosePushL( langValue ); // << langValue
       
   419         err = langId->GetValue( langValue );
       
   420         CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ReadServiceDataLC create language ID err = %d"), err );
       
   421         
       
   422         // converting language id to integer
       
   423         TInt langId;
       
   424         TLex lex( langValue );
       
   425         err = lex.Val(langId);
       
   426 
       
   427         if(err==KErrNone)
       
   428             serviceInfo->SetLanguageId(langId);
       
   429         CleanupStack::PopAndDestroy(); // >>> langValue              
       
   430         }
       
   431     CleanupStack::PopAndDestroy( langId ); // >>> langId
       
   432     
       
   433     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::ReadServiceDataLC end serviceInfo = %d"), serviceInfo );
       
   434     return serviceInfo;
       
   435     }
       
   436 
       
   437 // ---------------------------------------------------------------------------
       
   438 // CBPAS::SetServiceInfosL
       
   439 // ---------------------------------------------------------------------------
       
   440 //
       
   441 void CBPAS::SetServiceInfosL( RPointerArray<CBPASInfo>& aInfos )
       
   442     {
       
   443     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::SetServiceInfosL start") );
       
   444     TInt infoCount( aInfos.Count() );
       
   445     CBPASServiceInfo* serviceInfo(NULL);
       
   446     TInt serviceId(KErrNotFound);
       
   447     for( TInt i(0); i < infoCount; i++ )
       
   448         {
       
   449         // create service info only if service id is found from sp table
       
   450         if(GetServiceId(serviceId, (aInfos[i])->Identity()))
       
   451             {
       
   452             serviceInfo = ReadServiceDataLC(serviceId);
       
   453             
       
   454             if(serviceInfo) // if service info exists
       
   455                 {
       
   456                 (aInfos[i])->SetServiceInfoL(serviceInfo);
       
   457                 CleanupStack::Pop(serviceInfo); // ownership transferred
       
   458                 serviceInfo = NULL;
       
   459                 }
       
   460             }
       
   461         }
       
   462     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::SetServiceInfosL end") );
       
   463     }
       
   464 
       
   465    
       
   466 // ---------------------------------------------------------------------------
       
   467 // CBPAS::SetPresenceInfosL
       
   468 // ---------------------------------------------------------------------------
       
   469 //
       
   470 void CBPAS::SetPresenceInfosL( RPointerArray<CBPASInfo>& aInfos )
       
   471     {
       
   472     RPointerArray<CBPASPresenceInfo> presInfos;
       
   473     CleanupClosePushL( presInfos ); // << presInfos
       
   474     ReadPresenceInfosL( *iContacts, presInfos );
       
   475     CBPASInfo* info(NULL);
       
   476     
       
   477     TInt presInfoCount( presInfos.Count() );
       
   478     for ( TInt i( presInfoCount - 1 ); 0 <= i; i-- )
       
   479         {
       
   480          info = CBPASInfo::NewLC(iContacts->MdcaPoint(i));   // << info
       
   481          info->SetPresenceInfoL( presInfos[i] );
       
   482          aInfos.Append(info);
       
   483          CleanupStack::Pop( info );              // >> info
       
   484          info = NULL;
       
   485          }
       
   486         
       
   487     CleanupStack::Pop(); // >> presInfos
       
   488     presInfos.Close(); // ownerships are transfered
       
   489     }
       
   490     
       
   491 // ---------------------------------------------------------------------------
       
   492 // CBPAS::SetIconInfosL
       
   493 // ---------------------------------------------------------------------------
       
   494 //
       
   495 void CBPAS::SetIconInfosL( RPointerArray<CBPASInfo>& aInfos )
       
   496     {
       
   497     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::SetIconInfosL start") );
       
   498     TInt infoCount( aInfos.Count() );
       
   499     CBPASIconInfo* iconInfo(NULL);
       
   500     for( TInt i(0); i < infoCount; i++ )
       
   501         {
       
   502         
       
   503         // process only if service and presence infos are valid
       
   504         if( (aInfos[i])->HasPresence()  && (aInfos[i])->ServiceInfo() )
       
   505             {
       
   506             TPtrC attrValue = aInfos[i]->PresenceInfo()->AvailabilityText();
       
   507             
       
   508             // element id
       
   509             // NOTE Hardcoded EBPASPersonPresence here is ugly
       
   510             HBufC8* imageId =  CreateElementIdLC( EBPASPersonPresence, // << imageId
       
   511                                                   EBPASElementImage,
       
   512                                                   attrValue );
       
   513                                                   
       
   514             HBufC8* textId =  CreateElementIdLC( EBPASPersonPresence, // << textId
       
   515                               EBPASElementText,
       
   516                               attrValue );
       
   517             
       
   518             // image and text ids must have been created other leave would have happen
       
   519             iconInfo = CBPASIconInfo::NewLC(*imageId);
       
   520             iconInfo->SetTextElementIdL(*textId);
       
   521             aInfos[i]->SetIconInfoL( iconInfo ); //ownership tranfered
       
   522             CleanupStack::Pop(iconInfo);
       
   523             CleanupStack::PopAndDestroy(textId);
       
   524             CleanupStack::PopAndDestroy(imageId);   
       
   525             }
       
   526         }
       
   527     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::SetIconInfosL end") );
       
   528     }
       
   529          
       
   530        
       
   531 // ---------------------------------------------------------------------------
       
   532 // CBPAS::ReadAndCreatePresenceInfoL
       
   533 // ---------------------------------------------------------------------------
       
   534 //    
       
   535 void CBPAS::ReadAndCreatePresenceInfoL( const TDesC& aContact,
       
   536                                         RPointerArray<CBPASPresenceInfo>& aInfos )
       
   537     {
       
   538     CMS_DP(KBPasLoggerFile, CMS_L( "____CBPAS::ReadAndCreatePresenceInfoL start"));
       
   539     TInt serviceId(KErrNotFound);
       
   540 
       
   541     MPresenceBuddyInfo2* presInfo(NULL);    
       
   542     
       
   543     if (aContact.Length() == 0)
       
   544         User::Leave(KErrArgument);
       
   545     
       
   546     // get the service id from xsp prefix
       
   547     if (GetServiceId(serviceId, aContact))
       
   548         {
       
   549         //service id is found by xsp id      
       
   550         presInfo = iCacheReader->PresenceInfoLC(aContact);        
       
   551         }
       
   552     
       
   553     CMS_DP(KBPasLoggerFile, CMS_L( "________CBPAS::ReadPresenceInfosL presInfo = %d"),presInfo);
       
   554   
       
   555     CBPASPresenceInfo* PresBpasInfo(NULL);
       
   556     
       
   557     if (presInfo)
       
   558         { 
       
   559         MPresenceBuddyInfo2::TAvailabilityValues value = presInfo->Availability();
       
   560         TPtrC presTextValue = presInfo->AvailabilityText();
       
   561         if ( value != MPresenceBuddyInfo2::EUnknownAvailability)
       
   562             {
       
   563             PresBpasInfo = CBPASPresenceInfo::NewLC(presTextValue, value);
       
   564             }
       
   565         else
       
   566             {
       
   567             // nothing
       
   568             }
       
   569         }
       
   570     // append NULL presence info if its not found, just to keep compatiblity with current code
       
   571     aInfos.Append( PresBpasInfo );
       
   572     
       
   573     if (PresBpasInfo)
       
   574         CleanupStack::Pop(PresBpasInfo);// ownership transfered
       
   575     if (presInfo)
       
   576         CleanupStack::PopAndDestroy(); // >>>  presInfo
       
   577 
       
   578     
       
   579     CMS_DP(KBPasLoggerFile, CMS_L( "_____CBPAS::ReadAndCreatePresenceInfoL end"));
       
   580     }
       
   581 
       
   582 
       
   583 // ---------------------------------------------------------------------------
       
   584 // CBPAS::SubscribePresenceInfosL
       
   585 // ---------------------------------------------------------------------------
       
   586 //
       
   587 EXPORT_C void CBPAS::SubscribePresenceInfosL( MDesCArray& aContacts)
       
   588     {
       
   589     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::SubscribePresenceInfosL: start."));
       
   590     TInt contactsCount = aContacts.MdcaCount();
       
   591     CMS_DP(KBPasLoggerFile, CMS_L( "____contactsCount=%d"), contactsCount );
       
   592     TInt ignoredErr(KErrNone);
       
   593     TInt serviceId(KErrNotFound);
       
   594     
       
   595     for (TInt i=0;i<contactsCount;i++)
       
   596         {
       
   597         if (GetServiceId(serviceId, aContacts.MdcaPoint(i)))
       
   598             {
       
   599             iCacheReader->SubscribePresenceBuddyChangeL( aContacts.MdcaPoint(i) );
       
   600             }
       
   601         }   
       
   602     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::SubscribePresenceInfosL: Done ignoredErr(%d)"),ignoredErr);    
       
   603     }
       
   604     
       
   605 // ---------------------------------------------------------------------------
       
   606 // CBPAS::UnSubscribePresenceInfosL
       
   607 // ---------------------------------------------------------------------------
       
   608 //
       
   609 EXPORT_C void CBPAS::UnSubscribePresenceInfosL( MDesCArray& aContacts)
       
   610     {
       
   611     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::UnSubscribePresenceInfosL: start."));
       
   612     TInt contactsCount = aContacts.MdcaCount();
       
   613     CMS_DP(KBPasLoggerFile, CMS_L( "____contactsCount=%d"), contactsCount );
       
   614     TInt serviceId(KErrNotFound);
       
   615     TPtrC subUri(KNullDesC);
       
   616     
       
   617     for(TInt i=0;i<contactsCount;i++)
       
   618         {
       
   619         if (GetServiceId(serviceId, aContacts.MdcaPoint(i)))
       
   620             {
       
   621             iCacheReader->UnSubscribePresenceBuddyChangeL( aContacts.MdcaPoint(i) );
       
   622             }
       
   623          }  
       
   624     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::UnSubscribePresenceInfosL: Done."));
       
   625     }
       
   626 
       
   627 // ---------------------------------------------------------------------------
       
   628 // CBPAS::GetServiceId
       
   629 // ---------------------------------------------------------------------------
       
   630 //
       
   631 TBool CBPAS::GetServiceId(TInt& aServiceId, TPtrC aUri)
       
   632     {
       
   633     CMS_DP(KBPasLoggerFile, CMS_L( "____CBPAS::GetServiceId: start"));
       
   634     TBool ret(EFalse);
       
   635     _LIT(KColon, ":");
       
   636     TInt pos = aUri.Find(KColon);
       
   637     if (pos>0) // if colon found and there is something before colon, i.e. xsp id
       
   638         {
       
   639         aServiceId = XspToServiceId(aUri.Left(pos));
       
   640         if (aServiceId != KErrNotFound) // if service id found
       
   641             {
       
   642             ret = ETrue;
       
   643             }
       
   644         else
       
   645             {
       
   646             // Service not found
       
   647             }
       
   648         }
       
   649         
       
   650     CMS_DP(KBPasLoggerFile, CMS_L( "____CBPAS::GetServiceId: end (%d)"), ret);
       
   651     return ret;
       
   652     }
       
   653 
       
   654 // ---------------------------------------------------------------------------
       
   655 // CBPAS::XspToServiceId
       
   656 // ---------------------------------------------------------------------------
       
   657 //
       
   658 TInt CBPAS::XspToServiceId(TPtrC aXsp)
       
   659     {
       
   660     CMS_DP(KBPasLoggerFile, CMS_L( "____CBPAS::XspToServiceIds: start"));
       
   661     TInt pos, ret(KErrNotFound);
       
   662     TInt count = iSPNames->Count();
       
   663     
       
   664     if(count)
       
   665         {
       
   666         iSPNames->Find(aXsp, pos);
       
   667         if(pos<count) // if xsp id found
       
   668             ret = iSPIds[pos]; // return the service id at that location
       
   669         }
       
   670     
       
   671     if(ret==KErrNotFound) //update local spsettings and try again
       
   672         {
       
   673         TRAP_IGNORE( ReadSPSettingsToLocalStoresL() );
       
   674         count = iSPNames->Count();
       
   675         if(count)
       
   676             {
       
   677             iSPNames->Find(aXsp, pos);
       
   678             if(pos<count) // if xsp id found
       
   679                 ret = iSPIds[pos]; // return the service id at that location
       
   680             }
       
   681         }
       
   682     
       
   683     CMS_DP(KBPasLoggerFile, CMS_L( "____CBPAS::XspToServiceIds id(%d): Done"),ret);    
       
   684     return ret;    
       
   685     }
       
   686 
       
   687 // ---------------------------------------------------------------------------
       
   688 // CBPAS::VerifyServiceIdL
       
   689 // ---------------------------------------------------------------------------
       
   690 //
       
   691 TInt CBPAS::VerifyServiceIdL(TInt aServiceId)
       
   692     {
       
   693     CMS_DP(KBPasLoggerFile, CMS_L( "____CBPAS::VerifyServiceId: start"));
       
   694     
       
   695     TInt index = iSPIds.Find(aServiceId);
       
   696     
       
   697     //give another try if service not found, update local sp stores
       
   698     if(index==KErrNotFound)
       
   699         {
       
   700         CMS_DP(KBPasLoggerFile, CMS_L( "______Trying again!"));
       
   701         ReadSPSettingsToLocalStoresL();
       
   702         index = iSPIds.Find(aServiceId);
       
   703         }
       
   704     
       
   705     CMS_DP(KBPasLoggerFile, CMS_L( "____CBPAS::VerifyServiceId: end"));
       
   706     return index;
       
   707     }
       
   708 
       
   709 // ---------------------------------------------------------------------------
       
   710 // CBPAS::ReadSPSettingsToLocalStoresL()
       
   711 // ---------------------------------------------------------------------------
       
   712 //
       
   713 void CBPAS::ReadSPSettingsToLocalStoresL()
       
   714     {
       
   715     CMS_DP(KBPasLoggerFile, CMS_L( "____CBPAS::ReadSPSettingsToLocalStoresL: start"));
       
   716     
       
   717     iSPIds.Close();
       
   718     iSPNames->Reset();
       
   719 
       
   720     TInt ignoredError = iSettings->FindServiceIdsL(iSPIds);
       
   721     ignoredError =  iSettings->FindServiceNamesL(iSPIds, *iSPNames);
       
   722     
       
   723     CMS_DP(KBPasLoggerFile, CMS_L( "____CBPAS::ReadSPSettingsToLocalStoresL: end"));
       
   724     }
       
   725     
       
   726 // ---------------------------------------------------------------------------
       
   727 // CBPAS::HandlePresenceReadL()
       
   728 // ---------------------------------------------------------------------------
       
   729 //
       
   730 void CBPAS::HandlePresenceReadL(TInt /*aErrorCode*/,
       
   731         RPointerArray<MPresenceBuddyInfo2>&  /*aPresenceBuddyInfoList*/)
       
   732     {
       
   733     //intentionally not implemented
       
   734     return;
       
   735     }
       
   736     
       
   737 // ---------------------------------------------------------------------------
       
   738 // CBPAS::HandlePresenceNotificationL()
       
   739 // ---------------------------------------------------------------------------
       
   740 //
       
   741 void CBPAS::HandlePresenceNotificationL( TInt aErrorCode,
       
   742     MPresenceBuddyInfo2* aPresenceBuddyInfo )
       
   743     {
       
   744     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::HandlePresenceNotificationL: start"));
       
   745     
       
   746     if( (aErrorCode == KErrNone) && (aPresenceBuddyInfo) )
       
   747                                     
       
   748         {
       
   749         CleanupStack::PushL(aPresenceBuddyInfo);
       
   750         
       
   751         RPointerArray<CBPASInfo> infos;
       
   752         CleanupClosePushL(infos);
       
   753         CBPASInfo* info = CBPASInfo::NewLC(aPresenceBuddyInfo->BuddyId());
       
   754         infos.Append(info); //ownership transfered
       
   755         CleanupStack::Pop(info); 
       
   756         
       
   757         MPresenceBuddyInfo2::TAvailabilityValues value = aPresenceBuddyInfo->Availability();
       
   758         TPtrC presTextValue = aPresenceBuddyInfo->AvailabilityText(); 
       
   759         CBPASPresenceInfo* PresInfo = CBPASPresenceInfo::NewL( presTextValue, value );
       
   760         CleanupStack::PushL(PresInfo);
       
   761         info->SetPresenceInfoL(PresInfo);
       
   762         CleanupStack::Pop(PresInfo);// ownership transfered        
       
   763                                       
       
   764         // Make service info
       
   765         SetServiceInfosL(infos);  // RPointerArray<CBPASInfo>& aInfos
       
   766         
       
   767         // make icon infos only if service info is available
       
   768         if(info->ServiceInfo())
       
   769             SetIconInfosL(infos);
       
   770 
       
   771         if(iObserver)
       
   772             iObserver->HandleSubscribedInfoL(info);//ownership not transferred
       
   773         delete info;    
       
   774         CleanupStack::Pop(1); //infos
       
   775         infos.Close();
       
   776         delete aPresenceBuddyInfo;
       
   777         CleanupStack::Pop(1); //aPresenceBuddyInfo    
       
   778         }
       
   779     CMS_DP(KBPasLoggerFile, CMS_L( "CBPAS::HandlePresenceNotificationL: end"));
       
   780     }
       
   781 
       
   782     
       
   783 // end of file
       
   784 
       
   785