libraries/ltkutils/inc/settings.h
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // settings.h
       
     2 // 
       
     3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #ifndef __SETTINGSEDITOR_H__
       
    14 #define __SETTINGSEDITOR_H__
       
    15 
       
    16 #include <e32base.h>
       
    17 #include <e32hashtab.h>
       
    18 #include <fshell/ioutils.h>
       
    19 
       
    20 namespace LtkUtils
       
    21 	{
       
    22 	
       
    23 	_LIT(KSettingPanic, "FSHELL_settings");
       
    24 	enum TSettingPanic
       
    25 		{
       
    26 		EInvalidType,
       
    27 		ENotDefined,
       
    28 		};
       
    29 
       
    30 /**
       
    31 Class for encapsulating the context of an error within a text file that is being processed.
       
    32 
       
    33 Note, the valid lifetime of this class is tied to the lifetime of the decriptor it refers to.
       
    34 Often, this is a descriptor on the stack and so copies of this class must not be kept.
       
    35 */
       
    36 class TErrorContext
       
    37 	{
       
    38 public:
       
    39 	IMPORT_C TErrorContext();
       
    40 	IMPORT_C TErrorContext(const TDesC& aFilename);
       
    41 	IMPORT_C void NextLine();
       
    42 	IMPORT_C TInt LineNumber();
       
    43 	IMPORT_C const TDesC& StringLC();
       
    44 private:
       
    45 	const TPtrC iFilename;
       
    46 	TInt iLineNumber;
       
    47 	};
       
    48 	
       
    49 class MValueHandler
       
    50 	{
       
    51 public:
       
    52 	virtual void HandleValueL(const TDesC& aId, const TDesC& aValue, TErrorContext aErrorContext, TBool aOverwrite) = 0;
       
    53 	virtual void HandleCommentL(const TDesC& aComment, TErrorContext aErrorContext) = 0;
       
    54 	};
       
    55 	
       
    56 class CIniReader;
       
    57 
       
    58 enum TFileNotFoundAction
       
    59 	{
       
    60 	EFailIfFileNotFound,
       
    61 	ESucceedIfFileNotFound,
       
    62 	};
       
    63 	
       
    64 IMPORT_C void ReadIniFileL(const TDesC& aFilename, MValueHandler& aValueHandler, TErrorContext aErrorContext = TErrorContext(), TFileNotFoundAction aNotFoundAction = EFailIfFileNotFound);
       
    65 IMPORT_C void ReadIniFileL(RIoReadHandle& aReadHandle, MValueHandler& aValueHandler);
       
    66 IMPORT_C void WriteIniFileL(const TDesC& aFilename, CIniReader& aValues);
       
    67 
       
    68 class CValue : public CBase
       
    69 	{
       
    70 public:
       
    71 	IMPORT_C static CValue* NewL(const TDesC& aId, const TDesC& aValue, TErrorContext aErrorContext = TErrorContext());
       
    72 	IMPORT_C static CValue* NewLC(const TDesC& aId, const TDesC& aValue, TErrorContext aErrorContext = TErrorContext());
       
    73 	IMPORT_C ~CValue();
       
    74 	
       
    75 	IMPORT_C const TDesC& Id() const;
       
    76 	IMPORT_C const TDesC& Value() const;
       
    77 	IMPORT_C virtual void SetL(const TDesC& aNewValue, TErrorContext aErrorContext);
       
    78 	IMPORT_C TInt LineNumber() const;
       
    79 	IMPORT_C virtual TInt AsInt(TInt& aInt) const;
       
    80 	IMPORT_C virtual TInt AsBool(TBool& aBool) const;
       
    81 	IMPORT_C TInt AsIntL() const;
       
    82 	IMPORT_C TBool AsBoolL() const;
       
    83 	
       
    84 protected:
       
    85 	CValue(TErrorContext aErrorContext);
       
    86 	void ConstructL(const TDesC& aId, const TDesC& aValue);
       
    87 private:
       
    88 	RBuf iId;
       
    89 	RBuf iValue;
       
    90 protected:
       
    91 	TInt iLineNumber;
       
    92 	};
       
    93 
       
    94 /**
       
    95 Class for reading an fshell style INI file. This class is intended to be robust, and should always
       
    96 read the file even if it has errors in it.
       
    97 
       
    98 The class CIniFile is more strict and will validate the contents of the file as it is read.
       
    99 */
       
   100 class CIniReader	: public CBase
       
   101 					, public MValueHandler
       
   102 	{
       
   103 public:
       
   104 	IMPORT_C static CIniReader* NewL(const TDesC& aFilename, TErrorContext aErrorContext = TErrorContext());
       
   105 	IMPORT_C ~CIniReader();
       
   106 	
       
   107 	IMPORT_C const TDesC* GetValue(const TDesC& aId) const;
       
   108 	IMPORT_C void GetValuesL(RPointerArray<CValue>& aValues);
       
   109 	IMPORT_C void GetValuesL(RPointerArray<const CValue>& aValues) const;
       
   110 	IMPORT_C void GetIdsL(RArray<const TPtrC>& aIds) const;
       
   111 	
       
   112 	IMPORT_C void SetValueL(const TDesC& aId, const TDesC& aValue);
       
   113 	IMPORT_C void RemoveValueL(const TDesC& aId);
       
   114 	
       
   115 	void AppendFirstLineL(IoUtils::CTextFormatter* aFormatter);
       
   116 protected: // from MValueHandler
       
   117 	virtual void HandleValueL(const TDesC& aId, const TDesC& aValue, TErrorContext aErrorContext, TBool aOverwrite);
       
   118 	virtual void HandleCommentL(const TDesC& aComment, TErrorContext aErrorContext);
       
   119 protected:
       
   120 	virtual CValue* CreateValueLC(const TDesC& aId, const TDesC& aValue, TErrorContext aErrorContext);
       
   121 	virtual void HandleFirstLineCommentL(const TDesC&, TErrorContext) {};
       
   122 	virtual TBool IncludeValue(const CValue*) const { return ETrue; }
       
   123 	virtual void DoRemoveL(CValue* aValue);
       
   124 	CIniReader();
       
   125 protected:
       
   126 	RPtrHashMap<TDesC, CValue> iValues;
       
   127 	RBuf iFirstLineComment;
       
   128 	};
       
   129 	
       
   130 enum TSettingType
       
   131 	{
       
   132 	ETypeEnum,
       
   133 	ETypeFilename,
       
   134 	ETypeString,
       
   135 	ETypeInteger,
       
   136 	ETypeBoolean,
       
   137 	};
       
   138 	
       
   139 class CSetting : public CValue
       
   140 	{
       
   141 public:
       
   142 	IMPORT_C static CSetting* NewL(TSettingType aType, const TDesC& aId, const TDesC& aName, const TDesC& aDescription, const TDesC& aDefault = KNullDesC, const TDesC& aEnumValues = KNullDesC, TErrorContext aErrorContext = TErrorContext());
       
   143 	IMPORT_C static CSetting* NewLC(TSettingType aType, const TDesC& aId, const TDesC& aName, const TDesC& aDescription, const TDesC& aDefault = KNullDesC, const TDesC& aEnumValues = KNullDesC, TErrorContext aErrorContext = TErrorContext());
       
   144 	IMPORT_C ~CSetting();
       
   145 	
       
   146 	IMPORT_C virtual void SetL(const TDesC& aNewValue, TErrorContext aErrorContext = TErrorContext());
       
   147 	IMPORT_C void SetL(TInt aNewValue);
       
   148 	IMPORT_C virtual TInt AsInt(TInt& aInt) const;
       
   149 	IMPORT_C virtual TInt AsBool(TBool& aBool) const;
       
   150 	IMPORT_C TInt AsInt() const;
       
   151 	IMPORT_C TBool AsBool() const;
       
   152 	
       
   153 	IMPORT_C TBool IsSet() const;
       
   154 	IMPORT_C void ClearL();
       
   155 	
       
   156 	IMPORT_C const TDesC& Name();
       
   157 	IMPORT_C const TDesC& Description();
       
   158 private:
       
   159 	CSetting(TSettingType aType);
       
   160 	void ConstructL(const TDesC& aId, const TDesC& aName, const TDesC& aDescription, const TDesC& aDefault, const TDesC& aEnumValues, TErrorContext aErrorContext = TErrorContext());
       
   161 	TInt ParseL(const TDesC& aValue, TErrorContext aErrorContext);
       
   162 private:
       
   163 	const TSettingType iType;
       
   164 	RBuf iName;
       
   165 	RBuf iDescription;
       
   166 	RBuf iDefault;
       
   167 	RBuf iEnumValues;
       
   168 	TInt iIntValue;
       
   169 	TInt iDefaultInt;
       
   170 	TBool iIsSet;
       
   171 	IoUtils::TEnum iEnum;
       
   172 	};
       
   173 	
       
   174 /**
       
   175 Class for validating an fshell style ini file. This class will fail (and reports useful errors) if the
       
   176 file is corrupt of invalid in any way. The file will be validated using an INI description file (IDF).
       
   177 */
       
   178 class CIniFile : public CIniReader
       
   179 	{
       
   180 public:
       
   181 	/**
       
   182 	Read and validate an INI file.
       
   183 	
       
   184 	If an IDF file is given, it will be used to validate the INI file regardless of any #!iniedit
       
   185 	line at the start of the file. Also, if the INI file given does not exist, this function will
       
   186 	still succeed and can then be used to create the INI file by calling WriteIniFileL().
       
   187 	
       
   188 	If no IDF file is specified here, then an IDF file specified in the #!iniedit comment on the
       
   189 	first line will be used. Note, if the INI file does not exist this function will fail in this
       
   190 	case.
       
   191 	*/
       
   192 	IMPORT_C static CIniFile* NewL(const TDesC& aIniFile, const TDesC& aInfoFile = KNullDesC);
       
   193 	IMPORT_C ~CIniFile();
       
   194 	IMPORT_C CSetting* GetSetting(const TDesC& aId);
       
   195 	
       
   196 	IMPORT_C const TDesC& GetString(const TDesC& aId);
       
   197 	IMPORT_C TInt GetInt(const TDesC& aId);
       
   198 	IMPORT_C TBool GetBool(const TDesC& aId);
       
   199 	
       
   200 	IMPORT_C void SetL(const TDesC& aId, const TDesC& aString);
       
   201 	IMPORT_C void SetL(const TDesC& aId, TInt aInt);
       
   202 	
       
   203 	IMPORT_C void WriteL();
       
   204 	
       
   205 protected:// from CIniReader
       
   206 	virtual CValue* CreateValueLC(const TDesC& aId, const TDesC& aValue, TErrorContext aErrorContext);
       
   207 	virtual void HandleFirstLineCommentL(const TDesC& aComment, TErrorContext aErrorContext);
       
   208 	virtual TBool IncludeValue(const CValue* aValue) const;
       
   209 	virtual void DoRemoveL(CValue* aValue);
       
   210 protected: // from MValueHandler
       
   211 	virtual void HandleValueL(const TDesC& aId, const TDesC& aValue, TErrorContext aErrorContext, TBool aOverwrite);
       
   212 private:
       
   213 	CIniFile();
       
   214 	void ConstructL(const TDesC& aIniFile, const TDesC& aInfoFile);
       
   215 	void ReadInfoFileL(const TDesC& aFileName, TErrorContext aErrorContext);
       
   216 private:
       
   217 	CIniReader* iInfoFile;
       
   218 	RBuf iFilename;
       
   219 	};
       
   220 
       
   221 	} // namespace LtkUtils
       
   222 
       
   223 #endif //__SETTINGSEDITOR_H__