81
|
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 <QSet>
|
|
19 |
#include <qtcontacts.h>
|
|
20 |
#include <hbindexfeedback.h>
|
|
21 |
#include <hbframebackground.h>
|
|
22 |
#include <hbframedrawer.h>
|
|
23 |
#include <xqsettingsmanager.h>
|
|
24 |
#include <xqsettingskey.h>
|
|
25 |
#include <cntlistmodel.h>
|
|
26 |
#include <cntlistmodel_p.h>
|
|
27 |
#include <cntcache.h>
|
|
28 |
#include <cntdisplaytextformatter.h>
|
|
29 |
#include <cntdebug.h>
|
|
30 |
|
|
31 |
/*!
|
|
32 |
\class CntListModel
|
|
33 |
\brief List model for list with contacts.
|
|
34 |
|
|
35 |
CntListModel is a list model view for contacts database content. It uses
|
|
36 |
CntCache to fetch and cache entries displayed on the screen.
|
|
37 |
*/
|
|
38 |
|
|
39 |
const uint dummyMyCardId = 0;
|
|
40 |
|
|
41 |
/*!
|
|
42 |
Construct a new CntListModel object using manager as the QContactManager
|
|
43 |
instance to communicate with the contacts database.
|
|
44 |
|
|
45 |
\param manager a QContactManager instance to be used for communications
|
|
46 |
with the contacts persistant store
|
|
47 |
\param filter a filter that selects the contacts to show in the list
|
|
48 |
\param showMyCard true if my card entry should be shown at the top of
|
|
49 |
the list, otherwise false
|
|
50 |
\param parent parent of this QObject
|
|
51 |
*/
|
|
52 |
CntListModel::CntListModel(QContactManager* manager,
|
|
53 |
const QContactFilter& filter,
|
|
54 |
bool showMyCard,
|
|
55 |
QObject *parent)
|
|
56 |
: QAbstractListModel(parent)
|
|
57 |
{
|
|
58 |
CNT_ENTRY
|
|
59 |
|
|
60 |
// set up data
|
|
61 |
d = new CntListModelData(manager, filter, showMyCard);
|
|
62 |
|
|
63 |
// fetch IDs
|
|
64 |
updateContactIdsArray();
|
|
65 |
|
|
66 |
// get current setting how to show an item in the name list and subscribe for changes
|
|
67 |
d->mSettings = new XQSettingsManager;
|
|
68 |
d->mNameListRowSettingkey = new XQSettingsKey(XQSettingsKey::TargetCentralRepository,
|
|
69 |
KCRCntSettings.iUid,
|
|
70 |
KCntNameListRowSetting);
|
|
71 |
d->mCurrentRowSetting = d->mSettings->readItemValue(*d->mNameListRowSettingkey,
|
|
72 |
XQSettingsManager::TypeInt).toInt();
|
|
73 |
d->mSettings->startMonitoring(*d->mNameListRowSettingkey, XQSettingsManager::TypeInt);
|
|
74 |
connect(d->mSettings, SIGNAL(valueChanged(const XQSettingsKey&, const QVariant&)),
|
|
75 |
this, SLOT(handleRowSettingChanged(const XQSettingsKey&, const QVariant&)));
|
|
76 |
|
|
77 |
// listen to cache for changes in contacts
|
|
78 |
connect(d->mCache, SIGNAL(contactInfoUpdated(QContactLocalId)),
|
|
79 |
this, SLOT(handleContactInfoUpdated(QContactLocalId)));
|
|
80 |
connect(d->mCache, SIGNAL(contactsAdded(const QList<QContactLocalId>&)),
|
|
81 |
this, SLOT(handleAdded(const QList<QContactLocalId>&)));
|
|
82 |
connect(d->mCache, SIGNAL(contactsChanged(const QList<QContactLocalId>&)),
|
|
83 |
this, SLOT(handleChanged(const QList<QContactLocalId>&)));
|
|
84 |
connect(d->mCache, SIGNAL(contactsRemoved(const QList<QContactLocalId>&)),
|
|
85 |
this, SLOT(handleRemoved(const QList<QContactLocalId>&)));
|
|
86 |
connect(d->mCache, SIGNAL(dataChanged()),
|
|
87 |
this, SLOT(refreshModel()));
|
|
88 |
|
|
89 |
// listen to contactmanager for changes in relationships or mycard
|
|
90 |
connect(d->mContactManager, SIGNAL(selfContactIdChanged(const QContactLocalId&, const QContactLocalId&)),
|
|
91 |
this, SLOT(handleMyCardChanged(const QContactLocalId&, const QContactLocalId&)));
|
|
92 |
connect(d->mContactManager, SIGNAL(relationshipsAdded(const QList<QContactLocalId>&)),
|
|
93 |
this, SLOT(handleAddedRelationship(const QList<QContactLocalId>&)));
|
|
94 |
connect(d->mContactManager, SIGNAL(relationshipsRemoved(const QList<QContactLocalId>&)),
|
|
95 |
this, SLOT(handleRemovedRelationship(const QList<QContactLocalId>&)));
|
|
96 |
|
|
97 |
CNT_EXIT
|
|
98 |
}
|
|
99 |
|
|
100 |
CntListModel::~CntListModel()
|
|
101 |
{
|
|
102 |
}
|
|
103 |
|
|
104 |
/*!
|
|
105 |
Return the data to be used by the view or delegates for a particular
|
|
106 |
item and role.
|
|
107 |
|
|
108 |
\param index The index of the item to return data about.
|
|
109 |
\param role The data should be relevant for this particular purpose.
|
|
110 |
\return QVariant The data for the specified index and role.
|
|
111 |
*/
|
|
112 |
QVariant CntListModel::data(const QModelIndex &index, int role) const
|
|
113 |
{
|
|
114 |
CNT_ENTRY
|
|
115 |
|
|
116 |
QVariant returnData;
|
|
117 |
int row = index.row();
|
|
118 |
|
|
119 |
// check that row is ok
|
|
120 |
if (!isValidRow(row)) {
|
|
121 |
// invalid row
|
|
122 |
return QVariant();
|
|
123 |
}
|
|
124 |
|
|
125 |
// update current contact if needed
|
|
126 |
if (row != d->mCurrentRow) {
|
|
127 |
delete d->mCurrentContact;
|
|
128 |
if (d->mContactIds[row] == dummyMyCardId) {
|
|
129 |
// row contains dummy MyCard, so create dummy CntContactInfo
|
|
130 |
d->mCurrentContact = NULL;
|
|
131 |
} else {
|
|
132 |
d->mCurrentContact = d->mCache->fetchContactInfo(row, d->mContactIds);
|
|
133 |
}
|
|
134 |
d->mCurrentRow = row;
|
|
135 |
}
|
|
136 |
|
|
137 |
if (role == Qt::DisplayRole) {
|
|
138 |
returnData = dataForRole(row, role);
|
|
139 |
} else if (role == Hb::IndexFeedbackRole) {
|
|
140 |
if (row > 0 || (d->mMyCardId != d->mContactIds[0] && dummyMyCardId != d->mContactIds[0])) {
|
|
141 |
returnData = dataForRole(row, role).toStringList().at(0).toUpper();
|
|
142 |
}
|
|
143 |
} else if (role == Qt::BackgroundRole) {
|
|
144 |
if (d->mMyCardId == d->mContactIds[row] || dummyMyCardId == d->mContactIds[row]) {
|
|
145 |
returnData = HbFrameBackground("qtg_fr_list_parent_normal", HbFrameDrawer::NinePieces);
|
|
146 |
}
|
|
147 |
} else if (role == Qt::DecorationRole) {
|
|
148 |
if (d->mCurrentRowSetting == CntTwoRowsNameAndPhoneNumber) {
|
|
149 |
//icon fits only if user selected 2 rows in each name list item
|
|
150 |
QList<QVariant> icons;
|
|
151 |
|
|
152 |
if (d->mCurrentContact != NULL && !d->mCurrentContact->icon1().isNull()) {
|
|
153 |
icons.append(d->mCurrentContact->icon1());
|
|
154 |
} else if (d->mMyCardId == d->mContactIds[row] || dummyMyCardId == d->mContactIds[row]) {
|
|
155 |
icons.append(d->mDefaultMyCardIcon);
|
|
156 |
} else {
|
|
157 |
icons.append(d->mDefaultIcon);
|
|
158 |
}
|
|
159 |
|
|
160 |
if (d->mCurrentContact != NULL && !d->mCurrentContact->icon2().isNull()) {
|
|
161 |
icons.append(d->mCurrentContact->icon2());
|
|
162 |
}
|
|
163 |
|
|
164 |
returnData = icons;
|
|
165 |
}
|
|
166 |
}
|
|
167 |
|
|
168 |
CNT_EXIT
|
|
169 |
return returnData;
|
|
170 |
}
|
|
171 |
|
|
172 |
/*!
|
|
173 |
Get the number of rows (contacts) in this model.
|
|
174 |
|
|
175 |
\param parent Optional parent index value.
|
|
176 |
\return Number of contacts in this model.
|
|
177 |
*/
|
|
178 |
int CntListModel::rowCount(const QModelIndex& /*parent*/) const
|
|
179 |
{
|
|
180 |
return d->mContactIds.count();
|
|
181 |
}
|
|
182 |
|
|
183 |
/*!
|
|
184 |
Read a full contact entry from the database for the given model
|
|
185 |
index value. Only the row part of the index information will be
|
|
186 |
read. This is just an overload of CntListModel::contact() that
|
|
187 |
supports old behaviour and calls:
|
|
188 |
CntListModel::contact(int row);
|
|
189 |
|
|
190 |
The entry at the requested row will have its full contact information
|
|
191 |
(all fields) read from the database and returned as a QContact instance.
|
|
192 |
|
|
193 |
\param index Index for the sought contact entry in this model.
|
|
194 |
\return A newly constructed QContact instance for this entry - ownership
|
|
195 |
is transferred to the caller.
|
|
196 |
*/
|
|
197 |
QContact CntListModel::contact(const QModelIndex &index) const
|
|
198 |
{
|
|
199 |
return contact(index.row());
|
|
200 |
}
|
|
201 |
|
|
202 |
/*!
|
|
203 |
Returns the id for the contact at the requested row.
|
|
204 |
|
|
205 |
\param index Index for the sought contact entry in this model.
|
|
206 |
\return The id for the contact, 0 if invalid index.
|
|
207 |
*/
|
|
208 |
QContactLocalId CntListModel::contactId(const QModelIndex &index) const
|
|
209 |
{
|
|
210 |
CNT_ENTRY
|
|
211 |
|
|
212 |
if (!isValidRow(index.row())) {
|
|
213 |
return 0;
|
|
214 |
}
|
|
215 |
|
|
216 |
CNT_EXIT
|
|
217 |
return d->mContactIds[index.row()];
|
|
218 |
}
|
|
219 |
|
|
220 |
/*!
|
|
221 |
Return an index that points to the row relating to the supplied contact.
|
|
222 |
E.g. if the contact is at row 7, the index with the following properties
|
|
223 |
is returned:
|
|
224 |
index.row() == 7
|
|
225 |
|
|
226 |
\param contact The contact for whose row an index is required
|
|
227 |
\return a QModelIndex with the row set to match that of the contact.
|
|
228 |
*/
|
|
229 |
QModelIndex CntListModel::indexOfContact(const QContact &contact) const
|
|
230 |
{
|
|
231 |
return createIndex(row(contact.localId()), 0);
|
|
232 |
}
|
|
233 |
|
|
234 |
/*!
|
|
235 |
Return an index that points to the row relating to the supplied contact id.
|
|
236 |
E.g. if the contact with this id is at row 7, the index with the following
|
|
237 |
properties is returned:
|
|
238 |
index.row() == 7
|
|
239 |
|
|
240 |
\param contactId The id of the contact for whose row an index is required
|
|
241 |
\return a QModelIndex with the row set to match that of the contact id.
|
|
242 |
*/
|
|
243 |
QModelIndex CntListModel::indexOfContactId(const QContactLocalId &contactId) const
|
|
244 |
{
|
|
245 |
return createIndex(row(contactId), 0);
|
|
246 |
}
|
|
247 |
|
|
248 |
/*!
|
|
249 |
Set new filter and sort order for the model.
|
|
250 |
|
|
251 |
\param contactFilter New contact filter.
|
|
252 |
*/
|
|
253 |
void CntListModel::setFilter(const QContactFilter& contactFilter)
|
|
254 |
{
|
|
255 |
CNT_ENTRY
|
|
256 |
|
|
257 |
d->setFilter(contactFilter);
|
|
258 |
|
|
259 |
//refresh model
|
|
260 |
updateContactIdsArray();
|
|
261 |
|
|
262 |
beginResetModel();
|
|
263 |
endResetModel();
|
|
264 |
|
|
265 |
CNT_EXIT
|
|
266 |
}
|
|
267 |
|
|
268 |
/*!
|
|
269 |
Enable/disable MyCard appearance in the model.
|
|
270 |
|
|
271 |
\param enabled Status of MyCard appearance in the model.
|
|
272 |
*/
|
|
273 |
void CntListModel::showMyCard(bool enabled)
|
|
274 |
{
|
|
275 |
CNT_ENTRY
|
|
276 |
|
|
277 |
if (d->mShowMyCard == enabled) {
|
|
278 |
return;
|
|
279 |
}
|
|
280 |
|
|
281 |
QContactLocalId myCardId = d->mMyCardId;
|
|
282 |
if (enabled) {
|
|
283 |
//add MyCard to the list
|
|
284 |
if (myCardId <= 0) {
|
|
285 |
// create a placeholder for MyCard
|
|
286 |
d->mContactIds.insert(0, dummyMyCardId);
|
|
287 |
} else {
|
|
288 |
d->mContactIds.insert(0, myCardId);
|
|
289 |
}
|
|
290 |
} else {
|
|
291 |
// remove MyCard from the list
|
|
292 |
if (myCardId <= 0) {
|
|
293 |
d->mContactIds.removeOne(dummyMyCardId);
|
|
294 |
} else {
|
|
295 |
d->mContactIds.removeOne(myCardId);
|
|
296 |
}
|
|
297 |
}
|
|
298 |
d->mShowMyCard = enabled;
|
|
299 |
d->mCurrentRow = -1;
|
|
300 |
|
|
301 |
beginResetModel();
|
|
302 |
reset();
|
|
303 |
endResetModel();
|
|
304 |
|
|
305 |
CNT_EXIT
|
|
306 |
}
|
|
307 |
|
|
308 |
/*!
|
|
309 |
\return true if MyCard is shown, false otherwise.
|
|
310 |
*/
|
|
311 |
bool CntListModel::isMyCardShown() const
|
|
312 |
{
|
|
313 |
return d->mShowMyCard;
|
|
314 |
}
|
|
315 |
|
|
316 |
/*!
|
|
317 |
\return the id of the MyCard contact.
|
|
318 |
*/
|
|
319 |
QContactLocalId CntListModel::myCardId() const
|
|
320 |
{
|
|
321 |
return d->mMyCardId;
|
|
322 |
}
|
|
323 |
|
|
324 |
/*!
|
|
325 |
Gets the filtered list of the contact Ids in a sorted order
|
|
326 |
|
|
327 |
\return Error status
|
|
328 |
*/
|
|
329 |
void CntListModel::updateContactIdsArray()
|
|
330 |
{
|
|
331 |
CNT_ENTRY
|
|
332 |
|
|
333 |
QContactDetailFilter* detailFilter = NULL;
|
|
334 |
|
|
335 |
if (d->mFilter.type() == QContactFilter::ContactDetailFilter) {
|
|
336 |
detailFilter = static_cast<QContactDetailFilter*>(&d->mFilter);
|
|
337 |
}
|
|
338 |
|
|
339 |
// special handling for all-contacts filter
|
|
340 |
if (detailFilter
|
|
341 |
&& detailFilter->detailDefinitionName() == QContactType::DefinitionName
|
|
342 |
&& detailFilter->detailFieldName() == QContactType::FieldType
|
|
343 |
&& detailFilter->value() == QContactType::TypeContact) {
|
|
344 |
d->mContactIds = d->mCache->sortIdsByName(NULL);
|
|
345 |
} else if (detailFilter
|
|
346 |
&& detailFilter->detailDefinitionName() == QContactDisplayLabel::DefinitionName
|
|
347 |
&& detailFilter->detailFieldName() == QContactDisplayLabel::FieldLabel
|
|
348 |
&& detailFilter->matchFlags() == Qt::MatchStartsWith) {
|
|
349 |
QStringList searchList = detailFilter->value().toStringList();
|
|
350 |
d->mContactIds = d->mCache->sortIdsByName(searchList);
|
|
351 |
} else {
|
|
352 |
QSet<QContactLocalId> filterIds = d->mContactManager->contactIds(d->mFilter).toSet();
|
|
353 |
d->mContactIds = d->mCache->sortIdsByName(&filterIds);
|
|
354 |
}
|
|
355 |
|
|
356 |
//find MyCard contact and move it to the first position
|
|
357 |
QContactLocalId myCardId = d->mMyCardId;
|
|
358 |
if (myCardId > 0) {
|
|
359 |
// MyCard exists
|
|
360 |
d->mContactIds.removeOne(myCardId);
|
|
361 |
if (d->mShowMyCard) {
|
|
362 |
d->mContactIds.insert(0, myCardId);
|
|
363 |
}
|
|
364 |
} else if (d->mShowMyCard) {
|
|
365 |
// create a placeholder for MyCard
|
|
366 |
d->mContactIds.insert(0, dummyMyCardId);
|
|
367 |
}
|
|
368 |
|
|
369 |
CNT_EXIT
|
|
370 |
}
|
|
371 |
|
|
372 |
/*!
|
|
373 |
Read a full contact entry from the database for the row number.
|
|
374 |
|
|
375 |
The entry at the requested row will have its full contact information
|
|
376 |
(all fields) read from the database and returned as a QContact instance.
|
|
377 |
|
|
378 |
\param row Row at which the sought contact entry is in this model.
|
|
379 |
\return A newly constructed QContact instance for this entry - ownership
|
|
380 |
is transferred to the caller.
|
|
381 |
*/
|
|
382 |
QContact CntListModel::contact(int row) const
|
|
383 |
{
|
|
384 |
CNT_ENTRY
|
|
385 |
|
|
386 |
if (!isValidRow(row) || d->mContactIds[row] == dummyMyCardId) {
|
|
387 |
return QContact();
|
|
388 |
}
|
|
389 |
|
|
390 |
CNT_EXIT
|
|
391 |
return d->mContactManager->contact(d->mContactIds[row]);
|
|
392 |
}
|
|
393 |
|
|
394 |
/*!
|
|
395 |
Verify specified row id is valid.
|
|
396 |
|
|
397 |
\param row A row number
|
|
398 |
\return bool indicating validity of row id
|
|
399 |
*/
|
|
400 |
bool CntListModel::isValidRow(int row) const
|
|
401 |
{
|
|
402 |
return (row >= 0 && row < rowCount());
|
|
403 |
}
|
|
404 |
|
|
405 |
/*!
|
|
406 |
Fetch the row containing the contact with the specified id.
|
|
407 |
|
|
408 |
\param contactId The id of the contact
|
|
409 |
\return the row of the contact or -1 if no item matched.
|
|
410 |
*/
|
|
411 |
int CntListModel::row(const QContactLocalId &contactId) const
|
|
412 |
{
|
|
413 |
return d->mContactIds.indexOf(contactId);
|
|
414 |
}
|
|
415 |
|
|
416 |
/*!
|
|
417 |
Return the data to be used by the view for a display role.
|
|
418 |
|
|
419 |
\param row The row of the item to return data about.
|
|
420 |
\param column The column of the item to return data about.
|
|
421 |
\return QVariant The data for the specified index.
|
|
422 |
*/
|
|
423 |
QVariant CntListModel::dataForRole(int row, int role) const
|
|
424 |
{
|
|
425 |
CNT_ENTRY
|
|
426 |
|
|
427 |
QStringList list;
|
|
428 |
QString name;
|
|
429 |
QString infoText;
|
|
430 |
bool isSelfContact = false;
|
|
431 |
bool isNonEmptySelfContact = false;
|
|
432 |
|
|
433 |
QContactLocalId id = d->mContactIds[row];
|
|
434 |
if (d->mMyCardId == id || dummyMyCardId == id) {
|
|
435 |
isSelfContact = true;
|
|
436 |
if (d->mCurrentContact == NULL) {
|
|
437 |
// empty card
|
|
438 |
name = hbTrId("txt_phob_dblist_mycard");
|
|
439 |
infoText = hbTrId("txt_phob_dblist_mycard_val_create_my_identity");
|
|
440 |
} else {
|
|
441 |
isNonEmptySelfContact = true;
|
|
442 |
}
|
|
443 |
}
|
|
444 |
|
|
445 |
if (!isSelfContact || isNonEmptySelfContact) {
|
|
446 |
name = d->mCurrentContact->name();
|
|
447 |
if (name.isEmpty()) {
|
|
448 |
name = hbTrId("txt_phob_list_unnamed");
|
|
449 |
}
|
|
450 |
infoText = d->mCurrentContact->text();
|
|
451 |
}
|
|
452 |
|
|
453 |
if (role == Qt::DisplayRole) {
|
|
454 |
list << d->mFormat->formattedText(name, d->mFilter);
|
|
455 |
} else {
|
|
456 |
list << name;
|
|
457 |
}
|
|
458 |
|
|
459 |
if (!isNonEmptySelfContact) {
|
|
460 |
if (d->mCurrentRowSetting == CntTwoRowsNameAndPhoneNumber) {
|
|
461 |
//add additional text only if user wants 2 rows in each name list item
|
|
462 |
list << infoText;
|
|
463 |
}
|
|
464 |
}
|
|
465 |
|
|
466 |
CNT_EXIT
|
|
467 |
return list;
|
|
468 |
}
|
|
469 |
|
|
470 |
/*!
|
|
471 |
Handle adding of contacts.
|
|
472 |
|
|
473 |
\param contactIds Ids of contacts added.
|
|
474 |
*/
|
|
475 |
void CntListModel::handleAdded(const QList<QContactLocalId>& contactIds)
|
|
476 |
{
|
|
477 |
CNT_ENTRY
|
|
478 |
|
|
479 |
// if contacts are added already, no need to do anything
|
|
480 |
bool newContacts = false;
|
|
481 |
for (int k = 0; k < contactIds.count() && !newContacts; k++) {
|
|
482 |
if (!d->mContactIds.contains(contactIds.at(k))) {
|
|
483 |
newContacts = true;
|
|
484 |
}
|
|
485 |
}
|
|
486 |
if (!newContacts) {
|
|
487 |
return;
|
|
488 |
}
|
|
489 |
|
|
490 |
// invalidate cached contact
|
|
491 |
d->mCurrentRow = -1;
|
|
492 |
|
|
493 |
QList<QContactLocalId> oldIdList = d->mContactIds;
|
|
494 |
updateContactIdsArray();
|
|
495 |
|
|
496 |
QList<int> newRows;
|
|
497 |
for (int i = 0; i < d->mContactIds.count(); i++) {
|
|
498 |
if (!oldIdList.contains(d->mContactIds.at(i))) {
|
|
499 |
newRows.append(i);
|
|
500 |
}
|
|
501 |
}
|
|
502 |
|
|
503 |
if (newRows.size() == 1) {
|
|
504 |
beginInsertRows(QModelIndex(), newRows.at(0), newRows.at(0));
|
|
505 |
endInsertRows();
|
|
506 |
} else {
|
|
507 |
beginResetModel();
|
|
508 |
reset();
|
|
509 |
endResetModel();
|
|
510 |
}
|
|
511 |
|
|
512 |
CNT_EXIT
|
|
513 |
}
|
|
514 |
|
|
515 |
/*!
|
|
516 |
Handle changes in contacts.
|
|
517 |
|
|
518 |
\param contactIds Ids of contacts changed.
|
|
519 |
*/
|
|
520 |
void CntListModel::handleChanged(const QList<QContactLocalId>& contactIds)
|
|
521 |
{
|
|
522 |
CNT_ENTRY
|
|
523 |
|
|
524 |
if (contactIds.count() == 0) {
|
|
525 |
return;
|
|
526 |
}
|
|
527 |
|
|
528 |
//invalidate cached contact
|
|
529 |
d->mCurrentRow = -1;
|
|
530 |
|
|
531 |
int firstChangedContactPosBefore = row(contactIds.at(0));
|
|
532 |
updateContactIdsArray();
|
|
533 |
int firstChangedContactPosAfter = row(contactIds.at(0));
|
|
534 |
|
|
535 |
// if only one contact was updated and its position didn't change,
|
|
536 |
// refresh the corresponding row
|
|
537 |
if (contactIds.count() == 1 &&
|
|
538 |
firstChangedContactPosBefore == firstChangedContactPosAfter &&
|
|
539 |
firstChangedContactPosBefore >= 0) {
|
|
540 |
QModelIndex top = index(firstChangedContactPosBefore);
|
|
541 |
QModelIndex bottom = index(firstChangedContactPosBefore);
|
|
542 |
emit dataChanged(top, bottom);
|
|
543 |
} else {
|
|
544 |
beginResetModel();
|
|
545 |
reset();
|
|
546 |
endResetModel();
|
|
547 |
}
|
|
548 |
|
|
549 |
CNT_EXIT
|
|
550 |
}
|
|
551 |
|
|
552 |
/*!
|
|
553 |
Handle removing of contacts.
|
|
554 |
|
|
555 |
\param contactIds Ids of contacts removed.
|
|
556 |
*/
|
|
557 |
void CntListModel::handleRemoved(const QList<QContactLocalId>& contactIds)
|
|
558 |
{
|
|
559 |
CNT_ENTRY
|
|
560 |
|
|
561 |
bool removeContacts = false;
|
|
562 |
QList<QContactLocalId> idList = d->mContactIds;
|
|
563 |
for (int k = 0; k < contactIds.count() && !removeContacts; k++) {
|
|
564 |
if(idList.contains(contactIds.at(k))) {
|
|
565 |
removeContacts = true;
|
|
566 |
}
|
|
567 |
}
|
|
568 |
if (!removeContacts) {
|
|
569 |
return;
|
|
570 |
}
|
|
571 |
|
|
572 |
//Find contacts to remove (=rows)
|
|
573 |
QList<int> removeRows;
|
|
574 |
for(int i = 0; i < contactIds.count(); i++) {
|
|
575 |
if (idList.contains(contactIds.at(i))) {
|
|
576 |
removeRows.append(row(contactIds.at(i)));
|
|
577 |
}
|
|
578 |
}
|
|
579 |
|
|
580 |
// invalidate cached contact
|
|
581 |
d->mCurrentRow = -1;
|
|
582 |
|
|
583 |
int myCardRow = -1;
|
|
584 |
if (contactIds.contains(d->mMyCardId)) {
|
|
585 |
myCardRow = row(d->mMyCardId);
|
|
586 |
d->mMyCardId = 0;
|
|
587 |
}
|
|
588 |
|
|
589 |
// remove rows starting from the bottom
|
|
590 |
qSort(removeRows.begin(), removeRows.end(), qGreater<int>());
|
|
591 |
foreach (int row, removeRows) {
|
|
592 |
if (row != myCardRow || !d->mShowMyCard) {
|
|
593 |
beginRemoveRows(QModelIndex(), row, row);
|
|
594 |
endRemoveRows();
|
|
595 |
}
|
|
596 |
}
|
|
597 |
|
|
598 |
foreach (QContactLocalId id, contactIds) {
|
|
599 |
d->mContactIds.removeOne(id);
|
|
600 |
}
|
|
601 |
|
|
602 |
if (myCardRow != -1 && d->mShowMyCard) {
|
|
603 |
d->mContactIds.insert(0, dummyMyCardId);
|
|
604 |
QModelIndex index = createIndex(0, 0);
|
|
605 |
emit dataChanged(index, index);
|
|
606 |
}
|
|
607 |
|
|
608 |
CNT_EXIT
|
|
609 |
}
|
|
610 |
|
|
611 |
/*!
|
|
612 |
Handle my card change.
|
|
613 |
|
|
614 |
\param oldId Id of the old MyCard.
|
|
615 |
\param newId Id of the new MyCard.
|
|
616 |
*/
|
|
617 |
void CntListModel::handleMyCardChanged(const QContactLocalId& /*oldId*/, const QContactLocalId& newId)
|
|
618 |
{
|
|
619 |
CNT_ENTRY
|
|
620 |
|
|
621 |
//invalidate cached contact
|
|
622 |
d->mCurrentRow = -1;
|
|
623 |
d->mMyCardId = newId;
|
|
624 |
|
|
625 |
updateContactIdsArray();
|
|
626 |
|
|
627 |
beginResetModel();
|
|
628 |
reset();
|
|
629 |
endResetModel();
|
|
630 |
|
|
631 |
CNT_EXIT
|
|
632 |
}
|
|
633 |
|
|
634 |
/*!
|
|
635 |
Handle added relationships.
|
|
636 |
|
|
637 |
\param contactIds Ids of contacts added (group id and contact ids).
|
|
638 |
*/
|
|
639 |
void CntListModel::handleAddedRelationship(const QList<QContactLocalId>& contactIds)
|
|
640 |
{
|
|
641 |
CNT_ENTRY
|
|
642 |
|
|
643 |
if (contactIds.contains(d->mGroupId)) {
|
|
644 |
foreach (QContactLocalId id, contactIds) {
|
|
645 |
if (id != d->mGroupId && !d->mContactIds.contains(id)) {
|
|
646 |
// at least one new contact id has been added to this group,
|
|
647 |
// so update the model
|
|
648 |
updateRelationships();
|
|
649 |
break;
|
|
650 |
}
|
|
651 |
}
|
|
652 |
}
|
|
653 |
|
|
654 |
CNT_EXIT
|
|
655 |
}
|
|
656 |
|
|
657 |
/*!
|
|
658 |
Handle removed relationships.
|
|
659 |
|
|
660 |
\param contactIds Ids of contacts removed from a relationship (group id and contact ids).
|
|
661 |
*/
|
|
662 |
void CntListModel::handleRemovedRelationship(const QList<QContactLocalId>& contactIds)
|
|
663 |
{
|
|
664 |
CNT_ENTRY
|
|
665 |
|
|
666 |
if (contactIds.contains(d->mGroupId)) {
|
|
667 |
foreach (QContactLocalId id, contactIds) {
|
|
668 |
if (d->mContactIds.contains(id)) {
|
|
669 |
// at least one new contact id has been removed from this group,
|
|
670 |
// so update the model
|
|
671 |
updateRelationships();
|
|
672 |
break;
|
|
673 |
}
|
|
674 |
}
|
|
675 |
}
|
|
676 |
|
|
677 |
CNT_EXIT
|
|
678 |
}
|
|
679 |
|
|
680 |
/*!
|
|
681 |
Updates the model to reflect changes in the relationships.
|
|
682 |
*/
|
|
683 |
void CntListModel::updateRelationships()
|
|
684 |
{
|
|
685 |
CNT_ENTRY
|
|
686 |
|
|
687 |
//invalidate cached contact
|
|
688 |
d->mCurrentRow = -1;
|
|
689 |
|
|
690 |
QList<QContactLocalId> oldIdList = d->mContactIds;
|
|
691 |
updateContactIdsArray();
|
|
692 |
|
|
693 |
// find all changed rows
|
|
694 |
QList<int> newRows, removedRows;
|
|
695 |
for (int i = 0; i < d->mContactIds.count(); i++) {
|
|
696 |
if (!oldIdList.contains(d->mContactIds.at(i))) {
|
|
697 |
newRows.append(i);
|
|
698 |
}
|
|
699 |
}
|
|
700 |
for (int i = 0; i < oldIdList.count(); i++) {
|
|
701 |
if (!d->mContactIds.contains(oldIdList.at(i))) {
|
|
702 |
removedRows.append(i);
|
|
703 |
}
|
|
704 |
}
|
|
705 |
|
|
706 |
// currently only one-row-changes are handled with beginInsertRows/beginRemoveRows
|
|
707 |
// if there are more than one change, the whole model is reset
|
|
708 |
if (removedRows.count() == 1 && newRows.count() == 0) {
|
|
709 |
beginRemoveRows(QModelIndex(), removedRows.at(0), removedRows.at(0));
|
|
710 |
endRemoveRows();
|
|
711 |
} else if (newRows.count() == 1 && removedRows.count() == 0) {
|
|
712 |
beginInsertRows(QModelIndex(), newRows.at(0), newRows.at(0));
|
|
713 |
endInsertRows();
|
|
714 |
} else {
|
|
715 |
beginResetModel();
|
|
716 |
endResetModel();
|
|
717 |
}
|
|
718 |
|
|
719 |
CNT_EXIT
|
|
720 |
}
|
|
721 |
|
|
722 |
/*!
|
|
723 |
Notify views that info for a contact has become
|
|
724 |
available or has changed.
|
|
725 |
|
|
726 |
\param contactId the id of the contact
|
|
727 |
*/
|
|
728 |
void CntListModel::handleContactInfoUpdated(QContactLocalId contactId)
|
|
729 |
{
|
|
730 |
CNT_ENTRY
|
|
731 |
|
|
732 |
QModelIndex index = createIndex(row(contactId), 0);
|
|
733 |
if (index.row() == d->mCurrentRow) {
|
|
734 |
d->mCurrentRow = -1;
|
|
735 |
}
|
|
736 |
|
|
737 |
if (isValidRow(index.row())) {
|
|
738 |
emit dataChanged(index, index);
|
|
739 |
}
|
|
740 |
|
|
741 |
CNT_EXIT
|
|
742 |
}
|
|
743 |
|
|
744 |
/*!
|
|
745 |
Handle a change in how name list item should be represented.
|
|
746 |
|
|
747 |
\param key Central repository key
|
|
748 |
\param value New value in the key
|
|
749 |
*/
|
|
750 |
void CntListModel::handleRowSettingChanged(const XQSettingsKey& /*key*/, const QVariant& value)
|
|
751 |
{
|
|
752 |
bool ok = false;
|
|
753 |
int newSetting = value.toInt(&ok);
|
|
754 |
if (ok) {
|
|
755 |
d->mCurrentRowSetting = newSetting;
|
|
756 |
|
|
757 |
if (rowCount() > 0) {
|
|
758 |
QModelIndex first = createIndex(0, 0);
|
|
759 |
QModelIndex last = createIndex(rowCount() - 1, 0);
|
|
760 |
emit dataChanged(first, last);
|
|
761 |
}
|
|
762 |
}
|
|
763 |
}
|
|
764 |
|
|
765 |
/*!
|
|
766 |
Handle a change in data.
|
|
767 |
*/
|
|
768 |
void CntListModel::refreshModel()
|
|
769 |
{
|
|
770 |
d->mCurrentRow = -1;
|
|
771 |
|
|
772 |
updateContactIdsArray();
|
|
773 |
|
|
774 |
beginResetModel();
|
|
775 |
reset();
|
|
776 |
endResetModel();
|
|
777 |
}
|