upnpmediaserver/avobjects/src/upnpobject.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:  UPnPObject implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "upnpobject.h" 
       
    21 
       
    22 
       
    23 _LIT8(KContainer,"container"); 
       
    24 _LIT8(KItem,"item");
       
    25 _LIT8(KRes,"res");
       
    26 _LIT8(KProtocolinfo,"protocolInfo");
       
    27 
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CUpnpObject::CUpnpObject
       
    33 // C++ default constructor can NOT contain any code, that
       
    34 // might leave.
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CUpnpObject::CUpnpObject()
       
    38     {
       
    39     iRestricted = ETrue;
       
    40     }
       
    41     
       
    42 // Destructor
       
    43 EXPORT_C CUpnpObject::~CUpnpObject()
       
    44     {
       
    45     delete iId;
       
    46     delete iParentId;
       
    47     delete iTitle;
       
    48     delete iObjectClass;
       
    49     iElements.ResetAndDestroy();
       
    50     iElements.Close();
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CUpnpObject::ConstructL
       
    55 // Symbian 2nd phase constructor can leave.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 void CUpnpObject::ConstructL(  )
       
    59     {
       
    60     iId = KNullDesC8().AllocL();
       
    61     iParentId = KNullDesC8().AllocL();
       
    62     iTitle = KNullDesC8().AllocL();
       
    63     iObjectClass = KNullDesC8().AllocL();
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CUpnpObject::SetIdL
       
    68 // Sets Object Id
       
    69 // (other items were commented in a header).
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 EXPORT_C void CUpnpObject::SetIdL( const TDesC8& aId )
       
    73     {
       
    74     HBufC8* tmp = aId.AllocL();
       
    75     delete iId;
       
    76     iId = tmp;    
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CUpnpObject::Id
       
    81 // Returns Id of the object. If null, returns KNullDesC8
       
    82 // (other items were commented in a header).
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 EXPORT_C const TDesC8& CUpnpObject::Id() const
       
    86     {
       
    87     return *iId; 
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CUpnpObject::SetParentIdL
       
    92 // Sets Parent Id for the object.
       
    93 // (other items were commented in a header).
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 EXPORT_C void CUpnpObject::SetParentIdL( const TDesC8& aParentId )
       
    97     {
       
    98     HBufC8* tmp = aParentId.AllocL();
       
    99     delete iParentId;
       
   100     iParentId = tmp;
       
   101     }
       
   102        
       
   103 // -----------------------------------------------------------------------------
       
   104 // CUpnpObject::ParentId
       
   105 // Returns Parent Id of the object. If null, returns KNullDesC8
       
   106 // (other items were commented in a header).
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 EXPORT_C  const TDesC8& CUpnpObject::ParentId() const
       
   110     {
       
   111     return *iParentId;
       
   112     }
       
   113   
       
   114 // -----------------------------------------------------------------------------
       
   115 // CUpnpObject::AddElementL
       
   116 // Returns name of the object. If null, returns KNullDesC8
       
   117 // (other items were commented in a header).
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 EXPORT_C void CUpnpObject::AddElementL( CUpnpElement* aNewElement )
       
   121     {
       
   122     if ( aNewElement )
       
   123         {
       
   124         iElements.AppendL( aNewElement );
       
   125         }
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CUpnpObject::Name
       
   130 // Returns name of the object. If null, returns KNullDesC8
       
   131 // (other items were commented in a header).
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 EXPORT_C void CUpnpObject::RemoveElementL( const CUpnpElement* aNewElement )
       
   135     {
       
   136     for ( TInt index(0); index < iElements.Count(); index++ )
       
   137         {
       
   138         if ( iElements[index] == aNewElement )
       
   139             {
       
   140             iElements.Remove( index );
       
   141            break;
       
   142             }
       
   143         }
       
   144     }  
       
   145 // -----------------------------------------------------------------------------
       
   146 // CUpnpObject::GetElements
       
   147 // Returns name of the object. If null, returns KNullDesC8
       
   148 // (other items were commented in a header).
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 EXPORT_C const RUPnPElementsArray& CUpnpObject::GetElements()
       
   152     {
       
   153     return iElements;
       
   154     }
       
   155     
       
   156 // -----------------------------------------------------------------------------
       
   157 // CUpnpObject::SetNameL
       
   158 // Sets name for the object.
       
   159 // (other items were commented in a header).
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 EXPORT_C void CUpnpObject::SetTitleL( const TDesC8& aTitle )
       
   163     {
       
   164     HBufC8* tmp = aTitle.AllocL();
       
   165     delete iTitle;
       
   166     iTitle = tmp;  
       
   167     }
       
   168       
       
   169 // -----------------------------------------------------------------------------
       
   170 // CUpnpObject::Name
       
   171 // Returns name of the object. If null, returns KNullDesC8
       
   172 // (other items were commented in a header).
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 EXPORT_C const TDesC8& CUpnpObject::Title() const
       
   176     {
       
   177     return *iTitle;
       
   178     }
       
   179 // -----------------------------------------------------------------------------
       
   180 // CUpnpObject::SetObjectClassL
       
   181 // Sets class object for the object.
       
   182 // (other items were commented in a header).
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 EXPORT_C void CUpnpObject::SetObjectClassL( const TDesC8& aObjectClass )
       
   186     {
       
   187     ValidateClassL(aObjectClass); // leaves on error
       
   188     HBufC8* tmp = aObjectClass.AllocL();
       
   189     delete iObjectClass;
       
   190     iObjectClass = tmp;  
       
   191     }
       
   192 // -----------------------------------------------------------------------------
       
   193 // CUpnpObject::ValidateClassL
       
   194 // (other items were commented in a header).
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 void CUpnpObject::ValidateClassL(const TDesC8& aObjectClass)   
       
   198 {
       
   199     // verify the class
       
   200     if( ObjectType() == EUPnPContainer )
       
   201     { // it must contains "container" string
       
   202         if( aObjectClass.Find(KContainer) == KErrNotFound )
       
   203         {
       
   204             User::Leave(KErrArgument);
       
   205         }
       
   206     }
       
   207     else if(ObjectType() == EUPnPItem)
       
   208     {// it must contains "item" string
       
   209         if( aObjectClass.Find(KItem) == KErrNotFound )
       
   210         {
       
   211             User::Leave(KErrArgument);
       
   212         }
       
   213     }
       
   214     else
       
   215     { // should never happen
       
   216         User::Invariant();
       
   217     }
       
   218 }
       
   219 // -----------------------------------------------------------------------------
       
   220 // CUpnpObject::ObjectClass
       
   221 // Returns objectclass of the object. If null, returns KNullDesC8
       
   222 // (other items were commented in a header).
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 EXPORT_C const TDesC8& CUpnpObject::ObjectClass() const
       
   226     {
       
   227     return *iObjectClass;
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CUpnpObject::SetRestriction()
       
   232 // Sets the restriction info. 
       
   233 // (other items were commented in a header).
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 EXPORT_C void CUpnpObject::SetRestricted( TBool aRestriction )
       
   237     {
       
   238     iRestricted = aRestriction;
       
   239     }
       
   240    
       
   241 // -----------------------------------------------------------------------------
       
   242 // CUpnpObject::Restriction()
       
   243 // Gets the restriction info. 
       
   244 // (other items were commented in a header).
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 EXPORT_C TBool CUpnpObject::Restricted() const
       
   248     {
       
   249     return iRestricted;
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CUpnpObject::SetWriteStatus()
       
   254 // Sets the restriction info. 
       
   255 // (other items were commented in a header).
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 EXPORT_C void CUpnpObject::SetWriteStatus( TBool aWriteStatus )
       
   259     {
       
   260     iWriteStatus = aWriteStatus;
       
   261     }
       
   262    
       
   263 // -----------------------------------------------------------------------------
       
   264 // CUpnpObject::Restriction()
       
   265 // Gets the restriction info. 
       
   266 // (other items were commented in a header).
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 EXPORT_C TBool CUpnpObject::WriteStatus() const
       
   270     {
       
   271     return iWriteStatus;
       
   272     }
       
   273     
       
   274 // -----------------------------------------------------------------------------
       
   275 // CUpnpObject::CopyL
       
   276 // Copies the content from parameter CUpnpObject to this
       
   277 // (other items were commented in a header).
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 EXPORT_C void CUpnpObject::CopyL( const CUpnpObject& aObject )
       
   281     {
       
   282     // Check that parameter type is also item
       
   283     if ( aObject.ObjectType() != ObjectType() )
       
   284         {
       
   285         User::Leave( KErrArgument );
       
   286         }
       
   287         
       
   288     CBufFlat* buffer = CBufFlat::NewL( sizeof( aObject ) );
       
   289     CleanupStack::PushL( buffer );
       
   290 
       
   291     RBufWriteStream outStream;
       
   292     outStream.Open( *buffer );
       
   293     CleanupClosePushL( outStream );
       
   294 	
       
   295     RBufReadStream inStream;
       
   296     inStream.Open( *buffer );
       
   297     CleanupClosePushL( inStream );
       
   298 	
       
   299     aObject.ExternalizeL( outStream );
       
   300     InternalizeL( inStream );
       
   301 	
       
   302     CleanupStack::PopAndDestroy( 3 );   // inStream && outStream && buffer
       
   303     }
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CUpnpObject::BaseExternalizeL
       
   307 // Writes the content to stream.
       
   308 // (other items were commented in a header).
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 void CUpnpObject::BaseExternalizeL( RWriteStream& aStream ) const 
       
   312     { 
       
   313 		
       
   314     SafeExternalizeL(aStream, Id(), KMaxUpnpObjStringLen);
       
   315     SafeExternalizeL(aStream, ParentId(), KMaxUpnpObjStringLen);
       
   316     
       
   317     aStream.WriteInt8L(Restricted());
       
   318             
       
   319     SafeExternalizeL(aStream, Title(), KMaxUpnpObjStringLen);                            
       
   320     SafeExternalizeL(aStream, ObjectClass(), KMaxUpnpObjStringLen);
       
   321     
       
   322     // Elements
       
   323     aStream.WriteInt16L( iElements.Count() );
       
   324     for ( TInt index(0); index < iElements.Count(); index++ )
       
   325         {
       
   326         iElements[index]->ExternalizeL( aStream );
       
   327         }
       
   328     }
       
   329 // -----------------------------------------------------------------------------
       
   330 // CUpnpObject::SafeExternalizeL
       
   331 // Writes safely aNumber of characters from the content to the stream.
       
   332 // (other items were commented in a header).
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 void CUpnpObject::SafeExternalizeL(RWriteStream& aStream, const TDesC8& aDesc, const TInt aNumber) const
       
   336 {	
       
   337     if( aDesc.Length() > aNumber )    
       
   338 		aStream << aDesc.Mid(0, aNumber);
       
   339     else
       
   340 		aStream << aDesc;    
       
   341 }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CUpnpObject::BaseInternalizeL
       
   345 // Fills item information from stream
       
   346 // (other items were commented in a header).
       
   347 // -----------------------------------------------------------------------------
       
   348 //	
       
   349 void CUpnpObject::BaseInternalizeL( RReadStream& aStream )
       
   350     {
       
   351     delete iId;
       
   352     iId = NULL;
       
   353     iId = HBufC8::NewL( aStream, KMaxUpnpObjStringLen  );
       
   354     
       
   355     delete iParentId;
       
   356     iParentId = NULL;
       
   357     iParentId = HBufC8::NewL( aStream, KMaxUpnpObjStringLen );
       
   358     iRestricted = aStream.ReadInt8L();
       
   359     
       
   360     // Name (title)
       
   361     delete iTitle;
       
   362     iTitle = NULL;
       
   363     iTitle = HBufC8::NewL( aStream, KMaxUpnpObjStringLen );
       
   364     
       
   365     // ObjectClass
       
   366     delete iObjectClass;
       
   367     iObjectClass = NULL;
       
   368     iObjectClass = HBufC8::NewL( aStream, KMaxUpnpObjStringLen );	
       
   369             
       
   370     // Elements internalization
       
   371     iElements.ResetAndDestroy();
       
   372     
       
   373     // Elements count
       
   374     TInt elementsCount = aStream.ReadInt16L();	
       
   375     // Then internalize them from the stream one by one
       
   376     for (TInt index = 0; index < elementsCount; index++ )
       
   377         {
       
   378         CUpnpElement* newElement = CUpnpElement::NewLC();
       
   379         newElement->InternalizeL( aStream );
       
   380         iElements.AppendL( newElement );
       
   381         CleanupStack::Pop( newElement ); 
       
   382         }
       
   383 	    
       
   384     }
       
   385 // -----------------------------------------------------------------------------
       
   386 // CUpnpObject::ToDes8L
       
   387 // (other items were commented in a header).
       
   388 // -----------------------------------------------------------------------------
       
   389 //	
       
   390 EXPORT_C HBufC8* CUpnpObject::ToDes8L() const
       
   391 {
       
   392     // serialize object
       
   393     CBufFlat* buf = CBufFlat::NewL(KDefBufferGranularity);
       
   394     CleanupStack::PushL(buf);
       
   395     RBufWriteStream stream(*buf);
       
   396     CleanupClosePushL(stream);
       
   397     
       
   398     stream << *this;
       
   399     
       
   400     // create heap descriptor
       
   401     HBufC8* hbuf = HBufC8::NewLC(buf->Size());
       
   402     TPtr8 ptr(hbuf->Des());
       
   403     buf->Read(0, ptr, buf->Size());
       
   404     
       
   405     // clean up
       
   406     CleanupStack::Pop(hbuf);
       
   407     CleanupStack::PopAndDestroy(&stream);
       
   408     CleanupStack::PopAndDestroy(buf);
       
   409     
       
   410     return hbuf;
       
   411 }
       
   412 
       
   413 // -----------------------------------------------------------------------------
       
   414 // CUpnpObject::AddResourceL
       
   415 // Attaches proper <res> element (resource associated with an UPnP object) to protocolInfo
       
   416 // (other items were commented in a header).
       
   417 // -----------------------------------------------------------------------------
       
   418 //
       
   419 EXPORT_C void CUpnpObject::AddResourceL( const TDesC16& aFilename,const TDesC8& aProtocolinfo )
       
   420     {
       
   421 	CUpnpElement* elRes = CUpnpElement::NewL(KRes);
       
   422     CleanupStack::PushL( elRes ); 
       
   423     elRes->SetFilePathL(aFilename); 
       
   424    
       
   425     CUpnpAttribute* atImportUri =
       
   426     CUpnpAttribute::NewL(KProtocolinfo);
       
   427     CleanupStack::PushL(atImportUri);
       
   428     atImportUri->SetValueL(aProtocolinfo);
       
   429     elRes->AddAttributeL(atImportUri);
       
   430     CleanupStack::Pop(atImportUri);        
       
   431       
       
   432     AddElementL( elRes );
       
   433     CleanupStack::Pop( elRes );  
       
   434     }
       
   435 //  End of File