logsui/logsengine/tsrc/stubs/logscntfinder_stub.cpp
changeset 24 0ba2181d7c28
equal deleted inserted replaced
0:e686773b3f54 24:0ba2181d7c28
       
     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 "logscntfinder.h"
       
    19 #include "logslogger.h"
       
    20 
       
    21 #include <QStringList>
       
    22 #include <QContactManager.h>
       
    23 #include <QContact.h>
       
    24 #include <QContactName.h>
       
    25 #include <QContactPhoneNumber.h>
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // LogsCntEntry::richText()
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 QString LogsCntText::richText( QString startTag, 
       
    32                                QString endTag ) const
       
    33 {
       
    34     QString str = text();
       
    35     if ( str.length() > 0 && highlights() > 0 ) {
       
    36         str.insert( highlights() , endTag );
       
    37         str.insert( 0, startTag );
       
    38     }
       
    39 
       
    40     return str;
       
    41     
       
    42 }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // LogsCntEntry::LogsCntEntry()
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 LogsCntEntry::LogsCntEntry( LogsCntEntryHandle& handle, 
       
    49                             quint32 cid )
       
    50     : mType( EntryTypeHistory ), mCid( cid ), 
       
    51       mCached( false ),mHandle(&handle)
       
    52 {
       
    53     LogsCntText empty;
       
    54     mFirstName.append( empty );
       
    55     mLastName.append( empty );
       
    56     mAvatarPath = "c:\\data\\images\\bg_1.png";
       
    57 }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // LogsCntEntry::LogsCntEntry()
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 LogsCntEntry::LogsCntEntry( quint32 cid )
       
    64     : mType( EntryTypeContact ), mCid( cid ), 
       
    65       mCached( false ),mHandle(0)
       
    66 {
       
    67     LogsCntText empty;
       
    68     mFirstName.append( empty );
       
    69     mLastName.append( empty );
       
    70 }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // copy LogsCntEntry::LogsCntEntry()
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 LogsCntEntry::LogsCntEntry( const LogsCntEntry& entry )
       
    77     : mType(entry.mType),
       
    78       mCid(entry.mCid),
       
    79       mFirstName(entry.mFirstName),
       
    80       mLastName(entry.mLastName),
       
    81       mCached(entry.mCached),
       
    82       mHandle(entry.mHandle),
       
    83       mPhoneNumber(entry.mPhoneNumber)
       
    84 {
       
    85 }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // LogsCntEntry::~LogsCntEntry()
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 LogsCntEntry::~LogsCntEntry()
       
    92 {
       
    93 }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // LogsCntEntry::LogsCntEntry()
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 const LogsCntTextList& LogsCntEntry::firstName() const 
       
   100 {
       
   101     return mFirstName;
       
   102 }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // LogsCntEntry::lastName()
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 const LogsCntTextList& LogsCntEntry::lastName() const
       
   109 {
       
   110     return mLastName;
       
   111 }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // LogsCntEntry::avatarPath()
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 const QString& LogsCntEntry::avatarPath() const
       
   118 {
       
   119     return mAvatarPath;
       
   120 }
       
   121 
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // LogsCntEntry::phoneNumber()
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 const LogsCntText& LogsCntEntry::phoneNumber() const
       
   128 {
       
   129     return mPhoneNumber;
       
   130 }
       
   131 
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // LogsCntEntry::contactId()
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 quint32 LogsCntEntry::contactId() const
       
   138 {
       
   139     return mCid;
       
   140 }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // LogsCntEntry::handle()
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 LogsCntEntryHandle* LogsCntEntry::handle() const
       
   147 {
       
   148     return mHandle;
       
   149 }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // LogsCntEntry::setFirstName()
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void LogsCntEntry::setFirstName( const QString& name ) 
       
   156 {
       
   157     mCached=true;
       
   158     mFirstName.clear();
       
   159     doSetText( name, mFirstName );
       
   160 }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // LogsCntEntry::setLastName()
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void LogsCntEntry::setLastName( const QString& name ) 
       
   167 {
       
   168     mCached=true;
       
   169     mLastName.clear();
       
   170     doSetText( name, mLastName );
       
   171 }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // LogsCntEntry::setPhoneNumber()
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void LogsCntEntry::setPhoneNumber( const QString& number )
       
   178 {
       
   179     mPhoneNumber.mText = number;
       
   180 }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // LogsCntEntry::doSetText()
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 void LogsCntEntry::doSetText( const QString& text, LogsCntTextList& textlist ) 
       
   187 {
       
   188     const QChar separ(' ');
       
   189     QStringList parts = text.split( separ, QString::SkipEmptyParts );
       
   190     for( int i=0;i<parts.count();i++) {
       
   191         LogsCntText txt;
       
   192         txt.mText = parts[i];
       
   193         if ( type() == EntryTypeHistory  ) {
       
   194             txt.mTranslatedText = txt.mText;
       
   195         }
       
   196         textlist.append( txt );
       
   197     }
       
   198 }
       
   199 
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // LogsCntEntry::setHighlights()
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 void LogsCntEntry::setHighlights( const QString& pattern )
       
   206 {
       
   207 }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // LogsCntEntry::match()
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 bool LogsCntEntry::match( const QString& pattern ) const
       
   214 {
       
   215     return doMatch( pattern, mFirstName ) ||
       
   216            doMatch( pattern, mLastName );
       
   217 }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // LogsCntEntry::doMatch()
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 bool LogsCntEntry::doMatch( const QString& pattern, 
       
   224                             const LogsCntTextList& textlist ) const
       
   225 {
       
   226      //direct match with phone number is enough
       
   227     if ( mPhoneNumber.text().startsWith( pattern ) ) {
       
   228         return true;
       
   229     }
       
   230     
       
   231     bool found = false;
       
   232     int index=0;
       
   233     while( index < textlist.count() && !found ) {
       
   234         found = textlist.at( index++ ).mTranslatedText.startsWith( pattern );
       
   235     }
       
   236     return found;
       
   237     
       
   238 }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // LogsCntEntry::isCached()
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 bool LogsCntEntry::isCached() const
       
   245 {
       
   246     return mCached;
       
   247 }
       
   248 
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // LogsCntEntry::type()
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 LogsCntEntry::EntryType LogsCntEntry::type() const
       
   255 {
       
   256     return mType;
       
   257 }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // LogsCntFinder::LogsCntFinder()
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 LogsCntFinder::LogsCntFinder()
       
   264 {
       
   265     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::LogsCntFinder()" )
       
   266     
       
   267     // Create manager ourselves, object takes care of deletion when registering
       
   268     // as parent.
       
   269     QMap<QString, QString> dummyParams;
       
   270     mContactManager = new QContactManager("symbian", dummyParams, this);
       
   271     
       
   272     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::LogsCntFinder()" )
       
   273 }
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // LogsCntFinder::LogsCntFinder()
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 LogsCntFinder::LogsCntFinder(QContactManager& contactManager)
       
   280 {
       
   281     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::LogsCntFinder(), cntmgr from client" )
       
   282     
       
   283     mContactManager = &contactManager;
       
   284     
       
   285     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::LogsCntFinder()" )
       
   286 }
       
   287 
       
   288 
       
   289 // -----------------------------------------------------------------------------
       
   290 // LogsCntFinder::~LogsCntFinder()
       
   291 // -----------------------------------------------------------------------------
       
   292 //
       
   293 LogsCntFinder::~LogsCntFinder()
       
   294 {
       
   295     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::~LogsCntFinder()" )
       
   296 
       
   297     qDeleteAll( mResults );
       
   298     qDeleteAll( mHistoryEvents );
       
   299     
       
   300     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::~LogsCntFinder()" )
       
   301 }
       
   302 
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // LogsCntFinder::predictiveSearchQuery
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void LogsCntFinder::predictiveSearchQuery( const QString& pattern )
       
   309 {
       
   310     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::predictiveSearchQuery()" )
       
   311     LOGS_QDEBUG_2( "logs [FINDER] pattern= ", pattern )
       
   312     
       
   313     bool patternChanged = pattern != mCurrentPredictivePattern;
       
   314     mCurrentPredictivePattern = pattern;
       
   315     
       
   316     if ( !mCurrentPredictivePattern.isEmpty() && patternChanged ) {
       
   317         qDeleteAll( mResults );
       
   318         mResults.clear();
       
   319         doPredictiveHistoryQuery();
       
   320         doPredictiveContactQuery();
       
   321     }
       
   322     //emit queryReady(); // commented off to ease testing
       
   323  
       
   324     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::predictiveSearchQuery()" )
       
   325 }
       
   326 
       
   327 // -----------------------------------------------------------------------------
       
   328 // LogsCntFinder::doPredictiveHistoryQuery
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 void LogsCntFinder::doPredictiveHistoryQuery()
       
   332 {
       
   333     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::doPredictiveHistoryQuery()" )
       
   334     
       
   335     QListIterator<LogsCntEntry*> iter(mHistoryEvents);
       
   336     
       
   337     while( iter.hasNext() ) {
       
   338         LogsCntEntry* e = iter.next();
       
   339         if ( e->match( mCurrentPredictivePattern ) ) {
       
   340             LogsCntEntry* entry = new LogsCntEntry( *e );
       
   341             mResults.append( entry );
       
   342         }
       
   343     }
       
   344         
       
   345     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::doPredictiveHistoryQuery()" )
       
   346 }
       
   347 
       
   348 // -----------------------------------------------------------------------------
       
   349 // LogsCntFinder::doPredictiveContactQuery
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 void LogsCntFinder::doPredictiveContactQuery()
       
   353 {
       
   354     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::doPredictiveContactQuery()" )
       
   355 /*
       
   356     int index = 0;
       
   357     while( index < cntIds.count() ) {
       
   358       LogsCntEntry* entry = new LogsCntEntry( cntIds.at( index++ ) );
       
   359       mResults.append( entry );
       
   360     }
       
   361     */
       
   362     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::doPredictiveContactQuery()" )
       
   363     
       
   364 }
       
   365 
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // LogsCntFinder::resultsCount
       
   369 // -----------------------------------------------------------------------------
       
   370 //
       
   371 int LogsCntFinder::resultsCount() const
       
   372 {
       
   373     return mResults.count();
       
   374 }
       
   375 
       
   376 // -----------------------------------------------------------------------------
       
   377 // LogsCntFinder::resultAt
       
   378 // -----------------------------------------------------------------------------
       
   379 //
       
   380 const LogsCntEntry& LogsCntFinder::resultAt( int index )
       
   381 {
       
   382     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::resultAt()" )
       
   383     LOGS_QDEBUG_2( "logs [FINDER] index=", index )
       
   384     
       
   385     LogsCntEntry* entry = mResults.at( index );
       
   386     if ( !entry->isCached() ) {
       
   387         QContact contact = mContactManager->contact( entry->contactId() );
       
   388         QContactName contactName = contact.detail( QContactName::DefinitionName );
       
   389         entry->setFirstName( contactName.value( QContactName::FieldFirst ) );
       
   390         entry->setLastName( contactName.value( QContactName::FieldLast ) );
       
   391         QContactPhoneNumber contactPhoneNumber = 
       
   392               contact.detail( QContactPhoneNumber::DefinitionName );
       
   393         entry->setPhoneNumber( 
       
   394               contactPhoneNumber.value( QContactPhoneNumber::FieldNumber ) );
       
   395         
       
   396         entry->setHighlights( mCurrentPredictivePattern );
       
   397     }
       
   398     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::resultAt()" )
       
   399     return *entry;
       
   400   
       
   401 }
       
   402 
       
   403 // -----------------------------------------------------------------------------
       
   404 // LogsCntFinder::insertEntry
       
   405 // -----------------------------------------------------------------------------
       
   406 //
       
   407 void LogsCntFinder::insertEntry( int index, LogsCntEntry* entry )
       
   408 {
       
   409     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::insertEntry()" )
       
   410     LOGS_QDEBUG_4( "logs [FINDER] handle=", entry->handle()," to index ", index )
       
   411     
       
   412     mHistoryEvents.insert( index, entry );
       
   413     
       
   414     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::insertEntry()" )
       
   415 }
       
   416 
       
   417 // -----------------------------------------------------------------------------
       
   418 // LogsCntFinder::getEntry
       
   419 // -----------------------------------------------------------------------------
       
   420 //
       
   421 LogsCntEntry* LogsCntFinder::getEntry( const LogsCntEntryHandle& handle ) const
       
   422 {
       
   423     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::getEntry()" )
       
   424     return doGetEntry( mHistoryEvents, handle );      
       
   425 }
       
   426 
       
   427 // -----------------------------------------------------------------------------
       
   428 // LogsCntFinder::doGetEntry
       
   429 // -----------------------------------------------------------------------------
       
   430 //
       
   431 LogsCntEntry* LogsCntFinder::doGetEntry( const LogsCntEntryList& list, 
       
   432                                          const LogsCntEntryHandle& handle ) const
       
   433 {
       
   434     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::doGetEntry()" )
       
   435     LOGS_QDEBUG_2( "logs [FINDER] handle= ", &handle )
       
   436     
       
   437     LogsCntEntry* entry = 0;
       
   438     QListIterator<LogsCntEntry*> iter(list);
       
   439     
       
   440     while( iter.hasNext() && !entry ) {
       
   441         LogsCntEntry* e = iter.next();
       
   442         entry = e->handle() == &handle ? e : 0;
       
   443     }
       
   444     
       
   445     LOGS_QDEBUG_2( "logs [FINDER] found=", (entry!=0) )
       
   446     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::doGetEntry()" )
       
   447     return entry;
       
   448 }
       
   449 
       
   450 // -----------------------------------------------------------------------------
       
   451 // LogsCntFinder::deleteEntry
       
   452 // -----------------------------------------------------------------------------
       
   453 //
       
   454 void LogsCntFinder::deleteEntry( const LogsCntEntryHandle& handle )
       
   455 {
       
   456     LOGS_QDEBUG( "logs [FINDER] -> LogsCntFinder::deleteEntry()" )
       
   457     LOGS_QDEBUG_2( "logs [FINDER] handle= ", &handle )
       
   458     
       
   459     LogsCntEntry* toRemoveHistoryEv = doGetEntry( mHistoryEvents, handle );
       
   460     mHistoryEvents.removeOne( toRemoveHistoryEv );
       
   461     delete toRemoveHistoryEv;
       
   462     
       
   463     LOGS_QDEBUG( "logs [FINDER] <- LogsCntFinder::deleteEntry()" )
       
   464     
       
   465 }
       
   466 
       
   467 
       
   468 
       
   469