examples/SysLibs/CentRepExample/centrepexample.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: 
00028 This example program demonstrates the use of the Central Repository. 
00029 The code demonstrates how to open this repository, read its settings, 
00030 change them, perform read-write operations in a transaction, 
00031 find settings, restore default settings, request change notifications 
00032 and delete settings.
00033 The program requires that before it is run a data file representing
00034 a repository is present in the private\10202be9 directory on the
00035 emulator.
00036 The example defines an example repository in the E80000AD.txt file.
00037 This text file can be installed directly, but it is recommended
00038 that repository files are first converted to binary format using the 
00039 CentRepConv tool.
00040 To do this:  
00041 - Copy E80000AD.txt file to the epoc32\winscw\c location on your kit.
00042 - Start a command line prompt, cd to epoc32\RELEASE\WINSCW\UDEB, and run the
00043 command CentRepConv C:\E80000AD.txt 
00044 - This then displays that the file has been converted to the new binary file 
00045 E80000AD.cre. 
00046 - Copy this file to the epoc32\RELEASE\WINSCW\UDEB\Z\private\10202be9
00047 directory on your kit. 
00048 */
00049 
00050 
00051 
00052 
00053 
00057 #include "centrepexample.h"
00058 #include <e32cons.h>
00059 #include "asyncwaiter.h"
00060 
00061 _LIT(KTitle, "Central repository example");
00062 _LIT(KTextPressAKey, "\n\nPress any key to step through the example");
00063 _LIT(KExit,"\nPress any key to exit the application ");
00064 _LIT(KPressAKey,"\nPress any key to continue \n");
00065 _LIT(KErr,"\nThe repository file has not been set up. Please see the instructions in centrepexample.cpp for how to do this");
00066 
00067 _LIT(KOpen,"\n\nOpening the repository and displaying some initial settings");
00068 _LIT(KChangeSet,"\nChanging some settings in the repository");
00069 _LIT(KReadSet,"\nReading the changed settings");
00070 _LIT(KTransact,"\n\nPerforming read and write transactions");
00071 _LIT(KDelete,"\n\nCreating integer and real number settings and deleting them");
00072 _LIT(KMove,"\n\nMoving the key to target position");
00073 _LIT(KReset,"\nResetting settings to default values, and getting notifications that the changes have occurred");
00074 _LIT(KFind,"\nFinding settings for simple and structured data");
00075 _LIT(KNonExisting,"\n\nValue of new setting is %d");
00076 _LIT(KInt,"\nValue of setting with key x01 is %d");
00077 _LIT(KReal,"\nValue of setting with key x02 is %f");
00078 _LIT(KInteg,"\nValue of setting with key x06 is %d \n");
00079 _LIT(KIntBefore,"\nValue of setting with key x01 before reset is %d");
00080 _LIT(KIntAfter,"\nValue of setting with key x01 after reset is %d");
00081 _LIT(KIdsFound,"\nFound %d settings");
00082 _LIT(KStringName,"Another string");
00083 _LIT(KString1_UpdatedValue, "Value of setting with key x05 is %s");
00084 
00085 static const TUid KUidRepository = { 0xE80000AD };
00086 const TUint32 KNonExistentSetting = 0x10;
00087 const TUint32 KInt1 = 0x01;
00088 const TUint32 KInt3 = 0x06;
00089 const TUint32 KReal1 = 0x02;
00090 const TUint32 KString1 = 0x05;
00091 const TInt KInt1_InitialValue = 1;
00092 const TInt KInt1_UpdatedValue = 73;
00093 const TReal KReal1_InitialValue = 14.91;
00094 const TReal KReal1_UpdatedValue = 72.8;
00095 const TUint32 KMoveTarget = 0x30;
00096 
00102 CCentRepExample* CCentRepExample::NewLC()
00103         {
00104         CCentRepExample* rep = new(ELeave) CCentRepExample();
00105         CleanupStack::PushL(rep);
00106         rep->ConstructL();
00107         return rep;
00108         }
00109         
00113 CCentRepExample::CCentRepExample()
00114         {
00115         }       
00116 
00117 void CCentRepExample::ConstructL()
00118         {
00119         
00120         iConsole = Console::NewL(KTitle,TSize(KConsFullScreen,KConsFullScreen));
00121         iConsole->Printf ( KTextPressAKey );
00122         iConsole->Getch ();
00123         }
00124 
00128 CCentRepExample::~CCentRepExample()
00129         {
00130         iConsole->Printf(KExit);
00131         iConsole->Getch();
00132         
00133         delete iConsole;
00134         delete iRepository;
00135         }
00136 
00137 
00144 void CCentRepExample::ResetL()
00145         {
00146         CRepository* repository = NULL;
00147         TRAPD(err, repository = CRepository::NewL(KUidRepository););
00148         CleanupStack::PushL(repository);
00149         // test if an error has occurred because the repository
00150         // has not been set up: see instructions at the top
00151         // of this file if this occurs.
00152         if (err == KErrNotFound) 
00153                 {
00154                 iConsole->Printf(KErr); 
00155                 }
00156         User::LeaveIfError(err);
00157         User::LeaveIfError(err = repository->Reset());
00158         CleanupStack::PopAndDestroy(repository);
00159         }
00160         
00167 void CCentRepExample::OpenRepositoryL()
00168         {
00169         TInt i, k;
00170         TReal j;
00171         TBuf<50> tooShort;
00172         iConsole->Printf(KOpen);
00173         iRepository = CRepository::NewL(KUidRepository);
00174         
00175         User::LeaveIfError(iRepository->Get(KInt1, i));
00176         iConsole->Printf(KInt,i);
00177 
00178         User::LeaveIfError(iRepository->Get(KReal1, j));
00179         iConsole->Printf(KReal,j);
00180         
00181         User::LeaveIfError(iRepository->Get(KInt3, k));
00182         iConsole->Printf(KInteg,k);
00183         
00184         User::LeaveIfError(iRepository->Get(KString1, tooShort));
00185         iConsole->Printf(KString1_UpdatedValue,tooShort.PtrZ());
00186         
00187         iConsole->Printf(KPressAKey);
00188         iConsole->Getch();
00189                 
00190         }
00191         
00197 void CCentRepExample::ChangeSettingsL()
00198         {
00199         iConsole->Printf(KChangeSet);
00200         User::LeaveIfError(iRepository->Set(KNonExistentSetting, 10));
00201         
00202         User::LeaveIfError(iRepository->Set(KInt1, KInt1_UpdatedValue));
00203         
00204         User::LeaveIfError(iRepository->Set(KReal1, KReal1_InitialValue));
00205                 
00206         User::LeaveIfError(iRepository->Set(KInt3, KInt1_InitialValue));
00207 
00208         User::LeaveIfError(iRepository->Set(KString1, KStringName));
00209 
00210         }
00211         
00217 void CCentRepExample::ReadSettingsL()
00218         {
00219         TInt m, i, k;
00220         TReal j;
00221         TBuf<50> tooShort;
00222         iConsole->Printf(KReadSet);
00223         
00224         User::LeaveIfError(iRepository->Get(KNonExistentSetting, m));
00225 
00226         iConsole->Printf(KNonExisting,m);
00227         
00228         User::LeaveIfError(iRepository->Get(KInt1, i));
00229         
00230         iConsole->Printf(KInt,i);
00231 
00232         User::LeaveIfError(iRepository->Get(KReal1, j));
00233 
00234         iConsole->Printf(KReal,j);
00235         
00236         User::LeaveIfError(iRepository->Get(KInt3, k));
00237         
00238         iConsole->Printf(KInteg,k);
00239         
00240         User::LeaveIfError(iRepository->Get(KString1, tooShort));
00241         
00242         iConsole->Printf(KString1_UpdatedValue,tooShort.PtrZ());
00243         
00244         iConsole->Printf(KPressAKey);
00245         iConsole->Getch();
00246 
00247         }
00248         
00256 void CCentRepExample::FindSettingsL()
00257         {
00258         RArray<TUint32> foundIds;
00259 
00260         // Finds all the settings that exist and match the specification 
00261         //given by partialKey and mask. 
00262         iConsole->Printf(KFind);
00263         
00264         // These values will instruct Find to return the all settings keys which 
00265         //match the pattern 000001XXh where X indicates a ‘don’t care’ state.
00266         
00267         User::LeaveIfError(iRepository->FindL(0x100 /*partialKey*/, 0xF00/*mask*/, foundIds));
00268  
00269         iConsole->Printf(KIdsFound,foundIds.Count()); 
00270         foundIds.Reset();
00271 
00272         // Finds all the settings that contain a given integer and 
00273         //match the specification given by partialKey and mask. 
00274 
00275         User::LeaveIfError(iRepository->FindEqL(0x00/*partialKey*/, 0x00/*mask*/,KInt1_InitialValue /*integer value*/, foundIds));
00276         
00277         iConsole->Printf(KIdsFound,foundIds.Count()); 
00278         foundIds.Reset();
00279 
00280         // Finds all the settings that contain the given floating point value 
00281         //and match the specification given by partialKey and mask. 
00282         User::LeaveIfError(iRepository->FindEqL(0x00, 0x00,KReal1_InitialValue/*Real value*/, foundIds));
00283 
00284         iConsole->Printf(KIdsFound,foundIds.Count());
00285         foundIds.Reset();
00286 
00287         // Finds all the settings that contain a given string value 
00288         //and match the specification given by partialKey and mask. 
00289         
00290         User::LeaveIfError(iRepository->FindEqL(0x00, 0x00, KStringName/*string*/, foundIds));
00291 
00292         iConsole->Printf(KIdsFound,foundIds.Count()); 
00293         foundIds.Reset();
00294 
00295         // Finds all the settings that match the specification given 
00296         // by partialKey and mask, but are either not integer values or 
00297         //do not have the given value. 
00298         
00299         User::LeaveIfError(iRepository->FindNeqL(0x00/*partial key*/, 0x00/*mask*/, KInt1_UpdatedValue, foundIds));
00300         
00301         iConsole->Printf(KIdsFound,foundIds.Count()); 
00302         foundIds.Reset();
00303 
00304         // Finds all the settings that match the specification given by 
00305         //partialKey and mask, but are either not floating point values 
00306         //or do not have the given value.
00307         
00308         User::LeaveIfError(iRepository->FindNeqL(0x100, 0x0F0, KReal1_UpdatedValue, foundIds));
00309         
00310         iConsole->Printf(KIdsFound,foundIds.Count()); 
00311         foundIds.Reset();
00312         
00313         }
00319 void CCentRepExample::ResetAndNotifyL()
00320         {
00321         TInt x;
00322         
00323         // Ensure KInt1 is set to a different value to its initial value
00324         // First change to setting should cause notification
00325         iConsole->Printf(KReset);
00326         
00327         User::LeaveIfError(iRepository->Set(KInt1, KInt1_InitialValue+10));
00328 
00329         
00330         User::LeaveIfError(iRepository->Get(KInt1, x));
00331         
00332         iConsole->Printf(KIntBefore, x);
00333         
00334         CAsyncWaiter* waiter = CAsyncWaiter::NewL();
00335         CleanupStack::PushL(waiter);
00336         
00337         User::LeaveIfError(iRepository->NotifyRequest(KInt1, waiter->iStatus));
00338         
00339         // Get a notification on a reset as well
00340         
00341         User::LeaveIfError(iRepository->Reset(KInt1));
00342         
00343         // Check we got a notification
00344         waiter->StartAndWait();
00345         User::LeaveIfError(waiter->Result());
00346         
00347         // Check KInt1 now has the right value
00348         User::LeaveIfError(iRepository->Get(KInt1, x));
00349         iConsole->Printf(KIntAfter, x);
00350 
00351         CleanupStack::PopAndDestroy(waiter);
00352         }
00353         
00358 void CCentRepExample::MoveSettingsL()
00359         {
00360         TUint32 keyInfo;
00361                 
00362         iConsole->Printf(KMove);
00363         iConsole->Printf(KPressAKey);
00364         iConsole->Getch();
00365         // Move the key to the target position
00366         User::LeaveIfError(iRepository->Move(KInt3, KMoveTarget, 0xFFFFFFFF,
00367                         keyInfo));
00368         }       
00369         
00374 void CCentRepExample::TransactionFuncL()
00375         {
00376         TUint32 keyId;
00377         TInt intVal;
00378         
00379         iConsole->Printf(KTransact);
00380         
00381         // Attempts to start a read write transaction.
00382         User::LeaveIfError(iRepository->StartTransaction(CRepository::EReadWriteTransaction));
00383         
00384         // Calls FailTransaction if activated by a Leave or PopAndDestroy.
00385         iRepository->CleanupCancelTransactionPushL();
00386 
00387         // Creating the variable KNewInt at 0x16
00388         const TUint32 KNewInt = 0x16;
00389         const TInt KIntValue = 1201;
00390         intVal = KIntValue +33;
00391         
00392         // Perform some write operations.
00393         // Creating KNewInt with the value KIntValue.
00394         User::LeaveIfError(iRepository->Create(KNewInt, KIntValue));
00395         // Setting KNewInt to the new value
00396         User::LeaveIfError(iRepository->Set(KNewInt,intVal));
00397         
00398         // Persistence of all values read and written during the transaction 
00399         // is only guaranteed after a successful return from CommitTransaction.
00400         User::LeaveIfError(iRepository->CommitTransaction(keyId));
00401         
00402         // Read the data written.
00403         User::LeaveIfError(iRepository->Get(KNewInt, intVal));
00404         
00405         // Deleting the data after reading it.
00406         User::LeaveIfError(iRepository->Delete(KNewInt));
00407         CleanupStack::Pop(); 
00408 
00409         CRepository* repository1;
00410         User::LeaveIfNull(repository1 = CRepository::NewLC(KUidRepository));
00411         
00412         // Begin read transaction for repository1
00413         User::LeaveIfError(repository1->StartTransaction(CRepository::EReadTransaction));
00414         
00415         CRepository* repository2;
00416         User::LeaveIfNull(repository2 = CRepository::NewLC(KUidRepository));
00417         
00418         // Should be able to start another read transaction (Multiple read transactions)
00419         User::LeaveIfError(repository2->StartTransaction(CRepository::EReadTransaction));
00420         
00421         // Perform some gets using the open transactions and repositories
00422         // Read operation
00423         User::LeaveIfError(repository1->Get(KInt1, intVal));
00424         // Ensure transaction cancelled if following code Leaves:
00425     repository1->CleanupCancelTransactionPushL();
00426     
00427     // Commit the transaction 
00428     User::LeaveIfError(repository1->CommitTransaction(keyId));
00429     CleanupStack::Pop(); 
00430         
00431         // Calls FailTransaction if activated by a Leave or PopAndDestroy.
00432         repository2->CleanupCancelTransactionPushL();
00433         User::LeaveIfError(repository2->Get(KInt1, intVal));
00434         User::LeaveIfError(repository2->CommitTransaction(keyId));
00435         
00436         // Pop the transaction
00437         CleanupStack::Pop();
00438         CleanupStack::PopAndDestroy(2,repository1);
00439         
00440 
00441         }
00442         
00447 void CCentRepExample::DeleteL()
00448         {
00449         TInt x;
00450         TReal y;
00451         
00452         iConsole->Printf(KDelete);
00453                         
00454         User::LeaveIfError(iRepository->Get(KInt1_InitialValue, x));
00455         User::LeaveIfError(iRepository->Get(KReal1, y));
00456         User::LeaveIfError(iRepository->Delete(KInt1_InitialValue));
00457         User::LeaveIfError(iRepository->Delete(KMoveTarget));
00458         User::LeaveIfError(iRepository->Delete(KReal1));
00459         }
00460                                 
00461 LOCAL_C void MainL()
00462         {
00463         // Create an Active Scheduler to handle asychronous calls
00464         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
00465         CleanupStack::PushL(scheduler);
00466         CActiveScheduler::Install( scheduler );
00467         CCentRepExample * app = CCentRepExample ::NewLC();
00468 
00469         // Reset the repository
00470         app->ResetL();
00471         
00472         // Open the repository
00473         app->OpenRepositoryL();
00474         
00475         // Change the existing settings
00476         app->ChangeSettingsL();
00477         
00478         // Read the changed settings
00479         app->ReadSettingsL();
00480         
00481         // Find the settings using FindL, FindEqL and FindNeqL
00482         app->FindSettingsL();
00483         
00484         // Move settings
00485         app->MoveSettingsL();
00486         
00487         // Reset settings to default values and get notification
00488         app->ResetAndNotifyL();
00489 
00490         // Perform multiple operations in a transaction
00491         app->TransactionFuncL();
00492         
00493         // Delete settings
00494         app->DeleteL();
00495         
00496         CleanupStack::PopAndDestroy(2); //app, scheduler
00497 
00498         }
00499 
00500 GLDEF_C TInt E32Main()
00501         {
00502     __UHEAP_MARK;
00503     CTrapCleanup* cleanup = CTrapCleanup::New();
00504     if(cleanup == NULL)
00505         {
00506         return KErrNoMemory;
00507         }
00508     TRAPD(err, MainL());
00509         if(err != KErrNone)
00510                 {
00511                 User::Panic(_L("Failed to complete"),err);
00512                 }
00513 
00514     delete cleanup;
00515     __UHEAP_MARKEND;
00516     return KErrNone;
00517         }

Generated by  doxygen 1.6.2