languageinterworkingfw/servicehandler/src/liwgenericparam.cpp
changeset 57 61b27eec6533
parent 45 7aa6007702af
equal deleted inserted replaced
45:7aa6007702af 57:61b27eec6533
     1 /*
       
     2 * Copyright (c) 2003-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 the License "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 TLiwGenericParam and CLiwGenericParamList.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 // INCLUDES
       
    25 #include <s32mem.h>
       
    26 #include "liwgenericparam.h"
       
    27 using namespace LIW;
       
    28 namespace {
       
    29 
       
    30 // CONSTANTS
       
    31 // Protect against possible data format changes
       
    32 const TInt KVersion = 10;  // Version 1.0
       
    33 
       
    34 enum TPanicCode
       
    35     {
       
    36     EPanicPostCond_CopyLC = 1,
       
    37     EPanicPostCond_AppendL,
       
    38     EPanicPostCond_Reset
       
    39     };
       
    40 
       
    41 
       
    42 // ============================= LOCAL FUNCTIONS ===============================
       
    43 
       
    44 #ifdef _DEBUG
       
    45 void Panic(TPanicCode aCode)
       
    46     {
       
    47     _LIT(KPanicText, "GenericParam");
       
    48     User::Panic(KPanicText, aCode);
       
    49     }
       
    50 #endif
       
    51 
       
    52 }
       
    53 
       
    54 
       
    55 const TInt KRFsSlot = 2;
       
    56 const TInt KRFileSlot = 3;
       
    57 
       
    58 
       
    59 // ============================ MEMBER FUNCTIONS ===============================
       
    60 
       
    61 /**
       
    62  * Releases any dynamic data owned by this parameter.
       
    63  */
       
    64 void TLiwGenericParam::Destroy()
       
    65     {
       
    66     if (iSemanticId != EGenericParamNameAsStringTransient)
       
    67         {
       
    68         delete iReserved;
       
    69         iReserved = NULL;   
       
    70         }
       
    71     iValue.Destroy();
       
    72     }
       
    73 
       
    74 /**
       
    75  * Copies aParam to this parameter. Takes an own copy of the value held in 
       
    76  * aParam.
       
    77  */
       
    78 void TLiwGenericParam::CopyLC(const TLiwGenericParam& aParam)
       
    79     {
       
    80     // Take a copy of value
       
    81     iValue.SetL(aParam.iValue);
       
    82     // Copy semantic id
       
    83     iSemanticId = aParam.iSemanticId;
       
    84     // Copy name  
       
    85     if (iSemanticId == EGenericParamNameAsString) {
       
    86       delete iReserved;
       
    87       iReserved = NULL;
       
    88       iReserved = ((HBufC8*)aParam.iReserved)->AllocL();
       
    89     }
       
    90     else if (iSemanticId == EGenericParamNameAsStringTransient) {
       
    91       delete iReserved;
       
    92       iReserved = NULL;
       
    93       iReserved = ((TDesC8*)aParam.iReserved)->AllocL();
       
    94       iSemanticId = EGenericParamNameAsString;
       
    95     }
       
    96     CleanupStack::PushL(*this);
       
    97 
       
    98     //__ASSERT_DEBUG(*this==aParam, Panic(EPanicPostCond_CopyLC));
       
    99     }
       
   100 
       
   101 
       
   102 EXPORT_C void TLiwGenericParam::PushL()
       
   103 	{
       
   104 	CleanupStack::PushL( TCleanupItem( TLiwGenericParam::ParamCleanup , this) ); 
       
   105 	}
       
   106 
       
   107 EXPORT_C void TLiwGenericParam::ParamCleanup( TAny* aObj )
       
   108     {
       
   109     static_cast<TLiwGenericParam*>(aObj)->Destroy();
       
   110     }
       
   111 
       
   112 /**
       
   113  * Returns a cleanup item that will call Destroy to this object when 
       
   114  * PopAndDestroy'ed.
       
   115  */
       
   116 TLiwGenericParam::operator TCleanupItem()
       
   117     {
       
   118     return TCleanupItem(&CleanupDestroy,this);
       
   119     }
       
   120 
       
   121 /**
       
   122  * Callback for operator TCleanupItem().
       
   123  */
       
   124 void TLiwGenericParam::CleanupDestroy(TAny* aObj)
       
   125     {
       
   126     static_cast<TLiwGenericParam*>(aObj)->Destroy();
       
   127     }
       
   128 /**
       
   129  * Internalizes this parameter from aStream.
       
   130  */
       
   131 void TLiwGenericParam::InternalizeL(RReadStream& aStream)
       
   132     {
       
   133     /*const TInt version =*/ aStream.ReadInt8L();
       
   134     const TGenericParamId id = 
       
   135         static_cast<TGenericParamId>(aStream.ReadInt32L());
       
   136     if (id == EGenericParamNameAsString)
       
   137       {
       
   138         TInt len = aStream.ReadInt32L();
       
   139         delete iReserved;
       
   140         iReserved = NULL;
       
   141         iReserved = HBufC8::NewL(aStream, len);
       
   142       }
       
   143     iValue.InternalizeL(aStream);
       
   144     iSemanticId = id;
       
   145     }
       
   146 
       
   147 /**
       
   148  * Externalizes this parameter to aStream.
       
   149  */
       
   150 void TLiwGenericParam::ExternalizeL(RWriteStream& aStream) const
       
   151     {
       
   152     aStream.WriteInt8L(KVersion);
       
   153     aStream.WriteInt32L(iSemanticId);
       
   154     if (iSemanticId == EGenericParamNameAsString)
       
   155       {
       
   156         aStream.WriteInt32L(((HBufC8*)iReserved)->Length());
       
   157         aStream << (*((HBufC8*)iReserved));
       
   158       }
       
   159     iValue.ExternalizeL(aStream);
       
   160     }
       
   161 
       
   162 /**
       
   163  * Returns the maximum externalized size of this parameter.
       
   164  */
       
   165 TInt TLiwGenericParam::Size() const
       
   166     {
       
   167     TInt size = sizeof (TInt8); // version
       
   168     size += sizeof (TInt32); // semantic ID
       
   169     if (iSemanticId == EGenericParamNameAsString)
       
   170       {
       
   171         size += sizeof (TInt32); // name length
       
   172         //size += 1;   // header written by operator<<
       
   173         if(((HBufC8*)iReserved)->Size()>=64)
       
   174         	{
       
   175            	size += 2;
       
   176         	}
       
   177         else
       
   178         	{
       
   179         	size += 1;
       
   180            	}
       
   181         	
       
   182         size += ((HBufC8*)iReserved)->Size();
       
   183       }
       
   184     size += iValue.Size();
       
   185     return size;
       
   186     }
       
   187 
       
   188 EXPORT_C TBool operator==(const TLiwGenericParam& aLhs, 
       
   189     const TLiwGenericParam& aRhs)
       
   190     {
       
   191       if (((aLhs.SemanticId() == EGenericParamNameAsString) ||
       
   192            (aLhs.SemanticId() == EGenericParamNameAsStringTransient)) &&
       
   193           ((aRhs.SemanticId() == EGenericParamNameAsString) ||
       
   194            (aRhs.SemanticId() == EGenericParamNameAsStringTransient)))
       
   195         return ((aLhs.Name() == aRhs.Name()) && (aLhs.Value() == aRhs.Value()));
       
   196       else
       
   197         return 
       
   198           ((aLhs.SemanticId() == aRhs.SemanticId()) && 
       
   199            (aLhs.Value() == aRhs.Value()));
       
   200     }
       
   201 
       
   202 
       
   203 // CLiwGenericParamList
       
   204 
       
   205 inline CLiwGenericParamList::CLiwGenericParamList()
       
   206     {
       
   207     }
       
   208 
       
   209 EXPORT_C CLiwGenericParamList* CLiwGenericParamList::NewL()
       
   210     {
       
   211     CLiwGenericParamList *self = NewLC();
       
   212     CleanupStack::Pop(self);
       
   213     return self;
       
   214     }
       
   215 
       
   216 EXPORT_C CLiwGenericParamList* CLiwGenericParamList::NewL(RReadStream& aReadStream)
       
   217     {
       
   218     CLiwGenericParamList *self = NewLC(aReadStream);
       
   219     CleanupStack::Pop(self);
       
   220     return self;
       
   221     }
       
   222 
       
   223 EXPORT_C CLiwGenericParamList* CLiwGenericParamList::NewLC()
       
   224     {
       
   225     CLiwGenericParamList *self = new( ELeave ) CLiwGenericParamList();
       
   226     CleanupStack::PushL( self );
       
   227     return self;
       
   228     }
       
   229 
       
   230 EXPORT_C CLiwGenericParamList* CLiwGenericParamList::NewLC(RReadStream& aReadStream)
       
   231     {
       
   232     CLiwGenericParamList *self = new( ELeave ) CLiwGenericParamList();
       
   233     CleanupStack::PushL( self );
       
   234     self->AppendL(aReadStream);
       
   235     return self;
       
   236     }
       
   237 
       
   238 CLiwGenericParamList::~CLiwGenericParamList()
       
   239     {
       
   240     for (TInt index = iParameters.Count()-1; index >= 0; --index)
       
   241         {
       
   242         // Can use Destroy because the parameters cannot be accessed anymore
       
   243         iParameters[index].Destroy();
       
   244         }
       
   245     iParameters.Close();
       
   246     }
       
   247 
       
   248 EXPORT_C TInt CLiwGenericParamList::Count() const
       
   249     {
       
   250     return iParameters.Count();
       
   251     }
       
   252     
       
   253 EXPORT_C TInt CLiwGenericParamList::Count(TGenericParamId aSemanticId, 
       
   254     TVariantTypeId aDataType) const
       
   255     {
       
   256     TInt result = 0;
       
   257     const TInt count = iParameters.Count();
       
   258 
       
   259     for (TInt index = 0; index < count; index++)
       
   260         {
       
   261         const TLiwGenericParam& param = iParameters[index];
       
   262         if (param.SemanticId() == aSemanticId)
       
   263             {
       
   264             if (aDataType==EVariantTypeAny || param.Value().TypeId()==aDataType)
       
   265                 {
       
   266                 result++;                   
       
   267                 }
       
   268             }
       
   269         }       
       
   270     return result;     
       
   271     }
       
   272 
       
   273 
       
   274 EXPORT_C const TLiwGenericParam& CLiwGenericParamList::operator[](TInt aIndex) const
       
   275     {
       
   276         return iParameters[aIndex]; 
       
   277     }
       
   278 
       
   279 EXPORT_C void CLiwGenericParamList::AtL(TInt aIndex, TLiwGenericParam& aParam) const
       
   280     {
       
   281     aParam.CopyLC(iParameters[aIndex]); 
       
   282     CleanupStack::Pop(&aParam);   
       
   283     }
       
   284 
       
   285 EXPORT_C void CLiwGenericParamList::AppendL(const TLiwGenericParam& aParam)
       
   286     {
       
   287     TLiwGenericParam newParam;
       
   288     newParam.CopyLC(aParam);
       
   289     User::LeaveIfError(iParameters.Append(newParam));
       
   290     CleanupStack::Pop(&newParam);
       
   291 
       
   292     //__ASSERT_DEBUG((*this)[Count()-1]==aParam, Panic(EPanicPostCond_AppendL));
       
   293     }
       
   294 
       
   295 
       
   296 EXPORT_C TBool CLiwGenericParamList::Remove(TInt aSemanticId)
       
   297     {
       
   298     const TInt count = iParameters.Count();
       
   299     for (TInt index = 0; index < count; ++index)
       
   300         {
       
   301         const TLiwGenericParam& param = iParameters[index];
       
   302         if (param.SemanticId() == aSemanticId)
       
   303             {
       
   304             iParameters.Remove(index);
       
   305             return ETrue;
       
   306             }
       
   307         }
       
   308 
       
   309     return EFalse;
       
   310     }
       
   311 
       
   312 EXPORT_C void CLiwGenericParamList::Remove(const TDesC8& aName)
       
   313     {
       
   314     TInt start = 0;
       
   315     FindFirst(start, aName);
       
   316     while (start != KErrNotFound)
       
   317         {
       
   318         // Can use Destroy because the parameters cannot be accessed anymore
       
   319         iParameters[start].Destroy();
       
   320         iParameters.Remove(start);
       
   321         start = 0;   
       
   322         FindFirst(start, aName);
       
   323         }
       
   324     }
       
   325     
       
   326 EXPORT_C void CLiwGenericParamList::Reset()
       
   327     {
       
   328     for (TInt index = iParameters.Count()-1; index >= 0; --index)
       
   329         {
       
   330         // Can use Destroy because the parameters cannot be accessed anymore
       
   331         iParameters[index].Destroy();
       
   332         iParameters.Remove(index);
       
   333         }
       
   334 
       
   335     __ASSERT_DEBUG(Count()==0, Panic(EPanicPostCond_Reset));
       
   336     }
       
   337 
       
   338 EXPORT_C const TLiwGenericParam* CLiwGenericParamList::FindFirst(
       
   339     TInt &aIndex, 
       
   340     TGenericParamId aSemanticId, 
       
   341     TVariantTypeId aDataType) const
       
   342     {
       
   343     if (aIndex >= 0)
       
   344         {
       
   345         const TInt count = iParameters.Count();
       
   346         for (TInt index = aIndex; index < count; ++index)
       
   347             {
       
   348             const TLiwGenericParam& param = iParameters[index];
       
   349             if (param.SemanticId() == aSemanticId)
       
   350                 {
       
   351                 if (aDataType==EVariantTypeAny || param.Value().TypeId()==aDataType)
       
   352                     {
       
   353                     aIndex = index;
       
   354                     return &param;
       
   355                     }
       
   356                 }
       
   357             }
       
   358         }
       
   359     aIndex = KErrNotFound;
       
   360     return NULL;
       
   361     }
       
   362 
       
   363 EXPORT_C const TLiwGenericParam* CLiwGenericParamList::FindNext(
       
   364     TInt &aIndex, 
       
   365     TGenericParamId aSemanticId,
       
   366     TVariantTypeId aDataType) const
       
   367     {
       
   368     if (aIndex < 0)
       
   369         {
       
   370         aIndex = KErrNotFound;
       
   371         return NULL;
       
   372         }
       
   373 
       
   374     TInt index = aIndex+1;
       
   375     const TLiwGenericParam* result = FindFirst(index, aSemanticId, aDataType);
       
   376     aIndex = index;
       
   377     return result;
       
   378     }
       
   379 
       
   380 EXPORT_C const TLiwGenericParam* CLiwGenericParamList::FindNext(
       
   381     TInt &aIndex, 
       
   382     const TDesC8& aName,
       
   383     TVariantTypeId aDataType) const
       
   384     {
       
   385     if (aIndex < 0)
       
   386         {
       
   387         aIndex = KErrNotFound;
       
   388         return NULL;
       
   389         }
       
   390 
       
   391     TInt index = aIndex+1;
       
   392     const TLiwGenericParam* result = FindFirst(index, aName, aDataType);
       
   393     aIndex = index;
       
   394     return result;
       
   395     }
       
   396 
       
   397 EXPORT_C const TLiwGenericParam* CLiwGenericParamList::FindFirst 
       
   398         (TInt &aIndex, const TDesC8& aName, 
       
   399         TVariantTypeId aDataType) const
       
   400     {
       
   401     if (aIndex >= 0)
       
   402         {
       
   403         const TInt count = iParameters.Count();
       
   404         for (TInt index = aIndex; index < count; ++index)
       
   405             {
       
   406             const TLiwGenericParam& param = iParameters[index];
       
   407             if ((param.SemanticId() == EGenericParamNameAsString) &&
       
   408                 (param.Name() == aName))
       
   409                 {
       
   410                 if (aDataType==EVariantTypeAny || param.Value().TypeId()==aDataType)
       
   411                     {
       
   412                     aIndex = index;
       
   413                     return &param;
       
   414                     }
       
   415                 }
       
   416             }
       
   417         }
       
   418     aIndex = KErrNotFound;
       
   419     return NULL;
       
   420     }
       
   421 
       
   422 EXPORT_C void CLiwGenericParamList::ExternalizeL(RWriteStream& aStream) const
       
   423     {
       
   424     aStream.WriteInt8L(KVersion);
       
   425     const TInt count = iParameters.Count();
       
   426     aStream.WriteInt32L(count);
       
   427     for (TInt index = 0; index < count; ++index)
       
   428         {
       
   429         iParameters[index].ExternalizeL(aStream);
       
   430         }
       
   431     }
       
   432 
       
   433 EXPORT_C TInt CLiwGenericParamList::Size() const
       
   434     {
       
   435     TInt size = sizeof (TInt8); // version
       
   436     size += sizeof (TInt32);  // param count
       
   437     const TInt count = iParameters.Count();
       
   438     for (TInt index = 0; index < count; ++index)
       
   439         {
       
   440         size += iParameters[index].Size();
       
   441         }
       
   442     return size;
       
   443     }
       
   444 
       
   445 /**
       
   446  * Appends parameters to this list from aStream.
       
   447  */
       
   448 void CLiwGenericParamList::AppendL(RReadStream& aReadStream)
       
   449     {
       
   450     /*const TInt version =*/ aReadStream.ReadInt8L();
       
   451     
       
   452     TInt count = aReadStream.ReadInt32L();
       
   453     while (count--)
       
   454         {
       
   455         TLiwGenericParam param;
       
   456         CleanupStack::PushL(param);
       
   457         param.InternalizeL(aReadStream);
       
   458         User::LeaveIfError(iParameters.Append(param));
       
   459         CleanupStack::Pop(&param);
       
   460         }
       
   461     }
       
   462 
       
   463 
       
   464 
       
   465 EXPORT_C void CLiwGenericParamList::InternalizeL(RReadStream& aStream)
       
   466     {
       
   467     AppendL(aStream);
       
   468     }
       
   469 
       
   470 
       
   471 EXPORT_C HBufC8* CLiwGenericParamList::PackForServerL(TIpcArgs& aArgs)
       
   472     {   
       
   473     TInt index;
       
   474     TInt rfileCount = 0;
       
   475     RFile fhandle;
       
   476 
       
   477     HBufC8* buf = HBufC8::NewLC(Size() + iParameters.Count());
       
   478     TPtr8 des = buf->Des();
       
   479     RDesWriteStream outstrm(des);    
       
   480     CleanupClosePushL(outstrm);
       
   481 
       
   482     outstrm.WriteInt16L(iParameters.Count());   
       
   483 
       
   484     for (index = 0; index < iParameters.Count(); index++)
       
   485         {       
       
   486         if (iParameters[index].Value().TypeId() == EVariantTypeFileHandle)
       
   487             {
       
   488             if (rfileCount == 0)
       
   489                 {
       
   490                 outstrm.WriteInt8L(iParameters[index].Value().TypeId());
       
   491                 outstrm.WriteInt32L(iParameters[index].SemanticId());
       
   492                 if (iParameters[index].Value().Get(fhandle))
       
   493                     {
       
   494                     rfileCount++;
       
   495                     }
       
   496                 }
       
   497             }
       
   498         else
       
   499             {
       
   500             outstrm.WriteInt8L(iParameters[index].Value().TypeId());
       
   501             iParameters[index].ExternalizeL( outstrm );
       
   502             }
       
   503         }
       
   504         
       
   505     outstrm.CommitL();  
       
   506     CleanupStack::PopAndDestroy(&outstrm);
       
   507                         
       
   508     aArgs.Set(0, buf);
       
   509     aArgs.Set(1, des.MaxLength());    
       
   510         
       
   511     if (rfileCount)
       
   512         {
       
   513         fhandle.TransferToServer(aArgs, KRFsSlot, KRFileSlot);      
       
   514         }   
       
   515 
       
   516     CleanupStack::Pop(buf);    // buf
       
   517 
       
   518     return buf;
       
   519     }
       
   520 
       
   521 
       
   522 EXPORT_C void CLiwGenericParamList::UnpackFromClientL(const RMessage2& aMsg)
       
   523     {
       
   524     TInt8 type;
       
   525     TInt index;
       
   526     TLiwGenericParam param; 
       
   527     TGenericParamId handleId = EGenericParamUnspecified;
       
   528     TInt numFileHandles = 0;
       
   529 
       
   530     HBufC8* buf = HBufC8::NewLC(aMsg.Int1());
       
   531     TPtr8 ptr(buf->Des());
       
   532     aMsg.ReadL(0, ptr);
       
   533     RDesReadStream instrm(buf->Des());
       
   534 
       
   535     const TInt16 count = instrm.ReadInt16L();
       
   536     for (index = 0; index < count; index++)
       
   537         {
       
   538         type = instrm.ReadInt8L();
       
   539         if (type == EVariantTypeFileHandle)
       
   540             {
       
   541             if (numFileHandles == 0)
       
   542                 {
       
   543                 handleId = (TGenericParamId)instrm.ReadInt32L();
       
   544                 numFileHandles++;
       
   545                 }
       
   546             }
       
   547         else
       
   548             {
       
   549             param.InternalizeL(instrm);
       
   550             AppendL(param);
       
   551             param.Reset();
       
   552             }
       
   553         }
       
   554 
       
   555     if (numFileHandles)
       
   556         {
       
   557         RFile file;
       
   558         file.AdoptFromClient(aMsg, KRFsSlot, KRFileSlot);
       
   559         TLiwVariant variant(file);
       
   560         AppendL(TLiwGenericParam(handleId, variant));               
       
   561         }
       
   562         
       
   563     CleanupStack::PopAndDestroy(buf); // buf       
       
   564     }
       
   565     
       
   566 
       
   567 EXPORT_C void CLiwGenericParamList::AppendL(const CLiwGenericParamList& aList)
       
   568     {
       
   569     for (TInt index = 0; index < aList.iParameters.Count(); index++)
       
   570         {
       
   571         AppendL(aList.iParameters[index]);
       
   572         }
       
   573     }
       
   574 
       
   575 // End of file