plugins/contacts/symbian/contactsmodel/cntplsql/src/cpcskeymap.cpp
changeset 5 603d3f8b6302
parent 0 876b1a06bc25
equal deleted inserted replaced
3:e4ebb16b39ea 5:603d3f8b6302
    16 
    16 
    17 // INCLUDE FILES
    17 // INCLUDE FILES
    18 #include "cpcskeymap.h"
    18 #include "cpcskeymap.h"
    19 #include <QChar>
    19 #include <QChar>
    20 #include <QString>
    20 #include <QString>
    21 
       
    22 #if defined(USE_ORBIT_KEYMAP)
       
    23 #include <hbinputkeymap.h>
    21 #include <hbinputkeymap.h>
    24 #include <hbinputkeymapfactory.h>
    22 #include <hbinputkeymapfactory.h>
    25 #endif // #if defined(USE_ORBIT_KEYMAP)
       
    26 
    23 
    27 // This macro suppresses log writes
    24 // This macro suppresses log writes
    28 //#define NO_PRED_SEARCH_LOGS
    25 //#define NO_PRED_SEARCH_LOGS
    29 #include "predictivesearchlog.h"
    26 #include "predictivesearchlog.h"
    30 
    27 
    31 const QChar KSpaceChar = ' ';
    28 const QChar KSpaceChar = ' ';
    32 
    29 
    33 // Separator character stored in predictive search table columns
    30 // Separator character stored in predictive search table columns
    34 const QChar KSeparatorChar = ' ';
    31 const QChar KSeparatorChar = ' ';
       
    32 
       
    33 // Code using the new API (wk32 onwards) is put here. Remove old API code
       
    34 // when wk30 is no longer used.
       
    35 // #define NEW_KEYMAP_FACTORY_API
    35 
    36 
    36 
    37 
    37 // ============================== MEMBER FUNCTIONS ============================
    38 // ============================== MEMBER FUNCTIONS ============================
    38 
    39 
    39 // ----------------------------------------------------------------------------
    40 // ----------------------------------------------------------------------------
    87             destination.append(KSeparatorChar);
    88             destination.append(KSeparatorChar);
    88             }
    89             }
    89         else
    90         else
    90 			{
    91 			{
    91 			QChar ch(0);
    92 			QChar ch(0);
    92 #if defined(USE_ORBIT_KEYMAP)
       
    93             ch = MappedKeyForChar(aSource[i]);
    93             ch = MappedKeyForChar(aSource[i]);
    94 #else
       
    95             ch = UseHardcodedKeyMap(aSource[i]);            
       
    96 #endif
       
    97 			if (!ShouldSkipChar(ch, skipHashStar))
    94 			if (!ShouldSkipChar(ch, skipHashStar))
    98 				{
    95 				{
    99 				destination.append(ch);
    96 				destination.append(ch);
   100 				}
    97 				}
   101 			}
    98 			}
   132 		}
   129 		}
   133 	PRINT1(_L("End CPcsKeyMap::GetNumericLimits ret=%d"), err);
   130 	PRINT1(_L("End CPcsKeyMap::GetNumericLimits ret=%d"), err);
   134 	return err;
   131 	return err;
   135 	}
   132 	}
   136 
   133 
   137 #if defined(USE_ORBIT_KEYMAP)
       
   138 // ----------------------------------------------------------------------------
   134 // ----------------------------------------------------------------------------
   139 // CPcsKeyMap::Separator
   135 // CPcsKeyMap::Separator
   140 // ----------------------------------------------------------------------------
   136 // ----------------------------------------------------------------------------
   141 QChar CPcsKeyMap::Separator() const
   137 QChar CPcsKeyMap::Separator() const
   142     {
   138     {
   160 // Default implementation does nothing
   156 // Default implementation does nothing
   161 // ----------------------------------------------------------------------------
   157 // ----------------------------------------------------------------------------
   162 void CPcsKeyMap::SetHardcodedCharacters()
   158 void CPcsKeyMap::SetHardcodedCharacters()
   163 	{
   159 	{
   164 	}
   160 	}
   165 #endif // #if defined(USE_ORBIT_KEYMAP)
       
   166 
   161 
   167 // ----------------------------------------------------------------------------
   162 // ----------------------------------------------------------------------------
   168 // CPcsKeyMap::DetermineSpecialCharBehaviour
   163 // CPcsKeyMap::DetermineSpecialCharBehaviour
   169 // Default implementation
   164 // Default implementation
   170 // ----------------------------------------------------------------------------
   165 // ----------------------------------------------------------------------------
   183 	}
   178 	}
   184 
   179 
   185 // ----------------------------------------------------------------------------
   180 // ----------------------------------------------------------------------------
   186 // CPcsKeyMap::ConstructL
   181 // CPcsKeyMap::ConstructL
   187 // ----------------------------------------------------------------------------
   182 // ----------------------------------------------------------------------------
   188 #if defined(USE_ORBIT_KEYMAP)
       
   189 void CPcsKeyMap::ConstructL(HbKeyboardType aKeyboardType)
   183 void CPcsKeyMap::ConstructL(HbKeyboardType aKeyboardType)
   190 #else
       
   191 void CPcsKeyMap::ConstructL()
       
   192 #endif
       
   193 	{
   184 	{
   194 	PRINT(_L("Enter CPcsKeyMap::ConstructL"));
   185 	PRINT(_L("Enter CPcsKeyMap::ConstructL"));
   195 
   186 
   196 #if defined(USE_ORBIT_KEYMAP)
       
   197 	TInt err(KErrNone);
   187 	TInt err(KErrNone);
   198 	QT_TRYCATCH_ERROR(err,
   188 	QT_TRYCATCH_ERROR(err,
   199 		{
   189 		{
   200 		InitKeyMappings();
   190 		InitKeyMappings();
   201 		SetHardcodedCharacters();
   191 		SetHardcodedCharacters();
   204 	if (err != KErrNone)
   194 	if (err != KErrNone)
   205         {
   195         {
   206         PRINT1(_L("CPcsKeyMap::ConstructL exception, err=%d"), err);
   196         PRINT1(_L("CPcsKeyMap::ConstructL exception, err=%d"), err);
   207         User::Leave(err);
   197         User::Leave(err);
   208         }
   198         }
   209 #endif
       
   210 
   199 
   211 	PRINT(_L("End CPcsKeyMap::ConstructL"));
   200 	PRINT(_L("End CPcsKeyMap::ConstructL"));
   212 	}
   201 	}
   213 
   202 
   214 // ----------------------------------------------------------------------------
   203 // ----------------------------------------------------------------------------
   215 // CPcsKeyMap::CPcsKeyMap
   204 // CPcsKeyMap::CPcsKeyMap
   216 // ----------------------------------------------------------------------------
   205 // ----------------------------------------------------------------------------
   217 #if defined(USE_ORBIT_KEYMAP)
       
   218 CPcsKeyMap::CPcsKeyMap(TInt aAmountOfKeys,
   206 CPcsKeyMap::CPcsKeyMap(TInt aAmountOfKeys,
   219 					   QChar aPadChar,
   207 					   QChar aPadChar,
   220 					   TInt aMaxKeysStoredInDb) :
   208 					   TInt aMaxKeysStoredInDb) :
   221 	iKeyMapping(),
   209 	iKeyMapping(),
   222 	iAmountOfKeys(aAmountOfKeys),
   210 	iAmountOfKeys(aAmountOfKeys),
   223 	iPadChar(aPadChar),
   211 	iPadChar(aPadChar),
   224 	iMaxKeysStoredInDb(aMaxKeysStoredInDb)
   212 	iMaxKeysStoredInDb(aMaxKeysStoredInDb)
   225 	{
   213 	{
   226 	}
   214 	}
   227 #else // #if defined(USE_ORBIT_KEYMAP)
   215 
   228 CPcsKeyMap::CPcsKeyMap(TInt /*aAmountOfKeys*/,
       
   229 					   QChar /*aPadChar*/,
       
   230 					   TInt aMaxKeysStoredInDb) :
       
   231 	iMaxKeysStoredInDb(aMaxKeysStoredInDb)
       
   232 	{
       
   233 	}
       
   234 #endif // #if defined(USE_ORBIT_KEYMAP)
       
   235 
       
   236 #if defined(USE_ORBIT_KEYMAP)
       
   237 // ----------------------------------------------------------------------------
   216 // ----------------------------------------------------------------------------
   238 // CPcsKeyMap::InitKeyMappings
   217 // CPcsKeyMap::InitKeyMappings
   239 // Put string for each key into iKeyMapping.
   218 // Put string for each key into iKeyMapping.
   240 // ----------------------------------------------------------------------------
   219 // ----------------------------------------------------------------------------
   241 void CPcsKeyMap::InitKeyMappings()
   220 void CPcsKeyMap::InitKeyMappings()
   249 	}
   228 	}
   250 
   229 
   251 // ----------------------------------------------------------------------------
   230 // ----------------------------------------------------------------------------
   252 // CPcsKeyMap::ConstructLanguageMappings
   231 // CPcsKeyMap::ConstructLanguageMappings
   253 // Fetch keymap for selected languages.
   232 // Fetch keymap for selected languages.
   254 // ----------------------------------------------------------------------------
   233 // Currently QWERTY keymaps do not map digits ('0'..'9') to any key, even with
   255 void CPcsKeyMap::ConstructLanguageMappings(HbKeyboardType aKeyboardType) 
   234 // HbModifierChrPressed and HbModifierFnPressed modifiers.
       
   235 // ----------------------------------------------------------------------------
       
   236 void CPcsKeyMap::ConstructLanguageMappings(HbKeyboardType aKeyboardType)
   256 	{
   237 	{
   257     PRINT(_L("Enter CPcsKeyMap::ConstructLanguageMappings"));
   238     PRINT(_L("Enter CPcsKeyMap::ConstructLanguageMappings"));
   258 
   239 
   259 #if defined(WRITE_PRED_SEARCH_LOGS)
   240 #if defined(WRITE_PRED_SEARCH_LOGS)
   260     TInt count(0);
   241     TInt count(0);
   262 
   243 
   263 	QList<HbInputLanguage> languages = SelectLanguages();
   244 	QList<HbInputLanguage> languages = SelectLanguages();
   264     PRINT1(_L("build keymap from %d language(s)"), languages.count());
   245     PRINT1(_L("build keymap from %d language(s)"), languages.count());
   265 
   246 
   266 	TInt languageCount = languages.size();
   247 	TInt languageCount = languages.size();
       
   248 #if !defined(NEW_KEYMAP_FACTORY_API)
       
   249 	// Latest SDKs have so many keymaps contact server runs out of stack.
       
   250 	// So limit the amount of keymaps.
       
   251 	const TInt KMaxKeymapCount = 10;
       
   252 	if (languageCount > KMaxKeymapCount)
       
   253 	    {
       
   254         languageCount = KMaxKeymapCount;
       
   255 	    }
       
   256 #endif
       
   257 
   267 	for (TInt lang = 0; lang < languageCount; ++lang)
   258 	for (TInt lang = 0; lang < languageCount; ++lang)
   268 		{
   259 		{
   269         PRINT2(_L("(%d) handle language %d"), lang, languages[lang].language());
   260         PRINT2(_L("(%d) handle language %d"), lang, languages[lang].language());
   270 		if (IsLanguageSupported(languages[lang].language()))
   261 		if (IsLanguageSupported(languages[lang].language()))
   271 			{
   262 			{
   272 			PRINT2(_L("Constructing keymap for lang=%d,var=%d"),
   263 			PRINT2(_L("Constructing keymap for lang=%d,var=%d"),
   273 				   languages[lang].language(),
   264 				   languages[lang].language(),
   274 				   languages[lang].variant());
   265 				   languages[lang].variant());
       
   266 #if defined(NEW_KEYMAP_FACTORY_API)
       
   267 			// Gets ownership of keymap
       
   268 			const HbKeymap* keymap =
       
   269 			    HbKeymapFactory::instance()->keymap(languages[lang],  
       
   270                                                     HbKeymapFactory::NoCaching);
       
   271 #else
       
   272 			// Does not get ownership of keymap
   275 			const HbKeymap* keymap =
   273 			const HbKeymap* keymap =
   276 				HbKeymapFactory::instance()->keymap(languages[lang].language(),
   274 				HbKeymapFactory::instance()->keymap(languages[lang].language(),
   277                                                     languages[lang].variant());
   275                                                     languages[lang].variant());
       
   276 #endif
   278 			if (keymap)
   277 			if (keymap)
   279 			    {
   278 			    {
   280 				for (TInt key = 0; key < iAmountOfKeys; ++key)
   279 #if defined(WRITE_PRED_SEARCH_LOGS)
   281                     {
   280                 count +=
   282                     PRINT1(_L("handle key(enum value %d)"), key); // test
   281 #endif
   283                     const HbMappedKey* mappedKey = keymap->keyForIndex(aKeyboardType, key);
   282                 ReadKeymapCharacters(aKeyboardType, *keymap);
   284 					// 12-key: Most languages don't have mapping for EKeyStar, EKeyHash.
   283 				
   285 					// QWERTY: Different languages have different amount of keys,
   284 #if defined(NEW_KEYMAP_FACTORY_API)
   286 					// so mappedKey can be NULL.
   285 				delete keymap;
   287                     if (mappedKey)
   286 #endif
   288                         {
       
   289 						const QString lowerCase = mappedKey->characters(HbModifierNone); // "abc2.."
       
   290 						const QString upperCase = mappedKey->characters(HbModifierShiftPressed); // "ABC2.."
       
   291 						const QString charsForKey = lowerCase + upperCase; 
       
   292 	    
       
   293 						// Filter out duplicate characters
       
   294 						for (TInt i = charsForKey.length() - 1; i >= 0 ; --i) 
       
   295 							{
       
   296 							QChar ch = charsForKey[i];
       
   297 							if (!iKeyMapping[key].contains(ch) &&
       
   298 								!iHardcodedChars.contains(ch))
       
   299 								{
       
   300 #if defined(WRITE_PRED_SEARCH_LOGS)
       
   301 								char ascChar = ch.toAscii();
       
   302 								TChar logChar(ArrayIndexToMappedChar(key).unicode());
       
   303 	
       
   304 								if (ascChar == 0) // ch can't be represented in ASCII
       
   305 									{
       
   306 									PRINT2(_L("CPcsKeyMap: map key(%c) <-> char=0x%x"),
       
   307 									       logChar, ch);
       
   308 									}
       
   309 								else
       
   310 									{
       
   311 									PRINT3(_L("CPcsKeyMap: map key(%c) <-> char='%c'(0x%x)"),
       
   312 										   logChar,
       
   313 										   ascChar,
       
   314 										   ascChar);
       
   315 									}
       
   316 								++count;
       
   317 #endif // #if defined(WRITE_PRED_SEARCH_LOGS)
       
   318 								iKeyMapping[key] += ch;
       
   319 								}
       
   320 							}
       
   321 						}
       
   322                     }
       
   323 			    }
   287 			    }
   324 			else
   288 			else
   325                 {
   289                 {
   326                 PRINT(_L("CPcsKeyMap::ContructKeyboardMapping keymap not found"));
   290                 PRINT(_L("CPcsKeyMap::ContructKeyboardMapping keymap not found"));
   327                 }
   291                 }
   361 	PRINT2(_L("CPcsKeyMap::MappedKeyForChar no mapping for char '%c' (0x%x)"),
   325 	PRINT2(_L("CPcsKeyMap::MappedKeyForChar no mapping for char '%c' (0x%x)"),
   362 		   ch, ch);
   326 		   ch, ch);
   363 #endif
   327 #endif
   364 	return iPadChar;
   328 	return iPadChar;
   365     }
   329     }
   366 #endif // #if defined(USE_ORBIT_KEYMAP)
   330 
       
   331 // ----------------------------------------------------------------------------
       
   332 // CPcsKeyMap::ReadKeymapCharacters
       
   333 // ----------------------------------------------------------------------------
       
   334 TInt CPcsKeyMap::ReadKeymapCharacters(HbKeyboardType aKeyboardType,
       
   335                                       const HbKeymap& aKeymap)
       
   336     {
       
   337     PRINT(_L("Enter CPcsKeyMap::ReadKeymapCharacters"));
       
   338 
       
   339     TInt count(0);
       
   340 
       
   341     for (TInt key = 0; key < iAmountOfKeys; ++key)
       
   342         {
       
   343         PRINT1(_L("handle key(enum value %d)"), key); // test
       
   344         const HbMappedKey* mappedKey = aKeymap.keyForIndex(aKeyboardType, key);
       
   345         // 12-key: Most languages don't have mapping for EKeyStar, EKeyHash.
       
   346         // QWERTY: Different languages have different amount of keys,
       
   347         // so mappedKey can be NULL.
       
   348         if (mappedKey)
       
   349             {
       
   350             const QString lowerCase = mappedKey->characters(HbModifierNone); // "abc2.."
       
   351             const QString upperCase = mappedKey->characters(HbModifierShiftPressed); // "ABC2.."                        
       
   352             const QString charsForKey = lowerCase + upperCase; 
       
   353 
       
   354             // Filter out duplicate characters
       
   355             for (TInt i = charsForKey.length() - 1; i >= 0 ; --i) 
       
   356                 {
       
   357                 QChar ch = charsForKey[i];
       
   358                 if (!iKeyMapping[key].contains(ch) &&
       
   359                     !iHardcodedChars.contains(ch))
       
   360                     {
       
   361 #if defined(WRITE_PRED_SEARCH_LOGS)
       
   362                     char ascChar = ch.toAscii();
       
   363                     TChar logChar(ArrayIndexToMappedChar(key).unicode());
       
   364 
       
   365                     if (ascChar == 0) // ch can't be represented in ASCII
       
   366                         {
       
   367                         PRINT2(_L("CPcsKeyMap: map key(%c) <-> char=0x%x"),
       
   368                                logChar, ch);
       
   369                         }
       
   370                     else
       
   371                         {
       
   372                         PRINT3(_L("CPcsKeyMap: map key(%c) <-> char='%c'(0x%x)"),
       
   373                                logChar,
       
   374                                ascChar,
       
   375                                ascChar);
       
   376                         }
       
   377                     ++count;
       
   378 #endif // #if defined(WRITE_PRED_SEARCH_LOGS)
       
   379                     iKeyMapping[key] += ch;
       
   380                     }
       
   381                 }
       
   382             }
       
   383         }
       
   384     
       
   385     PRINT(_L("End CPcsKeyMap::ReadKeymapCharacters"));
       
   386     return count;
       
   387     }
   367 
   388 
   368 // End of file
   389 // End of file