browserutilities/downloadmgr/DownloadMgrClntSrv/src/DownloadMgrDefAttrib.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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:  This file contains the declaration of the default attrib of Download Mgr Server.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "DownloadMgrDefAttrib.h"
       
    22 #include "DownloadMgrLogger.h"
       
    23 
       
    24 // ============================= LOCAL FUNCTIONS ===============================
       
    25 // -----------------------------------------------------------------------------
       
    26 // TDMgrUtils::TDMgrUtils
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 TDMgrUtils::TDMgrUtils()
       
    30     {
       
    31     }
       
    32 
       
    33 // ---------------------------------------------------------
       
    34 // TDMgrUtils::NextAttribL
       
    35 // ---------------------------------------------------------
       
    36 //
       
    37 TBool TDMgrUtils::NextAttribL( CDefaultAttrib* aAttrib,
       
    38                                const TDesC8& aPackedAttribs )
       
    39     {
       
    40     CLOG_ENTERFN_NULL( "TDMgrUtils::NextDefaultAttribL" )
       
    41     // ||: type;attribute;length;value :||
       
    42     if( iCurrent >= aPackedAttribs.Length() )
       
    43         {
       
    44         return EFalse;
       
    45         }
       
    46 
       
    47     TInt current( 0 );
       
    48     THttpDownloadMgrIntegerBuf intBuf( 0 );
       
    49 
       
    50     //CLOG_WRITE( "TDMgrUtils::NextDefaultAttrib - start" );
       
    51     // type info length
       
    52     TPtrC8 type( iPtr.Mid( current, intBuf.Size() ) );
       
    53     //CLOG_WRITE( "type info length" );
       
    54     // + (type info length)
       
    55     current = current + intBuf.Size();
       
    56     //CLOG_WRITE( "+ (type info length)" );
       
    57     // attribute info length
       
    58     TPtrC8 attribute( iPtr.Mid( current, intBuf.Size() ) );
       
    59     //CLOG_WRITE( "attribute info length" );
       
    60     //  + (attribute info length)
       
    61     current = current + intBuf.Size();
       
    62     //CLOG_WRITE( "+ (attribute info length)" );
       
    63     // data length info length
       
    64     TPtrC8 data_length( iPtr.Mid( current, intBuf.Size() ) );
       
    65     //CLOG_WRITE( "data length info length" );
       
    66     //  + (data length info length)
       
    67     current = current + intBuf.Size();
       
    68     intBuf.Copy( data_length );
       
    69     TPtrC8 value( iPtr.Mid( current, intBuf() ) );
       
    70     //CLOG_WRITE( "+ (data length info length)" );
       
    71     //  + (data length)
       
    72     current = current + intBuf();
       
    73     //CLOG_WRITE( "+ (data length)" );
       
    74     // = next item
       
    75     iCurrent = iCurrent + current;
       
    76     iPtr.Set( aPackedAttribs.Mid( iCurrent, ( aPackedAttribs.Length() - iCurrent ) ) );
       
    77     //CLOG_WRITE( "= next item" );
       
    78     // set aAttrib
       
    79     aAttrib->Reset();
       
    80     //CLOG_WRITE( "reset atrib" );
       
    81     aAttrib->SetL( type, attribute, value );
       
    82     //CLOG_WRITE( "set aAttrib" );
       
    83 
       
    84     return ETrue;
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------
       
    88 // TDMgrUtils::AllocDefAttribBufLC
       
    89 // ---------------------------------------------------------
       
    90 //
       
    91 HBufC8* TDMgrUtils::AllocDefAttribBufLC( CArrayPtrFlat< CDefaultAttrib >* aAttribs )
       
    92     {
       
    93     //__ASSERT_DEBUG( ( aAttribs != NULL ), User::Panic( KErrArgument ) );
       
    94     
       
    95     CLOG_ENTERFN_NULL( "TDMgrUtils::AllocDefAttribBufLC" )
       
    96     TInt length( 0 );
       
    97 
       
    98     for( TInt i = 0; i < aAttribs->Count(); i++ )
       
    99         {
       
   100         CDefaultAttrib* attrib = (*aAttribs)[i];
       
   101         length = length + attrib->PackedAttribSize();
       
   102         }
       
   103 
       
   104     //CLOG_WRITE_FORMAT( "length %d", length );
       
   105     return HBufC8::NewLC( length );
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------
       
   109 // TDMgrUtils::PackedAttributesL
       
   110 // ---------------------------------------------------------
       
   111 //
       
   112 HBufC8* TDMgrUtils::PackedAttributesL( CArrayPtrFlat< CDefaultAttrib >* aAttribs )
       
   113     {
       
   114     //__ASSERT_DEBUG( aAttribs, User::Panic( KErrArgument ) );
       
   115     
       
   116     CLOG_WRITE_NULL( "TDMgrUtils::PackedDefaultAttributesLC" )
       
   117     HBufC8* buf = AllocDefAttribBufLC( aAttribs );
       
   118     TPtr8 ptr( buf->Des() );
       
   119 
       
   120     for( TInt i = 0; i < aAttribs->Count(); i++ )
       
   121         {
       
   122         CDefaultAttrib* attrib = (*aAttribs)[i];
       
   123         attrib->PackAttribute( ptr );
       
   124         }
       
   125 
       
   126     CleanupStack::Pop( buf );
       
   127     return buf;
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------
       
   131 // TDMgrUtils::PackEventAttribL
       
   132 // ---------------------------------------------------------
       
   133 //
       
   134 HBufC8* TDMgrUtils::PackEventAttribL( TInt32 aUserData, TInt32 aMODownloadedSize,TInt32 aDownloadedSize, TInt32 aMOAttrLength, TInt32 aAttrLength )
       
   135     {
       
   136     THttpDownloadMgrIntegerBuf intBuf( 0 );
       
   137     HBufC8* buf = HBufC8::NewL( 6 * intBuf.Size() );
       
   138     TPtr8 package = buf->Des();
       
   139     
       
   140     //EDlAttrUserData
       
   141     intBuf() = aUserData;
       
   142     package.Append( intBuf );
       
   143     
       
   144     //EDlAttrMODownloadedSize
       
   145     intBuf() = aMODownloadedSize;
       
   146     package.Append( intBuf );
       
   147 
       
   148     //EDlAttrDownloadedSize
       
   149     intBuf() = aDownloadedSize;
       
   150     package.Append( intBuf );
       
   151           
       
   152     //EDlAttrMOLength
       
   153     intBuf() = aMOAttrLength;
       
   154     package.Append( intBuf );
       
   155 
       
   156     //EDlAttrLength
       
   157     intBuf() = aAttrLength;
       
   158     package.Append( intBuf );
       
   159         
       
   160     return buf;
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------
       
   164 // TDMgrUtils::UnPackEventAttribL
       
   165 // ---------------------------------------------------------
       
   166 //        
       
   167 void TDMgrUtils::UnPackEventAttribL( const TDesC8& aPack, 
       
   168 									 TInt32& aUserData, 
       
   169 									 TInt32& aMODownloadedSize,
       
   170 									 TInt32& aDownloadedSize, 
       
   171 									 TInt32& aMOAttrLength,  
       
   172 									 TInt32& aAttrLength, 
       
   173 									 TInt32& aMoIndex )
       
   174     {
       
   175     THttpDownloadMgrIntegerBuf intBuf( 0 );
       
   176     TInt current( 0 );
       
   177     TInt32 maskedData = 0;
       
   178     
       
   179     //EDlAttrUserData
       
   180     intBuf.Copy( aPack.Mid( current, intBuf.Size() ) );
       
   181     maskedData = intBuf(); 
       
   182     current = current + intBuf.Size();
       
   183        
       
   184     //EDlAttrMultipleMODownloadedSize
       
   185     intBuf.Copy( aPack.Mid( current, intBuf.Size() ) );
       
   186     aMODownloadedSize= intBuf(); 
       
   187     current = current + intBuf.Size();
       
   188 
       
   189     //EDlAttrDownloadedSize
       
   190     intBuf.Copy( aPack.Mid( current, intBuf.Size() ) );
       
   191     aDownloadedSize= intBuf(); 
       
   192     current = current + intBuf.Size();
       
   193               
       
   194     //EDlAttrLength
       
   195     intBuf.Copy( aPack.Mid( current, intBuf.Size() ) );
       
   196     aMOAttrLength= intBuf(); 
       
   197     current = current + intBuf.Size();         
       
   198     
       
   199     //EDlAttrMultipleMOLength
       
   200     intBuf.Copy( aPack.Mid( current, intBuf.Size() ) );
       
   201     aAttrLength= intBuf(); 
       
   202     current = current + intBuf.Size();         
       
   203     
       
   204     // Unmask maskedData to get UserData and MoIndex
       
   205     aUserData = (maskedData & 0x0000FFFF);
       
   206     aMoIndex = (maskedData >> 16);
       
   207     }
       
   208     
       
   209 // -----------------------------------------------------------------------------
       
   210 // CDefaultAttrib::CDefaultAttrib
       
   211 // C++ default constructor can NOT contain any code, that
       
   212 // might leave.
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 CDefaultAttrib::CDefaultAttrib()
       
   216     {
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CDefaultAttrib::CDefaultAttrib
       
   221 // C++ default constructor can NOT contain any code, that
       
   222 // might leave.
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 CDefaultAttrib::CDefaultAttrib( THttpDownloadAttrib aAttribute, TAttribType aType )
       
   226     {
       
   227     iAttribute = aAttribute;
       
   228     iType = aType;
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CDefaultAttrib::ConstructL
       
   233 // Symbian 2nd phase constructor can leave.
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 void CDefaultAttrib::ConstructL()
       
   237     {
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CDefaultAttrib::ConstructL
       
   242 // Symbian 2nd phase constructor can leave.
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CDefaultAttrib::ConstructL( TInt32 aValue )
       
   246     {
       
   247     iInt32 = aValue;
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CDefaultAttrib::ConstructL
       
   252 // Symbian 2nd phase constructor can leave.
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 void CDefaultAttrib::ConstructL( TBool aValue )
       
   256     {
       
   257     iBool = aValue;
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CDefaultAttrib::ConstructL
       
   262 // Symbian 2nd phase constructor can leave.
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CDefaultAttrib::ConstructL( const TDesC8& aValue )
       
   266     {
       
   267     iBufC8 = aValue.AllocL();
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CDefaultAttrib::ConstructL
       
   272 // Symbian 2nd phase constructor can leave.
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 void CDefaultAttrib::ConstructL( const TDesC16& aValue )
       
   276     {
       
   277     iBufC16 = aValue.AllocL();
       
   278     }
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // CDefaultAttrib::NewL
       
   282 // Two-phased constructor.
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 CDefaultAttrib* CDefaultAttrib::NewL()
       
   286     {
       
   287     CDefaultAttrib* self = new( ELeave ) CDefaultAttrib();
       
   288      
       
   289     CleanupStack::PushL( self );
       
   290     self->ConstructL();
       
   291     CleanupStack::Pop();
       
   292 
       
   293     return self;
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CDefaultAttrib::NewL
       
   298 // Two-phased constructor.
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 CDefaultAttrib* CDefaultAttrib::NewL( THttpDownloadAttrib aAttribute,
       
   302                                       TInt32              aValue )
       
   303     {
       
   304     CDefaultAttrib* self = new( ELeave ) CDefaultAttrib( aAttribute, EInt );
       
   305     
       
   306     CleanupStack::PushL( self );
       
   307     self->ConstructL( aValue );
       
   308     CleanupStack::Pop();
       
   309 
       
   310     return self;
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // CDefaultAttrib::NewL
       
   315 // Two-phased constructor.
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 CDefaultAttrib* CDefaultAttrib::NewL( THttpDownloadAttrib aAttribute,
       
   319                                       TBool               aValue )
       
   320     {
       
   321     CDefaultAttrib* self = new( ELeave ) CDefaultAttrib( aAttribute, EBool );
       
   322     
       
   323     CleanupStack::PushL( self );
       
   324     self->ConstructL( aValue );
       
   325     CleanupStack::Pop();
       
   326 
       
   327     return self;
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CDefaultAttrib::NewL
       
   332 // Two-phased constructor.
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 CDefaultAttrib* CDefaultAttrib::NewL( THttpDownloadAttrib aAttribute,
       
   336                                       const TDesC8&       aValue )
       
   337     {
       
   338     CDefaultAttrib* self = new( ELeave ) CDefaultAttrib( aAttribute, EString8 );
       
   339     
       
   340     CleanupStack::PushL( self );
       
   341     self->ConstructL( aValue );
       
   342     CleanupStack::Pop();
       
   343 
       
   344     return self;
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CDefaultAttrib::NewL
       
   349 // Two-phased constructor.
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 CDefaultAttrib* CDefaultAttrib::NewL( THttpDownloadAttrib aAttribute,
       
   353                                         const TDesC16&      aValue )
       
   354     {
       
   355     CDefaultAttrib* self = new( ELeave ) CDefaultAttrib( aAttribute, EString16 );
       
   356     
       
   357     CleanupStack::PushL( self );
       
   358     self->ConstructL( aValue );
       
   359     CleanupStack::Pop();
       
   360 
       
   361     return self;
       
   362     }
       
   363     
       
   364 // Destructor
       
   365 CDefaultAttrib::~CDefaultAttrib()
       
   366     {
       
   367     delete iBufC16;
       
   368     iBufC16 = NULL;
       
   369 
       
   370     delete iBufC8;
       
   371     iBufC8 = NULL;
       
   372     }
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CDefaultAttrib::AsTInt32L
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 TInt32 CDefaultAttrib::AsTInt32L() const
       
   379     {
       
   380     if( iType == EInt )
       
   381         {
       
   382         return iInt32;
       
   383         }
       
   384     else
       
   385         {
       
   386         User::Leave( KErrNotFound );
       
   387         return KErrNotFound;
       
   388         }
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CDefaultAttrib::AsTBoolL
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 TBool CDefaultAttrib::AsTBoolL() const
       
   396     {
       
   397     if( iType == EBool )
       
   398         {
       
   399         return iBool;
       
   400         }
       
   401     else
       
   402         {
       
   403         User::Leave( KErrNotFound );
       
   404         return EFalse;
       
   405         }
       
   406     }
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CDefaultAttrib::AsTPtrC8L
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 TPtrC8 CDefaultAttrib::AsTPtrC8L() const
       
   413     {
       
   414     if( iType == EString8 )
       
   415         {
       
   416         return iBufC8->Des();
       
   417         }
       
   418     else
       
   419         {
       
   420         User::Leave( KErrNotFound );
       
   421         return KNullDesC8();
       
   422         }
       
   423     }
       
   424 
       
   425 // -----------------------------------------------------------------------------
       
   426 // CDefaultAttrib::AsTPtrC16L
       
   427 // -----------------------------------------------------------------------------
       
   428 //
       
   429 TPtrC16 CDefaultAttrib::AsTPtrC16L() const
       
   430     {
       
   431     if( iType == EString16 )
       
   432         {
       
   433         return iBufC16->Des();
       
   434         }
       
   435     else
       
   436         {
       
   437         User::Leave( KErrNotFound );
       
   438         return KNullDesC16();
       
   439         }
       
   440     }
       
   441 
       
   442 // -----------------------------------------------------------------------------
       
   443 // CDefaultAttrib::PackAttribute
       
   444 // -----------------------------------------------------------------------------
       
   445 //
       
   446 void CDefaultAttrib::PackAttribute( TDes8& aPackage )
       
   447     {
       
   448     THttpDownloadMgrIntegerBuf intBuf( 0 );
       
   449     intBuf() = iType;
       
   450     aPackage.Append( intBuf );
       
   451     intBuf() = (TInt32)iAttribute;
       
   452     aPackage.Append( intBuf );
       
   453 
       
   454     switch( iType )
       
   455         {
       
   456         case EString8:
       
   457             {
       
   458             intBuf() = iBufC8->Length();
       
   459             aPackage.Append( intBuf );
       
   460             aPackage.Append( iBufC8->Des() );
       
   461             }
       
   462             break;
       
   463         case EString16:
       
   464             {
       
   465             intBuf() = iBufC16->Size();
       
   466             aPackage.Append( intBuf );
       
   467             TUint8* ptr8 = &(aPackage[0]) + aPackage.Size();
       
   468             // Here we use the padding byte because we can
       
   469             // cast to even memory address only
       
   470             if(  (TInt)ptr8%2 == 1 )
       
   471                 {
       
   472                 ptr8 = ptr8 + 1;
       
   473                 }
       
   474             TPtr16 ptr( (TUint16*)(ptr8), iBufC16->Length() );
       
   475             ptr.Copy( *iBufC16 );
       
   476             aPackage.SetLength( aPackage.Size() + iBufC16->Size() );
       
   477             }
       
   478             break;
       
   479         case EInt:
       
   480             {
       
   481             intBuf() = intBuf.Size();
       
   482             aPackage.Append( intBuf );
       
   483             intBuf() = iInt32;
       
   484             aPackage.Append( intBuf );
       
   485             }
       
   486             break;
       
   487         case EBool:
       
   488             {
       
   489             intBuf() = intBuf.Size();
       
   490             aPackage.Append( intBuf );
       
   491             intBuf() = iBool;
       
   492             aPackage.Append( intBuf );
       
   493             }
       
   494             break;
       
   495         default:
       
   496             break;
       
   497         }
       
   498 
       
   499     }
       
   500 
       
   501 // -----------------------------------------------------------------------------
       
   502 // CDefaultAttrib::PackedAttribLength
       
   503 // -----------------------------------------------------------------------------
       
   504 //
       
   505 TInt CDefaultAttrib::PackedAttribSize()
       
   506     {
       
   507     TInt length( 0 );
       
   508             
       
   509     THttpDownloadMgrIntegerBuf intBuf( 0 );
       
   510     // length of type info
       
   511     length = length + intBuf.Size();
       
   512     // length of attribute info
       
   513     length = length + intBuf.Size();
       
   514     // length of size info
       
   515     length = length + intBuf.Size();
       
   516 
       
   517     switch( iType )
       
   518         {
       
   519         case EString8:
       
   520             {
       
   521             // data length
       
   522             length = length + iBufC8->Size();
       
   523             }
       
   524             break;
       
   525         case EString16:
       
   526             {
       
   527             // data length
       
   528             // We must add 1 padding byte, in case of odd memory address 
       
   529             // when the 16 bits descipror is copied to the 8 bits buffer.
       
   530             length = length + iBufC16->Size() + 1;
       
   531             }
       
   532             break;
       
   533         case EInt:
       
   534         case EBool:
       
   535             {
       
   536             // data length
       
   537             length = length + intBuf.Size();
       
   538             }
       
   539             break;
       
   540         default:
       
   541             break;
       
   542         }
       
   543 
       
   544     return length;
       
   545     }
       
   546 
       
   547 // -----------------------------------------------------------------------------
       
   548 // CDefaultAttrib::Reset
       
   549 // -----------------------------------------------------------------------------
       
   550 //
       
   551 void CDefaultAttrib::Reset()
       
   552     {
       
   553     delete iBufC16;
       
   554     iBufC16 = NULL;
       
   555 
       
   556     delete iBufC8;
       
   557     iBufC8 = NULL;
       
   558     }
       
   559 
       
   560 // -----------------------------------------------------------------------------
       
   561 // CDefaultAttrib::SetL
       
   562 // -----------------------------------------------------------------------------
       
   563 //
       
   564 void CDefaultAttrib::SetL( const TDesC8& aType,
       
   565                            const TDesC8& aAttribute,
       
   566                            const TDesC8& aValue )
       
   567     {
       
   568     THttpDownloadMgrIntegerBuf intBuf( 0 );
       
   569     intBuf.Copy( aType );
       
   570     iType = (TAttribType)intBuf();
       
   571     intBuf.Copy( aAttribute );
       
   572     iAttribute = (THttpDownloadAttrib)intBuf();
       
   573 
       
   574     delete iBufC16;
       
   575     iBufC16 = NULL;
       
   576 
       
   577     delete iBufC8;
       
   578     iBufC8 = NULL;
       
   579 
       
   580     switch( iType )
       
   581         {
       
   582         case EString8:
       
   583             {
       
   584             iBufC8 = aValue.AllocL();
       
   585             }
       
   586             break;
       
   587         case EString16:
       
   588             {
       
   589             iBufC16 = HBufC16::NewL( aValue.Size() / 2 );
       
   590             const TUint8* ptr8 = &(aValue[0]);
       
   591             // Here we use the padding byte because we can
       
   592             // cast to even memory address only
       
   593             if(  (TInt)ptr8%2 == 1 )
       
   594                 {
       
   595                 ptr8 = ptr8 + 1;
       
   596                 }
       
   597             TPtrC16 ptr( (TUint16*)ptr8, aValue.Size() / 2 );
       
   598             iBufC16->Des().Copy( ptr );
       
   599             }
       
   600             break;
       
   601         case EInt:
       
   602             {
       
   603             intBuf.Copy( aValue );
       
   604             iInt32 = intBuf();
       
   605             }
       
   606             break;
       
   607         case EBool:
       
   608             {
       
   609             intBuf.Copy( aValue );
       
   610             iBool = intBuf();
       
   611             }
       
   612             break;
       
   613         default:
       
   614             break;
       
   615         }
       
   616     }
       
   617 
       
   618 //  End of File