upnp/upnpstack/serviceframework/src/upnpdevicedescriptionstore.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2006-2009 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:  UPnP Device Description Store
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <upnpdominterface.h>
       
    20 
       
    21 #include <upnpdevicedescriptionstore.h>
       
    22 #include <upnpcommonupnplits.h>
       
    23 #include "upnpfileutils.h"
       
    24 #include "upnpicon.h"
       
    25 #include "upnpdevice.h"
       
    26 #include "upnpcontenthandlerscontroller.h"
       
    27 
       
    28 // ======== MEMBER FUNCTIONS ========
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // CUpnpDeviceDescriptionStore::NewL()
       
    32 // Two phased constructor
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 EXPORT_C CUpnpDeviceDescriptionStore *CUpnpDeviceDescriptionStore::NewL(
       
    36     const TDesC& aFilename )
       
    37     {
       
    38     CUpnpDeviceDescriptionStore* self = CUpnpDeviceDescriptionStore::NewLC( aFilename );    
       
    39     CleanupStack::Pop();
       
    40     return self;
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CUpnpDeviceDescriptionStore::NewLC()
       
    45 // Two phased constructor, leaves the object onto the CleanupStack
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 EXPORT_C CUpnpDeviceDescriptionStore* CUpnpDeviceDescriptionStore::NewLC(
       
    49     const TDesC& aFilename )
       
    50     {
       
    51     CUpnpDeviceDescriptionStore* self = new (ELeave) CUpnpDeviceDescriptionStore();
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL( aFilename );
       
    54     return self;
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // CUpnpDeviceDescriptionStore::~CUpnpDeviceDescriptionStore()
       
    59 // Destructor of CUpnpDeviceDescriptionStore class
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CUpnpDeviceDescriptionStore::~CUpnpDeviceDescriptionStore()
       
    63     {
       
    64     iFileName.Close();
       
    65     iDescription.Close();
       
    66     iParser.Close();
       
    67     iDOMImpl.Close();
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // CUpnpDeviceDescriptionStore::DescriptionFile()
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 EXPORT_C const TDesC& CUpnpDeviceDescriptionStore::DescriptionFile()
       
    75     {
       
    76     return iFileName;
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // CUpnpDeviceDescriptionStore::SetPropertyL()
       
    81 // Sets some property value in an emmbedded device.
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 EXPORT_C void CUpnpDeviceDescriptionStore::SetPropertyL( const TDesC8& aName,
       
    85     const TDesC8& aValue, const TDesC8& aUuid )
       
    86     {
       
    87     TXmlEngElement root = iDescription.DocumentElement();
       
    88     TXmlEngElement local;
       
    89     TXmlEngElement property;
       
    90     RArray<TXmlEngElement> elementList;
       
    91 
       
    92     TRAPD( error, UpnpDomInterface::GetElementListL( root, elementList, KUdn ) );
       
    93 
       
    94     for ( TInt i( 0); i < elementList.Count() ; i++ )
       
    95         {
       
    96         if ( !elementList[ i ].Value().Compare( aUuid ) )
       
    97             {
       
    98             elementList.Close();
       
    99 
       
   100             TRAP( error, UpnpDomInterface::GetElementListL( root,
       
   101                 elementList, aName ) );
       
   102 
       
   103             if ( elementList.Count() && i < elementList.Count() )
       
   104                 {
       
   105                 property = elementList[ i ];
       
   106                 elementList.Close();
       
   107                 if ( !error )
       
   108                     {
       
   109                     property.SetValueL( aValue );
       
   110                     return;
       
   111                     }
       
   112                 }
       
   113             else
       
   114                 {
       
   115                 User::Leave( KErrNotFound );
       
   116                 }
       
   117             }
       
   118         }
       
   119 
       
   120     elementList.Close();
       
   121     User::Leave( KErrNotFound );
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CUpnpDeviceDescriptionStore::RemoveTagL
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C void CUpnpDeviceDescriptionStore::RemoveTagL( const TDesC8& aTagName )
       
   129     {
       
   130     TXmlEngElement root = iDescription.DocumentElement();
       
   131     TXmlEngElement element;
       
   132     TBool elementFound = EFalse;
       
   133 
       
   134     elementFound = UpnpDomInterface::GetElementL( root, element, aTagName );
       
   135     if ( elementFound )
       
   136         {
       
   137         UpnpDomInterface::DeleteElement( element );
       
   138         }
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CUpnpDeviceDescriptionStore::AddTagL
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 EXPORT_C TXmlEngElement CUpnpDeviceDescriptionStore::AddTagL( const TDesC8& aTagName )
       
   146     {
       
   147     TXmlEngElement root = iDescription.DocumentElement();
       
   148     TXmlEngElement level1;
       
   149     TXmlEngElement level2;
       
   150     TBool elementFound = EFalse;
       
   151 
       
   152     elementFound = UpnpDomInterface::GetElementL( root, level1, KDevice );
       
   153     if ( elementFound )
       
   154         {
       
   155         elementFound = UpnpDomInterface::GetElementL( level1, level2,
       
   156             aTagName );
       
   157         if ( !elementFound )
       
   158             {
       
   159             level2 = level1.AddNewElementL( aTagName );
       
   160             }
       
   161         }
       
   162     return level2;
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // CUpnpDeviceDescriptionStore::AddIconL()
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 EXPORT_C void CUpnpDeviceDescriptionStore::AddIconL( const CUpnpIcon& aIcon )
       
   170     {
       
   171     TXmlEngElement root = iDescription.DocumentElement();
       
   172     TXmlEngElement propertyref;
       
   173     TXmlEngElement property;
       
   174     TXmlEngElement propertyiconlist;
       
   175     TXmlEngElement url;
       
   176     RArray<TXmlEngElement> IconList;
       
   177 
       
   178     TRAPD( error, UpnpDomInterface::GetElementL( root, property, KDevice ) );
       
   179     if ( !error && property.NotNull() )
       
   180         {
       
   181         TRAPD( error, UpnpDomInterface::GetElementL( property,
       
   182             propertyiconlist, KIconList ) );
       
   183         if ( !error && propertyiconlist.NotNull() )
       
   184             {
       
   185             TRAPD( error, UpnpDomInterface::GetElementListL(
       
   186                 propertyiconlist, IconList, KIcon ) );
       
   187             if ( !error && IconList.Count() != 0 )
       
   188                 {
       
   189                 AddIconElementL( propertyiconlist, aIcon.MimeType(),
       
   190                     aIcon.WidthTPtrC8(), aIcon.HeightTPtrC8(),
       
   191                     aIcon.DepthTPtrC8(), aIcon.Url() );
       
   192                 }
       
   193             User::LeaveIfError( error );
       
   194             }
       
   195         else
       
   196             {
       
   197             propertyref = property.AddNewElementL( KIconList );
       
   198             AddIconElementL( propertyref, aIcon.MimeType(),
       
   199                 aIcon.WidthTPtrC8(), aIcon.HeightTPtrC8(),
       
   200                 aIcon.DepthTPtrC8(), aIcon.Url() );
       
   201             }
       
   202         User::LeaveIfError( error );
       
   203         }
       
   204     User::LeaveIfError( error );
       
   205 
       
   206     IconList.Close();
       
   207     }
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 // CUpnpDeviceDescriptionStore::UpdateIconL()
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 EXPORT_C void CUpnpDeviceDescriptionStore::UpdateIconL( const CUpnpIcon& aIcon )
       
   214     {
       
   215     TXmlEngElement propertyiconlist = GetIconlistElementL();
       
   216     TXmlEngElement url;
       
   217     RArray<TXmlEngElement> IconList;
       
   218 
       
   219     TRAPD( error, UpnpDomInterface::GetElementListL( propertyiconlist,
       
   220         IconList, KIcon ) );
       
   221     if ( !error && IconList.Count() != 0 )
       
   222         {
       
   223         for ( TInt i=0; i<IconList.Count(); i++ )
       
   224             {
       
   225             TRAPD( error, UpnpDomInterface::GetElementL( IconList[ i ], url,
       
   226                 KUrl ) );
       
   227             if ( !error && url.NotNull() )
       
   228                 {
       
   229                 if ( !url.Value().Compare( aIcon.Url() ) )
       
   230                     {
       
   231                     IconList[i].Remove();
       
   232                     AddIconElementL( propertyiconlist, aIcon.MimeType(),
       
   233                         aIcon.WidthTPtrC8(), aIcon.HeightTPtrC8(),
       
   234                         aIcon.DepthTPtrC8(), aIcon.Url() );
       
   235                     }
       
   236                 }
       
   237             }
       
   238         User::LeaveIfError( error );
       
   239         }
       
   240     IconList.Close();
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // CUpnpDeviceDescriptionStore::RemoveIconL()
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 EXPORT_C void CUpnpDeviceDescriptionStore::RemoveIconL( const TDesC8& aUrl )
       
   248     {
       
   249     TXmlEngElement propertyiconlist = GetIconlistElementL();
       
   250     TXmlEngElement url;
       
   251     RArray<TXmlEngElement> IconList;
       
   252 
       
   253     TRAPD( error, UpnpDomInterface::GetElementListL( propertyiconlist,
       
   254         IconList, KIcon ) );
       
   255     if ( !error && IconList.Count()!=0 )
       
   256         {
       
   257         for ( TInt i=0; i<IconList.Count(); i++ )
       
   258             {
       
   259             TRAPD( error, UpnpDomInterface::GetElementL( IconList[ i ], url,
       
   260                 KUrl ) );
       
   261             if ( !error && url.NotNull() )
       
   262                 {
       
   263                 if ( !url.Value().Compare( aUrl ) )
       
   264                     {
       
   265                     if ( IconList.Count() == 1 )
       
   266                         {
       
   267                         propertyiconlist.Remove();
       
   268                         }
       
   269                     else
       
   270                         {
       
   271                         IconList[i].Remove();
       
   272                         }
       
   273                     }
       
   274                 }
       
   275             }
       
   276         User::LeaveIfError( error );
       
   277         }
       
   278     IconList.Close();
       
   279     }
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CUpnpDeviceDescriptionStore::RemoveIconsL()
       
   283 // ---------------------------------------------------------------------------
       
   284 //  
       
   285 EXPORT_C void CUpnpDeviceDescriptionStore::RemoveIconsL()
       
   286     {
       
   287     TXmlEngElement propertyiconlist = GetIconlistElementL();
       
   288     if ( propertyiconlist.NotNull() )
       
   289         {
       
   290         propertyiconlist.Remove();
       
   291         }
       
   292     }
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // CUpnpDeviceDescriptionStore::GetIconlistElementL()
       
   296 // Gets iconlist element
       
   297 // ---------------------------------------------------------------------------
       
   298 //    
       
   299 EXPORT_C TXmlEngElement CUpnpDeviceDescriptionStore::GetIconlistElementL()
       
   300     {
       
   301     TXmlEngElement root = iDescription.DocumentElement();
       
   302     TXmlEngElement property;
       
   303     TXmlEngElement propertyiconlist;
       
   304 
       
   305     TRAPD( error, UpnpDomInterface::GetElementL( root, property, KDevice ) );
       
   306     if ( !error && property.NotNull() )
       
   307         {
       
   308         TRAPD( error, UpnpDomInterface::GetElementL( property,
       
   309             propertyiconlist, KIconList ) );
       
   310         if ( !error && propertyiconlist.NotNull() )
       
   311             {
       
   312             return propertyiconlist;
       
   313             }
       
   314         User::LeaveIfError( error );
       
   315         }
       
   316     User::LeaveIfError( error );
       
   317 
       
   318     return NULL;
       
   319     }
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 // CUpnpDeviceDescriptionStore::Property()
       
   323 // Gets some property value from an embedded device.
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 EXPORT_C TPtrC8 CUpnpDeviceDescriptionStore::Property( const TDesC8& aName,
       
   327     const TDesC8& aUuid )
       
   328     {
       
   329     TXmlEngElement root = iDescription.DocumentElement();
       
   330     TXmlEngElement local;
       
   331     TXmlEngElement property;
       
   332     RArray<TXmlEngElement> elementList;
       
   333     TPtrC8 urn = KNullDesC8();
       
   334 
       
   335     TRAPD( error, UpnpDomInterface::GetElementListL( root, elementList, KUdn ) );
       
   336 
       
   337     for ( TInt i( 0); i < elementList.Count() ; i++ )
       
   338         {
       
   339         if ( !elementList[ i ].Value().Compare( aUuid ) )
       
   340             {
       
   341             elementList.Close();
       
   342 
       
   343             TRAP( error, UpnpDomInterface::GetElementListL( root,
       
   344                 elementList, aName ) );
       
   345 
       
   346             if ( elementList.Count() )
       
   347                 {
       
   348                 property = elementList[ i ];
       
   349 
       
   350                 if ( !error && property.Value().Length() )
       
   351                     {
       
   352                     urn.Set( property.Value() );
       
   353                     break;
       
   354                     }
       
   355                 }
       
   356             }
       
   357         }
       
   358     elementList.Close();
       
   359     return urn;
       
   360     }
       
   361 
       
   362 // ---------------------------------------------------------------------------
       
   363 // CUpnpDeviceDescriptionStore::SetPropertyL()
       
   364 // Sets some property value in a root device.
       
   365 // ---------------------------------------------------------------------------
       
   366 //   
       
   367 EXPORT_C void CUpnpDeviceDescriptionStore::SetPropertyL( const TDesC8& aName,
       
   368     const TDesC8& aValue )
       
   369     {
       
   370     TXmlEngElement root = iDescription.DocumentElement();
       
   371     TXmlEngElement level1;
       
   372 
       
   373     UpnpDomInterface::GetElementL( root, level1, aName );
       
   374 
       
   375     if ( level1.NotNull() )
       
   376         {
       
   377         level1.SetValueL( aValue );
       
   378         }
       
   379 
       
   380     else if ( !level1.NotNull() )
       
   381         {
       
   382         level1 = AddTagL( aName );
       
   383 
       
   384         if ( level1.NotNull() )
       
   385             {
       
   386             level1.SetValueL( aValue );
       
   387             }
       
   388         }
       
   389     }
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // CUpnpDeviceDescriptionStore::Property()
       
   393 // Gets some property value from a root device.
       
   394 // ---------------------------------------------------------------------------
       
   395 //
       
   396 EXPORT_C TPtrC8 CUpnpDeviceDescriptionStore::Property( const TDesC8& aName )
       
   397     {
       
   398     TXmlEngElement root = iDescription.DocumentElement();
       
   399     TXmlEngElement property;
       
   400     TPtrC8 urn = KNullDesC8();
       
   401 
       
   402     TRAPD( error, UpnpDomInterface::GetElementL( root, property, aName ) );
       
   403 
       
   404     if ( !error && property.NotNull() && property.Value().Length() )
       
   405         {
       
   406         urn.Set( property.Value() );
       
   407         }
       
   408 
       
   409     return urn;
       
   410     }
       
   411 
       
   412 // ---------------------------------------------------------------------------
       
   413 // CUpnpDeviceDescriptionStore::SaveL()
       
   414 // Saves device description file path
       
   415 // ---------------------------------------------------------------------------
       
   416 //
       
   417 EXPORT_C void CUpnpDeviceDescriptionStore::SaveL()
       
   418     {
       
   419     if ( UpnpFileUtil::CheckDiskSpaceShortL( EDriveC, KMinSpaceToWriteDescription ) )
       
   420         {
       
   421         User::Leave( KErrDiskFull );
       
   422         }
       
   423     iDescription.SaveL( iFileName );
       
   424     }
       
   425 
       
   426 // -----------------------------------------------------------------------------
       
   427 // CUpnpDeviceDescriptionStore::IconListL
       
   428 // -----------------------------------------------------------------------------
       
   429 //
       
   430 EXPORT_C RPointerArray< CUpnpIcon > CUpnpDeviceDescriptionStore::IconListL()
       
   431     {
       
   432     RPointerArray< CUpnpIcon > iconList;
       
   433 
       
   434     HBufC8* descr = UpnpFileUtil::ReadFileL( iFileName );
       
   435     CleanupStack::PushL( descr );
       
   436     
       
   437     CUpnpContentHandlersController* controller = CUpnpContentHandlersController::NewLC();
       
   438     CUpnpDevice* device = controller->ParseDeviceL( *descr );
       
   439     CleanupStack::PopAndDestroy( controller );
       
   440     CleanupStack::PushL( device );
       
   441 
       
   442     for ( TInt i = 0; i < device->Icons().Count(); i++ )
       
   443         {
       
   444         iconList.AppendL( device->Icons()[ i ] );
       
   445         }
       
   446     device->Icons().Reset();
       
   447     
       
   448     CleanupStack::PopAndDestroy( device );
       
   449     CleanupStack::PopAndDestroy( descr );
       
   450     
       
   451     return iconList;
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // CUpnpDeviceDescriptionStore::CUpnpDeviceDescriptionStore()
       
   456 // Default C++ constructor
       
   457 // ---------------------------------------------------------------------------
       
   458 //
       
   459 CUpnpDeviceDescriptionStore::CUpnpDeviceDescriptionStore()
       
   460     {
       
   461     }
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // CUpnpDeviceDescriptionStore::ConstructL()
       
   465 // 2nd phase constructor
       
   466 // ---------------------------------------------------------------------------
       
   467 //
       
   468 void CUpnpDeviceDescriptionStore::ConstructL( const TDesC& aFileName )
       
   469     {
       
   470     iFileName.CreateL( aFileName );
       
   471 
       
   472     HBufC8* descr = UpnpFileUtil::ReadFileL( aFileName );
       
   473     CleanupStack::PushL( descr );
       
   474 
       
   475     iDOMImpl.OpenL();
       
   476     User::LeaveIfError( iParser.Open( iDOMImpl ) );
       
   477     iDescription = iParser.ParseL( *descr );
       
   478     CleanupStack::PopAndDestroy( descr );
       
   479     }
       
   480 
       
   481 // ---------------------------------------------------------------------------
       
   482 // CUpnpDeviceDescriptionStore::AddIconElementL()
       
   483 // Adds icon element
       
   484 // ---------------------------------------------------------------------------
       
   485 //    
       
   486 void CUpnpDeviceDescriptionStore::AddIconElementL(
       
   487     TXmlEngElement& aPropertyIcon, const TDesC8& aMimeType,
       
   488     const TDesC8& aWidth, const TDesC8& aHeight, const TDesC8& aDepth,
       
   489     const TDesC8& aUrl )
       
   490     {
       
   491     TXmlEngElement propertyIcon = aPropertyIcon.AddNewElementL( KIcon );
       
   492     TXmlEngElement propertyref = propertyIcon.AddNewElementL( KMimeType );
       
   493     propertyref.SetTextL( aMimeType );
       
   494     propertyref = propertyIcon.AddNewElementL( KWidth );
       
   495     propertyref.SetTextL( aWidth );
       
   496     propertyref = propertyIcon.AddNewElementL( KHeight );
       
   497     propertyref.SetTextL( aHeight );
       
   498     propertyref = propertyIcon.AddNewElementL( KDepth );
       
   499     propertyref.SetTextL( aDepth );
       
   500     propertyref = propertyIcon.AddNewElementL( KUrl );
       
   501     propertyref.SetTextL( aUrl );
       
   502     }
       
   503 
       
   504 //  End of File