Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 19:00, 10 October 2008.

CS000967 - Storing application settings

From Forum Nokia Wiki



ID CS000967 Creation date May 20, 2008
Platform S60 3rd Edition, MR Tested on devices Nokia N95
Category Symbian C++ Subcategory Files/Data


Keywords (APIs, classes, methods, functions):

Overview

The following example shows how to handle application settings. Settings are stored into the private folder of the application.

MMP file

The following capabilities and libraries are required:

CAPABILITY      NONE
LIBRARY         efsrv.lib
LIBRARY		estor.lib

Header file, CSettingsData.h

Settings data class for storing/reading settings data from the stream.

#include <e32base.h>
class CSettingsData : public CBase
{
public:
    static CSettingsData* NewL();
    static CSettingsData* NewLC();
    virtual ~CSettingsData();
 
    // Loads itself from stream.
    void LoadL(RReadStream& aStream);
    
    // Saves itself to stream.
    void SaveL(RWriteStream& aStream) const;
 
    // Using default values (e.g. in first time)
    void SetDefaultValues();
 
    inline TBuf<100>& Text() { return iText; }
    inline TInt& Enumeration() { return iEnumeration; }
 
private:
    CSettingsData();
    void ConstructL();
 
public:
    TBuf<100> iText; //Data for text field
    TInt iEnumeration; // Data for enumerated text field

Source file, CSettingsData.cpp

CSettingsData *CSettingsData::NewL()
    {
    CSettingsData *self = CSettingsData::NewLC();
    CleanupStack::Pop(self);
    return self;
    }
 
CSettingsData *CSettingsData::NewLC()
    {
    CSettingsData *self = new (ELeave) CSettingsData();
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }
 
CSettingsData::~CSettingsData()
    {
    }
 
CSettingsData::CSettingsData()
    {
    }
 
void CSettingsData::SetDefaultValues()
    {
    // TODO: Set default values of your setting
    iEnumeration = 1000;
    }
 
void CSettingsData::ConstructL() 
    {
    }
 
// Reading setting data from stream
void CSettingsData::LoadL(RReadStream& aStream)
    {
    aStream >> iText;
    iEnumeration = aStream.ReadInt32L();
    }
 
// Storing setting data into stream    
void CSettingsData::SaveL(RWriteStream& aStream) const
    {
    aStream << iText;
    aStream.WriteInt32L(iEnumeration);
    }

Header file, CMyAppUi.h

Application class that reads settings on construction and stores them on exit.

#include <aknviewappui.h>
#include "CSettingsData.h"
 
class CMyAppUi : public CAknViewAppUi
    {
public:
    void ConstructL();
    CSettingExampleAppUi();
    ~CSettingExampleAppUi();
    
public: // from CEikAppUi
    void HandleCommandL(TInt aCommand);
    
private:
    void InternalizeSettingsDataL();
    void ExternalizeSettingsDataL() const;
    
private:
    CSettingsData* iData;   // Pointer to settings data
    TFileName iSettingsFile;
    };

Source file, CMyAppUi.cpp

void CMyAppUi::ConstructL()
    {
    BaseConstructL(EAknEnableSkin);
 
    // Create private path
    RFs& fsSession = iEikonEnv->FsSession();
    User::LeaveIfError(fsSession.CreatePrivatePath( EDriveC ) );
    User::LeaveIfError(fsSession.PrivatePath(iSettingsFile));
    iSettingsFile += KSettingsFile;
 
    // Construct the data object the settings list will use
    iData = CSettingsData::NewL();
    // Read settings from stream
    InternalizeSettingsDataL();
 
    // TODO: Construct and activate the app view as well
    }
 
// Handle menu commands
void CMyAppUi::HandleCommandL(TInt aCommand)
    {
    switch(aCommand)
        {
        case EEikCmdExit:
        case EAknSoftkeyExit:
            {
            // Store setting data on application exit
            ExternalizeSettingsDataL();
            Exit();
            break;
            }
        default:
            break;
        }
    }
 
// Load settings    
void CMyAppUi::InternalizeSettingsDataL()
    {
    RFs& fs = iEikonEnv->FsSession();
    RFileReadStream readStream;
    TInt error = readStream.Open(fs, iSettingsFile, EFileRead);
    TInt internalizationError = KErrNone;
    // If file existed, try to read settings.
    if (error == KErrNone)
        {
        TRAP(internalizationError, iData->LoadL(readStream);)
        }
    else
        {
        // Use default values in first time when no 
        // setting file exists
        iData->SetDefaultValues();
        }
    readStream.Release();
 
    // Reading failed, settings file might be corrupted.    
    if (internalizationError != KErrNone)
        {
        User::LeaveIfError(fs.Delete(iSettingsFile));
        }
    }
 
// Save settings
void CMyAppUi::ExternalizeSettingsDataL() const
    {
    RFs& fs = iEikonEnv->FsSession();
    RFileWriteStream writeStream;
    TInt error = writeStream.Open(fs, iSettingsFile, EFileWrite);
    // Setting file did not exist, create one.
    if (error != KErrNone)
        {
        User::LeaveIfError(writeStream.Create(fs, iSettingsFile, EFileWrite));
        }
    writeStream.PushL();
    iData->SaveL(writeStream);
    writeStream.CommitL();
    writeStream.Pop();
    writeStream.Release();
    }

Streaming

Some examples of data streaming (reading):

// String
    TBuf<100> iText;
    aStream >> iText;
 
    // Integer
    TInt iVolume;
    iVolume = aStream.ReadInt32L();
 
    // Boolean
    TBool iBinary;
    iBinary = aStream.ReadInt16L();
 
    // Ip address
    TInetAddr iIpAddress;
    iIpAddress.SetAddress(aStream.ReadUint32L());
 
    // Time    
    TTime iTime;
    TInt64 time;
    aStream >> time;
    iTime = TTime(time);

Postconditions

Application loads settings on startup and stores them on exit.

Related Discussions
Thread Thread Starter Forum Replies Last Post
S60 platform desijatt Series 40 & S60 Platform Feedback 7 2007-02-02 18:32
protecting personal GPRS connection settings geran Symbian Networking & Messaging 0 2002-11-22 10:05
Setup Default Access Point for j2me 2.0 midlet scheung3 Mobile Java General 3 2004-11-25 03:09
Creating connection to iap and opening browser ollipekkam Symbian Networking & Messaging 1 2007-07-04 15:07
Change SMS and MMS settings timatima General Symbian C++ 3 2006-03-17 13:35
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX