upnpsharing/upnpsharingalgorithm/src/upnpsharingalgorithmimpl.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 the class CUpnpSharingAlgorithmImpl.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES, SYSTEM
       
    19 #include <upnpdlnaprofiler.h>
       
    20 #include <upnpitem.h>
       
    21 #include "upnpcommonutils.h"
       
    22 #include "upnpmetadatafetcher.h"
       
    23 #include "upnpfileutility.h"
       
    24 #include <escapeutils.h>            // ConvertFromUnicodeToUtf8L
       
    25 #include <bautils.h>                // File existence check
       
    26 #include "upnpconstantdefs.h"
       
    27 #include <mm/mmcleanup.h>           // for array cleanup
       
    28 
       
    29 // INCLUDE FILES, USER
       
    30 #include "upnpcontainerresolver.h"
       
    31 #include "upnpsharingao.h"
       
    32 #include "upnpcdsreaderao.h"
       
    33 #include "upnpsharingalgorithmimpl.h"
       
    34 #include "upnpsharingalgorithmconstants.h"
       
    35 #include "upnplog.h"
       
    36 #include "upnpcdsliteobject.h"
       
    37 #include "upnpcdsliteobjectarray.h"
       
    38 
       
    39 // --------------------------------------------------------------------------
       
    40 // CUpnpSharingAlgorithm::NewL
       
    41 // --------------------------------------------------------------------------
       
    42 //
       
    43 CUpnpSharingAlgorithmImpl* CUpnpSharingAlgorithmImpl::NewL()
       
    44     {
       
    45     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithmImpl::NewL" );
       
    46 
       
    47     CUpnpSharingAlgorithmImpl* self = CUpnpSharingAlgorithmImpl::NewLC();
       
    48     CleanupStack::Pop( self );
       
    49 
       
    50     return self;
       
    51     }
       
    52 
       
    53 // --------------------------------------------------------------------------
       
    54 // CUpnpSharingAlgorithmImpl::NewLC
       
    55 // Two-phased constructor
       
    56 // --------------------------------------------------------------------------
       
    57 //
       
    58 CUpnpSharingAlgorithmImpl* CUpnpSharingAlgorithmImpl::NewLC()
       
    59     {
       
    60     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithmImpl::NewLC" );
       
    61 
       
    62     CUpnpSharingAlgorithmImpl* self =
       
    63         new ( ELeave ) CUpnpSharingAlgorithmImpl();
       
    64     CleanupStack::PushL( self );
       
    65     self->ConstructL();
       
    66 
       
    67     return self;
       
    68     }
       
    69 
       
    70 // --------------------------------------------------------------------------
       
    71 // CUpnpSharingAlgorithmImpl::~CUpnpSharingAlgorithmImpl
       
    72 // --------------------------------------------------------------------------
       
    73 //
       
    74 CUpnpSharingAlgorithmImpl::~CUpnpSharingAlgorithmImpl()
       
    75     {
       
    76     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithmImpl::\
       
    77 ~CUpnpSharingAlgorithmImpl" );
       
    78 
       
    79     delete iContainerResolver;
       
    80     delete iSharingAO;
       
    81     delete iSharedContentArray;
       
    82 
       
    83     iMediaServer.Close();
       
    84     }
       
    85 
       
    86 // --------------------------------------------------------------------------
       
    87 // CCUpnpSharingAlgorithmImpl::ConstructL
       
    88 // --------------------------------------------------------------------------
       
    89 //
       
    90 void CUpnpSharingAlgorithmImpl::ConstructL()
       
    91     {
       
    92     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithmImpl::\
       
    93 ConstructL" );
       
    94 
       
    95     // connect to server
       
    96     User::LeaveIfError( iMediaServer.Connect() );
       
    97     // Start server if not yet started
       
    98     StartServer();
       
    99 
       
   100     // An array to list all the shared files on the CDS
       
   101     iSharedContentArray = CUpnpCdsLiteObjectArray::NewL();
       
   102     // container structure resolver
       
   103     // contentarray is always up-to-date
       
   104     iContainerResolver =
       
   105         CUpnpContainerResolver::NewL( *iSharedContentArray );
       
   106 
       
   107     CUpnpCdsReaderAO* cdsReader =
       
   108         CUpnpCdsReaderAO::NewL( *iSharedContentArray );
       
   109     CleanupStack::PushL( cdsReader );
       
   110     cdsReader->ReadCdsStructureL();
       
   111     CleanupStack::PopAndDestroy( cdsReader );
       
   112 
       
   113     // Active object for sharing and unsharing the items
       
   114     iSharingAO = CUpnpSharingAO::NewL();
       
   115     }
       
   116 
       
   117 // --------------------------------------------------------------------------
       
   118 // CUpnpSharingAlgorithmImpl::ShareFileL
       
   119 // --------------------------------------------------------------------------
       
   120 //
       
   121 void CUpnpSharingAlgorithmImpl::ShareFileL(
       
   122                                         const TFileName & aFileName )
       
   123     {
       
   124     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   125     ShareFileL" );
       
   126 
       
   127     // Check that file exists. Leaves with KErrArgument if not exist.
       
   128     CheckFileExistenceL( aFileName );
       
   129     
       
   130     // Check that file is not protected. Leaves with KAccessDenied if yes.
       
   131     CheckFileProtectionL( aFileName );
       
   132 
       
   133     if ( !IsFileSharedL( aFileName ) )
       
   134         {
       
   135         // Start server if not yet started
       
   136         StartServer();
       
   137 
       
   138         // create upnpitem...
       
   139         CUpnpItem* upnpItem =
       
   140             UPnPMetadataFetcher::CreateItemFromFileLC( aFileName );
       
   141 
       
   142         // Resolve container structure
       
   143         RPointerArray<CUpnpContainer> containerArray =
       
   144                  iContainerResolver->ResolveContainerStructureL( *upnpItem );
       
   145         CleanupResetAndDestroyPushL( containerArray );
       
   146 
       
   147         if ( containerArray.Count() )
       
   148             {
       
   149             // Share containers
       
   150             ShareContainersL( containerArray );
       
   151 
       
   152             // parent container id for item is the last shared container id
       
   153             TPtrC8 itemParentContainerId =
       
   154                     containerArray[ containerArray.Count()-1 ]->Id();
       
   155             // ... and share it
       
   156             iSharingAO->ShareFileL( itemParentContainerId, upnpItem );
       
   157 
       
   158             // Add the file to the shared content array
       
   159             iSharedContentArray->AppendL( upnpItem );
       
   160             }
       
   161         else
       
   162             {
       
   163             // All containers exists already.
       
   164             // Resolver put parentid to item
       
   165             iSharingAO->ShareFileL( upnpItem->ParentId(), upnpItem );
       
   166 
       
   167             // Add the file to the shared content array
       
   168             iSharedContentArray->AppendL( upnpItem );
       
   169             }
       
   170 
       
   171         // SHARE reference containers and items
       
   172         ShareReferenceObjectsL( upnpItem );
       
   173 
       
   174         // Cleanup
       
   175         CleanupStack::PopAndDestroy( &containerArray );
       
   176         CleanupStack::PopAndDestroy( upnpItem );
       
   177         }
       
   178     }
       
   179 
       
   180 // --------------------------------------------------------------------------
       
   181 // CUpnpSharingAlgorithmImpl::UnshareFileL
       
   182 // --------------------------------------------------------------------------
       
   183 //
       
   184 void CUpnpSharingAlgorithmImpl::UnshareFileL(
       
   185                                         const TFileName& aFileName )
       
   186     {
       
   187     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::UnshareFileL" );
       
   188 
       
   189     // Convert the filename to 8bit
       
   190     HBufC8* filename = EscapeUtils::ConvertFromUnicodeToUtf8L( aFileName );
       
   191     CleanupStack::PushL( filename );
       
   192 
       
   193     // Search the array for the item
       
   194     TInt arrayindex = iSharedContentArray->FindByName( *filename );
       
   195 
       
   196     // Continue only if the file was found
       
   197     if ( arrayindex >= 0 )
       
   198         {
       
   199         // Start server if not yet started
       
   200         StartServer();
       
   201 
       
   202         // Get the object reference
       
   203         CUpnpCdsLiteObject& object =
       
   204             iSharedContentArray->ObjectAtL( arrayindex );
       
   205 
       
   206         // unshare references first
       
   207         UnshareReferenceObjectsL( object );
       
   208 
       
   209         // Checks the container tree and unshares if needed
       
   210         // First container id to be checked is item's parentid
       
   211         TBool containerUnshared =
       
   212                     UnshareEmptyContainersL( object.ParentId() );
       
   213 
       
   214         // if container is not unshared the item needs to be unshared
       
   215         if ( !containerUnshared )
       
   216             {
       
   217             // Leave if ObjectId not found
       
   218             if( object.ObjectId() == KNullDesC8 )
       
   219                 {
       
   220                 __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   221                 UnshareFileL, ObjectId not found!" );
       
   222                 User::Leave( KErrCorrupt );
       
   223                 }
       
   224 
       
   225             TInt objectIdInt( KErrNotFound );
       
   226             User::LeaveIfError(
       
   227              objectIdInt = UPnPCommonUtils::DesC8ToInt( object.ObjectId() ));
       
   228 
       
   229             // Unshare the item
       
   230             iSharingAO->UnshareFileL( objectIdInt );
       
   231 
       
   232             // index must be retrieved again because reference item removal
       
   233             // changes indexes
       
   234             arrayindex =
       
   235                 iSharedContentArray->FindByObjectId(object.ObjectId());
       
   236             // Remove the file from the shared content array
       
   237             iSharedContentArray->RemoveObjectAtL( arrayindex );
       
   238             }
       
   239         else
       
   240             {
       
   241             // index must be retrieved again because removal of containers
       
   242             // change indexes
       
   243             arrayindex = iSharedContentArray->FindByName( *filename );
       
   244             // item was also unshared when container was unshared
       
   245             // it's enough to remove item from contentarray
       
   246             iSharedContentArray->RemoveObjectAtL( arrayindex );
       
   247             }
       
   248         }
       
   249     // Cleanup
       
   250     CleanupStack::PopAndDestroy( filename );
       
   251     }
       
   252 
       
   253 // --------------------------------------------------------------------------
       
   254 // CUpnpSharingAlgorithmImpl::IsFileSharedL
       
   255 // --------------------------------------------------------------------------
       
   256 //
       
   257 TBool CUpnpSharingAlgorithmImpl::IsFileSharedL(
       
   258                                             const TFileName& aFileName )
       
   259     {
       
   260     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithmImpl::\
       
   261     IsFileSharedL" );
       
   262 
       
   263     TBool returnValue = EFalse;
       
   264 
       
   265     // Convert the filename to 8bit
       
   266     HBufC8* tempFilename =
       
   267         EscapeUtils::ConvertFromUnicodeToUtf8L( aFileName );
       
   268     CleanupStack::PushL( tempFilename );
       
   269 
       
   270     // Search the file from the array
       
   271     TInt index = iSharedContentArray->FindByName( *tempFilename );
       
   272     if( index >= 0 )
       
   273         {
       
   274         returnValue = ETrue;
       
   275         }
       
   276 
       
   277     // Clean up
       
   278     CleanupStack::PopAndDestroy( tempFilename );
       
   279 
       
   280     return returnValue;
       
   281     }
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // CUpnpSharingAlgorithmImpl::CheckFileExistenceL
       
   285 // --------------------------------------------------------------------------
       
   286 //
       
   287 void CUpnpSharingAlgorithmImpl::CheckFileExistenceL(
       
   288                                     const TFileName& aFileName )
       
   289     {
       
   290     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithmImpl::\
       
   291 CheckFileExistenceL" );
       
   292 
       
   293     TBool fileExists( EFalse );
       
   294 
       
   295     RFs fsSession;
       
   296     User::LeaveIfError( fsSession.Connect() ); // connect session
       
   297     CleanupClosePushL( fsSession );
       
   298 
       
   299     // check that file exists
       
   300     fileExists = BaflUtils::FileExists( fsSession, aFileName );
       
   301 
       
   302     CleanupStack::PopAndDestroy( &fsSession );
       
   303 
       
   304     if ( !fileExists )
       
   305         {
       
   306         User::Leave( KErrArgument );
       
   307         }
       
   308     }
       
   309 
       
   310 // --------------------------------------------------------------------------
       
   311 // CUpnpSharingAlgorithmImpl::UnshareEmptyContainersL
       
   312 // --------------------------------------------------------------------------
       
   313 //
       
   314 TBool CUpnpSharingAlgorithmImpl::UnshareEmptyContainersL( TDesC8& aParentId )
       
   315     {
       
   316     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithmImpl::\
       
   317 UnshareEmptyContainersL" );
       
   318 
       
   319     TBool containerUnshared( EFalse );
       
   320 
       
   321     // get top container id that does not contain item
       
   322     // starting from container where item is.
       
   323     RPointerArray<CUpnpCdsLiteObject> containerList =
       
   324         iContainerResolver->ResolveEmptyContainersL( aParentId );
       
   325     CleanupClosePushL( containerList );
       
   326 
       
   327     TInt containerCount = containerList.Count();
       
   328     if ( containerCount )
       
   329         {
       
   330         // Get top containerId which needs to be unshared.
       
   331         TPtrC8 containerIdStr = containerList[containerCount-1]->ObjectId();
       
   332         TInt objectIdInt( KErrNotFound );
       
   333         User::LeaveIfError(
       
   334               objectIdInt = UPnPCommonUtils::DesC8ToInt( containerIdStr ));
       
   335         // unshares all objecs under this container with single call
       
   336         TRAPD( error, iSharingAO->UnshareContainerL( objectIdInt ));
       
   337         if ( !error )
       
   338             {
       
   339             for ( TInt i = 0; i < containerCount; i++ )
       
   340                 {
       
   341                 // get container's array index
       
   342                 TInt arrayIndex =
       
   343                     iSharedContentArray->FindByObjectId(
       
   344                                               containerList[i]->ObjectId() );
       
   345 
       
   346                 // Remove the container from the shared content array
       
   347                 iSharedContentArray->RemoveObjectAtL( arrayIndex );
       
   348                 }
       
   349             }
       
   350         containerUnshared = ETrue;
       
   351         }
       
   352 
       
   353     CleanupStack::PopAndDestroy( &containerList );
       
   354 
       
   355     return containerUnshared;
       
   356     }
       
   357 
       
   358 // --------------------------------------------------------------------------
       
   359 // CUpnpSharingAlgorithmImpl::ShareContainersL
       
   360 // --------------------------------------------------------------------------
       
   361 //
       
   362 void CUpnpSharingAlgorithmImpl::ShareContainersL(
       
   363                     RPointerArray<CUpnpContainer>& aContainerArray )
       
   364     {
       
   365     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   366 ShareContainersL" );
       
   367 
       
   368     TPtrC8 previousContainerId( KNullDesC8 );
       
   369 
       
   370     // Share containers
       
   371     for ( TInt i = 0; i < aContainerArray.Count(); i++ )
       
   372         {
       
   373         CUpnpContainer* container = aContainerArray[i];
       
   374 
       
   375         // first container ( index = 0 ) have parent id ready
       
   376         if ( container->ParentId() == KNullDesC8 )
       
   377             {
       
   378             // parentid is previously shared container id
       
   379             container->SetParentIdL( previousContainerId );
       
   380             }
       
   381 
       
   382         TInt error( KErrNone );
       
   383         TRAP( error, iSharingAO->ShareContainerL( container ));
       
   384         if ( error )
       
   385             {
       
   386             // remove shared objects from server because of error
       
   387             RemoveSharedObjectsL( aContainerArray );
       
   388             User::LeaveIfError( error );
       
   389             }
       
   390         // container got id on sharing - keep it safe for next container
       
   391         // because it it used as parent container id
       
   392         previousContainerId.Set( container->Id());
       
   393 
       
   394         // no errors - append containers to cdsliteobjectarray
       
   395         TRAP( error, iSharedContentArray->AppendL( container ));
       
   396         if ( error )
       
   397             {
       
   398             // remove shared objects from array because of error
       
   399             RemoveSharedObjectsL( aContainerArray );
       
   400             User::LeaveIfError( error );
       
   401             }
       
   402         }
       
   403     }
       
   404 
       
   405 // --------------------------------------------------------------------------
       
   406 // CUpnpSharingAlgorithmImpl::ShareReferenceObjectsL
       
   407 // --------------------------------------------------------------------------
       
   408 //
       
   409 void CUpnpSharingAlgorithmImpl::ShareReferenceObjectsL( CUpnpItem* aItem )
       
   410     {
       
   411     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   412 ShareReferenceObjectsL" );
       
   413 
       
   414     // Get cds object from array. Reference item ids will be
       
   415     // added to this item
       
   416     TInt index = iSharedContentArray->FindByObjectId( aItem->Id() );
       
   417     CUpnpCdsLiteObject& object = iSharedContentArray->ObjectAtL( index );
       
   418 
       
   419     // reset id because a new one will be given when reference is shared
       
   420     aItem->SetIdL( KNullDesC8 );
       
   421     // reset refid field because item is reference
       
   422     aItem->SetRefIdL( KNullDesC8 );
       
   423     // reset title
       
   424     aItem->SetTitleL( KNullDesC8 );
       
   425     // reset parentId
       
   426     aItem->SetParentIdL( KNullDesC8 );
       
   427 
       
   428     if ( aItem->ObjectClass() == KClassAudioMusicTrack )
       
   429         {
       
   430         // Resolve genre containers
       
   431         RPointerArray<CUpnpContainer> refContainers =
       
   432                  iContainerResolver->ResolveReferenceContainersL( EGenreType,
       
   433                                                                   *aItem );
       
   434         CleanupResetAndDestroyPushL( refContainers );
       
   435         if ( refContainers.Count() ||
       
   436              aItem->ParentId() != KNullDesC8 )
       
   437             {
       
   438             // share container(s) and item
       
   439             ShareReferenceObjectL( refContainers, object, *aItem );
       
   440             }
       
   441         CleanupStack::PopAndDestroy( &refContainers );
       
   442 
       
   443         // reset parentId
       
   444         aItem->SetParentIdL( KNullDesC8 );
       
   445         // Resolve album containers
       
   446         refContainers =
       
   447                iContainerResolver->ResolveReferenceContainersL( EAlbumType,
       
   448                                                                 *aItem );
       
   449         CleanupResetAndDestroyPushL( refContainers );
       
   450         if ( refContainers.Count() ||
       
   451              aItem->ParentId() != KNullDesC8 )
       
   452             {
       
   453             // share container(s) and item
       
   454             ShareReferenceObjectL( refContainers, object, *aItem );
       
   455             }
       
   456         CleanupStack::PopAndDestroy( &refContainers );
       
   457 
       
   458         // reset parentId
       
   459         aItem->SetParentIdL( KNullDesC8 );
       
   460         // Resolve artist containers
       
   461         refContainers =
       
   462                iContainerResolver->ResolveReferenceContainersL( EArtistType,
       
   463                                                                 *aItem );
       
   464         CleanupResetAndDestroyPushL( refContainers );
       
   465         if ( refContainers.Count() ||
       
   466              aItem->ParentId() != KNullDesC8 )
       
   467             {
       
   468             // share container(s) and item
       
   469             ShareReferenceObjectL( refContainers, object, *aItem );
       
   470             }
       
   471         CleanupStack::PopAndDestroy( &refContainers );
       
   472 
       
   473         // reset parentId
       
   474         aItem->SetParentIdL( KNullDesC8 );
       
   475         // Resolve artist containers
       
   476         refContainers =
       
   477            iContainerResolver->ResolveReferenceContainersL( EMusicByDateType,
       
   478                                                             *aItem );
       
   479         CleanupResetAndDestroyPushL( refContainers );
       
   480         if ( refContainers.Count() ||
       
   481              aItem->ParentId() != KNullDesC8 )
       
   482             {
       
   483             // share container(s) and item
       
   484             ShareReferenceObjectL( refContainers, object, *aItem );
       
   485             }
       
   486         CleanupStack::PopAndDestroy( &refContainers );
       
   487     }
       
   488 
       
   489 
       
   490     else if ( aItem->ObjectClass() == KClassImage )
       
   491         {
       
   492         // reset parentId
       
   493         aItem->SetParentIdL( KNullDesC8 );
       
   494         // resolve date container
       
   495         RPointerArray<CUpnpContainer> refContainers =
       
   496            iContainerResolver->ResolveReferenceContainersL( EImageByDateType,
       
   497                                                             *aItem );
       
   498         CleanupResetAndDestroyPushL( refContainers );
       
   499         if ( refContainers.Count() ||
       
   500              aItem->ParentId() != KNullDesC8 )
       
   501             {
       
   502             // share container(s) and item
       
   503             ShareReferenceObjectL( refContainers, object, *aItem );
       
   504             }
       
   505         CleanupStack::PopAndDestroy( &refContainers );
       
   506         }
       
   507 
       
   508     else if ( aItem->ObjectClass() == KClassVideo )
       
   509         {
       
   510         // reset parentId
       
   511         aItem->SetParentIdL( KNullDesC8 );
       
   512         // resolve date container
       
   513         RPointerArray<CUpnpContainer> refContainers =
       
   514            iContainerResolver->ResolveReferenceContainersL( EVideoByDateType,
       
   515                                                             *aItem );
       
   516         CleanupResetAndDestroyPushL( refContainers );
       
   517         if ( refContainers.Count() )
       
   518             {
       
   519             // share container(s) and item
       
   520             ShareReferenceObjectL( refContainers, object, *aItem );
       
   521             }
       
   522         CleanupStack::PopAndDestroy( &refContainers );
       
   523         }
       
   524     else
       
   525         {
       
   526         __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   527     ShareReferenceObjectsL object class not defined" );
       
   528         }
       
   529     }
       
   530 
       
   531 // --------------------------------------------------------------------------
       
   532 // CUpnpSharingAlgorithmImpl::UnshareReferenceObjectsL
       
   533 // --------------------------------------------------------------------------
       
   534 //
       
   535 void CUpnpSharingAlgorithmImpl::UnshareReferenceObjectsL(
       
   536                                         CUpnpCdsLiteObject& aOriginalObject )
       
   537     {
       
   538     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   539 UnshareReferenceObjectsL" );
       
   540 
       
   541     TInt refIndex( KErrNotFound );
       
   542 
       
   543     do
       
   544         {
       
   545         // find reference items created from original item
       
   546         refIndex = iSharedContentArray->FindRefItemIdByOriginalIdL(
       
   547                                                 aOriginalObject.ObjectId() );
       
   548 
       
   549         if ( refIndex != KErrNotFound )
       
   550             {
       
   551             CUpnpCdsLiteObject& refObject =
       
   552                                   iSharedContentArray->ObjectAtL( refIndex );
       
   553 
       
   554             // Checks the container tree and unshares if needed
       
   555             // First container id to be checked is item's parentid
       
   556             TBool containerUnshared =
       
   557                         UnshareEmptyContainersL( refObject.ParentId() );
       
   558 
       
   559             // if container is not unshared the item needs to be unshared
       
   560             if ( !containerUnshared )
       
   561                 {
       
   562                 // Leave if ObjectId not found
       
   563                 if( refObject.ObjectId() == KNullDesC8 )
       
   564                     {
       
   565                     __LOG( "[CUpnpSharingAlgorithm]\t \
       
   566 CUpnpSharingAlgorithm::UnshareFileL, ObjectId not found!" );
       
   567                     User::Leave( KErrCorrupt );
       
   568                     }
       
   569 
       
   570                 TInt objectIdInt( KErrNotFound );
       
   571                 User::LeaveIfError( objectIdInt =
       
   572                     UPnPCommonUtils::DesC8ToInt( refObject.ObjectId() ));
       
   573 
       
   574                 // Unshare the item
       
   575                 iSharingAO->UnshareFileL( objectIdInt );
       
   576 
       
   577                 // Remove the file from the shared content array
       
   578                 iSharedContentArray->RemoveObjectAtL( refIndex );
       
   579                 }
       
   580             else
       
   581                 {
       
   582                 // index has changed because container(s) was unshared
       
   583                 refIndex = iSharedContentArray->FindByObjectId(
       
   584                                                      refObject.ObjectId() );
       
   585                 // item was also unshared when container was unshared
       
   586                 // it's enough to remove item from contentarray
       
   587                 iSharedContentArray->RemoveObjectAtL( refIndex );
       
   588                 }
       
   589             }
       
   590         } while ( refIndex != KErrNotFound );
       
   591     }
       
   592 
       
   593 // --------------------------------------------------------------------------
       
   594 // CUpnpSharingAlgorithmImpl::ShareReferenceObjectL
       
   595 // --------------------------------------------------------------------------
       
   596 //
       
   597 void CUpnpSharingAlgorithmImpl::ShareReferenceObjectL(
       
   598                             RPointerArray<CUpnpContainer>& aContainerArray,
       
   599                             CUpnpCdsLiteObject& aOriginalObject,
       
   600                             CUpnpItem& aItem )
       
   601     {
       
   602     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   603 ShareReferenceObjectL" );
       
   604 
       
   605     TPtrC8 refParentId( KNullDesC8 );
       
   606     TInt parentContainerId( KErrNotFound );
       
   607 
       
   608     TInt count = aContainerArray.Count();
       
   609 
       
   610     if ( count )
       
   611         {
       
   612         // reference container must be non-restricted
       
   613         aContainerArray[count-1]->SetRestricted(EFalse);
       
   614         // share containers
       
   615         ShareContainersL( aContainerArray );
       
   616 
       
   617         refParentId.Set( aContainerArray[count-1]->Id() );
       
   618         User::LeaveIfError(
       
   619             parentContainerId = UPnPCommonUtils::DesC8ToInt( refParentId ));
       
   620         }
       
   621     else // share just item
       
   622         {
       
   623         refParentId.Set( aItem.ParentId() );
       
   624         User::LeaveIfError(
       
   625             parentContainerId = UPnPCommonUtils::DesC8ToInt( refParentId ));
       
   626         }
       
   627 
       
   628     TInt originalItemId( KErrNotFound );
       
   629     User::LeaveIfError( originalItemId = UPnPCommonUtils::DesC8ToInt(
       
   630                                                aOriginalObject.ObjectId() ));
       
   631 
       
   632     iSharingAO->ShareReferenceL( parentContainerId, originalItemId, &aItem );
       
   633     // Add the file to the shared content array
       
   634     iSharedContentArray->AppendL( &aItem );
       
   635 
       
   636     }
       
   637 
       
   638 // --------------------------------------------------------------------------
       
   639 // CUpnpSharingAlgorithmImpl::RemoveSharedObjectsL
       
   640 // --------------------------------------------------------------------------
       
   641 //
       
   642 void CUpnpSharingAlgorithmImpl::RemoveSharedObjectsL(
       
   643                             RPointerArray<CUpnpContainer>& aContainerArray )
       
   644     {
       
   645     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   646 RemoveSharedObjectsL" );
       
   647 
       
   648     // Unshare starting from first container
       
   649     TPtrC8 containerIdStr = aContainerArray[0]->Id();
       
   650     TInt objectIdInt( KErrNotFound );
       
   651     User::LeaveIfError(
       
   652             objectIdInt= UPnPCommonUtils::DesC8ToInt( containerIdStr ));
       
   653     // unshares all objecs under this container with single call
       
   654     iSharingAO->UnshareContainerL( objectIdInt );
       
   655 
       
   656     for ( TInt i = 0; i < aContainerArray.Count(); i++)
       
   657         {
       
   658         // Remove object from the shared content array
       
   659         iSharedContentArray->RemoveL( aContainerArray[i] );
       
   660         }
       
   661     }
       
   662 
       
   663 // --------------------------------------------------------------------------
       
   664 // CUpnpSharingAlgorithmImpl::ReadSharedContent
       
   665 // --------------------------------------------------------------------------
       
   666 //
       
   667 CUpnpCdsLiteObjectArray& CUpnpSharingAlgorithmImpl::ReadSharedContent()
       
   668     {
       
   669     return *iSharedContentArray;
       
   670     }
       
   671 
       
   672 // --------------------------------------------------------------------------
       
   673 // CUpnpSharingAlgorithmImpl::StartServer
       
   674 // --------------------------------------------------------------------------
       
   675 //
       
   676 void CUpnpSharingAlgorithmImpl::StartServer()
       
   677     {
       
   678     __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   679 StartServer" );
       
   680 
       
   681     // start offline if not started
       
   682     TInt status( RUpnpMediaServerClient::EStopped );
       
   683     iMediaServer.Status( status );
       
   684     if ( status == RUpnpMediaServerClient::EStopped )
       
   685         {
       
   686         iMediaServer.StartOffline();
       
   687         __LOG( "[CUpnpSharingAlgorithm]\t CUpnpSharingAlgorithm::\
       
   688 StartServer started offline" );
       
   689         }
       
   690     }
       
   691 
       
   692 // --------------------------------------------------------------------------
       
   693 // CUpnpSharingAlgorithmImpl::Close
       
   694 // --------------------------------------------------------------------------
       
   695 //
       
   696 void CUpnpSharingAlgorithmImpl::Close()
       
   697     {
       
   698     delete this;
       
   699     }
       
   700     
       
   701 // --------------------------------------------------------------------------
       
   702 // CUpnpSharingAlgorithmImpl::CheckFileProtectionL
       
   703 // --------------------------------------------------------------------------
       
   704 //
       
   705 void CUpnpSharingAlgorithmImpl::CheckFileProtectionL(
       
   706                             const TFileName& aFileName )
       
   707     {
       
   708     if ( UPnPFileUtility::IsFileProtectedL( aFileName ) )
       
   709         {
       
   710         User::Leave( KErrAccessDenied );
       
   711         }
       
   712     }
       
   713     
       
   714 // End of file