×
Namespaces

Variants
Actions

Saving settings with Dictionary store

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: symbianyucca (23 Mar 2007)
Last edited: hamishwillee (12 Jan 2012)

Dictionary store nearly perfect solution for storing settings data, in general it is simpler to use than other stores, with it you can set your own UID for each stream in the store, thus you can find them easily when reading the setting back.

Following functions are illustrating how to store and load settings items from the store. With Dictionary stores there is small problem which causes the store file size to increase over time, as shown in the SaveValuesL()-function with this example this problem is fixed by deleting the store each time any changes are done.

Streams inside the Dictionary store are accessed by using Dictionary store read/write streams. For internalizing and externalizing you could use << and >> operator, as illustrated with the iName variable, for integers you should use the functions provided by read/write stream as shown with iIndex variable. There are separate functions for 8,16,32 and 64 bit wide signed/unsigned integers as well as for byte buffers.

As illustrated with the iBitmap variable you can also use InternalizeL/ExternalizeL functions provided by the class itself for reading and writing to the Dictionary store.

Note that when opening Dictionary store, you need to know the UID of the store (in example 0xDEADBEEF is used as store UID), using wrong value will cause a leave.

_LIT(KtxDicFileName			,":\\private\\<My SID>\\MyDicFile.ini" );
 
const TInt KIndexUID = 0x1000;
const TInt KImageUID = 0x2000;
const TInt KNameUID = 0x3000;
 
 
class CExampleItem : public CBase
{
public:
CExampleItem(): iIndex(-1){};
~CExampleItem(){ delete iBitmap;};
public:
TInt iIndex;
TBuf<100> iName;
CWsBitmap* iBitmap
};
 
 
 
void LoadValuesL(CExampleItem& aItem)
{
TFindFile AufFolder(CCoeEnv::Static()->FsSession());
if(KErrNone == AufFolder.FindByDir(KtxDicFileName, KNullDesC))
{
CDictionaryFileStore* MyDStore = CDictionaryFileStore::OpenLC(CCoeEnv::Static()->FsSession(),AufFolder.File(), TUid::Uid(0xDEADBEEF));
 
TUid FileUid = {0x0};
FileUid.iUid = KIndexUID;
 
if(aDStore->IsPresentL(FileUid))
{
RDictionaryReadStream in;
in.OpenLC(*aDStore,FileUid);
aItem.iIndex = in.ReadInt32L();
CleanupStack::PopAndDestroy(1);// in
}
 
FileUid.iUid = KNameUID;
if(aDStore->IsPresentL(FileUid))
{
RDictionaryReadStream in;
in.OpenLC(*aDStore,FileUid);
in >> aItem.iName;
CleanupStack::PopAndDestroy(1);// in
}
 
FileUid.iUid = KImageUID;
if(aDStore->IsPresentL(FileUid))
{
aItem.iBitmap = new(ELeave)CWsBitmap(CCoeEnv::Static()->WsSession());
 
RDictionaryReadStream in;
in.OpenLC(*aDStore,FileUid);
aItem.iBitmap->InternalizeL(in);
CleanupStack::PopAndDestroy(1);// in
}
 
CleanupStack::PopAndDestroy(1);// Store
}
}
 
 
void SaveValuesL(CExampleItem& aItem)
{
TFindFile AufFolder(CCoeEnv::Static()->FsSession());
if(KErrNone == AufFolder.FindByDir(KtxDicFileName, KNullDesC))
{
User::LeaveIfError(CCoeEnv::Static()->FsSession().Delete(AufFolder.File()));
 
CDictionaryFileStore* MyDStore = CDictionaryFileStore::OpenLC(CCoeEnv::Static()->FsSession(),AufFolder.File(), TUid::Uid(0xDEADBEEF));
 
TUid FileUid = {0x0};
FileUid.iUid = KIndexUID;
 
RDictionaryWriteStream out1;
out1.AssignLC(*MyDStore,FileUid);
out1.WriteInt32L(aItem.iIndex);
out1.CommitL();
CleanupStack::PopAndDestroy(1);// out1
 
FileUid.iUid = KNameUID;
RDictionaryWriteStream out2;
out2.AssignLC(*MyDStore,FileUid);
out2 << aItem.iName;
out2.CommitL();
CleanupStack::PopAndDestroy(1);// out2
 
FileUid.iUid = KImageUID;
RDictionaryWriteStream out3;
out3.AssignLC(*MyDStore,FileUid);
aItem.iBitmap->ExternalizeL(out3);
out3.CommitL();
CleanupStack::PopAndDestroy(1);// out3
 
MyDStore->CommitL();
CleanupStack::PopAndDestroy(1);// Store
}
}


Other Related Links

Using .ini Files Insert non-formatted text here

This page was last modified on 12 January 2012, at 03:28.
38 page views in the last 30 days.
×