phonebookengines/contactsmodel/cntplsql/src/c12keykeymap.cpp
changeset 46 efe85016a067
equal deleted inserted replaced
40:b46a585f6909 46:efe85016a067
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Retrieves the character map for each of the numeric keys.
       
    15 */
       
    16 
       
    17 // INCLUDE FILES
       
    18 #include "c12keykeymap.h"
       
    19 #include "predictivesearchlog.h"
       
    20 
       
    21 // Detection for Thai language is needed for both hardcoded and Orbit keymaps
       
    22 #include <QTextCodec>
       
    23 #include <hbinputkeymapfactory.h>
       
    24 //#include <hbinputsettingproxy.h>
       
    25 
       
    26 // How many keys have mappings in ITU-T keypad (keys 0..9, * and # have mappings)
       
    27 const TInt KAmountOfKeys = 12;
       
    28 
       
    29 // Largest amount of keypresses that can be stored in 12-key keyboard's
       
    30 // predictive search tables.
       
    31 // SQL BIGINT is a 64-bit signed integer, one bit is reserved for sign.
       
    32 // 12-key keyboard's keys are identified by TKeyId that needs 4 bits.
       
    33 // 63 / 4 = 15
       
    34 const TInt KMaxKeysStoredInDb = 15;
       
    35 
       
    36 
       
    37 // The first key of the keyboard has value zero ('1' in the 12-key virtual keypad) 
       
    38 enum TKeyId
       
    39     {
       
    40     EKey1 = 0,
       
    41     EKey2,
       
    42     EKey3,
       
    43     EKey4,
       
    44     EKey5,
       
    45     EKey6,
       
    46     EKey7,
       
    47     EKey8,
       
    48     EKey9,
       
    49 	EKey0,
       
    50 	EKeyStar,
       
    51 	EKeyHash
       
    52     };
       
    53 
       
    54 #if defined(USE_ORBIT_KEYMAP)
       
    55 const QChar KStar = '*';
       
    56 const QChar KPlus = '+';
       
    57 const QChar KHash = '#';
       
    58 #endif // #if defined(USE_ORBIT_KEYMAP)
       
    59 
       
    60 
       
    61 // * key is mapped to this
       
    62 const QChar KMappedCharForStar = 'a';
       
    63 // # key is mapped to this
       
    64 const QChar KMappedCharForHash = 'b';
       
    65 // Unmapped (unknown) characters are replaced with this
       
    66 const QChar KPadChar		   = 'f';
       
    67 
       
    68 const QChar KLowerLimitPadding = '0';
       
    69 const QChar KUpperLimitPadding = 'f';
       
    70 
       
    71 const int KLatin1 = 4; // ISO_8859-1
       
    72 const int KThaiLanguage = 2259; // Thai Industrial Standards Institute
       
    73 
       
    74 
       
    75 // ============================== MEMBER FUNCTIONS ============================
       
    76 
       
    77 // ----------------------------------------------------------------------------
       
    78 // C12keyKeyMap::NewL
       
    79 // ----------------------------------------------------------------------------
       
    80 C12keyKeyMap* C12keyKeyMap::NewL()
       
    81 	{
       
    82     PRINT(_L("Enter C12keyKeyMap::NewL"));
       
    83     
       
    84     C12keyKeyMap* self = new (ELeave) C12keyKeyMap();
       
    85     CleanupStack::PushL(self);
       
    86     self->ConstructL();
       
    87     CleanupStack::Pop(self);
       
    88 
       
    89     PRINT(_L("End C12keyKeyMap::NewL"));
       
    90     return self;
       
    91 	}
       
    92 
       
    93 // ----------------------------------------------------------------------------
       
    94 // C12keyKeyMap::~C12keyKeyMap
       
    95 // QTextCodec's destructor is protected, so it is not intended to be deleted.
       
    96 // ----------------------------------------------------------------------------
       
    97 C12keyKeyMap::~C12keyKeyMap()
       
    98     {
       
    99     PRINT(_L("Enter C12keyKeyMap::~C12keyKeyMap"));
       
   100     PRINT(_L("End C12keyKeyMap::~C12keyKeyMap"));
       
   101     }
       
   102 
       
   103 // ----------------------------------------------------------------------------
       
   104 // C12keyKeyMap::ArrayIndexToMappedChar
       
   105 // Map index of iKeyMapping list, to the number char that the mapping is for.
       
   106 // ----------------------------------------------------------------------------
       
   107 const QChar C12keyKeyMap::ArrayIndexToMappedChar(TInt aArrayIndex) const
       
   108     {
       
   109     __ASSERT_DEBUG(aArrayIndex < KAmountOfKeys,
       
   110                    User::Panic(_L("C12keyKeyMap::ArrayIndexToMappedChar"),
       
   111                    KErrOverflow));
       
   112     switch (aArrayIndex)
       
   113         {
       
   114         case EKey0:
       
   115             return '0';
       
   116         case EKeyStar:
       
   117             return KMappedCharForStar;
       
   118         case EKeyHash:
       
   119             return KMappedCharForHash;
       
   120         default:
       
   121             return aArrayIndex + '1';
       
   122         }
       
   123     }
       
   124 
       
   125 #if !defined(USE_ORBIT_KEYMAP)
       
   126 // ----------------------------------------------------------------------------
       
   127 // C12keyKeyMap::UseHardcodedKeyMap
       
   128 // ----------------------------------------------------------------------------
       
   129 QChar C12keyKeyMap::UseHardcodedKeyMap(QChar input) const
       
   130     {
       
   131 	QChar ret = '0';
       
   132     switch (input.unicode())
       
   133         {
       
   134 #if defined(THAI_KEYMAP)
       
   135 		case 0x21B2:
       
   136 			ret = '0';
       
   137 			break;
       
   138 
       
   139 		case 0x0E01:
       
   140 		case 0x0E02:
       
   141 		case 0x0E03:
       
   142 		case 0x0E04:
       
   143 		case 0x0E05:	
       
   144 #endif
       
   145 		case '-':
       
   146 		case '?':
       
   147 		    ret = '1';
       
   148 		    break;
       
   149 
       
   150         case 'a':
       
   151         case 'A':
       
   152         case 'b':
       
   153         case 'B':
       
   154         case 'c':
       
   155         case 'C':
       
   156 #if defined(THAI_KEYMAP)
       
   157 		case 0x0E06:
       
   158 		case 0x0E07:
       
   159 		case 0x0E08:
       
   160 		case 0x0E09:
       
   161 #endif
       
   162             ret = '2';
       
   163             break;
       
   164 
       
   165 		case 'd':
       
   166         case 'D':
       
   167         case 'e':
       
   168         case 'E':
       
   169         case 'f':
       
   170         case 'F':
       
   171 #if defined(THAI_KEYMAP)
       
   172 		case 0x0E0A:
       
   173 		case 0x0E0B:
       
   174 		case 0x0E0C:
       
   175 		case 0x0E0D:
       
   176 #endif
       
   177             ret = '3';
       
   178             break;
       
   179             
       
   180 		case 'g':
       
   181         case 'G':
       
   182         case 'h':
       
   183         case 'H':
       
   184         case 'i':
       
   185         case 'I':
       
   186 #if defined(THAI_KEYMAP)
       
   187 		case 0x0E0E:
       
   188 		case 0x0E0F:
       
   189 		case 0x0E10:
       
   190 		case 0x0E11:
       
   191 		case 0x0E12:
       
   192 		case 0x0E13:
       
   193 #endif
       
   194             ret = '4';
       
   195             break;
       
   196 
       
   197 		case 'j':
       
   198         case 'J':
       
   199         case 'k':
       
   200         case 'K':
       
   201         case 'l':
       
   202         case 'L':
       
   203 #if defined(THAI_KEYMAP)
       
   204 		case 0x0E14:
       
   205 		case 0x0E15:
       
   206 		case 0x0E16:
       
   207 		case 0x0E17:
       
   208 		case 0x0E18:
       
   209 #endif
       
   210             ret = '5';
       
   211             break;
       
   212 
       
   213 		case 'm':
       
   214         case 'M':
       
   215         case 'n':
       
   216         case 'N':
       
   217         case 'o':
       
   218         case 'O':
       
   219 #if defined(THAI_KEYMAP)
       
   220 		case 0x0E19:
       
   221 		case 0x0E1A:
       
   222 		case 0x0E1B:
       
   223 		case 0x0E1C:
       
   224 		case 0x0E1D:
       
   225 #endif
       
   226             ret = '6';
       
   227             break;
       
   228       
       
   229 		case 'p':
       
   230         case 'P':
       
   231         case 'q':
       
   232         case 'Q':
       
   233         case 'r':
       
   234         case 'R':
       
   235         case 's':
       
   236         case 'S':
       
   237 #if defined(THAI_KEYMAP)
       
   238 		case 0x0E1E:
       
   239 		case 0x0E1F:
       
   240 		case 0x0E20:
       
   241 		case 0x0E21:
       
   242 		case 0x0E22:
       
   243 #endif
       
   244             ret = '7';
       
   245             break;
       
   246 
       
   247 		case 't':
       
   248         case 'T':
       
   249         case 'u':
       
   250         case 'U':
       
   251         case 'v':
       
   252         case 'V':
       
   253 #if defined(THAI_KEYMAP)
       
   254 		case 0x0E23:
       
   255 		case 0x0E24:
       
   256 		case 0x0E25:
       
   257 		case 0x0E26:
       
   258 		case 0x0E27:
       
   259 		case 0x0E28:
       
   260 		case 0x0E29:
       
   261 #endif
       
   262             ret = '8';
       
   263             break;
       
   264 
       
   265         case 'w':
       
   266         case 'W':
       
   267         case 'X':
       
   268         case 'x':
       
   269         case 'Y':
       
   270         case 'y':
       
   271         case 'z':
       
   272         case 'Z':
       
   273 #if defined(THAI_KEYMAP)
       
   274 		case 0x0E2A:
       
   275 		case 0x0E2B:
       
   276 		case 0x0E2C:
       
   277 		case 0x0E2D:
       
   278 		case 0x0E2E:
       
   279 #endif
       
   280             ret = '9';
       
   281             break;
       
   282 
       
   283 		case '*':
       
   284 		case '+':
       
   285 #if defined(THAI_KEYMAP)
       
   286 		case 0x0E2F:
       
   287 		case 0x0E3F:
       
   288 		case 0x0E31:
       
   289 		case 0x0E34:
       
   290 		case 0x0E35:
       
   291 		case 0x0E36:
       
   292 		case 0x0E37:
       
   293 		case 0x0E38:
       
   294 		case 0x0E39:
       
   295 		case 0x0E46:
       
   296 		case 0x0E47:
       
   297 		case 0x0E48:
       
   298 		case 0x0E49:
       
   299 		case 0x0E4A:
       
   300 		case 0x0E4B:
       
   301 		case 0x0E4C:
       
   302 #endif
       
   303 		    ret = KMappedCharForStar;
       
   304 		    break;
       
   305 		    
       
   306 		case '#':
       
   307 #if defined(THAI_KEYMAP)
       
   308 		case 0x0E30:
       
   309 		case 0x0E32:
       
   310 		case 0x0E33:
       
   311 		case 0x0E40:
       
   312 		case 0x0E41:
       
   313 		case 0x0E42:
       
   314 		case 0x0E43:
       
   315 		case 0x0E44:
       
   316 #endif
       
   317 		    ret = KMappedCharForHash;
       
   318 		    break;
       
   319 
       
   320 		case '0':
       
   321 		case '1':
       
   322 		case '2':
       
   323 		case '3':
       
   324 		case '4':
       
   325         case '5':
       
   326         case '6':
       
   327         case '7':
       
   328         case '8':
       
   329         case '9':
       
   330         case ' ':
       
   331             ret = input; // Numbers and space
       
   332             break;
       
   333 
       
   334 		default: // Other (unknown) chars
       
   335 		    ret = KPadChar;
       
   336         }
       
   337     return ret;
       
   338     }
       
   339 #endif // #if !defined(USE_ORBIT_KEYMAP)
       
   340 
       
   341 // ----------------------------------------------------------------------------
       
   342 // C12keyKeyMap::ComputeValue
       
   343 // ----------------------------------------------------------------------------
       
   344 TInt C12keyKeyMap::ComputeValue(QString aString,
       
   345 							    TBool aUpperLimit,
       
   346 							    QString& aValue) const
       
   347 	{
       
   348 	QString hexString;
       
   349 	if (aString.length() < KMaxKeysStoredInDb)
       
   350 		{
       
   351 		QChar pad = KLowerLimitPadding;
       
   352 		if (aUpperLimit)
       
   353 			{
       
   354 			pad = KUpperLimitPadding;
       
   355 			}
       
   356 		hexString = aString.leftJustified(KMaxKeysStoredInDb, pad);
       
   357 		}
       
   358 	else
       
   359 		{
       
   360 		hexString = aString;
       
   361 		}
       
   362     
       
   363     const TInt KHexadecimalBase = 16;
       
   364     bool ok(true);
       
   365     // Use signed int to prevent underflow when padded is "00...00"
       
   366     qint64 value = hexString.toLongLong(&ok, KHexadecimalBase); 
       
   367     if (!ok)
       
   368         {
       
   369 		PRINT(_L("CPcsKeyMap::ComputeValue fails"));
       
   370 		return KErrArgument;
       
   371         }
       
   372 
       
   373 	// In order to write queries using '>' and '<' instead of '>=' and '<=',
       
   374 	// expand the limit by one.
       
   375 	if (aUpperLimit)
       
   376 		{
       
   377 		++value;
       
   378 		}
       
   379 	else
       
   380 		{
       
   381 		--value;
       
   382 		}
       
   383 
       
   384     aValue = QString::number(value); // Convert to decimal value
       
   385 	return KErrNone;
       
   386 	}
       
   387 
       
   388 // ----------------------------------------------------------------------------
       
   389 // C12keyKeyMap::SetHardcodedCharacters
       
   390 // In emulator (except in unit tests), select just the default language, as new
       
   391 // SDKs have over 40 languages, causing out of memory error.
       
   392 // In hardware and when compiling unit tests in emulator, select all languages
       
   393 // available in the device. Reason: it is though that HW does not come with
       
   394 // dozens of languages and some unit test cases require Thai keymap.
       
   395 // ----------------------------------------------------------------------------
       
   396 QList<HbInputLanguage> C12keyKeyMap::SelectLanguages()
       
   397 	{
       
   398 #if defined(__WINSCW__) && !defined(EUNIT_UNIT_TEST_COVERAGE)
       
   399 	return CPcsKeyMap::SelectLanguages();
       
   400 #else
       
   401 	return HbKeymapFactory::availableLanguages();
       
   402 #endif
       
   403 	}
       
   404 
       
   405 #if defined(USE_ORBIT_KEYMAP)
       
   406 // ----------------------------------------------------------------------------
       
   407 // C12keyKeyMap::SetHardcodedCharacters
       
   408 // Even though most languages map *, + and # to 1-key, they are here mapped to
       
   409 // the distinct *-key or #-key of the 12-key ITU-T keypad.
       
   410 // ----------------------------------------------------------------------------
       
   411 void C12keyKeyMap::SetHardcodedCharacters()
       
   412 	{
       
   413 	iKeyMapping[EKeyStar].append(KStar);
       
   414 	iKeyMapping[EKeyStar].append(KPlus);
       
   415 	iKeyMapping[EKeyHash].append(KHash);
       
   416 	iHardcodedChars.append(KStar);
       
   417 	iHardcodedChars.append(KPlus);
       
   418 	iHardcodedChars.append(KHash);
       
   419 	}
       
   420 #endif // #if defined(USE_ORBIT_KEYMAP)
       
   421 
       
   422 // ----------------------------------------------------------------------------
       
   423 // C12keyKeyMap::DetermineSpecialCharBehaviour
       
   424 // If input string is Thai language, skip chars that map to star or hash keys.
       
   425 // ----------------------------------------------------------------------------
       
   426 TBool C12keyKeyMap::DetermineSpecialCharBehaviour(QString aSource) const
       
   427 	{
       
   428 	/* this code would use the current input language to determine if Thai is in use
       
   429 	HbInputLanguage lang = HbInputSettingProxy::instance()->globalInputLanguage();
       
   430     TBool skipHashStar = lang.language() == QLocale::Thai; */
       
   431 
       
   432 	if (iLatinCodec && iLatinCodec->canEncode(aSource))
       
   433 		{
       
   434 		return EFalse;
       
   435 		}
       
   436     return iThaiCodec && iThaiCodec->canEncode(aSource);
       
   437 	}
       
   438 
       
   439 // ----------------------------------------------------------------------------
       
   440 // C12keyKeyMap::ShouldSkipChar
       
   441 // Characters mapped to certain keys (*,#) are skipped for certain languages.
       
   442 // ----------------------------------------------------------------------------
       
   443 TBool C12keyKeyMap::ShouldSkipChar(QChar aChar, TBool aSkipHashStar) const
       
   444 	{
       
   445 	return aSkipHashStar &&
       
   446 		   (aChar == KMappedCharForStar || aChar == KMappedCharForHash);
       
   447 	}
       
   448 
       
   449 // ----------------------------------------------------------------------------
       
   450 // C12keyKeyMap::C12keyKeyMap
       
   451 // Fill QList with empty strings
       
   452 // ----------------------------------------------------------------------------
       
   453 C12keyKeyMap::C12keyKeyMap() :
       
   454 	CPcsKeyMap(KAmountOfKeys, KPadChar, KMaxKeysStoredInDb)
       
   455 	{
       
   456 	}
       
   457 
       
   458 // ----------------------------------------------------------------------------
       
   459 // C12keyKeyMap::ConstructL
       
   460 // ----------------------------------------------------------------------------
       
   461 void C12keyKeyMap::ConstructL()
       
   462 	{
       
   463 	PRINT(_L("Enter C12keyKeyMap::ConstructL"));
       
   464 
       
   465 #if defined(USE_ORBIT_KEYMAP)
       
   466 	CPcsKeyMap::ConstructL(HbKeyboardVirtual12Key);
       
   467 #else
       
   468 	CPcsKeyMap::ConstructL();
       
   469 #endif
       
   470 
       
   471 	TInt err(KErrNone);
       
   472 	QT_TRYCATCH_ERROR(err, GetTextCodecs());
       
   473     if (err != KErrNone)
       
   474         {
       
   475         PRINT1(_L("C12keyKeyMap::ConstructL exception, err=%d"), err);
       
   476         User::Leave(err);
       
   477         }
       
   478 
       
   479 	PRINT(_L("End C12keyKeyMap::ConstructL"));
       
   480 	}
       
   481 
       
   482 // ----------------------------------------------------------------------------
       
   483 // C12keyKeyMap::GetTextCodecs
       
   484 // ----------------------------------------------------------------------------
       
   485 void C12keyKeyMap::GetTextCodecs()
       
   486     {
       
   487     iLatinCodec = QTextCodec::codecForMib(KLatin1);
       
   488     iThaiCodec = QTextCodec::codecForMib(KThaiLanguage);
       
   489     }
       
   490 
       
   491 // End of file