logsui/logscntfinder/src/logscntentry.cpp
changeset 17 90fe74753f71
parent 15 76d2cf7a585e
child 21 2f0af9ba7665
equal deleted inserted replaced
15:76d2cf7a585e 17:90fe74753f71
   270 void LogsCntEntry::doSetHighlights( const QString& pattern, 
   270 void LogsCntEntry::doSetHighlights( const QString& pattern, 
   271                                     LogsCntTextList& nameArray )
   271                                     LogsCntTextList& nameArray )
   272 {
   272 {
   273     
   273     
   274     LogsPredictiveTranslator* translator = LogsPredictiveTranslator::instance();
   274     LogsPredictiveTranslator* translator = LogsPredictiveTranslator::instance();
   275     QMutableListIterator<LogsCntText> names( nameArray ); 
   275     QMutableListIterator<LogsCntText> names( nameArray );
   276     bool hasSeparators = translator->hasPatternSeparators( pattern );
   276     QString modifiedPattern = pattern;
       
   277     modifiedPattern = translator->trimPattern( modifiedPattern, true );
       
   278     
       
   279     bool hasSeparators = translator->hasPatternSeparators( modifiedPattern );
   277     
   280     
   278     //simple
   281     //simple
   279     while( names.hasNext() ) {
   282     while( names.hasNext() ) {
   280         LogsCntText& nameItem = names.next();
   283         LogsCntText& nameItem = names.next();
   281         //must use non-optimized version with whole pattern
   284         //must use non-optimized version with whole pattern
   282         nameItem.mHighlights = startsWith( nameItem, pattern, false );
   285         nameItem.mHighlights = startsWith( nameItem, modifiedPattern );
   283     }
   286     }
   284     
   287 
   285     //complex
   288     if ( hasSeparators ) {
   286     QListIterator<QString> patternArray( translator->patternTokens( pattern ) );
   289         //complex
   287     while( hasSeparators && patternArray.hasNext() ) {
   290         QListIterator<QString> patternArray( translator->patternTokens( modifiedPattern ) );
   288         QString patternItem = patternArray.next();
   291         while( patternArray.hasNext() ) {
   289         names.toFront();
   292             QString patternItem = patternArray.next();
   290         while( names.hasNext() ) {
   293             doSetHighlights( patternItem, names );
   291             LogsCntText& nameItem = names.next();
   294             translator->trimPattern( patternItem );
   292             int matchSize = startsWith( nameItem, patternItem, !hasSeparators );
   295             doSetHighlights( patternItem, names );
   293             nameItem.mHighlights = matchSize > nameItem.mHighlights ?
       
   294                                    matchSize : nameItem.mHighlights; 
       
   295         }
   296         }
   296     }
   297     }
   297 }
   298     
   298 
   299 }
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // LogsCntEntry::doSetHighlights()
       
   303 // -----------------------------------------------------------------------------
       
   304 //
       
   305 void LogsCntEntry::doSetHighlights( const QString& patternItem, 
       
   306                                     QMutableListIterator<LogsCntText>& names )
       
   307                                             
       
   308 {
       
   309     names.toFront();
       
   310     while( names.hasNext() ) {
       
   311         LogsCntText& nameItem = names.next();
       
   312         int matchSize = startsWith( nameItem, patternItem );
       
   313         nameItem.mHighlights = matchSize > nameItem.mHighlights ?
       
   314                                matchSize : nameItem.mHighlights; 
       
   315     }
       
   316 
       
   317 }
   299 
   318 
   300 // -----------------------------------------------------------------------------
   319 // -----------------------------------------------------------------------------
   301 // LogsCntEntry::startsWith()
   320 // LogsCntEntry::startsWith()
   302 // -----------------------------------------------------------------------------
   321 // -----------------------------------------------------------------------------
   303 //
   322 //
   304 int LogsCntEntry::startsWith( const LogsCntText& nameItem, 
   323 int LogsCntEntry::startsWith( const LogsCntText& nameItem, 
   305                               const QString& pattern, bool optimize ) const
   324                               const QString& pattern ) const
   306 {
   325 {
   307     LOGS_QDEBUG( "logs [FINDER] -> LogsCntEntry::startsWith()" )
   326     LOGS_QDEBUG( "logs [FINDER] -> LogsCntEntry::startsWith()" )
   308     //assumed that text has found based on pattern, thus only checking with
   327     //assumed that text has found based on pattern, thus only checking with
   309     //first char is enough, if mightContainZeroes eq false
   328     //first char is enough, if mightContainZeroes eq false
   310     const QString& text = nameItem.mTranslatedText;
   329     const QString& text = nameItem.mTranslatedText;
   311     
   330     
   312     int matchCount = pattern.length();
   331     int matchCount = pattern.length();
   313     if ( text.isEmpty() || matchCount > text.length() ) {
   332     if ( text.isEmpty() || matchCount > text.length() ) {
   314         matchCount = 0;
   333         matchCount = 0;
   315     } else {
   334     } else {
   316         if ( !optimize ) {
   335         matchCount = text.startsWith( pattern ) ? matchCount : 0; 
   317             matchCount = text.startsWith( pattern ) ? matchCount : 0; 
       
   318         } else {
       
   319             matchCount = *text.data() == *pattern.data() ? 
       
   320                          matchCount : 0;
       
   321         }
       
   322     }
   336     }
   323     LOGS_QDEBUG( "logs [FINDER] -> LogsCntEntry::startsWith()" )
   337     LOGS_QDEBUG( "logs [FINDER] -> LogsCntEntry::startsWith()" )
   324     return matchCount;
   338     return matchCount;
   325 }
   339 }
   326 
   340 
   344 {
   358 {
   345     bool match = false;
   359     bool match = false;
   346     if ( pattern.length() > 0 ) {
   360     if ( pattern.length() > 0 ) {
   347         LogsPredictiveTranslator* translator = LogsPredictiveTranslator::instance();
   361         LogsPredictiveTranslator* translator = LogsPredictiveTranslator::instance();
   348         
   362         
       
   363         QString modifiedPattern = pattern;
       
   364         modifiedPattern = translator->trimPattern( modifiedPattern, true );
       
   365         
   349         //direct match with phone number is enough
   366         //direct match with phone number is enough
   350         match = ( type() == EntryTypeHistory && 
   367         match = ( type() == EntryTypeHistory && 
   351                   mPhoneNumber.mTranslatedText.startsWith( pattern ) ) ||
   368                   mPhoneNumber.mTranslatedText.startsWith( pattern ) ) ||
   352                 doSimpleMatch( pattern );
   369                 doSimpleMatch( modifiedPattern );
   353         
   370         
   354         match = !match && translator->hasPatternSeparators( pattern ) ? 
   371         if (!match && translator->hasPatternSeparators( modifiedPattern ) ) {
   355                 doComplexMatch( translator->patternTokens( pattern) ) : match;
   372             QStringList patternArray = translator->patternTokens( modifiedPattern );
       
   373             match = doComplexMatch( patternArray );
       
   374             if (!match ) {
       
   375                 for(int i=0;i<patternArray.length();i++ ) {
       
   376                     translator->trimPattern( patternArray[i] );
       
   377                 }
       
   378                 match = doComplexMatch( patternArray );
       
   379             }
       
   380         }
   356     }
   381     }
   357     
   382     
   358     return match;
   383     return match;
   359 }
   384 }
   360 
   385 
   378 
   403 
   379 // -----------------------------------------------------------------------------
   404 // -----------------------------------------------------------------------------
   380 // LogsCntEntry::doComplexMatch()
   405 // LogsCntEntry::doComplexMatch()
   381 // -----------------------------------------------------------------------------
   406 // -----------------------------------------------------------------------------
   382 //
   407 //
   383 bool LogsCntEntry::doComplexMatch( QStringList patternArray ) const
   408 bool LogsCntEntry::doComplexMatch( const QStringList& patternArray ) const
   384 {
   409 {
   385     const bool zero = false;
   410     const bool zero = false;
   386 
   411 
   387     LogsCntTextList nameArray = mFirstName + mLastName; //with empties
   412     LogsCntTextList nameArray = mFirstName + mLastName; //with empties
   388 
   413