Skip to content

Commit

Permalink
Merge branch 'jb51884-address-book-update' into 'master'
Browse files Browse the repository at this point in the history
[contacts] Update SimpleContactModel values correctly when address book changes. JB#51884

See merge request mer-core/nemo-qml-plugin-contacts!50
  • Loading branch information
blam committed Nov 10, 2020
2 parents bbe8483 + af03e92 commit f9cdf52
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
24 changes: 13 additions & 11 deletions src/seasidesimplecontactmodel.cpp
Expand Up @@ -53,6 +53,7 @@ SeasideSimpleContactModel::ContactInfo::ContactInfo(SeasideCache::CacheItem *cac
{
if (cacheItem) {
addressBook = SeasideAddressBook::fromCollectionId(cacheItem->contact.collectionId());
displayLabel = cacheItem->displayLabel;
} else {
qWarning() << "Invalid ContactInfo cache item!";
}
Expand Down Expand Up @@ -103,11 +104,11 @@ QVariant SeasideSimpleContactModel::data(const QModelIndex &index, int role) con
case IdRole:
return contactInfo.cacheItem->iid;
case PrimaryNameRole:
return getPrimaryName(contactInfo.cacheItem);
return getPrimaryName(contactInfo.cacheItem, contactInfo.displayLabel);
case SecondaryNameRole:
return SeasideCache::getSecondaryName(contactInfo.cacheItem->contact);
case DisplayLabelRole:
return contactInfo.cacheItem->displayLabel;
return contactInfo.displayLabel;
case AddressBookRole:
return QVariant::fromValue(contactInfo.addressBook);
}
Expand Down Expand Up @@ -210,21 +211,22 @@ void SeasideSimpleContactModel::setContactIds(const QList<int> &contactIds)
void SeasideSimpleContactModel::itemUpdated(SeasideCache::CacheItem *item)
{
for (int i = 0; i < m_contacts.count(); ++i) {
if (m_contacts[i].cacheItem == item) {
if (m_contacts[i].cacheItem->iid == item->iid) {
QVector<int> roles;
if (m_contacts[i].cacheItem->iid != item->iid) {
roles << IdRole;
}
if (getPrimaryName(m_contacts[i].cacheItem) != getPrimaryName(item)) {
if (getPrimaryName(m_contacts[i].cacheItem, m_contacts[i].displayLabel)
!= getPrimaryName(item, item->displayLabel)) {
roles << PrimaryNameRole;
}
if (SeasideCache::getSecondaryName(m_contacts[i].cacheItem->contact) != SeasideCache::getSecondaryName(item->contact)) {
roles << SecondaryNameRole;
}
if (m_contacts[i].cacheItem->displayLabel != item->displayLabel) {
if (m_contacts[i].displayLabel != item->displayLabel) {
m_contacts[i].displayLabel = item->displayLabel;
roles << DisplayLabelRole;
}
if (m_contacts[i].addressBook != SeasideAddressBook::fromCollectionId(item->contact.collectionId())) {
const SeasideAddressBook addressBook = SeasideAddressBook::fromCollectionId(item->contact.collectionId());
if (m_contacts[i].addressBook != addressBook) {
m_contacts[i].addressBook = addressBook;
roles << AddressBookRole;
}
emit dataChanged(createIndex(i, 0), createIndex(i, 0), roles);
Expand All @@ -245,8 +247,8 @@ void SeasideSimpleContactModel::itemAboutToBeRemoved(SeasideCache::CacheItem *it
}
}

QString SeasideSimpleContactModel::getPrimaryName(SeasideCache::CacheItem *item)
QString SeasideSimpleContactModel::getPrimaryName(SeasideCache::CacheItem *item, const QString &displayLabel)
{
const QString primaryName = SeasideCache::getPrimaryName(item->contact);
return !primaryName.isEmpty() ? primaryName : item->displayLabel;
return !primaryName.isEmpty() ? primaryName : displayLabel;
}
3 changes: 2 additions & 1 deletion src/seasidesimplecontactmodel.h
Expand Up @@ -87,6 +87,7 @@ class SeasideSimpleContactModel : public QAbstractListModel,

SeasideAddressBook addressBook;
SeasideCache::CacheItem *cacheItem = nullptr;
QString displayLabel;
};

virtual void reset() = 0;
Expand All @@ -95,7 +96,7 @@ class SeasideSimpleContactModel : public QAbstractListModel,
void setContactIds(const QList<int> &contactIds);
void updateOrReset(const QList<int> &newContactIds);

static QString getPrimaryName(SeasideCache::CacheItem *item);
static QString getPrimaryName(SeasideCache::CacheItem *item, const QString &displayLabel);

QList<ContactInfo> m_contacts;
bool m_complete = false;
Expand Down

0 comments on commit f9cdf52

Please sign in to comment.