logsui/logscntfinder/src/logspredictivetranslator.cpp
changeset 0 4a5361db8937
child 2 7119b73b84d6
equal deleted inserted replaced
-1:000000000000 0:4a5361db8937
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 #include <QObject>
       
    18 #include <QLocale>
       
    19 #include <hbinputkeymapfactory.h>
       
    20 #include <hbinputkeymap.h>
       
    21 #include <hbinputsettingproxy.h>
       
    22 
       
    23 #include "logspredictivetranslator.h"
       
    24 #include "logslogger.h"
       
    25 
       
    26 LogsPredictiveTranslator* LogsPredictiveTranslator::mInstance = 0;
       
    27 
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // LogsPredictiveTranslator::instance()
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 LogsPredictiveTranslator* LogsPredictiveTranslator::instance()
       
    34 {
       
    35     if ( !mInstance ) {
       
    36         mInstance = new LogsPredictiveTranslator();
       
    37     }
       
    38     return mInstance;
       
    39 }
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // LogsPredictiveTranslator::deleteInstance()
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 void LogsPredictiveTranslator::deleteInstance()
       
    46 {
       
    47     delete mInstance;
       
    48     mInstance = 0;
       
    49 }
       
    50 
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // LogsPredictiveTranslator::LogsPredictiveTranslator()
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 LogsPredictiveTranslator::LogsPredictiveTranslator()
       
    57 {
       
    58     LOGS_QDEBUG( "logs [FINDER] -> LogsPredictiveTranslator::\
       
    59 LogsPredictiveTranslator()" )
       
    60     HbInputLanguage lang = 
       
    61             HbInputSettingProxy::instance()->globalInputLanguage();
       
    62     mKeyMap = HbKeymapFactory::instance()->keymap( lang.language(), 
       
    63                                                    lang.variant() );
       
    64     
       
    65     LOGS_QDEBUG( "logs [FINDER] <- LogsPredictiveTranslator::\
       
    66 LogsPredictiveTranslator()" )
       
    67 }
       
    68 
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // LogsPredictiveTranslator::~LogsPredictiveTranslator()
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 LogsPredictiveTranslator::~LogsPredictiveTranslator()
       
    75 {
       
    76     LOGS_QDEBUG( "logs [FINDER] -> LogsPredictiveTranslator::\
       
    77 ~LogsPredictiveTranslator()" )
       
    78     mInstance = 0;
       
    79     LOGS_QDEBUG( "logs [FINDER] <- LogsPredictiveTranslator::\
       
    80 ~LogsPredictiveTranslator()" )
       
    81     
       
    82 }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // LogsPredictiveTranslator::translate()
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 const QChar LogsPredictiveTranslator::translate( const QChar character ) const
       
    89 {
       
    90     const HbMappedKey* mappedKey = mKeyMap->keyForCharacter( HbKeyboardVirtual12Key, 
       
    91                                      character );
       
    92     if (!mappedKey) {
       
    93         QString decomposed = character.decomposition();
       
    94         if (decomposed.isEmpty()) {
       
    95             return character;
       
    96         }
       
    97         return translate (decomposed.at(0));
       
    98     }
       
    99     return mappedKey->keycode;
       
   100 }
       
   101 
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // LogsPredictiveTranslator::translate()
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 const QString LogsPredictiveTranslator::translate( const QString& name, 
       
   108                                                    int count ) const
       
   109 {
       
   110     LOGS_QDEBUG( "logs [FINDER] -> LogsPredictiveTranslator::translate()" )
       
   111     LOGS_QDEBUG_2( "logs [FINDER] name ", name );
       
   112     
       
   113     count = count == -1 ? name.length() : count;
       
   114     QString result;
       
   115     const QChar* content = name.data();
       
   116     int index = 0;
       
   117     while( index < name.length() && index < count ) {
       
   118         result.insert( index++, translate( *content++ ) );
       
   119     }
       
   120     
       
   121     LOGS_QDEBUG( "logs [FINDER] <- LogsPredictiveTranslator::translate()" )
       
   122     return result;
       
   123 }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // LogsPredictiveTranslator::startsWith()
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 int LogsPredictiveTranslator::startsWith( const QString& text, 
       
   130                                           const QString& pattern,
       
   131                                           bool optimize ) const
       
   132 {
       
   133     LOGS_QDEBUG( "logs [FINDER] -> LogsPredictiveTranslator::startsWith()" )
       
   134     //assumed that text has found based on pattern, thus only checking with
       
   135     //first char is enough, if mightContainZeroes eq false
       
   136     int matchCount = pattern.length();
       
   137     if ( text.isEmpty() || matchCount > text.length() ) {
       
   138         matchCount = 0;
       
   139     } else {
       
   140         if ( !optimize ) {
       
   141             QString translatedText = translate( text, pattern.length() );
       
   142             matchCount = translatedText == pattern ? matchCount : 0; 
       
   143         } else {
       
   144             matchCount = translate( *text.data() ) == *pattern.data() ? 
       
   145                          matchCount : 0;
       
   146         }
       
   147     }
       
   148         
       
   149     LOGS_QDEBUG_2( "logs [FINDER] matchCount=", matchCount )
       
   150     LOGS_QDEBUG( "logs [FINDER] <- LogsPredictiveTranslator::startsWith()" )
       
   151     return matchCount;
       
   152 }
       
   153