mtpdataproviders/mtpimagedp/src/cmtpimagedpcopyobject.cpp
changeset 0 d0791faffa3f
child 2 4843bb5893b6
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <f32file.h>
       
    22 #include <bautils.h>
       
    23 #include <pathinfo.h> // PathInfo
       
    24 #include <sysutil.h>
       
    25 
       
    26 #include <mtp/mmtpdataproviderframework.h>
       
    27 #include <mtp/mmtpobjectmgr.h>
       
    28 #include <mtp/mmtpstoragemgr.h>
       
    29 #include <mtp/cmtpobjectmetadata.h>
       
    30 #include <mtp/cmtptypearray.h>
       
    31 #include <mtp/cmtptypestring.h>
       
    32 
       
    33 #include "cmtpimagedpcopyobject.h"
       
    34 #include "mtpimagedppanic.h"
       
    35 #include "mtpimagedputilits.h"
       
    36 #include "cmtpimagedp.h"
       
    37 
       
    38 __FLOG_STMT(_LIT8(KComponent,"CopyObject");)
       
    39 
       
    40 const TInt RollbackFuncCnt = 1;
       
    41 
       
    42 /**
       
    43 Verification data for the CopyObject request
       
    44 */
       
    45 const TMTPRequestElementInfo KMTPCopyObjectPolicy[] = 
       
    46     {
       
    47         {TMTPTypeRequest::ERequestParameter2, EMTPElementTypeStorageId, EMTPElementAttrWrite, 0, 0, 0},
       
    48         {TMTPTypeRequest::ERequestParameter3, EMTPElementTypeObjectHandle, EMTPElementAttrDir | EMTPElementAttrWrite, 1, 0, 0}
       
    49     };
       
    50     
       
    51 /**
       
    52 Two-phase construction method
       
    53 @param aPlugin	The data provider plugin
       
    54 @param aFramework	The data provider framework
       
    55 @param aConnection	The connection from which the request comes
       
    56 @return a pointer to the created request processor object
       
    57 */     
       
    58 MMTPRequestProcessor* CMTPImageDpCopyObject::NewL(MMTPDataProviderFramework& aFramework, MMTPConnection& aConnection,CMTPImageDataProvider& aDataProvider)
       
    59     {
       
    60     CMTPImageDpCopyObject* self = new (ELeave) CMTPImageDpCopyObject(aFramework, aConnection,aDataProvider);
       
    61     CleanupStack::PushL(self);
       
    62     self->ConstructL();
       
    63     CleanupStack::Pop(self);
       
    64     return self;
       
    65     }
       
    66     
       
    67 /**
       
    68 Destructor
       
    69 */	
       
    70 CMTPImageDpCopyObject::~CMTPImageDpCopyObject()
       
    71     {	
       
    72     __FLOG(_L8(">> CMTPImageDpCopyObject::~CMTPImageDpCopyObject"));
       
    73     delete iDest;
       
    74     delete iFileMan;
       
    75     delete iSrcObjectInfo;
       
    76     delete iTargetObjectInfo;
       
    77     iRollbackActionL.Close();
       
    78     __FLOG(_L8("<< CMTPImageDpCopyObject::~CMTPImageDpCopyObject"));
       
    79     __FLOG_CLOSE;
       
    80     
       
    81     }
       
    82     
       
    83 /**
       
    84 Standard c++ constructor
       
    85 */	
       
    86 CMTPImageDpCopyObject::CMTPImageDpCopyObject(MMTPDataProviderFramework& aFramework, MMTPConnection& aConnection,CMTPImageDataProvider& aDataProvider) :
       
    87     CMTPRequestProcessor(aFramework, aConnection, sizeof(KMTPCopyObjectPolicy)/sizeof(TMTPRequestElementInfo), KMTPCopyObjectPolicy),
       
    88     iDataProvider(aDataProvider)
       
    89     {
       
    90     __FLOG_OPEN(KMTPSubsystem, KComponent);
       
    91     }
       
    92     
       
    93 /**
       
    94 Second phase constructor
       
    95 */
       
    96 void CMTPImageDpCopyObject::ConstructL()
       
    97     {
       
    98     __FLOG(_L8(">> CMTPImageDpCopyObject::ConstructL"));
       
    99     iFileMan = CFileMan::NewL(iFramework.Fs());
       
   100     iSrcObjectInfo = CMTPObjectMetaData::NewL();
       
   101     iRollbackActionL.ReserveL(RollbackFuncCnt);
       
   102     __FLOG(_L8("<< CMTPImageDpCopyObject::ConstructL"));
       
   103     }
       
   104 
       
   105 TMTPResponseCode CMTPImageDpCopyObject::CheckRequestL()
       
   106     {
       
   107     __FLOG(_L8(">> CMTPImageDpCopyObject::CheckRequestL"));
       
   108     TMTPResponseCode responseCode = CMTPRequestProcessor::CheckRequestL();
       
   109     if (EMTPRespCodeOK == responseCode)
       
   110         {
       
   111         TUint32 objectHandle = Request().Uint32(TMTPTypeRequest::ERequestParameter1);
       
   112         // Check whether object handle is valid
       
   113         responseCode = MTPImageDpUtilits::VerifyObjectHandleL(iFramework, objectHandle, *iSrcObjectInfo);
       
   114         }
       
   115     else if(EMTPRespCodeInvalidObjectHandle == responseCode) //we only check the parent handle
       
   116         {
       
   117         responseCode = EMTPRespCodeInvalidParentObject;
       
   118         }
       
   119     
       
   120     __FLOG_VA((_L8("CheckRequestL - Exit with responseCode = 0x%04X"), responseCode));
       
   121     __FLOG(_L8("<< CMTPImageDpCopyObject::CheckRequestL"));
       
   122     return responseCode;
       
   123     }
       
   124 
       
   125 /**
       
   126 CopyObject request handler
       
   127 */      
       
   128 void CMTPImageDpCopyObject::ServiceL()
       
   129     {   
       
   130     __FLOG(_L8(">> CMTPImageDpCopyObject::ServiceL"));
       
   131     TUint32 handle = KMTPHandleNone;
       
   132     TMTPResponseCode responseCode = CopyObjectL(handle);
       
   133     if(responseCode == EMTPRespCodeOK)
       
   134         {
       
   135         SendResponseL(EMTPRespCodeOK, 1, &handle);
       
   136         }
       
   137     else
       
   138         {
       
   139         SendResponseL(responseCode);
       
   140         }
       
   141     __FLOG(_L8("<< CMTPImageDpCopyObject::ServiceL"));
       
   142     }
       
   143     
       
   144 /**
       
   145 Copy object operation
       
   146 @return the object handle of the resulting object.
       
   147 */
       
   148 TMTPResponseCode CMTPImageDpCopyObject::CopyObjectL(TUint32& aNewHandle)
       
   149     {
       
   150     __FLOG(_L8(">> CMTPImageDpCopyObject::CopyObjectL"));
       
   151     TMTPResponseCode responseCode = EMTPRespCodeOK;
       
   152     aNewHandle = KMTPHandleNone;
       
   153     
       
   154     GetParametersL();
       
   155     
       
   156     iNewFileName.Append(*iDest);
       
   157     const TDesC& oldFileName = iSrcObjectInfo->DesC(CMTPObjectMetaData::ESuid);
       
   158     TParsePtrC fileNameParser(oldFileName);
       
   159     
       
   160     if((iNewFileName.Length() + fileNameParser.NameAndExt().Length()) <= iNewFileName.MaxLength())
       
   161         {
       
   162         iNewFileName.Append(fileNameParser.NameAndExt());
       
   163         responseCode = CanCopyObjectL(oldFileName, iNewFileName);	
       
   164         }
       
   165     else
       
   166         {
       
   167         responseCode = EMTPRespCodeGeneralError;
       
   168         }
       
   169         
       
   170     
       
   171     if(responseCode == EMTPRespCodeOK)
       
   172         {
       
   173         aNewHandle = CopyFileL(oldFileName, iNewFileName);
       
   174         }
       
   175     __FLOG(_L8("<< CMTPImageDpCopyObject::CopyObjectL"));
       
   176     return responseCode;
       
   177     }
       
   178 
       
   179 /**
       
   180 A helper function of CopyObjectL.
       
   181 @param aNewFileName the new full filename after copy.
       
   182 @return objectHandle of new copy of object.
       
   183 */
       
   184 TUint32 CMTPImageDpCopyObject::CopyFileL(const TDesC& aOldFileName, const TDesC& aNewFileName)
       
   185     {
       
   186     __FLOG(_L8(">> CMTPImageDpCopyObject::CopyFileL"));
       
   187     TCleanupItem anItem(FailRecover, reinterpret_cast<TAny*>(this));
       
   188     CleanupStack::PushL(anItem);
       
   189     
       
   190     GetPreviousPropertiesL(aOldFileName);
       
   191     User::LeaveIfError(iFileMan->Copy(aOldFileName, *iDest));
       
   192     iRollbackActionL.Append(RollBackFromFsL);
       
   193     SetPreviousPropertiesL(aNewFileName);
       
   194     
       
   195     iFramework.ObjectMgr().InsertObjectL(*iTargetObjectInfo);
       
   196     __FLOG(_L8("<< CMTPImageDpCopyObject::CopyFileL"));
       
   197     CleanupStack::Pop(this);
       
   198     return iTargetObjectInfo->Uint(CMTPObjectMetaData::EHandle);
       
   199     }
       
   200 
       
   201 /**
       
   202 Retrieve the parameters of the request
       
   203 */	
       
   204 void CMTPImageDpCopyObject::GetParametersL()
       
   205     {
       
   206     __FLOG(_L8(">> CMTPImageDpCopyObject::GetParametersL"));
       
   207     __ASSERT_DEBUG(iRequestChecker, Panic(EMTPImageDpRequestCheckNull));
       
   208     
       
   209     TUint32 objectHandle  = Request().Uint32(TMTPTypeRequest::ERequestParameter1);
       
   210     iStorageId = Request().Uint32(TMTPTypeRequest::ERequestParameter2);
       
   211     TUint32 parentObjectHandle  = Request().Uint32(TMTPTypeRequest::ERequestParameter3);       
       
   212     
       
   213     if(parentObjectHandle == 0)
       
   214         {
       
   215         SetDefaultParentObjectL();
       
   216         }
       
   217     else	
       
   218         {
       
   219         CMTPObjectMetaData* parentObjectInfo = iRequestChecker->GetObjectInfo(parentObjectHandle);
       
   220         __ASSERT_DEBUG(parentObjectInfo, Panic(EMTPImageDpObjectNull));
       
   221         delete iDest;
       
   222         iDest = NULL;
       
   223         iDest = parentObjectInfo->DesC(CMTPObjectMetaData::ESuid).AllocL();        
       
   224         iNewParentHandle = parentObjectHandle;
       
   225         }
       
   226     __FLOG(_L8("<< CMTPImageDpCopyObject::GetParametersL"));	
       
   227     }
       
   228     
       
   229 /**
       
   230 Get a default parent object, ff the request does not specify a parent object, 
       
   231 */
       
   232 void CMTPImageDpCopyObject::SetDefaultParentObjectL()
       
   233     {
       
   234     __FLOG(_L8(">> CMTPImageDpCopyObject::SetDefaultParentObjectL"));
       
   235     TDriveNumber drive(static_cast<TDriveNumber>(iFramework.StorageMgr().DriveNumber(iStorageId)));
       
   236     User::LeaveIfError(drive);
       
   237     TChar driveLetter;
       
   238     iFramework.Fs().DriveToChar(drive, driveLetter);
       
   239     TFileName driveBuf;
       
   240     driveBuf.Append(driveLetter);
       
   241     driveBuf = BaflUtils::RootFolderPath(driveBuf.Left(1));
       
   242     delete iDest;
       
   243     iDest = NULL;
       
   244     iDest = driveBuf.AllocL();
       
   245     iNewParentHandle = KMTPHandleNoParent;
       
   246     __FLOG(_L8("<< CMTPImageDpCopyObject::SetDefaultParentObjectL"));
       
   247     }
       
   248     
       
   249 /**
       
   250 Check if we can copy the file to the new location
       
   251 */
       
   252 TMTPResponseCode CMTPImageDpCopyObject::CanCopyObjectL(const TDesC& aOldName, const TDesC& aNewName) const
       
   253     {
       
   254     __FLOG(_L8(">> CMTPImageDpCopyObject::CanCopyObjectL"));
       
   255     TMTPResponseCode result = EMTPRespCodeOK;
       
   256     
       
   257     TEntry fileEntry;
       
   258     User::LeaveIfError(iFramework.Fs().Entry(aOldName, fileEntry));
       
   259     TDriveNumber drive(static_cast<TDriveNumber>(iFramework.StorageMgr().DriveNumber(iStorageId)));
       
   260     User::LeaveIfError(drive);
       
   261     TVolumeInfo volumeInfo;
       
   262     User::LeaveIfError(iFramework.Fs().Volume(volumeInfo, drive));
       
   263     
       
   264     if(volumeInfo.iFree < fileEntry.iSize)
       
   265         {
       
   266         result = EMTPRespCodeStoreFull;
       
   267         }
       
   268     else if (BaflUtils::FileExists(iFramework.Fs(), aNewName))			
       
   269         {
       
   270         result = EMTPRespCodeInvalidParentObject;
       
   271         }
       
   272     __FLOG_VA((_L8("CanCopyObjectL - Exit with response code 0x%04X"), result));
       
   273     __FLOG(_L8("<< CMTPImageDpCopyObject::CanCopyObjectL"));  	
       
   274     return result;	
       
   275     }
       
   276     
       
   277 /**
       
   278 Save the object properties before doing the copy
       
   279 */
       
   280 void CMTPImageDpCopyObject::GetPreviousPropertiesL(const TDesC& aOldFileName)
       
   281     {
       
   282     __FLOG(_L8("GetPreviousPropertiesL - Entry"));
       
   283     User::LeaveIfError(iFramework.Fs().Modified(aOldFileName, iDateModified));
       
   284     __FLOG(_L8("GetPreviousPropertiesL - Exit"));
       
   285     }
       
   286     
       
   287 /**
       
   288 Set the object properties after doing the copy
       
   289 */
       
   290 void CMTPImageDpCopyObject::SetPreviousPropertiesL(const TDesC& aNewFileName)
       
   291     {
       
   292     __FLOG(_L8("SetPreviousPropertiesL - Entry"));        
       
   293     User::LeaveIfError(iFramework.Fs().SetModified(aNewFileName, iDateModified));
       
   294     
       
   295     iTargetObjectInfo = CMTPObjectMetaData::NewL();
       
   296     iTargetObjectInfo->SetUint(CMTPObjectMetaData::EDataProviderId, iSrcObjectInfo->Uint(CMTPObjectMetaData::EDataProviderId));
       
   297     iTargetObjectInfo->SetUint(CMTPObjectMetaData::EFormatCode, iSrcObjectInfo->Uint(CMTPObjectMetaData::EFormatCode));
       
   298     iTargetObjectInfo->SetDesCL(CMTPObjectMetaData::EName, iSrcObjectInfo->DesC(CMTPObjectMetaData::EName));
       
   299     iTargetObjectInfo->SetUint(CMTPObjectMetaData::ENonConsumable, iSrcObjectInfo->Uint(CMTPObjectMetaData::ENonConsumable));
       
   300     iTargetObjectInfo->SetUint(CMTPObjectMetaData::EParentHandle, iNewParentHandle);
       
   301     iTargetObjectInfo->SetUint(CMTPObjectMetaData::EStorageId, iStorageId);
       
   302     iTargetObjectInfo->SetDesCL(CMTPObjectMetaData::ESuid, aNewFileName);
       
   303     __FLOG(_L8("SetPreviousPropertiesL - Exit"));
       
   304     }
       
   305 
       
   306 void CMTPImageDpCopyObject::FailRecover(TAny* aCopyOperation)
       
   307     {
       
   308     reinterpret_cast<CMTPImageDpCopyObject*>(aCopyOperation)->RollBack();
       
   309     }
       
   310 
       
   311 void CMTPImageDpCopyObject::RollBack()
       
   312     {
       
   313     TInt i = iRollbackActionL.Count();
       
   314     while(-- i >= 0)
       
   315         {
       
   316         TRAP_IGNORE((*iRollbackActionL[i])(this));
       
   317         }
       
   318     iRollbackActionL.Reset();
       
   319     }
       
   320 
       
   321 void CMTPImageDpCopyObject::RollBackFromFsL()
       
   322     {
       
   323     User::LeaveIfError(iFramework.Fs().Delete(iNewFileName));
       
   324     }
       
   325 
       
   326 void CMTPImageDpCopyObject::RollBackFromFsL(CMTPImageDpCopyObject* aObject)
       
   327     {
       
   328     aObject->RollBackFromFsL();
       
   329     }
       
   330 
       
   331 // End of file
       
   332