epoc32/include/finditemengine.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
parent 0 061f57f2323e
child 4 837f303aceeb
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
williamr@2
     1
/*
williamr@2
     2
* Copyright (c) 2002-2006 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@2
     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
williamr@2
     6
* which accompanies this distribution, and is available
williamr@2
     7
* at the URL "http://www.symbianfoundation.org/legal/licencesv10.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@2
    14
* Description:  Find Item API offers methods for parsing phone numbers, email 
williamr@2
    15
*                addresses, URL addresses and URI addresses from given text.
williamr@2
    16
*
williamr@2
    17
*/
williamr@2
    18
williamr@2
    19
williamr@2
    20
#ifndef FINDITEMENGINE_H
williamr@2
    21
#define FINDITEMENGINE_H
williamr@2
    22
williamr@2
    23
//  INCLUDES
williamr@2
    24
#include <e32base.h>
williamr@2
    25
williamr@2
    26
// CLASS DECLARATION
williamr@2
    27
williamr@2
    28
/**
williamr@2
    29
* Class is used to parse phone numbers and email, URL and URI addresses from 
williamr@2
    30
* given text. Find Item API offers methods for parsing phone numbers and 
williamr@2
    31
* e-mail, URL and URI addresses from the given text. The API consist of the 
williamr@2
    32
* CFindItemEngine class.
williamr@2
    33
*
williamr@2
    34
* Usage:
williamr@2
    35
* 
williamr@2
    36
* @code
williamr@2
    37
*  #include <finditemengine.h>
williamr@2
    38
*
williamr@2
    39
*  // SFoundItem instance
williamr@2
    40
*  CFindItemEngine::SFoundItem item;
williamr@2
    41
*
williamr@2
    42
*  // Some text
williamr@2
    43
*  TBufC<256> strSomeText(_L("Mail to me@someplace.com or call 040 1234567. 
williamr@2
    44
*  You can also tune in to audio feed at rtsp://someplace.com/somefeed.ra."));
williamr@2
    45
*	
williamr@2
    46
*  // First the user has to create an instance of CFindItemEngine by using the
williamr@2
    47
*  // factory method NewL(). The method takes two parameters. The first 
williamr@2
    48
*  // parameter defines the text to be searched from and the second parameter 
williamr@2
    49
*  // tells what exactly is being looked for.
williamr@2
    50
*  CFindItemEngine* singleSearch = CFindItemEngine::NewL(strSomeText, 
williamr@2
    51
*                   CFindItemEngine::EFindItemSearchMailAddressBin);
williamr@2
    52
*
williamr@2
    53
*  // The passed text is parsed in construction, and found items can be fetched 
williamr@2
    54
*  // by using the ItemArray() method. It returns a constant array containing 
williamr@2
    55
*  // all the found items. The interface offers also helper functions for 
williamr@2
    56
*  // handling the item array by itself. 
williamr@2
    57
*
williamr@2
    58
*  // Get count of found items.
williamr@2
    59
*  TInt count(singleSearch->ItemCount());
williamr@2
    60
*
williamr@2
    61
*  // Get currently selected item (me@someplace.com) to the result1 variable.
williamr@2
    62
*  singleSearch->Item(item);
williamr@2
    63
*  TPtrC16 result1(strSomeText.Mid(item.iStartPos, item.iLength));
williamr@2
    64
*
williamr@2
    65
*  // Deallocate memory
williamr@2
    66
*  delete singleSearch;
williamr@2
    67
*
williamr@2
    68
*  // Create an instance of FindItemEngine and look for all possible 
williamr@2
    69
*  // things (cases work as binary mask).
williamr@2
    70
*  CFindItemEngine* multiSearch = CFindItemEngine::NewL(strSomeText,
williamr@2
    71
*                   (CFindItemEngine::TFindItemSearchCase)
williamr@2
    72
*                   (CFindItemEngine::EFindItemSearchPhoneNumberBin |           
williamr@2
    73
*                   CFindItemEngine::EFindItemSearchURLBin | 
williamr@2
    74
*                   CFindItemEngine::EFindItemSearchMailAddressBin | 
williamr@2
    75
*                   CFindItemEngine::EFindItemSearchScheme));
williamr@2
    76
*
williamr@2
    77
*  // Get count of found items.
williamr@2
    78
*  TInt count2(multiSearch->ItemCount());
williamr@2
    79
*
williamr@2
    80
*  // Get currently selected item to the result2 variable.
williamr@2
    81
*  multiSearch->Item(item);
williamr@2
    82
*
williamr@2
    83
*  // Debug print all items and their type.
williamr@2
    84
*  for( TInt i=0; i<count2; i++)
williamr@2
    85
*      {
williamr@2
    86
*      TPtrC16 result2(strSomeText.Mid(item.iStartPos, item.iLength));
williamr@2
    87
*      RDebug::Print(_L("Found type %d item:"), item.iItemType);
williamr@2
    88
*      RDebug::Print(_L("%S"), &result2);
williamr@2
    89
*      multiSearch->NextItem(item);
williamr@2
    90
*      }
williamr@2
    91
*
williamr@2
    92
*  // Deallocate memory
williamr@2
    93
*  delete multiSearch;
williamr@2
    94
* @endcode
williamr@2
    95
*
williamr@2
    96
* @lib commonengine.lib
williamr@2
    97
* @since S60 2.0
williamr@2
    98
*/
williamr@2
    99
williamr@2
   100
class CFindItemEngine :public CBase
williamr@2
   101
    {
williamr@2
   102
    public:
williamr@2
   103
williamr@2
   104
        /**
williamr@2
   105
        * Enumeration to define the search case. 
williamr@2
   106
        * Multiple enumerations can be used as binary mask.
williamr@2
   107
        */
williamr@2
   108
        enum TFindItemSearchCase
williamr@2
   109
            {
williamr@2
   110
            /** Searches phone numbers.
williamr@2
   111
            */
williamr@2
   112
			EFindItemSearchPhoneNumberBin = 4, 
williamr@2
   113
            /** Searches mail addresses.
williamr@2
   114
            */
williamr@2
   115
            EFindItemSearchMailAddressBin = 8,
williamr@2
   116
            /** Searches fixed start URLs ("http://", "https://", "rtsp://"), "www.", "wap." and IPv4 addresses.
williamr@2
   117
            */
williamr@2
   118
            EFindItemSearchURLBin  = 16,
williamr@2
   119
            /** Searches for all URIs containing a scheme.
williamr@2
   120
            */
williamr@2
   121
            EFindItemSearchScheme  = 32
williamr@2
   122
            };
williamr@2
   123
williamr@2
   124
        /** 
williamr@2
   125
        * Struct to contain an item.
williamr@2
   126
        */
williamr@2
   127
        struct SFoundItem
williamr@2
   128
            {
williamr@2
   129
            /**Start position of the found item.
williamr@2
   130
            */
williamr@2
   131
            TInt iStartPos;
williamr@2
   132
            /** Length of the found item (characters).
williamr@2
   133
            */
williamr@2
   134
            TInt iLength;
williamr@2
   135
			/** Search case of the found item
williamr@2
   136
			*/
williamr@2
   137
            TFindItemSearchCase iItemType;
williamr@2
   138
			};
williamr@2
   139
williamr@2
   140
    public:  // Constructors and destructor
williamr@2
   141
        
williamr@2
   142
        /**
williamr@2
   143
        * Two-phase constructor method that is used to create a new instance
williamr@2
   144
        * of the CFindItemEngine class. This instance can then be queried for
williamr@2
   145
        * the items defined by the second parameter. The actual search is 
williamr@2
   146
        * executed during construction.
williamr@2
   147
        *
williamr@2
   148
        * @param aText will be parsed.
williamr@2
   149
        * @param aSearchCase identifies what items are we looking for: 
williamr@2
   150
        * EFindItemSearchPhoneNumberBin
williamr@2
   151
        * EFindItemSearchMailAddressBin
williamr@2
   152
        * EFindItemSearchURLBin
williamr@2
   153
        * EFindItemSearchScheme
williamr@2
   154
        * Any combination of these flags can be given as a bit mask.
williamr@2
   155
        * @return a pointer to an new instance of CFindItemEngine class.
williamr@2
   156
        *
williamr@2
   157
        * @panic ENoSearchCase in debug build if there is no valid search case.
williamr@2
   158
        * @panic EItemOutOfDocumentRange in debug build if item's position 
williamr@2
   159
        * and/or length is out of the document's range.
williamr@2
   160
        * @leave one of the Symbian error codes.
williamr@2
   161
        */
williamr@2
   162
        IMPORT_C static CFindItemEngine* NewL( const TDesC& aText, 
williamr@2
   163
                                               const TFindItemSearchCase aSearchCase );
williamr@2
   164
williamr@2
   165
        /**
williamr@2
   166
        * Two-phase constructor method that is used to create a new instance
williamr@2
   167
        * of the CFindItemEngine class. This instance can then be queried for
williamr@2
   168
        * the items defined by the second parameter. The actual search is 
williamr@2
   169
        * executed during construction.
williamr@2
   170
        *
williamr@2
   171
        * @param aText will be parsed.
williamr@2
   172
        * @param aSearchCase identifies what items are we looking for: 
williamr@2
   173
        * EFindItemSearchPhoneNumberBin
williamr@2
   174
        * EFindItemSearchMailAddressBin
williamr@2
   175
        * EFindItemSearchURLBin
williamr@2
   176
        * EFindItemSearchScheme
williamr@2
   177
        * Any combination of these flags can be given as a bit mask.
williamr@2
   178
        * @param aMinNumbers defines minimun count of numbers in a string that 
williamr@2
   179
        * the string is considered as a phone number when phone numbers are 
williamr@2
   180
        * searched.
williamr@2
   181
        * @return a pointer to an new instance of CFindItemEngine class.
williamr@2
   182
        *
williamr@2
   183
        * @panic ENoSearchCase in debug build if there is no valid search case.
williamr@2
   184
        * @panic EItemOutOfDocumentRange in debug build if item's position 
williamr@2
   185
        * and/or length is out of the document's range.
williamr@2
   186
        * @leave one of the Symbian error codes.
williamr@2
   187
        */
williamr@2
   188
        IMPORT_C static CFindItemEngine* NewL( const TDesC& aText, 
williamr@2
   189
                                               const TFindItemSearchCase aSearchCase,
williamr@2
   190
                                               const TInt aMinNumbers );
williamr@2
   191
williamr@2
   192
        /**
williamr@2
   193
        * Destructor.
williamr@2
   194
        */
williamr@2
   195
        IMPORT_C virtual ~CFindItemEngine();
williamr@2
   196
williamr@2
   197
    public:
williamr@2
   198
        /**
williamr@2
   199
        * Gets the currently 'selected' item in the array of found items. 
williamr@2
   200
        *
williamr@2
   201
        * @param aItem contains the currently selected item after returning.
williamr@2
   202
        * @return ETrue if the item was found. EFalse if the item wasn't found.
williamr@2
   203
        */
williamr@2
   204
        IMPORT_C TBool Item( SFoundItem& aItem );
williamr@2
   205
williamr@2
   206
        /**
williamr@2
   207
        * Gets the next found item relative to the currently selected item 
williamr@2
   208
        * and moves the selection to point to the next item in the array of 
williamr@2
   209
        * found items. 
williamr@2
   210
        *
williamr@2
   211
        * @param aItem contains the next item after returning.
williamr@2
   212
        * @return ETrue if the item was found. EFalse if there's no next item.
williamr@2
   213
        */
williamr@2
   214
        IMPORT_C TBool NextItem( SFoundItem& aItem );
williamr@2
   215
williamr@2
   216
        /**
williamr@2
   217
        * Gets the previous found item relative to the currently selected 
williamr@2
   218
        * item and moves the selection to point to the previous item in the 
williamr@2
   219
        * array of found items.. 
williamr@2
   220
        *
williamr@2
   221
        * @param aItem contains the previous item after returning.
williamr@2
   222
        * @return ETrue if the item was found. EFalse if there's no previous item.
williamr@2
   223
        */
williamr@2
   224
        IMPORT_C TBool PrevItem( SFoundItem& aItem );
williamr@2
   225
		
williamr@2
   226
        /**
williamr@2
   227
        * Gets the array of found items. Returns a constant pointer to the 
williamr@2
   228
        * found items array of the CFindItemEngine instance. The items cannot
williamr@2
   229
        * be modified through this pointer, only accessed. The ownership of 
williamr@2
   230
        * the array stays with CFindItemEngine.
williamr@2
   231
        *
williamr@2
   232
        * @return a constant pointer to the array of found items. Ownership 
williamr@2
   233
        * stays with CFindItemEngine.
williamr@2
   234
        */
williamr@2
   235
        IMPORT_C const CArrayFixFlat<SFoundItem>* ItemArray() const;
williamr@2
   236
williamr@2
   237
        /**
williamr@2
   238
        * Gets the current position (or the position of the currently selected item) 
williamr@2
   239
        * in the found items array.
williamr@2
   240
        *
williamr@2
   241
        * @return the current position in the found items array of the 
williamr@2
   242
        * CFindItemEngine instance. If no items are in the array, zero is 
williamr@2
   243
        * returned.
williamr@2
   244
        */
williamr@2
   245
		IMPORT_C TInt Position() const;	
williamr@2
   246
	
williamr@2
   247
        /**
williamr@2
   248
        * Resets the position in item array to zero (beginning of the array).
williamr@2
   249
        */
williamr@2
   250
        IMPORT_C void ResetPosition();
williamr@2
   251
williamr@2
   252
        /**
williamr@2
   253
        * Gets the number of items in the found items array.
williamr@2
   254
        *
williamr@2
   255
        * @return the number of items in the found items array. 
williamr@2
   256
        */
williamr@2
   257
		IMPORT_C TInt ItemCount() const;
williamr@2
   258
		
williamr@2
   259
        /**
williamr@2
   260
        * Executes a new search with the already created CFindItemEngine 
williamr@2
   261
        * instance. The position in the found items array is reset to the 
williamr@2
   262
        * beginning of the array.
williamr@2
   263
        *
williamr@2
   264
        * @param aText will be parsed.
williamr@2
   265
        * @param aSearchCase identifies what items are we looking for: 
williamr@2
   266
        * EFindItemSearchPhoneNumberBin
williamr@2
   267
        * EFindItemSearchMailAddressBin
williamr@2
   268
        * EFindItemSearchURLBin
williamr@2
   269
        * EFindItemSearchScheme
williamr@2
   270
        * Any combination of these flags can be given as a bit mask.
williamr@2
   271
        * @return number of found items.
williamr@2
   272
        *
williamr@2
   273
        * @panic ENoSearchCase in debug build if there is no valid search case.
williamr@2
   274
        * @panic EItemOutOfDocumentRange in debug build if item's position 
williamr@2
   275
        * and/or length is out of the document's range.
williamr@2
   276
        * @leave one of the Symbian error codes.
williamr@2
   277
        */
williamr@2
   278
        IMPORT_C TInt DoNewSearchL( const TDesC& aText, const TFindItemSearchCase aSearchCase);
williamr@2
   279
williamr@2
   280
        /**
williamr@2
   281
        * Executes a new search with the already created CFindItemEngine 
williamr@2
   282
        * instance. The position in the found items array is reset to the 
williamr@2
   283
        * beginning of the array.
williamr@2
   284
        *
williamr@2
   285
        * @param aText will be parsed.
williamr@2
   286
        * @param aSearchCase identifies what items are we looking for: 
williamr@2
   287
        * EFindItemSearchPhoneNumberBin
williamr@2
   288
        * EFindItemSearchMailAddressBin
williamr@2
   289
        * EFindItemSearchURLBin
williamr@2
   290
        * EFindItemSearchScheme
williamr@2
   291
        * Any combination of these flags can be given as a bit mask.
williamr@2
   292
        * @param aMinNumbers defines minimun count of numbers in a string that 
williamr@2
   293
        * the string is considered as a phone number when phone numbers are 
williamr@2
   294
        * searched.
williamr@2
   295
        * @return number of found items.
williamr@2
   296
        *
williamr@2
   297
        * @panic ENoSearchCase in debug build if there is no valid search case.
williamr@2
   298
        * @panic EItemOutOfDocumentRange in debug build if item's position 
williamr@2
   299
        * and/or length is out of the document's range.
williamr@2
   300
        * @leave one of the Symbian error codes.
williamr@2
   301
        */
williamr@2
   302
        IMPORT_C TInt DoNewSearchL( const TDesC& aText, const TFindItemSearchCase aSearchCase, 
williamr@2
   303
                                    const TInt aMinNumbers );
williamr@2
   304
williamr@2
   305
    private:
williamr@2
   306
        
williamr@2
   307
        /**
williamr@2
   308
        * Adds item to search arrays. Adding is done so that arrays are always sorted.
williamr@2
   309
        * If added element would overlap a previously found element, it is not added.
williamr@2
   310
        *
williamr@2
   311
        * @param aStartPos  Start position of the found item
williamr@2
   312
        * @param aLength    Length of found item
williamr@2
   313
        * @param aType      Type of the found item
williamr@2
   314
        */
williamr@2
   315
        void AddItemL( const TInt& aStartPos, const TInt& aLength, 
williamr@2
   316
                       const TFindItemSearchCase& aType );
williamr@2
   317
williamr@2
   318
        /**
williamr@2
   319
        * Search algorithm for searching phone numbers
williamr@2
   320
        *
williamr@2
   321
        * @param aText Text that will be parsed
williamr@2
   322
        * @return Whether any items were found
williamr@2
   323
        */
williamr@2
   324
        TBool SearchPhoneNumberL( const TDesC& aText);  
williamr@2
   325
williamr@2
   326
        /**
williamr@2
   327
        * Search algorithm for searching e-mail addresses
williamr@2
   328
        *
williamr@2
   329
        * @param aText Text that will be parsed
williamr@2
   330
        * @return Whether any items were found
williamr@2
   331
        */
williamr@2
   332
        TBool SearchMailAddressL( const TDesC& aText);  
williamr@2
   333
williamr@2
   334
        /**
williamr@2
   335
        * Search algorithm for searching generic URIs
williamr@2
   336
        *
williamr@2
   337
        * @param aText Text that will be parsed
williamr@2
   338
        * @return Whether any items were found
williamr@2
   339
        */
williamr@2
   340
        TBool SearchGenericUriL( const TDesC& aText);
williamr@2
   341
williamr@2
   342
        /**
williamr@2
   343
        * Search fixed start URLs, i.e. URLs without schema (www., wap.).
williamr@2
   344
        * Also finds IPv4 addresses (*.*.*.*).
williamr@2
   345
        * As a special case, supports deprecated hardcoded schematic addresses finding 
williamr@2
   346
        * (http://, https://, rtsp://) to make sure deprecated search cases work 
williamr@2
   347
        * as they did previously.
williamr@2
   348
        *
williamr@2
   349
        * @param aText Text that will be parsed
williamr@2
   350
        * @param aFindFixedSchemas If true, will find old fixed schematic URLs also
williamr@2
   351
        * @return Whether any items were found
williamr@2
   352
        */
williamr@2
   353
        TBool SearchUrlL( const TDesC& aText, const TBool aFindFixedSchemas);
williamr@2
   354
williamr@2
   355
        /**
williamr@2
   356
        * Parses URL from a token. Is used by SearchUrlL method and if a URL
williamr@2
   357
        * was found it's appended to item array. Note that parsing for generic URIs 
williamr@2
   358
        * is done with SearchGenericUriL -method.
williamr@2
   359
        *
williamr@2
   360
        * @param aType  a Type of URL to seach, i.e.
williamr@2
   361
        *                   www.
williamr@2
   362
        *                   wap.
williamr@2
   363
        *                   IP e.g.127.0.0.1
williamr@2
   364
        * @param        aTokenPtr Pointer to token that will be parsed
williamr@2
   365
        * @param        aTextOffset Offset of the token (start position in the whole text)
williamr@2
   366
        * @return Whether any items were found
williamr@2
   367
        */
williamr@2
   368
        TBool ParseUrlL( const TDesC& aType, const TPtrC& aTokenPtr, const TInt aTextOffset );
williamr@2
   369
williamr@2
   370
        /**
williamr@2
   371
        * Character information methods
williamr@2
   372
        *
williamr@2
   373
        * @param charac a Character to be investigated
williamr@2
   374
        * @return Whether the parameter was a valid for:
williamr@2
   375
        */
williamr@2
   376
        TBool IsValidEmailChar( const TChar& charac ) const; // Login part of the e-mail address
williamr@2
   377
        TBool IsValidEmailHostChar( const TChar& charac ) const; // Host part of the e-mail address
williamr@2
   378
        TBool IsValidPhoneNumberChar( const TChar& charac ) const; // Phone number
williamr@2
   379
        TBool IsValidUrlChar( const TChar& charac ) const; // URL
williamr@2
   380
williamr@2
   381
        /**
williamr@2
   382
        * C++ default constructor.
williamr@2
   383
        */
williamr@2
   384
        CFindItemEngine();
williamr@2
   385
williamr@2
   386
        /**
williamr@2
   387
        * Symbian OS constructor
williamr@2
   388
        *
williamr@2
   389
        * @param aText          Text that will be parsed
williamr@2
   390
        * @param aSearchCase    Identifies what items are we looking for:
williamr@2
   391
        *                           EFindItemSearchPhoneNumberBin
williamr@2
   392
        *                           EFindItemSearchMailAddressBin
williamr@2
   393
        *                           EFindItemSearchURLBin
williamr@2
   394
        *                           EFindItemSearchScheme
williamr@2
   395
        *                       Any combination of these flags can be given
williamr@2
   396
        *                       as a bit mask.
williamr@2
   397
        * @param aMinNumbers    Minimun count of numbers in a string when 
williamr@2
   398
        *                       the string is considered as a phone number.
williamr@2
   399
        */
williamr@2
   400
        void ConstructL( const TDesC& aText, const TFindItemSearchCase aSearchCase, 
williamr@2
   401
                         const TInt aMinNumbers );
williamr@2
   402
        /**
williamr@2
   403
        * Performs the search.
williamr@2
   404
        * Uses search algorithms SearchGenericUriL(), SearchMailAddressL(), 
williamr@2
   405
        * SearchUrlL() and SearchPhoneNumberL().
williamr@2
   406
        *
williamr@2
   407
        * @param aText Reference to the text to be parsed.
williamr@2
   408
        * @param aSearchCase identifies what items are we looking for.
williamr@2
   409
        */
williamr@2
   410
		void PerformSearchL( const TDesC& aText , const TFindItemSearchCase aSearchCase );
williamr@2
   411
williamr@2
   412
williamr@2
   413
        /**
williamr@2
   414
        * Converts arabic numbers to western numbers. 
williamr@2
   415
        * When returning the aBuf contains the modified text.
williamr@2
   416
        *
williamr@2
   417
        * @param aBuf A pointer to the buffer containing the text.
williamr@2
   418
        */
williamr@2
   419
		void ConvertArabicNumbersToWesternNumbers( HBufC* aBuf );
williamr@2
   420
williamr@2
   421
        /**
williamr@2
   422
        * By default, prohibit copy constructor
williamr@2
   423
        */
williamr@2
   424
        CFindItemEngine( const CFindItemEngine& );
williamr@2
   425
        /**
williamr@2
   426
        * Prohibit assigment operator
williamr@2
   427
        */
williamr@2
   428
        CFindItemEngine& operator= ( const CFindItemEngine& );
williamr@2
   429
williamr@2
   430
    private:    // Data
williamr@2
   431
        // Array of all found items.
williamr@2
   432
        CArrayFixFlat<SFoundItem>* iItemArray;
williamr@2
   433
		// Engine's position in the iItemArray and iItemTypeArray.
williamr@2
   434
        TInt iPosition;
williamr@2
   435
        // Minimum count of numbers in a phone number
williamr@2
   436
        TInt iMinNumbers;
williamr@2
   437
    };
williamr@2
   438
williamr@2
   439
#endif      // FINDITEMENGINE_H   
williamr@2
   440
            
williamr@2
   441
// End of File