epoc32/include/textresolver.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@2
     1
/*
williamr@4
     2
* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
williamr@2
     3
* All rights reserved.
williamr@2
     4
* This component and the accompanying materials are made available
williamr@4
     5
* under the terms of "Eclipse Public License v1.0"
williamr@2
     6
* which accompanies this distribution, and is available
williamr@4
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
williamr@2
     8
*
williamr@2
     9
* Initial Contributors:
williamr@2
    10
* Nokia Corporation - initial contribution.
williamr@2
    11
*
williamr@2
    12
* Contributors:
williamr@2
    13
*
williamr@4
    14
* Description:
williamr@2
    15
*
williamr@2
    16
*/
williamr@2
    17
williamr@2
    18
williamr@2
    19
williamr@4
    20
williamr@4
    21
#ifndef TEXT_RESOLVER_H
williamr@2
    22
#define TEXT_RESOLVER_H
williamr@2
    23
williamr@2
    24
#include <coemain.h>    //  CCoeEnv
williamr@4
    25
#include <textresolver.hrh> // Resource flags 
williamr@4
    26
#include <eikenv.h>    //  CEikonEnv
williamr@2
    27
williamr@2
    28
// DEFINES
williamr@2
    29
typedef CArrayFixFlat<TInt> CErrorResourceIdArray;
williamr@2
    30
typedef CArrayFixFlat<TInt> CErrorResourceFlagArray;
williamr@2
    31
williamr@2
    32
/**
williamr@2
    33
* Class offers functionality for resolving corresponding error texts for 
williamr@2
    34
* error codes. Text Resolver API provides operations with which a specific 
williamr@2
    35
* error code can be resolved to a human readable text. Descriptions for the
williamr@2
    36
* different error codes must be defined in the resource files of this 
williamr@2
    37
* component. 
williamr@2
    38
*
williamr@2
    39
* Text Resolver uses the CCoeEnv environment class to access the resource
williamr@2
    40
* files if the CCoeEnv environment is available. If the CCoeEnv environment
williamr@2
    41
* is not available, the files are accessed through the RResourceFile API.
williamr@2
    42
*
williamr@2
    43
* The API consist of the CTextResolver class which is used together with 
williamr@2
    44
* Text Resolver flags defined in textresolver.hrh file. The flags are used
williamr@2
    45
* to tell the priority of the error to the client:
williamr@2
    46
*
williamr@2
    47
* - EErrorResBlankErrorFlag is used to tell that error has no proper explanation.
williamr@2
    48
* - EErrorResUnknownErrorFlag indicates that Text Resolver does not support the error. 
williamr@2
    49
* - EErrorResOOMFlag Flag is returned while processing KErrNoMemory error code. 
williamr@2
    50
*
williamr@2
    51
*
williamr@2
    52
* Usage:
williamr@2
    53
*
williamr@2
    54
* @code
williamr@2
    55
*  #include <textresolver.h>
williamr@2
    56
*
williamr@2
    57
*  // Typically used as an instance variable.
williamr@2
    58
*  
williamr@2
    59
*  // Call the factory method NewLC() to create a new instance of CTextResolver.
williamr@2
    60
*  // NewLC() method leaves the instance of the object on the cleanup stack.	
williamr@2
    61
*  // The passed CCoeEnv instance is needed to access the resource files.
williamr@2
    62
*  CTextResolver* iTextResolver = CTextResolver::NewLC(*iCoeEnv); 
williamr@2
    63
*
williamr@2
    64
*  // // Error Resolving, simple:
williamr@2
    65
*
williamr@2
    66
*  // Get error code to be resolved.
williamr@2
    67
*  // TInt err1 = MyMethod(KInvalidValue);
williamr@2
    68
*  TInt err1 = KErrNoMemory;
williamr@2
    69
*    
williamr@2
    70
*  TPtrC buf1;  // For returned error text
williamr@2
    71
*    
williamr@2
    72
*  if (err1 != KErrNone)
williamr@2
    73
*      {
williamr@2
    74
*      // Resolve the given error code. 
williamr@2
    75
*      // The operation returns the error text for the resolved error.
williamr@2
    76
*      // There's no limit how long the resolved string can be.
williamr@2
    77
*      // Add context to 2nd param if needed.
williamr@2
    78
*      buf1.Set(iTextResolver->ResolveErrorString(err)); 
williamr@2
    79
*      }
williamr@2
    80
*  else
williamr@2
    81
*      {
williamr@2
    82
*      // Do something.
williamr@2
    83
*      }
williamr@2
    84
*        
williamr@2
    85
*  // Note that buf1 will only be valid as long as CTextResolver 
williamr@2
    86
*  // instance is alive and no new error is resolved by the same instance.
williamr@2
    87
*  // If for some reason you need to store the resolved error
williamr@2
    88
*  // beyond immediate use, make a copy of it.
williamr@2
    89
*
williamr@2
    90
*  // Error Resolving, advanced:
williamr@2
    91
* 
williamr@2
    92
*  // Get error code to be resolved.
williamr@2
    93
*  // TInt err2 = MyMethod(KInvalidValue);
williamr@2
    94
*  TInt err2 = KErrNotSupported;
williamr@2
    95
*        
williamr@2
    96
*  TInt textId(0);    // ID of the returned text
williamr@2
    97
*  TUint flags(0);    // Priority of the returned error 
williamr@2
    98
*  TPtrC buf2;      // For returned error text
williamr@2
    99
*   
williamr@2
   100
*  if (err2 != KErrNone)
williamr@2
   101
*      {
williamr@2
   102
*      // Resolve the given error code.
williamr@2
   103
*      // The operation returns the error text for the resolved error.
williamr@2
   104
*      // There's no limit on how long the resolved string can be.
williamr@2
   105
*      // Add Context to 4th param if needed.
williamr@2
   106
*      buf2.Set(iTextResolver->ResolveErrorString(err, textId, flags)); 
williamr@2
   107
*
williamr@2
   108
*      if (flags & EErrorResUnknownErrorFlag)
williamr@2
   109
*          {
williamr@2
   110
*          // The flag indicates that Text Resolver does not support
williamr@2
   111
*          // the error code. 
williamr@2
   112
*          // Do something.
williamr@2
   113
*          }
williamr@2
   114
*      }
williamr@2
   115
*  else
williamr@2
   116
*      {
williamr@2
   117
*      // Do something.
williamr@2
   118
*      }
williamr@2
   119
*    
williamr@2
   120
*  // Note that buf2 will only be valid as long as CTextResolver 
williamr@2
   121
*  // instance is alive and no new error is resolved by the same instance.
williamr@2
   122
*  // If for some reason you need to store the resolved error
williamr@2
   123
*  // beyond immediate use, make a copy of it.
williamr@2
   124
*
williamr@2
   125
*  // iTextResolver, Free loaded resources
williamr@2
   126
*  CleanupStack::PopAndDestroy(); 
williamr@2
   127
* @endcode
williamr@2
   128
*
williamr@2
   129
* @lib commonengine.lib
williamr@2
   130
* @since S60 2.0
williamr@2
   131
*/
williamr@2
   132
class CTextResolver : public CBase
williamr@2
   133
    {
williamr@4
   134
public:
williamr@4
   135
    /**
williamr@4
   136
    * Defines used error contexts. 
williamr@4
   137
    * Optional error contexes for aiding the mapping of error codes to 
williamr@4
   138
    * texts in a unique way. If no context is given the assumption is 
williamr@4
   139
    * that error codes are unique.
williamr@4
   140
    */
williamr@4
   141
    enum TErrorContext
williamr@4
   142
        {
williamr@4
   143
        /** Context is defined automatically from error code value.
williamr@4
   144
        * Here it is assumed that each error code is unique and in
williamr@4
   145
        * own range. This is a default value when resolving errors. */
williamr@4
   146
        ECtxAutomatic = 0,
williamr@4
   147
        /** Context text is not added to the beginning of the resolved error text,
williamr@4
   148
        * just context separator ':' and newline are added. */
williamr@4
   149
        ECtxNoCtx = 1,
williamr@4
   150
        /** No context text, context separator ':' or newline added to the 
williamr@4
   151
        * beginning of the resolved error text. */
williamr@4
   152
        ECtxNoCtxNoSeparator = 2
williamr@4
   153
        };
williamr@4
   154
public:
williamr@4
   155
    IMPORT_C static CTextResolver* NewL(CCoeEnv& aEnv);
williamr@4
   156
    IMPORT_C static CTextResolver* NewLC(CCoeEnv& aEnv);
williamr@4
   157
    IMPORT_C static CTextResolver* NewL();
williamr@4
   158
	IMPORT_C static CTextResolver* NewLC();
williamr@4
   159
    IMPORT_C ~CTextResolver();
williamr@4
   160
	IMPORT_C const TDesC& ResolveErrorString(TInt aError, TInt& aTextId, TUint& aFlags,TErrorContext aContext = ECtxAutomatic);
williamr@4
   161
    IMPORT_C const TDesC& ResolveErrorString(TInt aError, TErrorContext aContext = ECtxAutomatic);
williamr@4
   162
private: // Construction
williamr@4
   163
    virtual TInt ResourceForError(TInt aError);
williamr@4
   164
    virtual void LoadResourceFilesL() { ASSERT(0); }	// deprecated
williamr@4
   165
    CTextResolver(CCoeEnv& aEnv);
williamr@4
   166
    CTextResolver();
williamr@4
   167
    void ConstructL();
williamr@4
   168
    
williamr@4
   169
    // Utility
williamr@4
   170
    void DoRawReadOfSystemErrorResourcesToArraysL(TInt& aError, TInt& aTextId);
williamr@4
   171
    void Reset();
williamr@4
   172
    void ReadLocalizedSeparatorCharacterFromResourceL(CCoeEnv& aCoeEnv);
williamr@4
   173
    void ReadLocalizedSeparatorCharacterFromResourceAndPrepareResourceReaderLC(TResourceReader& aResReader);
williamr@2
   174
williamr@4
   175
    // returns NULL if fails
williamr@4
   176
    static HBufC* AllocReadUnicodeString(RResourceFile& aResFile, TInt aTextId);
williamr@4
   177
    /** Returns false if any memory allocation fails or initial values 
williamr@4
   178
	of necessary pointers are NULL, indicating alloc failure earlier.*/
williamr@4
   179
    TBool AddContextAndSeparator(TErrorContext aContext);
williamr@4
   180
    void AllocBuffersL();
williamr@4
   181
    void DoResolveErrorStringL(TInt aError, TInt& aTextId, TUint& aFlags);
williamr@4
   182
private:
williamr@4
   183
    CCoeEnv* iCoe;
williamr@4
   184
    RResourceFile iResFile;
williamr@4
   185
    TInt iRDSupport;
williamr@4
   186
    TInt iBaseResourceFileOffset;
williamr@4
   187
    CArrayFix<TInt>* iStartError;
williamr@4
   188
    CArrayFix<TInt>* iAppTexts;
williamr@4
   189
    CArrayPtr<CErrorResourceIdArray>*   iErrorTexts;	// Array of arrays of ints
williamr@4
   190
    CArrayPtr<CErrorResourceFlagArray>* iFlags;			// Array of arrays of ints
williamr@4
   191
    HBufC* iTextBuffer;
williamr@4
   192
    HBufC* iTitleText;
williamr@4
   193
    HBufC* iContextSeparator;
williamr@4
   194
    RFs iFs;
williamr@4
   195
    TPtrC  iTruncatedTextPointer;
williamr@4
   196
	};
williamr@2
   197
williamr@2
   198
#endif
williamr@2
   199