messagingfw/msgsrvnstore/server/src/cmsvversion0version1converter.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2008-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 /**
       
    18  * HEADER FILES
       
    19  */
       
    20 
       
    21 #include <s32fileiter.h>
       
    22 #include "cmsvversion0version1converter.h"
       
    23 
       
    24 
       
    25 // Literals
       
    26 const TInt KMsvLongestStringAllowed=KMaxTInt32; 
       
    27 
       
    28 /*
       
    29 CMsvVersion0Version1Converter()
       
    30 Default Constructor
       
    31  
       
    32 @param aDBAdapter: CMsvDbAdapter object reference.
       
    33 @param aDriveNum: Drive Number
       
    34 @return None
       
    35 */	
       
    36 CMsvVersion0Version1Converter::CMsvVersion0Version1Converter(CMsvDBAdapter& aDBAdapter,TDriveNumber aDriveNum,TBool aConversionResume)
       
    37 : iDBAdapter(aDBAdapter),iDrive(aDriveNum),iResume(aConversionResume)
       
    38 	{
       
    39 	}
       
    40 
       
    41 /*
       
    42 NewL()
       
    43 Constructs and returns CMsvVersion0Version1Converter object
       
    44 
       
    45 @param aFileStore:  A reference to store object. This supports full manipulation of store contents.
       
    46 @param aDBAapter: A handle to db ada[ter object
       
    47 @param aDriveNum: The Drive number of the drive to be converted.
       
    48 @return : CMsvVersion0Version1Converter object
       
    49 */
       
    50 CMsvVersion0Version1Converter* CMsvVersion0Version1Converter::NewL(CMsvDBAdapter& aDBAapter,TDriveNumber aDriveNum,TBool aConversionResume)
       
    51 	{
       
    52 	CMsvVersion0Version1Converter* self = new(ELeave) CMsvVersion0Version1Converter(aDBAapter,aDriveNum,aConversionResume);
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL();
       
    55 	CleanupStack::Pop(self);
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 /*
       
    60 ~CMsvVersion0Version1Converter()
       
    61 Destructor
       
    62 
       
    63 @param : None 
       
    64 @return: None
       
    65 */
       
    66 CMsvVersion0Version1Converter::~CMsvVersion0Version1Converter()
       
    67 	{
       
    68 	delete iStream;
       
    69 	delete iDescriptionBuffer;
       
    70 	delete iDetailsBuffer;
       
    71 	}	
       
    72 
       
    73 /*
       
    74 ConstructL()
       
    75 Second phase constructor.
       
    76 
       
    77 @param aDriveNum: Drive to be converted.
       
    78 @param aConversionResume: Boolean flag indicating if conversion is to be resumed.
       
    79 @return None
       
    80 @internalComponent
       
    81 */	
       
    82 void CMsvVersion0Version1Converter::ConstructL()
       
    83 	{
       
    84 	// create the stream index
       
    85 	iStream=new (ELeave) CArrayFixFlat<TStreamId>(KMsvStreamIndexGranularity);
       
    86 	}
       
    87 
       
    88 /*
       
    89 ConvertMessageStoreL()
       
    90 Starts conversion of version 0 to version 1 message store.
       
    91 If the conversion is to be resumed, it reads the resume status from conversion status table and starts reading entries from their.
       
    92 For a new conversion request, it creates the conversion status entry in the status table.
       
    93 
       
    94 @param :None
       
    95 @return: None
       
    96 @internalComponent
       
    97 */
       
    98 void CMsvVersion0Version1Converter::ConvertMessageStoreL()
       
    99 	{
       
   100 	RFs fs;
       
   101 	User::LeaveIfError(fs.Connect()); 
       
   102 	CleanupClosePushL(fs);
       
   103 	
       
   104 	TBuf<KMaxPath> filePath;
       
   105 	TPtrC drive = TDriveUnit(iDrive).Name();
       
   106 	filePath.Append(drive);
       
   107 	filePath.Append(KIndexFilePath);
       
   108 		
       
   109 	RFile file;
       
   110 	TInt error = file.Open(fs, filePath, EFileShareAny|EFileWrite);
       
   111 	
       
   112 	iIndexStore = CPermanentFileStore::FromL(file);
       
   113 	CleanupStack::PushL(iIndexStore);
       
   114 			
       
   115 	RestoreStreamIndexL();
       
   116 
       
   117 	// Check for ConversionStatus table
       
   118 	if(iResume)
       
   119 		{
       
   120 		iDBAdapter.GetConversionStatusL(iResumeStatus);
       
   121 		iEntryStreamIndex = iStream->Count() - iResumeStatus.remainingCount;
       
   122 		}
       
   123 	else
       
   124 		{
       
   125 		iEntryStreamIndex += 2;
       
   126 
       
   127 		iResumeStatus.lastId = 0;
       
   128 		iResumeStatus.mtmId.iUid = 0;
       
   129 		iResumeStatus.serviceId = 0;
       
   130 		iResumeStatus.sourceVersion = 0;
       
   131 		iResumeStatus.targetVersion = 1;
       
   132 		iResumeStatus.remainingCount = iStream->Count() - 1;
       
   133 		iDBAdapter.AddConversionStatusEntryL(iResumeStatus);
       
   134 		iResumeStatus.remainingCount --;
       
   135 		}
       
   136 
       
   137 	// read TMsvEntries now
       
   138 	while(ReadNextEntriesL())
       
   139 		{
       
   140 		}
       
   141 		
       
   142 	// update remaining visible parent id's
       
   143 	UpdateVisibleParentL();
       
   144 	// we are finished with v0-v1 conversion. Lets remove the status entry from resumestatus table
       
   145 	iDBAdapter.RemoveConversionStatusEntriesL();
       
   146 
       
   147 	CleanupStack::PopAndDestroy(2);  //iIndexStore,fs
       
   148 	file.Close();
       
   149 	fs.Close();
       
   150 	}
       
   151 	
       
   152 /*
       
   153 GetPropertyValue()
       
   154 Gets the value of RProperty object. This value is used to check if cancel request is 
       
   155 issued for the store conversion in progress
       
   156 
       
   157 @param None
       
   158 @return: Value of the property
       
   159 @internalComponent
       
   160 */
       
   161 
       
   162 TInt CMsvVersion0Version1Converter::GetPropertyValue()
       
   163 	{
       
   164 	TInt value;
       
   165 	TInt err = RProperty::Get(KMyPropertyCat,KMyPropertyName, value);
       
   166 	return value;
       
   167 	}
       
   168 /*
       
   169 RestoreStreamIndexL()
       
   170 Restores all the stream index id's (id of TMveEntry) by reading them from the index file store.
       
   171 These ids are than stored in an array and for each of them, its TMsvEntry restored from the file store.
       
   172 
       
   173 @param None
       
   174 @return: None
       
   175 @internalComponent
       
   176 */
       
   177 void CMsvVersion0Version1Converter::RestoreStreamIndexL()
       
   178 	{
       
   179 	// Create a stream iterator
       
   180 	RPermanentFileStoreIter iter;
       
   181 	iter.ResetLC(*iIndexStore);
       
   182 
       
   183 	// Build the new stream index
       
   184 	TStreamId id;
       
   185 	while( (id = iter.NextL()) != KNullStreamIdValue )
       
   186 		{
       
   187 		iStream->AppendL(id);
       
   188 		}
       
   189 	CleanupStack::PopAndDestroy(&iter);
       
   190 	}
       
   191 
       
   192 
       
   193 /*
       
   194 ReadNextEntriesL()
       
   195 Prepares the index that determines how many entries will be read in each iteration
       
   196 It than reads the index file on the initial and final range specified.
       
   197 
       
   198 @param None
       
   199 @return: None
       
   200 @internalComponent
       
   201 */
       
   202 TBool CMsvVersion0Version1Converter::ReadNextEntriesL()
       
   203 	{
       
   204 	if (iEntryStreamIndex<iStream->Count())
       
   205 		{
       
   206 		TInt nextIndex = Min(iEntryStreamIndex+KIndexReadGranularity, iStream->Count());
       
   207 		PopulateDatabaseL(iEntryStreamIndex, nextIndex);
       
   208 		iEntryStreamIndex = nextIndex;
       
   209 		return ETrue;
       
   210 		}
       
   211 	return EFalse;
       
   212 	}
       
   213 
       
   214 /*
       
   215 PopulateDatabaseL()
       
   216 Reads each entry information from the file stream and stores this in the database table.
       
   217 Also updates the conversion status table entry for each iteration = 10.
       
   218 
       
   219 @param aFirstEntry: Initial index
       
   220 @param aNextEntry: Final index
       
   221 @return:None
       
   222 @internalComponent
       
   223 */
       
   224 void CMsvVersion0Version1Converter::PopulateDatabaseL(TInt aFirstEntry, TInt aNextEntry)
       
   225 	{
       
   226 	iDBAdapter.BeginTransactionL();
       
   227 	
       
   228 	TInt i=aFirstEntry;
       
   229 	while (i<aNextEntry)
       
   230 		{
       
   231 		RStoreReadStream readStream;
       
   232 		readStream.OpenLC(*iIndexStore, iStream->At(i++));
       
   233 
       
   234 		InternalizeL(readStream);		
       
   235 		
       
   236 		TMsvId visParent = -99;
       
   237 		// can we find parents visFlag. 
       
   238 		TBool parentVisFlag = iDBAdapter.GetParentVisibleFlagL(iEntry.Parent());	
       
   239 					
       
   240 		if(parentVisFlag)
       
   241 			{
       
   242 			if( (iEntry.Visible()) && 
       
   243 		    (iEntry.iType == KUidMsvFolderEntry || iEntry.iType == KUidMsvServiceEntry)
       
   244 		      )
       
   245 				{
       
   246 				iEntry.SetVisibleFolderFlag(ETrue);
       
   247 				}
       
   248 			else
       
   249 				{
       
   250 				iEntry.SetVisibleFolderFlag(EFalse);
       
   251 				}
       
   252 			iDBAdapter.GetVisibleParentIdL(iEntry.Parent(),visParent);				
       
   253 			}
       
   254 	
       
   255 		
       
   256 		//create the db entry
       
   257 		iDBAdapter.CreateIndexEntryL(iEntry,visParent);
       
   258 		CleanupStack::PopAndDestroy(); //  readStream
       
   259 		}
       
   260 	
       
   261 	//check if we have a request for cancellation
       
   262 	TInt cancel = GetPropertyValue();
       
   263 	if(cancel == KErrCancel	)
       
   264 		{
       
   265 		User::Leave(KErrCancel);
       
   266 		}
       
   267 	// commit for this iteration
       
   268 	iResumeStatus.lastId = iEntry.iId;
       
   269 	iResumeStatus.remainingCount -= 10;
       
   270 	iDBAdapter.UpdateConversionStatusEntryL(iResumeStatus);
       
   271 	iDBAdapter.CommitTransactionL();
       
   272 	
       
   273 	}
       
   274 
       
   275 /*
       
   276 InternalizeL()
       
   277 Reads TMsvEntry from the stream.
       
   278 
       
   279 @param aStream: File stream 
       
   280 @return: None
       
   281 @internalComponent
       
   282 */
       
   283 void CMsvVersion0Version1Converter::InternalizeL(RReadStream& aStream)
       
   284 	{
       
   285 	// NOTE: This has been added in case versioning is required in the future
       
   286 	TUint8 ver;
       
   287 	aStream >> ver;
       
   288 
       
   289 	aStream >> iEntry;
       
   290 	TInt dummy = aStream.ReadUint32L();
       
   291 	dummy  =  aStream.ReadUint32L();
       
   292 
       
   293 	// Get last change and creation dates
       
   294 	aStream >> iLastChangeDays;
       
   295 	aStream >> iLastChangeMinutes;
       
   296 	aStream >> iCreatedDays;
       
   297 	aStream >> iCreatedMinutes;
       
   298 
       
   299 	HBufC* des = HBufC::NewL(aStream, KMsvLongestStringAllowed);
       
   300 	delete iDescriptionBuffer;
       
   301 	iDescriptionBuffer = des;
       
   302 
       
   303 	HBufC* det = HBufC::NewL(aStream, KMsvLongestStringAllowed);
       
   304 	delete iDetailsBuffer;
       
   305 	iDetailsBuffer = det;
       
   306 
       
   307 	iEntry.iDescription.Set(*iDescriptionBuffer);
       
   308 	iEntry.iDetails.Set(*iDetailsBuffer);
       
   309 	}
       
   310 	
       
   311 /*
       
   312 UpdateVisibleParentL()
       
   313 Update ids whose visible parent was not set previously
       
   314 
       
   315 @param None: 
       
   316 @return: None
       
   317 @internalComponent
       
   318 */
       
   319 void CMsvVersion0Version1Converter::UpdateVisibleParentL()
       
   320 	{
       
   321 				
       
   322 	TMsvId visibleParent;;
       
   323 	RArray<TMsvId> idArray;
       
   324 	CleanupClosePushL(idArray);
       
   325 	
       
   326 	iDBAdapter.GetRemainingIdsL(idArray);
       
   327 	iDBAdapter.BeginTransactionL();
       
   328 	
       
   329 	for(TInt i = 0; i<idArray.Count(); i++)
       
   330 		{
       
   331 		iDBAdapter.GetVisibleParentL(idArray[i],visibleParent);
       
   332 		iDBAdapter.UpdateVisibleFolderIdL(idArray[i],visibleParent);
       
   333 		
       
   334 		if (i % 50 == 0 && i!=0)
       
   335 			{
       
   336 			iDBAdapter.CommitTransactionL();	
       
   337 			iDBAdapter.BeginTransactionL();
       
   338 			}
       
   339 		}
       
   340 	
       
   341 	iDBAdapter.CommitTransactionL();	
       
   342 	CleanupStack::PopAndDestroy();  //idArray
       
   343 	}
       
   344