68
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 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: Private implementation of Contact History Model API
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include "emailmru.h"
|
74
|
19 |
#include "emailtrace.h"
|
68
|
20 |
#include "nmcontacthistorymodel_p.h"
|
|
21 |
|
|
22 |
// --------------------------------------------------------------------------
|
|
23 |
// Start of implementation (NmContactHistoryModelPrivate)
|
|
24 |
// --------------------------------------------------------------------------
|
|
25 |
/*!
|
|
26 |
Constructor of NmContactHistoryModelPrivate
|
|
27 |
|
|
28 |
\param type The valid values are described by
|
|
29 |
NmContactHistoryModelPrivate::HistoryModelType enum.
|
|
30 |
*/
|
|
31 |
NmContactHistoryModelPrivate::NmContactHistoryModelPrivate(
|
|
32 |
const NmContactHistoryModelType modelType) :
|
|
33 |
mType(modelType),
|
74
|
34 |
mContactManager(NULL),
|
68
|
35 |
mModelReady(false)
|
|
36 |
{
|
74
|
37 |
NM_FUNCTION;
|
|
38 |
|
68
|
39 |
mContactManager = new QContactManager("symbian");
|
|
40 |
mNameOrder = EmailMRU::nameOrder();
|
|
41 |
}
|
|
42 |
|
|
43 |
/*!
|
|
44 |
Destructor of ContactHistoryModel
|
|
45 |
*/
|
|
46 |
NmContactHistoryModelPrivate::~NmContactHistoryModelPrivate()
|
|
47 |
{
|
74
|
48 |
NM_FUNCTION;
|
|
49 |
|
68
|
50 |
delete mContactManager;
|
|
51 |
mPrivateItemList.clear();
|
|
52 |
mModelItemList.clear();
|
|
53 |
}
|
|
54 |
|
|
55 |
/*!
|
|
56 |
Searches contact & MRU databases according to query parameters.
|
76
|
57 |
Emits queryCompleted() when ready.
|
68
|
58 |
|
|
59 |
\param query Text to be searched
|
|
60 |
\return none
|
|
61 |
|
|
62 |
Note:: QContactManager supports max. ten characters long queries.
|
|
63 |
*/
|
|
64 |
void NmContactHistoryModelPrivate::queryDatabases(const QString& query)
|
|
65 |
{
|
74
|
66 |
NM_FUNCTION;
|
|
67 |
|
68
|
68 |
mModelReady = false;
|
|
69 |
// Clear contacts in the list
|
|
70 |
mPrivateItemList.clear();
|
|
71 |
mModelItemList.clear();
|
|
72 |
mMruList.clear();
|
|
73 |
mMruMatches.clear();
|
|
74 |
|
74
|
75 |
mNameOrder = EmailMRU::nameOrder();
|
|
76 |
|
68
|
77 |
// Modify search to suit our needs
|
|
78 |
// Space must be removed, because it is understood as logigal AND
|
|
79 |
// with QContactManager API
|
|
80 |
QString modifiedQuery = query;
|
|
81 |
|
|
82 |
int spcPosition = query.indexOf(" ");
|
|
83 |
|
76
|
84 |
if ( spcPosition != -1 ) {
|
68
|
85 |
modifiedQuery = query.left(spcPosition);
|
74
|
86 |
}
|
68
|
87 |
|
74
|
88 |
// Get matching MRU items
|
68
|
89 |
queryMruDatabase(modifiedQuery);
|
74
|
90 |
|
|
91 |
// Populate mPrivateItemList with matching MRU items.
|
68
|
92 |
populateListWithMruItems(modifiedQuery);
|
|
93 |
|
74
|
94 |
// Get matching IDs from Contacts DB
|
|
95 |
QList<QContactLocalId> cnt_ids = queryContactDatabase(modifiedQuery);
|
68
|
96 |
|
74
|
97 |
// Populate mPrivateItemList with contact items.
|
|
98 |
populateListWithContactItems(cnt_ids, modifiedQuery);
|
68
|
99 |
|
|
100 |
|
|
101 |
emit queryCompleted(0);
|
|
102 |
}
|
|
103 |
|
|
104 |
/*!
|
|
105 |
Internal helper function for queryDatabases()
|
|
106 |
Not meant to be used alone.
|
|
107 |
|
|
108 |
\param query Text to be searched
|
|
109 |
\return list of Contact items.
|
|
110 |
\sa queryDatabases( )
|
|
111 |
*/
|
|
112 |
QList<QContactLocalId> NmContactHistoryModelPrivate::queryContactDatabase(
|
|
113 |
const QString &query)
|
|
114 |
{
|
74
|
115 |
NM_FUNCTION;
|
|
116 |
|
68
|
117 |
// Define filter
|
|
118 |
QContactDetailFilter df;
|
|
119 |
|
|
120 |
df.setDetailDefinitionName(QContactEmailAddress::DefinitionName,
|
|
121 |
QContactEmailAddress::FieldEmailAddress );
|
|
122 |
|
|
123 |
df.setMatchFlags( QContactFilter::MatchKeypadCollation );
|
|
124 |
|
|
125 |
// Construct pattern
|
|
126 |
QString pattern = query + QChar(30) + QString("vqwerty");
|
|
127 |
df.setValue( pattern );
|
|
128 |
|
|
129 |
// Get matching IDs
|
|
130 |
QList<QContactLocalId> cnt_ids = mContactManager->contactIds( df );
|
|
131 |
|
|
132 |
return cnt_ids;
|
|
133 |
}
|
|
134 |
|
|
135 |
/*!
|
|
136 |
Internal helper function for queryDatabases()
|
|
137 |
Not meant to be used alone.
|
|
138 |
|
|
139 |
\param query Text to be searched from MRU
|
|
140 |
\return true on success.
|
|
141 |
\sa queryDatabases( )
|
|
142 |
*/
|
|
143 |
bool NmContactHistoryModelPrivate::queryMruDatabase(
|
|
144 |
const QString &query)
|
|
145 |
{
|
74
|
146 |
NM_FUNCTION;
|
|
147 |
bool rVal = false;
|
|
148 |
|
76
|
149 |
if (mType == EmailAddressModel) {
|
68
|
150 |
bool mruListFilled = fillMruMatchList();
|
|
151 |
|
76
|
152 |
if (!mruListFilled) {
|
74
|
153 |
return rVal;
|
68
|
154 |
}
|
|
155 |
|
|
156 |
QHashIterator<QString, QString> i(mMruList);
|
|
157 |
|
76
|
158 |
while (i.hasNext()) {
|
68
|
159 |
i.next();
|
|
160 |
|
|
161 |
QString address = i.key();
|
|
162 |
QString name = i.value();
|
|
163 |
|
76
|
164 |
if ((name.length() == 0) && (address.contains('@'))) {
|
68
|
165 |
name = address.left(address.indexOf("@"));
|
|
166 |
}
|
|
167 |
|
76
|
168 |
if (address.contains(query, Qt::CaseInsensitive)) {
|
68
|
169 |
mMruMatches.insert(name, address);
|
|
170 |
}
|
76
|
171 |
else if (name.contains(query, Qt::CaseInsensitive)) {
|
68
|
172 |
mMruMatches.insert(name, address);
|
|
173 |
}
|
74
|
174 |
}
|
|
175 |
rVal = true;
|
68
|
176 |
}
|
|
177 |
|
74
|
178 |
return rVal;
|
68
|
179 |
}
|
|
180 |
|
|
181 |
/*!
|
|
182 |
Internal helper function for queryDatabases()
|
|
183 |
Not meant to be used alone.
|
|
184 |
|
|
185 |
\param cnt_ids List of contact IDs
|
|
186 |
\param modifiedQuery Query
|
|
187 |
\sa queryDatabases( )
|
|
188 |
*/
|
|
189 |
void NmContactHistoryModelPrivate::populateListWithContactItems(
|
|
190 |
const QList<QContactLocalId> cnt_ids,
|
|
191 |
const QString &modifiedQuery)
|
|
192 |
{
|
74
|
193 |
NM_FUNCTION;
|
|
194 |
|
68
|
195 |
int cntCount = cnt_ids.count();
|
|
196 |
// Populate mPrivateItemList with contact items.
|
76
|
197 |
for ( int cntIndex = 0; cntIndex < cntCount; cntIndex++) {
|
68
|
198 |
QContactLocalId cid = cnt_ids.at( cntIndex );
|
|
199 |
// Fetch extended contact information from DB by ID
|
|
200 |
QContact contact = mContactManager->contact( cid );
|
|
201 |
|
|
202 |
// Get number of associated emails for contact
|
|
203 |
int numberOfEmails = contact.details(
|
|
204 |
QContactEmailAddress::DefinitionName).count();
|
|
205 |
|
76
|
206 |
if (numberOfEmails == 0) {
|
|
207 |
continue;
|
68
|
208 |
}
|
|
209 |
|
|
210 |
// Get contact name details
|
|
211 |
QContactName contactName = contact.detail(
|
|
212 |
QContactName::DefinitionName);
|
|
213 |
|
|
214 |
QString firstName = contactName.value(
|
|
215 |
QContactName::FieldFirstName);
|
|
216 |
QString lastName = contactName.value(
|
|
217 |
QContactName::FieldLastName);
|
|
218 |
|
76
|
219 |
for (int emailIndex = 0; emailIndex < numberOfEmails; emailIndex++) {
|
68
|
220 |
// For Display name (ex. John Doe)
|
|
221 |
NmContactHistoryModelSubItem itemSubItem1;
|
|
222 |
// For Email address (ex. john.doe@company.com)
|
|
223 |
NmContactHistoryModelSubItem itemSubItem2;
|
|
224 |
|
|
225 |
int queryLength = modifiedQuery.length();
|
|
226 |
|
|
227 |
// Obey the contacts setting (first name last name order)
|
|
228 |
itemSubItem1.mItemText = obeyContactOrder(firstName, lastName);
|
|
229 |
|
76
|
230 |
if( firstName.indexOf(modifiedQuery, 0, Qt::CaseInsensitive) == 0) {
|
68
|
231 |
itemSubItem1.mMatchingRanges.append(0);
|
|
232 |
itemSubItem1.mMatchingRanges.append(queryLength-1);
|
|
233 |
}
|
|
234 |
|
76
|
235 |
if( lastName.indexOf(modifiedQuery, 0, Qt::CaseInsensitive) == 0) {
|
68
|
236 |
int indexLN = itemSubItem1.mItemText.indexOf(lastName,
|
|
237 |
firstName.length(),
|
|
238 |
Qt::CaseInsensitive );
|
|
239 |
|
|
240 |
itemSubItem1.mMatchingRanges.append(indexLN);
|
|
241 |
itemSubItem1.mMatchingRanges.append(indexLN+(queryLength-1));
|
|
242 |
}
|
|
243 |
|
|
244 |
itemSubItem2.mItemText = contact.details (
|
|
245 |
QContactEmailAddress::DefinitionName )[emailIndex].value (
|
|
246 |
QContactEmailAddress::FieldEmailAddress );
|
|
247 |
|
|
248 |
if (itemSubItem2.mItemText.indexOf(modifiedQuery, 0,
|
76
|
249 |
Qt::CaseInsensitive) == 0 ) {
|
68
|
250 |
itemSubItem2.mMatchingRanges.append(0);
|
|
251 |
itemSubItem2.mMatchingRanges.append(queryLength-1);
|
|
252 |
}
|
|
253 |
|
|
254 |
if ((itemSubItem1.mMatchingRanges.count() != 0) ||
|
76
|
255 |
(itemSubItem2.mMatchingRanges.count() != 0) ) {
|
68
|
256 |
QSharedPointer<NmContactHistoryModelItemData> itemData
|
|
257 |
(new NmContactHistoryModelItemData);
|
|
258 |
|
|
259 |
itemData->mContactId = cid;
|
|
260 |
itemData->mItems.append(itemSubItem1);
|
|
261 |
itemData->mItems.append(itemSubItem2);
|
|
262 |
|
|
263 |
mPrivateItemList.append(itemData);
|
|
264 |
}
|
|
265 |
}
|
|
266 |
}
|
|
267 |
}
|
|
268 |
|
|
269 |
/*!
|
|
270 |
Internal helper function for queryDatabases()
|
|
271 |
Not meant to be used alone.
|
|
272 |
|
|
273 |
\param modifiedQuery Query
|
|
274 |
\sa queryDatabases( )
|
|
275 |
*/
|
|
276 |
void NmContactHistoryModelPrivate::populateListWithMruItems(
|
|
277 |
const QString &query)
|
|
278 |
{
|
74
|
279 |
NM_FUNCTION;
|
|
280 |
|
|
281 |
QMapIterator<QString, QString> mruMatch(mMruMatches);
|
68
|
282 |
|
76
|
283 |
while (mruMatch.hasNext()) {
|
74
|
284 |
mruMatch.next();
|
68
|
285 |
// For Display name (ex. John Doe)
|
|
286 |
NmContactHistoryModelSubItem itemSubItem1;
|
|
287 |
// For Email address (ex. john.doe@company.com)
|
|
288 |
NmContactHistoryModelSubItem itemSubItem2;
|
|
289 |
|
74
|
290 |
QString name = mruMatch.key();
|
|
291 |
QString address = mruMatch.value();
|
|
292 |
|
|
293 |
itemSubItem1.mItemText = name;
|
|
294 |
itemSubItem2.mItemText = address;
|
68
|
295 |
|
|
296 |
// markup for first item.
|
76
|
297 |
if( name.indexOf(query, 0, Qt::CaseInsensitive) == 0) {
|
68
|
298 |
itemSubItem1.mMatchingRanges.append(0);
|
|
299 |
itemSubItem1.mMatchingRanges.append(query.length()-1);
|
|
300 |
}
|
|
301 |
|
|
302 |
QRegExp rx("[,\\s]", Qt::CaseInsensitive);
|
74
|
303 |
bool separatorExists = name.contains(rx);
|
68
|
304 |
|
76
|
305 |
if (separatorExists) {
|
74
|
306 |
int indexLN = name.indexOf(",", 0, Qt::CaseInsensitive);
|
68
|
307 |
|
76
|
308 |
if (indexLN == -1) {
|
74
|
309 |
indexLN = name.indexOf(" ", 0, Qt::CaseInsensitive);
|
68
|
310 |
}
|
|
311 |
|
76
|
312 |
if (indexLN > 0) {
|
74
|
313 |
int matchPos = name.indexOf(query, indexLN, Qt::CaseInsensitive);
|
68
|
314 |
|
76
|
315 |
if (matchPos > 0) {
|
68
|
316 |
itemSubItem1.mMatchingRanges.append(matchPos);
|
|
317 |
itemSubItem1.mMatchingRanges.append((matchPos+query.length())-1);
|
|
318 |
}
|
|
319 |
}
|
|
320 |
}
|
|
321 |
|
|
322 |
// markup for second item.
|
76
|
323 |
if (address.indexOf(query, 0, Qt::CaseInsensitive) == 0 ) {
|
68
|
324 |
itemSubItem2.mMatchingRanges.append(0);
|
|
325 |
itemSubItem2.mMatchingRanges.append(query.length()-1);
|
76
|
326 |
}
|
68
|
327 |
|
|
328 |
// If match, add to data list.
|
|
329 |
if ((itemSubItem1.mMatchingRanges.count() != 0) ||
|
76
|
330 |
(itemSubItem2.mMatchingRanges.count() != 0) ) {
|
68
|
331 |
QSharedPointer<NmContactHistoryModelItemData> itemData
|
|
332 |
(new NmContactHistoryModelItemData);
|
|
333 |
|
|
334 |
itemData->mContactId = 0; // No ID available, use 0.
|
|
335 |
itemData->mItems.append(itemSubItem1);
|
|
336 |
itemData->mItems.append(itemSubItem2);
|
|
337 |
|
|
338 |
mPrivateItemList.append(itemData);
|
|
339 |
}
|
|
340 |
}
|
|
341 |
}
|
|
342 |
|
|
343 |
/*!
|
|
344 |
After queryDatabases has completed this will be called from public class()
|
|
345 |
Not meant to be used alone. (NmContactHistoryModel is using this when needed)
|
|
346 |
|
|
347 |
\sa queryDatabases( )
|
|
348 |
*/
|
|
349 |
void NmContactHistoryModelPrivate::refreshDataModel()
|
|
350 |
{
|
74
|
351 |
NM_FUNCTION;
|
|
352 |
|
68
|
353 |
mModelItemList.clear();
|
|
354 |
|
|
355 |
for (int cntIndex = 0;
|
|
356 |
cntIndex < mPrivateItemList.count();
|
76
|
357 |
cntIndex++) {
|
68
|
358 |
// Construct modelItem to be added to list.
|
|
359 |
NmContactHistoryModelItem modelItem;
|
|
360 |
|
|
361 |
QSharedPointer<NmContactHistoryModelItemData> itemData(
|
|
362 |
mPrivateItemList[cntIndex]);
|
|
363 |
|
|
364 |
int id = itemData->mContactId;
|
|
365 |
modelItem.setContactId(itemData->mContactId);
|
|
366 |
|
|
367 |
for (int itemIndex = 0;
|
|
368 |
itemIndex < itemData->mItems.count();
|
76
|
369 |
itemIndex++) {
|
68
|
370 |
QString key = itemData->mItems[itemIndex].mItemText;
|
|
371 |
modelItem.appendSubItem(itemData->mItems[itemIndex]);
|
|
372 |
}
|
|
373 |
|
|
374 |
// Append modelItem to list.
|
|
375 |
mModelItemList.append(modelItem);
|
|
376 |
}
|
|
377 |
|
|
378 |
// Notify views that we are done updating model data.
|
|
379 |
mModelReady = true;
|
|
380 |
|
|
381 |
}
|
|
382 |
|
|
383 |
/*!
|
74
|
384 |
This is called by public class NmContactHistoryModel when model row count is requested
|
68
|
385 |
Not meant to be used alone.
|
|
386 |
|
|
387 |
*/
|
74
|
388 |
int NmContactHistoryModelPrivate::modelRowCount(const QModelIndex &parent) const
|
68
|
389 |
{
|
74
|
390 |
NM_FUNCTION;
|
|
391 |
|
68
|
392 |
Q_UNUSED(parent);
|
|
393 |
return mModelItemList.count();
|
|
394 |
}
|
|
395 |
|
|
396 |
/*!
|
74
|
397 |
This is called by public class NmContactHistoryModel when private data count is required
|
|
398 |
ie. When model isn't populated yet but data query is complete.
|
|
399 |
Not meant to be used alone.
|
|
400 |
|
|
401 |
*/
|
|
402 |
int NmContactHistoryModelPrivate::privateDataCount() const
|
|
403 |
{
|
|
404 |
NM_FUNCTION;
|
|
405 |
return mPrivateItemList.count();
|
|
406 |
}
|
|
407 |
|
|
408 |
/*!
|
68
|
409 |
This is called by public class NmContactHistoryModel when data() is requested
|
|
410 |
Not meant to be used alone.
|
|
411 |
|
|
412 |
*/
|
|
413 |
QVariant NmContactHistoryModelPrivate::data(const QModelIndex &index, int role) const
|
|
414 |
{
|
74
|
415 |
NM_FUNCTION;
|
76
|
416 |
QVariant rVariant = QVariant();
|
|
417 |
|
|
418 |
if ( mModelReady ) {
|
68
|
419 |
if (!index.isValid() ||
|
|
420 |
index.row() >= mModelItemList.count() ||
|
76
|
421 |
index.row() < 0) {
|
|
422 |
rVariant = QVariant();
|
68
|
423 |
}
|
76
|
424 |
else if (role == Qt::DisplayRole) {
|
68
|
425 |
NmContactHistoryModelItem i = mModelItemList.at(index.row());
|
76
|
426 |
rVariant.setValue(i);
|
68
|
427 |
}
|
|
428 |
}
|
|
429 |
|
76
|
430 |
return rVariant;
|
68
|
431 |
}
|
|
432 |
|
|
433 |
/*!
|
|
434 |
Fills MruMatchList, called from queryMruDatabase.
|
|
435 |
Function uses Email MRU API to fetch information from CenRep
|
|
436 |
|
|
437 |
\sa queryMruDatabase( )
|
|
438 |
*/
|
|
439 |
bool NmContactHistoryModelPrivate::fillMruMatchList()
|
|
440 |
{
|
74
|
441 |
NM_FUNCTION;
|
|
442 |
|
68
|
443 |
EmailMRU *mru = new EmailMRU();
|
74
|
444 |
bool rVal = false;
|
76
|
445 |
int updateCount = 0;
|
68
|
446 |
|
76
|
447 |
int addressCount = mru->entryCount();
|
68
|
448 |
|
76
|
449 |
for (int i = addressCount; i > 0; i--) {
|
|
450 |
QString key, value; // "Email or Number" & "Display Name"
|
|
451 |
bool success;
|
68
|
452 |
|
76
|
453 |
success = mru->getEntry(i, value, key);
|
68
|
454 |
|
76
|
455 |
if (success) {
|
|
456 |
mMruList.insertMulti(key, value);
|
|
457 |
updateCount++;
|
68
|
458 |
}
|
76
|
459 |
}
|
68
|
460 |
|
76
|
461 |
delete mru;
|
|
462 |
|
|
463 |
if (updateCount == addressCount) {
|
74
|
464 |
rVal = true;
|
76
|
465 |
}
|
74
|
466 |
|
|
467 |
return rVal;
|
68
|
468 |
}
|
|
469 |
|
|
470 |
/*!
|
|
471 |
This is called from populateListWithContactItems() when correct order of
|
|
472 |
first name, last name setting is required
|
|
473 |
|
|
474 |
\param firstName First name
|
|
475 |
\param lastName Last name
|
|
476 |
|
|
477 |
\sa populateListWithContactItems( )
|
|
478 |
|
|
479 |
\return concennated string formatted as contact setting specifies.
|
|
480 |
*/
|
|
481 |
QString NmContactHistoryModelPrivate::obeyContactOrder(const QString &firstName,
|
74
|
482 |
const QString &lastName) const
|
68
|
483 |
{
|
74
|
484 |
NM_FUNCTION;
|
|
485 |
|
68
|
486 |
QString result;
|
|
487 |
|
76
|
488 |
switch (mNameOrder) {
|
|
489 |
case LastNameFirstName:
|
|
490 |
result = lastName + " " + firstName;
|
68
|
491 |
break;
|
|
492 |
|
76
|
493 |
case LastNameCommaFirstName:
|
|
494 |
result = lastName + ", " + firstName;
|
68
|
495 |
break;
|
|
496 |
|
76
|
497 |
case FirstNameLastName:
|
|
498 |
result = firstName + " " + lastName;
|
68
|
499 |
break;
|
|
500 |
|
76
|
501 |
default:
|
|
502 |
result = firstName + " " + lastName;
|
68
|
503 |
break;
|
|
504 |
}
|
|
505 |
|
|
506 |
return result;
|
|
507 |
}
|
|
508 |
|
|
509 |
// --------------------------------------------------------------------------
|
|
510 |
// End of implementation (NmContactHistoryModelPrivate)
|
|
511 |
// --------------------------------------------------------------------------
|
|
512 |
|