mmappcomponents/mmmtpdataprovider/mmmtpdprequestprocessor/src/cmoveobject.cpp
changeset 0 a2952bb97e68
child 9 bee149131e4b
child 25 d881023c13eb
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implement the operation: MoveObject
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bautils.h>
       
    20 #include <mtp/mmtpdataproviderframework.h>
       
    21 #include <mtp/mmtpobjectmgr.h>
       
    22 #include <mtp/mmtpstoragemgr.h>
       
    23 #include <mtp/mmtpreferencemgr.h>
       
    24 #include <mtp/cmtpobjectmetadata.h>
       
    25 #include <mtp/cmtptypearray.h>
       
    26 #include <mtp/cmtptypestring.h>
       
    27 #include <mtp/cmtptypeobjectproplist.h>
       
    28 
       
    29 #include "cmoveobject.h"
       
    30 #include "mmmtpdplogger.h"
       
    31 #include "tmmmtpdppanic.h"
       
    32 #include "mmmtpdputility.h"
       
    33 #include "cmmmtpdpmetadataaccesswrapper.h"
       
    34 #include "mmmtpdpconfig.h"
       
    35 
       
    36 /**
       
    37 * Verification data for the MoveObject request
       
    38 */
       
    39 const TMTPRequestElementInfo KMTPMoveObjectPolicy[] =
       
    40     {
       
    41         {
       
    42         TMTPTypeRequest::ERequestParameter1, EMTPElementTypeObjectHandle,
       
    43                 EMTPElementAttrFileOrDir | EMTPElementAttrWrite, 0, 0, 0
       
    44         },
       
    45         {
       
    46         TMTPTypeRequest::ERequestParameter2, EMTPElementTypeStorageId,
       
    47                 EMTPElementAttrWrite, 0, 0, 0
       
    48         },
       
    49         {
       
    50         TMTPTypeRequest::ERequestParameter3, EMTPElementTypeObjectHandle,
       
    51                 EMTPElementAttrDir | EMTPElementAttrWrite, 1, 0, 0
       
    52         }
       
    53     };
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CMoveObject::NewL
       
    57 // Two-phase construction method
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 //EXPORT_C MMmRequestProcessor* CMoveObject::NewL( MMTPDataProviderFramework& aFramework,
       
    61 //        MMTPConnection& aConnection,
       
    62 //        CMmMtpDpMetadataAccessWrapper& aWrapper )
       
    63 //    {
       
    64 //    CMoveObject* self = new (ELeave) CMoveObject( aFramework, aConnection, aWrapper );
       
    65 //    CleanupStack::PushL( self );
       
    66 //    self->ConstructL();
       
    67 //    CleanupStack::Pop( self );
       
    68 //
       
    69 //    return self;
       
    70 //    }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CMoveObject::~CMoveObject
       
    74 // Destructor
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 EXPORT_C CMoveObject::~CMoveObject()
       
    78     {
       
    79     Cancel();
       
    80 
       
    81     delete iDest;
       
    82     delete iFileMan;
       
    83     delete iPathToMove;
       
    84     delete iNewRootFolder;
       
    85     iObjectHandles.Close();
       
    86     if ( iPropertyElement )
       
    87         delete iPropertyElement;
       
    88     delete iPropertyList;
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CMoveObject::CMoveObject
       
    93 // Standard c++ constructor
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 EXPORT_C CMoveObject::CMoveObject( MMTPDataProviderFramework& aFramework,
       
    97         MMTPConnection& aConnection,
       
    98         MMmMtpDpConfig& aDpConfig) :
       
    99     CRequestProcessor( aFramework, aConnection, sizeof( KMTPMoveObjectPolicy )
       
   100             /sizeof( TMTPRequestElementInfo ), KMTPMoveObjectPolicy),
       
   101     iDpConfig( aDpConfig ),
       
   102     iObjectHandles( KMmMtpRArrayGranularity ),
       
   103     iMoveObjectIndex( 0 )
       
   104     {
       
   105     PRINT( _L( "Operation: MoveObject(0x1019)" ) );
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CMoveObject::ServiceL
       
   110 // MoveObject request handler
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 EXPORT_C void CMoveObject::ServiceL()
       
   114     {
       
   115     PRINT( _L( "MM MTP => CMoveObject::ServiceL" ) );
       
   116     TMTPResponseCode ret = MoveObjectL();
       
   117     PRINT1( _L( "MM MTP <> CMoveObject::ServiceL ret = 0x%x" ), ret );
       
   118     if ( EMTPRespCodeOK != ret )
       
   119         {
       
   120         SendResponseL( ret );
       
   121         }
       
   122     PRINT( _L( "MM MTP <= CMoveObject::ServiceL" ) );
       
   123     }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CMoveObject::ConstructL
       
   127 // Second phase constructor
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 EXPORT_C void CMoveObject::ConstructL()
       
   131     {
       
   132     CActiveScheduler::Add( this );
       
   133 
       
   134     iPropertyList = CMTPTypeObjectPropList::NewL();
       
   135     SetPSStatus();
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CMoveObject::RunL
       
   140 //
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 EXPORT_C void CMoveObject::RunL()
       
   144     {
       
   145     PRINT( _L( "MM MTP => CMoveObject::RunL" ) );
       
   146     if ( MoveOwnedObjectsL() )
       
   147         {
       
   148         // Reset iMoveObjectIndex count since move completed
       
   149         iMoveObjectIndex = 0;
       
   150 
       
   151         TMTPResponseCode ret = EMTPRespCodeOK;
       
   152         // Delete the original folders in the device dp.
       
   153         if ( iObjectInfo->Uint( CMTPObjectMetaData::EDataProviderId )
       
   154                 == iFramework.DataProviderId() )
       
   155             {
       
   156             ret = FinalPhaseMove();
       
   157             }
       
   158         PRINT1( _L("MM MTP <> CMoveObject::RunL ret = 0x%x"), ret );
       
   159         SendResponseL( ret );
       
   160         }
       
   161     PRINT( _L( "MM MTP <= CMoveObject::RunL" ) );
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CMoveObject::RunError
       
   166 //
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 EXPORT_C TInt CMoveObject::RunError( TInt aError )
       
   170     {
       
   171     if ( aError != KErrNone )
       
   172         PRINT1( _L( "MM MTP <> CMoveObject::RunError aError = %d" ), aError );
       
   173     TRAP_IGNORE( SendResponseL( EMTPRespCodeGeneralError ) );
       
   174     return KErrNone;
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CMoveObject::MoveFileL
       
   179 // A helper function of MoveObjectL
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void CMoveObject::MoveFileL( const TDesC& aNewFileName )
       
   183     {
       
   184     PRINT1( _L( "MM MTP => CMoveObject::MoveFileL aNewFileName = %S" ), &aNewFileName );
       
   185     const TDesC& suid( iObjectInfo->DesC( CMTPObjectMetaData::ESuid ) );
       
   186     TFileName oldFileName( suid ); // save the old file name, the suid will be modified
       
   187     PRINT1( _L( "MM MTP <> CMoveObject::MoveFileL oldFileName = %S" ), &suid );
       
   188 
       
   189     if ( iStorageId == iObjectInfo->Uint( CMTPObjectMetaData::EStorageId ) )
       
   190         iSameStorage = ETrue;
       
   191     else
       
   192         iSameStorage = EFalse;
       
   193     GetPreviousPropertiesL( *iObjectInfo );
       
   194     User::LeaveIfError( iFileMan->Move( suid, aNewFileName ) ); // iDest just Folder
       
   195     User::LeaveIfError( iFramework.Fs().SetModified( aNewFileName, iPreviousModifiedTime ) );
       
   196 
       
   197     iObjectInfo->SetDesCL( CMTPObjectMetaData::ESuid, aNewFileName );
       
   198     iObjectInfo->SetUint( CMTPObjectMetaData::EStorageId, iStorageId );
       
   199     iObjectInfo->SetUint( CMTPObjectMetaData::EParentHandle, iNewParentHandle );
       
   200     iFramework.ObjectMgr().ModifyObjectL( *iObjectInfo );
       
   201     SetPropertiesL( oldFileName, aNewFileName, *iObjectInfo );
       
   202     PRINT( _L( "MM MTP <= CMoveObject::MoveFileL" ) );
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CMoveObject::MoveOwnedObjectsL
       
   207 // Move the objects through iterations of RunL
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 TBool CMoveObject::MoveOwnedObjectsL()
       
   211     {
       
   212     PRINT( _L( "MM MTP => CMoveObject::MoveOwnedObjectsL" ) );
       
   213     TBool ret = EFalse;
       
   214 
       
   215     if ( iMoveObjectIndex < iNumberOfObjects )
       
   216         {
       
   217         MoveAndUpdateL( iObjectHandles[iMoveObjectIndex++] );
       
   218 
       
   219         TRequestStatus* status = &iStatus;
       
   220         User::RequestComplete( status, iStatus.Int() );
       
   221         SetActive();
       
   222         }
       
   223     else
       
   224         {
       
   225         ret = ETrue;
       
   226         }
       
   227 
       
   228     PRINT1( _L( "MM MTP <= CMoveObject::MoveOwnedObjectsL ret = %d" ), ret );
       
   229     return ret;
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CMoveObject::MoveFolderL
       
   234 // A helper function of MoveObjectL
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CMoveObject::MoveFolderL()
       
   238     {
       
   239     PRINT( _L( "MM MTP => CMoveObject::MoveFolderL" ) );
       
   240     RBuf oldFolderName;
       
   241     oldFolderName.CreateL( KMaxFileName );
       
   242     oldFolderName.CleanupClosePushL(); // + oldFileName
       
   243     oldFolderName = iObjectInfo->DesC( CMTPObjectMetaData::ESuid );
       
   244     PRINT1( _L( "MM MTP <> CMoveObject::MoveFolderL oldFolderName = %S" ), &oldFolderName );
       
   245     iPathToMove = oldFolderName.AllocL();
       
   246     CleanupStack::PopAndDestroy( &oldFolderName ); // - oldFolderName
       
   247 
       
   248     GenerateObjectHandleListL( iObjectInfo->Uint( CMTPObjectMetaData::EHandle ) );
       
   249 
       
   250     iNumberOfObjects = iObjectHandles.Count();
       
   251     PRINT1( _L( "MM MTP <> CMoveObject::MoveFolderL iNumberOfObjects = %d" ), iNumberOfObjects );
       
   252 
       
   253     TRequestStatus* status = &iStatus;
       
   254     User::RequestComplete( status, iStatus.Int() );
       
   255     SetActive();
       
   256 
       
   257     PRINT( _L( "MM MTP <= CMoveObject::MoveFolderL" ) );
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CMoveObject::MoveObjectL
       
   262 // move object operations
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 TMTPResponseCode CMoveObject::MoveObjectL()
       
   266     {
       
   267     PRINT( _L( "MM MTP => CMoveObject::MoveObjectL" ) );
       
   268     TMTPResponseCode responseCode = EMTPRespCodeOK;
       
   269 
       
   270     GetParametersL();
       
   271 
       
   272     RBuf newObjectName;
       
   273     newObjectName.CreateL( KMaxFileName );
       
   274     newObjectName.CleanupClosePushL(); // + newObjectName
       
   275     newObjectName = *iDest;
       
   276 
       
   277     const TDesC& suid( iObjectInfo->DesC( CMTPObjectMetaData::ESuid ) );
       
   278     TParsePtrC fileNameParser( suid );
       
   279 
       
   280     // Check if the object is a folder or a file.
       
   281     TBool isFolder = EFalse;
       
   282     User::LeaveIfError( BaflUtils::IsFolder( iFramework.Fs(), suid, isFolder ) );
       
   283 
       
   284     if ( !isFolder )
       
   285         {
       
   286         if ( ( newObjectName.Length() + fileNameParser.NameAndExt().Length() ) <= newObjectName.MaxLength() )
       
   287             {
       
   288             newObjectName.Append( fileNameParser.NameAndExt() );
       
   289             }
       
   290         responseCode = CanMoveObjectL( suid, newObjectName );
       
   291         }
       
   292     else // It is a folder.
       
   293         {
       
   294         TFileName rightMostFolderName;
       
   295         User::LeaveIfError( BaflUtils::MostSignificantPartOfFullName( suid,
       
   296                 rightMostFolderName ) );
       
   297         if ( ( newObjectName.Length() + rightMostFolderName.Length() + 1 ) <= newObjectName.MaxLength() )
       
   298             {
       
   299             newObjectName.Append( rightMostFolderName );
       
   300             // Add backslash.
       
   301             _LIT( KBackSlash, "\\" );
       
   302             newObjectName.Append( KBackSlash );
       
   303             }
       
   304         }
       
   305 
       
   306     iNewRootFolder = newObjectName.AllocL();
       
   307 
       
   308     if ( responseCode == EMTPRespCodeOK )
       
   309         {
       
   310         delete iFileMan;
       
   311         iFileMan = NULL;
       
   312         iFileMan = CFileMan::NewL( iFramework.Fs() );
       
   313 
       
   314         if ( !isFolder )
       
   315             {
       
   316             MoveFileL( newObjectName );
       
   317             SendResponseL( responseCode );
       
   318             }
       
   319         else
       
   320             {
       
   321             MoveFolderL();
       
   322             }
       
   323         }
       
   324     CleanupStack::PopAndDestroy( &newObjectName ); // - newObjectName.
       
   325 
       
   326     PRINT1( _L( "MM MTP <= CMoveObject::MoveObjectL responseCode = 0x%x" ), responseCode );
       
   327     return responseCode;
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CMoveObject::GetParametersL
       
   332 // Retrieve the parameters of the request
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 void CMoveObject::GetParametersL()
       
   336     {
       
   337     PRINT( _L( "MM MTP => CMoveObject::GetParametersL" ) );
       
   338     __ASSERT_DEBUG( iRequestChecker, Panic( EMmMTPDpRequestCheckNull ) );
       
   339 
       
   340     TUint32 objectHandle = Request().Uint32( TMTPTypeRequest::ERequestParameter1 );
       
   341     iStorageId = Request().Uint32( TMTPTypeRequest::ERequestParameter2 );
       
   342     iNewParentHandle = Request().Uint32( TMTPTypeRequest::ERequestParameter3 );
       
   343     PRINT3( _L( "MM MTP <> objectHandle = 0x%x, iStoargeId = 0x%x, iNewParentHandle = 0x%x" ),
       
   344             objectHandle, iStorageId, iNewParentHandle );
       
   345 
       
   346     // not taking owernship
       
   347     iObjectInfo = iRequestChecker->GetObjectInfo( objectHandle );
       
   348     __ASSERT_DEBUG( iObjectInfo, Panic( EMmMTPDpObjectNull ) );
       
   349 
       
   350     if ( iNewParentHandle == 0 )
       
   351         {
       
   352         SetDefaultParentObjectL();
       
   353         }
       
   354     else
       
   355         {
       
   356         CMTPObjectMetaData* parentObjectInfo =
       
   357                 iRequestChecker->GetObjectInfo( iNewParentHandle );
       
   358         __ASSERT_DEBUG( parentObjectInfo, Panic( EMmMTPDpObjectNull ) );
       
   359         delete iDest;
       
   360         iDest = NULL;
       
   361         iDest = parentObjectInfo->DesC( CMTPObjectMetaData::ESuid ).AllocL();
       
   362         PRINT1( _L( "MM MTP <> CMoveObject::GetParametersL iDest = %S" ), iDest );
       
   363         }
       
   364     PRINT( _L( "MM MTP <= CMoveObject::GetParametersL" ) );
       
   365     }
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // CMoveObject::SetDefaultParentObjectL
       
   369 // Get a default parent object, ff the request does not specify a parent object,
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 void CMoveObject::SetDefaultParentObjectL()
       
   373     {
       
   374     PRINT( _L( "MM MTP => CMoveObject::SetDefaultParentObjectL" ) );
       
   375 
       
   376     delete iDest;
       
   377     iDest = NULL;
       
   378     iDest = ( iFramework.StorageMgr().StorageL( iStorageId ).DesC(
       
   379                 CMTPStorageMetaData::EStorageSuid ) ).AllocL();
       
   380     PRINT1( _L( "MM MTP <> CMoveObject::SetDefaultParentObjectL iDest = %S" ), iDest );
       
   381     iNewParentHandle = KMTPHandleNoParent;
       
   382     PRINT( _L( "MM MTP <= CMoveObject::SetDefaultParentObjectL" ) );
       
   383     }
       
   384 
       
   385 // -----------------------------------------------------------------------------
       
   386 // CMoveObject::CanMoveObjectL
       
   387 // Check if we can move the file to the new location
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 TMTPResponseCode CMoveObject::CanMoveObjectL( const TDesC& aOldName,
       
   391         const TDesC& aNewName ) const
       
   392     {
       
   393     PRINT( _L( "MM MTP => CMoveObject::CanMoveObjectL" ) );
       
   394     TMTPResponseCode result = EMTPRespCodeOK;
       
   395 
       
   396     TEntry fileEntry;
       
   397     User::LeaveIfError( iFramework.Fs().Entry( aOldName, fileEntry ) );
       
   398     TDriveNumber drive( static_cast<TDriveNumber>( iFramework.StorageMgr().DriveNumber( iStorageId ) ) );
       
   399     User::LeaveIfError( drive );
       
   400     TVolumeInfo volumeInfo;
       
   401     User::LeaveIfError( iFramework.Fs().Volume( volumeInfo, drive ) );
       
   402 
       
   403     if ( volumeInfo.iFree < fileEntry.iSize )
       
   404         {
       
   405         result = EMTPRespCodeStoreFull;
       
   406         }
       
   407     else if ( BaflUtils::FileExists( iFramework.Fs(), aNewName ) )
       
   408         {
       
   409 #ifdef MMMTPDP_REPLACE_EXIST_FILE
       
   410         // delete the old one and replace
       
   411         TInt delErr = iFramework.Fs().Delete( aNewName );
       
   412         PRINT1( _L( "MM MTP <> CMoveObject::CanMoveObjectL delErr = %d" ), delErr );
       
   413         // delete from the metadata DB
       
   414         TRAPD( err, iFramework.ObjectMgr().RemoveObjectL( aNewName ) );
       
   415         PRINT1( _L( "MM MTP <> CMoveObject::CanMoveObjectL err = %d" ), err );
       
   416         // delete from video/mpx DB
       
   417         CMTPObjectMetaData* objectInfo = CMTPObjectMetaData::NewLC(); // + objectInfo
       
   418         if ( iFramework.ObjectMgr().ObjectL( aNewName, *objectInfo ) )
       
   419             {
       
   420             TRAP( err, iDpConfig.GetWrapperL().DeleteObjectL( aNewName,
       
   421                 objectInfo->Uint( CMTPObjectMetaData::EFormatCode ) ) );
       
   422             }
       
   423         CleanupStack::PopAndDestroy( objectInfo ); // - objectInfo
       
   424         if ( err )
       
   425             {
       
   426             // do nothing
       
   427             }
       
   428 #else
       
   429         result = EMTPRespCodeInvalidParentObject;
       
   430 #endif
       
   431         }
       
   432 
       
   433     PRINT1( _L( "MM MTP <= CMoveObject::CanMoveObjectL result = 0x%x" ), result );
       
   434     return result;
       
   435     }
       
   436 
       
   437 // -----------------------------------------------------------------------------
       
   438 // CMoveObject::GetPreviousPropertiesL
       
   439 // Save the object properties before doing the move
       
   440 // -----------------------------------------------------------------------------
       
   441 //
       
   442 void CMoveObject::GetPreviousPropertiesL( const CMTPObjectMetaData& aObject )
       
   443     {
       
   444     PRINT( _L( "MM MTP => CMoveObject::GetPreviousPropertiesL" ) );
       
   445 
       
   446     const TDesC& suid( aObject.DesC( CMTPObjectMetaData::ESuid ) );
       
   447 
       
   448     User::LeaveIfError( iFramework.Fs().Modified( suid, iPreviousModifiedTime ) );
       
   449 
       
   450     // same storage, not necessary to get the properties
       
   451     if ( iSameStorage )
       
   452         return;
       
   453 
       
   454     TUint formatCode = aObject.Uint( CMTPObjectMetaData::EFormatCode );
       
   455     const RArray<TUint>* properties = iDpConfig.GetSupportedPropertiesL( formatCode );
       
   456     TInt count = properties->Count();
       
   457     CMTPTypeString* textData = NULL;
       
   458     TInt err = KErrNone;
       
   459     TUint16 propCode;
       
   460     TUint32 handle = aObject.Uint( CMTPObjectMetaData::EHandle ) ;
       
   461 
       
   462     if ( iPropertyElement )
       
   463         {
       
   464         delete iPropertyElement;
       
   465         iPropertyElement = NULL;
       
   466         }
       
   467 
       
   468     for ( TInt i = 0; i < count; i++ )
       
   469         {
       
   470         propCode = (*properties)[i];
       
   471         switch( propCode )
       
   472             {
       
   473             case EMTPObjectPropCodeStorageID:
       
   474             case EMTPObjectPropCodeObjectFormat:
       
   475             case EMTPObjectPropCodeProtectionStatus:
       
   476             case EMTPObjectPropCodeObjectSize:
       
   477             case EMTPObjectPropCodeObjectFileName:
       
   478             case EMTPObjectPropCodeParentObject:
       
   479             case EMTPObjectPropCodePersistentUniqueObjectIdentifier:
       
   480             case EMTPObjectPropCodeNonConsumable:
       
   481             case EMTPObjectPropCodeDateCreated:
       
   482             case EMTPObjectPropCodeDateModified:
       
   483                 break;
       
   484 
       
   485             case EMTPObjectPropCodeName:
       
   486             case EMTPObjectPropCodeDateAdded:
       
   487                 if ( ( propCode == EMTPObjectPropCodeName )
       
   488                    || ( ( !MmMtpDpUtility::IsVideoL( aObject.DesC( CMTPObjectMetaData::ESuid ), iFramework ) )
       
   489                         && ( propCode == EMTPObjectPropCodeDateAdded ) ) )
       
   490                     {
       
   491                     textData = CMTPTypeString::NewLC(); // + textData
       
   492 
       
   493                     TRAP( err, iDpConfig.GetWrapperL().GetObjectMetadataValueL( propCode,
       
   494                         *textData,
       
   495                         aObject ) );
       
   496 
       
   497                     PRINT1( _L( "MM MTP <> CMoveObject::GetPreviousPropertiesL::ServiceSpecificObjectPropertyL err = %d" ), err );
       
   498 
       
   499                     if ( err == KErrNone )
       
   500                         {
       
   501                         iPropertyElement = &(iPropertyList->ReservePropElemL(handle, propCode));
       
   502                         iPropertyElement->SetStringL(CMTPTypeObjectPropListElement::EValue, textData->StringChars());
       
   503 //                        iPropertyElement = CMTPTypeObjectPropListElement::NewL(
       
   504 //                            handle, propCode, *textData );
       
   505                         }
       
   506                     else if ( err == KErrNotFound )
       
   507                         {
       
   508                         iPropertyElement = NULL;
       
   509                         }
       
   510                     else
       
   511                        {
       
   512                        User::Leave( err );
       
   513                        }
       
   514 
       
   515                     CleanupStack::PopAndDestroy( textData ); // - textData
       
   516                     }
       
   517                 break;
       
   518 
       
   519             default:
       
   520                 {
       
   521                 ServiceGetSpecificObjectPropertyL( propCode, handle, aObject );
       
   522                 }
       
   523                 break;
       
   524             }
       
   525 
       
   526         if ( iPropertyElement )
       
   527             {
       
   528             iPropertyList->CommitPropElemL( *iPropertyElement );
       
   529 
       
   530             iPropertyElement = NULL;
       
   531             }
       
   532 
       
   533         } // end of for loop
       
   534 
       
   535     PRINT1( _L( "MM MTP <= CMoveObject::GetPreviousPropertiesL err = %d" ), err );
       
   536     }
       
   537 
       
   538 // -----------------------------------------------------------------------------
       
   539 // CMoveObject::ServiceMetaDataToWrapper
       
   540 //
       
   541 // -----------------------------------------------------------------------------
       
   542 //
       
   543 EXPORT_C TMTPResponseCode CMoveObject::ServiceMetaDataToWrapper(
       
   544     const TUint16 aPropCode,
       
   545     MMTPType& aNewData,
       
   546     const CMTPObjectMetaData& aObject )
       
   547     {
       
   548     TMTPResponseCode resCode = EMTPRespCodeOK;
       
   549 
       
   550     TRAPD( err, iDpConfig.GetWrapperL().SetObjectMetadataValueL( aPropCode,
       
   551         aNewData,
       
   552         aObject ) );
       
   553 
       
   554     PRINT1( _L("MM MTP <> CMoveObject::ServiceMetaDataToWrapper err = %d"), err);
       
   555 
       
   556     if ( err == KErrNone )
       
   557         {
       
   558         resCode = EMTPRespCodeOK;
       
   559         }
       
   560     else if ( err == KErrTooBig )
       
   561         // according to the codes of S60
       
   562         {
       
   563         resCode = EMTPRespCodeInvalidDataset;
       
   564         }
       
   565     else if ( err == KErrPermissionDenied )
       
   566         {
       
   567         resCode = EMTPRespCodeAccessDenied;
       
   568         }
       
   569     else if ( err == KErrNotFound )
       
   570         {
       
   571         if ( MmMtpDpUtility::HasMetadata( aObject.Uint( CMTPObjectMetaData::EFormatCode ) ) )
       
   572             resCode = EMTPRespCodeAccessDenied;
       
   573         else
       
   574             resCode = EMTPRespCodeOK;
       
   575         }
       
   576     else
       
   577         {
       
   578         resCode = EMTPRespCodeGeneralError;
       
   579         }
       
   580 
       
   581     PRINT1( _L("MM MTP <= CMoveObject::ServiceMetaDataToWrapper resCode = 0x%x"), resCode);
       
   582 
       
   583     return resCode;
       
   584     }
       
   585 
       
   586 // -----------------------------------------------------------------------------
       
   587 // CMoveObject::SetPreviousPropertiesL
       
   588 // Set the object properties after doing the move
       
   589 // -----------------------------------------------------------------------------
       
   590 //
       
   591 void CMoveObject::SetPreviousPropertiesL( const CMTPObjectMetaData& aObject )
       
   592     {
       
   593     PRINT( _L( "MM MTP => CMoveObject::SetPreviousPropertiesL" ) );
       
   594     const TInt count( iPropertyList->NumberOfElements() );
       
   595     PRINT1( _L( "MM MTP <> CMoveObject::SetPreviousPropertiesL count = %d" ), count );
       
   596     TMTPResponseCode respcode = EMTPRespCodeOK;
       
   597     CMTPTypeString* stringData = NULL;
       
   598     iPropertyList->ResetCursor();
       
   599     
       
   600     for ( TInt i = 0; i < count; i++ )
       
   601         {
       
   602         CMTPTypeObjectPropListElement& element = iPropertyList->GetNextElementL( );
       
   603 
       
   604         TUint32 handle = element.Uint32L(
       
   605                 CMTPTypeObjectPropListElement::EObjectHandle );
       
   606         TUint16 propertyCode = element.Uint16L(
       
   607                 CMTPTypeObjectPropListElement::EPropertyCode );
       
   608         TUint16 dataType = element.Uint16L(
       
   609                 CMTPTypeObjectPropListElement::EDatatype );
       
   610         PRINT3( _L( "MM MTP <> CMoveObject::SetPreviousPropertiesL = 0x%x, propertyCode = 0x%x, dataType = 0x%x" ),
       
   611           handle, propertyCode, dataType );
       
   612 
       
   613         switch ( propertyCode )
       
   614             {
       
   615             case EMTPObjectPropCodeStorageID:
       
   616             case EMTPObjectPropCodeObjectFormat:
       
   617             case EMTPObjectPropCodeProtectionStatus:
       
   618             case EMTPObjectPropCodeObjectSize:
       
   619             case EMTPObjectPropCodeObjectFileName:
       
   620             case EMTPObjectPropCodeParentObject:
       
   621             case EMTPObjectPropCodePersistentUniqueObjectIdentifier:
       
   622             case EMTPObjectPropCodeNonConsumable:
       
   623             case EMTPObjectPropCodeDateCreated:
       
   624             case EMTPObjectPropCodeDateModified:
       
   625             case EMTPObjectPropCodeDateAdded:
       
   626                 break;
       
   627 
       
   628             case EMTPObjectPropCodeName:
       
   629                 {
       
   630                 stringData = CMTPTypeString::NewLC(
       
   631                     element.StringL(CMTPTypeObjectPropListElement::EValue)); // + stringData
       
   632 
       
   633                 respcode = ServiceMetaDataToWrapper( propertyCode,
       
   634                     *stringData,
       
   635                     aObject );
       
   636 
       
   637                 CleanupStack::PopAndDestroy( stringData ); // - stringData
       
   638                 }
       
   639                 break;
       
   640 
       
   641             default:
       
   642                 {
       
   643                 respcode = ServiceSetSpecificObjectPropertyL( propertyCode,
       
   644                         aObject,
       
   645                         element );
       
   646                 }
       
   647                 break;
       
   648             }
       
   649         } // end of for loop
       
   650 
       
   651     if( respcode == EMTPRespCodeOK )
       
   652         {
       
   653         // do nothing, ignore warning
       
   654         }
       
   655 
       
   656     PRINT1( _L( "MM MTP <= CMoveObject::SetPreviousPropertiesL respcode = 0x%x" ), respcode );
       
   657     }
       
   658 
       
   659 // -----------------------------------------------------------------------------
       
   660 // CMoveObject::SetPropertiesL
       
   661 // Set the object properties in the object property store.
       
   662 // -----------------------------------------------------------------------------
       
   663 //
       
   664 void CMoveObject::SetPropertiesL( const TDesC& aOldFileName, const TDesC& aNewFileName,
       
   665         const CMTPObjectMetaData& aNewObject )
       
   666     {
       
   667     PRINT2( _L( "MM MTP => CMoveObject::SetPropertiesL aOldFileName = %S, aNewFileName = %S" ),
       
   668             &aOldFileName, &aNewFileName );
       
   669 
       
   670     TUint32 formatCode = aNewObject.Uint( CMTPObjectMetaData::EFormatCode );
       
   671     // This is used to keep the same behavior in mass storage and device file manager.
       
   672     if ( formatCode == EMTPFormatCodeAbstractAudioVideoPlaylist )
       
   673         {
       
   674         PRINT( _L( "MM MTP <> CMoveObject::SetPropertiesL Playlist file do not update the MPX DB" ) );
       
   675         }
       
   676     else
       
   677         {
       
   678         // if the two object in different stoarge, we should delete the old one and insert new one
       
   679         if ( iSameStorage )
       
   680             iDpConfig.GetWrapperL().RenameObjectL( aOldFileName, aNewFileName );
       
   681         else
       
   682             {
       
   683             iDpConfig.GetWrapperL().DeleteObjectL( aOldFileName, formatCode );
       
   684             iDpConfig.GetWrapperL().AddObjectL( aNewFileName );
       
   685             SetPreviousPropertiesL( aNewObject );
       
   686             }
       
   687         }
       
   688 
       
   689       // Reference DB is used PUID
       
   690 //    if ( formatCode == EMTPFormatCodeAbstractAudioVideoPlaylist
       
   691 //            || formatCode == EMTPFormatCodeM3UPlaylist )
       
   692 //        {
       
   693 //        MMTPReferenceMgr& referenceMgr = iFramework.ReferenceMgr();
       
   694 //        CDesCArray* references = referenceMgr.ReferencesLC( aOldFileName ); // + references
       
   695 //        referenceMgr.SetReferencesL( aNewFileName, *references );
       
   696 //        CleanupStack::PopAndDestroy( references ); // - references
       
   697 //        // delete the old references
       
   698 //        referenceMgr.RemoveReferencesL( aOldFileName );
       
   699 //        }
       
   700 
       
   701 
       
   702     PRINT( _L( "MM MTP <= CMoveObject::SetPropertiesL" ) );
       
   703     }
       
   704 
       
   705 // -----------------------------------------------------------------------------
       
   706 // CMoveObject::FinalPhaseMove
       
   707 // This function will actually delete the orginal folders from the file system
       
   708 // -----------------------------------------------------------------------------
       
   709 //
       
   710 TMTPResponseCode CMoveObject::FinalPhaseMove()
       
   711     {
       
   712     PRINT( _L( "MM MTP => CMoveObject::FinalPhaseMove" ) );
       
   713     TMTPResponseCode ret = EMTPRespCodeOK;
       
   714 
       
   715     TInt rel = iFileMan->RmDir( *iPathToMove );
       
   716     PRINT1( _L( "MM MTP <> CMoveObject::FinalPhaseMove rel = %d" ), rel );
       
   717 
       
   718     if ( rel != KErrNone )
       
   719         {
       
   720         ret = EMTPRespCodeGeneralError;
       
   721         }
       
   722 
       
   723     PRINT1( _L( "MM MTP <= CMoveObject::FinalPhaseMove ret = 0x%x" ), ret );
       
   724     return ret;
       
   725     }
       
   726 
       
   727 // -----------------------------------------------------------------------------
       
   728 // CMoveObject::GenerateObjectHandleListL
       
   729 // Generate the list of handles that need to be moved to the new location
       
   730 // -----------------------------------------------------------------------------
       
   731 //
       
   732 void CMoveObject::GenerateObjectHandleListL( TUint32 aParentHandle )
       
   733     {
       
   734     PRINT1( _L( "MM MTP => CMoveObject::GenerateObjectHandleListL aParentHandle = 0x%x" ), aParentHandle );
       
   735     RMTPObjectMgrQueryContext context;
       
   736     RArray<TUint> handles;
       
   737     TMTPObjectMgrQueryParams params( KMTPStorageAll, KMTPFormatsAll,
       
   738             aParentHandle );
       
   739     CleanupClosePushL( context ); // + context
       
   740     CleanupClosePushL( handles ); // - handles
       
   741 
       
   742     do
       
   743         {
       
   744         iFramework.ObjectMgr().GetObjectHandlesL( params, context, handles );
       
   745 
       
   746         TInt numberOfObjects = handles.Count();
       
   747         for ( TInt i = 0; i < numberOfObjects; i++ )
       
   748             {
       
   749             if ( iFramework.ObjectMgr().ObjectOwnerId( handles[i] ) == iFramework.DataProviderId() )
       
   750                 {
       
   751                 iObjectHandles.AppendL( handles[i] );
       
   752                 continue;
       
   753                 }
       
   754 
       
   755             // Folder
       
   756             if ( iFramework.ObjectMgr().ObjectOwnerId( handles[i] ) == 0 ) // We know that the device dp id is always 0, otherwise the whole MTP won't work.
       
   757                 {
       
   758                 GenerateObjectHandleListL( handles[i] );
       
   759                 }
       
   760             }
       
   761         }
       
   762     while ( !context.QueryComplete() );
       
   763 
       
   764     CleanupStack::PopAndDestroy( &handles ); // - handles
       
   765     CleanupStack::PopAndDestroy( &context ); // - contect
       
   766 
       
   767     PRINT( _L( "MM MTP <= CMoveObject::GenerateObjectHandleListL" ) );
       
   768     }
       
   769 
       
   770 // -----------------------------------------------------------------------------
       
   771 // CMoveObject::MoveAndUpdateL
       
   772 // Move a single object and update the database
       
   773 // -----------------------------------------------------------------------------
       
   774 //
       
   775 void CMoveObject::MoveAndUpdateL( TUint32 aObjectHandle )
       
   776     {
       
   777     PRINT1( _L( "MM MTP => CMoveObject::MoveAndUpdateL aObjectHanlde = 0x%x" ), aObjectHandle );
       
   778 
       
   779     CMTPObjectMetaData* objectInfo( CMTPObjectMetaData::NewLC() ); // + objectInfo
       
   780 
       
   781     RBuf fileName;
       
   782     fileName.CreateL( KMaxFileName );
       
   783     fileName.CleanupClosePushL(); // + fileName
       
   784 
       
   785     RBuf rightPartName;
       
   786     rightPartName.CreateL( KMaxFileName );
       
   787     rightPartName.CleanupClosePushL(); // + rightPartName
       
   788 
       
   789     RBuf oldName;
       
   790     oldName.CreateL( KMaxFileName );
       
   791     oldName.CleanupClosePushL(); // + oldName
       
   792 
       
   793     if ( iFramework.ObjectMgr().ObjectL( TMTPTypeUint32( aObjectHandle ), *objectInfo ) )
       
   794         {
       
   795         fileName = objectInfo->DesC( CMTPObjectMetaData::ESuid );
       
   796         oldName = fileName;
       
   797 
       
   798         if ( objectInfo->Uint( CMTPObjectMetaData::EDataProviderId )
       
   799                 == iFramework.DataProviderId() )
       
   800             {
       
   801             rightPartName = fileName.Right( fileName.Length() - iPathToMove->Length() );
       
   802 
       
   803             if ( ( iNewRootFolder->Length() + rightPartName.Length() ) > fileName.MaxLength() )
       
   804                 {
       
   805                 User::Leave( KErrCorrupt );
       
   806                 }
       
   807 
       
   808             fileName.Zero();
       
   809             fileName.Append( *iNewRootFolder );
       
   810             fileName.Append( rightPartName );
       
   811             PRINT1( _L( "MM MTP <> MoveAndUpdateL fileName = %S" ), &fileName );
       
   812 
       
   813             if ( iStorageId == objectInfo->Uint( CMTPObjectMetaData::EStorageId ) )
       
   814                 iSameStorage = ETrue;
       
   815             else
       
   816                 iSameStorage = EFalse;
       
   817             GetPreviousPropertiesL( *objectInfo );
       
   818             TInt err = iFileMan->Move( oldName, fileName );
       
   819             PRINT1( _L( "MM MTP <> CMoveObject::MoveAndUpdateL Move error code = %d" ), err );
       
   820             User::LeaveIfError( err );
       
   821             User::LeaveIfError( iFramework.Fs().SetModified( fileName, iPreviousModifiedTime ) );
       
   822 
       
   823             objectInfo->SetDesCL( CMTPObjectMetaData::ESuid, fileName );
       
   824             objectInfo->SetUint( CMTPObjectMetaData::EStorageId, iStorageId );
       
   825             TParsePtrC parentSuid( fileName );
       
   826             TUint32 parentHandle = iFramework.ObjectMgr().HandleL( parentSuid.DriveAndPath() );
       
   827             objectInfo->SetUint( CMTPObjectMetaData::EParentHandle, parentHandle );
       
   828 
       
   829             //TUint32 parentHandle = iFramework.ObjectMgr().HandleL( parentSuid.DriveAndPath() );
       
   830             PRINT1( _L( "MM MTP <> CMoveObject::MoveAndUpdateL parentHandle = 0x%x" ), parentHandle );
       
   831 
       
   832             iFramework.ObjectMgr().ModifyObjectL( *objectInfo );
       
   833 
       
   834             SetPropertiesL( oldName, fileName, *objectInfo );
       
   835             }
       
   836         }
       
   837     else
       
   838         {
       
   839         User::Leave( KErrCorrupt );
       
   840         }
       
   841 
       
   842     CleanupStack::PopAndDestroy( &oldName ); // - oldName
       
   843     CleanupStack::PopAndDestroy( &rightPartName ); // - rightPartName
       
   844     CleanupStack::PopAndDestroy( &fileName ); // - fileName
       
   845     CleanupStack::PopAndDestroy( objectInfo ); // - objectInfo
       
   846     PRINT( _L( "MM MTP <= CMoveObject::MoveAndUpdateL" ) );
       
   847     }
       
   848 
       
   849 // end of file