mtpfws/mtpfw/dataproviders/dputility/src/cmtpmoveobject.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 "cmtpmoveobject.h"
       
    27 #include "mtpdppanic.h"
       
    28 
       
    29 
       
    30 __FLOG_STMT(_LIT8(KComponent,"MoveObject");)
       
    31 
       
    32 /**
       
    33 Verification data for the MoveObject request
       
    34 */    
       
    35 const TMTPRequestElementInfo KMTPMoveObjectPolicy[] = 
       
    36     {
       
    37     	{TMTPTypeRequest::ERequestParameter1, EMTPElementTypeObjectHandle, EMTPElementAttrFileOrDir | EMTPElementAttrWrite, 0, 0, 0},   	
       
    38         {TMTPTypeRequest::ERequestParameter2, EMTPElementTypeStorageId, EMTPElementAttrWrite, 0, 0, 0},                
       
    39         {TMTPTypeRequest::ERequestParameter3, EMTPElementTypeObjectHandle, EMTPElementAttrDir | EMTPElementAttrWrite, 1, 0, 0}
       
    40     };
       
    41 
       
    42 /**
       
    43 Two-phase construction method
       
    44 @param aPlugin	The data provider plugin
       
    45 @param aFramework	The data provider framework
       
    46 @param aConnection	The connection from which the request comes
       
    47 @return a pointer to the created request processor object
       
    48 */     
       
    49 EXPORT_C MMTPRequestProcessor* CMTPMoveObject::NewL(MMTPDataProviderFramework& aFramework, MMTPConnection& aConnection)
       
    50 	{
       
    51 	CMTPMoveObject* self = new (ELeave) CMTPMoveObject(aFramework, aConnection);
       
    52 	CleanupStack::PushL(self);
       
    53 	self->ConstructL();
       
    54 	CleanupStack::Pop(self);	
       
    55 	return self;
       
    56 	}
       
    57 
       
    58 
       
    59 /**
       
    60 Destructor
       
    61 */	
       
    62 EXPORT_C CMTPMoveObject::~CMTPMoveObject()
       
    63 	{	
       
    64 	delete iDest;
       
    65 	delete iFileMan;
       
    66 	delete iPathToMove;
       
    67 	delete iNewRootFolder;
       
    68 	__FLOG_CLOSE;
       
    69 	}
       
    70 
       
    71 /**
       
    72 Standard c++ constructor
       
    73 */	
       
    74 CMTPMoveObject::CMTPMoveObject(MMTPDataProviderFramework& aFramework, MMTPConnection& aConnection) :
       
    75 	CMTPRequestProcessor(aFramework, aConnection, sizeof(KMTPMoveObjectPolicy)/sizeof(TMTPRequestElementInfo), KMTPMoveObjectPolicy),
       
    76 	iMoveObjectIndex(0)
       
    77 	{
       
    78 	__FLOG_OPEN(KMTPSubsystem, KComponent);
       
    79 	}
       
    80 	
       
    81 /**
       
    82 MoveObject request handler
       
    83 */		
       
    84 void CMTPMoveObject::ServiceL()
       
    85 	{	
       
    86 	TMTPResponseCode ret = MoveObjectL();
       
    87 	if (EMTPRespCodeOK != ret)
       
    88 		{
       
    89 		SendResponseL(ret);
       
    90 		}
       
    91 	}
       
    92 
       
    93 /**
       
    94  Second phase constructor
       
    95 */
       
    96 void CMTPMoveObject::ConstructL()
       
    97     {
       
    98     }
       
    99     
       
   100 void CMTPMoveObject::RunL()
       
   101 	{
       
   102 	__FLOG(_L8("RunL - Entry"));
       
   103     SendResponseL( EMTPRespCodeOK );
       
   104     __FLOG(_L8("RunL - Exit"));
       
   105 	}
       
   106     
       
   107 TInt CMTPMoveObject::RunError(TInt /*aError*/)
       
   108 	{
       
   109 	TRAP_IGNORE(SendResponseL(EMTPRespCodeGeneralError));
       
   110     return KErrNone;  
       
   111 	}
       
   112 
       
   113 /**
       
   114 A helper function of MoveObjectL.
       
   115 @param aNewFileName the new file name after the object is moved.
       
   116 */
       
   117 void CMTPMoveObject::MoveFileL(const TDesC& aNewFileName)	
       
   118 	{
       
   119 	__FLOG(_L8("MoveFileL - Entry"));
       
   120 	const TDesC& suid(iObjectInfo->DesC(CMTPObjectMetaData::ESuid));
       
   121 	GetPreviousPropertiesL(suid);
       
   122 	User::LeaveIfError(iFileMan->Move(suid, *iDest));
       
   123 	SetPreviousPropertiesL(aNewFileName);
       
   124 	iObjectInfo->SetDesCL(CMTPObjectMetaData::ESuid, aNewFileName);
       
   125 	iObjectInfo->SetUint(CMTPObjectMetaData::EStorageId, iStorageId);
       
   126 	iObjectInfo->SetUint(CMTPObjectMetaData::EParentHandle, iNewParentHandle);
       
   127 	iFramework.ObjectMgr().ModifyObjectL(*iObjectInfo);
       
   128 	__FLOG(_L8("MoveFileL - Exit"));
       
   129 	}
       
   130 
       
   131 /**
       
   132 A helper function of MoveObjectL.
       
   133 @param aNewFolderName the new file folder name after the folder is moved.
       
   134 */
       
   135 void CMTPMoveObject::MoveFolderL()
       
   136 	{
       
   137 	__FLOG(_L8("MoveFolderL - Entry"));
       
   138 	
       
   139 	RBuf oldFolderName;
       
   140 	oldFolderName.CreateL(KMaxFileName);
       
   141 	oldFolderName.CleanupClosePushL();
       
   142 	oldFolderName = iObjectInfo->DesC(CMTPObjectMetaData::ESuid);
       
   143 	iPathToMove = oldFolderName.AllocL();
       
   144 	
       
   145 	if (iObjectInfo->Uint(CMTPObjectMetaData::EDataProviderId) == iFramework.DataProviderId())
       
   146 		{
       
   147 		GetPreviousPropertiesL(oldFolderName);
       
   148 		// Remove backslash.
       
   149 		oldFolderName.SetLength(oldFolderName.Length() - 1);	
       
   150 		SetPreviousPropertiesL(*iNewRootFolder);
       
   151 		_LIT(KBackSlash, "\\");
       
   152 		oldFolderName.Append(KBackSlash);	
       
   153 			
       
   154 		iObjectInfo->SetDesCL(CMTPObjectMetaData::ESuid, *iNewRootFolder);
       
   155 		iObjectInfo->SetUint(CMTPObjectMetaData::EParentHandle, iNewParentHandle);
       
   156 		iObjectInfo->SetUint(CMTPObjectMetaData::EStorageId, iStorageId);
       
   157 		iFramework.ObjectMgr().ModifyObjectL(*iObjectInfo);
       
   158 		}
       
   159 	
       
   160 	CleanupStack::PopAndDestroy(); // oldFolderName.
       
   161 		
       
   162 	__FLOG(_L8("MoveFolderL - Exit"));
       
   163 	}
       
   164 		
       
   165 /**
       
   166 move object operations
       
   167 @return A valid MTP response code.
       
   168 */
       
   169 TMTPResponseCode CMTPMoveObject::MoveObjectL()
       
   170 	{
       
   171 	__FLOG(_L8("MoveObjectL - Entry"));
       
   172 	TMTPResponseCode responseCode = EMTPRespCodeOK;
       
   173 	
       
   174 	GetParametersL();
       
   175 				
       
   176 	RBuf newObjectName;
       
   177 	newObjectName.CreateL(KMaxFileName);
       
   178 	newObjectName.CleanupClosePushL();
       
   179 	newObjectName = *iDest;
       
   180 	
       
   181 	const TDesC& suid(iObjectInfo->DesC(CMTPObjectMetaData::ESuid));
       
   182 	TParsePtrC fileNameParser(suid);
       
   183 	
       
   184 	// Check if the object is a folder or a file.
       
   185 	TBool isFolder = EFalse;
       
   186 	User::LeaveIfError(BaflUtils::IsFolder(iFramework.Fs(), suid, isFolder));	
       
   187 				
       
   188 	if(!isFolder)
       
   189 		{
       
   190 		if((newObjectName.Length() + fileNameParser.NameAndExt().Length()) <= newObjectName.MaxLength())
       
   191 			{
       
   192 			newObjectName.Append(fileNameParser.NameAndExt());
       
   193 			}
       
   194 		responseCode = CanMoveObjectL(suid, newObjectName);			
       
   195 		}
       
   196 	else // It is a folder.
       
   197 		{
       
   198 		TFileName rightMostFolderName;		
       
   199 		User::LeaveIfError(BaflUtils::MostSignificantPartOfFullName(suid, rightMostFolderName));
       
   200 		if((newObjectName.Length() + rightMostFolderName.Length() + 1) <= newObjectName.MaxLength())
       
   201 			{
       
   202 			newObjectName.Append(rightMostFolderName);
       
   203 			// Add backslash.
       
   204 			_LIT(KBackSlash, "\\");
       
   205 			newObjectName.Append(KBackSlash);
       
   206 			}
       
   207 		}
       
   208 		
       
   209 	iNewRootFolder = newObjectName.AllocL();
       
   210 	__FLOG(*iNewRootFolder);
       
   211 		
       
   212 	if(responseCode == EMTPRespCodeOK)
       
   213 		{			
       
   214 		delete iFileMan;
       
   215 		iFileMan = NULL;
       
   216 		iFileMan = CFileMan::NewL(iFramework.Fs());
       
   217 		
       
   218 		if(!isFolder)
       
   219 			{
       
   220 			MoveFileL(newObjectName);
       
   221 			SendResponseL(responseCode);
       
   222 			}
       
   223 		else
       
   224 			{		
       
   225 			MoveFolderL();
       
   226 			SendResponseL(responseCode);
       
   227 			}
       
   228 		}
       
   229 	CleanupStack::PopAndDestroy(); // newObjectName.
       
   230 	__FLOG(_L8("MoveObjectL - Exit"));
       
   231 	return responseCode;
       
   232 	}
       
   233 
       
   234 /**
       
   235 Retrieve the parameters of the request
       
   236 */	
       
   237 void CMTPMoveObject::GetParametersL()
       
   238 	{
       
   239 	__FLOG(_L8("GetParametersL - Entry"));
       
   240 	__ASSERT_DEBUG(iRequestChecker, Panic(EMTPDpRequestCheckNull));
       
   241 	
       
   242 	TUint32 objectHandle  = Request().Uint32(TMTPTypeRequest::ERequestParameter1);
       
   243 	iStorageId = Request().Uint32(TMTPTypeRequest::ERequestParameter2);
       
   244 	iNewParentHandle  = Request().Uint32(TMTPTypeRequest::ERequestParameter3);
       
   245 	
       
   246 	//not taking owernship
       
   247 	iObjectInfo = iRequestChecker->GetObjectInfo(objectHandle); 
       
   248 	__ASSERT_DEBUG(iObjectInfo, Panic(EMTPDpObjectNull));	
       
   249 
       
   250 	if(iNewParentHandle == 0)
       
   251 		{
       
   252 		SetDefaultParentObjectL();
       
   253 		}
       
   254 	else	
       
   255 		{
       
   256 		CMTPObjectMetaData* parentObjectInfo = iRequestChecker->GetObjectInfo(iNewParentHandle);
       
   257 		__ASSERT_DEBUG(parentObjectInfo, Panic(EMTPDpObjectNull));
       
   258 		delete iDest;
       
   259 		iDest = NULL;
       
   260 		iDest = parentObjectInfo->DesC(CMTPObjectMetaData::ESuid).AllocL();
       
   261 		}
       
   262 	__FLOG(_L8("GetParametersL - Exit"));	
       
   263 	}
       
   264 	
       
   265 /**
       
   266 Get a default parent object, ff the request does not specify a parent object, 
       
   267 */
       
   268 void CMTPMoveObject::SetDefaultParentObjectL()
       
   269 	{
       
   270 	__FLOG(_L8("SetDefaultParentObjectL - Entry"));
       
   271 	const CMTPStorageMetaData& storageMetaData( iFramework.StorageMgr().StorageL(iStorageId) );
       
   272 	const TDesC& driveBuf( storageMetaData.DesC(CMTPStorageMetaData::EStorageSuid) );
       
   273 	delete iDest;
       
   274 	iDest = NULL;
       
   275 	iDest = driveBuf.AllocL();
       
   276 	iNewParentHandle = KMTPHandleNoParent;
       
   277 	__FLOG(_L8("SetDefaultParentObjectL - Exit"));
       
   278 	}
       
   279 
       
   280 /**
       
   281 Check if we can move the file to the new location
       
   282 */
       
   283 TMTPResponseCode CMTPMoveObject::CanMoveObjectL(const TDesC& aOldName, const TDesC& aNewName) const
       
   284 	{
       
   285 	__FLOG(_L8("CanMoveObjectL - Entry"));
       
   286 	TMTPResponseCode result = EMTPRespCodeOK;
       
   287 
       
   288 	TEntry fileEntry;
       
   289 	User::LeaveIfError(iFramework.Fs().Entry(aOldName, fileEntry));
       
   290 	TInt drive(iFramework.StorageMgr().DriveNumber(iStorageId));
       
   291 	User::LeaveIfError(drive);
       
   292 	TVolumeInfo volumeInfo;
       
   293 	User::LeaveIfError(iFramework.Fs().Volume(volumeInfo, drive));
       
   294 	
       
   295 #ifdef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
   296     if(volumeInfo.iFree < fileEntry.FileSize())
       
   297 #else
       
   298     if(volumeInfo.iFree < fileEntry.iSize)
       
   299 #endif
       
   300 		{
       
   301 		result = EMTPRespCodeStoreFull;
       
   302 		}
       
   303 	else if (BaflUtils::FileExists(iFramework.Fs(), aNewName))			
       
   304 		{
       
   305 		result = EMTPRespCodeInvalidParentObject;
       
   306 		}
       
   307 	__FLOG_VA((_L8("CanMoveObjectL - Exit with response code 0x%04X"), result));
       
   308 	return result;	
       
   309 	}
       
   310 
       
   311 /**
       
   312 Save the object properties before moving
       
   313 */
       
   314 void CMTPMoveObject::GetPreviousPropertiesL(const TDesC& aFileName)
       
   315 	{
       
   316 	__FLOG(_L8("GetPreviousPropertiesL - Entry"));
       
   317 	User::LeaveIfError(iFramework.Fs().Modified(aFileName, iPreviousModifiedTime));
       
   318 	__FLOG(_L8("GetPreviousPropertiesL - Exit"));
       
   319 	}
       
   320 
       
   321 /**
       
   322 Set the object properties after moving
       
   323 */
       
   324 void CMTPMoveObject::SetPreviousPropertiesL(const TDesC& aFileName)
       
   325 	{
       
   326 	__FLOG(_L8("SetPreviousPropertiesL - Entry"));
       
   327 	User::LeaveIfError(iFramework.Fs().SetModified(aFileName, iPreviousModifiedTime));
       
   328 	__FLOG(_L8("SetPreviousPropertiesL - Exit"));
       
   329 	}
       
   330 
       
   331 
       
   332 /* This function will actually delete the orginal folders from the file system. */
       
   333 TMTPResponseCode CMTPMoveObject::FinalPhaseMove()
       
   334 	{
       
   335 	__FLOG(_L8("FinalPhaseMove - Entry"));
       
   336 	TMTPResponseCode ret = EMTPRespCodeOK;
       
   337 	__FLOG(*iPathToMove);
       
   338 	TInt rel = iFileMan->RmDir(*iPathToMove);
       
   339 	__FLOG_VA((_L8("Error code of RmDir is %d"),rel));
       
   340 	if (rel != KErrNone)
       
   341 		{
       
   342 		ret = EMTPRespCodeGeneralError;
       
   343 		}
       
   344 	__FLOG(_L8("FinalPhaseMove - Exit"));
       
   345 	return ret;
       
   346 	}
       
   347 
       
   348 /* Move a single object and update the database */	
       
   349 void CMTPMoveObject::MoveAndUpdateL(TUint32 aObjectHandle)
       
   350 	{
       
   351 	__FLOG(_L8("MoveAndUpdateL - Entry"));
       
   352 	CMTPObjectMetaData* objectInfo(CMTPObjectMetaData::NewLC());
       
   353 	RBuf fileName;
       
   354 	fileName.CreateL(KMaxFileName);
       
   355 	fileName.CleanupClosePushL();	
       
   356 	RBuf rightPartName;
       
   357 	rightPartName.CreateL(KMaxFileName);
       
   358 	rightPartName.CleanupClosePushL();
       
   359 	RBuf oldName;
       
   360 	oldName.CreateL(KMaxFileName);	
       
   361 	oldName.CleanupClosePushL();
       
   362 		
       
   363 	if(iFramework.ObjectMgr().ObjectL(TMTPTypeUint32(aObjectHandle), *objectInfo))
       
   364 		{	
       
   365 		fileName = objectInfo->DesC(CMTPObjectMetaData::ESuid);
       
   366 		oldName = fileName;
       
   367 				
       
   368 		if (objectInfo->Uint(CMTPObjectMetaData::EDataProviderId) == iFramework.DataProviderId())
       
   369 			{	
       
   370 			rightPartName = fileName.Right(fileName.Length() - iPathToMove->Length());
       
   371 			
       
   372 			if((iNewRootFolder->Length() + rightPartName.Length()) > fileName.MaxLength())
       
   373 				{
       
   374 				User::Leave(KErrCorrupt);
       
   375 				}
       
   376 				
       
   377 			fileName.Zero();
       
   378 			fileName.Append(*iNewRootFolder);
       
   379 			fileName.Append(rightPartName);
       
   380 			objectInfo->SetDesCL(CMTPObjectMetaData::ESuid, fileName);				
       
   381 			objectInfo->SetUint(CMTPObjectMetaData::EStorageId, iStorageId);
       
   382 			iFramework.ObjectMgr().ModifyObjectL(*objectInfo);
       
   383 			}
       
   384 		}
       
   385 	else
       
   386 		{
       
   387 		User::Leave(KErrCorrupt);
       
   388 		}	
       
   389 			
       
   390 	iFileMan->Move(oldName, fileName);
       
   391 	
       
   392 	CleanupStack::PopAndDestroy(&oldName);	
       
   393 	CleanupStack::PopAndDestroy(&rightPartName); 
       
   394 	CleanupStack::PopAndDestroy(&fileName); 	
       
   395 	CleanupStack::PopAndDestroy(objectInfo);
       
   396 	__FLOG(_L8("MoveAndUpdateL - Exit"));	
       
   397 	}
       
   398 
       
   399 
       
   400 
       
   401 
       
   402