logsui/logscntfinder/src/logspredictive12keytranslator.cpp
changeset 21 2f0af9ba7665
parent 17 90fe74753f71
equal deleted inserted replaced
18:acd4e87b24b4 21:2f0af9ba7665
    23 #include <hbinputsettingproxy.h>
    23 #include <hbinputsettingproxy.h>
    24 
    24 
    25 #include "logspredictive12keytranslator.h"
    25 #include "logspredictive12keytranslator.h"
    26 #include "logslogger.h"
    26 #include "logslogger.h"
    27 
    27 
    28 const QChar ZeroSepar('0');
       
    29 const int NotAssigned = -1;
       
    30 
    28 
    31 // -----------------------------------------------------------------------------
    29 // -----------------------------------------------------------------------------
    32 // LogsPredictive12KeyTranslator::LogsPredictive12KeyTranslator()
    30 // LogsPredictive12KeyTranslator::LogsPredictive12KeyTranslator()
    33 // -----------------------------------------------------------------------------
    31 // -----------------------------------------------------------------------------
    34 //
    32 //
    54     LOGS_QDEBUG( "logs [FINDER] <- LogsPredictive12KeyTranslator::\
    52     LOGS_QDEBUG( "logs [FINDER] <- LogsPredictive12KeyTranslator::\
    55 ~LogsPredictive12KeyTranslator()" )
    53 ~LogsPredictive12KeyTranslator()" )
    56     
    54     
    57 }
    55 }
    58 
    56 
    59 // -----------------------------------------------------------------------------
       
    60 // LogsPredictive12KeyTranslator::patternTokens()
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 QStringList LogsPredictive12KeyTranslator::patternTokens( const QString& pattern ) const
       
    64 {
       
    65         
       
    66     LOGS_QDEBUG( "logs [FINDER] -> LogsPredictive12KeyTranslator::\
       
    67 patternTokens()" )
       
    68     LOGS_QDEBUG_2( "logs [FINDER] pattern ", pattern );
       
    69     QString car;
       
    70     QString cdr;
       
    71     
       
    72     QStringList target;
       
    73     splitPattern( pattern, car, cdr );
       
    74     if ( car.length() ) {
       
    75         target.append( car );
       
    76         if ( cdr.length() ) {
       
    77             target.append( cdr );
       
    78         }
       
    79     }
       
    80     LOGS_QDEBUG( "logs [FINDER] <- LogsPredictive12KeyTranslator::\
       
    81 patternTokens()" )
       
    82     return target;
       
    83 }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // LogsPredictive12KeyTranslator::splitPattern()
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void LogsPredictive12KeyTranslator::splitPattern( const QString& pattern, 
       
    90                                                   QString& car, QString& cdr ) const
       
    91 {
       
    92     car = "";
       
    93     cdr = "";
       
    94     
       
    95     QChar current;
       
    96     QChar previous;
       
    97     int splitStart = NotAssigned;
       
    98     int splitEnd = NotAssigned;
       
    99     int index = 0;
       
   100     while( splitEnd == NotAssigned && index < pattern.length() ) {
       
   101         current = pattern[index];
       
   102         splitStart = splitStart == NotAssigned &&
       
   103                     ( previous != ZeroSepar && previous != QChar() ) && 
       
   104                     current == ZeroSepar ? 
       
   105                         index : splitStart;
       
   106         splitEnd = splitStart != NotAssigned && 
       
   107                    previous == ZeroSepar && 
       
   108                    current != ZeroSepar ?
       
   109                       index : splitEnd;
       
   110         previous = current;
       
   111         index++;
       
   112     }
       
   113     
       
   114     if ( splitStart != NotAssigned && splitEnd != NotAssigned ) {
       
   115         car = pattern.left( splitStart );
       
   116         cdr = pattern.right( pattern.length() - splitEnd );  
       
   117     } else {
       
   118         car = pattern; 
       
   119     }
       
   120 }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // LogsPredictive12KeyTranslator::trimPattern()
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 QString& LogsPredictive12KeyTranslator::trimPattern( QString& pattern, 
       
   127                                                      bool tailOnly ) const
       
   128 {
       
   129     QRegExp lead("^0*");//remove leading zeros
       
   130     QRegExp trail("0*$");//remove trailing zeros
       
   131     
       
   132     if ( pattern.length() ) {
       
   133         if ( !tailOnly ) {
       
   134             pattern.remove( lead );
       
   135         }
       
   136         
       
   137         pattern.remove( trail );
       
   138         
       
   139         if( !pattern.length() ) {
       
   140             pattern += ZeroSepar;
       
   141         }
       
   142     }
       
   143     return pattern;
       
   144 }
       
   145 
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // LogsPredictive12KeyTranslator::hasPatternSeparators()
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 int LogsPredictive12KeyTranslator::hasPatternSeparators( 
       
   152         const QString& pattern ) const
       
   153 {
       
   154     return pattern.count( ZeroSepar );
       
   155 
       
   156 }
       
   157 
    57 
   158 // -----------------------------------------------------------------------------
    58 // -----------------------------------------------------------------------------
   159 // LogsPredictive12KeyTranslator::translateChar()
    59 // LogsPredictive12KeyTranslator::translateChar()
   160 // -----------------------------------------------------------------------------
    60 // -----------------------------------------------------------------------------
   161 //
    61 //
   162 const QChar LogsPredictive12KeyTranslator::translateChar( 
    62 const QString LogsPredictive12KeyTranslator::translateChar( 
   163         const QChar character ) const
    63         const QChar character ) const
   164 {
    64 {
   165     const HbMappedKey* mappedKey = 0;
    65     const HbMappedKey* mappedKey = 0;
   166     if ( mKeyMap ) {
    66     if ( mKeyMap ) {
   167         mappedKey = mKeyMap->keyForCharacter( HbKeyboardVirtual12Key, character );
    67         mappedKey = mKeyMap->keyForCharacter( HbKeyboardVirtual12Key, character );
       
    68         if ( !mappedKey ) {
       
    69             mappedKey = mKeyMap->keyForCharacter( HbKeyboardSctPortrait, character );
       
    70             return mappedKey ? QString( StarKey ) : QString();
       
    71         }
   168     }
    72     }
   169     return mappedKey ? mappedKey->keycode : QChar();
    73     return mappedKey ? QString( mappedKey->keycode ) : QString();
       
    74 }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // LogsPredictive12KeyTranslator::match()
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 bool LogsPredictive12KeyTranslator::match( 
       
    81         const QString& pattern, 
       
    82         LogsCntTokenIterator& names ) const
       
    83 {
       
    84     QString modifiedPattern = pattern;
       
    85     modifiedPattern = trimPattern( modifiedPattern, true );
       
    86     
       
    87     bool match = doSimpleMatch( modifiedPattern, names ); 
       
    88         
       
    89     if (!match && hasPatternSeparators( modifiedPattern ) ) {
       
    90         QStringList patternArray = patternTokens( modifiedPattern );
       
    91         match = doComplexMatch( patternArray, names );
       
    92         if (!match ) {
       
    93             for(int i=0;i<patternArray.length();i++ ) {
       
    94                 trimPattern( patternArray[i] );
       
    95             }
       
    96             match = doComplexMatch( patternArray, names );
       
    97         }
       
    98     }
       
    99     
       
   100     return match;
       
   101 }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // LogsPredictive12KeyTranslator::doSimpleMatch()
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 bool LogsPredictive12KeyTranslator::doSimpleMatch( 
       
   108                         const QString& pattern,
       
   109                         LogsCntTokenIterator& names ) const
       
   110 {
       
   111     int matchCount = 0;
       
   112     names.toFront();
       
   113     
       
   114     while( names.hasNext() && !matchCount ) {
       
   115         matchCount = (int)names.next().translation().startsWith( pattern );
       
   116     }
       
   117 
       
   118     return matchCount > 0;
   170 }
   119 }
   171 
   120 
   172 
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // LogsPredictive12KeyTranslator::doComplexMatch()
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 bool LogsPredictive12KeyTranslator::doComplexMatch( 
       
   127                         const QStringList& patternArray,
       
   128                         LogsCntTokenIterator& names ) const
       
   129 {
       
   130     const bool zero = false;
       
   131     names.toFront();
       
   132 
       
   133     int targetMatchCount = patternArray.count();
       
   134     int namesCount = names.count();
       
   135 
       
   136     //if pattern has more tokens than name(s), it is a missmatch
       
   137     if ( namesCount < targetMatchCount ) {
       
   138         return false;
       
   139     }
       
   140 
       
   141     QListIterator<QString> patterns( patternArray );
       
   142     QVector<bool> matchVector(targetMatchCount, zero );
       
   143     int currentPattern = 0;
       
   144     int matchCount = 0;
       
   145     bool match = false;
       
   146     
       
   147     while( names.hasNext() && matchCount < targetMatchCount ) {
       
   148         LogsCntToken name = names.next();
       
   149         currentPattern = 0;
       
   150         patterns.toFront();
       
   151         match = false;
       
   152         while ( !name.text().isEmpty() && 
       
   153                  patterns.hasNext() && !match ) {
       
   154             QString pattern = patterns.next();
       
   155             //unique match check
       
   156             if ( !matchVector.at( currentPattern ) ) {
       
   157                 match = matchVector[ currentPattern ] 
       
   158                       = name.translation().startsWith( pattern );
       
   159                 matchCount = match ? matchCount+1 : matchCount;
       
   160             }
       
   161             currentPattern++;
       
   162         }
       
   163     }
       
   164     return matchCount >= targetMatchCount;
       
   165 
       
   166     }
       
   167