upnpmediaserver/contentdirectoryservice/src/upnpcontentdirectorydb.cpp
changeset 0 7f85d04be362
child 12 cdcbf344a1d3
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:  Database for MediaServer
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <barsread.h>
       
    21 #include <barsc.h>   
       
    22 #include <f32file.h>
       
    23 #include <xmlengdom.h>
       
    24 #include <sysutil.h>
       
    25 #include <upnpdominterface.h>
       
    26 #include <upnpstring.h>
       
    27 #include "upnpprotocolinfo.h"
       
    28 
       
    29 #include "upnpfileutils.h"
       
    30 #include "upnpcontentdirectorydb.h"
       
    31 #include "upnpcontentdirectoryglobals.h"
       
    32 #include "upnpelementfactory.h"
       
    33 #include "upnpelementbean.h"
       
    34 #include "upnpobjectbean.h"
       
    35 #include "upnpattributebean.h"
       
    36 #include "upnpfilterelement.h"
       
    37 #include "upnpresourcesbean.h"
       
    38 #include "upnpcdutils.h"
       
    39 #include "upnpcddbfactory.h"
       
    40 #include "upnpmetadatastorage.h"
       
    41 
       
    42 #define KLogFile _L("ContentDirectoryDb.log")
       
    43 #include "upnpcustomlog.h"
       
    44 
       
    45 // ============================= LOCAL FUNCTIONS ===============================
       
    46 // -----------------------------------------------------------------------------
       
    47 // CleanupFilterElementPointerArray
       
    48 // Used by TCleanupItem to clean a RPointerArray<CUpnpFilterElement>
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void CleanupFilterElementPointerArray( TAny* aArray )
       
    52     {
       
    53     (reinterpret_cast<RPointerArray<CUpnpFilterElement>*> (aArray))->ResetAndDestroy( );
       
    54     }
       
    55 
       
    56 // ============================ MEMBER FUNCTIONS ===============================
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CUpnpContentDirectoryDb::CUpnpContentDirectoryDb
       
    60 // C++ default constructor can NOT contain any code, that
       
    61 // might leave.
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CUpnpContentDirectoryDb::CUpnpContentDirectoryDb(
       
    65     CUpnpMetadataStorage* aMetadataStorage )
       
    66     {
       
    67     iMetadataStorage =aMetadataStorage;
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CUpnpContentDirectoryDb::ConstructL
       
    72 // Symbian 2nd phase constructor can leave.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 void CUpnpContentDirectoryDb::ConstructL()
       
    76     {
       
    77     iDOMImpl.OpenL( );
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CUpnpContentDirectoryDb::NewLC
       
    82 // Two-phased constructor.
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CUpnpContentDirectoryDb* CUpnpContentDirectoryDb::NewLC(
       
    86     CUpnpMetadataStorage* aMetadataStorage )
       
    87     {
       
    88     CUpnpContentDirectoryDb* self = new ( ELeave ) CUpnpContentDirectoryDb(aMetadataStorage);
       
    89     CleanupStack::PushL( self );
       
    90     self->ConstructL( );
       
    91     return self;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CUpnpContentDirectoryDb::NewL
       
    96 // Two-phased constructor.
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 CUpnpContentDirectoryDb* CUpnpContentDirectoryDb::NewL(
       
   100     CUpnpMetadataStorage* aMetadataStorage )
       
   101     {
       
   102     CUpnpContentDirectoryDb* self = NewLC( aMetadataStorage );
       
   103     CleanupStack::Pop( self );
       
   104     return self;
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CUpnpContentDirectoryDb::~~CUpnpContentDirectoryDb
       
   109 // Destructor
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 CUpnpContentDirectoryDb::~CUpnpContentDirectoryDb()
       
   113     {
       
   114     iDOMImpl.Close( );
       
   115     LOGCD( "CUpnpContentDirectoryDb::~CUpnpContentDirectoryDb", "", 0,
       
   116         "DataBase closed" );
       
   117     }
       
   118 // -----------------------------------------------------------------------------
       
   119 // CUpnpContentDirectoryDb::InsertObjectL
       
   120 // (other items were commented in a header).
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 TUpnpErrorCode CUpnpContentDirectoryDb::InsertObjectL(
       
   124     RXmlEngDocument& aFragment, TInt aContainerId, TInt* aId )
       
   125     {
       
   126     TUpnpErrorCode ret( EUpnpUndefined );
       
   127     // get object element
       
   128     TXmlEngElement element = UpnpCdUtils::GetObjectElementL( aFragment );
       
   129 
       
   130     // start transaction
       
   131     iMetadataStorage->BeginTransactionL( );
       
   132 
       
   133     TRAPD( err, ret = DoInsertObjectL( element, aContainerId, aId ) );
       
   134     if ( err )
       
   135         { // rollback if error
       
   136         iMetadataStorage->RollbackTransactionL( );
       
   137         User::Leave( err );
       
   138         }
       
   139     iMetadataStorage->CommitTransactionL( );
       
   140 
       
   141     return ret;
       
   142     }
       
   143 // -----------------------------------------------------------------------------
       
   144 // CUpnpContentDirectoryDb::GetObjectListL
       
   145 // (other items were commented in a header).
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 TUpnpErrorCode CUpnpContentDirectoryDb::GetObjectListL( TInt aParentId,
       
   149     RArray<TInt>& aList )
       
   150     {
       
   151     return iMetadataStorage->GetObjectListL( aParentId, aList );
       
   152     }
       
   153 // -----------------------------------------------------------------------------
       
   154 // CUpnpContentDirectoryDb::GetObjectListL
       
   155 // (other items were commented in a header).
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CUpnpContentDirectoryDb::GetObjectListL( const RArray<TInt>& aIds,
       
   159     const TDesC8& aFilter, RArray<RXmlEngDocument>& aObjs )
       
   160     {
       
   161     for ( TInt i = 0; i < aIds.Count( ); i++ )
       
   162         {
       
   163         RXmlEngDocument obj;
       
   164         CleanupClosePushL( obj );
       
   165         GetObjectL( aIds[i], obj, aFilter );
       
   166         aObjs.AppendL( obj );
       
   167         CleanupStack::Pop( &obj );
       
   168         }
       
   169     }
       
   170 // -----------------------------------------------------------------------------
       
   171 // CUpnpContentDirectoryDb::DeleteObjectL
       
   172 // (other items were commented in a header).
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 TUpnpErrorCode CUpnpContentDirectoryDb::DeleteObjectL( TInt aObjId,
       
   176     RArray<TInt>& aRefList, TBool aDeleteResource )
       
   177     {
       
   178     return iMetadataStorage->DeleteObjectL( aObjId, aRefList, aDeleteResource );
       
   179     }
       
   180 // -----------------------------------------------------------------------------
       
   181 // CUpnpContentDirectoryDb::UpdateObjectL
       
   182 // (other items were commented in a header).
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 TUpnpErrorCode CUpnpContentDirectoryDb::UpdateObjectL( TInt aObjId,
       
   186     RXmlEngDocument& aFragment )
       
   187     {
       
   188     TUpnpErrorCode ret = EUpnpUndefined;
       
   189 
       
   190     // // get object element
       
   191     TXmlEngElement element = UpnpCdUtils::GetObjectElementL( aFragment );
       
   192 
       
   193     // start transaction
       
   194     iMetadataStorage->BeginTransactionL( );
       
   195 
       
   196     TRAPD( err, ret = DoUpdateObjectL( aObjId, element ) );
       
   197     if ( err )
       
   198         { // rollback - error
       
   199         iMetadataStorage->RollbackTransactionL( );
       
   200         User::Leave( err );
       
   201         }
       
   202     // commit if success
       
   203     iMetadataStorage->CommitTransactionL( );
       
   204 
       
   205     return ret;
       
   206     }
       
   207 // -----------------------------------------------------------------------------
       
   208 // CUpnpContentDirectoryDb::DoUpdateObjectL
       
   209 // (other items were commented in a header).
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 TUpnpErrorCode CUpnpContentDirectoryDb::DoUpdateObjectL( TInt aObjId,
       
   213     TXmlEngElement aFragment )
       
   214     {
       
   215     TUpnpErrorCode ret = EUpnpUndefined;
       
   216 
       
   217     // delete object
       
   218     ret = iMetadataStorage->DoDeleteObjectL( aObjId, EFalse, EFalse );
       
   219     TXmlEngElement el = aFragment;
       
   220     ret = InsertUpdatedL( aObjId, el );
       
   221     return ret;
       
   222     }
       
   223 // -----------------------------------------------------------------------------
       
   224 // CUpnpContentDirectoryDb::InsertUpdatedL
       
   225 // (other items were commented in a header).
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 TUpnpErrorCode CUpnpContentDirectoryDb::InsertUpdatedL( TInt aObjId,
       
   229     TXmlEngElement& aElement )
       
   230     {
       
   231     TUpnpErrorCode ret = EUpnpUndefined;
       
   232     iMetadataStorage->InsertObjectIntoObjectTableL( aElement );
       
   233     ret = EUpnpOk;
       
   234     RXmlEngNodeList<TXmlEngElement> elements;
       
   235     CleanupClosePushL( elements );
       
   236     aElement.GetChildElements( elements );
       
   237 
       
   238     while ( elements.HasNext( ) )
       
   239         {
       
   240         TXmlEngElement el = elements.Next( );
       
   241         if ( el.Name().CompareF( KClassTagName ) && el.Name().CompareF( KTitleTagName ) )
       
   242             {
       
   243             ret = InsertElementL( el, aObjId );
       
   244             }
       
   245         else
       
   246             {
       
   247             UpnpCdUtils::IsElementRequiredL( el ); // just remove the marker attr
       
   248             }
       
   249         }
       
   250     CleanupStack::PopAndDestroy( &elements );
       
   251     return ret;
       
   252     }
       
   253 // -----------------------------------------------------------------------------
       
   254 // CUpnpContentDirectoryDb::InsertElementL
       
   255 // (other items were commented in a header).
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 TUpnpErrorCode CUpnpContentDirectoryDb::InsertElementL(
       
   259     const TXmlEngElement& aElement, TInt aObjId )
       
   260     {
       
   261     return InsertAttributesL( aElement, iMetadataStorage->InsertElementL(
       
   262         aElement, aObjId ), aObjId );
       
   263     }
       
   264 // -----------------------------------------------------------------------------
       
   265 // CUpnpContentDirectoryDb::PrepareDidlDocumentL
       
   266 // (other items were commented in a header).
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 RXmlEngDocument CUpnpContentDirectoryDb::PrepareDidlDocumentL()
       
   270     {
       
   271     RXmlEngDocument doc;
       
   272     doc.OpenL( iDOMImpl );
       
   273     TXmlEngElement el = doc.CreateDocumentElementL( KDidlLite( ) );
       
   274     el.AddNamespaceDeclarationL( KXmlnsNsUri( ), KCmlnsNsPref( ) );
       
   275     el.AddNamespaceDeclarationL( KDcNsUri( ), KDcNsPref( ) );
       
   276     el.AddNamespaceDeclarationL( KUpnpNsUri( ), KUpnpNsPref( ) );
       
   277     return doc;
       
   278 
       
   279     }
       
   280 // -----------------------------------------------------------------------------
       
   281 // CUpnpContentDirectoryDb::CheckObjectRestrictionL
       
   282 // (other items were commented in a header).
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 TUpnpErrorCode CUpnpContentDirectoryDb::CheckObjectRestrictionL( TInt aObjId )
       
   286     {
       
   287     return iMetadataStorage->CheckObjectRestrictionL( aObjId );
       
   288     }
       
   289 // -----------------------------------------------------------------------------
       
   290 // CUpnpContentDirectoryDb::GetObjectL
       
   291 // (other items were commented in a header).
       
   292 // -----------------------------------------------------------------------------
       
   293 TUpnpErrorCode CUpnpContentDirectoryDb::GetObjectL( TInt aObjectID,
       
   294     RXmlEngDocument& aDoc, const TDesC8& aFilterString )
       
   295     {
       
   296     TUpnpErrorCode ret = EUpnpUndefined;
       
   297 
       
   298     if ( aFilterString == KAsterisk8 )
       
   299         { // get the whole object
       
   300         ret = DoGetObjectL( aObjectID, aDoc, ETrue );
       
   301         }
       
   302     else if ( !aFilterString.Length( ) )
       
   303         { // get only required properties
       
   304         ret = DoGetObjectL( aObjectID, aDoc, EFalse );
       
   305         }
       
   306     else
       
   307         {
       
   308         RXmlEngDocument doc; // before the whole obj is created do not change the given reference,
       
   309         CleanupClosePushL( doc ); // it prevents from returning not completed object if something goes wrong 
       
   310         // during construction of response object
       
   311         // get required properties
       
   312         ret = DoGetObjectL( aObjectID, doc, EFalse );
       
   313         if ( ret == EUpnpOk ) // do not not continue if something went wrong
       
   314             {
       
   315             // prepare a list of filter elements
       
   316             RPointerArray<CUpnpFilterElement> filterElements;
       
   317             TCleanupItem cleanupItem( CleanupFilterElementPointerArray,
       
   318                 &filterElements );
       
   319             CleanupStack::PushL( cleanupItem );
       
   320             // parse filter string
       
   321             ParseFilterStringL( aFilterString, filterElements );
       
   322             // add requested elements
       
   323             AddRequestedPropertiesL( aObjectID,
       
   324                 UpnpCdUtils::GetObjectElementL( doc ), filterElements ); // ignore if error
       
   325             //clean up
       
   326             CleanupStack::PopAndDestroy( &filterElements );
       
   327             }
       
   328         if ( ret == EUpnpOk )
       
   329             { // object is completed
       
   330             aDoc = doc;
       
   331             CleanupStack::Pop( &doc );
       
   332             }
       
   333         else
       
   334             { // something went wrong
       
   335             CleanupStack::PopAndDestroy( &doc );
       
   336             }
       
   337 
       
   338         }
       
   339 
       
   340     return ret;
       
   341     }
       
   342 // -----------------------------------------------------------------------------
       
   343 // CUpnpContentDirectoryDb::GetObjectL
       
   344 // (other items were commented in a header).
       
   345 // -----------------------------------------------------------------------------
       
   346 TUpnpErrorCode CUpnpContentDirectoryDb::AddRequestedPropertiesL(
       
   347     TInt aObjectID, TXmlEngElement aElement,
       
   348     const RPointerArray<CUpnpFilterElement>& aFilterElements )
       
   349     {
       
   350     TUpnpErrorCode ret = EUpnpUndefined;
       
   351     // aFilterElements must be sorted
       
   352     for ( TInt i = 0; i < aFilterElements.Count( ); i++ )
       
   353         {
       
   354         const CUpnpFilterElement* filter = aFilterElements[i];
       
   355         if ( filter->ElementName().Length( ) )
       
   356             { // element part defined
       
   357             AddElementL( aElement, aObjectID, filter );
       
   358             }
       
   359         else
       
   360             { // element part not defined - it is attribute of main element tag
       
   361             for ( TInt j = 0; j < filter->Count( ); j++ )
       
   362                 {
       
   363                 if ( (*filter)[j] == KChildCount )
       
   364                     {
       
   365                     SetChildCountL( aElement, aObjectID );
       
   366                     ret = EUpnpOk;
       
   367                     }
       
   368                 else
       
   369                     {
       
   370                     ret = iMetadataStorage->AddMainTagAttrL( aElement,
       
   371                         aObjectID, (*filter)[j] );
       
   372                     }
       
   373                 }
       
   374             }
       
   375         }
       
   376     return ret;
       
   377     }
       
   378 // -----------------------------------------------------------------------------
       
   379 // CUpnpContentDirectoryDb::SetChildCountL
       
   380 // (other items were commented in a header).
       
   381 // -----------------------------------------------------------------------------
       
   382 void CUpnpContentDirectoryDb::SetChildCountL( TXmlEngElement aElement,
       
   383     TInt aId )
       
   384     {
       
   385     if ( aElement.Name( ) == KContainer )
       
   386         {
       
   387         // value
       
   388         TInt childCount = iMetadataStorage->GetChildCountL( aId );
       
   389         TBuf8<KMaxIntegerLen> num;
       
   390         num.Num( childCount );
       
   391 
       
   392         // is it already exist?
       
   393         TXmlEngAttr childAttr = aElement.AttributeNodeL( KChildCount( ) );
       
   394         if ( childAttr.IsNull( ) )
       
   395             { // not exist - add 
       
   396             aElement.AddNewAttributeL( KChildCount( ), num );
       
   397             }
       
   398         else
       
   399             { // already exists - update
       
   400             childAttr.SetValueL( num );
       
   401             }
       
   402         }
       
   403     }
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CUpnpContentDirectoryDb::AddElementL
       
   407 // (other items were commented in a header).
       
   408 // -----------------------------------------------------------------------------
       
   409 void CUpnpContentDirectoryDb::AddElementL( TXmlEngElement aElement,
       
   410     TInt aObjectID, const CUpnpFilterElement* aReqElement )
       
   411     {
       
   412     // get elements from database
       
   413     RDbView view = iMetadataStorage->GetElementViewL( aObjectID,
       
   414         aReqElement->ElementName( ) );
       
   415     CleanupClosePushL( view );
       
   416 
       
   417     // for each element found
       
   418     while ( view.NextL( ) )
       
   419         {
       
   420         view.GetL( );
       
   421         CUpnpElementBean* elBean = CUpnpElementBean::NewLC( view );
       
   422 
       
   423         // add element
       
   424         TXmlEngElement newElement = elBean->AttachElmL( aElement );
       
   425 
       
   426         // add required attributes
       
   427         AddAttributesL( elBean->ElmId( ), aObjectID, newElement, EFalse );
       
   428 
       
   429         // for each requested attribute
       
   430         for ( TInt i = 0; i < aReqElement->Count( ); i++ )
       
   431             {
       
   432             TPtrC8 attrNamePtr((*aReqElement)[i]);
       
   433 
       
   434             TXmlEngAttr attr = newElement.AttributeNodeL( attrNamePtr );
       
   435             // add if not present
       
   436             // ignore if element has not any attribute
       
   437             if ( attr.IsNull( ) && elBean->ElmHasAttribute( ) )
       
   438                 {
       
   439                 AddAttributeL( newElement, attrNamePtr, elBean->ElmId( ),
       
   440                     aObjectID );
       
   441                 }
       
   442             }
       
   443         // clean up
       
   444         CleanupStack::PopAndDestroy( elBean );
       
   445         }
       
   446     // clean up
       
   447     CleanupStack::PopAndDestroy( &view );
       
   448     }
       
   449 
       
   450 // -----------------------------------------------------------------------------
       
   451 // CUpnpContentDirectoryDb::ParseFilterStringL
       
   452 // (other items were commented in a header).
       
   453 // -----------------------------------------------------------------------------
       
   454 //
       
   455 void CUpnpContentDirectoryDb::ParseFilterStringL( const TDesC8& aFilter,
       
   456     RPointerArray<CUpnpFilterElement>& aList )
       
   457     {
       
   458     // separate filter's parts
       
   459     CDesC8ArrayFlat* list = new(ELeave) CDesC8ArrayFlat(KDefaultGranularity);
       
   460     CleanupStack::PushL( list );
       
   461     TInt commaPos;
       
   462     TPtrC8 filter(aFilter);
       
   463     while ( (commaPos = filter.Find( KCommaStr8( ) ) ) != KErrNotFound )
       
   464         {
       
   465         HBufC8* buf = filter.Left(commaPos).AllocLC( );
       
   466         buf->Des().TrimAll( );
       
   467         list->AppendL( *buf );
       
   468         filter.Set( filter.Mid( commaPos+1 ) );
       
   469         CleanupStack::PopAndDestroy( buf );
       
   470         }
       
   471     // add last
       
   472     HBufC8* buf = filter.AllocLC( );
       
   473     buf->Des().TrimAll( );
       
   474     list->AppendL( *buf );
       
   475     filter.Set( filter.Mid( commaPos+1 ) );
       
   476     CleanupStack::PopAndDestroy( buf );
       
   477 
       
   478     // sort to simplify
       
   479     list->Sort( );
       
   480 
       
   481     TPtrC8 curr( KMinusOne8 );
       
   482     for ( TInt i = 0; i< list->Count( ); i++ ) // for each filter property
       
   483         {
       
   484         // split filter string - element@attribute
       
   485         TInt atPos = (*list)[i].Find( KAtStr8 );
       
   486         TPtrC8 attr;
       
   487         if ( atPos == KErrNotFound ) // is it element name
       
   488             { // it is element
       
   489             attr.Set( KNullDesC8 );
       
   490             atPos = (*list)[i].Length( );
       
   491             }
       
   492         else
       
   493             {
       
   494             attr.Set( (*list)[i].Mid( atPos+1 ) );
       
   495             }
       
   496         TPtrC8 element((*list)[i].Left( atPos ) );
       
   497 
       
   498         // do not add "class" and "title" elements - required by default
       
   499         if ( element.CompareF( KClassTagNameWithNs8 )
       
   500                 && element.CompareF( KTitleTagNameWithNs8 ) )
       
   501             { // it is neither a class nor title element
       
   502             if ( element == curr )
       
   503                 { // already exists, add next attribute
       
   504                 if ( attr.Length( ) ) // ignore empty attr
       
   505                     aList[aList.Count()-1]->AppendAttributeL( attr );
       
   506                 }
       
   507             else
       
   508                 { // create new filter element
       
   509                 CUpnpFilterElement* tmp = CUpnpFilterElement::NewLC( );
       
   510                 tmp->SetElementNameL( element );
       
   511                 if ( attr.Length( ) ) // ignore epmty attr
       
   512                     tmp->AppendAttributeL( attr );
       
   513                 aList.AppendL( tmp );
       
   514                 CleanupStack::Pop( tmp );
       
   515                 curr.Set( element );
       
   516                 }
       
   517             }
       
   518         }
       
   519     // clean up
       
   520     CleanupStack::PopAndDestroy( list );
       
   521     }
       
   522 // -----------------------------------------------------------------------------
       
   523 // CUpnpContentDirectoryDb::DoGetObjectL
       
   524 // Function returns DIDL-Lite XML fragment with metadata for requested object.
       
   525 // IMPORTANT: 1. Caller takes responsibility for returned document and is 
       
   526 //              obliged to close it.
       
   527 //           2. The root element of the document is <DIDL-Lite> element and
       
   528 //              the object's element is its direct, onluy child.
       
   529 // -----------------------------------------------------------------------------
       
   530 //
       
   531 TUpnpErrorCode CUpnpContentDirectoryDb::DoGetObjectL( TInt aObjectID,
       
   532     RXmlEngDocument& aDoc, TBool aAll )
       
   533     {
       
   534     TUpnpErrorCode ret = EUpnpUndefined;
       
   535     RDbView view = iMetadataStorage->GetObjViewL( aObjectID );
       
   536     CleanupClosePushL( view );
       
   537 
       
   538     // Get result
       
   539     if ( view.FirstL( ) )
       
   540         {
       
   541         // get row
       
   542         view.GetL( );
       
   543 
       
   544         // prepare DIDL-Lite
       
   545         RXmlEngDocument doc;
       
   546         CleanupClosePushL( doc );
       
   547         doc = PrepareDidlDocumentL( );
       
   548 
       
   549         // object bean      
       
   550         CUpnpObjectBean* objBean = CUpnpObjectBean::NewLC( view );
       
   551         TXmlEngElement objElement;
       
   552 
       
   553         if ( aAll )
       
   554             { // all object's properties
       
   555             objElement
       
   556                     = objBean->AttachWholeObjectElL( doc.DocumentElement( ) );
       
   557             // childCount
       
   558             SetChildCountL( UpnpCdUtils::GetObjectElementL( doc ),
       
   559                 objBean->ObjId( ) );
       
   560             }
       
   561         else
       
   562             { // only required
       
   563             objElement = objBean->AttachObjectElL( doc.DocumentElement( ) );
       
   564             }
       
   565 
       
   566         // clean up
       
   567         CleanupStack::PopAndDestroy( objBean );
       
   568 
       
   569         // aObjectID might be a reference object, so trying to get real object id        
       
   570         TInt realObjectId = ReferedObjectIdL( aObjectID );
       
   571 
       
   572         // add elements to the tree
       
   573         AddElementsL( realObjectId, objElement, aAll );
       
   574 
       
   575         // assign to the given reference
       
   576         aDoc = doc;
       
   577         // everything went right
       
   578         ret = EUpnpOk;
       
   579         // do not remove doc
       
   580         CleanupStack::Pop( &doc );
       
   581         }
       
   582     else
       
   583         { // the is no such object
       
   584         ret = ENoSuchObject;
       
   585         }
       
   586     // clean up
       
   587     CleanupStack::PopAndDestroy( &view );
       
   588 
       
   589     return ret;
       
   590     }
       
   591 // -----------------------------------------------------------------------------
       
   592 // CUpnpContentDirectoryDb::AddElementsL
       
   593 // (other items were commented in a header).
       
   594 // -----------------------------------------------------------------------------
       
   595 //
       
   596 void CUpnpContentDirectoryDb::AddElementsL( TInt aObjectID,
       
   597     TXmlEngElement aObjectElement, TBool aAll )
       
   598     {
       
   599     RDbView view = iMetadataStorage->GetElementsViewL( aObjectID, aAll );
       
   600     CleanupClosePushL( view );
       
   601     // Iterate through elements
       
   602     while ( view.NextL( ) )
       
   603         {
       
   604         view.GetL( );
       
   605         CUpnpElementBean* elBean = CUpnpElementBean::NewLC( view );
       
   606 
       
   607         // add new element
       
   608         TXmlEngElement newElement = elBean->AttachElmL( aObjectElement );
       
   609 
       
   610         // add attributes
       
   611         AddAttributesL( elBean->ElmId( ), aObjectID, newElement, aAll );
       
   612 
       
   613         // clean up
       
   614         CleanupStack::PopAndDestroy( elBean );
       
   615         }
       
   616     // clean up
       
   617     CleanupStack::PopAndDestroy( &view );
       
   618     }
       
   619 
       
   620 // -----------------------------------------------------------------------------
       
   621 // CUpnpContentDirectoryDb::AddAttributesL
       
   622 // (other items were commented in a header).
       
   623 // -----------------------------------------------------------------------------
       
   624 //  
       
   625 void CUpnpContentDirectoryDb::AddAttributesL( TInt aElementId,
       
   626     TInt aObjectID, TXmlEngElement& aElement, TBool aAll )
       
   627     {
       
   628     RDbView view = iMetadataStorage->GetAttributesViewByObjectIdL( aObjectID,
       
   629         aAll );
       
   630     CleanupClosePushL( view );
       
   631 
       
   632     CDbColSet* colSet = view.ColSetL( );
       
   633     const TInt elmIdColNo = colSet->ColNo( KAtrElmIdColName );
       
   634     delete colSet;
       
   635 
       
   636     // Iterate through attributes
       
   637     while ( view.NextL( ) )
       
   638         {
       
   639         view.GetL( );
       
   640 
       
   641         if ( view.ColInt( elmIdColNo ) == aElementId )
       
   642             {
       
   643             CUpnpAttributeBean* atr = CUpnpAttributeBean::NewLC( view );
       
   644             atr->AttachAttrL( aElement );
       
   645             CleanupStack::PopAndDestroy( atr );
       
   646             }
       
   647         }
       
   648     // clean up
       
   649     CleanupStack::PopAndDestroy( &view );
       
   650     }
       
   651 
       
   652 // -----------------------------------------------------------------------------
       
   653 // CUpnpContentDirectoryDb::DoInsertObjectL
       
   654 // (other items were commented in a header).
       
   655 // -----------------------------------------------------------------------------
       
   656 //
       
   657 TUpnpErrorCode CUpnpContentDirectoryDb::DoInsertObjectL(
       
   658     TXmlEngElement& aElement, TInt aContainerId, TInt* aId )
       
   659     {
       
   660     TUpnpErrorCode ret = EUpnpUndefined;
       
   661 
       
   662     *aId = SetObjectIdL( aElement );
       
   663     UpnpCdUtils::SetContainerIdL( aElement, aContainerId );
       
   664     iMetadataStorage->InsertObjectIntoObjectTableL( aElement );
       
   665     ret = EUpnpOk;
       
   666     RXmlEngNodeList<TXmlEngElement> elements;
       
   667     CleanupClosePushL( elements );
       
   668     aElement.GetChildElements( elements );
       
   669 
       
   670     while ( elements.HasNext( ) )
       
   671         {
       
   672         TXmlEngElement el = elements.Next( );
       
   673         if ( el.Name().CompareF( KClassTagName ) && el.Name().CompareF( KTitleTagName ) )
       
   674             {
       
   675             ret = InsertElementL( el, *aId );
       
   676             }
       
   677         else
       
   678             {
       
   679             UpnpCdUtils::IsElementRequiredL( el ); // just remove the marker attr
       
   680             }
       
   681         }
       
   682     // set object childCount
       
   683     SetChildCountL( aElement, *aId );
       
   684 
       
   685     CleanupStack::PopAndDestroy( &elements );
       
   686     return ret;
       
   687     }
       
   688 // -----------------------------------------------------------------------------
       
   689 // CUpnpContentDirectoryDb::InsertAttributesL
       
   690 // (other items were commented in a header).
       
   691 // -----------------------------------------------------------------------------
       
   692 //
       
   693 TUpnpErrorCode CUpnpContentDirectoryDb::InsertAttributesL(
       
   694     const TXmlEngElement aElement, TInt aElmId, TInt aObjId )
       
   695     {
       
   696     TUpnpErrorCode ret = EUpnpUndefined;
       
   697     RXmlEngNodeList<TXmlEngAttr> list;
       
   698     CleanupClosePushL( list );
       
   699     aElement.GetAttributes( list );
       
   700     if ( list.Count( ) )
       
   701         {
       
   702         RArray<TXmlEngAttr> attributes; // to store real attr
       
   703         CleanupClosePushL( attributes );
       
   704         RArray<TXmlEngAttr> markers; // to store marker attr
       
   705         CleanupClosePushL( markers );
       
   706         // separate markers from attr
       
   707         while ( list.HasNext( ) )
       
   708             {
       
   709             TXmlEngAttr attr = list.Next( );
       
   710             if ( attr.Name().Match( KRequiredAtrSufPattern ) == KErrNotFound )
       
   711                 {
       
   712                 attributes.AppendL( attr );
       
   713                 }
       
   714             else
       
   715                 {
       
   716                 markers.AppendL( attr );
       
   717                 }
       
   718             }
       
   719 
       
   720         // insert attributes
       
   721         for ( TInt i = 0; i < attributes.Count( ); i++ )
       
   722             {
       
   723             iMetadataStorage->InsertAttributeL( attributes[i], aElmId,
       
   724                 IsAttrRequiredL( attributes[i], markers ), aObjId );
       
   725             ret = EUpnpOk;
       
   726             }
       
   727         // remove markers from DOM tree
       
   728         for ( TInt i = 0; i < markers.Count( ); i++ )
       
   729             {
       
   730             markers[i].Remove( );
       
   731             }
       
   732         // clean up
       
   733         CleanupStack::PopAndDestroy( &markers );
       
   734         CleanupStack::PopAndDestroy( &attributes );
       
   735         }
       
   736     else
       
   737         {
       
   738         ret = EUpnpOk;
       
   739         }
       
   740 
       
   741     CleanupStack::PopAndDestroy( &list );
       
   742     return ret;
       
   743     }
       
   744 // -----------------------------------------------------------------------------
       
   745 // CUpnpContentDirectoryDb::IsAttrRequiredL
       
   746 // (other items were commented in a header).
       
   747 // -----------------------------------------------------------------------------
       
   748 //
       
   749 TBool CUpnpContentDirectoryDb::IsAttrRequiredL( const TXmlEngAttr& aAttr,
       
   750     RArray<TXmlEngAttr>& aMarkers )
       
   751     {
       
   752     TBool ret = EFalse;
       
   753 
       
   754     for ( TInt i = 0; i< aMarkers.Count( ); i++ )
       
   755         {
       
   756         if ( aMarkers[i].Name().Find( aAttr.Name( ) ) == 0 )
       
   757             { // attr required
       
   758             ret = ETrue;
       
   759             break;
       
   760             }
       
   761         }
       
   762 
       
   763     return ret;
       
   764     }
       
   765 // -----------------------------------------------------------------------------
       
   766 // CUpnpContentDirectoryDb::SetObjectIdL
       
   767 // Sets the id val in the objected to be created
       
   768 // (other items were commented in a header).
       
   769 // -----------------------------------------------------------------------------
       
   770 //
       
   771 TInt CUpnpContentDirectoryDb::SetObjectIdL( TXmlEngElement& aElement )
       
   772     {
       
   773     TInt ret = iMetadataStorage->GetNextKeyL( KObjectTableName8 );
       
   774     UpnpCdUtils::SetObjectIdL( aElement, ret );
       
   775     return ret;
       
   776     }
       
   777 // -----------------------------------------------------------------------------
       
   778 // CUpnpContentDirectoryDb::GetObjectIdL
       
   779 // Gets the id val in the objected to be created
       
   780 // (other items were commented in a header).
       
   781 // -----------------------------------------------------------------------------
       
   782 //
       
   783 TInt CUpnpContentDirectoryDb::GetObjectIdL()
       
   784     {
       
   785     TInt ret = iMetadataStorage->NextKeyL( KObjectTableName8 );
       
   786 
       
   787     return ret;
       
   788     }
       
   789 // -----------------------------------------------------------------------------
       
   790 // CUpnpContentDirectoryDb::GetObjectIdL
       
   791 // Gets the id val in the objected to be created
       
   792 // (other items were commented in a header).
       
   793 // -----------------------------------------------------------------------------
       
   794 //
       
   795 HBufC8* CUpnpContentDirectoryDb::GetObjectTitleL( TInt aId )
       
   796     {
       
   797     return iMetadataStorage->GetObjectTitleL( aId );
       
   798     }
       
   799 
       
   800 // -----------------------------------------------------------------------------
       
   801 // CUpnpContentDirectoryDb::ReferedObjectIdL
       
   802 // (other items were commented in a header).
       
   803 // -----------------------------------------------------------------------------
       
   804 //
       
   805 TInt CUpnpContentDirectoryDb::ReferedObjectIdL( TInt aObjectId )
       
   806     {
       
   807     TInt ret = KErrNotFound;
       
   808 
       
   809     TInt refId = iMetadataStorage->ReferedObjectIdL( aObjectId );
       
   810 
       
   811     if ( refId != KErrNotFound )
       
   812         {
       
   813         ret = ReferedObjectIdL( refId );
       
   814         }
       
   815     else
       
   816         {
       
   817         ret = aObjectId;
       
   818         }
       
   819 
       
   820     return ret;
       
   821     }
       
   822 // -----------------------------------------------------------------------------
       
   823 // CUpnpContentDirectoryDb::GetElIdByAttrL
       
   824 // (other items were commented in a header).
       
   825 // -----------------------------------------------------------------------------
       
   826 //
       
   827 TInt CUpnpContentDirectoryDb::GetObjIdByAttrL( const TDesC8& aAttrName,
       
   828     const TDesC8& aAttrVal )
       
   829     {
       
   830     TInt objId = KErrNotFound;
       
   831 
       
   832     TInt elmId = iMetadataStorage->GetElmIdForAttrL( aAttrName, aAttrVal );
       
   833     if ( elmId != KErrNotFound )
       
   834         {
       
   835         objId = iMetadataStorage->GetObjIdForElmIdL( elmId );
       
   836         }
       
   837 
       
   838     return objId;
       
   839     }
       
   840 // -----------------------------------------------------------------------------
       
   841 // CUpnpContentDirectoryDb::GetObjIdByAttrLikeL
       
   842 // (other items were commented in a header).
       
   843 // -----------------------------------------------------------------------------
       
   844 //
       
   845 TInt CUpnpContentDirectoryDb::GetObjIdByAttrLikeL( const TDesC8& aAttrName,
       
   846     const TDesC8& aAttrVal )
       
   847     {
       
   848     TInt objId = KErrNotFound;
       
   849 
       
   850     TInt elmId = iMetadataStorage->GetElmIdForAttrLikeL( aAttrName, aAttrVal );
       
   851     if ( elmId != KErrNotFound )
       
   852         {
       
   853         objId = iMetadataStorage->GetObjIdForElmIdL( elmId );
       
   854         }
       
   855 
       
   856     return objId;
       
   857     }
       
   858 // -----------------------------------------------------------------------------
       
   859 // CUpnpContentDirectoryDb::GetObjectByAttrL
       
   860 // (other items were commented in a header).
       
   861 // -----------------------------------------------------------------------------
       
   862 //
       
   863 TUpnpErrorCode CUpnpContentDirectoryDb::GetObjectByAttrL(
       
   864     RXmlEngDocument& aDoc, TInt* aObjId, const TDesC8& aAttrName,
       
   865     const TDesC8& aAttrVal )
       
   866     {
       
   867     TUpnpErrorCode ret = EUndefined;
       
   868 
       
   869     *aObjId = GetObjIdByAttrL( aAttrName, aAttrVal );
       
   870     if ( *aObjId >= 0 )
       
   871         {
       
   872         ret = GetObjectL( *aObjId, aDoc, KAsterisk8 );
       
   873         }
       
   874     else
       
   875         { // something went wrong
       
   876         ret = ENoSuchObject;
       
   877         }
       
   878     return ret;
       
   879     }
       
   880 // -----------------------------------------------------------------------------
       
   881 // CUpnpContentDirectoryDb::CheckParentRestrictionL
       
   882 // (other items were commented in a header).
       
   883 // -----------------------------------------------------------------------------
       
   884 //
       
   885 TUpnpErrorCode CUpnpContentDirectoryDb::CheckParentRestrictionL( TInt aObjId )
       
   886     {
       
   887     return iMetadataStorage->CheckParentRestrictionL( aObjId );
       
   888     }
       
   889 // -----------------------------------------------------------------------------
       
   890 // CUpnpContentDirectoryDb::DeleteResourceL
       
   891 // (other items were commented in a header).
       
   892 // -----------------------------------------------------------------------------
       
   893 //
       
   894 void CUpnpContentDirectoryDb::DeleteResourceL( const TDesC8& aResVal,
       
   895     RArray<TInt>& aContIds )
       
   896     {
       
   897     // start transaction
       
   898     iMetadataStorage->BeginTransactionL( );
       
   899 
       
   900     TRAPD( err, DoDeleteResourceL( aResVal, aContIds ) );
       
   901     if ( err != KErrNone )
       
   902         { // rollback - error
       
   903         iMetadataStorage->RollbackTransactionL( );
       
   904         User::Leave( err );
       
   905         }
       
   906     // commit if success
       
   907     iMetadataStorage->CommitTransactionL( );
       
   908     }
       
   909 // -----------------------------------------------------------------------------
       
   910 // CUpnpContentDirectoryDb::DoDeleteResourceL
       
   911 // (other items were commented in a header).
       
   912 // -----------------------------------------------------------------------------
       
   913 //
       
   914 void CUpnpContentDirectoryDb::DoDeleteResourceL( const TDesC8& aResVal,
       
   915     RArray<TInt>& aContIds )
       
   916     {
       
   917     HBufC* resVal = HBufC::NewLC( aResVal.Length( ) );
       
   918     resVal->Des().Copy( aResVal );
       
   919 
       
   920     HBufC* resValReplaced16 = UpnpCdUtils::EscapeAposL( *resVal );
       
   921     CleanupStack::PushL( resValReplaced16 );
       
   922     HBufC8* resValReplaced8 = UpnpCdUtils::DesToDes8LC( *resValReplaced16 );
       
   923     RArray<TInt> objs;
       
   924     CleanupClosePushL( objs );
       
   925     GetObjectsByResourceL( objs, *resValReplaced16 );
       
   926 
       
   927     // not any obj?
       
   928     if ( !objs.Count( ) )
       
   929         {
       
   930         User::Leave( ENoSourceResource );
       
   931         }
       
   932 
       
   933     TUpnpErrorCode err = EUpnpOk;
       
   934 
       
   935     // for each object
       
   936     for ( TInt i = 0; i < objs.Count( ); i++ )
       
   937         {
       
   938         CUpnpObjectBean* objBn = GetObjBeanLC( objs[i] );
       
   939         if ( !objBn->ObjRestricted( ) )
       
   940             {
       
   941             if ( CheckObjectRestrictionL( objBn->ObjParentId( ) ) == EUpnpOk )
       
   942                 {
       
   943                 iMetadataStorage->DeleteResElL( *resValReplaced16,
       
   944                     objBn->ObjId( ) );
       
   945                 aContIds.AppendL( objBn->ObjParentId( ) );
       
   946                 }
       
   947             else
       
   948                 {
       
   949                 err = ERestrictedParentObject;
       
   950                 }
       
   951             }
       
   952         else
       
   953             {
       
   954             err = ERestrictedObject;
       
   955             }
       
   956         CleanupStack::PopAndDestroy( objBn );
       
   957         }
       
   958 
       
   959     // was any deleted
       
   960     if ( !aContIds.Count( ) )
       
   961         {
       
   962         User::Leave( err );
       
   963         }
       
   964 
       
   965     // if each deleted?
       
   966     if ( aContIds.Count( ) == objs.Count( ) )
       
   967         {
       
   968         // try to fetch resource id
       
   969         TRAPD( err, UpnpCdUtils::ResIdFromUriL( *resValReplaced8 ) );
       
   970         // ignore if error - resource id could not be fetched
       
   971         if ( !err )
       
   972             { // OK
       
   973             // delete resource
       
   974             DeleteResourceByResIdL( UpnpCdUtils::ResIdFromUriL( *resValReplaced8 ) );
       
   975             }
       
   976         }
       
   977 
       
   978     CleanupStack::PopAndDestroy( &objs );
       
   979     CleanupStack::PopAndDestroy( resValReplaced8 );
       
   980     CleanupStack::PopAndDestroy( resValReplaced16 );
       
   981     CleanupStack::PopAndDestroy( resVal );
       
   982     }
       
   983 // -----------------------------------------------------------------------------
       
   984 // CUpnpContentDirectoryDb::GetObjBeanLC
       
   985 // (other items were commented in a header).
       
   986 // -----------------------------------------------------------------------------
       
   987 //
       
   988 CUpnpObjectBean* CUpnpContentDirectoryDb::GetObjBeanLC( TInt aObjId )
       
   989     {
       
   990     CUpnpObjectBean* objBn = 0;
       
   991 
       
   992     RDbView view = iMetadataStorage->GetObjViewL( aObjId );
       
   993     CleanupClosePushL( view );
       
   994 
       
   995     if ( view.FirstL( ) )
       
   996         {
       
   997         // get values
       
   998         view.GetL( );
       
   999         objBn = CUpnpObjectBean::NewL( view );
       
  1000         }
       
  1001 
       
  1002     // clean up 
       
  1003     CleanupStack::PopAndDestroy( &view );
       
  1004 
       
  1005     CleanupStack::PushL( objBn );
       
  1006     return objBn;
       
  1007     }
       
  1008 // -----------------------------------------------------------------------------
       
  1009 // CUpnpContentDirectoryDb::GetObjectIdsL
       
  1010 // (other items were commented in a header).
       
  1011 // -----------------------------------------------------------------------------
       
  1012 //
       
  1013 void CUpnpContentDirectoryDb::GetObjectsByResourceL( RArray<TInt>& aIds,
       
  1014     const TDesC& aResVal )
       
  1015     {
       
  1016 
       
  1017     RDbView view = iMetadataStorage->GetViewOfObjectListForResL( aResVal );
       
  1018     CleanupClosePushL( view );
       
  1019 
       
  1020     CDbColSet* colSet = view.ColSetL( );
       
  1021     const TInt colNo = colSet->ColNo( KElmObjIdColName );
       
  1022     delete colSet;
       
  1023 
       
  1024     // for each row
       
  1025     while ( view.NextL( ) )
       
  1026         {
       
  1027         view.GetL( );
       
  1028         TInt no = view.ColInt( colNo );
       
  1029         aIds.AppendL( no );
       
  1030         }
       
  1031 
       
  1032     // clean up
       
  1033     CleanupStack::PopAndDestroy( &view );
       
  1034     }
       
  1035 // -----------------------------------------------------------------------------
       
  1036 // CUpnpContentDirectoryDb::GetProtocolInfoL
       
  1037 // (other items were commented in a header).
       
  1038 // -----------------------------------------------------------------------------
       
  1039 //
       
  1040 TInt CUpnpContentDirectoryDb::GetProtocolInfoL( const TDesC8& aContentUri,
       
  1041     CUpnpDlnaProtocolInfo*& aProtocolInfo )
       
  1042     {
       
  1043     HBufC* uri = HBufC::NewLC( aContentUri.Length( ) );
       
  1044     TPtr uriPtr(uri->Des( ));
       
  1045     uriPtr.Copy( aContentUri );
       
  1046     TInt ret = GetElmIdByNameAndValL( KRes16, uriPtr );
       
  1047     if ( ret >= 0 )
       
  1048         {
       
  1049         HBufC8* attrVal;
       
  1050         GetAttrValueByNameL( ret, KprotocolInfo, attrVal );
       
  1051         CleanupStack::PushL( attrVal );
       
  1052         aProtocolInfo = CUpnpDlnaProtocolInfo::NewL( *attrVal );
       
  1053         CleanupStack::PopAndDestroy( attrVal );
       
  1054         }
       
  1055     CleanupStack::PopAndDestroy( uri );
       
  1056 
       
  1057     if ( ret >= 0 ) //item ID number is't meaningfull for function caller
       
  1058         {
       
  1059         ret = KErrNone;
       
  1060         }
       
  1061     return ret;
       
  1062     }
       
  1063 // -----------------------------------------------------------------------------
       
  1064 // CUpnpContentDirectoryDb::GetElmIdByNameAndValL
       
  1065 // (other items were commented in a header).
       
  1066 // -----------------------------------------------------------------------------
       
  1067 //
       
  1068 TInt CUpnpContentDirectoryDb::GetElmIdByNameAndValL( const TDesC& aElmName,
       
  1069     const TDesC& aElmValue )
       
  1070     {
       
  1071     TInt ret = KErrNotFound;
       
  1072 
       
  1073     RDbView view = iMetadataStorage->GetViewOfElmIdByNameAndValL( aElmName,
       
  1074         aElmValue );
       
  1075     CleanupClosePushL( view );
       
  1076 
       
  1077     // col id
       
  1078     CDbColSet* colSet = view.ColSetL( );
       
  1079     const TInt colNo = colSet->ColNo( KElmIdColName );
       
  1080     delete colSet;
       
  1081 
       
  1082     while ( view.NextL( ) )
       
  1083         {
       
  1084         view.GetL( );
       
  1085         ret = view.ColInt( colNo );
       
  1086         }
       
  1087 
       
  1088     // clean up
       
  1089     CleanupStack::PopAndDestroy( &view );
       
  1090 
       
  1091     return ret;
       
  1092     }
       
  1093 // -----------------------------------------------------------------------------
       
  1094 // CUpnpContentDirectoryDb::GetAttrValueByNameL
       
  1095 // (other items were commented in a header).
       
  1096 // -----------------------------------------------------------------------------
       
  1097 //
       
  1098 
       
  1099 HBufC8* CUpnpContentDirectoryDb::GetProtocolInfoByImportUriL(
       
  1100     const TDesC8& aImportUri )
       
  1101     {
       
  1102     CUpnpAttributeBean* attBean =
       
  1103             GetAttrByValueL( KImportUri8( ), aImportUri );
       
  1104     CleanupStack::PushL( attBean );
       
  1105 
       
  1106     HBufC8* attValBuf = NULL;
       
  1107     if ( attBean )
       
  1108         {
       
  1109         //KprotocolInfo
       
  1110         CUpnpAttributeBean* att = GetAttrByNameL( attBean->AtrElmId( ),
       
  1111             KprotocolInfo( ), attValBuf );
       
  1112         delete att;
       
  1113         }
       
  1114     CleanupStack::PopAndDestroy( attBean );
       
  1115     return attValBuf;
       
  1116     }
       
  1117 
       
  1118 // -----------------------------------------------------------------------------
       
  1119 // CUpnpContentDirectoryDb::GetAttrValueByNameL
       
  1120 // (other items were commented in a header).
       
  1121 // -----------------------------------------------------------------------------
       
  1122 //
       
  1123 void CUpnpContentDirectoryDb::GetAttrValueByNameL( TInt aElmId,
       
  1124     const TDesC8& aAttrName, HBufC8*& aAttrVal )
       
  1125     {
       
  1126     CUpnpAttributeBean* atrBean =
       
  1127             GetAttrByNameL( aElmId, aAttrName, aAttrVal );
       
  1128     delete atrBean;
       
  1129     }
       
  1130 // -----------------------------------------------------------------------------
       
  1131 // CUpnpContentDirectoryDb::GetAttrByValueL
       
  1132 // (other items were commented in a header).
       
  1133 // -----------------------------------------------------------------------------
       
  1134 //
       
  1135 CUpnpAttributeBean* CUpnpContentDirectoryDb::GetAttrByValueL(
       
  1136     const TDesC8& aAttrName, const TDesC8& aAttrVal )
       
  1137     {
       
  1138     RDbView view = iMetadataStorage->GetAttrViewL( aAttrName, aAttrVal );
       
  1139     CleanupClosePushL( view );
       
  1140 
       
  1141     TInt number = view.CountL( );
       
  1142 
       
  1143     CDbColSet* colSet = view.ColSetL( );
       
  1144     const TInt colNo = colSet->ColNo( KAtrValueColName );
       
  1145     delete colSet;
       
  1146 
       
  1147     CUpnpAttributeBean* atrBean = NULL;
       
  1148     if ( view.FirstL( ) )
       
  1149         {
       
  1150         view.GetL( );
       
  1151         atrBean = CUpnpAttributeBean::NewLC( view );
       
  1152         CleanupStack::Pop( atrBean );
       
  1153         }
       
  1154     else
       
  1155         {
       
  1156         User::Leave( KErrNotFound );
       
  1157         }
       
  1158 
       
  1159     // clean up
       
  1160     CleanupStack::PopAndDestroy( &view );
       
  1161     return atrBean;
       
  1162     }
       
  1163 // -----------------------------------------------------------------------------
       
  1164 // CUpnpContentDirectoryDb::GetAttrByNameL
       
  1165 // (other items were commented in a header).
       
  1166 // -----------------------------------------------------------------------------
       
  1167 //
       
  1168 CUpnpAttributeBean* CUpnpContentDirectoryDb::GetAttrByNameL( TInt aElmId,
       
  1169     const TDesC8& aAttrName, HBufC8*& aAttrVal )
       
  1170     {
       
  1171     RDbView view = iMetadataStorage->GetAttrViewL( aElmId, aAttrName );
       
  1172     CleanupClosePushL( view );
       
  1173 
       
  1174     CDbColSet* colSet = view.ColSetL( );
       
  1175     const TInt colNo = colSet->ColNo( KAtrValueColName );
       
  1176     delete colSet;
       
  1177 
       
  1178     CUpnpAttributeBean* atrBean = NULL;
       
  1179     if ( view.FirstL( ) )
       
  1180         {
       
  1181         view.GetL( );
       
  1182         atrBean = CUpnpAttributeBean::NewLC( view );
       
  1183         aAttrVal = atrBean->AtrValue().AllocL( );
       
  1184         CleanupStack::Pop( atrBean );
       
  1185         }
       
  1186     else
       
  1187         {
       
  1188         User::Leave( KErrNotFound );
       
  1189         }
       
  1190 
       
  1191     // clean up
       
  1192     CleanupStack::PopAndDestroy( &view );
       
  1193     return atrBean;
       
  1194     }
       
  1195 
       
  1196 // -----------------------------------------------------------------------------
       
  1197 // CUpnpContentDirectoryDb::GetObjectIdByElementValueLikeL
       
  1198 // (other items were commented in a header).
       
  1199 // -----------------------------------------------------------------------------
       
  1200 //
       
  1201 TInt CUpnpContentDirectoryDb::GetObjectIdByElementValueLikeL(
       
  1202     const TDesC8& aElmName, const TDesC8& aElmValue )
       
  1203     {
       
  1204     // convert to unicode
       
  1205     HBufC* elmName = UpnpCdUtils::Des8ToDesLC( aElmName );
       
  1206     HBufC* elmValue = UpnpCdUtils::Des8ToDesLC( aElmValue );
       
  1207 
       
  1208     // prepare query buf
       
  1209     HBufC* query = HBufC::NewLC( KSelectFromSqlCmd().Length( ) + KElmObjIdColName().Length( ) + KElementTableName().Length( ) + KElmNameColName().Length( ) + elmName->Length( ) + KAndLikeConditionSqlCmd().Length( ) + KElmValueColName().Length( ) + elmValue->Length( ) );
       
  1210 
       
  1211     TPtr queryPtr(query->Des( ));
       
  1212 
       
  1213     // format query
       
  1214     queryPtr.Format( KSelectFromSqlCmd, &KElmObjIdColName,
       
  1215         &KElementTableName, &KElmNameColName, elmName );
       
  1216     queryPtr.AppendFormat( KAndLikeConditionSqlCmd, &KElmValueColName,
       
  1217         elmValue );
       
  1218 
       
  1219     // prepare
       
  1220     RDbView view = iMetadataStorage->GetEvaluatedViewL( queryPtr );
       
  1221     CleanupClosePushL( view );
       
  1222 
       
  1223     // col no
       
  1224     CDbColSet* colSet = view.ColSetL( );
       
  1225     const TInt colNo = colSet->ColNo( KElmObjIdColName );
       
  1226     delete colSet;
       
  1227 
       
  1228     TInt ret = KErrNotFound;
       
  1229 
       
  1230     // for one row
       
  1231     if ( view.NextL( ) )
       
  1232         {
       
  1233         view.GetL( );
       
  1234         if ( !view.IsColNull( colNo ) )
       
  1235             {
       
  1236             ret = view.ColInt( colNo );
       
  1237             }
       
  1238         }
       
  1239 
       
  1240     CleanupStack::PopAndDestroy( &view );
       
  1241     CleanupStack::PopAndDestroy( query );
       
  1242 
       
  1243     CleanupStack::PopAndDestroy( elmValue );
       
  1244     CleanupStack::PopAndDestroy( elmName );
       
  1245 
       
  1246     return ret;
       
  1247     }
       
  1248 
       
  1249 // -----------------------------------------------------------------------------
       
  1250 // CUpnpContentDirectoryDb::GetParentIdL
       
  1251 // (other items were commented in a header).
       
  1252 // -----------------------------------------------------------------------------
       
  1253 //
       
  1254 TInt CUpnpContentDirectoryDb::GetParentIdL( TInt aObjectId )
       
  1255     {
       
  1256     return iMetadataStorage->GetParentIdL( aObjectId );
       
  1257     }
       
  1258 
       
  1259 // -----------------------------------------------------------------------------
       
  1260 // CUpnpContentDirectoryDb::AddAttributeL
       
  1261 // (other items were commented in a header).
       
  1262 // -----------------------------------------------------------------------------
       
  1263 //
       
  1264 
       
  1265 void CUpnpContentDirectoryDb::AddAttributeL( TXmlEngElement aElement,
       
  1266     const TDesC8& aAttrName, TInt aElementId, TInt aObjectId )
       
  1267     {
       
  1268     RDbView view = iMetadataStorage->GetAttrViewByObjectIdL( aObjectId,
       
  1269         aAttrName );
       
  1270     CleanupClosePushL( view );
       
  1271 
       
  1272     CDbColSet* colSet = view.ColSetL( );
       
  1273     const TInt elmIdColNo = colSet->ColNo( KAtrElmIdColName );
       
  1274     delete colSet;
       
  1275 
       
  1276     // Iterate through attributes - usually there is only one
       
  1277     while ( view.NextL( ) )
       
  1278         {
       
  1279         view.GetL( );
       
  1280 
       
  1281         if ( view.ColInt( elmIdColNo ) == aElementId )
       
  1282             {
       
  1283             CUpnpAttributeBean* atr = CUpnpAttributeBean::NewLC( view );
       
  1284             atr->AttachAttrL( aElement );
       
  1285             CleanupStack::PopAndDestroy( atr );
       
  1286             }
       
  1287         }
       
  1288 
       
  1289     // clean up
       
  1290     CleanupStack::PopAndDestroy( &view );
       
  1291     }
       
  1292 // -----------------------------------------------------------------------------
       
  1293 // CContentDirectoryDb::CanBeNestedL
       
  1294 // (other items were commented in a header).
       
  1295 // -----------------------------------------------------------------------------
       
  1296 //
       
  1297 TBool CUpnpContentDirectoryDb::CanBeNestedL( TInt aObjId )
       
  1298     {
       
  1299     // get path
       
  1300     HBufC8* path = iMetadataStorage->GetObjectPathL( aObjId );
       
  1301     CleanupStack::PushL( path );
       
  1302 
       
  1303     // count levels
       
  1304     TPtrC8 rest(path->Des( ));
       
  1305     TInt levelNo(1); // root level
       
  1306     TInt pos( KErrNotFound );
       
  1307     while ( (pos = rest.Find( KPathSeparator8 )) != KErrNotFound )
       
  1308         {
       
  1309         rest.Set( rest.Mid( pos + KPathSeparator8().Length( ) ) );
       
  1310         levelNo++;
       
  1311         }
       
  1312     // clean up
       
  1313     CleanupStack::PopAndDestroy( path );
       
  1314     return (levelNo < KAvailableNestedLevels);
       
  1315     }
       
  1316 // -----------------------------------------------------------------------------
       
  1317 // CUpnpContentDirectoryDb::AddResourceL
       
  1318 // (other items were commented in a header).
       
  1319 // -----------------------------------------------------------------------------
       
  1320 //
       
  1321 void CUpnpContentDirectoryDb::AddResourceL( CUpnpResourcesBean* aResBean,
       
  1322     TInt aObjId )
       
  1323     {
       
  1324     iMetadataStorage->AddResourceL( aResBean, aObjId );
       
  1325     }
       
  1326 // -----------------------------------------------------------------------------
       
  1327 // CUpnpContentDirectoryDb::DeleteResourceL
       
  1328 // (other items were commented in a header).
       
  1329 // -----------------------------------------------------------------------------
       
  1330 //
       
  1331 void CUpnpContentDirectoryDb::DeleteResourceByResIdL( TInt64 aResId,
       
  1332     TBool aDeleteFile )
       
  1333     {
       
  1334     iMetadataStorage->DeleteResourceByResIdL( aResId, aDeleteFile );
       
  1335     }
       
  1336 // -----------------------------------------------------------------------------
       
  1337 // CUpnpContentDirectoryDb::DeleteResourceL
       
  1338 // (other items were commented in a header).
       
  1339 // -----------------------------------------------------------------------------
       
  1340 //
       
  1341 void CUpnpContentDirectoryDb::DeleteResourceByObjIdL( TInt aObjId,
       
  1342     TBool aDeleteFile )
       
  1343     {
       
  1344     iMetadataStorage->DeleteResourceByObjIdL( aObjId, aDeleteFile );
       
  1345     }
       
  1346 // -----------------------------------------------------------------------------
       
  1347 // CUpnpContentDirectoryDb::GetResourceL
       
  1348 // (other items were commented in a header).
       
  1349 // -----------------------------------------------------------------------------
       
  1350 //
       
  1351 CUpnpResourcesBean* CUpnpContentDirectoryDb::GetResourceL( TInt64 aResId )
       
  1352     {
       
  1353     return iMetadataStorage->GetResourceL( aResId );
       
  1354     }
       
  1355 // -----------------------------------------------------------------------------
       
  1356 // CUpnpContentDirectoryDb::ResNewDownloadDirectoryL
       
  1357 // (other items were commented in a header).
       
  1358 // -----------------------------------------------------------------------------
       
  1359 //
       
  1360 void CUpnpContentDirectoryDb::NewMediaDirectoryL( const TDesC& aOldDir )
       
  1361     {
       
  1362     //UPDATE resources SET rsc_readonly=1 WHERE KRscPathColName LIKE $aOldDir* AND KRscReadonlyColName=0
       
  1363     // prepare command
       
  1364     HBufC* query = HBufC::NewLC( KNewMediaDirSqlCmd().Length( ) + aOldDir.Length( ) );
       
  1365     TPtr SqlCmd(query->Des( ));
       
  1366     SqlCmd.Format( KNewMediaDirSqlCmd, &aOldDir );
       
  1367 
       
  1368     // execute
       
  1369     iMetadataStorage->ExecuteL( SqlCmd );
       
  1370 
       
  1371     CleanupStack::PopAndDestroy( query );
       
  1372     }
       
  1373 // -----------------------------------------------------------------------------
       
  1374 // CUpnpContentDirectoryDb::GetKeyForUriL
       
  1375 // (other items were commented in a header).
       
  1376 // -----------------------------------------------------------------------------
       
  1377 //
       
  1378 TInt CUpnpContentDirectoryDb::GetKeyForUriL()
       
  1379     {
       
  1380     return iMetadataStorage->GetNextKeyL( KImportUriId8 );
       
  1381     }
       
  1382 // -----------------------------------------------------------------------------
       
  1383 // CUpnpContentDirectoryDb::CheckDatabaseL
       
  1384 // -----------------------------------------------------------------------------
       
  1385 //
       
  1386 void CUpnpContentDirectoryDb::CheckDatabaseL()
       
  1387     {
       
  1388     iMetadataStorage->CheckDatabaseL( );
       
  1389     }
       
  1390 // -----------------------------------------------------------------------------
       
  1391 // CUpnpContentDirectoryDb::RecreateDatabaseFile
       
  1392 // -----------------------------------------------------------------------------
       
  1393 //
       
  1394 TInt CUpnpContentDirectoryDb::RecreateDatabaseFile()
       
  1395     {
       
  1396     TRAPD( err, iMetadataStorage->RecreateDatabaseFileL( ) );
       
  1397     return err;
       
  1398     }
       
  1399 
       
  1400 // -----------------------------------------------------------------------------
       
  1401 // CUpnpContentDirectoryDb::HandleDbError
       
  1402 // -----------------------------------------------------------------------------
       
  1403 //
       
  1404 TInt CUpnpContentDirectoryDb::HandleDbError( TInt aError )
       
  1405     {
       
  1406     return iMetadataStorage->HandleDbError( aError );
       
  1407     }
       
  1408 
       
  1409 // -----------------------------------------------------------------------------
       
  1410 // CUpnpContentDirectoryDb::IsDbCreated
       
  1411 // -----------------------------------------------------------------------------
       
  1412 //
       
  1413 TBool CUpnpContentDirectoryDb::IsDbCreated()
       
  1414     {
       
  1415     return iMetadataStorage->IsDbCreated( );
       
  1416     }
       
  1417 
       
  1418 //  End of File