|
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 "logsmatchesmodel.h" |
|
18 #include "logsmodel.h" |
|
19 |
|
20 #include "logsevent.h" |
|
21 #include "logslogger.h" |
|
22 #include "logsengdefs.h" |
|
23 #include "logsdbconnector.h" |
|
24 #include "logscntfinder.h" |
|
25 #include "logsdetailsmodel.h" |
|
26 #include "logscall.h" |
|
27 #include "logsmessage.h" |
|
28 #include "logscontact.h" |
|
29 #include "logseventdata.h" |
|
30 #include "logsthumbnailmanager.h" |
|
31 #include "logscommondata.h" |
|
32 #include <hblineedit.h> |
|
33 #include <QStringList> |
|
34 #include <qcontactphonenumber.h> |
|
35 #include <qcontactmanager.h> |
|
36 |
|
37 Q_DECLARE_METATYPE(LogsEvent *) |
|
38 Q_DECLARE_METATYPE(LogsCall *) |
|
39 Q_DECLARE_METATYPE(LogsMessage *) |
|
40 Q_DECLARE_METATYPE(LogsContact *) |
|
41 Q_DECLARE_METATYPE(LogsDetailsModel *) |
|
42 |
|
43 // ----------------------------------------------------------------------------- |
|
44 // |
|
45 // ----------------------------------------------------------------------------- |
|
46 // |
|
47 LogsMatchesModel::LogsMatchesModel( |
|
48 LogsAbstractModel& parentModel, LogsDbConnector& dbConnector ) |
|
49 : LogsAbstractModel(), |
|
50 mParentModel(parentModel), |
|
51 mLogsCntFinder(0), |
|
52 mIconManager(0), |
|
53 mSearchEnabled(false), |
|
54 mResultCount(0) |
|
55 { |
|
56 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::LogsMatchesModel()" ) |
|
57 |
|
58 mDbConnector = &dbConnector; |
|
59 initPredictiveSearch(); |
|
60 |
|
61 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::LogsMatchesModel()" ) |
|
62 } |
|
63 |
|
64 // ----------------------------------------------------------------------------- |
|
65 // |
|
66 // ----------------------------------------------------------------------------- |
|
67 // |
|
68 LogsMatchesModel::~LogsMatchesModel() |
|
69 { |
|
70 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::~LogsMatchesModel()" ) |
|
71 |
|
72 delete mLogsCntFinder; |
|
73 qDeleteAll(mMatches); |
|
74 delete mIconManager; |
|
75 |
|
76 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::~LogsMatchesModel()" ) |
|
77 } |
|
78 |
|
79 // ----------------------------------------------------------------------------- |
|
80 // From QAbstractItemModel |
|
81 // ----------------------------------------------------------------------------- |
|
82 // |
|
83 int LogsMatchesModel::rowCount(const QModelIndex & /* parent */) const |
|
84 { |
|
85 return mResultCount; |
|
86 } |
|
87 |
|
88 // ----------------------------------------------------------------------------- |
|
89 // From QAbstractItemModel |
|
90 // ----------------------------------------------------------------------------- |
|
91 // |
|
92 QVariant LogsMatchesModel::data(const QModelIndex &index, int role) const |
|
93 { |
|
94 if (!index.isValid() || index.row() >= mResultCount || index.row() < 0 ) { |
|
95 return QVariant(); |
|
96 } |
|
97 |
|
98 LogsMatchesModelItemContainer* item = 0; |
|
99 if ( index.row() < mMatches.count() ){ |
|
100 item = mMatches.at(index.row()); |
|
101 } else { |
|
102 item = const_cast<LogsMatchesModel*>(this)->addSearchResult(index.row()); |
|
103 } |
|
104 |
|
105 updateSearchResult(*item); |
|
106 if (role == Qt::DisplayRole){ |
|
107 return( QVariant( item->texts() ) ); |
|
108 } else if (role == Qt::DecorationRole) { |
|
109 return( QVariant( item->icons(index.row()) ) ); |
|
110 } else if ( role == RoleDetailsModel ) { |
|
111 LOGS_QDEBUG( "logs [ENG] LogsMatchesModel::data() RoleDetailsModel" ) |
|
112 LogsDetailsModel* detailsModel = 0; |
|
113 LogsEvent* event = item->event(); |
|
114 if ( event ) { |
|
115 detailsModel = new LogsDetailsModel( *mDbConnector, *event ); |
|
116 } |
|
117 QVariant var = qVariantFromValue( detailsModel ); |
|
118 return var; |
|
119 } |
|
120 return doGetData(role, *item); |
|
121 } |
|
122 |
|
123 // ----------------------------------------------------------------------------- |
|
124 // |
|
125 // ----------------------------------------------------------------------------- |
|
126 // |
|
127 QVariant LogsMatchesModel::createCall(const LogsModelItemContainer& item) const |
|
128 { |
|
129 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::createCall()" ) |
|
130 LogsEvent* event = item.event(); |
|
131 if ( event ){ |
|
132 return LogsAbstractModel::createCall(item); |
|
133 } |
|
134 const LogsMatchesModelItemContainer& matchItem = |
|
135 static_cast<const LogsMatchesModelItemContainer&>( item ); |
|
136 LogsCall* logscall = new LogsCall(matchItem.contact(), matchItem.number()); |
|
137 if (!logscall->isAllowedCallType()) { |
|
138 delete logscall; |
|
139 logscall = 0; |
|
140 } |
|
141 QVariant var = qVariantFromValue(logscall); |
|
142 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::createCall()" ) |
|
143 return var; |
|
144 } |
|
145 |
|
146 // ----------------------------------------------------------------------------- |
|
147 // |
|
148 // ----------------------------------------------------------------------------- |
|
149 // |
|
150 QVariant LogsMatchesModel::createMessage(const LogsModelItemContainer& item) const |
|
151 { |
|
152 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::createMessage()" ) |
|
153 LogsEvent* event = item.event(); |
|
154 if ( event ){ |
|
155 return LogsAbstractModel::createMessage(item); |
|
156 } |
|
157 const LogsMatchesModelItemContainer& matchItem = |
|
158 static_cast<const LogsMatchesModelItemContainer&>( item ); |
|
159 LogsMessage* logsMessage = new LogsMessage(matchItem.contact(), matchItem.number(),matchItem.contactName()); |
|
160 if (!logsMessage->isMessagingAllowed()) { |
|
161 delete logsMessage; |
|
162 logsMessage = 0; |
|
163 } |
|
164 QVariant var = qVariantFromValue(logsMessage); |
|
165 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::createMessage()" ) |
|
166 return var; |
|
167 } |
|
168 |
|
169 // ----------------------------------------------------------------------------- |
|
170 // |
|
171 // ----------------------------------------------------------------------------- |
|
172 // |
|
173 QVariant LogsMatchesModel::createContact(const LogsModelItemContainer& item) const |
|
174 { |
|
175 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::createContact()" ) |
|
176 LogsEvent* event = item.event(); |
|
177 if ( event ){ |
|
178 return LogsAbstractModel::createContact(item); |
|
179 } |
|
180 const LogsMatchesModelItemContainer& matchItem = |
|
181 static_cast<const LogsMatchesModelItemContainer&>( item ); |
|
182 LogsContact* logsContact = |
|
183 new LogsContact(matchItem.number(), *mDbConnector, matchItem.contact()); |
|
184 if ( !logsContact->isContactRequestAllowed() ) { |
|
185 delete logsContact; |
|
186 logsContact = 0; |
|
187 } else { |
|
188 connect( logsContact, SIGNAL(saveCompleted(bool)), this, SLOT(forceSearchQuery()) ); |
|
189 } |
|
190 QVariant var = qVariantFromValue(logsContact); |
|
191 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::createContact()" ) |
|
192 return var; |
|
193 } |
|
194 |
|
195 |
|
196 // ----------------------------------------------------------------------------- |
|
197 // |
|
198 // ----------------------------------------------------------------------------- |
|
199 // |
|
200 int LogsMatchesModel::predictiveSearchStatus() |
|
201 { |
|
202 return mDbConnector->predictiveSearchStatus(); |
|
203 } |
|
204 |
|
205 // ----------------------------------------------------------------------------- |
|
206 // |
|
207 // ----------------------------------------------------------------------------- |
|
208 // |
|
209 int LogsMatchesModel::setPredictiveSearch(bool enabled) |
|
210 { |
|
211 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::setPredictiveSearch()" ) |
|
212 int err = mDbConnector->setPredictiveSearch(enabled); |
|
213 if (!err) { |
|
214 if ((mSearchEnabled && !enabled) || (!mSearchEnabled && enabled)) { |
|
215 //in case of search is turned off, getLogMatches will only reset |
|
216 //previous search results |
|
217 mSearchEnabled = enabled; |
|
218 forceSearchQuery(); |
|
219 } |
|
220 } |
|
221 LOGS_QDEBUG_2( "logs [ENG] <- LogsMatchesModel::setPredictiveSearch(), err: ", err ) |
|
222 return err; |
|
223 } |
|
224 |
|
225 // ----------------------------------------------------------------------------- |
|
226 // |
|
227 // ----------------------------------------------------------------------------- |
|
228 // |
|
229 void LogsMatchesModel::queryReady() |
|
230 { |
|
231 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::queryReady()" ); |
|
232 |
|
233 qDeleteAll(mMatches); |
|
234 mMatches.clear(); |
|
235 // Just store number of matches at this point, real data is read only |
|
236 // once it is asked first time from model. |
|
237 mResultCount = mLogsCntFinder->resultsCount(); |
|
238 reset(); |
|
239 } |
|
240 |
|
241 // ----------------------------------------------------------------------------- |
|
242 // |
|
243 // ----------------------------------------------------------------------------- |
|
244 // |
|
245 void LogsMatchesModel::eventsUpdated(const QModelIndex& first, const QModelIndex& last) |
|
246 { |
|
247 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::eventsUpdated()" ); |
|
248 bool updated( false ); |
|
249 QMap<LogsCntEntryHandle*, LogsEvent*>::const_iterator i; |
|
250 for (i = mSearchEvents.constBegin(); i != mSearchEvents.constEnd(); ++i){ |
|
251 if ( i.value()->index() >= first.row() && i.value()->index() <= last.row() ){ |
|
252 LogsCntEntry* entry = mLogsCntFinder->getEntry(*i.key()); |
|
253 if ( entry ){ |
|
254 updateSearchEntry(*entry, *i.value()); |
|
255 updated = true; |
|
256 } |
|
257 } |
|
258 } |
|
259 |
|
260 if ( updated ){ |
|
261 forceSearchQuery(); |
|
262 } |
|
263 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::eventsUpdated()" ); |
|
264 } |
|
265 |
|
266 // ----------------------------------------------------------------------------- |
|
267 // |
|
268 // ----------------------------------------------------------------------------- |
|
269 // |
|
270 void LogsMatchesModel::eventsAdded(const QModelIndex& parent, int first, int last) |
|
271 { |
|
272 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::eventsAdded()" ); |
|
273 Q_UNUSED(parent); |
|
274 readEvents(first, last); |
|
275 forceSearchQuery(); |
|
276 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::eventsAdded()" ); |
|
277 } |
|
278 |
|
279 // ----------------------------------------------------------------------------- |
|
280 // |
|
281 // ----------------------------------------------------------------------------- |
|
282 // |
|
283 void LogsMatchesModel::eventsRemoved(const QModelIndex& parent, int first, int last) |
|
284 { |
|
285 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::eventsRemoved()" ); |
|
286 Q_UNUSED(parent); |
|
287 bool removed( false ); |
|
288 QMap<LogsCntEntryHandle*, LogsEvent*>::iterator i; |
|
289 for (i = mSearchEvents.begin(); i != mSearchEvents.end(); ++i){ |
|
290 if ( i.value()->index() >= first && i.value()->index() <= last && |
|
291 !i.value()->isInView() ){ |
|
292 mLogsCntFinder->deleteEntry(*i.key()); |
|
293 i = mSearchEvents.erase(i); |
|
294 removed = true; |
|
295 } |
|
296 } |
|
297 |
|
298 if ( removed ){ |
|
299 forceSearchQuery(); |
|
300 } |
|
301 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::eventsRemoved()" ); |
|
302 } |
|
303 |
|
304 // ----------------------------------------------------------------------------- |
|
305 // |
|
306 // ----------------------------------------------------------------------------- |
|
307 // |
|
308 void LogsMatchesModel::initPredictiveSearch() |
|
309 { |
|
310 int searchStatus = mDbConnector->predictiveSearchStatus(); |
|
311 //searchStatus equal to 0 means that search should be permanently disabled |
|
312 if (searchStatus != 0) { |
|
313 mSearchEnabled = (searchStatus == 1 || searchStatus < 0); |
|
314 |
|
315 mLogsCntFinder = new LogsCntFinder(LogsCommonData::getInstance().contactManager()); |
|
316 connect(mLogsCntFinder, SIGNAL(queryReady()),this, SLOT(queryReady())); |
|
317 |
|
318 connect( &mParentModel, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)), |
|
319 this, SLOT(eventsUpdated(const QModelIndex&,const QModelIndex&))); |
|
320 connect( &mParentModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)), |
|
321 this, SLOT(eventsAdded(const QModelIndex&,int,int))); |
|
322 connect( &mParentModel, SIGNAL(rowsRemoved(const QModelIndex&,int,int)), |
|
323 this, SLOT(eventsRemoved(const QModelIndex&,int,int))); |
|
324 connect( &mParentModel, SIGNAL(modelReset()), this, SLOT(doModelReset())); |
|
325 readEvents(0, mParentModel.rowCount()); |
|
326 mIconManager = new LogsThumbIconManager(); |
|
327 connect(mIconManager, SIGNAL(contactIconReady(int)), |
|
328 this, SLOT(updateContactIcon(int))); |
|
329 } |
|
330 } |
|
331 |
|
332 // ----------------------------------------------------------------------------- |
|
333 // Add result container but don't get real data yet. Get data once it is |
|
334 // really needed. |
|
335 // ----------------------------------------------------------------------------- |
|
336 // |
|
337 LogsMatchesModelItemContainer* LogsMatchesModel::addSearchResult(int resultIndex) |
|
338 { |
|
339 LogsMatchesModelItemContainer* item = new LogsMatchesModelItemContainer( |
|
340 mParentModel, *mIconManager, resultIndex); |
|
341 mMatches.append(item); |
|
342 return item; |
|
343 } |
|
344 |
|
345 // ----------------------------------------------------------------------------- |
|
346 // Fill result container with real data if it does not yet contain it |
|
347 // ----------------------------------------------------------------------------- |
|
348 // |
|
349 bool LogsMatchesModel::updateSearchResult(LogsMatchesModelItemContainer& item) const |
|
350 { |
|
351 bool updated( false ); |
|
352 int resIndex = item.resultIndex(); |
|
353 if ( item.isNull() && resIndex >= 0 && mLogsCntFinder |
|
354 && resIndex < mLogsCntFinder->resultsCount() ){ |
|
355 const LogsCntEntry& result = mLogsCntFinder->resultAt(resIndex); |
|
356 if ( result.type() == LogsCntEntry::EntryTypeHistory ){ |
|
357 LogsEvent* event = mSearchEvents.value(result.handle()); |
|
358 if ( event ){ |
|
359 item.setEvent(*event); |
|
360 item.updateData(result); |
|
361 updated = true; |
|
362 } |
|
363 } else { |
|
364 item.setContact(result.contactId()); |
|
365 item.updateData(result); |
|
366 updated = true; |
|
367 } |
|
368 } |
|
369 return updated; |
|
370 } |
|
371 |
|
372 // ----------------------------------------------------------------------------- |
|
373 // |
|
374 // ----------------------------------------------------------------------------- |
|
375 // |
|
376 void LogsMatchesModel::readEvents(int first, int last) |
|
377 { |
|
378 for ( int i = first; i < mParentModel.rowCount() && i <= last; ++i ){ |
|
379 LogsEvent* event = qVariantValue<LogsEvent*>( |
|
380 mParentModel.data( mParentModel.index(i, 0), LogsModel::RoleFullEvent ) ); |
|
381 if ( event ){ |
|
382 QObject* entryHandle = new QObject(this); |
|
383 LogsCntEntry* entry = new LogsCntEntry(*entryHandle, 0); |
|
384 updateSearchEntry(*entry, *event); |
|
385 mLogsCntFinder->insertEntry(i, entry); |
|
386 mSearchEvents.insert(entryHandle, event); |
|
387 } |
|
388 } |
|
389 } |
|
390 |
|
391 // ----------------------------------------------------------------------------- |
|
392 // |
|
393 // ----------------------------------------------------------------------------- |
|
394 // |
|
395 void LogsMatchesModel::logsMatches(const QString& pattern) |
|
396 { |
|
397 // Do user inputted searches in async manner to avoid from |
|
398 // blocking next input. This also decreases amount of queries when |
|
399 // user types fast. |
|
400 getLogsMatches(pattern, true); |
|
401 } |
|
402 |
|
403 // ----------------------------------------------------------------------------- |
|
404 // |
|
405 // ----------------------------------------------------------------------------- |
|
406 // |
|
407 LogsContact* LogsMatchesModel::createContact(const QString& number) |
|
408 { |
|
409 LogsContact* contact = new LogsContact(number, *mDbConnector); |
|
410 connect( contact, SIGNAL(saveCompleted(bool)), this, SLOT(forceSearchQuery()) ); |
|
411 return contact; |
|
412 } |
|
413 |
|
414 // ----------------------------------------------------------------------------- |
|
415 // |
|
416 // ----------------------------------------------------------------------------- |
|
417 // |
|
418 void LogsMatchesModel::getLogsMatches(const QString& pattern, |
|
419 bool async, |
|
420 bool force ) |
|
421 { |
|
422 LOGS_QDEBUG_2( "logs [ENG] -> LogsMatchesModel::getLogsMatches(), pattern:", pattern ); |
|
423 mCurrentSearchPattern = pattern; |
|
424 if ( force ){ |
|
425 // Clear previous results immeditely as some data associated with results |
|
426 // might be already gone. |
|
427 doModelReset(); |
|
428 mPrevSearchPattern.clear(); |
|
429 } |
|
430 if ( async ){ |
|
431 QMetaObject::invokeMethod(this, "doSearchQuery", Qt::QueuedConnection ); |
|
432 } else { |
|
433 doSearchQuery(); |
|
434 } |
|
435 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::getLogsMatches()" ); |
|
436 } |
|
437 |
|
438 // ----------------------------------------------------------------------------- |
|
439 // |
|
440 // ----------------------------------------------------------------------------- |
|
441 // |
|
442 void LogsMatchesModel::doSearchQuery() |
|
443 { |
|
444 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::doSearchQuery()" ); |
|
445 if (mCurrentSearchPattern != mPrevSearchPattern && mSearchEnabled){ |
|
446 mPrevSearchPattern = mCurrentSearchPattern; |
|
447 if (mCurrentSearchPattern.length() > 0) { |
|
448 LOGS_QDEBUG( "logs [ENG] do search" ); |
|
449 mLogsCntFinder->predictiveSearchQuery( mCurrentSearchPattern ); |
|
450 } else { |
|
451 // Clear old results for empty search pattern |
|
452 doModelReset(); |
|
453 } |
|
454 } else { |
|
455 LOGS_QDEBUG( "logs [ENG] pattern hasn't changed or search is OFF" ); |
|
456 } |
|
457 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::doSearchQuery()" ); |
|
458 } |
|
459 |
|
460 // ----------------------------------------------------------------------------- |
|
461 // |
|
462 // ----------------------------------------------------------------------------- |
|
463 // |
|
464 void LogsMatchesModel::doModelReset() |
|
465 { |
|
466 qDeleteAll(mMatches); |
|
467 mMatches.clear(); |
|
468 mResultCount = 0; |
|
469 reset(); |
|
470 } |
|
471 |
|
472 // ----------------------------------------------------------------------------- |
|
473 // |
|
474 // ----------------------------------------------------------------------------- |
|
475 // |
|
476 void LogsMatchesModel::forceSearchQuery() |
|
477 { |
|
478 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::forceSearchQuery()" ); |
|
479 getLogsMatches( mCurrentSearchPattern, true, true ); |
|
480 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::forceSearchQuery()" ); |
|
481 } |
|
482 |
|
483 // ----------------------------------------------------------------------------- |
|
484 // |
|
485 // ----------------------------------------------------------------------------- |
|
486 // |
|
487 void LogsMatchesModel::updateSearchEntry(LogsCntEntry& entry, LogsEvent& event) |
|
488 { |
|
489 if ( event.remoteParty().length() > 0 ) { |
|
490 entry.setFirstName(event.remoteParty()); |
|
491 } else if ( event.number().length() > 0 ) { |
|
492 entry.setPhoneNumber(stripPhoneNumber(event.number())); |
|
493 } else if ( event.eventType() == LogsEvent::TypeVoIPCall && event.logsEventData() ){ |
|
494 if ( event.logsEventData()->isCsCompatible() ){ |
|
495 entry.setPhoneNumber(stripPhoneNumber(event.logsEventData()->remoteUrl())); |
|
496 } else { |
|
497 entry.setFirstName(event.logsEventData()->remoteUrl()); |
|
498 } |
|
499 } |
|
500 } |
|
501 |
|
502 // ----------------------------------------------------------------------------- |
|
503 //Update the icon for the contact with the given index (row) |
|
504 //param index of the contact |
|
505 // ----------------------------------------------------------------------------- |
|
506 // |
|
507 void LogsMatchesModel::updateContactIcon(int index) |
|
508 { |
|
509 LOGS_QDEBUG( "logs [ENG] -> LogsMatchesModel::updateContactIcon()" ); |
|
510 QModelIndex modelIndex = createIndex(index, 0); |
|
511 emit dataChanged(modelIndex, modelIndex); |
|
512 LOGS_QDEBUG( "logs [ENG] <- LogsMatchesModel::updateContactIcon()" ); |
|
513 } |
|
514 |
|
515 // ----------------------------------------------------------------------------- |
|
516 // |
|
517 // ----------------------------------------------------------------------------- |
|
518 // |
|
519 QString LogsMatchesModel::stripPhoneNumber(const QString& phoneNumber) const |
|
520 { |
|
521 if ( phoneNumber.startsWith(QLatin1Char('+')) ){ |
|
522 return phoneNumber.mid(1); |
|
523 } |
|
524 return phoneNumber; |
|
525 } |
|
526 |
|
527 // ----------------------------------------------------------------------------- |
|
528 // |
|
529 // ----------------------------------------------------------------------------- |
|
530 // |
|
531 LogsMatchesModelItemContainer::LogsMatchesModelItemContainer( |
|
532 LogsAbstractModel& parentModel, |
|
533 LogsThumbIconManager& thumbIconManager, |
|
534 int resultIndex) : |
|
535 LogsModelItemContainer(), |
|
536 mParentModel(parentModel), |
|
537 mContactId( 0 ), |
|
538 mIconManager(thumbIconManager), |
|
539 mResultIndex(resultIndex) |
|
540 { |
|
541 |
|
542 } |
|
543 |
|
544 // ----------------------------------------------------------------------------- |
|
545 // |
|
546 // ----------------------------------------------------------------------------- |
|
547 // |
|
548 LogsMatchesModelItemContainer::~LogsMatchesModelItemContainer() |
|
549 { |
|
550 delete mEvent; |
|
551 } |
|
552 |
|
553 // ----------------------------------------------------------------------------- |
|
554 // |
|
555 // ----------------------------------------------------------------------------- |
|
556 // |
|
557 void LogsMatchesModelItemContainer::setEvent(const LogsEvent& event) |
|
558 { |
|
559 delete mEvent; |
|
560 mEvent = 0; |
|
561 mEvent = new LogsEvent(event); |
|
562 } |
|
563 |
|
564 // ----------------------------------------------------------------------------- |
|
565 // |
|
566 // ----------------------------------------------------------------------------- |
|
567 // |
|
568 void LogsMatchesModelItemContainer::setContact(unsigned int contactId) |
|
569 { |
|
570 mContactId = contactId; |
|
571 } |
|
572 |
|
573 // ----------------------------------------------------------------------------- |
|
574 // |
|
575 // ----------------------------------------------------------------------------- |
|
576 // |
|
577 unsigned int LogsMatchesModelItemContainer::contact() const |
|
578 { |
|
579 return mContactId; |
|
580 } |
|
581 |
|
582 // ----------------------------------------------------------------------------- |
|
583 // |
|
584 // ----------------------------------------------------------------------------- |
|
585 // |
|
586 QString LogsMatchesModelItemContainer::number() const |
|
587 { |
|
588 QString num; |
|
589 if ( mEvent ){ |
|
590 num = mEvent->getNumberForCalling(); |
|
591 } else if ( mContactId > 0 ) { |
|
592 QContact contact = LogsCommonData::getInstance().contactManager().contact( mContactId ); |
|
593 QContactPhoneNumber contactNum = |
|
594 contact.detail( QContactPhoneNumber::DefinitionName ); |
|
595 num = contactNum.value(QContactPhoneNumber::FieldNumber); |
|
596 } |
|
597 return num; |
|
598 } |
|
599 |
|
600 // ----------------------------------------------------------------------------- |
|
601 // |
|
602 // ----------------------------------------------------------------------------- |
|
603 // |
|
604 QString LogsMatchesModelItemContainer::contactName() const |
|
605 { |
|
606 return mContactName; |
|
607 } |
|
608 |
|
609 // ----------------------------------------------------------------------------- |
|
610 // |
|
611 // ----------------------------------------------------------------------------- |
|
612 // |
|
613 bool LogsMatchesModelItemContainer::isNull() const |
|
614 { |
|
615 return ( !mEvent && !mContactId ); |
|
616 } |
|
617 |
|
618 // ----------------------------------------------------------------------------- |
|
619 // |
|
620 // ----------------------------------------------------------------------------- |
|
621 // |
|
622 bool LogsMatchesModelItemContainer::isEventMatch() const |
|
623 { |
|
624 return ( mEvent != 0 ); |
|
625 } |
|
626 |
|
627 // ----------------------------------------------------------------------------- |
|
628 // |
|
629 // ----------------------------------------------------------------------------- |
|
630 // |
|
631 QStringList LogsMatchesModelItemContainer::texts() |
|
632 { |
|
633 QStringList list; |
|
634 if ( mEvent ){ |
|
635 list << mFormattedCallerId; |
|
636 list << mEvent->time().toTimeSpec(Qt::LocalTime).toString(); |
|
637 } else if ( mContactId > 0 ) { |
|
638 list << mContactName; |
|
639 list << mContactNumber; |
|
640 } |
|
641 return list; |
|
642 } |
|
643 |
|
644 // ----------------------------------------------------------------------------- |
|
645 // |
|
646 // ----------------------------------------------------------------------------- |
|
647 // |
|
648 QList<QVariant> LogsMatchesModelItemContainer::icons(int row) |
|
649 { |
|
650 QList<QVariant> icons; |
|
651 if ( mEvent ) { |
|
652 mParentModel.getDecorationData(*mEvent, icons); |
|
653 } else if ( mContactId > 0 ) { |
|
654 QIcon& icon = mIconManager.contactIcon( mAvatarPath, row ); |
|
655 icons.append(icon); |
|
656 } |
|
657 return icons; |
|
658 |
|
659 } |
|
660 |
|
661 // ----------------------------------------------------------------------------- |
|
662 // |
|
663 // ----------------------------------------------------------------------------- |
|
664 // |
|
665 void LogsMatchesModelItemContainer::updateData(const LogsCntEntry& entry) |
|
666 { |
|
667 if ( mEvent ){ |
|
668 mFormattedCallerId = getFormattedCallerId(entry); |
|
669 } else if ( mContactId > 0 ){ |
|
670 getFormattedContactInfo(entry, mContactName, mContactNumber); |
|
671 mAvatarPath.clear(); |
|
672 mAvatarPath = entry.avatarPath(); |
|
673 } |
|
674 } |
|
675 |
|
676 // ----------------------------------------------------------------------------- |
|
677 // |
|
678 // ----------------------------------------------------------------------------- |
|
679 // |
|
680 int LogsMatchesModelItemContainer::resultIndex() const |
|
681 { |
|
682 return mResultIndex; |
|
683 } |
|
684 |
|
685 // ----------------------------------------------------------------------------- |
|
686 // Note: Mapping of search result entry into caller ID is depended on |
|
687 // updateSearchEntry() implemention. |
|
688 // ----------------------------------------------------------------------------- |
|
689 // |
|
690 QString LogsMatchesModelItemContainer::getFormattedCallerId( |
|
691 const LogsCntEntry& entry) const |
|
692 { |
|
693 QString callerId; |
|
694 foreach( LogsCntText name, entry.firstName() ) { |
|
695 callerId.append( name.richText() ); |
|
696 if ( name.text().length() > 0 ) { |
|
697 callerId.append(" "); |
|
698 } |
|
699 } |
|
700 |
|
701 if ( callerId.length() == 0 ) { |
|
702 callerId = entry.phoneNumber().richText(); |
|
703 } |
|
704 |
|
705 return callerId.trimmed(); |
|
706 } |
|
707 |
|
708 // ----------------------------------------------------------------------------- |
|
709 // |
|
710 // ----------------------------------------------------------------------------- |
|
711 // |
|
712 void LogsMatchesModelItemContainer::getFormattedContactInfo( |
|
713 const LogsCntEntry& entry, |
|
714 QString& contactName, |
|
715 QString& contactNumber ) const |
|
716 { |
|
717 contactName.clear(); |
|
718 foreach( LogsCntText name, entry.firstName() ) { |
|
719 contactName.append( name.richText() ); |
|
720 if ( name.text().length() > 0 ) { |
|
721 contactName.append(" "); |
|
722 } |
|
723 } |
|
724 |
|
725 foreach( LogsCntText lastname, entry.lastName() ) { |
|
726 contactName.append( lastname.richText() ); |
|
727 if ( lastname.text().length() > 0 ) { |
|
728 contactName.append(" "); |
|
729 } |
|
730 } |
|
731 contactName = contactName.trimmed(); |
|
732 |
|
733 contactNumber = entry.phoneNumber().richText(); |
|
734 } |