upnpsharing/upnpsharingalgorithm/src/upnpcdsliteobjectarray.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
       
     1 /*
       
     2 * Copyright (c) 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: Implementation of CUpnpCdsLiteObjectArray
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES, SYSTEM
       
    19 #include <f32file.h>        // TParse
       
    20 #include <e32def.h>         // const_cast
       
    21 #include <escapeutils.h>    // ConvertFromUnicodeToUtf8L
       
    22 #include <upnpobject.h>
       
    23 #include <upnpcontainer.h>
       
    24 #include <upnpitem.h>
       
    25 #include <upnpelement.h>
       
    26 #include "upnpconstantdefs.h"
       
    27 
       
    28 // INCLUDE FILES, USER
       
    29 #include "upnpcdsliteobject.h"
       
    30 #include "upnpcdsliteobjectarray.h"
       
    31 #include "upnpsharingalgorithmconstants.h"
       
    32 #include "upnplog.h"
       
    33 
       
    34 // CONSTANTS
       
    35 _LIT8( KSpace, " ");
       
    36 
       
    37 // --------------------------------------------------------------------------
       
    38 // CUpnpCdsLiteObjectArray::NewL
       
    39 // --------------------------------------------------------------------------
       
    40 //
       
    41 EXPORT_C CUpnpCdsLiteObjectArray* CUpnpCdsLiteObjectArray::NewL()
       
    42     {
       
    43     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::NewL" );
       
    44 
       
    45     CUpnpCdsLiteObjectArray* self = new ( ELeave ) CUpnpCdsLiteObjectArray();
       
    46     CleanupStack::PushL( self );
       
    47     self->ConstructL();
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 // --------------------------------------------------------------------------
       
    53 // CUpnpCdsLiteObjectArray::~CUpnpCdsLiteObjectArray
       
    54 // --------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C CUpnpCdsLiteObjectArray::~CUpnpCdsLiteObjectArray()
       
    57     {
       
    58     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::\
       
    59 ~CUpnpCdsLiteObjectArray" );
       
    60 
       
    61     // Empty and close the array
       
    62     iArray.ResetAndDestroy();
       
    63     }
       
    64 
       
    65 // --------------------------------------------------------------------------
       
    66 // CUpnpCdsLiteObjectArray::AppendL
       
    67 // --------------------------------------------------------------------------
       
    68 //
       
    69 EXPORT_C void CUpnpCdsLiteObjectArray::AppendL( CUpnpCdsLiteObject *aObject )
       
    70     {
       
    71     // Validate the parameter
       
    72     if( !aObject ||
       
    73         aObject->Name() == KNullDesC8 ||
       
    74         aObject->ObjectId() == KNullDesC8 ||
       
    75         aObject->ParentId() == KNullDesC8 ||
       
    76         aObject->ObjectClass() == KNullDesC8 ||
       
    77         ( aObject->Type() != CUpnpCdsLiteObject::EContainer &&
       
    78           aObject->Type() != CUpnpCdsLiteObject::EItem &&
       
    79           aObject->Type() != CUpnpCdsLiteObject::EItemReference ) ||
       
    80         ( aObject->Type() == CUpnpCdsLiteObject::EItemReference &&
       
    81           aObject->OriginalItemIdL() == KNullDesC8 ) )
       
    82         {
       
    83         User::Leave( KErrArgument );
       
    84         }
       
    85 
       
    86     // Append the item into the array. Leave if there is a problem.
       
    87     TInt appendError = iArray.Append( aObject );
       
    88     if( appendError != KErrNone )
       
    89         {
       
    90         User::Leave( appendError );
       
    91         }
       
    92     }
       
    93 
       
    94 // --------------------------------------------------------------------------
       
    95 // CUpnpCdsLiteObjectArray::FindByName
       
    96 // --------------------------------------------------------------------------
       
    97 //
       
    98 EXPORT_C TInt CUpnpCdsLiteObjectArray::FindByName( TDesC8& aName )
       
    99     {
       
   100     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::FindByName" );
       
   101     TInt returnvalue = KErrNotFound;
       
   102 
       
   103     // Continue only if the given parameter is valid
       
   104     if( aName != KNullDesC8 )
       
   105         {
       
   106         // Go through the array
       
   107         for( TInt index=0; index<iArray.Count(); index++ )
       
   108             {
       
   109             // if a match is found, exit the loop
       
   110             if( aName.Match( iArray[index]->Name() ) >= 0 )
       
   111                 {
       
   112                 returnvalue = index;
       
   113                 index = iArray.Count() + 1;
       
   114                 }
       
   115             }
       
   116         }
       
   117     return returnvalue;
       
   118     }
       
   119 
       
   120 // --------------------------------------------------------------------------
       
   121 // CUpnpCdsLiteObjectArray::FindByNameAndParentId
       
   122 // --------------------------------------------------------------------------
       
   123 //
       
   124 EXPORT_C TInt CUpnpCdsLiteObjectArray::FindByNameAndParentId(
       
   125                                                    const TDesC8& aName,
       
   126                                                    const TDesC8& aParentId )
       
   127     {
       
   128     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::\
       
   129 FindByNameAndParentId" );
       
   130 
       
   131     TInt returnvalue = KErrNotFound;
       
   132 
       
   133     // Continue only if the given parameter is valid
       
   134     if( aName != KNullDesC8 && aParentId != KNullDesC8 )
       
   135         {
       
   136         // Go through the array
       
   137         for( TInt index=0; index<iArray.Count(); index++ )
       
   138             {
       
   139             // if a match is found, exit the loop
       
   140             if( aName.Match( iArray[index]->Name() ) >= 0 &&
       
   141                 aParentId.Match( iArray[index]->ParentId() ) >= 0 )
       
   142                 {
       
   143                 returnvalue = index;
       
   144                 index = iArray.Count() + 1;
       
   145                 }
       
   146             }
       
   147         }
       
   148     return returnvalue;
       
   149     }
       
   150 
       
   151 // --------------------------------------------------------------------------
       
   152 // CUpnpCdsLiteObjectArray::FindByMediaClassAndParentId
       
   153 // --------------------------------------------------------------------------
       
   154 //
       
   155 EXPORT_C TInt CUpnpCdsLiteObjectArray::FindByMediaClassAndParentId(
       
   156                                                   const TDesC8& aMediaClass,
       
   157                                                   const TDesC8& aParentId )
       
   158     {
       
   159     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::\
       
   160 FindByMediaClassAndParentId" );
       
   161 
       
   162     TInt returnvalue = KErrNotFound;
       
   163 
       
   164     // Continue only if the given parameter is valid
       
   165     if( aMediaClass != KNullDesC8 &&
       
   166         aParentId != KNullDesC8 )
       
   167         {
       
   168         // Go through the array
       
   169         for( TInt index=0; index<iArray.Count(); index++ )
       
   170             {
       
   171             // if a match is found, exit the loop
       
   172             if( aMediaClass.Match( iArray[index]->ObjectClass() ) >= 0 &&
       
   173                 aParentId.Match( iArray[index]->ParentId() ) >= 0 )
       
   174                 {
       
   175                 returnvalue = index;
       
   176                 index = iArray.Count() + 1;
       
   177                 }
       
   178             }
       
   179         }
       
   180     return returnvalue;
       
   181     }
       
   182 
       
   183 // --------------------------------------------------------------------------
       
   184 // CUpnpCdsLiteObjectArray::FindByObjectId
       
   185 // --------------------------------------------------------------------------
       
   186 //
       
   187 EXPORT_C TInt CUpnpCdsLiteObjectArray::FindByObjectId(
       
   188                                                  const TDesC8& aObjectId )
       
   189     {
       
   190     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::\
       
   191 FindByObjectId" );
       
   192 
       
   193     TInt returnvalue = KErrNotFound;
       
   194 
       
   195     // Continue only if the given parameter is valid
       
   196     if( aObjectId != KNullDesC8 )
       
   197         {
       
   198         // Go through the array
       
   199         for( TInt index=0; index<iArray.Count(); index++ )
       
   200             {
       
   201             // if a match is found, exit the loop
       
   202             if( aObjectId.Match( iArray[index]->ObjectId() ) >= 0 )
       
   203                 {
       
   204                 returnvalue = index;
       
   205                 index = iArray.Count() + 1;
       
   206                 }
       
   207             }
       
   208         }
       
   209     return returnvalue;
       
   210     }
       
   211 
       
   212 // --------------------------------------------------------------------------
       
   213 // CUpnpCdsLiteObjectArray::FindRefItemIdByOriginalIdL
       
   214 // --------------------------------------------------------------------------
       
   215 //
       
   216 EXPORT_C TInt CUpnpCdsLiteObjectArray::FindRefItemIdByOriginalIdL(
       
   217                                                  const TDesC8& aOriginalId )
       
   218     {
       
   219     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::\
       
   220 FindRefItemIdByOriginalIdL" );
       
   221 
       
   222     TInt returnvalue = KErrNotFound;
       
   223 
       
   224     // Continue only if the given parameter is valid
       
   225     if( aOriginalId != KNullDesC8 )
       
   226         {
       
   227         // Go through the array
       
   228         for( TInt index=0; index<iArray.Count(); index++ )
       
   229             {
       
   230             // if a match is found, exit the loop
       
   231             if( iArray[index]->Type() ==
       
   232                     CUpnpCdsLiteObject::EItemReference &&
       
   233                 aOriginalId.Match( iArray[index]->OriginalItemIdL() ) >= 0 )
       
   234                 {
       
   235                 returnvalue = index;
       
   236                 index = iArray.Count() + 1;
       
   237                 }
       
   238             }
       
   239         }
       
   240     return returnvalue;
       
   241     }
       
   242 
       
   243 // --------------------------------------------------------------------------
       
   244 // CUpnpCdsLiteObjectArray::ChildCount
       
   245 // --------------------------------------------------------------------------
       
   246 //
       
   247 EXPORT_C TInt CUpnpCdsLiteObjectArray::ChildCount( const TDesC8& aObjectId )
       
   248     {
       
   249     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::ChildCount" );
       
   250 
       
   251     TInt returnvalue ( 0 );
       
   252 
       
   253     // Continue only if the given parameter is valid
       
   254     if( aObjectId != KNullDesC8 )
       
   255         {
       
   256         // Go through the array
       
   257         for( TInt index=0; index<iArray.Count(); index++ )
       
   258             {
       
   259             // if a match is found, increase child count
       
   260             if( aObjectId.Match( iArray[index]->ParentId() ) >= 0 )
       
   261                 {
       
   262                 returnvalue++;
       
   263                 }
       
   264             }
       
   265         }
       
   266     return returnvalue;
       
   267     }
       
   268 
       
   269 // --------------------------------------------------------------------------
       
   270 // CUpnpCdsLiteObjectArray::Count
       
   271 // --------------------------------------------------------------------------
       
   272 //
       
   273 EXPORT_C TInt CUpnpCdsLiteObjectArray::Count( )
       
   274     {
       
   275     return iArray.Count();
       
   276     }
       
   277 
       
   278 // --------------------------------------------------------------------------
       
   279 // CUpnpCdsLiteObjectArray::ObjectAtL
       
   280 // --------------------------------------------------------------------------
       
   281 //
       
   282 EXPORT_C CUpnpCdsLiteObject& CUpnpCdsLiteObjectArray::ObjectAtL(
       
   283                                                            TUint aIndex )
       
   284     {
       
   285     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::ObjectAtL" );
       
   286 
       
   287     // Validate the parameter
       
   288     if( aIndex > iArray.Count() )
       
   289         {
       
   290         User::Leave( KErrArgument );
       
   291         }
       
   292 
       
   293     // Return a reference to the object
       
   294     CUpnpCdsLiteObject* object = iArray[aIndex];
       
   295     return *object;
       
   296     }
       
   297 
       
   298 // --------------------------------------------------------------------------
       
   299 // CUpnpCdsLiteObjectArray::RemoveObjectAtL
       
   300 // --------------------------------------------------------------------------
       
   301 //
       
   302 EXPORT_C void CUpnpCdsLiteObjectArray::RemoveObjectAtL( TUint aIndex )
       
   303     {
       
   304     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::\
       
   305 RemoveObjectAtL" );
       
   306 
       
   307     // Validate the parameter
       
   308     if( aIndex > iArray.Count() )
       
   309         {
       
   310         User::Leave( KErrArgument );
       
   311         }
       
   312 
       
   313     // Get the pointer from the array
       
   314     CUpnpCdsLiteObject* object = iArray[aIndex];
       
   315 
       
   316     // Remove the object and compress the array
       
   317     iArray.Remove( aIndex );
       
   318     iArray.Compress();
       
   319 
       
   320     // Delete the object
       
   321     delete object;
       
   322     }
       
   323 
       
   324 // --------------------------------------------------------------------------
       
   325 // CUpnpCdsLiteObjectArray::AppendL
       
   326 // --------------------------------------------------------------------------
       
   327 //
       
   328 EXPORT_C void CUpnpCdsLiteObjectArray::AppendL( const CUpnpObject *aObject )
       
   329     {
       
   330     // First, create a new CreateUpnpCdsLiteObject
       
   331     CUpnpCdsLiteObject* object = CreateUpnpCdsLiteObjectL( aObject );
       
   332     CleanupStack::PushL( object );
       
   333 
       
   334     // Append the item to the array
       
   335     AppendL( object );
       
   336     CleanupStack::Pop( object ); // ownership transfered
       
   337     }
       
   338 
       
   339 // --------------------------------------------------------------------------
       
   340 // CUpnpCdsLiteObjectArray::RemoveL
       
   341 // --------------------------------------------------------------------------
       
   342 //
       
   343 EXPORT_C void CUpnpCdsLiteObjectArray::RemoveL( const CUpnpObject *aObject )
       
   344     {
       
   345     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::RemoveL" );
       
   346 
       
   347     // First, create a new CreateUpnpCdsLiteObject
       
   348     CUpnpCdsLiteObject* object = CreateUpnpCdsLiteObjectL( aObject );
       
   349     CleanupStack::PushL( object );
       
   350 
       
   351     // Search the array and remove the object if found
       
   352     TInt index = FindByObjectId( object->ObjectId() );
       
   353     if( index >= 0 )
       
   354         {
       
   355         RemoveObjectAtL( index );
       
   356         }
       
   357 
       
   358     // Clean up
       
   359     CleanupStack::PopAndDestroy( object );
       
   360     }
       
   361 
       
   362 // --------------------------------------------------------------------------
       
   363 // CUpnpCdsLiteObjectArray::CreateUpnpCdsLiteObjectL
       
   364 // --------------------------------------------------------------------------
       
   365 //
       
   366 CUpnpCdsLiteObject* CUpnpCdsLiteObjectArray::CreateUpnpCdsLiteObjectL(
       
   367                                                 const CUpnpObject *aObject )
       
   368     {
       
   369     // Check the param
       
   370     if( !aObject )
       
   371         {
       
   372         User::Leave( KErrArgument );
       
   373         }
       
   374 
       
   375     CUpnpCdsLiteObject* object = NULL;
       
   376 
       
   377     // Object is a container
       
   378     if( aObject->ObjectClass().Find( KContainerClass() ) >= 0 )
       
   379         {
       
   380         // Cast to container object
       
   381         CUpnpContainer* container = (CUpnpContainer*)aObject;
       
   382 
       
   383         // Create the object accordingly
       
   384         object = CUpnpCdsLiteObject::NewL( container->Title(),
       
   385                                            CUpnpCdsLiteObject::EContainer,
       
   386                                            container->Id(),
       
   387                                            container->ParentId(),
       
   388                                            container->ObjectClass() );
       
   389         CleanupStack::PushL( object );
       
   390         }
       
   391 
       
   392     // Object is an item / reference item
       
   393     else if( aObject->ObjectClass().Find( KItemClass() ) >= 0 )
       
   394         {
       
   395         // Cast to container object
       
   396         CUpnpItem* item = (CUpnpItem*)aObject;
       
   397 
       
   398         HBufC8* fileName = NULL;
       
   399         // Resolve the type
       
   400         CUpnpCdsLiteObject::TCdsLiteObjectType type =
       
   401                                     CUpnpCdsLiteObject::EUnknown;
       
   402         if( item->RefId() == KNullDesC8 )
       
   403             {
       
   404             type = CUpnpCdsLiteObject::EItem;
       
   405             fileName = ResolveFileNameL( aObject );
       
   406             CleanupStack::PushL( fileName );
       
   407             }
       
   408         else
       
   409             {
       
   410             type = CUpnpCdsLiteObject::EItemReference;
       
   411             // Set space for reference filename.
       
   412             // Item validation requires that CDS lite object's name field must
       
   413             // be different that KNullDesC8.
       
   414             // Mediaserver doesn't store this field for reference item and it
       
   415             // is KNullDesC8 when structrure is read from server.
       
   416             fileName = KSpace().AllocLC();
       
   417             }
       
   418 
       
   419         // Create the object accordingly
       
   420         object = CUpnpCdsLiteObject::NewL( *fileName,
       
   421                                            type,
       
   422                                            item->Id(),
       
   423                                            item->ParentId(),
       
   424                                            item->ObjectClass() );
       
   425 
       
   426         if ( fileName )
       
   427             {
       
   428             CleanupStack::PopAndDestroy( fileName );    
       
   429             }
       
   430 
       
   431         CleanupStack::PushL( object );
       
   432 
       
   433         // Set the reference ID, if necessary
       
   434         if( type == CUpnpCdsLiteObject::EItemReference )
       
   435             {
       
   436             object->SetOriginalItemIdL( item->RefId() );
       
   437             }
       
   438         }
       
   439 
       
   440     // Object is neither a container nor an item, leave
       
   441     else
       
   442         {
       
   443         User::Leave( KErrArgument );
       
   444         }
       
   445 
       
   446     CleanupStack::Pop( object );
       
   447 
       
   448     return object;
       
   449     }
       
   450 
       
   451 // --------------------------------------------------------------------------
       
   452 // CUpnpCdsLiteObjectArray::ResolveFileNameL
       
   453 // --------------------------------------------------------------------------
       
   454 //
       
   455 HBufC8* CUpnpCdsLiteObjectArray::ResolveFileNameL(
       
   456                                         const CUpnpObject *aObject )
       
   457     {
       
   458     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpCdsLiteObjectArray::\
       
   459 ResolveFileNameL" );
       
   460 
       
   461     // Check parameter
       
   462     if( !aObject )
       
   463         {
       
   464         User::Leave( KErrArgument );
       
   465         }
       
   466 
       
   467     HBufC8* returnValue = NULL;
       
   468 
       
   469     // Get all the elements
       
   470     const RUPnPElementsArray& elements =
       
   471                     ( const_cast<CUpnpObject*>(aObject) )->GetElements();
       
   472 
       
   473     // Find the "res" element
       
   474     for( TInt index=0; index<elements.Count(); index++ )
       
   475         {
       
   476         if( elements[index]->Name() == KElementRes )
       
   477             {
       
   478             // Parse the filename
       
   479             TParse fp;
       
   480             fp.Set( elements[index]->FilePath(), 0, 0 );
       
   481 
       
   482             // Full filename (path and drive letter included)
       
   483             returnValue =
       
   484                 EscapeUtils::ConvertFromUnicodeToUtf8L( fp.FullName() );
       
   485 
       
   486             // Exit the loop
       
   487             index = elements.Count() + 1;
       
   488             }
       
   489         }
       
   490 
       
   491     return returnValue;
       
   492     }
       
   493 
       
   494 // End of file