mtpfws/mtpfw/dataproviders/dputility/src/cmtpcopyobject.cpp
changeset 0 d0791faffa3f
child 3 8b094906a049
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2007-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 #include <f32file.h>
       
    17 #include <bautils.h>
       
    18 
       
    19 #include <mtp/mmtpdataproviderframework.h>
       
    20 #include <mtp/mmtpobjectmgr.h>
       
    21 #include <mtp/mmtpstoragemgr.h>
       
    22 #include <mtp/cmtpobjectmetadata.h>
       
    23 #include <mtp/cmtptypearray.h>
       
    24 #include <mtp/cmtptypestring.h>
       
    25 
       
    26 #include "cmtpcopyobject.h"
       
    27 #include "mtpdppanic.h"
       
    28 
       
    29 __FLOG_STMT(_LIT8(KComponent,"CopyObject");)
       
    30 
       
    31 /**
       
    32 Verification data for the CopyObject request
       
    33 */
       
    34 const TMTPRequestElementInfo KMTPCopyObjectPolicy[] = 
       
    35     {
       
    36     	{TMTPTypeRequest::ERequestParameter1, EMTPElementTypeObjectHandle, EMTPElementAttrFileOrDir, 0, 0, 0},   	
       
    37         {TMTPTypeRequest::ERequestParameter2, EMTPElementTypeStorageId, EMTPElementAttrWrite, 0, 0, 0},                
       
    38         {TMTPTypeRequest::ERequestParameter3, EMTPElementTypeObjectHandle, EMTPElementAttrDir | EMTPElementAttrWrite, 1, 0, 0}
       
    39     };
       
    40 
       
    41 /**
       
    42 Two-phase construction method
       
    43 @param aPlugin	The data provider plugin
       
    44 @param aFramework	The data provider framework
       
    45 @param aConnection	The connection from which the request comes
       
    46 @return a pointer to the created request processor object
       
    47 */     
       
    48 EXPORT_C MMTPRequestProcessor* CMTPCopyObject::NewL(MMTPDataProviderFramework& aFramework, MMTPConnection& aConnection)
       
    49 	{
       
    50 	CMTPCopyObject* self = new (ELeave) CMTPCopyObject(aFramework, aConnection);
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL();
       
    53 	CleanupStack::Pop(self);	
       
    54 	return self;
       
    55 	}
       
    56 
       
    57 
       
    58 /**
       
    59 Destructor
       
    60 */	
       
    61 EXPORT_C CMTPCopyObject::~CMTPCopyObject()
       
    62 	{	
       
    63 	delete iDest;
       
    64 	delete iFileMan;
       
    65 
       
    66 	__FLOG_CLOSE;
       
    67 	}
       
    68 
       
    69 /**
       
    70 Standard c++ constructor
       
    71 */	
       
    72 CMTPCopyObject::CMTPCopyObject(MMTPDataProviderFramework& aFramework, MMTPConnection& aConnection) :
       
    73 	CMTPRequestProcessor(aFramework, aConnection, sizeof(KMTPCopyObjectPolicy)/sizeof(TMTPRequestElementInfo), KMTPCopyObjectPolicy)
       
    74 	{
       
    75 	__FLOG_OPEN(KMTPSubsystem, KComponent);
       
    76 	}
       
    77 
       
    78 
       
    79 /**
       
    80 CopyObject request handler
       
    81 */		
       
    82 void CMTPCopyObject::ServiceL()
       
    83 	{	
       
    84 	TUint32 handle = KMTPHandleNone;
       
    85 	TMTPResponseCode responseCode = CopyObjectL(handle);
       
    86 	if(responseCode == EMTPRespCodeOK)
       
    87 		{
       
    88 		SendResponseL(EMTPRespCodeOK, 1, &handle);
       
    89 		}
       
    90 	else
       
    91 		{
       
    92 		SendResponseL(responseCode);
       
    93 		}
       
    94 	}
       
    95 
       
    96 
       
    97 /**
       
    98  Second phase constructor
       
    99 */
       
   100 void CMTPCopyObject::ConstructL()
       
   101     {
       
   102 
       
   103     }
       
   104 
       
   105 	
       
   106 /**
       
   107 A helper function of CopyObjectL.
       
   108 @param aNewFileName the new full filename after copy.
       
   109 @return objectHandle of new copy of object.
       
   110 */
       
   111 TUint32 CMTPCopyObject::CopyFileL(const TDesC& aNewFileName)
       
   112 	{
       
   113 	__FLOG(_L8("CopyFileL - Entry"));
       
   114 	const TDesC& suid(iObjectInfo->DesC(CMTPObjectMetaData::ESuid));
       
   115 	GetPreviousPropertiesL(suid);
       
   116 	User::LeaveIfError(iFileMan->Copy(suid, *iDest));
       
   117 	SetPreviousPropertiesL(aNewFileName);
       
   118 	
       
   119 	TUint32 handle = UpdateObjectInfoL(aNewFileName);
       
   120 	
       
   121 	__FLOG(_L8("CopyFileL - Exit"));
       
   122 	
       
   123 	return handle;
       
   124 	}
       
   125 
       
   126 /**
       
   127 A helper function of CopyObjectL.
       
   128 @param aNewFolderName the new full file folder name after copy.
       
   129 @return objecthandle of new copy of the folder.
       
   130 */
       
   131 TUint32 CMTPCopyObject::CopyFolderL(const TDesC& aNewFolderName)
       
   132 	{
       
   133 	__FLOG(_L8("CopyFolderL - Entry"));
       
   134 	const TDesC& suid(iObjectInfo->DesC(CMTPObjectMetaData::ESuid));
       
   135 	TUint32 handle;
       
   136 	if (iObjectInfo->Uint(CMTPObjectMetaData::EDataProviderId) == iFramework.DataProviderId())
       
   137 		{
       
   138 		GetPreviousPropertiesL(suid);
       
   139 		User::LeaveIfError(iFramework.Fs().MkDir(aNewFolderName));
       
   140 		SetPreviousPropertiesL(aNewFolderName);	
       
   141 		handle = UpdateObjectInfoL(aNewFolderName);
       
   142 		}
       
   143 	else
       
   144 		{
       
   145 		handle = iFramework.ObjectMgr().HandleL(aNewFolderName);
       
   146 		}
       
   147 	__FLOG(_L8("CopyFolderL - Exit"));
       
   148 	return handle;
       
   149 	}
       
   150 		
       
   151 /**
       
   152 Copy object operation
       
   153 @return the object handle of the resulting object.
       
   154 */
       
   155 TMTPResponseCode CMTPCopyObject::CopyObjectL(TUint32& aNewHandle)
       
   156 	{
       
   157 	__FLOG(_L8("CopyObjectL - Entry"));
       
   158 	TMTPResponseCode responseCode = EMTPRespCodeOK;
       
   159 	aNewHandle = KMTPHandleNone;
       
   160 	
       
   161 	GetParametersL();
       
   162 			
       
   163 	RBuf newObjectName;
       
   164 	newObjectName.CleanupClosePushL();
       
   165 	newObjectName.CreateL(KMaxFileName);
       
   166 	newObjectName = *iDest;
       
   167 	
       
   168 	const TDesC& suid(iObjectInfo->DesC(CMTPObjectMetaData::ESuid));
       
   169 	TParsePtrC fileNameParser(suid);
       
   170 	
       
   171 	// Check if the object is a folder or a file.
       
   172 	TBool isFolder = EFalse;
       
   173 	User::LeaveIfError(BaflUtils::IsFolder(iFramework.Fs(), suid, isFolder));	
       
   174 	
       
   175 	if(!isFolder)
       
   176 		{
       
   177 		if((newObjectName.Length() + fileNameParser.NameAndExt().Length()) <= newObjectName.MaxLength())
       
   178 			{
       
   179 			newObjectName.Append(fileNameParser.NameAndExt());
       
   180 			}
       
   181 		}
       
   182 	else // It is a folder.
       
   183 		{
       
   184 		TFileName rightMostFolderName;
       
   185 		User::LeaveIfError(BaflUtils::MostSignificantPartOfFullName(suid, rightMostFolderName));
       
   186 		if((newObjectName.Length() + rightMostFolderName.Length() + 1) <= newObjectName.MaxLength())
       
   187 			{
       
   188 			newObjectName.Append(rightMostFolderName);
       
   189 			// Add backslash.
       
   190 			_LIT(KBackSlash, "\\");
       
   191 			newObjectName.Append(KBackSlash);
       
   192 			}
       
   193 		}
       
   194 	responseCode = CanCopyObjectL(suid, newObjectName);    
       
   195 	if(responseCode == EMTPRespCodeOK)
       
   196 		{			
       
   197 		delete iFileMan;
       
   198 		iFileMan = NULL;
       
   199 		iFileMan = CFileMan::NewL(iFramework.Fs());
       
   200 		
       
   201 		if(!isFolder) // It is a file.
       
   202 			{
       
   203 			aNewHandle = CopyFileL(newObjectName);
       
   204 			}
       
   205 		else // It is a folder.
       
   206 			{
       
   207 			aNewHandle = CopyFolderL(newObjectName);
       
   208 			}
       
   209 		}
       
   210 	
       
   211 	CleanupStack::PopAndDestroy(); // newObjectName.
       
   212 	__FLOG(_L8("CopyObjectL - Exit"));
       
   213 	return responseCode;
       
   214 	}
       
   215 
       
   216 /**
       
   217 Retrieve the parameters of the request
       
   218 */	
       
   219 void CMTPCopyObject::GetParametersL()
       
   220 	{
       
   221 	__FLOG(_L8("GetParametersL - Entry"));
       
   222 	__ASSERT_DEBUG(iRequestChecker, Panic(EMTPDpRequestCheckNull));
       
   223 	
       
   224 	TUint32 objectHandle  = Request().Uint32(TMTPTypeRequest::ERequestParameter1);
       
   225 	iStorageId = Request().Uint32(TMTPTypeRequest::ERequestParameter2);
       
   226 	TUint32 parentObjectHandle  = Request().Uint32(TMTPTypeRequest::ERequestParameter3);
       
   227 	
       
   228 	//not taking owernship
       
   229 	iObjectInfo = iRequestChecker->GetObjectInfo(objectHandle); 
       
   230 	__ASSERT_DEBUG(iObjectInfo, Panic(EMTPDpObjectNull));	
       
   231 
       
   232 	if(parentObjectHandle == 0)
       
   233 		{
       
   234 		SetDefaultParentObjectL();
       
   235 		}
       
   236 	else	
       
   237 		{
       
   238 		CMTPObjectMetaData* parentObjectInfo = iRequestChecker->GetObjectInfo(parentObjectHandle);
       
   239 		__ASSERT_DEBUG(parentObjectInfo, Panic(EMTPDpObjectNull));
       
   240 		delete iDest;
       
   241 		iDest = NULL;
       
   242 		iDest = parentObjectInfo->DesC(CMTPObjectMetaData::ESuid).AllocL();
       
   243 		iNewParentHandle = parentObjectHandle;
       
   244 		}
       
   245 	__FLOG(_L8("GetParametersL - Exit"));	
       
   246 	}
       
   247 	
       
   248 /**
       
   249 Get a default parent object, ff the request does not specify a parent object, 
       
   250 */
       
   251 void CMTPCopyObject::SetDefaultParentObjectL()
       
   252 	{
       
   253 	__FLOG(_L8("SetDefaultParentObjectL - Entry"));
       
   254 
       
   255 	const CMTPStorageMetaData& storageMetaData( iFramework.StorageMgr().StorageL(iStorageId) );
       
   256 	const TDesC& driveBuf( storageMetaData.DesC(CMTPStorageMetaData::EStorageSuid) );
       
   257 	delete iDest;
       
   258 	iDest = NULL;
       
   259 	iDest = driveBuf.AllocL();
       
   260 	iNewParentHandle = KMTPHandleNoParent;
       
   261 	    
       
   262 	__FLOG(_L8("SetDefaultParentObjectL - Exit"));
       
   263 	}
       
   264 
       
   265 /**
       
   266 Check if we can copy the file to the new location
       
   267 */
       
   268 TMTPResponseCode CMTPCopyObject::CanCopyObjectL(const TDesC& aOldName, const TDesC& aNewName) const
       
   269 	{
       
   270 	__FLOG(_L8("CanCopyObjectL - Entry"));
       
   271 	TMTPResponseCode result = EMTPRespCodeOK;
       
   272 
       
   273 	TEntry fileEntry;
       
   274 	User::LeaveIfError(iFramework.Fs().Entry(aOldName, fileEntry));
       
   275 	TInt drive(iFramework.StorageMgr().DriveNumber(iStorageId));
       
   276 	User::LeaveIfError(drive);
       
   277 	TVolumeInfo volumeInfo;
       
   278 	User::LeaveIfError(iFramework.Fs().Volume(volumeInfo, drive));
       
   279 	
       
   280 #ifdef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
   281     if(volumeInfo.iFree < fileEntry.FileSize())
       
   282 #else
       
   283     if(volumeInfo.iFree < fileEntry.iSize)
       
   284 #endif
       
   285 		{
       
   286 		result = EMTPRespCodeStoreFull;
       
   287 		}
       
   288 	else if (BaflUtils::FileExists(iFramework.Fs(), aNewName))			
       
   289 		{
       
   290 		result = EMTPRespCodeInvalidParentObject;
       
   291 		}
       
   292 	__FLOG_VA((_L8("CanCopyObjectL - Exit with response code 0x%04X"), result));
       
   293 	return result;	
       
   294 	}
       
   295 
       
   296 /**
       
   297 Save the object properties before doing the copy
       
   298 */
       
   299 void CMTPCopyObject::GetPreviousPropertiesL(const TDesC& aFileName)
       
   300 	{
       
   301 	__FLOG(_L8("GetPreviousPropertiesL - Entry"));
       
   302 	User::LeaveIfError(iFramework.Fs().Modified(aFileName, iPreviousModifiedTime));
       
   303 	__FLOG(_L8("GetPreviousPropertiesL - Exit"));
       
   304 	}
       
   305 
       
   306 /**
       
   307 Set the object properties after doing the copy
       
   308 */
       
   309 void CMTPCopyObject::SetPreviousPropertiesL(const TDesC& aFileName)
       
   310 	{
       
   311 	__FLOG(_L8("SetPreviousPropertiesL - Entry"));
       
   312 	User::LeaveIfError(iFramework.Fs().SetModified(aFileName, iPreviousModifiedTime));
       
   313 	__FLOG(_L8("SetPreviousPropertiesL - Exit"));
       
   314 	}
       
   315 
       
   316 /**
       
   317  Update object info in the database.
       
   318 */
       
   319 TUint32 CMTPCopyObject::UpdateObjectInfoL(const TDesC& aNewObjectName)
       
   320 	{
       
   321 	__FLOG(_L8("UpdateObjectInfoL - Entry"));	
       
   322 	
       
   323 	// We should not modify this object's handle, so just get a "copy".
       
   324 	CMTPObjectMetaData* objectInfo(CMTPObjectMetaData::NewLC());
       
   325 	const TMTPTypeUint32 objectHandle(iObjectInfo->Uint(CMTPObjectMetaData::EHandle));
       
   326 	if(iFramework.ObjectMgr().ObjectL(objectHandle, *objectInfo))
       
   327 		{
       
   328 		objectInfo->SetDesCL(CMTPObjectMetaData::ESuid, aNewObjectName);
       
   329 		objectInfo->SetUint(CMTPObjectMetaData::EParentHandle, iNewParentHandle);
       
   330 		//Modify storage Id.
       
   331 		objectInfo->SetUint(CMTPObjectMetaData::EStorageId, iStorageId);
       
   332 		iFramework.ObjectMgr().InsertObjectL(*objectInfo);
       
   333 		}
       
   334 	else
       
   335 		{
       
   336 		User::Leave(KErrCorrupt);
       
   337 		}
       
   338 	TUint32 handle = objectInfo->Uint(CMTPObjectMetaData::EHandle);	
       
   339 	CleanupStack::PopAndDestroy(objectInfo);
       
   340 	
       
   341 	__FLOG(_L8("UpdateObjectInfoL - Exit"));
       
   342 	
       
   343 	return handle;	
       
   344 	}