upnpmediaserver/avobjects/src/upnpelement.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /** @file
       
     2 * Copyright (c) 2005-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:  UPnPElement implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "upnpelement.h"
       
    21 #include "upnpobject.h"
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 // ============================ MEMBER FUNCTIONS ===============================
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // CUpnpElement::ConstructL
       
    30 // Symbian 2nd phase constructor can leave.
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 void CUpnpElement::ConstructL(const TDesC8& aName)
       
    34 {
       
    35     iElementName = aName.AllocL();
       
    36     iElementValue = KNullDesC8().AllocL();
       
    37     iFilePath = KNullDesC().AllocL();
       
    38 }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CUpnpElement::NewLC
       
    42 // Two-phased constructor.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C CUpnpElement* CUpnpElement::NewLC(const TDesC8& aName)
       
    46 {
       
    47     CUpnpElement* self = new( ELeave ) CUpnpElement;
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL(aName);
       
    50     return self;
       
    51 }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CUpnpElement::NewLC
       
    55 // Two-phased constructor.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 EXPORT_C CUpnpElement* CUpnpElement::NewL(const TDesC8& aName)
       
    59 {
       
    60     CUpnpElement* self = NewLC(aName);
       
    61     CleanupStack::Pop(self);
       
    62     return self;
       
    63 }    
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CUpnpElement::CUpnpElement
       
    67 // C++ default constructor can NOT contain any code, that
       
    68 // might leave.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CUpnpElement::CUpnpElement()
       
    72     {
       
    73     }
       
    74 
       
    75 // Destructor
       
    76 CUpnpElement::~CUpnpElement()
       
    77     {
       
    78     delete iElementName;
       
    79     delete iElementValue;
       
    80     delete iFilePath;
       
    81     iAttributes.ResetAndDestroy();
       
    82     iAttributes.Close();
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CUpnpElement::SetElementNameL
       
    87 // Sets name for the attribute.
       
    88 // (other items were commented in a header).
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 EXPORT_C void CUpnpElement::SetNameL( const TDesC8& aElementName )
       
    92     {
       
    93     HBufC8* tmp = aElementName.AllocL();
       
    94     delete iElementName;
       
    95     iElementName = tmp;    
       
    96     }
       
    97     
       
    98 // -----------------------------------------------------------------------------
       
    99 // CUpnpElement::Name
       
   100 // Returns name of the attribute. If null, returns KNullDesC
       
   101 // (other items were commented in a header).
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 EXPORT_C const TDesC8& CUpnpElement::Name() const
       
   105     {
       
   106     return *iElementName;
       
   107     }
       
   108     
       
   109 // -----------------------------------------------------------------------------
       
   110 // CUpnpElement::SetElementNameL
       
   111 // Sets name for the attribute.
       
   112 // (other items were commented in a header).
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 EXPORT_C void CUpnpElement::SetValueL( const TDesC8& aElementValue )
       
   116     {
       
   117     HBufC8* tmp = aElementValue.AllocL();
       
   118     delete iElementValue;
       
   119     iElementValue = tmp;
       
   120     }
       
   121     
       
   122 // -----------------------------------------------------------------------------
       
   123 // CUpnpElement::Name
       
   124 // Returns name of the attribute. If null, returns KNullDesC
       
   125 // (other items were commented in a header).
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C const TDesC8& CUpnpElement::Value() const
       
   129     {
       
   130     return *iElementValue;
       
   131     }
       
   132     
       
   133 // -----------------------------------------------------------------------------
       
   134 // CUpnpElement::SetFilePathL
       
   135 // Returns path of the attribute res. If null, returns KNullDesC
       
   136 // (other items were commented in a header).
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 EXPORT_C void CUpnpElement::SetFilePathL(const TDesC& aFilePath)
       
   140     {
       
   141     HBufC* tmp = aFilePath.AllocL();
       
   142     delete iFilePath;
       
   143     iFilePath = tmp;
       
   144     }
       
   145     
       
   146 // -----------------------------------------------------------------------------
       
   147 // CUpnpElement::FilePath
       
   148 // Returns path of the attribute res. If null, returns KNullDesC
       
   149 // (other items were commented in a header).
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 EXPORT_C const TDesC& CUpnpElement::FilePath() const
       
   153     {
       
   154     return *iFilePath;
       
   155     }
       
   156     
       
   157 // -----------------------------------------------------------------------------
       
   158 // CUpnpElement::AddAttributeL
       
   159 // Returns name of the object. If null, returns KNullDesC8
       
   160 // (other items were commented in a header).
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 EXPORT_C void CUpnpElement::AddAttributeL( CUpnpAttribute* aNewAttribute )
       
   164     {
       
   165     if ( aNewAttribute )
       
   166         {
       
   167         iAttributes.AppendL( aNewAttribute );
       
   168         }
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CUpnpElement::RemoveAttributeL
       
   173 // Returns name of the object. If null, returns KNullDesC8
       
   174 // (other items were commented in a header).
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 EXPORT_C void CUpnpElement::RemoveAttributeL( const CUpnpAttribute* aNewAttribute )
       
   178     {
       
   179     for ( TInt index(0); index < iAttributes.Count(); index++ )
       
   180         {
       
   181         if ( iAttributes[index] == aNewAttribute )
       
   182             {
       
   183             iAttributes.Remove( index );
       
   184             break;
       
   185             }
       
   186         }
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CUpnpElement::Name
       
   191 // Returns name of the object. If null, returns KNullDesC8
       
   192 // (other items were commented in a header).
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 EXPORT_C const RUPnPAttributesArray& CUpnpElement::GetAttributes()
       
   196     {
       
   197     return iAttributes;
       
   198     }
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CUpnpElement::CopyL
       
   202 // Copies the content from parameter CUpnpElement to this
       
   203 // (other items were commented in a header).
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 EXPORT_C void CUpnpElement::CopyL( const CUpnpElement& aElement )
       
   207     {
       
   208       
       
   209     CBufFlat* buffer = CBufFlat::NewL( sizeof( aElement ) );
       
   210     CleanupStack::PushL( buffer );
       
   211 
       
   212     RBufWriteStream outStream;
       
   213     outStream.Open( *buffer );
       
   214     CleanupClosePushL( outStream );
       
   215 	
       
   216     RBufReadStream inStream;
       
   217     inStream.Open( *buffer );
       
   218     CleanupClosePushL( inStream );
       
   219 	
       
   220     aElement.ExternalizeL( outStream );
       
   221     InternalizeL( inStream );
       
   222 	
       
   223     CleanupStack::PopAndDestroy( 3 );   // inStream && outStream && buffer
       
   224     }
       
   225     
       
   226 // -----------------------------------------------------------------------------
       
   227 // CUpnpElement::ExternalizeL
       
   228 // C++ default constructor can NOT contain any code, that
       
   229 // might leave.
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 EXPORT_C void CUpnpElement::ExternalizeL( RWriteStream& aStream ) const
       
   233     {
       
   234 	TInt ExternalizationLength = MapItemNameToProperLength(*iElementName);
       
   235 
       
   236 	SafeExternalizeL(aStream, *iElementName, KMaxUpnpObjStringLen);
       
   237 	SafeExternalizeL(aStream, *iElementValue, ExternalizationLength);
       
   238 	SafeExternalize16L(aStream, *iFilePath, KMaxUpnpObjLongStringLen);
       
   239 
       
   240     aStream.WriteInt8L( IsRequired() );
       
   241        
       
   242     aStream.WriteInt16L( iAttributes.Count() );
       
   243     for ( TInt index(0); index < iAttributes.Count(); index++ )
       
   244         {
       
   245         iAttributes[index]->ExternalizeL( aStream );
       
   246         }
       
   247     }
       
   248         
       
   249 // -----------------------------------------------------------------------------
       
   250 // CUpnpElement::InternalizeL
       
   251 // C++ default constructor can NOT contain any code, that
       
   252 // might leave.
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 EXPORT_C void CUpnpElement::InternalizeL( RReadStream& aStream )
       
   256     {	
       
   257     delete iElementName;
       
   258     iElementName = NULL;
       
   259     iElementName = HBufC8::NewL( aStream, KMaxUpnpObjStringLen );
       
   260     
       
   261 	TInt InternalizationLength = MapItemNameToProperLength(*iElementName);
       
   262 
       
   263     // Element value   
       
   264     delete iElementValue;
       
   265     iElementValue = NULL;
       
   266     iElementValue = HBufC8::NewL( aStream, InternalizationLength );
       
   267     
       
   268     // FilePath
       
   269     delete iFilePath;
       
   270     iFilePath = NULL;
       
   271     iFilePath = HBufC::NewL( aStream, KMaxUpnpObjLongStringLen );
       
   272     
       
   273     // Is required
       
   274     SetIsRequired( aStream.ReadInt8L() );
       
   275       
       
   276     // cleanup
       
   277     iAttributes.ResetAndDestroy();
       
   278     
       
   279     // Attributes count
       
   280     TInt attributesCount = aStream.ReadInt16L();
       
   281 	
       
   282     // Then internalize them from the stream one by one
       
   283     for (TInt index = 0; index < attributesCount; index++ )
       
   284         {
       
   285         CUpnpAttribute* newAttribute = CUpnpAttribute::NewLC();
       
   286         newAttribute->InternalizeL( aStream );
       
   287         iAttributes.AppendL( newAttribute );
       
   288         CleanupStack::Pop( newAttribute ); 
       
   289         }
       
   290     }
       
   291 // -----------------------------------------------------------------------------
       
   292 // CUpnpElement::SetIsRequired
       
   293 // (other items were commented in a header).
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 EXPORT_C void CUpnpElement::SetIsRequired(TBool aIsRequired)
       
   297 {
       
   298     iIsRequired = aIsRequired;
       
   299 }
       
   300 // -----------------------------------------------------------------------------
       
   301 // CUpnpElement::IsRequired
       
   302 // (other items were commented in a header).
       
   303 // -----------------------------------------------------------------------------
       
   304 //
       
   305 EXPORT_C TBool CUpnpElement::IsRequired() const
       
   306 {
       
   307     return iIsRequired;
       
   308 }
       
   309 
       
   310 // -----------------------------------------------------------------------------
       
   311 // CUpnpElement::SafeExternalizeL
       
   312 // Writes safely aNumber of characters from the content to the stream.
       
   313 // (other items were commented in a header).
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 void CUpnpElement::SafeExternalizeL(RWriteStream& aStream, const TDesC8& aDesc, const TInt aNumber) const
       
   317 {	
       
   318     if( aDesc.Length() > aNumber )    
       
   319 		aStream << aDesc.Mid(0, aNumber);
       
   320     else
       
   321 		aStream << aDesc;    
       
   322 }
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CUpnpElement::SafeExternalize16L
       
   326 // Writes safely aNumber of characters from the content to the stream.
       
   327 // (other items were commented in a header).
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 void CUpnpElement::SafeExternalize16L(RWriteStream& aStream, const TDesC& aDesc, const TInt aNumber) const
       
   331 {	
       
   332     if( aDesc.Length() > aNumber )    
       
   333 		aStream << aDesc.Mid(0, aNumber);
       
   334     else
       
   335 		aStream << aDesc;    
       
   336 }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // CUpnpElement::MapItemNameToProperLength
       
   340 // Counts how many bytes will be used for internalization
       
   341 // (other items were commented in a header).
       
   342 // -----------------------------------------------------------------------------
       
   343 //
       
   344 TInt CUpnpElement::MapItemNameToProperLength(TDesC8& aItemName) const
       
   345 {
       
   346 	// DLNA Networked Device Interoperability Guidelines
       
   347 	// Requirement [7.3.17.4]: The following metadata properties must not exceed 256 bytes each in the XML-escaped form encoded in UTF-8	
       
   348 	if( aItemName == _L8("dc:date") ||
       
   349 		aItemName == _L8("dc:creator") ||
       
   350 		aItemName == _L8("upnp:genre") ||
       
   351 		aItemName == _L8("upnp:album") ||
       
   352 		aItemName == _L8("upnp:albumArtURI") ||
       
   353 		aItemName == _L8("upnp:channelNr") ||
       
   354 		aItemName == _L8("upnp:channelName")
       
   355 	)
       
   356     {
       
   357 		return KMaxUpnpObjStringLen; // 256
       
   358     }	
       
   359 	else 
       
   360 	{
       
   361 		return KMaxUpnpObjLongStringLen; //1024
       
   362 	}
       
   363 }
       
   364 
       
   365 //  End of File