Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[contacts] Use cache display label where possible. JB#50658
In the past, display names were not typically set in QContactDisplayLabel
so SeasidePerson had to calculate a display label. This is no longer
the case, so the QContactDisplayLabel value should be preferred.

In cases where the cache item does not exist (e.g. a temporary
contact not saved to db), SeasidePerson still needs to calculate the
display label.
  • Loading branch information
Bea Lam committed Sep 24, 2020
1 parent a49e8e1 commit 6c222eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/seasidecache.cpp
Expand Up @@ -1219,10 +1219,12 @@ void SeasideCache::decomposeDisplayLabel(const QString &formattedDisplayLabel, Q
// small helper to avoid inconvenience
QString SeasideCache::generateDisplayLabel(const QContact &contact, DisplayLabelOrder order, bool fallbackToNonNameDetails)
{
QContactName name = contact.detail<QContactName>();

QString displayLabel;
QString displayLabel = contact.detail<QContactDisplayLabel>().label();
if (!displayLabel.isEmpty()) {
return displayLabel;
}

QContactName name = contact.detail<QContactName>();
QString nameStr1(name.firstName());
QString nameStr2(name.lastName());

Expand Down
19 changes: 14 additions & 5 deletions src/seasideperson.cpp
Expand Up @@ -128,24 +128,24 @@ SeasidePerson::SeasidePerson(const QContact &contact, QObject *parent)
: QObject(parent)
, mContact(new QContact(contact))
, mAddressBook(SeasideAddressBook::fromCollectionId(contact.collectionId()))
, mDisplayLabel(generateDisplayLabel(contact))
, mComplete(true)
, mResolving(false)
, mAttachState(Unattached)
, mItem(0)
{
recalculateDisplayLabel();
}

SeasidePerson::SeasidePerson(QContact *contact, bool complete, QObject *parent)
: QObject(parent)
, mContact(contact)
, mAddressBook(SeasideAddressBook::fromCollectionId(contact->collectionId()))
, mDisplayLabel(generateDisplayLabel(*contact))
, mComplete(complete)
, mResolving(false)
, mAttachState(Attached)
, mItem(0)
{
recalculateDisplayLabel();
}

SeasidePerson::~SeasidePerson()
Expand Down Expand Up @@ -280,7 +280,13 @@ QString SeasidePerson::placeholderDisplayLabel()
void SeasidePerson::recalculateDisplayLabel(SeasideCache::DisplayLabelOrder order) const
{
QString oldDisplayLabel = mDisplayLabel;
QString newDisplayLabel = generateDisplayLabel(*mContact, order);
QString newDisplayLabel;
SeasideCache::CacheItem *cacheItem = SeasideCache::existingItem(mContact->id());
if (cacheItem) {
newDisplayLabel = cacheItem->displayLabel.isEmpty();
} else {
newDisplayLabel = generateDisplayLabel(*mContact, order);
}

if (oldDisplayLabel != newDisplayLabel) {
mDisplayLabel = newDisplayLabel;
Expand All @@ -294,6 +300,10 @@ void SeasidePerson::recalculateDisplayLabel(SeasideCache::DisplayLabelOrder orde

QString SeasidePerson::displayLabel() const
{
SeasideCache::CacheItem *cacheItem = SeasideCache::existingItem(mContact->id());
if (cacheItem && !cacheItem->displayLabel.isEmpty()) {
return cacheItem->displayLabel;
}
if (mDisplayLabel.isEmpty()) {
return SeasidePerson::placeholderDisplayLabel();
}
Expand All @@ -308,7 +318,7 @@ QString SeasidePerson::primaryName() const

if (secondaryName().isEmpty()) {
// No real name details - fall back to the display label for primary name
return mDisplayLabel;
return displayLabel();
}

return QString();
Expand Down Expand Up @@ -2407,7 +2417,6 @@ void SeasidePerson::addressResolved(const QString &, const QString &, SeasideCac
void SeasidePerson::itemUpdated(SeasideCache::CacheItem *)
{
// We don't know what has changed - report everything changed
recalculateDisplayLabel();
emitChangeSignals();
}

Expand Down

0 comments on commit 6c222eb

Please sign in to comment.