00001 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // This example program demonstrates the use of the Central Repository. 00015 // The code demonstrates how to open this repository, read its settings, 00016 // change them, perform read-write operations in a transaction, 00017 // find settings, restore default settings, request change notifications 00018 // and delete settings. 00019 // The program requires that before it is run a data file representing 00020 // a repository is present in the private\10202be9 directory on the 00021 // emulator. 00022 // The example defines an example repository in the E80000AD.txt file. 00023 // This text file can be installed directly, but it is recommended 00024 // that repository files are first converted to binary format using the 00025 // CentRepConv tool. 00026 // To do this: 00027 // - Copy E80000AD.txt file to the epoc32\winscw\c location on your kit. 00028 // - Start a command line prompt, cd to epoc32\RELEASE\WINSCW\UDEB, and run the 00029 // command CentRepConv C:\E80000AD.txt 00030 // - This then displays that the file has been converted to the new binary file 00031 // E80000AD.cre. 00032 // - Copy this file to the epoc32\RELEASE\WINSCW\UDEB\Z\private\10202be9 00033 // directory on your kit. 00034 // 00035 00036 00037 00041 #include "centrepexample.h" 00042 #include <e32cons.h> 00043 #include "asyncwaiter.h" 00044 00045 _LIT(KTitle, "Central repository example"); 00046 _LIT(KTextPressAKey, "\n\nPress any key to step through the example"); 00047 _LIT(KExit,"\nPress any key to exit the application "); 00048 _LIT(KPressAKey,"\nPress any key to continue \n"); 00049 _LIT(KErr,"\nThe repository file has not been set up. Please see the instructions in centrepexample.cpp for how to do this"); 00050 00051 _LIT(KOpen,"\n\nOpening the repository and displaying some initial settings"); 00052 _LIT(KChangeSet,"\nChanging some settings in the repository"); 00053 _LIT(KReadSet,"\nReading the changed settings"); 00054 _LIT(KTransact,"\n\nPerforming read and write transactions"); 00055 _LIT(KDelete,"\n\nCreating integer and real number settings and deleting them"); 00056 _LIT(KMove,"\n\nMoving the key to target position"); 00057 _LIT(KReset,"\nResetting settings to default values, and getting notifications that the changes have occurred"); 00058 _LIT(KFind,"\nFinding settings for simple and structured data"); 00059 _LIT(KNonExisting,"\n\nValue of new setting is %d"); 00060 _LIT(KInt,"\nValue of setting with key x01 is %d"); 00061 _LIT(KReal,"\nValue of setting with key x02 is %f"); 00062 _LIT(KInteg,"\nValue of setting with key x06 is %d \n"); 00063 _LIT(KIntBefore,"\nValue of setting with key x01 before reset is %d"); 00064 _LIT(KIntAfter,"\nValue of setting with key x01 after reset is %d"); 00065 _LIT(KIdsFound,"\nFound %d settings"); 00066 _LIT(KStringName,"Another string"); 00067 _LIT(KString1_UpdatedValue, "Value of setting with key x05 is %s"); 00068 00069 static const TUid KUidRepository = { 0xE80000AD }; 00070 const TUint32 KNonExistentSetting = 0x10; 00071 const TUint32 KInt1 = 0x01; 00072 const TUint32 KInt3 = 0x06; 00073 const TUint32 KReal1 = 0x02; 00074 const TUint32 KString1 = 0x05; 00075 const TInt KInt1_InitialValue = 1; 00076 const TInt KInt1_UpdatedValue = 73; 00077 const TReal KReal1_InitialValue = 14.91; 00078 const TReal KReal1_UpdatedValue = 72.8; 00079 const TUint32 KMoveTarget = 0x30; 00080 00086 CCentRepExample* CCentRepExample::NewLC() 00087 { 00088 CCentRepExample* rep = new(ELeave) CCentRepExample(); 00089 CleanupStack::PushL(rep); 00090 rep->ConstructL(); 00091 return rep; 00092 } 00093 00097 CCentRepExample::CCentRepExample() 00098 { 00099 } 00100 00101 void CCentRepExample::ConstructL() 00102 { 00103 00104 iConsole = Console::NewL(KTitle,TSize(KConsFullScreen,KConsFullScreen)); 00105 iConsole->Printf ( KTextPressAKey ); 00106 iConsole->Getch (); 00107 } 00108 00112 CCentRepExample::~CCentRepExample() 00113 { 00114 iConsole->Printf(KExit); 00115 iConsole->Getch(); 00116 00117 delete iConsole; 00118 delete iRepository; 00119 } 00120 00121 00128 void CCentRepExample::ResetL() 00129 { 00130 CRepository* repository = NULL; 00131 TRAPD(err, repository = CRepository::NewL(KUidRepository);); 00132 CleanupStack::PushL(repository); 00133 // test if an error has occurred because the repository 00134 // has not been set up: see instructions at the top 00135 // of this file if this occurs. 00136 if (err == KErrNotFound) 00137 { 00138 iConsole->Printf(KErr); 00139 } 00140 User::LeaveIfError(err); 00141 User::LeaveIfError(err = repository->Reset()); 00142 CleanupStack::PopAndDestroy(repository); 00143 } 00144 00151 void CCentRepExample::OpenRepositoryL() 00152 { 00153 TInt i, k; 00154 TReal j; 00155 TBuf<50> tooShort; 00156 iConsole->Printf(KOpen); 00157 iRepository = CRepository::NewL(KUidRepository); 00158 00159 User::LeaveIfError(iRepository->Get(KInt1, i)); 00160 iConsole->Printf(KInt,i); 00161 00162 User::LeaveIfError(iRepository->Get(KReal1, j)); 00163 iConsole->Printf(KReal,j); 00164 00165 User::LeaveIfError(iRepository->Get(KInt3, k)); 00166 iConsole->Printf(KInteg,k); 00167 00168 User::LeaveIfError(iRepository->Get(KString1, tooShort)); 00169 iConsole->Printf(KString1_UpdatedValue,tooShort.PtrZ()); 00170 00171 iConsole->Printf(KPressAKey); 00172 iConsole->Getch(); 00173 00174 } 00175 00181 void CCentRepExample::ChangeSettingsL() 00182 { 00183 iConsole->Printf(KChangeSet); 00184 User::LeaveIfError(iRepository->Set(KNonExistentSetting, 10)); 00185 00186 User::LeaveIfError(iRepository->Set(KInt1, KInt1_UpdatedValue)); 00187 00188 User::LeaveIfError(iRepository->Set(KReal1, KReal1_InitialValue)); 00189 00190 User::LeaveIfError(iRepository->Set(KInt3, KInt1_InitialValue)); 00191 00192 User::LeaveIfError(iRepository->Set(KString1, KStringName)); 00193 00194 } 00195 00201 void CCentRepExample::ReadSettingsL() 00202 { 00203 TInt m, i, k; 00204 TReal j; 00205 TBuf<50> tooShort; 00206 iConsole->Printf(KReadSet); 00207 00208 User::LeaveIfError(iRepository->Get(KNonExistentSetting, m)); 00209 00210 iConsole->Printf(KNonExisting,m); 00211 00212 User::LeaveIfError(iRepository->Get(KInt1, i)); 00213 00214 iConsole->Printf(KInt,i); 00215 00216 User::LeaveIfError(iRepository->Get(KReal1, j)); 00217 00218 iConsole->Printf(KReal,j); 00219 00220 User::LeaveIfError(iRepository->Get(KInt3, k)); 00221 00222 iConsole->Printf(KInteg,k); 00223 00224 User::LeaveIfError(iRepository->Get(KString1, tooShort)); 00225 00226 iConsole->Printf(KString1_UpdatedValue,tooShort.PtrZ()); 00227 00228 iConsole->Printf(KPressAKey); 00229 iConsole->Getch(); 00230 00231 } 00232 00240 void CCentRepExample::FindSettingsL() 00241 { 00242 RArray<TUint32> foundIds; 00243 00244 // Finds all the settings that exist and match the specification 00245 //given by partialKey and mask. 00246 iConsole->Printf(KFind); 00247 00248 // These values will instruct Find to return the all settings keys which 00249 //match the pattern 000001XXh where X indicates a ‘don’t care’ state. 00250 00251 User::LeaveIfError(iRepository->FindL(0x100 /*partialKey*/, 0xF00/*mask*/, foundIds)); 00252 00253 iConsole->Printf(KIdsFound,foundIds.Count()); 00254 foundIds.Reset(); 00255 00256 // Finds all the settings that contain a given integer and 00257 //match the specification given by partialKey and mask. 00258 00259 User::LeaveIfError(iRepository->FindEqL(0x00/*partialKey*/, 0x00/*mask*/,KInt1_InitialValue /*integer value*/, foundIds)); 00260 00261 iConsole->Printf(KIdsFound,foundIds.Count()); 00262 foundIds.Reset(); 00263 00264 // Finds all the settings that contain the given floating point value 00265 //and match the specification given by partialKey and mask. 00266 User::LeaveIfError(iRepository->FindEqL(0x00, 0x00,KReal1_InitialValue/*Real value*/, foundIds)); 00267 00268 iConsole->Printf(KIdsFound,foundIds.Count()); 00269 foundIds.Reset(); 00270 00271 // Finds all the settings that contain a given string value 00272 //and match the specification given by partialKey and mask. 00273 00274 User::LeaveIfError(iRepository->FindEqL(0x00, 0x00, KStringName/*string*/, foundIds)); 00275 00276 iConsole->Printf(KIdsFound,foundIds.Count()); 00277 foundIds.Reset(); 00278 00279 // Finds all the settings that match the specification given 00280 // by partialKey and mask, but are either not integer values or 00281 //do not have the given value. 00282 00283 User::LeaveIfError(iRepository->FindNeqL(0x00/*partial key*/, 0x00/*mask*/, KInt1_UpdatedValue, foundIds)); 00284 00285 iConsole->Printf(KIdsFound,foundIds.Count()); 00286 foundIds.Reset(); 00287 00288 // Finds all the settings that match the specification given by 00289 //partialKey and mask, but are either not floating point values 00290 //or do not have the given value. 00291 00292 User::LeaveIfError(iRepository->FindNeqL(0x100, 0x0F0, KReal1_UpdatedValue, foundIds)); 00293 00294 iConsole->Printf(KIdsFound,foundIds.Count()); 00295 foundIds.Reset(); 00296 00297 } 00303 void CCentRepExample::ResetAndNotifyL() 00304 { 00305 TInt x; 00306 00307 // Ensure KInt1 is set to a different value to its initial value 00308 // First change to setting should cause notification 00309 iConsole->Printf(KReset); 00310 00311 User::LeaveIfError(iRepository->Set(KInt1, KInt1_InitialValue+10)); 00312 00313 00314 User::LeaveIfError(iRepository->Get(KInt1, x)); 00315 00316 iConsole->Printf(KIntBefore, x); 00317 00318 CAsyncWaiter* waiter = CAsyncWaiter::NewL(); 00319 CleanupStack::PushL(waiter); 00320 00321 User::LeaveIfError(iRepository->NotifyRequest(KInt1, waiter->iStatus)); 00322 00323 // Get a notification on a reset as well 00324 00325 User::LeaveIfError(iRepository->Reset(KInt1)); 00326 00327 // Check we got a notification 00328 waiter->StartAndWait(); 00329 User::LeaveIfError(waiter->Result()); 00330 00331 // Check KInt1 now has the right value 00332 User::LeaveIfError(iRepository->Get(KInt1, x)); 00333 iConsole->Printf(KIntAfter, x); 00334 00335 CleanupStack::PopAndDestroy(waiter); 00336 } 00337 00342 void CCentRepExample::MoveSettingsL() 00343 { 00344 TUint32 keyInfo; 00345 00346 iConsole->Printf(KMove); 00347 iConsole->Printf(KPressAKey); 00348 iConsole->Getch(); 00349 // Move the key to the target position 00350 User::LeaveIfError(iRepository->Move(KInt3, KMoveTarget, 0xFFFFFFFF, 00351 keyInfo)); 00352 } 00353 00358 void CCentRepExample::TransactionFuncL() 00359 { 00360 TUint32 keyId; 00361 TInt intVal; 00362 00363 iConsole->Printf(KTransact); 00364 00365 // Attempts to start a read write transaction. 00366 User::LeaveIfError(iRepository->StartTransaction(CRepository::EReadWriteTransaction)); 00367 00368 // Calls FailTransaction if activated by a Leave or PopAndDestroy. 00369 iRepository->CleanupCancelTransactionPushL(); 00370 00371 // Creating the variable KNewInt at 0x16 00372 const TUint32 KNewInt = 0x16; 00373 const TInt KIntValue = 1201; 00374 intVal = KIntValue +33; 00375 00376 // Perform some write operations. 00377 // Creating KNewInt with the value KIntValue. 00378 User::LeaveIfError(iRepository->Create(KNewInt, KIntValue)); 00379 // Setting KNewInt to the new value 00380 User::LeaveIfError(iRepository->Set(KNewInt,intVal)); 00381 00382 // Persistence of all values read and written during the transaction 00383 // is only guaranteed after a successful return from CommitTransaction. 00384 User::LeaveIfError(iRepository->CommitTransaction(keyId)); 00385 00386 // Read the data written. 00387 User::LeaveIfError(iRepository->Get(KNewInt, intVal)); 00388 00389 // Deleting the data after reading it. 00390 User::LeaveIfError(iRepository->Delete(KNewInt)); 00391 CleanupStack::Pop(); 00392 00393 CRepository* repository1; 00394 User::LeaveIfNull(repository1 = CRepository::NewLC(KUidRepository)); 00395 00396 // Begin read transaction for repository1 00397 User::LeaveIfError(repository1->StartTransaction(CRepository::EReadTransaction)); 00398 00399 CRepository* repository2; 00400 User::LeaveIfNull(repository2 = CRepository::NewLC(KUidRepository)); 00401 00402 // Should be able to start another read transaction (Multiple read transactions) 00403 User::LeaveIfError(repository2->StartTransaction(CRepository::EReadTransaction)); 00404 00405 // Perform some gets using the open transactions and repositories 00406 // Read operation 00407 User::LeaveIfError(repository1->Get(KInt1, intVal)); 00408 // Ensure transaction cancelled if following code Leaves: 00409 repository1->CleanupCancelTransactionPushL(); 00410 00411 // Commit the transaction 00412 User::LeaveIfError(repository1->CommitTransaction(keyId)); 00413 CleanupStack::Pop(); 00414 00415 // Calls FailTransaction if activated by a Leave or PopAndDestroy. 00416 repository2->CleanupCancelTransactionPushL(); 00417 User::LeaveIfError(repository2->Get(KInt1, intVal)); 00418 User::LeaveIfError(repository2->CommitTransaction(keyId)); 00419 00420 // Pop the transaction 00421 CleanupStack::Pop(); 00422 CleanupStack::PopAndDestroy(2,repository1); 00423 00424 00425 } 00426 00431 void CCentRepExample::DeleteL() 00432 { 00433 TInt x; 00434 TReal y; 00435 00436 iConsole->Printf(KDelete); 00437 00438 User::LeaveIfError(iRepository->Get(KInt1_InitialValue, x)); 00439 User::LeaveIfError(iRepository->Get(KReal1, y)); 00440 User::LeaveIfError(iRepository->Delete(KInt1_InitialValue)); 00441 User::LeaveIfError(iRepository->Delete(KMoveTarget)); 00442 User::LeaveIfError(iRepository->Delete(KReal1)); 00443 } 00444 00445 LOCAL_C void MainL() 00446 { 00447 // Create an Active Scheduler to handle asychronous calls 00448 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler; 00449 CleanupStack::PushL(scheduler); 00450 CActiveScheduler::Install( scheduler ); 00451 CCentRepExample * app = CCentRepExample ::NewLC(); 00452 00453 // Reset the repository 00454 app->ResetL(); 00455 00456 // Open the repository 00457 app->OpenRepositoryL(); 00458 00459 // Change the existing settings 00460 app->ChangeSettingsL(); 00461 00462 // Read the changed settings 00463 app->ReadSettingsL(); 00464 00465 // Find the settings using FindL, FindEqL and FindNeqL 00466 app->FindSettingsL(); 00467 00468 // Move settings 00469 app->MoveSettingsL(); 00470 00471 // Reset settings to default values and get notification 00472 app->ResetAndNotifyL(); 00473 00474 // Perform multiple operations in a transaction 00475 app->TransactionFuncL(); 00476 00477 // Delete settings 00478 app->DeleteL(); 00479 00480 CleanupStack::PopAndDestroy(2); //app, scheduler 00481 00482 } 00483 00484 GLDEF_C TInt E32Main() 00485 { 00486 __UHEAP_MARK; 00487 CTrapCleanup* cleanup = CTrapCleanup::New(); 00488 if(cleanup == NULL) 00489 { 00490 return KErrNoMemory; 00491 } 00492 TRAPD(err, MainL()); 00493 if(err != KErrNone) 00494 { 00495 User::Panic(_L("Failed to complete"),err); 00496 } 00497 00498 delete cleanup; 00499 __UHEAP_MARKEND; 00500 return KErrNone; 00501 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.