logsui/logscntfinder/src/logscntentry.cpp
changeset 2 7119b73b84d6
child 4 e52d42f9500c
equal deleted inserted replaced
0:4a5361db8937 2:7119b73b84d6
       
     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 
       
    18 #include <QListIterator>
       
    19 #include <QStringList>
       
    20 #include <QVector>
       
    21 
       
    22 #include "logscntentry.h"
       
    23 #include "logspredictivetranslator.h"
       
    24 #include "logslogger.h"
       
    25 
       
    26 
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // LogsCntEntry::richText()
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 QString LogsCntText::richText( QString startTag, 
       
    33                                QString endTag ) const
       
    34 {
       
    35     QString str = text();
       
    36     if ( str.length() > 0 && highlights() > 0 ) {
       
    37         str.insert( highlights() , endTag );
       
    38         str.insert( 0, startTag );
       
    39     }
       
    40 
       
    41     return str;
       
    42     
       
    43 }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // LogsCntEntry::LogsCntEntry()
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 LogsCntEntry::LogsCntEntry( LogsCntEntryHandle& handle, 
       
    50                             quint32 cid )
       
    51     : mType( EntryTypeHistory ), mCid( cid ), 
       
    52       mCached( true ),mHandle(&handle)
       
    53 {
       
    54     LogsCntText empty;
       
    55     mFirstName.append( empty );
       
    56     mLastName.append( empty );
       
    57     mAvatarPath = "";
       
    58 }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // LogsCntEntry::LogsCntEntry()
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 LogsCntEntry::LogsCntEntry( quint32 cid )
       
    65     : mType( EntryTypeContact ), mCid( cid ), 
       
    66       mCached( false ),mHandle(0)
       
    67 {
       
    68     LogsCntText empty;
       
    69     mFirstName.append( empty );
       
    70     mLastName.append( empty );
       
    71     mAvatarPath = "";
       
    72 }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // copy LogsCntEntry::LogsCntEntry()
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 LogsCntEntry::LogsCntEntry( const LogsCntEntry& entry )
       
    79     : mType(entry.mType),
       
    80       mCid(entry.mCid),
       
    81       mFirstName(entry.mFirstName),
       
    82       mLastName(entry.mLastName),
       
    83       mCached(entry.mCached),
       
    84       mHandle(entry.mHandle),
       
    85       mPhoneNumber(entry.mPhoneNumber),
       
    86       mAvatarPath(entry.mAvatarPath)
       
    87 {
       
    88 }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // LogsCntEntry::~LogsCntEntry()
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 LogsCntEntry::~LogsCntEntry()
       
    95 {
       
    96 }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // LogsCntEntry::firstName()
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 const LogsCntTextList& LogsCntEntry::firstName() const 
       
   103 {
       
   104     return mFirstName;
       
   105 }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // LogsCntEntry::lastName()
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 const LogsCntTextList& LogsCntEntry::lastName() const
       
   112 {
       
   113     return mLastName;
       
   114 }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // LogsCntEntry::avatarPath()
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 const QString& LogsCntEntry::avatarPath() const
       
   121 {
       
   122     return mAvatarPath;
       
   123 }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // LogsCntEntry::phoneNumber()
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 const LogsCntText& LogsCntEntry::phoneNumber() const
       
   130 {
       
   131     return mPhoneNumber;
       
   132 }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // LogsCntEntry::speedDial()
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 const QString& LogsCntEntry::speedDial() const
       
   139 {
       
   140     return mSpeedDial;
       
   141 }
       
   142 
       
   143 
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // LogsCntEntry::contactId()
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 quint32 LogsCntEntry::contactId() const
       
   150 {
       
   151     return mCid;
       
   152 }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // LogsCntEntry::handle()
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 LogsCntEntryHandle* LogsCntEntry::handle() const
       
   159 {
       
   160     return mHandle;
       
   161 }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // LogsCntEntry::setFirstName()
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 void LogsCntEntry::setFirstName( const QString& name ) 
       
   168 {
       
   169     mCached=true;
       
   170     mFirstName.clear();
       
   171     doSetText( name, mFirstName );
       
   172 }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // LogsCntEntry::setLastName()
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 void LogsCntEntry::setLastName( const QString& name ) 
       
   179 {
       
   180     mCached=true;
       
   181     mLastName.clear();
       
   182     doSetText( name, mLastName );
       
   183 }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // LogsCntEntry::setAvatarPath()
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 void LogsCntEntry::setAvatarPath( const QString& avatarpath ) 
       
   190 {
       
   191     mCached=true;
       
   192     mAvatarPath.clear();
       
   193     mAvatarPath = avatarpath;
       
   194 }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // LogsCntEntry::setPhoneNumber()
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void LogsCntEntry::setPhoneNumber( const QString& number )
       
   201 {
       
   202     LogsPredictiveTranslator* translator = LogsPredictiveTranslator::instance();
       
   203     
       
   204     mCached=true;
       
   205     mPhoneNumber.mText = number;
       
   206     mPhoneNumber.mTranslatedText = translator->translate( mPhoneNumber.mText );
       
   207     
       
   208 }
       
   209 
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // LogsCntEntry::doSetText()
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void LogsCntEntry::doSetText( const QString& text, LogsCntTextList& textlist ) 
       
   216 {
       
   217     LogsPredictiveTranslator* translator = LogsPredictiveTranslator::instance();
       
   218     
       
   219     QListIterator<QString> iter( translator->nameTokens( text ) );
       
   220 
       
   221     while( iter.hasNext() ) {
       
   222         LogsCntText txt;
       
   223         txt.mText = iter.next();
       
   224         txt.mTranslatedText = translator->translate( txt.mText );
       
   225         textlist.append( txt );
       
   226     }
       
   227     if ( textlist.count() == 0 ) {
       
   228         textlist.append( LogsCntText() );
       
   229     }
       
   230 }
       
   231 
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // LogsCntEntry::resetHighlights()
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void LogsCntEntry::resetHighlights( LogsCntTextList& nameArray )
       
   238 {
       
   239     QMutableListIterator<LogsCntText> names( nameArray ); 
       
   240     while( names.hasNext() ) {
       
   241         names.next().mHighlights = 0;
       
   242     }
       
   243     
       
   244 }
       
   245 
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // LogsCntEntry::setHighlights()
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 void LogsCntEntry::setHighlights( const QString& pattern )
       
   252 {
       
   253     resetHighlights( mFirstName );
       
   254     resetHighlights( mLastName );
       
   255     
       
   256     mPhoneNumber.mHighlights =
       
   257               mPhoneNumber.mTranslatedText.startsWith( pattern ) &&
       
   258               mPhoneNumber.mTranslatedText.length() >= pattern.length() ?
       
   259             pattern.length(): 0;
       
   260     
       
   261     doSetHighlights( pattern, mFirstName );
       
   262     doSetHighlights( pattern, mLastName );
       
   263     
       
   264 }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // LogsCntEntry::doSetHighlights()
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 void LogsCntEntry::doSetHighlights( const QString& pattern, 
       
   271                                     LogsCntTextList& nameArray )
       
   272 {
       
   273     
       
   274     LogsPredictiveTranslator* translator = LogsPredictiveTranslator::instance();
       
   275     QMutableListIterator<LogsCntText> names( nameArray ); 
       
   276     bool hasSeparators = translator->hasPatternSeparators( pattern );
       
   277     
       
   278     //simple
       
   279     while( names.hasNext() ) {
       
   280         LogsCntText& nameItem = names.next();
       
   281         nameItem.mHighlights = 
       
   282                 translator->startsWith( nameItem.mText, pattern, false );
       
   283     }
       
   284     
       
   285     //complex
       
   286     QListIterator<QString> patternArray( translator->patternTokens( pattern ) );
       
   287     while( hasSeparators && patternArray.hasNext() ) {
       
   288         QString patternItem = patternArray.next();
       
   289         names.toFront();
       
   290         while( names.hasNext() ) {
       
   291             LogsCntText& nameItem = names.next();
       
   292             int matchSize = translator->startsWith( nameItem.mText, 
       
   293                                                     patternItem, !hasSeparators );
       
   294             nameItem.mHighlights = matchSize > nameItem.mHighlights ?
       
   295                                    matchSize : nameItem.mHighlights; 
       
   296         }
       
   297     }
       
   298 }
       
   299 
       
   300 // -----------------------------------------------------------------------------
       
   301 // LogsCntEntry::setSpeedDial()
       
   302 // -----------------------------------------------------------------------------
       
   303 //
       
   304 void LogsCntEntry::setSpeedDial( const QString& number )
       
   305 {
       
   306     mSpeedDial = number;
       
   307 }
       
   308 
       
   309 
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // LogsCntEntry::match()
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 bool LogsCntEntry::match( const QString& pattern ) const
       
   316 {
       
   317     bool match = false;
       
   318     LogsPredictiveTranslator* translator = LogsPredictiveTranslator::instance();
       
   319     
       
   320     //direct match with phone number is enough
       
   321     match = ( type() == EntryTypeHistory && 
       
   322               mPhoneNumber.mTranslatedText.startsWith( pattern ) ) ||
       
   323             doSimpleMatch( pattern );
       
   324     
       
   325     match = !match && translator->hasPatternSeparators( pattern ) ? 
       
   326             doComplexMatch( translator->patternTokens( pattern) ) : match;
       
   327     
       
   328     return match;
       
   329 }
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // LogsCntEntry::doSimpleMatch()
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 bool LogsCntEntry::doSimpleMatch( const QString& pattern ) const
       
   336 {
       
   337     LogsCntTextList nameArray = mFirstName + mLastName; //with empties
       
   338     QListIterator<LogsCntText> names( nameArray ); 
       
   339     int matchCount = 0;
       
   340 
       
   341     while( names.hasNext() && !matchCount ) {
       
   342         matchCount = (int)names.next().mTranslatedText.startsWith( pattern );
       
   343     }
       
   344 
       
   345     return matchCount > 0;
       
   346 }
       
   347 
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // LogsCntEntry::doComplexMatch()
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 bool LogsCntEntry::doComplexMatch( QStringList patternArray ) const
       
   354 {
       
   355     const bool zero = false;
       
   356 
       
   357     LogsCntTextList nameArray = mFirstName + mLastName; //with empties
       
   358 
       
   359     int targetMatchCount = patternArray.count();
       
   360     int namesCount = nameArray.count();
       
   361 
       
   362     //if pattern has more tokens than name(s), it is a missmatch
       
   363     if ( namesCount < targetMatchCount ) {
       
   364         return false;
       
   365     }
       
   366 
       
   367     QListIterator<LogsCntText> names( nameArray ); 
       
   368     QListIterator<QString> patterns( patternArray );
       
   369     QVector<bool> matchVector(targetMatchCount, zero );
       
   370     int currentPattern = 0;
       
   371     int matchCount = 0;
       
   372     bool match = false;
       
   373     
       
   374     while( names.hasNext() && matchCount < targetMatchCount ) {
       
   375         LogsCntText name = names.next();
       
   376         currentPattern = 0;
       
   377         patterns.toFront();
       
   378         match = false;
       
   379         while ( !name.mText.isEmpty() && 
       
   380                  patterns.hasNext() && !match ) {
       
   381             QString pattern = patterns.next();
       
   382             //unique match check
       
   383             if ( !matchVector.at( currentPattern ) ) {
       
   384                 match = matchVector[ currentPattern ] 
       
   385                       = name.mTranslatedText.startsWith( pattern );
       
   386                 matchCount = match ? matchCount+1 : matchCount;
       
   387             }
       
   388             currentPattern++;
       
   389         }
       
   390     }
       
   391     return matchCount >= targetMatchCount;
       
   392 
       
   393     }
       
   394     
       
   395     
       
   396 
       
   397 // -----------------------------------------------------------------------------
       
   398 // LogsCntEntry::isCached()
       
   399 // -----------------------------------------------------------------------------
       
   400 //
       
   401 bool LogsCntEntry::isCached() const
       
   402 {
       
   403     return mCached;
       
   404 }
       
   405 
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // LogsCntEntry::type()
       
   409 // -----------------------------------------------------------------------------
       
   410 //
       
   411 LogsCntEntry::EntryType LogsCntEntry::type() const
       
   412 {
       
   413     return mType;
       
   414 }
       
   415 
       
   416 
       
   417