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