upnpmediaserver/contentdirectoryservice/src/upnpcontentdirectory.cpp
changeset 0 7f85d04be362
child 1 499c41045f6d
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:  Content Directory
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <fbs.h>
       
    21 #include <uri8.h>
       
    22 #include <pathinfo.h>
       
    23 #include <xmlengdom.h>
       
    24 #include <xmlengserializationoptions.h>
       
    25 #include <xmlengdomparser.h>
       
    26 #include <sysutil.h>
       
    27 #include <bautils.h>
       
    28 #include <caf/caf.h>
       
    29 
       
    30 #include "upnpsender.h"
       
    31 #include "upnpcontentdirectory.h"
       
    32 #include "upnperrors.h"
       
    33 #include "upnpdominterface.h"
       
    34 #include "upnpstring.h"
       
    35 #include "upnpargument.h" 
       
    36 #include "upnphttpmessagefactory.h" 
       
    37 #include "upnpcons.h"
       
    38 #include "upnpdevice.h"
       
    39 #include "upnpfileutils.h"
       
    40 #include "upnpcommonupnplits.h"
       
    41 #include "upnpcontainerupdateid.h"
       
    42 #include "upnpcontentdirectorydb.h"
       
    43 #include "upnpfiletransfertimerobserver.h"
       
    44 #include "upnperror.h"
       
    45 #include "upnpcustomlog.h"
       
    46 #include "upnpprotocolinfo.h"
       
    47 #include "upnpcontentdirectoryglobals.h"
       
    48 #include "upnpresourcesbean.h"
       
    49 #include "upnpcdutils.h"
       
    50 #include "upnpmetadatastorage.h"
       
    51 #include "upnpautodestroyobjectlist.h"
       
    52 #include "upnpautodestroyobject.h"
       
    53 #include "upnpcontentdirectoryeventobserver.h"
       
    54 #include "upnpfiletransferevent.h"
       
    55 #include "upnpfiletransfer.h"
       
    56 #include "upnpprotocolinfolocal.h"
       
    57 #include "upnpxmlcontentfilter.h"
       
    58 #include "upnpstatehandler.h"
       
    59 #include "upnptransferuploader.h"
       
    60 #include "upnpmimemapper.h"
       
    61 #include "upnptransfercontroller.h"
       
    62 #include "upnptransferinterface.h"
       
    63 #include "upnpdlnafilter.h"
       
    64 #include "upnphttpserversession.h"
       
    65 #include "upnphttpserverruntime.h"
       
    66 #include "upnpsettings.h"
       
    67 #include "upnpdeviceimplementationbase.h"
       
    68 #include "upnpsecuritymanager.h"
       
    69 #include "upnpmediaserversettings.h"
       
    70 
       
    71 // Constant definitions
       
    72 using namespace UpnpPaths;
       
    73 using namespace UpnpDlnaProtocolInfo;
       
    74 
       
    75 // ============================= LOCAL FUNCTIONS ===============================
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // DestroyRPointerArray
       
    79 // Defined in upnpelementfactory.cpp
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 void DestroyRArray( TAny* aArray );
       
    83 void RPointerArrayTPtrC8( TAny* aArray )
       
    84     {
       
    85     RPointerArray<TPtrC8>* array =
       
    86             reinterpret_cast<RPointerArray<TPtrC8>*>(aArray);
       
    87     array->ResetAndDestroy( );
       
    88     }
       
    89 
       
    90 // ================= MEMBER FUNCTIONS =======================
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CUpnpContentDirectory::CUpnpContentDirectory
       
    94 // C++ default constructor can NOT contain any code, that
       
    95 // might leave.
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 CUpnpContentDirectory::CUpnpContentDirectory( CUpnpDevice& aDevice,
       
    99     MUpnpThumbnailCreatorObserver* aThmbObs, MUpnpTransferInterface* aTransfer ) :
       
   100 
       
   101     CUpnpServiceImplementation(aDevice), iThmbObs(aThmbObs),
       
   102             iTransferHandler(aTransfer)
       
   103 
       
   104     {
       
   105     SetExecutedAction( EUndefinedAction );
       
   106     }
       
   107 // -----------------------------------------------------------------------------
       
   108 // CUpnpContentDirectory::~CUpnpContentDirectory
       
   109 // C++ default destructor
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 CUpnpContentDirectory::~CUpnpContentDirectory()
       
   113     {
       
   114     delete iDownloadDir;
       
   115     delete iThumbDir;
       
   116     delete iElementdb;
       
   117 
       
   118     delete iTransferController;
       
   119     delete iMimeToExtMap;
       
   120 
       
   121     delete iContentDirectoryDb;
       
   122 
       
   123     iThObjectIds.Close( );
       
   124 
       
   125     iThumbnailCreators.ResetAndDestroy( );
       
   126 
       
   127     iDOMImpl.Close( );
       
   128     if ( iBitmapServerConnection == KErrNone )
       
   129         RFbsSession::Disconnect( );
       
   130     iFs.Close( );
       
   131     if ( iEcomUsed )
       
   132         {
       
   133         REComSession::FinalClose( );
       
   134         }
       
   135     delete iAutoDestroyObjects;
       
   136     delete iStateHandler;
       
   137     if ( iHttpServerSession )
       
   138         {
       
   139         iHttpServerSession->Stop();
       
   140         }
       
   141     delete iHttpServerSession;
       
   142     delete iDlnaFilter;
       
   143     delete iSecurityManager;
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // CUpnpContentDirectory::ConstructL
       
   148 // Two-phased constructor.
       
   149 // Initialize variables, set parent service and create XML reader.
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CUpnpContentDirectory::ConstructL( TUpnpCdSettings aSettings,
       
   153     CUpnpMetadataStorage* aMetadataStorage )
       
   154     {
       
   155     iDOMImpl.OpenL( );
       
   156 
       
   157     iTransferController = CUpnpTransferController::NewL( this );
       
   158 
       
   159     iMimeToExtMap = CUpnpMimeMapper::NewL( );
       
   160 
       
   161     BaseConstructL( aSettings.iDescriptionPath, KContentDirectoryType( ) );
       
   162 
       
   163     User::LeaveIfNull( aSettings.iDevice );
       
   164     
       
   165     CUpnpSecurityManager* secMan = CUpnpSecurityManager::NewInstanceFromUpnpSettingsL();
       
   166     
       
   167     iSecurityManager = secMan;
       
   168     iDlnaFilter = CUpnpDlnaFilter::NewL( this, secMan );
       
   169     
       
   170     iElementdb = CUpnpElementFactory::NewL( aSettings.iObjectsXmlPath );
       
   171     iElementdb->GetContentDirectoryReference( this );
       
   172 
       
   173     SetStateVariableL( KSystemUpdateID( ), KZero( ) );
       
   174     SetStateVariableL( KContainerUpdateIDs( ), UpnpCD::KEmptyString( ) );
       
   175     SetStateVariableL( KTransferIDs( ), UpnpCD::KEmptyString( ) , EFalse );
       
   176     SetStateVariableL( KSortCapsVariableName( ), KSortCapabilities( ) );
       
   177     SetStateVariableL( KSearchCapsVariableName( ), KSearchCapabilities( ) );
       
   178 
       
   179     // file session 
       
   180     User::LeaveIfError( iFs.Connect( ) );
       
   181 
       
   182     iBitmapServerConnection = RFbsSession::Connect( );
       
   183     iDownloadDir = aSettings.iDownloadDir.AllocL( );
       
   184     iThumbDir = aSettings.iThumbnailDir.AllocL( );
       
   185     iContentDirectoryDb = CUpnpContentDirectoryDb::NewL( aMetadataStorage );
       
   186     iAutoDestroyObjects = CUpnpAutoDestroyObjectList::NewL( );
       
   187     iCreateThumbnails = aSettings.iCreateThumbnail;
       
   188     iMaxRequestCount = aSettings.iMaxRequestCount;
       
   189     iEventObserver = aSettings.iEventObserver;
       
   190     iStateHandler = CUpnpStateHandler::NewL( this );
       
   191     
       
   192     ConstructHttpL();
       
   193     }
       
   194 
       
   195 void CUpnpContentDirectory::ConstructHttpL()
       
   196     {
       
   197     TInt iapId = CUpnpSettings::GetIapL();
       
   198     iHttpServerSession = CUpnpHttpServerSession::NewL( iapId, *this );
       
   199     iHttpServerSession->DefaultRuntime().SetCreator( *this );
       
   200     iHttpServerSession->StartL();
       
   201     }
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CUpnpContentDirectory::NewL
       
   205 // Two-phased constructor.
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 CUpnpContentDirectory* CUpnpContentDirectory::NewL(
       
   209     TUpnpCdSettings aSettings, CUpnpMetadataStorage* aMetadataStorage,
       
   210     MUpnpTransferInterface* aTransfer )
       
   211     {
       
   212     CUpnpContentDirectory* cd = new (ELeave) CUpnpContentDirectory( *aSettings.iDevice,
       
   213             aSettings.iThmbObs,
       
   214             aTransfer
       
   215     );
       
   216     CleanupStack::PushL( cd );
       
   217     cd->ConstructL( aSettings, aMetadataStorage );
       
   218     CleanupStack::Pop( cd );
       
   219     return cd;
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CUpnpContentDirectory::ActionReceivedLD
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 void CUpnpContentDirectory::ActionReceivedLD( CUpnpAction* aAction )
       
   227     {
       
   228     CleanupStack::PushL( aAction );
       
   229     //check security
       
   230     if ( iSecurityManager &&
       
   231             KErrNone != iSecurityManager->AuthorizeAction( aAction ) )
       
   232         {
       
   233         //server returns upnp error action failed when not authorized
       
   234         User::Leave(EActionFailed);
       
   235         }
       
   236     // in case of low disk space
       
   237     if ( !IsDbOrDiskProblemL( aAction ) )
       
   238         {
       
   239         // handle event
       
   240         TInt ret = EUpnpUndefined;
       
   241         TRAPD( err, ret = DoActionReceivedL( aAction ) );
       
   242         if ( DataBaseHasBeenRecoveredL( err ) )
       
   243             {
       
   244             TRAP( err, ret = DoActionReceivedL( aAction ) );
       
   245             if ( err == KErrCorrupt )
       
   246                 {
       
   247                 err = iContentDirectoryDb->RecreateDatabaseFile();
       
   248                 }
       
   249             }
       
   250         else if ( err == KErrNone )
       
   251             {
       
   252             err = ret;
       
   253             }
       
   254         LOGS( "ContentDirectory ActionReceivedLD sending soon." );
       
   255         DoSendActionL( aAction, TUpnpError( err ) );
       
   256         }
       
   257     CleanupStack::PopAndDestroy( aAction );
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CUpnpContentDirectory::DoActionReceivedL
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 TInt CUpnpContentDirectory::DoActionReceivedL( CUpnpAction* aAction )
       
   265     {
       
   266     // sendAndDestroy defines that after processing action must be 
       
   267     // sent and destroyed
       
   268     TUpnpErrorCode err = EUndefined;
       
   269 
       
   270     // Get the name, saves calls in the if-else statements
       
   271     TPtrC8 name( aAction->Name() );
       
   272     // Verify the action and do the operation(s)
       
   273     if ( name.Compare( KBrowse ) == 0 )
       
   274         {
       
   275         err = BrowseL( aAction );
       
   276         }
       
   277     else if ( name.Compare( KCreateReference ) == 0 )
       
   278         {
       
   279         err = CreateReferenceL( aAction );
       
   280         }
       
   281     else if ( name.Compare( KCreateObject ) == 0 )
       
   282         {
       
   283         LOGS( "ContentDirectory: Creating a new object" );
       
   284         err = CreateObjectL( aAction );
       
   285         }
       
   286     else if ( name.Compare( KDeleteResource ) == 0 )
       
   287         {
       
   288         err = DeleteResourceL( aAction );
       
   289         }
       
   290     else if ( name.Compare( KDestroyObject ) == 0 )
       
   291         {
       
   292         err = DestroyObjectL( aAction );
       
   293         }
       
   294     else if ( name.Compare( KExportResource ) == 0 )
       
   295         {
       
   296         err = ExportResourceL( aAction );
       
   297         }
       
   298     else if ( name.Compare( KGetSearchCapabilities ) == 0 )
       
   299         {
       
   300         err = GetSearchCapabilitiesL( aAction );
       
   301         }
       
   302     else if ( name.Compare( KGetSortCapabilities ) == 0 )
       
   303         {
       
   304         err = GetSortCapabilitiesL( aAction );
       
   305         }
       
   306     else if ( name.Compare( KGetSystemUpdateID ) == 0 )
       
   307         {
       
   308         err = GetSystemUpdateIdL( aAction );
       
   309         }
       
   310     else if ( name.Compare( KGetTransferProgress ) == 0 )
       
   311         {
       
   312         err = GetTransferProgressL( aAction );
       
   313         }
       
   314     else if ( name.Compare( KImportResource ) == 0 )
       
   315         {
       
   316         err = ImportResourceL( aAction );
       
   317         }
       
   318     else if ( name.Compare( KStopTransferResource ) == 0 )
       
   319         {
       
   320         err = StopTransferResourceL( aAction );
       
   321         }
       
   322     else
       
   323         {
       
   324         err = EInvalidAction;
       
   325         }
       
   326     return err;
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // CUpnpContentDirectory::DoActionReceivedCDL
       
   331 // -----------------------------------------------------------------------------
       
   332 //    
       
   333 void CUpnpContentDirectory::HttpGetStartedSoapNotifyL( TInt /*aSessionId*/)
       
   334     {
       
   335     // not needed upnp response returned by ActionReceivedCDL
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // CUpnpContentDirectory::DoActionReceivedCDL
       
   340 // -----------------------------------------------------------------------------
       
   341 //    
       
   342 void CUpnpContentDirectory::HttpPostStartedL( TInt /*aSessionId*/)
       
   343     {
       
   344     // not needed upnp response returned by ActionReceivedCDL
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CUpnpContentDirectory::UnknownHttpEventReceivedL 
       
   349 // -----------------------------------------------------------------------------
       
   350 //
       
   351 EXPORT_C void CUpnpContentDirectory::UnknownHttpEventReceivedL( CUpnpHttpMessage& aHttpMsg )
       
   352     {
       
   353     CUpnpFileTransferEvent* transferEvent =
       
   354             iTransferController->TransferL( aHttpMsg.SessionId( ) );
       
   355     if ( !transferEvent )
       
   356         {
       
   357         return;
       
   358         }
       
   359 
       
   360     TInt err = aHttpMsg.Error( );
       
   361     if ( err != EHttpRequestTimeout && err != EHttpInsufficientStorage )
       
   362         {
       
   363         err = EHttpOk;
       
   364         }
       
   365 
       
   366     HBufC* fileName16 = UpnpString::ToUnicodeL( aHttpMsg.InFilename( ) );
       
   367     CleanupStack::PushL( fileName16 );
       
   368     transferEvent->SetFilePathL( *fileName16 );
       
   369     CleanupStack::PopAndDestroy( fileName16 );
       
   370 
       
   371     iTransferController->HttpResponseReceivedL( aHttpMsg.SessionId( ), err );
       
   372     }
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CUpnpContentDirectory::IsDrmFileL 
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 TBool CUpnpContentDirectory::IsDrmFileL( const TDesC8& aFileName )
       
   379     {
       
   380     HBufC* tmp = UpnpCdUtils::Des8ToDesLC( aFileName );
       
   381     TBool ret = IsDrmFileL( *tmp );
       
   382     CleanupStack::PopAndDestroy( tmp );
       
   383     return ret;
       
   384     }
       
   385 
       
   386 // -----------------------------------------------------------------------------
       
   387 // CUpnpContentDirectory::IsDrmFileL 
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 TBool CUpnpContentDirectory::IsDrmFileL( const TDesC& aFileName )
       
   391     {
       
   392     if ( !BaflUtils::FileExists( iFs, aFileName ) )
       
   393         {
       
   394         return EFalse;
       
   395         }
       
   396     TBool ret = EFalse;
       
   397     TInt value=0, err;
       
   398 
       
   399     ContentAccess::CManager *manager = ContentAccess::CManager::NewL( );
       
   400     CleanupStack::PushL( manager );
       
   401 
       
   402     ContentAccess::CVirtualPath *path =
       
   403             ContentAccess::CVirtualPath::NewL( aFileName );
       
   404     CleanupStack::PushL( path );
       
   405 
       
   406     ContentAccess::TVirtualPathPtr virtualPath = *path;
       
   407 
       
   408     err = manager->GetAttribute( ContentAccess::EIsProtected, value,
       
   409         virtualPath );
       
   410     if ( err == KErrNone )
       
   411         {
       
   412         if ( value )
       
   413             {
       
   414             // delete file
       
   415             iFs.Delete( aFileName );
       
   416             ret = ETrue;
       
   417             }
       
   418         }
       
   419     CleanupStack::PopAndDestroy( 2, manager );
       
   420 
       
   421     return ret;
       
   422     }
       
   423 
       
   424 // -----------------------------------------------------------------------------
       
   425 // CUpnpContentDirectory::CreateReferenceL
       
   426 // -----------------------------------------------------------------------------
       
   427 //
       
   428 TUpnpErrorCode CUpnpContentDirectory::CreateReferenceL( CUpnpAction*& aAction )
       
   429     {
       
   430     TUpnpErrorCode ret = EUpnpUndefined;
       
   431 
       
   432     // Get IDs from action 
       
   433     TInt conId( KErrNotFound );
       
   434     TInt objId( KErrNotFound );
       
   435 
       
   436     TInt err = UpnpCdUtils::StringToInteger(
       
   437         aAction->ArgumentValue( KContainerID ), &conId );
       
   438     if ( err!=KErrNone )
       
   439         {
       
   440         err=ENoContainer;
       
   441         User::Leave( ENoContainer );
       
   442         }
       
   443 
       
   444     err = UpnpCdUtils::StringToInteger(
       
   445         aAction->ArgumentValue( UpnpCD::KObjectID ), &objId );
       
   446     if ( err!=KErrNone )
       
   447         {
       
   448         err=ENoContainer;
       
   449         User::Leave( ENoSuchObject );
       
   450         }
       
   451 
       
   452     objId = iContentDirectoryDb->ReferedObjectIdL( objId );
       
   453 
       
   454     RXmlEngDocument conDidl;
       
   455     RXmlEngDocument objDidl;
       
   456     CleanupClosePushL( conDidl );
       
   457     CleanupClosePushL( objDidl );
       
   458 
       
   459     ret = iContentDirectoryDb->GetObjectL( conId, conDidl, KAsterisk8 );
       
   460     if ( ret != EUpnpOk )
       
   461         User::Leave( ENoContainer );
       
   462 
       
   463     ret = iContentDirectoryDb->GetObjectL( objId, objDidl, KAsterisk8 );
       
   464     if ( ret != EUpnpOk )
       
   465         User::Leave( ENoSuchObject );
       
   466 
       
   467     TXmlEngElement con = conDidl.DocumentElement().FirstChild().AsElement( );
       
   468     TXmlEngElement obj = objDidl.DocumentElement().FirstChild().AsElement( );
       
   469 
       
   470     if ( obj.Name().Compare( KItem ) != 0 )
       
   471         {
       
   472         User::Leave( ENoSuchObject );
       
   473         }
       
   474     if ( con.Name().Compare( KContainer ) != 0 )
       
   475         {
       
   476         User::Leave( ENoContainer );
       
   477         }
       
   478     if ( iContentDirectoryDb->CheckObjectRestrictionL( conId ) != EUpnpOk )
       
   479         {
       
   480         User::Leave( ERestrictedParentObject );
       
   481         }
       
   482 
       
   483     TInt refId;
       
   484     RXmlEngDocument refDidl = iContentDirectoryDb->PrepareDidlDocumentL( );
       
   485     CleanupClosePushL( refDidl );
       
   486 
       
   487     TXmlEngElement refEl = refDidl.DocumentElement().AddNewElementSameNsL( KItem( ) );
       
   488 
       
   489     refEl.AddNewAttributeL( KRefID( ),
       
   490         aAction->ArgumentValue( UpnpCD::KObjectID ) );
       
   491 
       
   492     refEl.AddNewAttributeL( KParentID( ),
       
   493         aAction->ArgumentValue( KContainerID ) );
       
   494 
       
   495     refEl.AddNewAttributeL( KRestricted( ), KFalseValue8( ) );
       
   496 
       
   497     // title
       
   498     TXmlEngElement objTitle;
       
   499     UpnpDomInterface::GetElementL( obj, objTitle, KObjTiltleColName8( ) );
       
   500     refEl.AppendChildL( objTitle.Unlink().AsElement( ) );
       
   501 
       
   502     // class
       
   503     TXmlEngElement objClass;
       
   504     UpnpDomInterface::GetElementL( obj, objClass, KObjClassColName8( ) );
       
   505     refEl.AppendChildL( objClass.Unlink().AsElement( ) );
       
   506 
       
   507     iContentDirectoryDb->InsertObjectL( refDidl, conId, &refId );
       
   508 
       
   509     //increase system update ID
       
   510     iStateHandler->IncreaseSystemUpdateIdL( );
       
   511     iStateHandler->IncreaseContainerUpdateIdL( (TDesC8&)aAction->ArgumentValue( KContainerID ) );
       
   512 
       
   513     TBuf8<KMaxIntegerLen> newid;
       
   514     newid.Num( refId );
       
   515     aAction->SetArgumentL( (TDesC8&)KNewID, newid );
       
   516 
       
   517     CleanupStack::PopAndDestroy( &refDidl );
       
   518     CleanupStack::PopAndDestroy( &objDidl );
       
   519     CleanupStack::PopAndDestroy( &conDidl );
       
   520 
       
   521     return EHttpOk;
       
   522     }
       
   523 
       
   524 // -----------------------------------------------------------------------------
       
   525 // CUpnpContentDirectory::BrowseL
       
   526 // -----------------------------------------------------------------------------
       
   527 //
       
   528 TUpnpErrorCode CUpnpContentDirectory::BrowseL( CUpnpAction*& aAction )
       
   529     {
       
   530     RDebug::Print( _L( "CUpnpContentDirectory::BrowseL start" ) );
       
   531     TInt err( KErrGeneral );
       
   532 
       
   533     //Check ObjectID
       
   534     TInt objectID( KErrNotFound );
       
   535     err = UpnpCdUtils::StringToInteger(
       
   536         (TDesC8&)aAction->ArgumentValue( UpnpCD::KObjectID( ) ), &objectID );
       
   537     if ( err != KErrNone )
       
   538         {
       
   539         User::Leave( ENoSuchObject );
       
   540         }
       
   541     //Check SortCriteria
       
   542     if ( aAction->ArgumentValue( (TDesC8&) KSortCriteria ).Compare( (TDesC8&)KSortCapabilities )
       
   543             != 0 )
       
   544         {
       
   545         User::Leave( EInvalidSort ); //wrong sort flag
       
   546         }
       
   547 
       
   548     //Check BrowseFlag
       
   549     TDesC8& browseFlag =
       
   550             (TDesC8&)aAction->ArgumentValue( (TDesC8&)KBrowseFlag );
       
   551     if ( browseFlag.Compare( KBrowseMeta ) != 0
       
   552             && browseFlag.Compare( KBrowseChilds ) != 0 )
       
   553         {
       
   554         User::Leave( EInvalidArgs ); //wrong browse flag    
       
   555         }
       
   556 
       
   557     //Check StartIndex
       
   558     TInt startIndex( KErrNotFound );
       
   559     err = UpnpString::StringToInt(
       
   560         (TDesC8&)aAction->ArgumentValue( (TDesC8&)KStartingIndex ),
       
   561         &startIndex );
       
   562     if ( KErrNone != err )
       
   563         {
       
   564         User::Leave( EInvalidArgs );
       
   565         }
       
   566 
       
   567     //Additional check of StartIndex, see documentation
       
   568     if ( browseFlag.Compare( KBrowseMeta ) == 0 && startIndex != 0 )
       
   569         {
       
   570         User::Leave( EInvalidArgs );
       
   571         }
       
   572 
       
   573     //Check RequestedCount
       
   574     TInt reqCount( KErrNotFound );
       
   575     err = UpnpString::StringToInt(
       
   576         (TDesC8&)aAction->ArgumentValue( (TDesC8&)KRequestedCount ),
       
   577         &reqCount );
       
   578     if ( KErrNone != err )
       
   579         {
       
   580         User::Leave( EInvalidArgs );
       
   581         }
       
   582 
       
   583     //Filter
       
   584     HBufC8* filter = aAction->ArgumentValue( KFilter() ).AllocLC( );
       
   585     filter->Des().TrimAll( );
       
   586 
       
   587     // Metadata browse
       
   588     if ( browseFlag.Compare( KBrowseMeta ) == 0 )
       
   589         {
       
   590 
       
   591         RXmlEngDocument objectDoc;
       
   592         CleanupClosePushL( objectDoc );
       
   593         // Try to get original object not a reference 
       
   594         TInt id = iContentDirectoryDb->ReferedObjectIdL( objectID );
       
   595         CleanupStack::Check( &objectDoc );
       
   596         TUpnpErrorCode err = EUndefined;
       
   597         err = iContentDirectoryDb->GetObjectL( id, objectDoc, *filter );
       
   598         if ( err != EUpnpOk )
       
   599             User::Leave( err );
       
   600         CleanupStack::Check( &objectDoc );
       
   601         //--------- new solution for references --------
       
   602 
       
   603         // if id != objectID it means that we have a reference, so now id = 'real object id' and objectID = 'reference object id' 
       
   604         if ( id != objectID )
       
   605             {
       
   606             TInt refParentID = iContentDirectoryDb->GetParentIdL( objectID );
       
   607             User::LeaveIfError( refParentID );
       
   608 
       
   609             UpnpCdUtils::SetObjectIdL(
       
   610                 UpnpCdUtils::GetObjectElementL( objectDoc ), objectID );
       
   611             UpnpCdUtils::SetObjectRefIdL(
       
   612                 UpnpCdUtils::GetObjectElementL( objectDoc ), id );
       
   613             UpnpCdUtils::SetObjectParentIdL(
       
   614                 UpnpCdUtils::GetObjectElementL( objectDoc ), refParentID );
       
   615             UpnpCdUtils::SetRestrictedFieldL(
       
   616                 UpnpCdUtils::GetObjectElementL( objectDoc ), EFalse );
       
   617             }
       
   618         //---------------------------------------------
       
   619 
       
   620         // Serialize object to descriptor - without DIDL-Lite element
       
   621         RBuf8 buf;
       
   622         TXmlEngSerializationOptions
       
   623                 options(TXmlEngSerializationOptions::KOptionOmitXMLDeclaration
       
   624                         | TXmlEngSerializationOptions::KOptionIncludeNsPrefixes);
       
   625         objectDoc.SaveL( buf, iElementdb->ActiveElementL( objectDoc ),
       
   626             options );
       
   627         CleanupStack::Check( &objectDoc );
       
   628         CleanupClosePushL( buf );
       
   629         HBufC8* subResponse = buf.AllocLC( );
       
   630 
       
   631         CBufFlat* response = CBufFlat::NewL( KRespBufGranularity );
       
   632         CleanupStack::PushL( response );
       
   633         UpnpString::AppendStringL( *response, KDidlTag );
       
   634         UpnpString::AppendStringL( *response, *subResponse );
       
   635         UpnpString::AppendStringL( *response, (TDesC8&) KDidlEnd( ) );
       
   636 
       
   637         HBufC8* resultTmp =
       
   638                 UpnpCdUtils::EncodeXmlStringL( response->Ptr( 0 ) );
       
   639         CleanupStack::PushL( resultTmp );
       
   640         TPtr8 resultTmpPtr(resultTmp->Des( ));
       
   641         InsertRealIpAndPortL( resultTmpPtr );
       
   642 
       
   643         // Set action's OUT arguments       
       
   644         TBuf8<KMaxIntegerLen> num;
       
   645 
       
   646         //Result
       
   647         aAction->SetArgumentL( KResult, *resultTmp );
       
   648 
       
   649         //NumberReturned
       
   650         num.Num( 1 );
       
   651         aAction->SetArgumentL( KNumberReturned, num );
       
   652 
       
   653         //TotalMatches
       
   654         //num.Num(1);
       
   655         aAction->SetArgumentL( KTotalMatches, num );
       
   656 
       
   657         //UpadateID 
       
   658         num.Format(
       
   659             UpnpCD::KOneNumber,
       
   660             ContainerUpdateId( (TDesC8&)aAction->ArgumentValue( (TDesC8&)UpnpCD::KObjectID ) ) );
       
   661         aAction->SetArgumentL( KUpdateID, num );
       
   662 
       
   663         // clean up
       
   664         CleanupStack::PopAndDestroy( resultTmp );
       
   665         CleanupStack::PopAndDestroy( response );
       
   666         CleanupStack::PopAndDestroy( subResponse );
       
   667         CleanupStack::PopAndDestroy( &buf );
       
   668         CleanupStack::PopAndDestroy( &objectDoc );
       
   669 
       
   670         }
       
   671     //Children browse
       
   672     else if ( browseFlag.Compare( KBrowseChilds ) == 0 )
       
   673         {
       
   674         //array contains pointers to direct childrens.
       
   675         RArray<TInt> listOfAllObjects;
       
   676         CleanupClosePushL( listOfAllObjects );
       
   677 
       
   678         TUpnpErrorCode err = iContentDirectoryDb->GetObjectListL( objectID,
       
   679             listOfAllObjects );
       
   680 
       
   681         if ( err == ENoSuchObject )
       
   682             {
       
   683             User::Leave( err );
       
   684             }
       
   685 
       
   686         // if requestedCount==0 then return all matched objects
       
   687         if ( !reqCount )
       
   688             {
       
   689             reqCount = listOfAllObjects.Count( );
       
   690             }
       
   691 
       
   692         // if requestedCount cannot be greater than iMaxRequestCount
       
   693         if ( iMaxRequestCount > 0 && reqCount > iMaxRequestCount )
       
   694             {
       
   695             reqCount = iMaxRequestCount;
       
   696             }
       
   697 
       
   698         // get upnp objects
       
   699         RArray<RXmlEngDocument> objs;
       
   700         CleanupStack::PushL(TCleanupItem( DestroyRArray, &objs ));
       
   701 
       
   702         if ( startIndex < listOfAllObjects.Count( ) )
       
   703             { // prepare list of objects which are to be returned
       
   704             if ( startIndex + reqCount > listOfAllObjects.Count( ) )
       
   705                 reqCount = listOfAllObjects.Count( ) - startIndex;
       
   706             RArray<TInt> alistOfWantedObjs( &listOfAllObjects[startIndex],
       
   707                 reqCount);
       
   708 
       
   709             iContentDirectoryDb->GetObjectListL( alistOfWantedObjs, *filter,
       
   710                 objs );
       
   711             }
       
   712 
       
   713         // prepare response
       
   714         // Result
       
   715         HBufC8* resp = SerializeObjectsListL( objs );
       
   716         CleanupStack::PushL( resp );
       
   717         TPtr8 respPtr(resp->Des( ));
       
   718         InsertRealIpAndPortL( respPtr );
       
   719         aAction->SetArgumentL( KResult, *resp );
       
   720         // NumberReturned
       
   721         TBuf8<KMaxIntegerLen> num;
       
   722         num.Num( objs.Count( ) );
       
   723         aAction->SetArgumentL( KNumberReturned, num );
       
   724         // TotalMatches
       
   725         num.Num( listOfAllObjects.Count( ) );
       
   726         aAction->SetArgumentL( KTotalMatches, num );
       
   727         // UpdateID
       
   728         num.Format(
       
   729             UpnpCD::KOneNumber,
       
   730             ContainerUpdateId( (TDesC8&)aAction->ArgumentValue( (TDesC8&)UpnpCD::KObjectID ) ) );
       
   731         aAction->SetArgumentL( KUpdateID, num );
       
   732 
       
   733         // clean up
       
   734         CleanupStack::PopAndDestroy( resp );
       
   735         CleanupStack::PopAndDestroy( &objs );
       
   736         CleanupStack::PopAndDestroy( &listOfAllObjects );
       
   737         }
       
   738     else //no valid browseflag
       
   739         {
       
   740         User::Leave( EInvalidArgs );
       
   741         }
       
   742 
       
   743     RDebug::Print( _L( "CUpnpContentDirectory::BrowseL Result got OK" ) );
       
   744 
       
   745     CleanupStack::PopAndDestroy( filter );
       
   746 
       
   747     return EHttpOk;
       
   748     }
       
   749 
       
   750 // -----------------------------------------------------------------------------
       
   751 // CUpnpContentDirectory::SerializeObjectsListL
       
   752 // -----------------------------------------------------------------------------
       
   753 //
       
   754 HBufC8* CUpnpContentDirectory::SerializeObjectsListL(
       
   755     const RArray<RXmlEngDocument>& aObjs )
       
   756     {
       
   757     CBufFlat* buff = CBufFlat::NewL( KRespBufGranularity );
       
   758     CleanupStack::PushL( buff );
       
   759 
       
   760     // Didl start
       
   761     UpnpString::AppendStringL( *buff, KDidlTag );
       
   762 
       
   763     // content
       
   764     for ( TInt i = 0; i < aObjs.Count( ); i++ )
       
   765         {
       
   766         RBuf8 tmp;
       
   767         TXmlEngSerializationOptions
       
   768                 options(TXmlEngSerializationOptions::KOptionOmitXMLDeclaration
       
   769                         | TXmlEngSerializationOptions::KOptionIncludeNsPrefixes);
       
   770         aObjs[i].SaveL( tmp, iElementdb->ActiveElementL( aObjs[i] ), options );
       
   771         CleanupClosePushL( tmp );
       
   772         UpnpString::AppendStringL( *buff, tmp );
       
   773         UpnpString::AppendStringL( *buff, KLF8 );
       
   774         CleanupStack::PopAndDestroy( &tmp );
       
   775         }
       
   776 
       
   777     // Didl end
       
   778     UpnpString::AppendStringL( *buff, KDidlEnd );
       
   779 
       
   780     // prepare to return
       
   781     HBufC8* ret = UpnpCdUtils::EncodeXmlStringL( buff->Ptr( 0 ) );
       
   782 
       
   783     // clean up
       
   784     CleanupStack::PopAndDestroy( buff );
       
   785 
       
   786     return ret;
       
   787     }
       
   788 
       
   789 // -----------------------------------------------------------------------------
       
   790 // CUpnpContentDirectory::DestroyObjectL
       
   791 // -----------------------------------------------------------------------------
       
   792 //
       
   793 TUpnpErrorCode CUpnpContentDirectory::DestroyObjectL( CUpnpAction*& aAction )
       
   794     {
       
   795     TInt objId( KErrNotFound );
       
   796 
       
   797     TInt error(0);
       
   798     error = UpnpCdUtils::StringToInteger(
       
   799         (TDesC8&)aAction->ArgumentValue( UpnpCD::KObjectID( ) ), &objId );
       
   800     if ( (error == KErrArgument)||(error == KErrOverflow) )
       
   801         {
       
   802         User::Leave( ENoSuchObject );
       
   803         }
       
   804     else if ( error<0 )
       
   805         {
       
   806         User::Leave( error );
       
   807         }
       
   808     // check object restriction
       
   809     TUpnpErrorCode err = iContentDirectoryDb->CheckObjectRestrictionL( objId ); // if returned ENoContainer it is OK because first of all the restriction is checked
       
   810     if ( err == ERestrictedObject || err == ENoSuchObject )
       
   811         {
       
   812         User::Leave( err );
       
   813         }
       
   814 
       
   815     // check parent restriction
       
   816     err = iContentDirectoryDb->CheckObjectRestrictionL( objId );
       
   817     if ( err == ERestrictedObject )
       
   818         {
       
   819         User::Leave( ERestrictedParentObject );
       
   820         }
       
   821 
       
   822     // root and download containers must not be deleted
       
   823     if ( objId <= KDownloadContId )
       
   824         {
       
   825         /*
       
   826          Designing a UPnP AV MediaServer (3.2.4): 
       
   827          "If a CDS implementation exposes a CDS object that is not restricted but cannot fulfill a
       
   828          CDS:DestroyObject(), CDS:CreateObject(), or CDS:UpdateObject() request, then the MediaServer should
       
   829          return error code 720 to indicate the request could not be processed."
       
   830          */
       
   831         User::Leave( ECannotProcess );
       
   832         }
       
   833 
       
   834     RArray<TInt> refList;
       
   835     CleanupClosePushL( refList );
       
   836 
       
   837     TBuf8<KMaxIntegerLen> containerID;
       
   838     containerID.Num( iContentDirectoryDb->GetParentIdL( objId ) );
       
   839 
       
   840     iContentDirectoryDb->DeleteObjectL( objId, refList, ETrue );
       
   841 
       
   842     iStateHandler->IncreaseSystemUpdateIdL( );
       
   843     iStateHandler->IncreaseContainerUpdateIdL( containerID );
       
   844 
       
   845     for ( TInt i(0); i<refList.Count( ); i++ )
       
   846         {
       
   847         if ( objId!=refList[i] )
       
   848             {
       
   849             containerID.Num( refList[i] );
       
   850             iStateHandler->IncreaseSystemUpdateIdL( );
       
   851             iStateHandler->IncreaseContainerUpdateIdL( containerID );
       
   852             }
       
   853         }
       
   854     // clean up
       
   855     CleanupStack::PopAndDestroy( &refList );
       
   856     return EHttpOk;
       
   857     }
       
   858 
       
   859 // -----------------------------------------------------------------------------
       
   860 // CUpnpContentDirectory::AddToMetaDbL
       
   861 // -----------------------------------------------------------------------------
       
   862 //
       
   863 TUpnpErrorCode CUpnpContentDirectory::AddToMetaDbL(
       
   864     RXmlEngDocument& aFragment, TInt aContainer, TInt* aId, TBool aIsLocal )
       
   865     {
       
   866     // res list
       
   867     RArray<TXmlEngElement> resList;
       
   868     CleanupClosePushL( resList );
       
   869     UpnpDomInterface::GetElementListL( aFragment.DocumentElement( ), resList,
       
   870         KRes );
       
   871 
       
   872     // a source file for thumbnail
       
   873     HBufC* thumbSrcFile = NULL;
       
   874 
       
   875     if ( aIsLocal )
       
   876         { // local action
       
   877         // create thumbnail if there is only one res in the object
       
   878         if ( resList.Count( ) == 1 )
       
   879             {
       
   880             // get resource file name
       
   881             TPtrC8 val(resList[0].Text( ));
       
   882             // ignore if empty string
       
   883             if ( val.Length( ) && (!UpnpCdUtils::IsWhiteString( val )) )
       
   884                 {
       
   885                 TUriParser8 up;
       
   886                 User::LeaveIfError( up.Parse( val ) );
       
   887                 if ( up.Extract( EUriScheme ) == UpnpHTTP::KSchemeFile8 )
       
   888                     {
       
   889                     thumbSrcFile = up.GetFileNameL( ); // thumbnail will be created at the end of this function
       
   890                     CleanupStack::PushL( thumbSrcFile );// and the pointer will be poped 
       
   891                     }
       
   892                 }
       
   893             }
       
   894         TInt nextId = iContentDirectoryDb->GetObjectIdL( );
       
   895         // handle resources
       
   896         for ( TInt i = 0; i < resList.Count( ); i++ )
       
   897             {
       
   898             HandleLocalResourcesL( resList[i], nextId );
       
   899             }
       
   900         }
       
   901 
       
   902     // if only one empty res add importUri
       
   903     if ( resList.Count( ) )
       
   904         {
       
   905         TPtrC8 resValue(resList[0].Text( ));
       
   906         if ( !resValue.Length( ) || UpnpCdUtils::IsWhiteString( resValue ) )
       
   907             {
       
   908             AddImportUriToElL( resList[0] );
       
   909             }
       
   910         }
       
   911     if ( !resList.Count( ) )
       
   912         {
       
   913         AddNewResTagL( aFragment.DocumentElement( ) );
       
   914         }
       
   915     // add to database
       
   916     TUpnpErrorCode ret = iContentDirectoryDb->InsertObjectL( aFragment,
       
   917         aContainer, aId );
       
   918     if ( ret == EUpnpOk )
       
   919         {
       
   920         PrepareAutoDestroyObjectL( aFragment.DocumentElement( ), *aId );
       
   921         ret = EHttpOk;
       
   922         }
       
   923 
       
   924     if ( thumbSrcFile )
       
   925         {
       
   926         CreateThumbnailL( *thumbSrcFile, *aId );
       
   927         CleanupStack::PopAndDestroy( thumbSrcFile );
       
   928         }
       
   929     CleanupStack::PopAndDestroy( &resList );
       
   930     return ret;
       
   931     }
       
   932 
       
   933 // -----------------------------------------------------------------------------
       
   934 // CUpnpContentDirectory::CreateThumbnailForResourceL
       
   935 // -----------------------------------------------------------------------------
       
   936 //
       
   937 void CUpnpContentDirectory::PrepareAutoDestroyObjectL(
       
   938     TXmlEngElement aObject, TInt aObjId )
       
   939     {
       
   940     // check res
       
   941     RArray<TXmlEngElement> resList;
       
   942     CleanupClosePushL( resList );
       
   943     UpnpDomInterface::GetElementListL( aObject, resList, KRes );
       
   944     if ( resList.Count( ) > 0 )
       
   945         {
       
   946         // only first res is important
       
   947         TPtrC8 resValue(resList[0].Text( ));
       
   948         if ( !resValue.Length( ) || UpnpCdUtils::IsWhiteString( resValue ) )
       
   949             {
       
   950             TXmlEngAttr impUri = resList[0].AttributeNodeL( KImportUri( ) );
       
   951             // auto destroy
       
   952             CUpnpAutoDestroyObject* ado = CUpnpAutoDestroyObject::NewLC(
       
   953                 this, aObjId, UpnpCdUtils::ResIdFromUriL( impUri.Value( ) ) );
       
   954             iAutoDestroyObjects->AppendL( ado );
       
   955             CleanupStack::Pop( ado );
       
   956             }
       
   957         }
       
   958 
       
   959     // clean up
       
   960     CleanupStack::PopAndDestroy( &resList );
       
   961     }
       
   962 // -----------------------------------------------------------------------------
       
   963 // CUpnpContentDirectory::CreateThumbnailForResourceL
       
   964 // -----------------------------------------------------------------------------
       
   965 //
       
   966 void CUpnpContentDirectory::CreateThumbnailL( const TDesC& aSrcFile,
       
   967     TInt aObjId )
       
   968     {
       
   969     if ( iCreateThumbnails && IsThumbnailAvailableL( aSrcFile ) )
       
   970         {
       
   971         // thumbnail name
       
   972         HBufC* thumbName = CreateNameForThumbnailL( aSrcFile );
       
   973         CleanupStack::PushL( thumbName );
       
   974 
       
   975         // create thumbnail creator
       
   976         CUpnpThumbnailCreator* thCreator = CreateThumbnailCreatorL( aObjId ); // do not push it onto CleanupStack
       
   977         // ThumbnailCreator is created and added to iThumbnailCreators
       
   978         // but the creation process is not yet started,
       
   979         // therefore, not leaving code should be performed until the thCreator->CreateL method is called
       
   980         if ( thCreator )
       
   981             {
       
   982             // start creation process
       
   983             TRAPD( err, thCreator->CreateL( aSrcFile, *thumbName ) );
       
   984             if ( err )
       
   985                 {
       
   986                 RDebug::Print( _L( "Error while creating thumbnail, it won't be created. Removing from creator list." ) );
       
   987                 RemoveThCreatorAndObjectIdLD( thCreator );
       
   988                 }
       
   989             iEcomUsed = ETrue;
       
   990             }
       
   991         // clean up
       
   992         CleanupStack::PopAndDestroy( thumbName );
       
   993         }
       
   994     }
       
   995 // -----------------------------------------------------------------------------
       
   996 // CUpnpContentDirectory::IsThumbnailAvailableL
       
   997 // -----------------------------------------------------------------------------
       
   998 //
       
   999 TBool CUpnpContentDirectory::IsThumbnailAvailableL( const TDesC& aFile )
       
  1000     {
       
  1001     HBufC8* tmp = HBufC8::NewLC( aFile.Length( ) );
       
  1002     tmp->Des().Copy( aFile );
       
  1003     TBool ret = IsThumbnailAvailableL( *tmp );
       
  1004     CleanupStack::PopAndDestroy( tmp );
       
  1005     return ret;
       
  1006     }
       
  1007 
       
  1008 // -----------------------------------------------------------------------------
       
  1009 // CUpnpContentDirectory::IsThumbnailAvailableL
       
  1010 // -----------------------------------------------------------------------------
       
  1011 //
       
  1012 TBool CUpnpContentDirectory::IsThumbnailAvailableL( const TDesC8& aFile )
       
  1013     {
       
  1014     TBool result = EFalse;
       
  1015 
       
  1016     // get mime type for file
       
  1017     HBufC8* mt = GetMimeTypeLC( aFile );
       
  1018     TPtr8 mimeType(mt->Des( ));
       
  1019     mimeType.LowerCase( );
       
  1020 
       
  1021     if ( !mimeType.Compare( KMT_IMAGE_JPEG( ) ) )
       
  1022         {
       
  1023         result = ETrue;
       
  1024         }
       
  1025     else if ( !mimeType.Compare( KMT_IMAGE_PNG( ) ) )
       
  1026         {
       
  1027         result = ETrue;
       
  1028         }
       
  1029     else if ( !mimeType.Compare( KMT_IMAGE_GIF( ) ) )
       
  1030         {
       
  1031         result = ETrue;
       
  1032         }
       
  1033     else if ( !mimeType.Compare( KMT_IMAGE_BMP( ) ) )
       
  1034         {
       
  1035         result = ETrue;
       
  1036         }
       
  1037     CleanupStack::PopAndDestroy( mt );
       
  1038 
       
  1039     return result;
       
  1040     }
       
  1041 
       
  1042 // -----------------------------------------------------------------------------
       
  1043 // CUpnpContentDirectory::AddImportUriToElL
       
  1044 // -----------------------------------------------------------------------------
       
  1045 //
       
  1046 void CUpnpContentDirectory::AddImportUriToElL( TXmlEngElement aElement )
       
  1047     {
       
  1048     HBufC8* impUriVal = BuildImportUriShorterLC( );
       
  1049     HBufC8* impUriValRandomize = HBufC8::NewLC( impUriVal->Des().Length( )+ KRandomRangeLength );
       
  1050     HBufC8* number = UpnpCdUtils::RandomizeL( KRandomMax );
       
  1051     CleanupStack::PushL( number );
       
  1052     impUriValRandomize->Des().Copy( *impUriVal );
       
  1053     impUriValRandomize->Des().Append( *number );
       
  1054     aElement.AddNewAttributeL( KImportUri( ), *impUriValRandomize );
       
  1055     // clean up
       
  1056     CleanupStack::PopAndDestroy( number );
       
  1057     CleanupStack::PopAndDestroy( impUriValRandomize );
       
  1058     CleanupStack::PopAndDestroy( impUriVal );
       
  1059     }
       
  1060 
       
  1061 // -----------------------------------------------------------------------------
       
  1062 // CUpnpContentDirectory::AddNewResTagL
       
  1063 // -----------------------------------------------------------------------------
       
  1064 //
       
  1065 void CUpnpContentDirectory::AddNewResTagL( TXmlEngElement aObjEl )
       
  1066     {
       
  1067     TXmlEngElement item;
       
  1068     UpnpDomInterface::GetElementL( aObjEl, item, KItem );
       
  1069 
       
  1070     // ignore if null
       
  1071     if ( item.NotNull( ) )
       
  1072         {
       
  1073         TXmlEngElement newEl = item.AddNewElementL( KRes( ) );
       
  1074         newEl.AddNewAttributeL( KprotocolInfo( ), KEmptyProtocolInfoVal8( ) );
       
  1075         newEl.AddNewAttributeL( KProtInfoReqAtrSufPattern( ), KTrueValue8( ) );
       
  1076         // add importUri
       
  1077         AddImportUriToElL( newEl );
       
  1078         }
       
  1079     }
       
  1080 
       
  1081 // -----------------------------------------------------------------------------
       
  1082 // CUpnpContentDirectory::BuildContentUriL
       
  1083 // -----------------------------------------------------------------------------
       
  1084 HBufC8* CUpnpContentDirectory::BuildContentUriL( const TDesC& aFileExt,
       
  1085     TInt aObjectId )
       
  1086     {
       
  1087     TBuf8<KMaxIntegerLen> objectId;
       
  1088     objectId.Num( aObjectId );
       
  1089     return UpnpCdUtils::BuildContentUriL(
       
  1090         iContentDirectoryDb->GetKeyForUriL( ), aFileExt, objectId );
       
  1091     }
       
  1092 
       
  1093 // -----------------------------------------------------------------------------
       
  1094 // CUpnpContentDirectory::BuildImportUriL
       
  1095 // -----------------------------------------------------------------------------
       
  1096 HBufC8* CUpnpContentDirectory::BuildImportUriLC()
       
  1097     {
       
  1098     return UpnpCdUtils::BuildImportUriLC( iContentDirectoryDb->GetKeyForUriL( ) );
       
  1099     }
       
  1100 
       
  1101 // -----------------------------------------------------------------------------
       
  1102 // CUpnpContentDirectory::BuildImportUriShorterLC
       
  1103 // -----------------------------------------------------------------------------
       
  1104 HBufC8* CUpnpContentDirectory::BuildImportUriShorterLC()
       
  1105     {
       
  1106     return UpnpCdUtils::BuildImportUriShorterLC( iContentDirectoryDb->GetKeyForUriL( ) );
       
  1107     }
       
  1108 
       
  1109 // -----------------------------------------------------------------------------
       
  1110 // CUpnpContentDirectory::InsertRealIpAndPortL
       
  1111 // -----------------------------------------------------------------------------
       
  1112 void CUpnpContentDirectory::InsertRealIpAndPortL( TDes8& aString )
       
  1113     {
       
  1114     // prepare pettern
       
  1115     HBufC8* pattern = HBufC8::NewLC( KHttpTag().Length( ) + KIpPortPlaceholder8().Length( ) );
       
  1116     TPtr8 patternPtr(pattern->Des( ));
       
  1117     patternPtr.Append( KHttpTag );
       
  1118     patternPtr.Append( KIpPortPlaceholder8 );
       
  1119 
       
  1120     // repare address
       
  1121     HBufC8* address = GetIpAndPortDes8L( );
       
  1122     CleanupStack::PushL( address );
       
  1123 
       
  1124     TInt pos = aString.Find( patternPtr );
       
  1125 
       
  1126     while ( pos != KErrNotFound )
       
  1127         {
       
  1128         aString.Replace( pos + KHttpTag().Length( ), KIpPortPlaceholder8().Length( ), *address );
       
  1129         pos = aString.Find( patternPtr );
       
  1130         }
       
  1131 
       
  1132     // clean up
       
  1133     CleanupStack::PopAndDestroy( address );
       
  1134     CleanupStack::PopAndDestroy( pattern );
       
  1135     }
       
  1136 
       
  1137 // -----------------------------------------------------------------------------
       
  1138 // CUpnpContentDirectory::InsertIpAndPortPlaceholderL
       
  1139 // -----------------------------------------------------------------------------
       
  1140 HBufC8* CUpnpContentDirectory::InsertIpAndPortPlaceholderL(
       
  1141     const TDesC8& aString )
       
  1142     {
       
  1143     HBufC8* ipAndPort = GetIpAndPortDes8L( );
       
  1144     CleanupStack::PushL( ipAndPort );
       
  1145 
       
  1146     HBufC8* ret = aString.AllocLC( );
       
  1147 
       
  1148     TInt pos = KErrNotFound;
       
  1149     while ( (pos = ret->Find( *ipAndPort )) != KErrNotFound )
       
  1150         {
       
  1151         // realloc if too small
       
  1152         if ( ipAndPort->Length( ) < KIpPortPlaceholder8().Length( ) )
       
  1153             {
       
  1154             HBufC8* tmp = ret->ReAllocL( ret->Length( )
       
  1155                     + KIpPortPlaceholder8().Length( ) - ipAndPort->Length( ) );
       
  1156             CleanupStack::Pop( ret );
       
  1157             ret = tmp;
       
  1158             CleanupStack::PushL( ret );
       
  1159             }
       
  1160         ret->Des().Replace( pos, ipAndPort->Length( ), KIpPortPlaceholder8 );
       
  1161         }
       
  1162     // clean up
       
  1163     CleanupStack::Pop( ret );
       
  1164     CleanupStack::PopAndDestroy( ipAndPort );
       
  1165 
       
  1166     return ret;
       
  1167     }
       
  1168 
       
  1169 // -----------------------------------------------------------------------------
       
  1170 // CUpnpContentDirectory::GetAddress
       
  1171 // -----------------------------------------------------------------------------
       
  1172 TInetAddr CUpnpContentDirectory::GetAddress()
       
  1173     {
       
  1174     TInetAddr resourceServerAddress;    
       
  1175     iHttpServerSession->GetAddress( resourceServerAddress );
       
  1176     return resourceServerAddress;
       
  1177     }
       
  1178 
       
  1179 // -----------------------------------------------------------------------------
       
  1180 // CUpnpContentDirectory::AutoDestroyObjectList
       
  1181 // -----------------------------------------------------------------------------
       
  1182 CUpnpAutoDestroyObjectList* CUpnpContentDirectory::GetAutoDestroyObjectList()
       
  1183     {
       
  1184     return iAutoDestroyObjects;
       
  1185     }
       
  1186 
       
  1187 // -----------------------------------------------------------------------------
       
  1188 // CUpnpContentDirectory::GetIpAndPortDes8L
       
  1189 // -----------------------------------------------------------------------------
       
  1190 HBufC8* CUpnpContentDirectory::GetIpAndPortDes8L()
       
  1191     {
       
  1192     TInetAddr resourceServerAddress = GetAddress();        
       
  1193     
       
  1194     TBuf<KMaxName> addrTmp;
       
  1195     resourceServerAddress.Output( addrTmp );
       
  1196     HBufC8* addr = UpnpString::FromUnicodeL( addrTmp );
       
  1197     CleanupStack::PushL( addr );
       
  1198     
       
  1199     TInt portNo = resourceServerAddress.Port();
       
  1200     TBuf8<KMaxName> port;
       
  1201     port.Num( portNo );
       
  1202     
       
  1203     HBufC8* temp = HBufC8::NewLC( addr->Length( ) + KColon8().Length( ) + port.Length( ) );
       
  1204     TPtr8 tempPtr( temp->Des() );
       
  1205     tempPtr.Copy( *addr );
       
  1206     tempPtr.Append( KColon8 );
       
  1207     tempPtr.Append( port );
       
  1208 
       
  1209     CleanupStack::Pop( temp );
       
  1210     CleanupStack::PopAndDestroy( addr );
       
  1211 
       
  1212     return temp;
       
  1213     }
       
  1214 
       
  1215 // -----------------------------------------------------------------------------
       
  1216 // CUpnpContentDirectory::GetSearchCapabilitiesL
       
  1217 // -----------------------------------------------------------------------------
       
  1218 //
       
  1219 TUpnpErrorCode CUpnpContentDirectory::GetSearchCapabilitiesL(
       
  1220     CUpnpAction*& aAction )
       
  1221     {
       
  1222     aAction->SetArgumentL( KSearchCaps,
       
  1223         StateVariableValue( KSearchCapsVariableName ) );
       
  1224     return EHttpOk;
       
  1225     }
       
  1226 
       
  1227 // -----------------------------------------------------------------------------
       
  1228 // CUpnpContentDirectory::GetSortCapabilitiesL
       
  1229 // -----------------------------------------------------------------------------
       
  1230 //
       
  1231 TUpnpErrorCode CUpnpContentDirectory::GetSortCapabilitiesL(
       
  1232     CUpnpAction*& aAction )
       
  1233     {
       
  1234     aAction->SetArgumentL( KSortCaps,
       
  1235         StateVariableValue( KSortCapsVariableName ) );
       
  1236     return EHttpOk;
       
  1237     } 
       
  1238  
       
  1239 // -----------------------------------------------------------------------------
       
  1240 // CUpnpContentDirectory::GetSystemUpdateIdL
       
  1241 // -----------------------------------------------------------------------------
       
  1242 //
       
  1243 TUpnpErrorCode CUpnpContentDirectory::GetSystemUpdateIdL(
       
  1244     CUpnpAction*& aAction )
       
  1245     {
       
  1246     const TPtrC8 idValue = StateVariableValue( (TDesC8&)KSystemUpdateID );
       
  1247     aAction->SetArgumentL( KId, idValue );
       
  1248     return EHttpOk;
       
  1249     }
       
  1250 
       
  1251 // -----------------------------------------------------------------------------
       
  1252 // CUpnpContentDirectory::ImportResourceL
       
  1253 // -----------------------------------------------------------------------------
       
  1254 //
       
  1255 TUpnpErrorCode CUpnpContentDirectory::ImportResourceL( CUpnpAction*& aAction )
       
  1256     {
       
  1257     if ( iTransferController->IsMaxImportExportTransfers( ) )
       
  1258         {
       
  1259         return ETransferBusy;
       
  1260         }
       
  1261 
       
  1262     TPtrC8 src = aAction->ArgumentValue( (TDesC8&) KSourceURI );
       
  1263     TPtrC8 dest = aAction->ArgumentValue( (TDesC8&) KDestinationURI );
       
  1264     TUriParser8 srcParser;
       
  1265     TUriParser8 dstParser;
       
  1266 
       
  1267     if ( !(dstParser.Parse( dest ) == 
       
  1268           KErrNone && dstParser.Extract( EUriScheme ).CompareF( UpnpHTTP::KSchemeHTTP8 ) == 0 ) )
       
  1269         {
       
  1270         User::Leave( ENoDestinationResource );
       
  1271         }
       
  1272 
       
  1273     // there are placeholders instead the real values in the database
       
  1274     HBufC8* destBuf = InsertIpAndPortPlaceholderL( dest );
       
  1275     CleanupStack::PushL( destBuf );
       
  1276 
       
  1277     TInt objId;
       
  1278     // object existence
       
  1279     if ( (objId
       
  1280             = iContentDirectoryDb->GetObjIdByAttrL( KImportUri8, *destBuf ) )
       
  1281             == KErrNotFound )
       
  1282         {
       
  1283         User::Leave( ENoDestinationResource );
       
  1284         }
       
  1285     // object restrictions
       
  1286     if ( iContentDirectoryDb->CheckObjectRestrictionL( objId )
       
  1287             == ERestrictedObject )
       
  1288         {
       
  1289         User::Leave( ERestrictedObject );
       
  1290         }
       
  1291     // parent restricions
       
  1292     if ( iContentDirectoryDb->CheckParentRestrictionL( objId ) != EUpnpOk )
       
  1293         {
       
  1294         User::Leave( ERestrictedParentObject );
       
  1295         }
       
  1296 
       
  1297     TInt destinationCut = dest.LocateReverse( '/' );
       
  1298     if ( destinationCut < KErrNone )
       
  1299         {
       
  1300         User::Leave( EInvalidArgs );
       
  1301         }
       
  1302 
       
  1303     HBufC* name = GetFileNameL( objId );
       
  1304     CleanupStack::PushL( name );
       
  1305     if ( !name )
       
  1306         {
       
  1307         User::Leave( ENoDestinationResource );
       
  1308         }
       
  1309 
       
  1310     for ( TInt i( 0); i < iTransferController->iFileTransfers.Count( ) ; i++ )
       
  1311         {
       
  1312         if ( iTransferController->iFileTransfers[i]->ObjectId( ) == objId
       
  1313                 && iTransferController->iFileTransfers[i]->TransferState( )
       
  1314                         == ETransferInProgress )
       
  1315             {
       
  1316             User::Leave( EDestinationAccess );
       
  1317             }
       
  1318         }
       
  1319 
       
  1320     HBufC8* name8 = UpnpString::FromUnicodeL( *name );
       
  1321     CleanupStack::PushL( name8 );
       
  1322 
       
  1323     TPtr8 fileNamePtr(name8->Des( ) );
       
  1324 
       
  1325     HBufC16* fileNamePtr16 = UpnpString::ToUnicodeL( fileNamePtr );
       
  1326     CleanupStack::PushL( fileNamePtr16 );
       
  1327     TParse parseFilename;
       
  1328     parseFilename.Set( fileNamePtr16->Des( ), NULL, NULL );
       
  1329     HBufC16* diskNameTemp=parseFilename.Drive().Alloc( );
       
  1330     HBufC16* diskName = diskNameTemp->ReAlloc( diskNameTemp->Des().Length( ) +
       
  1331                                                KDiskPathElSep().Length( ) );
       
  1332     CleanupStack::PushL( diskName );
       
  1333     TPtr16 diskNamePtr = diskName->Des( );
       
  1334     diskNamePtr.Append( KDiskPathElSep );
       
  1335     if ( !BaflUtils::PathExists( iFs, diskNamePtr ) )
       
  1336         {
       
  1337         User::Leave( ENoDestinationResource );
       
  1338         }
       
  1339 
       
  1340     UpnpString::ReplaceHttpCharacters( fileNamePtr );
       
  1341 
       
  1342     // forbidden chars
       
  1343     UpnpCdUtils::ValidateFilePath( fileNamePtr );
       
  1344 
       
  1345     HBufC* targetPath = HBufC::NewMaxLC( fileNamePtr.Length( ) );
       
  1346     TPtr targetPtr = targetPath->Des( );
       
  1347     targetPtr.Copy( fileNamePtr );
       
  1348 
       
  1349     TInt transferid = CUpnpHttpMessage::NewSessionIdL( );
       
  1350     iTransferHandler->DownloadFileL( transferid, src, targetPtr );
       
  1351 
       
  1352     CleanupStack::PopAndDestroy( targetPath );
       
  1353 
       
  1354     TransferStartL( dest );
       
  1355 
       
  1356     TBuf8<KMaxIntegerLen> tid;
       
  1357     tid.Num( transferid );
       
  1358     aAction->SetArgumentL( (TDesC8&)KTransferID, tid );
       
  1359 
       
  1360     CUpnpFileTransfer* transfer = CUpnpFileTransfer::NewLC( transferid,
       
  1361         objId, CUpnpFileTransfer::EImport );
       
  1362 
       
  1363     transfer->SetTransferState(:: ETransferInProgress );
       
  1364 
       
  1365     // starting to get the number if import
       
  1366     TPtrC8 dest1 = aAction->ArgumentValue( (TDesC8&) KDestinationURI );
       
  1367     TInt posOfLastSlash = dest1.LocateReverse( '/' );
       
  1368     if( posOfLastSlash != KErrNotFound )
       
  1369         {
       
  1370         dest1.Set( dest1.Mid( posOfLastSlash + 1 ) );
       
  1371         // converting value
       
  1372         TLex8 string( dest1 );
       
  1373         TInt value( 0 );
       
  1374         TInt error = string.Val( value );
       
  1375         // conversion ok, we can add this to 
       
  1376         if( error == KErrNone )
       
  1377             {
       
  1378             transfer->SetImportNumber( value );
       
  1379             }
       
  1380         }
       
  1381     //add transfer to transfers list
       
  1382     CleanupStack::Pop( transfer );
       
  1383     iTransferController->iFileTransfers.Append( transfer );
       
  1384 
       
  1385     HBufC* fileName16 = UpnpString::ToUnicodeL( *name8 );
       
  1386     CleanupStack::PushL( fileName16 );
       
  1387 
       
  1388     CUpnpFileTransferEvent* event = CUpnpFileTransferEvent::NewLC();
       
  1389     event->SetProgressState( CUpnpFileTransferEvent::ECompleted );
       
  1390     event->SetDirection( CUpnpFileTransferEvent::EIncoming );
       
  1391     event->SetFilePathL( *fileName16 );
       
  1392     event->SetContentDirectoryObjectId( objId );
       
  1393     event->SetTransferId( transferid );
       
  1394     CleanupStack::Pop( event );
       
  1395     transfer->SetEvent( event );
       
  1396 
       
  1397     AddFileTransferToTransferIDsStateVariableL( transferid );
       
  1398     // clean up
       
  1399     CleanupStack::PopAndDestroy( fileName16 );
       
  1400     CleanupStack::PopAndDestroy( diskName );
       
  1401     CleanupStack::PopAndDestroy( fileNamePtr16 );
       
  1402     CleanupStack::PopAndDestroy( name8 );
       
  1403     CleanupStack::PopAndDestroy( name );
       
  1404     CleanupStack::PopAndDestroy( destBuf );
       
  1405 
       
  1406     return EHttpOk;
       
  1407     }
       
  1408 
       
  1409 // -----------------------------------------------------------------------------
       
  1410 // CUpnpContentDirectory::ExportResourceL
       
  1411 // -----------------------------------------------------------------------------
       
  1412 //
       
  1413 TUpnpErrorCode CUpnpContentDirectory::ExportResourceL( CUpnpAction*& aAction )
       
  1414     {
       
  1415     if ( iTransferController->IsMaxImportExportTransfers( ) )
       
  1416         {
       
  1417         return ETransferBusy;
       
  1418         }
       
  1419     HBufC8* srcBuf = aAction->ArgumentValue( KSourceURI ).AllocLC( );
       
  1420     TPtr8 src(srcBuf->Des( ));
       
  1421     UpnpString::ReplaceHttpCharacters( src );
       
  1422 
       
  1423     HBufC8* ipAndPort = GetIpAndPortDes8L( );
       
  1424     CleanupStack::PushL( ipAndPort );
       
  1425     UpnpCdUtils::ValidateSrcUriL( src, *ipAndPort );
       
  1426     CleanupStack::PopAndDestroy( ipAndPort );
       
  1427 
       
  1428     TPtrC8 dest = aAction->ArgumentValue( KDestinationURI );
       
  1429 
       
  1430     // check src existence -----------------------------
       
  1431     CUpnpResourcesBean* rscBean = NULL;
       
  1432     TRAPD(
       
  1433         error,
       
  1434         rscBean
       
  1435                 = iContentDirectoryDb->GetResourceL( UpnpCdUtils::ResIdFromUriL( src ) ) );
       
  1436     if ( error < 0 || !rscBean )
       
  1437         {
       
  1438         User::Leave( ENoSourceResource );
       
  1439         }
       
  1440     CleanupStack::PushL( rscBean );
       
  1441     // check whether resource and src uri file names matches
       
  1442     TPtrC rscPath(rscBean->Path( ) );
       
  1443 
       
  1444     if ( !BaflUtils::FileExists( iFs, rscPath ) )
       
  1445         {
       
  1446         User::Leave( ENoSourceResource );
       
  1447         }
       
  1448 
       
  1449     TParse rscParse;
       
  1450     rscParse.Set( rscPath, NULL, NULL );
       
  1451     TPtrC rscFileName = rscParse.NameAndExt( );
       
  1452 
       
  1453     TBuf8<KMaxIntegerLen> objId;
       
  1454     objId.Num( rscBean->ObjectId( ) );
       
  1455 
       
  1456     TPtrC rscFileExt = rscParse.Ext( );
       
  1457     HBufC8* srcNoEsc = src.AllocLC( );
       
  1458     TPtr8 srcNoEscPtr(srcNoEsc->Des( ));
       
  1459 
       
  1460     HBufC* srcPath = UpnpCdUtils::Des8ToDesLC( srcNoEscPtr );
       
  1461     TPtr srcPathPtr(srcPath->Des( ) );
       
  1462 
       
  1463     TInt position = srcPathPtr.LocateReverse( '/' );
       
  1464     HBufC* newFileName = HBufC::NewLC( KMaxIntegerLen + rscFileExt.Length( ) );
       
  1465     newFileName->Des().Copy( objId );
       
  1466     newFileName->Des().Append( rscFileExt );
       
  1467     if ( position )
       
  1468         {
       
  1469         position = srcPathPtr.Length( )-position-1;
       
  1470         TPtrC srcFilePtr = srcPathPtr.Right( position );
       
  1471         if ( srcFilePtr.Compare( *newFileName ) )
       
  1472             {
       
  1473             User::Leave( ENoSourceResource );
       
  1474             }
       
  1475         }
       
  1476     if ( srcPathPtr.Find( *newFileName ) == KErrNotFound )
       
  1477         {
       
  1478         User::Leave( ENoSourceResource );
       
  1479         }
       
  1480     // get mimeType = Content-Type in the header
       
  1481     HBufC8* contentType = UpnpFileUtil::GetMimeTypeForFileL( *newFileName );
       
  1482     TPtrC8 contentTypePtr(contentType->Des( ) );
       
  1483 
       
  1484     CleanupStack::PopAndDestroy( newFileName );
       
  1485     CleanupStack::PushL( contentType );
       
  1486 
       
  1487     //checking number of slash in sourceURI 
       
  1488     TInt numberOfSlash(0);
       
  1489     position = srcPathPtr.LocateReverse( '/' );
       
  1490     while ( position>0 )
       
  1491         {
       
  1492         numberOfSlash++;
       
  1493         if ( numberOfSlash>KMaxSlashAmount )
       
  1494             {
       
  1495             User::Leave( ENoSourceResource );
       
  1496             }
       
  1497         srcPathPtr = srcPathPtr.Left( position );
       
  1498         position = srcPathPtr.LocateReverse( '/' );
       
  1499         }
       
  1500 
       
  1501     TInt transferid = CUpnpHttpMessage::NewSessionIdL( );
       
  1502     iTransferHandler->UploadFileL( transferid, dest, rscPath, contentTypePtr );
       
  1503 
       
  1504     TBuf8<KMaxIntegerLen> tid;
       
  1505     tid.Num( transferid );
       
  1506     aAction->SetArgumentL( (TDesC8&)KTransferID, tid );
       
  1507 
       
  1508     TPtrC8 pert = UpnpCdUtils::ResIdFromUriDesL( src );
       
  1509 
       
  1510     TInt objectId = rscBean->ObjectId( );
       
  1511     CUpnpFileTransfer* transfer = CUpnpFileTransfer::NewL( transferid,
       
  1512         objectId, CUpnpFileTransfer::EExport );
       
  1513     transfer->SetTransferState(:: ETransferInProgress);
       
  1514 
       
  1515     iTransferController->iFileTransfers.Append( transfer );
       
  1516 
       
  1517     CUpnpFileTransferEvent* event = CUpnpFileTransferEvent::NewLC();
       
  1518     event->SetProgressState( CUpnpFileTransferEvent::ECompleted );
       
  1519     event->SetDirection( CUpnpFileTransferEvent::EOutgoing );
       
  1520     event->SetFilePathL( rscPath);
       
  1521     event->SetContentDirectoryObjectId( objectId );
       
  1522     event->SetTransferId( transferid );
       
  1523     CleanupStack::Pop( event );
       
  1524     transfer->SetEvent(event);
       
  1525 
       
  1526     AddFileTransferToTransferIDsStateVariableL( transferid );
       
  1527     CleanupStack::PopAndDestroy( contentType );
       
  1528     CleanupStack::PopAndDestroy( srcPath );
       
  1529     CleanupStack::PopAndDestroy( srcNoEsc );
       
  1530     CleanupStack::PopAndDestroy( rscBean );
       
  1531     CleanupStack::PopAndDestroy( srcBuf );
       
  1532 
       
  1533     return EHttpOk;
       
  1534     }
       
  1535 
       
  1536 // -----------------------------------------------------------------------------
       
  1537 // CUpnpContentDirectory::StopTransferResourceL
       
  1538 // -----------------------------------------------------------------------------
       
  1539 //
       
  1540 TUpnpErrorCode CUpnpContentDirectory::StopTransferResourceL(
       
  1541     CUpnpAction*& aAction )
       
  1542     {
       
  1543     TInt transferId;
       
  1544     TInt err =
       
  1545             UpnpString::StringToInt(
       
  1546                 (TDesC8&) aAction->ArgumentValue( (TDesC8&)KTransferID ),
       
  1547                 &transferId );
       
  1548     if ( KErrNone != err )
       
  1549         {
       
  1550         User::Leave( ENoFileTransfer );
       
  1551         }
       
  1552     TUpnpErrorCode
       
  1553             upnpErr(iTransferController->StopTransferResourceL( transferId ) );
       
  1554     TransferCompletedL( transferId, KErrCancel );
       
  1555     return upnpErr;
       
  1556     }
       
  1557 
       
  1558 // -----------------------------------------------------------------------------
       
  1559 // CUpnpContentDirectory::GetTransferProgressL
       
  1560 // -----------------------------------------------------------------------------
       
  1561 //
       
  1562 TUpnpErrorCode CUpnpContentDirectory::GetTransferProgressL(
       
  1563     CUpnpAction*& aAction )
       
  1564     {
       
  1565     TUint transferId;
       
  1566     TInt length( KErrNone );
       
  1567     TInt totalLength( KErrNone );
       
  1568     // TInt lenght is max 15 digits, KMaxInfoName is 16
       
  1569     TBuf8<KMaxInfoName> res;
       
  1570 
       
  1571     TInt err = UpnpCdUtils::StringToTUint(
       
  1572         (TDesC8&)aAction->ArgumentValue( (TDesC8&)KTransferID ), &transferId );
       
  1573 
       
  1574     if ( KErrNone != err )
       
  1575         {
       
  1576         User::Leave( EInvalidArgs );
       
  1577         }
       
  1578     if ( transferId > KMaxTInt )
       
  1579         {
       
  1580         User::Leave( ENoFileTransfer );
       
  1581         }
       
  1582     TInt transferFound = iTransferController->Transfer( transferId );
       
  1583     if ( KErrNotFound == transferFound )
       
  1584         {
       
  1585         User::Leave( ENoFileTransfer );
       
  1586         }
       
  1587 
       
  1588     switch ( (iTransferController->iFileTransfers[ transferFound ])->TransferState( ) )
       
  1589         {
       
  1590         case:: ETransferInProgress:
       
  1591             {
       
  1592             aAction->SetArgumentL( (TDesC8&)KTransferStatus, KInProgress );
       
  1593             break;
       
  1594             }
       
  1595         case EStopped:
       
  1596             {
       
  1597             aAction->SetArgumentL( (TDesC8&)KTransferStatus, KStopped );
       
  1598             break;
       
  1599             }
       
  1600         case EError:
       
  1601             {
       
  1602             aAction->SetArgumentL( (TDesC8&)KTransferStatus, KError );
       
  1603             break;
       
  1604             }
       
  1605         case ECompleted:
       
  1606             {
       
  1607             aAction->SetArgumentL( (TDesC8&)KTransferStatus, KCompleted );
       
  1608             break;
       
  1609             }
       
  1610         default:
       
  1611             {
       
  1612             break;
       
  1613             }
       
  1614         }
       
  1615     length = (iTransferController->iFileTransfers[ transferFound ])->TransferLength();
       
  1616     totalLength = (iTransferController->iFileTransfers[ transferFound ])->TransferTotal();
       
  1617 
       
  1618     res.Num( length );
       
  1619     aAction->SetArgumentL( (TDesC8&)KTransferLength, res );
       
  1620     res.Num( totalLength );
       
  1621     aAction->SetArgumentL( (TDesC8&)KTransferTotal, res );
       
  1622     return EHttpOk;
       
  1623     }
       
  1624 
       
  1625 // -----------------------------------------------------------------------------
       
  1626 // CUpnpContentDirectory::DeleteResourceL
       
  1627 // -----------------------------------------------------------------------------
       
  1628 //
       
  1629 TUpnpErrorCode CUpnpContentDirectory::DeleteResourceL( CUpnpAction*& aAction )
       
  1630     {
       
  1631     TPtrC8 uri8(aAction->ArgumentValue( KResourceURI( ) ));
       
  1632 
       
  1633     if ( !uri8.Length( ) )
       
  1634         User::Leave( ENoSourceResource );
       
  1635 
       
  1636     // replace http characters
       
  1637     HBufC8* uri = uri8.AllocLC( );
       
  1638     TPtr8 uriPtr(uri->Des( ));
       
  1639 
       
  1640     // ip placeholder
       
  1641     HBufC8* internalUri = InsertIpAndPortPlaceholderL( uriPtr );
       
  1642     CleanupStack::PushL( internalUri );
       
  1643     // delete resource
       
  1644     RArray<TInt> contIds;
       
  1645     CleanupClosePushL( contIds );
       
  1646     iContentDirectoryDb->DeleteResourceL( *internalUri, contIds );
       
  1647 
       
  1648     // updateIDs
       
  1649     for ( TInt i = 0; i < contIds.Count( ); i++ )
       
  1650         {
       
  1651         TBuf8<KMaxIntegerLen> num;
       
  1652         num.Num( contIds[i] );
       
  1653         iStateHandler->IncreaseContainerUpdateIdL( num );
       
  1654         }
       
  1655 
       
  1656     // systemUpdateId
       
  1657     iStateHandler->IncreaseSystemUpdateIdL( );
       
  1658 
       
  1659     // clean up
       
  1660     CleanupStack::PopAndDestroy( &contIds );
       
  1661     CleanupStack::PopAndDestroy( internalUri );
       
  1662     CleanupStack::PopAndDestroy( uri );
       
  1663 
       
  1664     return EHttpOk;
       
  1665     }
       
  1666 
       
  1667 // -----------------------------------------------------------------------------
       
  1668 // CUpnpContentDirectory::AddFileTransferL
       
  1669 // -----------------------------------------------------------------------------
       
  1670 //
       
  1671 void CUpnpContentDirectory::AddFileTransferToTransferIDsStateVariableL(
       
  1672     TInt aTransferID )
       
  1673     {
       
  1674     TBuf8<KMaxIntegerLen> tid;
       
  1675     tid.Format( UpnpCD::KOneNumber, aTransferID );
       
  1676     TPtrC8 tmp = StateVariableValue( KTransferIDs( ) );
       
  1677     HBufC8* buf = UpnpString::AddValueToCsvL( tmp, tid );
       
  1678     CleanupStack::PushL( buf );
       
  1679     SetStateVariableL( KTransferIDs( ), *buf, EFalse );
       
  1680     CleanupStack::PopAndDestroy( buf );
       
  1681     }
       
  1682 
       
  1683 // -----------------------------------------------------------------------------
       
  1684 // CUpnpContentDirectory::RemoveFileTransferL
       
  1685 // -----------------------------------------------------------------------------
       
  1686 //
       
  1687 void CUpnpContentDirectory::RemoveFileTransferFromTransferIDsStateVariableL(
       
  1688     TInt aTransferID )
       
  1689     {
       
  1690     TBuf8<KMaxIntegerLen> tid;
       
  1691     tid.Format( UpnpCD::KOneNumber, aTransferID );
       
  1692     TPtrC8 tmp = StateVariableValue( KTransferIDs( ) );
       
  1693     HBufC8* buf = UpnpString::RemoveFromCsvLC( tmp, tid );
       
  1694     SetStateVariableL( KTransferIDs( ), *buf, EFalse );
       
  1695     CleanupStack::PopAndDestroy( buf );
       
  1696     }
       
  1697 
       
  1698 // -----------------------------------------------------------------------------
       
  1699 // CUpnpContentDirectory::ContainerUpdateId
       
  1700 // -----------------------------------------------------------------------------
       
  1701 //
       
  1702 TInt CUpnpContentDirectory::ContainerUpdateId( const TDesC8& aContainerID )
       
  1703     {
       
  1704     return iStateHandler->ContainerUpdateId( aContainerID );
       
  1705     }
       
  1706 
       
  1707 // -----------------------------------------------------------------------------
       
  1708 // CUpnpContentDirectory::CreateObjectL
       
  1709 // -----------------------------------------------------------------------------
       
  1710 //
       
  1711 TUpnpErrorCode CUpnpContentDirectory::CreateObjectL( CUpnpAction*& aAction )
       
  1712     {
       
  1713     SetExecutedAction( ECreateObjectAction );
       
  1714 
       
  1715     HBufC8* objNoDes(aAction->ArgumentValue(KContainerID()).AllocLC( ) );
       
  1716     TPtr8 objNoPtr(objNoDes->Des( ));
       
  1717     objNoPtr.TrimAll( );
       
  1718 
       
  1719     TInt objNo = KErrNotFound; // container id
       
  1720     TInt error( KErrGeneral );
       
  1721 
       
  1722     // any container support
       
  1723     if ( !objNoPtr.Compare( KAnyContainerIndicator8 ) )
       
  1724         {
       
  1725         objNo = KAnyContainerId;
       
  1726         }
       
  1727     else
       
  1728         {
       
  1729         // checking whether containerID has proper decimal value
       
  1730         error = UpnpCdUtils::StringToInteger( objNoPtr, &objNo );
       
  1731         if ( error != KErrNone )
       
  1732             {
       
  1733             User::Leave( ENoContainer );
       
  1734             }
       
  1735 
       
  1736         TUpnpErrorCode err =
       
  1737                 iContentDirectoryDb->CheckObjectRestrictionL( objNo );
       
  1738         if ( err != EUpnpOk )
       
  1739             {
       
  1740             CleanupStack::PopAndDestroy( objNoDes );
       
  1741             if ( err == ENoSuchObject )
       
  1742                 err = ENoContainer;
       
  1743             if ( err == ERestrictedObject )
       
  1744                 err = ERestrictedParentObject;
       
  1745             return err;
       
  1746             }
       
  1747         }
       
  1748     CleanupStack::PopAndDestroy( objNoDes );
       
  1749 
       
  1750     // now we have found the unrestricted parent container
       
  1751     // let's inspect the element that should be added
       
  1752     // decode "elements" argument of the create action
       
  1753     const TDesC8& elem = (TDesC8&)aAction->ArgumentValue( KElements( ) );
       
  1754 
       
  1755     // check that we've got correctly formed xml
       
  1756     if ( elem.FindF( KXmlNsDc( ) ) == KErrNotFound )
       
  1757         {
       
  1758         User::Leave( EBadMetadata );
       
  1759         }
       
  1760 
       
  1761     HBufC8* trimmed = UpnpString::TrimLC( elem, EFalse );
       
  1762 
       
  1763     HBufC8* trimmedlf8 = UpnpString::StringReplaceL( *trimmed, KLF8,
       
  1764         KNullString8 );
       
  1765     CleanupStack::PushL( trimmedlf8 );
       
  1766     // parse decoded buffer
       
  1767     RXmlEngDocument fragment;
       
  1768     TRAPD( xmlerr, fragment = XmlFragmentL( *trimmedlf8 ) );
       
  1769     if ( xmlerr )
       
  1770         {
       
  1771         RDebug::Print( _L( "CUpnpContentDirectory::CreateObjectL ERROR IN XML" ) );
       
  1772         User::Leave( xmlerr );
       
  1773         }
       
  1774     CleanupClosePushL( fragment );
       
  1775 
       
  1776     // if container, check the number of nested levels
       
  1777     TXmlEngElement container;
       
  1778     UpnpDomInterface::GetElementL( fragment.DocumentElement( ), container,
       
  1779         KContainer );
       
  1780     if ( container.NotNull( ) && !iContentDirectoryDb->CanBeNestedL( objNo ) )
       
  1781         {
       
  1782         User::Leave( ECannotProcess );
       
  1783         }
       
  1784 
       
  1785     RArray<TXmlEngAttr> importUris;
       
  1786     CleanupClosePushL( importUris );
       
  1787     RArray<TXmlEngElement> ress;
       
  1788     CleanupClosePushL( ress );
       
  1789 
       
  1790     TBool isLocal = aAction->Local( );
       
  1791 
       
  1792     // to avoid errors in validation process
       
  1793     UpnpCdUtils::SetObjectIdL( UpnpCdUtils::GetObjectElementL( fragment ),
       
  1794         objNo ); // value does not matter - it will be changed later
       
  1795     UpnpCdUtils::SetContainerIdL( UpnpCdUtils::GetObjectElementL( fragment ),
       
  1796         objNo );
       
  1797 
       
  1798     //created new object shouldn't have refID value
       
  1799     if ( UpnpCdUtils::HasRefIdL( fragment ) )
       
  1800         {
       
  1801         User::Leave( EParameterMismatch );
       
  1802         }
       
  1803 
       
  1804     // validate the new element
       
  1805     iElementdb->ValidateNewObjectL( fragment.DocumentElement( ), ETrue,
       
  1806         isLocal );
       
  1807 
       
  1808     // now we have fragment, lets add this to CD metadata
       
  1809     TInt id( KErrNotFound );
       
  1810     TUpnpErrorCode err = AddToMetaDbL( fragment, objNo, &id, isLocal );
       
  1811 
       
  1812     CleanupStack::PopAndDestroy( &ress );
       
  1813     CleanupStack::PopAndDestroy( &importUris );
       
  1814 
       
  1815     ConstructCreateResultsetL( aAction, fragment, id );
       
  1816 
       
  1817     CleanupStack::PopAndDestroy( &fragment );
       
  1818     CleanupStack::PopAndDestroy( trimmedlf8 );
       
  1819     CleanupStack::PopAndDestroy( trimmed );
       
  1820 
       
  1821     if ( err == EHttpOk )
       
  1822         {
       
  1823         iStateHandler->IncreaseSystemUpdateIdL( );
       
  1824         iStateHandler->IncreaseContainerUpdateIdL( 
       
  1825                              ( TDesC8& )aAction->ArgumentValue( KContainerID ) );
       
  1826 
       
  1827         }
       
  1828     return err;
       
  1829     }
       
  1830 
       
  1831 // -----------------------------------------------------------------------------
       
  1832 // CUpnpContentDirectory::HandleLocalResourcesL
       
  1833 // -----------------------------------------------------------------------------
       
  1834 //
       
  1835 void CUpnpContentDirectory::HandleLocalResourcesL( TXmlEngElement aRes,
       
  1836     TInt aObjectId )
       
  1837     {
       
  1838     // contentUri, contentPath          
       
  1839     TUriParser8 up;
       
  1840     TPtrC8 resVal = aRes.Text( );
       
  1841     if ( resVal.Length( ) )
       
  1842         {
       
  1843         TPtrC8 resValCandidatePtr(resVal);
       
  1844 
       
  1845         // There should be no slash at the end of URI, if so -> leave occurs
       
  1846         if ( resValCandidatePtr.Length( ) > 0 )
       
  1847             {
       
  1848             if ( !resValCandidatePtr.Right(1).Compare( KSlash8( ) ) )
       
  1849                 {
       
  1850                 User::Leave( EBadMetadata );
       
  1851                 }
       
  1852             }
       
  1853 
       
  1854         User::LeaveIfError( up.Parse( resValCandidatePtr ) );
       
  1855 
       
  1856         // get schema
       
  1857         TPtrC8 scheme(up.Extract( EUriScheme ));
       
  1858 
       
  1859         // only local sharing
       
  1860         if ( !scheme.Compare( UpnpHTTP::KSchemeFile8 ) )
       
  1861             {
       
  1862             HBufC* fn = UpnpString::ToUnicodeL( resValCandidatePtr );
       
  1863             CleanupStack::PushL( fn );
       
  1864 
       
  1865             TInt extPosition = fn->Des().LocateReverse( '.' );
       
  1866             TPtrC ext;
       
  1867             if ( extPosition>0 )
       
  1868                 ext.Set( fn->Des().Mid( extPosition ) );
       
  1869             else
       
  1870                 ext.Set( KNullString );
       
  1871 
       
  1872             HBufC8* contentUri = BuildContentUriL( ext, aObjectId );
       
  1873             CleanupStack::PushL( contentUri );
       
  1874 
       
  1875             HBufC* contentPath = up.GetFileNameL( );
       
  1876             CleanupStack::PushL( contentPath );
       
  1877 
       
  1878             // update res tag
       
  1879             aRes.SetTextL( *contentUri );
       
  1880 
       
  1881             // add resource to the database
       
  1882             CUpnpResourcesBean* resBn = CUpnpResourcesBean::NewLC( );
       
  1883             resBn->SetId( UpnpCdUtils::ResIdFromUriL( *contentUri ) );
       
  1884             resBn->SetPathL( *contentPath );
       
  1885             resBn->SetIsReadonly( ETrue );
       
  1886             resBn->SetIsThumbnail( EFalse );
       
  1887 
       
  1888             iContentDirectoryDb->AddResourceL( resBn, aObjectId );
       
  1889 
       
  1890             // clean up
       
  1891             CleanupStack::PopAndDestroy( resBn );
       
  1892             CleanupStack::PopAndDestroy( contentPath );
       
  1893             CleanupStack::PopAndDestroy( contentUri );
       
  1894             CleanupStack::PopAndDestroy( fn );
       
  1895             }
       
  1896         // else -> ignore
       
  1897         }
       
  1898     // else -> ignore
       
  1899     }
       
  1900 
       
  1901 // -----------------------------------------------------------------------------
       
  1902 // CUpnpContentDirectory::ConstructCreateResultsetL
       
  1903 // -----------------------------------------------------------------------------
       
  1904 //
       
  1905 void CUpnpContentDirectory::ConstructCreateResultsetL( CUpnpAction*& aAction,
       
  1906     RXmlEngDocument aDocument, TInt aId )
       
  1907     {
       
  1908     TBuf8<KMaxIntegerLen> num;
       
  1909     num.Num( aId );
       
  1910 
       
  1911     aAction->SetArgumentL( KObjectID, num );
       
  1912     TXmlEngElement elem = aDocument.DocumentElement( );
       
  1913 
       
  1914     HBufC8* buf = CUpnpXmlContentFilter::SerializeLC( aDocument, elem );
       
  1915 
       
  1916     TPtr8 bufPtr(buf->Des( ));
       
  1917     InsertRealIpAndPortL( bufPtr );
       
  1918     HBufC8* encoded = UpnpCdUtils::EncodeXmlStringL( bufPtr );
       
  1919     CleanupStack::PushL( encoded );
       
  1920 
       
  1921     aAction->SetArgumentL( KResult, *encoded );
       
  1922 
       
  1923     CleanupStack::PopAndDestroy( encoded );
       
  1924     CleanupStack::PopAndDestroy( buf );
       
  1925     }
       
  1926 
       
  1927 // -----------------------------------------------------------------------------
       
  1928 // CUpnpContentDirectory::XmlFragmentL
       
  1929 // -----------------------------------------------------------------------------
       
  1930 //
       
  1931 RXmlEngDocument CUpnpContentDirectory::XmlFragmentL( const TDesC8 &aBuffer )
       
  1932     {
       
  1933     //Create a parser
       
  1934     RXmlEngDOMParser parser;
       
  1935     User::LeaveIfError( parser.Open( iDOMImpl ) );
       
  1936     CleanupClosePushL( parser );
       
  1937     // DOM document
       
  1938     RXmlEngDocument docTree;
       
  1939 
       
  1940     if ( aBuffer.FindF( KXmlNsDc( ) ) == KErrNotFound )
       
  1941         {
       
  1942         HBufC8* withDidl = HBufC8::NewLC( KDidlTag().Length( ) + aBuffer.Length( ) +
       
  1943                                           KDidlEnd().Length( ) );
       
  1944         withDidl->Des().Append( KDidlTag( ) );
       
  1945         withDidl->Des().Append( aBuffer );
       
  1946         withDidl->Des().Append( KDidlEnd( ) );
       
  1947 
       
  1948         docTree = parser.ParseL( *withDidl );
       
  1949         CleanupStack::PopAndDestroy( withDidl );
       
  1950         }
       
  1951     else
       
  1952         {
       
  1953         docTree = parser.ParseL( aBuffer );
       
  1954         }
       
  1955     CleanupStack::PopAndDestroy( &parser );
       
  1956     return docTree;
       
  1957     }
       
  1958 
       
  1959 // -----------------------------------------------------------------------------
       
  1960 // CUpnpContentDirectory::GetMimeTypeLC
       
  1961 // -----------------------------------------------------------------------------
       
  1962 //
       
  1963 HBufC8* CUpnpContentDirectory::GetMimeTypeLC( const TDesC8& aFilename )
       
  1964     {
       
  1965     HBufC16* filename16 = UpnpString::ToUnicodeL( aFilename );
       
  1966     CleanupStack::PushL( filename16 );
       
  1967     HBufC8* mimetype = UpnpFileUtil::GetMimeTypeForFileL( *filename16 );
       
  1968     if ( !mimetype )
       
  1969         {
       
  1970         mimetype = KNullString8().AllocL( );
       
  1971         }
       
  1972     CleanupStack::PopAndDestroy( filename16 );
       
  1973     CleanupStack::PushL( mimetype );
       
  1974     return mimetype;
       
  1975     }
       
  1976 
       
  1977 // -----------------------------------------------------------------------------
       
  1978 // CUpnpContentDirectory::ChangeFileNameWithExtensionL
       
  1979 // -----------------------------------------------------------------------------
       
  1980 HBufC8* CUpnpContentDirectory::ChangeFileNameWithExtensionL(
       
  1981     TDesC8& aFilePath, const TDesC8& aMimeType )
       
  1982     {
       
  1983 
       
  1984     HBufC8* temp = NULL;
       
  1985     TInt position = aFilePath.LocateReverse( '.' );
       
  1986     if ( position != KErrNotFound )
       
  1987         {
       
  1988         return temp;
       
  1989         }
       
  1990     if ( aMimeType == KNullDesC8 )
       
  1991         {
       
  1992         return temp;
       
  1993         }
       
  1994 
       
  1995     HBufC8* ext = iMimeToExtMap->GetExtensionL( aMimeType );
       
  1996     if ( ext == NULL )
       
  1997         {
       
  1998         return temp;
       
  1999         }
       
  2000     CleanupStack::PushL( ext );
       
  2001     HBufC8* newFilename = HBufC8::NewLC( ext->Length( ) + 1
       
  2002             + aFilePath.Length( ) );
       
  2003     TPtr8 fileNamePtr(newFilename->Des( ) );
       
  2004     fileNamePtr.Append( aFilePath );
       
  2005     //fileNamePtr.Append( KDot8());
       
  2006     fileNamePtr.Append( *ext );
       
  2007 
       
  2008     HBufC* oldFilename16 = HBufC::NewLC( aFilePath.Length( ) );
       
  2009     TPtr oldFilename16Ptr(oldFilename16->Des( ));
       
  2010     oldFilename16Ptr.Copy( aFilePath );
       
  2011 
       
  2012     HBufC* newFilename16 = HBufC::NewLC( newFilename->Length( ) );
       
  2013     TPtr newFilename16Ptr(newFilename16->Des( ));
       
  2014     newFilename16Ptr.Copy( *newFilename );
       
  2015 
       
  2016     TBool exists = BaflUtils::FileExists( iFs, *newFilename16 );
       
  2017     if ( exists )
       
  2018         {
       
  2019         TInt error( KErrNone );
       
  2020         TInt count( 0);
       
  2021         _LIT( KUnderline, "_" );
       
  2022 
       
  2023         CleanupStack::PopAndDestroy( newFilename16 );
       
  2024         newFilename16 = NULL;
       
  2025 
       
  2026         HBufC16* ext16 = HBufC16::NewLC( ext->Length( ) );
       
  2027         ext16->Des().Copy( *ext );
       
  2028 
       
  2029         TBuf<KMaxIntegerLen> value;
       
  2030         newFilename16 = HBufC16::NewLC( aFilePath.Length( ) + KMaxIntegerLen
       
  2031                 + ext16->Length( ) + KUnderline().Length( ) + KDot().Length( ) );
       
  2032         for ( ;; )
       
  2033             {
       
  2034             // name_xxxxx.ext
       
  2035             value.Num( count );
       
  2036 
       
  2037             newFilename16->Des().Copy( aFilePath );
       
  2038             newFilename16->Des().Append( KUnderline );
       
  2039             newFilename16->Des().Append( value );
       
  2040             //adding dot is not not needed anymore
       
  2041             newFilename16->Des().Append( *ext16 );
       
  2042 
       
  2043             RFile file;
       
  2044             error = file.Open( iFs, *newFilename16, EFileShareExclusive );
       
  2045             file.Close( );
       
  2046 
       
  2047             if ( error == KErrNotFound )
       
  2048                 {
       
  2049                 break;
       
  2050                 }
       
  2051             else
       
  2052                 {
       
  2053                 count++;
       
  2054                 }
       
  2055             }
       
  2056         CleanupStack::Pop( newFilename16 );
       
  2057         CleanupStack::PopAndDestroy( ext16 );
       
  2058         CleanupStack::PushL( newFilename16 );
       
  2059         }
       
  2060 
       
  2061     TInt result = iFs.Rename( *oldFilename16, *newFilename16 );
       
  2062     if ( result == KErrNone )
       
  2063         {
       
  2064         temp = HBufC8::NewL( newFilename16->Length( ) );
       
  2065         temp->Des().Copy( *newFilename16 );
       
  2066 
       
  2067         }
       
  2068     CleanupStack::PopAndDestroy( newFilename16 );
       
  2069     CleanupStack::PopAndDestroy( oldFilename16 );
       
  2070     CleanupStack::PopAndDestroy( newFilename );
       
  2071     CleanupStack::PopAndDestroy( ext );
       
  2072 
       
  2073     return temp;
       
  2074     }
       
  2075 
       
  2076 // -----------------------------------------------------------------------------
       
  2077 // CUpnpContentDirectory::CreateResTagWithDBRecoveyrL
       
  2078 // -----------------------------------------------------------------------------
       
  2079 //
       
  2080 TInt CUpnpContentDirectory::CreateResTagInternalWithDBRecoveryL(
       
  2081     TInt aSessionId, TDesC8& aFilePath, HBufC8* aImportUri,
       
  2082     TBool aOnlyThumbnail )
       
  2083     {
       
  2084     TInt result = 0;
       
  2085     TRAPD( err, result = CreateResTagInternalL( aSessionId, aFilePath,
       
  2086         aImportUri, aOnlyThumbnail ) );
       
  2087     if ( err )
       
  2088         {
       
  2089         if ( DataBaseHasBeenRecoveredL( err ) )
       
  2090             {
       
  2091             TRAP( err, iContentDirectoryDb->CheckDatabaseL( ) );
       
  2092             if ( err == KErrCorrupt )
       
  2093                 {
       
  2094                 err = iContentDirectoryDb->RecreateDatabaseFile( );
       
  2095                 User::LeaveIfError( err );
       
  2096                 }
       
  2097             TRAP( err, result = CreateResTagInternalL( aSessionId, aFilePath,
       
  2098                 aImportUri, aOnlyThumbnail ) );
       
  2099             if ( err == KErrCorrupt )
       
  2100                 {
       
  2101                 err = iContentDirectoryDb->RecreateDatabaseFile( );
       
  2102                 }
       
  2103             }
       
  2104         User::LeaveIfError( err );
       
  2105         }
       
  2106     return result;
       
  2107     }
       
  2108 
       
  2109 // -----------------------------------------------------------------------------
       
  2110 // CUpnpContentDirectory::CreateResTagInternalL
       
  2111 // -----------------------------------------------------------------------------
       
  2112 //
       
  2113 TInt CUpnpContentDirectory::CreateResTagInternalL( TInt aSessionId,
       
  2114     TDesC8& aFilePath, HBufC8* aImportUri, TBool aOnlyThumbnail )
       
  2115     {
       
  2116     TInt ret = KErrGeneral;
       
  2117     HBufC8* number = UpnpCdUtils::RandomizeL( KRandomMax );
       
  2118     CleanupStack::PushL( number );
       
  2119     HBufC8* number2 = UpnpCdUtils::RandomizeL( KRandomMax );
       
  2120     CleanupStack::PushL( number2 );
       
  2121     HBufC8* importRandomize = HBufC8::NewLC( aImportUri->Des().Length( )+number->Des().Length( ) );
       
  2122     importRandomize->Des().Copy( *aImportUri );
       
  2123     importRandomize->Des().Replace( importRandomize->Length( ) - 4, 4, *number2 );
       
  2124     importRandomize->Des().Append( *number );
       
  2125 
       
  2126     RXmlEngDocument obj;
       
  2127     TInt objId = KErrNotFound;
       
  2128     iContentDirectoryDb->GetObjectByAttrL( obj, &objId, KImportUri8,
       
  2129         *aImportUri );
       
  2130     CleanupClosePushL( obj );
       
  2131 
       
  2132     if ( !obj.NotNull( ) )
       
  2133         {
       
  2134         CleanupStack::PopAndDestroy( &obj );
       
  2135         CleanupStack::PopAndDestroy( importRandomize );
       
  2136         CleanupStack::PopAndDestroy( number2 );
       
  2137         CleanupStack::PopAndDestroy( number );
       
  2138         return ret;
       
  2139         }
       
  2140 
       
  2141     HBufC8* mimeType = GetMimeTypeLC( aFilePath );
       
  2142     // CheckFileName 
       
  2143     HBufC8* newName = ChangeFileNameWithExtensionL( aFilePath, *mimeType );
       
  2144     if ( !newName )
       
  2145         {
       
  2146         // renaming file failed, set the old  name
       
  2147         newName = HBufC8::NewL( aFilePath.Length( ) );
       
  2148         newName->Des().Copy( aFilePath );
       
  2149         }
       
  2150     CleanupStack::PushL( newName );
       
  2151 
       
  2152     TSize objectSize( 0, 0);
       
  2153 
       
  2154     //GetFileName
       
  2155     HBufC16* filename16 = UpnpString::ToUnicodeL( *newName );
       
  2156     CleanupStack::PushL( filename16 );
       
  2157 
       
  2158     if ( obj.NotNull( ) && !aOnlyThumbnail )
       
  2159         {
       
  2160         // if there is already a file related to this importUri delete it
       
  2161         DeleteOldResourcesL( obj, *aImportUri, *filename16 );
       
  2162 
       
  2163         // set new contentUri
       
  2164         TXmlEngElement objEl = obj.DocumentElement( );
       
  2165 
       
  2166         TPtrC8 fileNamePtr =
       
  2167                 newName->Mid( newName->LocateReverse( '\\' ) + 1 );
       
  2168 
       
  2169         TInt extPosition = fileNamePtr.LocateReverse( '.' );
       
  2170         TPtrC8 ext;
       
  2171         if ( extPosition>0 )
       
  2172             ext.Set( fileNamePtr.Mid( extPosition ) );
       
  2173         else
       
  2174             ext.Set( KNullString8 );
       
  2175         HBufC8* contUri = HBufC8::NewLC( importRandomize->Length( ) + KSlash().Length( ) +
       
  2176                                          KMaxLongIntegerLen + ext.Length( ) );
       
  2177         TPtr8 contUriPtr(contUri->Des( ));
       
  2178         contUriPtr.Copy( *importRandomize );
       
  2179         contUriPtr.Append( KSlash );
       
  2180 
       
  2181         TBuf8<KMaxLongIntegerLen> num;
       
  2182         num.Num( objId );
       
  2183         contUriPtr.Append( num );
       
  2184         contUriPtr.Append( ext );
       
  2185 
       
  2186         TXmlEngElement resEl;
       
  2187         UpnpDomInterface::GetDirectoryElementL( objEl, resEl, KRes( ),
       
  2188             KImportUri8( ), aImportUri->Des( ) );
       
  2189         resEl.SetTextL( contUriPtr );
       
  2190 
       
  2191         CleanupStack::PopAndDestroy( contUri );
       
  2192 
       
  2193         // set res@size  
       
  2194         TXmlEngAttr size = resEl.AttributeNodeL( KSize( ) );
       
  2195         if ( size.NotNull( ) )
       
  2196             {
       
  2197             SetResSizeL( size, *filename16 );
       
  2198             }
       
  2199 
       
  2200         // set res@protocolInfo
       
  2201         HBufC8* protocolInfo =
       
  2202                 ProtocolInfoFromMimeL( mimeType->Des( ), resEl );
       
  2203         CleanupStack::PushL( protocolInfo );
       
  2204 
       
  2205         TXmlEngAttr protInf = resEl.AttributeNodeL( KprotocolInfo( ) );
       
  2206         protInf.SetValueL( *protocolInfo );
       
  2207 
       
  2208         CleanupStack::PopAndDestroy( protocolInfo );
       
  2209 
       
  2210         CheckItemTypeForObjectL( obj, *mimeType );
       
  2211 
       
  2212         RemoveFileTransferFromTransferIDsStateVariableL( aSessionId );
       
  2213 
       
  2214         // update database
       
  2215         iElementdb->ValidateNewObjectL( obj.DocumentElement( ), EFalse );
       
  2216         iContentDirectoryDb->UpdateObjectL( objId, obj );
       
  2217 
       
  2218         // add resource
       
  2219         CUpnpResourcesBean* resBn = CUpnpResourcesBean::NewLC( );
       
  2220         resBn->SetId( UpnpCdUtils::ResIdFromUriL( *importRandomize ) );
       
  2221         resBn->SetPathL( *newName );
       
  2222         resBn->SetIsReadonly( EFalse );
       
  2223         resBn->SetIsThumbnail( EFalse );
       
  2224         iContentDirectoryDb->AddResourceL( resBn, objId );
       
  2225         // clean up
       
  2226         CleanupStack::PopAndDestroy( resBn );
       
  2227         ret = KErrNone;
       
  2228         }
       
  2229 
       
  2230     //send event to Media server      
       
  2231 
       
  2232 
       
  2233     CUpnpFileTransferEvent* event =
       
  2234             iTransferController->TransferL( aSessionId );
       
  2235 
       
  2236     if ( event )
       
  2237         {
       
  2238         event->SetFilePathL( *filename16 );
       
  2239         //CleanupStack::Pop( event);
       
  2240         //iEventObserver->FileTransferEvent( event );
       
  2241         }
       
  2242 
       
  2243     // create thumbnail
       
  2244     CreateThumbnailL( *filename16, objId );
       
  2245 
       
  2246     // clean up
       
  2247     CleanupStack::PopAndDestroy( filename16 );
       
  2248     CleanupStack::PopAndDestroy( newName );
       
  2249     CleanupStack::PopAndDestroy( mimeType );
       
  2250     CleanupStack::PopAndDestroy( &obj );
       
  2251     CleanupStack::PopAndDestroy( importRandomize );
       
  2252     CleanupStack::PopAndDestroy( number2 );
       
  2253     CleanupStack::PopAndDestroy( number );
       
  2254 
       
  2255     return ret;
       
  2256     }
       
  2257 
       
  2258 // -----------------------------------------------------------------------------
       
  2259 // CUpnpContentDirectory::SetResSizeL
       
  2260 // -----------------------------------------------------------------------------
       
  2261 //
       
  2262 void CUpnpContentDirectory::SetResSizeL( TXmlEngAttr aSize,
       
  2263     const TDesC& aFilePath )
       
  2264     {
       
  2265     // get file size
       
  2266     RFile file;
       
  2267     CleanupClosePushL( file );
       
  2268     User::LeaveIfError( file.Open( iFs, aFilePath, EFileRead|EFileShareAny ) );
       
  2269     TInt size;
       
  2270     User::LeaveIfError( file.Size( size ) );
       
  2271 
       
  2272     // set size
       
  2273     TBuf8<KMaxLongIntegerLen> num;
       
  2274     num.Num( size );
       
  2275     aSize.SetValueL( num );
       
  2276 
       
  2277     CleanupStack::PopAndDestroy( &file );
       
  2278     }
       
  2279 
       
  2280 // -----------------------------------------------------------------------------
       
  2281 // CUpnpContentDirectory::DeleteOldResourcesL
       
  2282 // -----------------------------------------------------------------------------
       
  2283 //
       
  2284 void CUpnpContentDirectory::DeleteOldResourcesL( RXmlEngDocument& aObj,
       
  2285     const TDesC8& aImportUri, const TDesC& aNewFile )
       
  2286     {
       
  2287     // for main res tag delete only corresponding record in the resources table
       
  2288     TXmlEngElement el;
       
  2289     UpnpDomInterface::GetDirectoryElementL( aObj.DocumentElement( ), el,
       
  2290         KImportUri8( ), aImportUri );
       
  2291     if ( el.NotNull( ) ) //  should always be true
       
  2292         {
       
  2293         TPtrC8 resVal(UpnpDomInterface::GetElementValueL( el ));
       
  2294         if ( resVal.Length( ) )
       
  2295             {
       
  2296             TInt64 resId = UpnpCdUtils::ResIdFromUriL( resVal ) ;
       
  2297             // check src existence -----------------------------
       
  2298             CUpnpResourcesBean* rscBean = NULL;
       
  2299             rscBean = iContentDirectoryDb->GetResourceL( resId );
       
  2300             TBool deletefile = ETrue;
       
  2301             //checking if a new file is the same as the old one
       
  2302             //if so, it means the old one was by chance deleted from the file system
       
  2303             if ( rscBean )
       
  2304                 {
       
  2305                 TPtrC rscPath(rscBean->Path( ) );
       
  2306                 if ( !rscPath.Compare( aNewFile ) )
       
  2307                     deletefile = EFalse;
       
  2308                 delete rscBean;
       
  2309                 }
       
  2310             iContentDirectoryDb->DeleteResourceByResIdL( resId, deletefile );
       
  2311             }
       
  2312 
       
  2313         }
       
  2314 
       
  2315     // destroy all other res tags   
       
  2316     RArray<TXmlEngElement> elms;
       
  2317     CleanupClosePushL( elms );
       
  2318     UpnpDomInterface::GetElementListL( aObj.DocumentElement( ), elms, KRes );
       
  2319     // for each element
       
  2320     for ( TInt i=0; i < elms.Count( ); i++ )
       
  2321         {
       
  2322         TXmlEngAttr impUri(elms[i].AttributeNodeL( KImportUri( ) ) );
       
  2323         if ( impUri.IsNull( ) && !el.IsSameNode( elms[i] ) )
       
  2324             {
       
  2325             TPtrC8 resVal(UpnpDomInterface::GetElementValueL( elms[i] ));
       
  2326             if ( resVal.Length( ) )
       
  2327                 {
       
  2328                 iContentDirectoryDb->DeleteResourceByResIdL( 
       
  2329                                                         UpnpCdUtils::ResIdFromUriL( resVal ) );
       
  2330                 }
       
  2331             elms[i].Remove( );
       
  2332             }
       
  2333         }
       
  2334     CleanupStack::PopAndDestroy( &elms );
       
  2335     }
       
  2336 // -----------------------------------------------------------------------------
       
  2337 // CUpnpContentDirectory::CreateThumbnailCreatorL
       
  2338 // -----------------------------------------------------------------------------
       
  2339 //
       
  2340 CUpnpThumbnailCreator* CUpnpContentDirectory::CreateThumbnailCreatorL(
       
  2341     TInt aObjectId )
       
  2342     {
       
  2343     CUpnpThumbnailCreator* thumbCreator = NULL;
       
  2344 
       
  2345     if ( iBitmapServerConnection == KErrNone )
       
  2346         {
       
  2347         //check if from the file THUMB can be created. 
       
  2348         //create thumbnail 
       
  2349         // in thumbnail save the information. 
       
  2350         thumbCreator = CUpnpThumbnailCreator::NewLC( this );
       
  2351         iThumbnailCreators.AppendL( thumbCreator );
       
  2352         TInt index = iThumbnailCreators.Find( thumbCreator );
       
  2353         User::LeaveIfError( index );
       
  2354         iThObjectIds.Insert( aObjectId, index );
       
  2355         CleanupStack::Pop( thumbCreator );
       
  2356         }
       
  2357     return thumbCreator;
       
  2358     }
       
  2359 
       
  2360 // -----------------------------------------------------------------------------
       
  2361 // CUpnpContentDirectory::RemoveThCreatorAndObjectIdLD
       
  2362 // -----------------------------------------------------------------------------
       
  2363 //
       
  2364 void CUpnpContentDirectory::RemoveThCreatorAndObjectIdLD(
       
  2365     CUpnpThumbnailCreator* aThCreator )
       
  2366     {
       
  2367     TInt index = iThumbnailCreators.Find( aThCreator );
       
  2368     if ( index == KErrNotFound )
       
  2369         {
       
  2370         RDebug::Print( _L( "There is no corresponding thCreator in list. Error situation." ) );
       
  2371         delete aThCreator;
       
  2372         return;
       
  2373         }
       
  2374     else
       
  2375         {
       
  2376         iThumbnailCreators.Remove( index );
       
  2377         }
       
  2378     if ( index < iThObjectIds.Count( ) )
       
  2379         {
       
  2380         iThObjectIds.Remove( index );
       
  2381         }
       
  2382     delete aThCreator;
       
  2383     }
       
  2384 
       
  2385 // -----------------------------------------------------------------------------
       
  2386 // CUpnpContentDirectory::GetObjectIdFromThCreator
       
  2387 // -----------------------------------------------------------------------------
       
  2388 //
       
  2389 TInt CUpnpContentDirectory::GetObjectIdFromThCreator(
       
  2390     CUpnpThumbnailCreator* aThCreator )
       
  2391     {
       
  2392     TInt result = KErrNotFound;
       
  2393     TInt index = iThumbnailCreators.Find( aThCreator );
       
  2394     RDebug::Print( _L( "Whole number of objects. %d" ) ,
       
  2395         iThObjectIds.Count( ) );
       
  2396     if ( index < iThObjectIds.Count( ) )
       
  2397         {
       
  2398         result = iThObjectIds[index];
       
  2399         }
       
  2400     return result;
       
  2401     }
       
  2402 
       
  2403 // -----------------------------------------------------------------------------
       
  2404 // CUpnpContentDirectory::NotifyThumbnailResultL
       
  2405 // -----------------------------------------------------------------------------
       
  2406 //
       
  2407 void CUpnpContentDirectory::NotifyThumbnailResultL(
       
  2408     CUpnpThumbnailCreator* aThCreator, TInt aResult )
       
  2409     {
       
  2410     TInt result = aResult;
       
  2411     RDebug::Print( _L( "Thumbnail has been created with result: %d" ),
       
  2412         aResult );
       
  2413     if ( aResult == KErrNone )
       
  2414         {
       
  2415         RXmlEngDocument obj;
       
  2416         CleanupClosePushL( obj );
       
  2417         TInt objectId = GetObjectIdFromThCreator( aThCreator );
       
  2418         TInt err =
       
  2419                 iContentDirectoryDb->GetObjectL( objectId, obj, KAsterisk8 );
       
  2420         if ( err == EUpnpOk )
       
  2421             {
       
  2422             // get item element
       
  2423             TXmlEngElement objElement;
       
  2424             UpnpDomInterface::GetElementL( obj.DocumentElement( ),
       
  2425                 objElement, KItem );
       
  2426 
       
  2427             // add new res tag
       
  2428             TXmlEngElement thumbRes(objElement.AddNewElementL( KRes( ) ) );
       
  2429 
       
  2430             // add protocolInfo
       
  2431             HBufC8* protocolInfo = ProtocolInfoFromMimeL( KThumbMimeType( ),
       
  2432                 JPEG_TN, ETrue, ETrue );
       
  2433             CleanupStack::PushL( protocolInfo );
       
  2434             thumbRes.AddNewAttributeL( KprotocolInfo( ), *protocolInfo );
       
  2435             CleanupStack::PopAndDestroy( protocolInfo );
       
  2436 
       
  2437             // add contentUri
       
  2438             HBufC8* contUri = BuildContentUriL( aThCreator->GetFileExt( ),
       
  2439                 objectId );
       
  2440             CleanupStack::PushL( contUri );
       
  2441             thumbRes.SetTextL( *contUri );
       
  2442 
       
  2443             // add new resource
       
  2444             CUpnpResourcesBean* resBean = CUpnpResourcesBean::NewLC( );
       
  2445             resBean->SetId( UpnpCdUtils::ResIdFromUriL( *contUri ) );
       
  2446             resBean->SetPathL( aThCreator->GetPath( ) );
       
  2447             resBean->SetIsReadonly( EFalse );
       
  2448             resBean->SetIsThumbnail( ETrue );
       
  2449             iContentDirectoryDb->AddResourceL( resBean, objectId );
       
  2450 
       
  2451             // update
       
  2452             iElementdb->ValidateNewObjectL( obj.DocumentElement( ), EFalse );
       
  2453             iContentDirectoryDb->UpdateObjectL( objectId, obj );
       
  2454             RemoveThCreatorAndObjectIdLD( aThCreator );
       
  2455 
       
  2456             // clean up
       
  2457             CleanupStack::PopAndDestroy( resBean );
       
  2458             CleanupStack::PopAndDestroy( contUri );
       
  2459             }
       
  2460         else
       
  2461             {
       
  2462             // delete thumbnail file if exists
       
  2463             iFs.Delete( aThCreator->GetPath( ) ); // ignore errors
       
  2464             // remove thCreator and objectID from list 
       
  2465             RemoveThCreatorAndObjectIdLD( aThCreator );
       
  2466             result = err;
       
  2467             }
       
  2468         CleanupStack::PopAndDestroy( &obj );
       
  2469         }
       
  2470     else
       
  2471         {
       
  2472         // delete thumbnail file if exists
       
  2473         iFs.Delete( aThCreator->GetPath( ) ); // ignore errors
       
  2474         // remove thCreator and objectID from list 
       
  2475         RemoveThCreatorAndObjectIdLD( aThCreator );
       
  2476         }
       
  2477 
       
  2478     // notify
       
  2479     if ( iThmbObs )
       
  2480         {
       
  2481         iThmbObs->NotifyThumbnailResultL( 0, result );
       
  2482         }
       
  2483     }
       
  2484 
       
  2485 // -----------------------------------------------------------------------------
       
  2486 // CUpnpContentDirectory::CreateResTagL
       
  2487 // -----------------------------------------------------------------------------
       
  2488 //
       
  2489 TInt CUpnpContentDirectory::CreateResTagL( TInt aSessionId,
       
  2490     TDesC8& aFilePath, TInt aImportNumber, TBool aOnlyThumbnail )
       
  2491     {
       
  2492     HBufC8* importUri = UpnpCdUtils::BuildImportUriLC( aImportNumber );
       
  2493     TInt ret = CreateResTagInternalWithDBRecoveryL( aSessionId, aFilePath,
       
  2494         importUri, aOnlyThumbnail );
       
  2495     CleanupStack::PopAndDestroy( importUri );
       
  2496     return ret;
       
  2497     }
       
  2498 
       
  2499 // -----------------------------------------------------------------------------
       
  2500 // CUpnpContentDirectory::ProtocolInfoFromMimeLC
       
  2501 // -----------------------------------------------------------------------------
       
  2502 //
       
  2503 CUpnpDlnaProtocolInfo* CUpnpContentDirectory::CreateProtocolInfoFromMimeL(
       
  2504     const TDesC8& aMimeType, TInt aAddInfo )
       
  2505     {
       
  2506     CUpnpProtocolInfoLocal* protInfo = CUpnpProtocolInfoLocal::NewL(
       
  2507         aMimeType, aAddInfo );
       
  2508     CleanupStack::PushL( protInfo );
       
  2509     protInfo->SetThirdFieldL( aMimeType );
       
  2510     CleanupStack::Pop( protInfo );
       
  2511     return protInfo;
       
  2512     }
       
  2513 
       
  2514 // -----------------------------------------------------------------------------
       
  2515 // CUpnpContentDirectory::ProtocolInfoFromMimeL
       
  2516 // -----------------------------------------------------------------------------
       
  2517 //
       
  2518 HBufC8* CUpnpContentDirectory::ProtocolInfoFromMimeL(
       
  2519     const TDesC8& aMimeType, TInt aAddInfo, TBool aOpRangeParam,
       
  2520     TBool aCiParam /*= EFalse*/)
       
  2521     {
       
  2522     CUpnpDlnaProtocolInfo* protInfo = CreateProtocolInfoFromMimeL( aMimeType,
       
  2523         aAddInfo );
       
  2524     if ( aCiParam )
       
  2525         {
       
  2526         protInfo->SetCiParameter( ETrue );
       
  2527         }
       
  2528     if ( aOpRangeParam )
       
  2529         {
       
  2530         protInfo->SetOpParameterL( B_VAL, ETrue );
       
  2531         }
       
  2532 
       
  2533     CleanupStack::PushL( protInfo );
       
  2534     TPtrC8 protocolInfo = protInfo->ProtocolInfoL( );
       
  2535     HBufC8* protocolInfoDes = protocolInfo.Alloc( );
       
  2536     CleanupStack::PopAndDestroy( protInfo );
       
  2537     return protocolInfoDes;
       
  2538     }
       
  2539 // -----------------------------------------------------------------------------
       
  2540 // CUpnpContentDirectory::ProtocolInfoFromMimeL
       
  2541 // -----------------------------------------------------------------------------
       
  2542 //
       
  2543 HBufC8* CUpnpContentDirectory::ProtocolInfoFromMimeL(
       
  2544     const TDesC8& aMimeType, TXmlEngElement& aResEl )
       
  2545     {
       
  2546     HBufC8* result = NULL;
       
  2547     const TDesC8& value = UpnpDomInterface::GetAttrValueL( aResEl,
       
  2548         KprotocolInfo );
       
  2549     if ( aMimeType.Length( ) > 0 )
       
  2550         {
       
  2551         if ( value.Length( ) )
       
  2552             {
       
  2553             CUpnpDlnaProtocolInfo* protocolInfo =
       
  2554                     CUpnpDlnaProtocolInfo::NewL( (TDesC8&) value );
       
  2555             CleanupStack::PushL( protocolInfo );
       
  2556             TPtrC8 third = protocolInfo->ThirdField( );
       
  2557             if ( !(aMimeType == third ) )
       
  2558                 {
       
  2559                 protocolInfo->SetThirdFieldL( (TDesC8&)aMimeType );
       
  2560                 protocolInfo->SetFourthFieldL( KAsterisk8( ) );
       
  2561                 }
       
  2562             TPtrC8 protInfo = protocolInfo->ProtocolInfoL( );
       
  2563             result = protInfo.Alloc( );
       
  2564             CleanupStack::PopAndDestroy( protocolInfo );
       
  2565             }
       
  2566         }
       
  2567     else
       
  2568         {
       
  2569         result = value.AllocL( );
       
  2570         }
       
  2571     return result;
       
  2572     }
       
  2573 
       
  2574 // -----------------------------------------------------------------------------
       
  2575 // CUpnpContentDirectory::GetFileNameL
       
  2576 // -----------------------------------------------------------------------------
       
  2577 //
       
  2578 HBufC* CUpnpContentDirectory::GetFileNameL( TInt aObjId )
       
  2579     {
       
  2580     TInt objId = aObjId;
       
  2581     HBufC* result = NULL;
       
  2582     RXmlEngDocument objDidl;
       
  2583     iContentDirectoryDb->GetObjectL( objId, objDidl, KAsterisk8( ) );
       
  2584     CleanupClosePushL( objDidl );
       
  2585     TXmlEngElement obj = objDidl.DocumentElement().FirstChild().AsElement( );
       
  2586     if ( obj.IsNull( ) )
       
  2587         {
       
  2588         CleanupStack::PopAndDestroy( &objDidl );
       
  2589         return NULL;
       
  2590         }
       
  2591 
       
  2592     // file name
       
  2593     TXmlEngElement objTitle;
       
  2594     UpnpDomInterface::GetElementL( obj, objTitle, KObjTiltleColName8( ) );
       
  2595     if ( objTitle.IsNull( ) )
       
  2596         {
       
  2597         CleanupStack::PopAndDestroy( &objDidl );
       
  2598         return NULL;
       
  2599         }
       
  2600 
       
  2601     // mime type
       
  2602     RArray<TXmlEngElement> reses;
       
  2603     CleanupClosePushL( reses );
       
  2604     UpnpDomInterface::GetElementListL( obj, reses, KRes );
       
  2605     HBufC8* mimeType = NULL;
       
  2606     for ( TInt i = 0; i < reses.Count( ); i++ )
       
  2607         { // should be only one importUri
       
  2608         TXmlEngAttr impUri;
       
  2609         impUri = reses[i].AttributeNodeL( KImportUri );
       
  2610         if ( impUri.NotNull( ) )
       
  2611             {
       
  2612             TXmlEngAttr prInfoAttr = reses[i].AttributeNodeL( KprotocolInfo );
       
  2613             CUpnpDlnaProtocolInfo* prInfo =
       
  2614                     CUpnpDlnaProtocolInfo::NewL( prInfoAttr.Value( ) );
       
  2615             CleanupStack::PushL( prInfo );
       
  2616             mimeType = prInfo->ThirdField().AllocL( );
       
  2617             CleanupStack::PopAndDestroy( prInfo );
       
  2618             break;
       
  2619             }
       
  2620         }
       
  2621     CleanupStack::PopAndDestroy( &reses );
       
  2622 
       
  2623     TPtrC ext;
       
  2624     if ( mimeType )
       
  2625         {
       
  2626         TPtrC8 mimeTypeCut( *mimeType);
       
  2627         TInt position = mimeType->Des().Find( KSemicolon8 );
       
  2628         if ( position != KErrNotFound )
       
  2629             {
       
  2630             mimeTypeCut.Set( mimeType->Des().Left( position ) );
       
  2631             }
       
  2632 
       
  2633         const HBufC* tmp = iMimeToExtMap->Get( mimeTypeCut );
       
  2634         if ( tmp )
       
  2635             {
       
  2636             ext.Set( tmp->Ptr( ), tmp->Length( ) ); // do not delete
       
  2637             }
       
  2638         delete mimeType;
       
  2639         }
       
  2640 
       
  2641     result = HBufC::NewLC( objTitle.Value().Length( ) + ext.Length( ) );
       
  2642     TPtr resPtr(result->Des( ) );
       
  2643     HBufC* title = UpnpCdUtils::Des8ToDesLC( objTitle.Value( ) );
       
  2644 
       
  2645     resPtr.Copy( *title );
       
  2646 
       
  2647     TInt wholeNameLength = title->Des().LocateReverse( '.' );
       
  2648     TInt extenLength = title->Length( )-wholeNameLength;
       
  2649     TPtrC exten;
       
  2650     if ( extenLength )
       
  2651         {
       
  2652         exten.Set( title->Right( extenLength ) );
       
  2653         }
       
  2654 
       
  2655     if ( ext.CompareF( exten ) != 0 )
       
  2656         {
       
  2657         resPtr.Append( ext );
       
  2658         }
       
  2659 
       
  2660     // forbidden chars
       
  2661     UpnpCdUtils::ValidateFilePath( resPtr );
       
  2662 
       
  2663     HBufC* basePath = HBufC::NewL( iDownloadDir->Des().Length( ) + result->Des().Length( ) );
       
  2664     basePath -> Des().Append( iDownloadDir->Des( ) );
       
  2665     basePath -> Des().Append( result->Des( ) );
       
  2666 
       
  2667     CleanupStack::PopAndDestroy( title );
       
  2668     CleanupStack::PopAndDestroy( result );
       
  2669     CleanupStack::PopAndDestroy( &objDidl );
       
  2670 
       
  2671     if ( BaflUtils::FileExists( iFs, *basePath ) )
       
  2672         {
       
  2673         CleanupStack::PushL( basePath );
       
  2674         HBufC* fileName = CreateFileNameL( basePath->Des( ) );
       
  2675         CleanupStack::PopAndDestroy( basePath );
       
  2676         return fileName;
       
  2677         }
       
  2678 
       
  2679     return basePath;
       
  2680     }
       
  2681 
       
  2682 // -----------------------------------------------------------------------------
       
  2683 // CUpnpContentDirectory::GetFileNameWithoutPathL
       
  2684 // -----------------------------------------------------------------------------
       
  2685 //
       
  2686 HBufC* CUpnpContentDirectory::GetFileNameWithoutPathL( TInt aObjId )
       
  2687     {
       
  2688     TInt objId = aObjId;
       
  2689     HBufC* result = NULL;
       
  2690     RXmlEngDocument objDidl;
       
  2691     iContentDirectoryDb->GetObjectL( objId, objDidl, KAsterisk8( ) );
       
  2692     CleanupClosePushL( objDidl );
       
  2693     TXmlEngElement obj = objDidl.DocumentElement().FirstChild().AsElement( );
       
  2694     if ( obj.IsNull( ) )
       
  2695         {
       
  2696         CleanupStack::PopAndDestroy( &objDidl );
       
  2697         return NULL;
       
  2698         }
       
  2699 
       
  2700     // file name
       
  2701     TXmlEngElement objTitle;
       
  2702     UpnpDomInterface::GetElementL( obj, objTitle, KObjTiltleColName8( ) );
       
  2703     if ( objTitle.IsNull( ) )
       
  2704         {
       
  2705         CleanupStack::PopAndDestroy( &objDidl );
       
  2706         return NULL;
       
  2707         }
       
  2708 
       
  2709     // mime type
       
  2710     RArray<TXmlEngElement> reses;
       
  2711     CleanupClosePushL( reses );
       
  2712     UpnpDomInterface::GetElementListL( obj, reses, KRes );
       
  2713     HBufC8* mimeType = NULL;
       
  2714     for ( TInt i = 0; i < reses.Count( ); i++ )
       
  2715         { // should be only one importUri
       
  2716         TXmlEngAttr impUri;
       
  2717         impUri = reses[i].AttributeNodeL( KImportUri );
       
  2718         if ( impUri.NotNull( ) )
       
  2719             {
       
  2720             TXmlEngAttr prInfoAttr = reses[i].AttributeNodeL( KprotocolInfo );
       
  2721             CUpnpDlnaProtocolInfo* prInfo =
       
  2722                     CUpnpDlnaProtocolInfo::NewL( prInfoAttr.Value( ) );
       
  2723             CleanupStack::PushL( prInfo );
       
  2724             mimeType = prInfo->ThirdField().AllocL( );
       
  2725             CleanupStack::PopAndDestroy( prInfo );
       
  2726             break;
       
  2727             }
       
  2728         }
       
  2729     CleanupStack::PopAndDestroy( &reses );
       
  2730 
       
  2731     TPtrC ext;
       
  2732     if ( mimeType )
       
  2733         {
       
  2734         TPtrC8 mimeTypeCut(*mimeType);
       
  2735         TInt position = mimeType->Des().Find( KSemicolon8 );
       
  2736         if ( position != KErrNotFound )
       
  2737             {
       
  2738             mimeTypeCut.Set( mimeType->Des().Left( position ) );
       
  2739             }
       
  2740 
       
  2741         const HBufC* tmp = iMimeToExtMap->Get( mimeTypeCut );
       
  2742         if ( tmp )
       
  2743             {
       
  2744             ext.Set( tmp->Ptr( ), tmp->Length( ) ); // do not delete
       
  2745             }
       
  2746         delete mimeType;
       
  2747         }
       
  2748 
       
  2749     result = HBufC::NewLC( objTitle.Value().Length( ) + ext.Length( ) );
       
  2750     TPtr resPtr(result->Des( ));
       
  2751     HBufC* title = UpnpCdUtils::Des8ToDesLC( objTitle.Value( ) );
       
  2752 
       
  2753     resPtr.Copy( *title );
       
  2754 
       
  2755     TInt wholeNameLength = title->Des().LocateReverse( '.' );
       
  2756     TInt extenLength = title->Length( )-wholeNameLength;
       
  2757     TPtrC exten;
       
  2758     if ( extenLength )
       
  2759         {
       
  2760         exten.Set( title->Right( extenLength ) );
       
  2761         }
       
  2762 
       
  2763     if ( ext.CompareF( exten )!=0 )
       
  2764         resPtr.Append( ext );
       
  2765 
       
  2766     // forbidden chars
       
  2767     UpnpCdUtils::ValidateFilePath( resPtr );
       
  2768 
       
  2769     // clean up
       
  2770     CleanupStack::PopAndDestroy( title );
       
  2771     CleanupStack::Pop( result );
       
  2772     CleanupStack::PopAndDestroy( &objDidl );
       
  2773     return result;
       
  2774     }
       
  2775 
       
  2776 // -----------------------------------------------------------------------------
       
  2777 // CUpnpContentDirectory::CreateFileNameL
       
  2778 // -----------------------------------------------------------------------------
       
  2779 //
       
  2780 HBufC* CUpnpContentDirectory::CreateFileNameL( const TDesC16& aFileName )
       
  2781     {
       
  2782     const TInt KMaxSameFileNames = 10000;
       
  2783     TParse parseFilename;
       
  2784     parseFilename.Set( aFileName, NULL, NULL );
       
  2785 
       
  2786     // orginal file + underscore + number 
       
  2787     HBufC* name = HBufC::NewLC( aFileName.Length( ) + KMaxIntegerLen );
       
  2788     TPtr namePtr(name->Des( ) );
       
  2789 
       
  2790     for ( TInt count = 1; count < KMaxSameFileNames; count++ )
       
  2791         {
       
  2792         TBuf<KMaxIntegerLen> value;
       
  2793         value.Num( count );
       
  2794 
       
  2795         namePtr.Append( parseFilename.DriveAndPath( ) );
       
  2796         namePtr.Append( parseFilename.Name( ) );
       
  2797         namePtr.Append( KUndersc( ) );
       
  2798         namePtr.Append( value );
       
  2799         namePtr.Append( parseFilename.Ext( ) );
       
  2800 
       
  2801         if ( !BaflUtils::FileExists( iFs, namePtr ) )
       
  2802             {
       
  2803             break;
       
  2804             }
       
  2805         namePtr.Zero( );
       
  2806         }
       
  2807 
       
  2808     CleanupStack::Pop( name );
       
  2809     return name;
       
  2810     }
       
  2811 
       
  2812 // -----------------------------------------------------------------------------
       
  2813 // CUpnpContentDirectory::GetTitleForUriL
       
  2814 // -----------------------------------------------------------------------------
       
  2815 //
       
  2816 void CUpnpContentDirectory::GetTitleForUriL( TInt aObjectId, TPtr& aValue )
       
  2817     {
       
  2818     HBufC* result = GetFileNameWithoutPathL( aObjectId );
       
  2819     TPtr des = result->Des( );
       
  2820     UpnpCdUtils::ValidateFilePath( des );
       
  2821     if ( result == NULL )
       
  2822         {
       
  2823         return;
       
  2824         }
       
  2825     aValue.Copy( *result );
       
  2826     delete result;
       
  2827     }
       
  2828 // -----------------------------------------------------------------------------
       
  2829 // CUpnpContentDirectory::CheckImportUriL
       
  2830 // -----------------------------------------------------------------------------
       
  2831 //
       
  2832 TInt CUpnpContentDirectory::CheckImportUriL( const TDesC8& aImportUri )
       
  2833     {
       
  2834     TInt objId = KErrNotFound;
       
  2835 
       
  2836     // http characters  
       
  2837     HBufC8* dest8 = HBufC8::NewLC( aImportUri.Length( ) );
       
  2838     TPtr8 dest8Ptr(dest8->Des( ));
       
  2839     dest8Ptr.Copy( aImportUri );
       
  2840 
       
  2841     UpnpString::ReplaceHttpCharacters( dest8Ptr );
       
  2842 
       
  2843     // ip and port placeholder
       
  2844     HBufC8* dest = InsertIpAndPortPlaceholderL( dest8Ptr );
       
  2845     CleanupStack::PushL( dest );
       
  2846 
       
  2847     objId = iContentDirectoryDb->GetObjIdByAttrLikeL( KImportUri8, *dest );
       
  2848 
       
  2849     // clean up
       
  2850     CleanupStack::PopAndDestroy( dest );
       
  2851     CleanupStack::PopAndDestroy( dest8 );
       
  2852 
       
  2853     return objId;
       
  2854     }
       
  2855 
       
  2856 // -----------------------------------------------------------------------------
       
  2857 // CUpnpContentDirectory::CreateAddressValueLC
       
  2858 // -----------------------------------------------------------------------------
       
  2859 //
       
  2860 HBufC8* CUpnpContentDirectory::CreateAddressValueLC()
       
  2861     {
       
  2862 
       
  2863     HBufC8* result = NULL;
       
  2864 
       
  2865     CBufFlat* resBuf = CBufFlat::NewL( KMaxName );
       
  2866     CleanupStack::PushL( resBuf );
       
  2867 
       
  2868     UpnpString::AppendStringL( *resBuf, KHttpTag( ) );
       
  2869     UpnpString::AppendStringL( *resBuf, KIpPortPlaceholder8( ) );
       
  2870 
       
  2871     result = HBufC8::NewL( resBuf->Size( ) );
       
  2872     TPtr8 resultPtr(result->Des( ));
       
  2873     resultPtr.Copy( resBuf->Ptr( 0 ) );
       
  2874     CleanupStack::PopAndDestroy( resBuf );
       
  2875     CleanupStack::PushL( result );
       
  2876     return result;
       
  2877     // path 
       
  2878     }
       
  2879 
       
  2880 // -----------------------------------------------------------------------------
       
  2881 // CUpnpContentDirectory::GetImportUriLC
       
  2882 // -----------------------------------------------------------------------------
       
  2883 //
       
  2884 HBufC8* CUpnpContentDirectory::GetImportUriLC( CUpnpHttpMessage* aMessage )
       
  2885     {
       
  2886     TPtrC8 path = aMessage->SenderPathFromHeader( );
       
  2887     if ( path.Find( KHttpTag ) == KErrNotFound )
       
  2888         {
       
  2889         HBufC8* serverAddr = CreateAddressValueLC( );
       
  2890         HBufC8* result =
       
  2891                 HBufC8::NewL( serverAddr->Length( ) + path.Length( ) );
       
  2892         TPtr8 resultPtr(result->Des( ) );
       
  2893         resultPtr.Append( *serverAddr );
       
  2894         resultPtr.Append( path );
       
  2895         CleanupStack::PopAndDestroy( serverAddr );
       
  2896         CleanupStack::PushL( result );
       
  2897         return result;
       
  2898         }
       
  2899     else
       
  2900         {
       
  2901         HBufC8* pathHBuf = InsertIpAndPortPlaceholderL( path );
       
  2902         CleanupStack::PushL( pathHBuf );
       
  2903         return pathHBuf;
       
  2904         }
       
  2905     }
       
  2906 
       
  2907 // -----------------------------------------------------------------------------
       
  2908 // CUpnpContentDirectory::GetProtocolInfoByImportUriL
       
  2909 // -----------------------------------------------------------------------------
       
  2910 //
       
  2911 CUpnpDlnaProtocolInfo* CUpnpContentDirectory::GetProtocolInfoByImportUriL(
       
  2912     const TDesC8& aImportUri )
       
  2913     {
       
  2914     TPtrC8 importUri;
       
  2915     importUri.Set( UpdateImportUriL( aImportUri ) );
       
  2916 
       
  2917     CUpnpDlnaProtocolInfo* protocolInfo = NULL;
       
  2918     // check restriction - cannot export to the restricted object
       
  2919     TInt objId = iContentDirectoryDb->
       
  2920     GetObjIdByAttrLikeL( KImportUri8, importUri );
       
  2921 
       
  2922     if ( objId < 0 )
       
  2923         {
       
  2924         User::Leave( ENoSuchObject );
       
  2925         }
       
  2926 
       
  2927     if ( iContentDirectoryDb->
       
  2928     CheckObjectRestrictionL( objId ) == ERestrictedObject )
       
  2929         {
       
  2930         User::Leave( ERestrictedObject );
       
  2931         }
       
  2932     // parent restricions
       
  2933     if ( iContentDirectoryDb->CheckParentRestrictionL( objId ) != EUpnpOk )
       
  2934         {
       
  2935         User::Leave( ERestrictedParentObject );
       
  2936         }
       
  2937     else
       
  2938         {
       
  2939         HBufC8* protInfoBuf = iContentDirectoryDb->
       
  2940         GetProtocolInfoByImportUriL( importUri );
       
  2941 
       
  2942         if ( protInfoBuf )
       
  2943             {
       
  2944             CleanupStack::PushL( protInfoBuf );
       
  2945             protocolInfo = CUpnpDlnaProtocolInfo::NewL( protInfoBuf->Des( ) );
       
  2946             CleanupStack::PopAndDestroy( protInfoBuf );
       
  2947             }
       
  2948         }
       
  2949     return protocolInfo;
       
  2950     }
       
  2951 
       
  2952 // -----------------------------------------------------------------------------
       
  2953 // CUpnpContentDirectory::UpdateImportUriL
       
  2954 // -----------------------------------------------------------------------------
       
  2955 //
       
  2956 TPtrC8 CUpnpContentDirectory::UpdateImportUriL( const TDesC8& aImportUri )
       
  2957     {
       
  2958     HBufC8* portAndIp =  GetIpAndPortDes8L();
       
  2959     CleanupStack::PushL( portAndIp );
       
  2960 
       
  2961     TPtrC8 importUri;
       
  2962     importUri.Set( aImportUri );
       
  2963     
       
  2964     TInt index = aImportUri.Find( *portAndIp );
       
  2965     if ( index > 0 )
       
  2966         {
       
  2967         importUri.Set( aImportUri.Mid( index + portAndIp->Des().Length() ) );
       
  2968         }
       
  2969                 
       
  2970     CleanupStack::PopAndDestroy( portAndIp );
       
  2971     
       
  2972     //--- replaceing e.g '/0000000004' -> '0000000004'   
       
  2973     index = importUri.Find( KSlash8() );
       
  2974 	// importUri must begins with '/'
       
  2975     if ( !index )
       
  2976         {
       
  2977         importUri.Set( importUri.Mid( index + 1 ) );        
       
  2978         }
       
  2979      else
       
  2980         {
       
  2981         User::Leave( ENoSuchObject );         
       
  2982         }   
       
  2983     //----------------------------------------
       
  2984     
       
  2985     if ( importUri.Length() < KMaxIntegerLen )
       
  2986         {
       
  2987         User::Leave(ENoSuchObject);        
       
  2988         }
       
  2989     
       
  2990     // Check if all signs in uri are digits
       
  2991     TLex8 lex( importUri );
       
  2992     for( TInt i( 0 ); i < importUri.Length(); i++ )
       
  2993         {
       
  2994         if ( !( lex.Get() ).IsDigit() ) 
       
  2995             {
       
  2996             User::Leave( ENoSuchObject ); 
       
  2997             }
       
  2998         }    
       
  2999     return importUri; 
       
  3000     }
       
  3001     
       
  3002 // -----------------------------------------------------------------------------
       
  3003 // CUpnpContentDirectory::GetProtocolInfoL
       
  3004 // -----------------------------------------------------------------------------
       
  3005 //
       
  3006 TInt CUpnpContentDirectory::GetProtocolInfoL( const TDesC8& aContentUri,
       
  3007     CUpnpDlnaProtocolInfo*& aProtocolInfo )
       
  3008     {
       
  3009     HBufC8 *buf = InsertIpAndPortPlaceholderL( aContentUri );
       
  3010     CleanupStack::PushL( buf );
       
  3011     TInt err = iContentDirectoryDb->GetProtocolInfoL( buf->Des( ),
       
  3012         aProtocolInfo );
       
  3013     CleanupStack::PopAndDestroy( buf );
       
  3014     return err;
       
  3015     }
       
  3016 
       
  3017 // -----------------------------------------------------------------------------
       
  3018 // CUpnpContentDirectory::FindSharedFolderL
       
  3019 // -----------------------------------------------------------------------------
       
  3020 //
       
  3021 TInt CUpnpContentDirectory::FindSharedFolderL( const TDesC& aUrlPath,
       
  3022     const TDesC& aFileName, HBufC*& aSystemPath )
       
  3023     {
       
  3024     TInt ret = KErrNotFound;
       
  3025     aSystemPath = NULL;
       
  3026     if ( aUrlPath.Length( ) )
       
  3027         {
       
  3028         // validate the path string
       
  3029         if ( IsValidUrlPath( aUrlPath ) )
       
  3030             {
       
  3031             // get resource id
       
  3032             TLex lexer(aUrlPath);
       
  3033             TInt64 resId;
       
  3034             if ( !lexer.Val( resId ) )
       
  3035                 {
       
  3036                 // get resource
       
  3037                 CUpnpResourcesBean* resBn = iContentDirectoryDb->
       
  3038                 GetResourceL( resId );
       
  3039                 if ( resBn )
       
  3040                     {
       
  3041                     CleanupStack::PushL( resBn );
       
  3042                     aSystemPath = resBn->Path().AllocL( );
       
  3043                     TInt objectId = resBn->ObjectId( );
       
  3044                     CleanupStack::PopAndDestroy( resBn );
       
  3045 
       
  3046                     // make sure that systam path points to the wanted file
       
  3047                     TParse fp;
       
  3048                     fp.Set( *aSystemPath, 0, 0 );
       
  3049                     HBufC* tmp = aFileName.AllocLC( );
       
  3050                     TPtr tmpPtr(tmp->Des( ));
       
  3051                     UpnpString::ReplaceHttpCharactersL( tmpPtr );
       
  3052                     TPtrC ext = fp.Ext( );
       
  3053                     HBufC* file = HBufC::NewLC( KMaxIntegerLen
       
  3054                             + ext.Length( ) );
       
  3055                     TBuf<KMaxIntegerLen> num;
       
  3056                     num.Num( objectId );
       
  3057                     file->Des().Append( num );
       
  3058                     file->Des().Append( ext );
       
  3059 
       
  3060                     if ( file->Des( ) == tmpPtr )
       
  3061                         {
       
  3062                         ret = KErrNone;
       
  3063                         }
       
  3064                     CleanupStack::PopAndDestroy( file );
       
  3065                     CleanupStack::PopAndDestroy( tmp );
       
  3066                     }
       
  3067                 }
       
  3068             }
       
  3069         }
       
  3070     else
       
  3071         { // default download dir
       
  3072         aSystemPath = iDownloadDir->AllocL( );
       
  3073         }
       
  3074 
       
  3075     return ret;
       
  3076     }
       
  3077 
       
  3078 // -----------------------------------------------------------------------------
       
  3079 // CUpnpContentDirectory::IsValidUrlPathL
       
  3080 // -----------------------------------------------------------------------------
       
  3081 //
       
  3082 TBool CUpnpContentDirectory::IsValidUrlPath( const TDesC& aUrlPath )
       
  3083     {
       
  3084     TInt ret = ETrue;
       
  3085     if ( (aUrlPath.Length( ) != KMaxIntegerLen + KRandomRangeLength)
       
  3086             && (aUrlPath.Length( ) != KMaxIntegerLen + KRandomRangeLength
       
  3087                     + KRandomRangeLength) )
       
  3088         {
       
  3089         ret = EFalse;
       
  3090         }
       
  3091     else
       
  3092         {
       
  3093         for ( TInt i = 0; i < aUrlPath.Length( ); i++ )
       
  3094             if ( aUrlPath[i] < '0' || aUrlPath[i] > '9' )
       
  3095                 {
       
  3096                 ret = EFalse;
       
  3097                 break;
       
  3098                 }
       
  3099         }
       
  3100 
       
  3101     return ret;
       
  3102     }
       
  3103 // -----------------------------------------------------------------------------
       
  3104 // CUpnpContentDirectory::GetItemTypeFromMimeTypeLC
       
  3105 // -----------------------------------------------------------------------------
       
  3106 HBufC8* CUpnpContentDirectory::GetItemTypeFromMimeTypeLC(
       
  3107     const TDesC8& aMimeType )
       
  3108     {
       
  3109     HBufC8* result = NULL;
       
  3110     if ( aMimeType.Compare( KNullDesC8( ) ) == 0 )
       
  3111         {
       
  3112         return result;
       
  3113         }
       
  3114     TInt indexOfSlash = aMimeType.Find( KSlash8( ) );
       
  3115     if ( indexOfSlash < KErrNone )
       
  3116         {
       
  3117         return result;
       
  3118         }
       
  3119     TPtrC8 firstValue = aMimeType.Left( indexOfSlash );
       
  3120     if ( firstValue.Compare( KImageMime( ) ) == 0 )
       
  3121         {
       
  3122         result = KObjectClassImageItem().AllocLC( );
       
  3123         }
       
  3124     else if ( firstValue.Compare( KTextMime( ) ) == 0 )
       
  3125         {
       
  3126         result = KObjectClassTextItem().AllocLC( );
       
  3127         }
       
  3128     else if ( firstValue.Compare( KVideoMime( ) ) == 0 )
       
  3129         {
       
  3130         result = KObjectClassVideoItem().AllocLC( );
       
  3131         }
       
  3132     else if ( firstValue.Compare( KAudioMime( ) ) == 0 )
       
  3133         {
       
  3134         result = KObjectClassAudioItem().AllocLC( );
       
  3135         }
       
  3136     return result;
       
  3137     }
       
  3138 
       
  3139 // -----------------------------------------------------------------------------
       
  3140 // CUpnpContentDirectory::GetItemTypeFromMimeTypeLC
       
  3141 // -----------------------------------------------------------------------------
       
  3142 //
       
  3143 void CUpnpContentDirectory::CheckItemTypeForObjectL( RXmlEngDocument& aObj,
       
  3144     const TDesC8& aMimeType )
       
  3145     {
       
  3146     //  temporary disable because of mime-type recognition problem 
       
  3147     HBufC8* itemType = GetItemTypeFromMimeTypeLC( aMimeType );
       
  3148     if ( !itemType )
       
  3149         {
       
  3150         return;
       
  3151         }
       
  3152     TXmlEngElement obj = aObj.DocumentElement( );
       
  3153     TXmlEngElement objClass;
       
  3154     UpnpDomInterface::GetElementL( obj, objClass, KObjClassColName8 );
       
  3155     TPtrC8 classValue(UpnpDomInterface::GetElementValueL( objClass ));
       
  3156     if ( classValue.Compare( itemType->Des( ) ) < 0 )
       
  3157         {
       
  3158         objClass.SetTextL( *itemType );
       
  3159         }
       
  3160     CleanupStack::PopAndDestroy( itemType );
       
  3161 
       
  3162     }
       
  3163 
       
  3164 // -----------------------------------------------------------------------------
       
  3165 // CUpnpContentDirectory::SetDownloadDirectoryL
       
  3166 // -----------------------------------------------------------------------------
       
  3167 //
       
  3168 void CUpnpContentDirectory::SetDownloadDirectoryL( const TDesC& aDownloadDir )
       
  3169     {
       
  3170     // perform necessary changes in database
       
  3171     iContentDirectoryDb->NewMediaDirectoryL( *iDownloadDir );
       
  3172 
       
  3173     // alloc new string
       
  3174     delete iDownloadDir;
       
  3175     iDownloadDir = NULL;
       
  3176     TPtrC last = aDownloadDir.Right( 1 );
       
  3177     if ( last.Compare( KDiskPathElSep )!=0 )
       
  3178         {
       
  3179         iDownloadDir = HBufC::NewL( aDownloadDir.Length( )+ KDiskPathElSep().Length( ) );
       
  3180         iDownloadDir->Des().Copy( aDownloadDir );
       
  3181         iDownloadDir->Des().Append( KDiskPathElSep );
       
  3182         }
       
  3183     else
       
  3184         {
       
  3185         iDownloadDir = HBufC::NewL( aDownloadDir.Length( ) );
       
  3186         iDownloadDir->Des().Copy( aDownloadDir );
       
  3187         }
       
  3188     }
       
  3189 // -----------------------------------------------------------------------------
       
  3190 // CUpnpContentDirectory::SetThumbnailDirectoryL
       
  3191 // -----------------------------------------------------------------------------
       
  3192 //
       
  3193 void CUpnpContentDirectory::SetThumbnailDirectoryL( const TDesC& aDownloadDir )
       
  3194     {
       
  3195     // perform necessary changes in database
       
  3196     iContentDirectoryDb->NewMediaDirectoryL( *iThumbDir );
       
  3197 
       
  3198     // alloc new string
       
  3199     delete iThumbDir;
       
  3200     iThumbDir = NULL;
       
  3201     iThumbDir = aDownloadDir.AllocL( );
       
  3202     }
       
  3203 // -----------------------------------------------------------------------------
       
  3204 // CUpnpContentDirectory::HandleDbError
       
  3205 // -----------------------------------------------------------------------------
       
  3206 //
       
  3207 TInt CUpnpContentDirectory::HandleDbError( TInt aError )
       
  3208     {
       
  3209     return iContentDirectoryDb->HandleDbError( aError );
       
  3210     }
       
  3211 
       
  3212 // -----------------------------------------------------------------------------
       
  3213 // CUpnpContentDirectory::DataBaseHasBeenRecoveredL
       
  3214 // -----------------------------------------------------------------------------
       
  3215 //
       
  3216 TBool CUpnpContentDirectory::DataBaseHasBeenRecoveredL( TInt aError )
       
  3217     {
       
  3218     TInt err = aError;
       
  3219     if ( err == KErrCorrupt )
       
  3220         {
       
  3221         err = HandleDbError( err );
       
  3222         }
       
  3223     return (err == KErrNone && aError == KErrCorrupt ) ? ETrue : EFalse;
       
  3224     }
       
  3225 
       
  3226 // -----------------------------------------------------------------------------
       
  3227 // CUpnpContentDirectory::IsDbOrDiskProblemL
       
  3228 // -----------------------------------------------------------------------------
       
  3229 //
       
  3230 TBool CUpnpContentDirectory::IsDbOrDiskProblemL( CUpnpAction* aAction )
       
  3231     {
       
  3232     TBool noSpace = SysUtil::DiskSpaceBelowCriticalLevelL( &iFs,
       
  3233         KMinDiskSpace, EDriveC );
       
  3234     TInt error( KErrNone );
       
  3235     if ( !noSpace )
       
  3236         {
       
  3237         if ( !IsDbCreated() )
       
  3238             {
       
  3239             error = HandleDbError( KErrCorrupt );
       
  3240             }
       
  3241         }
       
  3242     if ( noSpace || error != KErrNone )
       
  3243         {
       
  3244         DoSendActionL( aAction, ECannotProcess );
       
  3245         return ETrue;
       
  3246         }
       
  3247     return EFalse;
       
  3248     }
       
  3249 
       
  3250 // -----------------------------------------------------------------------------
       
  3251 // CUpnpContentDirectory::IsDbCreated
       
  3252 // -----------------------------------------------------------------------------
       
  3253 //
       
  3254 TBool CUpnpContentDirectory::IsDbCreated()
       
  3255     {
       
  3256     return iContentDirectoryDb->IsDbCreated( );
       
  3257     }
       
  3258 // -----------------------------------------------------------------------------
       
  3259 // CUpnpContentDirectory::CreateNameForThumbnailL
       
  3260 // -----------------------------------------------------------------------------
       
  3261 //
       
  3262 HBufC* CUpnpContentDirectory::CreateNameForThumbnailL( const TDesC& aFileName )
       
  3263     {
       
  3264     // only name
       
  3265     TParse fp;
       
  3266     User::LeaveIfError( fp.Set( aFileName, 0, 0 ) );
       
  3267     TFileName bareName = fp.Name( );
       
  3268     // full name
       
  3269     User::LeaveIfError( fp.Set( bareName, iThumbDir, &KDefaultThumbExtension ) );
       
  3270     TFileName fullName = fp.FullName( );
       
  3271 
       
  3272     //check if exists
       
  3273     TUint ignored;
       
  3274     TInt q = 0;
       
  3275     while ( iFs.Att( fullName, ignored ) == KErrNone )
       
  3276         {
       
  3277         q++;
       
  3278         TBuf<KMaxIntegerLen> num;
       
  3279         num.Num( q );
       
  3280         TFileName tmpName;
       
  3281         tmpName.Copy( bareName );
       
  3282         tmpName.Append( KUndersc );
       
  3283         tmpName.Append( num );
       
  3284         fp.Set( tmpName, iThumbDir, &KDefaultThumbExtension );
       
  3285         fullName = fp.FullName( );
       
  3286         }
       
  3287 
       
  3288     return fullName.AllocL( );
       
  3289     }
       
  3290 
       
  3291 // -----------------------------------------------------------------------------
       
  3292 // CUpnpContentDirectory::AutoDestroyEventL
       
  3293 // -----------------------------------------------------------------------------
       
  3294 //
       
  3295 void CUpnpContentDirectory::AutoDestroyEventL( TInt aObjId, TBool aDeleteObj )
       
  3296     {
       
  3297     if ( !ValidateObjectL( aObjId ) )
       
  3298         {
       
  3299         return;
       
  3300         }
       
  3301 
       
  3302     if ( aDeleteObj )
       
  3303         {
       
  3304         TBuf8<KMaxIntegerLen> containerID;
       
  3305         containerID.Num( iContentDirectoryDb->GetParentIdL( aObjId ) );
       
  3306         RArray<TInt> refList;
       
  3307         CleanupClosePushL( refList );
       
  3308         iContentDirectoryDb->DeleteObjectL( aObjId, refList, ETrue );
       
  3309 
       
  3310         // supporting ContainerUpdateIDs behaviour
       
  3311         iStateHandler->IncreaseSystemUpdateIdL( );
       
  3312         iStateHandler->IncreaseContainerUpdateIdL( containerID );
       
  3313         for ( TInt i(0); i<refList.Count( ); i++ )
       
  3314             {
       
  3315             iStateHandler->IncreaseSystemUpdateIdL( );
       
  3316             containerID.Num( refList[i] );
       
  3317             iStateHandler->IncreaseContainerUpdateIdL( containerID );
       
  3318             }
       
  3319 
       
  3320         // clean up
       
  3321         CleanupStack::PopAndDestroy( &refList );
       
  3322         }
       
  3323     iAutoDestroyObjects->RemoveAndDestroyL( iAutoDestroyObjects->FindObjIndexByObjId( aObjId ) );
       
  3324     }
       
  3325 
       
  3326 // -----------------------------------------------------------------------------
       
  3327 // CUpnpContentDirectory::HttpTransferEventReceivedL
       
  3328 // -----------------------------------------------------------------------------
       
  3329 //
       
  3330 EXPORT_C void CUpnpContentDirectory::HttpTransferEventReceivedL( CUpnpHttpMessage& aHttpMsg )
       
  3331     {
       
  3332     switch ( aHttpMsg.Type( ) )
       
  3333         {
       
  3334         case ETransferStart:
       
  3335             {
       
  3336             TransferStartL( aHttpMsg.SenderPathFromHeader( ) );
       
  3337 
       
  3338             CUpnpFileTransfer::TTransferType type;
       
  3339             TInt objectid(0);
       
  3340 
       
  3341             if ( aHttpMsg.Method().Find( KHttpGet ) == 0 )
       
  3342                 {
       
  3343                 objectid = iContentDirectoryDb->GetObjIdByAttrL( KImportUri8,
       
  3344                     aHttpMsg.SenderPathFromHeader( ) );
       
  3345                 type = CUpnpFileTransfer::EImport;
       
  3346                 }
       
  3347             else
       
  3348                 {
       
  3349                 objectid = iContentDirectoryDb->GetObjIdByAttrLikeL(
       
  3350                     KImportUri8, aHttpMsg.SenderPathFromHeader( ) );
       
  3351                 type = CUpnpFileTransfer::EUpload;
       
  3352 
       
  3353                 }
       
  3354             CUpnpFileTransfer* transfer = CUpnpFileTransfer::NewLC(
       
  3355                 aHttpMsg.SessionId( ), objectid, type );
       
  3356 
       
  3357             transfer->SetTransferState(:: ETransferInProgress);
       
  3358             transfer->SetImportNumber(UpnpCdUtils::ResIdFromUriL(
       
  3359                     aHttpMsg.SenderPathFromHeader()));
       
  3360 
       
  3361             iTransferController->iFileTransfers.Append(transfer);
       
  3362             CleanupStack::Pop( transfer );
       
  3363 
       
  3364             iTransferController->AddIncomingTransferInternalL(&aHttpMsg);
       
  3365             break;
       
  3366             }
       
  3367         case ETransferError:
       
  3368             {
       
  3369             iTransferController->TransferFailL( &aHttpMsg );
       
  3370             break;
       
  3371             }
       
  3372         default:
       
  3373         break;
       
  3374         }
       
  3375     }
       
  3376 
       
  3377 // -----------------------------------------------------------------------------
       
  3378 // CUpnpContentDirectory::ImportComplete
       
  3379 // -----------------------------------------------------------------------------
       
  3380 //
       
  3381 void CUpnpContentDirectory::TransferProgress( TInt aId, TInt aBytes,
       
  3382     TInt aTotalBytes )
       
  3383     {
       
  3384     TInt transferId = iTransferController->Transfer( aId );
       
  3385     if ( transferId != KErrNotFound )
       
  3386         {
       
  3387         (iTransferController->iFileTransfers[ transferId ])->SetTransferLength( aBytes );
       
  3388         (iTransferController->iFileTransfers[ transferId ])->SetTransferTotal( aTotalBytes );
       
  3389         }
       
  3390     }
       
  3391 
       
  3392 // -----------------------------------------------------------------------------
       
  3393 // CUpnpContentDirectory::TransferStartL
       
  3394 // -----------------------------------------------------------------------------
       
  3395 //
       
  3396 void CUpnpContentDirectory::TransferStartL( const TDesC8& aImpUri )
       
  3397     {
       
  3398     TInt index = iAutoDestroyObjects->FindObjIndexByResId( UpnpCdUtils::ResIdFromUriL( aImpUri ) );
       
  3399     if ( index != KErrNotFound )
       
  3400         {
       
  3401         (*iAutoDestroyObjects)[index]->SetTransferStartedL( ETrue );
       
  3402         }
       
  3403     }
       
  3404 
       
  3405 // -----------------------------------------------------------------------------
       
  3406 // CUpnpContentDirectory::TransferCompleteL
       
  3407 // -----------------------------------------------------------------------------
       
  3408 //
       
  3409 void CUpnpContentDirectory::TransferCompleteL( TInt aResId, TInt aObjId )
       
  3410     {
       
  3411     TInt index = iAutoDestroyObjects->FindObjIndexByResId( aResId );
       
  3412     if ( index != KErrNotFound )
       
  3413         {
       
  3414         iAutoDestroyObjects->RemoveAndDestroyL( index );
       
  3415         }
       
  3416 
       
  3417     if ( (aObjId>=0)&&(aResId>=0)
       
  3418             &&(iContentDirectoryDb->GetParentIdL( aObjId )>=0) )
       
  3419         {
       
  3420         TBuf8<KMaxIntegerLen> containerID;
       
  3421         containerID.Num( iContentDirectoryDb->GetParentIdL( aObjId ) );
       
  3422         iStateHandler->IncreaseSystemUpdateIdL( );
       
  3423         iStateHandler->IncreaseContainerUpdateIdL( containerID );
       
  3424         }
       
  3425     }
       
  3426 
       
  3427 // -----------------------------------------------------------------------------
       
  3428 // CUpnpContentDirectory::NotifyTransferFailL
       
  3429 // -----------------------------------------------------------------------------
       
  3430 //
       
  3431 void CUpnpContentDirectory::NotifyTransferFailL( TInt aSessionId,
       
  3432     TInt aStatusCode )
       
  3433     {
       
  3434     CUpnpFileTransferEvent* event =
       
  3435             iTransferController->RemoveTransferLC( aSessionId );
       
  3436     if ( event )
       
  3437         {
       
  3438         event->SetProgressState( CUpnpFileTransferEvent::EFailed );
       
  3439         event->SetErrorCode( aStatusCode );
       
  3440         CleanupStack::Pop( event );
       
  3441         iEventObserver->FileTransferEvent( event );
       
  3442         }
       
  3443 
       
  3444     }
       
  3445 // -----------------------------------------------------------------------------
       
  3446 // CUpnpContentDirectory::NotifyTransferStoppedL
       
  3447 // -----------------------------------------------------------------------------
       
  3448 //
       
  3449 void CUpnpContentDirectory::NotifyTransferStoppedL( TInt aSessionId,
       
  3450     CUpnpFileTransfer::TTransferType aType )
       
  3451     {
       
  3452     CUpnpFileTransferEvent* event =
       
  3453             iTransferController->RemoveTransferLC( aSessionId );
       
  3454     if ( event )
       
  3455         {
       
  3456         event->SetProgressState( CUpnpFileTransferEvent::EStopped );
       
  3457         if ( aType == CUpnpFileTransfer::EImport )
       
  3458             {
       
  3459             event->SetDirection( CUpnpFileTransferEvent::EIncoming );
       
  3460             }
       
  3461         else
       
  3462             {
       
  3463             event->SetDirection( CUpnpFileTransferEvent::EOutgoing );
       
  3464             }
       
  3465 
       
  3466         CleanupStack::Pop( event );
       
  3467         iEventObserver->FileTransferEvent( event );
       
  3468         }
       
  3469 
       
  3470     }
       
  3471 // -----------------------------------------------------------------------------
       
  3472 // CUpnpContentDirectory::NotifyOutgoingTransferL
       
  3473 // -----------------------------------------------------------------------------
       
  3474 //
       
  3475 void CUpnpContentDirectory::NotifyOutgoingTransferL( TInt aSessionId )
       
  3476     {
       
  3477     CUpnpFileTransferEvent* event =
       
  3478             iTransferController->RemoveTransferLC( aSessionId );
       
  3479     if ( event )
       
  3480         {
       
  3481         CleanupStack::Pop( event );
       
  3482         //send event to Media Server        
       
  3483         iEventObserver->FileTransferEvent( event );
       
  3484         }
       
  3485     }
       
  3486 
       
  3487 // -----------------------------------------------------------------------------
       
  3488 // CUpnpContentDirectory::SetSender
       
  3489 // -----------------------------------------------------------------------------
       
  3490 //
       
  3491 EXPORT_C void CUpnpContentDirectory::SetSender( MUpnpCdsSender* aSender )
       
  3492     {
       
  3493     iSender = aSender;
       
  3494     }
       
  3495 
       
  3496 // -----------------------------------------------------------------------------
       
  3497 // CUpnpContentDirectory::DoSendActionL
       
  3498 // -----------------------------------------------------------------------------
       
  3499 //
       
  3500 void CUpnpContentDirectory::DoSendActionL( CUpnpAction* aAction,
       
  3501     TUpnpErrorCode aError )
       
  3502     {
       
  3503     iSender->SendL( aAction, aError );
       
  3504     }
       
  3505 // -----------------------------------------------------------------------------
       
  3506 // CUpnpContentDirectory::DoSendActionL
       
  3507 // -----------------------------------------------------------------------------
       
  3508 //
       
  3509 void CUpnpContentDirectory::DoSendMessageL( CUpnpHttpMessage* aMessage )
       
  3510     {
       
  3511     iSender->SendL( aMessage );
       
  3512     }
       
  3513 // -----------------------------------------------------------------------------
       
  3514 // CUpnpContentDirectory::SendActionL
       
  3515 // -----------------------------------------------------------------------------
       
  3516 //
       
  3517 EXPORT_C void CUpnpContentDirectory::SendActionL( CUpnpAction* aAction,
       
  3518     TUpnpErrorCode aError )
       
  3519     {
       
  3520     SendL( aAction, aError );
       
  3521     }
       
  3522 // -----------------------------------------------------------------------------
       
  3523 // CUpnpContentDirectory::DoSendActionL
       
  3524 // -----------------------------------------------------------------------------
       
  3525 //
       
  3526 EXPORT_C void CUpnpContentDirectory::SendMessageL( CUpnpHttpMessage* aMessage )
       
  3527     {
       
  3528     SendL( aMessage );
       
  3529     }
       
  3530 
       
  3531 // -----------------------------------------------------------------------------
       
  3532 // CUpnpContentDirectory::StateVariableEvented
       
  3533 // -----------------------------------------------------------------------------
       
  3534 //
       
  3535 void CUpnpContentDirectory::StateVariableEvented( const TDesC8& aVariableName )
       
  3536     {
       
  3537     iStateHandler->StateVariableEvented( aVariableName );
       
  3538     }
       
  3539 // -----------------------------------------------------------------------------
       
  3540 // CUpnpContentDirectory::StateHandler
       
  3541 // -----------------------------------------------------------------------------
       
  3542 //
       
  3543 CUpnpStateHandler* CUpnpContentDirectory::StateHandler()
       
  3544     {
       
  3545     return iStateHandler;
       
  3546     }
       
  3547 
       
  3548 // helper functions
       
  3549 // -----------------------------------------------------------------------------
       
  3550 // CUpnpContentDirectory::ExecutedAction
       
  3551 // -----------------------------------------------------------------------------
       
  3552 //
       
  3553 TCurrentAction CUpnpContentDirectory::ExecutedAction()
       
  3554     {
       
  3555     return iCurrentAction;
       
  3556     }
       
  3557 
       
  3558 // -----------------------------------------------------------------------------
       
  3559 // CUpnpContentDirectory::SetExecutedAction
       
  3560 // -----------------------------------------------------------------------------
       
  3561 //
       
  3562 void CUpnpContentDirectory::SetExecutedAction( TCurrentAction aAction )
       
  3563     {
       
  3564     iCurrentAction = aAction;
       
  3565     }
       
  3566 
       
  3567 // -----------------------------------------------------------------------------
       
  3568 // CUpnpContentDirectory::HttpResponseReceivedL
       
  3569 // -----------------------------------------------------------------------------
       
  3570 //
       
  3571 void CUpnpContentDirectory::HttpResponseReceivedL( TInt aSessionId,
       
  3572     TInt aStatusCode )
       
  3573     {
       
  3574     iTransferController->HttpResponseReceivedL( aSessionId, aStatusCode );
       
  3575     }
       
  3576 
       
  3577 // -----------------------------------------------------------------------------
       
  3578 // CUpnpContentDirectory::EventObserver
       
  3579 // -----------------------------------------------------------------------------
       
  3580 //
       
  3581 MUpnpContentDirectoryEventObserver* CUpnpContentDirectory::EventObserver()
       
  3582     {
       
  3583     return iEventObserver;
       
  3584     }
       
  3585 
       
  3586 // -----------------------------------------------------------------------------
       
  3587 // CUpnpContentDirectory::ContentDirectoryDb
       
  3588 // -----------------------------------------------------------------------------
       
  3589 //
       
  3590 CUpnpContentDirectoryDb* CUpnpContentDirectory::ContentDirectoryDb()
       
  3591     {
       
  3592     return iContentDirectoryDb;
       
  3593     }
       
  3594 
       
  3595 // -----------------------------------------------------------------------------
       
  3596 // CUpnpContentDirectory::IsObjectExistL
       
  3597 // -----------------------------------------------------------------------------
       
  3598 //    
       
  3599 TBool CUpnpContentDirectory::IsObjectExistL( TInt aObjectId )
       
  3600     {
       
  3601     if ( iContentDirectoryDb->CheckObjectRestrictionL( aObjectId )
       
  3602             == ENoSuchObject )
       
  3603         {
       
  3604         return EFalse;
       
  3605         }
       
  3606     else
       
  3607         {
       
  3608         return ETrue;
       
  3609         }
       
  3610     }
       
  3611 
       
  3612 // -----------------------------------------------------------------------------
       
  3613 // CUpnpContentDirectory::ValidateObjectL
       
  3614 // -----------------------------------------------------------------------------
       
  3615 //   
       
  3616 TBool CUpnpContentDirectory::ValidateObjectL( TInt aObjectId )
       
  3617     {
       
  3618     //removes from autodestroy array orphaned objects
       
  3619     TInt i(0);
       
  3620     TInt index(0);
       
  3621     while ( i < iAutoDestroyObjects->Count( ) )
       
  3622         {
       
  3623         TInt parentID( iContentDirectoryDb->GetParentIdL(
       
  3624                         ( *iAutoDestroyObjects )[ i ]->GetObjId( ) ) );
       
  3625         if ( parentID < 0 )
       
  3626             {
       
  3627             index = iAutoDestroyObjects->FindObjIndexByObjId( (
       
  3628                                              *iAutoDestroyObjects )[ i ]->GetObjId( ) );
       
  3629             iAutoDestroyObjects->RemoveAndDestroyL( index );
       
  3630             continue;
       
  3631             }
       
  3632         i++;
       
  3633         }
       
  3634 
       
  3635     //checks if given object exists
       
  3636     if ( !IsObjectExistL( aObjectId ) )
       
  3637         {
       
  3638         index = iAutoDestroyObjects->FindObjIndexByObjId( aObjectId );
       
  3639         if ( index != KErrNotFound )
       
  3640             {
       
  3641             iAutoDestroyObjects->RemoveAndDestroyL( index );
       
  3642             }
       
  3643         }
       
  3644 
       
  3645     //cheks if given object exists in autodestroy array
       
  3646     index = iAutoDestroyObjects->FindObjIndexByObjId( aObjectId );
       
  3647     if ( index != KErrNotFound )
       
  3648         {
       
  3649         return ETrue;
       
  3650         }
       
  3651 
       
  3652     return EFalse;
       
  3653     }
       
  3654 
       
  3655 // -----------------------------------------------------------------------------
       
  3656 // CUpnpContentDirectory::TransferHandler
       
  3657 // -----------------------------------------------------------------------------
       
  3658 //
       
  3659 MUpnpTransferInterface* CUpnpContentDirectory::TransferHandler()
       
  3660     {
       
  3661     return iTransferHandler;
       
  3662     }
       
  3663 
       
  3664 // -----------------------------------------------------------------------------
       
  3665 // CUpnpContentDirectory::TransferCompletedL
       
  3666 // -----------------------------------------------------------------------------
       
  3667 //    
       
  3668 void CUpnpContentDirectory::TransferCompletedL( TInt aKey, TInt aResultCode )
       
  3669     {
       
  3670     HttpResponseReceivedL( aKey, aResultCode );
       
  3671     if ( aResultCode == EHttpOk )
       
  3672         {
       
  3673         TInt transferid = iTransferController->Transfer( aKey );
       
  3674         if ( transferid > KErrNotFound )
       
  3675             {
       
  3676             TInt
       
  3677                     len =
       
  3678                             iTransferController->iFileTransfers[transferid]->TransferTotal( );
       
  3679             iTransferController->iFileTransfers[transferid]->SetTransferLength( len );
       
  3680             }
       
  3681         }
       
  3682     }
       
  3683     
       
  3684 // -----------------------------------------------------------------------------
       
  3685 // CUpnpContentDirectory::NewTransactionL
       
  3686 // -----------------------------------------------------------------------------
       
  3687 //
       
  3688 EXPORT_C void CUpnpContentDirectory::NewTransactionL(
       
  3689         const TDesC8& aMethod, const TDesC8& aUri, 
       
  3690         const TInetAddr& aSender, CUpnpHttpServerTransaction*& aResultTrans )
       
  3691     {
       
  3692     iDlnaFilter->NewTransactionL( aMethod, aUri, aSender, aResultTrans );
       
  3693     }
       
  3694 
       
  3695 // -----------------------------------------------------------------------------
       
  3696 // CUpnpContentDirectory::HttpEventLD
       
  3697 // -----------------------------------------------------------------------------
       
  3698 //
       
  3699 void CUpnpContentDirectory::HttpEventLD( CUpnpHttpMessage* aMessage )
       
  3700     {
       
  3701     if ( aMessage )
       
  3702         {
       
  3703         CleanupStack::PushL( aMessage );
       
  3704         if( aMessage->Type() >= ETransferStart )
       
  3705             {
       
  3706             HttpTransferEventReceivedL( *aMessage );
       
  3707             }
       
  3708         else
       
  3709             {
       
  3710             UnknownHttpEventReceivedL( *aMessage );
       
  3711             }
       
  3712         CleanupStack::PopAndDestroy( aMessage );
       
  3713         }
       
  3714     }
       
  3715 
       
  3716 // -----------------------------------------------------------------------------
       
  3717 // CUpnpContentDirectory::AddressChangeL
       
  3718 // -----------------------------------------------------------------------------
       
  3719 //
       
  3720 void CUpnpContentDirectory::AddressChangeL()
       
  3721     {
       
  3722     iHttpServerSession->Stop();
       
  3723     iHttpServerSession->StartL();        
       
  3724     }
       
  3725 
       
  3726 //End of File