symhelp/helpmodel/tsrc/tstore.cpp
changeset 0 1f04cf54edd8
equal deleted inserted replaced
-1:000000000000 0:1f04cf54edd8
       
     1 // Copyright (c) 2001-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 "tstore.h"
       
    17 
       
    18 // System includes
       
    19 #include "DBWRITER.H"
       
    20 #include <txtrich.h>
       
    21 #include <txtfmlyr.h>
       
    22 #include <txtmrtsr.h>
       
    23 #include <gdi.h>
       
    24 
       
    25 // Constants
       
    26 const TUint32 KBaseCategoryUID = 1;
       
    27 const TUint32 KHelpFileUID = 0;
       
    28 const TUint32 KNumberHelpTopics = 20;
       
    29 
       
    30 // Literal constants
       
    31 _LIT(KProgressText, ".");
       
    32 
       
    33 // Classes
       
    34 CTestWriter::CTestWriter(RFs& aFs)
       
    35 :	iFs(aFs), iCategoryUID(KBaseCategoryUID)
       
    36 	{
       
    37 	}
       
    38 
       
    39 CTestWriter::~CTestWriter()
       
    40 	{
       
    41 	delete iWriter;
       
    42 	delete iRichText;
       
    43 	delete iCharFormatLayer;
       
    44 	delete iParaFormatLayer;
       
    45 	}
       
    46 
       
    47 CTestWriter* CTestWriter::NewL(RFs& aFs)
       
    48 	{
       
    49 	CTestWriter* self = new(ELeave) CTestWriter(aFs);
       
    50 	CleanupStack::PushL(self);
       
    51 	self->ConstructL();
       
    52 	CleanupStack::Pop(self);
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 void CTestWriter::ConstructL()
       
    57 	{
       
    58 	iWriter = CHlpDbWriter::NewL(iFs);
       
    59 	iCharFormatLayer = CCharFormatLayer::NewL();
       
    60 	iParaFormatLayer = CParaFormatLayer::NewL();
       
    61 	iRichText = CRichText::NewL(iParaFormatLayer, iCharFormatLayer);	
       
    62 	}
       
    63 
       
    64 void CTestWriter::BuildFilesL()
       
    65 	{
       
    66 	//SinceDBWriter can not add/remove files from the protected rsc directory we put the file in the public area
       
    67 	_LIT(KHelpFileNameFormatString, "C:\\Public\\Help\\file%d.dat");
       
    68 
       
    69 	TheFsSession.MkDirAll(KHelpFileNameFormatString);
       
    70 	DeleteFilesL();
       
    71 	TInt i = 0;
       
    72 	TFileName fileName;
       
    73 	TInt numberOfFiles=25;
       
    74 
       
    75 	// build 25 1UID database files 
       
    76 	
       
    77 	
       
    78 	TheTest.Next(_L("Creating 25 databases with 1 category"));
       
    79 	for (i=0; i<numberOfFiles; i++)
       
    80 		{
       
    81 		iFileCount=i;
       
    82 		fileName.Format(KHelpFileNameFormatString, i);
       
    83 		BuildFileL(fileName, TUid::Uid(KHelpFileUID + i), 1);
       
    84 		}
       
    85 
       
    86 	// build 25 3UID database files 
       
    87 		
       
    88 	TheTest.Next(_L("\nCreating 25 databases with 3 categories"));
       
    89 	for (i=0; i<numberOfFiles; i++)
       
    90 		{
       
    91 		iFileCount=i;
       
    92 		fileName.Format(KHelpFileNameFormatString, (i+numberOfFiles));
       
    93 		BuildFileL(fileName, TUid::Uid(KHelpFileUID + i), 3);
       
    94 		}
       
    95 	
       
    96 	// build 25 15UID database files 
       
    97 	
       
    98 	TheTest.Next(_L("\nCreating 25 databases with 15 categories"));
       
    99 	for (i=0; i<numberOfFiles; i++)
       
   100 		{
       
   101 		iFileCount=i;
       
   102 		fileName.Format(KHelpFileNameFormatString, (i+2*numberOfFiles));
       
   103 		BuildFileL(fileName, TUid::Uid(KHelpFileUID + i), 15);
       
   104 		}
       
   105 	}
       
   106 
       
   107 void CTestWriter::BuildFileL(const TDesC& aFileName, TUid aUid, TUint aNumberCategoryUID)
       
   108 	{
       
   109 	// Pre writing
       
   110 	iWriter->CreateFileL(aFileName);
       
   111 	iWriter->SetDatabaseUidL(aUid);
       
   112 	iWriter->CreateDatabaseL();
       
   113 
       
   114 	// Writing
       
   115 	WriteUidTableL(aNumberCategoryUID);
       
   116 	WriteTopicTableL(aNumberCategoryUID);
       
   117 	WriteContextTableL();
       
   118 	WriteTopicIndexTableL();
       
   119 	WriteIndexTableL();
       
   120 
       
   121 	// Post writing
       
   122 	iWriter->CompressDatabaseL();
       
   123 	iWriter->CloseFileL();
       
   124 	}
       
   125 
       
   126 // write out the uid table 
       
   127 void CTestWriter::WriteUidTableL(TUint aNumberCategoryUID)
       
   128 	{
       
   129 	for (TUint i=0; i < aNumberCategoryUID; i++)
       
   130 		{
       
   131 		TUid uid;
       
   132 		uid.iUid=iCategoryUID+i;
       
   133 		iWriter->SetUidL(uid);
       
   134 		}
       
   135 	}
       
   136 
       
   137 void CTestWriter::WriteTopicTableL( TUint aNumberCategoryUID)
       
   138 	{
       
   139 	iWriter->BeginTransaction();
       
   140 
       
   141 	// loop for the number of categories in the database
       
   142 	for( TUint i = 0; i < aNumberCategoryUID;i++)
       
   143 		{
       
   144 		// write out a set of topics
       
   145 		WriteTopicsL(i);
       
   146 		iCategoryUID++;
       
   147 		}
       
   148 
       
   149 	// commit the transaction
       
   150 	iWriter->CommitTransaction();
       
   151 
       
   152 	TheTest.Printf(KProgressText);
       
   153 	}
       
   154 
       
   155 // write out a set of topics
       
   156 void CTestWriter::WriteTopicsL(TUint aTopicIDMultiplier)
       
   157 	{
       
   158 	TBuf<32> buf;
       
   159 	TBuf<1042> buf2;
       
   160 
       
   161 	CHlpTable* table = iWriter->TopicTable();
       
   162 
       
   163 	for(TUint i=0; i<KNumberHelpTopics; i++)
       
   164 		{
       
   165 		// create and write out a title
       
   166 		table->InsertL();
       
   167 		buf.Zero();
       
   168 		buf.Format(KTopicTitle, (KNumberHelpTopics*aTopicIDMultiplier)+i+(1000*iFileCount), iFileCount);
       
   169 		table->SetColL(KTopicTblTopicTitle,buf);
       
   170 
       
   171 		// add the title to the body text
       
   172 		buf2.Append(buf);
       
   173 		buf2.Append(TChar(2029));
       
   174 
       
   175 		// create and write the category string
       
   176 		buf.Zero();
       
   177 		buf.Format(KCategory, iCategoryUID);
       
   178 		table->SetColL(KTopicTblCategoryTitle, buf);
       
   179 
       
   180 		// add the category to the body text
       
   181 		buf2.Append(buf);
       
   182 		buf2.Append(TChar(2029));
       
   183 		buf2.Append(KBodyText);
       
   184 		iRichText->Reset();
       
   185 		iRichText->InsertL(0, buf2);
       
   186 		
       
   187 		// write out the body text
       
   188 		table->SetColL(KTopicTblTopicText, KTopicTblTopicMarkup, *iRichText);
       
   189 
       
   190 		// write out some synonyms
       
   191 		buf2.Zero();
       
   192 		table->SetColL(KTopicTblSynonym, CONST_CAST(TDesC&, KSynonymText()));
       
   193 
       
   194 		// calculate and write out the topicID
       
   195 		TInt id;
       
   196 		id=(KNumberHelpTopics*aTopicIDMultiplier)+i+(1000*iFileCount);
       
   197 		table->SetColL(KTopicTblTopicId, id);
       
   198 
       
   199 		// set the category UID
       
   200 		table->SetColL(KTopicTblCategoryUID, iCategoryUID);
       
   201 		
       
   202 		table->PutL();
       
   203 		}
       
   204 
       
   205 	TheTest.Printf(KProgressText);
       
   206 	}
       
   207 
       
   208 
       
   209 void CTestWriter::WriteContextTableL()
       
   210 	{
       
   211 	CHlpTable* table = iWriter->ContextTable();
       
   212 	iWriter->BeginTransaction();
       
   213 
       
   214 	TBuf<32> buf;
       
   215 	for(TInt i=1; i <=20; i++)
       
   216 		{
       
   217 		buf = KContextText;
       
   218 		buf.AppendNum(iFileCount);
       
   219 		buf.AppendNum(i);
       
   220 		table->InsertL();
       
   221 		table->SetColL(KContextTblTopicId, i+(1000*iFileCount));
       
   222 		table->SetColL(KContextTblContext, buf);
       
   223 		table->PutL();
       
   224 		}
       
   225 
       
   226 	iWriter->CommitTransaction();
       
   227 
       
   228 	TheTest.Printf(KProgressText);
       
   229 	}
       
   230 
       
   231 void CTestWriter::WriteIndexTableL()
       
   232 	{
       
   233 	CHlpTable* table = iWriter->IndexTable();
       
   234 	iWriter->BeginTransaction();
       
   235 
       
   236 	TBuf<120> buf;
       
   237 	for(TInt i=1; i<=20; i++)
       
   238 		{
       
   239 		table->InsertL();
       
   240 		buf.Format(KIndexPhrase, i, iFileCount);
       
   241 		table->SetColL(KIndexTblIndex, buf);
       
   242 		table->SetColL(KIndexTblIndexId, i);
       
   243 		table->PutL();
       
   244 		}
       
   245 
       
   246 	iWriter->CommitTransaction();
       
   247 
       
   248 	TheTest.Printf(KProgressText);
       
   249 	}
       
   250 
       
   251 void CTestWriter::WriteTopicIndexTableL()
       
   252 	{
       
   253 	CHlpTable* table = iWriter->TopicIndexTable();
       
   254 	iWriter->BeginTransaction();
       
   255 
       
   256 	for(TInt i=1; i<=20; i++)
       
   257 		{
       
   258 		table->InsertL();
       
   259 		const TInt id = i+(1000*iFileCount);
       
   260 		table->SetColL(KTopicIndexTblTopicId, id);
       
   261 		table->SetColL(KTopicIndexTblIndexId, i);
       
   262 		table->PutL();
       
   263 		}
       
   264 
       
   265 	iWriter->CommitTransaction();
       
   266 
       
   267 	TheTest.Printf(KProgressText);
       
   268 	}
       
   269 
       
   270 void CTestWriter::DeleteFilesL()
       
   271 	{
       
   272 	_LIT(KHelpFileNameFormatString, "C:\\Public\\Help\\*.*");
       
   273 	CFileMan* fileman = CFileMan::NewL(iFs);
       
   274 	CleanupStack::PushL(fileman);
       
   275 	TInt error = (fileman->Delete(KHelpFileNameFormatString)); 
       
   276 	if	(error != KErrNone && error != KErrNotFound)
       
   277 		User::Leave(error);
       
   278 
       
   279 	CleanupStack::PopAndDestroy(fileman);
       
   280 	}
       
   281 
       
   282 static void StartTestL()
       
   283     {
       
   284 	TheWriter = CTestWriter::NewL(TheFsSession);
       
   285 
       
   286 	TRAPD(error,
       
   287 		TheWriter->BuildFilesL();
       
   288 	); 
       
   289 	if	(error)
       
   290 		TheTest.Printf(_L("leave code=%d\n"), error);
       
   291 	else
       
   292 		TheTest.Printf(_L("\nok"));
       
   293 	TheTest(error == KErrNone || error == KErrNoMemory);
       
   294 
       
   295 	TheTest.Printf(_L("Last category written %d\n"), TheWriter->CategoryUID());
       
   296 	delete TheWriter;
       
   297     }
       
   298 
       
   299 static void TidyFilesL(TAny*)
       
   300 	{
       
   301 	TheWriter = CTestWriter::NewL(TheFsSession);
       
   302 	CleanupStack::PushL(TheWriter);
       
   303 	TheWriter->DeleteFilesL();
       
   304 	CleanupStack::PopAndDestroy(TheWriter);
       
   305 	}
       
   306 
       
   307 static void DoOOMTestL()
       
   308 	{
       
   309 	TInt error = KErrNoMemory;
       
   310 	TInt failAt = 0;
       
   311 	
       
   312 	while(error!=KErrNone)
       
   313 		{
       
   314 		failAt++;
       
   315 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   316 		__UHEAP_MARK;
       
   317 		TRAP(error, StartTestL());
       
   318 		if	(error != KErrNone)
       
   319 			{
       
   320 			__UHEAP_MARKEND;
       
   321 			}
       
   322 
       
   323 		__UHEAP_RESET;
       
   324 		TheTest(error == KErrNoMemory || error == KErrNone);
       
   325 		}
       
   326 	}
       
   327 
       
   328 static void doMainL()
       
   329 	{
       
   330 	TInt error = KErrNone;
       
   331 
       
   332 	CleanupStack::PushL(TCleanupItem(TidyFilesL, 0));
       
   333 
       
   334 	// Normal test
       
   335 	TRAP(error, StartTestL());
       
   336 	TheTest(error == KErrNone);
       
   337 
       
   338 	// OOM test
       
   339 	TRAP(error, DoOOMTestL());
       
   340 	TheTest(error == KErrNone);
       
   341 
       
   342 	CleanupStack::PopAndDestroy();
       
   343 	}
       
   344 
       
   345 /**
       
   346 @SYMTestCaseID PIM-TSTORE-0001
       
   347 */	
       
   348 GLDEF_C TInt E32Main()
       
   349     {
       
   350 	__UHEAP_MARK;
       
   351 	TheTest.Start(_L("@SYMTestCaseID PIM-TSTORE-0001 Creating cleanup objects"));
       
   352 
       
   353 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   354 	TheTest(TheFsSession.Connect() == KErrNone);
       
   355 
       
   356 	TRAPD(error, doMainL());
       
   357 	TheTest(error == KErrNone);
       
   358 
       
   359 	// Cleanup & close
       
   360 	delete cleanup;
       
   361 	TheFsSession.Close();
       
   362 
       
   363 	TheTest.End();
       
   364 	TheTest.Close();
       
   365 
       
   366 	__UHEAP_MARKEND;
       
   367 	return KErrNone;
       
   368     }