webservices/wsstar/wsstarplugin/src/wsstardictionary.cpp
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2006-2006 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 #include <e32std.h>
       
    28 #include <utf.h>
       
    29 #include <sysutil.h>
       
    30 #include <ezcompressor.h>
       
    31 #include "wsstardictionary.h"
       
    32 #include "wsstarhttpclient.h"
       
    33 #include "wsstarpolicy.h"
       
    34 #include "sendebug.h"
       
    35 #include "senxmldebug.h"
       
    36 #include "senlogger.h"
       
    37 
       
    38 using namespace WSPolicy;
       
    39 namespace
       
    40 {
       
    41     _LIT8(KHttp, "http");
       
    42     _LIT(KDictionaryFile, "dictionary.xml");
       
    43     _LIT8(KDictionaryType, "Type");
       
    44     _LIT8(KDictionaryId, "Id");
       
    45     _LIT8(KDictionaryChkSum, "Chksum");
       
    46     // helper methods          
       
    47     /*
       
    48     void TUint32ToDes(TDes8& aResult, TUint32 aData)
       
    49         {
       
    50         TUint32 part1= aData & 0xff000000 ;
       
    51         part1 = part1 >> 24;
       
    52         TUint32 part2= aData & 0x00ff0000;
       
    53         part2 = part2 >> 16;
       
    54         TUint16 part3= aData & 0x0000ff00;
       
    55         part3 = part3 >> 8;
       
    56         TUint16 part4= aData & 0x000000ff;
       
    57         
       
    58         TUint8 p1(part1);
       
    59         TUint8 p2(part2);
       
    60         TUint8 p3(part3);
       
    61         TUint8 p4(part4);
       
    62         
       
    63         aResult.Append(p1);
       
    64         aResult.Append(p2);
       
    65         aResult.Append(p3);
       
    66         aResult.Append(p4);
       
    67         }
       
    68     */
       
    69     
       
    70     /*
       
    71     TUint32 DesToTUint32(const TDesC8& aData)
       
    72         {
       
    73         TUint32 part1 = aData[0];
       
    74         part1 = part1-48;
       
    75         part1 = part1 << 24;
       
    76         TUint32 part2 = aData[1];
       
    77         part2 = part2-48;
       
    78         part2 = part2 << 16;
       
    79         TUint32 part3 = aData[2];
       
    80         part3 = part3-48;
       
    81         part3 = part3 << 8;
       
    82         TUint32 part4 = aData[3];
       
    83         part4 = part4-48;
       
    84        
       
    85         TUint32 ret = part1+part2+part3+part4;
       
    86         return ret;
       
    87         }
       
    88     */      
       
    89 }
       
    90 //---------------------------------------------------------------------------
       
    91 // Create instance of this class
       
    92 //---------------------------------------------------------------------------
       
    93 //
       
    94 CWSStarDictionary* CWSStarDictionary::NewL(CSIF& aSIF, RFileLogger& aLogger)
       
    95     {
       
    96     
       
    97     CWSStarDictionary* self   = new (ELeave) CWSStarDictionary(aSIF, aLogger);
       
    98     CleanupStack::PushL (self);
       
    99     self->ConstructL();
       
   100     CleanupStack::Pop(self);
       
   101     return self;
       
   102     }
       
   103 
       
   104 
       
   105 //---------------------------------------------------------------------------
       
   106 // Constructor
       
   107 //---------------------------------------------------------------------------
       
   108 //
       
   109 CWSStarDictionary::CWSStarDictionary(CSIF& aSIF, RFileLogger& aLogger) 
       
   110 :iSIF(&aSIF), iLog(aLogger)
       
   111     {
       
   112     }
       
   113 
       
   114 //---------------------------------------------------------------------------
       
   115 // Destructor
       
   116 //---------------------------------------------------------------------------
       
   117 //
       
   118 CWSStarDictionary::~CWSStarDictionary()
       
   119     {
       
   120     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("CWSStarDictionary::~CWSStarDictionary()")));
       
   121     iDictionaires.ResetAndDestroy();
       
   122     delete iSavePath;
       
   123     }
       
   124 
       
   125 //---------------------------------------------------------------------------
       
   126 // void CWSStarDictionary::ConstructL()
       
   127 //---------------------------------------------------------------------------
       
   128 //
       
   129 void CWSStarDictionary::ConstructL()
       
   130     {   
       
   131      TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("CWSStarDictionary::ConstructL()")));
       
   132 
       
   133 #if !defined( EKA2 ) && !defined( RD_SECURE_PRIV_DATA )
       
   134         iSavePath = KPrivateFolder().AllocL();
       
   135 #else
       
   136         TBuf<KMaxPath> privatePath;
       
   137         RFs fss;
       
   138             User::LeaveIfError(fss.Connect());
       
   139             CleanupClosePushL(fss);
       
   140         fss.CreatePrivatePath(EDriveC);
       
   141         fss.PrivatePath(privatePath);
       
   142         iSavePath = privatePath.AllocL();
       
   143         CleanupStack::PopAndDestroy(1);
       
   144 #endif
       
   145     }
       
   146 CZlibDictionary* CWSStarDictionary::Dictionary(const TDesC8& aName)
       
   147 {
       
   148  
       
   149     TInt count(iDictionaires.Count());
       
   150     for (TInt i = 0; i< count; i++)
       
   151     {
       
   152         CZlibDictionary* tempDict = iDictionaires[i];
       
   153         if(tempDict->DictName() == aName)
       
   154         {
       
   155           return tempDict;  
       
   156         }
       
   157     }
       
   158 
       
   159     return NULL;
       
   160 }    
       
   161 
       
   162 TBool CWSStarDictionary::IfDictionaryExist(CZlibDictionary* aDict)
       
   163 {
       
   164  
       
   165     TInt count(iDictionaires.Count());
       
   166     for (TInt i = 0; i< count; i++)
       
   167     {
       
   168         CZlibDictionary* tempDict = iDictionaires[i];
       
   169         if(tempDict->DictName() == aDict->DictName() &&  
       
   170         tempDict->DictCheckSumPtr() == aDict->DictCheckSumPtr()
       
   171         ) //Id and checksum is same, its same dictionary
       
   172         {
       
   173           return ETrue;  
       
   174         }
       
   175     }
       
   176     
       
   177     return EFalse;
       
   178 }
       
   179 
       
   180 //---------------------------------------------------------------------------
       
   181 // void CWSStarDictionary::CreateZlibL
       
   182 // create a CZlibDictionary object from given dictionary inforamtion
       
   183 //---------------------------------------------------------------------------
       
   184 //
       
   185 void CWSStarDictionary::CreateMSZlibL(const TDesC8* name, CSenElement* aDict)
       
   186 {
       
   187 
       
   188     const TDesC8* type = aDict->AttrValue(KMwsType);
       
   189     const TDesC8* checksum = aDict->AttrValue(KMwsId);
       
   190 
       
   191    if(aDict->HasContent())
       
   192     {
       
   193         TPtrC8 uri = aDict->Content();
       
   194             
       
   195         if(name && type && checksum && uri!= KNullDesC8())
       
   196         {
       
   197 
       
   198             CZlibDictionary* dict = CZlibDictionary::NewLC(uri, name, type, checksum);
       
   199             if(dict)
       
   200             {
       
   201                 if(!IfDictionaryExist(dict))
       
   202                 {
       
   203                     iDictionaires.Append(dict);    
       
   204                     CleanupStack::Pop( dict );                    
       
   205                 }
       
   206                 else //exist
       
   207                 {
       
   208 //                Reset only when we know that the uri has been changed
       
   209                
       
   210                     CZlibDictionary* tempDict = Dictionary(dict->DictName());
       
   211                     if(tempDict)                     
       
   212                     {
       
   213                         if( tempDict->DictUri() == dict->DictUri()
       
   214 //                         && tempDict->DictType() == dict->DictType()
       
   215                             )
       
   216                         {
       
   217                             //do nuthhing
       
   218                         }
       
   219                         else
       
   220                         {
       
   221                             tempDict->ResetL(dict);
       
   222                         }
       
   223                     }
       
   224                     CleanupStack::PopAndDestroy( dict );
       
   225                 }
       
   226             }
       
   227             else
       
   228             {
       
   229                 CleanupStack::PopAndDestroy( dict );
       
   230             }
       
   231         }
       
   232         
       
   233     }    
       
   234     
       
   235 }
       
   236 
       
   237 void CWSStarDictionary::CreateZlibL(const TDesC8* name, CSenElement* aDict)
       
   238 {
       
   239     if(!name || ! aDict)
       
   240         return;
       
   241 
       
   242     const TDesC8* type = aDict->AttrValue(KDictionaryType);
       
   243     const TDesC8* checksum = aDict->AttrValue(KDictionaryChkSum);
       
   244 
       
   245    if(aDict->HasContent())
       
   246     {
       
   247         TPtrC8 uri = aDict->Content();
       
   248             
       
   249         if(name && type && checksum && uri!= KNullDesC8())
       
   250         {
       
   251 
       
   252             CZlibDictionary* dict = CZlibDictionary::NewLC(uri, name, type, checksum);
       
   253             if(dict && !IfDictionaryExist(dict))
       
   254             {
       
   255                 iDictionaires.Append(dict);
       
   256                 CleanupStack::Pop( dict );
       
   257                 
       
   258             }
       
   259             else
       
   260             {
       
   261                 CleanupStack::PopAndDestroy( dict );
       
   262             }
       
   263         }
       
   264         
       
   265     }    
       
   266     
       
   267 }
       
   268 
       
   269 //---------------------------------------------------------------------------
       
   270 // TInt CWSStarDictionary::InvokeL
       
   271 // This method invoke the Http download of dictionaries. The dictionaries will 
       
   272 // be downloaded adn saves to private folder of the serene
       
   273 //---------------------------------------------------------------------------
       
   274 //
       
   275 TInt CWSStarDictionary::InvokeL(MSenHandlerContext* iHndlrCtx, CSenWSDescription* aSD)
       
   276     {
       
   277     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("CWSStarDictionary::InvokeL(MSenMessageContext& aCtx)")));
       
   278     TInt error(KErrNotFound);
       
   279 //1..load dictionary.xml file
       
   280 //2..load all of dictionaries and verify it
       
   281     LoadDictionariesL();
       
   282 
       
   283 
       
   284 //3..get dictionary element from policy
       
   285 //only sts policy will be considered at this point
       
   286    CSenElement* ret = CWSStarPolicy::GetElementL(KDictionaries, KNullDesC8(), *iHndlrCtx, aSD);
       
   287     if(ret)
       
   288     {
       
   289         RPointerArray<CSenElement>& children = ret->ElementsL();
       
   290         TInt count = children.Count();
       
   291         for (TInt i =0; i < count; i++)
       
   292         {
       
   293             CSenElement* ele = children[i];
       
   294             if(ele)
       
   295             {
       
   296                 const TDesC8* name = ele->AttrValue(KMwsId);
       
   297                 RPointerArray<CSenElement> dicts;
       
   298                 CleanupClosePushL(dicts);
       
   299                 ele->ElementsL(dicts, KDictionary);
       
   300 
       
   301                 TInt count2 = dicts.Count();
       
   302                 for (TInt j =0; j < count2; j++)
       
   303                 {
       
   304                     CSenElement* dictEle = dicts[j];
       
   305                     if(dictEle)
       
   306                     {
       
   307                        CreateMSZlibL(name,dictEle);
       
   308                        
       
   309                     }
       
   310                     
       
   311                 }
       
   312                 CleanupStack::PopAndDestroy(); //dicts
       
   313                     
       
   314              }
       
   315 
       
   316         }
       
   317 
       
   318     }
       
   319 //4..start putting dictionaries in xml file
       
   320 
       
   321         
       
   322  //5.. now use http to get all invalid dictionaries from iDictionaries
       
   323    
       
   324     TInt count(iDictionaires.Count());
       
   325     TPtrC8 finalUri(KHttp);
       
   326     if(finalUri != KNullDesC8)
       
   327         {
       
   328         TRAP(error, GetHttpDictionaryL(finalUri, iHndlrCtx));
       
   329         }
       
   330     else 
       
   331         {
       
   332         error = KErrPathNotFound;
       
   333         }
       
   334 
       
   335 //6..write iDictionaries to xml file
       
   336     SaveConfigurationL();
       
   337     iDictionaires.ResetAndDestroy(); //we dont need it any more
       
   338     return error;
       
   339     }
       
   340 
       
   341 //---------------------------------------------------------------------------
       
   342 // TInt CWSStarDictionary::SaveL
       
   343 // Saves a dictionary to a file
       
   344 //---------------------------------------------------------------------------
       
   345 //
       
   346 TInt CWSStarDictionary::SaveL(TFileName aFileName, const TDesC8& aContents)
       
   347 {
       
   348     if (aFileName.Length() < 1 || aContents.Length() < 1)
       
   349         return KErrArgument;
       
   350     RFs fss;
       
   351     User::LeaveIfError(fss.Connect());
       
   352     CleanupClosePushL(fss);
       
   353 
       
   354     TInt size = aContents.Length();
       
   355 
       
   356     TInt retVal(SysUtil::FFSSpaceBelowCriticalLevelL(&fss, size));
       
   357     if(retVal == KErrNone)
       
   358         {
       
   359         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"File system check: OK, there is enough space available.");
       
   360         }
       
   361     else
       
   362         {
       
   363         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"File system check: FAILED, NOT enough space available.");
       
   364         CleanupStack::PopAndDestroy(); //fss
       
   365         User::Leave(retVal);
       
   366         }
       
   367         
       
   368         RFile  dictFile;
       
   369         CleanupClosePushL(dictFile);
       
   370 
       
   371         retVal = dictFile.Replace(fss , aFileName, EFileWrite );
       
   372         if(retVal == KErrNone)
       
   373             {
       
   374             retVal = dictFile.Write(aContents);        
       
   375             }
       
   376 
       
   377        
       
   378        CleanupStack::PopAndDestroy(2); //fss, dictFile
       
   379        return retVal;
       
   380 
       
   381 }
       
   382 //---------------------------------------------------------------------------
       
   383 // CWSStarDictionary::SaveFileL
       
   384 // Saves a dictionary to a file
       
   385 //---------------------------------------------------------------------------
       
   386 //
       
   387 
       
   388 void CWSStarDictionary::SaveFileL(const TDesC8& aUri, const TDesC8& aContents)
       
   389 {
       
   390     TFileName filepath(iSavePath->Des());
       
   391 
       
   392     TInt loc = aUri.LocateReverse('/');
       
   393     if(loc != KErrNotFound)
       
   394     {
       
   395         TPtrC8 name = aUri.Right(aUri.Length() - loc - 1);
       
   396         HBufC* nameBuf = HBufC::NewLC(name.Length());
       
   397         TPtr nameBufPtr(nameBuf->Des());
       
   398         TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(nameBufPtr, name);        
       
   399         if(ret == KErrNone)
       
   400         {
       
   401             TFileName filename; 
       
   402             filename.Append(nameBufPtr);
       
   403             filepath.Append(filename);
       
   404             ret = SaveL(filepath, aContents);
       
   405         }
       
   406         CleanupStack::PopAndDestroy(nameBuf);
       
   407     }
       
   408 }
       
   409 //---------------------------------------------------------------------------
       
   410 // TInt CWSStarDictionary::GetHttpDictionaryL
       
   411 //Method to download the dictionaries by HTTP
       
   412 //---------------------------------------------------------------------------
       
   413 //
       
   414 
       
   415 TInt CWSStarDictionary::GetHttpDictionaryL( const TDesC8& aEndpoint, MSenHandlerContext* aHndlrCtx )
       
   416     {
       
   417     if( !aHndlrCtx )
       
   418         {
       
   419         return KErrArgument;
       
   420         }    
       
   421 //find the service description from handlercontext i.e. STS service        
       
   422     TPtrC8 contract(KWSStarSTSContract);
       
   423     RPointerArray<CSenWSDescription> serviceDescriptions;
       
   424     CleanupClosePushL(serviceDescriptions);
       
   425     TInt retVal(KErrNone);
       
   426     TInt leaveCode(KErrNone);
       
   427     TRAP(leaveCode, retVal = aHndlrCtx->GetSenCoreServiceManager()->ServiceDescriptionsL(serviceDescriptions, contract));
       
   428     CSenWSDescription* pSD = NULL;   
       
   429     
       
   430     if(leaveCode == KErrNone && retVal == KErrNone && serviceDescriptions.Count() > 0)
       
   431         {
       
   432          
       
   433         TUint32 iap(0);
       
   434         for(TInt j = 0; j < serviceDescriptions.Count(); j++)
       
   435             {
       
   436             pSD = serviceDescriptions[j];
       
   437             if( pSD )
       
   438                 {
       
   439                 TInt getIapRetCode = pSD->IapId(iap);    
       
   440                 if( getIapRetCode == KErrNone )
       
   441                     {
       
   442                     pSD = serviceDescriptions[j]; // value IAP property over others (temp solution)
       
   443                     break;
       
   444                     }
       
   445                 }
       
   446             }
       
   447         }
       
   448     CleanupStack::PopAndDestroy( );  // serviceDescriptions.Close()
       
   449 
       
   450     CSenWSDescription* pInitializer = CSenWSDescription::NewLC();
       
   451     pInitializer->SetEndPointL(aEndpoint);
       
   452 
       
   453     CWSStarHttpClient* pHttpClient = CWSStarHttpClient::NewLC(*(CSIF*)aHndlrCtx->GetAnyL(HandlerContextKey::KSIF()), pInitializer, *Log()); //*(CSIF*)iSenHandlerContext.GetAny(HandlerContextKey::KSIF()), pInitializer, *Log());//codescannerwarnings
       
   454         
       
   455     // Fix the first URI and request part of the request
       
   456     TInt count( iDictionaires.Count() );
       
   457     for (TInt i = 0; i< count; i++)
       
   458         {
       
   459         CZlibDictionary* tempDict = iDictionaires[i];   
       
   460         if(!tempDict->Verified())
       
   461             {
       
   462         
       
   463             TPtrC8 endpoint(tempDict->DictUri());        
       
   464             TPtrC8 request(endpoint.Right(endpoint.Length() - aEndpoint.Length()));        
       
   465 
       
   466             HBufC8* pResponse = NULL;
       
   467             TInt leaveCode(KErrNone);
       
   468             TInt submitRetCode(KErrNone);
       
   469 
       
   470             TRAP( leaveCode, submitRetCode = pHttpClient->GetHttpL( request, pResponse, pSD ); )
       
   471             pHttpClient->DataTrafficDetails(iDictDetails);
       
   472             if( submitRetCode && retVal == KErrNone )
       
   473                 {
       
   474                 // Store the error code from the 1st failing request 
       
   475                 retVal = submitRetCode;
       
   476                 }    
       
   477             if( leaveCode && retVal == KErrNone )
       
   478                 {
       
   479                 retVal = leaveCode;
       
   480                 }    
       
   481             if(pResponse && pResponse->Length() > 0)
       
   482                 {
       
   483                 CleanupStack::PushL(pResponse);            
       
   484                 TPtrC8 resPtr(pResponse->Des());
       
   485                 if(tempDict->SetDictContentsL(resPtr) == KErrNone)
       
   486                 {
       
   487                     SaveFileL(endpoint, resPtr);
       
   488                     
       
   489                 }
       
   490                 CleanupStack::PopAndDestroy();
       
   491                 }    
       
   492             else // if(retVal == 404)    
       
   493                 {
       
   494                 delete pResponse;
       
   495                 }
       
   496             }
       
   497         } // for each request
       
   498     CleanupStack::PopAndDestroy(pHttpClient); 
       
   499     CleanupStack::PopAndDestroy(pInitializer);  
       
   500     return retVal;
       
   501 }
       
   502 
       
   503 TInt CWSStarDictionary::LoadDictionariesL()
       
   504 {
       
   505     
       
   506     TFileName dictXml(iSavePath->Des());
       
   507     dictXml.Append(KDictionaryFile);
       
   508     
       
   509     CDictXml* dict= CDictXml::NewL();
       
   510     CleanupStack::PushL(dict);
       
   511     TInt fileError = dict->ReadFileL(dictXml);
       
   512     if(fileError == KErrNone)
       
   513     {
       
   514         CSenElement* dictEle = dict->XMLDocL();
       
   515         if(dictEle)
       
   516         {
       
   517             LoadDictionariesL(dictEle);
       
   518         }
       
   519     
       
   520     }
       
   521     CleanupStack::PopAndDestroy(dict);
       
   522     return fileError;    
       
   523 }
       
   524 //---------------------------------------------------------------------------
       
   525 // void CWSStarDictionary::LoadFileL
       
   526 // This method Loads the dictionaries from the files present in private folder.
       
   527 //---------------------------------------------------------------------------
       
   528 //
       
   529 void CWSStarDictionary::LoadFileL(CZlibDictionary* aDict)
       
   530 {
       
   531     TFileName filepath(iSavePath->Des());
       
   532     
       
   533     TPtrC8 uri = aDict->DictUri();
       
   534     TInt loc = uri.LocateReverse('/');
       
   535     
       
   536     if(loc != KErrNotFound)
       
   537     {
       
   538         TPtrC8 name = uri.Right(uri.Length() - loc - 1);
       
   539         HBufC* nameBuf = HBufC::NewLC(name.Length());
       
   540         TPtr nameBufPtr(nameBuf->Des());
       
   541         TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(nameBufPtr, name);        
       
   542         if(ret == KErrNone)
       
   543         {
       
   544             TFileName filename; 
       
   545             filename.Append(nameBufPtr);
       
   546             filepath.Append(filename);
       
   547             {
       
   548                 RFs fss;
       
   549                 User::LeaveIfError(fss.Connect());
       
   550                 CleanupClosePushL(fss);
       
   551                 
       
   552                 RFile  dictFile;
       
   553                 TInt error = dictFile.Open(fss , filepath, EFileRead );
       
   554                 CleanupClosePushL(dictFile);                
       
   555                 if(error == KErrNone)
       
   556                 {
       
   557                     TInt size;
       
   558                     dictFile.Size(size);
       
   559                     if(size > 0)
       
   560                     {
       
   561                         HBufC8 *iBuf=HBufC8::NewLC(size);
       
   562                         TPtr8 buf8ptr(iBuf->Des());
       
   563                         dictFile.Read(buf8ptr,size);
       
   564                         aDict->SetDictContentsL(buf8ptr);
       
   565                         aDict->DeleteDictContents();
       
   566                         CleanupStack::PopAndDestroy(iBuf);
       
   567                         
       
   568                     }
       
   569                 }
       
   570                 CleanupStack::PopAndDestroy(2); //fss, dictFile
       
   571             }
       
   572         }
       
   573         CleanupStack::PopAndDestroy(nameBuf);
       
   574     }
       
   575 }
       
   576 
       
   577 //---------------------------------------------------------------------------
       
   578 // void CWSStarDictionary::LoadDictionariesLs
       
   579 // This method Loads the dictionaries from the private folder. It reads the 
       
   580 // policy file for the possible dictionaries and load those dictionaries from 
       
   581 // priavte folder.
       
   582 //---------------------------------------------------------------------------
       
   583 //
       
   584 TInt CWSStarDictionary::LoadDictionariesL(CSenElement*  aPolicy)
       
   585 {
       
   586     if(aPolicy)
       
   587     {
       
   588         RPointerArray<CSenElement>& children = aPolicy->ElementsL();
       
   589         TInt count = children.Count();
       
   590         for (TInt i =0; i < count; i++)
       
   591         {
       
   592             CSenElement* ele = children[i];
       
   593             if(ele)
       
   594             {
       
   595                 const TDesC8* name = ele->AttrValue(KDictionaryId);
       
   596                 CreateZlibL(name,ele);
       
   597                        
       
   598             }
       
   599         }
       
   600         
       
   601     }
       
   602 
       
   603     TInt count(iDictionaires.Count());
       
   604     for (TInt i = 0; i< count; i++)
       
   605     {
       
   606         CZlibDictionary* tempDict = iDictionaires[i];
       
   607         if(tempDict)
       
   608             LoadFileL(tempDict);
       
   609     }
       
   610 
       
   611     return KErrNone;
       
   612 }
       
   613 TInt CWSStarDictionary::SaveConfigurationL()
       
   614 {
       
   615     TInt error(KErrNone);
       
   616     HBufC8* ret = WriteAllAsXMlL();
       
   617     CleanupStack::PushL(ret);
       
   618     if(ret)
       
   619     {
       
   620         TPtrC filename(KDictionaryFile);
       
   621         TPtrC8 dataptr(ret->Des());
       
   622         error = SaveL(filename, dataptr);
       
   623     }
       
   624     CleanupStack::PopAndDestroy(ret);
       
   625     return error;   
       
   626 }
       
   627 
       
   628 TPtrC8 CWSStarDictionary::WriteToBufL(CBufBase& aBuf)
       
   629     {
       
   630     RBufWriteStream bufWs(aBuf);
       
   631     CleanupClosePushL(bufWs);
       
   632     WriteL(bufWs);
       
   633     CleanupStack::PopAndDestroy(); // bufWs.Close();
       
   634     return aBuf.Ptr(0);
       
   635     }
       
   636 HBufC8* CWSStarDictionary::WriteAllAsXMlL()
       
   637 {
       
   638     CBufFlat *pBuf = CBufFlat::NewL(128);
       
   639     CleanupStack::PushL(pBuf);
       
   640     TPtrC8 p = WriteToBufL(*pBuf);
       
   641     HBufC8* pRet = p.AllocL();
       
   642     CleanupStack::PopAndDestroy(); // pBuf;
       
   643     return pRet;    
       
   644 }
       
   645          
       
   646 TInt CWSStarDictionary::WriteL(RWriteStream& aWriteStream)
       
   647 {
       
   648     aWriteStream.WriteL(KSenLessThan);
       
   649     aWriteStream.WriteL(KDictionaries);
       
   650     aWriteStream.WriteL(KSenGreaterThan);
       
   651     
       
   652     TInt count(iDictionaires.Count());
       
   653     for (TInt i = 0; i< count; i++)
       
   654     {
       
   655         CZlibDictionary* tempDict = iDictionaires[i];
       
   656         TPtrC8 endpoint(tempDict->DictUri());
       
   657         TPtrC8 type(tempDict->DictType());
       
   658         TPtrC8 id(tempDict->DictName());
       
   659         TPtrC8 chksum(tempDict->DictCheckSumPtr());
       
   660           
       
   661         if(id != KNullDesC8() && endpoint != KNullDesC8() && type != KNullDesC8() && chksum != KNullDesC8)
       
   662         {
       
   663             aWriteStream.WriteL(KSenLessThan);
       
   664             aWriteStream.WriteL(KDictionary);
       
   665             aWriteStream.WriteL(KSenSpace);
       
   666             aWriteStream.WriteL(KDictionaryId);
       
   667             aWriteStream.WriteL(KSenEqualsDblQuot);
       
   668             aWriteStream.WriteL(id);
       
   669             aWriteStream.WriteL(KSenDblQuot);
       
   670             aWriteStream.WriteL(KSenSpace);            
       
   671             aWriteStream.WriteL(KDictionaryType);
       
   672             aWriteStream.WriteL(KSenEqualsDblQuot);
       
   673             aWriteStream.WriteL(type);
       
   674             aWriteStream.WriteL(KSenDblQuot);
       
   675             aWriteStream.WriteL(KSenSpace);
       
   676             aWriteStream.WriteL(KDictionaryChkSum);
       
   677             aWriteStream.WriteL(KSenEqualsDblQuot);
       
   678             aWriteStream.WriteL(chksum);
       
   679             aWriteStream.WriteL(KSenDblQuot);
       
   680             aWriteStream.WriteL(KSenSpace);
       
   681             aWriteStream.WriteL(KSenGreaterThan);
       
   682             aWriteStream.WriteL(endpoint);
       
   683             
       
   684             aWriteStream.WriteL(KSenLessThanSlash);
       
   685             aWriteStream.WriteL(KDictionary);
       
   686             aWriteStream.WriteL(KSenGreaterThan);
       
   687 
       
   688         }
       
   689     }
       
   690 
       
   691     aWriteStream.WriteL(KSenLessThanSlash);
       
   692     aWriteStream.WriteL(KDictionaries);
       
   693 	aWriteStream.WriteL(KSenGreaterThan);
       
   694 
       
   695 return KErrNone;    
       
   696 }
       
   697 void CWSStarDictionary::DataTrafficDetails(TSenDataTrafficDetails& aDetails) 		
       
   698 	{
       
   699 		aDetails = iDictDetails;
       
   700 	}
       
   701 //---------------------------------------------------------------------------
       
   702 // CWSStarDictionary::Log()
       
   703 //---------------------------------------------------------------------------
       
   704 //    
       
   705 RFileLogger* CWSStarDictionary::Log() const
       
   706     {
       
   707     return &iLog;
       
   708 //        return ((RFileLogger*)iHandlerContext.GetAny(HandlerContextKey::KLogger));
       
   709     }
       
   710 
       
   711 
       
   712 //---------------------------------------------------------------------------
       
   713 // A simple Dictionary Object and its implementation
       
   714 //---------------------------------------------------------------------------
       
   715 //
       
   716 CZlibDictionary* CZlibDictionary::NewL( const TDesC8& aDictUri, 
       
   717                                         const TDesC8* aDictName, 
       
   718                                         const TDesC8* aDictType, 
       
   719                                         const TDesC8* aDictCheckSum )
       
   720 {
       
   721     CZlibDictionary* zlib   = CZlibDictionary::NewLC(aDictUri, aDictName, aDictType, aDictCheckSum);
       
   722     CleanupStack::Pop();
       
   723     return zlib;
       
   724 }
       
   725                                 
       
   726 CZlibDictionary* CZlibDictionary::NewLC( const TDesC8& aDictUri, 
       
   727                                          const TDesC8* aDictName, 
       
   728                                          const TDesC8* aDictType, 
       
   729                                          const TDesC8* aDictCheckSum )
       
   730 {
       
   731 
       
   732     CZlibDictionary* zlib   = new (ELeave) CZlibDictionary();
       
   733     CleanupStack::PushL (zlib);
       
   734     zlib->ConstructL(aDictUri, aDictName, aDictType, aDictCheckSum);
       
   735     return zlib;
       
   736     
       
   737 }
       
   738 
       
   739 CZlibDictionary::~CZlibDictionary()
       
   740 {
       
   741     delete iDictUri; //uri to download
       
   742     delete iDictName; //name or mws:Id tag
       
   743     delete iDictType; //zlib or mws:type
       
   744     delete iDictContents; //contents
       
   745     delete iDictCheckSum; //provided checksum  
       
   746 }
       
   747 CZlibDictionary::CZlibDictionary()
       
   748 :iVerfied(EFalse)
       
   749 {
       
   750 }
       
   751 
       
   752 void CZlibDictionary::ConstructL( const TDesC8& aDictUri, 
       
   753                                   const TDesC8* aDictName,
       
   754                                   const TDesC8* aDictType, 
       
   755                                   const TDesC8* aDictCheckSum )
       
   756     {
       
   757     iDictUri = aDictUri.AllocL();
       
   758     iDictName = aDictName->AllocL(); 
       
   759     iDictType = aDictType->AllocL();
       
   760     iDictCheckSum = aDictCheckSum->AllocL();
       
   761     }
       
   762     
       
   763 TBool  CZlibDictionary::Verified()
       
   764 {
       
   765     return iVerfied;
       
   766 }
       
   767 TPtrC8  CZlibDictionary::DictUri()
       
   768 {
       
   769     return iDictUri->Des();    
       
   770 }
       
   771 TPtrC8  CZlibDictionary::DictName()
       
   772 {
       
   773     return iDictName->Des();
       
   774 }
       
   775 TPtrC8  CZlibDictionary::DictType()
       
   776 {
       
   777     return iDictType->Des();
       
   778 }
       
   779 TPtrC8  CZlibDictionary::DictContents()
       
   780 {
       
   781     return iDictContents->Des();
       
   782 }
       
   783 
       
   784 TUint32 CZlibDictionary::DictCheckSum()
       
   785     {
       
   786     return iChkSum;
       
   787     }
       
   788 
       
   789 TPtrC8 CZlibDictionary::DictCheckSumPtr()
       
   790     {
       
   791     return iDictCheckSum->Des();
       
   792     }
       
   793 
       
   794 TInt CZlibDictionary::VerifyContentsL(const TDesC8& aDictContents)
       
   795 {
       
   796     TPtrC8 dictChk(iDictCheckSum->Des());
       
   797     
       
   798     TLex8 lex(dictChk);
       
   799     TUint32 givenChkSum = 0;
       
   800     TInt error = lex.Val(givenChkSum, EDecimal);
       
   801 
       
   802     if(error == KErrNone)
       
   803     {
       
   804         if(givenChkSum == 0)
       
   805             return KErrArgument;
       
   806         
       
   807         iChkSum = givenChkSum;
       
   808         TUint32 calc_chksum(0);
       
   809         calc_chksum = adler32(calc_chksum,0,0);		// init Adler32 checksum
       
   810         calc_chksum = adler32(calc_chksum,aDictContents.Ptr(),aDictContents.Length()); // compute Adler32 checksum
       
   811         if(givenChkSum == calc_chksum) 
       
   812         {
       
   813             return KErrNone;
       
   814         }        
       
   815     }
       
   816     return error;
       
   817 }
       
   818 TInt CZlibDictionary::DeleteDictContents()
       
   819 {
       
   820     if(iDictContents)
       
   821     {
       
   822         delete iDictContents;
       
   823         iDictContents = NULL;
       
   824     }
       
   825     return KErrNone;      
       
   826 }
       
   827 TInt CZlibDictionary::SetDictContentsL(const TDesC8& aDictContents)
       
   828 {
       
   829     if(aDictContents == KNullDesC8())    
       
   830         return KErrArgument;
       
   831 
       
   832     if (VerifyContentsL(aDictContents) == KErrNone)
       
   833     {
       
   834         iVerfied = ETrue;
       
   835         if(iDictContents)
       
   836         {
       
   837             delete iDictContents;
       
   838             iDictContents = NULL;
       
   839         }
       
   840         
       
   841         iDictContents = aDictContents.AllocL();
       
   842         return KErrNone;
       
   843     }
       
   844     
       
   845     return KErrGeneral;
       
   846 }
       
   847 
       
   848 TInt CZlibDictionary::ResetL(CZlibDictionary* aDict)
       
   849 {
       
   850     delete iDictUri; 
       
   851     iDictUri = NULL;
       
   852     delete iDictName; 
       
   853     iDictName = NULL;
       
   854     delete iDictType; 
       
   855     iDictType = NULL;
       
   856     delete iDictContents;
       
   857 	iDictContents = NULL;
       
   858     delete iDictCheckSum;
       
   859     iDictCheckSum = NULL;
       
   860     iDictUri = aDict->DictUri().AllocL();
       
   861     iDictName = aDict->DictName().AllocL();
       
   862     iDictType = aDict->DictType().AllocL();
       
   863     iDictCheckSum = aDict->DictCheckSumPtr().AllocL();
       
   864     iVerfied = EFalse;
       
   865     iChkSum = 0;
       
   866     return KErrNone;
       
   867 }
       
   868 
       
   869 CDictXml* CDictXml::NewL()
       
   870 {
       
   871      CDictXml* pSelf = CDictXml::NewLC();
       
   872     CleanupStack::Pop(pSelf);
       
   873     return pSelf;
       
   874 }
       
   875 CDictXml* CDictXml::NewLC()
       
   876 {
       
   877        CDictXml* pSelf = new (ELeave) CDictXml();
       
   878     CleanupStack::PushL(pSelf);
       
   879     pSelf->ConstructL();
       
   880     return pSelf;
       
   881   
       
   882 }
       
   883 CDictXml::~CDictXml()
       
   884 {
       
   885   delete iXmlReader;
       
   886 }
       
   887 CDictXml::CDictXml() :iParsed(EFalse)
       
   888 {
       
   889     
       
   890 }
       
   891 void CDictXml::ConstructL()
       
   892 {
       
   893   iXmlReader = CSenXmlReader::NewL(KXmlParserMimeType); // use libxml2 sax parser
       
   894 }
       
   895 
       
   896 CSenElement* CDictXml::XMLDocL()
       
   897 {
       
   898     if(iParsed)
       
   899     {
       
   900         CSenElement* ele = &AsElement();
       
   901         if(ele)
       
   902             return ele;
       
   903     }
       
   904     
       
   905     return NULL;
       
   906 }
       
   907 TInt CDictXml::ReadFileL(TFileName aPath)
       
   908 {
       
   909       RFs fss;
       
   910        User::LeaveIfError(fss.Connect());
       
   911         CleanupClosePushL(fss);
       
   912 
       
   913     TFileName iFileName1(aPath);
       
   914     RFile  xmlFile;
       
   915     TInt error = xmlFile.Open(fss , iFileName1, EFileRead );
       
   916     CleanupClosePushL(xmlFile);    
       
   917 
       
   918     if(error == KErrNone)
       
   919     {
       
   920         error = KErrGeneral;
       
   921                 
       
   922         TInt size;
       
   923         xmlFile.Size(size);
       
   924 
       
   925         HBufC8 *pBuf=HBufC8::NewL(size);	//CodeScannerWarnings
       
   926         CleanupStack::PushL(pBuf);
       
   927         TPtr8 buf8ptr(pBuf->Des());
       
   928         xmlFile.Read(buf8ptr,size);
       
   929         size = pBuf->Length();
       
   930         if(size > 0)
       
   931             {
       
   932             TPtr8 buf8ptr(pBuf->Des());            
       
   933             TRAP( error, ParseL(buf8ptr);)
       
   934             if (error == KErrNone)
       
   935                 iParsed = ETrue;
       
   936             }
       
   937         CleanupStack::PopAndDestroy(pBuf);
       
   938     }
       
   939     CleanupStack::PopAndDestroy(2); // fss  , xmlFile  
       
   940     return error;    
       
   941 } 
       
   942 
       
   943 // END OF FILE
       
   944