webservices/wsstar/wsstarpolicy/src/senwspolicymanager.cpp
changeset 0 62f9d29f7211
child 1 272b002df977
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 
       
    28 #include "senwspolicymanager.h"
       
    29 #include "seninternalwspolicy.h"
       
    30 
       
    31 #include <s32mem.h>
       
    32 #include <s32file.h>
       
    33 #include <sysutil.h>
       
    34 #include <SenFacet.h>
       
    35 #include <SenXmlUtils.h>
       
    36 #include "sendebug.h"               // internal \Utils\inc - filelogging MACROs
       
    37 #include "msencoreservicemanager.h" // internal \Framework\inc
       
    38 #include "SenParser.h"
       
    39 #include "SenDateUtils.h"
       
    40 #include "senservicepolicy.h"
       
    41 #include "wspolicyutils.h"
       
    42 #include "wspolicy.h"
       
    43 #include "senguidgen.h"
       
    44 #include "senxmldebug.h"
       
    45 #include "senlogger.h"
       
    46 
       
    47 
       
    48 #include <SenServiceConnection.h> 
       
    49 #include <xmlengnodelist.h> 
       
    50 using namespace WSPolicy;
       
    51 using namespace SenWSInternalPolicy;
       
    52 namespace
       
    53     {
       
    54 #if !defined( EKA2 ) && !defined( RD_SECURE_PRIV_DATA )
       
    55     _LIT(KWsPolicyFile,           "C:\\system\\data\\senwspolicies.xml");
       
    56 #else
       
    57     _LIT(KWsPolicyFile,           "senwspolicies.xml");
       
    58 #endif
       
    59 
       
    60     const TInt KStateParsingContainer   = 99;
       
    61     const TInt KStateParsingIdentifier  = 100;
       
    62     const TInt KStateParsingProperties  = 102;
       
    63     const TInt KStateParsingPolicy            = 104;
       
    64     const TInt KFlatBufSize                       = 128;
       
    65     
       
    66     const TUint KValidityTime = +7;
       
    67     
       
    68     }
       
    69 
       
    70 
       
    71 
       
    72 
       
    73 EXPORT_C CSenWSPolicyManager* CSenWSPolicyManager::NewL(MSenCoreServiceManager& aManager)
       
    74     {
       
    75     CSenWSPolicyManager* pNew = NewLC(aManager);
       
    76     CleanupStack::Pop(pNew);
       
    77     return(pNew);
       
    78        
       
    79     }
       
    80 
       
    81 EXPORT_C CSenWSPolicyManager* CSenWSPolicyManager::NewLC(MSenCoreServiceManager& aManager)
       
    82     {
       
    83     CSenWSPolicyManager* pNew = new (ELeave) CSenWSPolicyManager(aManager);
       
    84     CleanupStack::PushL(pNew);
       
    85     pNew->ConstructL();
       
    86     return pNew;
       
    87     
       
    88     }
       
    89 
       
    90 EXPORT_C CSenWSPolicyManager::~CSenWSPolicyManager()
       
    91     {
       
    92     delete iPolicDataLoader;
       
    93     iPolicyArray.ResetAndDestroy();
       
    94     iConsumerArray.ResetAndDestroy();
       
    95 
       
    96     }
       
    97 CSenWSPolicyManager::CSenWSPolicyManager(MSenCoreServiceManager& aManager) 
       
    98 :iManager(aManager),
       
    99 iLog((RFileLogger*)aManager.Log())
       
   100     {
       
   101     
       
   102     }
       
   103 void CSenWSPolicyManager::ConstructL()
       
   104     {
       
   105         iPolicDataLoader  = CSenWSPolicyLoader::NewL(iManager);
       
   106         RefreshPolicyContainerL();	//codescannerwarnings
       
   107     }
       
   108 RFileLogger* CSenWSPolicyManager::Log()
       
   109     {
       
   110     return iLog;
       
   111     }
       
   112 
       
   113 EXPORT_C TInt CSenWSPolicyManager::RegisterWsPolicyL(CSenWSDescription* aPattern,
       
   114                                                     TInt& aErrorTo)
       
   115     {
       
   116     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::RegisterWsPolicyL");                
       
   117         TInt retVal(KErrGeneral);
       
   118         if(iPolicDataLoader == NULL)
       
   119         {
       
   120           iPolicDataLoader  = CSenWSPolicyLoader::NewL(iManager);
       
   121         }
       
   122         
       
   123         retVal = iPolicDataLoader->RegisterWsPolicyL(aPattern, aErrorTo);
       
   124         
       
   125         if(retVal == KErrNone)
       
   126             RefreshPolicyContainerL();	//codescannerwarnings
       
   127         
       
   128         if(iPolicDataLoader)
       
   129         {
       
   130             delete iPolicDataLoader;
       
   131             iPolicDataLoader = NULL;
       
   132         }
       
   133         return retVal;
       
   134     }
       
   135 
       
   136 EXPORT_C TInt CSenWSPolicyManager::UnRegisterWsPolicyL(CSenWSDescription* aPattern, 
       
   137                                                         TInt& aErrorTo)
       
   138     {
       
   139     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::UnRegisterWsPolicyL");
       
   140         TInt retVal(KErrGeneral);
       
   141         if(iPolicDataLoader == NULL)
       
   142         {
       
   143           iPolicDataLoader  = CSenWSPolicyLoader::NewL(iManager);
       
   144         }
       
   145         
       
   146         retVal = iPolicDataLoader->UnRegisterWsPolicyL(aPattern, aErrorTo);
       
   147         
       
   148         if(retVal == KErrNone)
       
   149             RefreshPolicyContainerL();	//codescannerwarnings
       
   150         
       
   151         if(iPolicDataLoader)
       
   152         {
       
   153             delete iPolicDataLoader;
       
   154             iPolicDataLoader = NULL;
       
   155         }
       
   156         return retVal;
       
   157     
       
   158 }
       
   159 
       
   160 EXPORT_C TInt CSenWSPolicyManager::AddWsPolicyL(CSenWSDescription* aPattern, const TDesC8& aUri,
       
   161                                                const TDesC8& aPolicy, TInt& aErrorTo)
       
   162     {
       
   163     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::AddWsPolicyL");        
       
   164         TInt retVal(KErrGeneral);
       
   165         if(iPolicDataLoader == NULL)
       
   166         {
       
   167           iPolicDataLoader  = CSenWSPolicyLoader::NewL(iManager);
       
   168         }
       
   169         
       
   170         retVal = iPolicDataLoader->AddWsPolicyL(aPattern, aUri, aPolicy, aErrorTo);
       
   171         if(retVal == KErrNone)
       
   172             RefreshPolicyContainerL();	//codescannerwarnings
       
   173         
       
   174         if(iPolicDataLoader)
       
   175         {
       
   176             delete iPolicDataLoader;
       
   177             iPolicDataLoader = NULL;
       
   178         }
       
   179         return retVal;
       
   180     
       
   181     }
       
   182 
       
   183 EXPORT_C TInt CSenWSPolicyManager::RemoveWsPolicyL( CSenWSDescription* aPattern, 
       
   184                                                 const TDesC8& aUri, TInt& aErrorTo)
       
   185     {
       
   186         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::RemoveWsPolicyL");
       
   187         TInt retVal(KErrGeneral);
       
   188         if(iPolicDataLoader == NULL)
       
   189         {
       
   190           iPolicDataLoader  = CSenWSPolicyLoader::NewL(iManager);
       
   191         }
       
   192         
       
   193         retVal = iPolicDataLoader->RemoveWsPolicyL(aPattern, aUri, aErrorTo);
       
   194         
       
   195         if(retVal == KErrNone)
       
   196             RefreshPolicyContainerL();  //codescannerwarnings
       
   197         
       
   198         if(iPolicDataLoader)
       
   199         {
       
   200             delete iPolicDataLoader;
       
   201             iPolicDataLoader = NULL;
       
   202         }
       
   203         return retVal;
       
   204     
       
   205     }
       
   206 TInt CSenWSPolicyManager::IsConsumerExist(const TDesC8& aEndpoint, const TDesC8& aConsumerId)
       
   207 {
       
   208     TInt count(iConsumerArray.Count());
       
   209     for(TInt i = 0; i< count; i++)
       
   210     {
       
   211         CConsumerContainer* consumer = iConsumerArray[i];
       
   212         if(consumer->Uri() == aEndpoint && consumer->ConsumerId() == aConsumerId)
       
   213             return i;
       
   214     }
       
   215     return KErrNotFound;
       
   216 }
       
   217 TInt CSenWSPolicyManager::PolicyConsumerCountL(CSenInternalWsPolicy* aInternalPolicy)	//codescannerwarnings
       
   218 	{
       
   219     TInt retVal(KErrNone);
       
   220 	if (aInternalPolicy)
       
   221 		{
       
   222 	    TPtrC8 metadata = aInternalPolicy->IdentifierL().MetadataEndpointL();	//codescannerwarnings
       
   223 	    TInt count(iConsumerArray.Count());
       
   224 	    for(TInt i = 0; i< count; i++)
       
   225 			{
       
   226 			CConsumerContainer* consumer = iConsumerArray[i];
       
   227 			if(consumer->Uri() == metadata)
       
   228 			    retVal++;
       
   229 			}
       
   230 		}    
       
   231     return retVal;
       
   232 	}
       
   233 
       
   234 EXPORT_C TInt CSenWSPolicyManager::AddPolicyConsumerL(const TDesC8& aUri, const TDesC8& aConsumerId)
       
   235     {
       
   236     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::AddPolicyConsumerL");
       
   237     
       
   238     TInt indx = IsConsumerExist(aUri,aConsumerId);
       
   239     if(indx == KErrNotFound)
       
   240     {
       
   241         CConsumerContainer* consumer = CConsumerContainer::NewL(aUri,aConsumerId);    
       
   242         if(consumer) 
       
   243         {
       
   244             iConsumerArray.Append(consumer);
       
   245             RefreshPolicyContainerL();	//codescannerwarnings
       
   246         }
       
   247         else
       
   248         {
       
   249             
       
   250         }
       
   251             
       
   252         
       
   253     }
       
   254     return KErrNone;
       
   255     }
       
   256 
       
   257 EXPORT_C TInt CSenWSPolicyManager::RemovePolicyConsumerL(const TDesC8& aUri, const TDesC8& aConsumerId)
       
   258     {
       
   259     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::RemovePolicyConsumerL");
       
   260     TInt indx = IsConsumerExist(aUri,aConsumerId);
       
   261     if(indx > KErrNotFound)
       
   262         {
       
   263         CConsumerContainer* consumer = iConsumerArray[indx];
       
   264         if(consumer) 
       
   265             {
       
   266             TInt count (iPolicyArray.Count());
       
   267             for(TInt i = 0; i< count; i++)
       
   268                 {
       
   269                 CNormalizedPolicyConatiner* container =  iPolicyArray[i];
       
   270                 if(container->IsSameContainer(aUri))
       
   271                    {
       
   272                     container->CountDec();
       
   273                     if(container->ConsumerCount() < 1)
       
   274                         { //we no longer ahve any consumer for this conatiner
       
   275                         iPolicyArray.Remove(i);
       
   276                         delete container;
       
   277                         container = NULL;
       
   278                         }
       
   279                     }
       
   280             }
       
   281             iConsumerArray.Remove(indx);
       
   282             delete consumer;
       
   283             consumer = NULL;
       
   284             return KErrNone;
       
   285             }
       
   286         }
       
   287     return KErrNotFound;
       
   288     }
       
   289 
       
   290 EXPORT_C CSenElement*  CSenWSPolicyManager::WsPolicyL( CSenWSDescription* aPattern, 
       
   291                                                 const TDesC8& aUri, TInt& aErrorTo)
       
   292     {
       
   293         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::WsPolicyL");
       
   294         aErrorTo = KErrNotFound;
       
   295         CSenElement* ele = NULL;
       
   296 
       
   297         if(aPattern == NULL && aUri.Length() > 0) //special case for MS
       
   298             {
       
   299             TInt count (iPolicyArray.Count());
       
   300             for(TInt i = 0; i< count; i++)
       
   301                 {
       
   302                 CNormalizedPolicyConatiner* container =  iPolicyArray[i];
       
   303                 if(container->URI() == aUri)
       
   304                     {
       
   305                     aErrorTo = KErrNone;
       
   306                     return container->Policy();
       
   307                     }
       
   308                 }
       
   309                 
       
   310             }
       
   311     
       
   312         if(aUri.Length() == 0)
       
   313             {
       
   314             ele = PolicyFromSDL(aPattern);	//codescannerwarnings
       
   315             }
       
   316         else
       
   317             {
       
   318             ele = PolicyFromUri(aPattern, aUri);   
       
   319             }
       
   320          
       
   321         
       
   322         if(ele != NULL)
       
   323             aErrorTo = KErrNone;
       
   324         
       
   325         return ele;
       
   326     }
       
   327     
       
   328 CSenElement* CSenWSPolicyManager::PolicyByProviderId(const TDesC8& aProviderId, TInt&  aErrorTo)
       
   329 {
       
   330     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::PolicyByProviderId");
       
   331     TInt count (iPolicyArray.Count());
       
   332     for(TInt i = 0; i< count; i++)
       
   333     {
       
   334        CNormalizedPolicyConatiner* container =  iPolicyArray[i];
       
   335        if(container->ProviderId() == aProviderId)
       
   336        {
       
   337        CSenElement* ele = container->Policy();
       
   338        if(ele)
       
   339            {
       
   340             aErrorTo = KErrNone;
       
   341             return ele;
       
   342            }
       
   343        }
       
   344     }
       
   345     aErrorTo = KErrNotFound;
       
   346     return NULL;
       
   347   
       
   348 }
       
   349 
       
   350 CSenElement* CSenWSPolicyManager::PolicyFromUri(CSenWSDescription* /*aPolicy*/, const TDesC8& aUri)
       
   351 {
       
   352     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::PolicyFromUri");
       
   353     TInt count (iPolicyArray.Count());
       
   354     for(TInt i = 0; i< count; i++)
       
   355     {
       
   356        CNormalizedPolicyConatiner* container =  iPolicyArray[i];
       
   357        if(container->IsSameContainer(aUri))
       
   358        {
       
   359         return container->Policy();
       
   360        }
       
   361     }
       
   362     return NULL;
       
   363 }
       
   364 
       
   365 CSenElement* CSenWSPolicyManager::PolicyFromSDL(CSenWSDescription* aPolicy)	//codescannerwarnings
       
   366 {
       
   367     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::PolicyFromSD");
       
   368 
       
   369     if( aPolicy )
       
   370         {
       
   371         CSenServicePolicy* pol = (CSenServicePolicy*)aPolicy->ServicePolicy();
       
   372         CSenElement& servicePolicy = pol->AsElement();
       
   373         CSenElement* clientPolicy = servicePolicy.Element( KClientPolicyLocalName );
       
   374         if(clientPolicy)
       
   375             {
       
   376             RPointerArray<CSenElement> metaDataPoliciesUris;
       
   377             CleanupClosePushL(metaDataPoliciesUris);
       
   378             clientPolicy->ElementsL(metaDataPoliciesUris, KMetadataEndpointLocalname);
       
   379             TInt count(metaDataPoliciesUris.Count());
       
   380             if(count> 0)
       
   381                 {
       
   382                 CSenElement* metaData = NULL;
       
   383                 for(TInt i=0; i<count; i++)
       
   384                     {
       
   385                     metaData = metaDataPoliciesUris[i];
       
   386                     if(metaData && metaData->HasContent())
       
   387                         {
       
   388                          TPtrC8 uriName = metaData->Content();
       
   389                          CleanupStack::PopAndDestroy(&metaDataPoliciesUris);            
       
   390                          return PolicyFromUri(aPolicy, uriName);                   
       
   391                         }
       
   392                     }
       
   393                 }
       
   394              CleanupStack::PopAndDestroy(&metaDataPoliciesUris);            
       
   395             }
       
   396         }
       
   397     return NULL;
       
   398     }
       
   399 
       
   400 
       
   401 TInt CSenWSPolicyManager::UpdatePolicyContainerL(CSenInternalWsPolicy* aInternalPolicy) //codescannerwarnings
       
   402     {
       
   403     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::UpdatePolicyContainer");
       
   404         TInt indx(IsContainerL(aInternalPolicy));
       
   405         CNormalizedPolicyConatiner* container =  iPolicyArray[indx];
       
   406         if(container && container->URI().Compare(aInternalPolicy->IdentifierL().MetadataEndpointL()) == 0)	//codescannerwarnings
       
   407         {
       
   408             TBool retval = container->UpdateL(aInternalPolicy);
       
   409             container->SetConsumerCount(PolicyConsumerCountL(aInternalPolicy));//codescannerwarnings
       
   410             return KErrNone;
       
   411         }
       
   412     return KErrNotFound;
       
   413     
       
   414     }
       
   415 TInt CSenWSPolicyManager::IsContainerL(CSenInternalWsPolicy* aInternalPolicy) //codescannerwarnings
       
   416     {
       
   417     TInt count (iPolicyArray.Count());
       
   418     for(TInt i = 0; i< count && aInternalPolicy; i++)
       
   419     {
       
   420         CNormalizedPolicyConatiner* container =  iPolicyArray[i];
       
   421         TPtrC8 endpoint = aInternalPolicy->IdentifierL().MetadataEndpointL();	//codescannerwarnings
       
   422         if(container)
       
   423         {
       
   424             if(container->URI().Compare(endpoint) == 0)
       
   425                 return i;
       
   426         }
       
   427     }
       
   428     return KErrNotFound;
       
   429     
       
   430     }    
       
   431     
       
   432 TInt CSenWSPolicyManager::CreatePolicyContainerL(CSenInternalWsPolicy* aInternalPolicy)	//codescannerwarnings
       
   433     {
       
   434     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::CreatePolicyContainer");
       
   435     TInt retVal(KErrNone);
       
   436     TInt indx = IsContainerL(aInternalPolicy);	//codescannerwarnings
       
   437     if(indx == KErrNotFound)
       
   438         {
       
   439         TPtrC8 metaendpoint = aInternalPolicy->IdentifierL().MetadataEndpointL();	//codescannerwarnings
       
   440         CSenElement* metadta = aInternalPolicy->IdentifierL().MetadataEndpointElementL();	//codescannerwarnings
       
   441         {
       
   442         
       
   443         if(metadta && metadta->HasContent())
       
   444             {
       
   445             TPtrC8 endpoint = metadta->Content();
       
   446             CNormalizedPolicyConatiner* container = 
       
   447             CNormalizedPolicyConatiner::NewL(aInternalPolicy, endpoint);
       
   448             TPtrC8 metaendpoint = aInternalPolicy->IdentifierL().MetadataEndpointL();	//codescannerwarnings
       
   449             
       
   450             if(container && container->IsSameContainer(metaendpoint))
       
   451                 {
       
   452                 container->UpdateL(aInternalPolicy);
       
   453                 retVal = iPolicyArray.Append(container);
       
   454                 }
       
   455 
       
   456             }
       
   457         }
       
   458         
       
   459         }
       
   460     return retVal;
       
   461     
       
   462     }    
       
   463 
       
   464 TInt CSenWSPolicyManager::RefreshPolicyContainerL()	//codescannerwarnings
       
   465     {
       
   466     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::RefreshPolicyContainer");
       
   467     TInt retVal(KErrGeneral);
       
   468     if(iPolicDataLoader == NULL)
       
   469         {
       
   470         iPolicDataLoader  = CSenWSPolicyLoader::NewL(iManager);
       
   471         }
       
   472         
       
   473     RSenWSPolicyArray policies;
       
   474     CleanupClosePushL(policies);
       
   475     retVal = iPolicDataLoader->InternalWsPoliciesL(policies);
       
   476     if(retVal == KErrNone)
       
   477         {
       
   478         TInt count(policies.Count());
       
   479         for(TInt i = 0; i< count; i++)
       
   480             {
       
   481             CSenInternalWsPolicy* policy =  policies[i];
       
   482             TInt consumerCount = PolicyConsumerCountL(policy);//codescannerwarnings
       
   483             if( consumerCount > 0)
       
   484             {
       
   485             //for every internal policy we should have a container here            
       
   486             if(policy && IsContainerL(policy) != KErrNotFound)	//codescannerwarnings
       
   487                 {//We have alreadya  containere
       
   488                 // we already have this Policy container
       
   489                 // we just got a new consumer for this policy
       
   490                 // so the count will be increased
       
   491                 retVal = UpdatePolicyContainerL(policy); //codescannerwarnings
       
   492                 if(retVal == KErrNone)
       
   493                 { //we got proper update
       
   494                     
       
   495                 }
       
   496                 }
       
   497             else
       
   498                 {//create a new container
       
   499 				retVal = CreatePolicyContainerL(policy); //codescannerwarnings
       
   500                 }
       
   501                 
       
   502             }
       
   503             }
       
   504         }
       
   505 
       
   506     CleanupStack::PopAndDestroy(&policies);
       
   507     
       
   508     if(iPolicDataLoader)
       
   509         {
       
   510         delete iPolicDataLoader;
       
   511         iPolicDataLoader = NULL;
       
   512         }
       
   513 
       
   514     return retVal;
       
   515         
       
   516     }
       
   517 
       
   518 EXPORT_C TInt CSenWSPolicyManager::ExpiredPoliciesL(RExpiredPolicyArray& aArray,  TInt& aErrorTo)
       
   519 {
       
   520 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyManager::ExpiredPoliciesL");    
       
   521     TTime now;
       
   522     now.UniversalTime();
       
   523     
       
   524     TInt count (iPolicyArray.Count());
       
   525     
       
   526     for(TInt i = 0; i< count; i++)
       
   527     {
       
   528         CNormalizedPolicyConatiner* container =  iPolicyArray[i];
       
   529         if(container && container->ValidUntil() < now)
       
   530         {
       
   531             CExpiredPolicy* expir = CExpiredPolicy::NewL(container->Contract(),
       
   532                                                     container->Endpoint(),
       
   533                                                     container->URI(), container->ProviderId()); 
       
   534             if(expir)                                                    
       
   535             {
       
   536                 aArray.Append(expir);
       
   537                 aErrorTo = KErrNone;
       
   538             }
       
   539         }
       
   540     }
       
   541     
       
   542     if(aArray.Count() > 0)
       
   543         return KErrNone;
       
   544     else
       
   545         return KErrNotFound;
       
   546     
       
   547 }
       
   548 
       
   549 TInt CSenWSPolicyManager::AddWsPolicy1L(CSenWSDescription* aPattern, const TDesC8& aUri,
       
   550                                                const TDesC8& aPolicy, TInt& aErrorTo)
       
   551 {
       
   552     return AddWsPolicyL(aPattern, aUri, aPolicy, aErrorTo);   
       
   553 }
       
   554 
       
   555 TInt CSenWSPolicyManager::RemoveWsPolicy1L( CSenWSDescription* aPattern, 
       
   556                                                 const TDesC8& aUri, TInt& aErrorTo)
       
   557 {
       
   558     return RemoveWsPolicyL(aPattern, aUri, aErrorTo);  
       
   559 }
       
   560                                                                                                                                                                                                
       
   561 TInt CSenWSPolicyManager::RegisterWsPolicy1L(CSenWSDescription* aPattern,
       
   562                                                     TInt& aErrorTo)
       
   563 {
       
   564    return RegisterWsPolicyL(aPattern, aErrorTo);   
       
   565 }
       
   566 
       
   567 TInt CSenWSPolicyManager::UnRegisterWsPolicy1L(CSenWSDescription* aPattern,
       
   568                                                     TInt& aErrorTo)
       
   569 {
       
   570     return UnRegisterWsPolicyL(aPattern, aErrorTo);  
       
   571 }
       
   572                                                     
       
   573 CSenElement*  CSenWSPolicyManager::WsPolicy1L( CSenWSDescription* aPattern, 
       
   574                                                 const TDesC8& aUri, TInt& aErrorTo)
       
   575 {
       
   576     return WsPolicyL(aPattern, aUri, aErrorTo);  
       
   577 }
       
   578 
       
   579 CSenElement*  CSenWSPolicyManager::WsPolicy1L( CSenWSDescription* aPattern, 
       
   580                                                 TInt& aErrorTo)
       
   581 {
       
   582     return WsPolicyL(aPattern, KNullDesC8(), aErrorTo);  
       
   583 }
       
   584 CSenElement*  CSenWSPolicyManager::WsPolicy1L( const TDesC8& aProviderId, TInt& aErrorTo)
       
   585 {
       
   586     return PolicyByProviderId(aProviderId, aErrorTo);
       
   587 }
       
   588 TInt CSenWSPolicyManager::AddPolicyConsumer1L(const TDesC8& aUri, const TDesC8& aConsumerId)
       
   589 {
       
   590     return AddPolicyConsumerL(aUri,aConsumerId);    
       
   591 }
       
   592 
       
   593 TInt CSenWSPolicyManager::RemovePolicyConsumer1L(const TDesC8& aUri, const TDesC8& aConsumerId)
       
   594 {
       
   595     return RemovePolicyConsumerL(aUri,aConsumerId);    
       
   596 }
       
   597 
       
   598 
       
   599 CNormalizedPolicyConatiner* CNormalizedPolicyConatiner::NewL(CSenInternalWsPolicy* aPolicy, const TDesC8& aUri)
       
   600     {
       
   601     CNormalizedPolicyConatiner* pNew = CNormalizedPolicyConatiner::NewLC(aPolicy, aUri);
       
   602     CleanupStack::Pop(pNew);
       
   603     return(pNew);
       
   604         
       
   605     }
       
   606 CNormalizedPolicyConatiner* CNormalizedPolicyConatiner::NewLC(CSenInternalWsPolicy* aPolicy, const TDesC8& aUri)
       
   607     {
       
   608     CNormalizedPolicyConatiner* pNew = new (ELeave) CNormalizedPolicyConatiner();
       
   609     CleanupStack::PushL(pNew);
       
   610     pNew->ConstructL(aPolicy, aUri);
       
   611     return pNew;
       
   612  
       
   613     }
       
   614 CNormalizedPolicyConatiner::~CNormalizedPolicyConatiner()
       
   615     {
       
   616     delete iContract; 
       
   617     delete iEndpoint; 
       
   618     delete iUri; 
       
   619     delete iProviderId;
       
   620     delete iPolicy;
       
   621     }
       
   622 CNormalizedPolicyConatiner::CNormalizedPolicyConatiner()
       
   623     {
       
   624         
       
   625     }
       
   626 void CNormalizedPolicyConatiner::ConstructL(CSenInternalWsPolicy* aPolicy, const TDesC8& aUri)
       
   627     {
       
   628     TPtrC8 contract = aPolicy->IdentifierL().ContractL();	//codescannerwarnings
       
   629     TPtrC8 endpoint = aPolicy->IdentifierL().EndpointL();	//codescannerwarnings
       
   630     TPtrC8 providerId = aPolicy->IdentifierL().ProviderIdL();	//codescannerwarnings
       
   631     TTime valid = aPolicy->ValidUntilL(aUri);
       
   632     
       
   633     iCount = 1;
       
   634     iContract = contract.AllocL();
       
   635     iEndpoint = endpoint.AllocL();
       
   636     iProviderId= providerId.AllocL();
       
   637     iUri = aUri.AllocL();
       
   638     iValidUntil = valid;
       
   639     }
       
   640 
       
   641 TBool CNormalizedPolicyConatiner::UpdateL(CSenInternalWsPolicy* aInternalPolicy)
       
   642     {
       
   643     TTime valid = aInternalPolicy->ValidUntilL(URI());
       
   644     CSenElement* normPolicy = NULL;
       
   645     if(valid  > iValidUntil ) //we have new expiry time
       
   646     {
       
   647         CSenElement*  policy = aInternalPolicy->WsPolicyByUriL(URI());
       
   648         TInt length = 0;
       
   649         if(policy)
       
   650         {
       
   651         CBufFlat* pBuf = NULL;
       
   652         pBuf = CBufFlat::NewL(KFlatBufSize);
       
   653         CleanupStack::PushL(pBuf);
       
   654         RBufWriteStream bufWs(*pBuf);
       
   655         CleanupClosePushL(bufWs);
       
   656         policy->WriteAsXMLToL(bufWs);
       
   657         TPtr8 p8 = pBuf->Ptr(0);
       
   658         length = p8.Length();
       
   659         CleanupStack::PopAndDestroy(2); // bufWs (close), pBuf
       
   660         }
       
   661 
       
   662         if(length < 100000)        
       
   663         {
       
   664         CWSPolicy* wspolicy = CWSPolicy::NewLC();
       
   665         TRAPD(error, normPolicy = wspolicy->NormalizePolicy2L(*policy, NULL);)
       
   666         
       
   667         CleanupStack::PopAndDestroy(wspolicy);
       
   668         if(error == KErrNone && normPolicy)
       
   669         {
       
   670             if(iPolicy)
       
   671                 delete iPolicy;
       
   672             
       
   673             iPolicy = normPolicy;
       
   674             iValidUntil   = valid;            
       
   675             return ETrue;            
       
   676         }
       
   677         //set this policy as new policy
       
   678     }
       
   679     }
       
   680     else if(valid == iValidUntil)
       
   681     {
       
   682         if(iPolicy == NULL)
       
   683         {
       
   684         CSenElement*  policy = aInternalPolicy->WsPolicyByUriL(URI());
       
   685         TInt length = 0;
       
   686         if(policy)
       
   687         {
       
   688         CBufFlat* pBuf = NULL;
       
   689         pBuf = CBufFlat::NewL(KFlatBufSize);
       
   690         CleanupStack::PushL(pBuf);
       
   691         RBufWriteStream bufWs(*pBuf);
       
   692         CleanupClosePushL(bufWs);
       
   693         policy->WriteAsXMLToL(bufWs);
       
   694         TPtr8 p8 = pBuf->Ptr(0);
       
   695         length = p8.Length();
       
   696         CleanupStack::PopAndDestroy(2); // bufWs (close), pBuf
       
   697         }
       
   698 
       
   699         if(length < 100000)        
       
   700         {
       
   701         CWSPolicy* wspolicy = CWSPolicy::NewLC();
       
   702         TRAPD(error, normPolicy = wspolicy->NormalizePolicy2L(*policy, NULL);)    
       
   703         CleanupStack::PopAndDestroy(wspolicy);
       
   704         if(error == KErrNone && normPolicy)
       
   705         {            
       
   706             iPolicy = normPolicy;
       
   707             iValidUntil   = valid;            
       
   708             return ETrue;            
       
   709         }
       
   710         //set this policy as new policy
       
   711             
       
   712         }
       
   713     }
       
   714     }
       
   715     return EFalse;
       
   716     }   
       
   717 TBool CNormalizedPolicyConatiner::IsSameContainer(const TDesC8& aUri)
       
   718     {
       
   719     
       
   720     if(aUri.Compare(URI()) == 0)
       
   721         {
       
   722             return ETrue;    
       
   723         }
       
   724        return EFalse;
       
   725     }
       
   726 void CNormalizedPolicyConatiner::CountInc()
       
   727 {
       
   728     iCount++;    
       
   729 }
       
   730 void CNormalizedPolicyConatiner::CountDec()
       
   731 {
       
   732     iCount--;
       
   733     if(iCount < 0)
       
   734         iCount = 0;
       
   735 }
       
   736 void CNormalizedPolicyConatiner::SetConsumerCount(TInt aCount)
       
   737 {
       
   738     iCount = aCount;
       
   739 }
       
   740 TInt  CNormalizedPolicyConatiner::ConsumerCount()
       
   741     {
       
   742     return iCount;
       
   743     }
       
   744 TPtrC8  CNormalizedPolicyConatiner::Contract()
       
   745     {
       
   746     return iContract->Des();
       
   747     }
       
   748 TPtrC8  CNormalizedPolicyConatiner::Endpoint()
       
   749     {
       
   750     return iEndpoint->Des();
       
   751     }
       
   752 TPtrC8  CNormalizedPolicyConatiner::ProviderId()
       
   753     {
       
   754     return iProviderId->Des();
       
   755     }
       
   756 TPtrC8  CNormalizedPolicyConatiner::URI()
       
   757     {
       
   758     return iUri->Des();
       
   759     }
       
   760 TTime  CNormalizedPolicyConatiner::ValidUntil()
       
   761     {
       
   762     return iValidUntil;
       
   763     }
       
   764 CSenElement*  CNormalizedPolicyConatiner::Policy()
       
   765     {
       
   766     return iPolicy;
       
   767     }
       
   768 
       
   769     
       
   770 CSenWSPolicyLoader* CSenWSPolicyLoader::NewL(MSenCoreServiceManager& aManager)
       
   771     {
       
   772     CSenWSPolicyLoader* pNew = NewLC(aManager);
       
   773     CleanupStack::Pop(pNew);
       
   774     return(pNew);
       
   775     }
       
   776 
       
   777 CSenWSPolicyLoader* CSenWSPolicyLoader::NewLC(MSenCoreServiceManager& aManager)
       
   778     {
       
   779     CSenWSPolicyLoader* pNew = new (ELeave) CSenWSPolicyLoader(aManager);
       
   780     CleanupStack::PushL(pNew);
       
   781     pNew->ConstructL();
       
   782     return pNew;
       
   783     }
       
   784 
       
   785 CSenWSPolicyLoader::CSenWSPolicyLoader(MSenCoreServiceManager& aManager)
       
   786 : iManager(aManager),
       
   787   iLog((RFileLogger*)aManager.Log()),
       
   788   iMaxPolicyID(0)
       
   789     {
       
   790     }
       
   791 
       
   792 CSenWSPolicyLoader::~CSenWSPolicyLoader()
       
   793     {
       
   794     SaveDB();
       
   795     
       
   796     TInt count = iPolicyArray.Count();
       
   797     for (TInt i=0; i < count; i++)
       
   798         {
       
   799         iPolicyArray[i].Close();
       
   800         }
       
   801 
       
   802     iPolicyArray.Close();
       
   803     
       
   804     delete ipPolicyContainer;
       
   805     delete ipPolicyIdentifier;
       
   806     delete ipWSpolicy;
       
   807    
       
   808     }
       
   809 
       
   810 void CSenWSPolicyLoader::ConstructL()
       
   811     {
       
   812     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::ConstructL");
       
   813     BaseConstructL(KSenWsPolicies);
       
   814     LoadDBL();	//codescannerwarnings
       
   815     }
       
   816 
       
   817 RFileLogger* CSenWSPolicyLoader::Log()
       
   818     {
       
   819     return iLog;
       
   820     }
       
   821     
       
   822 void CSenWSPolicyLoader::StartElementL(const TDesC8& aNsUri,
       
   823                                             const TDesC8& aLocalName,
       
   824                                             const TDesC8& aQName,
       
   825                                             const RAttributeArray& aAttributes)
       
   826     {
       
   827     const TPtrC8 localName   = aLocalName;
       
   828     
       
   829     switch (iState)
       
   830         {
       
   831         case KSenStateSave:
       
   832             {
       
   833 
       
   834             if (aLocalName.Compare(KWsPolicyContainer) == 0)
       
   835                 {
       
   836                 if ( ipPolicyContainer )
       
   837                     {
       
   838                     CSenElement* element  = ipPolicyContainer->ExtractElement();
       
   839                     delete ipPolicyContainer;
       
   840                     ipPolicyContainer = NULL;
       
   841                     }
       
   842                     
       
   843 
       
   844                 CSenElement& element = AsElement();
       
   845 
       
   846                 TPtrC8 prefix(KNullDesC8);
       
   847                 if(aQName.Length()>0)
       
   848                     {
       
   849                     TInt colon = aQName.Locate(':');
       
   850                     if(colon>0) // Note: 0 also treated as no prefix
       
   851                         {
       
   852                         prefix.Set(aQName.Ptr(), colon);
       
   853                         }
       
   854                     }
       
   855                 
       
   856 				ipPolicyContainer = CSenBaseFragment::NewL(
       
   857 					                         aNsUri, aLocalName,
       
   858 					                         aQName, aAttributes,
       
   859 					                         element);
       
   860 				
       
   861 				ipPolicyContainer->AsElement().SetNamespaceL(prefix, aNsUri);	                         
       
   862                 iState = KStateParsingContainer;
       
   863                 }
       
   864 
       
   865             break;
       
   866             }
       
   867         case KStateParsingContainer: 
       
   868             {
       
   869             if (aLocalName.Compare(KSenWsPolicyIdentifierLocalname) == 0)
       
   870                 {
       
   871                 if ( ipPolicyIdentifier )
       
   872                     {
       
   873                     CSenElement* element  = ipPolicyIdentifier->ExtractElement();
       
   874                     delete ipPolicyIdentifier;
       
   875                     ipPolicyIdentifier = NULL;
       
   876                     }
       
   877 
       
   878                 CSenElement& element  = ipPolicyContainer->AsElement();
       
   879                 TPtrC8 prefix(KNullDesC8);
       
   880                 if(aQName.Length()>0)
       
   881                     {
       
   882                     TInt colon = aQName.Locate(':');
       
   883                     if(colon>0) // Note: 0 also treated as no prefix
       
   884                         {
       
   885                         prefix.Set(aQName.Ptr(), colon);
       
   886                         }
       
   887                     }
       
   888 
       
   889 				ipPolicyIdentifier = CSenWSPolicyIdentifier::NewL(
       
   890 					                         aNsUri, aLocalName,
       
   891 					                         aQName, aAttributes,
       
   892 					                         element);
       
   893 
       
   894                 iState = KStateParsingIdentifier;
       
   895                 ipPolicyIdentifier->AsElement().SetNamespaceL(prefix, aNsUri);
       
   896                 DelegateParsingL(*ipPolicyIdentifier);
       
   897                 }                
       
   898             else if(aLocalName.Compare(KSenWSPolicyMetadata) == 0)
       
   899                 {
       
   900                 // Parsing Policy
       
   901                 // 
       
   902                 // <=> Container can have only two elements:
       
   903                 //     1) Identifier,
       
   904                 //     2) PolicyMetadata
       
   905                 
       
   906                 if ( ipWSpolicy )
       
   907                     {
       
   908                     CSenElement* element  = ipWSpolicy->ExtractElement();
       
   909                     delete ipWSpolicy;
       
   910                     ipWSpolicy = NULL;
       
   911                     }
       
   912                 
       
   913 
       
   914                 CSenElement& element  = ipPolicyContainer->AsElement();
       
   915 
       
   916 				ipWSpolicy = CSenInternalWsPolicy::NewL(
       
   917 					                         aNsUri, aLocalName,
       
   918 					                         aQName, aAttributes,
       
   919 					                         element
       
   920 					                         );
       
   921 		        ipWSpolicy->SetIdentifier(ipPolicyIdentifier);
       
   922 		        ipPolicyIdentifier = NULL;
       
   923 
       
   924                 iState = KStateParsingPolicy;
       
   925 
       
   926                 DelegateParsingL(*ipWSpolicy);
       
   927                 }
       
   928               
       
   929             break;
       
   930             }
       
   931         default:
       
   932             {
       
   933 
       
   934             if (aLocalName.Compare(KSenWsPolicies) == 0)
       
   935                 {
       
   936                 TInt count(aAttributes.Count());
       
   937 
       
   938                 for (TInt i=0; i<count; i++)
       
   939                     {
       
   940                     TPtrC8 localName = aAttributes[i].Attribute().LocalName().DesC();
       
   941                     TPtrC8 value = aAttributes[i].Value().DesC();
       
   942                     
       
   943                     if ( localName == KWsPoliciesMaxID )
       
   944                         {
       
   945                         TLex8 lex;
       
   946                         lex.Assign(value);
       
   947                         lex.Val( iMaxPolicyID );                        
       
   948                         break;
       
   949                         }
       
   950                     }
       
   951                 }
       
   952                 
       
   953             CSenBaseFragment::StartElementL(aNsUri, aLocalName, aQName, aAttributes);
       
   954             break;
       
   955             }
       
   956         }
       
   957     }
       
   958 
       
   959 void CSenWSPolicyLoader::EndElementL(const TDesC8& aNsUri,
       
   960                                       const TDesC8& aLocalName,
       
   961                                       const TDesC8& aQName)
       
   962     
       
   963 
       
   964     {
       
   965     switch(iState)
       
   966         {
       
   967         case KStateParsingIdentifier:
       
   968             {
       
   969             iState = KStateParsingContainer;
       
   970             break;
       
   971             }
       
   972         case KStateParsingProperties:
       
   973             {
       
   974             iState = KStateParsingContainer;
       
   975             break;
       
   976             }           
       
   977         case KStateParsingPolicy:
       
   978             {
       
   979 
       
   980             RSenWSPolicyPtr policyPtr;
       
   981             policyPtr.OpenL(ipWSpolicy);
       
   982             CleanupClosePushL(policyPtr);
       
   983             iPolicyArray.Append(policyPtr);
       
   984             CleanupStack::Pop(&policyPtr);    
       
   985             
       
   986             CSenElement& element = this->AsElement().AddElementL(KWsPolicyContainer);
       
   987             element.AddElementL(ipWSpolicy->IdentifierL().AsElement());	//codescannerwarnings
       
   988             ipWSpolicy->IdentifierL().AsElement().SetParent(&element);	//codescannerwarnings
       
   989             ipWSpolicy->IdentifierL().SetIdL(NextId());	//codescannerwarnings
       
   990             element.AddElementL(ipWSpolicy->AsElement());
       
   991             ipWSpolicy->AsElement().SetParent(&element);
       
   992 
       
   993                 
       
   994             ipWSpolicy = NULL;
       
   995 
       
   996             iState = KStateParsingContainer;            
       
   997             break;
       
   998             }
       
   999         case KStateParsingContainer:
       
  1000             {
       
  1001             if ( ipPolicyContainer )
       
  1002                 {
       
  1003                 CSenElement& element  = ipPolicyContainer->AsElement();
       
  1004                 RPointerArray<CSenElement>& children = element.ElementsL();
       
  1005                     
       
  1006                 // Element should always have 2 child elements:
       
  1007                 //         1) Identifier and
       
  1008                 //         2) PolicyMetadata
       
  1009                 if ( children.Count() < 2 )
       
  1010                     {
       
  1011                     if ( ipPolicyIdentifier )
       
  1012                         {
       
  1013                         CSenElement* element  = ipPolicyIdentifier->ExtractElement();
       
  1014                         delete ipPolicyIdentifier;
       
  1015                         ipPolicyIdentifier = NULL;
       
  1016                         }
       
  1017                     if ( ipWSpolicy )
       
  1018                         {
       
  1019                         CSenElement* element  = ipWSpolicy->ExtractElement();
       
  1020                         delete ipWSpolicy;
       
  1021                         ipWSpolicy = NULL;
       
  1022                         }
       
  1023                     }
       
  1024 
       
  1025                 delete ipPolicyContainer;
       
  1026                 ipPolicyContainer = NULL;
       
  1027                 }
       
  1028                 
       
  1029             iState = KSenStateSave;
       
  1030             break;
       
  1031             }
       
  1032         default:
       
  1033             {
       
  1034             CSenBaseFragment::EndElementL(aNsUri, aLocalName, aQName);
       
  1035             break;
       
  1036             }
       
  1037         }
       
  1038     }    
       
  1039     
       
  1040 TInt CSenWSPolicyLoader::LoadDBL()	//codescannerwarnings
       
  1041     {
       
  1042 
       
  1043     TInt retVal(KErrNone);
       
  1044     TRAP(retVal, LoadFromL(KWsPolicyFile));
       
  1045 
       
  1046     return retVal;
       
  1047     }
       
  1048 
       
  1049 void CSenWSPolicyLoader::LoadFromL(const TDesC& aFile)
       
  1050     {
       
  1051     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::LoadFromL");            
       
  1052     RFs fss;
       
  1053     User::LeaveIfError(fss.Connect());
       
  1054     CleanupClosePushL(fss);
       
  1055 
       
  1056     TInt leaveCode(KErrNone);
       
  1057 
       
  1058 #if defined( EKA2 ) || defined( RD_SECURE_PRIV_DATA )
       
  1059     TBuf<KMaxPath> file;
       
  1060     fss.CreatePrivatePath(EDriveC);
       
  1061     fss.PrivatePath(file);
       
  1062     file.Append(aFile);
       
  1063 
       
  1064     RFile  xmlFile;
       
  1065     leaveCode = xmlFile.Open(fss , file, EFileRead );
       
  1066     if(leaveCode == KErrNone)
       
  1067         {
       
  1068         TInt size;
       
  1069         xmlFile.Size(size);
       
  1070 
       
  1071         HBufC8 *pBuf=HBufC8::NewL(size);	//CodeScannerWarnings
       
  1072 
       
  1073         CleanupStack::PushL(pBuf);
       
  1074         TPtr8 buf8ptr(pBuf->Des());
       
  1075         xmlFile.Read(buf8ptr,size);
       
  1076         iXmlReader = CSenXmlReader::NewL(KXmlParserMimeType); // use libxml2 sax parser
       
  1077         TRAP(leaveCode, ParseL(buf8ptr));
       
  1078         delete iXmlReader;
       
  1079         iXmlReader = NULL;  //CodeScannerWarnings
       
  1080         CleanupStack::PopAndDestroy(pBuf);
       
  1081         }
       
  1082     xmlFile.Close();        
       
  1083 #else
       
  1084     TRAP(leaveCode, ParseL(fss, aFile, *this));
       
  1085 #endif
       
  1086 
       
  1087     iState = KSenStateSave;
       
  1088     
       
  1089 #ifdef _SENDEBUG
       
  1090     if(leaveCode == KErrNotFound)
       
  1091         {
       
  1092         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"- The senwspolicies.xml does not exist!");
       
  1093         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"- New file will be generated.");
       
  1094         }
       
  1095     else if(leaveCode != KErrNone)
       
  1096         {
       
  1097         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel, _L8("- Parsing of senwspolicies.xml leaved: %d"), leaveCode));
       
  1098         }
       
  1099     else
       
  1100         {
       
  1101         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"- Policy database (senwspolicies.xml) successfully read.");
       
  1102         }
       
  1103 #else
       
  1104     leaveCode=0; // this is a decision: we are not interested
       
  1105                  // of invalid input in read/parse phase
       
  1106 #endif
       
  1107 
       
  1108     SaveDB(); // Always validatby saving
       
  1109     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::LoadL - End");        
       
  1110     CleanupStack::PopAndDestroy(); // fss
       
  1111     }
       
  1112 
       
  1113 TInt CSenWSPolicyLoader::SaveDB()
       
  1114     {
       
  1115     TInt retVal(KErrNone);
       
  1116     TRAP(retVal, SaveToL(KWsPolicyFile));
       
  1117     return retVal;
       
  1118     }
       
  1119 TInt CSenWSPolicyLoader::MergeToL()
       
  1120 {
       
  1121 //    CSenWSPolicyDBLoader* dbLoader = CSenWSPolicyDBLoader::NewL(iManager); 
       
  1122 return KErrNone;   
       
  1123 }
       
  1124 TInt CSenWSPolicyLoader::SaveToL(const TDesC& aFile)
       
  1125     {
       
  1126     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::SaveToL");        
       
  1127     TInt count = iPolicyArray.Count();
       
  1128     TInt maxId = 0;
       
  1129     for (TInt i=0; i<count; i++)
       
  1130         {
       
  1131         if ( iPolicyArray[i].Policy()->IdentifierL().IdL() > maxId )	//codescannerwarnings
       
  1132             {
       
  1133             maxId = iPolicyArray[i].Policy()->IdentifierL().IdL();	//codescannerwarnings
       
  1134             }
       
  1135         }
       
  1136     iMaxPolicyID = maxId;
       
  1137 //    UpdateMaxId();
       
  1138 
       
  1139     // First, collect everything into MEMORY
       
  1140     CBufFlat *pBuf = CBufFlat::NewL(KFlatBufSize);
       
  1141     CleanupStack::PushL(pBuf);
       
  1142 
       
  1143     RBufWriteStream bufWs(*pBuf);
       
  1144     CleanupClosePushL(bufWs);
       
  1145 
       
  1146     AsElement().WriteAsXMLToL(bufWs);
       
  1147 
       
  1148     TPtrC8 p8 = pBuf->Ptr(0);
       
  1149 
       
  1150     CleanupStack::PopAndDestroy(1); // bufWs
       
  1151 
       
  1152     // Everything in MEMORY ok, prepare to write into file
       
  1153     RFs fss;
       
  1154     User::LeaveIfError(fss.Connect());
       
  1155     CleanupClosePushL(fss);
       
  1156 
       
  1157     RFileWriteStream fileOutStream;
       
  1158     CleanupClosePushL(fileOutStream);
       
  1159 
       
  1160     if(!SysUtil::FFSSpaceBelowCriticalLevelL(&fss, p8.Length()) )
       
  1161         {
       
  1162         //Data caging 2 implementation
       
  1163 #if defined( EKA2 ) || defined( RD_SECURE_PRIV_DATA )
       
  1164         TBuf<KMaxPath> file;
       
  1165         fss.CreatePrivatePath(EDriveC);
       
  1166         fss.PrivatePath(file);
       
  1167         file.Append(aFile);
       
  1168         fileOutStream.Replace(fss, file, EFileWrite);
       
  1169 #else
       
  1170         fileOutStream.Replace(fss, aFile, EFileWrite);
       
  1171 #endif
       
  1172         // finally write the UTF-8 into the file.
       
  1173         fileOutStream.WriteL(p8);
       
  1174         }
       
  1175 
       
  1176     CleanupStack::PopAndDestroy(3); // fileOutStream, fss, pBuf
       
  1177     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::SaveToL - End");        
       
  1178     return KErrNone;
       
  1179     }
       
  1180 
       
  1181 TInt CSenWSPolicyLoader::InternalWsPoliciesL(RSenWSPolicyArray& aPolicies)
       
  1182     {
       
  1183 
       
  1184     TInt retVal(KErrNotFound);
       
  1185 
       
  1186     TInt count = iPolicyArray.Count();
       
  1187     
       
  1188     for (TInt i=0; i<count; i++)
       
  1189         {
       
  1190         aPolicies.AppendL(iPolicyArray[i].Policy());                
       
  1191         }
       
  1192 
       
  1193     if ( aPolicies.Count() == 0 )
       
  1194         {
       
  1195         return retVal;
       
  1196         }
       
  1197 
       
  1198     return KErrNone;
       
  1199     
       
  1200     }
       
  1201 TInt CSenWSPolicyLoader::InternalWsPolicyL(CSenWSDescription& aPattern,
       
  1202 								         RSenWSPolicyArray& aPolicies)
       
  1203     {
       
  1204     
       
  1205     TInt retVal(KErrNotFound);
       
  1206 
       
  1207     TInt count = iPolicyArray.Count();
       
  1208     
       
  1209     for (TInt i=0; i<count; i++)
       
  1210         {
       
  1211         if ( iPolicyArray[i].Policy()->IsApplicableL(aPattern) )	//codescannerwarnings
       
  1212             {
       
  1213             aPolicies.AppendL(iPolicyArray[i].Policy());                
       
  1214             }
       
  1215         }
       
  1216 
       
  1217     if ( aPolicies.Count() == 0 )
       
  1218         {
       
  1219         return retVal;
       
  1220         }
       
  1221 
       
  1222     return KErrNone;
       
  1223     }
       
  1224 TInt CSenWSPolicyLoader::InternalWsPolicyL(const TDesC8& aUri,
       
  1225 								         RSenWSPolicyArray& aPolicies)
       
  1226     {
       
  1227     
       
  1228     TInt retVal(KErrNotFound);
       
  1229 
       
  1230     TInt count = iPolicyArray.Count();
       
  1231     
       
  1232     for (TInt i=0; i<count; i++)
       
  1233         {
       
  1234         if ( iPolicyArray[i].Policy()->IsApplicableL(aUri) )	//codescannerwarnings
       
  1235             {
       
  1236             aPolicies.AppendL(iPolicyArray[i].Policy());                
       
  1237             }
       
  1238         }
       
  1239 
       
  1240     if ( aPolicies.Count() == 0 )
       
  1241         {
       
  1242         return retVal;
       
  1243         }
       
  1244 
       
  1245     return KErrNone;
       
  1246     }
       
  1247 
       
  1248 TInt CSenWSPolicyLoader::VerifyPolicyContentsL(CSenInternalWsPolicy* apInternalPolicy, TInt& aErrorTo)	//codescannerwarnings
       
  1249 {
       
  1250     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::VerifyPolicyContents Final");    
       
  1251     TInt retVal(KErrNotFound);
       
  1252 
       
  1253     TInt count = iPolicyArray.Count();
       
  1254     TPtrC8 uri = apInternalPolicy->IdentifierL().MetadataEndpointL();
       
  1255     for (TInt i=0; i<count; i++)
       
  1256         {
       
  1257         CSenInternalWsPolicy* pIntPolicy = iPolicyArray[i].Policy();
       
  1258         if(pIntPolicy)
       
  1259         {
       
  1260             TPtrC8 pUri = pIntPolicy->IdentifierL().MetadataEndpointL();
       
  1261             if(uri == pUri)
       
  1262             {
       
  1263                 TPtrC8 contract = apInternalPolicy->IdentifierL().ContractL();	//codescannerwarnings
       
  1264                 TPtrC8 endpoint = apInternalPolicy->IdentifierL().EndpointL();	//codescannerwarnings
       
  1265                 TPtrC8 pContract = pIntPolicy->IdentifierL().ContractL();	//codescannerwarnings
       
  1266                 TPtrC8 pEndpoint = pIntPolicy->IdentifierL().EndpointL();	//codescannerwarnings
       
  1267                 if(contract == pContract && endpoint== pEndpoint)
       
  1268                 {
       
  1269                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"same policy");    
       
  1270                     //same do nuthing
       
  1271                 }
       
  1272                 else
       
  1273                 {
       
  1274                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"different policy");    
       
  1275                 aErrorTo = KErrNone;
       
  1276                 CSenElement*  policy = pIntPolicy->WsPolicyByUriL(pUri);
       
  1277                 CSenElement*  policyNew = apInternalPolicy->WsPolicyByUriL(pUri);
       
  1278                 if(policy)
       
  1279                 {
       
  1280                     if(policyNew == NULL) //no existing policy
       
  1281                     {
       
  1282                         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"different policy, add");    
       
  1283                         retVal = apInternalPolicy->AddMetadataPolicyL(policy);    
       
  1284                     }
       
  1285                     else //existing policy so verify it 
       
  1286                     {
       
  1287                         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"different policy, update");    
       
  1288                         retVal = apInternalPolicy->UpdateMetadataPolicyL(policy);
       
  1289                     // if the Metadata of Policy (aElement) is already euqal to 
       
  1290                     }                
       
  1291                 }
       
  1292                 }
       
  1293             }
       
  1294             
       
  1295         }
       
  1296         
       
  1297         }
       
  1298         return retVal;
       
  1299         
       
  1300 }
       
  1301 RSenWSPolicyPtr CSenWSPolicyLoader::AddWsPolicyL(CSenInternalWsPolicy* apInternalPolicy,
       
  1302                                                         TInt& aErrorTo)
       
  1303     {
       
  1304     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::AddWsPolicyL Final");    
       
  1305     if ( !apInternalPolicy )
       
  1306         {
       
  1307         SENDEBUG_L("AddPolicyL(*) - Illegal argument: NULL");
       
  1308         aErrorTo = KErrArgument;
       
  1309         return RSenWSPolicyPtr();
       
  1310         }
       
  1311         
       
  1312     CleanupStack::PushL(apInternalPolicy);
       
  1313     RSenWSPolicyPtr policyPtr;
       
  1314     policyPtr.OpenL(apInternalPolicy);
       
  1315     CleanupStack::Pop(apInternalPolicy);
       
  1316     CleanupClosePushL(policyPtr);
       
  1317     iPolicyArray.AppendL(policyPtr);
       
  1318     CleanupStack::Pop(&policyPtr);
       
  1319     
       
  1320     CSenElement& element = this->AsElement().AddElementL(KWsPolicyContainer);
       
  1321     element.AddElementL(apInternalPolicy->IdentifierL().AsElement());	//codescannerwarnings
       
  1322     apInternalPolicy->IdentifierL().AsElement().SetParent(&element);	//codescannerwarnings
       
  1323     apInternalPolicy->IdentifierL().SetIdL(NextId());	//codescannerwarnings
       
  1324     element.AddElementL(apInternalPolicy->AsElement());
       
  1325     apInternalPolicy->AsElement().SetParent(&element);
       
  1326     
       
  1327     VerifyPolicyContentsL(apInternalPolicy,aErrorTo);	//codescannerwarnings
       
  1328     SaveDB();
       
  1329     
       
  1330     aErrorTo = KErrNone;
       
  1331     return policyPtr;
       
  1332     }
       
  1333 TInt CSenWSPolicyLoader::AddWsPolicyL(const TDesC8& aUri, CSenElement* aPolicy, TInt& aErrorTo)
       
  1334 {
       
  1335     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::AddWsPolicyL as Element");
       
  1336     TInt retVal(KErrNotFound);
       
  1337     RSenWSPolicyArray array;
       
  1338     CleanupClosePushL(array);
       
  1339     aErrorTo = InternalWsPolicyL(aUri,array);
       
  1340     
       
  1341     //array should have only one policy
       
  1342     TInt count(array.Count()); 
       
  1343     TPtrC8 metaendpoint = CWSPolicyUtils::PolicyUriL(aPolicy);
       
  1344     
       
  1345     for (TInt i = 0; i< count; i++)   
       
  1346     {
       
  1347         CSenInternalWsPolicy* ptr = array[i];
       
  1348         CSenWSPolicyIdentifier& identifier = ptr->IdentifierL();	//codescannerwarnings
       
  1349         if(identifier.MetadataEndpointL() == metaendpoint)	//codescannerwarnings
       
  1350         {
       
  1351 
       
  1352         CSenElement* policy = ptr->WsPolicyByUriL(metaendpoint);
       
  1353         if(policy == NULL) //no existing policy
       
  1354         {
       
  1355             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::AddWsPolicyL Add");
       
  1356             retVal = ptr->AddMetadataPolicyL(aPolicy);    
       
  1357         }
       
  1358         else //existing policy so verify it 
       
  1359         {
       
  1360             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::AddWsPolicyL Replace");
       
  1361             retVal = ptr->UpdateMetadataPolicyL(aPolicy);
       
  1362         // if the Metadata of Policy (aElement) is already euqal to 
       
  1363         }
       
  1364         SaveDB();            
       
  1365             
       
  1366         }
       
  1367 
       
  1368         
       
  1369     }
       
  1370     CleanupStack::PopAndDestroy(&array);
       
  1371     
       
  1372     return retVal; 
       
  1373 }
       
  1374 TInt CSenWSPolicyLoader::AddWsPolicyL(CSenWSDescription* /*aPattern*/, const TDesC8& aUri,
       
  1375                                                const TDesC8& aPolicy, TInt& aErrorTo)
       
  1376 {
       
  1377     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::AddWsPolicyL");
       
  1378     TInt retVal(KErrNotFound);
       
  1379     CSenElement* policy = CreatePolicyElementL(aUri, aPolicy);
       
  1380     if(policy)
       
  1381     {
       
  1382         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::AddWsPolicyL adding Policy as string");
       
  1383         CleanupStack::PushL(policy);
       
  1384         retVal = AddWsPolicyL(aUri, policy, aErrorTo);
       
  1385         CleanupStack::PopAndDestroy(policy);
       
  1386         policy = NULL;        
       
  1387     }
       
  1388     else
       
  1389     {
       
  1390         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"Not a Valid xml policy to add ");
       
  1391     }
       
  1392     return retVal; 
       
  1393 }
       
  1394 TInt CSenWSPolicyLoader::RemoveWsPolicyL( CSenWSDescription* aPattern, 
       
  1395                                                     const TDesC8& aUri, TInt& aErrorTo)
       
  1396 {
       
  1397     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::RemoveWsPolicyL");
       
  1398     TInt retVal(KErrNotFound);
       
  1399 
       
  1400     RSenWSPolicyArray array;
       
  1401     CleanupClosePushL(array);
       
  1402     aErrorTo = InternalWsPolicyL(*aPattern,array);
       
  1403     
       
  1404     //array should have only one Entry in array i.e. Matching Service
       
  1405     TInt count(array.Count()); 
       
  1406     for (TInt i = 0; i< count; i++)   
       
  1407     {
       
  1408         CSenInternalWsPolicy* ptr = array[i];
       
  1409         if(aUri.Length() == 0)
       
  1410             {
       
  1411             // delete all policies
       
  1412            //delete all metadatas
       
  1413            
       
  1414             }
       
  1415         else
       
  1416             {
       
  1417             CSenElement* policy = ptr->WsPolicyByUriL(aUri);
       
  1418             if(policy) //we have policy so remove it
       
  1419                 {
       
  1420                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"Removing CSenWSPolicyLoader::RemoveWsPolicyL");
       
  1421                 aErrorTo = ptr->RemoveMetadataPolicyL(aUri);
       
  1422                 retVal = KErrNone;    
       
  1423                 }
       
  1424                 
       
  1425             }
       
  1426     }
       
  1427 
       
  1428     CleanupStack::PopAndDestroy(&array);
       
  1429     SaveDB(); 
       
  1430     return retVal;
       
  1431 }
       
  1432     
       
  1433 TInt CSenWSPolicyLoader::NextId()
       
  1434     {
       
  1435     iMaxPolicyID++;
       
  1436 
       
  1437     UpdateMaxId();    
       
  1438     
       
  1439     return iMaxPolicyID;
       
  1440     }
       
  1441     
       
  1442 void CSenWSPolicyLoader::UpdateMaxId()
       
  1443     {
       
  1444 /*    
       
  1445     TBool found = EFalse;
       
  1446     CSenElement& element = AsElement();
       
  1447     RPointerArray<CSenBaseAttribute>& attrs = element.AttributesL();
       
  1448     
       
  1449     CSenBaseAttribute* bs = NULL;
       
  1450     TInt ele_count = attrs.Count();
       
  1451          
       
  1452     for(TInt j=0; j < ele_count; j++)
       
  1453         {
       
  1454             
       
  1455         bs = (attrs)[j];
       
  1456         if(bs)
       
  1457             {
       
  1458             if(bs->Name().Compare(KWsPoliciesMaxID) == 0)
       
  1459                 {
       
  1460                 TBuf8<KFlatBufSize> buffer;
       
  1461                 buffer.Num(iMaxPolicyID);
       
  1462                 bs->SetValueL(buffer);
       
  1463                 found = ETrue;
       
  1464                 }
       
  1465             }
       
  1466         }
       
  1467         
       
  1468     if ( !found )
       
  1469         {
       
  1470         TBuf8<KFlatBufSize> buffer;
       
  1471         buffer.Num(iMaxPolicyID);
       
  1472         element.AddAttributeL(KWsPoliciesMaxID, buffer);
       
  1473         }
       
  1474 */
       
  1475     }
       
  1476     
       
  1477 TInt CSenWSPolicyLoader::RegisterWsPolicyL(CSenInternalWsPolicy* aInternalPolicy,
       
  1478                                                     TInt& aErrorTo)
       
  1479 {
       
  1480 	TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::RegisterWsPolicyL2");
       
  1481     if(aInternalPolicy == NULL)
       
  1482     {
       
  1483         aErrorTo = KErrArgument;
       
  1484         return KErrGeneral;
       
  1485     }
       
  1486     
       
  1487     TInt count = iPolicyArray.Count();
       
  1488     TInt isPresent(KErrNotFound);
       
  1489 
       
  1490     
       
  1491     for (TInt i=0; i<count; i++)
       
  1492         {
       
  1493         CSenInternalWsPolicy* internalPolicy = iPolicyArray[i].Policy();
       
  1494         TBool retVal = internalPolicy->IsApplicableL(aInternalPolicy);	//codescannerwarnings
       
  1495         if(retVal)
       
  1496             {
       
  1497             isPresent = KErrNone;
       
  1498             break;
       
  1499             }
       
  1500         }
       
  1501 
       
  1502     if(isPresent == KErrNotFound)
       
  1503     {
       
  1504     	TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"Adding CSenWSPolicyLoader::RegisterWsPolicyL");
       
  1505         AddWsPolicyL(aInternalPolicy, aErrorTo);
       
  1506         return KErrNone;
       
  1507     }
       
  1508     
       
  1509     return KErrAlreadyExists;
       
  1510         
       
  1511 }
       
  1512 
       
  1513         
       
  1514 TInt CSenWSPolicyLoader::RegisterWsPolicyL(CSenWSDescription* aPattern,
       
  1515                                                     TInt& aErrorTo)
       
  1516 {
       
  1517 //check if the policy is already in DB
       
  1518 //if yes then use that
       
  1519 //else
       
  1520 //create new one
       
  1521     TInt retVal(KErrGeneral);
       
  1522     TInt ret(KErrGeneral);
       
  1523     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::RegisterWsPolicyL");    
       
  1524 
       
  1525     TPtrC8 cont = aPattern->Contract();
       
  1526     TPtrC8 endpo = aPattern->Endpoint();
       
  1527     CSenElement* providerId = aPattern->AsElement().Element(KProviderIdLocalName);
       
  1528 
       
  1529     CSenServicePolicy* pol = (CSenServicePolicy*)aPattern->ServicePolicy();
       
  1530     CSenElement& servicePolicy = pol->AsElement();
       
  1531     CSenElement* clientPolicy = servicePolicy.Element( KClientPolicyLocalName );
       
  1532     if(clientPolicy)
       
  1533         {
       
  1534         RPointerArray<CSenElement> metaDataPoliciesUris;
       
  1535         CleanupClosePushL(metaDataPoliciesUris);
       
  1536         clientPolicy->ElementsL(metaDataPoliciesUris, KMetadataEndpointLocalname);
       
  1537         TInt count(metaDataPoliciesUris.Count());
       
  1538         if(count> 0)
       
  1539             {
       
  1540             CSenElement* metaData = NULL;
       
  1541             for(TInt i=0; i<count; i++)
       
  1542                 {
       
  1543                 metaData = metaDataPoliciesUris[i];
       
  1544                 if(metaData && metaData->HasContent())
       
  1545                     {
       
  1546                     TPtrC8 uriName = metaData->Content();
       
  1547                     
       
  1548                     CSenInternalWsPolicy* pInternalPolicy = CSenInternalWsPolicy::NewLC();
       
  1549                     CSenWSPolicyIdentifier& identifier = pInternalPolicy->IdentifierL();	//codescannerwarnings
       
  1550                     
       
  1551                     identifier.AddMetadataEndpointL(metaData);
       
  1552 
       
  1553                     if(cont.Length() > 0)
       
  1554                         identifier.SetPropertyL(KContractLocalname, cont);
       
  1555                     if(endpo.Length() > 0)
       
  1556                         identifier.SetPropertyL(KEndpointLocalname, endpo);
       
  1557                     if(providerId && providerId->HasContent())     
       
  1558                         {
       
  1559                         TPtrC8 provider = providerId->Content();    
       
  1560                         identifier.SetPropertyL(KProviderIdLocalName, provider);
       
  1561                         }
       
  1562                     TLSLOG_ALL(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,(uriName));
       
  1563                     retVal = RegisterWsPolicyL(pInternalPolicy, aErrorTo);
       
  1564                         
       
  1565                     if(retVal != KErrNone )
       
  1566                     {
       
  1567                         CleanupStack::PopAndDestroy(pInternalPolicy);
       
  1568                     }
       
  1569                     else
       
  1570                     {
       
  1571                         CleanupStack::Pop(pInternalPolicy);
       
  1572                         ret = KErrNone;
       
  1573                     }
       
  1574                     
       
  1575                     }
       
  1576                 }
       
  1577             }
       
  1578         CleanupStack::PopAndDestroy(&metaDataPoliciesUris);
       
  1579         }
       
  1580 
       
  1581     return ret;          
       
  1582 }
       
  1583 
       
  1584 TInt CSenWSPolicyLoader::UnRegisterWsPolicyL(CSenWSDescription* aPattern,
       
  1585                                                     TInt& aErrorTo)
       
  1586 {
       
  1587 	TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,"CSenWSPolicyLoader::UnRegisterWsPolicyL");
       
  1588     TInt isPresent(KErrNotFound);
       
  1589     TInt count = 0;
       
  1590     for (TInt i=0; i<iPolicyArray.Count(); i++)
       
  1591     {
       
  1592         if(iPolicyArray[i].Policy()->IsApplicableL(*aPattern))	//codescannerwarnings
       
  1593         {
       
  1594             count++;
       
  1595         }
       
  1596     }
       
  1597 
       
  1598     for(TInt j=0; j<count; j++)
       
  1599         {
       
  1600         for (TInt i=0; i<iPolicyArray.Count(); i++)
       
  1601             {
       
  1602                 if(iPolicyArray[i].Policy()->IsApplicableL(*aPattern))	//codescannerwarnings
       
  1603                 {
       
  1604                     CSenElement& root = this->AsElement();
       
  1605                     CSenElement& element = iPolicyArray[i].Policy()->AsElement();
       
  1606                     CSenElement* parent = element.Parent();
       
  1607                     iPolicyArray[i].Close();
       
  1608                     iPolicyArray.Remove(i);
       
  1609 
       
  1610                     if(parent)
       
  1611                     {
       
  1612                         CSenElement* removed = root.RemoveElement(*parent);
       
  1613                         delete removed;
       
  1614                         removed = NULL;
       
  1615                     }
       
  1616                         
       
  1617                     aErrorTo = KErrNone;
       
  1618                     isPresent=  KErrNone; 
       
  1619                 }
       
  1620             }
       
  1621         }
       
  1622     SaveDB();        
       
  1623     return isPresent;
       
  1624 }
       
  1625 
       
  1626 CSenElement*  CSenWSPolicyLoader::WsPolicyL( CSenWSDescription* /*aPattern*/, 
       
  1627                                                 const TDesC8& /*aUri*/, TInt& /*aErrorTo*/)
       
  1628 {
       
  1629  return NULL;   
       
  1630 }
       
  1631 
       
  1632 CSenElement* CSenWSPolicyLoader::GetExpiryTagL(CSenElement& aPolicy)
       
  1633     {
       
  1634 //if there is no expiry tag in the policy the default expiry time is UTC +7 days    
       
  1635     RPointerArray<CSenElement> timeStamps;
       
  1636     CleanupClosePushL(timeStamps);       
       
  1637     CSenElement* validity = NULL;
       
  1638     const CSenNamespace* msNsUri = aPolicy.Namespace(MSWSPolicy::KMSNsPrefix);
       
  1639     if(msNsUri)
       
  1640         {
       
  1641         if(aPolicy.ElementsL(timeStamps, msNsUri->URI(), MSWSPolicy::KMetaDataValidity) == KErrNone)    
       
  1642             {
       
  1643             TInt validityStampsCount = timeStamps.Count();
       
  1644             
       
  1645             if(validityStampsCount > 0)
       
  1646                 {
       
  1647                 validity = timeStamps[0];//mws:metadatavalidity 
       
  1648                 CleanupStack::PopAndDestroy();
       
  1649                 return validity->Element(msNsUri->URI(), MSWSPolicy::KExpiry);
       
  1650                 }
       
  1651             }
       
  1652         
       
  1653         }
       
  1654     else
       
  1655         {
       
  1656         if(aPolicy.ElementsL(timeStamps, MSWSPolicy::KMetaDataValidity) == KErrNone)
       
  1657             {
       
  1658                     
       
  1659             TInt validityStampsCount = timeStamps.Count();
       
  1660             
       
  1661             if(validityStampsCount > 0)
       
  1662                 {
       
  1663                 validity = timeStamps[0];//mws:metadatavalidity 
       
  1664                 CleanupStack::PopAndDestroy();
       
  1665                 return validity->Element(MSWSPolicy::KExpiry);
       
  1666                 }
       
  1667             }
       
  1668         
       
  1669         }
       
  1670     CleanupStack::PopAndDestroy();        
       
  1671     return NULL;
       
  1672     }
       
  1673 CSenElement* CSenWSPolicyLoader::CreatePolicyElementL(const TDesC8& aUri, const TDesC8& aPolicy)
       
  1674 {
       
  1675     TInt retVal(KErrGeneral);
       
  1676     
       
  1677     CMyXml* xml = CMyXml::NewLC();
       
  1678     
       
  1679     retVal = xml->ReadFromDescL(aPolicy);
       
  1680   
       
  1681     CSenElement* ele = NULL;
       
  1682 
       
  1683     if(retVal == KErrNone)
       
  1684         {
       
  1685          ele = xml->ExtractElement();
       
  1686          TPtrC8 eleName = ele->LocalName();
       
  1687          TPtrC8 eleNs = ele->NsPrefix();
       
  1688         if(ele)
       
  1689            if((eleName == KWsdlDocument && eleNs == KWsdlNsPrefix) || 
       
  1690             (eleName == KPolicyDocument && eleNs == KPolicyNsPrefix))
       
  1691             {//this is definitely a poliycy or wsdl file
       
  1692             //Add Name, Id, Metaendpoint; validity
       
  1693             ProcessNewPolicyL(aUri, *ele);
       
  1694             }
       
  1695         else            
       
  1696             {
       
  1697             delete ele; 
       
  1698             ele = NULL;
       
  1699             }
       
  1700         }
       
  1701 
       
  1702         CleanupStack::PopAndDestroy();
       
  1703         return ele;
       
  1704 
       
  1705 }    
       
  1706 TInt CSenWSPolicyLoader::ProcessNewPolicyL( const TDesC8& aUri, CSenElement& aPolicy)
       
  1707 {
       
  1708      TPtrC8 policyUri = CWSPolicyUtils::PolicyUriL(&aPolicy);
       
  1709      if(policyUri.Length() == 0)
       
  1710         SenXmlUtils::AddAttributeL(aPolicy, KMetadataEndpointLocalname, aUri);   
       
  1711      
       
  1712      TPtrC8 name = CWSPolicyUtils::PolicyNameL(&aPolicy);
       
  1713      if(name.Length() == 0)
       
  1714         SenXmlUtils::AddAttributeL(aPolicy, KName, aUri);   
       
  1715      
       
  1716      TPtrC8 id = CWSPolicyUtils::PolicyIdL(&aPolicy);
       
  1717      if(id.Length() == 0)
       
  1718         {
       
  1719 
       
  1720         CSenGuidGen* pGuidGenerator = CSenGuidGen::NewLC();
       
  1721         HBufC8* iPolicyId = pGuidGenerator->GetRandomGuid8LC();
       
  1722         TPtr8 policyIdDes = iPolicyId->Des();
       
  1723         policyIdDes.LowerCase();
       
  1724 
       
  1725         aPolicy.AddAttrL(KWsuId, policyIdDes);
       
  1726         aPolicy.AddNamespaceL(KWsuPrefix, KWsuNsUri);
       
  1727                 
       
  1728         CleanupStack::PopAndDestroy(iPolicyId);
       
  1729         CleanupStack::PopAndDestroy(pGuidGenerator);
       
  1730         }
       
  1731 
       
  1732         
       
  1733     CSenElement* expiry = GetExpiryTagL(aPolicy);
       
  1734     TInt addtimeTag(KErrGeneral);
       
  1735     TTime expiryTime = Time::NullTTime();
       
  1736     if(expiry)
       
  1737         {
       
  1738         TInt updateTime = EvaluateExpiryL(expiry, expiryTime);
       
  1739         if(updateTime == KErrNone)
       
  1740             {
       
  1741             addtimeTag = KErrNone;
       
  1742             }
       
  1743         }
       
  1744     
       
  1745     if(addtimeTag != KErrNone)
       
  1746         {
       
  1747         expiryTime.UniversalTime();
       
  1748         TTimeIntervalDays interval(KValidityTime);
       
  1749         expiryTime+=interval;
       
  1750         }
       
  1751     
       
  1752     TBuf8<SenDateUtils::KXmlDateTimeMaxLength> ts;
       
  1753     SenDateUtils::ToXmlDateTimeUtf82L(ts, expiryTime);
       
  1754     SenXmlUtils::AddAttributeL(aPolicy, KSenWSPolicyValidUntil, ts); 
       
  1755         
       
  1756     return KErrNone;   
       
  1757 }    
       
  1758 TInt CSenWSPolicyLoader::EvaluateExpiryL(CSenElement* aExpiry, TTime& aExpiryTime)
       
  1759 { 
       
  1760     TInt updateTime(KErrNotFound);
       
  1761     if(aExpiry && aExpiry->HasContent())
       
  1762     {
       
  1763         TPtrC8 timeTag = aExpiry->Content();
       
  1764         _LIT8(KUtc,"UTC");
       
  1765         _LIT8(KDays,"days");
       
  1766         TInt utc = timeTag.Find(KUtc);  //CodeScannerWarnings      
       
  1767         TInt days = timeTag.Find(KDays);
       
  1768         if(utc != KErrNotFound && days != KErrNotFound)
       
  1769         {   //We found UTC +x days timestamp
       
  1770             //replace this time stamp with expiry time starting from now
       
  1771         
       
  1772             TInt plus = timeTag.Locate('+');
       
  1773             if(plus != KErrNotFound)
       
  1774             {
       
  1775                 TInt timeLength = days-plus-2;
       
  1776                 TPtrC8 timeValue =   timeTag.Mid(plus+2,timeLength-1);
       
  1777                 if(timeValue != KNullDesC8)
       
  1778                 {
       
  1779                     TInt value = 0;        
       
  1780                     TLex8 lex(timeValue);
       
  1781                     TInt retVal = lex.Val(value);
       
  1782                     
       
  1783                     if(retVal != KErrNone)
       
  1784                     {
       
  1785                     value = KValidityTime;
       
  1786                     }
       
  1787 
       
  1788                     if(value > 0)
       
  1789                     {
       
  1790                         TTime expiry;
       
  1791                         expiry.UniversalTime();
       
  1792                         TTimeIntervalDays interval(value);
       
  1793                         expiry+=interval;
       
  1794                         
       
  1795                         TBuf8<SenDateUtils::KXmlDateTimeMaxLength> ts;
       
  1796                         TRAP (retVal, SenDateUtils::ToXmlDateTimeUtf82L(ts, expiry));
       
  1797                         if(retVal == KErrNone && ts.Length() > 0)
       
  1798                         {
       
  1799                             aExpiry->SetContentL(ts);
       
  1800                             aExpiryTime = expiry;
       
  1801                             updateTime = KErrNone;
       
  1802                         }
       
  1803                     }
       
  1804                 }
       
  1805             }
       
  1806         }
       
  1807         
       
  1808         if(updateTime != KErrNone)
       
  1809         //the content is not in "UTC +7 days" format so try the XML time format
       
  1810         {
       
  1811             TTime now;
       
  1812             now.UniversalTime();
       
  1813      
       
  1814             TTime expiry = Time::NullTTime();
       
  1815             TRAPD(retval, expiry = SenDateUtils::FromXmlDateTimeL(timeTag);)
       
  1816             if(retval == KErrNone)
       
  1817                 {   
       
  1818                 if(expiry > now)
       
  1819                     {
       
  1820                     aExpiryTime = expiry;
       
  1821                     updateTime = KErrNone;
       
  1822                     }
       
  1823                     else //strange case. the policy provided to us is already expired
       
  1824                     {
       
  1825                     updateTime = KErrNone;                        
       
  1826                     }
       
  1827                 }
       
  1828             else
       
  1829                 {
       
  1830              updateTime = KErrGeneral;
       
  1831                 }
       
  1832             
       
  1833         }
       
  1834     }
       
  1835     
       
  1836     return updateTime;
       
  1837 }
       
  1838 /////////////////////////////////////////////////////////////////////////////////////////
       
  1839 /////////////////////////////////////////////////////////////////////////////////////////
       
  1840 /////////////////////////////////////////////////////////////////////////////////////////
       
  1841 /////////////////////////////////////////////////////////////////////////////////////////
       
  1842 /////////////////////////////////////////////////////////////////////////////////////////
       
  1843 
       
  1844 EXPORT_C CExpiredPolicy* CExpiredPolicy::NewL(const TDesC8& aContract, const TDesC8& aEndpoint,
       
  1845                                              const TDesC8& aUri, const TDesC8& aProviderId)
       
  1846 {
       
  1847     CExpiredPolicy* pNew = NewLC(aContract, aEndpoint, aUri, aProviderId);
       
  1848     CleanupStack::Pop(pNew);
       
  1849     return(pNew);
       
  1850 }
       
  1851 EXPORT_C CExpiredPolicy* CExpiredPolicy::NewLC(const TDesC8& aContract, const TDesC8& aEndpoint,
       
  1852                                              const TDesC8& aUri, const TDesC8& aProviderId)
       
  1853 {
       
  1854     CExpiredPolicy* pNew = new (ELeave) CExpiredPolicy();
       
  1855     CleanupStack::PushL(pNew);
       
  1856     pNew->ConstructL(aContract, aEndpoint, aUri, aProviderId);
       
  1857     return pNew;
       
  1858 }
       
  1859 EXPORT_C CExpiredPolicy::~CExpiredPolicy()
       
  1860 {
       
  1861     delete iContract; //service contract
       
  1862     delete iEndpoint; //service contract
       
  1863     delete iURI; //uri to download
       
  1864     delete iProvideId;
       
  1865     delete iContents; //contents
       
  1866 
       
  1867 }
       
  1868 
       
  1869 EXPORT_C TPtrC8  CExpiredPolicy::Contract()
       
  1870 {
       
  1871     return  iContract->Des();  
       
  1872 }
       
  1873 EXPORT_C TPtrC8  CExpiredPolicy::Endpoint()
       
  1874 {
       
  1875     return iEndpoint->Des();    
       
  1876 }
       
  1877 EXPORT_C TPtrC8  CExpiredPolicy::Uri()
       
  1878 {
       
  1879     return iURI->Des();
       
  1880 }
       
  1881 TPtrC8  CExpiredPolicy::ProvideId()
       
  1882 {
       
  1883     return iProvideId->Des();
       
  1884 }
       
  1885 EXPORT_C TPtrC8  CExpiredPolicy::Contents()
       
  1886 {
       
  1887     if(iContents)
       
  1888         return iContents->Des();
       
  1889     else
       
  1890         return KNullDesC8();
       
  1891 }
       
  1892 EXPORT_C TInt    CExpiredPolicy::SetContentsL(const TDesC8& aContent)
       
  1893 {
       
  1894     if(aContent.Length() > 0)
       
  1895         {
       
  1896         if(iContents)
       
  1897             {
       
  1898                 delete iContents;
       
  1899                 iContents = NULL;
       
  1900             }
       
  1901         iContents = aContent.AllocL();
       
  1902         return KErrNone;
       
  1903         }
       
  1904     
       
  1905     return KErrArgument;        
       
  1906 }
       
  1907     
       
  1908 
       
  1909 CExpiredPolicy::CExpiredPolicy() :iContents(NULL)
       
  1910 {
       
  1911     
       
  1912 }
       
  1913 void CExpiredPolicy::ConstructL(const TDesC8& aContract, const TDesC8& aEndpoint, const TDesC8& aUri, const TDesC8& aProviderId)
       
  1914 {
       
  1915 
       
  1916     iContract = aContract.AllocL();
       
  1917     iEndpoint = aEndpoint.AllocL();
       
  1918     iProvideId = aProviderId.AllocL();
       
  1919     iURI = aUri.AllocL();
       
  1920  
       
  1921 }
       
  1922     
       
  1923 CConsumerContainer* CConsumerContainer::NewL(const TDesC8& aUri, const TDesC8& aConsumerId)
       
  1924 {
       
  1925     CConsumerContainer* pNew = NewLC(aUri, aConsumerId);
       
  1926     CleanupStack::Pop(pNew);
       
  1927     return(pNew);
       
  1928 
       
  1929 }
       
  1930 CConsumerContainer* CConsumerContainer::NewLC(const TDesC8& aUri, const TDesC8& aConsumerId)
       
  1931 {
       
  1932     CConsumerContainer* pNew = new (ELeave) CConsumerContainer();
       
  1933     CleanupStack::PushL(pNew);
       
  1934     pNew->ConstructL(aUri, aConsumerId);
       
  1935     return pNew;
       
  1936 
       
  1937 }
       
  1938 CConsumerContainer::~CConsumerContainer()
       
  1939 {
       
  1940     delete iConsumerId;
       
  1941     delete iURI;
       
  1942 
       
  1943 }
       
  1944 TPtrC8  CConsumerContainer::Uri()
       
  1945 {
       
  1946     return iURI->Des();
       
  1947 }
       
  1948 TPtrC8  CConsumerContainer::ConsumerId()
       
  1949 {
       
  1950     return iConsumerId->Des();
       
  1951 }
       
  1952 CConsumerContainer::CConsumerContainer()
       
  1953 {
       
  1954     
       
  1955 }
       
  1956 void CConsumerContainer::ConstructL(const TDesC8& aUri, const TDesC8& aConsumerId)
       
  1957 {
       
  1958     iConsumerId =  aConsumerId.AllocL();
       
  1959     iURI = aUri.AllocL();
       
  1960 }
       
  1961 
       
  1962 
       
  1963 
       
  1964 // End of file