vpnengine/dmadengine/src/dmadutil.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2002-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: Implementation of TDmAdUtil.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <utf.h>
       
    21 #include "dmadutil.h"
       
    22 #include "vpnlogger.h"
       
    23 
       
    24 //-----------------------------------------------------------------------   
       
    25 
       
    26 DMAD_EXPORT_C TInt TDmAdUtil::DesToInt(const TDesC& aDes)
       
    27     {
       
    28     if (aDes.Length() == 0)
       
    29         {
       
    30         return 0;
       
    31         }
       
    32     TLex16 lex(aDes);
       
    33     TInt value = 0;
       
    34     if (lex.Val(value) != KErrNone)
       
    35         {
       
    36         ;
       
    37         }
       
    38     return value;
       
    39     }
       
    40 
       
    41 DMAD_EXPORT_C TInt TDmAdUtil::DesToInt(const TDesC8& aDes)
       
    42     {
       
    43     if (aDes.Length() == 0)
       
    44         {
       
    45         return 0;
       
    46         }
       
    47     TLex8 lex(aDes);
       
    48     TInt value = 0;
       
    49     if (lex.Val(value) != KErrNone)
       
    50         {
       
    51         ;
       
    52         }
       
    53     return value;
       
    54     }
       
    55 
       
    56 DMAD_EXPORT_C HBufC* TDmAdUtil::IntToDesLC(const TInt aInt)
       
    57     {
       
    58     HBufC* buf = HBufC::NewLC(10); //10 = max length of 32bit integer
       
    59     TPtr ptrBuf = buf->Des();
       
    60     ptrBuf.Num(aInt);
       
    61     return buf;
       
    62     }
       
    63 
       
    64 DMAD_EXPORT_C HBufC8* TDmAdUtil::IntToDes8LC(const TInt aInt)
       
    65     {
       
    66     HBufC8* buf = HBufC8::NewLC(10); //10 = max length of 32bit integer
       
    67     TPtr8 ptrBuf = buf->Des();
       
    68     ptrBuf.Num(aInt);
       
    69     return buf;
       
    70     }
       
    71 
       
    72 DMAD_EXPORT_C HBufC* TDmAdUtil::IntToDesLC(const TInt32 aInt32)
       
    73     {
       
    74     return IntToDesLC((TInt)aInt32);
       
    75     }
       
    76     
       
    77 DMAD_EXPORT_C HBufC8* TDmAdUtil::IntToDes8LC(const TInt32 aInt32)
       
    78     {
       
    79     return IntToDes8LC((TInt)aInt32);
       
    80     }
       
    81     
       
    82 //-----------------------------------------------------------------------
       
    83     
       
    84 DMAD_EXPORT_C TUint TDmAdUtil::DesToUint(const TDesC& aDes)
       
    85     {
       
    86     if (aDes.Length() == 0)
       
    87         {
       
    88         return 0;
       
    89         }
       
    90     TLex16 lex(aDes);
       
    91     TUint value = 0;
       
    92     if (lex.Val(value) != KErrNone)
       
    93         {
       
    94         ;
       
    95         }
       
    96     return value;
       
    97     }
       
    98 
       
    99 DMAD_EXPORT_C TUint TDmAdUtil::DesToUint(const TDesC8& aDes)
       
   100     {
       
   101     if (aDes.Length() == 0)
       
   102         {
       
   103         return 0;
       
   104         }
       
   105     TLex8 lex(aDes);
       
   106     TUint value = 0;
       
   107     if (lex.Val(value) != KErrNone)
       
   108         {
       
   109         ;
       
   110         }
       
   111     return value;
       
   112     }
       
   113 
       
   114 DMAD_EXPORT_C HBufC* TDmAdUtil::IntToDesLC(const TUint aUint)
       
   115     {
       
   116     HBufC* buf = HBufC::NewLC(10); //10 = max length of 32bit integer
       
   117     TPtr ptrBuf = buf->Des();
       
   118     ptrBuf.Num(aUint);
       
   119     return buf;
       
   120     }
       
   121 
       
   122 DMAD_EXPORT_C HBufC8* TDmAdUtil::IntToDes8LC(const TUint aUint)
       
   123     {
       
   124     HBufC8* buf = HBufC8::NewLC(10); //10 = max length of 32bit integer
       
   125     TPtr8 ptrBuf = buf->Des();
       
   126     ptrBuf.Num(aUint);
       
   127     return buf;
       
   128     }
       
   129     
       
   130 DMAD_EXPORT_C HBufC* TDmAdUtil::IntToDesLC(const TUint32 aUint32)
       
   131     {
       
   132     return IntToDesLC((TUint)aUint32);
       
   133     }
       
   134 
       
   135 DMAD_EXPORT_C HBufC8* TDmAdUtil::IntToDes8LC(const TUint32 aUint32)
       
   136     {
       
   137     return IntToDes8LC((TUint)aUint32);
       
   138     }
       
   139     
       
   140 //-----------------------------------------------------------------------   
       
   141     
       
   142 DMAD_EXPORT_C TPtrC8 TDmAdUtil::LastUriSeg(const TDesC8& aUri)
       
   143     {
       
   144     TInt i;
       
   145     for (i=aUri.Length()-1; i>=0; i--)
       
   146         {
       
   147         if (aUri[i] == '/')
       
   148             {
       
   149             break;
       
   150             }
       
   151         }
       
   152     if (i==0)
       
   153         {
       
   154         return aUri;
       
   155         }
       
   156     else
       
   157         {
       
   158         return aUri.Mid(i+1);
       
   159         }
       
   160     }
       
   161 
       
   162 DMAD_EXPORT_C TPtrC8 TDmAdUtil::FirstUriSeg(const TDesC8& aUri)
       
   163     {
       
   164     TInt i;
       
   165     TBool found = EFalse;
       
   166     for (i=0; i<aUri.Length(); i++)
       
   167         {
       
   168         if (aUri[i] == '/')
       
   169             {
       
   170             found = ETrue;
       
   171             break;
       
   172             }
       
   173         }
       
   174     if (found)
       
   175         {
       
   176         return aUri.Left(i);
       
   177         }
       
   178     else
       
   179         {
       
   180         return aUri;
       
   181         }
       
   182     }
       
   183 
       
   184 DMAD_EXPORT_C TPtrC8 TDmAdUtil::RemoveLastUriSeg(const TDesC8& aUri)
       
   185     {
       
   186     TInt i;
       
   187     for (i=aUri.Length()-1; i>=0; i--)
       
   188         {
       
   189         if (aUri[i] == '/')
       
   190             {
       
   191             break;
       
   192             }
       
   193         }
       
   194     return aUri.Left(i);
       
   195     }
       
   196 
       
   197 DMAD_EXPORT_C TInt TDmAdUtil::NumOfURISegs(const TDesC8& aUri)
       
   198     {
       
   199     TPtrC8 uri(TDmAdUtil::RemoveDotSlash(aUri));
       
   200     TInt numOfURISegs = 1;
       
   201     for (TInt i=0; i<uri.Length(); i++)
       
   202         {
       
   203         if (uri[i] == '/')
       
   204             {
       
   205             numOfURISegs++;
       
   206             }
       
   207         }
       
   208     return numOfURISegs;
       
   209     }
       
   210 
       
   211 DMAD_EXPORT_C TPtrC8 TDmAdUtil::RemoveDotSlash(const TDesC8& aUri)
       
   212     {
       
   213     if (aUri.Find(KDmAdUriDotSlash) == 0)
       
   214         {
       
   215         return aUri.Right(aUri.Length() - KDmAdUriDotSlash().Length());
       
   216         }
       
   217     else
       
   218         {
       
   219         return aUri;
       
   220         }
       
   221     }
       
   222 
       
   223 DMAD_EXPORT_C TInt TDmAdUtil::CompareUris(const TDesC8& aUri1, const TDesC8& aUri2)
       
   224     {
       
   225     TPtrC8 uri1(TDmAdUtil::RemoveDotSlash(aUri1));
       
   226     TPtrC8 uri2(TDmAdUtil::RemoveDotSlash(aUri2));
       
   227     return uri1.Compare(uri2);
       
   228     }
       
   229 
       
   230 DMAD_EXPORT_C HBufC8* TDmAdUtil::BuildUriLC(const TDesC8& aUriPath, const TDesC8& aUriSeg)
       
   231     {
       
   232     HBufC8* uri = BuildUriL(aUriPath, aUriSeg);
       
   233     CleanupStack::PushL(uri);
       
   234     return uri;
       
   235     }
       
   236     
       
   237 DMAD_EXPORT_C HBufC8* TDmAdUtil::BuildUriL(const TDesC8& aUriPath, const TDesC8& aUriSeg)
       
   238     {
       
   239     HBufC8* uri = HBufC8::NewL(aUriPath.Length() + 1 + aUriSeg.Length());
       
   240     TPtr8 uriDesc = uri->Des();
       
   241     uriDesc.Copy(aUriPath);
       
   242     uriDesc.Append(KDmAdSeparator);
       
   243     uriDesc.Append(aUriSeg);
       
   244     return uri;
       
   245     }
       
   246 
       
   247 DMAD_EXPORT_C MSmlDmAdapter::TError TDmAdUtil::MapStatusCode(TInt aStatus)
       
   248     {
       
   249     TRACE("TDmAdUtil::MapStatusCode");
       
   250     
       
   251     
       
   252     MSmlDmAdapter::TError dmStatus;
       
   253     if (aStatus == KErrNone)
       
   254         {
       
   255         dmStatus = MSmlDmAdapter::EOk;
       
   256         }
       
   257     else if (aStatus == KErrNotFound)
       
   258         {
       
   259         dmStatus = MSmlDmAdapter::ENotFound;
       
   260         }
       
   261     else if (aStatus == KErrCorrupt)
       
   262         {
       
   263         dmStatus = MSmlDmAdapter::EInvalidObject;
       
   264         }
       
   265     else if (aStatus == KErrAlreadyExists)
       
   266         {
       
   267         dmStatus = MSmlDmAdapter::EAlreadyExists;
       
   268         }
       
   269     else if (aStatus == KErrTooBig)
       
   270         {
       
   271         dmStatus = MSmlDmAdapter::ETooLargeObject;
       
   272         }
       
   273     else if (aStatus == KErrDiskFull)
       
   274         {
       
   275         dmStatus = MSmlDmAdapter::EDiskFull;
       
   276         }
       
   277     /*
       
   278                 ERollbackFailed,
       
   279                 EObjectInUse,
       
   280                 ENoMemory,
       
   281                 ECommitOK,
       
   282                 ERollbackOK,
       
   283                 ECommitFailed
       
   284     */
       
   285     else
       
   286         {
       
   287         dmStatus = MSmlDmAdapter::EError;
       
   288         }
       
   289     return dmStatus;
       
   290     }
       
   291 
       
   292 DMAD_EXPORT_C void TDmAdUtil::ParseUriLC(const TDesC8& aUri, CArrayFix<TPtrC8>*& aUriSegList)
       
   293     {
       
   294     TPtrC8 uri(TDmAdUtil::RemoveDotSlash(aUri));
       
   295     
       
   296     CArrayFix<TPtrC8>* uriSegList;
       
   297     uriSegList = new (ELeave) CArrayFixFlat<TPtrC8>(8);
       
   298     CleanupStack::PushL(uriSegList);
       
   299 
       
   300     TPtrC8 seg;
       
   301     TPtrC8 curr(uri);
       
   302 
       
   303     while (curr.Length() > 0)
       
   304         {
       
   305         TInt offset = curr.Locate('/');
       
   306         if (offset == KErrNotFound)
       
   307             {
       
   308             seg.Set(curr);
       
   309             curr.Set(KNullDesC8);
       
   310             }
       
   311         else
       
   312             {
       
   313             seg.Set(curr.Left(offset));
       
   314             TInt rightLth = curr.Length() - offset - 1;
       
   315             if (rightLth <= 0)           
       
   316                 {
       
   317                 DEBUG_LOG(_L("TDmAdUtil::ParseUriLC: corrupted uri"));
       
   318                 DEBUG_LOG1(_L8("URI: %S"), &aUri);
       
   319                 
       
   320                 User::Leave(KErrGeneral);
       
   321                 }
       
   322             curr.Set(curr.Right(rightLth));
       
   323             }
       
   324         uriSegList->AppendL(seg);
       
   325         }
       
   326     
       
   327     aUriSegList = uriSegList;
       
   328     }
       
   329 
       
   330 DMAD_EXPORT_C HBufC8* TDmAdUtil::LuidTo8L(const TDesC& aLuid16)
       
   331     {
       
   332     HBufC8* luid8 = HBufC8::NewL(aLuid16.Length());
       
   333     luid8->Des().Copy(aLuid16);
       
   334     return luid8;
       
   335     }
       
   336     
       
   337 DMAD_EXPORT_C HBufC* TDmAdUtil::LuidTo16L(const TDesC8& aLuid8)
       
   338     {
       
   339     HBufC* luid16 = HBufC::NewL(aLuid8.Length());
       
   340     luid16->Des().Copy(aLuid8);
       
   341     return luid16;
       
   342     }
       
   343 
       
   344 //-----------------------------------------------------------------------   
       
   345     
       
   346 DMAD_EXPORT_C void TDmAdUtil::FillNodeInfoL(MSmlDmDDFObject&               aNode,
       
   347                                             TSmlDmAccessTypes              aAccTypes,
       
   348                                             MSmlDmDDFObject::TOccurence    aOccurrence,
       
   349                                             MSmlDmDDFObject::TScope        aScope,
       
   350                                             MSmlDmDDFObject::TDFFormat     aFormat,
       
   351                                             const TDesC8&                  aDescription,
       
   352                                             TBool                          aObjectGroup,
       
   353                                             const TDesC8&                  aMimeType)
       
   354 {
       
   355     aNode.SetAccessTypesL(aAccTypes);
       
   356     aNode.SetOccurenceL(aOccurrence);
       
   357     aNode.SetScopeL(aScope);
       
   358     aNode.SetDFFormatL(aFormat);
       
   359 
       
   360     if (aMimeType.Length() > 0)
       
   361         {
       
   362         aNode.AddDFTypeMimeTypeL(aMimeType);
       
   363         }
       
   364 
       
   365     aNode.SetDescriptionL(aDescription);
       
   366 
       
   367     (void)aObjectGroup;
       
   368     }
       
   369 
       
   370 DMAD_EXPORT_C MSmlDmDDFObject& TDmAdUtil::AddChildObjectL(MSmlDmDDFObject& aNode, const TDesC8& aNodeName)
       
   371     {
       
   372     if (aNodeName.Length() == 0)
       
   373         {
       
   374         MSmlDmDDFObject& object = aNode.AddChildObjectGroupL();
       
   375         return object;
       
   376         }
       
   377     else
       
   378         {
       
   379         MSmlDmDDFObject& object = aNode.AddChildObjectL(aNodeName);
       
   380         return object;
       
   381         }
       
   382     }
       
   383 
       
   384 DMAD_EXPORT_C HBufC8* TDmAdUtil::BuildLocallyCreatedRtNodeUriSegLC(TInt& aLargest)
       
   385     {
       
   386     ++aLargest;
       
   387     HBufC8* number = TDmAdUtil::IntToDes8LC(aLargest);
       
   388     HBufC8* uri = HBufC8::NewL(KDmAdLocallyCreatedRtNodeUriSegPrefix().Length() + number->Length());
       
   389     uri->Des().Copy(KDmAdLocallyCreatedRtNodeUriSegPrefix);
       
   390     uri->Des().Append(*number);
       
   391     CleanupStack::PopAndDestroy(); // number
       
   392     CleanupStack::PushL(uri);
       
   393     return uri;
       
   394     }
       
   395 
       
   396 TInt TDmAdUtil::FindLargestLocallyCreated(const CArrayFix<TSmlDmMappingInfo>& aPreviousUriSegmentList)
       
   397     {
       
   398     TRACE("TDmAdUtil::FindLargestLocallyCreated");
       
   399     TInt largest = 0;
       
   400     for (TInt i=0; i < aPreviousUriSegmentList.Count(); i++)
       
   401         {
       
   402         const TSmlDmMappingInfo& mappingInfo = aPreviousUriSegmentList.At(i);        
       
   403         if (mappingInfo.iURISeg.Find(KDmAdLocallyCreatedRtNodeUriSegPrefix) == 0)
       
   404             {            
       
   405             TPtrC8 numberPart(mappingInfo.iURISeg.Mid(KDmAdLocallyCreatedRtNodeUriSegPrefix().Length()));
       
   406             TInt number = TDmAdUtil::DesToInt(numberPart);
       
   407             if (number > largest)
       
   408                 {
       
   409                 largest = number;
       
   410                 }
       
   411             }
       
   412         }
       
   413     return largest;
       
   414     }
       
   415     
       
   416     
       
   417 void PointerArrayCleanup(TAny* item)
       
   418     {
       
   419     RPointerArray<HBufC8>* a = static_cast<RPointerArray<HBufC8>*>(item);
       
   420     a->ResetAndDestroy();
       
   421     a->Close();
       
   422     }
       
   423 
       
   424 
       
   425 #define DMAD_DUMP_PREVIOUS_URI_SEGMENT_LIST
       
   426     
       
   427 DMAD_EXPORT_C void TDmAdUtil::BuildRtNodeChildUriListL(MDmAdCallBack*                          aDmAdCallBack,
       
   428                                                   MDmAdStoreApi*                          aStoreApi,
       
   429                                                   const TDesC8&                           aUri,
       
   430                                                   const TDesC8&                           aParentLuid,
       
   431                                                   const CArrayFix<TSmlDmMappingInfo>&     aPreviousUriSegmentList,
       
   432                                                   CBufBase&                               aCurrentList)
       
   433     {
       
   434 #ifdef DMAD_DUMP_PREVIOUS_URI_SEGMENT_LIST
       
   435         DEBUG_LOG(_L("BuildRtNodeChildUriListL:"));
       
   436         {
       
   437         for (TInt i=0; i < aPreviousUriSegmentList.Count(); i++)
       
   438             {
       
   439             const TSmlDmMappingInfo& mappingInfo = aPreviousUriSegmentList.At(i);
       
   440                         
       
   441             DEBUG_LOG1(_L("entry %d:"), i);            
       
   442             DEBUG_LOG1(_L8("Uri: %S"), &(mappingInfo.iURISeg));
       
   443             DEBUG_LOG_HEX(mappingInfo.iURISegLUID);
       
   444             
       
   445             }
       
   446         }
       
   447 #endif
       
   448 
       
   449     RPointerArray<HBufC8> luidList;
       
   450     CleanupStack::PushL(TCleanupItem(PointerArrayCleanup, &luidList));
       
   451     
       
   452     aStoreApi->LuidListL(aUri, aParentLuid, luidList);
       
   453 
       
   454 
       
   455     // Finds largest number used in cli<x> named nodes.
       
   456     TInt largest = FindLargestLocallyCreated(aPreviousUriSegmentList);
       
   457     DEBUG_LOG1(_L("largest is cli%d"), largest);
       
   458     
       
   459     TInt countLuidList = luidList.Count();
       
   460     for (TInt j=0; j < countLuidList; j++)
       
   461         {
       
   462         const HBufC8* luidElem = luidList[j];
       
   463 
       
   464         HBufC8* uriSeg = 0;
       
   465         
       
   466         //Tries to find the luid from the aPreviousUriSegmentList
       
   467         for (TInt i=0; i < aPreviousUriSegmentList.Count(); i++)
       
   468             {
       
   469             const TSmlDmMappingInfo& mappingInfo = aPreviousUriSegmentList.At(i);
       
   470                         
       
   471             if (mappingInfo.iURISegLUID.Compare(*luidElem) == 0)
       
   472                 {            
       
   473                 uriSeg = mappingInfo.iURISeg.AllocLC();
       
   474                 break;
       
   475                 }
       
   476             }
       
   477         
       
   478         if (uriSeg == 0)
       
   479             {
       
   480             //Uri was not found from the aPreviousUriSegmentList
       
   481             uriSeg = BuildLocallyCreatedRtNodeUriSegLC(largest);
       
   482             
       
   483             DEBUG_LOG2(_L8("uriSeg %S, largest %d"), uriSeg, largest);
       
   484             
       
   485             HBufC8* wholeUri = TDmAdUtil::BuildUriLC(aUri, *uriSeg);            
       
   486             aDmAdCallBack->SetMappingL(*wholeUri, *luidElem);
       
   487             CleanupStack::PopAndDestroy(); //wholeUri
       
   488             }
       
   489 
       
   490         //If this is not the first element, inserts slash at the beginning
       
   491         //of the result list.
       
   492         if (j > 0)
       
   493             {            
       
   494             aCurrentList.InsertL(aCurrentList.Size(), KDmAdSeparator);
       
   495             }
       
   496         aCurrentList.InsertL(aCurrentList.Size(), *uriSeg);
       
   497         
       
   498         CleanupStack::PopAndDestroy(); // uriSeg
       
   499         }
       
   500     
       
   501     CleanupStack::PopAndDestroy(); //luidList
       
   502     }