2 * Copyright (c) 2002-2006 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: Offers functionality for resolving corresponding error texts
21 #if !defined TEXT_RESOLVER_H
22 #define TEXT_RESOLVER_H
24 #include <coemain.h> // CCoeEnv
25 #include <TextResolver.hrh> // Resource flags
28 typedef CArrayFixFlat<TInt> CErrorResourceIdArray;
29 typedef CArrayFixFlat<TInt> CErrorResourceFlagArray;
32 * Class offers functionality for resolving corresponding error texts for
33 * error codes. Text Resolver API provides operations with which a specific
34 * error code can be resolved to a human readable text. Descriptions for the
35 * different error codes must be defined in the resource files of this
38 * Text Resolver uses the CCoeEnv environment class to access the resource
39 * files if the CCoeEnv environment is available. If the CCoeEnv environment
40 * is not available, the files are accessed through the RResourceFile API.
42 * The API consist of the CTextResolver class which is used together with
43 * Text Resolver flags defined in textresolver.hrh file. The flags are used
44 * to tell the priority of the error to the client:
46 * - EErrorResBlankErrorFlag is used to tell that error has no proper explanation.
47 * - EErrorResUnknownErrorFlag indicates that Text Resolver does not support the error.
48 * - EErrorResOOMFlag Flag is returned while processing KErrNoMemory error code.
54 * #include <textresolver.h>
56 * // Typically used as an instance variable.
58 * // Call the factory method NewLC() to create a new instance of CTextResolver.
59 * // NewLC() method leaves the instance of the object on the cleanup stack.
60 * // The passed CCoeEnv instance is needed to access the resource files.
61 * CTextResolver* iTextResolver = CTextResolver::NewLC(*iCoeEnv);
63 * // // Error Resolving, simple:
65 * // Get error code to be resolved.
66 * // TInt err1 = MyMethod(KInvalidValue);
67 * TInt err1 = KErrNoMemory;
69 * TPtrC buf1; // For returned error text
71 * if (err1 != KErrNone)
73 * // Resolve the given error code.
74 * // The operation returns the error text for the resolved error.
75 * // There's no limit how long the resolved string can be.
76 * // Add context to 2nd param if needed.
77 * buf1.Set(iTextResolver->ResolveErrorString(err));
84 * // Note that buf1 will only be valid as long as CTextResolver
85 * // instance is alive and no new error is resolved by the same instance.
86 * // If for some reason you need to store the resolved error
87 * // beyond immediate use, make a copy of it.
89 * // Error Resolving, advanced:
91 * // Get error code to be resolved.
92 * // TInt err2 = MyMethod(KInvalidValue);
93 * TInt err2 = KErrNotSupported;
95 * TInt textId(0); // ID of the returned text
96 * TUint flags(0); // Priority of the returned error
97 * TPtrC buf2; // For returned error text
99 * if (err2 != KErrNone)
101 * // Resolve the given error code.
102 * // The operation returns the error text for the resolved error.
103 * // There's no limit on how long the resolved string can be.
104 * // Add Context to 4th param if needed.
105 * buf2.Set(iTextResolver->ResolveErrorString(err, textId, flags));
107 * if (flags & EErrorResUnknownErrorFlag)
109 * // The flag indicates that Text Resolver does not support
119 * // Note that buf2 will only be valid as long as CTextResolver
120 * // instance is alive and no new error is resolved by the same instance.
121 * // If for some reason you need to store the resolved error
122 * // beyond immediate use, make a copy of it.
124 * // iTextResolver, Free loaded resources
125 * CleanupStack::PopAndDestroy();
128 * @lib commonengine.lib
132 class CTextResolver : public CBase
138 * Defines used error contexts.
139 * Optional error contexes for aiding the mapping of error codes to
140 * texts in a unique way. If no context is given the assumption is
141 * that error codes are unique.
145 /** Context is defined automatically from error code value.
146 * Here it is assumed that each error code is unique and in
147 * own range. This is a default value when resolving errors.
150 /** Context text is not added to the beginning of the resolved error text,
151 * just context separator ':' and newline are added.
154 /** No context text, context separator ':' or newline added to the
155 * beginning of the resolved error text.
157 ECtxNoCtxNoSeparator = 2
161 * Two-phase constructor method that is used to create a new instance
162 * of the CTextResolver class. The implementation uses the passed
163 * CCoeEnv instance to access the resource files.
165 * @param aEnv the CCoeEnv instance to be used to access the resource
167 * @return a pointer to a new instance of the CTextResolver class.
169 IMPORT_C static CTextResolver* NewL(CCoeEnv& aEnv);
172 * Constructor creates a new instance of CTextResolver. The
173 * implementation uses the passed CCoeEnv instance to access the
174 * resource files. Leaves the object on the cleanup stack.
176 * @param aEnv the CCoeEnv instance to be used to access the resource
178 * @return a pointer to a new instance of the CTextResolver class.
180 IMPORT_C static CTextResolver* NewLC(CCoeEnv& aEnv);
183 * Constructor creates a new instance of CTextResolver. Resource files
184 * are accessed through the RResourceFile API.
186 * @return a pointer to a new instance of the CTextResolver class.
188 IMPORT_C static CTextResolver* NewL();
191 * Constructor creates a new instance of CTextResolver.Resource files
192 * are accessed through the RResourceFile API. Leaves the object on
195 * @return a pointer to a new instance of the CTextResolver class.
197 IMPORT_C static CTextResolver* NewLC();
203 IMPORT_C ~CTextResolver();
206 * Resolves the given error code and returns the error text for the
207 * resolved error. Resolved text can be of any length. This version
208 * is for advanced use
210 * @param aError The error code to be resolved.
211 * @param aTextId ID of the returned text.
212 * @param aFlags The priority of the returned error. The priority is
213 * defined by the this module! Flags are defined in textresolver.hrh.
214 * @param aContext Optional context for error numbers. If the aContext
215 * parameter is not passed to the function, it uses the default value
217 * @return the error text for the resolved error. "System error" (in
218 * English localisation) is returned when error code is not known. For
219 * unknown errors blank error flag (flags are defined in
220 * textresolver.hrh) is also set to hide errors without proper
221 * explanation. There is no limit on how long the resolved string
224 IMPORT_C const TDesC& ResolveErrorString(
228 TErrorContext aContext = ECtxAutomatic);
231 * Resolves the given error code and returns the error text for the
232 * resolved error. Resolved text can be of any length. This version
233 * is for "normal" use.
235 * @param aError The error code to be resolved.
236 * @param aContext Optional context for error numbers. If the aContext
237 * parameter is not passed to the function, it uses the default value
239 * @return the error text for the resolved error. "System error" (in
240 * English localisation) is returned when error code is not known. For
241 * unknown errors blank error flag (flags are defined in
242 * textresolver.hrh) is also set to hide errors without proper
243 * explanation. There is no limit on how long the resolved string
246 IMPORT_C const TDesC& ResolveErrorString(
248 TErrorContext aContext = ECtxAutomatic);
252 virtual TInt ResourceForError(TInt aError);
253 virtual void LoadResourceFilesL();
256 CTextResolver(CCoeEnv& aEnv);
261 void FindFullPathOfResourceFile(TFileName& aResFile) const;
262 void ReadResourcesToArraysL(TInt& aError, TInt& aTextId);
264 void PrepareReaderLC(TResourceReader& reader);
266 // returns NULL if fails
267 HBufC* ReadUnicodeString(const TInt& aTextId);
269 // returns false if any memory allocation fails or initial values
270 // of necessary pointers are NULL, indicating alloc failure earlier.
271 TBool AddContextAndSeparator(TErrorContext aContext);
276 RResourceFile iRFile;
278 TInt iBaseResourceFileOffset;
279 CArrayFix<TInt>* iStartError;
280 CArrayFix<TInt>* iAppTexts;
281 CArrayPtr<CErrorResourceIdArray>* iErrorTexts;
282 CArrayPtr<CErrorResourceFlagArray>* iFlags;
285 HBufC* iContextSeparator;
287 TPtrC iTruncatedTextPointer;