phonebookui/Phonebook2/Presentation/src/CPbk2ServiceManager.cpp
changeset 0 e686773b3f54
child 5 81f8547efd4f
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002-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: 
       
    15 *     Service manager for external service providers.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <barsread.h>
       
    22 #include <Pbk2DataCaging.hrh>
       
    23 #include <Pbk2Presentation.rsg>
       
    24 #include <RPbk2LocalizedResourceFile.h>
       
    25 #include "CPbk2ServiceManager.h"
       
    26 #include "Pbk2PresentationUtils.h"
       
    27 #include <spsettings.h>
       
    28 #include <spproperty.h>
       
    29 #include <cbsfactory.h>
       
    30 #include <mbsaccess.h>
       
    31 #include <fbs.h>
       
    32 #include <contactpresencebrandids.h> 
       
    33 #include <spnotifychange.h>
       
    34 #include <PbkUID.h>
       
    35 
       
    36 //constants
       
    37 const TInt KMaxBufLength = 128; 
       
    38 
       
    39 void CPbk2ServiceManager::TService::Delete()
       
    40     {
       
    41     delete iBitmap;
       
    42     iBitmap = NULL;
       
    43     delete iMask;
       
    44     iBitmap = NULL;
       
    45     }
       
    46 
       
    47 EXPORT_C CPbk2ServiceManager* CPbk2ServiceManager::NewL(RFs& aRFs)
       
    48     {
       
    49     CPbk2ServiceManager* self = new (ELeave) CPbk2ServiceManager(aRFs);
       
    50     CleanupStack::PushL(self);
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop(self);
       
    53     return self;
       
    54     }
       
    55 
       
    56 CPbk2ServiceManager::~CPbk2ServiceManager()
       
    57     {
       
    58     if(iSPNotifyChange)
       
    59         {
       
    60         iSPNotifyChange->NotifyChangeCancel();
       
    61         }
       
    62     
       
    63     delete iSPNotifyChange;
       
    64     iSpIdsToObserve.Close(); //Empty, no entries
       
    65     delete iSettings;
       
    66     DeleteData();
       
    67     }
       
    68 
       
    69 void CPbk2ServiceManager::DeleteData()
       
    70     {
       
    71     for (TInt i = 0; i < iServices.Count(); i++)
       
    72         {
       
    73         iServices[i].Delete();
       
    74         }
       
    75     iServices.Close();
       
    76 
       
    77     for (TInt i = 0; i < iStringsOwner.Count(); i++)
       
    78         {
       
    79         delete iStringsOwner[i];
       
    80         }
       
    81     iStringsOwner.Close();
       
    82     iBitmapIdCounter = 0;
       
    83     }
       
    84 
       
    85 CPbk2ServiceManager::CPbk2ServiceManager(RFs& aRFs) :
       
    86     iRFs(aRFs)
       
    87     {
       
    88     }
       
    89 
       
    90 void CPbk2ServiceManager::ConstructL()
       
    91     {
       
    92     iSettings = CSPSettings::NewL();    
       
    93     iSPNotifyChange = CSPNotifyChange::NewL(*this);
       
    94     iSPNotifyChange->NotifyChangeL( iSpIdsToObserve );
       
    95     iRunRefreshData = ETrue;    
       
    96     }
       
    97 
       
    98 EXPORT_C const CPbk2ServiceManager::RServicesArray& CPbk2ServiceManager::Services()
       
    99     {
       
   100     //If got one or more notifications of changed data run refresh first
       
   101      if(iRunRefreshData)
       
   102         {
       
   103         iRunRefreshData = EFalse;
       
   104         TRAP_IGNORE( RunRefreshDataL() );
       
   105         }
       
   106     
       
   107     return iServices;
       
   108     }
       
   109 
       
   110 EXPORT_C void CPbk2ServiceManager::RefreshDataL()
       
   111     {
       
   112     iRunRefreshData = ETrue;
       
   113     }
       
   114 
       
   115 void CPbk2ServiceManager::RunRefreshDataL()
       
   116     {
       
   117     DeleteData();    
       
   118     ReadWellKnownServicesL();    
       
   119     AppendInstalledServicesL();
       
   120     UpdateBrandingInfoL();
       
   121     }
       
   122 
       
   123 
       
   124 void CPbk2ServiceManager::ReadWellKnownServicesL()
       
   125     {
       
   126     RPbk2LocalizedResourceFile resFile( &iRFs );
       
   127     resFile.OpenLC(KPbk2RomFileDrive,
       
   128                    KDC_RESOURCE_FILES_DIR, 
       
   129                    Pbk2PresentationUtils::PresentationResourceFile());
       
   130     
       
   131     // Read well known services from resource
       
   132     TResourceReader reader;
       
   133     reader.SetBuffer( resFile.AllocReadLC( 
       
   134             R_PHONEBOOK2_WELL_KNOWN_SERVICES ) );
       
   135     TInt count = reader.ReadInt16();
       
   136     HBufC* buf = NULL;
       
   137     for (TInt i = 0; i < count; i++)
       
   138         {
       
   139         TService service;
       
   140         buf = reader.ReadHBufCL();
       
   141         CleanupStack::PushL(buf);
       
   142         service.iName.Set(*buf);
       
   143         iStringsOwner.AppendL(buf);
       
   144         CleanupStack::Pop(buf);
       
   145         buf = reader.ReadHBufCL();
       
   146         CleanupStack::PushL(buf);
       
   147         service.iDisplayName.Set(*buf);
       
   148         iStringsOwner.AppendL(buf);
       
   149         CleanupStack::Pop(buf);
       
   150         service.iFlags = EWellKnown;
       
   151         service.iBitmap = service.iMask = NULL;
       
   152         service.iBitmapId = 0;
       
   153         service.iBrandId.Set(KNullDesC);
       
   154         service.iApplicationId.Set(TPtrC8(KCPBrandAppId));
       
   155         AppendServiceL(service); 
       
   156         }
       
   157     CleanupStack::PopAndDestroy(2, &resFile);
       
   158     }
       
   159 
       
   160 void CPbk2ServiceManager::AppendInstalledServicesL()
       
   161     {
       
   162     RIdArray idArray;
       
   163     CleanupClosePushL(idArray);
       
   164     User::LeaveIfError(iSettings->FindServiceIdsL(idArray));
       
   165     const TInt granularity = 16;
       
   166     CDesCArrayFlat* names = new (ELeave) CDesCArrayFlat(granularity);
       
   167     CleanupStack::PushL(names);
       
   168     User::LeaveIfError(iSettings->FindServiceNamesL(idArray, *names));
       
   169     
       
   170     //Append if not yet in iServices, otherwise update iServices  
       
   171     for (TInt ni = 0; ni < names->Count(); ni++)
       
   172         {
       
   173         TBool found = EFalse;
       
   174         TInt si = 0;
       
   175         while(si < iServices.Count()&& !found)
       
   176             {
       
   177             iServices[si].iName.CompareF(names->operator[](ni)) ? 
       
   178                 si++ :found=ETrue; 
       
   179             }
       
   180 
       
   181         //For installed well-known services the service data needs to be updated.
       
   182         //For non-well known services some service attributes are first checked
       
   183         //to decide whether to add or skip the service.
       
   184         if( found )
       
   185             {
       
   186             //Update existing
       
   187             UpdateServiceL(iServices[si], idArray[ni], names->operator[](ni));
       
   188             }
       
   189         else
       
   190             {
       
   191             //Append new if ok to add 
       
   192             TBool okToAppend(EFalse);
       
   193 
       
   194             // Check whether service supports cs voice call. If so, discard it.            
       
   195             CSPProperty* servAttrMask = CSPProperty::NewLC();        
       
   196             TInt ret = iSettings->FindPropertyL( 
       
   197                 idArray[ni], EPropertyServiceAttributeMask, *servAttrMask );
       
   198             
       
   199             if( ret == KErrNone )
       
   200                 {
       
   201                 TInt mask(0);
       
   202                 servAttrMask->GetValue(mask);
       
   203                 okToAppend = !(mask & ESupportsCSVoiceCall); 
       
   204                 }
       
   205             CleanupStack::PopAndDestroy(servAttrMask);            
       
   206             
       
   207             if(okToAppend) 
       
   208                 {
       
   209                 TService service;                
       
   210                 UpdateServiceL(service, idArray[ni], names->operator[](ni));
       
   211                 AppendServiceL(service);                
       
   212                 }            
       
   213             }    
       
   214         }
       
   215     CleanupStack::PopAndDestroy(names);
       
   216     CleanupStack::PopAndDestroy(); //idArray
       
   217     }
       
   218 
       
   219 void CPbk2ServiceManager::UpdateServiceL(
       
   220     TService& aService,
       
   221     TServiceId aServiceId, 
       
   222     const TDesC& aServiceName )
       
   223     {
       
   224     //Set service name
       
   225     HBufC* name = HBufC::NewLC(aServiceName.Length());
       
   226     iStringsOwner.AppendL(name);  
       
   227     CleanupStack::Pop(name);        
       
   228     name->Des().Copy(aServiceName);
       
   229 
       
   230     aService.iName.Set(*name);
       
   231     aService.iDisplayName.Set(*name); 
       
   232     aService.iFlags = EInstalled;
       
   233     aService.iBitmap = aService.iMask = NULL;
       
   234     aService.iBitmapId = 0;
       
   235 
       
   236     //Brand id 
       
   237     CSPProperty* prop = CSPProperty::NewLC();
       
   238     HBufC* brandID = HBufC::NewLC(KMaxBufLength);
       
   239 
       
   240     if (iSettings->FindPropertyL(aServiceId, EPropertyBrandId, *prop) == KErrNone)
       
   241         {
       
   242         TPtr des = brandID->Des();
       
   243         if (prop->GetValue(des) != KErrNone)
       
   244             {
       
   245             des.Zero();
       
   246             }
       
   247         }
       
   248     
       
   249     aService.iBrandId.Set(*brandID);
       
   250     aService.iServiceId = aServiceId;
       
   251     iStringsOwner.AppendL(brandID);
       
   252     
       
   253     //Appid
       
   254     aService.iApplicationId.Set(TPtrC8(KCPBrandAppId));
       
   255     CleanupStack::Pop(brandID);     
       
   256     CleanupStack::PopAndDestroy(prop);       
       
   257     }
       
   258 
       
   259 void CPbk2ServiceManager::AppendServiceL(TService aService)
       
   260     {
       
   261     iServices.AppendL(aService);
       
   262     }
       
   263 
       
   264 void CPbk2ServiceManager::UpdateBrandingInfoL()
       
   265     {
       
   266     TLanguage userLang = User::Language();
       
   267     TLanguage defLang = ELangInternationalEnglish;
       
   268     for (TInt i = 0; i < iServices.Count(); i++)    
       
   269         {
       
   270         TLanguage servLang = ELangTest;  
       
   271         //ELangTest CAN BE USED AS DEFAULT VALUE AS THIS DOES NOT BELONG TO ANY LANG
       
   272         //SEE THE HEADER FILE e32lang.h
       
   273         if(iServices[i].iBrandId.Length())
       
   274             {
       
   275             //Search for BrandPackage using PhoneLanguage
       
   276             //PhoneLanguage gets the Highest Priority
       
   277             TRAPD(err, UpdateBrandL(iServices[i], userLang ));
       
   278             if (err)
       
   279                 {
       
   280                 //The next priority goes to BrandLanguage set in the SpSettings/service table
       
   281                 //during provisioning
       
   282                 //Search for BrandPackage using this BrandLanguage      
       
   283                 CSPProperty* prop = CSPProperty::NewLC();
       
   284                 if(!iSettings->FindPropertyL(
       
   285                     iServices[i].iServiceId, EPropertyBrandLanguage, *prop))
       
   286                     {
       
   287                     if(!prop->GetValue((TInt&)servLang))
       
   288                         {
       
   289                         if (userLang != servLang)
       
   290                             {
       
   291                             TRAP(err, UpdateBrandL(iServices[i], servLang ));
       
   292                             }
       
   293                         }
       
   294                     }
       
   295                 CleanupStack::PopAndDestroy(prop);                
       
   296                 }
       
   297             
       
   298             if ( ( err ) && ( servLang !=  defLang ) && ( userLang != defLang ) )
       
   299                 {
       
   300                 //Fetching of brand with UserLang & with the one mentioned in the Servicetable
       
   301                 //was not successfull. 
       
   302                 //Finally try with Default Language ID and even if this fails
       
   303                 //proceed without any brand icons/texts
       
   304                 TRAP( err, UpdateBrandL( iServices[i], defLang ) );
       
   305                 }
       
   306             }
       
   307         }
       
   308     }
       
   309 
       
   310 void CPbk2ServiceManager::UpdateBrandL( TService& aService, TLanguage aLanguage )
       
   311     {
       
   312     HBufC8* brandID = HBufC8::NewLC(aService.iBrandId.Length());
       
   313     brandID->Des().Copy(aService.iBrandId);
       
   314 
       
   315     CBSFactory* bsFactory = CBSFactory::NewL( KCPBrandDefaultId, aService.iApplicationId );
       
   316     CleanupStack::PushL(bsFactory);
       
   317     MBSAccess* access = bsFactory->CreateAccessLC(*brandID, aLanguage); //leaves if none found    
       
   318     CFbsBitmap* bitmap = NULL;
       
   319     CFbsBitmap* mask = NULL;
       
   320     access->GetBitmapL(KCPBrandElementIdServiceIcon, bitmap, mask);    
       
   321     CleanupStack::PushL(bitmap);
       
   322     CleanupStack::PushL(mask);
       
   323     
       
   324     //Update service
       
   325     if(bitmap)
       
   326         {
       
   327         aService.iBitmap = bitmap;
       
   328         aService.iMask = mask;
       
   329         aService.iBitmapId = ++iBitmapIdCounter;
       
   330         }
       
   331     CleanupStack::Pop(2);    //bitmap, mask
       
   332     //Get Localised Name of Service
       
   333     HBufC* localizedName = access->GetTextLC(KCPBrandElementIdLocalizedServiceName);    
       
   334     if(localizedName)
       
   335         {
       
   336         aService.iDisplayName.Set(*localizedName);
       
   337         iStringsOwner.AppendL(localizedName); 
       
   338         }
       
   339     
       
   340     CleanupStack::Pop();  //localizedName  
       
   341     CleanupStack::PopAndDestroy(3, brandID);
       
   342     }
       
   343 
       
   344 // from MSPNotifyChangeObserver
       
   345 void CPbk2ServiceManager::HandleNotifyChange( TUint /*aServiceId*/ )
       
   346     {
       
   347     iRunRefreshData = ETrue;
       
   348     }
       
   349 
       
   350 // from MSPNotifyChangeObserver
       
   351 void CPbk2ServiceManager::HandleError( TInt /*aError*/ )
       
   352     {
       
   353     }
       
   354 
       
   355 
       
   356 // End of File